summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_svc.cpp
blob: 6c2bdb3d129394c444a941d9e294c4d68a7abb4d (plain)
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
/*

Jabber Protocol Plugin for Miranda IM
Copyright (C) 2002-04  Santithorn Bunchua
Copyright (C) 2005-12  George Hazan
Copyright (C) 2007     Maxim Mluhov
Copyright (C) 2012-13  Miranda NG Project

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 "jabber.h"

#include <fcntl.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "jabber_list.h"
#include "jabber_iq.h"
#include "jabber_caps.h"
#include "m_addcontact.h"
#include "jabber_disco.h"

/////////////////////////////////////////////////////////////////////////////////////////
// GetMyAwayMsg - obtain the current away message

INT_PTR __cdecl CJabberProto::GetMyAwayMsg(WPARAM wParam, LPARAM lParam)
{
	TCHAR *szStatus = NULL;
	INT_PTR nRetVal = 0;

	mir_cslock lck(m_csModeMsgMutex);
	switch (wParam ? (int)wParam : m_iStatus) {
	case ID_STATUS_ONLINE:
		szStatus = m_modeMsgs.szOnline;
		break;
	case ID_STATUS_AWAY:
	case ID_STATUS_ONTHEPHONE:
	case ID_STATUS_OUTTOLUNCH:
		szStatus = m_modeMsgs.szAway;
		break;
	case ID_STATUS_NA:
		szStatus = m_modeMsgs.szNa;
		break;
	case ID_STATUS_DND:
	case ID_STATUS_OCCUPIED:
		szStatus = m_modeMsgs.szDnd;
		break;
	case ID_STATUS_FREECHAT:
		szStatus = m_modeMsgs.szFreechat;
		break;
	default: // Should not reach here
		break;
	}
	if (szStatus)
		nRetVal = (lParam & SGMA_UNICODE) ? (INT_PTR)mir_t2u(szStatus) : (INT_PTR)mir_t2a(szStatus);

	return nRetVal;
}

/////////////////////////////////////////////////////////////////////////////////////////
// JabberGetAvatar - retrieves the file name of my own avatar

INT_PTR __cdecl CJabberProto::JabberGetAvatar(WPARAM wParam, LPARAM lParam)
{
	TCHAR *buf = (TCHAR*)wParam;
	int  size = (int)lParam;

	if (buf == NULL || size <= 0)
		return -1;

	if ( !m_options.EnableAvatars)
		return -2;

	GetAvatarFileName(NULL, buf, size);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// JabberGetAvatarCaps - returns directives how to process avatars

INT_PTR __cdecl CJabberProto::JabberGetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
	switch(wParam) {
	case AF_MAXSIZE:
		{
			POINT* size = (POINT*)lParam;
			if (size)
				size->x = size->y = 96;
		}
      return 0;

	case AF_PROPORTION:
		return PIP_NONE;

	case AF_FORMATSUPPORTED: // Jabber supports avatars of virtually all formats
		return 1;

	case AF_ENABLED:
		return m_options.EnableAvatars;
	}
	return -1;
}

/////////////////////////////////////////////////////////////////////////////////////////
// JabberGetAvatarInfo - retrieves the avatar info

INT_PTR __cdecl CJabberProto::JabberGetAvatarInfo(WPARAM wParam, LPARAM lParam)
{
	if ( !m_options.EnableAvatars)
		return GAIR_NOAVATAR;

	PROTO_AVATAR_INFORMATIONT* AI = (PROTO_AVATAR_INFORMATIONT*)lParam;

	ptrA szHashValue( getStringA(AI->hContact, "AvatarHash"));
	if (szHashValue == NULL) {
		Log("No avatar");
		return GAIR_NOAVATAR;
	}

	TCHAR tszFileName[ MAX_PATH ];
	GetAvatarFileName(AI->hContact, tszFileName, SIZEOF(tszFileName));
	_tcsncpy(AI->filename, tszFileName, SIZEOF(AI->filename));

	AI->format = (AI->hContact == NULL) ? PA_FORMAT_PNG : getByte(AI->hContact, "AvatarType", 0);

	if (::_taccess(AI->filename, 0) == 0) {
		ptrA szSavedHash( getStringA(AI->hContact, "AvatarSaved"));
		if (szSavedHash != NULL && !strcmp(szSavedHash, szHashValue)) {
			Log("Avatar is Ok: %s == %s", szSavedHash, szHashValue);
			return GAIR_SUCCESS;
		}
	}

	if ((wParam & GAIF_FORCE) != 0 && AI->hContact != NULL && m_bJabberOnline) {
		DBVARIANT dbv;
		if ( !getTString(AI->hContact, "jid", &dbv)) {
			JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_ROSTER, dbv.ptszVal);
			if (item != NULL) {
				BOOL isXVcard = getByte(AI->hContact, "AvatarXVcard", 0);

				TCHAR szJid[JABBER_MAX_JID_LEN];
				if (item->arResources.getCount() != NULL && !isXVcard) {
					TCHAR *bestResName = ListGetBestClientResourceNamePtr(dbv.ptszVal);
					mir_sntprintf(szJid, SIZEOF(szJid), bestResName?_T("%s/%s"):_T("%s"), dbv.ptszVal, bestResName);
				}
				else lstrcpyn(szJid, dbv.ptszVal, SIZEOF(szJid));

				Log("Rereading %s for %S", isXVcard ? JABBER_FEAT_VCARD_TEMP : JABBER_FEAT_AVATAR, szJid);

				int iqId = SerialNext();
				if (isXVcard)
					IqAdd(iqId, IQ_PROC_NONE, &CJabberProto::OnIqResultGetVCardAvatar);
				else
					IqAdd(iqId, IQ_PROC_NONE, &CJabberProto::OnIqResultGetClientAvatar);

				XmlNodeIq iq(_T("get"), iqId, szJid);
				if (isXVcard)
					iq << XCHILDNS(_T("vCard"), JABBER_FEAT_VCARD_TEMP);
				else
					iq << XQUERY(isXVcard ? _T("") : JABBER_FEAT_AVATAR);
				m_ThreadInfo->send(iq);

				db_free(&dbv);
				return GAIR_WAITFOR;
			}
			db_free(&dbv);
		}
	}

	Log("No avatar");
	return GAIR_NOAVATAR;
}

////////////////////////////////////////////////////////////////////////////////////////
// JabberGetEventTextChatStates - retrieves a chat state description from an event

INT_PTR __cdecl CJabberProto::OnGetEventTextChatStates(WPARAM, LPARAM lParam)
{
	DBEVENTGETTEXT *pdbEvent = (DBEVENTGETTEXT *)lParam;

	INT_PTR nRetVal = 0;

	if (pdbEvent->dbei->cbBlob > 0) {
		if (pdbEvent->dbei->pBlob[0] == JABBER_DB_EVENT_CHATSTATES_GONE) {
			if (pdbEvent->datatype == DBVT_WCHAR)
				nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("closed chat session")));
			else if (pdbEvent->datatype == DBVT_ASCIIZ)
				nRetVal = (INT_PTR)mir_strdup(Translate("closed chat session"));
		}
	}

	return nRetVal;
}

////////////////////////////////////////////////////////////////////////////////////////
// OnGetEventTextPresence - retrieves presence state description from an event

INT_PTR __cdecl CJabberProto::OnGetEventTextPresence(WPARAM, LPARAM lParam)
{
	DBEVENTGETTEXT *pdbEvent = (DBEVENTGETTEXT *)lParam;

	INT_PTR nRetVal = 0;

	if (pdbEvent->dbei->cbBlob > 0) {
		switch (pdbEvent->dbei->pBlob[0])
		{
		case JABBER_DB_EVENT_PRESENCE_SUBSCRIBE:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("sent subscription request")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("sent subscription request"));
				break;
			}
		case JABBER_DB_EVENT_PRESENCE_SUBSCRIBED:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("approved subscription request")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("approved subscription request"));
				break;
			}
		case JABBER_DB_EVENT_PRESENCE_UNSUBSCRIBE:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("declined subscription")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("declined subscription"));
				break;
			}
		case JABBER_DB_EVENT_PRESENCE_UNSUBSCRIBED:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("declined subscription")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("declined subscription"));
				break;
			}
		case JABBER_DB_EVENT_PRESENCE_ERROR:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("sent error presence")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("sent error presence"));
				break;
			}
		default:
			{
				if (pdbEvent->datatype == DBVT_WCHAR)
					nRetVal = (INT_PTR)mir_tstrdup(TranslateTS(_T("sent unknown presence type")));
				else if (pdbEvent->datatype == DBVT_ASCIIZ)
					nRetVal = (INT_PTR)mir_strdup(Translate("sent unknown presence type"));
				break;
			}
		}
	}

	return nRetVal;
}

////////////////////////////////////////////////////////////////////////////////////////
// JabberSetAvatar - sets an avatar without UI

INT_PTR __cdecl CJabberProto::JabberSetAvatar(WPARAM, LPARAM lParam)
{
	TCHAR *tszFileName = (TCHAR*)lParam;

	if (m_bJabberOnline) {
		SetServerVcard(TRUE, tszFileName);
		SendPresence(m_iDesiredStatus, false);
	}
	else if (tszFileName == NULL || tszFileName[0] == 0) {
		// Remove avatar
		TCHAR tFileName[ MAX_PATH ];
		GetAvatarFileName(NULL, tFileName, MAX_PATH);
		DeleteFile(tFileName);

		delSetting("AvatarSaved");
		delSetting("AvatarHash");
	}
	else {
		int fileIn = _topen(tszFileName, O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
		if (fileIn == -1) {
			mir_free(tszFileName);
			return 1;
		}

		long  dwPngSize = _filelength(fileIn);
		char* pResult = new char[ dwPngSize ];
		if (pResult == NULL) {
			_close(fileIn);
			mir_free(tszFileName);
			return 2;
		}

		_read(fileIn, pResult, dwPngSize);
		_close(fileIn);

		BYTE digest[MIR_SHA1_HASH_SIZE];
		mir_sha1_ctx sha1ctx;
		mir_sha1_init(&sha1ctx);
		mir_sha1_append(&sha1ctx, (BYTE*)pResult, dwPngSize);
		mir_sha1_finish(&sha1ctx, digest);

		TCHAR tFileName[ MAX_PATH ];
		GetAvatarFileName(NULL, tFileName, MAX_PATH);
		DeleteFile(tFileName);

		char buf[MIR_SHA1_HASH_SIZE*2+1];
		bin2hex(digest, sizeof(digest), buf);

		m_options.AvatarType = ProtoGetBufferFormat(pResult);

		GetAvatarFileName(NULL, tFileName, MAX_PATH);
		FILE* out = _tfopen(tFileName, _T("wb"));
		if (out != NULL) {
			fwrite(pResult, dwPngSize, 1, out);
			fclose(out);
		}
		delete[] pResult;

		setString("AvatarSaved", buf);
	}

	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////
// JabberSetNickname - sets the user nickname without UI

INT_PTR __cdecl CJabberProto::JabberSetNickname(WPARAM wParam, LPARAM lParam)
{
	TCHAR *nickname = (wParam & SMNN_UNICODE) ? mir_u2t((WCHAR *) lParam) : mir_a2t((char *) lParam);

	setTString("Nick", nickname);
	SetServerVcard(FALSE, _T(""));
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// "/SendXML" - Allows external plugins to send XML to the server

INT_PTR __cdecl CJabberProto::ServiceSendXML(WPARAM, LPARAM lParam)
{
	return m_ThreadInfo->send((char*)lParam);
}

/////////////////////////////////////////////////////////////////////////////////////////
// "/GCGetToolTipText" - gets tooltip text

static const TCHAR * JabberEnum2AffilationStr[]={ _T("None"), _T("Outcast"), _T("Member"), _T("Admin"), _T("Owner") };
static const TCHAR * JabberEnum2RoleStr[]={ _T("None"), _T("Visitor"), _T("Participant"), _T("Moderator") };

static void appendString(bool bIsTipper, const TCHAR *tszTitle, const TCHAR *tszValue, TCHAR* buf, size_t bufSize)
{
	if (*buf) {
		const TCHAR *szSeparator = bIsTipper ? _T("\n") : _T("\r\n");
 		_tcsncat(buf, szSeparator, bufSize);
	}

	size_t len = _tcslen(buf);
	buf += len;
	bufSize -= len;

	if (bIsTipper)
		mir_sntprintf(buf, bufSize, _T("%s%s%s%s"), _T("<b>"), TranslateTS(tszTitle), _T("</b>\t"), tszValue);
	else {
		TCHAR *p = TranslateTS(tszTitle);
		mir_sntprintf(buf, bufSize, _T("%s%s\t%s"), p, _tcslen(p)<=7 ? _T("\t") : _T(""), tszValue);
	}
}

INT_PTR __cdecl CJabberProto::JabberGCGetToolTipText(WPARAM wParam, LPARAM lParam)
{
	if ( !wParam || !lParam)
		return 0; //room global tooltip not supported yet

	JABBER_LIST_ITEM *item = ListGetItemPtr(LIST_CHATROOM, (TCHAR*)wParam);
	if (item == NULL)
		return 0;  //no room found

	pResourceStatus info( item->findResource((TCHAR*)lParam));
	if (info == NULL)
		return 0; //no info found

	// ok process info output will be:
	// JID:			real@jid/resource or
	// Nick:		Nickname
	// Status:		StatusText
	// Role:		Moderator
	// Affiliation:  Affiliation

	TCHAR outBuf[2048];
	outBuf[0]=_T('\0');

	bool bIsTipper = db_get_b(NULL, "Tab_SRMsg", "adv_TipperTooltip", 0) && ServiceExists("mToolTip/HideTip");

	//JID:
	if (_tcschr(info->m_tszResourceName, _T('@')) != NULL)
		appendString(bIsTipper, _T("JID:"), info->m_tszResourceName, outBuf, SIZEOF(outBuf));
	else if (lParam) //or simple nick
		appendString(bIsTipper, _T("Nick:"), (TCHAR*) lParam, outBuf, SIZEOF(outBuf));

	// status
	if (info->m_iStatus >= ID_STATUS_OFFLINE && info->m_iStatus <= ID_STATUS_IDLE )
		appendString(bIsTipper, _T("Status:"), pcli->pfnGetStatusModeDescription(info->m_iStatus, 0), outBuf, SIZEOF(outBuf));

	// status text
	if (info->m_tszStatusMessage)
		appendString(bIsTipper, _T("Status text:"), info->m_tszStatusMessage, outBuf, SIZEOF(outBuf));

	// Role
	appendString(bIsTipper, _T("Role:"), TranslateTS(JabberEnum2RoleStr[info->m_role]), outBuf, SIZEOF(outBuf));

	// Affiliation
	appendString(bIsTipper, _T("Affiliation:"), TranslateTS(JabberEnum2AffilationStr[info->m_affiliation]), outBuf, SIZEOF(outBuf));

	// real jid
	if (info->m_tszRealJid)
		appendString(bIsTipper, _T("Real JID:"), info->m_tszRealJid, outBuf, SIZEOF(outBuf));

	return (INT_PTR)(outBuf[0] == 0 ? NULL : mir_tstrdup(outBuf));
}

// File Association Manager plugin support

INT_PTR __cdecl CJabberProto::JabberServiceParseXmppURI(WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(wParam);

	TCHAR *arg = (TCHAR *)lParam;
	if (arg == NULL)
		return 1;

	TCHAR szUri[ 1024 ];
	mir_sntprintf(szUri, SIZEOF(szUri), _T("%s"), arg);

	TCHAR *szJid = szUri;

	// skip leading prefix
	szJid = _tcschr(szJid, _T(':'));
	if (szJid == NULL)
		return 1;

	// skip //
	for (++szJid; *szJid == _T('/'); ++szJid);

	// empty jid?
	if ( !*szJid)
		return 1;

	// command code
	TCHAR *szCommand = szJid;
	szCommand = _tcschr(szCommand, _T('?'));
	if (szCommand)
		*(szCommand++) = 0;

	// parameters
	TCHAR *szSecondParam = szCommand ? _tcschr(szCommand, _T(';')) : NULL;
	if (szSecondParam)
		*(szSecondParam++) = 0;

//	TCHAR *szThirdParam = szSecondParam ? _tcschr(szSecondParam, _T(';')) : NULL;
//	if (szThirdParam)
//		*(szThirdParam++) = 0;

	// no command or message command
	if ( !szCommand || (szCommand && !_tcsicmp(szCommand, _T("message")))) {
		// message
		if (ServiceExists(MS_MSG_SENDMESSAGE)) {
			HANDLE hContact = HContactFromJID(szJid, TRUE);
            TCHAR *szMsgBody = NULL;
			if ( !hContact)
				hContact = DBCreateContact(szJid, szJid, TRUE, TRUE);
			if ( !hContact)
				return 1;

			if (szSecondParam) { //there are parameters to message
				szMsgBody = _tcsstr(szSecondParam, _T("body="));
				if (szMsgBody) {
					szMsgBody += 5;
					TCHAR *szDelim = _tcschr(szMsgBody, _T(';'));
					if (szDelim)
						szDelim = 0;
					JabberHttpUrlDecode(szMsgBody);
			}	}

			CallService(MS_MSG_SENDMESSAGE "W",(WPARAM)hContact, (LPARAM)szMsgBody);

			return 0;
		}
		return 1;
	}
	else if ( !_tcsicmp(szCommand, _T("roster")))
	{
		if ( !HContactFromJID(szJid)) {
			JABBER_SEARCH_RESULT jsr = { 0 };
			jsr.hdr.cbSize = sizeof(JABBER_SEARCH_RESULT);
			jsr.hdr.flags = PSR_TCHAR;
			jsr.hdr.nick = szJid;
			jsr.hdr.id = szJid;
			_tcsncpy(jsr.jid, szJid, SIZEOF(jsr.jid) - 1);

			ADDCONTACTSTRUCT acs;
			acs.handleType = HANDLE_SEARCHRESULT;
			acs.szProto = m_szModuleName;
			acs.psr = &jsr.hdr;
			CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
		}
		return 0;
	}
	else if ( !_tcsicmp(szCommand, _T("join")))
	{
		// chat join invitation
		GroupchatJoinRoomByJid(NULL, szJid);
		return 0;
	}
	else if ( !_tcsicmp(szCommand, _T("disco")))
	{
		// service discovery request
		OnMenuHandleServiceDiscovery(0, (LPARAM)szJid);
		return 0;
	}
	else if ( !_tcsicmp(szCommand, _T("command")))
	{
		// ad-hoc commands
		if (szSecondParam) {
			if ( !_tcsnicmp(szSecondParam, _T("node="), 5)) {
				szSecondParam += 5;
				if ( !*szSecondParam)
					szSecondParam = NULL;
			}
			else
				szSecondParam = NULL;
		}
		CJabberAdhocStartupParams* pStartupParams = new CJabberAdhocStartupParams(this, szJid, szSecondParam);
		ContactMenuRunCommands(0, (LPARAM)pStartupParams);
		return 0;
	}
	else if ( !_tcsicmp(szCommand, _T("sendfile")))
	{
		// send file
		if (ServiceExists(MS_FILE_SENDFILE)) {
			HANDLE hContact = HContactFromJID(szJid, TRUE);
			if ( !hContact)
				hContact = DBCreateContact(szJid, szJid, TRUE, TRUE);
			if ( !hContact)
				return 1;
			CallService(MS_FILE_SENDFILE, (WPARAM)hContact, (LPARAM)NULL);
			return 0;
		}
		return 1;
	}

	return 1; /* parse failed */
}

