summaryrefslogtreecommitdiff
path: root/plugins/SecureIM/src/svcs_proto.cpp
blob: 8d2aade1485411723b53f0cb0834d6972950c21d (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
#include "commonheaders.h"


// return SignID
int getSecureSig(LPCSTR szMsg, LPSTR *szPlainMsg=NULL) {
	if(szPlainMsg) *szPlainMsg=(LPSTR)szMsg;
	for(int i=0;signs[i].len;i++) {
		if (memcmp(szMsg,signs[i].sig,signs[i].len)==0) {
/*			for(int i=strlen(szMsg)-1;i;i--) {
				if ( szMsg[i] == '\x0D' || szMsg[i] == '\x0A' )
					((LPSTR)szMsg)[i] = '\0';
				else
					break;
			}*/
			if(szPlainMsg) *szPlainMsg = (LPSTR)(szMsg+signs[i].len);
			if(signs[i].key==SiG_GAME && !bDGP)
				return SiG_NONE;
			return signs[i].key;
		}
	}
	return SiG_NONE;
}


int returnNoError(HANDLE hContact) {
	HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
	unsigned int tID;
	CloseHandle( (HANDLE) _beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent,hContact,777,0), 0, &tID));
	SetEvent( hEvent );
	return 777;
}


int returnError(HANDLE hContact, LPCSTR err) {
	HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
	unsigned int tID;
	CloseHandle( (HANDLE) _beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent,hContact,666,err), 0, &tID));
	SetEvent( hEvent );
	return 666;
}


LPSTR szUnrtfMsg = NULL;


