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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#pragma once
typedef __int64 SnowFlake;
__forceinline int compareInt64(const SnowFlake i1, const SnowFlake i2)
{
return (i1 == i2) ? 0 : (i1 < i2) ? -1 : 1;
}
class CDiscordProto;
typedef void (CDiscordProto::*HttpCallback)(NETLIBHTTPREQUEST*, struct AsyncHttpRequest*);
typedef void (CDiscordProto::*GatewayHandlerFunc)(const JSONNode&);
struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject
{
AsyncHttpRequest();
AsyncHttpRequest(CDiscordProto*, int iRequestType, LPCSTR szUrl, HttpCallback pFunc, JSONNode *pNode = nullptr);
~AsyncHttpRequest();
void AddHeader(LPCSTR, LPCSTR);
CMStringA m_szUrl;
CMStringA m_szParam;
HttpCallback m_pCallback;
int m_iErrorCode, m_iReqNum;
bool m_bMainSite;
void *pUserInfo;
};
AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT_PARAM&);
AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT64_PARAM&);
AsyncHttpRequest* operator<<(AsyncHttpRequest*, const CHAR_PARAM&);
AsyncHttpRequest* operator<<(AsyncHttpRequest*, const WCHAR_PARAM&);
/////////////////////////////////////////////////////////////////////////////////////////
struct CDiscordRole : public MZeroedObject
{
SnowFlake id;
COLORREF color;
DWORD permissions;
int position;
CMStringW wszName;
};
/////////////////////////////////////////////////////////////////////////////////////////
struct COwnMessage
{
SnowFlake nonce;
int reqId;
COwnMessage(SnowFlake _id, int _reqId) :
nonce(_id),
reqId(_reqId)
{}
};
/////////////////////////////////////////////////////////////////////////////////////////
enum CDiscordHistoryOp
{
MSG_NOFILTER, MSG_AFTER, MSG_BEFORE
};
struct CDiscordUser : public MZeroedObject
{
CDiscordUser(SnowFlake _id) :
id(_id)
{}
~CDiscordUser();
SnowFlake id;
MCONTACT hContact;
SnowFlake channelId;
SnowFlake lastReadId, lastMsgId;
SnowFlake parentId;
bool bIsPrivate;
bool bIsGroup;
struct CDiscordGuild *pGuild;
CMStringW wszUsername, wszChannelName, wszTopic;
int iDiscriminator;
};
/////////////////////////////////////////////////////////////////////////////////////////
struct CDiscordGuildMember : public MZeroedObject
{
CDiscordGuildMember(SnowFlake id) :
userId(id)
{}
~CDiscordGuildMember()
{}
SnowFlake userId;
CMStringW wszNick, wszRole;
int iStatus;
};
struct CDiscordGuild : public MZeroedObject
{
CDiscordGuild(SnowFlake _id);
~CDiscordGuild();
__forceinline CDiscordGuildMember* FindUser(SnowFlake userId)
{
return arChatUsers.find((CDiscordGuildMember*)&userId);
}
SnowFlake id, ownerId;
CMStringW wszName;
MCONTACT hContact;
MGROUP groupId;
bool bSynced = false;
LIST<CDiscordUser> arChannels;
SESSION_INFO *pParentSi;
OBJLIST<CDiscordGuildMember> arChatUsers;
OBJLIST<CDiscordRole> arRoles; // guild roles
};
/////////////////////////////////////////////////////////////////////////////////////////
class CDiscordProto : public PROTO<CDiscordProto>
{
friend struct AsyncHttpRequest;
friend class CDiscardAccountOptions;
//////////////////////////////////////////////////////////////////////////////////////
// threads
void __cdecl SendFileThread(void*);
void __cdecl ServerThread(void*);
void __cdecl SearchThread(void *param);
void __cdecl SendMessageAckThread(void* param);
void __cdecl BatchChatCreate(void* param);
//////////////////////////////////////////////////////////////////////////////////////
// session control
void ConnectionFailed(int iReason);
void ShutdownSession(void);
ptrA m_szAccessToken, m_szAccessCookie;
mir_cs m_csHttpQueue;
HANDLE m_evRequestsQueue;
LIST<AsyncHttpRequest> m_arHttpQueue;
void ExecuteRequest(AsyncHttpRequest *pReq);
AsyncHttpRequest* Push(AsyncHttpRequest *pReq, int iTimeout = 10000);
HANDLE m_hWorkerThread; // worker thread handle
HNETLIBCONN m_hAPIConnection; // working connection
bool
m_bOnline, // protocol is online
m_bTerminated; // Miranda's going down
//////////////////////////////////////////////////////////////////////////////////////
// gateway
CMStringA
m_szGateway, // gateway url
m_szGatewaySessionId; // current session id
HNETLIBUSER m_hGatewayNetlibUser; // the separate netlib user handle for gateways
HNETLIBCONN m_hGatewayConnection; // gateway connection
void __cdecl GatewayThread(void*);
bool GatewayThreadWorker(void);
void GatewaySend(const JSONNode&, int opCode = 1);
void GatewayProcess(const JSONNode&);
void GatewaySendHeartbeat(void);
void GatewaySendIdentify(void);
void GatewaySendGuildInfo(SnowFlake id);
void GatewaySendResume(void);
GatewayHandlerFunc GetHandler(const wchar_t*);
int m_iHartbeatInterval; // in milliseconds
int m_iGatewaySeq; // gateway sequence number
//////////////////////////////////////////////////////////////////////////////////////
// options
CMOption<wchar_t*> m_wszEmail; // my own email
CMOption<wchar_t*> m_wszDefaultGroup; // clist group to store contacts
CMOption<BYTE> m_bHideGroupchats; // hide guild chats on startup
CMOption<BYTE> m_bUseGuildGroups; // use special subgroups for guilds
//////////////////////////////////////////////////////////////////////////////////////
// common data
SnowFlake m_ownId;
mir_cs csMarkReadQueue;
LIST<CDiscordUser> arMarkReadQueue;
OBJLIST<CDiscordUser> arUsers;
OBJLIST<COwnMessage> arOwnMessages;
CDiscordUser* FindUser(SnowFlake id);
CDiscordUser* FindUser(const wchar_t *pwszUsername, int iDiscriminator);
CDiscordUser* FindUserByChannel(SnowFlake channelId);
CDiscordUser* PrepareUser(const JSONNode&);
//////////////////////////////////////////////////////////////////////////////////////
// menu items
void InitMenus(void);
int __cdecl OnMenuPrebuild(WPARAM, LPARAM);
INT_PTR __cdecl OnMenuCopyId(WPARAM, LPARAM);
INT_PTR __cdecl OnMenuCreateChannel(WPARAM, LPARAM);
INT_PTR __cdecl OnMenuJoinGuild(WPARAM, LPARAM);
INT_PTR __cdecl OnMenuLeaveGuild(WPARAM, LPARAM);
HGENMENU m_hMenuLeaveGuild, m_hMenuCreateChannel;
//////////////////////////////////////////////////////////////////////////////////////
// guilds
OBJLIST<CDiscordGuild> arGuilds;
__forceinline CDiscordGuild* FindGuild(SnowFlake id) const
{
return arGuilds.find((CDiscordGuild*)&id);
}
void ProcessGuild(const JSONNode&);
void AddGuildUser(CDiscordGuild *guild, const CDiscordGuildMember &pUser);
void ParseGuildContents(CDiscordGuild *guild, const JSONNode &);
CDiscordUser* ProcessGuildChannel(CDiscordGuild *guild, const JSONNode&);
void ProcessRole(CDiscordGuild *guild, const JSONNode&);
void ProcessType(CDiscordUser *pUser, const JSONNode&);
//////////////////////////////////////////////////////////////////////////////////////
// group chats
int __cdecl GroupchatEventHook(WPARAM, LPARAM);
int __cdecl GroupchatMenuHook(WPARAM, LPARAM);
void Chat_SendPrivateMessage(GCHOOK *gch);
void Chat_ProcessLogMenu(GCHOOK *gch);
void CreateChat(CDiscordGuild *pGuild, CDiscordUser *pUser);
void ParseSpecialChars(SESSION_INFO *si, CMStringW &str);
//////////////////////////////////////////////////////////////////////////////////////
// misc methods
SnowFlake getId(const char *szName);
SnowFlake getId(MCONTACT hContact, const char *szName);
void setId(const char *szName, SnowFlake iValue);
void setId(MCONTACT hContact, const char *szName, SnowFlake iValue);
public:
CDiscordProto(const char*,const wchar_t*);
~CDiscordProto();
//////////////////////////////////////////////////////////////////////////////////////
// PROTO_INTERFACE
INT_PTR GetCaps(int, MCONTACT = 0) override;
HWND CreateExtendedSearchUI(HWND owner) override;
HWND SearchAdvanced(HWND owner) override;
HANDLE SearchBasic(const wchar_t *id) override;
MCONTACT AddToList(int flags, PROTOSEARCHRESULT *psr) override;
int AuthRecv(MCONTACT, PROTORECVEVENT *pre) override;
int Authorize(MEVENT hDbEvent) override;
int AuthDeny(MEVENT hDbEvent, const wchar_t* szReason) override;
int AuthRequest(MCONTACT hContact, const wchar_t*) override;
int SendMsg(MCONTACT hContact, int flags, const char *pszSrc) override;
HANDLE SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles) override;
int UserIsTyping(MCONTACT hContact, int type) override;
int SetStatus(int iNewStatus) override;
void OnContactDeleted(MCONTACT) override;
void OnModulesLoaded() override;
void OnShutdown() override;
//////////////////////////////////////////////////////////////////////////////////////
// Services
INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM);
INT_PTR __cdecl GetAvatarCaps(WPARAM, LPARAM);
INT_PTR __cdecl GetAvatarInfo(WPARAM, LPARAM);
INT_PTR __cdecl GetMyAvatar(WPARAM, LPARAM);
INT_PTR __cdecl SetMyAvatar(WPARAM, LPARAM);
//////////////////////////////////////////////////////////////////////////////////////
// Events
int __cdecl OnOptionsInit(WPARAM, LPARAM);
int __cdecl OnAccountChanged(WPARAM, LPARAM);
int __cdecl OnDbEventRead(WPARAM, LPARAM);
//////////////////////////////////////////////////////////////////////////////////////
// dispatch commands
void OnCommandChannelCreated(const JSONNode&);
void OnCommandChannelDeleted(const JSONNode&);
void OnCommandChannelUpdated(const JSONNode&);
void OnCommandGuildCreated(const JSONNode&);
void OnCommandGuildDeleted(const JSONNode&);
void OnCommandGuildMemberAdded(const JSONNode&);
void OnCommandGuildMemberRemoved(const JSONNode&);
void OnCommandGuildMemberUpdated(const JSONNode&);
void OnCommandGuildSync(const JSONNode&);
void OnCommandFriendAdded(const JSONNode&);
void OnCommandFriendRemoved(const JSONNode&);
void OnCommandMessage(const JSONNode&, bool);
void OnCommandMessageCreate(const JSONNode&);
void OnCommandMessageUpdate(const JSONNode&);
void OnCommandMessageAck(const JSONNode&);
void OnCommandPresence(const JSONNode&);
void OnCommandReady(const JSONNode&);
void OnCommandRoleCreated(const JSONNode&);
void OnCommandRoleDeleted(const JSONNode&);
void OnCommandTyping(const JSONNode&);
void OnCommandUserUpdate(const JSONNode&);
void OnCommandUserSettingsUpdate(const JSONNode&);
void OnLoggedIn();
void OnLoggedOut();
void OnReceiveCreateChannel(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveFile(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveGateway(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveMessageAck(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveToken(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void RetrieveMyInfo();
void OnReceiveMyInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void RetrieveHistory(MCONTACT hContact, CDiscordHistoryOp iOp = MSG_NOFILTER, SnowFlake msgid = 0, int iLimit = 50);
void OnReceiveHistory(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
bool RetrieveAvatar(MCONTACT hContact);
void OnReceiveAvatar(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
//////////////////////////////////////////////////////////////////////////////////////
// Misc
void SetServerStatus(int iStatus);
void RemoveFriend(SnowFlake id);
CMStringW GetAvatarFilename(MCONTACT hContact);
void CheckAvatarChange(MCONTACT hContact, const CMStringW &wszNewHash);
__forceinline int getHeartbeatInterval() const { return m_iHartbeatInterval; }
static void CALLBACK HeartbeatTimerProc(HWND hwnd, UINT msg, UINT_PTR id, DWORD);
static void CALLBACK MarkReadTimerProc(HWND hwnd, UINT msg, UINT_PTR id, DWORD);
};
/////////////////////////////////////////////////////////////////////////////////////////
struct CMPlugin : public ACCPROTOPLUGIN<CDiscordProto>
{
CMPlugin();
int Load() override;
int Unload() override;
};
|