summaryrefslogtreecommitdiffstats
path: root/src/modules/dcc/libkvidcc.cpp
blob: de818ce415941f63e4271cb408114c9da56e3751 (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
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
//=============================================================================
//
//   File : libkviobjects.cpp
//   Creation date : Wed Sep 09 2000 20:59:01 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2005 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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.
//
//=============================================================================

#include "kvi_debug.h"
#include "kvi_settings.h"
#include "kvi_string.h"
#include "kvi_module.h"
#include "kvi_sparser.h"
#include "kvi_locale.h"
#include "kvi_out.h"
#include "kvi_console.h"
#include "kvi_netutils.h"
#include "kvi_frame.h"
#include "kvi_console.h"
#include "kvi_error.h"
#include "kvi_options.h"
#include "kvi_defaults.h"
#include "kvi_mirccntrl.h"
#include "kvi_app.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"

#include "gsmcodec.h"
#include "broker.h"
#include "voice.h"
#include "utils.h"
#include "send.h"
#include "window.h"

#include <tqfileinfo.h>

#ifdef COMPILE_ON_WINDOWS
	// Ugly Windoze compiler...
	#include "dialogs.h"
#endif

//#warning "KviOption_boolIgnoreDccChat and other types too"

//extern KVIRC_API KviSharedFilesManager * g_pSharedFilesManager;

KviDccBroker * g_pDccBroker = 0;


static void dcc_module_set_dcc_type(KviDccDescriptor * d,const char * szBaseType)
{
	d->szType = szBaseType;
#ifdef COMPILE_SSL_SUPPORT
	if(d->bIsSSL)d->szType.prepend('S');
#endif
	if(d->bIsTdcc)d->szType.prepend('T');
}

static bool dcc_kvs_parse_default_parameters(KviDccDescriptor * d,KviKvsModuleCommandCall *c)
{
	d->bIsTdcc = c->switches()->find('t',"tdcc");

	KviKvsVariant * pSw = c->switches()->find('m',"minimize");

	if(pSw != 0)
		d->bOverrideMinimize = pSw->asBoolean();
	else
		d->bOverrideMinimize = false;

	if(!d->console())
	{
		// We don't need a console with -c and -n , otherwise we need it
		if(!(c->switches()->find('c',"connect") || c->switches()->find('n',"no-ctcp")))
		{
			delete d;
			c->error(__tr2qs_ctx("This window has no associated IRC context (an IRC context is required unless -c or -n are passed)","dcc"));
			return false;
		} else d->setConsole(c->window()->frame()->firstConsole());
	}

	__range_valid(d->console());

	if(!d->console()->isConnected())
	{
		// We don't need a connection with -c and -n , otherwise we need it
		if(!(c->switches()->find('c',"connect") || c->switches()->find('n',"no-ctcp")))
		{
			delete d;
			c->error(__tr2qs_ctx("You're not connected to a server (an active connection is required unless -c or -n are passed)","dcc"));
			return false;
		} else {
			// -c or -n , grab a local nick from somewhere
			d->szLocalNick  = KVI_OPTION_STRING(KviOption_stringNickname1);
			d->szLocalNick.stripWhiteSpace();
			if(d->szLocalNick.isEmpty())d->szLocalNick = KVI_DEFAULT_NICKNAME1;
			d->szLocalUser  = __tr2qs_ctx("unknown","dcc"); // we can live without it
			d->szLocalHost  = d->szLocalUser;  // we can live without it
		}
	} else {
		// We know everything
		d->szLocalNick  = d->console()->connection()->userInfo()->nickName();
		d->szLocalUser  = d->console()->connection()->userInfo()->userName();
		d->szLocalHost  = d->console()->connection()->userInfo()->hostName();
	}

	if(pSw = c->switches()->find('i',"ip"))
	{
		pSw->asString(d->szListenIp);
		if(!(d->szListenIp.contains('.') || d->szListenIp.contains(':')))
		{
			// This will magically work with the same buffer as source and dst!
			if(!KviNetUtils::getInterfaceAddress(d->szListenIp,d->szListenIp))
			{
				c->error(__tr2qs_ctx("Unable to get address of interface %Q","dcc"),&(d->szListenIp));
				delete d;
				return false;
			}
		}
	} else {
		TQString tmp;
		if(!dcc_kvs_get_listen_ip_address(c,d->console(),tmp))
		{
			delete d;
			c->error(__tr2qs_ctx("No suitable interfaces to listen on, use -i","dcc"));
			return false;
		}
		d->szListenIp=tmp;
	}

	if(pSw = c->switches()->find('p',"port"))
	{
		pSw->asString(d->szListenPort); // fixme!
	}
	else d->szListenPort = "0"; // any port is ok

	if(pSw = c->switches()->find('a',"fake-address"))
	{
		pSw->asString(d->szFakeIp);
	}
	else {
		if(KVI_OPTION_BOOL(KviOption_boolDccSendFakeAddressByDefault))
		{
			d->szFakeIp = KVI_OPTION_STRING(KviOption_stringDefaultDccFakeAddress);
			if(d->szFakeIp.isEmpty())KVI_OPTION_BOOL(KviOption_boolDccSendFakeAddressByDefault) = false;
		}
	}

	if(pSw = c->switches()->find('f',"fake-port"))
	{
		pSw->asString(d->szFakePort);
	}

	d->bDoTimeout = (!c->switches()->find('u',"unlimited"));
#ifdef COMPILE_SSL_SUPPORT
	d->bIsSSL = c->switches()->find('s',"ssl");
#else
	if(c->switches()->find('s',"ssl"))c->warning(__tr2qs_ctx("This executable was built without SSL support, -s switch ignored","dcc"));
#endif

	return true;
}


/*
	@doc: dcc.chat
	@type:
		command
	@title:
		dcc.chat
	@short:
		Starts a DCC Chat connection
	@syntax:
		dcc.chat [-s] [-n] [-c] [-u] [-m[=<boolean>]] [-i=<interface>] [-p=<port>] [-a=<fake address>] [-f=<fake port>] <nickname>
	@switches:
		!sw: -m[=<boolean>] | --minimize[=<boolean>]
		If the -m switch is passed, the default boolCreateMinimizedDccChat option
		is overridden with the <boolean> parameter passed. So actually
		by passing -m=1 will create a minimized DCC send even
		if the [fnc]$option[/fnc](boolCreateMinimizedDccChat) returns false.[br]
		In the same way, by passing -m=0 you will create a non minimized DCC send.
		If no <boolean> value is specified, it defaults to 1.[br]

		!sw: -n | --no-ctcp
		Do NOT send the CTCP request to the target user, you will have to do it manually,
		or the remote user will have to connect manually (for example by using dcc.chat -c).[br]
		
		!sw: -c | --connect
		Attempt to CONNECT to the remote host specified as <interface> and <port>,
		instead of listening (active connection instead of a passive one).
		In this case the -i and -p switches are mandatory.[br]
		The 'c' switch takes precedence over 'n' (In fact both should
		be mutually exclusive).[br]
		If the 'c' and 'n' switches are missing, this commands
		needs to be executed in a window that is bound to a connected
		IRC context (you need a third entity to accomplish the negotiation).[br]

		!sw: -i=<interface> | --ip=<interface>
		Bind the local listening socket to the specified <interface> (which is an IP address, IPv4 or IPv6).
		If this switch is NOT specified, the socket is bound to the interface of
		the current IRC connection (if any) or to "127.0.0.1".[br]
		You can also specify a local interface name to get the address from (this works only for IPv4 interfaces
		since IPv6 ones seems to be unsupported by the system ioctl() calls at the moment (for linux at least)).[br]
		Here go some examples:[br]
		-i=215.243.12.12: this will bind to the IPv4 interface with the specified address.[br]
		-i=3ffe:1001::1: this will bind to the IPv6 interface with the specified address.[br]
		-i=ppp0: this will bind to the IPv4 address of the interface ppp0 (if supported by the underlying system).[br]
		The -i switch parameter may serve also as a target address when the -c switch is used.[br]
		
		!sw: -p=<port> | --port=<port>
		Bind the local listening socket to the specified <port>.
		If this switch is NOT specified, the port will be a "random" one choosen by the kernel.[br]

		!sw: -a=<fake address> | --fake-address=<fake address>
		Send the <fake address> as target for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real IP address of the listening
		interface.[br]
		
		!sw: -f=<fake port> | --fake-port=<fake port>
		Send the <fake port> as target port for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real port of the listening socket.
		[br][br]
		All these switches are meant to allow maximum flexibility of the
		DCC negotiation, earlier KVIrc releases had serious problems
		with firewalled and/or masqueraded machines. With the -a and -f switches
		you can work around it.[br]
		[br]

		!sw: -u | --unlimited
		If the 'u' switch is given, the connection attempt will
		never time out; this might be useful if you want to leave
		a listening socket for a friend of yours while you are sleeping
		and have the CTCP processing disabled. The 'u' switch works either
		in active and passive mode.[br]

		!sw: -s | --ssl
		Use a Secure Socket Layer for the transfer; the whole communication will be encrypted
		with a private key algorithm after a public key handshake.[br]
		This option will work only if the KVIrc executable has been compiled with SSL support
		and the remote end supports the SSL protocol too.[br]
		Please note that this will may down the transfer somewhat.[br]
		-s can be combined with -t.[br]
		The CTCP negotiation will use SSEND as parameter (or eventually TSSEND).[br]
		When requesting a SSL based DCC send to someone you probably will need a
		certificate. If you don't have one, create it (for example with CA.pl -newcert)
		and set it in the options dialog.

		!sw: -z | --zero-port
		Use the 0 port method. This is a dirty hack that allows you to use the CHAT
		protocol with mIrc receiving clients.
	@description:
		Attempts a DCC connection to <nickname>.[br]
		The simplest case "dcc.chat <nickname>" will work just as in all
		the other IRC clients, but this command is really more powerful...[br]
		Before attempting to understand the possibilities of this command,
		be sure to know how [doc:dcc_connection]DCC negotiation and connections[/doc] work.
		If the 'i' switch is specified, the local listening socket
		will be bound to the specified <interface> (which is an IP address, IPv4 or IPv6),
		otherwise it will be bound to the interface of the
		current IRC connection.[br]
		You can also specify a local interface name to get the address from (this works only for IPv4 interfaces
		since IPv6 ones seem to be unsupported by the system ioctl() calls at the moment (in Linux at least)).[br]
		Here are some examples:[br]
		-i=215.243.12.12: This will bind to the IPv4 interface with the specified address.[br]
		-i=3ffe:1001::1: This will bind to the IPv6 interface with the specified address.[br]
		-i=ppp0: This will bind to the IPv4 address of the interface ppp0 (if supported by the underlying system).[br]
		The -i switch parameter may serve also as a target address when the -c switch is used.[br]
		If the 'p' switch is specified, the local listening socket
		will be bound to the <port>, otherwise it will be bound to
		a random port choosen by the kernel.[br]
		If the 'a' switch is specified, the requesting CTCP message
		will contain <fake address> as target for the remote user,
		otherwise the CTCP message will contain the real IP address
		of the listening interface.
		If the 'f' switch is specified, the requesting CTCP message
		will contain <fake port> as target for the remote user,
		otherwise the CTCP message will contain the real port of the
		listening socket.
		All these switches are meant to allow maximum flexibility of the
		DCC negotiation, earlier KVIrc releases had serious problems
		with firewalled and/or masqueraded machines. With the -a and -f switches
		you can workaround it.
		If the 'n' switch is specified, KVIrc will NOT send the CTCP request
		to the target user; you will have to do it manually, or the remote user
		will have to connect manually (for example by using dcc.chat -c).
		If the 'c' switch is specified, KVIrc will attempt to connect
		to the remote host specified as <interface> and <port>, instead
		of listening (active connection instead of a passive one).
		In this case the -i and -p switches are mandatory.[br]
		The 'c' switch takes precedence over 'n' (In fact both should
		be mutually exclusive).[br]
		If the 'c' and 'n' switches are missing, this commands
		needs to be executed in a window that is bound to a connected
		IRC context (you need a third entity to accomplish the negotiation).[br]
		If the 'u' switch is given, the connection attempt will
		never time out; this might be useful if you want to leave
		a listening socket for a friend of yours while you are sleeping
		and have the CTCP processing disabled. The 'u' switch works either
		in active and passive mode.[br]
		If the -m switch is passed, the default boolCreateMinimizedDccChat option
		is overridden with the <boolean> parameter passed. So actually
		by passing -m=1 will create a minimized DCC chat even
		if the [fnc]$option[/fnc](boolCreateMinimizedDccChat) returns false.[br]
		In the same way, by passing -m=0 you will create a non minimized DCC chat.
		If no <boolean> value is specified, it defaults to 1.[br]
		-s will cause the DCC chat to be attempted in Secure Sockets Layer mode:
		the connection will be encrypted with a private key algorithm after a
		public key handshake. -s will work only if the KVIrc executable was compiled
		with SSL support enabled and if the remote end supports it.
		The eventual DCC request will contain the string SCHAT instead of CHAT.[br]
		When requesting a SSL based DCC chat to someone you probably will need a
		certificate. If you don't have one, create it (for example with CA.pl -newcert)
		and set it in the options dialog.
	@examples:
		Simple examples:
		[example]
			# Simple DCC chat to Pragma
			dcc.chat Pragma
			# DCC chat to Pragma, listen on address 127.0.0.1
			dcc.chat -i=127.0.0.1 Pragma
			# DCC chat to Pragma, listen on address 168.0.0.1 and port 1025
			dcc.chat -i=168.0.0.1 -p=1025 Pragma
		[/example]
		More tricky ones:
		[example]
			# DCC chat to Pragma, listen on address 127.0.0.1 and port 1080
			# but tell him to connect to address 212.134.22.11 port 1080
			dcc.chat -i=127.0.0.1 -p=1080 -a=212.134.22.11 Pragma
			# DCC chat to Pragma, listen on address 127.0.0.1 and port 1080
			# but tell him to connect to address 212.134.22.11 port 1090
			dcc.chat -i=127.0.0.1 -p=1080 -a=212.134.22.11 -f=1090 Pragma
		[/example]
		Now run completely out of bounds. Use dcc.chat connections
		to do various things:
		[example]
			# Tricky: simulate a HTTP server
			dcc.chat -n -i=127.0.0.1 -p=80 Netscape
			# Now open http://127.0.0.1 with netscape
			# and type "<html><body>Hello!</body></html>" :)
			#
			# Tricky 2: simulate the ident daemon (if you have none)
			dcc.chat -n -i=127.0.0.1 -p=113 Client
			#
			# Now a self-DCC connection without the IRC negotiation
			# Src: Setup a listening socket awaiting the "Destination" user connection
			dcc.chat -n -i=127.0.0.1 -p=1080 Dst
			# Dst: Connect to the listening socket at addr 127.0.0.1 and port 1080
			dcc.chat -c -i=127.0.0.1 -p=1080 Src
			# The above example will mess you a bit...
			# Try to guess why in both windows YOU have the same nickname
			# that is probably not Dst nor Src... :)
		[/example]
		Using the shell ftp proggie is too easy:
		we're [b]real hackers[/b] and want to do complicated things...
		[example]
			# Connect to the local ftp server and get the list of contents
			/dcc.chat -c -i=127.0.0.1 -p=21 FtpServer
			# Now login, type in the new window (the following lines are NOT commands):
			USER youruser
			PASS yourpass
			# Now enter passive mode
			PASV
			# And watch the line that you have received...sth like
			# 227 Entering passive mode (127,0,0,1,210,195)
			/dcc.chat -c -i=127.0.0.1 -p=$((210 * 256) + 195) FtpList
			# (Change the port numbers accordingly)
			# And then type in the FtpServer window (this is NOT a KVIrc command):
			LIST
			# Then watch the ls output in the FtpList window. :)
			# In this way you can also read ascii files by ftp:
			# Assume that in the previous ls output you have seen
			# a README file.
			# In the FtpServer window type:
			PASV
			# Watch the message
			# 227 Entering passive mode (127,0,0,1,22,227)
			/dcc.chat -c -i=127.0.0.1 -p=$((22 * 256) + 227) README
			# In the FtpServer window type:
			RETR README
			# And read the file in the README window :)
		[/example]
*/

static bool dcc_kvs_cmd_chat(KviKvsModuleCommandCall * c)
{
	TQString szTarget;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());

	d->szNick       = szTarget;    // we always specify the nickname
	d->szUser       = __tr2qs_ctx("unknown","dcc"); // username is always unknown
	d->szHost       = d->szUser;       // host is always unknown

	if(!dcc_kvs_parse_default_parameters(d,c))return false;
	dcc_module_set_dcc_type(d,"CHAT");

	if(c->switches()->find('z',"zero-port"))
	{
		// we want to have it reversed... add a tag and send out the request
		KviDccZeroPortTag * t = g_pDccBroker->addZeroPortTag();

		d->console()->connection()->sendFmtData("PRIVMSG %s :%cDCC %s chat 127.0.0.1 0 %s%c",
			d->console()->connection()->encodeText(d->szNick).data(),
			0x01,
			d->console()->connection()->encodeText(d->szType).data(),
			d->console()->connection()->encodeText(t->m_szTag).data(),
			0x01);

		return true;
	}

	if(c->switches()->find('c',"connect"))
	{
		if(!(c->switches()->find('i',"ip") && c->switches()->find('p',"port")))
		{
			delete d;
			c->error(__tr2qs_ctx("-c requires -i and -p","dcc"));
			return false;
		}
		d->szIp         = d->szListenIp;
		d->szPort       = d->szListenPort;
		d->szListenIp   = ""; // useless
		d->szListenPort = ""; // useless
		d->bActive      = true;
	} else {
		d->szIp         = __tr2qs_ctx("unknown","dcc");
		d->szPort       = d->szIp;
		d->bActive      = false;
		d->bSendRequest = !c->switches()->find('n',"no-ctcp");
	}
	
	//c->window()->output(0,"%Q %Q %Q",&(d->szIp),&(d->szPort),&(d->szListenIp));
	d->triggerCreationEvent();
	g_pDccBroker->executeChat(0,d);

	return true;
}


