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
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
|
// ---------------------------------------------------------------------------80
// ICQ plugin for Miranda Instant Messenger
// ________________________________________
//
// Copyright © 2000-2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2010 Joe Kucera, George Hazan
// Copyright © 2012-2014 Miranda NG Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// -----------------------------------------------------------------------------
// DESCRIPTION:
//
// Protocol Interface Implementation
// -----------------------------------------------------------------------------
#include "icqoscar.h"
#include "m_icolib.h"
extern PLUGININFOEX pluginInfo;
#pragma warning(disable:4355)
static int CompareConns(const directconnect *p1, const directconnect *p2)
{
if (p1 < p2)
return -1;
return (p1 == p2) ? 0 : 1;
}
static int CompareCookies(const icq_cookie_info *p1, const icq_cookie_info *p2)
{
if (p1->dwCookie < p2->dwCookie)
return -1;
return (p1->dwCookie == p2->dwCookie) ? 0 : 1;
}
static int CompareFT(const filetransfer *p1, const filetransfer *p2)
{
if (p1->dwCookie < p2->dwCookie)
return -1;
return (p1->dwCookie == p2->dwCookie) ? 0 : 1;
}
static int CompareContactsCache(const icq_contacts_cache *p1, const icq_contacts_cache *p2)
{
if (p1->dwUin < p2->dwUin)
return -1;
if (p1->dwUin > p2->dwUin)
return 1;
return stricmpnull(p1->szUid, p2->szUid);
}
CIcqProto::CIcqProto(const char* aProtoName, const TCHAR* aUserName) :
PROTO<CIcqProto>(aProtoName, aUserName),
cookies(10, CompareCookies),
directConns(10, CompareConns),
expectedFileRecvs(10, CompareFT),
contactsCache(10, CompareContactsCache),
CustomCapList(1),
cheekySearchId(-1)
{
debugLogA("Setting protocol/module name to '%s'", m_szModuleName);
oftMutex = new icq_critical_section();
// Initialize direct connections
directConnListMutex = new icq_critical_section();
expectedFileRecvMutex = new icq_critical_section();
// Initialize server lists
servlistMutex = new icq_critical_section();
servlistQueueMutex = new icq_critical_section();
HookProtoEvent(ME_CLIST_GROUPCHANGE, &CIcqProto::ServListCListGroupChange);
// Initialize status message struct
ZeroMemory(&m_modeMsgs, sizeof(icq_mode_messages));
m_modeMsgsMutex = new icq_critical_section();
connectionHandleMutex = new icq_critical_section();
localSeqMutex = new icq_critical_section();
m_modeMsgsEvent = CreateProtoEvent(ME_ICQ_STATUSMSGREQ);
// Initialize cookies
cookieMutex = new icq_critical_section();
wCookieSeq = 2;
// Initialize rates
m_ratesMutex = new icq_critical_section();
// Initialize avatars
m_avatarsMutex = new icq_critical_section();
// Initialize temporary DB settings
db_set_resident(m_szModuleName, "Status"); // NOTE: XStatus cannot be temporary
db_set_resident(m_szModuleName, "TemporaryVisible");
db_set_resident(m_szModuleName, "TickTS");
db_set_resident(m_szModuleName, "IdleTS");
db_set_resident(m_szModuleName, "AwayTS");
db_set_resident(m_szModuleName, "LogonTS");
db_set_resident(m_szModuleName, "DCStatus");
db_set_resident(m_szModuleName, "CapBuf"); //capabilities bufer
db_set_resident(m_szModuleName, DBSETTING_STATUS_NOTE_TIME);
db_set_resident(m_szModuleName, DBSETTING_STATUS_MOOD);
// Setup services
CreateProtoService(PS_CREATEACCMGRUI, &CIcqProto::OnCreateAccMgrUI);
CreateProtoService(MS_ICQ_SENDSMS, &CIcqProto::SendSms);
CreateProtoService(PS_SETMYNICKNAME, &CIcqProto::SetNickName);
CreateProtoService(PS_GETMYAWAYMSG, &CIcqProto::GetMyAwayMsg);
CreateProtoService(PS_GETINFOSETTING, &CIcqProto::GetInfoSetting);
CreateProtoService(PSS_ADDED, &CIcqProto::SendYouWereAdded);
// ChangeInfo API
CreateProtoService(PS_CHANGEINFOEX, &CIcqProto::ChangeInfoEx);
// Avatar API
CreateProtoService(PS_GETAVATARINFOT, &CIcqProto::GetAvatarInfo);
CreateProtoService(PS_GETAVATARCAPS, &CIcqProto::GetAvatarCaps);
CreateProtoService(PS_GETMYAVATART, &CIcqProto::GetMyAvatar);
CreateProtoService(PS_SETMYAVATART, &CIcqProto::SetMyAvatar);
// Custom Status API
CreateProtoService(PS_SETCUSTOMSTATUSEX, &CIcqProto::SetXStatusEx);
CreateProtoService(PS_GETCUSTOMSTATUSEX, &CIcqProto::GetXStatusEx);
CreateProtoService(PS_GETCUSTOMSTATUSICON, &CIcqProto::GetXStatusIcon);
CreateProtoService(PS_GETADVANCEDSTATUSICON, &CIcqProto::RequestAdvStatusIconIdx);
CreateProtoService(PS_ICQ_REQUESTCUSTOMSTATUS, &CIcqProto::RequestXStatusDetails);
CreateProtoService(MS_ICQ_ADDSERVCONTACT, &CIcqProto::AddServerContact);
CreateProtoService(MS_REQ_AUTH, &CIcqProto::RequestAuthorization);
CreateProtoService(MS_GRANT_AUTH, &CIcqProto::GrantAuthorization);
CreateProtoService(MS_REVOKE_AUTH, &CIcqProto::RevokeAuthorization);
CreateProtoService(MS_XSTATUS_SHOWDETAILS, &CIcqProto::ShowXStatusDetails);
// Custom caps
CreateProtoService(PS_ICQ_ADDCAPABILITY, &CIcqProto::IcqAddCapability);
CreateProtoService(PS_ICQ_CHECKCAPABILITY, &CIcqProto::IcqCheckCapability);
// Reset a bunch of session specific settings
UpdateGlobalSettings();
ResetSettingsOnLoad();
// Initialize Contacts Cache
InitContactsCache();
// Startup Auto Info-Update thread
icq_InitInfoUpdate();
HookProtoEvent(ME_CLIST_PREBUILDSTATUSMENU, &CIcqProto::OnPreBuildStatusMenu);
// Register netlib users
NETLIBUSER nlu = { 0 };
TCHAR szBuffer[MAX_PATH + 64];
mir_sntprintf(szBuffer, SIZEOF(szBuffer), TranslateT("%s server connection"), m_tszUserName);
nlu.cbSize = sizeof(nlu);
nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_TCHAR;
nlu.ptszDescriptiveName = szBuffer;
nlu.szSettingsModule = m_szModuleName;
nlu.szHttpGatewayHello = "http://http.proxy.icq.com/hello";
nlu.szHttpGatewayUserAgent = "Mozilla/4.08 [en] (WinNT; U ;Nav)";
nlu.pfnHttpGatewayInit = icq_httpGatewayInit;
nlu.pfnHttpGatewayBegin = icq_httpGatewayBegin;
nlu.pfnHttpGatewayWrapSend = icq_httpGatewayWrapSend;
nlu.pfnHttpGatewayUnwrapRecv = icq_httpGatewayUnwrapRecv;
m_hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
char szP2PModuleName[MAX_PATH];
mir_snprintf(szP2PModuleName, SIZEOF(szP2PModuleName), "%sP2P", m_szModuleName);
mir_sntprintf(szBuffer, SIZEOF(szBuffer), TranslateT("%s client-to-client connections"), m_tszUserName);
nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_TCHAR;
nlu.ptszDescriptiveName = szBuffer;
nlu.szSettingsModule = szP2PModuleName;
nlu.minIncomingPorts = 1;
m_hDirectNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
// Register custom database events
DBEVENTTYPEDESCR eventType = { sizeof(eventType) };
eventType.eventType = ICQEVENTTYPE_MISSEDMESSAGE;
eventType.module = m_szModuleName;
eventType.descr = "Missed message notifications";
eventType.textService = ICQ_DB_GETEVENTTEXT_MISSEDMESSAGE;
eventType.flags = DETF_HISTORY | DETF_MSGWINDOW;
// for now keep default "message" icon
CallService(MS_DB_EVENT_REGISTERTYPE, 0, (LPARAM)&eventType);
// Protocol instance is ready
debugLogA("%s: Protocol instance '%s' created.", ICQ_PROTOCOL_NAME, m_szModuleName);
}
CIcqProto::~CIcqProto()
{
m_bXStatusEnabled = 10; // block clist changing
m_bMoodsEnabled = 10;
// Serv-list update board clean-up
FlushServerIDs();
/// TODO: make sure server-list handler thread is not running
/// TODO: save state of server-list update board to DB
servlistPendingFlushOperations();
SAFE_FREE((void**)&servlistQueueList);
// Finalize avatars
/// TODO: cleanup remaining avatar requests
SAFE_DELETE(&m_avatarsMutex);
// NetLib clean-up
NetLib_SafeCloseHandle(&m_hDirectNetlibUser);
NetLib_SafeCloseHandle(&m_hNetlibUser);
// Destroy hookable events
if (m_modeMsgsEvent)
DestroyHookableEvent(m_modeMsgsEvent);
// Clean-up remaining protocol instance members
UninitContactsCache();
SAFE_DELETE(&m_ratesMutex);
SAFE_DELETE(&servlistMutex);
SAFE_DELETE(&servlistQueueMutex);
SAFE_DELETE(&m_modeMsgsMutex);
SAFE_DELETE(&localSeqMutex);
SAFE_DELETE(&connectionHandleMutex);
SAFE_DELETE(&oftMutex);
SAFE_DELETE(&directConnListMutex);
SAFE_DELETE(&expectedFileRecvMutex);
SAFE_DELETE(&cookieMutex);
SAFE_FREE(&m_modeMsgs.szOnline);
SAFE_FREE(&m_modeMsgs.szAway);
SAFE_FREE(&m_modeMsgs.szNa);
SAFE_FREE(&m_modeMsgs.szOccupied);
SAFE_FREE(&m_modeMsgs.szDnd);
SAFE_FREE(&m_modeMsgs.szFfc);
debugLogA("%s: Protocol instance '%s' destroyed.", ICQ_PROTOCOL_NAME, m_szModuleName);
}
////////////////////////////////////////////////////////////////////////////////////////
// OnModulesLoadedEx - performs hook registration
int CIcqProto::OnModulesLoaded(WPARAM wParam, LPARAM lParam)
{
char pszP2PName[MAX_PATH];
char pszGroupsName[MAX_PATH];
char pszSrvGroupsName[MAX_PATH];
char* modules[5] = { 0, 0, 0, 0, 0 };
mir_snprintf(pszP2PName, SIZEOF(pszP2PName), "%sP2P", m_szModuleName);
mir_snprintf(pszGroupsName, SIZEOF(pszGroupsName), "%sGroups", m_szModuleName);
mir_snprintf(pszSrvGroupsName, SIZEOF(pszSrvGroupsName), "%sSrvGroups", m_szModuleName);
modules[0] = m_szModuleName;
modules[1] = pszP2PName;
modules[2] = pszGroupsName;
modules[3] = pszSrvGroupsName;
CallService("DBEditorpp/RegisterModule", (WPARAM)modules, (LPARAM)4);
HookProtoEvent(ME_OPT_INITIALISE, &CIcqProto::OnOptionsInit);
HookProtoEvent(ME_USERINFO_INITIALISE, &CIcqProto::OnUserInfoInit);
HookProtoEvent(ME_IDLE_CHANGED, &CIcqProto::OnIdleChanged);
// Init extra optional modules
ModuleLoad(0, 0);
InitXStatusItems(FALSE);
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
DWORD bXStatus = getContactXStatus(hContact);
if (bXStatus > 0)
setContactExtraIcon(hContact, bXStatus);
}
return 0;
}
int CIcqProto::OnPreShutdown(WPARAM wParam, LPARAM lParam)
{
// signal info update thread to stop
icq_InfoUpdateCleanup();
// Make sure all connections are closed
CloseContactDirectConns(NULL);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_AddToList - adds a contact to the contact list
MCONTACT CIcqProto::AddToList(int flags, PROTOSEARCHRESULT *psr)
{
if (psr == NULL) return 0;
char szUid[MAX_PATH];
if (psr->cbSize == sizeof(ICQSEARCHRESULT)) {
ICQSEARCHRESULT *isr = (ICQSEARCHRESULT*)psr;
if (isr->uin)
return AddToListByUIN(isr->uin, flags);
// aim contact
if (isr->hdr.flags & PSR_UNICODE)
unicode_to_ansi_static((WCHAR*)isr->hdr.id, szUid, MAX_PATH);
else
null_strcpy(szUid, (char*)isr->hdr.id, MAX_PATH);
return (szUid[0] == 0) ? 0 : AddToListByUID(szUid, flags);
}
if (psr->flags & PSR_UNICODE)
unicode_to_ansi_static((WCHAR*)psr->id, szUid, MAX_PATH);
else
null_strcpy(szUid, (char*)psr->id, MAX_PATH);
if (szUid[0] == 0)
return 0;
if (IsStringUIN(szUid))
return AddToListByUIN(atoi(szUid), flags);
return AddToListByUID(szUid, flags);
}
MCONTACT __cdecl CIcqProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent)
{
DWORD uin = 0;
uid_str uid = { 0 };
DBEVENTINFO dbei = { sizeof(dbei) };
if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == -1)
return 0;
dbei.pBlob = (PBYTE)_alloca(dbei.cbBlob + 1);
dbei.pBlob[dbei.cbBlob] = '\0';
if (db_event_get(hDbEvent, &dbei))
return 0; // failed to get event
if (strcmpnull(dbei.szModule, m_szModuleName))
return 0; // this event is not ours
switch (dbei.eventType) {
case EVENTTYPE_CONTACTS:
{
char *pbOffset = (char*)dbei.pBlob;
char *pbEnd = pbOffset + dbei.cbBlob;
for (int i = 0; i <= iContact; i++) {
pbOffset += strlennull(pbOffset) + 1; // Nick
if (pbOffset >= pbEnd) break;
if (i == iContact) { // we found the contact, get uid
if (IsStringUIN((char*)pbOffset))
uin = atoi((char*)pbOffset);
else {
uin = 0;
strcpy(uid, (char*)pbOffset);
}
}
pbOffset += strlennull(pbOffset) + 1; // Uin
if (pbOffset >= pbEnd) break;
}
}
break;
case EVENTTYPE_AUTHREQUEST:
case EVENTTYPE_ADDED:
if (!getContactUid(DbGetAuthEventContact(&dbei), &uin, &uid))
break;
default:
return 0;
}
if (uin != 0)
return AddToListByUIN(uin, flags); // Success
// add aim contact
if (strlennull(uid))
return AddToListByUID(uid, flags); // Success
return NULL; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_AuthAllow - processes the successful authorization
int CIcqProto::Authorize(HANDLE hDbEvent)
{
if (icqOnline() && hDbEvent) {
MCONTACT hContact = HContactFromAuthEvent(hDbEvent);
if (hContact == INVALID_CONTACT_ID)
return 1;
DWORD uin;
uid_str uid;
if (getContactUid(hContact, &uin, &uid))
return 1;
icq_sendAuthResponseServ(uin, uid, 1, _T(""));
delSetting(hContact, "Grant");
return 0; // Success
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_AuthDeny - handles the unsuccessful authorization
int CIcqProto::AuthDeny(HANDLE hDbEvent, const TCHAR* szReason)
{
if (icqOnline() && hDbEvent) {
MCONTACT hContact = HContactFromAuthEvent(hDbEvent);
if (hContact == INVALID_CONTACT_ID)
return 1;
DWORD uin;
uid_str uid;
if (getContactUid(hContact, &uin, &uid))
return 1;
icq_sendAuthResponseServ(uin, uid, 0, szReason);
if (db_get_b(hContact, "CList", "NotOnList", 0))
CallService(MS_DB_CONTACT_DELETE, hContact, 0);
return 0; // Success
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PSR_AUTH
int __cdecl CIcqProto::AuthRecv(MCONTACT hContact, PROTORECVEVENT* pre)
{
setContactHidden(hContact, 0);
ICQAddRecvEvent(NULL, EVENTTYPE_AUTHREQUEST, pre, pre->lParam, (PBYTE)pre->szMessage, 0);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// PSS_AUTHREQUEST
int __cdecl CIcqProto::AuthRequest(MCONTACT hContact, const TCHAR* szMessage)
{
if (!icqOnline())
return 1;
if (hContact) {
DWORD dwUin;
uid_str szUid;
if (getContactUid(hContact, &dwUin, &szUid))
return 1; // Invalid contact
if (dwUin) {
char *utf = tchar_to_utf8(szMessage);
icq_sendAuthReqServ(dwUin, szUid, utf);
SAFE_FREE(&utf);
return 0; // Success
}
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_FileAllow - starts a file transfer
HANDLE __cdecl CIcqProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const TCHAR* szPath)
{
DWORD dwUin;
uid_str szUid;
if (getContactUid(hContact, &dwUin, &szUid))
return 0; // Invalid contact
if (icqOnline() && hContact && szPath && hTransfer) { // approve old fashioned file transfer
basic_filetransfer *ft = (basic_filetransfer *)hTransfer;
if (!IsValidFileTransfer(ft))
return 0; // Invalid transfer
if (dwUin && ft->ft_magic == FT_MAGIC_ICQ) {
filetransfer *ft = (filetransfer *)hTransfer;
ft->szSavePath = tchar_to_utf8(szPath);
{
icq_lock l(expectedFileRecvMutex);
expectedFileRecvs.insert(ft);
}
// Was request received thru DC and have we a open DC, send through that
if (ft->bDC && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0))
icq_sendFileAcceptDirect(hContact, ft);
else
icq_sendFileAcceptServ(dwUin, ft, 0);
return hTransfer; // Success
}
else if (ft->ft_magic == FT_MAGIC_OSCAR) { // approve oscar file transfer
return oftFileAllow(hContact, hTransfer, szPath);
}
}
return 0; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_FileCancel - cancels a file transfer
int __cdecl CIcqProto::FileCancel(MCONTACT hContact, HANDLE hTransfer)
{
DWORD dwUin;
uid_str szUid;
if (getContactUid(hContact, &dwUin, &szUid))
return 1; // Invalid contact
if (hContact && hTransfer) {
basic_filetransfer *ft = (basic_filetransfer *)hTransfer;
if (!IsValidFileTransfer(ft))
return 1; // Invalid transfer
if (dwUin && ft->ft_magic == FT_MAGIC_ICQ) { // cancel old fashioned file transfer
filetransfer * ft = (filetransfer*)hTransfer;
icq_CancelFileTransfer(hContact, ft);
return 0; // Success
}
else if (ft->ft_magic == FT_MAGIC_OSCAR) { // cancel oscar file transfer
return oftFileCancel(hContact, hTransfer);
}
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_FileDeny - denies a file transfer
int __cdecl CIcqProto::FileDeny(MCONTACT hContact, HANDLE hTransfer, const TCHAR* szReason)
{
int nReturnValue = 1;
DWORD dwUin;
uid_str szUid;
basic_filetransfer *ft = (basic_filetransfer*)hTransfer;
if (getContactUid(hContact, &dwUin, &szUid))
return 1; // Invalid contact
if (icqOnline() && hTransfer && hContact) {
if (!IsValidFileTransfer(hTransfer))
return 1; // Invalid transfer
if (dwUin && ft->ft_magic == FT_MAGIC_ICQ) { // deny old fashioned file transfer
filetransfer *ft = (filetransfer*)hTransfer;
char *szReasonUtf = tchar_to_utf8(szReason);
// Was request received thru DC and have we a open DC, send through that
if (ft->bDC && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0))
icq_sendFileDenyDirect(hContact, ft, szReasonUtf);
else
icq_sendFileDenyServ(dwUin, ft, szReasonUtf, 0);
SAFE_FREE(&szReasonUtf);
nReturnValue = 0; // Success
}
else if (ft->ft_magic == FT_MAGIC_OSCAR) { // deny oscar file transfer
return oftFileDeny(hContact, hTransfer, szReason);
}
}
// Release possible orphan structure
SafeReleaseFileTransfer((void**)&ft);
return nReturnValue;
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_FileResume - processes file renaming etc
int __cdecl CIcqProto::FileResume(HANDLE hTransfer, int* action, const TCHAR** szFilename)
{
if (icqOnline() && hTransfer) {
basic_filetransfer *ft = (basic_filetransfer *)hTransfer;
if (!IsValidFileTransfer(ft))
return 1; // Invalid transfer
if (ft->ft_magic == FT_MAGIC_ICQ) {
char *szFileNameUtf = tchar_to_utf8(*szFilename);
icq_sendFileResume((filetransfer *)hTransfer, *action, szFileNameUtf);
SAFE_FREE(&szFileNameUtf);
}
else if (ft->ft_magic == FT_MAGIC_OSCAR) {
oftFileResume((oscar_filetransfer *)hTransfer, *action, *szFilename);
}
else
return 1; // Failure
return 0; // Success
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// GetCaps - return protocol capabilities bits
DWORD_PTR __cdecl CIcqProto::GetCaps(int type, MCONTACT hContact)
{
DWORD_PTR nReturn = 0;
switch (type) {
case PFLAGNUM_1:
nReturn = PF1_IM | PF1_URL | PF1_AUTHREQ | PF1_BASICSEARCH | PF1_ADDSEARCHRES |
PF1_VISLIST | PF1_INVISLIST | PF1_MODEMSG | PF1_FILE | PF1_EXTSEARCH |
PF1_EXTSEARCHUI | PF1_SEARCHBYEMAIL | PF1_SEARCHBYNAME |
PF1_ADDED | PF1_CONTACT;
if (!m_bAimEnabled)
nReturn |= PF1_NUMERICUSERID;
if (m_bSsiEnabled && getByte("ServerAddRemove", DEFAULT_SS_ADDSERVER))
nReturn |= PF1_SERVERCLIST;
break;
case PFLAGNUM_2:
nReturn = PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND |
PF2_FREECHAT | PF2_INVISIBLE;
if (m_bAimEnabled)
nReturn |= PF2_ONTHEPHONE;
break;
case PFLAGNUM_3:
nReturn = PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND |
PF2_FREECHAT | PF2_INVISIBLE;
break;
case PFLAGNUM_4:
nReturn = PF4_SUPPORTIDLE | PF4_IMSENDUTF | PF4_IMSENDOFFLINE | PF4_INFOSETTINGSVC;
if (m_bAvatarsEnabled)
nReturn |= PF4_AVATARS;
#ifdef DBG_CAPMTN
nReturn |= PF4_SUPPORTTYPING;
#endif
break;
case PFLAGNUM_5:
nReturn = PF2_FREECHAT;
if (m_bAimEnabled)
nReturn |= PF2_ONTHEPHONE;
break;
case PFLAG_UNIQUEIDTEXT:
nReturn = (DWORD_PTR)Translate("User ID");
break;
case PFLAG_UNIQUEIDSETTING:
nReturn = (DWORD_PTR)UNIQUEIDSETTING;
break;
case PFLAG_MAXCONTACTSPERPACKET:
if (hContact) { // determine per contact
BYTE bClientId = getByte(hContact, "ClientID", CLID_GENERIC);
if (bClientId == CLID_MIRANDA) {
if (CheckContactCapabilities(hContact, CAPF_CONTACTS) && getContactStatus(hContact) != ID_STATUS_OFFLINE)
nReturn = 0x100; // limited only by packet size
else
nReturn = MAX_CONTACTSSEND;
}
else if (bClientId == CLID_ICQ6) {
if (CheckContactCapabilities(hContact, CAPF_CONTACTS))
nReturn = 1; // crapy ICQ6 cannot handle multiple contacts in the transfer
else
nReturn = 0; // this version does not support contacts transfer at all
}
else
nReturn = MAX_CONTACTSSEND;
}
else // return generic limit
nReturn = MAX_CONTACTSSEND;
break;
case PFLAG_MAXLENOFMESSAGE:
nReturn = MAX_MESSAGESNACSIZE - 102;
}
return nReturn;
}
////////////////////////////////////////////////////////////////////////////////////////
// GetInfo - retrieves a contact info
int __cdecl CIcqProto::GetInfo(MCONTACT hContact, int infoType)
{
if (icqOnline()) {
DWORD dwUin;
uid_str szUid;
if (getContactUid(hContact, &dwUin, &szUid))
return 1; // Invalid contact
DWORD dwCookie;
if (dwUin)
dwCookie = icq_sendGetInfoServ(hContact, dwUin, (infoType & SGIF_ONOPEN) != 0);
else // TODO: this needs something better
dwCookie = icq_sendGetAimProfileServ(hContact, szUid);
return (dwCookie) ? 0 : 1;
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// SearchBasic - searches the contact by UID
void CIcqProto::CheekySearchThread(void*)
{
char szUin[UINMAXLEN];
ICQSEARCHRESULT isr = { 0 };
isr.hdr.cbSize = sizeof(isr);
if (cheekySearchUin) {
_itoa(cheekySearchUin, szUin, 10);
isr.hdr.id = (FNAMECHAR*)szUin;
}
else {
isr.hdr.id = (FNAMECHAR*)cheekySearchUid;
}
isr.uin = cheekySearchUin;
ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)cheekySearchId, (LPARAM)&isr);
ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)cheekySearchId, 0);
cheekySearchId = -1;
}
HANDLE __cdecl CIcqProto::SearchBasic(const PROTOCHAR *pszSearch)
{
if (strlennull(pszSearch) == 0)
return 0;
char pszUIN[255];
int nHandle = 0;
int i, j;
if (!m_bAimEnabled) {
for (i = j = 0; (i < strlennull(pszSearch)) && (j < 255); i++) { // we take only numbers
if ((pszSearch[i] >= 0x30) && (pszSearch[i] <= 0x39)) {
pszUIN[j] = pszSearch[i];
j++;
}
}
}
else {
for (i = j = 0; (i < strlennull(pszSearch)) && (j < 255); i++) { // we remove spaces and slashes
if ((pszSearch[i] != 0x20) && (pszSearch[i] != '-')) {
if (pszSearch[i] >= 0x80) continue;
pszUIN[j] = pszSearch[i];
j++;
}
}
}
pszUIN[j] = 0;
if (strlennull(pszUIN)) {
DWORD dwUin;
if (IsStringUIN(pszUIN))
dwUin = atoi(pszUIN);
else
dwUin = 0;
// Cheeky instant UIN search
if (!dwUin || GetKeyState(VK_CONTROL) & 0x8000) {
cheekySearchId = GenerateCookie(0);
cheekySearchUin = dwUin;
cheekySearchUid = null_strdup(pszUIN);
ForkThread(&CIcqProto::CheekySearchThread, 0); // The caller needs to get this return value before the results
nHandle = cheekySearchId;
}
else if (icqOnline()) {
nHandle = SearchByUin(dwUin);
}
// Success
return (HANDLE)nHandle;
}
// Failure
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// SearchByEmail - searches the contact by its e-mail
HANDLE __cdecl CIcqProto::SearchByEmail(const PROTOCHAR *email)
{
if (email && icqOnline() && strlennull(email) > 0) {
char *szEmail = tchar_to_ansi(email);
// Success
DWORD dwSearchId = SearchByMail(szEmail);
DWORD dwSecId = (dwSearchId == 0 && m_bAimEnabled) ? icq_searchAimByEmail(szEmail, dwSearchId) : 0;
SAFE_FREE(&szEmail);
if (dwSearchId)
return (HANDLE)dwSearchId;
else
return (HANDLE)dwSecId;
}
return 0; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_SearchByName - searches the contact by its first or last name, or by a nickname
HANDLE __cdecl CIcqProto::SearchByName(const PROTOCHAR *nick, const PROTOCHAR *firstName, const PROTOCHAR *lastName)
{
if (icqOnline()) {
if (nick || firstName || lastName) {
char *nickUtf = tchar_to_utf8(nick);
char *firstNameUtf = tchar_to_utf8(firstName);
char *lastNameUtf = tchar_to_utf8(lastName);
// Success
HANDLE dwCookie = (HANDLE)SearchByNames(nickUtf, firstNameUtf, lastNameUtf, 0);
SAFE_FREE(&nickUtf);
SAFE_FREE(&firstNameUtf);
SAFE_FREE(&lastNameUtf);
return dwCookie;
}
}
return 0; // Failure
}
HWND __cdecl CIcqProto::CreateExtendedSearchUI(HWND parent)
{
if (parent && hInst)
return CreateDialog(hInst, MAKEINTRESOURCE(IDD_ICQADVANCEDSEARCH), parent, AdvancedSearchDlgProc);
return NULL; // Failure
}
HWND __cdecl CIcqProto::SearchAdvanced(HWND hwndDlg)
{
if (icqOnline() && IsWindow(hwndDlg)) {
int nDataLen;
BYTE* bySearchData;
if (bySearchData = createAdvancedSearchStructure(hwndDlg, &nDataLen)) {
int result = icq_sendAdvancedSearchServ(bySearchData, nDataLen);
SAFE_FREE((void**)&bySearchData);
return (HWND)result; // Success
}
}
return NULL; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// RecvContacts
int __cdecl CIcqProto::RecvContacts(MCONTACT hContact, PROTORECVEVENT* pre)
{
ICQSEARCHRESULT **isrList = (ICQSEARCHRESULT**)pre->szMessage;
int i;
DWORD cbBlob = 0;
DWORD flags = 0;
if (pre->flags & PREF_UTF || pre->flags & PREF_UNICODE)
flags |= DBEF_UTF;
for (i = 0; i < pre->lParam; i++) {
if (pre->flags & PREF_UNICODE)
cbBlob += get_utf8_size((WCHAR*)isrList[i]->hdr.nick) + 2;
else
cbBlob += strlennull((char*)isrList[i]->hdr.nick) + 2; // both trailing zeros
if (isrList[i]->uin)
cbBlob += getUINLen(isrList[i]->uin);
else if (pre->flags & PREF_UNICODE)
cbBlob += strlennull((WCHAR*)isrList[i]->hdr.id);
else
cbBlob += strlennull((char*)isrList[i]->hdr.id);
}
PBYTE pBlob = (PBYTE)_alloca(cbBlob), pCurBlob;
for (i = 0, pCurBlob = pBlob; i < pre->lParam; i++) {
if (pre->flags & PREF_UNICODE)
make_utf8_string_static((WCHAR*)isrList[i]->hdr.nick, (char*)pCurBlob, cbBlob - (pCurBlob - pBlob));
else
strcpy((char*)pCurBlob, (char*)isrList[i]->hdr.nick);
pCurBlob += strlennull((char*)pCurBlob) + 1;
if (isrList[i]->uin) {
char szUin[UINMAXLEN];
_itoa(isrList[i]->uin, szUin, 10);
strcpy((char*)pCurBlob, szUin);
}
else { // aim contact
if (pre->flags & PREF_UNICODE)
unicode_to_ansi_static((WCHAR*)isrList[i]->hdr.id, (char*)pCurBlob, cbBlob - (pCurBlob - pBlob));
else
strcpy((char*)pCurBlob, (char*)isrList[i]->hdr.id);
}
pCurBlob += strlennull((char*)pCurBlob) + 1;
}
ICQAddRecvEvent(hContact, EVENTTYPE_CONTACTS, pre, cbBlob, pBlob, flags);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// RecvFile
int __cdecl CIcqProto::RecvFile(MCONTACT hContact, PROTORECVFILET* evt)
{
return Proto_RecvFile(hContact, evt);
}
////////////////////////////////////////////////////////////////////////////////////////
// RecvMsg
int __cdecl CIcqProto::RecvMsg(MCONTACT hContact, PROTORECVEVENT* pre)
{
DWORD cbBlob;
DWORD flags = 0;
cbBlob = strlennull(pre->szMessage) + 1;
// process utf-8 encoded messages
if ((pre->flags & PREF_UTF) && !IsUSASCII(pre->szMessage, strlennull(pre->szMessage)))
flags |= DBEF_UTF;
// process unicode ucs-2 messages
if ((pre->flags & PREF_UNICODE) && !IsUnicodeAscii((WCHAR*)(pre->szMessage + cbBlob), strlennull((WCHAR*)(pre->szMessage + cbBlob))))
cbBlob *= (sizeof(WCHAR)+1);
ICQAddRecvEvent(hContact, EVENTTYPE_MESSAGE, pre, cbBlob, (PBYTE)pre->szMessage, flags);
// stop contact from typing - some clients do not sent stop notify
if (CheckContactCapabilities(hContact, CAPF_TYPING))
CallService(MS_PROTO_CONTACTISTYPING, hContact, PROTOTYPE_CONTACTTYPING_OFF);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// RecvUrl
int __cdecl CIcqProto::RecvUrl(MCONTACT hContact, PROTORECVEVENT*)
{
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////
// SendContacts
int __cdecl CIcqProto::SendContacts(MCONTACT hContact, int flags, int nContacts, MCONTACT *hContactsList)
{
if (hContact && hContactsList) {
int i;
DWORD dwUin;
uid_str szUid;
WORD wRecipientStatus;
DWORD dwCookie;
if (getContactUid(hContact, &dwUin, &szUid)) { // Invalid contact
return ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "The receiver has an invalid user ID.");
}
wRecipientStatus = getContactStatus(hContact);
// Failures
if (!icqOnline()) {
dwCookie = ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "You cannot send messages when you are offline.");
}
else if (!hContactsList || (nContacts < 1) || (nContacts > MAX_CONTACTSSEND)) {
dwCookie = ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "Bad data (internal error #1)");
}
// OK
else {
if (CheckContactCapabilities(hContact, CAPF_CONTACTS) && wRecipientStatus != ID_STATUS_OFFLINE) { // Use the new format if possible
int nDataLen, nNamesLen;
struct icq_contactsend_s* contacts = NULL;
// Format the data part and the names part
// This is kinda messy, but there is no simple way to do it. First
// we need to calculate the length of the packet.
contacts = (struct icq_contactsend_s*)_alloca(sizeof(struct icq_contactsend_s)*nContacts);
ZeroMemory(contacts, sizeof(struct icq_contactsend_s)*nContacts);
{
nDataLen = 0; nNamesLen = 0;
for (i = 0; i < nContacts; i++) {
uid_str szContactUid;
if (!IsICQContact(hContactsList[i]))
break; // Abort if a non icq contact is found
if (getContactUid(hContactsList[i], &contacts[i].uin, &szContactUid))
break; // Abort if invalid contact
contacts[i].uid = contacts[i].uin ? NULL : null_strdup(szContactUid);
contacts[i].szNick = NickFromHandleUtf(hContactsList[i]);
nDataLen += getUIDLen(contacts[i].uin, contacts[i].uid) + 4;
nNamesLen += strlennull(contacts[i].szNick) + 8;
}
if (i == nContacts) {
debugLogA("Sending contacts to %s.", strUID(dwUin, szUid));
// Do not calculate the exact size of the data packet - only the maximal size (easier)
// Sumarize size of group information
// - we do not utilize the full power of the protocol and send all contacts with group "General"
// just like ICQ6 does
nDataLen += 9;
nNamesLen += 9;
// Create data structures
icq_packet mData, mNames;
mData.wPlace = 0;
mData.pData = (LPBYTE)SAFE_MALLOC(nDataLen);
mData.wLen = nDataLen;
mNames.wPlace = 0;
mNames.pData = (LPBYTE)SAFE_MALLOC(nNamesLen);
// pack Group Name
packWord(&mData, 7);
packBuffer(&mData, (LPBYTE)"General", 7);
packWord(&mNames, 7);
packBuffer(&mNames, (LPBYTE)"General", 7);
// all contacts in one group
packWord(&mData, (WORD)nContacts);
packWord(&mNames, (WORD)nContacts);
for (i = 0; i < nContacts; i++) {
uid_str szContactUid;
WORD wLen;
if (contacts[i].uin)
strUID(contacts[i].uin, szContactUid);
else
strcpy(szContactUid, contacts[i].uid);
// prepare UID
wLen = strlennull(szContactUid);
packWord(&mData, wLen);
packBuffer(&mData, (LPBYTE)szContactUid, wLen);
// prepare Nick
wLen = strlennull(contacts[i].szNick);
packWord(&mNames, (WORD)(wLen + 4));
packTLV(&mNames, 0x01, wLen, (LPBYTE)contacts[i].szNick);
}
// Cleanup temporary list
for (i = 0; i < nContacts; i++) {
SAFE_FREE(&contacts[i].szNick);
SAFE_FREE(&contacts[i].uid);
}
// Rate check
if (IsServerOverRate(ICQ_MSG_FAMILY, ICQ_MSG_SRV_SEND, RML_LIMIT)) { // rate is too high, the message will not go thru...
SAFE_FREE((void**)&mData.pData);
SAFE_FREE((void**)&mNames.pData);
return ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "The message could not be delivered. You are sending too fast. Wait a while and try again.");
}
// Set up the ack type
cookie_message_data *pCookieData = CreateMessageCookieData(MTYPE_CONTACTS, hContact, dwUin, FALSE);
// AIM clients do not send acknowledgement
if (!dwUin && pCookieData->nAckType == ACKTYPE_CLIENT)
pCookieData->nAckType = ACKTYPE_SERVER;
// Send the message
dwCookie = icq_SendChannel2Contacts(dwUin, szUid, hContact, (char*)mData.pData, mData.wPlace, (char*)mNames.pData, mNames.wPlace, pCookieData);
// This will stop the message dialog from waiting for the real message delivery ack
if (pCookieData->nAckType == ACKTYPE_NONE) {
SendProtoAck(hContact, dwCookie, ACKRESULT_SUCCESS, ACKTYPE_CONTACTS, NULL);
// We need to free this here since we will never see the real ack
// The actual cookie value will still have to be returned to the message dialog though
ReleaseCookie(dwCookie);
}
// Release our buffers
SAFE_FREE((void**)&mData.pData);
SAFE_FREE((void**)&mNames.pData);
}
else {
dwCookie = ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "Bad data (internal error #2)");
}
for (i = 0; i < nContacts; i++) {
SAFE_FREE(&contacts[i].szNick);
SAFE_FREE(&contacts[i].uid);
}
}
}
else if (dwUin) { // old format is only understood by ICQ clients
int nBodyLength;
char szContactUin[UINMAXLEN];
char szCount[17];
struct icq_contactsend_s* contacts = NULL;
uid_str szContactUid;
// Format the body
// This is kinda messy, but there is no simple way to do it. First
// we need to calculate the length of the packet.
contacts = (struct icq_contactsend_s*)_alloca(sizeof(struct icq_contactsend_s)*nContacts);
ZeroMemory(contacts, sizeof(struct icq_contactsend_s)*nContacts);
{
nBodyLength = 0;
for (i = 0; i < nContacts; i++) {
if (!IsICQContact(hContactsList[i]))
break; // Abort if a non icq contact is found
if (getContactUid(hContactsList[i], &contacts[i].uin, &szContactUid))
break; // Abort if invalid contact
contacts[i].uid = contacts[i].uin ? NULL : null_strdup(szContactUid);
contacts[i].szNick = NickFromHandle(hContactsList[i]);
// Compute this contact's length
nBodyLength += getUIDLen(contacts[i].uin, contacts[i].uid) + 1;
nBodyLength += strlennull(contacts[i].szNick) + 1;
}
if (i == nContacts) {
debugLogA("Sending contacts to %d.", dwUin);
// Compute count record's length
_itoa(nContacts, szCount, 10);
nBodyLength += strlennull(szCount) + 1;
// Finally we need to copy the contact data into the packet body
char *pBody, *pBuffer = pBody = (char *)SAFE_MALLOC(nBodyLength);
null_strcpy(pBuffer, szCount, nBodyLength - 1);
pBuffer += strlennull(pBuffer);
*pBuffer++ = (char)0xFE;
for (i = 0; i < nContacts; i++) {
if (contacts[i].uin) {
_itoa(contacts[i].uin, szContactUin, 10);
strcpy(pBuffer, szContactUin);
}
else
strcpy(pBuffer, contacts[i].uid);
pBuffer += strlennull(pBuffer);
*pBuffer++ = (char)0xFE;
strcpy(pBuffer, contacts[i].szNick);
pBuffer += strlennull(pBuffer);
*pBuffer++ = (char)0xFE;
}
for (i = 0; i < nContacts; i++) { // release memory
SAFE_FREE(&contacts[i].szNick);
SAFE_FREE(&contacts[i].uid);
}
// Set up the ack type
cookie_message_data *pCookieData = CreateMessageCookieData(MTYPE_CONTACTS, hContact, dwUin, TRUE);
if (m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) {
int iRes = icq_SendDirectMessage(hContact, pBody, nBodyLength, 1, pCookieData, NULL);
if (iRes) {
SAFE_FREE((void**)&pBody);
return iRes; // we succeded, return
}
}
// Rate check
if (IsServerOverRate(ICQ_MSG_FAMILY, ICQ_MSG_SRV_SEND, RML_LIMIT)) { // rate is too high, the message will not go thru...
SAFE_FREE((void**)&pCookieData);
SAFE_FREE((void**)&pBody);
return ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "The message could not be delivered. You are sending too fast. Wait a while and try again.");
}
// Select channel and send
/*
if (!CheckContactCapabilities(hContact, CAPF_SRV_RELAY) || wRecipientStatus == ID_STATUS_OFFLINE)
{
dwCookie = icq_SendChannel4Message(dwUin, hContact, MTYPE_CONTACTS, (WORD)nBodyLength, pBody, pCookieData);
}
else
*/
{
WORD wPriority;
if (wRecipientStatus == ID_STATUS_ONLINE || wRecipientStatus == ID_STATUS_FREECHAT)
wPriority = 0x0001;
else
wPriority = 0x0021;
dwCookie = icq_SendChannel2Message(dwUin, hContact, pBody, nBodyLength, wPriority, pCookieData, NULL);
}
// This will stop the message dialog from waiting for the real message delivery ack
if (pCookieData->nAckType == ACKTYPE_NONE) {
SendProtoAck(hContact, dwCookie, ACKRESULT_SUCCESS, ACKTYPE_CONTACTS, NULL);
// We need to free this here since we will never see the real ack
// The actual cookie value will still have to be returned to the message dialog though
ReleaseCookie(dwCookie);
}
SAFE_FREE((void**)&pBody);
}
else {
dwCookie = ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "Bad data (internal error #2)");
}
}
}
else {
dwCookie = ReportGenericSendError(hContact, ACKTYPE_CONTACTS, "The reciever does not support receiving of contacts.");
}
}
return dwCookie;
}
// Exit with Failure
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// SendFile - sends a file
HANDLE __cdecl CIcqProto::SendFile(MCONTACT hContact, const TCHAR* szDescription, TCHAR** ppszFiles)
{
if (!icqOnline())
return 0;
if (hContact && szDescription && ppszFiles) {
DWORD dwUin;
uid_str szUid;
if (getContactUid(hContact, &dwUin, &szUid))
return 0; // Invalid contact
if (getContactStatus(hContact) != ID_STATUS_OFFLINE) {
if (CheckContactCapabilities(hContact, CAPF_OSCAR_FILE))
return oftInitTransfer(hContact, dwUin, szUid, (LPCTSTR*)ppszFiles, szDescription);
if (dwUin) {
WORD wClientVersion = getWord(hContact, "Version", 7);
if (wClientVersion < 7)
debugLogA("IcqSendFile() can't send to version %u", wClientVersion);
else {
int i;
filetransfer* ft;
struct _stat statbuf;
// Initialize filetransfer struct
ft = CreateFileTransfer(hContact, dwUin, (wClientVersion == 7) ? 7 : 8);
for (ft->dwFileCount = 0; ppszFiles[ft->dwFileCount]; ft->dwFileCount++);
ft->pszFiles = (char **)SAFE_MALLOC(sizeof(char *)* ft->dwFileCount);
ft->dwTotalSize = 0;
for (i = 0; i < (int)ft->dwFileCount; i++) {
ft->pszFiles[i] = (ppszFiles[i]) ? tchar_to_utf8(ppszFiles[i]) : NULL;
if (_tstat(ppszFiles[i], &statbuf))
debugLogA("IcqSendFile() was passed invalid filename(s)");
else
ft->dwTotalSize += statbuf.st_size;
}
ft->szDescription = tchar_to_utf8(szDescription);
ft->dwTransferSpeed = 100;
ft->sending = 1;
ft->fileId = -1;
ft->iCurrentFile = 0;
ft->dwCookie = AllocateCookie(CKT_FILE, 0, hContact, ft);
ft->hConnection = NULL;
// Send file transfer request
debugLogA("Init file send");
char *pszFiles;
if (ft->dwFileCount == 1) {
pszFiles = strchr(ft->pszFiles[0], '\\');
if (pszFiles)
pszFiles++;
else
pszFiles = ft->pszFiles[0];
}
else {
char szFiles[64], tmp[64];
mir_snprintf(szFiles, SIZEOF(szFiles), ICQTranslateUtfStatic("%d Files", tmp, SIZEOF(tmp)), ft->dwFileCount);
pszFiles = szFiles;
}
// Send packet
if (ft->nVersion == 7) {
if (m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) {
int iRes = icq_sendFileSendDirectv7(ft, pszFiles);
if (iRes) return ft; // Success
}
debugLogA("Sending v%u file transfer request through server", 7);
icq_sendFileSendServv7(ft, pszFiles);
}
else {
if (m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) {
int iRes = icq_sendFileSendDirectv8(ft, pszFiles);
if (iRes)
return ft; // Success
}
debugLogA("Sending v%u file transfer request through server", 8);
icq_sendFileSendServv8(ft, pszFiles, ACKTYPE_NONE);
}
return ft; // Success
}
}
}
}
return 0; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_SendMessage - sends a message
int __cdecl CIcqProto::SendMsg(MCONTACT hContact, int flags, const char* pszSrc)
{
if (hContact == NULL || pszSrc == NULL)
return NULL;
DWORD dwCookie;
char* puszText = NULL;
int bNeedFreeU = 0;
cookie_message_data *pCookieData = NULL;
// Invalid contact
DWORD dwUin;
uid_str szUID;
if (getContactUid(hContact, &dwUin, &szUID))
return ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "The receiver has an invalid user ID.");
if (flags & PREF_UNICODE) {
puszText = make_utf8_string((WCHAR*)(pszSrc + strlennull(pszSrc) + 1)); // get the UTF-16 part
bNeedFreeU = 1;
}
else if (flags & PREF_UTF)
puszText = (char*)pszSrc;
else {
puszText = (char*)ansi_to_utf8(pszSrc);
bNeedFreeU = 1;
}
WORD wRecipientStatus = getContactStatus(hContact);
BOOL plain_ascii = IsUSASCII(puszText, strlennull(puszText));
BOOL oldAnsi = plain_ascii || !m_bUtfEnabled ||
(!(flags & (PREF_UTF | PREF_UNICODE)) && m_bUtfEnabled == 1) ||
!CheckContactCapabilities(hContact, CAPF_UTF) ||
!getByte(hContact, "UnicodeSend", 1);
if (m_bTempVisListEnabled && m_iStatus == ID_STATUS_INVISIBLE)
makeContactTemporaryVisible(hContact); // make us temporarily visible to contact
// Failure scenarios
if (!icqOnline())
dwCookie = ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "You cannot send messages when you are offline.");
else if ((wRecipientStatus == ID_STATUS_OFFLINE) && (strlennull(puszText) > 4096))
dwCookie = ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "Messages to offline contacts must be shorter than 4096 characters.");
// Looks OK
else {
debugLogA("Send %smessage - Message cap is %u", puszText ? "unicode " : "", CheckContactCapabilities(hContact, CAPF_SRV_RELAY));
debugLogA("Send %smessage - Contact status is %u", puszText ? "unicode " : "", wRecipientStatus);
if (dwUin && m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) { // send thru direct
char *dc_msg = puszText;
char *dc_cap = plain_ascii ? NULL : CAP_UTF8MSGS;
char *szUserAnsi = NULL;
if (!plain_ascii && oldAnsi) {
szUserAnsi = ConvertMsgToUserSpecificAnsi(hContact, puszText);
if (szUserAnsi) {
dc_msg = szUserAnsi;
dc_cap = NULL;
}
}
// Set up the ack type
pCookieData = CreateMessageCookieData(MTYPE_PLAIN, hContact, dwUin, TRUE);
pCookieData->nAckType = ACKTYPE_CLIENT;
dwCookie = icq_SendDirectMessage(hContact, dc_msg, strlennull(dc_msg), 1, pCookieData, dc_cap);
SAFE_FREE(&szUserAnsi);
if (dwCookie) { // free the buffers if alloced
if (bNeedFreeU) SAFE_FREE(&puszText);
return dwCookie; // we succeded, return
}
// on failure, fallback to send thru server
}
/// TODO: add support for RTL & user customizable font
{
char *mng = MangleXml(puszText, strlennull(puszText));
int len = strlennull(mng);
mng = (char*)SAFE_REALLOC(mng, len + 28);
memmove(mng + 12, mng, len + 1);
memcpy(mng, "<HTML><BODY>", 12);
strcat(mng, "</BODY></HTML>");
if (bNeedFreeU) SAFE_FREE(&puszText);
puszText = mng;
bNeedFreeU = 1;
}
WCHAR *pwszText = plain_ascii ? NULL : make_unicode_string(puszText);
if ((plain_ascii ? strlennull(puszText) : strlennull(pwszText) * sizeof(WCHAR)) > MAX_MESSAGESNACSIZE) { // max length check // TLV(2) is currently limited to 0xA00 bytes in online mode
// only limit to not get disconnected, all other will be handled by error 0x0A
dwCookie = ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "The message could not be delivered, it is too long.");
// free the buffers if alloced
SAFE_FREE((void**)&pwszText);
if (bNeedFreeU) SAFE_FREE(&puszText);
return dwCookie;
}
// Rate check
if (IsServerOverRate(ICQ_MSG_FAMILY, ICQ_MSG_SRV_SEND, RML_LIMIT)) { // rate is too high, the message will not go thru...
dwCookie = ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "The message could not be delivered. You are sending too fast. Wait a while and try again.");
// free the buffers if alloced
SAFE_FREE((void**)&pwszText);
if (bNeedFreeU) SAFE_FREE(&puszText);
return dwCookie;
}
pCookieData = CreateMessageCookieData(MTYPE_PLAIN, hContact, dwUin, FALSE);
if (plain_ascii)
dwCookie = icq_SendChannel1Message(dwUin, szUID, hContact, puszText, pCookieData);
else
dwCookie = icq_SendChannel1MessageW(dwUin, szUID, hContact, pwszText, pCookieData);
// free the unicode message
SAFE_FREE((void**)&pwszText);
// This will stop the message dialog from waiting for the real message delivery ack
if (pCookieData && pCookieData->nAckType == ACKTYPE_NONE) {
SendProtoAck(hContact, dwCookie, ACKRESULT_SUCCESS, ACKTYPE_MESSAGE, NULL);
// We need to free this here since we will never see the real ack
// The actual cookie value will still have to be returned to the message dialog though
ReleaseCookie(dwCookie);
}
}
// free the buffers if alloced
if (bNeedFreeU) SAFE_FREE(&puszText);
return dwCookie; // Success
}
////////////////////////////////////////////////////////////////////////////////////////
// SendUrl
int __cdecl CIcqProto::SendUrl(MCONTACT hContact, int flags, const char* url)
{
if (hContact == NULL || url == NULL)
return 0;
DWORD dwUin;
if (getContactUid(hContact, &dwUin, NULL)) // Invalid contact
return ReportGenericSendError(hContact, ACKTYPE_URL, "The receiver has an invalid user ID.");
WORD wRecipientStatus = getContactStatus(hContact);
// Failure
if (!icqOnline()) {
ReportGenericSendError(hContact, ACKTYPE_URL, "You cannot send messages when you are offline.");
return 0;
}
// Set up the ack type
cookie_message_data *pCookieData = CreateMessageCookieData(MTYPE_URL, hContact, dwUin, TRUE);
// Format the body
int nUrlLen = strlennull(url);
char *szDesc = (char *)url + nUrlLen + 1;
int nDescLen = strlennull(szDesc);
int nBodyLen = nUrlLen + nDescLen + 2;
char *szBody = (char *)_alloca(nBodyLen);
strcpy(szBody, szDesc);
szBody[nDescLen] = (char)0xFE; // Separator
strcpy(szBody + nDescLen + 1, url);
if (m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) {
int iRes = icq_SendDirectMessage(hContact, szBody, nBodyLen, 1, pCookieData, NULL);
if (iRes)
return iRes; // we succeded, return
}
// Rate check
if (IsServerOverRate(ICQ_MSG_FAMILY, ICQ_MSG_SRV_SEND, RML_LIMIT)) { // rate is too high, the message will not go thru...
SAFE_FREE((void**)&pCookieData);
return ReportGenericSendError(hContact, ACKTYPE_URL, "The message could not be delivered. You are sending too fast. Wait a while and try again.");
}
// Select channel and send
WORD wPriority;
if (wRecipientStatus == ID_STATUS_ONLINE || wRecipientStatus == ID_STATUS_FREECHAT)
wPriority = 0x0001;
else
wPriority = 0x0021;
DWORD dwCookie = icq_SendChannel2Message(dwUin, hContact, szBody, nBodyLen, wPriority, pCookieData, NULL);
// This will stop the message dialog from waiting for the real message delivery ack
if (pCookieData->nAckType == ACKTYPE_NONE) {
SendProtoAck(hContact, dwCookie, ACKRESULT_SUCCESS, ACKTYPE_URL, NULL);
// We need to free this here since we will never see the real ack
// The actual cookie value will still have to be returned to the message dialog though
ReleaseCookie(dwCookie);
}
return dwCookie; // Success
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_SetApparentMode - sets the visibility status
int __cdecl CIcqProto::SetApparentMode(MCONTACT hContact, int mode)
{
DWORD uin;
uid_str uid;
if (getContactUid(hContact, &uin, &uid))
return 1; // Invalid contact
if (hContact) {
// Only 3 modes are supported
if (mode == 0 || mode == ID_STATUS_ONLINE || mode == ID_STATUS_OFFLINE) {
int oldMode = getWord(hContact, "ApparentMode", 0);
// Don't send redundant updates
if (mode != oldMode) {
setWord(hContact, "ApparentMode", (WORD)mode);
// Not being online is only an error when in SS mode. This is not handled
// yet so we just ignore this for now.
if (icqOnline()) {
if (oldMode != 0) { // Remove from old list
if (oldMode == ID_STATUS_OFFLINE && getWord(hContact, DBSETTING_SERVLIST_IGNORE, 0)) { // Need to remove Ignore item as well
icq_removeServerPrivacyItem(hContact, uin, uid, getWord(hContact, DBSETTING_SERVLIST_IGNORE, 0), SSI_ITEM_IGNORE);
setWord(hContact, DBSETTING_SERVLIST_IGNORE, 0);
}
icq_sendChangeVisInvis(hContact, uin, uid, oldMode == ID_STATUS_OFFLINE, 0);
}
if (mode != 0) { // Add to new list
if (mode == ID_STATUS_OFFLINE && getWord(hContact, DBSETTING_SERVLIST_IGNORE, 0))
return 0; // Success: offline by ignore item
icq_sendChangeVisInvis(hContact, uin, uid, mode == ID_STATUS_OFFLINE, 1);
}
}
return 0; // Success
}
}
}
return 1; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PrepareStatusNote - returns correct status note for given status
char* CIcqProto::PrepareStatusNote(int nStatus)
{
char *szStatusNote = NULL;
BYTE bXStatus = getContactXStatus(NULL);
// use custom status message as status note
if (bXStatus)
szStatusNote = getSettingStringUtf(NULL, DBSETTING_XSTATUS_MSG, "");
if (!szStatusNote || !szStatusNote[0]) { // get standard status message (no custom status defined)
icq_lock l(m_modeMsgsMutex);
char **pszStatusNote = MirandaStatusToAwayMsg(nStatus);
if (pszStatusNote)
szStatusNote = null_strdup(*pszStatusNote);
}
if (!szStatusNote)
// nothing available set empty status note
szStatusNote = null_strdup("");
return szStatusNote;
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_SetStatus - sets the protocol status
int __cdecl CIcqProto::SetStatus(int iNewStatus)
{
int nNewStatus = MirandaStatusToSupported(iNewStatus);
// check if netlib handles are ready
if (!m_hNetlibUser)
return 0;
if (m_bTempVisListEnabled && icqOnline()) // remove temporary visible users
sendEntireListServ(ICQ_BOS_FAMILY, ICQ_CLI_REMOVETEMPVISIBLE, BUL_TEMPVISIBLE);
if (nNewStatus == m_iStatus)
return 0;
// clear custom status on status change
if (getByte("XStatusReset", DEFAULT_XSTATUS_RESET))
setXStatusEx(0, 0);
// New status is OFFLINE
if (nNewStatus == ID_STATUS_OFFLINE) { // for quick logoff
if (icqOnline()) { // set offline status note (otherwise the old will remain)
char *szOfflineNote = PrepareStatusNote(nNewStatus);
SetStatusNote(szOfflineNote, 0, FALSE);
SAFE_FREE(&szOfflineNote);
}
m_iDesiredStatus = nNewStatus;
if (hServerConn) { // Connected, Send disconnect packet
icq_sendCloseConnection();
icq_serverDisconnect();
m_bConnectionLost = false;
debugLogA("Logged off.");
}
}
else {
switch (m_iStatus) {
// We are offline and need to connect
case ID_STATUS_OFFLINE:
// Update user connection settings
UpdateGlobalSettings();
// Read UIN from database
m_dwLocalUIN = getContactUin(NULL);
if (m_dwLocalUIN == 0) {
SetCurrentStatus(ID_STATUS_OFFLINE);
ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGINERR_BADUSERID);
icq_LogMessage(LOG_FATAL, LPGEN("You have not entered an ICQ number.\nConfigure this in Options->Network->ICQ and try again."));
return 0;
}
// Set status to 'Connecting'
m_iDesiredStatus = nNewStatus;
SetCurrentStatus(ID_STATUS_CONNECTING);
// Read password from database
{
char *pszPwd = GetUserPassword(FALSE);
if (pszPwd)
icq_login(pszPwd);
else
RequestPassword();
}
break;
// We are connecting... We only need to change the going online status
case ID_STATUS_CONNECTING:
m_iDesiredStatus = nNewStatus;
break;
// We are already connected so we should just change status
default:
SetCurrentStatus(nNewStatus);
char *szStatusNote = PrepareStatusNote(nNewStatus);
//! This is a bit tricky, we do trigger status note change thread and then
// change the status note right away (this spares one packet) - so SetStatusNote()
// will only change User Details Directory
SetStatusNote(szStatusNote, 6000, FALSE);
if (m_iStatus == ID_STATUS_INVISIBLE) {
if (m_bSsiEnabled)
updateServVisibilityCode(3);
icq_setstatus(MirandaStatusToIcq(m_iStatus), szStatusNote);
}
else {
icq_setstatus(MirandaStatusToIcq(m_iStatus), szStatusNote);
if (m_bSsiEnabled)
updateServVisibilityCode(4);
}
SAFE_FREE(&szStatusNote);
if (m_bAimEnabled) {
icq_lock l(m_modeMsgsMutex);
char ** pszStatusNote = MirandaStatusToAwayMsg(m_iStatus);
if (pszStatusNote)
icq_sendSetAimAwayMsgServ(*pszStatusNote);
else // clear the away message
icq_sendSetAimAwayMsgServ(NULL);
}
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// GetAwayMsgThread - return a contact's status message
struct status_message_thread_data
{
MCONTACT hContact;
char *szMessage;
HANDLE hProcess;
};
void __cdecl CIcqProto::GetAwayMsgThread(void *pStatusData)
{
status_message_thread_data *pThreadData = (status_message_thread_data*)pStatusData;
if (pThreadData) {
// wait a little
Sleep(100);
setStatusMsgVar(pThreadData->hContact, pThreadData->szMessage, false);
TCHAR *tszMsg = mir_utf8decodeT(pThreadData->szMessage);
ProtoBroadcastAck(pThreadData->hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, pThreadData->hProcess, (LPARAM)tszMsg);
mir_free(tszMsg);
SAFE_FREE(&pThreadData->szMessage);
SAFE_FREE((void**)&pThreadData);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_GetAwayMsg - returns a contact's away message
HANDLE __cdecl CIcqProto::GetAwayMsg(MCONTACT hContact)
{
DWORD dwUin;
uid_str szUID;
if (getContactUid(hContact, &dwUin, &szUID))
return 0; // Invalid contact
if (!dwUin || !CheckContactCapabilities(hContact, CAPF_STATUS_MESSAGES)) { // No individual status messages, check if the contact has Status Note, if yes give it
char *szStatusNote = getSettingStringUtf(hContact, DBSETTING_STATUS_NOTE, NULL);
if (strlennull(szStatusNote) > 0) { // Give Status Note
status_message_thread_data *pThreadData = (status_message_thread_data*)SAFE_MALLOC(sizeof(status_message_thread_data));
pThreadData->hContact = hContact;
pThreadData->szMessage = szStatusNote;
pThreadData->hProcess = (HANDLE)GenerateCookie(0);
ForkThread(&CIcqProto::GetAwayMsgThread, pThreadData);
return pThreadData->hProcess;
}
SAFE_FREE(&szStatusNote);
}
if (!icqOnline())
return 0;
WORD wStatus = getContactStatus(hContact);
if (dwUin) {
int wMessageType = 0;
switch (wStatus) {
case ID_STATUS_ONLINE:
if (CheckContactCapabilities(hContact, CAPF_STATUS_MESSAGES))
wMessageType = MTYPE_AUTOONLINE;
break;
case ID_STATUS_AWAY:
wMessageType = MTYPE_AUTOAWAY;
break;
case ID_STATUS_NA:
wMessageType = MTYPE_AUTONA;
break;
case ID_STATUS_OCCUPIED:
wMessageType = MTYPE_AUTOBUSY;
break;
case ID_STATUS_DND:
wMessageType = MTYPE_AUTODND;
break;
case ID_STATUS_FREECHAT:
wMessageType = MTYPE_AUTOFFC;
break;
}
if (wMessageType) {
if (m_bDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0)) {
int iRes = icq_sendGetAwayMsgDirect(hContact, wMessageType);
if (iRes) return (HANDLE)iRes; // we succeded, return
}
WORD wVer = getWord(hContact, "Version", 0) <= 8 ? 8 : ICQ_VERSION;
if (CheckContactCapabilities(hContact, CAPF_STATUS_MESSAGES))
return (HANDLE)icq_sendGetAwayMsgServExt(hContact, dwUin, szUID, wMessageType, wVer);
return (HANDLE)icq_sendGetAwayMsgServ(hContact, dwUin, wMessageType, wVer);
}
}
else { // AIM contact
if (wStatus == ID_STATUS_AWAY)
return (HANDLE)icq_sendGetAimAwayMsgServ(hContact, szUID, MTYPE_AUTOAWAY);
}
return 0; // Failure
}
////////////////////////////////////////////////////////////////////////////////////////
// PSR_AWAYMSG - processes received status mode message
int __cdecl CIcqProto::RecvAwayMsg(MCONTACT hContact, int statusMode, PROTORECVEVENT* evt)
{
if (evt->flags & PREF_UTF) {
setStatusMsgVar(hContact, evt->szMessage, false);
TCHAR* pszMsg = mir_utf8decodeT(evt->szMessage);
ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)pszMsg);
mir_free(pszMsg);
}
else {
setStatusMsgVar(hContact, evt->szMessage, true);
ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)(TCHAR*)_A2T(evt->szMessage));
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
// PS_SetAwayMsg - sets the away status message
int __cdecl CIcqProto::SetAwayMsg(int status, const TCHAR* msg)
{
icq_lock l(m_modeMsgsMutex);
char **ppszMsg = MirandaStatusToAwayMsg(MirandaStatusToSupported(status));
if (!ppszMsg)
return 1; // Failure
// Prepare UTF-8 status message
char *szNewUtf = tchar_to_utf8(msg);
if (strcmpnull(szNewUtf, *ppszMsg)) {
// Free old message
SAFE_FREE(ppszMsg);
// Set new message
*ppszMsg = szNewUtf;
szNewUtf = NULL;
if ((m_iStatus == status) && icqOnline()) { // update current status note
char *szNote = *ppszMsg ? *ppszMsg : "";
BYTE bXStatus = getContactXStatus(NULL);
if (!bXStatus)
SetStatusNote(szNote, 1000, FALSE);
if (m_bAimEnabled)
icq_sendSetAimAwayMsgServ(*ppszMsg);
}
}
SAFE_FREE(&szNewUtf);
return 0; // Success
}
/////////////////////////////////////////////////////////////////////////////////////////
// GetMyAwayMsg - obtain the current away message
INT_PTR CIcqProto::GetMyAwayMsg(WPARAM wParam, LPARAM lParam)
{
icq_lock l(m_modeMsgsMutex);
char **ppszMsg = MirandaStatusToAwayMsg(wParam ? wParam : m_iStatus);
if (!ppszMsg || !*ppszMsg)
return 0;
int nMsgLen = strlennull(*ppszMsg) + 1;
if (lParam & SGMA_UNICODE) {
WCHAR *szMsg = (WCHAR*)_alloca(nMsgLen * sizeof(WCHAR));
make_unicode_string_static(*ppszMsg, szMsg, nMsgLen);
return (INT_PTR)mir_wstrdup(szMsg);
}
else { // convert to ansi
char *szMsg = (char*)_alloca(nMsgLen);
if (utf8_decode_static(*ppszMsg, szMsg, nMsgLen))
return (INT_PTR)mir_strdup(szMsg);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// PS_UserIsTyping - sends a UTN notification
int __cdecl CIcqProto::UserIsTyping(MCONTACT hContact, int type)
{
if (hContact && icqOnline()) {
if (CheckContactCapabilities(hContact, CAPF_TYPING)) {
switch (type) {
case PROTOTYPE_SELFTYPING_ON:
sendTypingNotification(hContact, MTN_BEGUN);
return 0;
case PROTOTYPE_SELFTYPING_OFF:
sendTypingNotification(hContact, MTN_FINISHED);
return 0;
}
}
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////
// OnEvent - maintain protocol events
int __cdecl CIcqProto::OnEvent(PROTOEVENTTYPE eventType, WPARAM wParam, LPARAM lParam)
{
switch (eventType) {
case EV_PROTO_ONLOAD:
return OnModulesLoaded(0, 0);
case EV_PROTO_ONEXIT:
return OnPreShutdown(0, 0);
case EV_PROTO_ONOPTIONS:
return OnOptionsInit(wParam, lParam);
case EV_PROTO_ONERASE:
{
char szDbSetting[MAX_PATH];
mir_snprintf(szDbSetting, sizeof(szDbSetting), "%sP2P", m_szModuleName);
CallService(MS_DB_MODULE_DELETE, 0, (LPARAM)szDbSetting);
mir_snprintf(szDbSetting, sizeof(szDbSetting), "%sSrvGroups", m_szModuleName);
CallService(MS_DB_MODULE_DELETE, 0, (LPARAM)szDbSetting);
mir_snprintf(szDbSetting, sizeof(szDbSetting), "%sGroups", m_szModuleName);
CallService(MS_DB_MODULE_DELETE, 0, (LPARAM)szDbSetting);
}
break;
case EV_PROTO_ONCONTACTDELETED:
return ServListDbContactDeleted(wParam, lParam);
case EV_PROTO_DBSETTINGSCHANGED:
return ServListDbSettingChanged(wParam, lParam);
}
return 1;
}
|