// XEP-0224 support (Attention/Nudge)
INT_PTR __cdecl CJabberProto::JabberSendNudge(WPARAM wParam, LPARAM)
{
	if ( !m_bJabberOnline)
		return 0;

	HANDLE hContact = (HANDLE)wParam;
	DBVARIANT dbv;
	if ( !getTString(hContact, "jid", &dbv)) {
		TCHAR tszJid[JABBER_MAX_JID_LEN];
		TCHAR *szResource = ListGetBestClientResourceNamePtr(dbv.ptszVal);
		if (szResource)
			mir_sntprintf(tszJid, SIZEOF(tszJid), _T("%s/%s"), dbv.ptszVal, szResource);
		else
			mir_sntprintf(tszJid, SIZEOF(tszJid), _T("%s"), dbv.ptszVal);
		db_free(&dbv);

		JabberCapsBits jcb = GetResourceCapabilites(tszJid, FALSE);

		m_ThreadInfo->send(
			XmlNode(_T("message")) << XATTR(_T("type"), _T("headline")) << XATTR(_T("to"), tszJid)
				<< XCHILDNS(_T("attention"),
				jcb & JABBER_CAPS_ATTENTION ? JABBER_FEAT_ATTENTION : JABBER_FEAT_ATTENTION_0));
	}
	return 0;
}