/*
	@doc: dcc.send
	@type:
		command
	@title:
		dcc.send
	@short:
		Sends a file
	@syntax:
		dcc.send [-s] [-n] [-c] [-u] [-b] [-g[=<file size>]] [-t] [-m[=<boolean>]] [-i=<interface>] [-p=<port>] [-a=<fake address>] [-f=<fake port>] <nickname> [filename]
	@switches:
		!sw: -g[=<file size>] | --get[=<file size>]
		This switch is a dirty trick, you can use it to receive files from people
		behind a firewall with masquerading enabled.[br]
		It causes the transfer direction to be inverted; your client will receive
		the file from the remote host instead of sending it.[br]
		<file size> is the expected file size in bytes. This parameter can be omitted,
		and in this case the DCC will "blindly" trust the remote end and assume
		that the file has been transferred correctly when the remote end closes the connection.[br]
		If you don't pass the -n option, the remote end will receive an informational DCC RECV request,
		specifying the IP address and the port to connect to.[br]
		-t can be used to prevent sending acknowledges to the remote end, and -u can be used
		to avoid the listening socket to timeout.[br]
		-a and -f can be used as well, but I see no real reason for that...[br]
		Here is an example of usage of this option:[br]
		spion can't accept connections (is behind a firewall with masquerading or some other reason...),
		to his machine.[br]
		spion wants to send the file important.jpg to Pragma.[br]
		spion tells to Pragma that he wants to send him the file and Pragma sets up a connection in the following way:[br]
		[b]dcc.send -g spion important.png[/b][br]
		spion will see the informational DCC RECV request with the IP address and port that Pragma is listening on.
		Assume that the address was 212.212.231.220 and the port 32344.[br]
		spion will then use the following command:[br]
		[b]dcc.send -c -i=212.212.231.220 -p=32344 Pragma /home/spion/important.jpg[/b][br]
		Et voila!..the file is transferring.[br]
		Pragma will see no file progress indication, since the file size is unknown on Pragma's side.[br]
		If spion had specified the file size, Pragma could use -g=<file size> while setting up the connection,
		to be able to see the progress indications.[br]
		If Pragma used the the -n option, the DCC RECV indication wouldn't have been sent, in this case
		Pragma would need to communicate the Ip address and the port "manually" to spion.[br]

		!sw: -b | --blind
		Assume that no acknowledges are sent.
		Assume that the transfer was successful when the whole file has been sent,
		then close the socket.[br]
		This is called a "blind" DCC send.[br]
		
		!sw: -t | -tdcc
		Emulate the TDCC protocol: Use the TDCC CTCP message (DCC TSEND) for requesting the connection
		and assume that no acknowledges are sent. Wait for the remote end to close the connection.[br]

		!sw: -m[=<boolean>] | --minimize[=<boolean>]
		If the -m switch is passed, the default boolCreateMinimizedDccSend option
		is overridden with the <boolean> parameter passed. So actually
		by passing -m=1 will create a minimized DCC send even
		if the [fnc]$option[/fnc](boolCreateMinimizedDccSend) returns false.[br]
		In the same way, by passing -m=0 you will create a non minimized DCC send.
		If no <boolean> value is specified, it defaults to 1.[br]

		!sw: -n | --no-ctcp
		Do NOT send the CTCP request to the target user, you will have to do it manually,
		or the remote user will have to connect manually (for example by using dcc.recv -c).[br]
		
		!sw: -c | --connect
		Attempt to CONNECT to the remote host specified as <interface> and <port>,
		instead of listening (active connection instead of a passive one).
		In this case the -i and -p switches are mandatory.[br]
		The 'c' switch takes precedence over 'n' (In fact both should
		be mutually exclusive).[br]
		If the 'c' and 'n' switches are missing, this commands
		needs to be executed in a window that is bound to a connected
		IRC context (you need a third entity to accomplish the negotiation).[br]

		!sw: -i=<interface> | --ip=<interface>
		Bind the local listening socket to the specified <interface> (which is an IP address, IPv4 or IPv6).
		If this switch is NOT specified, the socket is bound to the interface of
		the current IRC connection (if any) or to "127.0.0.1".[br]
		You can also specify a local interface name to get the address from (this works only for IPv4 interfaces
		since IPv6 ones seems to be unsupported by the system ioctl() calls at the moment (for linux at least)).[br]
		Here go some examples:[br]
		-i=215.243.12.12: this will bind to the IPv4 interface with the specified address.[br]
		-i=3ffe:1001::1: this will bind to the IPv6 interface with the specified address.[br]
		-i=ppp0: this will bind to the IPv4 address of the interface ppp0 (if supported by the underlying system).[br]
		The -i switch parameter may serve also as a target address when the -c switch is used.[br]
		
		!sw: -p=<port> | --port=<port>
		Bind the local listening socket to the specified <port>.
		If this switch is NOT specified, the port will be a "random" one choosen by the kernel.[br]

		!sw: -a=<fake address> | --fake-address=<fake address>
		Send the <fake address> as target for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real IP address of the listening
		interface.[br]
		
		!sw: -f=<fake port> | --fake-port=<fake port>
		Send the <fake port> as target port for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real port of the listening socket.
		[br][br]
		All these switches are meant to allow maximum flexibility of the
		DCC negotiation, earlier KVIrc releases had serious problems
		with firewalled and/or masqueraded machines. With the -a and -f switches
		you can work around it.[br]
		[br]

		!sw: -u | --unlimited
		If the 'u' switch is given, the connection attempt will
		never time out; this might be useful if you want to leave
		a listening socket for a friend of yours while you are sleeping
		and have the CTCP processing disabled. The 'u' switch works either
		in active and passive mode.[br]

		!sw: -s | --ssl
		Use a Secure Socket Layer for the transfer; the whole communication will be encrypted
		with a private key algorithm after a public key handshake.[br]
		This option will work only if the KVIrc executable has been compiled with SSL support
		and the remote end supports the SSL protocol too.[br]
		Please note that this will may down the transfer somewhat.[br]
		-s can be combined with -t.[br]
		The CTCP negotiation will use SSEND as parameter (or eventually TSSEND).[br]
		When requesting a SSL based DCC send to someone you probably will need a
		certificate. If you don't have one, create it (for example with CA.pl -newcert)
		and set it in the options dialog.

	@description:
		Attempts to send the file <filename> to <nickname>.[br]
		If [filename] is specified it must be an absolute file path,
		otherwise a file selection dialog is opened.[br]
		The simplest case "dcc.send <nickname> <filename>" will work just as in all
		the other IRC clients, but this command is really more powerful...[br]
		Before attempting to understand the possibilities of this command,
		be sure to know how a [doc:dcc_connection]DCC negotiation and connection[/doc] works.[br]
		The file will be sent as a sequence of packets which must
		be acknowledged one by one by the active client.[br]
		There is a special option (see $option()) called "fast send" (also known
		as "send ahead") that makes KVIrc avoid to wait for the acknowledge
		of the last packet before sending the next one.[br]
		Anyway, the connection is declared as successful only
		when the whole file (all the packets) has been acknowledged.[br]
		If you need to send a file but you're firewalled, you might take
		a look at the [cmd]dcc.rsend[/cmd] command. It also handles
		the mIrc zero port protocol extension.
	@examples:

*/

