summaryrefslogtreecommitdiff
path: root/protocols/IcqOscarJ/src/icq_proto.cpp
blob: e00ed256f2e9832f4c3b500f6fcd07749d340c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
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
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
// ---------------------------------------------------------------------------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
//
// 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 ) :
cookies(10, CompareCookies),
directConns(10, CompareConns),
expectedFileRecvs(10, CompareFT),
contactsCache(10, CompareContactsCache),
cheekySearchId( -1 )
{
	m_iVersion = 2;
	m_iStatus = ID_STATUS_OFFLINE;
	m_tszUserName = mir_tstrdup( aUserName );
	m_szModuleName = mir_strdup( aProtoName );
	m_szProtoName = mir_strdup( aProtoName );
	_strlwr( m_szProtoName );
	m_szProtoName[0] = toupper( m_szProtoName[0] );
	NetLog_Server( "Setting protocol/module name to '%s/%s'", m_szProtoName, 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
	CreateResidentSetting("Status"); // NOTE: XStatus cannot be temporary
	CreateResidentSetting("TemporaryVisible");
	CreateResidentSetting("TickTS");
	CreateResidentSetting("IdleTS");
	CreateResidentSetting("AwayTS");
	CreateResidentSetting("LogonTS");
	CreateResidentSetting("DCStatus");
	CreateResidentSetting("CapBuf"); //capabilities bufer
	CreateResidentSetting(DBSETTING_STATUS_NOTE_TIME);
	CreateResidentSetting(DBSETTING_STATUS_MOOD);

	// Setup services
	CreateProtoService(PS_CREATEACCMGRUI, &CIcqProto::OnCreateAccMgrUI );
	CreateProtoService(MS_ICQ_SENDSMS, &CIcqProto::SendSms);
	CreateProtoService(PS_SET_NICKNAME, &CIcqProto::SetNickName);

	CreateProtoService(PS_GETMYAWAYMSG, &CIcqProto::GetMyAwayMsg);

	CreateProtoService(PS_GETINFOSETTING, &CIcqProto::GetInfoSetting);

	CreateProtoService(PSS_ADDED, &CIcqProto::SendYouWereAdded);
	// Session password API
	CreateProtoService(PS_ICQ_SETPASSWORD, &CIcqProto::SetPassword);
	// 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);

	m_hIconProtocol = (HANDLE)CallService(MS_SKIN2_ISMANAGEDICON, (WPARAM)LoadSkinnedProtoIcon(m_szModuleName, ID_STATUS_ONLINE), 0);

	// 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];
	null_snprintf(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_hServerNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);

	char szP2PModuleName[MAX_PATH];
	null_snprintf(szP2PModuleName, SIZEOF(szP2PModuleName), "%sP2P", m_szModuleName);
	null_snprintf(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 = {0};
	eventType.cbSize = DBEVENTTYPEDESCR_SIZE;
	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
	NetLog_Server("%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_hServerNetlibUser);

	// Destroy hookable events
	if (m_modeMsgsEvent)
		DestroyHookableEvent(m_modeMsgsEvent);

	// Clean-up remaining protocol instance members
	cookies.destroy();

	UninitContactsCache();

	CustomCapList.clear();

	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);

	// Remove account icons
	Skin_RemoveIconHandle(m_hIconProtocol);

	NetLog_Server("%s: Protocol instance '%s' destroyed.", ICQ_PROTOCOL_NAME, m_szModuleName);

	mir_free( m_szProtoName );
	mir_free( m_szModuleName );
	mir_free( m_tszUserName );
}


////////////////////////////////////////////////////////////////////////////////////////
// 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};

	null_snprintf(pszP2PName, SIZEOF(pszP2PName), "%sP2P", m_szModuleName);
	null_snprintf(pszGroupsName, SIZEOF(pszGroupsName), "%sGroups", m_szModuleName);
	null_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);

	InitAvatars();

	// Init extra optional modules
	ModuleLoad(0, 0);
	InitXStatusItems(FALSE);

	HANDLE hContact = FindFirstContact();
	while (hContact != NULL)
	{
		DWORD bXStatus = getContactXStatus(hContact);
		if (bXStatus > 0)
			setContactExtraIcon(hContact, bXStatus);

		hContact = FindNextContact(hContact);
	}

	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

HANDLE CIcqProto::AddToList( int flags, PROTOSEARCHRESULT* psr )
{
	if (psr)
	{
		if (psr->cbSize == sizeof(ICQSEARCHRESULT))
		{
			ICQSEARCHRESULT *isr = (ICQSEARCHRESULT*)psr;
			if (isr->uin)
				return AddToListByUIN(isr->uin, flags);
			else
			{ // aim contact
				char szUid[MAX_PATH];

				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);

				if (szUid[0] == 0) return 0;
				return AddToListByUID(szUid, flags);
			}
		}
		else
		{
			char szUid[MAX_PATH];

			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);
			else
				return AddToListByUID(szUid, flags);
		}
	}

	return 0; // Failure
}