// RecvMsg handler
INT_PTR __cdecl onRecvMsg(WPARAM wParam, LPARAM lParam) {

	CCSDATA *pccsd = (CCSDATA *)lParam;
	PROTORECVEVENT *ppre = (PROTORECVEVENT *)pccsd->lParam;
	pUinKey ptr = getUinKey(pccsd->hContact);
	LPSTR szEncMsg = ppre->szMessage, szPlainMsg = NULL;

#if defined(_DEBUG) || defined(NETLIB_LOG)
	Sent_NetLog("onRecvMsg: %s", szEncMsg);
#endif

	// cut rtf tags
	if ( pRtfconvString && memcmp(szEncMsg,"{\\rtf1",6)==0 ) {
		SAFE_FREE(szUnrtfMsg);
		int len = (int)strlen(szEncMsg)+1;
		LPWSTR szTemp = (LPWSTR)mir_alloc(len*sizeof(WCHAR));
	   	if(ppre->flags & PREF_UNICODE)
	   		rtfconvW((LPWSTR)(szEncMsg+len),szTemp);
	   	else
	   		rtfconvA(szEncMsg,szTemp);
	   	len = (int)wcslen(szTemp)-1;
	   	while(len) {
	   		if ( szTemp[len] == 0x0D || szTemp[len] == 0x0A )
	   			szTemp[len] = 0;
	   		else
	   			break;
			len--;
	   	}
	   	len = (int)wcslen(&szTemp[1])+1;
	   	szUnrtfMsg = (LPSTR)mir_alloc(len*(sizeof(WCHAR)+1));
		WideCharToMultiByte(CP_ACP, 0, &szTemp[1], -1, szUnrtfMsg, len*(sizeof(WCHAR)+1), NULL, NULL);
		memcpy(szUnrtfMsg+len,&szTemp[1],len*sizeof(WCHAR));
	   	ppre->szMessage = szEncMsg = szUnrtfMsg;
	   	ppre->flags |= PREF_UNICODE;
	   	mir_free(szTemp);
	}

	int ssig = getSecureSig(ppre->szMessage,&szEncMsg);
	BOOL bSecured = isContactSecured(pccsd->hContact)&SECURED;
	BOOL bPGP = isContactPGP(pccsd->hContact);
	BOOL bGPG = isContactGPG(pccsd->hContact);

//	HANDLE hMetaContact = getMetaContact(pccsd->hContact);
//	if ( hMetaContact ) {
//		ptr = getUinKey(hMetaContact);
//	}

	// pass any unchanged message
	if ( !ptr ||
		ssig==SiG_GAME ||
		!isSecureProtocol(pccsd->hContact) ||
		(isProtoMetaContacts(pccsd->hContact) && (pccsd->wParam & PREF_SIMNOMETA)) ||
		isChatRoom(pccsd->hContact) ||
		(ssig==SiG_NONE && !ptr->msgSplitted && !bSecured && !bPGP && !bGPG)
	  ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: pass unhandled");
#endif
		return CallService(MS_PROTO_CHAINRECV, wParam, lParam);
	}

	// drop message: fake, unsigned or from invisible contacts
	if ( isContactInvisible(pccsd->hContact) || ssig==SiG_FAKE ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: drop unhandled (contact invisible or hidden)");
#endif
		return 1;
	}

	// receive non-secure message in secure mode
	if ( ssig==SiG_NONE && !ptr->msgSplitted ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: non-secure message");
#endif
		if(ppre->flags & PREF_UNICODE) {
			szPlainMsg = m_awstrcat(Translate(sim402),szEncMsg);
		}
		else {
			szPlainMsg = m_aastrcat(Translate(sim402),szEncMsg);
		}
		ppre->szMessage = szPlainMsg;
		pccsd->wParam |= PREF_SIMNOMETA;
		int ret = CallService(MS_PROTO_CHAINRECV, wParam, lParam);
		mir_free(szPlainMsg);
		return ret;
	}

	// received non-pgp secure message from disabled contact
	if ( ssig!=SiG_PGPM && !bPGP && !bGPG && ptr->status==STATUS_DISABLED ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
	    Sent_NetLog("onRecvMsg: message from disabled");
#endif
	    if ( ptr->mode==MODE_NATIVE ) {
		// tell to the other side that we have the plugin disabled with him
	    	pccsd->wParam |= PREF_METANODB;
		pccsd->lParam = (LPARAM) SIG_DISA;
		pccsd->szProtoService = PSS_MESSAGE;
		CallService(MS_PROTO_CHAINSEND, wParam, lParam);

		showPopUp(sim003,pccsd->hContact,g_hPOP[POP_PU_DIS],0);
	    }
	    else {
		createRSAcntx(ptr);
		exp->rsa_disabled(ptr->cntx);
		deleteRSAcntx(ptr);
	    }
	    SAFE_FREE(ptr->msgSplitted);
	    return 1;
	}

	// combine message splitted by protocol (no tags)
	if ( ssig==SiG_NONE && ptr->msgSplitted ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: combine untagged splitted message");
#endif
		LPSTR tmp = (LPSTR) mir_alloc(strlen(ptr->msgSplitted)+strlen(szEncMsg)+1);
		strcpy(tmp,ptr->msgSplitted);
		strcat(tmp,szEncMsg);
		mir_free(ptr->msgSplitted);
		ptr->msgSplitted = szEncMsg = ppre->szMessage = tmp;
		ssig = getSecureSig(tmp,&szEncMsg);
	}
	else {
		SAFE_FREE(ptr->msgSplitted);
	}

	// combine message splitted by secureim (with tags)
	if ( ssig==SiG_SECP || ssig==SiG_PART ) {
		LPSTR msg = combineMessage(ptr,szEncMsg);
		if ( !msg ) return 1;
		szEncMsg = ppre->szMessage = msg;
		ssig = getSecureSig(msg,&szEncMsg);
	}

	// decrypt PGP/GPG message
	if ( ssig==SiG_PGPM &&
	   ((bPGPloaded && (bPGPkeyrings || bPGPprivkey))||
	   (bGPGloaded && bGPGkeyrings))
	  ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: PGP/GPG message");
#endif
		szEncMsg = ppre->szMessage;
		if ( !ptr->cntx ) {
			ptr->cntx = cpp_create_context(((bGPGloaded && bGPGkeyrings)?CPP_MODE_GPG:CPP_MODE_PGP) | ((db_get_b(pccsd->hContact,szModuleName,"gpgANSI",0))?CPP_MODE_GPG_ANSI:0));
			ptr->keyLoaded = 0;
		}

		if (!strstr(szEncMsg,"-----END PGP MESSAGE-----"))
			return 1; // no end tag, don't display it ...

		LPSTR szNewMsg = NULL;
		LPSTR szOldMsg = NULL;

		if (!ptr->keyLoaded && bPGPloaded) ptr->keyLoaded = LoadKeyPGP(ptr);
		if (!ptr->keyLoaded && bGPGloaded) ptr->keyLoaded = LoadKeyGPG(ptr);

		if(ptr->keyLoaded==1) szOldMsg = pgp_decode(ptr->cntx, szEncMsg);
		else
		if(ptr->keyLoaded==2) szOldMsg = gpg_decode(ptr->cntx, szEncMsg);

		if (!szOldMsg) { // error while decrypting message, send error
			SAFE_FREE(ptr->msgSplitted);
			ppre->flags &= ~(PREF_UNICODE|PREF_UTF);
			pccsd->wParam &= ~(PREF_UNICODE|PREF_UTF);
			ppre->szMessage = Translate(sim401);
			return CallService(MS_PROTO_CHAINRECV, wParam, lParam);
		}

		// receive encrypted message in non-encrypted mode
		if (!isContactPGP(pccsd->hContact) && !isContactGPG(pccsd->hContact)) {
			szNewMsg = m_ustrcat(TranslateU(sim403),szOldMsg);
			szOldMsg = szNewMsg;
		}
		szNewMsg = utf8_to_miranda(szOldMsg,ppre->flags); pccsd->wParam = ppre->flags;
		ppre->szMessage = szNewMsg;

		// show decoded message
		showPopUpRM(ptr->hContact);
		SAFE_FREE(ptr->msgSplitted);
		pccsd->wParam |= PREF_SIMNOMETA;
		int ret = CallService(MS_PROTO_CHAINRECV, wParam, lParam);
		mir_free(szNewMsg);
		return ret;
	}

#if defined(_DEBUG) || defined(NETLIB_LOG)
	Sent_NetLog("onRecvMsg: switch(ssig)=%d",ssig);
#endif
	switch(ssig) {

	case SiG_PGPM:
		return CallService(MS_PROTO_CHAINRECV, wParam, lParam);

	case SiG_SECU: { // new secured msg, pass to rsa_recv
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: RSA/AES message");
#endif
		if ( ptr->mode==MODE_NATIVE ) {
		    ptr->mode = MODE_RSAAES;
		    deleteRSAcntx(ptr);
		    db_set_b(ptr->hContact, szModuleName, "mode", ptr->mode);
		}
		createRSAcntx(ptr);
		loadRSAkey(ptr);
		if ( exp->rsa_get_state(ptr->cntx)==0 )
		    showPopUpKR(ptr->hContact);

		LPSTR szOldMsg = exp->rsa_recv(ptr->cntx,szEncMsg);
		if ( !szOldMsg )	return 1; // don't display it ...

		LPSTR szNewMsg = utf8_to_miranda(szOldMsg,ppre->flags); pccsd->wParam = ppre->flags;
		ppre->szMessage = szNewMsg;

		// show decoded message
		showPopUpRM(ptr->hContact);
		SAFE_FREE(ptr->msgSplitted);
		pccsd->wParam |= PREF_SIMNOMETA;
		int ret = CallService(MS_PROTO_CHAINRECV, wParam, lParam);
		mir_free(szNewMsg);
		return ret;
	} break;

	case SiG_ENON: { // online message
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: Native SiG_ENON message");
#endif
		if ( cpp_keyx(ptr->cntx)) {
			// decrypting message
			szPlainMsg = decodeMsg(ptr,lParam,szEncMsg);
			if (!ptr->decoded) {
				mir_free(szPlainMsg);
				SAFE_FREE(ptr->msgSplitted);
				ptr->msgSplitted=mir_strdup(szEncMsg);
				return 1; // don't display it ...
			}
//			showPopUpRM(ptr->hContact);
		}
		else {
			// reinit key exchange user has send an encrypted message and i have no key
			cpp_reset_context(ptr->cntx);

			LPSTR reSend = (LPSTR) mir_alloc(strlen(szEncMsg)+LEN_RSND);
			strcpy(reSend,SIG_RSND); // copy resend sig
			strcat(reSend,szEncMsg); // add mess

	    		pccsd->wParam |= PREF_METANODB;
			pccsd->lParam = (LPARAM) reSend; // reSend Message to reemit
			pccsd->szProtoService = PSS_MESSAGE;
			CallService(MS_PROTO_CHAINSEND, wParam, lParam); // send back cipher message
			mir_free(reSend);

			LPSTR keyToSend = InitKeyA(ptr,0); // calculate public and private key

			pccsd->lParam = (LPARAM) keyToSend;
			CallService(MS_PROTO_CHAINSEND, wParam, lParam); // send new key
			mir_free(keyToSend);

			showPopUp(sim005,NULL,g_hPOP[POP_PU_DIS],0);
			showPopUpKS(ptr->hContact);

			return 1;
		}
	} break;

	case SiG_ENOF: { // offline message
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: Native SiG_ENOF message");
#endif
		// if offline key is set and we have not an offline message unset key
		if (ptr->offlineKey && cpp_keyx(ptr->cntx)) {
			cpp_reset_context(ptr->cntx);
			ptr->offlineKey = false;
		}
		// decrypting message with last offline key
		DBVARIANT dbv;
		dbv.type = DBVT_BLOB;

		if ( DBGetContactSetting(ptr->hContact,szModuleName,"offlineKey",&dbv) == 0 ) {
			// if valid key is succefully retrieved
			ptr->offlineKey = true;
			InitKeyX(ptr,dbv.pbVal);
			DBFreeVariant(&dbv);

			// decrypting message
			szPlainMsg = decodeMsg(ptr,lParam,szEncMsg);

//			showPopUpRM(ptr->hContact);
			ShowStatusIconNotify(ptr->hContact);
		}
		else {
			// exit and show messsage
			return CallService(MS_PROTO_CHAINRECV, wParam, lParam);
		}
	} break;

	case SiG_RSND: { // resend message
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: Native SiG_RSND message");
#endif
		if (cpp_keyx(ptr->cntx)) {
			// decrypt sended back message and save message for future sending with a new secret key
			szPlainMsg = decodeMsg(ptr,(LPARAM)pccsd,szEncMsg);
			addMsg2Queue(ptr,pccsd->wParam,szPlainMsg);
			mir_free(szPlainMsg);

			showPopUpRM(ptr->hContact);
			showPopUp(sim004,NULL,g_hPOP[POP_PU_DIS],0);
		}
		return 1; // don't display it ...
	} break;

	case SiG_DISA: { // disabled message
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: Native SiG_DISA message");
#endif
//		ptr->status=ptr->tstatus=STATUS_DISABLED;
//		db_set_b(ptr->hContact, szModuleName, "StatusID", ptr->status);
	}
	case SiG_DEIN: { // deinit message
		// other user has disabled SecureIM with you
		cpp_delete_context(ptr->cntx); ptr->cntx=0;

		showPopUpDC(ptr->hContact);
		ShowStatusIconNotify(ptr->hContact);

		waitForExchange(ptr,3); // äîñëàòü íåøèôðîâàííî
		return 1;
	} break;

	case SiG_KEYR:   // key3 message
	case SiG_KEYA:   // keyA message
	case SiG_KEYB: { // keyB message
		if ( ptr->mode==MODE_RSAAES ) {
		    ptr->mode = MODE_NATIVE;
		    cpp_delete_context(ptr->cntx);
		    ptr->cntx = 0;
		    ptr->keyLoaded = 0;
		    db_set_b(ptr->hContact, szModuleName, "mode", ptr->mode);
		}
		switch(ssig) {
		case SiG_KEYR: { // key3 message
#if defined(_DEBUG) || defined(NETLIB_LOG)
			Sent_NetLog("onRecvMsg: SiG_KEYR received");
#endif
			// receive KeyB from user;
			showPopUpKR(ptr->hContact);

			if ( ptr->cntx && cpp_keyb(ptr->cntx)) {
				// reinit key exchange if an old key from user is found
				cpp_reset_context(ptr->cntx);
			}

			if ( InitKeyB(ptr,szEncMsg)!=CPP_ERROR_NONE ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("onRecvMsg: SiG_KEYR InitKeyB error");
#endif
				// tell to the other side that we have the plugin disabled with him
/*
	    			pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM) SIG_DISA;
				pccsd->szProtoService = PSS_MESSAGE;
				CallService(MS_PROTO_CHAINSEND, wParam, lParam);
*/
				showPopUp(sim013,ptr->hContact,g_hPOP[POP_PU_DIS],0);
				ShowStatusIconNotify(ptr->hContact);

				waitForExchange(ptr,3); // äîñëàòü íåøèôðîâàííî
				return 1;
			}

			// other side support RSA mode ?
			if ( ptr->features & CPP_FEATURES_RSA ) {
				// switch to RSAAES mode
				ptr->mode = MODE_RSAAES;
				db_set_b(ptr->hContact, szModuleName, "mode", ptr->mode);

				resetRSAcntx(ptr);
				loadRSAkey(ptr);
				exp->rsa_connect(ptr->cntx);

				showPopUpKS(pccsd->hContact);
				ShowStatusIconNotify(pccsd->hContact);
				return 1;
			}

			// other side support new key format ?
			if ( ptr->features & CPP_FEATURES_NEWPG ) {
				cpp_reset_context(ptr->cntx);

				LPSTR keyToSend = InitKeyA(ptr,CPP_FEATURES_NEWPG|KEY_A_SIG); // calculate NEW public and private key
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("onRecvMsg: Sending KEYA %s", keyToSend);
#endif
	    			pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM)keyToSend;
				pccsd->szProtoService = PSS_MESSAGE;
				CallService(MS_PROTO_CHAINSEND, wParam, lParam);
				mir_free(keyToSend);

				showPopUpKS(ptr->hContact);
				waitForExchange(ptr); // çàïóñòèì îæèäàíèå
				return 1;
			}

			// auto send my public key to keyB user if not done before
			if ( !cpp_keya(ptr->cntx)) {
				LPSTR keyToSend = InitKeyA(ptr,0); // calculate public and private key
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("onRecvMsg: Sending KEYA %s", keyToSend);
#endif
	    			pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM)keyToSend;
				pccsd->szProtoService = PSS_MESSAGE;
				CallService(MS_PROTO_CHAINSEND, wParam, lParam);
				mir_free(keyToSend);

				showPopUpKS(ptr->hContact);
			}
		} break;

		case SiG_KEYA: { // keyA message
#if defined(_DEBUG) || defined(NETLIB_LOG)
			Sent_NetLog("onRecvMsg: SiG_KEYA received");
#endif
			// receive KeyA from user;
			showPopUpKR(ptr->hContact);

			cpp_reset_context(ptr->cntx);
			if(InitKeyB(ptr,szEncMsg)!=CPP_ERROR_NONE) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("onRecvMsg: SiG_KEYA InitKeyB error");
#endif
				// tell to the other side that we have the plugin disabled with him
/*
	    			pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM) SIG_DISA;
				pccsd->szProtoService = PSS_MESSAGE;
				CallService(MS_PROTO_CHAINSEND, wParam, lParam);
*/
				showPopUp(sim013,ptr->hContact,g_hPOP[POP_PU_DIS],0);
				ShowStatusIconNotify(ptr->hContact);

				waitForExchange(ptr,3); // äîñëàòü íåøèôðîâàííî
				return 1;
			}

			LPSTR keyToSend = InitKeyA(ptr,CPP_FEATURES_NEWPG|KEY_B_SIG); // calculate NEW public and private key
#if defined(_DEBUG) || defined(NETLIB_LOG)
			Sent_NetLog("onRecvMsg: Sending KEYB %s", keyToSend);
#endif
	    		pccsd->wParam |= PREF_METANODB;
			pccsd->lParam = (LPARAM)keyToSend;
			pccsd->szProtoService = PSS_MESSAGE;
			CallService(MS_PROTO_CHAINSEND, wParam, lParam);
			mir_free(keyToSend);
		} break;

		case SiG_KEYB: { // keyB message
#if defined(_DEBUG) || defined(NETLIB_LOG)
			Sent_NetLog("onRecvMsg: SiG_KEYB received");
#endif
			// receive KeyB from user;
			showPopUpKR(ptr->hContact);

			// clear all and send DISA if received KeyB, and not exist KeyA or error on InitKeyB
			if (!cpp_keya(ptr->cntx) || InitKeyB(ptr,szEncMsg)!=CPP_ERROR_NONE) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("onRecvMsg: SiG_KEYB InitKeyB error");
#endif
				// tell to the other side that we have the plugin disabled with him
/*
	    			pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM) SIG_DISA;
				pccsd->szProtoService = PSS_MESSAGE;
				CallService(MS_PROTO_CHAINSEND, wParam, lParam);
*/
				showPopUp(sim013,ptr->hContact,g_hPOP[POP_PU_DIS],0);
				ShowStatusIconNotify(ptr->hContact);

				cpp_reset_context(ptr->cntx);
				waitForExchange(ptr,3); // äîñëàòü íåøèôðîâàííî
				return 1;
			}
		} break;

		} // switch

		/* common part (CalcKeyX & SendQueue) */
		//  calculate KeyX
		if ( cpp_keya(ptr->cntx) && cpp_keyb(ptr->cntx) && !cpp_keyx(ptr->cntx))
			CalculateKeyX(ptr,ptr->hContact);

		ShowStatusIconNotify(ptr->hContact);
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: Session established");
#endif

		waitForExchange(ptr,2); // äîøëåì ÷åðåç øèôðîâàííîå ñîåäèíåíèå
		return 1;
		/* common part (CalcKeyX & SendQueue) */
	} break;

	} //switch

	// receive message
	if ( cpp_keyx(ptr->cntx) && (ssig==SiG_ENON||ssig==SiG_ENOF)) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onRecvMsg: message received");