//#warning "Example for -r"
//#warning "OPTION FOR NO GUI ? (this is hard...)"
//#warning "When in IPv6 mode, should be able to use the IPv4 binding!"

static bool dcc_kvs_cmd_send(KviKvsModuleCommandCall * c)
{
	TQString szTarget,szFileName;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
		KVSM_PARAMETER("file name",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szFileName)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());

	d->szNick            = szTarget;    // we always specify the nickname

	TQString szTmp;
	KviKvsVariant * pSw = 0;

	if(pSw = c->switches()->find('g',"get"))
	{
		d->szFileName = TQFileInfo(szFileName).fileName();

		if(!pSw->isBoolean())
		{
			kvs_int_t iSize;
			if(pSw->asInteger(iSize)) // is an integer
			{
				pSw->asString(szTmp);
				// avoid sprintf as long as possibile
				d->szFileSize = szTmp;
			} else {
				d->szFileSize = __tr_ctx("<unknown size>","dcc");
			}
		}
	} else {
		d->szFileName      = szFileName;
		d->szLocalFileName = szFileName;
	}

	d->szUser            = __tr2qs_ctx("unknown","dcc"); // username is always unknown
	d->szHost            = d->szUser;                 // host is always unknown
	d->bRecvFile         = pSw != 0;
	d->bNoAcks           = c->switches()->find('b',"blind") || c->switches()->find('t',"tdcc");
	d->bResume           = false;
	d->bAutoAccept       = pSw != 0;
	d->bIsIncomingAvatar = false;

	if(!dcc_kvs_parse_default_parameters(d,c))return false;

	if(c->switches()->find('c',"connect"))
	{
		if(!(c->switches()->find('i',"ip") && c->switches()->find('p',"port")))
		{
			delete d;
			c->error(__tr2qs_ctx("-c requires -i and -p","dcc"));
			return false;
		}
		d->szIp         = d->szListenIp;
		d->szPort       = d->szListenPort;
		d->szListenIp   = ""; // useless
		d->szListenPort = ""; // useless
		d->bActive      = true;
	} else {
		d->szIp         = __tr2qs_ctx("unknown","dcc");
		d->szPort       = d->szIp;
		d->bActive      = false;
		d->bSendRequest = !c->switches()->find('n',"no-ctcp");
	}

	if(c->switches()->find('g',"get"))
	{
		dcc_module_set_dcc_type(d,"RECV");
		d->triggerCreationEvent();
		g_pDccBroker->recvFileManage(d);
	} else {
		dcc_module_set_dcc_type(d,"SEND");
		d->triggerCreationEvent();
		if(!d->szLocalFileName.isEmpty())
		{
			g_pDccBroker->sendFileExecute(0,d);
		} else {
			g_pDccBroker->sendFileManage(d);
		}
	}

	return true;
}

/*
	@doc: dcc.recv
	@type:
		command
	@title:
		dcc.recv
	@short:
		Sets up a file receiving connection
	@syntax:
		dcc.recv [-s] [-t] [-u] [-b] [-n] [-c] [-i=<interface>] [-p=<port>] [-m[=<boolean>]] <nickname> <filename> <remote file size>
	@switches:
		!sw: -b | --blind
		Assume that no acknowledges are sent.
		Assume that the transfer was successful when the whole file has been sent,
		then close the socket.[br]
		This is called a "blind" DCC send.[br]
		
		!sw: -t | -tdcc
		Emulate the TDCC protocol: Use the TDCC CTCP message (DCC TSEND) for requesting the connection
		and assume that no acknowledges are sent. Wait for the remote end to close the connection.[br]

		!sw: -m[=<boolean>] | --minimize[=<boolean>]
		If the -m switch is passed, the default boolCreateMinimizedDccSend option
		is overridden with the <boolean> parameter passed. So actually
		by passing -m=1 will create a minimized DCC send even
		if the [fnc]$option[/fnc](boolCreateMinimizedDccSend) returns false.[br]
		In the same way, by passing -m=0 you will create a non minimized DCC send.
		If no <boolean> value is specified, it defaults to 1.[br]

		!sw: -n | --no-ctcp
		Do NOT send the CTCP request to the target user, you will have to do it manually,
		or the remote user will have to connect manually (for example by using dcc.recv -c).[br]
		
		!sw: -i=<interface> | --ip=<interface>
		Bind the local listening socket to the specified <interface> (which is an IP address, IPv4 or IPv6).
		If this switch is NOT specified, the socket is bound to the interface of
		the current IRC connection (if any) or to "127.0.0.1".[br]
		You can also specify a local interface name to get the address from (this works only for IPv4 interfaces
		since IPv6 ones seems to be unsupported by the system ioctl() calls at the moment (for linux at least)).[br]
		Here go some examples:[br]
		-i=215.243.12.12: this will bind to the IPv4 interface with the specified address.[br]
		-i=3ffe:1001::1: this will bind to the IPv6 interface with the specified address.[br]
		-i=ppp0: this will bind to the IPv4 address of the interface ppp0 (if supported by the underlying system).[br]
		The -i switch parameter may serve also as a target address when the -c switch is used.[br]
		
		!sw: -p=<port> | --port=<port>
		Bind the local listening socket to the specified <port>.
		If this switch is NOT specified, the port will be a "random" one choosen by the kernel.[br]

		!sw: -a=<fake address> | --fake-address=<fake address>
		Send the <fake address> as target for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real IP address of the listening
		interface.[br]
		
		!sw: -f=<fake port> | --fake-port=<fake port>
		Send the <fake port> as target port for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real port of the listening socket.
		[br][br]
		All these switches are meant to allow maximum flexibility of the
		DCC negotiation, earlier KVIrc releases had serious problems
		with firewalled and/or masqueraded machines. With the -a and -f switches
		you can work around it.[br]
		[br]

		!sw: -u | --unlimited
		If the 'u' switch is given, the connection attempt will
		never time out; this might be useful if you want to leave
		a listening socket for a friend of yours while you are sleeping
		and have the CTCP processing disabled. The 'u' switch works either
		in active and passive mode.[br]

		!sw: -s | --ssl
		Use a Secure Socket Layer for the transfer; the whole communication will be encrypted
		with a private key algorithm after a public key handshake.[br]
		This option will work only if the KVIrc executable has been compiled with SSL support
		and the remote end supports the SSL protocol too.[br]
		Please note that this will may down the transfer somewhat.[br]
		-s can be combined with -t.[br]
		The CTCP negotiation will use SSEND as parameter (or eventually TSSEND).[br]
		When requesting a SSL based DCC send to someone you probably will need a
		certificate. If you don't have one, create it (for example with CA.pl -newcert)
		and set it in the options dialog.

		!sw: -c | --connect
		Accepted for compatibility: don't use it!
	@description:
		Sets up a connection ready to receive a file.[br]
		In most 'common' cases you will not need this command, you might want to use [cmd]dcc.get[/cmd] instead.[br]
		This works like dcc.send but has two main differences: The file is INCOMING, and the CTCP sent to the other side
		is a CTCP RECV.[br]
		This command is the counterpart of [cmd]dcc.send[/cmd] and its parameters are exactly the same, so please refer to that
		help page for the full discussion. This help page contains only a brief resume of these parameters.[br]
		The [doc:dcc_connection]dcc documentation[/doc] explains the DCC Recv subprotocol in detail.[br]
	@examples:

*/

//#warning "ENCODE THE CTCP'S!!!!!!!"
//#warning "DOCS FOR dcc.recv (examples!)"

static bool dcc_kvs_cmd_recv(KviKvsModuleCommandCall * c)
{
	TQString szTarget,szFileName;
	kvs_uint_t uSize;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
		KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFileName)
		KVSM_PARAMETER("size",KVS_PT_UINT,0,uSize)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());
	d->szNick            = szTarget;
	d->szUser            = __tr2qs_ctx("unknown","dcc");
	d->szHost            = d->szUser;
	d->szIp              = __tr2qs_ctx("unknown","dcc");
	d->szPort            = d->szIp;

	// -c is senseless here...but we accept it for compatibility

	if(!dcc_kvs_parse_default_parameters(d,c))return false;

	d->szFileName        = szFileName;
	d->szFileSize.setNum(uSize);

	d->bActive           = false; // we have to listen!
	d->bResume           = false; 
	d->bRecvFile         = true;  // we have to receive the file!
	d->bSendRequest      = !c->switches()->find('n',"no-ctcp");
	d->bNoAcks           = d->bIsTdcc || c->switches()->find('b',"blind");
	d->bAutoAccept       = KVI_OPTION_BOOL(KviOption_boolAutoAcceptDccSend);
	d->bIsIncomingAvatar = g_pApp->findPendingAvatarChange(d->console(),d->szNick,d->szFileName);

	if(KVI_OPTION_BOOL(KviOption_boolAutoAcceptIncomingAvatars))d->bAutoAccept = d->bAutoAccept || d->bIsIncomingAvatar;
	dcc_module_set_dcc_type(d,"RECV");
	d->triggerCreationEvent();
	g_pDccBroker->recvFileManage(d);

	return true;
}

/*
	@doc: dcc.rsend
	@type:
		command
	@title:
		dcc.rsend
	@short:
		Sends a file by using the Reverse DCC Send protocol
	@syntax:
		dcc.rsend [-s] [-t] <nickname> [filename]
	@switches:
		!sw: -t | -tdcc
		Emulate the TDCC protocol.

		!sw: -s | --ssl
		Use a Secure Socket Layer for the transfer; the whole communication will be encrypted
		with a private key algorithm after a public key handshake.[br]
		This option will work only if the KVIrc executable has been compiled with SSL support
		and the remote end supports the SSL protocol too.[br]
		Please note that this will may down the transfer somewhat.[br]
		-s can be combined with -t.[br]
		The CTCP negotiation will use SSEND as parameter (or eventually TSSEND).[br]
		When requesting a SSL based DCC send to someone you probably will need a
		certificate. If you don't have one, create it (for example with CA.pl -newcert)
		and set it in the options dialog.
		!sw: -z | --zero-port
		Use the 0 port method. This is a dirty hack that allows you to use the RSEND
		protocol with mIrc receiving clients.
	@description:
		Sends a DCC RSEND request to <nickname> notifying him that you want to
		send him the file [filename].[br]
		The remote end may acknowledge the request by sending a DCC RECV request.
		This command effects are similar to [cmd]dcc.send[/cmd], but will work also on machines
		that can't accept incoming connections (firewalling or masquerading problems).[br]
		A 120 seconds file offer is added for the specified file and mask "<nickname>!*@*".
	@examples:

*/

//#warning "ENCODE THE CTCP'S!!!!!!!"
//#warning "DOCS FOR dcc.rsend"