HANDLE __cdecl CIcqProto::AddToListByEvent( int flags, int iContact, HANDLE hDbEvent )
{
	DWORD uin = 0;
	uid_str uid = {0};

	DBEVENTINFO dbei = {0};
	dbei.cbSize = sizeof(dbei);
	if ((dbei.cbBlob = CallService(MS_DB_EVENT_GETBLOBSIZE, (WPARAM)hDbEvent, 0)) == -1)
		return 0;

	dbei.pBlob = (PBYTE)_alloca(dbei.cbBlob + 1);
	dbei.pBlob[dbei.cbBlob] = '\0';

	if (CallService(MS_DB_EVENT_GET, (WPARAM)hDbEvent, (LPARAM)&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))
			return 0;

	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)
	{
		HANDLE hContact = HContactFromAuthEvent( hDbEvent );
		if (hContact == INVALID_HANDLE_VALUE)
			return 1;

		DWORD uin;
		uid_str uid;
		if (getContactUid(hContact, &uin, &uid))
			return 1;

		icq_sendAuthResponseServ(uin, uid, 1, _T(""));

		deleteSetting(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)
	{
		HANDLE hContact = HContactFromAuthEvent(hDbEvent);
		if (hContact == INVALID_HANDLE_VALUE)
			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, (WPARAM)hContact, 0);

		return 0; // Success
	}

	return 1; // Failure
}


////////////////////////////////////////////////////////////////////////////////////////
// PSR_AUTH