BOOL CJabberProto::SendHttpAuthReply(CJabberHttpAuthParams *pParams, BOOL bAuthorized)
{
	if ( !m_bJabberOnline || !pParams || !m_ThreadInfo)
		return FALSE;

	if (pParams->m_nType == CJabberHttpAuthParams::IQ) {
		XmlNodeIq iq(bAuthorized ? _T("result") : _T("error"), pParams->m_szIqId, pParams->m_szFrom);
		if ( !bAuthorized) {
			iq << XCHILDNS(_T("confirm"), JABBER_FEAT_HTTP_AUTH) << XATTR(_T("id"), pParams->m_szId)
					<< XATTR(_T("method"), pParams->m_szMethod) << XATTR(_T("url"), pParams->m_szUrl);
			iq << XCHILD(_T("error")) << XATTRI(_T("code"), 401) << XATTR(_T("type"), _T("auth"))
					<< XCHILDNS(_T("not-authorized"), _T("urn:ietf:params:xml:xmpp-stanzas"));
		}
		m_ThreadInfo->send(iq);
	}
	else if (pParams->m_nType == CJabberHttpAuthParams::MSG) {
		XmlNode msg(_T("message"));
		msg << XATTR(_T("to"), pParams->m_szFrom);
		if ( !bAuthorized)
			msg << XATTR(_T("type"), _T("error"));
		if (pParams->m_szThreadId)
			msg << XCHILD(_T("thread"), pParams->m_szThreadId);

		msg << XCHILDNS(_T("confirm"), JABBER_FEAT_HTTP_AUTH) << XATTR(_T("id"), pParams->m_szId)
					<< XATTR(_T("method"), pParams->m_szMethod) << XATTR(_T("url"), pParams->m_szUrl);

		if ( !bAuthorized)
			msg << XCHILD(_T("error")) << XATTRI(_T("code"), 401) << XATTR(_T("type"), _T("auth"))
					<< XCHILDNS(_T("not-authorized"), _T("urn:ietf:params:xml:xmpp-stanzas"));

		m_ThreadInfo->send(msg);
	}
	else
		return FALSE;


	return TRUE;
}