static bool dcc_kvs_cmd_rsend(KviKvsModuleCommandCall * c)
{
	TQString szTarget,szFileName;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
		KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szFileName)
	KVSM_PARAMETERS_END(c)

	KVSM_REQUIRE_CONNECTION(c)

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());
	d->szNick            = szTarget;
	d->szLocalFileName   = szFileName;
	d->bIsTdcc           = c->switches()->find('t',"tdcc");
#ifdef COMPILE_SSL_SUPPORT
	d->bIsSSL            = c->switches()->find('s',"ssl");
#else
	if(c->switches()->find('s',"ssl"))c->warning(__tr2qs_ctx("This executable has been built without SSL support, -s switch ignored","dcc"));
#endif //!COMPILE_SSL_SUPPORT

	if(c->switches()->find('z',"zero-port"))
	{
		dcc_module_set_dcc_type(d,"SEND");
		d->setZeroPortRequestTag("nonempty"); // just to tag it
	} else
		dcc_module_set_dcc_type(d,"RSEND");
	d->triggerCreationEvent();
	g_pDccBroker->rsendManage(d);

	return true;
}




/*
	@doc: dcc.get
	@type:
		command
	@title:
		dcc.get
	@short:
		Requests a file
	@syntax:
		dcc.get [-s] [-t] <nickname> <filename> [filesize]
	@description:
		Sends a CTCP DCC GET to <nickname> requesting the file <filename>.
		The remote end should reply with a DCC SEND request CTCP.
		<filename> must not contain any leading path.
		If the -t switch is given, the message is a DCC TGET, expecting
		a TSEND reply.[br]
		If the -s switch is given, the message will be a DCC SGET, expecting
		a SSEND reply.[br]
		-t and -s can be combined together to obtain a "turbo"+"SSL" extension transfer.[br]
		-s will work only if the KVIrc executable has been compiled with SSL support and
		the remote client supports it.[br]
	@examples:

*/

//#warning "ENCODE THE CTCP'S!!!!!!!"
//#warning "DOCS FOR dcc.get"

static bool dcc_kvs_cmd_get(KviKvsModuleCommandCall * c)
{
	TQString szTarget,szFileName;
	kvs_uint_t uSize;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
		KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFileName)
		KVSM_PARAMETER("size",KVS_PT_UINT,KVS_PF_OPTIONAL,uSize)
	KVSM_PARAMETERS_END(c)

	KVSM_REQUIRE_CONNECTION(c)

	KviTQString::cutToLast(szFileName,'/');

	if(szFileName.contains(' '))
	{
		szFileName.prepend('"');
		szFileName.append('"');
	}

	KviStr szDCC("GET");
#ifdef COMPILE_SSL_SUPPORT
	if(c->switches()->find('s',"ssl"))szDCC.prepend('S');
#else
	if(c->switches()->find('s',"ssl"))c->warning(__tr2qs_ctx("This executable has no SSL support, -s switch ignored","dcc"));
#endif
	if(c->switches()->find('t',"tdcc"))szDCC.prepend('T');

	if(uSize == 0)
	{
		c->window()->console()->connection()->sendFmtData("PRIVMSG %s :%cDCC %s %s%c",
			c->window()->console()->connection()->encodeText(szTarget).data(),
			0x01,
			c->window()->console()->connection()->encodeText(szDCC.ptr()).data(),
			c->window()->console()->connection()->encodeText(szFileName).data(),
			0x01);
	} else {
		c->window()->console()->connection()->sendFmtData("PRIVMSG %s :%cDCC %s %s %u%c",
			c->window()->console()->connection()->encodeText(szTarget).data(),
			0x01,
			c->window()->console()->connection()->encodeText(szDCC.ptr()).data(),
			c->window()->console()->connection()->encodeText(szFileName).data(),
			uSize,0x01);
	}

	return true;
}




// FIXME: SSL support for DCC VOICE ? (has a sense ?)

/*
	@doc: dcc.voice
	@type:
		command
	@title:
		dcc.voice
	@short:
		Starts a DCC Voice connection
	@syntax:
		dcc.voice [-g=<codec>] [-n] [-c] [-u] [-h=<sample_rate_in_hz>] [-m[=<boolean>]] [-i=<interface>] [-p=<port>] [-a=<fake address>] [-f=<fake port>] <nickname>
	@switches:
		!sw: -g=<codec> | --codec=<codec>
		Use the codec specified as parameter.
		Actually the supported codecs are "null","adpcm" and "gsm".
		
		!sw: -h=<rate> | --sample-rate=<rate>
		Use the sample rate specified by <rage>.
		Valid sample rates are 8000, 11025, 22050 and 44100 Hz.
		
		!sw: -m[=<boolean>] | --minimize[=<boolean>]
		If the -m switch is passed, the default boolCreateMinimizedDccSend option
		is overridden with the <boolean> parameter passed. So actually
		by passing -m=1 will create a minimized DCC send even
		if the [fnc]$option[/fnc](boolCreateMinimizedDccSend) returns false.[br]
		In the same way, by passing -m=0 you will create a non minimized DCC send.
		If no <boolean> value is specified, it defaults to 1.[br]

		!sw: -n | --no-ctcp
		Do NOT send the CTCP request to the target user, you will have to do it manually,
		or the remote user will have to connect manually (for example by using dcc.recv -c).[br]
		
		!sw: -c | --connect
		Attempt to CONNECT to the remote host specified as <interface> and <port>,
		instead of listening (active connection instead of a passive one).
		In this case the -i and -p switches are mandatory.[br]
		The 'c' switch takes precedence over 'n' (In fact both should
		be mutually exclusive).[br]
		If the 'c' and 'n' switches are missing, this commands
		needs to be executed in a window that is bound to a connected
		IRC context (you need a third entity to accomplish the negotiation).[br]

		!sw: -i=<interface> | --ip=<interface>
		Bind the local listening socket to the specified <interface> (which is an IP address, IPv4 or IPv6).
		If this switch is NOT specified, the socket is bound to the interface of
		the current IRC connection (if any) or to "127.0.0.1".[br]
		You can also specify a local interface name to get the address from (this works only for IPv4 interfaces
		since IPv6 ones seems to be unsupported by the system ioctl() calls at the moment (for linux at least)).[br]
		Here go some examples:[br]
		-i=215.243.12.12: this will bind to the IPv4 interface with the specified address.[br]
		-i=3ffe:1001::1: this will bind to the IPv6 interface with the specified address.[br]
		-i=ppp0: this will bind to the IPv4 address of the interface ppp0 (if supported by the underlying system).[br]
		The -i switch parameter may serve also as a target address when the -c switch is used.[br]
		
		!sw: -p=<port> | --port=<port>
		Bind the local listening socket to the specified <port>.
		If this switch is NOT specified, the port will be a "random" one choosen by the kernel.[br]

		!sw: -a=<fake address> | --fake-address=<fake address>
		Send the <fake address> as target for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real IP address of the listening
		interface.[br]
		
		!sw: -f=<fake port> | --fake-port=<fake port>
		Send the <fake port> as target port for the remote client in the requesting CTCP message.
		If this switch is not given, the CTCP will contain the real port of the listening socket.
		[br][br]
		All these switches are meant to allow maximum flexibility of the
		DCC negotiation, earlier KVIrc releases had serious problems
		with firewalled and/or masqueraded machines. With the -a and -f switches
		you can work around it.[br]
		[br]

		!sw: -u | --unlimited
		If the 'u' switch is given, the connection attempt will
		never time out; this might be useful if you want to leave
		a listening socket for a friend of yours while you are sleeping
		and have the CTCP processing disabled. The 'u' switch works either
		in active and passive mode.[br]

	@description:
		Attempts a DCC Voice connection to <nickname>.[br]
		The -g option is used to select the GSM codec, available codecs are "gsm", "adpcm" and "null".[br]
		The adpcm codec is the one that was used in previous KVIrc versions, it provides a 1:4 compression rate
		and is designed for 8 KHz audio sampling rate (but will work also with other sampling rates).[br]
		The gsm codec offers 1:10 compression at the cost of some quality and cpu time. If you have a good
		cpu and plan to transmit voice only, use this codec.<br> The null codec
		offers no compression and may be used to transfer plain audio data over a fast connection (usually loopback
		connection or local networks). The null codec with 44100 sampling rate would provide CD quality audio
		streaming, but it is practically not usable (at the time I'm writing) since requires a
		monster bandwidth. If the -g switch is not present, the adpcm codec is used by default (for compatibility reasons).[br]
 		The -h switch is used to select the sampling rate, if not given the sampling rate defaults to 8000 Hz.
		This switch accepts any value, but in fact the soundcards have limits on the values. Typically
		the lowest limit is 5 KHz and the upper limit is 44.1 KHz (but some soundcards support 96 KHz).
		It is also possible that the soundcard can't support a continous range of frequencies and
		will select a discrete closest match instead.[br]
		The "commonly used" sample rates are 8000, 11025, 22050 and 44100 Hz.[br]
		The remaining parameters are equivalent to the ones used in [cmd]dcc.send[/cmd], so please refer to that
		help page for the full discussion. This help page contains only a brief resume of these parameters.[br]
	@examples:
		[example]
			[comment]# Setup a DCC VOICE connection with Pragma (IRC user)[/comment]
			dcc.voice Pragma
			[comment]# Setup a DCC VOICE connection with Pragma and use the gsm codec[/comment]
			dcc.voice -g=gsm Pragma
			[comment]# Setup a DCC VOICE connection with Pragma, use the gsm codec and 22050 Hz sampling rate[/comment]
			dcc.voice -g=gsm -h=22050 Pragma
			[comment]# Setup a listening socket for a DCC VOICE connection on interface 127.0.0.1 and port 8088[/comment]
			dcc.voice -n -i=127.0.0.1 -p=8088 Pippo
			[comment]# Connect to the socket above[/comment]
			dcc.voice -c -i=127.0.0.1 -p=8088 Pluto
			[comment]# Same as above but use the NULL codec with 11025 Hz sampling rate[/comment]
			dcc.voice -g=null -h=11025 -n -i=127.0.0.1 -p=8088 Pippo
			[comment]# Connect to the socket above[/comment]
			dcc.voice -g=null -h=11025 -c -i=127.0.0.1 -p=8088 Pluto
		[/example]
*/

static bool dcc_kvs_cmd_voice(KviKvsModuleCommandCall * c)
{
	TQString szTarget;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("target",KVS_PT_NONEMPTYSTRING,0,szTarget)
	KVSM_PARAMETERS_END(c)

#ifdef COMPILE_DISABLE_DCC_VOICE
	c->warning(__tr2qs_ctx("DCC VOICE support not enabled at compilation time","dcc"));
	return true;
#endif

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());

	d->szNick       = szTarget;              // we always specify the nickname
	d->szUser       = __tr2qs_ctx("unknown","dcc"); // username is always unknown
	d->szHost       = d->szUser;                 // host is always unknown
	d->iSampleRate  = 8000;

	if(!dcc_kvs_parse_default_parameters(d,c))return false;

	if(KviKvsVariant * pSR = c->switches()->find('h',"sample-rate"))
	{
		kvs_int_t iSR;
		if(!pSR->asInteger(iSR))
		{
			c->warning(__tr2qs_ctx("Invalid sample rate specified, defaulting to 8000","dcc"));
			d->iSampleRate = 8000;
		} else {
			d->iSampleRate = iSR;
		}
	}

	d->szCodec = "adpcm";

	if(KviKvsVariant * pCodec = c->switches()->find('g',"codec"))
	{
		TQString szCodec;
		pCodec->asString(szCodec);

		if(!kvi_dcc_voice_is_valid_codec(szCodec))
		{
			c->warning(__tr2qs_ctx("Invalid codec specified, defaulting to 'adpcm'","dcc"));
			d->szCodec = "adpcm";
		}
	}

	dcc_module_set_dcc_type(d,"VOICE");
	if(c->switches()->find('c',"connect"))
	{
		if(!(c->switches()->find('i',"ip") && c->switches()->find('p',"port")))
		{
			delete d;
			c->error(__tr2qs_ctx("-c requires -i and -p","dcc"));
			return false;
		}
		d->szIp         = d->szListenIp;
		d->szPort       = d->szListenPort;
		d->szListenIp   = ""; // useless
		d->szListenPort = ""; // useless
		d->bActive      = true;

		d->triggerCreationEvent();
		g_pDccBroker->activeVoiceExecute(0,d);
	} else {
		d->szIp         = __tr2qs_ctx("unknown","dcc");
		d->szPort       = d->szIp;
		d->bActive      = false;
		d->bSendRequest = !(c->switches()->find('n',"no-ctcp"));

		d->triggerCreationEvent();
		g_pDccBroker->passiveVoiceExecute(d);
	}

	return true;
}