#endif
		showPopUpRM(ptr->hContact);
	}
#if defined(_DEBUG) || defined(NETLIB_LOG)
	Sent_NetLog("onRecvMsg: exit");
#endif
	pccsd->wParam |= PREF_SIMNOMETA;
	int ret = CallService(MS_PROTO_CHAINRECV, wParam, lParam);
	SAFE_FREE(szPlainMsg);
	return ret;
}


// SendMsgW handler
INT_PTR __cdecl onSendMsgW(WPARAM wParam, LPARAM lParam) {
	if (!lParam) return 0;

	CCSDATA *ccs = (CCSDATA *) lParam;
	if ( !(ccs->wParam & PREF_UTF))
		ccs->wParam |= PREF_UNICODE;
	
	return onSendMsg(wParam, lParam);
}


// SendMsg handler
INT_PTR __cdecl onSendMsg(WPARAM wParam, LPARAM lParam) {

	CCSDATA *pccsd = (CCSDATA *)lParam;
	pUinKey ptr = getUinKey(pccsd->hContact);
	int ssig = getSecureSig((LPCSTR)pccsd->lParam);
	int stat = getContactStatus(pccsd->hContact);

//	HANDLE hMetaContact = getMetaContact(pccsd->hContact);
//	if ( hMetaContact ) {
//		ptr = getUinKey(hMetaContact);
//	}
#if defined(_DEBUG) || defined(NETLIB_LOG)
	Sent_NetLog("onSend: %s",(LPSTR)pccsd->lParam);
#endif
	// pass unhandled messages
	if ( !ptr ||
		ssig==SiG_GAME || ssig==SiG_PGPM || ssig==SiG_SECU || ssig==SiG_SECP ||
		isChatRoom(pccsd->hContact) ||
/*		(ssig!=SiG_NONE && hMetaContact && (pccsd->wParam & PREF_METANODB)) || */
		stat==-1 ||
		(ssig==SiG_NONE && ptr->sendQueue) ||
		(ssig==SiG_NONE && ptr->status==STATUS_DISABLED) // Disabled - pass unhandled
	   ) {
		return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: pass unhandled");
#endif
	}

	//
	// PGP/GPG mode
	// 
	if ( ptr->mode==MODE_PGP || ptr->mode==MODE_GPG ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
	    Sent_NetLog("onSendMsg: PGP|GPG mode");
#endif
	    // åñëè ìîæíî çàøèôðîâàòü - øèôðóåì
            if ( isContactPGP(ptr->hContact) || isContactGPG(ptr->hContact)) {
/*
		if(stat==ID_STATUS_OFFLINE) {
			if (msgbox1(0,sim110,szModuleName,MB_YESNO|MB_ICONQUESTION)==IDNO) {
				return returnNoError(pccsd->hContact);
			}
			// exit and send unencrypted message
			return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
		}
*/
		if ( !ptr->cntx ) {
			ptr->cntx = cpp_create_context((isContactGPG(ptr->hContact)?CPP_MODE_GPG:CPP_MODE_PGP) | ((db_get_b(ptr->hContact,szModuleName,"gpgANSI",0))?CPP_MODE_GPG_ANSI:0));
			ptr->keyLoaded = 0;
		}
		if ( !ptr->keyLoaded && bPGPloaded ) ptr->keyLoaded = LoadKeyPGP(ptr);
		if ( !ptr->keyLoaded && bGPGloaded ) ptr->keyLoaded = LoadKeyGPG(ptr);
		if ( !ptr->keyLoaded ) return returnError(pccsd->hContact,Translate(sim108));

		LPSTR szNewMsg = NULL;
		LPSTR szUtfMsg = miranda_to_utf8((LPCSTR)pccsd->lParam,pccsd->wParam);
		if ( ptr->keyLoaded == 1 ) { // PGP
    			szNewMsg = pgp_encode(ptr->cntx,szUtfMsg);
    		}
    		else
		if ( ptr->keyLoaded == 2 ) { // GPG
    			szNewMsg = gpg_encode(ptr->cntx,szUtfMsg);
		}
		mir_free(szUtfMsg);
		if ( !szNewMsg ) {
			return returnError(pccsd->hContact,Translate(sim109));
		}

		// îòïðàâëÿåì çàøèôðîâàííîå ñîîáùåíèå
		splitMessageSend(ptr,szNewMsg);

		showPopUpSM(ptr->hContact);

		return returnNoError(pccsd->hContact);
	    }
	    else {
	    	// îòïðàâëÿåì íåçàøèôðîâàííîå
		return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	    }
	}

	// get contact SecureIM status
	int stid = ptr->status;

	//
	// RSA/AES mode
	//
	if ( ptr->mode==MODE_RSAAES ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: RSA/AES mode");
#endif
		// contact is offline
		if ( stat==ID_STATUS_OFFLINE ) {
        		if ( ptr->cntx ) {
        			if ( exp->rsa_get_state(ptr->cntx)!=0 ) {
        				resetRSAcntx(ptr);
				}
        		}
        		else {
        			createRSAcntx(ptr);
        		}
			if ( !bSOM || (!isClientMiranda(ptr,1) && !isSecureIM(ptr,1)) || !loadRSAkey(ptr)) {
				if ( ssig==SiG_NONE ) {
        				// ïðîñòî øëåì íåçàøèôðîâàííîå â îôôëàéí
					return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
				}
				else {
					// íè÷åãî íå øëåì äàëüøå - ýòî ñëóæåáíîå ñîîáùåíèå
					return returnNoError(pccsd->hContact);
				}
			}
			// øëåì øèôðîâàííîå â îôôëàéí
			LPSTR szUtfMsg = miranda_to_utf8((LPCSTR)pccsd->lParam,pccsd->wParam);
			exp->rsa_send(ptr->cntx,szUtfMsg);
			mir_free(szUtfMsg);
			showPopUpSM(ptr->hContact);
			return returnNoError(pccsd->hContact);
		}
		// SecureIM connection with this contact is disabled
		if ( stid==STATUS_DISABLED ) {
			if ( ptr->cntx ) {
				exp->rsa_disabled(ptr->cntx);
				deleteRSAcntx(ptr);
			}
			if ( ssig==SiG_NONE ) {
			    // ïðîñòî øëåì íåçàøèôðîâàííîå
			    return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
			}
			// íè÷åãî íå øëåì äàëüøå - ýòî ñëóæåáíîå ñîîáùåíèå
			return returnNoError(pccsd->hContact);
		}
		// ðàçîðâàòü ñîåäèíåíèå
		if ( ssig==SiG_DEIN ) {
			if ( ptr->cntx ) {
				exp->rsa_disconnect(ptr->cntx);
				deleteRSAcntx(ptr);
			}
			waitForExchange(ptr,3); // äîøëåì íåøèôðîâàííî
			return returnNoError(pccsd->hContact);
		}
		// ñîåäèíåíèå óñòàíîâëåíî
		if ( ptr->cntx && exp->rsa_get_state(ptr->cntx)==7 ) {
			LPSTR szUtfMsg = miranda_to_utf8((LPCSTR)pccsd->lParam,pccsd->wParam);
			exp->rsa_send(ptr->cntx,szUtfMsg);
			mir_free(szUtfMsg);
			showPopUpSM(ptr->hContact);
			return returnNoError(pccsd->hContact);
		}
		// ïðîñòî ñîîáùåíèå (áåç òýãîâ, íåò êîíòåêñòà è ðàáîòàþò AIP & NOL)
		if ( ssig==SiG_NONE && isSecureIM(ptr->hContact)) {
			// äîáàâèì åãî â î÷åðåäü
			addMsg2Queue(ptr, pccsd->wParam, (LPSTR)pccsd->lParam);
			// çàïóñêàåì ïðîöåññ óñòàíîâêè ñîåäèíåíèÿ
			ssig=SiG_INIT;
			// çàïóñêàåì òðýä îæèäàíèÿ è äîñûëêè
			waitForExchange(ptr);
		}
		// óñòàíîâèòü ñîåäèíåíèå
		if ( ssig==SiG_INIT ) {
			createRSAcntx(ptr);
			loadRSAkey(ptr);
			exp->rsa_connect(ptr->cntx);
			showPopUpKS(pccsd->hContact);
			ShowStatusIconNotify(pccsd->hContact);
			return returnNoError(pccsd->hContact);
		}
		// ïðîñòî øëåì íåçàøèôðîâàííîå (íå çíàþ äàæå êîãäà òàêîå ñëó÷èòñÿ)
		return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	}

	//
	// Native mode
	//
#if defined(_DEBUG) || defined(NETLIB_LOG)
	Sent_NetLog("onSendMsg: Native mode");
#endif

	// SecureIM connection with this contact is disabled
	if ( stid==STATUS_DISABLED ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: message for Disabled");
#endif
		// if user try initialize connection
		if ( ssig==SiG_INIT ) {
			// secure IM is disabled ...
			return returnError(pccsd->hContact,Translate(sim105));
		}
		if (ptr->cntx) { // if exist secure context
			cpp_delete_context(ptr->cntx); ptr->cntx=0;

			CCSDATA ccsd;
			memcpy(&ccsd, (HLOCAL)lParam, sizeof(CCSDATA));

	    		pccsd->wParam |= PREF_METANODB;
			ccsd.lParam = (LPARAM) SIG_DEIN;
			ccsd.szProtoService = PSS_MESSAGE;
			CallService(MS_PROTO_CHAINSEND, wParam, (LPARAM)&ccsd);

			showPopUpDC(pccsd->hContact);
			ShowStatusIconNotify(pccsd->hContact);
		}
		return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	}

	// contact is offline
	if ( stat==ID_STATUS_OFFLINE ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: message for offline");
#endif
		if ( ssig==SiG_INIT && cpp_keyx(ptr->cntx)) {
			// reinit key exchange
			cpp_reset_context(ptr->cntx);
		}

		if ( !bSOM ) {
		    if ( ssig!=SiG_NONE ) {
				return returnNoError(pccsd->hContact);
		    }
		    // exit and send unencrypted message
		    return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
		}
		BOOL isMiranda = isClientMiranda(ptr->hContact);

		if ( stid==STATUS_ALWAYSTRY && isMiranda ) {  // always try && Miranda
			// set key for offline user
			DBVARIANT dbv; dbv.type = DBVT_BLOB;
			if ( DBGetContactSettingDword(ptr->hContact, szModuleName, "offlineKeyTimeout", 0) > gettime() &&
			    DBGetContactSetting(ptr->hContact, szModuleName, "offlineKey", &dbv) == 0
			  ) {
				// if valid key is succefully retrieved
				ptr->offlineKey = true;
				InitKeyX(ptr,dbv.pbVal);
				DBFreeVariant(&dbv);
			}
			else {
				DBDeleteContactSetting(ptr->hContact,szModuleName,"offlineKey");
				DBDeleteContactSetting(ptr->hContact,szModuleName,"offlineKeyTimeout");
				if (msgbox1(0,sim106,szModuleName,MB_YESNO|MB_ICONQUESTION)==IDNO) {
					return returnNoError(pccsd->hContact);
				}
				// exit and send unencrypted message
				return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
			}
		}
		else {
/*			if (stid==STATUS_ALWAYSTRY && !isMiranda || stid!=STATUS_ALWAYSTRY && isMiranda) {
				int res=msgbox1(0,"User is offline now, Do you want to send your message ?\nIt will be unencrypted !","Can't Send Encrypted Message !",MB_YESNO);
				if (res==IDNO) return 1;
			}*/
		    if ( ssig!=SiG_NONE ) {
			return returnNoError(pccsd->hContact);
		    }
			// exit and send unencrypted message
			return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
		}

	}
	else {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: message for online");
#endif
		// contact is online and we use an offline key -> reset offline key
		if ( ptr->offlineKey ) {
			cpp_reset_context(ptr->cntx);
			ptr->offlineKey = false;
			ShowStatusIconNotify(ptr->hContact);
		}
	}

	// if init is called from contact menu list reinit secure im connection
	if ( ssig==SiG_INIT ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: SiG_INIT");
#endif
		cpp_reset_context(ptr->cntx);
	}

	// if deinit is called from contact menu list deinit secure im connection
	if ( ssig==SiG_DEIN ) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: SiG_DEIN");
#endif
		// disable SecureIM only if it was enabled
		if (ptr->cntx) {
			cpp_delete_context(ptr->cntx); ptr->cntx=0;

			pccsd->wParam |= PREF_METANODB;
			CallService(MS_PROTO_CHAINSEND, wParam, lParam);

			showPopUpDC(pccsd->hContact);
			ShowStatusIconNotify(pccsd->hContact);
		}
		return returnNoError(pccsd->hContact);
	}

	if ( cpp_keya(ptr->cntx) && cpp_keyb(ptr->cntx) && !cpp_keyx(ptr->cntx))
		CalculateKeyX(ptr,ptr->hContact);

	ShowStatusIconNotify(pccsd->hContact);

	// if cryptokey exist
	if ( cpp_keyx(ptr->cntx)) {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: cryptokey exist");
#endif
/*	    if ( !hMetaContact && isProtoMetaContacts(pccsd->hContact) && (db_get_b(NULL, "MetaContacts", "SubcontactHistory", 1) == 1)) {
		// add sent event to subcontact
    		DBEVENTINFO dbei; HANDLE hC = getMostOnline(pccsd->hContact);
		ZeroMemory(&dbei, sizeof(dbei));
		dbei.cbSize = sizeof(dbei);
		dbei.szModule = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hC, 0);
		dbei.flags = DBEF_SENT;
		dbei.timestamp = time(NULL);
		dbei.eventType = EVENTTYPE_MESSAGE;
		if(pccsd->wParam & PREF_RTL) dbei.flags |= DBEF_RTL;
		if(pccsd->wParam & PREF_UTF) dbei.flags |= DBEF_UTF;
		dbei.cbBlob = strlen((char *)pccsd->lParam) + 1;
		if ( pccsd->wParam & PREF_UNICODE )
			dbei.cbBlob *= ( sizeof( wchar_t )+1 );
		dbei.pBlob = (PBYTE)pccsd->lParam;

		CallService(MS_DB_EVENT_ADD, (WPARAM)hC, (LPARAM)&dbei);
	    } */

	    LPSTR szNewMsg = encodeMsg(ptr,(LPARAM)pccsd);

#if defined(_DEBUG) || defined(NETLIB_LOG)
	    Sent_NetLog("onSend: encrypted msg '%s'",szNewMsg);
#endif

	    pccsd->wParam |= PREF_METANODB;
	    pccsd->lParam = (LPARAM) szNewMsg;
            pccsd->szProtoService = PSS_MESSAGE;
            int ret = CallService(MS_PROTO_CHAINSEND, wParam, lParam);

	    mir_free(szNewMsg);

	    showPopUpSM(ptr->hContact);

	    return ret;
	}
	else {
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: cryptokey not exist, try establishe connection");
#endif
	  	// send KeyA if init || always_try || waitkey || always_if_possible
  		if ( ssig==SiG_INIT || (stid==STATUS_ALWAYSTRY && isClientMiranda(ptr->hContact)) || isSecureIM(ptr->hContact) || ptr->waitForExchange ) {
			if (ssig==SiG_NONE) {
				addMsg2Queue(ptr, pccsd->wParam, (LPSTR)pccsd->lParam);
			}
			if ( !ptr->waitForExchange ) {
				// init || always_try || always_if_possible
				LPSTR keyToSend = InitKeyA(ptr,0);	// calculate public and private key & fill KeyA
#if defined(_DEBUG) || defined(NETLIB_LOG)
				Sent_NetLog("Sending KEY3: %s", keyToSend);
#endif
				pccsd->wParam &= ~PREF_UNICODE;
				pccsd->wParam |= PREF_METANODB;
				pccsd->lParam = (LPARAM) keyToSend;
	  			pccsd->szProtoService = PSS_MESSAGE;
	  			CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	  			mir_free(keyToSend);

	  			showPopUpKS(pccsd->hContact);
	  			ShowStatusIconNotify(pccsd->hContact);

				waitForExchange(ptr); // çàïóñêàåì îæèäàíèå
	  		}
			return returnNoError(pccsd->hContact);
	  	}
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("onSendMsg: pass unchanged to chain");
#endif
 		return CallService(MS_PROTO_CHAINSEND, wParam, lParam);
	}
}