class CJabberDlgHttpAuth: public CJabberDlgBase
{
	typedef CJabberDlgBase CSuper;

public:
	CJabberDlgHttpAuth(CJabberProto *proto, HWND hwndParent, CJabberHttpAuthParams *pParams):
		CSuper(proto, IDD_HTTP_AUTH, hwndParent, true),
		m_txtInfo(this, IDC_EDIT_HTTP_AUTH_INFO),
		m_btnAuth(this, IDOK),
		m_btnDeny(this, IDCANCEL),
		m_pParams(pParams)
	{
		m_btnAuth.OnClick = Callback(this, &CJabberDlgHttpAuth::btnAuth_OnClick);
		m_btnDeny.OnClick = Callback(this, &CJabberDlgHttpAuth::btnDeny_OnClick);
	}

	void OnInitDialog()
	{
		CSuper::OnInitDialog();

		WindowSetIcon(m_hwnd, m_proto, "openid");

		SetDlgItemText(m_hwnd, IDC_TXT_URL, m_pParams->m_szUrl);
		SetDlgItemText(m_hwnd, IDC_TXT_FROM, m_pParams->m_szFrom);
		SetDlgItemText(m_hwnd, IDC_TXT_ID, m_pParams->m_szId);
		SetDlgItemText(m_hwnd, IDC_TXT_METHOD, m_pParams->m_szMethod);
	}