/*
static bool dcc_module_cmd_canvas(KviModule *m,KviCommand *c)
{
	ENTER_STACK_FRAME(c,"dcc_module_cmd_canvas");

	KviStr target;
	if(!g_pUserParser->parseCmdFinalPart(c,target))return false;

	if(target.isEmpty())return c->error(KviError_notEnoughParameters,"%s",__tr_ctx("Missing target nickname","dcc"));

	KviDccDescriptor * d = new KviDccDescriptor(c->window()->console());

	d->szNick       = target.ptr();              // we always specify the nickname
	d->szUser       = __tr2qs_ctx("unknown","dcc"); // username is always unknown
	d->szHost       = d->szUser;                 // host is always unknown
*/
/*
	d->bIsTdcc = c->hasSwitch('t');

	d->bOverrideMinimize = c->hasSwitch('m');

	if(d->bOverrideMinimize)
	{
		KviStr tmpVal;
		if(!(c->getSwitchValue('m',tmpVal)))d->bShowMinimized = false;
		else d->bShowMinimized = kvi_strEqualCI(tmpVal.ptr(),"1");
	}


	if(!d->console())
	{
		// We don't need a console with -c and -n , otherwise we need it
		if(!(c->hasSwitch('c') || c->hasSwitch('n')))return c->noIrcContext();
		else d->console() = c->window()->frame()->firstConsole();
	}

	__range_valid(d->console());

	if(!d->console()->isConnected())
	{
		// We don't need a connection with -c and -n , otherwise we need it
		if(!(c->hasSwitch('c') || c->hasSwitch('n')))return c->notConnectedToServer();
		else {
			// -c or -n , grab a local nick from somewhere
			d->szLocalNick  = KVI_OPTION_STRING(KviOption_stringNickname1);
			d->szLocalNick.stripWhiteSpace();
			if(d->szLocalNick.isEmpty())d->szLocalNick = KVI_DEFAULT_NICKNAME1;
			d->szLocalUser  = __tr("unknown"); // we can live without it
			d->szLocalHost  = d->szLocalUser;  // we can live without it
		}
	} else {
		// We know everything
		d->szLocalNick  = d->console()->currentNickName();
		d->szLocalUser  = d->console()->currentUserName();
		d->szLocalHost  = d->console()->currentLocalHostName();
	}


	if(c->hasSwitch('i'))
	{
		c->getSwitchValue('i',d->szListenIp);
		if(!(d->szListenIp.contains('.') || d->szListenIp.contains(':')))
		{
			// This will magically work with the same buffer as source and dst!
			if(!kvi_getInterfaceAddress(d->szListenIp.ptr(),d->szListenIp))
			{
				return c->error(KviError_invalidParameter,__tr("Can't get address of interface %s"),d->szListenIp.ptr());
			}
		}
	} else {
		if(d->console()->isConnected())
		{
			d->console()->socket()->getLocalHostIp(d->szListenIp,d->console()->isIpV6Connection());
		} else d->szListenIp = "127.0.0.1"; // huh ? :)
	}

	if(c->hasSwitch('p'))c->getSwitchValue('p',d->szListenPort);
	else d->szListenPort = "0"; // any port is ok

	if(c->hasSwitch('a'))c->getSwitchValue('a',d->szFakeIp);

	if(c->hasSwitch('f'))c->getSwitchValue('f',d->szFakePort);

	d->bDoTimeout = (!c->hasSwitch('u'));

*/
/*
	if(!dcc_module_parse_default_parameters(d,c))return false;
	dcc_module_set_dcc_type(d,"VOICE");

	if(c->hasSwitch('c'))
	{
		if(!(c->hasSwitch('i') && c->hasSwitch('p')))
		{
			delete d;
			return c->error(KviError_notEnoughParameters,__tr_ctx("-c requires -i and -p","dcc"));
		}
		d->szIp         = d->szListenIp;
		d->szPort       = d->szListenPort;
		d->szListenIp   = ""; // useless
		d->szListenPort = ""; // useless
		d->bActive      = true;

		d->triggerCreationEvent();
		g_pDccBroker->activeCanvasExecute(0,d);
	} else {
		d->szIp         = __tr2qs_ctx("unknown","dcc");
		d->szPort       = d->szIp;
		d->bActive      = false;
		d->bSendRequest = !c->hasSwitch('n');

		d->triggerCreationEvent();
		g_pDccBroker->passiveCanvasExecute(d);
	}

	return c->leaveStackFrame();
}
*/





