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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/*
Copyright © 2016-17 Miranda NG team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
/////////////////////////////////////////////////////////////////////////////////////////
// removes a friend from the server
void CDiscordProto::RemoveFriend(SnowFlake id)
{
Push(new AsyncHttpRequest(this, REQUEST_DELETE, CMStringA(FORMAT, "/users/@me/relationships/%lld", id), NULL));
}
/////////////////////////////////////////////////////////////////////////////////////////
// retrieves server history
void CDiscordProto::RetrieveHistory(MCONTACT hContact, CDiscordHistoryOp iOp, SnowFlake msgid, int iLimit)
{
CDiscordUser *pUser = FindUser(getId(hContact, DB_KEY_ID));
if (pUser == NULL)
return;
CMStringA szUrl(FORMAT, "/channels/%lld/messages", pUser->channelId);
AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveHistory);
pReq << INT_PARAM("limit", iLimit);
switch (iOp) {
case MSG_AFTER:
pReq << INT64_PARAM("after", msgid); break;
case MSG_BEFORE:
pReq << INT64_PARAM("before", msgid); break;
}
pReq->pUserInfo = pUser;
Push(pReq);
}
static int compareMsgHistory(const JSONNode *p1, const JSONNode *p2)
{
return wcscmp((*p1)["id"].as_mstring(), (*p2)["id"].as_mstring());
}
void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
{
CDiscordUser *pUser = (CDiscordUser*)pReq->pUserInfo;
if (pReply->resultCode != 200)
return;
JSONNode root = JSONNode::parse(pReply->pData);
if (!root)
return;
DBEVENTINFO dbei = {};
dbei.szModule = m_szModuleName;
dbei.flags = DBEF_UTF;
dbei.eventType = EVENTTYPE_MESSAGE;
SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database
LIST<JSONNode> arNodes(10, compareMsgHistory);
int iNumMessages = 0;
for (auto it = root.begin(); it != root.end(); ++it, ++iNumMessages) {
JSONNode &p = *it;
arNodes.insert(&p);
}
for (int i = 0; i < arNodes.getCount(); i++) {
JSONNode &p = *arNodes[i];
SnowFlake authorid = ::getId(p["author"]["id"]);
if (authorid == m_ownId)
dbei.flags |= DBEF_SENT;
else
dbei.flags &= ~DBEF_SENT;
SnowFlake msgid = ::getId(p["id"]);
if (msgid <= pUser->lastReadId)
dbei.flags |= DBEF_READ;
else
dbei.flags &= ~DBEF_READ;
CMStringW wszText = PrepareMessageText(p);
CMStringA szBody(ptrA(mir_utf8encodeW(wszText)));
szBody.AppendFormat("%c%lld", 0, msgid);
dbei.timestamp = StringToDate(p["timestamp"].as_mstring());
dbei.pBlob = (PBYTE)szBody.GetBuffer();
dbei.cbBlob = szBody.GetLength();
db_event_add(pUser->hContact, &dbei);
if (lastId < msgid)
lastId = msgid;
}
setId(pUser->hContact, DB_KEY_LASTMSGID, lastId);
// if we fetched 99 messages, but have smth more to go, continue fetching
if (iNumMessages == 99 && lastId < pUser->lastMessageId)
RetrieveHistory(pUser->hContact, MSG_AFTER, lastId, 99);
}
/////////////////////////////////////////////////////////////////////////////////////////
// retrieves user info
void CDiscordProto::RetrieveUserInfo(MCONTACT hContact)
{
CMStringA szUrl;
if (hContact != 0) {
SnowFlake id = getId(hContact, DB_KEY_ID);
if (id == 0)
return;
szUrl.Format("/users/%lld", id);
}
else szUrl = "/users/@me";
AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveUserInfo);
pReq->pUserInfo = (void*)hContact;
Push(pReq);
}
void CDiscordProto::OnReceiveUserInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
{
MCONTACT hContact = (MCONTACT)pReq->pUserInfo;
if (pReply->resultCode != 200) {
if (hContact == NULL)
ConnectionFailed(LOGINERR_WRONGPASSWORD);
return;
}
JSONNode root = JSONNode::parse(pReply->pData);
if (!root) {
if (hContact == NULL)
ConnectionFailed(LOGINERR_NOSERVER);
return;
}
SnowFlake id = ::getId(root["id"]);
setId(hContact, DB_KEY_ID, id);
setByte(hContact, DB_KEY_MFA, root["mfa_enabled"].as_bool());
setDword(hContact, DB_KEY_DISCR, _wtoi(root["discriminator"].as_mstring()));
setWString(hContact, DB_KEY_NICK, root["username"].as_mstring());
setWString(hContact, DB_KEY_EMAIL, root["email"].as_mstring());
if (hContact == NULL) {
m_ownId = id;
OnLoggedIn();
}
else {
CDiscordUser *pUser = FindUser(id);
ProcessType(pUser, root);
}
CheckAvatarChange(hContact, root["avatar"].as_mstring());
}
/////////////////////////////////////////////////////////////////////////////////////////
// finds a gateway address
void CDiscordProto::OnReceiveGateway(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200) {
ShutdownSession();
return;
}
JSONNode root = JSONNode::parse(pReply->pData);
if (!root) {
ShutdownSession();
return;
}
m_szGateway = root["url"].as_mstring();
ForkThread(&CDiscordProto::GatewayThread, NULL);
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::SetServerStatus(int iStatus)
{
if (!m_bOnline)
return;
if (iStatus == ID_STATUS_OFFLINE)
Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/logout", NULL));
else {
const char *pszStatus;
switch (iStatus) {
case ID_STATUS_AWAY:
case ID_STATUS_NA:
pszStatus = "idle"; break;
case ID_STATUS_DND:
pszStatus = "dnd"; break;
case ID_STATUS_INVISIBLE:
pszStatus = "invisible"; break;
default:
pszStatus = "online"; break;
}
JSONNode root; root << CHAR_PARAM("status", pszStatus);
Push(new AsyncHttpRequest(this, REQUEST_PATCH, "/users/@me/settings", NULL, &root));
}
int iOldStatus = m_iStatus; m_iStatus = iStatus;
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)iOldStatus, m_iStatus);
}
void CDiscordProto::OnReceiveAuth(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
{
MCONTACT hContact = (MCONTACT)pReq->pUserInfo;
if (pReply->resultCode == 204)
RetrieveUserInfo(hContact);
}
/////////////////////////////////////////////////////////////////////////////////////////
// channels
void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200)
return;
JSONNode root = JSONNode::parse(pReply->pData);
if (root)
OnCommandChannelCreated(root);
}
void CDiscordProto::OnReceiveChannels(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200)
return;
JSONNode root = JSONNode::parse(pReply->pData);
if (!root)
return;
for (auto it = root.begin(); it != root.end(); ++it) {
const JSONNode &p = *it;
const JSONNode &user = p["recipient"];
if (!user)
continue;
CDiscordUser *pUser = PrepareUser(user);
pUser->lastMessageId = ::getId(p["last_message_id"]);
pUser->channelId = ::getId(p["id"]);
pUser->bIsPrivate = p["is_private"].as_bool();
setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::OnReceiveFriends(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200)
return;
JSONNode root = JSONNode::parse(pReply->pData);
if (!root)
return;
for (auto it = root.begin(); it != root.end(); ++it) {
JSONNode &p = *it;
JSONNode &user = p["user"];
if (user)
PrepareUser(user);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::OnReceiveGuilds(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200)
return;
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::OnReceiveMessage(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
{
MCONTACT hContact = (MCONTACT)pReq->pUserInfo;
bool bSucceeded = true;
if (pReply->resultCode != 200 && pReply->resultCode != 204)
bSucceeded = false;
JSONNode root = JSONNode::parse(pReply->pData);
if (root) {
SnowFlake newLastId = ::getId(root["id"]);
SnowFlake oldLastId = getId(hContact, DB_KEY_LASTMSGID); // as stored in a database
if (oldLastId < newLastId)
setId(hContact, DB_KEY_LASTMSGID, newLastId);
}
ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, bSucceeded ? ACKRESULT_SUCCESS : ACKRESULT_FAILED, (HANDLE)pReq->m_iReqNum, 0);
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::OnReceiveMessageAck(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200)
return;
JSONNode root = JSONNode::parse(pReply->pData);
if (!root)
return;
CMStringW wszToken(root["token"].as_mstring());
if (!wszToken.IsEmpty()) {
JSONNode props; props.set_name("properties");
JSONNode reply; reply << props;
reply << CHAR_PARAM("event", "ack_messages") << WCHAR_PARAM("token", root["token"].as_mstring());
Push(new AsyncHttpRequest(this, REQUEST_POST, "/track", NULL, &reply));
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void CDiscordProto::OnReceiveToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
if (pReply->resultCode != 200) {
ConnectionFailed(LOGINERR_WRONGPASSWORD);
return;
}
JSONNode root = JSONNode::parse(pReply->pData);
if (!root) {
LBL_Error:
ConnectionFailed(LOGINERR_NOSERVER);
return;
}
CMStringA szToken = root["token"].as_mstring();
if (szToken.IsEmpty())
goto LBL_Error;
m_szAccessToken = szToken.Detach();
setString("AccessToken", m_szAccessToken);
RetrieveUserInfo(NULL);
}
|