summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/msn_misc.cpp
blob: 4f84bc25564c960e5748dc04be62f1eb837c64b5 (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
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.

Copyright (c) 2012-2013 Miranda NG Team
Copyright (c) 2006-2012 Boris Krasnovskiy.
Copyright (c) 2003-2005 George Hazan.
Copyright (c) 2002-2003 Richard Hughes (original version).

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, see <http://www.gnu.org/licenses/>.
*/

#include "msn_global.h"
#include "msn_proto.h"
#include "version.h"


/////////////////////////////////////////////////////////////////////////////////////////
// MirandaStatusToMSN - status helper functions

const char*  CMsnProto::MirandaStatusToMSN(int status)
{
	switch(status)
	{
		case ID_STATUS_OFFLINE:		return "FLN";
		case ID_STATUS_ONTHEPHONE:
		case ID_STATUS_OUTTOLUNCH:
		case ID_STATUS_NA:
		case ID_STATUS_AWAY:		return "AWY";
		case ID_STATUS_DND:
		case ID_STATUS_OCCUPIED:	return "BSY";
		case ID_STATUS_INVISIBLE:	return "HDN";
		case ID_STATUS_IDLE:		return "IDL";
		default:					return "NLN";
}	}

WORD  CMsnProto::MSNStatusToMiranda(const char *status)
{
	switch((*(PDWORD)status&0x00FFFFFF) | 0x20000000)
	{
		case ' LDI': return ID_STATUS_IDLE;
		case ' NLN': return ID_STATUS_ONLINE;
		case ' NHP':
		case ' NUL':
		case ' BRB':
		case ' YWA': return ID_STATUS_AWAY;
		case ' YSB': return ID_STATUS_OCCUPIED;
		case ' NDH': return ID_STATUS_INVISIBLE;
		default: return ID_STATUS_OFFLINE;
	}
}

char**  CMsnProto::GetStatusMsgLoc(int status)
{
	static const int modes[MSN_NUM_MODES] =
	{
		ID_STATUS_ONLINE,
		ID_STATUS_AWAY,
		ID_STATUS_DND,
		ID_STATUS_NA,
		ID_STATUS_OCCUPIED,
		ID_STATUS_FREECHAT,
		ID_STATUS_INVISIBLE,
		ID_STATUS_ONTHEPHONE,
		ID_STATUS_OUTTOLUNCH,
	};

	for (int i=0; i < MSN_NUM_MODES; i++)
		if (modes[i] == status) return &msnModeMsgs[i];

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_AddAuthRequest - adds the authorization event to the database

void  CMsnProto::MSN_AddAuthRequest(const char *email, const char *nick, const char *reason)
{
	//blob is: UIN=0(DWORD), hContact(DWORD), nick(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ), email(ASCIIZ), ""(ASCIIZ)

	HANDLE hContact = MSN_HContactFromEmail(email, nick, true, true);

	int emaillen = (int)strlen(email);

	if (nick == NULL) nick = "";
	int nicklen = (int)strlen(nick);

	if (reason == NULL) reason = "";
	int reasonlen = (int)strlen(reason);

	PROTORECVEVENT pre = { 0 };
	pre.flags = PREF_UTF;
	pre.timestamp = (DWORD)time(NULL);
	pre.lParam = sizeof(DWORD) + sizeof(HANDLE) + nicklen + emaillen + 5 + reasonlen;

	char* pCurBlob = (char*)alloca(pre.lParam);
	pre.szMessage = pCurBlob;

	*(PDWORD)pCurBlob = 0; pCurBlob += sizeof(DWORD);               // UID
	*(PHANDLE)pCurBlob = hContact; pCurBlob += sizeof(HANDLE);      // Contact Handle
	strcpy(pCurBlob, nick); pCurBlob += nicklen + 1;                // Nickname
	*pCurBlob = '\0'; pCurBlob++;                                   // First Name
	*pCurBlob = '\0'; pCurBlob++;	                                // Last Name
	strcpy(pCurBlob, email); pCurBlob += emaillen + 1;              // E-mail
	strcpy(pCurBlob, reason);                                       // Reason

	ProtoChainRecv(hContact, PSR_AUTH, 0, (LPARAM)&pre);
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_DebugLog - writes a line of comments to the network log

void CMsnProto::MSN_DebugLog(const char *fmt, ...)
{
	char	str[4096];
	va_list	vararg;

	va_start(vararg, fmt);
	if (_vsnprintf(str, sizeof(str), fmt, vararg) != 0)
	{
		str[sizeof(str)-1] = 0;
		CallService(MS_NETLIB_LOG, (WPARAM)hNetlibUser, (LPARAM)str);
	}
	va_end(vararg);
}

void CMsnProto::InitCustomFolders(void)
{
	if (InitCstFldRan) return;

	TCHAR folder[MAX_PATH];
	TCHAR *tszModuleName = mir_a2t(m_szModuleName);

	mir_sntprintf(folder, SIZEOF(folder), _T("%%miranda_avatarcache%%\\%s"), tszModuleName);
	hMSNAvatarsFolder = FoldersRegisterCustomPathT(LPGEN("Avatars"), m_szModuleName, folder, m_tszUserName);

	mir_sntprintf(folder, SIZEOF(folder), _T("%%miranda_avatarcache%%\\%s"), tszModuleName);
	hCustomSmileyFolder = FoldersRegisterCustomPathT(LPGEN("Custom Smileys"), m_szModuleName, folder, m_tszUserName);

	mir_free(tszModuleName);
	InitCstFldRan = true;
}


char* MSN_GetAvatarHash(char* szContext, char** pszUrl)
{
	if (pszUrl)
		*pszUrl = NULL;

	if (szContext == NULL)
		return NULL;

	char *res  = NULL;

	ezxml_t xmli = ezxml_parse_str(NEWSTR_ALLOCA(szContext), strlen(szContext));
	const char *szAvatarHash = ezxml_attr(xmli, "SHA1D");
	if (szAvatarHash != NULL) {
		unsigned hashLen;
		mir_ptr<BYTE> hash((BYTE*)mir_base64_decode(szAvatarHash, &hashLen));
		if (hash)
			res = arrayToHex(hash, hashLen);

		if (pszUrl) {
			const char *pszUrlAttr;
			for (int i=0; ; i++) {
				char szSetting[20];
				if (i == 0)
					strcpy(szSetting, "Url");
				else
					mir_snprintf(szSetting, sizeof(szSetting), "Url%d", i);
				pszUrlAttr = ezxml_attr(xmli, szSetting);
				if (pszUrlAttr == NULL)
					break;

				if (pszUrlAttr[0] != 0) {
					*pszUrl = mir_strdup(pszUrlAttr);
					break;
				}
			}
		}
	}
	ezxml_free(xmli);

	return res;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GetAvatarFileName - gets a file name for an contact's avatar

void  CMsnProto::MSN_GetAvatarFileName(HANDLE hContact, TCHAR* pszDest, size_t cbLen, const TCHAR *ext)
{
	size_t tPathLen;

	InitCustomFolders();

	TCHAR* path = (TCHAR*)alloca(cbLen * sizeof(TCHAR));
	if (hMSNAvatarsFolder == NULL || FoldersGetCustomPathT(hMSNAvatarsFolder, path, (int)cbLen, _T("")))
	{
		TCHAR *tmpPath = Utils_ReplaceVarsT(_T("%miranda_userdata%"));
		TCHAR *sztModuleName = mir_a2t(m_szModuleName);
		tPathLen = mir_sntprintf(pszDest, cbLen, _T("%s\\Avatars\\%s"), tmpPath, sztModuleName);
		mir_free(sztModuleName);
		mir_free(tmpPath);
	}
	else {
		_tcscpy(pszDest, path);
		tPathLen = _tcslen(pszDest);
	}

	if (_taccess(pszDest, 0))
		CreateDirectoryTreeT(pszDest);

	size_t tPathLen2 = tPathLen;
	if (hContact != NULL) {
		DBVARIANT dbv;
		if (getString(hContact, "PictContext", &dbv) == 0) {
			char* szAvatarHash = MSN_GetAvatarHash(dbv.pszVal);
			if (szAvatarHash != NULL) {
				TCHAR *sztAvatarHash = mir_a2t(szAvatarHash);
				tPathLen += mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s."), sztAvatarHash);
				mir_free(sztAvatarHash);
				mir_free(szAvatarHash);
			}
			else {
				delSetting(hContact, "PictContext");
				if (cbLen) pszDest[0] = 0;
			}
			db_free(&dbv);
		}
		else if (cbLen)
			pszDest[0] = 0;
	}
	else {
		TCHAR *sztModuleName = mir_a2t(m_szModuleName);
		tPathLen += mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s avatar."), sztModuleName);
		mir_free(sztModuleName);
	}

	if (ext == NULL) {
		mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("*"));

		bool found = false;
		_tfinddata_t c_file;
		long hFile = _tfindfirst(pszDest, &c_file);
		if (hFile > -1L) {
			do {
				if (_tcsrchr(c_file.name, '.')) {
					mir_sntprintf(pszDest + tPathLen2, cbLen - tPathLen2, _T("\\%s"), c_file.name);
					found = true;
				}
			}
				while(_tfindnext(hFile, &c_file) == 0);
			_findclose( hFile );
		}

		if (!found) pszDest[0] = 0;
	}
	else mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, ext);
}

int MSN_GetImageFormat(void* buf, const TCHAR** ext)
{
	if (*(unsigned short*)buf == 0xd8ff) {
		*ext = _T("jpg");
		return PA_FORMAT_JPEG;
	}
	if (*(unsigned short*)buf == 0x4d42) {
		*ext = _T("bmp");
		return PA_FORMAT_BMP;
	}
	if (*(unsigned*)buf == 0x474e5089) {
		*ext = _T("png");
		return PA_FORMAT_PNG;
	}
	if (*(unsigned*)buf == 0x38464947) {
		*ext = _T("gif");
		return PA_FORMAT_GIF;
	}

	*ext = _T("unk");
	return PA_FORMAT_UNKNOWN;
}

int MSN_GetImageFormat(const TCHAR* file)
{
	const TCHAR *ext = _tcsrchr(file, '.');
	if (ext == NULL)
		return PA_FORMAT_UNKNOWN;
	if (_tcsicmp(ext, _T(".gif")) == 0)
		return PA_FORMAT_GIF;
	else if (_tcsicmp(ext, _T(".bmp")) == 0)
		return PA_FORMAT_BMP;
	else if (_tcsicmp(ext, _T(".png")) == 0)
		return PA_FORMAT_PNG;
	else
		return PA_FORMAT_JPEG;
}

int  CMsnProto::MSN_SetMyAvatar(const TCHAR* sztFname, void* pData, size_t cbLen)
{
	mir_sha1_ctx sha1ctx;
	BYTE sha1c[MIR_SHA1_HASH_SIZE], sha1d[MIR_SHA1_HASH_SIZE];

	char *szFname = mir_utf8encodeT(sztFname);

	mir_sha1_init(&sha1ctx);
	mir_sha1_append(&sha1ctx, (mir_sha1_byte_t*)pData, (int)cbLen);
	mir_sha1_finish(&sha1ctx, sha1d);

	ptrA szSha1d( mir_base64_encode((PBYTE)sha1d, sizeof(sha1d)));

	mir_sha1_init(&sha1ctx);
	ezxml_t xmlp = ezxml_new("msnobj");

	mir_sha1_append(&sha1ctx, (PBYTE)"Creator", 7);
	mir_sha1_append(&sha1ctx, (PBYTE)MyOptions.szEmail, (int)strlen(MyOptions.szEmail));
	ezxml_set_attr(xmlp, "Creator", MyOptions.szEmail);

	char szFileSize[20];
	_ultoa((unsigned)cbLen, szFileSize, 10);
	mir_sha1_append(&sha1ctx, (PBYTE)"Size", 4);
	mir_sha1_append(&sha1ctx, (PBYTE)szFileSize, (int)strlen(szFileSize));
	ezxml_set_attr(xmlp, "Size", szFileSize);

	mir_sha1_append(&sha1ctx, (PBYTE)"Type", 4);
	mir_sha1_append(&sha1ctx, (PBYTE)"3", 1);  // MSN_TYPEID_DISPLAYPICT
	ezxml_set_attr(xmlp, "Type", "3");

	mir_sha1_append(&sha1ctx, (PBYTE)"Location", 8);
	mir_sha1_append(&sha1ctx, (PBYTE)szFname, (int)strlen(szFname));
	ezxml_set_attr(xmlp, "Location", szFname);

	mir_sha1_append(&sha1ctx, (PBYTE)"Friendly", 8);
	mir_sha1_append(&sha1ctx, (PBYTE)"AAA=", 4);
	ezxml_set_attr(xmlp, "Friendly", "AAA=");

	mir_sha1_append(&sha1ctx, (PBYTE)"SHA1D", 5);
	mir_sha1_append(&sha1ctx, (PBYTE)(char*)szSha1d, (int)strlen(szSha1d));
	ezxml_set_attr(xmlp, "SHA1D", szSha1d);

	mir_sha1_finish(&sha1ctx, sha1c);

	ptrA szSha1c( mir_base64_encode((PBYTE)sha1c, sizeof(sha1c)));

	//	ezxml_set_attr(xmlp, "SHA1C", szSha1c);

	char* szBuffer = ezxml_toxml(xmlp, false);
	ezxml_free(xmlp);
	mir_free(szFname);

	char szEncodedBuffer[2000];
	UrlEncode(szBuffer, szEncodedBuffer, sizeof(szEncodedBuffer));
	free(szBuffer);

	const TCHAR *szExt;
	int fmt = MSN_GetImageFormat(pData, &szExt);
	if (fmt == PA_FORMAT_UNKNOWN) return fmt;

	TCHAR szFileName[MAX_PATH];
	MSN_GetAvatarFileName(NULL, szFileName, SIZEOF(szFileName), NULL);
	_tremove(szFileName);

	MSN_GetAvatarFileName(NULL, szFileName, SIZEOF(szFileName), szExt);

	int fileId = _topen(szFileName, _O_CREAT | _O_TRUNC | _O_WRONLY | O_BINARY, _S_IREAD | _S_IWRITE);
	if (fileId >= 0) {
		_write(fileId, pData, (unsigned)cbLen);
		_close(fileId);

		char szAvatarHashdOld[41] = "";
		getStaticString(NULL, "AvatarHash", szAvatarHashdOld, sizeof(szAvatarHashdOld));
		char *szAvatarHash = arrayToHex(sha1d, sizeof(sha1d));
		if (strcmp(szAvatarHashdOld, szAvatarHash))
		{
			setString("PictObject", szEncodedBuffer);
			setString("AvatarHash", szAvatarHash);
		}
		mir_free(szAvatarHash);
	}
	else MSN_ShowError("Cannot set avatar. File '%s' could not be created/overwritten", szFileName);

	return fmt;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GetCustomSmileyFileName - gets a file name for an contact's custom smiley

void  CMsnProto::MSN_GetCustomSmileyFileName(HANDLE hContact, TCHAR* pszDest, size_t cbLen, const char* SmileyName, int type)
{
	size_t tPathLen;

	InitCustomFolders();

	TCHAR* path = (TCHAR*)alloca(cbLen * sizeof(TCHAR));
	if (hCustomSmileyFolder == NULL || FoldersGetCustomPathT(hCustomSmileyFolder, path, (int)cbLen, _T(""))) {
		TCHAR *tmpPath = Utils_ReplaceVarsT(_T("%miranda_userdata%"));
		TCHAR *tszModuleName = mir_a2t(m_szModuleName);
		tPathLen = mir_sntprintf(pszDest, cbLen, _T("%s\\%s\\CustomSmiley"), tmpPath, tszModuleName);
		mir_free(tszModuleName);
		mir_free(tmpPath);
	}
	else {
		_tcscpy(pszDest, path);
		tPathLen = _tcslen(pszDest);
	}

	if (hContact != NULL)
	{
		DBVARIANT dbv = {0};
		if (getTString(hContact, "e-mail", &dbv))
		{
			dbv.type = DBVT_ASCIIZ;
			dbv.ptszVal = (TCHAR*)mir_alloc(11);
			_ui64tot((UINT_PTR)hContact, dbv.ptszVal, 10);
		}

		tPathLen += mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s"), dbv.ptszVal);
		db_free(&dbv);
	}
	else {
		TCHAR *tszModuleName = mir_a2t(m_szModuleName);
		tPathLen += mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s"), tszModuleName);
		mir_free(tszModuleName);
	}

	bool exist = _taccess(pszDest, 0) == 0;

	if (type == 0) {
		if (!exist) pszDest[0] = 0;
		return;
	}

	if (!exist)
		CreateDirectoryTreeT(pszDest);

	TCHAR *sztSmileyName = mir_a2t(SmileyName);
	mir_sntprintf(pszDest + tPathLen, cbLen - tPathLen, _T("\\%s.%s"), sztSmileyName,
		type == MSN_APPID_CUSTOMSMILEY ? _T("png") : _T("gif"));
	mir_free(sztSmileyName);
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GoOffline - performs several actions when a server goes offline

void CMsnProto::MSN_GoOffline(void)
{
	if (m_iStatus == ID_STATUS_OFFLINE) return;

	msnLoggedIn = false;

	if (mStatusMsgTS)
		ForkThread(&CMsnProto::msn_storeProfileThread, NULL);

	mir_free(msnPreviousUUX);
	msnPreviousUUX = NULL;
	msnSearchId = NULL;

	if (!Miranda_Terminated())
		MSN_EnableMenuItems(false);

	MSN_FreeGroups();
	MsgQueue_Clear();
	clearCachedMsg();

	if (!Miranda_Terminated()) {
		int msnOldStatus = m_iStatus; m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
		ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)msnOldStatus, ID_STATUS_OFFLINE);
		isIdle = false;

		int count = -1;
		for (;;) {
			MsnContact *msc = Lists_GetNext(count);
			if (msc == NULL) break;

			if (ID_STATUS_OFFLINE != getWord(msc->hContact, "Status", ID_STATUS_OFFLINE)) {
				setWord(msc->hContact, "Status", ID_STATUS_OFFLINE);
				setDword(msc->hContact, "IdleTS", 0);
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SendMessage - formats and sends a MSG packet through the server

int ThreadData::sendMessage(int msgType, const char* email, int netId, const char* parMsg, int parFlags)
{
	char buf[2048];
	int off;

	off = mir_snprintf(buf, sizeof(buf), "MIME-Version: 1.0\r\n");

	if ((parFlags & MSG_DISABLE_HDR) == 0) {
		char  tFontName[100], tFontStyle[3];
		DWORD tFontColor;

		strcpy(tFontName, "Arial");

		if (proto->getByte("SendFontInfo", 1)) {
			char* p;

			DBVARIANT dbv;
			if (!db_get_s(NULL, "SRMsg", "Font0", &dbv)) {
				for (p = dbv.pszVal; *p; p++)
					if (BYTE(*p) >= 128 || *p < 32)
						break;

				if (*p == 0) {
					UrlEncode(dbv.pszVal, tFontName, sizeof(tFontName));
					db_free(&dbv);
				}
			}

			int  tStyle = db_get_b(NULL, "SRMsg", "Font0Sty", 0);
			p = tFontStyle;
			if (tStyle & 1) *p++ = 'B';
			if (tStyle & 2) *p++ = 'I';
			*p = 0;

			tFontColor = db_get_dw(NULL, "SRMsg", "Font0Col", 0);
		}
		else {
			tFontColor = 0;
			tFontStyle[0] = 0;
		}

		if (parFlags & MSG_OFFLINE)
			off += mir_snprintf(buf + off, sizeof(buf) - off, "Dest-Agent: client\r\n");

		off += mir_snprintf(buf + off, sizeof(buf) - off, "Content-Type: text/plain; charset=UTF-8\r\n");
		off += mir_snprintf(buf + off, sizeof(buf) - off, "X-MMS-IM-Format: FN=%s; EF=%s; CO=%x; CS=0; PF=31%s\r\n\r\n",
			tFontName, tFontStyle, tFontColor, (parFlags & MSG_RTL) ? ";RL=1" : "");
	}

	int seq;
	if (netId == NETID_YAHOO || netId == NETID_MOB || (parFlags & MSG_OFFLINE))
		seq = sendPacket("UUM", "%s %d %c %d\r\n%s%s", email, netId, msgType,
		strlen(parMsg)+off, buf, parMsg);
	else
		seq = sendPacket("MSG", "%c %d\r\n%s%s", msgType,
		strlen(parMsg)+off, buf, parMsg);

	return seq;
}

void ThreadData::sendCaps(void)
{
	char mversion[100], capMsg[1000];
	CallService(MS_SYSTEM_GETVERSIONTEXT, sizeof(mversion), (LPARAM)mversion);

	mir_snprintf(capMsg, sizeof(capMsg),
		"Content-Type: text/x-clientcaps\r\n\r\n"
		"Client-Name: Miranda NG %s (MSN v.%s)\r\n",
		mversion, __VERSION_STRING);

	sendMessage('U', NULL, 1, capMsg, MSG_DISABLE_HDR);
}

void ThreadData::sendTerminate(void)
{
	if (!termPending) {
		sendPacket("OUT", NULL);
		termPending = true;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SendRawPacket - sends a packet accordingly to the MSN protocol

int ThreadData::sendRawMessage(int msgType, const char* data, int datLen)
{
	if (data == NULL)
		data = "";

	if (datLen == -1)
		datLen = (int)strlen(data);

	char* buf = (char*)alloca(datLen + 100);

	int thisTrid = InterlockedIncrement(&mTrid);
	int nBytes = mir_snprintf(buf, 100, "MSG %d %c %d\r\nMIME-Version: 1.0\r\n",
		thisTrid, msgType, datLen + 19);
	memcpy(buf + nBytes, data, datLen);

	send(buf, nBytes + datLen);

	return thisTrid;
}

// Typing notifications support

void CMsnProto::MSN_SendTyping(ThreadData* info, const char* email, int netId )
{
	char tCommand[1024];
	mir_snprintf(tCommand, sizeof(tCommand),
		"Content-Type: text/x-msmsgscontrol\r\n"
		"TypingUser: %s\r\n\r\n\r\n", MyOptions.szEmail);

	info->sendMessage(netId == NETID_MSN ? 'U' : '2', email, netId, tCommand, MSG_DISABLE_HDR);
}


static ThreadData* FindThreadTimer(UINT timerId)
{
	ThreadData* res = NULL;
	for (int i = 0; i < g_Instances.getCount() && res == NULL; ++i)
		res = g_Instances[i].MSN_GetThreadByTimer(timerId);

	return res;
}

static VOID CALLBACK TypingTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	ThreadData* T = FindThreadTimer(idEvent);
	if (T != NULL)
		T->proto->MSN_SendTyping(T, NULL, 1);
	else
		KillTimer(NULL, idEvent);
}


void  CMsnProto::MSN_StartStopTyping(ThreadData* info, bool start)
{
	if (start && info->mTimerId == 0) {
		info->mTimerId = SetTimer(NULL, 0, 5000, TypingTimerProc);
		MSN_SendTyping(info, NULL, 1);
	}
	else if (!start && info->mTimerId != 0) {
		KillTimer(NULL, info->mTimerId);
		info->mTimerId = 0;
	}
}


/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SendStatusMessage - notify a server about the status message change

// Helper to process texts
static char * HtmlEncodeUTF8T(const TCHAR *src)
{
	if (src == NULL)
		return mir_strdup("");

	return HtmlEncode(UTF8(src));
}

void  CMsnProto::MSN_SendStatusMessage(const char* msg)
{
	if (!msnLoggedIn)
		return;

	char* msgEnc = HtmlEncode(msg ? msg : "");

	size_t sz;
	char  szMsg[2048];
	if (msnCurrentMedia.cbSize == 0) {
		sz = mir_snprintf(szMsg, sizeof(szMsg), "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia><MachineGuid>%s</MachineGuid>"
			"<DDP></DDP><SignatureSound></SignatureSound><Scene></Scene><ColorScheme></ColorScheme></Data>",
			msgEnc, MyOptions.szMachineGuid);
	}
	else {
		char *szFormatEnc;
		if (ServiceExists(MS_LISTENINGTO_GETPARSEDTEXT)) {
			LISTENINGTOINFO lti = {0};
			lti.cbSize = sizeof(lti);
			if (msnCurrentMedia.ptszTitle != NULL) lti.ptszTitle = _T("{0}");
			if (msnCurrentMedia.ptszArtist != NULL) lti.ptszArtist = _T("{1}");
			if (msnCurrentMedia.ptszAlbum != NULL) lti.ptszAlbum = _T("{2}");
			if (msnCurrentMedia.ptszTrack != NULL) lti.ptszTrack = _T("{3}");
			if (msnCurrentMedia.ptszYear != NULL) lti.ptszYear = _T("{4}");
			if (msnCurrentMedia.ptszGenre != NULL) lti.ptszGenre = _T("{5}");
			if (msnCurrentMedia.ptszLength != NULL) lti.ptszLength = _T("{6}");
			if (msnCurrentMedia.ptszPlayer != NULL) lti.ptszPlayer = _T("{7}");
			if (msnCurrentMedia.ptszType != NULL) lti.ptszType = _T("{8}");

			TCHAR *tmp = (TCHAR *)CallService(MS_LISTENINGTO_GETPARSEDTEXT, (WPARAM) _T("%title% - %artist%"), (LPARAM) &lti);
			szFormatEnc = HtmlEncodeUTF8T(tmp);
			mir_free(tmp);
		}
		else szFormatEnc = HtmlEncodeUTF8T(_T("{0} - {1}"));

		char *szArtist = HtmlEncodeUTF8T(msnCurrentMedia.ptszArtist);
		char *szAlbum = HtmlEncodeUTF8T(msnCurrentMedia.ptszAlbum);
		char *szTitle = HtmlEncodeUTF8T(msnCurrentMedia.ptszTitle);
		char *szTrack = HtmlEncodeUTF8T(msnCurrentMedia.ptszTrack);
		char *szYear = HtmlEncodeUTF8T(msnCurrentMedia.ptszYear);
		char *szGenre = HtmlEncodeUTF8T(msnCurrentMedia.ptszGenre);
		char *szLength = HtmlEncodeUTF8T(msnCurrentMedia.ptszLength);
		char *szPlayer = HtmlEncodeUTF8T(msnCurrentMedia.ptszPlayer);
		char *szType = HtmlEncodeUTF8T(msnCurrentMedia.ptszType);

		sz = mir_snprintf(szMsg, sizeof szMsg,
			"<Data>"
			"<PSM>%s</PSM>"
			"<CurrentMedia>%s\\0%s\\01\\0%s\\0%s\\0%s\\0%s\\0%s\\0%s\\0%s\\0%s\\0%s\\0%s\\0\\0</CurrentMedia>"
			"<MachineGuid>%s</MachineGuid><DDP></DDP><SignatureSound></SignatureSound><Scene></Scene><ColorScheme></ColorScheme>"
			"<DDP></DDP><SignatureSound></SignatureSound><Scene></Scene><ColorScheme></ColorScheme>"
			"</Data>",
			msgEnc, szPlayer, szType, szFormatEnc, szTitle, szArtist, szAlbum, szTrack, szYear, szGenre,
			szLength, szPlayer, szType, MyOptions.szMachineGuid);

		mir_free(szArtist);
		mir_free(szAlbum);
		mir_free(szTitle);
		mir_free(szTrack);
		mir_free(szYear);
		mir_free(szGenre);
		mir_free(szLength);
		mir_free(szPlayer);
		mir_free(szType);
	}
	mir_free(msgEnc);

	if (msnPreviousUUX == NULL || strcmp(msnPreviousUUX, szMsg)) {
		replaceStr(msnPreviousUUX, szMsg);
		msnNsThread->sendPacket("UUX", "%d\r\n%s", sz, szMsg);
		mStatusMsgTS = clock();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SendPacket - sends a packet accordingly to the MSN protocol

int ThreadData::sendPacket(const char* cmd, const char* fmt,...)
{
	if (this == NULL) return 0;

	size_t strsize = 512;
	char* str = (char*)mir_alloc(strsize);

	int thisTrid = 0;

	if (fmt == NULL)
		mir_snprintf(str, strsize, "%s", cmd);
	else {
		thisTrid = InterlockedIncrement(&mTrid);
		if (fmt[0] == '\0')
			mir_snprintf(str, strsize, "%s %d", cmd, thisTrid);
		else {
			va_list vararg;
			va_start(vararg, fmt);

			int paramStart = mir_snprintf(str, strsize, "%s %d ", cmd, thisTrid);
			while (_vsnprintf(str+paramStart, strsize-paramStart-3, fmt, vararg) == -1)
				str = (char*)mir_realloc(str, strsize += 512);

			str[strsize-3] = 0;
			va_end(vararg);
		}
	}

	if (strchr(str, '\r') == NULL)
		strcat(str,"\r\n");

	int result = send(str, strlen(str));
	mir_free(str);
	return (result > 0) ? thisTrid : -1;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SetServerStatus - changes plugins status at the server

void  CMsnProto::MSN_SetServerStatus(int newStatus)
{
	MSN_DebugLog("Setting MSN server status %d, logged in = %d", newStatus, msnLoggedIn);

	if (!msnLoggedIn)
		return;

	if (isIdle)
		newStatus = ID_STATUS_IDLE;

	const char* szStatusName = MirandaStatusToMSN(newStatus);

	if (newStatus != ID_STATUS_OFFLINE) {
		DBVARIANT msnObject = {0};
		if (ServiceExists(MS_AV_SETMYAVATAR))
			getString("PictObject", &msnObject);

		// Capabilties: WLM 2009, Chunking, UUN Bootstrap
		myFlags = 0xA0000000 | cap_SupportsChunking | cap_SupportsP2PBootstrap | cap_SupportsSipInvite;
		if (getByte("MobileEnabled", 0) && getByte("MobileAllowed", 0))
			myFlags |= cap_MobileEnabled;

		unsigned myFlagsEx = capex_SupportsPeerToPeerV2;

		char szMsg[256];
		if (m_iStatus < ID_STATUS_ONLINE) {
			int sz = mir_snprintf(szMsg, sizeof(szMsg),
				"<EndpointData><Capabilities>%u:%u</Capabilities></EndpointData>", myFlags, myFlagsEx);
			msnNsThread->sendPacket( "UUX", "%d\r\n%s", sz, szMsg );

			msnNsThread->sendPacket("BLP", msnOtherContactsBlocked ? "BL" : "AL");

			DBVARIANT dbv;
			if (!getStringUtf("Nick", &dbv)) {
				if (dbv.pszVal[0])
					MSN_SetNicknameUtf(dbv.pszVal);
				db_free(&dbv);
			}
		}

		char *szPlace;
		DBVARIANT dbv;
		if	(!getStringUtf("Place", &dbv))
			szPlace = dbv.pszVal;
		else {
			TCHAR buf[128] = _T("Miranda");
			DWORD buflen = SIZEOF(buf);
			GetComputerName(buf, &buflen);
			szPlace =  mir_utf8encodeT(buf);
		}

		int sz = mir_snprintf(szMsg, sizeof(szMsg),
			"<PrivateEndpointData>"
			"<EpName>%s</EpName>"
			"<Idle>%s</Idle>"
			"<ClientType>1</ClientType>"
			"<State>%s</State>"
			"</PrivateEndpointData>",
			szPlace, newStatus == ID_STATUS_IDLE ? "true" : "false", szStatusName);
		msnNsThread->sendPacket("UUX", "%d\r\n%s", sz, szMsg);
		mir_free(szPlace);

		if (newStatus != ID_STATUS_IDLE) {
			char** msgptr = GetStatusMsgLoc(newStatus);
			if (msgptr != NULL)
				MSN_SendStatusMessage(*msgptr);
		}

		msnNsThread->sendPacket("CHG", "%s %u:%u %s", szStatusName, myFlags, myFlagsEx, msnObject.pszVal ? msnObject.pszVal : "0");
		db_free(&msnObject);
	}
	else msnNsThread->sendPacket("CHG", szStatusName);
}


/////////////////////////////////////////////////////////////////////////////////////////
// Display Hotmail Inbox thread

void CMsnProto::MsnInvokeMyURL(bool ismail, const char* url)
{
	char* hippy = NULL;
	if (!url)
		url = ismail ? "http://mail.live.com?rru=inbox" : "http://profile.live.com";

	static const char postdataM[] = "ct=%u&bver=7&wa=wsignin1.0&ru=%s&pl=MBI";
	static const char postdataS[] = "ct=%u&bver=7&id=73625&ru=%s&js=yes&pl=%%3Fid%%3D73625";
	const char *postdata = ismail ? postdataM : postdataS;

	char passport[256];
	if (getStaticString(NULL, "MsnPassportHost", passport, 256))
		strcpy(passport, "https://login.live.com/");

	char *p = strchr(passport, '/');
	if (p && p[1] == '/') p = strchr(p + 2, '/');
	if (p)
		*p = 0;

	char ruenc[256];
	UrlEncode(url, ruenc, sizeof(ruenc));

	const size_t fnpstlen = strlen(postdata) +  strlen(ruenc) + 30;
	char* fnpst = (char*)alloca(fnpstlen);

	mir_snprintf(fnpst, fnpstlen, postdata, time(NULL), ruenc);

	char* post = HotmailLogin(fnpst);
	if (post) {
		size_t hipsz = strlen(passport) + 3*strlen(post) + 70;
		hippy = (char*)alloca(hipsz);

		strcpy(hippy, passport);
		strcat(hippy, "\\ppsecure\\sha1auth.srf?lc=");
		strcat(hippy, itoa(langpref, passport, 10));
		strcat(hippy, "&token=");
		size_t hiplen = strlen(hippy);
		UrlEncode(post, hippy+hiplen, hipsz-hiplen);
		mir_free(post);
	}

	MSN_DebugLog("Starting URL: '%s'", hippy);
	CallService(MS_UTILS_OPENURL, 1, (LPARAM)hippy);
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_ShowError - shows an error

void CMsnProto::MSN_ShowError(const char* msgtext, ...)
{
	TCHAR   tBuffer[4096];
	va_list tArgs;

	TCHAR *buf = (TCHAR*)CallService(MS_LANGPACK_PCHARTOTCHAR, 0, (LPARAM)msgtext);

	va_start(tArgs, msgtext);
	mir_vsntprintf(tBuffer, SIZEOF(tBuffer), buf, tArgs);
	va_end(tArgs);

	mir_free(buf);

	MSN_ShowPopup(m_tszUserName, tBuffer, MSN_ALLOW_MSGBOX | MSN_SHOW_ERROR, NULL);
}

/////////////////////////////////////////////////////////////////////////////////////////
// Popup plugin window proc

LRESULT CALLBACK NullWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PopupData *tData = (PopupData*)PUGetPluginData(hWnd);

	switch (msg) {
	case WM_COMMAND:
		if (tData != NULL) {
			if (tData->flags & MSN_HOTMAIL_POPUP) {
				HANDLE hContact = tData->proto->MSN_HContactFromEmail(tData->proto->MyOptions.szEmail, NULL);
				if (hContact) CallService(MS_CLIST_REMOVEEVENT, (WPARAM)hContact, (LPARAM) 1);
				if (tData->flags & MSN_ALLOW_ENTER)
					tData->proto->MsnInvokeMyURL(true, tData->url);
			}
			else if (tData->url != NULL)
				CallService(MS_UTILS_OPENURL, 1, (LPARAM)tData->url);
		}
		PUDeletePopup(hWnd);
		break;

	case WM_CONTEXTMENU:
		if (tData != NULL && tData->flags & MSN_HOTMAIL_POPUP) {
			HANDLE hContact = tData->proto->MSN_HContactFromEmail(tData->proto->MyOptions.szEmail, NULL);
			if (hContact)
				CallService(MS_CLIST_REMOVEEVENT, (WPARAM)hContact, (LPARAM) 1);
		}
		PUDeletePopup(hWnd);
		break;

	case UM_FREEPLUGINDATA:
		if (tData != NULL && tData != (PopupData*)CALLSERVICE_NOTFOUND) {
			mir_free(tData->title);
			mir_free(tData->text);
			mir_free(tData->url);
			mir_free(tData);
		}
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

/////////////////////////////////////////////////////////////////////////////////////////
// InitPopups - popup plugin support

void CMsnProto::InitPopups(void)
{
	TCHAR desc[256];
	char name[256];

	POPUPCLASS ppc = { sizeof(ppc) };
	ppc.flags = PCF_TCHAR;
	ppc.PluginWindowProc = NullWindowProc;
	ppc.hIcon = LoadIconEx("main");
	ppc.ptszDescription = desc;
	ppc.pszName = name;

	ppc.colorBack = RGB(173, 206, 247);
	ppc.colorText =  GetSysColor(COLOR_WINDOWTEXT);
	ppc.iSeconds = 3;
	mir_sntprintf(desc, SIZEOF(desc), _T("%s/%s"), m_tszUserName, TranslateT("Hotmail"));
	mir_snprintf(name, SIZEOF(name), "%s_%s", m_szModuleName, "Hotmail");
	hPopupHotmail = Popup_RegisterClass(&ppc);

	ppc.colorBack = RGB(173, 206, 247);
	ppc.colorText =  GetSysColor(COLOR_WINDOWTEXT);
	ppc.iSeconds = 3;
	mir_sntprintf(desc, SIZEOF(desc), _T("%s/%s"), m_tszUserName, TranslateT("Notify"));
	mir_snprintf(name, SIZEOF(name), "%s_%s", m_szModuleName, "Notify");
	hPopupNotify = Popup_RegisterClass(&ppc);

	ppc.hIcon = (HICON)LoadImage(NULL, IDI_WARNING, IMAGE_ICON, 0, 0, LR_SHARED);
	ppc.colorBack = RGB(191, 0, 0); //Red
	ppc.colorText = RGB(255, 245, 225); //Yellow
	ppc.iSeconds = 60;

	mir_sntprintf(desc, SIZEOF(desc), _T("%s/%s"), m_tszUserName, TranslateT("Error"));
	mir_snprintf(name, SIZEOF(name), "%s_%s", m_szModuleName, "Error");
	hPopupError = Popup_RegisterClass(&ppc);
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_ShowPopup - popup plugin support

void CALLBACK sttMainThreadCallback(PVOID dwParam)
{
	PopupData* pud = (PopupData*)dwParam;

	bool iserr = (pud->flags & MSN_SHOW_ERROR) != 0;
	if ((iserr && !pud->proto->MyOptions.ShowErrorsAsPopups) || !ServiceExists(MS_POPUP_ADDPOPUPCLASS)) {
		if (pud->flags & MSN_ALLOW_MSGBOX) {
			TCHAR szMsg[MAX_SECONDLINE + MAX_CONTACTNAME];
			mir_sntprintf(szMsg, SIZEOF(szMsg), _T("%s:\n%s"), pud->title, pud->text);
			MessageBox(NULL, szMsg, TranslateT("MSN Protocol"),
				MB_OK | (iserr ? MB_ICONERROR : MB_ICONINFORMATION));
		}
		mir_free(pud->title);
		mir_free(pud->text);
		mir_free(pud->url);
		mir_free(pud);
		return;
	}

	char name[256];

	POPUPDATACLASS ppd = { sizeof(ppd) };
	ppd.ptszTitle = pud->title;
	ppd.ptszText = pud->text;
	ppd.PluginData = pud;
	ppd.pszClassName = name;

	if (pud->flags & MSN_SHOW_ERROR)
		mir_snprintf(name, SIZEOF(name), "%s_%s", pud->proto->m_szModuleName, "Error");
	else if (pud->flags & (MSN_HOTMAIL_POPUP | MSN_ALERT_POPUP))
		mir_snprintf(name, SIZEOF(name), "%s_%s", pud->proto->m_szModuleName, "Hotmail");
	else
		mir_snprintf(name, SIZEOF(name), "%s_%s", pud->proto->m_szModuleName, "Notify");

	CallService(MS_POPUP_ADDPOPUPCLASS, 0, (LPARAM)&ppd);
}

void CMsnProto::MSN_ShowPopup(const TCHAR* nickname, const TCHAR* msg, int flags, const char* url, HANDLE hContact)
{
	if (Miranda_Terminated()) return;

	PopupData *pud = (PopupData*)mir_calloc(sizeof(PopupData));
	pud->flags = flags;
	pud->url = mir_strdup(url);
	pud->title = mir_tstrdup(nickname);
	pud->text = mir_tstrdup(msg);
	pud->proto = this;

	CallFunctionAsync(sttMainThreadCallback, pud);
}


void CMsnProto::MSN_ShowPopup(const HANDLE hContact, const TCHAR* msg, int flags)
{
	const TCHAR* nickname = hContact ? GetContactNameT(hContact) : _T("Me");
	MSN_ShowPopup(nickname, msg, flags, NULL, hContact);
}


/////////////////////////////////////////////////////////////////////////////////////////
// filetransfer class members

filetransfer::filetransfer(CMsnProto* prt)
{
	memset(this, 0, sizeof(filetransfer));
	fileId = -1;
	std.cbSize = sizeof(std);
	std.flags = PFTS_TCHAR;
	proto = prt;

	hLockHandle = CreateMutex(NULL, FALSE, NULL);
}

filetransfer::~filetransfer(void)
{
	if (p2p_sessionid)
		proto->MSN_DebugLog("Destroying file transfer session %08X", p2p_sessionid);

	WaitForSingleObject(hLockHandle, 2000);
	CloseHandle(hLockHandle);

	if (fileId != -1)
	{
		_close(fileId);
		if (p2p_appID != MSN_APPID_FILE && !(std.flags & PFTS_SENDING))
			proto->p2p_pictureTransferFailed(this);
	}

	if (!bCompleted && p2p_appID == MSN_APPID_FILE)
	{
		std.ptszFiles = NULL;
		std.totalFiles = 0;
		proto->ProtoBroadcastAck(std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, this, 0);
	}

	mir_free(p2p_branch);
	mir_free(p2p_callID);
	mir_free(p2p_dest);
	mir_free(p2p_object);

	mir_free(std.tszCurrentFile);
	mir_free(std.tszWorkingDir);
	if (std.ptszFiles != NULL)
	{
		for (int i=0; std.ptszFiles[i]; i++)
			mir_free(std.ptszFiles[i]);
		mir_free(std.ptszFiles);
	}

	mir_free(szInvcookie);
}

void filetransfer::close(void)
{
	if (fileId != -1) _close(fileId);
	fileId = -1;
}

void filetransfer::complete(void)
{
	close();

	bCompleted = true;
	proto->ProtoBroadcastAck(std.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, this, 0);
}

int filetransfer::create(void)
{
	fileId = _topen(std.tszCurrentFile, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);

	if (fileId == -1)
		proto->MSN_ShowError("Cannot create file '%s' during a file transfer", std.tszCurrentFile);
	//	else if (std.currentFileSize != 0)
	//		_chsize(fileId, std.currentFileSize);

	return fileId;
}

int filetransfer::openNext(void)
{
	if (fileId != -1)
	{
		close();
		++std.currentFileNumber;
		++cf;
	}

	while (std.ptszFiles && std.ptszFiles[cf])
	{
		struct _stati64 statbuf;
		if (_tstati64(std.ptszFiles[cf], &statbuf) == 0 && (statbuf.st_mode & _S_IFDIR) == 0)
			break;

		++cf;
	}

	if (std.ptszFiles && std.ptszFiles[cf])
	{
		bCompleted = false;
		replaceStrT(std.tszCurrentFile, std.ptszFiles[cf]);
		fileId = _topen(std.tszCurrentFile, _O_BINARY | _O_RDONLY, _S_IREAD);
		if (fileId != -1)
		{
			std.currentFileSize = _filelengthi64(fileId);
			std.currentFileProgress = 0;

			p2p_sendmsgid = 0;
			p2p_byemsgid = 0;
			tType = SERVER_NOTIFICATION;
			bAccepted = false;

			mir_free(p2p_branch); p2p_branch = NULL;
			mir_free(p2p_callID); p2p_callID = NULL;
		}
		else
			proto->MSN_ShowError("Unable to open file '%s' for the file transfer, error %d", std.tszCurrentFile, errno);
	}

	return fileId;
}

directconnection::directconnection(const char* CallID, const char* Wlid)
{
	memset(this, 0, sizeof(directconnection));

	wlid = mir_strdup(Wlid);
	callId = mir_strdup(CallID);
	mNonce = (UUID*)mir_alloc(sizeof(UUID));
	UuidCreate(mNonce);
	ts = time(NULL);
}

directconnection::~directconnection()
{
	mir_free(wlid);
	mir_free(callId);
	mir_free(mNonce);
	mir_free(xNonce);
}


char* directconnection::calcHashedNonce(UUID* nonce)
{
	mir_sha1_ctx sha1ctx;
	BYTE sha[MIR_SHA1_HASH_SIZE];

	mir_sha1_init(&sha1ctx);
	mir_sha1_append(&sha1ctx, (BYTE*)nonce, sizeof(UUID));
	mir_sha1_finish(&sha1ctx, sha);

	char* p;
	UuidToStringA((UUID*)&sha, (BYTE**)&p);
	size_t len = strlen(p) + 3;
	char* result = (char*)mir_alloc(len);
	mir_snprintf(result, len, "{%s}", p);
	_strupr(result);
	RpcStringFreeA((BYTE**)&p);

	return result;
}

char* directconnection::mNonceToText(void)
{
	char* p;
	UuidToStringA(mNonce, (BYTE**)&p);
	size_t len = strlen(p) + 3;
	char* result = (char*)mir_alloc(len);
	mir_snprintf(result, len, "{%s}", p);
	_strupr(result);
	RpcStringFreeA((BYTE**)&p);

	return result;
}


void directconnection::xNonceToBin(UUID* nonce)
{
	size_t len = strlen(xNonce);
	char *p = (char*)alloca(len);
	strcpy(p, xNonce + 1);
	p[len-2] = 0;
	UuidFromStringA((BYTE*)p, nonce);
}

/////////////////////////////////////////////////////////////////////////////////////////
// TWinErrorCode class

TWinErrorCode::TWinErrorCode() : mErrorText(NULL)
{
	mErrorCode = ::GetLastError();
}

TWinErrorCode::~TWinErrorCode()
{
	mir_free(mErrorText);
}

char* TWinErrorCode::getText()
{
	if (mErrorText == NULL)
		return NULL;

	int tBytes = 0;
	mErrorText = (char*)mir_alloc(256);

	if (tBytes == 0)
		tBytes = FormatMessageA(
		FORMAT_MESSAGE_FROM_SYSTEM, NULL,
		mErrorCode, LANG_NEUTRAL, mErrorText, 256, NULL);

	if (tBytes == 0)
		tBytes = mir_snprintf(mErrorText, 256, "unknown Windows error code %d", mErrorCode);

	*mErrorText = (char)tolower(*mErrorText);

	if (mErrorText[tBytes-1] == '\n')
		mErrorText[--tBytes] = 0;
	if (mErrorText[tBytes-1] == '\r')
		mErrorText[--tBytes] = 0;
	if (mErrorText[tBytes-1] == '.')
		mErrorText[tBytes-1] = 0;

	return mErrorText;
}

bool CMsnProto::MSN_IsMyContact(HANDLE hContact)
{
	const char* szProto = GetContactProto(hContact);
	return szProto != NULL && strcmp(m_szModuleName, szProto) == 0;
}

bool CMsnProto::MSN_IsMeByContact(HANDLE hContact, char* szEmail)
{
	char tEmail[MSN_MAX_EMAIL_LEN];
	char *emailPtr = szEmail ? szEmail : tEmail;

	*emailPtr = 0;
	if (getStaticString(hContact, "e-mail", emailPtr, sizeof(tEmail)))
		return false;

	return _stricmp(emailPtr, MyOptions.szEmail) == 0;
}

bool MSN_MsgWndExist(HANDLE hContact)
{
	MessageWindowInputData msgWinInData =
	{ sizeof(MessageWindowInputData), hContact, MSG_WINDOW_UFLAG_MSG_BOTH };
	MessageWindowData msgWinData = {0};
	msgWinData.cbSize = sizeof(MessageWindowData);

	bool res = CallService(MS_MSG_GETWINDOWDATA, (WPARAM)&msgWinInData, (LPARAM)&msgWinData) != 0;
	res = res || msgWinData.hwndWindow;
	if (res) {
		msgWinInData.hContact = (HANDLE)CallService(MS_MC_GETMETACONTACT, (WPARAM)hContact, 0);
		if (msgWinInData.hContact != NULL) {
			res = CallService(MS_MSG_GETWINDOWDATA, (WPARAM)&msgWinInData, (LPARAM)&msgWinData) != 0;
			res |= (msgWinData.hwndWindow == NULL);
		}
	}
	return res;
}

void MSN_MakeDigest(const char* chl, char* dgst)
{
	//Digest it
	DWORD md5hash[4], md5hashOr[4];
	mir_md5_state_t context;
	mir_md5_init(&context);
	mir_md5_append(&context, (BYTE*)chl, (int)strlen(chl));
	mir_md5_append(&context, (BYTE*)msnProtChallenge, (int)strlen(msnProtChallenge));
	mir_md5_finish(&context, (BYTE*)md5hash);

	memcpy(md5hashOr, md5hash, sizeof(md5hash));

	size_t i;
	for (i=0; i < 4; i++)
		md5hash[i] &= 0x7FFFFFFF;

	char chlString[128];
	mir_snprintf(chlString, sizeof(chlString), "%s%s00000000", chl, msnProductID);
	chlString[(strlen(chl)+strlen(msnProductID)+7) & 0xF8] = 0;

	LONGLONG high=0, low=0;
	int* chlStringArray = (int*)chlString;
	for (i=0; i < strlen(chlString) / 4; i += 2) {
		LONGLONG temp = chlStringArray[i];

		temp = (0x0E79A9C1 * temp) % 0x7FFFFFFF;
		temp += high;
		temp = md5hash[0] * temp + md5hash[1];
		temp = temp % 0x7FFFFFFF;

		high = chlStringArray[i + 1];
		high = (high + temp) % 0x7FFFFFFF;
		high = md5hash[2] * high + md5hash[3];
		high = high % 0x7FFFFFFF;

		low = low + high + temp;
	}
	high = (high + md5hash[1]) % 0x7FFFFFFF;
	low = (low + md5hash[3]) % 0x7FFFFFFF;

	md5hashOr[0] ^= high;
	md5hashOr[1] ^= low;
	md5hashOr[2] ^= high;
	md5hashOr[3] ^= low;

	char* str = arrayToHex((PBYTE)md5hashOr, sizeof(md5hashOr));
	strcpy(dgst, str);
	mir_free(str);
}

char* GetGlobalIp(void)
{
	NETLIBIPLIST* ihaddr = (NETLIBIPLIST*)CallService(MS_NETLIB_GETMYIP, 1, 0);
	for (unsigned i = 0; i < ihaddr->cbNum; ++i)
		if (strchr(ihaddr->szIp[i], ':'))
			return mir_strdup(ihaddr->szIp[i]);

	mir_free(ihaddr);
	return NULL;
}