	BOOL SendReply(BOOL bAuthorized)
	{
		BOOL bRetVal = m_proto->SendHttpAuthReply(m_pParams, bAuthorized);
		m_pParams->Free();
		mir_free(m_pParams);
		m_pParams = NULL;
		return bRetVal;
	}

	void btnAuth_OnClick(CCtrlButton*)
	{
		SendReply(TRUE);
		Close();
	}
	void btnDeny_OnClick(CCtrlButton*)
	{
		SendReply(FALSE);
		Close();
	}

	UI_MESSAGE_MAP(CJabberDlgHttpAuth, CSuper);
		UI_MESSAGE(WM_CTLCOLORSTATIC, OnCtlColorStatic);
	UI_MESSAGE_MAP_END();

	INT_PTR OnCtlColorStatic(UINT, WPARAM, LPARAM)
	{
		return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
	}

private:
	CCtrlEdit	m_txtInfo;
	CCtrlButton	m_btnAuth;
	CCtrlButton	m_btnDeny;

	CJabberHttpAuthParams *m_pParams;
};

// XEP-0070 support (http auth)
INT_PTR __cdecl CJabberProto::OnHttpAuthRequest(WPARAM wParam, LPARAM lParam)
{
	CLISTEVENT *pCle = (CLISTEVENT *)lParam;
	CJabberHttpAuthParams *pParams = (CJabberHttpAuthParams *)pCle->lParam;
	if ( !pParams)
		return 0;

	CJabberDlgHttpAuth *pDlg = new CJabberDlgHttpAuth(this, (HWND)wParam, pParams);
	if ( !pDlg) {
		pParams->Free();
		mir_free(pParams);
		return 0;
	}

	pDlg->Show();

	return 0;
}