/*
	@doc: dcc_connection
	@type:
		generic
	@title:
		DCC negotiation and connection
	@short:
		Overview of the DCC internals
	@keyterms:
		DCC without IRC
	@body:
		[big]What is DCC?[/big][br]
		'DCC' stands for Direct Client Connection, it is used to exchange data
		directly between two IRC clients (with no IRC server in the middle).[br]
		DCC itself is not a well-defined protocol, but rather a set of
		subprotocols with (more or less) standardized rules.[br]
		Sub-protocols are also (historically) called [b]"DCC types"[/b]; this term often
		leads to confusion and it will become clear later.[br]
		Each subprotocol has two main parts: The [b]DCC negotiation[/b] and the [b]DCC transfer[/b].[br]
		The [b]DCC negotiation[/b] part is used to request the [b]DCC transfer[/b] and define its necessary parameters,[br]
		while the [b]DCC transfer[/b] part is the real data transfer between clients.[br]
		The [b]DCC negotiation[/b] requires a third entity that routes the negotiation data between clients,
		this is usually an IRC server.[br]
		[br]
		[big]DCC Negotiation[/big][br]
		This part of the protocol is the most tricky and difficult one, and is different for almost every DCC subprotocol.[br]
		The "constant" scenario of the negotiation is more or less the following:[br]
		There are two IRC clients connected to the same IRC network and they want to exchange some data in
		a direct client connection.[br]
		Each client knows the other by nickname only (and eventually by the host displayed by the IRC server,
		but this cannot be trusted for several reasons), and can send text messages to each other by using the
		IRC network as data channel.[br]
		To initiate a direct client connection, one of the clients must start listening on some port (this is called [b]passive client[/b])
		and the other must connect to that port on the first client's machine (this is the [b]active client[/b]).[br]
		Both clients must agree on who is the passive and who is the active client. The active client must also
		know the passive client's IP address and port (in order to be able to contact it).[br]
		Finally, both clients must agree on the transfer type that has to be initiated.[br]
		The negotiation exchanges these informations between clients by using IRC as channel and CTCP messages
		as encoding method.[br]
		An example will make things clearer:[br]
		DCC Chat is the simplest (and most widely implemented) DCC subprotocol:
		it is used to exchange <cr><lf> separated text data between clients.[br]
		Assume that you want to establish a DCC Chat
		connection to 'Sarah' that is actually connected to your IRC network (so
		she/he is an IRC user just like you).
		All you have to do is type sth as "/dcc chat Sarah" in your IRC client.
		The client will setup a listening socket on a random port choosen
		usually by the kernel of your OS. In this case YOU are the [b]passive client[/b], and Sarah is the active one.[br]
		Once the socket is ready to accept connections,
		your client will send a [doc:ctcp_handling]CTCP message[/doc] to Sarah using the IRC connection (and protocol) as channel:[br]
		PRIVMSG Sarah :<0x01>DCC CHAT chat <ip_address> <port><0x01>[br]
		where <ip_address> is the address of the listening socket and <port>
		is the port that it has been bound to (these informations are obtained
		after the socket has been setup). Once Sarah has received the CTCP message,
		and agreed to connect, her (active) client will attempt to connect to the
		specified <ip_address> and <port> (eg. to your listening socket).[br]
		Once the connection has been established, it continues using the
		specific CHAT transfer protocol.[br]
		Some IRC clients allow modifications of this procedure:[br]
		First of all, the port to listen on can be specified by the user
		and not by the kernel; this is useful when the passive client
		is behind a firewall that "shades" some sets of ports.
		The ip address for the listening socket
		can be specified by the user as well (especially when the machine has more than one network interface).[br]
		A more challenging trick is to listen on a specified ip address and port
		and notify different ones to the remote user (eg, <ip_address> and <port>
		parameters of the CTCP message are not the ones that the client is listening on).
		This is especially useful with "transparent proxy" firewalls that
		often are not transparent enough to allow the DCC connections.
		(If you have one of these firewalls you know what I'm talking about,
		otherwise just read on). KVIrc allows to avoid the usage of a third entity
		for the protocol negotiation too.
		You can setup a listening socket on a specified port and ip address
		without notyfying anyone of this.
		You can also manually connect to a specified port and ip address without
		having been notified of a DCC request.[br][br][br]
		Is everything clear ?...I don't think so... my English is really too bad...
		[br]
		[big]DCC Transfer[/big][br]
		The DCC transfer part is different for every DCC subprotocol, but
		it always happens over a direct client to client TCP connection.[br]
		[br]
		[big]DCC Subprotocols[/big][br]
		There are two main standardized DCC subprotocols that are widely implemented in IRC clients:
		[b]DCC Chat[/b] and [b]DCC Send[/b].[br]
		DCC Chat is quite simple and the protocol is more or less completely defined.[br]
		DCC Send is a *real mess*, the original definition was not very flexible
		so many IRC clients tried to enchance both the negotiation and the transfer, leading
		often to incompatible implementations. (I can remember the Turbo File Transfer implemented
		by VIrc, the Send-Ahead enchancement implemented in many clients, the RESUME facility...)[br]
		Many clients introduced new DCC subprotocols with non-standard implementations,
		leading again to client incompatibility.[br]
		Some of the notable subprotocols are DCC Voice, DCC Draw, DCC Whiteboard...[br]
		[br]
		[big]DCC Chat[/big][br]
		This is the simplest and most standardized DCC subprotocol. Almost every IRC client implements it.[br]
		It is used to exchange lines of text between the two clients.[br]
		The negotiation is quite simple, we assume that Client A wants to establish a DCC Chat connection to Client B.
		Client A sets up a listening socket and retrieves its adress (ip address and port).[br]
		Once the socket is ready Client A sends a CTCP request to B, in the following form:[br]
		[b]DCC CHAT chat <ipaddress> <port>[/b][br]
		Where <ipaddress> is a string representing an positive integer that is the A socket's IP address
		in network byte order, and where <port> is a string representing an positive integer that is the
		A socket's port.[br]
		The original purpose of the second "chat" string in the CTCP request is quite obscure, it was probably
		introduced to have the <ipaddress> as second parameter, as in the DCC Send subprotocol.[br]
		Client B receives the CTCP, parses it, eventually asks the user for permission and connects
		to the specified ip address and port.
		The transfer protocol is quite simple, both clients can send text lines separated by <cr><lf> pairs.[br]
		Some clients use only <lf> as line terminator so the general idea is that one of <cr> <cr><lf> or <lf>
		can be used as line terminator.[br]
		As extension to the protocol, KVIrc allows <ipaddress> to be an IPv6 address in the standard hexadecimal
		notation, the connection will be made over the IPv6 protocol in this case (obviously if both clients
		support this feature).[br]
		(It is not clear why the original DCC specification used the unsigned int format instead of a
		standard string representation of the IP address... missing inet_aton() function on the target system?).[br]
		KVIrc adds the Secure Sockets Layer to the DCC Chat protocol. In this case the negotiation string becomes:[br]
		[b]DCC SCHAT chat <ipaddress> <port>[/b][br]
		where "SCHAT" stands for Secure CHAT.[br] The external protocol is exactly the same but is built on top of a Secure Sockets Layer
		implementation (specifically OpenSSL). The connection will be encrypted with a private key algorithm after
		a public key handshake.[br]
		[br]
		[big]DCC Send[/big][br]
		DCC Send is another standard subprotocol. Most clients implement this as well, many have tried
		to enchance it.[br]
		The basic DCC Send protocol allows transferring a file from the requesting client to the receiving client.[br]
		The requesting client (the one that sends the file) is always passive and the receiving client is always active.[br]
		This is a huge protocol limitation since firewalled clients are often unable to accept incoming connections.[br]
		The negotiation protocol is more complex than DCC Chat; we assume that Client A wants to send the file F to Client B.[br]
		Client A sets up a listening socket and retrieves its ip address and port.[br]
		Client A sends a CTCP request to Client B in the following form:[br]
		[b]DCC SEND <filename> <ipaddress> <port> <filesize>[/b][br]
		<ipaddress> and <port> have the same semantics as in the DCC Chat subprotocol.[br]
		<filename> is the name (without path!) of the file to be sent, and <filesize> is (yeah), the file size.[br]
		Client B receives the CTCP, parses it, eventually asks the user for confirmation and connects to the
		specified ip address and port; the transfer then begins.[br]
		Client A sends blocks of data (usually 1-2 KB) and at every block awaits confirmation from the Client B,
		that when receiving a block should reply 4 bytes containing an positive number specifying the total size
		of the file received up to that moment.[br]
		The transmission closes when the last acknowledge is received by Client A.[br]
		The acknowledges were meant to include some sort of coherency check in the transmission, but in fact
		no client can "recover" from an acknowledge error/desync, all of them just close the connection declaring the
		transfer as failed (the situation is even worse in fact, often acknowledge errors aren't even detected!).[br]
		Since the packet-acknowledge round trip eats a lot of time, many clients included
		the "send-ahead" feature; the Client A does NOT wait for the acknowledge of the first packet before sending the second one.[br]
		The acknowledges are still sent, but just a reverse independent stream.[br] This makes the DCC Send considerably faster.[br]
		Since the acknowledge stream has non-zero bandwidth usage, no client can recover from an acknowledge error and
		having them as an independant stream is more or less like having no acknowledges, the "Turbo" ( :) ) extension has been added:
		Client B will send no acknowledges and will just close the connection when he has received all the expected data.[br]
		This makes the DCC Send as fast as FTP transfers.[br]
		The "Turbo" extension is specified during the negotiation phase, bu using TSEND as DCC message type (instead of SEND).[br]
		The "Turbo" extension is not widely implemented.[br]
		Later implementations have added the support for resuming interrupted DCC Send transfers:[br]
		Client A sets up the socket and sends the CTCP request as before.[br]
		If Client B discovers that the file has been partially received in a previous DCC Send session it sends
		a resume request in the following form:[br]
		[b]DCC RESUME <filename> <port> <resume position>[/b][br]
		Where <port> is the <port> sent in the DCC SEND request and <resume position> is the position in the file
		from where the transfer should start.[br]
		Cilent A receives the request, parses it and eventually replies with:[br]
		[b]DCC ACCEPT <filename> <port> <resume position>[/b][br]
		Client B receives the ACCEPT message, connects to Client A and the transfer initiates as before.[br]
		The "Send-ahead" and "Turbo" extensions can obviously be used also in this case (But 'T' is NOT prepended to the RESUME and ACCEPT messages).[br]
		The IPv6 extension can be used also in this subprotocol, so <ipaddress> can be also an IPv6 address in hexadecimal notation.[br]
		KVIrc introduces the SSL extension also to DCC Send. The protocol remains the same again but it is built on top of
		a Secure Sockets Layer implementation just like DCC Chat.[br]
		With SSL the negotiation string becomes:[br]
		[b]DCC SSEND <filename> <ipaddress> <port> <filesize>[/b][br]
		where "SSEND" stands for Secure SEND.[br]
		The "turbo" extension can be combined with the SSL extension too. In this case the second parameter
		of the negotiation string must be "TSSEND" or "STSEND".[br]
		[br]
		[big]DCC Recv[/big][br]
		DCC Recv is the counterpart of DCC Send. This is a KVIrc extension and is not standard yet.[br]
		The purpose of this subprotocol will not be immediately clear, but read on for an explanation.[br]
		It is used to request a file from another client; we assume that Client A knows that Client B has
		a specific file and is able/wants to send it.[br]
		Client A sets up a listening socket, retrieves its address and port and then
		sends a CTCP request to Client B in the following form:[br]
		[b]DCC RECV <filename> <ipaddress> <port> <resume position>[/b][br]
		where <filename> is the name of the requested file without path, <ipaddress> and <port> have the usual meaning and <resume position>
		is the position from that the transfer should start from.[br]
		<ipaddress> can be an IPv6 address as well.[br]
		Client B receives the CTCP message, parses it, looks for the file to send (in some unspecified way)
		and connects to the specified ip address and port. The transfer then begins just as in the DCC send, but in the inverse way:
		Client B sends blocks of data to Client A and Client B sends back acknowledges.[br]
		This subprotocol is useful in transferring data from clients that are behind a firewall and are not able to accept
		incoming connections (this is not possible with a normal DCC Send). In this case the client that receives
		the file is passive and the client that sends it is active (as opposite to DCC Send).[br]
		The "Send ahead" extension can be used also in this case and the "Turbo" extension is activated by prepending a 'T' to the
		DCC message, "TRECV" instead of "RECV". The SSL extension is activated by prepending an 'S' to the
		DCC message, "SRECV", "STRECV" or "TSRECV".[br]
		This subprotocol has an implicit resume capability and thus has no need for RESUME and ACCEPT messages.[br]
		DCC Recv requires the initiating (passive) client to know that the file to be transferred is avaiable on the B's side
		and probably also know the file size. This subprotocol does not specify how this information is obtained, but it
		will become clear soon that it can be obtained either manually (User B can simply tell the info to User A),
		or automatically (as in the DCC Rsend subprotocol (keep reading)).[br]
		[br]
		[big]DCC RSend[/big][br]
		DCC RSend stands for Reverse Send. This is a KVIrc extension to the SEND protocol to allow firewalled clients
		to send files.[br] In fact, this is a "half" subprotocol, since it defines only a part of the DCC negotiation;
		the transfer is defined by another subprotocol (and specifically bu DCC Recv).[br]
		The requesting client (the one that sends the file) is active and the receiving client is passive.[br]
		Assume that Client A wants to send a file to Client B and that Client A cannot accept incoming connections.[br]
		Client A sends a CTCP request to Client B in the following form:[br]
		[b]DCC RSEND <filename> <filesize>[/b][br]
		Client B receives the request, parses it, eventually asks the user for confirmation, sets up a listening socket, retrieves
		its ip address and port and switches to the DCC Recv subprotocol by effectively sending the following CTCP message:[br]
		[b]DCC RECV <filename> <ipaddress> <port> <resume position>[/b][br]
		The rest of the transfer is defined by the DCC Recv subprotocol.[br]
		The "Turbo" extension is again activated by prepending a 'T' to the RSEND string, so the initial CTCP will become:[br]
		[b]DCC TRSEND <filename> <filesize>[/b][br]
		The "SSL" extension is also activated by prepending an 'S' to the RSEND string. It can be again combined
		with the "turbo" extension. The negotiation parameter becomes then "SRSEND","TSRSEND" or "STRSEND".[br]
		Easy, no ? :)[br]
		[br]
		[big]DCC Get[/big][br]
		This is again a "half" subprotocol in fact since it defines only a part of the negotiation for file transfers.[br]
		It is also NON standard, since actually no client except KVIrc implements it (AFAIK).[br]
		DCC Get is used to request a file from a remote client. Assume that Client A wants to request a file from Client B
		(and assume that Client A knows that B has that file and wants to send it).[br]
		Client A sends a CTCP message to Client B in the following form:[br]
		[b]DCC GET <filename>[/b][br]
		Where <filename> is a name of a file without path.[br]
		Client B receives the message, parses it, looks for an association of the <filename> to a real filesystem file
		and starts one of the two DCC File transfer subprotocols, DCC Send or DCC RSend.[br]
		Client B should prefer the DCC Send method and choose DCC RSend only if it is not able to accept incoming connections.[br]
		This subprotocol can be used by firewalled clients that can't accept connections but still want to request a file
		from another client, this one can fail only if both clients are firewalled (in this case no DCC transfer is possible at all).[br]
		This subprotocol also does not need to "magically" know the file size, the size definition
		is found in the subprotocol that the remote client will choose.[br]
		The association of <filename> with a real file on the B's machine is not explicitly defined by the subprotocol;
		KVIrc uses an internal "file-offer" table with a list of files that are available for download.[br]
		The "turbo" and "SSL" extensions are activated as usual, "TGET", "SGET", "TSGET" and "STGET" are supported.[br]
		[br]
		[big]DCC File Transfer[/big][br]
		DCC Send: Send a file, sender is passive, receiver is active (not good for firewalled senders)[br]
		DCC Recv: Receive a file, sender is active, receiver is passive (not good for firewalled receivers)[br]
		DCC RSend: Send a file, sender is active, receiver is passive (not good for firewalled receivers)[br]
		DCC Get: Receive a file, sender is passive if not firewalled, receiver active if sender not firewalled (will fail only if both are firewalled)[br]
		The "turbo" extension disables the stream of acknowledges and is activated by prepending the 'T' character to the DCC subprotocol name[br]
		The "SSL" extension causes a Secure Socket Layer to be used and is activated by prepending the 'S' character to the DCC subprotocol name[br]
		[br]
		[big]DCC Voice[/big][br]
		DCC Voice is a KVIrc extension (there is a Windows client called VIrc that implements such
		a protocol, but it is incompatible with KVIrc).[br]
		DCC Voice allows audio level communication between two clients, the audio stream is compressed
		with a specified codec.[br]
		KVIrc currently supports the ADPCM (core support) and the GSM codec (if the libgsm is available on the target system).[br]
		[b]TODO: Finish the DCC Voice doc :)[/b]
		[big]More tricks[/big][br]
		KVIrc supports another "hack" to the DCC negotiation, it recognizes "XDCC" as
		a DCC negotiation CTCP parameter.[br]
		This can be used to circumvent limitations of some IRC clients (read mIRC) that will not allow
		you to send a /DCC GET since it is an unrecognized DCC type.[br]
		"XDCC" has exactly the same meaning as "DCC" (at least in KVIrc).[br]
*/

static KviDccDescriptor * dcc_kvs_find_dcc_descriptor(const kvs_uint_t &uId,KviKvsModuleRunTimeCall * c,bool bWarn = true)
{
	KviDccDescriptor * dcc = 0;
	if(uId == 0)
	{
		if(c->window()->inherits("KviDccWindow"))
		{
			dcc = ((KviDccWindow *)(c->window()))->descriptor();
		}
		if((!dcc) && bWarn)
			c->warning(__tr2qs_ctx("The current window has no associated DCC session","dcc"));
		return dcc;
	}
	dcc = KviDccDescriptor::find(uId);
	if((!dcc) && bWarn)
		c->warning(__tr2qs_ctx("The specified parameter is not a valid DCC identifier","dcc"));
	return dcc;
}


/*
	@doc: dcc.abort
	@type:
		command
	@title:
		dcc.abort
	@short:
		Aborts a dcc session
	@syntax:
		dcc.abort [-q] [dcc_id:uint]
	@description:
		Terminates the Direct Client Connection specified by <dcc_id>.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function doesn't abort anything and prints a warning unless the -q switch is used.[br]
		If <dcc_id> refers to a file transfer then it the transfer is simply
		terminated. If <dcc_id> refers to a dcc chat then the result
		is equivalent to closing the related window.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
	@examples:
*/

static bool dcc_kvs_cmd_abort(KviKvsModuleCommandCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c,!c->switches()->find('q',"quiet"));

	if(dcc)
	{
		if(dcc->transfer())dcc->transfer()->abort();
		else if(dcc->window())dcc->window()->close();
	}

	return true;
}

