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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
#include "stdafx.h"
static const wchar_t *sttStatuses[] = { LPGENW("Members"), LPGENW("Owners") };
enum
{
IDM_CANCEL,
IDM_INVITE, IDM_LEAVE, IDM_TOPIC,
IDM_AVATAR, IDM_KICK,
IDM_CPY_NICK, IDM_CPY_TOPIC,
IDM_ADD_RJID, IDM_CPY_RJID
};
/////////////////////////////////////////////////////////////////////////////////////////
// protocol menu handler - create a new group
INT_PTR __cdecl WhatsAppProto::OnCreateGroup(WPARAM, LPARAM)
{
ENTER_STRING es = { 0 };
es.cbSize = sizeof(es);
es.type = ESF_MULTILINE;
es.caption = L"Enter a subject for new group";
es.szModuleName = m_szModuleName;
if (EnterString(&es)) {
if (isOnline()) {
std::string groupName(T2Utf(es.ptszResult));
m_pConnection->sendCreateGroupChat(groupName);
}
mir_free(es.ptszResult);
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////
// handler to pass events from SRMM to WAConnection
int WhatsAppProto::onGroupChatEvent(WPARAM, LPARAM lParam)
{
GCHOOK *gch = (GCHOOK*)lParam;
if (mir_strcmp(gch->pDest->pszModule, m_szModuleName))
return 0;
std::string chat_id(T2Utf(gch->pDest->ptszID));
WAChatInfo *pInfo = SafeGetChat(chat_id);
if (pInfo == NULL)
return 0;
switch (gch->pDest->iType) {
case GC_USER_LOGMENU:
ChatLogMenuHook(pInfo, gch);
break;
case GC_USER_NICKLISTMENU:
NickListMenuHook(pInfo, gch);
break;
case GC_USER_MESSAGE:
if (isOnline()) {
std::string msg(T2Utf(gch->ptszText));
try {
int msgId = GetSerial();
time_t now = time(NULL);
std::string id = Utilities::intToStr(now) + "-" + Utilities::intToStr(msgId);
FMessage fmsg(chat_id, true, id);
fmsg.timestamp = now;
fmsg.data = msg;
m_pConnection->sendMessage(&fmsg);
pInfo->m_unsentMsgs[id] = gch->ptszText;
}
CODE_BLOCK_CATCH_ALL
}
break;
case GC_USER_PRIVMESS:
string jid = string(_T2A(gch->ptszUID)) + "@s.whatsapp.net";
MCONTACT hContact = ContactIDToHContact(jid);
if (hContact == 0) {
hContact = AddToContactList(jid, (char*)_T2A(gch->ptszUID));
setWord(hContact, "Status", ID_STATUS_ONLINE);
db_set_b(hContact, "CList", "Hidden", 1);
setWString(hContact, "Nick", gch->ptszUID);
db_set_dw(hContact, "Ignore", "Mask1", 0);
}
CallService(MS_MSG_SENDMESSAGE, hContact, 0);
break;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// chat log menu event handler
static gc_item sttLogListItems[] =
{
{ LPGENW("&Invite a user"), IDM_INVITE, MENU_ITEM },
{ NULL, 0, MENU_SEPARATOR },
{ LPGENW("&Room options"), 0, MENU_NEWPOPUP },
{ LPGENW("View/change &topic"), IDM_TOPIC, MENU_POPUPITEM },
{ LPGENW("&Quit chat session"), IDM_LEAVE, MENU_POPUPITEM },
#ifdef _DEBUG
{ LPGENW("Set &avatar"), IDM_AVATAR, MENU_POPUPITEM }, // doesn't work, therefore commented out
#endif
{ NULL, 0, MENU_SEPARATOR },
{ LPGENW("Copy room &JID"), IDM_CPY_RJID, MENU_ITEM },
{ LPGENW("Copy room topic"), IDM_CPY_TOPIC, MENU_ITEM },
};
void WhatsAppProto::ChatLogMenuHook(WAChatInfo *pInfo, struct GCHOOK *gch)
{
switch (gch->dwData) {
case IDM_INVITE:
InviteChatUser(pInfo);
break;
case IDM_TOPIC:
EditChatSubject(pInfo);
break;
case IDM_CPY_RJID:
utils::copyText(pcli->hwndContactList, pInfo->tszJid);
break;
case IDM_CPY_TOPIC:
utils::copyText(pcli->hwndContactList, pInfo->tszNick);
break;
case IDM_LEAVE:
if (isOnline())
m_pConnection->sendJoinLeaveGroup(_T2A(pInfo->tszJid), false);
break;
case IDM_AVATAR:
SetChatAvatar(pInfo);
break;
}
}
void WhatsAppProto::EditChatSubject(WAChatInfo *pInfo)
{
CMStringW title(FORMAT, TranslateT("Set new subject for %s"), pInfo->tszNick);
ptrW tszOldValue(getWStringA(pInfo->hContact, WHATSAPP_KEY_NICK));
ENTER_STRING es = { 0 };
es.cbSize = sizeof(es);
es.type = ESF_RICHEDIT;
es.szModuleName = m_szModuleName;
es.ptszInitVal = tszOldValue;
es.caption = title;
es.szDataPrefix = "setSubject_";
if (EnterString(&es)) {
T2Utf gjid(pInfo->tszJid);
T2Utf gsubject(es.ptszResult);
m_pConnection->sendSetNewSubject(std::string(gjid), std::string(gsubject));
mir_free(es.ptszResult);
}
}
void WhatsAppProto::SetChatAvatar(WAChatInfo *pInfo)
{
wchar_t tszFileName[MAX_PATH], filter[256];
Bitmap_GetFilter(filter, _countof(filter));
OPENFILENAME ofn = { 0 };
ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
ofn.lpstrFilter = filter;
ofn.hwndOwner = 0;
ofn.lpstrFile = tszFileName;
ofn.nMaxFile = ofn.nMaxFileTitle = _countof(tszFileName);
ofn.Flags = OFN_HIDEREADONLY;
ofn.lpstrInitialDir = L".";
ofn.lpstrDefExt = L"";
if (GetOpenFileName(&ofn))
if (_waccess(tszFileName, 4) != -1)
InternalSetAvatar(pInfo->hContact, _T2A(pInfo->tszJid), tszFileName);
}
/////////////////////////////////////////////////////////////////////////////////////////
// nicklist menu event handler
static gc_item sttNickListItems[] =
{
{ LPGENW("&Add to roster"), IDM_ADD_RJID, MENU_POPUPITEM },
{ NULL, 0, MENU_SEPARATOR },
{ LPGENW("&Kick"), IDM_KICK, MENU_ITEM },
{ NULL, 0, MENU_SEPARATOR },
{ LPGENW("Copy &nickname"), IDM_CPY_NICK, MENU_ITEM },
{ LPGENW("Copy real &JID"), IDM_CPY_RJID, MENU_ITEM },
};
void WhatsAppProto::NickListMenuHook(WAChatInfo *pInfo, struct GCHOOK *gch)
{
switch (gch->dwData) {
case IDM_ADD_RJID:
AddChatUser(pInfo, gch->ptszUID);
break;
case IDM_KICK:
KickChatUser(pInfo, gch->ptszUID);
break;
case IDM_CPY_NICK:
utils::copyText(pcli->hwndContactList, ptrW(GetChatUserNick(std::string((char*)_T2A(gch->ptszUID)))));
break;
case IDM_CPY_RJID:
utils::copyText(pcli->hwndContactList, gch->ptszUID);
break;
}
}
void WhatsAppProto::AddChatUser(WAChatInfo*, const wchar_t *ptszJid)
{
std::string jid((char*)_T2A(ptszJid));
MCONTACT hContact = ContactIDToHContact(jid);
if (hContact && !db_get_b(hContact, "CList", "NotInList", 0))
return;
PROTOSEARCHRESULT psr = { 0 };
psr.cbSize = sizeof(psr);
psr.flags = PSR_UNICODE;
psr.id.w = (wchar_t*)ptszJid;
psr.nick.w = GetChatUserNick(jid);
ADDCONTACTSTRUCT acs = { 0 };
acs.handleType = HANDLE_SEARCHRESULT;
acs.szProto = m_szModuleName;
acs.psr = &psr;
CallService(MS_ADDCONTACT_SHOW, (WPARAM)pcli->hwndContactList, (LPARAM)&acs);
}
void WhatsAppProto::KickChatUser(WAChatInfo *pInfo, const wchar_t *ptszJid)
{
if (!isOnline())
return;
std::string gjid((char*)_T2A(pInfo->tszJid));
std::vector<std::string> jids(1);
jids[0] = (char*)_T2A(ptszJid);
m_pConnection->sendRemoveParticipants(gjid, jids);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Leave groupchat emulator for contact's deletion
int WhatsAppProto::OnDeleteChat(WPARAM hContact, LPARAM)
{
if (isChatRoom(hContact) && isOnline()) {
ptrW tszID(getWStringA(hContact, WHATSAPP_KEY_ID));
if (tszID)
m_pConnection->sendJoinLeaveGroup(_T2A(tszID), false);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// handler to customize chat menus
int WhatsAppProto::OnChatMenu(WPARAM, LPARAM lParam)
{
GCMENUITEMS *gcmi = (GCMENUITEMS*)lParam;
if (gcmi == NULL)
return 0;
if (mir_strcmpi(gcmi->pszModule, m_szModuleName))
return 0;
if (gcmi->Type == MENU_ON_LOG) {
gcmi->nItems = _countof(sttLogListItems);
gcmi->Item = sttLogListItems;
}
else if (gcmi->Type == MENU_ON_NICKLIST) {
gcmi->nItems = _countof(sttNickListItems);
gcmi->Item = sttNickListItems;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// chat helpers
WAChatInfo* WhatsAppProto::InitChat(const std::string &jid, const std::string &nick)
{
wchar_t *ptszJid = str2t(jid), *ptszNick = str2t(nick);
WAChatInfo *pInfo = new WAChatInfo(ptszJid, ptszNick);
m_chats[jid] = pInfo;
MCONTACT hOldContact = ContactIDToHContact(jid);
if (hOldContact && !isChatRoom(hOldContact)) {
delSetting(hOldContact, "ID");
setByte(hOldContact, "ChatRoom", 1);
setString(hOldContact, "ChatRoomID", jid.c_str());
}
GCSESSION gcw = { sizeof(GCSESSION) };
gcw.iType = GCW_CHATROOM;
gcw.pszModule = m_szModuleName;
gcw.ptszName = ptszNick;
gcw.ptszID = ptszJid;
CallServiceSync(MS_GC_NEWSESSION, NULL, (LPARAM)&gcw);
pInfo->hContact = (hOldContact != NULL) ? hOldContact : ContactIDToHContact(jid);
GCDEST gcd = { m_szModuleName, ptszJid, GC_EVENT_ADDGROUP };
GCEVENT gce = { sizeof(gce), &gcd };
for (int i = _countof(sttStatuses) - 1; i >= 0; i--) {
gce.ptszStatus = TranslateW(sttStatuses[i]);
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
}
gcd.iType = GC_EVENT_CONTROL;
CallServiceSync(MS_GC_EVENT, getBool(WHATSAPP_KEY_AUTORUNCHATS, true) ? SESSION_INITDONE : WINDOW_HIDDEN, (LPARAM)&gce);
CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce);
if (m_pConnection)
m_pConnection->sendGetParticipants(jid);
return pInfo;
}
wchar_t* WhatsAppProto::GetChatUserNick(const std::string &jid)
{
wchar_t* tszNick;
if (m_szJid != jid) {
MCONTACT hContact = ContactIDToHContact(jid);
tszNick = (hContact == 0) ? utils::removeA(str2t(jid)) : mir_wstrdup(pcli->pfnGetContactDisplayName(hContact, 0));
}
else tszNick = str2t(m_szNick);
if (tszNick == NULL)
tszNick = mir_wstrdup(TranslateT("Unknown user"));
return tszNick;
}
WAChatInfo* WhatsAppProto::SafeGetChat(const std::string &jid)
{
mir_cslock lck(m_csChats);
return m_chats[jid];
}
///////////////////////////////////////////////////////////////////////////////
// WAGroupListener members
void WhatsAppProto::onGroupInfo(const std::string &jid, const std::string &owner, const std::string &subject, const std::string &subject_owner, int time_subject, int)
{
WAChatInfo *pInfo = SafeGetChat(jid);
if (pInfo == NULL) {
pInfo = InitChat(jid, subject);
pInfo->bActive = true;
time_subject = 0;
}
else {
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_CONTROL };
GCEVENT gce = { sizeof(gce), &gcd };
CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce);
}
if (!subject.empty()) {
pInfo->tszOwner = str2t(owner);
onGroupNewSubject(jid, subject_owner, subject, time_subject);
}
}
void WhatsAppProto::onGroupMessage(const FMessage &pMsg)
{
// we need to add a contact, so there's no difference at all
if (pMsg.media_wa_type == FMessage::WA_TYPE_CONTACT) {
onMessageForMe(pMsg);
return;
}
WAChatInfo *pInfo = SafeGetChat(pMsg.key.remote_jid);
if (pInfo == NULL) {
pInfo = InitChat(pMsg.key.remote_jid, "");
pInfo->bActive = true;
}
std::string msg(pMsg.data);
if (!pMsg.media_url.empty()) {
if (!msg.empty())
msg.append("\n");
msg += pMsg.media_url;
}
ptrW tszText(str2t(msg));
ptrW tszUID(str2t(pMsg.remote_resource));
ptrW tszNick(GetChatUserNick(pMsg.remote_resource));
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_MESSAGE };
GCEVENT gce = { sizeof(gce), &gcd };
gce.dwFlags = GCEF_ADDTOLOG;
gce.ptszUID = tszUID;
gce.ptszNick = tszNick;
gce.time = pMsg.timestamp;
gce.ptszText = tszText;
gce.bIsMe = m_szJid == pMsg.remote_resource;
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
if (isOnline())
m_pConnection->sendMessageReceived(pMsg);
}
void WhatsAppProto::onGroupNewSubject(const std::string &gjid, const std::string &author, const std::string &newSubject, int ts)
{
WAChatInfo *pInfo = SafeGetChat(gjid);
if (pInfo == NULL)
return;
ptrW tszText(str2t(newSubject));
ptrW tszTextDb(getWStringA(pInfo->hContact, WHATSAPP_KEY_NICK));
if (!mir_wstrcmp(tszText, tszTextDb)) // notify about subject change only if differs from the stored one
return;
ptrW tszUID(str2t(author));
ptrW tszNick(GetChatUserNick(author));
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_TOPIC };
GCEVENT gce = { sizeof(gce), &gcd };
gce.dwFlags = GCEF_ADDTOLOG + ((ts == 0) ? GCEF_NOTNOTIFY : 0);
gce.ptszUID = tszUID;
gce.ptszNick = tszNick;
gce.time = ts;
gce.ptszText = tszText;
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
setWString(pInfo->hContact, WHATSAPP_KEY_NICK, tszText);
}
void WhatsAppProto::onGroupAddUser(const std::string &gjid, const std::string &ujid, int ts)
{
WAChatInfo *pInfo = SafeGetChat(gjid);
if (pInfo == NULL || !pInfo->bActive)
return;
ptrW tszUID(str2t(ujid));
ptrW tszNick(GetChatUserNick(ujid));
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_JOIN };
GCEVENT gce = { sizeof(gce), &gcd };
gce.dwFlags = GCEF_ADDTOLOG;
gce.ptszUID = tszUID;
gce.ptszNick = tszNick;
gce.time = ts;
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
}
void WhatsAppProto::onGroupRemoveUser(const std::string &gjid, const std::string &ujid, int ts)
{
WAChatInfo *pInfo = SafeGetChat(gjid);
if (pInfo == NULL)
return;
ptrW tszUID(str2t(ujid));
ptrW tszNick(GetChatUserNick(ujid));
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_PART };
GCEVENT gce = { sizeof(gce), &gcd };
gce.dwFlags = GCEF_ADDTOLOG;
gce.ptszUID = tszUID;
gce.ptszNick = tszNick;
gce.time = ts;
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
}
void WhatsAppProto::onLeaveGroup(const std::string &gjid)
{
WAChatInfo *pInfo = SafeGetChat(gjid);
if (pInfo == NULL)
return;
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_CONTROL };
GCEVENT gce = { sizeof(gce), &gcd };
gce.ptszUID = pInfo->tszJid;
CallServiceSync(MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce);
db_delete_contact(pInfo->hContact);
m_chats.erase((char*)_T2A(pInfo->tszJid));
}
void WhatsAppProto::onGetParticipants(const std::string &gjid, const std::vector<string> &participants)
{
mir_cslock lck(m_csChats);
WAChatInfo *pInfo = m_chats[gjid];
if (pInfo == NULL)
return;
pInfo->bActive = true;
for (size_t i = 0; i < participants.size(); i++) {
std::string curr = participants[i];
ptrW ujid(str2t(curr)), nick(GetChatUserNick(curr));
bool bIsOwner = !mir_wstrcmp(ujid, pInfo->tszOwner);
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_JOIN };
GCEVENT gce = { sizeof(gce), &gcd };
gce.ptszNick = nick;
gce.ptszUID = utils::removeA(ujid);
gce.ptszStatus = (bIsOwner) ? L"Owners" : L"Members";
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
}
}
void WhatsAppProto::onGroupCreated(const std::string &gjid, const std::string &subject)
{
WAChatInfo *pInfo = InitChat(gjid, subject);
pInfo->tszOwner = str2t(m_szJid);
// also set new subject if it's present
if (!subject.empty())
onGroupNewSubject(gjid, "Server", subject, 0);
}
void WhatsAppProto::onGroupMessageReceived(const FMessage &msg)
{
WAChatInfo *pInfo = SafeGetChat(msg.key.remote_jid);
if (pInfo == NULL)
return;
auto p = pInfo->m_unsentMsgs.find(msg.key.id);
if (p == pInfo->m_unsentMsgs.end())
return;
ptrW tszUID(str2t(m_szJid));
ptrW tszNick(str2t(m_szNick));
GCDEST gcd = { m_szModuleName, pInfo->tszJid, GC_EVENT_MESSAGE };
GCEVENT gce = { sizeof(gce), &gcd };
gce.dwFlags = GCEF_ADDTOLOG;
gce.ptszUID = tszUID;
gce.ptszNick = tszNick;
gce.time = time(NULL);
gce.ptszText = p->second.c_str();
gce.bIsMe = m_szJid == msg.remote_resource;
CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce);
pInfo->m_unsentMsgs.erase(p);
}
|