int file_idx = 0;

INT_PTR __cdecl onSendFile(WPARAM wParam, LPARAM lParam) {

	CCSDATA *pccsd=(CCSDATA*)lParam;

	pUinKey ptr = getUinKey(pccsd->hContact);
	if (!ptr || !bSFT) return CallService(PSS_FILE, wParam, lParam);

	if ( isContactSecured(pccsd->hContact)&SECURED ) {

		char **file=(char **)pccsd->lParam;
		if(file_idx==100) file_idx=0;
		int i;
		for(i=0;file[i];i++) {

			if (strstr(file[i],".AESHELL")) continue;

		    char *name = strrchr(file[i],'\\');
		    if ( !name ) name = file[i];
		    else name++;

			char *file_out = (char*) mir_alloc(TEMP_SIZE+strlen(name)+20);
			sprintf(file_out,"%s\\%s.AESHELL(%d)",TEMP,name,file_idx++);

			char buf[MAX_PATH];
			sprintf(buf,"%s\n%s",Translate(sim011),file[i]);
			showPopUp(buf,NULL,g_hPOP[POP_PU_MSS],2);

			if ( ptr->mode==MODE_RSAAES ) {
				exp->rsa_encrypt_file(ptr->cntx,file[i],file_out);
			}
			else {
				cpp_encrypt_file(ptr->cntx,file[i],file_out);
			}

			mir_free(file[i]);
			file[i]=file_out;
		}
		if ( ptr->fileSend ) { // î÷èñòèì ñîõðàíåííûé ñïèñîê
			for(int j=0;ptr->fileSend[j];j++) {
				mir_free(ptr->fileSend[j]);
			}
			SAFE_FREE(ptr->fileSend);
		}
		if ( i ) { // ñêîïèðóåì íîâûé ñïèñîê
			ptr->fileSend = (char **) mir_alloc(sizeof(char*)*(i+1));
			for(i=0;file[i];i++) {
				ptr->fileSend[i] = mir_strdup(file[i]);
			}
			ptr->fileSend[i] = NULL;
		}
	}
	return CallService(PSS_FILE, wParam, lParam);
}