// Jabber API functions
INT_PTR __cdecl CJabberProto::JabberGetApi(WPARAM wParam, LPARAM lParam)
{
	IJabberInterface **ji = (IJabberInterface**)lParam;
	if ( !ji)
		return -1;
	*ji = &m_JabberApi;
	return 0;
}

DWORD CJabberInterface::GetFlags() const
{
	return JIF_UNICODE;
}

int CJabberInterface::GetVersion() const
{
	return 1;
}

DWORD CJabberInterface::GetJabberVersion() const
{
	return __VERSION_DWORD;
}

IJabberSysInterface *CJabberInterface::Sys() const
{
	return &m_psProto->m_JabberSysApi;
}

IJabberNetInterface *CJabberInterface::Net() const
{
	return &m_psProto->m_JabberNetApi;
}

int CJabberSysInterface::GetVersion() const
{
	return 1;
}

int CJabberSysInterface::CompareJIDs(LPCTSTR jid1, LPCTSTR jid2)
{
	if ( !jid1 || !jid2) return 0;
	return JabberCompareJids(jid1, jid2);
}

HANDLE CJabberSysInterface::ContactFromJID(LPCTSTR jid)
{
	if (jid == NULL) return NULL;
	return m_psProto->HContactFromJID(jid);
}

LPTSTR CJabberSysInterface::ContactToJID(HANDLE hContact)
{
	return m_psProto->getTStringA(hContact, m_psProto->isChatRoom(hContact) ? "ChatRoomID" : "jid");
}

LPTSTR CJabberSysInterface::GetBestResourceName(LPCTSTR jid)
{
	if (jid == NULL)
		return NULL;
	LPCTSTR p = _tcschr(jid, '/');
	if (p == NULL) {
		mir_cslock lck(m_psProto->m_csLists);
		return mir_tstrdup(m_psProto->ListGetBestClientResourceNamePtr(jid));
	}
	return mir_tstrdup(jid);
}

LPTSTR CJabberSysInterface::GetResourceList(LPCTSTR jid)
{
	if (jid == NULL)
		return NULL;

	mir_cslock lck(m_psProto->m_csLists);
	JABBER_LIST_ITEM *item = NULL;
	if ((item = m_psProto->ListGetItemPtr(LIST_VCARD_TEMP, jid)) == NULL)
		item = m_psProto->ListGetItemPtr(LIST_ROSTER, jid);
	if (item == NULL)
		return NULL;

	if (!item->arResources.getCount())
		return NULL;

	CMString res;
	for (int i=0; i < item->arResources.getCount(); i++) {
		res.Append(item->arResources[i]->m_tszResourceName);
		res.AppendChar(0);
	}
	res.AppendChar(0);

	return mir_tstrndup(res, res.GetLength());
}

char *CJabberSysInterface::GetModuleName() const
{
	return m_psProto->m_szModuleName;
}

int CJabberNetInterface::GetVersion() const
{
	return 1;
}

unsigned int CJabberNetInterface::SerialNext()
{
	return m_psProto->SerialNext();
}

int CJabberNetInterface::SendXmlNode(HXML node)
{
	return m_psProto->m_ThreadInfo->send(node);
}


typedef struct
{
	JABBER_HANDLER_FUNC Func;
	void *pUserData;
} sHandlerData;

void CJabberProto::ExternalTempIqHandler(HXML node, CJabberIqInfo *pInfo)
{
	sHandlerData *d = (sHandlerData*)pInfo->GetUserData();
	d->Func(&m_JabberApi, node, d->pUserData);
	free(d); // free IqHandlerData allocated in CJabberNetInterface::AddIqHandler below
}

BOOL CJabberProto::ExternalIqHandler(HXML node, CJabberIqInfo *pInfo)
{
	sHandlerData *d = (sHandlerData*)pInfo->GetUserData();
	return d->Func(&m_JabberApi, node, d->pUserData);
}

BOOL CJabberProto::ExternalMessageHandler(HXML node, ThreadData *pThreadData, CJabberMessageInfo* pInfo)
{
	sHandlerData *d = (sHandlerData*)pInfo->GetUserData();
	return d->Func(&m_JabberApi, node, d->pUserData);
}

BOOL CJabberProto::ExternalPresenceHandler(HXML node, ThreadData *pThreadData, CJabberPresenceInfo* pInfo)
{
	sHandlerData *d = (sHandlerData*)pInfo->GetUserData();
	return d->Func(&m_JabberApi, node, d->pUserData);
}

BOOL CJabberProto::ExternalSendHandler(HXML node, ThreadData *pThreadData, CJabberSendInfo* pInfo)
{
	sHandlerData *d = (sHandlerData*)pInfo->GetUserData();
	return d->Func(&m_JabberApi, node, d->pUserData);
}

HJHANDLER CJabberNetInterface::AddPresenceHandler(JABBER_HANDLER_FUNC Func, void *pUserData, int iPriority)
{
	sHandlerData *d = (sHandlerData*)malloc(sizeof(sHandlerData));
	d->Func = Func;
	d->pUserData = pUserData;
	return (HJHANDLER)m_psProto->m_presenceManager.AddPermanentHandler(&CJabberProto::ExternalPresenceHandler, d, free, iPriority);
}