int __cdecl CIcqProto::AuthRecv( HANDLE 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( HANDLE 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
}


////////////////////////////////////////////////////////////////////////////////////////
// ChangeInfo

HANDLE __cdecl CIcqProto::ChangeInfo( int iInfoType, void* pInfoData )
{
	return NULL;
}


////////////////////////////////////////////////////////////////////////////////////////
// PS_FileAllow - starts a file transfer

HANDLE __cdecl CIcqProto::FileAllow( HANDLE 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( HANDLE 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( HANDLE 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, HANDLE 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 && getSettingByte(NULL, "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 = getSettingByte(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;
}

////////////////////////////////////////////////////////////////////////////////////////
// GetIcon - loads an icon for the contact list

HICON __cdecl CIcqProto::GetIcon( int iconIndex )
{
	if (LOWORD(iconIndex) == PLI_PROTOCOL)
	{
		if (iconIndex & PLIF_ICOLIBHANDLE)
			return (HICON)m_hIconProtocol;

		bool big = (iconIndex & PLIF_SMALL) == 0;
		HICON hIcon = Skin_GetIconByHandle(m_hIconProtocol, big);

		if (iconIndex & PLIF_ICOLIB)
			return hIcon;

		HICON hIconNew = CopyIcon(hIcon);
		Skin_ReleaseIcon(hIcon);
		return hIconNew;
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
// GetInfo - retrieves a contact info

int __cdecl CIcqProto::GetInfo(HANDLE 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;

	BroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)cheekySearchId, (LPARAM)&isr);
	BroadcastAck(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)
	{
		DWORD dwSearchId, dwSecId;
		char *szEmail = tchar_to_ansi(email);

		// Success
		dwSearchId = SearchByMail(szEmail);
		if (m_bAimEnabled)
			dwSecId = icq_searchAimByEmail(szEmail, dwSearchId);
		else
			dwSecId = 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( HANDLE 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( HANDLE hContact, PROTORECVFILET* evt )
{
	return Proto_RecvFile(hContact, evt);
}


////////////////////////////////////////////////////////////////////////////////////////
// RecvMsg

int __cdecl CIcqProto::RecvMsg( HANDLE 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, (WPARAM)hContact, PROTOTYPE_CONTACTTYPING_OFF);

	return 0;
}


////////////////////////////////////////////////////////////////////////////////////////
// RecvUrl

int __cdecl CIcqProto::RecvUrl( HANDLE hContact, PROTORECVEVENT* )
{
	return 1;
}


////////////////////////////////////////////////////////////////////////////////////////
// SendContacts

int __cdecl CIcqProto::SendContacts( HANDLE hContact, int flags, int nContacts, HANDLE* 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)
					{
						icq_packet mData, mNames;

#ifdef _DEBUG
						NetLog_Server("Sending contacts to %s.", strUID(dwUin, szUid));
#endif
						// 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
						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)
					{
						char* pBody;
						char* pBuffer;

#ifdef _DEBUG
						NetLog_Server("Sending contacts to %d.", dwUin);
#endif
						// 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
						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( HANDLE 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 = getSettingWord(hContact, "Version", 7);

				if (wClientVersion < 7)
					NetLog_Server("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))
							NetLog_Server("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
					{
						char szFiles[64], tmp[64];
						char *pszFiles;


						NetLog_Server("Init file send");

						if (ft->dwFileCount == 1)
						{
							pszFiles = strchr(ft->pszFiles[0], '\\');
							if (pszFiles)
								pszFiles++;
							else
								pszFiles = ft->pszFiles[0];
						}
						else
						{
							null_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
								}
								NetLog_Server("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
								}
								NetLog_Server("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( HANDLE hContact, int flags, const char* pszSrc )
{
	if (hContact && pszSrc)
	{
		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) ||
			!getSettingByte(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
		{
#ifdef _DEBUG
			NetLog_Server("Send %smessage - Message cap is %u", puszText ? "unicode " : "", CheckContactCapabilities(hContact, CAPF_SRV_RELAY));
			NetLog_Server("Send %smessage - Contact status is %u", puszText ? "unicode " : "", wRecipientStatus);
#endif
			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
			}

			if (!dwUin || !CheckContactCapabilities(hContact, CAPF_SRV_RELAY) ||
				wRecipientStatus == ID_STATUS_OFFLINE || wRecipientStatus == ID_STATUS_INVISIBLE ||
				getSettingByte(hContact, "OnlyServerAcks", getSettingByte(NULL, "OnlyServerAcks", DEFAULT_ONLYSERVERACKS)) ||
				!getSettingByte(hContact, "SlowSend", getSettingByte(NULL, "SlowSend", DEFAULT_SLOWSEND)))
			{
				/// 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);
			}
			else
			{
				WORD wPriority;

				char *srv_msg = puszText;
				char *srv_cap = plain_ascii ? NULL : CAP_UTF8MSGS;
				char *szUserAnsi = NULL;

				if (!plain_ascii && oldAnsi)
				{
					szUserAnsi = ConvertMsgToUserSpecificAnsi(hContact, puszText);
					if (szUserAnsi)
					{
						srv_msg = szUserAnsi;
						srv_cap = NULL;
					}
				}

				if (wRecipientStatus == ID_STATUS_ONLINE || wRecipientStatus == ID_STATUS_FREECHAT)
					wPriority = 0x0001;
				else
					wPriority = 0x0021;

				if (strlennull(srv_msg) + (!oldAnsi ? 144 : 102) > MAX_MESSAGESNACSIZE)
				{ // max length check
					dwCookie = ReportGenericSendError(hContact, ACKTYPE_MESSAGE, "The message could not be delivered, it is too long.");

					SAFE_FREE(&szUserAnsi);
					// free the buffers if alloced
					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.");

					SAFE_FREE(&szUserAnsi);
					// free the buffers if alloced
					if (bNeedFreeU) SAFE_FREE(&puszText);

					return dwCookie;
				}

				pCookieData = CreateMessageCookieData(MTYPE_PLAIN, hContact, dwUin, TRUE);
				dwCookie = icq_SendChannel2Message(dwUin, hContact, srv_msg, strlennull(srv_msg), wPriority, pCookieData, srv_cap);
				SAFE_FREE(&szUserAnsi);
			}

			// 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
	}

	return 0; // Failure
}


////////////////////////////////////////////////////////////////////////////////////////
// SendUrl

int __cdecl CIcqProto::SendUrl( HANDLE hContact, int flags, const char* url )
{
	if (hContact && url)
	{
		DWORD dwCookie;
		WORD wRecipientStatus;
		DWORD dwUin;

		if (getContactUid(hContact, &dwUin, NULL))
		{ // Invalid contact
			return ReportGenericSendError(hContact, ACKTYPE_URL, "The receiver has an invalid user ID.");
		}

		wRecipientStatus = getContactStatus(hContact);

		// Failure
		if (!icqOnline())
		{
			dwCookie = ReportGenericSendError(hContact, ACKTYPE_URL, "You cannot send messages when you are offline.");
		}
		// Looks OK
		else
		{
			char* szDesc;
			char* szBody;
			int nBodyLen;
			int nDescLen;
			int nUrlLen;


			// Set up the ack type
			cookie_message_data *pCookieData = CreateMessageCookieData(MTYPE_URL, hContact, dwUin, TRUE);

			// Format the body
			nUrlLen = strlennull(url);
			szDesc = (char *)url + nUrlLen + 1;
			nDescLen = strlennull(szDesc);
			nBodyLen = nUrlLen + nDescLen + 2;
			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
/*
			if (!CheckContactCapabilities(hContact, CAPF_SRV_RELAY) ||
				wRecipientStatus == ID_STATUS_OFFLINE)
			{
				dwCookie = icq_SendChannel4Message(dwUin, hContact, MTYPE_URL,
					(WORD)nBodyLen, szBody, pCookieData);
			}
			else
*/
			{
				WORD wPriority;

				if (wRecipientStatus == ID_STATUS_ONLINE || wRecipientStatus == ID_STATUS_FREECHAT)
					wPriority = 0x0001;
				else
					wPriority = 0x0021;

				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
	}

	return 0; // Failure
}

////////////////////////////////////////////////////////////////////////////////////////
// PS_SetApparentMode - sets the visibility status

int __cdecl CIcqProto::SetApparentMode( HANDLE 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 = getSettingWord(hContact, "ApparentMode", 0);

			// Don't send redundant updates
			if (mode != oldMode)
			{
				setSettingWord(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 && getSettingWord(hContact, DBSETTING_SERVLIST_IGNORE, 0))
						{ // Need to remove Ignore item as well
							icq_removeServerPrivacyItem(hContact, uin, uid, getSettingWord(hContact, DBSETTING_SERVLIST_IGNORE, 0), SSI_ITEM_IGNORE);

							setSettingWord(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 && getSettingWord(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_hServerNetlibUser)
		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 (getSettingByte(NULL, "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);

			// Create unnamed event to wait until the status note change process is completed
			m_hNotifyNameInfoEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

			int bNoteChanged = SetStatusNote(szOfflineNote, 0, FALSE);

			SAFE_FREE(&szOfflineNote);

			// Note was changed, wait until the process is over
			if (bNoteChanged)
				ICQWaitForSingleObject(m_hNotifyNameInfoEvent, 4000, TRUE);

			// Release the event
			CloseHandle(m_hNotifyNameInfoEvent);
			m_hNotifyNameInfoEvent = NULL;
		}

		m_iDesiredStatus = nNewStatus;

		if (hServerConn)
		{ // Connected, Send disconnect packet
			icq_sendCloseConnection();

			icq_serverDisconnect(FALSE);

			SetCurrentStatus(ID_STATUS_OFFLINE);

			NetLog_Server("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);
					BroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGINERR_BADUSERID);
					icq_LogMessage(LOG_FATAL, LPGEN("You have not entered a 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
{
	HANDLE 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);
		BroadcastAck(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( HANDLE 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;

		default:
			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
			}
			if (CheckContactCapabilities(hContact, CAPF_STATUS_MESSAGES))
				return (HANDLE)icq_sendGetAwayMsgServExt(hContact, dwUin, szUID, wMessageType,
				(WORD)(getSettingWord(hContact, "Version", 0)==9?9:ICQ_VERSION)); // Success
			else
				return (HANDLE)icq_sendGetAwayMsgServ(hContact, dwUin, wMessageType,
				(WORD)(getSettingWord(hContact, "Version", 0)==9?9:ICQ_VERSION)); // Success
		}
	}
	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( HANDLE hContact, int statusMode, PROTORECVEVENT* evt )
{
	if (evt->flags & PREF_UTF) {
		setStatusMsgVar(hContact, evt->szMessage, false);

		TCHAR* pszMsg = mir_utf8decodeT(evt->szMessage);
		BroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)pszMsg);
		mir_free(pszMsg);
	}
	else {
		setStatusMsgVar(hContact, evt->szMessage, true);
		BroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)(TCHAR*)_A2T(evt->szMessage));
	}
	return 0;
}


////////////////////////////////////////////////////////////////////////////////////////
// PSS_AWAYMSG - send status mode message (individual mode)

int __cdecl CIcqProto::SendAwayMsg( HANDLE hContact, HANDLE hProcess, const char* msg )
{
	return 1;
}


////////////////////////////////////////////////////////////////////////////////////////
// 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( HANDLE 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];

			null_snprintf(szDbSetting, sizeof(szDbSetting), "%sP2P", m_szModuleName);
			CallService(MS_DB_MODULE_DELETE, 0, (LPARAM)szDbSetting);
			null_snprintf(szDbSetting, sizeof(szDbSetting), "%sSrvGroups", m_szModuleName);
			CallService(MS_DB_MODULE_DELETE, 0, (LPARAM)szDbSetting);
			null_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;
}