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
|
/*
Jabber Protocol Plugin for Miranda IM
Tlen Protocol Plugin for Miranda NG
Copyright (C) 2002-2004 Santithorn Bunchua
Copyright (C) 2004-2007 Piotr Piastucki
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 "tlen.h"
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "resource.h"
#include "tlen_list.h"
#include "tlen_iq.h"
#include "tlen_p2p_old.h"
#include "tlen_avatar.h"
#include "tlen_file.h"
DWORD_PTR TlenProtocol::GetCaps(int type, MCONTACT hContact)
{
switch(type) {
case PFLAGNUM_1:
return PF1_IM|PF1_AUTHREQ|PF1_SERVERCLIST|PF1_MODEMSG|PF1_BASICSEARCH|PF1_SEARCHBYEMAIL|PF1_EXTSEARCH|PF1_EXTSEARCHUI|PF1_SEARCHBYNAME|PF1_FILE;//|PF1_VISLIST|PF1_INVISLIST;
case PFLAGNUM_2:
return PF2_ONLINE|PF2_INVISIBLE|PF2_SHORTAWAY|PF2_LONGAWAY|PF2_HEAVYDND|PF2_FREECHAT;
case PFLAGNUM_3:
return PF2_ONLINE|PF2_INVISIBLE|PF2_SHORTAWAY|PF2_LONGAWAY|PF2_HEAVYDND|PF2_FREECHAT;
case PFLAGNUM_4:
return PF4_FORCEAUTH|PF4_NOCUSTOMAUTH|PF4_SUPPORTTYPING|PF4_AVATARS|PF4_IMSENDOFFLINE|PF4_OFFLINEFILES;
case PFLAG_UNIQUEIDTEXT:
return (INT_PTR) Translate("Tlen login");
case PFLAG_UNIQUEIDSETTING:
return (INT_PTR) "jid";
default:
return 0;
}
}
INT_PTR TlenProtocol::GetName(WPARAM wParam, LPARAM lParam)
{
strncpy((char*)lParam, m_szModuleName, wParam);
return 0;
}
int TlenRunSearch(TlenProtocol *proto) {
int iqId = 0;
if (!proto->isOnline) return 0;
if (proto->searchQuery != NULL && proto->searchIndex < 10) {
iqId = proto->searchID;
TlenIqAdd(proto, iqId, IQ_PROC_GETSEARCH, TlenIqResultSearch);
if (proto->searchIndex == 0) {
TlenSend(proto, "<iq type='get' id='"TLEN_IQID"%d' to='tuba'><query xmlns='jabber:iq:search'>%s</query></iq>", iqId, proto->searchQuery);
} else {
TlenSend(proto, "<iq type='get' id='"TLEN_IQID"%d' to='tuba'><query xmlns='jabber:iq:search'>%s<f>%d</f></query></iq>", iqId, proto->searchQuery, proto->searchIndex * TLEN_MAX_SEARCH_RESULTS_PER_PAGE);
}
proto->searchIndex ++;
}
return iqId;
}
void TlenResetSearchQuery(TlenProtocol *proto) {
if (proto->searchQuery != NULL) {
mir_free(proto->searchQuery);
proto->searchQuery = NULL;
}
proto->searchQueryLen = 0;
proto->searchIndex = 0;
proto->searchID = TlenSerialNext(proto);
}
HANDLE TlenProtocol::SearchBasic(const TCHAR* id)
{
int iqId = 0;
if (!isOnline) return 0;
if (id == NULL) return 0;
char* id_A = mir_t2a(id);
char *jid=TlenTextEncode(id_A);
if (jid != NULL) {
searchJID = mir_strdup(id_A);
TlenResetSearchQuery(this);
TlenStringAppend(&searchQuery, &searchQueryLen, "<i>%s</i>", jid);
iqId = TlenRunSearch(this);
mir_free(jid);
}
mir_free(id_A);
return (HANDLE)iqId;
}
HANDLE TlenProtocol::SearchByEmail(const TCHAR* email)
{
int iqId = 0;
if (!isOnline) return 0;
if (email == NULL) return 0;
char* email_A = mir_t2a(email);
char *emailEnc=TlenTextEncode(email_A);
if (emailEnc != NULL) {
TlenResetSearchQuery(this);
TlenStringAppend(&searchQuery, &searchQueryLen, "<email>%s</email>", emailEnc);
iqId = TlenRunSearch(this);
mir_free(emailEnc);
}
mir_free(email_A);
return (HANDLE)iqId;
}
HANDLE TlenProtocol::SearchByName(const TCHAR* nickT, const TCHAR* firstNameT, const TCHAR* lastNameT)
{
if (!isOnline) return 0;
char *nick = mir_t2a(nickT);
char *firstName = mir_t2a(firstNameT);
char *lastName = mir_t2a(lastNameT);
char *p;
int iqId = 0;
TlenResetSearchQuery(this);
if (nick != NULL && nick[0] != '\0') {
if ((p=TlenTextEncode(nick)) != NULL) {
TlenStringAppend(&searchQuery, &searchQueryLen, "<nick>%s</nick>", p);
mir_free(p);
}
}
if (firstName != NULL && firstName[0] != '\0') {
if ((p=TlenTextEncode(firstName)) != NULL) {
TlenStringAppend(&searchQuery, &searchQueryLen, "<first>%s</first>", p);
mir_free(p);
}
}
if (lastName != NULL && lastName[0] != '\0') {
if ((p=TlenTextEncode(lastName)) != NULL) {
TlenStringAppend(&searchQuery, &searchQueryLen, "<last>%s</last>", p);
mir_free(p);
}
}
iqId = TlenRunSearch(this);
return (HANDLE)iqId;
}
HWND TlenProtocol::CreateExtendedSearchUI(HWND owner)
{
return (HWND) CreateDialog(hInst, MAKEINTRESOURCE(IDD_ADVSEARCH), owner, TlenAdvSearchDlgProc);
}
HWND TlenProtocol::SearchAdvanced(HWND owner)
{
if (!isOnline) return 0;
TlenResetSearchQuery(this);
int iqId = TlenSerialNext(this);
if ((searchQuery = TlenAdvSearchCreateQuery(owner, iqId)) != NULL) {
iqId = TlenRunSearch(this);
}
return (HWND)iqId;
}
static MCONTACT AddToListByJID(TlenProtocol *proto, const char *newJid, DWORD flags)
{
MCONTACT hContact = TlenHContactFromJID(proto, newJid);
if (hContact == NULL) {
// not already there: add
char *jid = mir_strdup(newJid); _strlwr(jid);
hContact = (MCONTACT) CallService(MS_DB_CONTACT_ADD, 0, 0);
Proto_AddToContact(hContact, proto->m_szModuleName);
db_set_s(hContact, proto->m_szModuleName, "jid", jid);
char *nick=TlenNickFromJID(newJid);
if (nick == NULL)
nick = mir_strdup(newJid);
db_set_s(hContact, "CList", "MyHandle", nick);
mir_free(nick);
mir_free(jid);
// Note that by removing or disable the "NotOnList" will trigger
// the plugin to add a particular contact to the roster list.
// See DBSettingChanged hook at the bottom part of this source file.
// But the add module will delete "NotOnList". So we will not do it here.
// Also because we need "MyHandle" and "Group" info, which are set after
// PS_ADDTOLIST is called but before the add dialog issue deletion of
// "NotOnList".
// If temporary add, "NotOnList" won't be deleted, and that's expected.
db_set_b(hContact, "CList", "NotOnList", 1);
if (flags & PALF_TEMPORARY)
db_set_b(hContact, "CList", "Hidden", 1);
}
else {
// already exist
// Set up a dummy "NotOnList" when adding permanently only
if (!(flags&PALF_TEMPORARY))
db_set_b(hContact, "CList", "NotOnList", 1);
}
return hContact;
}
MCONTACT TlenProtocol::AddToList(int flags, PROTOSEARCHRESULT *psr)
{
TLEN_SEARCH_RESULT *jsr = (TLEN_SEARCH_RESULT*)psr;
if (jsr->hdr.cbSize != sizeof(TLEN_SEARCH_RESULT))
return NULL;
return AddToListByJID(this, jsr->jid, flags);// wParam is flag e.g. PALF_TEMPORARY
}
MCONTACT TlenProtocol::AddToListByEvent(int flags, int iContact, MEVENT hDbEvent)
{
DBEVENTINFO dbei = { sizeof(dbei) };
if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1))
return NULL;
if ((dbei.pBlob=(PBYTE) mir_alloc(dbei.cbBlob)) == NULL)
return NULL;
if (db_event_get(hDbEvent, &dbei)) {
mir_free(dbei.pBlob);
return NULL;
}
if (mir_strcmp(dbei.szModule, m_szModuleName)) {
mir_free(dbei.pBlob);
return NULL;
}
/*
// EVENTTYPE_CONTACTS is when adding from when we receive contact list (not used in Tlen)
// EVENTTYPE_ADDED is when adding from when we receive "You are added" (also not used in Tlen)
// Tlen will only handle the case of EVENTTYPE_AUTHREQUEST
// EVENTTYPE_AUTHREQUEST is when adding from the authorization request dialog
*/
if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
mir_free(dbei.pBlob);
return NULL;
}
char *nick = (char *)dbei.pBlob + sizeof(DWORD)*2;
char *firstName = nick + mir_strlen(nick) + 1;
char *lastName = firstName + mir_strlen(firstName) + 1;
char *jid = lastName + mir_strlen(lastName) + 1;
MCONTACT hContact = (MCONTACT) AddToListByJID(this, jid, flags);
mir_free(dbei.pBlob);
return hContact;
}
int TlenProtocol::Authorize(MEVENT hDbEvent)
{
if (!isOnline)
return 1;
DBEVENTINFO dbei = { sizeof(dbei) };
if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)-1)
return 1;
if ((dbei.pBlob=(PBYTE) mir_alloc(dbei.cbBlob)) == NULL)
return 1;
if (db_event_get(hDbEvent, &dbei)){
mir_free(dbei.pBlob);
return 1;
}
if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
mir_free(dbei.pBlob);
return 1;
}
if (mir_strcmp(dbei.szModule, m_szModuleName)) {
mir_free(dbei.pBlob);
return 1;
}
char *nick = (char *)dbei.pBlob + sizeof(DWORD)*2;
char *firstName = nick + mir_strlen(nick) + 1;
char *lastName = firstName + mir_strlen(firstName) + 1;
char *jid = lastName + mir_strlen(lastName) + 1;
TlenSend(this, "<presence to='%s' type='subscribed'/>", jid);
// Automatically add this user to my roster if option is enabled
if (db_get_b(NULL, m_szModuleName, "AutoAdd", TRUE) == TRUE) {
MCONTACT hContact;
TLEN_LIST_ITEM *item = TlenListGetItemPtr(this, LIST_ROSTER, jid);
if (item == NULL || (item->subscription != SUB_BOTH && item->subscription != SUB_TO)) {
debugLogA("Try adding contact automatically jid=%s", jid);
if ((hContact=AddToListByJID(this, jid, 0)) != NULL) {
// Trigger actual add by removing the "NotOnList" added by AddToListByJID()
// See AddToListByJID() and TlenDbSettingChanged().
db_unset(hContact, "CList", "NotOnList");
}
}
}
mir_free(dbei.pBlob);
return 0;
}
int TlenProtocol::AuthDeny(MEVENT hDbEvent, const TCHAR* szReason)
{
if (!isOnline)
return 1;
DBEVENTINFO dbei = { sizeof(dbei) };
if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1))
return 1;
if ((dbei.pBlob = (PBYTE) mir_alloc(dbei.cbBlob)) == NULL)
return 1;
if (db_event_get(hDbEvent, &dbei)) {
mir_free(dbei.pBlob);
return 1;
}
if (dbei.eventType != EVENTTYPE_AUTHREQUEST) {
mir_free(dbei.pBlob);
return 1;
}
if (mir_strcmp(dbei.szModule, m_szModuleName)) {
mir_free(dbei.pBlob);
return 1;
}
char *nick = (char *)dbei.pBlob + sizeof(DWORD)*2;
char *firstName = nick + mir_strlen(nick) + 1;
char *lastName = firstName + mir_strlen(firstName) + 1;
char *jid = lastName + mir_strlen(lastName) + 1;
TlenSend(this, "<presence to='%s' type='unsubscribed'/>", jid);
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item jid='%s' subscription='remove'/></query></iq>", jid);
mir_free(dbei.pBlob);
return 0;
}
static void TlenConnect(TlenProtocol *proto, int initialStatus)
{
if (!proto->isConnected) {
ThreadData *thread = (ThreadData *) mir_alloc(sizeof(ThreadData));
memset(thread, 0, sizeof(ThreadData));
thread->proto = proto;
proto->m_iDesiredStatus = initialStatus;
int oldStatus = proto->m_iStatus;
proto->m_iStatus = ID_STATUS_CONNECTING;
ProtoBroadcastAck(proto->m_szModuleName, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, proto->m_iStatus);
thread->hThread = (HANDLE) forkthread((void (__cdecl *)(void*))TlenServerThread, 0, thread);
}
}
int TlenProtocol::SetStatus(int iNewStatus)
{
int oldStatus;
HANDLE s;
m_iDesiredStatus = iNewStatus;
if (iNewStatus == ID_STATUS_OFFLINE) {
if (threadData) {
if (isConnected) {
TlenSendPresence(this, ID_STATUS_OFFLINE);
}
// TODO bug? s = proto;
s = threadData->s;
threadData = NULL;
if (isConnected) {
Sleep(200);
// TlenSend(s, "</s>");
// Force closing connection
isConnected = FALSE;
isOnline = FALSE;
Netlib_CloseHandle(s);
}
}
else {
if (m_iStatus != ID_STATUS_OFFLINE) {
oldStatus = m_iStatus;
m_iStatus = ID_STATUS_OFFLINE;
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
}
}
}
else if (iNewStatus != m_iStatus) {
if (!isConnected){
TlenConnect(this, iNewStatus);
} else {
// change status
oldStatus = m_iStatus;
// send presence update
TlenSendPresence(this, iNewStatus);
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
}
}
return 0;
}
INT_PTR TlenProtocol::GetStatus(WPARAM wParam, LPARAM lParam)
{
return m_iStatus;
}
int TlenProtocol::SetAwayMsg(int iStatus, const TCHAR* msg)
{
char **szMsg;
char *newModeMsg;
newModeMsg = mir_t2a(msg);
debugLogA("SetAwayMsg called, wParam=%d lParam=%s", iStatus, newModeMsg);
mir_cslock lck(modeMsgMutex);
switch (iStatus) {
case ID_STATUS_ONLINE:
szMsg = &modeMsgs.szOnline;
break;
case ID_STATUS_AWAY:
case ID_STATUS_ONTHEPHONE:
case ID_STATUS_OUTTOLUNCH:
szMsg = &modeMsgs.szAway;
break;
case ID_STATUS_NA:
szMsg = &modeMsgs.szNa;
break;
case ID_STATUS_DND:
case ID_STATUS_OCCUPIED:
szMsg = &modeMsgs.szDnd;
break;
case ID_STATUS_FREECHAT:
szMsg = &modeMsgs.szFreechat;
break;
case ID_STATUS_INVISIBLE:
szMsg = &modeMsgs.szInvisible;
break;
default:
return 1;
}
if ((*szMsg == NULL && newModeMsg == NULL) ||
(*szMsg != NULL && newModeMsg != NULL && !mir_strcmp(*szMsg, newModeMsg))) {
// Message is the same, no update needed
if (newModeMsg != NULL) mir_free(newModeMsg);
}
else {
// Update with the new mode message
if (*szMsg != NULL) mir_free(*szMsg);
*szMsg = newModeMsg;
// Send a presence update if needed
if (iStatus == m_iStatus) {
TlenSendPresence(this, m_iStatus);
}
}
return 0;
}
int TlenProtocol::GetInfo(MCONTACT hContact, int infoType)
{
DBVARIANT dbv;
int iqId;
char *nick, *pNick;
if (!isOnline) return 1;
if (hContact == NULL) {
iqId = TlenSerialNext(this);
TlenIqAdd(this, iqId, IQ_PROC_NONE, TlenIqResultVcard);
TlenSend(this, "<iq type='get' id='"TLEN_IQID"%d' to='tuba'><query xmlns='jabber:iq:register'></query></iq>", iqId);
} else {
if (db_get(hContact, m_szModuleName, "jid", &dbv)) return 1;
if ((nick=TlenNickFromJID(dbv.pszVal)) != NULL) {
if ((pNick=TlenTextEncode(nick)) != NULL) {
iqId = TlenSerialNext(this);
TlenIqAdd(this, iqId, IQ_PROC_NONE, TlenIqResultVcard);
TlenSend(this, "<iq type='get' id='"TLEN_IQID"%d' to='tuba'><query xmlns='jabber:iq:search'><i>%s</i></query></iq>", iqId, pNick);
mir_free(pNick);
}
mir_free(nick);
}
db_free(&dbv);
}
return 0;
}
int TlenProtocol::SetApparentMode(MCONTACT hContact, int mode)
{
DBVARIANT dbv;
int oldMode;
char *jid;
if (!isOnline) return 0;
if (!db_get_b(NULL, m_szModuleName, "VisibilitySupport", FALSE)) return 0;
if (mode != 0 && mode != ID_STATUS_ONLINE && mode != ID_STATUS_OFFLINE) return 1;
oldMode = db_get_w(hContact, m_szModuleName, "ApparentMode", 0);
if ((int) mode == oldMode) return 1;
db_set_w(hContact, m_szModuleName, "ApparentMode", (WORD) mode);
if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
jid = dbv.pszVal;
switch (mode) {
case ID_STATUS_ONLINE:
if (m_iStatus == ID_STATUS_INVISIBLE || oldMode == ID_STATUS_OFFLINE)
TlenSend(this, "<presence to='%s'><show>available</show></presence>", jid);
break;
case ID_STATUS_OFFLINE:
if (m_iStatus != ID_STATUS_INVISIBLE || oldMode == ID_STATUS_ONLINE)
TlenSend(this, "<presence to='%s' type='invisible'/>", jid);
break;
case 0:
if (oldMode == ID_STATUS_ONLINE && m_iStatus == ID_STATUS_INVISIBLE)
TlenSend(this, "<presence to='%s' type='invisible'/>", jid);
else if (oldMode == ID_STATUS_OFFLINE && m_iStatus != ID_STATUS_INVISIBLE)
TlenSend(this, "<presence to='%s'><show>available</show></presence>", jid);
break;
}
db_free(&dbv);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
struct SENDACKTHREADDATA
{
__inline SENDACKTHREADDATA(TlenProtocol *_ppro, MCONTACT _hContact, int _msgid=0) :
proto(_ppro), hContact(_hContact), msgid(_msgid)
{}
TlenProtocol *proto;
MCONTACT hContact;
int msgid;
};
static void __cdecl TlenSendMessageAckThread(void *ptr)
{
SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
SleepEx(10, TRUE);
ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)data->msgid, 0);
delete data;
}
static void __cdecl TlenSendMessageFailedThread(void *ptr)
{
SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
SleepEx(10, TRUE);
ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)data->msgid, 0);
delete data;
}
static void __cdecl TlenGetAwayMsgThread(void *ptr)
{
DBVARIANT dbv;
SENDACKTHREADDATA *data = (SENDACKTHREADDATA *)ptr;
Sleep(50);
if (!db_get(data->hContact, data->proto->m_szModuleName, "jid", &dbv)) {
TLEN_LIST_ITEM *item=TlenListGetItemPtr(data->proto, LIST_ROSTER, dbv.pszVal);
if (item != NULL) {
db_free(&dbv);
ProtoBroadcastAck(data->proto->m_szModuleName, data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1,
item->statusMessage == NULL ? NULL : _A2T(item->statusMessage));
} else {
ptrA ownJid(db_get_sa(NULL, data->proto->m_szModuleName, "jid"));
if (!mir_strcmp(ownJid, dbv.pszVal)){
DBVARIANT dbv2;
if (!db_get_s(data->hContact, "CList", "StatusMsg", &dbv2, DBVT_TCHAR)){
data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, (LPARAM)dbv2.ptszVal);
db_free(&dbv2);
} else {
data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, NULL);
}
}
db_free(&dbv);
}
} else {
data->proto->ProtoBroadcastAck(data->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)1, NULL);
}
delete data;
}
INT_PTR TlenProtocol::SendAlert(WPARAM hContact, LPARAM lParam)
{
DBVARIANT dbv;
if (isOnline && !db_get(hContact, m_szModuleName, "jid", &dbv)) {
TlenSend(this, "<m tp='a' to='%s'/>", dbv.pszVal);
db_free(&dbv);
}
return 0;
}
int TlenProtocol::SendMsg(MCONTACT hContact, int, const char* msgRAW)
{
DBVARIANT dbv;
if (!isOnline || db_get(hContact, m_szModuleName, "jid", &dbv)) {
forkthread(TlenSendMessageFailedThread, 0, new SENDACKTHREADDATA(this, hContact, 2));
return 2;
}
TLEN_LIST_ITEM *item;
char msgType[16];
char* msg = mir_utf8decodeA(msgRAW);
int id = TlenSerialNext(this);
if (!mir_strcmp(msg, "<alert>")) {
TlenSend(this, "<m tp='a' to='%s'/>", dbv.pszVal);
forkthread(TlenSendMessageAckThread, 0, new SENDACKTHREADDATA(this, hContact, id));
}
else if (!mir_strcmp(msg, "<image>")) {
TlenSend(this, "<message to='%s' type='%s' crc='%x' idt='%d'/>", dbv.pszVal, "pic", 0x757f044, id);
forkthread(TlenSendMessageAckThread, 0, new SENDACKTHREADDATA(this, hContact, id));
}
else {
char *msgEnc = TlenTextEncode(msg);
if (msgEnc != NULL) {
if (TlenListExist(this, LIST_CHATROOM, dbv.pszVal) && strchr(dbv.pszVal, '/') == NULL)
mir_strcpy(msgType, "groupchat");
else if (db_get_b(hContact, m_szModuleName, "bChat", FALSE))
mir_strcpy(msgType, "privchat");
else
mir_strcpy(msgType, "chat");
if (!mir_strcmp(msgType, "groupchat") || db_get_b(NULL, m_szModuleName, "MsgAck", FALSE) == FALSE) {
if (!mir_strcmp(msgType, "groupchat"))
TlenSend(this, "<message to='%s' type='%s'><body>%s</body></message>", dbv.pszVal, msgType, msgEnc);
else if (!mir_strcmp(msgType, "privchat"))
TlenSend(this, "<m to='%s'><b n='6' s='10' f='0' c='000000'>%s</b></m>", dbv.pszVal, msgEnc);
else
TlenSend(this, "<message to='%s' type='%s' id='"TLEN_IQID"%d'><body>%s</body><x xmlns='jabber:x:event'><composing/></x></message>", dbv.pszVal, msgType, id, msgEnc);
forkthread(TlenSendMessageAckThread, 0, new SENDACKTHREADDATA(this, hContact, id));
}
else {
if ((item=TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != NULL)
item->idMsgAckPending = id;
TlenSend(this, "<message to='%s' type='%s' id='"TLEN_IQID"%d'><body>%s</body><x xmlns='jabber:x:event'><offline/><delivered/><composing/></x></message>", dbv.pszVal, msgType, id, msgEnc);
}
}
mir_free(msgEnc);
}
mir_free(msg);
db_free(&dbv);
return id;
}
/////////////////////////////////////////////////////////////////////////////////////////
// TlenGetAvatarInfo - retrieves the avatar info
INT_PTR TlenProtocol::GetAvatarInfo(WPARAM wParam, LPARAM lParam)
{
if (!tlenOptions.enableAvatars) return GAIR_NOAVATAR;
BOOL downloadingAvatar = FALSE;
char *avatarHash = NULL;
TLEN_LIST_ITEM *item = NULL;
DBVARIANT dbv;
PROTO_AVATAR_INFORMATION *pai = (PROTO_AVATAR_INFORMATION*)lParam;
if (pai->hContact != NULL) {
if (!db_get(pai->hContact, m_szModuleName, "jid", &dbv)) {
item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal);
db_free(&dbv);
if (item != NULL) {
downloadingAvatar = item->newAvatarDownloading;
avatarHash = item->avatarHash;
}
}
}
else if (threadData != NULL)
avatarHash = threadData->avatarHash;
if ((avatarHash == NULL || avatarHash[0] == '\0') && !downloadingAvatar)
return GAIR_NOAVATAR;
if (avatarHash != NULL && !downloadingAvatar) {
TlenGetAvatarFileName(this, item, pai->filename, _countof(pai->filename)-1);
pai->format = ( pai->hContact == NULL ) ? threadData->avatarFormat : item->avatarFormat;
return GAIR_SUCCESS;
}
/* get avatar */
if (( wParam & GAIF_FORCE ) != 0 && pai->hContact != NULL && isOnline)
return GAIR_WAITFOR;
return GAIR_NOAVATAR;
}
HANDLE TlenProtocol::GetAwayMsg(MCONTACT hContact)
{
SENDACKTHREADDATA *tdata = new SENDACKTHREADDATA(this, hContact, 0);
forkthread((void (__cdecl *)(void*))TlenGetAwayMsgThread, 0, (void*)tdata);
return (HANDLE)1;
}
int TlenProtocol::RecvAwayMsg(MCONTACT hContact, int mode, PROTORECVEVENT* evt)
{
return 0;
}
HANDLE TlenProtocol::FileAllow(MCONTACT hContact, HANDLE hTransfer, const TCHAR* szPath)
{
if (!isOnline) return 0;
TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *) hTransfer;
ft->szSavePath = mir_strdup(mir_t2a(szPath)); //TODO convert to TCHAR*
TLEN_LIST_ITEM *item = TlenListAdd(this, LIST_FILE, ft->iqId);
if (item != NULL) {
item->ft = ft;
}
char *nick = TlenNickFromJID(ft->jid);
if (ft->newP2P) {
TlenSend(this, "<iq to='%s'><query xmlns='p2p'><fs t='%s' e='5' i='%s' v='1'/></query></iq>", ft->jid, ft->jid, ft->iqId);
} else {
TlenSend(this, "<f t='%s' i='%s' e='5' v='1'/>", nick, ft->iqId);
}
mir_free(nick);
return (HANDLE)hTransfer;
}
int TlenProtocol::FileDeny(MCONTACT hContact, HANDLE hTransfer, const TCHAR* szReason)
{
if (!isOnline) return 1;
TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *) hTransfer;
char *nick = TlenNickFromJID(ft->jid);
if (ft->newP2P) {
TlenSend(this, "<f i='%s' e='4' t='%s'/>", ft->iqId, nick);\
} else {
TlenSend(this, "<f i='%s' e='4' t='%s'/>", ft->iqId, nick);\
}
mir_free(nick);
TlenP2PFreeFileTransfer(ft);
return 0;
}
int TlenProtocol::FileResume(HANDLE hTransfer, int* action, const TCHAR** szFilename) {
return 0;
}
int TlenProtocol::FileCancel(MCONTACT hContact, HANDLE hTransfer)
{
TLEN_FILE_TRANSFER *ft = (TLEN_FILE_TRANSFER *) hTransfer;
debugLogA("Invoking FileCancel()");
if (ft->s != NULL) {
ft->state = FT_ERROR;
Netlib_CloseHandle(ft->s);
ft->s = NULL;
if (ft->hFileEvent != NULL) {
HANDLE hEvent = ft->hFileEvent;
ft->hFileEvent = NULL;
SetEvent(hEvent);
}
} else {
TlenP2PFreeFileTransfer(ft);
}
return 0;
}
HANDLE TlenProtocol::SendFile(MCONTACT hContact, const TCHAR* szDescription, TCHAR** ppszFiles)
{
int i, j;
struct _stat statbuf;
DBVARIANT dbv;
char *nick, *p, idStr[10];
if (!isOnline) return 0;
// if (db_get_w(ccs->hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) return 0;
if (db_get(hContact, m_szModuleName, "jid", &dbv)) return 0;
TLEN_FILE_TRANSFER *ft = TlenFileCreateFT(this, dbv.pszVal);
for (ft->fileCount=0; ppszFiles[ft->fileCount]; ft->fileCount++);
ft->files = (char **) mir_alloc(sizeof(char *) * ft->fileCount);
ft->filesSize = (long *) mir_alloc(sizeof(long) * ft->fileCount);
ft->allFileTotalSize = 0;
for (i=j=0; i<ft->fileCount; i++) {
char* ppszFiles_i_A = mir_t2a(ppszFiles[i]);
if (_stat(ppszFiles_i_A, &statbuf))
debugLogA("'%s' is an invalid filename", ppszFiles[i]);
else {
ft->filesSize[j] = statbuf.st_size;
ft->files[j++] = mir_strdup(ppszFiles_i_A);
ft->allFileTotalSize += statbuf.st_size;
}
mir_free(ppszFiles_i_A);
}
ft->fileCount = j;
ft->szDescription = mir_t2a(szDescription);
ft->hContact = hContact;
ft->currentFile = 0;
db_free(&dbv);
int id = TlenSerialNext(this);
mir_snprintf(idStr, _countof(idStr), "%d", id);
TLEN_LIST_ITEM *item = TlenListAdd(this, LIST_FILE, idStr);
if (item != NULL) {
ft->iqId = mir_strdup(idStr);
nick = TlenNickFromJID(ft->jid);
item->ft = ft;
if (tlenOptions.useNewP2P) {
TlenSend(this, "<iq to='%s'><query xmlns='p2p'><fs t='%s' e='1' i='%s' c='%d' s='%d' v='%d'/></query></iq>",
ft->jid, ft->jid, idStr, ft->fileCount, ft->allFileTotalSize, ft->fileCount);
ft->newP2P = TRUE;
} else {
if (ft->fileCount == 1) {
char* ppszFiles_0_A = mir_t2a(ppszFiles[0]);
if ((p=strrchr(ppszFiles_0_A, '\\')) != NULL) {
p++;
} else {
p = ppszFiles_0_A;
}
p = TlenTextEncode(p);
TlenSend(this, "<f t='%s' n='%s' e='1' i='%s' c='1' s='%d' v='1'/>", nick, p, idStr, ft->allFileTotalSize);
mir_free(ppszFiles[0]);
mir_free(p);
} else {
TlenSend(this, "<f t='%s' e='1' i='%s' c='%d' s='%d' v='1'/>", nick, idStr, ft->fileCount, ft->allFileTotalSize);
}
}
mir_free(nick);
}
return (HANDLE) ft;
}
int TlenProtocol::SendContacts(MCONTACT hContact, int flags, int nContacts, MCONTACT *hContactsList){
return 0;
}
int TlenProtocol::SendUrl(MCONTACT hContact, int flags, const char* urlt){
return 0;
}
int TlenProtocol::RecvMsg(MCONTACT hContact, PROTORECVEVENT* evt)
{
return Proto_RecvMessage(hContact, evt);
}
int TlenProtocol::RecvFile(MCONTACT hContact, PROTORECVFILET* evt)
{
return Proto_RecvFile(hContact, evt);
}
int TlenProtocol::RecvUrl(MCONTACT hContact, PROTORECVEVENT*)
{
return 0;
}
static char* settingToChar( DBCONTACTWRITESETTING* cws )
{
switch( cws->value.type ) {
case DBVT_ASCIIZ:
return mir_strdup( cws->value.pszVal );
case DBVT_UTF8:
return mir_utf8decode(mir_strdup(cws->value.pszVal), NULL);
}
return NULL;
}
int TlenProtocol::TlenDbSettingChanged(WPARAM wParam, LPARAM lParam)
{
MCONTACT hContact = (MCONTACT) wParam;
DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *) lParam;
// no action for hContact == NULL or when offline
if (hContact == NULL) return 0;
if (!isConnected) return 0;
if (!mir_strcmp(cws->szModule, "CList")) {
DBVARIANT dbv;
TLEN_LIST_ITEM *item;
char *nick, *jid, *group;
char *szProto = GetContactProto(hContact);
if (szProto == NULL || mir_strcmp(szProto, m_szModuleName)) return 0;
// A contact's group is changed
if (!mir_strcmp(cws->szSetting, "Group")) {
if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
if ((item=TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != NULL) {
db_free(&dbv);
if (!db_get(hContact, "CList", "MyHandle", &dbv)) {
nick = TlenTextEncode(dbv.pszVal);
db_free(&dbv);
}
else if (!db_get(hContact, this->m_szModuleName, "Nick", &dbv)) {
nick = TlenTextEncode(dbv.pszVal);
db_free(&dbv);
}
else nick = TlenNickFromJID(item->jid);
if (nick != NULL) {
// Note: we need to compare with item->group to prevent infinite loop
if (cws->value.type == DBVT_DELETED && item->group != NULL) {
debugLogA("Group set to nothing");
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'></item></query></iq>", nick, item->jid);
}
else if (cws->value.pszVal != NULL) {
char *newGroup = settingToChar(cws);
if (item->group == NULL || mir_strcmp(newGroup, item->group)) {
debugLogA("Group set to %s", newGroup);
if ((group=TlenGroupEncode(newGroup)) != NULL) {
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>", nick, item->jid, group);
mir_free(group);
}
}
mir_free(newGroup);
}
mir_free(nick);
}
}
else {
db_free(&dbv);
}
}
}
// A contact is renamed
else if (!mir_strcmp(cws->szSetting, "MyHandle")) {
char *newNick;
// hContact = (MCONTACT) wParam;
// szProto = GetContactProto(hContact);
// if (szProto == NULL || mir_strcmp(szProto, proto->m_szModuleName)) return 0;
if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
jid = dbv.pszVal;
if ((item=TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal)) != NULL) {
if (cws->value.type == DBVT_DELETED) {
newNick = mir_strdup((char *) pcli->pfnGetContactDisplayName(hContact, GCDNF_NOMYHANDLE));
} else if (cws->value.pszVal != NULL) {
newNick = settingToChar(cws);
} else {
newNick = NULL;
}
// Note: we need to compare with item->nick to prevent infinite loop
if (newNick != NULL && (item->nick == NULL || (item->nick != NULL && mir_strcmp(item->nick, newNick)))) {
if ((nick=TlenTextEncode(newNick)) != NULL) {
debugLogA("Nick set to %s", newNick);
if (item->group != NULL && (group=TlenGroupEncode(item->group)) != NULL) {
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>", nick, jid, group);
mir_free(group);
} else {
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'></item></query></iq>", nick, jid);
}
mir_free(nick);
}
}
if (newNick != NULL) mir_free(newNick);
}
db_free(&dbv);
}
}
// A temporary contact has been added permanently
else if (!mir_strcmp(cws->szSetting, "NotOnList")) {
char *jid, *nick, *pGroup;
if (cws->value.type==DBVT_DELETED || (cws->value.type==DBVT_BYTE && cws->value.bVal==0)) {
if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
jid = mir_strdup(dbv.pszVal);
db_free(&dbv);
debugLogA("Add %s permanently to list", jid);
if (!db_get(hContact, "CList", "MyHandle", &dbv)) {
nick = TlenTextEncode(dbv.pszVal); //Utf8Encode
db_free(&dbv);
}
else {
nick = TlenNickFromJID(jid);
}
if (nick != NULL) {
debugLogA("jid=%s nick=%s", jid, nick);
if (!db_get(hContact, "CList", "Group", &dbv)) {
if ((pGroup=TlenGroupEncode(dbv.pszVal)) != NULL) {
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>", nick, jid, pGroup);
TlenSend(this, "<presence to='%s' type='subscribe'/>", jid);
mir_free(pGroup);
}
db_free(&dbv);
}
else {
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'/></query></iq>", nick, jid);
TlenSend(this, "<presence to='%s' type='subscribe'/>", jid);
}
mir_free(nick);
db_unset(hContact, "CList", "Hidden");
}
mir_free(jid);
}
}
}
}
return 0;
}
int TlenProtocol::TlenContactDeleted(WPARAM wParam, LPARAM lParam)
{
if (!isOnline) // should never happen
return 0;
char *szProto = GetContactProto(wParam);
if (szProto == NULL || mir_strcmp(szProto, m_szModuleName))
return 0;
DBVARIANT dbv;
if (!db_get(wParam, m_szModuleName, "jid", &dbv)) {
char *p, *q;
char *jid = dbv.pszVal;
if ((p=strchr(jid, '@')) != NULL) {
if ((q=strchr(p, '/')) != NULL)
*q = '\0';
}
// Remove from roster, server also handles the presence unsubscription process.
if (TlenListExist(this, LIST_ROSTER, jid))
TlenSend(this, "<iq type='set'><query xmlns='jabber:iq:roster'><item jid='%s' subscription='remove'/></query></iq>", jid);
db_free(&dbv);
}
return 0;
}
int TlenProtocol::UserIsTyping(MCONTACT hContact, int type)
{
DBVARIANT dbv;
if (!isOnline) return 0;
if (!db_get(hContact, m_szModuleName, "jid", &dbv)) {
TLEN_LIST_ITEM *item = TlenListGetItemPtr(this, LIST_ROSTER, dbv.pszVal);
if (item != NULL /*&& item->wantComposingEvent == TRUE*/) {
switch (type) {
case PROTOTYPE_SELFTYPING_OFF:
TlenSend(this, "<m tp='u' to='%s'/>", dbv.pszVal);
break;
case PROTOTYPE_SELFTYPING_ON:
TlenSend(this, "<m tp='t' to='%s'/>", dbv.pszVal);
break;
}
}
db_free(&dbv);
}
return 0;
}
INT_PTR TlenProtocol::GetMyAvatar(WPARAM wParam, LPARAM lParam)
{
TCHAR* buf = (TCHAR*)wParam;
int size = ( int )lParam;
if ( buf == NULL || size <= 0 )
return -1;
TlenGetAvatarFileName(this, NULL, buf, size);
return 0;
}
static INT_PTR CALLBACK TlenChangeAvatarDlgProc( HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch ( msg ) {
case WM_INITDIALOG:
TranslateDialogDefault( hwndDlg );
{
HICON hIcon = GetIcolibIcon(IDI_TLEN);
SendMessage(hwndDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIcon);
ReleaseIcolibIcon(hIcon);
}
CheckDlgButton(hwndDlg, IDC_PUBLICAVATAR, BST_CHECKED);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
{
int result = LOWORD(wParam);
if (IsDlgButtonChecked(hwndDlg, IDC_PUBLICAVATAR)) {
result |= 0x10000;
}
EndDialog(hwndDlg, result);
}
return TRUE;
}
break;
}
return 0;
}
INT_PTR TlenProtocol::SetMyAvatar(WPARAM wParam, LPARAM lParam)
{
if (!isOnline){
PUShowMessageT(TranslateT("You need to be connected to Tlen account to set avatar."), SM_WARNING);
return 1;
}
TCHAR* szFileName = ( TCHAR* )lParam;
TCHAR tFileName[ MAX_PATH ];
if (szFileName != NULL) {
int result = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_USER_CHANGEAVATAR), NULL, TlenChangeAvatarDlgProc, (LPARAM) NULL);
TlenGetAvatarFileName(this, NULL, tFileName, MAX_PATH);
if ( CopyFile( szFileName, tFileName, FALSE ) == FALSE )
return 1;
char* tFileNameA = mir_t2a(tFileName); //TODO - drop io.h
int fileIn = open( tFileNameA, O_RDWR | O_BINARY, S_IREAD | S_IWRITE );
if ( fileIn != -1 ) {
long dwPngSize = filelength(fileIn);
BYTE* pResult = (BYTE *)mir_alloc(dwPngSize);
if (pResult != NULL) {
read( fileIn, pResult, dwPngSize );
close( fileIn );
TlenUploadAvatar(this, pResult, dwPngSize, (result & 0x10000) != 0);
mir_free(pResult);
}
} else debugLogA("SetMyAvatar open error");
mir_free(tFileName);
mir_free(tFileNameA);
}
else TlenRemoveAvatar(this);
return 0;
}
INT_PTR TlenProtocol::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
switch (wParam) {
case AF_MAXSIZE:
{
POINT* size = (POINT*)lParam;
if ( size )
size->x = size->y = 64;
}
return 0;
case AF_PROPORTION:
return PIP_SQUARE;
case AF_FORMATSUPPORTED:
return (lParam == PA_FORMAT_PNG) ? 1 : 0;
case AF_ENABLED:
return tlenOptions.enableAvatars;
case AF_DONTNEEDDELAYS:
return 1;
case AF_MAXFILESIZE:
return 10 * 1024;
case AF_FETCHIFCONTACTOFFLINE:
return 1;
default:
return 0;
}
}
int TlenProtocol::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam)
{
//TlenProtocol *proto = (TlenProtocol *)ptr;
switch( iEventType ) {
case EV_PROTO_ONLOAD: return OnModulesLoaded(0, 0);
case EV_PROTO_ONOPTIONS: return OptionsInit(wParam, lParam);
case EV_PROTO_ONEXIT: return PreShutdown(0, 0);
case EV_PROTO_ONRENAME:
Menu_ModifyItem(hMenuRoot, m_tszUserName);
}
return 1;
}
extern INT_PTR CALLBACK TlenAccMgrUIDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
INT_PTR TlenProtocol::AccMgrUI(WPARAM wParam, LPARAM lParam)
{
return (INT_PTR)CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ACCMGRUI), (HWND)lParam, TlenAccMgrUIDlgProc, (LPARAM)this);
}
void TlenInitServicesVTbl(TlenProtocol *proto)
{
proto->CreateProtoService(PS_GETNAME, &TlenProtocol::GetName);
proto->CreateProtoService(PS_GETAVATARINFO, &TlenProtocol::GetAvatarInfo);
proto->CreateProtoService(PS_SEND_NUDGE, &TlenProtocol::SendAlert);
proto->CreateProtoService(PS_GETAVATARCAPS, &TlenProtocol::GetAvatarCaps);
proto->CreateProtoService(PS_SETMYAVATAR, &TlenProtocol::SetMyAvatar);
proto->CreateProtoService(PS_GETMYAVATAR, &TlenProtocol::GetMyAvatar);
proto->CreateProtoService(PS_GETSTATUS, &TlenProtocol::GetStatus);
proto->CreateProtoService(PS_CREATEACCMGRUI, &TlenProtocol::AccMgrUI);
}
TlenProtocol::TlenProtocol( const char *aProtoName, const TCHAR *aUserName) :
PROTO<TlenProtocol>(aProtoName, aUserName)
{
TlenInitServicesVTbl(this);
hTlenNudge = CreateProtoEvent("/Nudge");
HookProtoEvent(ME_OPT_INITIALISE, &TlenProtocol::OptionsInit);
HookProtoEvent(ME_DB_CONTACT_SETTINGCHANGED, &TlenProtocol::TlenDbSettingChanged);
HookProtoEvent(ME_DB_CONTACT_DELETED, &TlenProtocol::TlenContactDeleted);
HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &TlenProtocol::PrebuildContactMenu);
DBVARIANT dbv;
if (!db_get(NULL, m_szModuleName, "LoginServer", &dbv))
db_free(&dbv);
else
db_set_s(NULL, m_szModuleName, "LoginServer", "tlen.pl");
if (!db_get(NULL, m_szModuleName, "ManualHost", &dbv))
db_free(&dbv);
else
db_set_s(NULL, m_szModuleName, "ManualHost", "s1.tlen.pl");
TlenLoadOptions(this);
TlenWsInit(this);
TlenSerialInit(this);
TlenIqInit(this);
TlenListInit(this);
initMenuItems();
}
TlenProtocol::~TlenProtocol()
{
TlenVoiceCancelAll(this);
TlenFileCancelAll(this);
if (hTlenNudge)
DestroyHookableEvent(hTlenNudge);
TlenListUninit(this);
TlenIqUninit(this);
TlenWsUninit(this);
mir_free(modeMsgs.szOnline);
mir_free(modeMsgs.szAway);
mir_free(modeMsgs.szNa);
mir_free(modeMsgs.szDnd);
mir_free(modeMsgs.szFreechat);
mir_free(modeMsgs.szInvisible);
}
|