HJHANDLER CJabberNetInterface::AddMessageHandler(JABBER_HANDLER_FUNC Func, int iMsgTypes, LPCTSTR szXmlns, LPCTSTR szTag, void *pUserData, int iPriority)
{
	sHandlerData *d = (sHandlerData*)malloc(sizeof(sHandlerData));
	d->Func = Func;
	d->pUserData = pUserData;
	return (HJHANDLER)m_psProto->m_messageManager.AddPermanentHandler(&CJabberProto::ExternalMessageHandler, iMsgTypes, 0, szXmlns, FALSE, szTag, d, free, iPriority);
}

HJHANDLER CJabberNetInterface::AddIqHandler(JABBER_HANDLER_FUNC Func, int iIqTypes, LPCTSTR szXmlns, LPCTSTR szTag, void *pUserData, int iPriority)
{
	sHandlerData *d = (sHandlerData*)malloc(sizeof(sHandlerData));
	d->Func = Func;
	d->pUserData = pUserData;
	return (HJHANDLER)m_psProto->m_iqManager.AddPermanentHandler(&CJabberProto::ExternalIqHandler, iIqTypes, 0, szXmlns, FALSE, szTag, d, free, iPriority);
}

HJHANDLER CJabberNetInterface::AddTemporaryIqHandler(JABBER_HANDLER_FUNC Func, int iIqTypes, int iIqId, void *pUserData, DWORD dwTimeout, int iPriority)
{
	sHandlerData *d = (sHandlerData*)malloc(sizeof(sHandlerData));
	d->Func = Func;
	d->pUserData = pUserData;
	CJabberIqInfo* pInfo = m_psProto->m_iqManager.AddHandler(&CJabberProto::ExternalTempIqHandler, iIqTypes, NULL, 0, iIqId, d, iPriority);
	if (pInfo && dwTimeout > 0)
		pInfo->SetTimeout(dwTimeout);
	return (HJHANDLER)pInfo;
}

HJHANDLER CJabberNetInterface::AddSendHandler(JABBER_HANDLER_FUNC Func, void *pUserData, int iPriority)
{
	sHandlerData *d = (sHandlerData*)malloc(sizeof(sHandlerData));
	d->Func = Func;
	d->pUserData = pUserData;
	return (HJHANDLER)m_psProto->m_sendManager.AddPermanentHandler(&CJabberProto::ExternalSendHandler, d, free, iPriority);
}

int CJabberNetInterface::RemoveHandler(HJHANDLER hHandler)
{
	return m_psProto->m_sendManager.DeletePermanentHandler((CJabberSendPermanentInfo*)hHandler) ||
		m_psProto->m_presenceManager.DeletePermanentHandler((CJabberPresencePermanentInfo*)hHandler) ||
		m_psProto->m_messageManager.DeletePermanentHandler((CJabberMessagePermanentInfo*)hHandler) ||
		m_psProto->m_iqManager.DeletePermanentHandler((CJabberIqPermanentInfo*)hHandler) ||
		m_psProto->m_iqManager.DeleteHandler((CJabberIqInfo*)hHandler);
}

JabberFeatCapPairDynamic *CJabberNetInterface::FindFeature(LPCTSTR szFeature)
{
	int i;
	for (i = 0; i < m_psProto->m_lstJabberFeatCapPairsDynamic.getCount(); i++)
		if ( !lstrcmp(m_psProto->m_lstJabberFeatCapPairsDynamic[i]->szFeature, szFeature))
			return m_psProto->m_lstJabberFeatCapPairsDynamic[i];
	return NULL;
}

int CJabberNetInterface::RegisterFeature(LPCTSTR szFeature, LPCTSTR szDescription)
{
	if ( !szFeature) {
		return false;
	}

	// check for this feature in core features, and return false if it's present, to prevent re-registering a core feature
	int i;
	for (i = 0; g_JabberFeatCapPairs[i].szFeature; i++)
	{
		if ( !lstrcmp(g_JabberFeatCapPairs[i].szFeature, szFeature))
		{
			return false;
		}
	}

	mir_cslock lck(m_psProto->m_csLists);
	JabberFeatCapPairDynamic *fcp = FindFeature(szFeature);
	if ( !fcp) { // if the feature is not registered yet, allocate new bit for it
		JabberCapsBits jcb = JABBER_CAPS_OTHER_SPECIAL; // set all bits not included in g_JabberFeatCapPairs

		// set all bits occupied by g_JabberFeatCapPairs
		for (i = 0; g_JabberFeatCapPairs[i].szFeature; i++)
			jcb |= g_JabberFeatCapPairs[i].jcbCap;

		// set all bits already occupied by external plugins
		for (i = 0; i < m_psProto->m_lstJabberFeatCapPairsDynamic.getCount(); i++)
			jcb |= m_psProto->m_lstJabberFeatCapPairsDynamic[i]->jcbCap;

		// Now get first zero bit. The line below is a fast way to do it. If there are no zero bits, it returns 0.
		jcb = (~jcb) & (JabberCapsBits)(-(__int64)(~jcb));

		// no more free bits
		if ( !jcb)
			return false;

		// remove unnecessary symbols from szFeature to make the string shorter, and use it as szExt
		LPTSTR szExt = mir_tstrdup(szFeature);
		LPTSTR pSrc, pDst;
		for (pSrc = szExt, pDst = szExt; *pSrc; pSrc++)
			if (_tcschr(_T("bcdfghjklmnpqrstvwxz0123456789"), *pSrc))
				*pDst++ = *pSrc;
		*pDst = 0;
		m_psProto->m_clientCapsManager.SetClientCaps(JABBER_CAPS_MIRANDA_NODE, szExt, jcb);

		fcp = new JabberFeatCapPairDynamic();
		fcp->szExt = szExt; // will be deallocated along with other values of JabberFeatCapPairDynamic in CJabberProto destructor
		fcp->szFeature = mir_tstrdup(szFeature);
		fcp->szDescription = szDescription ? mir_tstrdup(szDescription) : NULL;
		fcp->jcbCap = jcb;
		m_psProto->m_lstJabberFeatCapPairsDynamic.insert(fcp);
	}
	else if (szDescription) { // update description
		if (fcp->szDescription)
			mir_free(fcp->szDescription);
		fcp->szDescription = mir_tstrdup(szDescription);
	}
	return true;
}