/*
	@doc: dcc.setBandwidthLimit
	@type:
		command
	@title:
		dcc.setBandwidthLimit
	@short:
		Set the bandwidthlimit of a dcc.send session.
	@syntax:
		dcc.setBandwidthLimit [-q] [dcc_id:uint]
	@description:
		Terminates the Direct Client Connection specified by <dcc_id>.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function  prints a warning unless the -q switch is used.[br]
		If <dcc_id> does not refers to a file transfer a warning will be printing unless the -q switch is used.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
	@examples:
*/
static bool dcc_kvs_cmd_setBandwidthLimit(KviKvsModuleCommandCall * c)
{
	kvs_uint_t uDccId,uVal;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("limit_value",KVS_PT_UINT,0,uVal)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c,!c->switches()->find('q',"quiet"));
	if(dcc)
	{
		if (dcc->transfer())dcc->transfer()->setBandwidthLimit(uVal);
		else if (!c->switches()->find('q',"quiet"))c->warning(__tr2qs_ctx("This DCC session is not a DCC transfer session","dcc"));
	}
	return true;
}

/*
	@doc: dcc.protocol
	@type:
		function
	@title:
		$dcc.protocol
	@short:
		Returns the protocol of the specified DCC
	@syntax:
		<string> $dcc.protocol
		<string> $dcc.protocol(<dcc_id:uint>)
	@description:
		Returns the string describing the protocol of the
		Direct Client Connection specified by <dcc_id>.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_protocol(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->protocol());
	return true;
}


/*
	@doc: dcc.connectionType
	@type:
		function
	@title:
		$dcc.connectionType
	@short:
		Returns the connection type of the specified DCC
	@syntax:
		<string> $dcc.connectionType
		<string> $dcc.connectionType(<dcc_id:uint>)
	@description:
		Returns the connection type of the specified DCC.[br]
		Returns the string "ACTIVE" for active DCC connections
		and the string "PASSIVE" for passive DCC connections.
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_connectionType(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->isActive() ? "ACTIVE" : "PASSIVE");
	return true;
}


/*
	@doc: dcc.isFileTransfer
	@type:
		function
	@title:
		$dcc.isFileTransfer
	@short:
		Checks if a DCC is a file transfer
	@syntax:
		<boolean> $dcc.isFileTransfer
		<boolean> $dcc.isFileTransfer(<dcc_id:uint>)
	@description:
		Returns 1 if the specified Direct Client Connection
		is a file transfer and 0 otherwise.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this and returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_isFileTransfer(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c,false);

	if(dcc)c->returnValue()->setBoolean(dcc->isFileTransfer());
	return true;
}


/*
	@doc: dcc.isFileUpload
	@type:
		function
	@title:
		$dcc.isFileUpload
	@short:
		Checks if a DCC is an upload file transfer
	@syntax:
		<boolean> $dcc.isFileUpload
		<boolean> $dcc.isFileUpload(<dcc_id:uint>)
	@description:
		Returns 1 if the specified Direct Client Connection
		is an upload file transfer and 0 otherwise.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_isFileUpload(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setBoolean(dcc->isFileUpload());
	return true;
}


/*
	@doc: dcc.isFileDownload
	@type:
		function
	@title:
		$dcc.isFileDownload
	@short:
		Checks if a DCC is a download file transfer
	@syntax:
		<boolean> $dcc.isFileDownload
		<boolean> $dcc.isFileDownload(<dcc_id:uint>)
	@description:
		Returns 1 if the specified Direct Client Connection
		is a download file transfer and 0 otherwise.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_isFileDownload(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setBoolean(dcc->isFileDownload());
	return true;
}


/*
	@doc: dcc.localNick
	@type:
		function
	@title:
		$dcc.localNick
	@short:
		Returns the local nickname associated to the specified DCC
	@syntax:
		<string> $dcc.localNick
		<string> $dcc.localNick(<dcc_id:uint>)
	@description:
		Returns the local nickname associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_localNick(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localNick());
	return true;
}


/*
	@doc: dcc.localUser
	@type:
		function
	@title:
		$dcc.localUser
	@short:
		Returns the local username associated to the specified DCC
	@syntax:
		<string> $dcc.localUser
		<string> $dcc.localUser(<dcc_id:uint>)
	@description:
		Returns the local username associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_localUser(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localUser());
	return true;
}

/*
	@doc: dcc.localHost
	@type:
		function
	@title:
		$dcc.localHost
	@short:
		Returns the local hostname associated to the specified DCC
	@syntax:
		<string> $dcc.localHost
		<string> $dcc.localHost(<dcc_id:uint>)
	@description:
		Returns the local hostname associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_localHost(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localHost());
	return true;
}


/*
	@doc: dcc.localIp
	@type:
		function
	@title:
		$dcc.localIp
	@short:
		Returns the local ip address associated to the specified DCC
	@syntax:
		<string> $dcc.localIp
		<string> $dcc.localIp(<dcc_id:uint>)
	@description:
		Returns the local ip address associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_localIp(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localIp());
	return true;
}


/*
	@doc: dcc.localPort
	@type:
		function
	@title:
		$dcc.localPort
	@short:
		Returns the local port associated to the specified DCC
	@syntax:
		<integer> $dcc.localPort
		<integer> $dcc.localPort(<dcc_id:uint>)
	@description:
		Returns the local port associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_localPort(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localPort());
	return true;
}


/*
	@doc: dcc.localFileName
	@type:
		function
	@title:
		$dcc.localFileName
	@short:
		Returns the local file name associated to the specified DCC
	@syntax:
		<string> $dcc.localFileName(<dcc_id:uint>)
	@description:
		Returns the local file name associated to the specified DCC.[br]
		If <dcc_id> does not identify a file transfer DCC then this
		function returns an empty string.
		If <dcc_id> is not a valid Direct Client Connection identifier
		then this function prints a warning and returns an empty string.
*/

static bool dcc_kvs_fnc_localFileName(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localFileName());
	return true;
}


/*
	@doc: dcc.localFileSize
	@type:
		function
	@title:
		$dcc.localFileSize
	@short:
		Returns the local file size associated to the specified DCC
	@syntax:
		<integer> $dcc.localFileSize(<dcc_id:uint>)
	@description:
		Returns the local file size associated to the specified DCC.[br]
		If <dcc_id> does not identify a file transfer DCC then this
		function returns '0'[br]
		If <dcc_id> is not a valid Direct Client Connection identifier
		then this function prints a warning and returns '0'[br]
		In upload transfers the local file size rappresents the
		total size of the file to be transferred. In download transfers
		the local file size is non zero only if the transfer has to resume
		a file already existing on the local disk and it rappresents the
		size of that file (and thus the offset that the transfer started on).
*/

static bool dcc_kvs_fnc_localFileSize(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->localFileSize().isEmpty() ? TQString("0") : dcc->localFileSize());
	return true;
}


/*
	@doc: dcc.remoteNick
	@type:
		function
	@title:
		$dcc.remoteNick
	@short:
		Returns the remote nickname associated to the specified DCC
	@syntax:
		<string> $dcc.remoteNick
		<string> $dcc.remoteNick(<dcc_id:uint>)
	@description:
		Returns the remote nickname associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_remoteNick(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteNick());
	return true;
}


/*
	@doc: dcc.remoteUser
	@type:
		function
	@title:
		$dcc.remoteUser
	@short:
		Returns the remote username associated to the specified DCC
	@syntax:
		<string> $dcc.remoteUser
		<string> $dcc.remoteUser(<dcc_id:uint>)
	@description:
		Returns the remote username associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_remoteUser(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteUser());
	return true;
}

/*
	@doc: dcc.remoteHost
	@type:
		function
	@title:
		$dcc.remoteHost
	@short:
		Returns the remote hostname associated to the specified DCC
	@syntax:
		<string> $dcc.remoteHost
		<string> $dcc.remoteHost(<dcc_id:uint>)
	@description:
		Returns the remote hostname associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_remoteHost(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteHost());
	return true;
}

/*
	@doc: dcc.remoteIp
	@type:
		function
	@title:
		$dcc.remoteIp
	@short:
		Returns the remote ip address associated to the specified DCC
	@syntax:
		<string> $dcc.remoteIp
		<string> $dcc.remoteIp(<dcc_id:uint>)
	@description:
		Returns the remote ip address associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_remoteIp(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteIp());
	return true;
}


/*
	@doc: dcc.remotePort
	@type:
		function
	@title:
		$dcc.remotePort
	@short:
		Returns the remote port associated to the specified DCC
	@syntax:
		<integer> $dcc.remotePort
		<integer> $dcc.remotePort(<dcc_id:uint>)
	@description:
		Returns the remote port associated to the specified DCC.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_remotePort(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remotePort());
	return true;
}


/*
	@doc: dcc.remoteFileName
	@type:
		function
	@title:
		$dcc.remoteFileName
	@short:
		Returns the remote file name associated to the specified DCC
	@syntax:
		<string> $dcc.remoteFileName(<dcc_id:uint>)
	@description:
		Returns the remote file name associated to the specified DCC.[br]
		If <dcc_id> does not identify a file transfer DCC then this
		function returns an empty string.
		If <dcc_id> is not a valid Direct Client Connection identifier
		then this function prints a warning and returns an empty string.
*/

static bool dcc_kvs_fnc_remoteFileName(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteFileName());
	return true;
}


/*
	@doc: dcc.remoteFileSize
	@type:
		function
	@title:
		$dcc.remoteFileSize
	@short:
		Returns the remote file size associated to the specified DCC
	@syntax:
		<integer> $dcc.remoteFileSize(<dcc_id:uint>)
	@description:
		Returns the remote file size associated to the specified DCC.[br]
		If <dcc_id> does not identify a file transfer DCC then this
		function returns '0'[br]
		If <dcc_id> is not a valid Direct Client Connection identifier
		then this function prints a warning and returns '0'[br]
		In download transfers the remote file size rappresents the
		total size of the file to be transferred (advertished by the remote end).[br]
		In upload transfers the remote file size is non zero only if the
		remote user has issued a resume request and is rappresents the requested offset
		in bytes from which the transfer has started.
*/

static bool dcc_kvs_fnc_remoteFileSize(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setString(dcc->remoteFileSize().isEmpty() ? TQString("0") : dcc->remoteFileSize());
	return true;
}


