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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/*
WhatsApp plugin for Miranda NG
Copyright © 2019-22 George Hazan
*/
#if !defined(PROTO_H)
#define PROTO_H
#define S_WHATSAPP_NET "@s.whatsapp.net"
#define APP_VERSION "2.2230.15"
#define KEY_BUNDLE_TYPE "\x05"
class WhatsAppProto;
typedef void (WhatsAppProto:: *WA_PKT_HANDLER)(const WANode &node);
struct WAMSG
{
union {
uint32_t dwFlags = 0;
struct {
bool bPrivateChat : 1;
bool bGroupChat : 1;
bool bDirectStatus : 1;
bool bOtherStatus : 1;
bool bPeerBroadcast : 1;
bool bOtherBroadcast : 1;
bool bOffline : 1;
};
};
};
struct WARequest
{
WARequest(const CMStringA &_1, WA_PKT_HANDLER _2, void *_3 = nullptr) :
szPacketId(_1),
pHandler(_2),
pUserInfo(_3)
{}
CMStringA szPacketId;
WA_PKT_HANDLER pHandler;
void *pUserInfo;
};
struct WADevice
{
WADevice(const char *_1, int _2) :
jid(_1),
key_index(_2)
{}
WAJid jid;
int key_index;
};
struct WAPersistentHandler
{
WAPersistentHandler(const char *_1, const char *_2, const char *_3, const char *_4, WA_PKT_HANDLER _5) :
pszTitle(_1), pszType(_2), pszXmlns(_3), pszChild(_4), pHandler(_5)
{}
const char *pszTitle, *pszType, *pszXmlns, *pszChild;
WA_PKT_HANDLER pHandler;
};
struct WAUser
{
WAUser(MCONTACT _1, const char *_2, bool _3 = false) :
hContact(_1),
szId(mir_strdup(_2)),
bIsGroupChat(_3)
{
}
~WAUser()
{
mir_free(szId);
}
MCONTACT hContact;
DWORD dwModifyTag = 0;
char *szId;
bool bInited = false, bIsGroupChat;
SESSION_INFO *si = 0;
time_t m_timer1 = 0, m_timer2 = 0;
};
struct WAOwnMessage
{
WAOwnMessage(int _1, const char *_2, const char *_3) :
pktId(_1),
szJid(_2),
szMessageId(_3)
{}
int pktId;
CMStringA szJid, szMessageId;
};
struct WACollection
{
WACollection(const char *_1, int _2 = 0) :
szName(mir_strdup(_1)),
version(_2)
{}
ptrA szName;
int version;
LT_HASH hash;
std::map<std::string, std::string> indexValueMap;
};
class WANoise
{
friend class WhatsAppProto;
WhatsAppProto *ppro;
uint32_t readCounter = 0, writeCounter = 0;
bool bInitFinished = false, bSendIntro = false;
MBinBuffer salt, encKey, decKey;
uint8_t hash[32];
struct {
MBinBuffer priv, pub;
} noiseKeys, ephemeral;
void deriveKey(const void *pData, size_t cbLen, MBinBuffer &write, MBinBuffer &read);
void mixIntoKey(const void *n, const void *p);
void updateHash(const void *pData, size_t cbLen);
public:
WANoise(WhatsAppProto *_ppro);
void finish();
void init();
MBinBuffer decrypt(const void *pData, size_t cbLen);
MBinBuffer encrypt(const void *pData, size_t cbLen);
size_t decodeFrame(const uint8_t *&pData, size_t &cbLen);
MBinBuffer encodeFrame(const void *pData, size_t cbLen);
};
class MSignalSession : public MZeroedObject
{
friend class MSignalStore;
signal_protocol_address address;
session_cipher *cipher = nullptr;
public:
CMStringA szName;
MBinBuffer sessionData;
MSignalSession(const CMStringA &_1, int _2);
~MSignalSession();
bool hasAddress(const char *name, size_t name_len) const;
__forceinline session_cipher* getCipher(void) const { return cipher; }
__forceinline int getDeviceId() const { return address.device_id; }
CMStringA getSetting() const;
};
class MSignalStore
{
void init();
signal_context *m_pContext;
signal_protocol_store_context *m_pStore;
public:
PROTO_INTERFACE *pProto;
const char *prefix;
OBJLIST<MSignalSession> arSessions;
struct
{
MBinBuffer priv, pub;
}
signedIdentity;
struct
{
MBinBuffer priv, pub, signature;
uint32_t keyid;
} preKey;
MSignalStore(PROTO_INTERFACE *_1, const char *_2);
~MSignalStore();
__forceinline signal_context *CTX() const { return m_pContext; }
MSignalSession *createSession(const CMStringA &szName, int deviceId);
MBinBuffer decryptSignalProto(const CMStringA &from, const char *pszType, const MBinBuffer &encrypted);
MBinBuffer decryptGroupSignalProto(const CMStringA &from, const CMStringA &author, const MBinBuffer &encrypted);
MBinBuffer encryptSignalProto(const WAJid &to, const MBinBuffer &buf, int &type);
MBinBuffer encodeSignedIdentity(bool);
void generatePrekeys(int count);
void logError(int code, const char *szMessage);
void processSenderKeyMessage(const CMStringA &author, const Wa__Message__SenderKeyDistributionMessage *msg);
};
class WhatsAppProto : public PROTO<WhatsAppProto>
{
friend class WANoise;
friend class CWhatsAppQRDlg;
class CWhatsAppProtoImpl
{
friend class WhatsAppProto;
WhatsAppProto &m_proto;
CTimer m_keepAlive, m_resyncApp;
void OnKeepAlive(CTimer *)
{ m_proto.SendKeepAlive();
}
void OnResync(CTimer *pTimer)
{
pTimer->Stop();
m_proto.ResyncAll();
}
CWhatsAppProtoImpl(WhatsAppProto &pro) :
m_proto(pro),
m_keepAlive(Miranda_GetSystemWindow(), UINT_PTR(this)),
m_resyncApp(Miranda_GetSystemWindow(), UINT_PTR(this)+1)
{
m_keepAlive.OnEvent = Callback(this, &CWhatsAppProtoImpl::OnKeepAlive);
m_resyncApp.OnEvent = Callback(this, &CWhatsAppProtoImpl::OnResync);
}
} m_impl;
bool m_bTerminated, m_bRespawn, m_bUpdatedPrekeys;
ptrW m_tszDefaultGroup;
CMStringA m_szJid;
CMStringW m_tszAvatarFolder;
EVP_PKEY *m_pKeys; // private & public keys
WANoise *m_noise;
void UploadMorePrekeys();
// App state management
OBJLIST<WACollection> m_arCollections;
void InitSync(void);
void ApplyPatch(const JSONNode &index, const Wa__SyncActionValue *data);
void ParsePatch(WACollection *pColl, const Wa__SyncdRecord *rec, bool bSet);
void ResyncServer(const OBJLIST<WACollection> &task);
void ResyncAll(void);
__forceinline WACollection *FindCollection(const char *pszName)
{ return m_arCollections.find((WACollection *)&pszName);
}
// Contacts management /////////////////////////////////////////////////////////////////
mir_cs m_csUsers;
OBJLIST<WAUser> m_arUsers;
mir_cs m_csOwnMessages;
OBJLIST<WAOwnMessage> m_arOwnMsgs;
OBJLIST<WADevice> m_arDevices;
WAUser* FindUser(const char *szId);
WAUser* AddUser(const char *szId, bool bTemporary, bool isChat = false);
// Group chats /////////////////////////////////////////////////////////////////////////
void InitChat(WAUser *pUser);
// UI //////////////////////////////////////////////////////////////////////////////////
void CloseQrDialog();
bool ShowQrCode(const CMStringA &ref);
/// Network ////////////////////////////////////////////////////////////////////////////
time_t m_lastRecvTime;
HNETLIBCONN m_hServerConn;
mir_cs m_csPacketQueue;
OBJLIST<WARequest> m_arPacketQueue;
LIST<WAPersistentHandler> m_arPersistent;
WA_PKT_HANDLER FindPersistentHandler(const WANode &node);
int m_iPacketId;
uint16_t m_wMsgPrefix[2];
CMStringA GenerateMessageId();
void ProcessMessage(WAMSG type, const Wa__WebMessageInfo &msg);
bool CreateMsgParticipant(WANode *pParticipants, const WAJid &jid, const MBinBuffer &orig);
void ProcessReceipt(MCONTACT hContact, const char *msgId, bool bRead);
bool WSReadPacket(const WSHeader &hdr, MBinBuffer &buf);
int WSSend(const ProtobufCMessage &msg);
int WSSendNode(WANode &node, WA_PKT_HANDLER = nullptr);
MBinBuffer DownloadEncryptedFile(const char *url, const ProtobufCBinaryData &mediaKeys, const char *pszType);
CMStringW GetTmpFileName(const char *pszClass, const char *addition);
void OnLoggedIn(void);
void OnLoggedOut(void);
void ServerThreadWorker(void);
void ShutdownSession(void);
void SendAck(const WANode &node);
void SendReceipt(const char *pszTo, const char *pszParticipant, const char *pszId, const char *pszType);
void SendKeepAlive();
int SendTextMessage(const char *jid, const char *pszMsg);
void SetServerStatus(int iStatus);
/// Popups /////////////////////////////////////////////////////////////////////////////
HANDLE m_hPopupClass;
CMOption<bool> m_bUsePopups;
void InitPopups(void);
void Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t *szTitle);
/// Request handlers ///////////////////////////////////////////////////////////////////
void OnGetChatInfo(const JSONNode &node, void*);
void OnProcessHandshake(const uint8_t *pData, int cbLen);
void InitPersistentHandlers();
void OnAccountSync(const WANode &node);
void OnIqBlockList(const WANode &node);
void OnIqCountPrekeys(const WANode &node);
void OnIqDoNothing(const WANode &node);
void OnIqGetAvatar(const WANode &node);
void OnIqGetUsync(const WANode &node);
void OnIqPairDevice(const WANode &node);
void OnIqPairSuccess(const WANode &node);
void OnIqResult(const WANode &node);
void OnIqServerSync(const WANode &node);
void OnNotifyAny(const WANode &node);
void OnNotifyDevices(const WANode &node);
void OnNotifyEncrypt(const WANode &node);
void OnNotifyPicture(const WANode &node);
void OnReceiveAck(const WANode &node);
void OnReceiveChatState(const WANode &node);
void OnReceiveInfo(const WANode &node);
void OnReceiveMessage(const WANode &node);
void OnReceiveReceipt(const WANode &node);
void OnServerSync(const WANode &node);
void OnStreamError(const WANode &node);
void OnSuccess(const WANode &node);
// Signal
MSignalStore m_signalStore;
// Binary packets
void ProcessBinaryPacket(const uint8_t *pData, size_t cbLen);
// unzip operations
MBinBuffer unzip(const MBinBuffer &src);
/// Avatars ////////////////////////////////////////////////////////////////////////////
CMStringW GetAvatarFileName(MCONTACT hContact);
void ServerFetchAvatar(const char *jid);
INT_PTR __cdecl GetAvatarInfo(WPARAM, LPARAM);
INT_PTR __cdecl GetAvatarCaps(WPARAM, LPARAM);
INT_PTR __cdecl GetMyAvatar(WPARAM, LPARAM);
INT_PTR __cdecl SetMyAvatar(WPARAM, LPARAM);
public:
WhatsAppProto(const char *proto_name, const wchar_t *username);
~WhatsAppProto();
__forceinline bool isOnline() const
{ return m_hServerConn != 0;
}
__forceinline void writeStr(const char *pszSetting, const JSONNode &node)
{
CMStringW str(node.as_mstring());
if (!str.IsEmpty())
setWString(pszSetting, str);
}
class CWhatsAppQRDlg *m_pQRDlg;
// PROTO_INTERFACE /////////////////////////////////////////////////////////////////////
MCONTACT AddToList(int flags, PROTOSEARCHRESULT *psr) override;
INT_PTR GetCaps(int type, MCONTACT hContact = NULL) override;
HANDLE SearchBasic(const wchar_t* id) override;
int SendMsg(MCONTACT hContact, int flags, const char* msg) override;
int SetStatus(int iNewStatus) override;
int UserIsTyping(MCONTACT hContact, int type) override;
void OnModulesLoaded() override;
// Services ////////////////////////////////////////////////////////////////////////////
INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM);
// Events //////////////////////////////////////////////////////////////////////////////
int __cdecl OnOptionsInit(WPARAM, LPARAM);
int __cdecl OnBuildStatusMenu(WPARAM, LPARAM);
// Options /////////////////////////////////////////////////////////////////////////////
CMOption<wchar_t*> m_wszNick; // your nick name in presence
CMOption<wchar_t*> m_wszDefaultGroup; // clist group to store contacts
CMOption<bool> m_bHideGroupchats; // do not open chat windows on creation
// Processing Threads //////////////////////////////////////////////////////////////////
void __cdecl SearchAckThread(void*);
void __cdecl ServerThread(void*);
};
struct CMPlugin : public ACCPROTOPLUGIN<WhatsAppProto>
{
HNETLIBUSER hAvatarUser = nullptr;
HNETLIBCONN hAvatarConn = nullptr;
bool SaveFile(const char *pszUrl, PROTO_AVATAR_INFORMATION &ai);
bool bHasMessageState = false;
CMPlugin();
int Load() override;
int Unload() override;
};
#endif
|