int CJabberNetInterface::AddFeatures(LPCTSTR szFeatures)
{
	if ( !szFeatures)
		return false;

	mir_cslockfull lck(m_psProto->m_csLists);
	BOOL ret = true;
	LPCTSTR szFeat = szFeatures;
	while (szFeat[0]) {
		JabberFeatCapPairDynamic *fcp = FindFeature(szFeat);
		// if someone is trying to add one of core features, RegisterFeature() will return false, so we don't have to perform this check here
		if ( !fcp) { // if the feature is not registered yet
			if ( !RegisterFeature(szFeat, NULL))
				ret = false;
			else
				fcp = FindFeature(szFeat); // update fcp after RegisterFeature()
		}
		if (fcp)
			m_psProto->m_uEnabledFeatCapsDynamic |= fcp->jcbCap;
		else
			ret = false;
		szFeat += lstrlen(szFeat) + 1;
	}
	lck.unlock();

	if (m_psProto->m_bJabberOnline)
		m_psProto->SendPresence(m_psProto->m_iStatus, true);

	return ret;
}

int CJabberNetInterface::RemoveFeatures(LPCTSTR szFeatures)
{
	if ( !szFeatures)
		return false;

	mir_cslockfull lck(m_psProto->m_csLists);
	BOOL ret = true;
	LPCTSTR szFeat = szFeatures;
	while (szFeat[0]) {
		JabberFeatCapPairDynamic *fcp = FindFeature(szFeat);
		if (fcp)
			m_psProto->m_uEnabledFeatCapsDynamic &= ~fcp->jcbCap;
		else
			ret = false; // indicate that there was an error removing at least one of the specified features

		szFeat += lstrlen(szFeat) + 1;
	}
	lck.unlock();

	if (m_psProto->m_bJabberOnline)
		m_psProto->SendPresence(m_psProto->m_iStatus, true);

	return ret;
}

LPTSTR CJabberNetInterface::GetResourceFeatures(LPCTSTR jid)
{
	JabberCapsBits jcb = m_psProto->GetResourceCapabilites(jid, true);
	if (jcb & JABBER_RESOURCE_CAPS_ERROR)
		return NULL;

	mir_cslockfull lck(m_psProto->m_csLists);
	int i;
	int iLen = 1; // 1 for extra zero terminator at the end of the string
	// calculate total necessary string length
	for (i = 0; g_JabberFeatCapPairs[i].szFeature; i++)
		if (jcb & g_JabberFeatCapPairs[i].jcbCap)
			iLen += lstrlen(g_JabberFeatCapPairs[i].szFeature) + 1;

	for (i = 0; i < m_psProto->m_lstJabberFeatCapPairsDynamic.getCount(); i++)
		if (jcb & m_psProto->m_lstJabberFeatCapPairsDynamic[i]->jcbCap)
			iLen += lstrlen(m_psProto->m_lstJabberFeatCapPairsDynamic[i]->szFeature) + 1;

	// allocate memory and fill it
	LPTSTR str = (LPTSTR)mir_alloc(iLen * sizeof(TCHAR));
	LPTSTR p = str;
	for (i = 0; g_JabberFeatCapPairs[i].szFeature; i++)
		if (jcb & g_JabberFeatCapPairs[i].jcbCap) {
			lstrcpy(p, g_JabberFeatCapPairs[i].szFeature);
			p += lstrlen(g_JabberFeatCapPairs[i].szFeature) + 1;
		}

	for (i = 0; i < m_psProto->m_lstJabberFeatCapPairsDynamic.getCount(); i++)
		if (jcb & m_psProto->m_lstJabberFeatCapPairsDynamic[i]->jcbCap) {
			lstrcpy(p, m_psProto->m_lstJabberFeatCapPairsDynamic[i]->szFeature);
			p += lstrlen(m_psProto->m_lstJabberFeatCapPairsDynamic[i]->szFeature) + 1;
		}

	*p = 0; // extra zero terminator
	return str;
}

HANDLE CJabberNetInterface::GetHandle()
{
	return m_psProto->m_hNetlibUser;
}