-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
160 lines (148 loc) · 4.42 KB
/
matrix.cpp
File metadata and controls
160 lines (148 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "matrix.h"
Matrix::Matrix() {
this->m_wifi_client = NULL;
this->m_http_client = NULL;
this->m_accesstoken = "";
this->m_scheme = "";
this->m_domain = "";
setCallback(NULL);
this->m_lastMessageToken = "";
}
Matrix::Matrix(WiFiClientSecure& _wificlient, HTTPClient& _httpclient) {
this->m_http_client = &_httpclient;
this->m_wifi_client = &_wificlient;
this->m_accesstoken = "";
this->m_scheme = "";
this->m_domain = "";
setCallback(NULL);
this->m_lastMessageToken = "";
}
Matrix::~Matrix() {
}
Matrix& Matrix::setCallback(MATRIX_CALLBACK_SIGNATURE) {
this->callback = callback;
return *this;
}
void Matrix::setDomain(String _domain) {
this->m_domain = _domain;
return;
}
void Matrix::setScheme(String _scheme){
this->m_scheme = _scheme;
return;
}
bool Matrix::login(const char* _username, const char* _password){
bool res = false;
String msg;
DynamicJsonDocument root(256);
root["type"] = "m.login.password";
root["user"] = _username;
root["password"] = _password;
serializeJson(root, msg);
String url = m_scheme + String("://") + m_domain + String("/_matrix/client/r0/login");
m_http_client->begin(*m_wifi_client, url);
//IN MORE POMPLEX SCENARIOS (LDAP USER LOOKUP ETC.) ALLOW FOR LARGER TIMEOUT VALUES
m_http_client->setTimeout(10000);
m_http_client->addHeader("Content-Type", "application/json");
int rc = m_http_client->POST(msg);
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
String body = m_http_client->getString();
m_http_client->end();
DynamicJsonDocument resp(256);
deserializeJson(resp, body);
this->m_accesstoken = String(resp["access_token"]);
res = true;
}
else{
Serial.print("ERROR: HTTP CODE: ");
Serial.println(rc);
}
}
else{
Serial.print("ERROR: HTTP CLIENT RC: ");
Serial.println(rc);
}
return res;
}
void Matrix::setAccessToken(String _accesstoken){
this->m_accesstoken = _accesstoken;
}
bool Matrix::retrieve(const char* _roomid){
bool res = true;
String start = "something";
String end = "somethingelse";
while(start != end){
String url = m_scheme + String("://") + m_domain + String("/_matrix/client/r0/rooms/") + String(_roomid) + "/messages?access_token=" + m_accesstoken + "&limit=1";
if (m_lastMessageToken == "") {
url += "&dir=b";
}
else {
url += "&dir=f&from=" + String(m_lastMessageToken);
}
m_http_client->begin(*m_wifi_client, url);
int rc = m_http_client->GET();
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
String cont = m_http_client->getString();
DynamicJsonDocument doc(2048);
deserializeJson(doc, cont);
start = String(doc["start"]);
end = String(doc["end"]);
JsonObject chunk_0 = doc["chunk"][0];
String ID = chunk_0["event_id"];
String sender = chunk_0["user_id"];
if(end != start && ID != m_latestMessageID){
callback((char*)cont.c_str(), (char*)_roomid);
}
m_latestMessageID = ID;
m_lastMessageToken = String(doc["end"]);
m_lastMessageStartToken = String(doc["start"]);
}
else {
Serial.print("ERROR: HTTP CODE: ");
Serial.println(rc);
res = false;
m_http_client->end();
return res;
}
}
else{
Serial.print("ERROR: HTTP CLIENT RC: ");
Serial.println(rc);
}
m_http_client->end();
}
return res;
}
bool Matrix::sendPlaintext(const char* _msg, const char* _roomid){
String msgcontent;
DynamicJsonDocument root(1024); //THIS MIGHT BE DIRTY, DEPENDING ON THE LENGTH OF _msg
root["msgtype"] = "m.text";
root["body"] = _msg;
serializeJson(root, msgcontent);
bool res = this->sendJSON(msgcontent.c_str(), _roomid);
return res;
}
bool Matrix::sendJSON(const char* _JSON, const char* _roomid){
bool res = false;
String url = m_scheme + String("://") + m_domain + String("/_matrix/client/r0/rooms/") + String(_roomid) + String("/send/m.room.message/") + String(millis()) + String("?access_token=") + m_accesstoken;
m_http_client->begin(*m_wifi_client, url);
m_http_client->addHeader("Content-Type", "application/json");
int rc = m_http_client->PUT(_JSON);
if (rc > 0) {
if (rc == HTTP_CODE_OK) {
res = true;
}
else{
Serial.print("ERROR: HTTP CODE: ");
Serial.println(rc);
}
}
else{
Serial.print("ERROR: HTTP CLIENT RC: ");
Serial.println(rc);
}
m_http_client->end();
return res;
}