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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
|
/*
Jabber Protocol Plugin for Miranda NG
Copyright (c) 2002-04 Santithorn Bunchua
Copyright (c) 2005-12 George Hazan
Copyright (C) 2012-19 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdafx.h"
#include "jabber_caps.h"
int CJabberProto::SerialNext(void)
{
return ::InterlockedIncrement(&m_nSerial);
}
///////////////////////////////////////////////////////////////////////////////
// JabberChatRoomHContactFromJID - looks for the char room MCONTACT with required JID
MCONTACT CJabberProto::ChatRoomHContactFromJID(const wchar_t *jid)
{
JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_CHATROOM, jid);
if (item != nullptr && item->hContact)
return item->hContact;
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// JabberHContactFromJID - looks for the MCONTACT with required JID
MCONTACT CJabberProto::HContactFromJID(const wchar_t *jid, bool bStripResource)
{
if (jid == nullptr)
return 0;
JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_ROSTER, jid);
if (item != nullptr && item->hContact)
return item->hContact;
if (bStripResource) {
wchar_t szJid[JABBER_MAX_JID_LEN];
JabberStripJid(jid, szJid, _countof(szJid));
item = ListGetItemPtr(LIST_ROSTER, szJid);
if (item != nullptr && item->hContact)
return item->hContact;
}
return 0;
}
wchar_t* __stdcall JabberNickFromJID(const wchar_t *jid)
{
if (jid == nullptr)
return mir_wstrdup(L"");
const wchar_t *p = wcschr(jid, '@');
if (p == nullptr)
p = wcschr(jid, '/');
return (p != nullptr) ? mir_wstrndup(jid, p - jid) : mir_wstrdup(jid);
}
pResourceStatus CJabberProto::ResourceInfoFromJID(const wchar_t *jid)
{
if (jid == nullptr)
return nullptr;
const wchar_t *p = wcschr(jid, '/');
JABBER_LIST_ITEM *item = nullptr;
if (p) {
wchar_t szJid[JABBER_MAX_JID_LEN];
JabberStripJid(jid, szJid, _countof(szJid));
item = ListGetItemPtr(LIST_CHATROOM, szJid);
}
if (item == nullptr)
item = ListGetItemPtr(LIST_VCARD_TEMP, jid);
if (item == nullptr)
item = ListGetItemPtr(LIST_ROSTER, jid);
if (item == nullptr)
return nullptr;
if (p == nullptr)
return item->getTemp();
return item->findResource(p + 1);
}
wchar_t* JabberPrepareJid(const wchar_t *jid)
{
if (jid == nullptr) return nullptr;
wchar_t *szNewJid = mir_wstrdup(jid);
if (!szNewJid) return nullptr;
wchar_t *pDelimiter = wcschr(szNewJid, '/');
if (pDelimiter) *pDelimiter = 0;
CharLower(szNewJid);
if (pDelimiter) *pDelimiter = '/';
return szNewJid;
}
char* __stdcall JabberSha1(const char *str, JabberShaStrBuf buf)
{
BYTE digest[MIR_SHA1_HASH_SIZE];
mir_sha1_ctx sha;
mir_sha1_init(&sha);
mir_sha1_append(&sha, (BYTE*)str, (int)mir_strlen(str));
mir_sha1_finish(&sha, digest);
bin2hex(digest, sizeof(digest), buf);
return buf;
}
wchar_t* __stdcall JabberStrFixLines(const wchar_t *str)
{
if (str == nullptr)
return nullptr;
const wchar_t *p;
int add = 0;
bool prev_r = false;
bool prev_n = false;
for (p = str; p && *p; ++p)
if (*p == '\r' || *p == '\n')
++add;
wchar_t *buf = (wchar_t *)mir_alloc((mir_wstrlen(str) + add + 1) * sizeof(wchar_t));
wchar_t *res = buf;
for (p = str; p && *p; ++p) {
if (*p == '\n' && !prev_r)
*res++ = '\r';
if (*p != '\r' && *p != '\n' && prev_r)
*res++ = '\n';
*res++ = *p;
prev_r = *p == '\r';
prev_n = *p == '\n';
}
*res = 0;
return buf;
}
void __stdcall JabberHttpUrlDecode(wchar_t *str)
{
wchar_t *p, *q;
unsigned int code;
if (str == nullptr) return;
for (p = q = (wchar_t*)str; *p != '\0'; p++, q++) {
if (*p == '%' && *(p + 1) != '\0' && isxdigit(*(p + 1)) && *(p + 2) != '\0' && isxdigit(*(p + 2))) {
swscanf((wchar_t*)p + 1, L"%2x", &code);
*q = (unsigned char)code;
p += 2;
}
else *q = *p;
}
*q = '\0';
}
int __stdcall JabberCombineStatus(int status1, int status2)
{
// Combine according to the following priority (high to low)
// ID_STATUS_FREECHAT
// ID_STATUS_ONLINE
// ID_STATUS_DND
// ID_STATUS_AWAY
// ID_STATUS_NA
// ID_STATUS_INVISIBLE (valid only for TLEN_PLUGIN)
// ID_STATUS_OFFLINE
// other ID_STATUS in random order (actually return status1)
if (status1 == ID_STATUS_FREECHAT || status2 == ID_STATUS_FREECHAT)
return ID_STATUS_FREECHAT;
if (status1 == ID_STATUS_ONLINE || status2 == ID_STATUS_ONLINE)
return ID_STATUS_ONLINE;
if (status1 == ID_STATUS_DND || status2 == ID_STATUS_DND)
return ID_STATUS_DND;
if (status1 == ID_STATUS_AWAY || status2 == ID_STATUS_AWAY)
return ID_STATUS_AWAY;
if (status1 == ID_STATUS_NA || status2 == ID_STATUS_NA)
return ID_STATUS_NA;
if (status1 == ID_STATUS_INVISIBLE || status2 == ID_STATUS_INVISIBLE)
return ID_STATUS_INVISIBLE;
if (status1 == ID_STATUS_OFFLINE || status2 == ID_STATUS_OFFLINE)
return ID_STATUS_OFFLINE;
return status1;
}
struct tagErrorCodeToStr
{
int code;
wchar_t *str;
}
static JabberErrorCodeToStrMapping[] = {
{ JABBER_ERROR_REDIRECT, LPGENW("Redirect") },
{ JABBER_ERROR_BAD_REQUEST, LPGENW("Bad request") },
{ JABBER_ERROR_UNAUTHORIZED, LPGENW("Unauthorized") },
{ JABBER_ERROR_PAYMENT_REQUIRED, LPGENW("Payment required") },
{ JABBER_ERROR_FORBIDDEN, LPGENW("Forbidden") },
{ JABBER_ERROR_NOT_FOUND, LPGENW("Not found") },
{ JABBER_ERROR_NOT_ALLOWED, LPGENW("Not allowed") },
{ JABBER_ERROR_NOT_ACCEPTABLE, LPGENW("Not acceptable") },
{ JABBER_ERROR_REGISTRATION_REQUIRED, LPGENW("Registration required") },
{ JABBER_ERROR_REQUEST_TIMEOUT, LPGENW("Request timeout") },
{ JABBER_ERROR_CONFLICT, LPGENW("Conflict") },
{ JABBER_ERROR_INTERNAL_SERVER_ERROR, LPGENW("Internal server error") },
{ JABBER_ERROR_NOT_IMPLEMENTED, LPGENW("Not implemented") },
{ JABBER_ERROR_REMOTE_SERVER_ERROR, LPGENW("Remote server error") },
{ JABBER_ERROR_SERVICE_UNAVAILABLE, LPGENW("Service unavailable") },
{ JABBER_ERROR_REMOTE_SERVER_TIMEOUT, LPGENW("Remote server timeout") },
{ -1, LPGENW("Unknown error") }
};
wchar_t* __stdcall JabberErrorStr(int errorCode)
{
int i;
for (i = 0; JabberErrorCodeToStrMapping[i].code != -1 && JabberErrorCodeToStrMapping[i].code != errorCode; i++);
return JabberErrorCodeToStrMapping[i].str;
}
wchar_t* __stdcall JabberErrorMsg(HXML errorNode, int* pErrorCode)
{
wchar_t *errorStr = (wchar_t*)mir_alloc(256 * sizeof(wchar_t));
if (errorNode == nullptr) {
if (pErrorCode)
*pErrorCode = -1;
mir_snwprintf(errorStr, 256, L"%s -1: %s", TranslateT("Error"), TranslateT("Unknown error message"));
return errorStr;
}
int errorCode = -1;
const wchar_t *str = XmlGetAttrValue(errorNode, L"code");
if (str != nullptr)
errorCode = _wtoi(str);
str = XmlGetText(errorNode);
if (str == nullptr)
str = XmlGetText(XmlGetChild(errorNode, L"text"));
if (str == nullptr) {
for (int i = 0;; i++) {
HXML c = XmlGetChild(errorNode, i);
if (c == nullptr) break;
const wchar_t *attr = XmlGetAttrValue(c, L"xmlns");
if (attr && !mir_wstrcmp(attr, L"urn:ietf:params:xml:ns:xmpp-stanzas")) {
str = XmlGetName(c);
break;
}
}
}
if (str != nullptr)
mir_snwprintf(errorStr, 256, L"%s %d: %s\r\n%s", TranslateT("Error"), errorCode, TranslateW(JabberErrorStr(errorCode)), str);
else
mir_snwprintf(errorStr, 256, L"%s %d: %s", TranslateT("Error"), errorCode, TranslateW(JabberErrorStr(errorCode)));
if (pErrorCode)
*pErrorCode = errorCode;
return errorStr;
}
void CJabberProto::SendVisibleInvisiblePresence(bool invisible)
{
if (!m_bJabberOnline) return;
LISTFOREACH(i, this, LIST_ROSTER)
{
JABBER_LIST_ITEM *item = ListGetItemPtrFromIndex(i);
if (item == nullptr)
continue;
MCONTACT hContact = HContactFromJID(item->jid);
if (hContact == 0)
continue;
WORD apparentMode = getWord(hContact, "ApparentMode", 0);
if (invisible && apparentMode == ID_STATUS_OFFLINE)
m_ThreadInfo->send(XmlNode(L"presence") << XATTR(L"to", item->jid) << XATTR(L"type", L"invisible"));
else if (!invisible && apparentMode == ID_STATUS_ONLINE)
SendPresenceTo(m_iStatus, item->jid, nullptr);
}
}
time_t __stdcall JabberIsoToUnixTime(const wchar_t *stamp)
{
wchar_t date[9];
int i, y;
if (stamp == nullptr)
return 0;
const wchar_t *p = stamp;
// Get the date part
for (i = 0; *p != '\0' && i < 8 && isdigit(*p); p++, i++)
date[i] = *p;
// Parse year
if (i == 6) {
// 2-digit year (1970-2069)
y = (date[0] - '0') * 10 + (date[1] - '0');
if (y < 70) y += 100;
}
else if (i == 8) {
// 4-digit year
y = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + date[3] - '0';
y -= 1900;
}
else return 0;
struct tm timestamp;
timestamp.tm_year = y;
// Parse month
timestamp.tm_mon = (date[i - 4] - '0') * 10 + date[i - 3] - '0' - 1;
// Parse date
timestamp.tm_mday = (date[i - 2] - '0') * 10 + date[i - 1] - '0';
// Skip any date/time delimiter
for (; *p != '\0' && !isdigit(*p); p++);
// Parse time
if (swscanf(p, L"%d:%d:%d", ×tamp.tm_hour, ×tamp.tm_min, ×tamp.tm_sec) != 3)
return (time_t)0;
timestamp.tm_isdst = 0; // DST is already present in _timezone below
time_t t = mktime(×tamp);
_tzset();
t -= _timezone;
return (t >= 0) ? t : 0;
}
void CJabberProto::SendPresenceTo(int status, const wchar_t* to, HXML extra, const wchar_t *msg)
{
if (!m_bJabberOnline) return;
// Send <presence/> update for status (we won't handle ID_STATUS_OFFLINE here)
int iPriority = getDword("Priority", 0);
UpdatePriorityMenu(iPriority);
wchar_t szPriority[40];
_itow(iPriority, szPriority, 10);
XmlNode p(L"presence"); p << XCHILD(L"priority", szPriority);
if (to != nullptr)
p << XATTR(L"to", to);
if (extra)
XmlAddChild(p, extra);
// XEP-0115:Entity Capabilities
if (m_bAllowVersionRequests) {
HXML c = p << XCHILDNS(L"c", JABBER_FEAT_ENTITY_CAPS) << XATTR(L"hash", L"sha-1")
<< XATTR(L"node", JABBER_CAPS_MIRANDA_NODE) << XATTR(L"ver", m_clientCapsManager.GetFeaturesCrc());
LIST<wchar_t> arrExtCaps(5);
if (bSecureIM)
arrExtCaps.insert(JABBER_EXT_SECUREIM);
if (bMirOTR)
arrExtCaps.insert(JABBER_EXT_MIROTR);
if (bNewGPG)
arrExtCaps.insert(JABBER_EXT_NEWGPG);
if(m_bUseOMEMO)
arrExtCaps.insert(JABBER_EXT_OMEMO);
if (bPlatform)
arrExtCaps.insert(JABBER_EXT_PLATFORMX64);
else
arrExtCaps.insert(JABBER_EXT_PLATFORMX86);
if (m_bEnableRemoteControl)
arrExtCaps.insert(JABBER_EXT_COMMANDS);
if (m_bEnableUserMood)
arrExtCaps.insert(JABBER_EXT_USER_MOOD);
if (m_bEnableUserTune)
arrExtCaps.insert(JABBER_EXT_USER_TUNE);
if (m_bEnableUserActivity)
arrExtCaps.insert(JABBER_EXT_USER_ACTIVITY);
if (m_bAcceptNotes)
arrExtCaps.insert(JABBER_EXT_MIR_NOTES);
NotifyFastHook(hExtListInit, (WPARAM)&arrExtCaps, (LPARAM)(IJabberInterface*)this);
// add features enabled through IJabberNetInterface::AddFeatures()
for (auto &it : m_lstJabberFeatCapPairsDynamic)
if (m_uEnabledFeatCapsDynamic & it->jcbCap)
arrExtCaps.insert(it->szExt);
if (arrExtCaps.getCount()) {
CMStringW szExtCaps = arrExtCaps[0];
for (int i = 1; i < arrExtCaps.getCount(); i++) {
szExtCaps.AppendChar(' ');
szExtCaps += arrExtCaps[i];
}
XmlAddAttr(c, L"ext", szExtCaps);
}
}
if (m_tmJabberIdleStartTime)
p << XQUERY(JABBER_FEAT_LAST_ACTIVITY) << XATTRI(L"seconds", time(0) - m_tmJabberIdleStartTime);
if (m_bEnableAvatars) {
HXML x = p << XCHILDNS(L"x", L"vcard-temp:x:update");
ptrW vcardHash(getWStringA("VCardHash"));
if (vcardHash != nullptr)
x << XATTR(L"vcard", vcardHash);
ptrW hashValue(getWStringA("AvatarHash"));
if (hashValue != nullptr) // XEP-0153: vCard-Based Avatars
x << XCHILD(L"photo", hashValue);
else
x << XCHILD(L"photo");
}
{
mir_cslock lck(m_csModeMsgMutex);
switch (status) {
case ID_STATUS_ONLINE:
if (!msg) msg = m_modeMsgs.szOnline;
break;
case ID_STATUS_INVISIBLE:
p << XATTR(L"type", L"invisible");
break;
case ID_STATUS_AWAY:
case ID_STATUS_ONTHEPHONE:
case ID_STATUS_OUTTOLUNCH:
p << XCHILD(L"show", L"away");
if (!msg) msg = m_modeMsgs.szAway;
break;
case ID_STATUS_NA:
p << XCHILD(L"show", L"xa");
if (!msg) msg = m_modeMsgs.szNa;
break;
case ID_STATUS_DND:
case ID_STATUS_OCCUPIED:
p << XCHILD(L"show", L"dnd");
if (!msg) msg = m_modeMsgs.szDnd;
break;
case ID_STATUS_FREECHAT:
p << XCHILD(L"show", L"chat");
if (!msg) msg = m_modeMsgs.szFreechat;
break;
default: // Should not reach here
break;
}
if (msg)
p << XCHILD(L"status", msg);
}
m_ThreadInfo->send(p);
}
void CJabberProto::SendPresence(int status, bool bSendToAll)
{
SendPresenceTo(status, nullptr, nullptr);
SendVisibleInvisiblePresence(status == ID_STATUS_INVISIBLE);
// Also update status in all chatrooms
if (bSendToAll) {
LISTFOREACH(i, this, LIST_CHATROOM)
{
JABBER_LIST_ITEM *item = ListGetItemPtrFromIndex(i);
if (item != nullptr && item->nick != nullptr) {
wchar_t text[1024];
mir_snwprintf(text, L"%s/%s", item->jid, item->nick);
SendPresenceTo(status == ID_STATUS_INVISIBLE ? ID_STATUS_ONLINE : status, text, nullptr);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// JabberGetPacketID - converts the xml id attribute into an integer
int __stdcall JabberGetPacketID(HXML n)
{
const wchar_t *str = XmlGetAttrValue(n, L"id");
if (str)
if (!wcsncmp(str, _T(JABBER_IQID), _countof(JABBER_IQID) - 1))
return _wtoi(str + _countof(JABBER_IQID) - 1);
return -1;
}
wchar_t* __stdcall JabberId2string(int id)
{
wchar_t text[100];
mir_snwprintf(text, _T(JABBER_IQID) L"%d", id);
return mir_wstrdup(text);
}
///////////////////////////////////////////////////////////////////////////////
// JabberGetClientJID - adds a resource postfix to a JID
wchar_t* CJabberProto::GetClientJID(MCONTACT hContact, wchar_t *dest, size_t destLen)
{
if (hContact == 0)
return nullptr;
ptrW jid(getWStringA(hContact, "jid"));
return GetClientJID(jid, dest, destLen);
}
wchar_t* CJabberProto::GetClientJID(const wchar_t *jid, wchar_t *dest, size_t destLen)
{
if (jid == nullptr)
return nullptr;
wcsncpy_s(dest, destLen, jid, _TRUNCATE);
wchar_t *p = wcschr(dest, '/');
mir_cslock lck(m_csLists);
JABBER_LIST_ITEM *LI = ListGetItemPtr(LIST_ROSTER, jid);
if (LI != nullptr) {
if (p == nullptr) {
pResourceStatus r(LI->getBestResource());
if (r != nullptr)
mir_snwprintf(dest, destLen, L"%s/%s", jid, r->m_tszResourceName);
}
}
return dest;
}
///////////////////////////////////////////////////////////////////////////////
// JabberStripJid - strips a resource postfix from a JID
wchar_t* __stdcall JabberStripJid(const wchar_t *jid, wchar_t *dest, size_t destLen)
{
if (jid == nullptr)
*dest = 0;
else {
wcsncpy_s(dest, destLen, jid, _TRUNCATE);
wchar_t *p = wcschr(dest, '/');
if (p != nullptr)
*p = 0;
}
return dest;
}
/////////////////////////////////////////////////////////////////////////////////////////
// TStringPairs class members
TStringPairs::TStringPairs(char* buffer) :
elems(nullptr)
{
TStringPairsElem tempElem[100];
char* token = strtok(buffer, ",");
for (numElems = 0; token != nullptr; numElems++) {
char* p = strchr(token, '='), *p1;
if (p == nullptr)
break;
while (isspace(*token))
token++;
tempElem[numElems].name = rtrim(token);
*p++ = 0;
if ((p1 = strchr(p, '\"')) != nullptr) {
*p1 = 0;
p = p1 + 1;
}
if ((p1 = strrchr(p, '\"')) != nullptr)
*p1 = 0;
tempElem[numElems].value = rtrim(p);
token = strtok(nullptr, ",");
}
if (numElems) {
elems = new TStringPairsElem[numElems];
memcpy(elems, tempElem, sizeof(tempElem[0]) * numElems);
}
}
TStringPairs::~TStringPairs()
{
delete[] elems;
}
const char* TStringPairs::operator[](const char* key) const
{
for (int i = 0; i < numElems; i++)
if (!mir_strcmp(elems[i].name, key))
return elems[i].value;
return "";
}
////////////////////////////////////////////////////////////////////////
// Manage combo boxes with recent item list
void CJabberProto::ComboLoadRecentStrings(HWND hwndDlg, UINT idcCombo, char *param, int recentCount)
{
for (int i = 0; i < recentCount; i++) {
char setting[MAXMODULELABELLENGTH];
mir_snprintf(setting, "%s%d", param, i);
ptrW tszRecent(getWStringA(setting));
if (tszRecent != nullptr)
SendDlgItemMessage(hwndDlg, idcCombo, CB_ADDSTRING, 0, tszRecent);
}
if (!SendDlgItemMessage(hwndDlg, idcCombo, CB_GETCOUNT, 0, 0))
SendDlgItemMessage(hwndDlg, idcCombo, CB_ADDSTRING, 0, (LPARAM)L"");
}
void CJabberProto::ComboAddRecentString(HWND hwndDlg, UINT idcCombo, char *param, const wchar_t *string, int recentCount)
{
if (!string || !*string)
return;
if (SendDlgItemMessage(hwndDlg, idcCombo, CB_FINDSTRING, (WPARAM)-1, (LPARAM)string) != CB_ERR)
return;
int id;
SendDlgItemMessage(hwndDlg, idcCombo, CB_ADDSTRING, 0, (LPARAM)string);
if ((id = SendDlgItemMessage(hwndDlg, idcCombo, CB_FINDSTRING, (WPARAM)-1, (LPARAM)L"")) != CB_ERR)
SendDlgItemMessage(hwndDlg, idcCombo, CB_DELETESTRING, id, 0);
id = getByte(param, 0);
char setting[MAXMODULELABELLENGTH];
mir_snprintf(setting, "%s%d", param, id);
setWString(setting, string);
setByte(param, (id + 1) % recentCount);
}
/////////////////////////////////////////////////////////////////////////////////////////
// jabber frame maintenance code
static VOID CALLBACK sttRebuildInfoFrameApcProc(void* param)
{
CJabberProto *ppro = (CJabberProto *)param;
if (!ppro->m_pInfoFrame)
return;
ppro->m_pInfoFrame->LockUpdates();
if (!ppro->m_bJabberOnline) {
ppro->m_pInfoFrame->RemoveInfoItem("$/PEP");
ppro->m_pInfoFrame->RemoveInfoItem("$/Transports");
ppro->m_pInfoFrame->UpdateInfoItem("$/JID", Skin_GetIconHandle(SKINICON_OTHER_USERDETAILS), TranslateT("Offline"));
}
else {
ppro->m_pInfoFrame->UpdateInfoItem("$/JID", Skin_GetIconHandle(SKINICON_OTHER_USERDETAILS), ppro->m_szJabberJID);
if (!ppro->m_bPepSupported)
ppro->m_pInfoFrame->RemoveInfoItem("$/PEP");
else {
ppro->m_pInfoFrame->RemoveInfoItem("$/PEP/");
ppro->m_pInfoFrame->CreateInfoItem("$/PEP", false);
ppro->m_pInfoFrame->UpdateInfoItem("$/PEP", ppro->GetIconHandle(IDI_PL_LIST_ANY), TranslateT("Advanced Status"));
ppro->m_pInfoFrame->CreateInfoItem("$/PEP/mood", true);
ppro->m_pInfoFrame->SetInfoItemCallback("$/PEP/mood", &CJabberProto::InfoFrame_OnUserMood);
ppro->m_pInfoFrame->UpdateInfoItem("$/PEP/mood", Skin_GetIconHandle(SKINICON_OTHER_SMALLDOT), TranslateT("Set mood..."));
ppro->m_pInfoFrame->CreateInfoItem("$/PEP/activity", true);
ppro->m_pInfoFrame->SetInfoItemCallback("$/PEP/activity", &CJabberProto::InfoFrame_OnUserActivity);
ppro->m_pInfoFrame->UpdateInfoItem("$/PEP/activity", Skin_GetIconHandle(SKINICON_OTHER_SMALLDOT), TranslateT("Set activity..."));
}
ppro->m_pInfoFrame->RemoveInfoItem("$/Transports/");
ppro->m_pInfoFrame->CreateInfoItem("$/Transports", false);
ppro->m_pInfoFrame->UpdateInfoItem("$/Transports", ppro->GetIconHandle(IDI_TRANSPORT), TranslateT("Transports"));
JABBER_LIST_ITEM *item = nullptr;
LISTFOREACH(i, ppro, LIST_ROSTER)
{
if ((item = ppro->ListGetItemPtrFromIndex(i)) != nullptr) {
if (wcschr(item->jid, '@') == nullptr && wcschr(item->jid, '/') == nullptr && item->subscription != SUB_NONE) {
MCONTACT hContact = ppro->HContactFromJID(item->jid);
if (hContact == 0)
continue;
char name[128];
char *jid_copy = mir_u2a(item->jid);
mir_snprintf(name, "$/Transports/%s", jid_copy);
ppro->m_pInfoFrame->CreateInfoItem(name, true, hContact);
ppro->m_pInfoFrame->UpdateInfoItem(name, ppro->GetIconHandle(IDI_TRANSPORTL), (wchar_t *)item->jid);
ppro->m_pInfoFrame->SetInfoItemCallback(name, &CJabberProto::InfoFrame_OnTransport);
mir_free(jid_copy);
}
}
}
}
ppro->m_pInfoFrame->Update();
}
void CJabberProto::RebuildInfoFrame()
{
if (!m_bShutdown)
CallFunctionAsync(sttRebuildInfoFrameApcProc, this);
}
////////////////////////////////////////////////////////////////////////
// time2str & str2time
wchar_t* time2str(time_t _time, wchar_t *buf, size_t bufLen)
{
struct tm* T = gmtime(&_time);
mir_snwprintf(buf, bufLen, L"%04d-%02d-%02dT%02d:%02d:%02dZ",
T->tm_year + 1900, T->tm_mon + 1, T->tm_mday, T->tm_hour, T->tm_min, T->tm_sec);
return buf;
}
time_t str2time(const wchar_t *buf)
{
struct tm T = { 0 };
if (swscanf(buf, L"%04d-%02d-%02dT%02d:%02d:%02dZ", &T.tm_year, &T.tm_mon, &T.tm_mday, &T.tm_hour, &T.tm_min, &T.tm_sec) != 6) {
int boo;
if (swscanf(buf, L"%04d-%02d-%02dT%02d:%02d:%02d.%dZ", &T.tm_year, &T.tm_mon, &T.tm_mday, &T.tm_hour, &T.tm_min, &T.tm_sec, &boo) != 7)
return 0;
}
T.tm_year -= 1900;
T.tm_mon--;
return _mkgmtime(&T);
}
////////////////////////////////////////////////////////////////////////
// case-insensitive wcsstr
const wchar_t *JabberStrIStr(const wchar_t *str, const wchar_t *substr)
{
wchar_t *str_up = NEWWSTR_ALLOCA(str);
wchar_t *substr_up = NEWWSTR_ALLOCA(substr);
CharUpperBuff(str_up, (DWORD)mir_wstrlen(str_up));
CharUpperBuff(substr_up, (DWORD)mir_wstrlen(substr_up));
wchar_t *p = wcsstr(str_up, substr_up);
return p ? (str + (p - str_up)) : nullptr;
}
////////////////////////////////////////////////////////////////////////
// clipboard processing
void JabberCopyText(HWND hwnd, const wchar_t *text)
{
if (!hwnd || !text) return;
if (OpenClipboard(hwnd)) {
EmptyClipboard();
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t)*(mir_wstrlen(text) + 1));
wchar_t *s = (wchar_t *)GlobalLock(hMem);
mir_wstrcpy(s, text);
GlobalUnlock(hMem);
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
}
}
BOOL CJabberProto::EnterString(CMStringW &result, const wchar_t *caption, int type, char *windowName, int recentCount, int timeout)
{
if (caption == nullptr) {
caption = NEWWSTR_ALLOCA(result.GetString());
result.Empty();
}
ENTER_STRING param = { sizeof(param) };
param.type = type;
param.caption = caption;
param.szModuleName = m_szModuleName;
param.szDataPrefix = windowName;
param.recentCount = recentCount;
param.timeout = timeout;
param.ptszInitVal = result;
BOOL res = ::EnterString(¶m);
if (res) {
result = param.ptszResult;
mir_free(param.ptszResult);
}
return res;
}
/////////////////////////////////////////////////////////////////////////////////////////
// XEP-0203 delay support
bool JabberReadXep203delay(HXML node, time_t &msgTime)
{
HXML n = XmlGetChildByTag(node, "delay", "xmlns", L"urn:xmpp:delay");
if (n == nullptr)
return false;
const wchar_t *ptszTimeStamp = XmlGetAttrValue(n, L"stamp");
if (ptszTimeStamp == nullptr)
return false;
// skip '-' chars
wchar_t *szStamp = NEWWSTR_ALLOCA(ptszTimeStamp);
int si = 0, sj = 0;
while (true) {
if (szStamp[si] == '-')
si++;
else if (!(szStamp[sj++] = szStamp[si++]))
break;
};
msgTime = JabberIsoToUnixTime(szStamp);
return msgTime != 0;
}
bool CJabberProto::IsMyOwnJID(const wchar_t *szJID)
{
if (m_ThreadInfo == nullptr)
return false;
ptrW szFrom(JabberPrepareJid(szJID));
if (szFrom == nullptr)
return false;
ptrW szTo(JabberPrepareJid(m_ThreadInfo->fullJID));
if (szTo == nullptr)
return false;
wchar_t *pDelimiter = wcschr(szFrom, '/');
if (pDelimiter)
*pDelimiter = 0;
pDelimiter = wcschr(szTo, '/');
if (pDelimiter)
*pDelimiter = 0;
return mir_wstrcmp(szFrom, szTo) == 0;
}
void __cdecl CJabberProto::LoadHttpAvatars(void* param)
{
Thread_SetName("Jabber: LoadHttpAvatars");
OBJLIST<JABBER_HTTP_AVATARS> &avs = *(OBJLIST<JABBER_HTTP_AVATARS>*)param;
HNETLIBCONN hHttpCon = nullptr;
for (auto &it : avs) {
NETLIBHTTPREQUEST nlhr = { 0 };
nlhr.cbSize = sizeof(nlhr);
nlhr.requestType = REQUEST_GET;
nlhr.flags = NLHRF_HTTP11 | NLHRF_REDIRECT | NLHRF_PERSISTENT;
nlhr.szUrl = it->Url;
nlhr.nlc = hHttpCon;
NETLIBHTTPREQUEST *res = Netlib_HttpTransaction(m_hNetlibUser, &nlhr);
if (res) {
hHttpCon = res->nlc;
if (res->resultCode == 200 && res->dataLength) {
int pictureType = ProtoGetBufferFormat(res->pData);
if (pictureType != PA_FORMAT_UNKNOWN) {
PROTO_AVATAR_INFORMATION ai;
ai.format = pictureType;
ai.hContact = it->hContact;
if (getByte(ai.hContact, "AvatarType", PA_FORMAT_UNKNOWN) != (unsigned char)pictureType) {
wchar_t tszFileName[MAX_PATH];
GetAvatarFileName(ai.hContact, tszFileName, _countof(tszFileName));
DeleteFile(tszFileName);
}
setByte(ai.hContact, "AvatarType", pictureType);
char buffer[2 * MIR_SHA1_HASH_SIZE + 1];
BYTE digest[MIR_SHA1_HASH_SIZE];
mir_sha1_ctx sha;
mir_sha1_init(&sha);
mir_sha1_append(&sha, (BYTE*)res->pData, res->dataLength);
mir_sha1_finish(&sha, digest);
bin2hex(digest, sizeof(digest), buffer);
ptrA cmpsha(getStringA(ai.hContact, "AvatarSaved"));
if (cmpsha == nullptr || strnicmp(cmpsha, buffer, sizeof(buffer))) {
wchar_t tszFileName[MAX_PATH];
GetAvatarFileName(ai.hContact, tszFileName, _countof(tszFileName));
wcsncpy_s(ai.filename, tszFileName, _TRUNCATE);
FILE* out = _wfopen(tszFileName, L"wb");
if (out != nullptr) {
fwrite(res->pData, res->dataLength, 1, out);
fclose(out);
setString(ai.hContact, "AvatarSaved", buffer);
ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai, 0);
debugLogW(L"Broadcast new avatar: %s", ai.filename);
}
else ProtoBroadcastAck(ai.hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, &ai, 0);
}
}
}
Netlib_FreeHttpRequest(res);
}
else hHttpCon = nullptr;
}
delete &avs;
if (hHttpCon)
Netlib_CloseHandle(hHttpCon);
}
/////////////////////////////////////////////////////////////////////////////////////////
// UI utilities
int UIEmulateBtnClick(HWND hwndDlg, UINT idcButton)
{
if (IsWindowEnabled(GetDlgItem(hwndDlg, idcButton)))
PostMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(idcButton, BN_CLICKED), (LPARAM)GetDlgItem(hwndDlg, idcButton));
return 0;
}
void UIShowControls(HWND hwndDlg, int *idList, int nCmdShow)
{
for (; *idList; ++idList)
ShowWindow(GetDlgItem(hwndDlg, *idList), nCmdShow);
}
|