/*
	@doc: dcc.ircContext
	@type:
		function
	@title:
		$dcc.ircContext
	@short:
		Returns the ircContext from which this DCC has originated
	@syntax:
		<integer> $dcc.ircContext
		<integer> $dcc.ircContext(<dcc_id:uint>)
	@description:
		Returns the identifier of the irc context from which
		the specified DCC has been originated.[br]
		When the DCC is not originated from an IRC context
		then this function returns '0' : an invalid irc context id.
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_ircContext(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)c->returnValue()->setInteger(dcc->console()->context()->id());
	return true;
}



/*
	@doc: dcc.transferStatus
	@type:
		function
	@title:
		$dcc.transferStatus
	@short:
		Returns the current status of a dcc file transfer
	@syntax:
		<string> $dcc.transferStatus
		<string> $dcc.transferStatus(<dcc_id:uint>)
	@description:
		Returns the status in the specified DCC session.[br]
		The status is one of the strings "connecting", "transferring", "success" and "failure".
		"success" and "failure" are reported when the transfer is terminated.
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		If the DCC session does not refer to a file transfer then
		this function returns "".[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_transferStatus(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)
	{
		if(dcc->transfer())
		{
			TQString tmp;
			dcc->transfer()->fillStatusString(tmp);
			c->returnValue()->setString(tmp);
		}
	}
	return true;
}


/*
	@doc: dcc.transferredBytes
	@type:
		function
	@title:
		$dcc.transferredBytes
	@short:
		Returns the number of transferred bytes in a dcc file transfer
	@syntax:
		<integer> $dcc.transferredBytes
		<integer> $dcc.transferredBytes(<dcc_id:uint>)
	@description:
		Returns the number of transferred bytes in the specified DCC session.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		If the DCC session does not refer to a file transfer then
		this function returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_transferredBytes(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)
	{
		if(dcc->transfer())
		{
			c->returnValue()->setInteger(dcc->transfer()->transferredBytes());
		} else {
			c->returnValue()->setInteger(0);
		}
	}
	return true;
}



/*
	@doc: dcc.averageSpeed
	@type:
		function
	@title:
		$dcc.averageSpeed
	@short:
		Returns the average speed of a dcc file transfer
	@syntax:
		$dcc.averageSpeed
		$dcc.averageSpeed(<dcc_id>)
	@description:
		Returns the average speed (in bytes/sec) of the specified DCC session.[br]
		If <dcc_id> is omitted then the DCC Session associated
		to the current window is assumed.[br]
		If <dcc_id> is not a valid DCC session identifier (or it is omitted
		and the current window has no associated DCC session) then
		this function prints a warning and returns an empty sting.[br]
		If the DCC session does not refer to a file transfer then
		this function returns 0.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_averageSpeed(KviKvsModuleFunctionCall * c)
{
	kvs_uint_t uDccId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("dcc_id",KVS_PT_UINT,KVS_PF_OPTIONAL,uDccId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = dcc_kvs_find_dcc_descriptor(uDccId,c);

	if(dcc)
	{
		if(dcc->transfer())
		{
			c->returnValue()->setInteger(dcc->transfer()->averageSpeed());
		} else {
			c->returnValue()->setInteger(0);
		}
	}
	return true;
}



/*
	@doc: dcc.session
	@type:
		function
	@title:
		$dcc.session
	@short:
		Returns the DCC session identifier associated to a window
	@syntax:
		<uint> $dcc.session
		<uint> $dcc.session(<window_id>)
	@description:
		Returns the DCC session identifier associated to the DCC window specified
		by <window_id>. If <window_id> is omitted then the DCC session identifier
		associated to the current window is returned. If the specified window
		has no associated DCC session then a warning is printed and 0 is returned.[br]
*/

static bool dcc_kvs_fnc_session(KviKvsModuleFunctionCall * c)
{
	TQString szWinId;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("window_id",KVS_PT_STRING,KVS_PF_OPTIONAL,szWinId)
	KVSM_PARAMETERS_END(c)

	KviDccDescriptor * dcc = 0;
	if(szWinId.isEmpty())
	{
		if(c->window()->inherits("KviDccWindow"))
			dcc = ((KviDccWindow *)(c->window()))->descriptor();
		if(!dcc)
		{
			c->warning(__tr2qs_ctx("The current window has no associated DCC session","dcc"));
			c->returnValue()->setInteger(0);
		} else {
			c->returnValue()->setInteger(dcc->id());
		}
		return true;
	}

	KviWindow * pWnd = g_pApp->findWindow(szWinId);
	if(!pWnd)
	{
		c->warning(__tr2qs_ctx("The specified window identifier is not valid","dcc"));
		c->returnValue()->setInteger(0);
		return true;
	}

	if(pWnd->inherits("KviDccWindow"))
		dcc = ((KviDccWindow *)pWnd)->descriptor();
	if(!dcc)
	{
		c->warning(__tr2qs_ctx("The current window has no associated DCC session","dcc"));
		c->returnValue()->setInteger(0);
	} else {
		c->returnValue()->setInteger(dcc->id());
	}
	return true;
}


/*
	@doc: dcc.sessionList
	@type:
		function
	@title:
		$dcc.sessionList
	@short:
		List the existing dcc session identifiers
	@syntax:
		<array> $dcc.sessionList
		<array> $dcc.sessionList(<filter:string>)
	@description:
		The first form returns an array with all the currently existing dcc session
		identifiers. The second form returns an array with the session types specified
		in <filter> which may be a combination of the flags 'u' (for file upload), 
		'd' (for file download) and 'c' (for dcc chat). To select all the file transfers
		please use the combination 'ud'.[br]
		See the [module:dcc]dcc module[/module] documentation for more informations.[br]
*/

static bool dcc_kvs_fnc_sessionList(KviKvsModuleFunctionCall * c)
{
	TQString szFlags;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("filter",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags)
	KVSM_PARAMETERS_END(c)

	KviKvsArray * a = new KviKvsArray();
	c->returnValue()->setArray(a);

	KviPointerHashTable<int,KviDccDescriptor> * dict = KviDccDescriptor::descriptorDict();
	if(!dict)return true;

	KviPointerHashTableIterator<int,KviDccDescriptor> it(*dict);

	int idx = 0;
	
	if(szFlags.isEmpty())
	{
		// all
		while(KviDccDescriptor * dcc = it.current())
		{
			a->set(idx++,new KviKvsVariant((kvs_int_t)(dcc->id())));
			++it;
		}
	} else {
		bool bWantFileUploads = szFlags.find('u',false) != -1;
		bool bWantFileDownloads = szFlags.contains('d',false) != -1;
		bool bWantChats = szFlags.contains('c',false) != -1;

		while(KviDccDescriptor * dcc = it.current())
		{
			if((dcc->isFileUpload() && bWantFileUploads) ||
				(dcc->isFileDownload() && bWantFileDownloads) ||
				(dcc->isDccChat() && bWantChats))
			{
				a->set(idx++,new KviKvsVariant((kvs_int_t)(dcc->id())));
			}
			++it;
		}
	}

	return true;
}


/*
	@doc: dcc
	@type:
		module
	@short:
		Direct Client Connections
	@title:
		The DCC module
	@body:
		[big]Overview[/big][br]
		The DCC module handles the Direct Client Connection
		protocol layer and all it's sub-protocols.[br]
		The sub-protocols include the standard CHAT
		the standard SEND and its variants plus several
		KVIrc extensions like RECV,RSEND,GET and VOICE.[br]
		[br]
		[big]Initiating a DCC negotiation[/big][br]
		The following commands initiate a specific DCC session
		with a remote client:[br]
		[cmd]dcc.chat[/cmd][br]
		[cmd]dcc.send[/cmd][br]
		[cmd]dcc.rsend[/cmd][br]
		[cmd]dcc.recv[/cmd][br]
		[cmd]dcc.get[/cmd][br]
		[cmd]dcc.voice[/cmd][br]
		[br]
		[big]Handling the DCC events[/big][br]
		Each DCC session has an associated unique identifier (&lt;dcc_id&gt;).[br]
		You can interact with the session by using several commands
		and functions exported by this module and by passing the above session
		id as parameter.[br]
		The session related commands and functions are the following:[br]
		[fnc]$dcc.sessionList[/fnc][br]
		[fnc]$dcc.protocol[/fnc][br]
		[fnc]$dcc.connectionType[/fnc][br]
		[fnc]$dcc.transferStatus[/fnc][br]
		[fnc]$dcc.isFileTransfer[/fnc][br]
		[fnc]$dcc.isFileUpload[/fnc][br]
		[fnc]$dcc.isFileDownload[/fnc][br]
		[fnc]$dcc.localNick[/fnc][br]
		[fnc]$dcc.localUser[/fnc][br]
		[fnc]$dcc.localHost[/fnc][br]
		[fnc]$dcc.localIp[/fnc][br]
		[fnc]$dcc.localPort[/fnc][br]
		[fnc]$dcc.localFileName[/fnc][br]
		[fnc]$dcc.localFileSize[/fnc][br]
		[fnc]$dcc.remoteNick[/fnc][br]
		[fnc]$dcc.remoteUser[/fnc][br]
		[fnc]$dcc.remoteHost[/fnc][br]
		[fnc]$dcc.remoteIp[/fnc][br]
		[fnc]$dcc.remotePort[/fnc][br]
		[fnc]$dcc.remoteFileName[/fnc][br]
		[fnc]$dcc.remoteFileSize[/fnc][br]
		[fnc]$dcc.ircContext[/fnc][br]
		[fnc]$dcc.session[/fnc][br]
*/


static bool dcc_module_init(KviModule * m)
{
	g_pDccBroker = new KviDccBroker();

	KVSM_REGISTER_SIMPLE_COMMAND(m,"send",dcc_kvs_cmd_send);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"chat",dcc_kvs_cmd_chat);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"voice",dcc_kvs_cmd_voice);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"recv",dcc_kvs_cmd_recv);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"rsend",dcc_kvs_cmd_rsend);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"get",dcc_kvs_cmd_get);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"abort",dcc_kvs_cmd_abort);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"setBandwidthLimit",dcc_kvs_cmd_setBandwidthLimit);


	// FIXME: file upload / download state ?

	KVSM_REGISTER_FUNCTION(m,"transferStatus",dcc_kvs_fnc_transferStatus);
	KVSM_REGISTER_FUNCTION(m,"protocol",dcc_kvs_fnc_protocol);
	KVSM_REGISTER_FUNCTION(m,"connectionType",dcc_kvs_fnc_connectionType);
	KVSM_REGISTER_FUNCTION(m,"isFileTransfer",dcc_kvs_fnc_isFileTransfer);
	KVSM_REGISTER_FUNCTION(m,"isFileUpload",dcc_kvs_fnc_isFileUpload);
	KVSM_REGISTER_FUNCTION(m,"isFileDownload",dcc_kvs_fnc_isFileDownload);
	KVSM_REGISTER_FUNCTION(m,"localNick",dcc_kvs_fnc_localNick);
	KVSM_REGISTER_FUNCTION(m,"localUser",dcc_kvs_fnc_localUser);
	KVSM_REGISTER_FUNCTION(m,"localHost",dcc_kvs_fnc_localHost);
	KVSM_REGISTER_FUNCTION(m,"localIp",dcc_kvs_fnc_localIp);
	KVSM_REGISTER_FUNCTION(m,"localPort",dcc_kvs_fnc_localPort);
	KVSM_REGISTER_FUNCTION(m,"localFileName",dcc_kvs_fnc_localFileName);
	KVSM_REGISTER_FUNCTION(m,"localFileSize",dcc_kvs_fnc_localFileSize);
	KVSM_REGISTER_FUNCTION(m,"remoteNick",dcc_kvs_fnc_remoteNick);
	KVSM_REGISTER_FUNCTION(m,"remoteUser",dcc_kvs_fnc_remoteUser);
	KVSM_REGISTER_FUNCTION(m,"remoteHost",dcc_kvs_fnc_remoteHost);
	KVSM_REGISTER_FUNCTION(m,"remoteIp",dcc_kvs_fnc_remoteIp);
	KVSM_REGISTER_FUNCTION(m,"remotePort",dcc_kvs_fnc_remotePort);
	KVSM_REGISTER_FUNCTION(m,"remoteFileName",dcc_kvs_fnc_remoteFileName);
	KVSM_REGISTER_FUNCTION(m,"remoteFileSize",dcc_kvs_fnc_remoteFileSize);
	KVSM_REGISTER_FUNCTION(m,"averageSpeed",dcc_kvs_fnc_averageSpeed);
	KVSM_REGISTER_FUNCTION(m,"transferredBytes",dcc_kvs_fnc_transferredBytes);
	KVSM_REGISTER_FUNCTION(m,"ircContext",dcc_kvs_fnc_ircContext);
	KVSM_REGISTER_FUNCTION(m,"session",dcc_kvs_fnc_session);
	KVSM_REGISTER_FUNCTION(m,"sessionList",dcc_kvs_fnc_sessionList);

	return true;
}

static bool dcc_module_cleanup(KviModule *m)
{
	delete g_pDccBroker;
	g_pDccBroker = 0;
#ifdef COMPILE_USE_GSM
	kvi_gsm_codec_done();
#endif

	return true;
}

static bool dcc_module_can_unload(KviModule *m)
{
	return g_pDccBroker ? g_pDccBroker->canUnload() : true;
}

KVIRC_MODULE(
	"Dcc",
	"1.0.0",
	"Copyright (C) 2000-2004:\n" \
	"  Szymon Stefanek (pragma at kvirc dot net)\n",
	"DCC extension for KVIrc\n",
	dcc_module_init,
	dcc_module_can_unload,
	0,
	dcc_module_cleanup
)