/*
typedef struct {
	size_t cbSize;
	HANDLE hContact;
	int sending;	//true if sending, false if receiving
	char **files;
	int totalFiles;
	int currentFileNumber;
	unsigned long totalBytes;
	unsigned long totalProgress;
	char *workingDir;
	char *currentFile;
	unsigned long currentFileSize;
	unsigned long currentFileProgress;
	unsigned long currentFileTime;  //as seconds since 1970
} PROTOFILETRANSFERSTATUS;
*/

int __cdecl onProtoAck(WPARAM wParam,LPARAM lParam) {

	ACKDATA *ack=(ACKDATA*)lParam;
	if (ack->type!=ACKTYPE_FILE) return 0; //quit if not file transfer event
	PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*) ack->lParam;

	pUinKey ptr = getUinKey(ack->hContact);
	if (!ptr || (f && (f->flags & PFTS_SENDING) && !bSFT)) return 0;

	if ( isContactSecured(ack->hContact)&SECURED ) {
		switch(ack->result) {
//		case ACKRESULT_FILERESUME:
		case ACKRESULT_DATA: {
			if ( !( f->flags & PFTS_SENDING )) {
				ptr->finFileRecv = (f->currentFileSize == f->currentFileProgress);
				if ( !ptr->lastFileRecv ) ptr->lastFileRecv = mir_strdup(f->szCurrentFile);
			}
			else
			if ( f->flags & PFTS_SENDING ) {
				ptr->finFileSend = (f->currentFileSize == f->currentFileProgress);
				if ( !ptr->lastFileSend ) ptr->lastFileSend = mir_strdup(f->szCurrentFile);
			}
		} break;
//		case ACKRESULT_INITIALISING:
		case ACKRESULT_DENIED:
		case ACKRESULT_FAILED: {
			if ( ptr->lastFileRecv ) {
				if (strstr(ptr->lastFileRecv,".AESHELL")) mir_unlink(ptr->lastFileRecv);
				SAFE_FREE(ptr->lastFileRecv);
			}
			if ( ptr->lastFileSend ) {
				if (strstr(ptr->lastFileSend,".AESHELL")) mir_unlink(ptr->lastFileSend);
				SAFE_FREE(ptr->lastFileSend);
			}
			if ( ptr->fileSend ) {
				char **file=ptr->fileSend;
        			for(int j=0;file[j];j++) {
					if ( strstr(file[j],".AESHELL")) mir_unlink(file[j]);
					mir_free(file[j]);
				}
				SAFE_FREE(ptr->fileSend);
			}
			return 0;
		} break;
		case ACKRESULT_NEXTFILE:
		case ACKRESULT_SUCCESS: {
			if ( ptr->finFileRecv && ptr->lastFileRecv ) {
				if ( strstr(ptr->lastFileRecv,".AESHELL")) {
					char buf[MAX_PATH];
					LPSTR file_out=mir_strdup(ptr->lastFileRecv);
					LPSTR pos=strrchr(file_out,'.'); //find last .
					if (pos) *pos='\0'; //remove AESHELL from name

					if ( isFileExist(file_out)) {
						buf[0]='\0';
						LPSTR p=strrchr(file_out,'.');
						LPSTR x=strrchr(file_out,'\\');
						if(p>x) {
							strcpy(buf,p);
							pos=p;
						}
						for(int i=1;i<10000;i++) {
							sprintf(pos," (%d)%s",i,buf);
							if ( !isFileExist(file_out)) break;
						}
					}

					sprintf(buf,"%s\n%s",Translate(sim012),file_out);
					showPopUp(buf,NULL,g_hPOP[POP_PU_MSR],2);

					if ( ptr->mode==MODE_RSAAES ) {
						exp->rsa_decrypt_file(ptr->cntx,ptr->lastFileRecv,file_out);
					}
					else {
						cpp_decrypt_file(ptr->cntx,ptr->lastFileRecv,file_out);
					}
					mir_free(file_out);
					mir_unlink(ptr->lastFileRecv);
				}
				SAFE_FREE(ptr->lastFileRecv);
				ptr->finFileRecv = false;
			}
			if ( ptr->finFileSend && ptr->lastFileSend ) {
				if ( strstr(ptr->lastFileSend,".AESHELL")) mir_unlink(ptr->lastFileSend);
				SAFE_FREE(ptr->lastFileSend);
				ptr->finFileSend = false;
			}
		} break;
		} // switch
	}
	return 0;
}


// EOF