summaryrefslogtreecommitdiffstats
path: root/ksirc/dsirc
blob: d73b6a501c1c92d501510ca2405d532d8887ccf9 (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
#!/usr/bin/perl

# dsirc: dumb-mode small irc client in perl
# by orabidoo <roger.espel.llima@pobox.com>
#
# Copyright (C) 1995-1997 Roger Espel Llima
#
# for a full-screen termcap interface, use this with ssfe
#
# use: dsirc [options] [nick [server[:port[:password]]]]
# options are:
#	-p = specify port number
#	-i = specify IRCNAME
#	-n = specify nickname (quite useless as an option)
#	-s = specify server (quite useless as an option)
#	-l = specify file to be loaded instead of ~/.sircrc.pl
#	-L = specify file to be loaded instead of ~/.sircrc
#	-H = specify virtual host to bind to
#	-q = don't load ~/.sircrc or ~/.sircrc.pl
#	-Q = don't load system sircrc or sircrc.pl
#	-R = run in restricted (secure) mode
#	-r = raw mode (no control-char filtering)
#	-8 = 8-bit mode
#       -S = connect using SSL

# 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. See the file LICENSE for more details.
#
# If you make improvements to sirc, please send me the modifications
# (context diffs appreciated) and they might make it to the next release.
#
# For bug reports, comments, questions, email roger.espel.llima@pobox.com
#
# You can always find the latest version of sirc at the following URL:
# http://www.eleves.ens.fr:8080/home/espel/sirc/sirc.html

# Concerning the use in ksirc you'll find a mail from the author below:
#
# Subject:   Re: dsirc in kde
#    Date:   Thu, 7 Sep 2000 13:16:30 -0400
#    From:   Roger Espel Llima <espel@iagora.net>
#      To:   Harri Porten <porten@kde.org>
#
# On Thu, Sep 07, 2000 at 07:12:33PM +0200, Harri Porten wrote:
# [....]
# > Ok. Your dsirc script is used in ksirc. I haven't checked how it is
# > invoked and what legal ramifications that would have licensing wise but
# > I would like to "officially" ask you anyway:
# > 
# > Do you have oppose to your code being used this way in the past and in
# > the future ? Do you "forgive" us [for use in prev. versions of KDE] ? :)
#
# I "officially" find it perfectly fine that dsirc is used in KDE.  I knew
# of ksirc when it started, and found it very flattering that someone
# would write 200k of C++ to interface with my 62k of perl :=)

$version='2.212';
$date='21 Sep 2024';
$add_ons='';

$libdir=$ENV{"SIRCLIB"} || ".";
push(@INC, $libdir, $ENV{"HOME"});
@loadpath=($ENV{"HOME"}."/.sirc", $libdir, ".");
$ENV{"SIRCWAIT"} or $ready=1;

$|=1;

$publicAway = 1;

use Getopt::Std;

if ($] >= 5 && (eval "use Socket;", $@ eq '')) {
    $sock6 = eval ("require Socket6;") and eval("use Socket6;");
} elsif (-f "$libdir/sircsock.ph") {
  do "$libdir/sircsock.ph";
} elsif (-f $ENV{'HOME'}."/sircsock.ph") {
  do $ENV{'HOME'}."/sircsock.ph";
} elsif (!eval "require 'sys/socket.ph';") {
    print "\n\n\
Your perl installation is wrong somewhere, the sys/socket.ph include file
couldn't be found. Have you even bothered to run 'install'?\n";
    exit;
}

$hasPOSIX = 1;
eval "use POSIX;";
if($@) {
  $hasPOSIX = 0;
  print "*** No Posix library, falling back to blocking IO (dcc will suck)\n";
}
    

getopts('n:s:p:u:i:l:L:H:rqQR78S', \my %opts);

%set=("LOGFILE", "", "LOG", "off", "PRINTUH", "none", "PRINTCHAN", "off",
 	"LOCALHOST", "", "CTCP", "noflood", "SENDAHEAD", 4096,
	"USERINFO", "", "FINGER", "", "IRCNAME", "", "EIGHT_BIT", "on",
	"LOADPATH", join(":", @loadpath), "CTRL_T", "/next");

$raw_mode=$opts{r} || (!-t STDOUT);
$ansi=!$raw_mode && $ENV{"TERM"} =~ /^vt|^xterm|^ansi/i;
$server=$opts{s} || $ARGV[1] || $ENV{"SIRCSERVER"} || $ENV{"IRCSERVER"} ||
                  "irc.primenet.com";
$port0=$opts{p} || $ENV{"SIRCPORT"} || $ENV{"IRCPORT"} || 6667;
$username=$opts{u} || $ENV{"SIRCUSER"} || $ENV{"IRCUSER"} || (getpwuid($<))[0] ||
                $ENV{"USER"} || "blah";
$set{"IRCNAME"}=$opts{i} || $ENV{"SIRCNAME"} || $ENV{"IRCNAME"} || "sirc user";
$nick=$opts{n} || $ARGV[0] || $ENV{"SIRCNICK"} || $ENV{"IRCNICK"} || $username;
$set{"FINGER"}=$ENV{"IRCFINGER"} || "keep your fingers to yourself";
$set{"USERINFO"}=$ENV{"USERINFO"} || "yep, I'm a user";
if ($server =~ /^\[([^\]]+)\]:([0-9]*):?(.*)$/
    or $server =~ /^([^:]+):([0-9]*):?(.*)$/)
{
	($server, $port, $pass)=($1, $2, $3);
}
$port || ($port=$port0);
$server0=$server1=$server;
$port0=$port1=$port;
$pass0=$pass1=$pass;
$initfile=$opts{l} || $ENV{"SIRCRCPL"} || $ENV{'HOME'}."/.sircrc.pl" 
  if $opts{l} || !$opts{q};
$sysinit=$libdir."/sircrc.pl" if $libdir ne '.' && !$opts{Q};
$rcfile=$opts{L} || $ENV{"SIRCRC"} || $ENV{'HOME'}."/.sircrc" 
  if $opts{L} || !$opts{q};
$sysrc=$libdir."/sircrc" if $libdir ne '.' && !$opts{Q};
$set{"LOGFILE"}=$logfile=$ENV{'HOME'}."/sirc.log";
$opts{8} || ($set{"EIGHT_BIT"}="off");
$restrict=$opts{R};
$set{"LOCALHOST"}=$opts{H} || $ENV{"SIRCHOST"} || $ENV{"IRCHOST"} ||
		$ENV{"LOCALHOST"} || "";
$SSL=$opts{S};

@ARGV=();  # ignore any more arguments

if (open(H, "$libdir/sirc.help") || ((-f "$libdir/sirc.help.gz") &&
     open(H, "gzip -cd $libdir/sirc.help.gz |"))) {
  @help=<H>;
  close H;
  foreach (@help) {
    chop;
    s/\$version/$version/g;
    s/\$date/$date/g;
  }
} else {
  print "*** Warning: help file ($libdir/sirc.help) not found!\n";
}
$floodtimer=0;

sub exit {
  &dohooks("quit");
  &sl("QUIT :using sirc version $version$add_ons") if $connected;
  close LOG if $logging;
  exit 0;
}

$SIG{'PIPE'}='IGNORE';
$SIG{'QUIT'}='IGNORE';
$SIG{'INT'}='exit';
$SIG{'TERM'}='exit'; # KSIRC MOD

sub eq {
  local($a, $b)=@_;
  $a =~ tr/A-Z/a-z/;
  $b =~ tr/A-Z/a-z/;
  return ($a eq $b);
}

sub tilde {
  $_[0] =~ s|^\~(\w+)|(getpwnam($1))[7]|e;
  $_[0] =~ s/^\~/$ENV{'HOME'}/;
  $_[0]="." if $_[0] eq '';
}

sub sigquit {
  # really ugly hack, but it works...
  &dohooks("quit");
  close($trysock);
}

sub resolve {
  if ($sock6) {
    my $addr = $_[0];
    if ("$addr" =~ /^\d+$/)
    {
      $addr = pack("N", $addr);
      my @i = unpack("C4", $addr);
      $addr = "$i[0].$i[1].$i[2].$i[3]";
    }
    return getaddrinfo($addr, $_[1], $_[2] || &AF_UNSPEC, &SOCK_STREAM);
  }
  my $addr;
  if ($_[0] =~ /^\d+$/) {
    $addr = pack("N", $_[0]+0);
  } elsif ($_[0] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
    $addr = pack("c4", $1, $2, $3, $4);
  } else {
    $addr=(gethostbyname($_[0]))[4];
    return -1 unless (defined($addr));
  }
  return (&AF_INET, &SOCK_STREAM, 0, pack_sockaddr_in($_[1], $addr), undef);
}

$nextfh="sircblah000";
sub newfh {
  return ++$nextfh;
}

sub connect {
  $_[0]=&newfh;
  local($fh, $host, $port)=@_;
  my @res = resolve($host, $port);
  &tell("*\cbE\cb* Hostname `$host' not found"), return -1 if scalar(@res) < 5;
  $family = -1;
  my $bindfailed;
  while (scalar(@res) >= 5) {
    ($family, my ($socktype, $proto, $addr), undef, @res) = @res;
    &print("*\cbE\cb* Out of file descriptors: $!"), return -2
      unless socket($fh, $family, $socktype, $proto);

    $bindfailed = undef;
    if ($set{"LOCALHOST"}) {
      # once again, DCC only does ipv4
      $bindaddr =  (&resolve($set{"LOCALHOST"}, 0, &AF_INET))[3];
      $bindfailed = 1 unless bind($fh, $bindaddr);
    }

    $trysock=$fh;
    $SIG{'QUIT'}='sigquit';    
    $SIG{'QUIT'}='IGNORE', last if connect($fh, $addr);
    $SIG{'QUIT'}='IGNORE';
    $family = -1;
  }
  &print("*\cbE\cb* Can't connect to host: $!"), return -3 if $family == -1;
  # Tried to just check for $family != &AF_INET where needed, but
  # that segfaulted perl (!), guess it's a bug in Socket6.pm, but I won't try
  # to debug that. (malte)
  $ipv6 = 1 if ($sock6 && $family == &AF_INET6);
  &tell("*\cbE\cb* Warning: can't bind to sirc host: ".$set{'LOCALHOST'})
    if $bindfailed;

  if ($ipv6 != 1)
  {
    $bindaddr=getsockname($fh) unless $bindaddr;
  }
  select($fh); $|=1; select(STDOUT);
  return 1;
}

sub connectSSL {
  eval "use IO::Socket::SSL;";
  
  if($@){
      &tell("Can't load SSL socket library, perl does not support SSL!");
      &tell("To use SSL you must install the IO::Socket::SSL perl library");
      &tell("Try as root: perl -MCPAN -e 'install IO::Socket::SSL'");
      &tell("Giving up connect");
      return 0;
  }
  local($fh, $host, $port)=@_;
  &tell("*** Doing SSL server connect...");
  $fh = new IO::Socket::SSL("$host:$port");
  if(defined $fh){
      $_[0] = $fh;
      select($fh); $|=1; select(STDOUT);
      return 1;
  }
  else {
      warn "*** I encountered a problem: ($!) ",
      &IO::Socket::SSL::errstr();
      warn "*** Invalid hostname or port?\n";
      return -1;
  }
}

sub sel_nbconnecthandler {
    local($fh) = $_[0];
    &remwsel($fh);
    $!="";        
    my $res = unpack("i", getsockopt("$fh", SOL_SOCKET(), SO_ERROR()) || die "Failed to get sockopt: $!");
    select($fh); $|=1; select(STDOUT);
    &{$nbconnectlist{$fh}{"callback"}}($fh, $res);
    $nbconnectlist{$fh} = undef;
}

#
# Non blocking connect
# arguments are: filehandle(returned), host, port, callback function.
#

sub connectnb {
    if($hasPOSIX == 0){
        my $cb = $_[3];
        $_[3] = undef;
        my $ret = &connect(@_);
        if($ret == 1){
            &$cb($_[0], 0);
        }
        else {
            &$cb($_[0], -1);
        }
        return $ret;
        
    }
    $_[0]=&newfh;
    local($fh, $host, $port, $callback)=@_;
    my @res = resolve($host, $port);
    &tell("*\cbE\cb* Hostname `$host' not found"), return -1 if scalar(@res) < 5;

    while (scalar(@res) >= 5) {
        ($family, my ($socktype, $proto, $addr), undef, @res) = @res;
        &print("*\cbE\cb* Out of file descriptors: $!"), return -2
        unless socket($fh, $family, $socktype, $proto);                
        
        fcntl($fh, F_SETFL(), O_NONBLOCK());
        &addwsel($fh, "nbconnecthandler", 0);
        if(connect($fh, $addr)){
            &$callback($fh, 0);
        }
        else {
            if($! == EINPROGRESS()){
                $nbconnectlist{$fh}{"callback"} = $callback;
            }
            else {
                &print("*\cbI\cb* got other error $!");
                return -1;
            }            
        }        
    }
    return 1;
}

sub listen {
  $_[0]=&newfh;
  local($fh, $port)=@_;
  local($thisend);

  &tell("\cbE\cb* first set your ipv4 hostname with /set LOCALHOST <hostname>"), return 0
    unless (length $bindaddr);


# XXX: don't use ipv6 for the time being as ipv6 and dcc don't mix
#  if ($ipv6) {
    # XXX: substr() hack to avoid problems on some Linux systems
#    (undef, my $addr) = unpack_sockaddr_in6(substr($bindaddr, 0, 24));
#    $thisend = pack_sockaddr_in6($port, $addr);
#  } else {
    (undef, my $addr) = unpack_sockaddr_in($bindaddr);
    $thisend = pack_sockaddr_in($port, $addr);
#  }
  &tell("*\cbE\cb* Out of file descriptors"), return 0
    unless socket($fh, &AF_INET, &SOCK_STREAM, 0);
  &tell("*\cbE\cb* Can't bind local socket!"), close $fh, return 0
    unless bind($fh, $thisend);
  &tell("*\cbE\cb* Can't listen to socket!"), close $fh, return
    unless listen($fh, 5);
  $ipv6=0;
  return getsockname($fh);
}

sub accept {
  $_[0]=&newfh;
  return (accept($_[0], $_[1]), close($_[1]))[0];
}

sub bindtoserver {
  @channels=(); $talkchannel='';
  %mode=(); $umode=''; %limit=(); %haveops=(); %chankey=(); $away='';
  $listmin=0; $listmax=100000; $listpat='';
  @waituh=(); @douh=(); @erruh=(); $invited='';
  &dostatus;
  &tell("*** Connecting to $server, port $port...");
  if($SSL == 1){
      sleep 10, &bindtoserver if &connectSSL($S, $server, $port) < 0;
  } else {
      sleep 10, &bindtoserver if &connect($S, $server, $port) < 0;
  }
  $connected=1;
  $server1=$server;
  $port1=$port;
  $pass1=$pass;
  &sl("PASS $pass") if $pass;
  &sl("USER $username blah blah :".$set{'IRCNAME'});
  &sl("NICK $nick");
  @channels=(); $talkchannel=''; %mode=(); $umode=''; %limit=();
  %haveops=(); %chankey=();
}

sub gl {
  if ($buffer{$_[0]} =~ /^([^\n\r]*)\r?\n\r?/) {
    $buffer{$_[0]}=$';
    $_=$1."\n";
    return 1;
  }
  local($buf)='';
  #  &tell("About to sysread: $_[0]");
  if (sysread($_[0], $buf, 4096)) {
    $buffer{$_[0]}.=$buf;
    if ($buffer{$_[0]} =~ /^([^\n\r]*)\r?\n\r?/) {
      $buffer{$_[0]}=$';
      $_=$1."\n";
      return 1;
    }
    return '';
  }
  $_='';
  return 1;
}

sub sl {
  $logging && print LOG "<<".$_[0]."\n";
  if(!print $S $_[0]."\n"){
    &print("*\cbE\cb* Error writing to server: $!");
    &tell("*\cbE\cb* Connection to server lost");
    close($S);
    delete $buffer{$S};
    $connected=0;
    &dohooks("disconnect");
    &bindtoserver;
  }
  elsif (time-$floodtimer < 1){
    select(undef, undef, undef, 0.5);
  }
  $floodtimer=time;
}

sub dostatus {
  return unless $ssfe;
  local($t, $s)=($talkchannel, " [sirc]  ");
  my($i);
  for($i=0; $i<=$#channels; $i++){
    $s = " [sirc]  ";
    $t = $channels[$i];
    $t =~ tr/A-Z/a-z/;
    $s.="*" if $umode =~ /o/;
    $s.="\@" if $t && $haveops{$t};
    $s.=$nick;
    $s.=" (+$umode)" if $umode;
    $s.=" [query: ${query}]" if $query;
    $s.=" (away)" if $away;
    if ($talkchannel ne '') {
      $s.=" on $t (+$mode{$t})";
      $s.=" <key: $chankey{$t}>" if $chankey{$t};
      $s.=" <limit: $limit{$t}>" if $limit{$t};
    }
    &dohooks("status", $s);
#    $laststatus=$s, print "~${t}~`#ssfe#s$s\n" if $laststatus ne $s;
    $laststatus=$s;
    $logging && print LOG "** ~${t}~`#ssfe#s$s\n";
    print "~${t}~`#ssfe#s$s\n";
  }
}

$bold="\c[[1m";
$underline="\c[[4m";
$reverse="\c[[7m";
$normal="\c[[m";
$cls="\c[[H\c[[2J";

sub enhance {
  local($what)=@_;
  $what =~ tr/\c@-\c^/@-^/;
  return "\cv${what}\cv";
}

sub print {
  local($skip, $what)=(0, @_);
  &dohooks("print", $what);
  return if $skip;
  $what =~ s/\s+$//;
  # thanks to Toy (wacren@obspm.fr) for this translation
  $what =~ tr/\x80-\xff/\x00-\x1f !cLxY|$_ca<\-\-R_o+23\'mp.,1o>123?AAAAAAACEEEEIIIIDNOOOOO*0UUUUYPBaaaaaaaceeeeiiiidnooooo:0uuuuypy/
    if $set{"EIGHT_BIT"} ne 'on';
  $logging && print LOG "-> " . $what."\n";
  if ($raw_mode) {
    print $what, "\n" || &exit;
  } elsif ($ansi) {
    # this is buggy if you combine effects
    $what =~ s/([\ca\cc-\ch\cj-\cu\cw-\c^])/&enhance($1)/eg;
    while ($what =~ /\cb/) {
      ($what =~ s/\cb([^\cb]*)\cb/$bold$1$normal/) ||
      $what =~ s/\cb/$bold/g;
    }
    while ($what =~ /\c_/) {
      ($what =~ s/\c_([^\c_]*)\c_/$underline$1$normal/) ||
      $what =~ s/\c_/$underline/g;
    }
    while ($what =~ /\cv/) {
      ($what =~ s/\cv([^\cv]*)\cv/$reverse$1$normal/) ||
      $what =~ s/\cv/$reverse/g;
    }
    print $what, $normal, "\n" || &exit;
  } else {
    $what =~ tr/\ca-\ch\cj-\c_//d;
    print $what, "\n" || &exit;
  }
}

sub tell {
  $silent || &print;
}

sub dohooks {
  $hooktype=shift;
  local(@hl);
  eval "\@hl=\@${hooktype}_hooks;";
  foreach $h (@hl) {
    eval { &$h(@_); };
    $@ =~ s/\n$//, &tell("*\cbE\cb* error in $hooktype hook &$h: $@")
	if $@ ne '';
  }
}

sub dcerror {
  local($fh, $n)=($_[0], $dcnick{$_[0]});
  &dohooks("chat_disconnect", $n);
  &tell("*\cbE\cb* DCC chat with $n lost");
  &tell("~!dcc~Closing DCC CHAT with who: $n");
  close($fh);
  $n =~ tr/A-Z/a-z/;
  delete $dcnick{$fh};
  delete $dcvol{$n};
  delete $dcfh{$n};
  delete $buffer{$fh};
}

sub dgsclose {
  local($sfh, $rfh, $type, $err)=@_;
  &dohooks("dcc_disconnect", $dnick{$sfh}, $dfile{$rfh}, $dtransferred{$sfh},
	   time-$dstarttime{$rfh}, $rfh);
  &tell("*\cbD\cb* DCC $type with $dnick{$sfh} ($dfile{$rfh}) terminated; $dtransferred{$sfh} bytes transferred in ".(time-$dstarttime{$rfh}). " seconds");
  &tell("~!dcc~DCC $type terminated who: $dnick{$sfh} file: $dfile{$rfh} reason: $err");
  close($sfh);
  close($rfh);
  delete $dgrfh{$sfh};
  delete $dsrfh{$sfh};
  delete $dfile{$rfh};
  delete $dstarttime{$rfh};
  delete $dtransferred{$sfh};
  delete $dsoffset{$sfh};
  delete $dsport{$sfh};
  delete $dsresumedb{$sfh};
  delete $dgxferadd{$sfh};
  delete $dnick{$sfh};
}

sub msg {
  local($towho, $what)=@_;
  print "`#ssfe#t/m $towho \n" if $ssfe && !&eq($towho, $talkchannel);
  if ($towho =~ s/^=//) {
    local($n, $fh)=($towho);
    $n =~ tr/A-Z/a-z/;
    $fh=$dcfh{$n};
    if ($fh) {
      (print $fh $what."\n") || &dcerror($fh);
      $dcvol{$n}+=length($what);
      &dohooks("send_dcc_chat", $towho, $what);
      &tell("~=${towho}~|\cb$towho\cb| $what"); #KSIRC MOD
    } else {
      &tell("*\cbE\cb* No active DCC chat with $towho");
    }
  } elsif ($connected>1) {
    $what=substr($what, 0, 485);
    &dohooks("send_text", $towho, $what);
    if (&eq($towho, $talkchannel) && !$printchan) {
      &tell("~${towho}~<${nick}> $what"); # KSIRC MOD
    } elsif ($towho =~ /^[\&\#\+]/) {
      &tell("~${towho}~<$nick> $what"); #KSIRC MOD
    } else {
      &tell("~${towho}~>${nick}< $what"); #KSIRC MOD
    }
    &sl("PRIVMSG $towho :$what");
  } else {
    &tell("*** You're not connected to a server");
  }
}

sub say {
  if ($query)
  {
    &msg($query, @_);
  }
  elsif ($talkchannel) {
    &msg($talkchannel, @_);
  } else {
    &tell("*\cbE\cb* Not on a channel");
  }
}

sub notice {
  local($towho, $what)=@_;
  $what=substr($what, 0, 485);
  &dohooks("send_notice", $towho, $what);
  &tell("~${towho}~-> -~n${towho}~n- $what");
  &sl("NOTICE $towho :$what");
}

sub describe {
  local($towho, $what)=@_;
  $what=substr($what, 0, 480);
  &dohooks("send_action", $towho, $what);
  if (&eq($towho, $talkchannel) && !$printchan) {
    &tell("~${towho}~* $nick $what"); # KSIRC MOD
  } elsif ($towho =~ /^[\#\&\+]/) {
    &tell("~${towho}~* $nick $what"); # KSIRC MOD
  } else {
    &tell("~${towho}~* $nick $what"); #KSIRC MOD
#    &tell("~${towho}~*-> \cb${towho}\cb: $nick $what"); #KSIRC MOD
  }
  &sl("PRIVMSG $towho :\caACTION".($what eq "" ? "" : " ").$what."\ca");
}

sub me {
  if ($talkchannel) {
    &describe($talkchannel, @_);
  } else {
    &tell("*\cbE\cb* Not on a channel");
  }
}

sub yetonearg {
  ($newarg, $args)=split(/ +/, $args, 2);
  $args =~ s/^://;
}

sub getarg {
  ($newarg, $args)=split(/ +/, $args, 2);
}

@weekdays=("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
@months=("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
  "Nov", "Dec");

sub date {
  local($sec, $min, $hour, $mday, $mon, $year, $wday)=localtime($_[0]);
  return sprintf("$weekdays[$wday] $months[$mon]  $mday %.2d:%.2d:%.2d %d",
		 $hour, $min, $sec, $year+1900);
}

sub reply {
  return if $set{"CTCP"} eq 'noreply';
  if ($lastrep<time-10) {
    $lastrep=time;
    $nreps=1;
  } else {
    return if $nreps++>=2 && $set{"CTCP"} eq 'noflood';
  }
  &sl("NOTICE $who :\ca$_[0]\ca");
}

sub ctcp {
  local($towho, $to, $what)=$_[0];
  ($what, $args)=split(/ +/, $_[1], 2);
  $what =~ tr/a-z/A-Z/;
  &dohooks("ctcp", $towho, $what, $args);
  return if $skip;
  local($a)=$args;
  $a && ($a=' '.$a);
  $to = (&eq($towho, $nick) ? "you" : $towho);
  
  &tell("~$to~*** $who$puh1 did a CTCP $what$a to $to")
    unless $what =~ /^(ACTION|PING|DCC|VERSION)$/;
  if ($what eq 'ACTION') {
    &dohooks("action", $towho, $args);
    if (&eq($towho, $nick)) {
      &tell("~$who~* \cb${who}\cb$puh1 $args"); # KSIRC MOD
    } elsif (&eq($towho, $talkchannel) && !$printchan) {
      &tell("~$towho~* $who $args"); #KSIRC MOD
    } else {
      &tell("~$towho~* $who$puh2 $args"); #KSIRC MOD
    }
  } elsif ($what eq 'TIME') {
    &reply("TIME ".&date(time));
  } elsif ($what eq 'CLIENTINFO') {
    &reply("CLIENTINFO ACTION, CLIENTINFO, DCC, ECHO, ERRMSG, FINGER, PING, TIME, USERINFO, VERSION");
  } elsif ($what eq 'FINGER') {
    &reply("FINGER ".$set{"FINGER"});
  } elsif ($what eq 'USERINFO') {
    &reply("USERINFO ".$set{"USERINFO"});
  } elsif ($what eq 'VERSION') {
    local($u)=$add_ons;
    $u =~ s/^\+//;
    $u =~ s/\+/ + /g;
    $u=" -- using $u" if $u;
    if($to eq 'you'){
      &tell("~$who~*** $who$puh1 did a CTCP $what$a to $to")
    }
    else {
      &tell("~$to~*** $who$puh1 did a CTCP $what$a to $to")
    }
    &reply("VERSION sirc $version, a \cbperl\cb client$u");
  } elsif ($what eq 'PING') {
    &reply("PING $args");
    &tell("*** $who$puh1 did a CTCP PING to $to"); #KSIRC
  } elsif ($what eq 'ECHO' || $what eq 'ERRMSG') {
    &reply("$what $args");
  } elsif ($what eq 'DCC') {
    &getarg;
    if ($newarg eq 'CHAT' || $newarg eq 'SEND' && !$restrict) {
      local($dfile, $dhost, $dport, $dsize)=split(/ +/, $args, 4);
      $dfile=$1 if $dfile =~ m|/([^/]*)$|;
      $dfile =~ s/^\./_/;
      if ($dhost==2130706433 || !$dport>1024 || $dhost !~ /^\d+$/ ||
	  $dport !~ /^\d+$/) {
	&tell("*\cbE\cb* DCC $newarg ($dfile) from $who$puh1 rejected");
      } elsif ($newarg eq 'CHAT' && grep (&eq($who, $dcwait{$_}),
		keys(%dcwait))) {
	&tell("*\cbD\cb* DCC chat already requested from $who, connecting...");
	my ($wfh)=(grep(&eq($dcwait{$_}, $who), keys(%dcwait)));
        my ($n, $fh)=$who;
	delete $dcwait{$wfh};
        close($wfh);
        my $w = $who;
        my $cb = sub {
            my ($lfh, $lres) = @_;
            if($lres != 0){
                &tell("*\cbD\cb* DCC CHAT with $w failed: " . strerror($lres));
                &tell("~!dcc~DCC CHAT failed who: $who reason: " . strerror($lres));
                close($lfh);
                return;
            }
            $dcnick{$lfh}=$w;
            &tell("*\cbD\cb* DCC CHAT with $w established");
            &tell("~!dcc~DCC CHAT established who: $w");
            $n =~ tr/A-Z/a-z/;
            $dcvol{$n}=0;
            $dcfh{$n}=$lfh;
            print "`#ssfe#t/m =$w \n" if $ssfe;
        };
        if(&connectnb($fh, $dhost, $dport, $cb) < 1) { 
            return; 
        }
      } elsif ($newarg eq 'CHAT' && grep(&eq($who, $_), keys(%dcfh))) {
	&tell("*\cbD\cb* DCC chat from $who$puh1 ignored (already established)");
      } else {
        #&tell("*\cbD\cb* DCC $newarg ($dfile) from $who$puh1 ".
        #     ($dsize ? "(size: $dsize) " : "")."[$dhost, $dport]");
        my $ip = inet_ntoa(pack("N", $dhost));
        if ($newarg eq 'CHAT') {
            &tell("~!dcc~DCC CHAT OFFERED who: $who$puh1 ip: $ip port: $dport");
	  $dcoffered{$who}="$dhost $dport";
	  &dohooks("dcc_request", "CHAT", $dhost, $dport);
        } else {
            my $index = 1; # KSIRC MOD - Make the file name unique
            UNIQ: {
              foreach $i (keys(%dgoffered)) {
                my($h, $p, $f) = split(/ /, $i);
                if (&eq($f, $dfile)) {
                  $dfile =~ s/(.*)\.\d+$/$1/;
                  $dfile .= ".$index";
                  $index++;
                  redo UNIQ;
                }
              }
          }
          &tell("~!dcc~INBOUND DCC SEND who: $who$puh1 file: $dfile size: $dsize ip: $ip port: $dport");
            
	  $dgoffered{"$dhost $dport $dfile"}=$who;
	  &dohooks("dcc_request", "SEND", $dhost, $dport, $dfile, $dsize);
	}
      }
    } else {
      &tell("*** $who$puh1 did a CTCP ${what}$a to $to");
    }
  }
}

sub doset {
  local($var, $val)=@_;
  $var =~ tr/a-z/A-Z/;
  $val="" unless defined($val);
  if ($var eq 'PRINTUH') {
    $set{$var}="all" if $val =~ /^(on|all)$/i;
    $set{$var}="some" if $val =~ /^some$/i;
    $set{$var}="none" if $val =~ /^(off|none)$/i;
  } elsif ($var eq 'PRINTCHAN') {
    $set{$var}="on", $printchan=1 if $val =~ /^on$/i;
    $set{$var}="off", $printchan=0 if $val =~ /^off$/i;
  } elsif ($var eq 'CTCP') {
    $val =~ tr/A-Z/a-z/;
    $set{$var}=$val if $val =~ /^(none|all)$/;
    $set{$var}="noreply" if $val =~ /^(noreply|off)$/;
    $set{$var}="noflood" if $val =~ /^(noflood|on)$/;
  } elsif ($var eq 'SENDAHEAD') {
    $set{$var}=$val if $val =~ /^\d+$/ && $val<=65536;
  } elsif ($var eq 'USERINFO') {
    $set{$var}=$val;
  } elsif ($var eq 'FINGER') {
    $set{$var}=$val;
  } elsif ($var eq 'IRCNAME') {
    $set{$var}=$val;
  } elsif ($var eq 'EIGHT_BIT') {
    $val =~ tr/A-Z/a-z/;
    $set{$var}=$val if $val =~ /^(on|off)$/;
  } elsif ($var eq 'LOCALHOST') {
    &restrict || return;
    # IPV6: DCC is always ipv4 :(
    local($ad) = (&resolve($val, 0, &AF_INET))[3];
    $set{$var}=$val, $bindaddr=$ad if $ad;
  } elsif ($var eq 'LOADPATH') {
    @loadpath=split(/:/, $val);
    foreach (@loadpath) {
      &tilde($_);
    }
    $set{$var}=join(":", @loadpath);
  } elsif ($var eq 'CTRL_T') {
    $set{$var}=$val;
    print "`#ssfe#T$val\n" if $ssfe;
  } elsif ($var eq 'LOGFILE') {
    &restrict || return;
    &tilde($val);
    $logfile=$set{$var}=$val;
  } elsif ($var eq 'LOG') {
    &restrict || return;
    if ($val =~ /^on$/i) {
      $logging && close LOG;
      if (open(LOG, 
	  ($logfile =~ /\.gz$/ ? "| gzip >> $logfile" : ">> $logfile"))) {
	$logging=1;
	$set{$var}="on";
	select(LOG); $|=1; select(STDOUT);
	print LOG "*\cbL\cb* IRC log started on ".&date(time)."\n";
      } else {
	$logging='';
	$set{$var}="off";
	&tell("*\cbE\cb* Can't write to logfile $logfile");
      }
    } elsif ($val =~ /^off$/i) {
      print LOG "*\cbL\cb* Log ended on ".&date(time)."\n", close LOG
	if $logging;
      $logging='';
      $set{$var}="off";
    }
  } elsif (defined($sets{$var})) {
    local($f)=$sets{$var};
    eval { &$f($val); };
    $@ =~ s/\n$//, &tell("*\cbE\cb* error in SET $var hook: $@") if $@ ne '';
  }
}

sub ctcpreply {
  local($ctcp, $rest)=split(/ +/, $_[1], 2);
  $ctcp =~ tr/a-z/A-Z/;
  &dohooks("ctcp_reply", $_[0], $ctcp, $rest);
  $rest=(time-$rest)." seconds" if $ctcp eq 'PING';
  if (&eq($_[0], $nick)) {
      &tell("*** CTCP $ctcp reply from $who$puh1: $rest");
  } else {
    &tell("*** CTCP $ctcp reply to $_[0] from $who$puh2: $rest");
  }
}

sub load {
  local($f)=@_;
  &tilde($f);
  if ($f !~ /\//) {
    foreach (@loadpath) {
      $f="$_/$f", last if -f "$_/$f";
      $f="$_/${f}.pl", last if $f !~ /\.pl$/ && -f "$_/${f}.pl";
    }
  } else {
    $f.=".pl" if -f "${f}.pl" && !-f $f;
  }
  if ($f =~ /\// && -f $f) {
    do $f;
    $@ =~ s/\n$//, &tell("*\cbE\cb* Load error in $f: $@") if $@ ne '';
  } else {
    &tell("*\cbE\cb* $f: File not found");
  }
}

sub restrict {
  &tell("*\cbE\cb* Command not available"), return 0 if $restrict;
  1;
}

sub dosplat {
  $args =~ s/^\s*\*($|\s)/${talkchannel}${1}/ if $talkchannel;
}

sub expand {
  if ($_[0] eq '$') {
    return '$';
  } elsif ($_[0] =~ /^(\d+)$/) {
    return (split(/ +/, $args))[$1];
  } elsif ($_[0] =~ /^(\d+)-$/) {
    return (split(/ +/, $args, 1+$1))[$1];
  } else {
    return eval "\$$_[0]";
  }
}

$recdepth=0;
$maxrecursion=20;

sub docommand {
  local($line)=@_;
  local($recdepth)=$recdepth+1;
  &print("*\cbE\cb* Max recursion exceeded!"), return
    if $recdepth > $maxrecursion;
  local($noalias)=($line =~ s/^\///);
  local($silent)=1 if $line =~ s/^\^//;
  local($cmd, $args)=split(/ +/, $line, 2);
  $cmd =~ tr/a-z/A-Z/;
  if (!$noalias && defined($aliases{$cmd})) {
    $line=$aliases{$cmd};
    $line.=($args ne '' ? " ".$args : "")
      unless ($line =~ s/\$(\$|\d+-?|\w+)/&expand($1)/eg);
    $line =~ s/^\///;
    $noalias=1 if $line =~ s/^\///;
    $silent=1 if $line =~ s/^\^//;
    ($cmd, $args)=split(/ +/, $line, 2);
    $cmd =~ tr/a-z/A-Z/;
  }
  if (!$noalias && defined($cmds{$cmd})) {
    eval $cmds{$cmd};
    $@ =~ s/\n$//, &tell("*\cbE\cb* error in command $cmd: $@") if $@ ne '';
  } elsif ($cmd eq 'ALIAS') {
    &getarg;
    if ($newarg =~ /^-/) {
      local($a)=$';
      if ($a eq '') {
	%aliases=();
	&tell("*** All aliases removed");
      } else {
	$a =~ tr/a-z/A-Z/;
	delete $aliases{$a};
	&tell("*** Alias $a removed");
      }
    } elsif ($newarg ne '') {
      $newarg =~ tr/a-z/A-Z/;
      if ($args ne '') {
	$aliases{$newarg}=$args;
	&tell("*** $newarg aliased to $args");
      } else {
	if (defined($aliases{$newarg})) {
	  &tell("*** $newarg is aliased to: $aliases{$newarg}");
	} else {
	  &tell("*** $newarg: no such alias");
	}
      }
    } else {
      foreach $a (sort(keys(%aliases))) {
	&tell("*** $a is aliased to $aliases{$a}");
      }
    }
  } elsif ($cmd eq 'SET') {
    &getarg;
    local($s)=$newarg;
    $s =~ tr/a-z/A-Z/;
    if ($s =~ s/^-//) {
      &tell("*** No such variable $s"), return unless defined($set{$s});
      &doset($s, "");
      &tell("*** $s is ".($set{$s} ne '' ? "set to $set{$s}" : "unset"));
    } elsif ($s ne '') {
      &tell("*** No such variable $s"), return unless defined($set{$s});
      &doset($s, $args) if $args ne '';
      &tell("*** $s is ".($set{$s} ne '' ? "set to $set{$s}" : "unset"));
    } else {
      foreach $s (sort(keys (%set))) {
	&tell("*** $s is ".($set{$s} ne '' ? "set to $set{$s}" : "unset"));
      }
    }
  } elsif ($cmd eq 'NOTIFY' || $cmd eq 'N') {
    if ($args eq '-') {
      &tell("*** Notify list cleared");
      my($value);
      while(($_, $value) = each %notify){           # Remove all nicks
	&tell("*\cb(\cb* Signoff by $_ detected!"); # KSIRC MOD
      }      
      %notify=();
    } elsif ($args eq '') {
      local($l)='';
      foreach (grep($notify{$_}, keys %notify)) {
	&tell("*** Currently present: $l"), $l='' if length($l)>450;
	&tell("*\cb)\cb* Signon by $_ detected!");  # KSIRC MOD
	$l.=$_." ";
      }
      $l && &tell("*** Currently present: $l");
      $l='';
      foreach (grep(!$notify{$_}, keys %notify)) {
	&tell("*** Currently absent: $l"), $l='' if length($l)>450;
	&tell("*\cb(\cb* Signoff by $_ detected!"); # KSIRC MOD
	$l.=$_." ";
      }
      $l && &tell("*** Currently absent: $l");
    } else {
      local($w, $n);
      foreach $w (split(/ +/, $args)) {
	if ($w =~ s/^-//) {
	  ($n)=(grep(&eq($_, $w), keys(%notify)), '');
	  $n ne '' && delete $notify{$n};
	  &tell("*** $w removed from notify list");
	  &tell("*\cb(\cb* Signoff by $w detected!"); # KSIRC MOD
	} else {
	  $notify{$w}='0';
	  &tell("*** $w added to notify list");
	  $newisons=1;
	}
      }
    }
  } elsif ($cmd eq 'IGNORE' || $cmd eq 'IG') {
    &getarg;
    if ($newarg eq '-') {
      @ignore=();
      &tell("*** Ignore list cleared");
    } elsif ($newarg eq '') {
      local($p);
      &tell("*** You're ignoring:");
      foreach (@ignore) {
	$p=$_;
	$p =~ s/\\//g;
	$p =~ s/\.\*/*/g;
	&tell("*** $p");
      }
    } else {
      local($d, $p)=('');
      $d=1 if $newarg =~ s/^-//;
      if ($newarg =~ /\!.*\@/) {
      } elsif ($newarg !~ /[\@\!]/) {
	$newarg.="!*";
      } elsif ($newarg =~ /\@/) {
	$newarg="*!".$newarg;
      } else {
	$newarg.="\@*";
      }
      $p=$newarg;
      $newarg =~ s/([^\\])\./$1\\./g;
      $newarg =~ s/\*/\.\*/g;
      $newarg =~ s/([^\.\*\\\w])/\\$1/g;
      if ($d) {
        &tell("*** Removing $p from the ignore list");
	@ignore=grep(!&eq($_, $newarg), @ignore);
      } else {
        &tell("*** Ignoring $p ... what a relief!");
	push(@ignore, $newarg);
      }
    }
  } elsif ($cmd eq 'ECHO') {
    &print($args);
  } elsif ($cmd eq 'CLEAR' || $cmd eq 'CL') {
    print $cls if $ansi;
    print "`#ssfe#l\n" if $ssfe;
  } elsif ($cmd eq 'EVAL') {
    &restrict || return;
    eval ($args);
    $@ =~ s/\n$//, &tell("*\cbE\cb* eval error: $@") if $@ ne '';
  } elsif ($cmd eq 'HELP') {
    &tell("*\cbH\cb* Help not available"), return unless @help;
    $args='main' if $args =~ /^\s*$/;
    $args =~ s/ *$//;
    local($found)='';
    foreach (@help) {
      if (/^\@/) {
	last if $found;
	if (&eq($_, "\@$args")) {
	  $found=1;
	  &tell("*\cbH\cb* Help on $args") if $args ne 'main';
	}
      } else {
	&tell("*\cbH\cb* $_") if $found;
      }
    }
    &tell("*\cbH\cb* Unknown help topic; try /help") unless $found;
  } elsif ($cmd eq 'LOAD') {
    &restrict || return;
    &getarg;
    &tell("*\cbE\cb* Yeah, but what?"), return if $newarg eq '';
    &load($newarg);
  } elsif ($cmd eq 'VERSION') {
    &tell("*** \cbsirc\cb version $version, written in \cbperl\cb by \cborabidoo\cb");
    $_=$add_ons;
    s/^\+//;
    s/\+/, /g;
    &tell("*** add-ons: $_") if $_;
    $connected==2 && &sl("VERSION $args");
  } elsif ($cmd eq 'CD') {
    &restrict || return;
    &getarg;
    if ($newarg ne '') {
      &tilde($newarg);
      chdir($newarg) || &tell("*\cbE\cb* Can't chdir to $newarg");
    }
    local($cwd); chop($cwd=`pwd`);
    &tell("*** Current directory is $cwd");
  } elsif ($cmd eq 'SYSTEM') {
    &restrict || return;
    system($args);
  } elsif ($cmd eq 'BYE' || $cmd eq 'QUIT' || $cmd eq 'EXIT' ||
		$cmd eq 'SIGNOFF') {
    $args || ($args="using sirc version $version$add_ons");
    &dohooks("quit");
    &sl("QUIT :$args") if $connected;
    &exit;
  } elsif ($cmd eq 'SERVER') {
    $args=$1 if $args =~ /^\s*(.*)\s*$/;
    $args="$server0:$port0:$pass0" if $args eq '0';
    $args="$server1:$port1:$pass1" if $args eq '1';
    if ($args eq '') {
      &tell($connected ? "*** Your current server is $server" :
			 "*** You're not connected to a server");
    } else {
      ($server, $port, $pass)=split(/[\s:]+/, $args);
      $server=$', $nick=$1 if $server =~ /^([^\@]+)\@/;
      $port || ($port=$port0);
      &sl("QUIT :changing servers"), close $S, delete $buffer{$S} if $connected;
      $connected=0;
    }
  } elsif ($cmd eq 'MSG' || $cmd eq 'M') {
    &dosplat;
    if ($args) {
      ($newarg, $args)=split(/ /, $args, 2);
      &msg($newarg, $args);
    } else {
      &tell("*\cbE\cb* You must specify a nick or channel!");
    }
  } elsif ($cmd eq 'QUERY' || $cmd eq 'Q') {
    if ($args) {
      $args =~ s/\s+$//;
      $query=$args;
      &tell("*** Starting conversation with $query");
      &dostatus;
    } elsif ($query) {
      &tell("*** Ending conversation with $query");
      $query='';
      &dostatus;
    } else {
      &tell("*** You aren't querying anyone :p");
    }
  } elsif ($cmd eq 'DCC') {
    &getarg;
    if ($newarg =~ /^chat$/i) {
      &getarg;
      local($n)=grep(&eq($newarg, $_), keys(%dcoffered));
      if ($n) {
	local($dcadr, $dcport)=split(/ +/, $dcoffered{$n});
	local($fh);
        delete $dcoffered{$n};
        my $w = $n;
        my $cb = sub {
            my ($lfh, $lres) = @_;
            if($lres != 0){
                &tell("*\cbD\cb* DCC CHAT with $w failed: " . strerror($lres));
                &tell("~!dcc~DCC CHAT failed who: $w reason: " . strerror($lres));
                close($lfh);
                return;
            }
            $dcnick{$lfh}=$w;
            &tell("*\cbD\cb* DCC CHAT with $w established");
            &tell("~!dcc~DCC CHAT established who: $w");
            print "`#ssfe#t/m =$w \n" if $ssfe;
            my $n = $w;
            $n =~ tr/A-Z/a-z/;
            $dcvol{$n}=0;
            $dcfh{$n}=$fh;
        };
        if(&connectnb($fh, $dcadr, $dcport, $cb) < 1){ 
            return; 
        }
      } elsif (grep (&eq($newarg, $dcwait{$_}), keys(%dcwait))) {
	&tell("*\cbE\cb* DCC CHAT request to $newarg already sent");
      } elsif (grep(&eq($newarg, $dcnick{$_}), keys(%dcnick))) {
	&tell("*\cbE\cb* DCC CHAT with $newarg already established");
      } elsif ($newarg) {
	&tell("*** You're not connected to a server"), return if $connected<2;
	&tell("*** Don't be antisocial!"), return if &eq($newarg, $nick);
	local($mynumber, $myport, $fh);
	my $sockaddr = &listen($fh) or return;
	if ($ipv6) {
          # XXX: substr is used in order to avoid dying on Linux with older
          # glibc that lacks the scope field from sockaddr_in6 but the kernel
          # has it and returns it from getsockname()
          ($myport, undef) = unpack_sockaddr_in6(substr($sockaddr, 0, 24));
          $mynumber = '0';
        } else {
          ($myport, $mynumber) = unpack_sockaddr_in(&listen($fh)) or return;
          $mynumber = unpack("N", $mynumber);
        }
	$dcwait{$fh}=$newarg;
	&sl("PRIVMSG $newarg :\caDCC CHAT chat $mynumber $myport\ca");
	&dohooks("send_ctcp", $newarg, "DCC CHAT chat $mynumber $myport");
        &tell("*\cbD\cb* Sent DCC CHAT request to $newarg");
        &tell("~!dcc~DCC CHAT SEND who: $newarg");
      } else {
	&tell("*** I need a nick");
      }
    } elsif ($newarg =~ /^rchat$/i) {
      &getarg;
      local($n)=$newarg;
      &getarg;
      if ($newarg) {
        local($fh)=grep(&eq($dcnick{$_}, $n), keys(%dcnick));
        if( ! $fh){  
            &tell("*\cbE\cb* No DCC CHAT established with $n");
            &tell("~!dcc~No DCC CHAT established who: $n");
            return;
        }
	&tell("*\cbE\cb* DCC CHAT already established with $newarg"), return 
	  if grep(&eq($dcnick{$_}, $newarg), keys(%dcnick));
        &tell("*\cbD\cb* DCC CHAT with $n renamed to $newarg");
        &tell("~!dcc~DCC CHAT renamed who: $n to: $newarg");
	$dcnick{$fh}=$newarg;
	$n =~ tr/A-Z/a-z/;
	$newarg =~ tr/A-Z/a-z/;
	$dcfh{$newarg}=$dcfh{$n};
	$dcvol{$newarg}=$dcvol{$n};
	delete $dcfh{$n};
	delete $dcvol{$n};
      } else {
	&tell("*** I need *two* nicks");
      }
    } elsif ($newarg =~ /^close$/i) {
      &getarg;
      if ($newarg =~ /^chat$/i) {
	&getarg;
	local($n)=$newarg;
	$newarg =~ tr/A-Z/a-z/;
	local($fh)=$dcfh{$newarg};
	local($nn)=(grep(&eq($_, $newarg), keys(%dcoffered)));
	if ($nn) {
          &tell("*\cbD\cb* Forgetting offered DCC CHAT from $nn");
          &tell("~!dcc~Closing DCC CHAT who: $nn");
          delete $dcoffered{$nn};
          if($no_reject == 0){
              $who = $nn;
              &reply("DCC REJECT CHAT chat");
          }
          $no_reject = 0;
	} elsif ($fh) {
	  &dohooks("chat_disconnect", $n);
          &tell("*\cbD\cb* Closing DCC CHAT connection with $n");
          &tell("~!dcc~Closing DCC CHAT who: $n");
	  close($fh);
	  delete $dcnick{$fh};
	  delete $dcvol{$newarg};
	  delete $dcfh{$newarg};
          delete $buffer{$fh};
          if($no_reject == 0){
              $who = $n;
              &reply("DCC REJECT CHAT chat");
          }
          $no_reject = 0;

	} elsif (($fh)=grep(&eq($dcwait{$_}, $n), keys (%dcwait)), $fh) {
	  close($fh);
	  delete $dcwait{$fh};
          &tell("*\cbD\cb* Closing listening DCC CHAT with $n");
          &tell("~!dcc~Closing DCC CHAT who: $n");
          if($no_reject == 0){
              $who = $n;
              &reply("DCC REJECT CHAT chat");
          }
          $no_reject = 0;
        } else {
            if($n){
                &tell("*\cbE\cb* No DCC CHAT connection with $n");
                &tell("~!dcc~No DCC CHAT connection who: $n");
            }
	}
      } elsif ($newarg =~ /^get$/i) {
        &getarg;
        my $arg = $newarg;  
        local($found)='';
	foreach $i (keys(%dgoffered)) {
            if (&eq($dgoffered{$i}, $newarg) && (!$args ||
                &eq($args, (split(/ +/, $i))[2]))) {
              &tell("*\cbE\cb* Forgetting pending DCC GET from $newarg");
              my($host, $port, $file) = split(/ /, $i);
              &tell("~!dcc~Closing DCC GET connection with who: $newarg file: $file"); # KSIRC MOD
              delete $dgoffered{$i};
              $found=1;
              if($no_reject == 0){
                  $who = $newarg;
                  &reply("DCC REJECT GET $file");
              }
              $no_reject = 0;
	  }
	}
	foreach $sfh (grep(&eq($newarg, $dnick{$_}), keys(%dnick))) {
	  if (!$found && $dgrfh{$sfh}) {
              local($fh)=$dgrfh{$sfh};
              my($file)=$dfile{$fh};
	    next if $args && ($args ne $dfile{$fh});
	    &dohooks("dcc_disconnect", $dnick{$sfh}, $dfile{$fh}, 
            $dtransferred{$sfh}, time-$dstarttime{$fh}, $fh);
            
            &tell("*\cbE\cb* Closing DCC GET connection with: $newarg ($file)"); # KSIRC MOD
            &tell("~!dcc~Closing DCC GET connection with who: $newarg file: $file"); # KSIRC MOD
	    $found=1;
	    close $sfh;
	    close $fh;
	    delete $dgrfh{$sfh};
	    delete $dfile{$fh};
	    delete $dstarttime{$fh};
            delete $dtransferred{$sfh};
            delete $dgxferadd{$sfh};
            delete $dnick{$sfh};
            if($no_reject == 0){
                $who = $newarg;
                &reply("DCC REJECT GET $file");
            }
            $no_reject = 0;
	  }
        }
        if( ! $found){
            &tell("*\cbE\cb* No DCC GET connection with $newarg for $arg");
            &tell("~!dcc~No DCC GET connection who: $newarg file: $arg");
        }
      } elsif ($newarg =~ /^send$/i) {
        &getarg;
	local($n, $found, $fh)=($newarg, '');
        &getarg;
        my $arg = $newarg;
	$newarg =~ s/(\W)/\\$1/g;
	foreach $sfh (keys(%dswait), keys(%dsrfh)) {
	  next unless &eq($dnick{$sfh}, $n);
	  $fh=$dswait{$sfh} || $dsrfh{$sfh} || next;
	  if ($newarg eq '' || $dfile{$fh} =~ /^${newarg}$/ ||
	      $dfile{$fh} =~ /\/${newarg}$/) {
              #&tell("*\cbD\cb* DCC SEND connection with $n closed");
              #my($file)=$dfile{$fh};
              #&tell("~!dcc~Closing DCC SEND connection with who: $n file: $file"); # KSIRC MOD
              #&dohooks("dcc_disconnect", $dnick{$sfh}, $dfile{$fh}, 
              #  $dtransferred{$sfh}, time-$dstarttime{$fh}, $fh);
              #close($sfh);
              #close($fh);
              #delete $dswait{$sfh};
              #delete $dsrfh{$sfh};
              #delete $dfile{$fh};
              #delete $dstarttime{$fh};
              #delete $dtransferred{$sfh};
              #delete $dsoffset{$sfh};
              #delete $dsport{$sfh};
              #delete $dsresumedb{$sfh};
              #delete $dgxferadd{$sfh};
              #delete $dnick{$sfh};
              if($no_reject == 0){
                  $who = $n;
                  &reply("DCC REJECT SEND $dfile{$fh}");
              }
              $no_reject = 0;
              
              if($dstarttime{$fh} == undef) {
                  $dstarttime{$fh} = time;
              }
              &dgsclose($sfh, $fh, "SEND", "CLOSE");
              
              $found=1;
          }
        }
        if(!$found){
            &tell("*\cbE\cb* No DCC SEND connection with $n for $arg");
            &tell("~!dcc~No DCC SEND connection with who: $n file: $arg");
        }
      } else {
	&tell("*\cbE\cb* Unknown DCC type");
      }
    } elsif ($newarg =~ /^rename$/i) {
      local($found, $n);
      &getarg;
      $n=$newarg;
      &getarg;
      $args=$newarg, $newarg='' if $args eq '';
      &tell("*\cbE\cb* I need a filename :p"), return if $args eq '';
      &tilde($args);
      foreach $i (keys(%dgoffered)) {
	if (&eq($dgoffered{$i}, $n) && (!$newarg ||
	    &eq($newarg, (split(/ +/, $i))[2]))) {
	  local($m, $p, $f)=split(/ +/, $i);
	  delete $dgoffered{$i};
	  $dgoffered{"$m $p $args"}=$n;
	  &tell("*\cbD\cb* Renaming \"$f\" (offered by $n) to \"$args\"");
	  $found=1;
	  last;
	}
      }
      &tell("*\cbE\cb* No such file offered by $n") unless $found;
    } elsif ($newarg =~ /^get$/i) {
      &getarg;
      local($n)=grep((&eq($newarg, $dgoffered{$_}) && (!$args ||
		      &eq($args, (split(/ +/, $_))[2]))),
		     keys(%dgoffered));
      if ($n) {
        my($dgadr, $dgport, $file)=split(/ +/, $n);
        my($fh, $sfh);
        my $offset = 0;
	$n=(delete $dgoffered{$n});
        $fh=&newfh;
        if($dgresume{$dgport} && $dgresume{$dgport}{"GotReply"}){
            &print("*\cbE\cb* Can't write to file $file"), return unless open($fh, ">> $file");
            seek($fh, $dgresume{$dgport}{"pos"}, SEEK_SET);
            $offset = $dgresume{$dgport}{"pos"};
            delete $dgresume{$dgport};
        }
        else {
            &print("*\cbE\cb* Can't write to file $file"), return unless open($fh, "> $file");
        }
        my $who = $n;
        my $cb = sub {
            my ($lfh, $lres) = @_;
            if($lres != 0){
                &tell("*\cbD\cb* DCC GET connection with $who ($file) failed: " . strerror($lres));
                &tell("~!dcc~DCC GET failed who: $who file: $file reason: " . strerror($lres));
                close($lfh);
                return;
            }
            $dgrfh{$lfh}=$fh;
            $dnick{$lfh}=$who;
            $dfile{$fh}=$file;
            $dstarttime{$fh}=time;
            $dtransferred{$lfh}=0;
            $dgxferadd{$lfh}=$offset;
            &tell("*\cbD\cb* DCC GET connection with $who established");
            &tell("~!dcc~DCC GET established who: $who file: $file");
            &dohooks("dcc_get", $who, $file, $fh);
        };
        if(&connectnb($sfh, $dgadr, $dgport, $cb) < 1){ 
            return;
        }
      } else {
	if ($newarg) {
	  &tell("*\cbE\cb* No pending DCC GET from $newarg");
	} else {
	  &tell("*\cbE\cb* Uhm, who from?");
	}
      }
    } elsif ($newarg =~ /^list$/i || $newarg eq '') {
      &tell("*\cbD\cb* List of DCC connections:");
      foreach $n (keys(%dcfh)) {
	&tell("*\cbD\cb* Established DCC CHAT with $n ($dcvol{$n} bytes)");
      }
      foreach $n (keys(%dcoffered)) {
        my ($pip, $port) = split(/ /, $dcoffered{$n});
        my $ip = inet_ntoa(pack("N", $pip));          
	&tell("*\cbD\cb* DCC CHAT offered by $n ($ip:$port)");
      }
      foreach $f (keys(%dcwait)) {
	&tell("*\cbD\cb* DCC CHAT offered to $dcwait{$f}");
      }
      foreach $i (keys(%dgoffered)) {
          my ($pip, $port, $file) = split(/ /, $i);
          my $ip = inet_ntoa(pack("N", $pip));
          &tell("*\cbD\cb* DCC GET \"$file\" ($ip:$port) offered by $dgoffered{$i}");
      }
      foreach $s (keys(%dgrfh)) {
        local($f)=$dgrfh{$s};
	&tell("*\cbD\cb* DCC GET \"$dfile{$f}\" established with $dnick{$s}, $dtransferred{$s} bytes read in ".(time-$dstarttime{$f})." seconds.");
      }
      foreach $s (keys(%dswait)) {
        local($f)=$dswait{$s};
	&tell("*\cbD\cb* DCC SEND \"$dfile{$f}\" offered to $dnick{$s}");
      }
      foreach $s (keys(%dsrfh)) {
        local($f)=$dsrfh{$s};
	&tell("*\cbD\cb* DCC SEND \"$dfile{$f}\" established with $dnick{$s}, $dtransferred{$s} bytes sent in ".(time-$dstarttime{$f})." seconds.");
      }
    } elsif ($newarg =~ /^send$/i) {
      &tell("*** You're not connected to a server"), return if $connected<2;
      &restrict || return;
      local(($n),($f)) = $args =~ /^(.+?) (.+)/;
      local($tf, $mynumber, $sz, $fh, $myport, $lfh)=($f);
      &tilde($f);
      while (my($fh, $ni) = each %dnick ) {
          if(&eq($n, $ni)){
              my $lfh = $dswait{$fh};
              if(&eq($dfile{$lfh}, $f)){
                  &tell("*\cbE\cb* DCC Send already pending of $f to $n");
                  return;
              }
              if($dsrfh{$fh}){
                  &tell("*\cbE\cb* DCC Send already in progress $f to $n");
                  return;

              }
          }
      }
      $fh=&newfh;
      &tell("*\cbE\cb* Can't open file $f"), return unless open($fh, "<$f");
      my $sockaddr = &listen($lfh) or (close $fh, return);
      if ($ipv6) {
        # XXX: substr is used in order to avoid dying on Linux with older
        # glibc that lacks the scope field from sockaddr_in6 but the kernel
        # has it and returns it from getsockname()
        ($myport, undef) = unpack_sockaddr_in6(substr($sockaddr, 0, 24));
        $mynumber = 0;
      } else {
        ($myport, $mynumber) = unpack_sockaddr_in($sockaddr);
        $mynumber = unpack("N", $mynumber);
      }
      $dswait{$lfh}=$fh;
      $tf=$1 if $f =~ m|/([^/]*)$|;
      $sz=(-s $f);
      $tf =~ s/ /_/g; # we have to convert spaces in the filename to underscores
      &sl("PRIVMSG $n :\caDCC SEND $tf $mynumber $myport $sz\ca");
      &dohooks("send_ctcp", $n, "DCC SEND $tf $mynumber $myport $sz");
      &dohooks("dcc_send", $n, $f, $sz, $fh);
      #&tell("*\cbD\cb* Sent DCC SEND request to $n ($f,$sz)");
      &tell("~!dcc~Sent DCC SEND request to who: $n file: $f size: $sz");
      $dfile{$fh}=$f;
      $dswait{$lfh}=$fh;
      $dnick{$lfh}=$n;
      $dsport{$lfh}=$myport;
      $dsoffset{$lfh}=0;
    } else {
      &tell("*** I can \"only\" do DCC CHAT, RCHAT, GET, SEND, CLOSE, RENAME and LIST, *sheesh*");
    }
  } elsif ($cmd eq 'QUOTE') { #KSIRC MOD
     $args ne '' && &sl($args); #Allow this even if not connected to talk to proxies
  } elsif ($connected<2) {
    &tell("*** You're not connected to a server");
  } elsif ($cmd eq 'AWAY') {
      &sl($args ? "AWAY :$args" : "AWAY");
	  my $oldchannel = $talkchannel;
      if ( $publicAway == 1 ) {
	      foreach $talkchannel (@channels) {
	          &me($args ? "is away: $args" : "is back");
	      }
      }
	  $talkchannel = $oldchannel;
  } elsif ($cmd eq 'NEXT') {
    if ($#channels>0) {
      $talkchannel=shift(@channels);
      push(@channels, $talkchannel);
      !$ssfe && &tell("*** Talking to $talkchannel now");
      &dostatus;
    }
  } elsif ($cmd eq 'SAY' || $cmd eq '') {
    &say($args);
  } elsif ($cmd eq 'NOTICE' || $cmd eq 'NO') {
    &dosplat;
    if ($args) {
      ($newarg, $args)=split(/ /, $args, 2);
      &notice($newarg, $args);
    } else {
      &tell("*\cbE\cb* You must specify a nick or channel!");
    }
  } elsif ($cmd eq 'DESCRIBE' || $cmd eq 'DE') {
    &dosplat;
    if ($args) {
      ($newarg, $args)=split(/ /, $args, 2);
      &describe($newarg, $args);
    } else {
      &tell("*\cbE\cb* You must specify a nick or channel!");
    }
  } elsif ($cmd eq 'KICK' || $cmd eq 'K') {
    &dosplat;
    &getarg;
    local($c)=$talkchannel;
    if ($newarg =~ /^[\#\&\+]/) {
      $c=$newarg;
      &getarg;
    }
    if ($newarg) {
      $args || ($args=$nick);
      &sl("KICK $c $newarg :$args");
    } else {
      &tell("*\cbE\cb* You must specify a nick!");
    }
  } elsif ($cmd eq 'DISCONNECT' || $cmd eq 'DIS') {
    &tell("*** Disconnecting from $server");
    close($S);
    delete $buffer{$S};
    $connected=0;
    &dohooks("disconnect");
    &bindtoserver;
    
  } elsif ($cmd eq 'INVITE' || $cmd eq 'INV' || $cmd eq 'I') {
    local(@ns)=split(/ +/, $args);
    local($l, $c)=(pop(@ns), $talkchannel);
    if ($l =~ /^[\#\&\+]/) {
      $c=$l;
    } else {
      $l && push(@ns, $l);
    }
    foreach (@ns) {
      &sl("INVITE $_ $c");
    }
  } elsif ($cmd eq 'CTCP') {
    &dosplat;
    if ($args) {
      &getarg;
      local($towho)=$newarg;
      &getarg;
      $newarg =~ tr/a-z/A-Z/;
      $args=" ".$args if $args ne '';
      &sl("PRIVMSG $towho :\ca$newarg$args\ca");
      &dohooks("send_ctcp", $towho, $newarg.$args);
      &tell("*** Sending a CTCP $newarg$args to $towho");
    } else {
      &tell("*\cbE\cb* You must specify a nick or channel!");
    }
  } elsif ($cmd eq 'PING' || $cmd eq 'P') {
    &dosplat;
    if ($args) {
      &getarg;
      local($t)=time;
      &sl("PRIVMSG $newarg :\caPING $t\ca");
      &dohooks("send_ctcp", $newarg, "PING $t");
      &tell("*** Sending a CTCP PING to $newarg");
    } else {
      &tell("*\cbE\cb* You must specify a nick or channel!");
    }
  } elsif ($cmd eq 'ME') {
    if ($talkchannel) {
      &describe($talkchannel, $args);
    } else {
      &tell("*\cbE\cb* Not on a channel");
    }
  } elsif ($cmd eq 'TOPIC' || $cmd eq 'T') {
    &dosplat;
    local($c)=$talkchannel;
    if ($args =~ /^[\#\&\+]/) {
      &getarg;
      $c=$newarg;
    }
    if ($args) {
      &sl("TOPIC $c :$args");
    } else {
      &sl("TOPIC $c");
    }
  } elsif ($cmd eq 'LEAVE' || $cmd eq 'PART' || $cmd eq 'HOP') {
    &dosplat;
    $args=$talkchannel if $args eq '';
    &sl("PART $args");
  } elsif ($cmd eq 'LL') {
    if ($talkchannel) {
      &sl("WHO $talkchannel");
    } else {
      &tell("*\cbE\cb* Not on a channel");
    }
  } elsif ($cmd eq 'O' || $cmd eq 'OP') {
    local($c, $n, $l)=($talkchannel, 0, '');
    &getarg, $c=$newarg if ($args =~ /^[\#\&\+]/);
    local(@ppl)=split(/ +/, $args);
    foreach (@ppl) {
      if ($n<4) {
	$l .= " ".$_;
	$n++;
      } else {
	&sl("MODE $c +oooo $l");
	$l=$_;
	$n=1;
      }
    }
    $l && &sl("MODE $c +oooo $l");
  } elsif ($cmd eq 'D' || $cmd eq 'DEOP') {
    local($c, $n, $l)=($talkchannel, 0, '');
    &getarg, $c=$newarg if ($args =~ /^[\#\&\+]/);
    local(@ppl)=split(/ +/, $args);
    foreach (@ppl) {
      if ($n<4) {
	$l .= " ".$_;
	$n++;
      } else {
	&sl("MODE $c -oooo $l");
	$l=$_;
	$n=1;
      }
    }
    $l && &sl("MODE $c -oooo $l");
  } elsif ($cmd eq 'W' || $cmd eq 'WHOIS') {
    &sl($args eq '' ? "WHOIS $nick" : "WHOIS $args");
  } elsif ($cmd eq 'WI') {
    &getarg;
    $newarg=$nick if $newarg eq '';
    &sl("WHOIS $newarg $newarg");
  } elsif ($cmd eq 'WHO') {
    &dosplat;
    if ($args =~ /^[\s\*]*$/) {
      &tell("*** Uhm, better not");
    } else {
      &sl("WHO $args");
    }
  } elsif ($cmd eq 'JOIN' || $cmd eq 'J') {
    $args=$invited if $args eq '';
    if ($args !~ /^[\#\&\+]/) {
      $query = $args;
    }
    elsif (grep(&eq($_, $args), @channels)) {
#      &tell("*** Talking to $args now"); # KSIRC MOD
      $talkchannel=$args;
      $query = "";
      &dostatus;
    } else {
      &sl("JOIN $args");
    }
  } elsif ($cmd eq 'UMODE') {
    &sl("MODE $nick $args");
  } elsif ($cmd eq 'MO') {
    if ($talkchannel) {
      &sl("MODE $talkchannel $args");
    } else {
      &tell("*\cbE\cb* You're not on any channel anyway");
    }
  } elsif ($cmd eq 'LIST') {
    &dosplat;
    $listmin=0;
    $listmax=100000;
    $listpat='';
    if ($args =~ /\*/ || $args =~ /-m[ia][nx]\s/i) {
      while (&getarg, $newarg ne '') {
	if ($newarg =~ /^-min$/i) {
	  &getarg;
	  $listmin=$newarg if $newarg>0;
	} elsif ($newarg =~ /^-max$/i) {
	  &getarg;
	  $listmax=$newarg if $newarg>0;
	} else {
	  $newarg =~ s/([^\\])\./$1\\./g;
	  $newarg =~ s/\*/\.\*/g;
	  $newarg =~ s/([^\.\*\\\w])/\\$1/g;
	  $listpat=$newarg;
	}
      }
      &sl("LIST");
    } else {
      &sl($line);
    }
  } elsif ($cmd eq 'RPING') {
    &getarg;
    &sl("RPING $newarg ".time);
  } elsif ($cmd eq 'KILL') {
    &getarg;
    if ($newarg) {
      $args || ($args=$nick);
      &sl("KILL $newarg :$args");
    } else {
      &tell("*\cbE\cb* You must specify a nick!");
    }
  } elsif ($cmd eq 'MODE' || $cmd eq 'NAMES') {
    &dosplat;
    &sl("$cmd $args");
  } elsif ($cmd eq 'OPER') {
    &getarg;
    $newarg=$nick unless $newarg;
    &getuserpass("Oper password? ", "Passwd: "), $args=$_ unless $args;
    &sl("OPER $newarg $args");
  } elsif ($cmd eq 'CONNECT') {
    &getarg;
    local($srv)=$newarg;
    &getarg;
    if ($args) {
      &sl("CONNECT $srv $newarg $args");
    } else {
      &sl("CONNECT $srv 6667 $newarg");
    }
  } elsif ($cmd eq 'SQUIT') {
    &getarg;
    &sl("SQUIT $newarg :$args");
  } elsif ($cmd eq 'WHOWAS' || $cmd eq 'ADMIN' || $cmd eq 'STATS' ||
           $cmd eq 'INFO' || $cmd eq 'LUSERS' || $cmd eq 'SQUIT' ||
	   $cmd eq 'REHASH' || $cmd eq 'DIE' || $cmd eq 'LINKS' ||
	   $cmd eq 'NOTE' || $cmd eq 'WALLOPS' || $cmd eq 'NICK' ||
	   $cmd eq 'MOTD' || $cmd eq 'TIME' || $cmd eq 'TRACE' ||
	   $cmd eq 'USERS' || $cmd eq 'SILENCE' || $cmd eq 'MAP' ||
	   $cmd eq 'UPING') {
    &sl($line);
  } else {
     # Unknown command sucks. People want to use extensions like /nickserv, which works
     # on some servers (Simon)
     &sl($line);
#    &tell("*\cbE\cb* Unknown command: $cmd");
  }
}

sub douserline {
  local($skip, $line)=(0, @_);
  if ($line =~ /^\@ssfe\@/) {
    $ssfe=$raw_mode=1;
    $add_ons.="+ssfe";
    &dostatus;
  } else {
    &dohooks("command", $line);
    return if $skip;
    if ($line =~ s/^\///) {
      &docommand($line);
    } elsif ($query ne '') {
      &msg($query, $line);
    } else {
      &say($line);
    }
  }
}

$ssfe_getline="`#ssfe#p";
sub getuserline {
  local($skip)='';
  &dohooks("input", $_[0], $_[1]);
  return if $skip;
  print $_[0];
  print "\n" if $raw_mode;
  print $ssfe_getline.$_[1]."\n" if $ssfe;
  while (($_=<STDIN>) ne '') {
    if (/^\@ssfe\@/) {
      $ssfe || ($add_ons.="+ssfe");
      $ssfe=$raw_mode=1;
      &dostatus;
    } else {
      &exit if $_ eq '';
      chop;
      return;
    }
  }
  &exit;
}

sub getuserpass {
  local($ssfe_getline)="`#ssfe#P";
  &getuserline;
}

%cmds=();
sub addcmd {
  local($cmd)=$_[0];
  $cmd =~ tr/a-z/A-Z/;
  $cmds{$cmd}="&cmd_".$_[0].";";
}

sub addhelp {
  local($cmd, $txt)=@_;
  $cmd =~ tr/A-Z/a-z/;
  foreach (reverse(split(/\n/, $txt))) {
    s/\$v/$version/g;
    s/\$d/$date/g;
    unshift (@help, $_);
  }
  unshift(@help, "\@".$cmd);
}

sub addset {
  local($var)=$_[0];
  $var =~ tr/a-z/A-Z/;
  $sets{$var}="set_".$_[0];
}

sub addsel {
  $buf_fds{$_[0]}="sel_".$_[1] if $_[2];
  $sel_fds{$_[0]}="sel_".$_[1] unless $_[2];
}

sub remsel {
  delete $buf_fds{$_[0]};
  delete $sel_fds{$_[0]};
}

sub addwsel {
  $sel_w_fds{$_[0]}="sel_".$_[1];
}

sub remwsel {
  delete $sel_w_fds{$_[0]};
}

@hooks=("action", "ctcp", "ctcp_reply", "dcc_chat", "dcc_request", "input",
	"invite", "join", "kick", "leave", "mode", "msg", "nick", "notice",
	"server_notice", "notify_signoff", "notify_signon", "public",
	"raw_irc", "send_action", "send_dcc_chat", "send_text", "send_notice",
	"signoff", "topic", "disconnect", "status", "print", "command",
        "chat_disconnect", "dcc_disconnect", "send_ctcp",
        "dcc_send", "dcc_send_status", "dcc_get", "dcc_get_status", "quit",
        "pong"); # ksirc additions

sub addhook {
  local($type, $name)=@_;
  $type =~ tr/A-Z/a-z/;
  $name="hook_".$name;
  if ($type =~ /^\d\d\d$/ || grep(($_ eq $type), @hooks)) {
    ($type =~ /^\d\d\d$/) && ($type="num_".$type);
    eval "*ugly_hack_hooks=*${type}_hooks;";
    unless (grep(($_ eq $name), @ugly_hack_hooks)) {
      push(@ugly_hack_hooks, $name);
    }
  } else {
    &tell("*\cbE\cb* $type: no such hook");
  }
}

sub remhook {
  local($type, $name)=@_;
  $type =~ tr/A-Z/a-z/;
  $name="hook_".$name;
  if ($type =~ /^\d\d\d$/ || grep(($_ eq $type), @hooks)) {
    ($type =~ /^\d\d\d$/) && ($type="num_".$type);
    eval "*ugly_hack_hooks=*${type}_hooks;";
    @ugly_hack_hooks=grep(($_ ne $name), @ugly_hack_hooks);
  } else {
    &tell("*\cbE\cb* $type: no such hook");
  }
}

sub userhost {
  push (@waituh, $_[0]);
  push (@douh, $_[1]);
  push (@erruh, $_[2]);
  &sl("USERHOST $_[0]");
}

sub deltimer {
  local($ref)=$_[0];
  local($i);
  if ($#trefs>=0 && $ref!=0) {
    # delete the timer if it exists
    for ($i=0; $i<=$#trefs; $i++) {
      if ($trefs[$i]==$ref) {
        splice(@trefs,$i,1);
        splice(@timers,$i,1);
        splice(@timeactions,$i,1);
        last;
      }
    }
  }
}

sub timer {
  local(@r, @t, @a)=();
  local($t)=$_[0]+time;
  local($ref)=$_[2] || 0;
  &deltimer($ref) if $ref;
  while ($#timers>=0 && $timers[0]<=$t) {
    push (@r, shift(@trefs));
    push (@t, shift(@timers));
    push (@a, shift(@timeactions));
  }
  @trefs=(@r, $ref, @trefs);
  @timers=(@t, $t, @timers);
  @timeactions=(@a, $_[1], @timeactions);
}

sub disappeared {
  local($n)=(grep(&eq($_, $_[0]), keys(%notify)));
  if ($n ne '' && $notify{$n}>0) {
    local($silent)=0;
    &dohooks("notify_signoff", $_[0]);
    &tell("*\cb(\cb* Signoff by $_[0] detected");
    $notify{$n}=0;
  }
}

sub appeared {
  local($t, $n)=(time, grep(&eq($_, $_[0]), keys(%notify)));
  if ($n ne '') {
    if ($notify{$n}==0) {
      local($silent)=0;
      &dohooks("notify_signon", $_[0]);
      &tell("*\cb)\cb* Signon by $_[0] detected!");
    }
    else {
#      &tell("*\cb(\cb* Signoff by $_[0] detected!");
    }
    $notify{$n}=$t;
  }
}

$lastsendison=0;
sub send_isons {
  local($l)='';
  foreach (keys %notify) {
    &sl("ISON : $l"), $l='' if (length($l)>500);
    $l.=$_." ";
  }
  &sl("ISON :$l") if $l;
  $lastsendison=time;
  $newisons='';
  $checkisons=1;
}

sub signoffs {
  foreach (keys %notify) {
    if ($notify{$_}>0 && $notify{$_}<$lastsendison) {
      $notify{$_}=0;
      local($silent)=0;
      &dohooks("notify_signoff", $_);
      &tell("*\cb(\cb* Signoff by $_ detected");
    }
  }
  $checkisons='';
}

sub modestripper {
  local($chnl, $what)=@_;
  $chnl =~ tr/A-Z/a-z/;
  local($how, $modes, @args)=('+', split(/ +/, $what));
  foreach $m (split(//, $modes)) {
    if ($m =~ /[\-\+]/) {
      $how=$m;
    } elsif ($m =~ /[vb]/) {
      shift(@args);
    } elsif ($m eq 'k') {
      $how eq '+' ? ($chankey{$chnl}=$args[0]) : delete $chankey{$chnl};
      shift(@args);
    } elsif ($m eq 'l') {
      $how eq '+' ? ($limit{$chnl}=shift(@args)) : delete $limit{$chnl};
    } elsif ($m eq 'o') {
      $haveops{$chnl}=($how eq '+') if (&eq(shift(@args), $nick));
    } else {
      $mode{$chnl} =~ s/$m//g;
      $mode{$chnl}.=$m if $how eq '+';
    }
  }
}

sub umodechange {
  local($what)=@_;
  local($how)='+';
  foreach $m (split(//, $what)) {
    if ($m =~ /[\-\+]/) {
      $how=$m;
    } else {
      $umode =~ s/$m//g;
      $umode.=$m if ($how eq '+' && $m !~ /\s/);
    }
  }
}

sub ignored {
  foreach (@ignore) {
    return 1 if $_[0] =~ /^${_}$/;
  }
  return '';
}

sub dorcfile {
  return if !open(RCFILE, "<$_[0]");
  while (<RCFILE>) {
    chop;
    s/^\///;
    next if /^\#/;
    &docommand($_) if $_;
    $silent=$skip='';
  }
  close RCFILE;
}

sub loadrc {
  $rcloaded=1; 
  $sysrc && &dorcfile($sysrc);
  $rcfile && &dorcfile($rcfile);
}

sub selline {
  $leftover=0;
  $rin=$rout="\0" x 32;
  $win=$wout="\0" x 32;
  foreach ($S, 'STDIN', keys(%dcnick), keys(%buf_fds)) {
    $leftover=1, return $_ if $buffer{$_} =~ /\n/;
  }
  foreach ('STDIN', keys(%dcnick), keys(%dcwait), keys(%dgrfh), keys(%dswait),
		keys(%dsrfh), keys(%sel_fds), keys(%buf_fds)) {
    vec($rin, fileno($_), 1)=1;
  }
  foreach (keys(%sel_w_fds)){
    vec($win, fileno($_), 1)=1;
  }
  vec($rin, fileno($S), 1)=1 if $connected;
  if ($#timers<0 || $timers[0]>time+30) {
    select($rout=$rin, $wout=$win, undef, 30);
  } elsif ($timers[0]<=time) {
    select($rout=$rin, $wout=$win, undef, 0);
  } else {
    select($rout=$rin, $wout=$win, undef, $timers[0]-time);
  }
}

sub getnick {
  if ($ENV{'BACKUPNICK'} && !($nick eq $ENV{'BACKUPNICK'})) {
    $nick=$ENV{'BACKUPNICK'};
  } else {  
    &getuserline("Pick a nick: ", "Nick: ");
    $nick=$_;
  }
  &sl("NICK $nick");
  &dostatus;
}

sub donumeric {
  local($from)=($who eq $myserver ? '' : " (from ${who})");
  if ($cmd eq '401') {
    &yetonearg;
    &yetonearg;
    &tell("*\cb?\cb* Cannot find $newarg on irc$from");
  } elsif ($cmd eq '402') {
    &yetonearg;
    &yetonearg;
    &tell("*\cb?\cb* $newarg: no such server$from");
  } elsif ($cmd eq '403') {
    &yetonearg;
    &yetonearg;
    &tell("*\cb?\cb* $newarg: no such channel$from");
  } elsif ($cmd eq '406') {
    &yetonearg;
    &yetonearg;
    &tell("*\cb?\cb* $newarg: there was no such nickname$from");
  } elsif ($cmd eq '421') {
    &yetonearg;
    &yetonearg;
    &tell("*\cb?\cb* $newarg: unknown command$from");
  } elsif ($cmd =~ /^4[012]/) {
    $args =~ s/^[^:]*://;
    &tell("*** $args$from");
  } elsif ($cmd eq '431') {
    &tell("*** Was expecting a nickname somewhere...");
    &getnick if $connected<2;
  } elsif ($cmd eq '432') {
    if ($connected==2) {
      &tell("*\cbN\cb* Invalid nickname, you're still \"$nick\"");
    } else {
      &tell("*\cbN\cb* Invalid nickname!");
      &getnick;
    }
  } elsif ($cmd eq '433') {
    if ($connected==2) {
      &tell("*\cbN\cb* Nick already taken, you're still \"$nick\"");
    } else {
      &tell("*\cbN\cb* Nick already taken!");
      &getnick;
    }
  } elsif ($cmd eq '441') {
    local($g, $w, $c)=split(/ +/, $args);
    &tell("*\cbE\cb* $w is not on channel $c$from");
  } elsif ($cmd eq '442') {
    local($w, $c)=split(/ +/, $args);
#    &tell("*\cbE\cb* You're not on channel $c$from");  # KSIRC MOD
  } elsif ($cmd eq '443') {
    local($w, $o, $c)=split(/ +/, $args);
    &tell("*\cbE\cb* $o is already on channel $c$from");
  } elsif ($cmd eq '465') {
    &tell("*\cbE\cb* You are banned from this server$from");
  } elsif ($cmd eq '461') {
    &yetonearg;
    &yetonearg;
    &tell("*\cbE\cb* The command $newarg needs more arguments than that$from");
  } elsif ($cmd =~ /^47[1345]$/) {
    &yetonearg;
    &yetonearg;
    local($r);
    if ($cmd eq '471') {
      $r="channel is full";
    } elsif ($cmd eq '473') {
      $r="channel is invite-only";
    } elsif ($cmd eq '474') {
      $r="banned from channel";
    } else {
      $r="bad channel key";
    }
    &tell("*\cbE\cb* Can't join $newarg: ${r}$from");
  } elsif ($cmd eq '301') {
    &yetonearg;
    &yetonearg;
    &tell("*** $newarg is away: $args");
  } elsif ($cmd eq '302') {
    &yetonearg;
    &yetonearg;
    local($n, $do, $err)=(shift(@waituh), shift(@douh), shift(@erruh));
    if ($newarg =~ /^([^\s\*=]+)[\*]?=([\-+])/) {
      $who=$1;
      local($adr)=$';
      if ($adr =~ /\@/) {
	$user=$`;
	$host=$';
      } else {
	$user=$host='';
      }
      if (&eq($who, $n)) {
	eval $do;
	$@ =~ s/\n$//, &tell("*\cbE\cb* error in userhost: $@") if $@ ne '';
      } else {
	&tell("*\cbE\cb* userhost returned for unexpected nick $who");
      }
    } else {
      if (defined($err)) {
	eval $err;
	$@ =~ s/\n$//, &tell("*\cbE\cb* error in userhost: $@") if $@ ne '';
      } else {
	&tell("*\cb?\cb* Cannot find $n on irc");
      }
    }
  } elsif ($cmd eq '303') {
    &yetonearg;
    local($n);
    foreach $n (split(/ +/, $args)) {
      &appeared($n);
    }
  } elsif ($cmd eq '305') {
    &tell("*** You are no longer marked as away");
    $away='';
    &dostatus;
  } elsif ($cmd eq '306') {
    &tell("*** You are marked as being away");
    $away=1;
    &dostatus;
  } elsif ($cmd eq '311') {
    local($g, $n, $u, $m, $g, $r)=split(/ +/, $args, 6);
    $r =~ s/^://;
    &tell("*** $n is $u\@$m ($r)");
  } elsif ($cmd eq '312') {
    &yetonearg;
    &yetonearg;
    &yetonearg;
    local($s)=$newarg;
    &tell("*** on IRC via server $s ($args)");
  } elsif ($cmd eq '313') {
    &yetonearg;
    &yetonearg;
    &tell("*** $newarg $args");
  } elsif ($cmd eq '314') {
    local($g, $n, $u, $m, $g, $r)=split(/ +/, $args, 6);
    $r =~ s/^://;
    &tell("*** $n was $u\@$m ($r)");
  } elsif ($cmd eq '317') {
    &yetonearg;
    &yetonearg;
    local($n)=$newarg;
    &yetonearg;
    if ($newarg>=3600) {
      &tell("*** $n has been idle for ".int($newarg/3600)." hours, ".
	  int(($newarg%3600)/60)." minutes and ".
	  ($newarg%60)." seconds");
    } elsif ($newarg>=60) {
      &tell("*** $n has been idle for ".int($newarg/60)." minutes and ".
	    ($newarg%60)." seconds");
    } else {
      &tell("*** $n has been idle for $newarg seconds");
    }
  } elsif ($cmd eq '319') {
    local($g, $g, $c)=split(/ +/, $args, 3);
    $c =~ s/^://;
    &tell("*** on channels: $c");
  } elsif ($cmd eq '322') {
    local($g, $c, $n, $r)=split(/ +/, $args, 4);
    $r =~ s/^://;
    $n>=$listmin && $n <=$listmax && (!$listpat || $c =~ /^${listpat}$/i)
      && &tell(sprintf("*** %-10s %-5s %s", $c, $n, $r));
  } elsif ($cmd eq '323') {
    $listmin=0;
    $listmax=100000;
    $listpat='';
  } elsif ($cmd eq '324') {
    local($g, $c, $m)=split(/ +/, $args, 3);
    $m =~ s/^://;
    $m =~ s/ $//;
    $c =~ tr/A-Z/a-z/;
    if (grep(&eq($_, $c), @channels)) {
      if (defined($mode{$c})) {
	&tell("*\cb+\cb* Mode for channel $c is \"$m\"");
      } else {
	$mode{$c}='';
      }
      &modestripper($c, $m);
      &dostatus;
    } else {
      &tell("*\cb+\cb* Mode for channel $c is \"$m\"");
    }
  } elsif ($cmd eq '329') {
    &yetonearg;
    &yetonearg;
    local($c)=$newarg;
    &yetonearg;
    local($t)=($newarg ? ("created " . &date($newarg)) : "0 TS");
    &tell("*** $c : $t");
  } elsif ($cmd eq '331') {
    &yetonearg;
    &yetonearg;
    &tell("*\cbT\cb* No topic is set on channel $newarg");
  } elsif ($cmd eq '332') {
    &yetonearg;
    &yetonearg;
    &tell("*\cbT\cb* Topic for $newarg: $args");
  } elsif ($cmd eq '333') {
    local($g, $c, $n, $t)=split(/ +/, $args, 4);
    local($d)=&date($t);
    &tell("*\cbT\cb* Topic for $c set by $n on $d");
  } elsif ($cmd eq '318' || $cmd eq '315' || $cmd eq '369' ||
                $cmd eq '321' || $cmd eq '376' || # KSIRC MOD
	   $cmd eq '365' || $cmd eq '368' || $cmd eq '374' ||
	   $cmd eq '219' || $cmd eq '007') {
    #nothing!
  } elsif ($cmd eq '341') {
    local($g, $n, $c)=split(/ +/, $args, 3);
    &tell("*\cbI\cb* Inviting $n to channel $c");
  } elsif ($cmd eq '352') {
    local($g, $c, $u, $m, $s, $n, $st, $g, $i)=split(/ +/, $args, 9);
    &tell(sprintf("%-10s %-9s %4s %s\@%s (%s)", $c, $n, $st, $u, $m, $i));
  } elsif ($cmd eq '353') {
    local($g, $m, $c, $r)=split(/ +/, $args, 4);
    local($n)=$nick;
    $n =~ s/(\W)/\\$1/g;
    $r =~ s/^://;
    if($DSIRC_NAMES eq ''){ #KSIRC MOD
      &tell("*I* Users on $c: $r"); # KSIRC MOD
      $DSIRC_NAMES = $c; # KSIRC MOD
    }                    # KSIRC MOD
    else {               # KSIRC MOD
        &tell("*\cbI\cb* Users on $c: $r"); # KSIRC MOD
    }                                           # KSIRC MOD
    $c =~ tr/A-Z/a-z/;
    $haveops{$c}=1 if ($r =~ /\@${n}( |$)/i);
    &dostatus if &eq($c, $talkchannel);
  } elsif ($cmd eq '366'){                      # KSIRC MOD
    #&tell("*I* Users on $DSIRC_NAMES:"); # KSIRC MOD
    $DSIRC_NAMES = '';                                   # KSIRC MOD
  } elsif ($cmd eq '221') {
    &yetonearg;
    &tell("*\cb+\cb* Your user mode is \"$args\"");
  } elsif ($cmd eq '200') {
    local($b, $l, $v, $n, $s)=split(/ +/, $args);
    $s =~ s/^://;
    &tell("*** $l $who ($v) ==> $n $s");
  } elsif ($cmd eq '205') {
    local($b, $u, $h, $n)=split(/ +/, $args);
    $n =~ s/^://;
    &tell("*** $u [$h] ==> $n");
  } elsif ($cmd =~ /^20/) {
    local($b, $t, $n, $r)=split(/ +/, $args, 4);
    &tell("*** $t [$n] ==> $r");
  } elsif ($cmd eq '375' || $cmd eq '372' || $cmd =~ /^25/) {
    &yetonearg;
    &tell("*** $args");
  } elsif ($cmd eq '379' ) { # RPL_FORWARD (Simon)
    &yetonearg;
    local( $from_channel, $to_channel ) = split( / +/, $args );
    &tell("~$from_channel~*\cb<\cb* You have left channel $from_channel");
  } else {
    &yetonearg;
    #$args =~ s/ :/ /;
    &tell("*** $args$from");
  }
}

# main prog

print "`#ssfe#i\n" unless (-t STDOUT);
&tell("*** Welcome to \cbsirc\cb version $version; type /help for help");

&load($sysinit) if $sysinit ne '' && -f $sysinit;
&load($initfile) if !$restrict && $initfile ne '' && -f $initfile;

while (1) {
  &bindtoserver, undef $ready if $ready;
  $silent=$skip='';
  if ($connected==2) {
    $time=time;
    &loadrc unless $rcloaded;
    &send_isons
      if $time>=$lastsendison+90 || ($newisons && $time>=$lastsendison+10);
    &signoffs if $checkisons && ($time>=$lastsendison+30);
  }
  $fh=&selline;
  foreach $rfh (keys (%buf_fds)) {
    if (vec($rout, fileno($rfh), 1) || ($leftover && $fh eq $rfh)) {
      &gl($rfh) || next;
      local($line, $h)=($_, $buf_fds{$rfh});
      delete $buf_fds{$rfh}, delete $buffer{$rfh}, close($rfh) if $_ eq '';
      eval { &$h($line); };
      $@ =~ s/\n$//, &tell("*\cbE\cb* error in buffered fd hook &$h: $@")
	if $@ ne '';
    }
  }
  foreach $rfh (keys (%sel_fds)) {
    if (vec($rout, fileno($rfh), 1)) {
      local($h)=$sel_fds{$rfh};
      eval { &$h($rfh); }; #KSIRC MOD
      $@ =~ s/\n$//, &tell("*\cbE\cb* error in unbuffered fd hook &$h: $@")
	if $@ ne '';
    }
  }
  foreach $rfh (keys (%sel_w_fds)) {
    if (vec($wout, fileno($rfh), 1)) {
      local($h)=$sel_w_fds{$rfh};
      eval { &$h($rfh); }; #KSIRC MOD
      $@ =~ s/\n$//, &tell("*\cbE\cb* error in unbuffered fd hook &$h: $@")
	if $@ ne '';
    }
  }
  foreach $rfh (keys (%dcnick)) {
    if (vec($rout, fileno($rfh), 1) || ($leftover && $fh eq $rfh)) {
      &gl($rfh) || next;
      &dcerror($rfh), next if $_ eq '';
      chop;
      local($who, $what)=($dcnick{$rfh}, $_);
      $dcvol{$dcnick{$rfh}}+=length($what);
      print "`#ssfe#t/m =$who \n" if $ssfe;
      print "`#ssfe#o=${who}= $what\n" if $ssfe;
      &dohooks("dcc_chat", $who, $what);
      &tell("~=${who}~=\cb${who}\cb= $what"); # KSIRC MOD
      $silent='';
    }
  }
  foreach $rfh (keys (%dcwait)) {
    if (vec($rout, fileno($rfh), 1)) {
      local($n, $fh);
      my $paddr;
      if ($paddr = &accept($fh, $rfh)) {
        select($fh); $|=1; select(STDOUT);
        my($port,$iaddr) = sockaddr_in($paddr);
        my $ip = inet_ntoa($iaddr);
	$n=$dcwait{$rfh};
	$dcnick{$fh}=$n;
	$n =~ tr/A-Z/a-z/;
	$dcvol{$n}=0;
	$dcfh{$n}=$fh;
        &tell("*\cbD\cb* DCC CHAT connection with $n established");
        &tell("~!dcc~DCC CHAT inbound established who: $n ip: $ip");
	print "`#ssfe#t/m =$n \n" if $ssfe;
      }
      delete $dcwait{$rfh};
    }
  }
  foreach $sfh (keys (%dswait)) {
    local($rfh, $fh)=$dswait{$sfh};
    if (vec($rout, fileno($sfh), 1)) {
      my $paddr;  
      if ($paddr = &accept($fh, $sfh)) {
        my($port,$iaddr) = sockaddr_in($paddr);
        my $ip = inet_ntoa($iaddr);
	select($fh); $|=1; select(STDOUT);
	$dsrfh{$fh}=$rfh;
	$dstarttime{$rfh}=time;
	$dtransferred{$fh}=0;
        $dnick{$fh}=$dnick{$sfh};
        $dsoffset{$fh}=$dsoffset{$sfh};
        &tell("*\cbD\cb* DCC SEND connection with $dnick{$sfh}/$ip ($dfile{$rfh}) established");
        &tell("~!dcc~DCC SEND established who: $dnick{$sfh} file: $dfile{$rfh} ip: $ip");
      }
      delete $dnick{$sfh};
      delete $dswait{$sfh};
      delete $dsoffset{$sfh};
      delete $dsport{$sfh};
    }
  }
  foreach $sfh (keys (%dgrfh)) {
    local($rfh)=$dgrfh{$sfh};
    if (vec($rout, fileno($sfh), 1)) {
      local($a, $buf)=(0, '');
      $a=sysread($sfh, $buf, 4096);
      if ($a) {
        $dtransferred{$sfh}+=$a;
        &dohooks("dcc_get_status", $dfile{$rfh}, $dtransferred{$sfh}, $rfh);
        #	&tell("*\cbD\cb* DCC GET read: $dfile{$rfh} bytes: $dtransferred{$sfh}"); # KSIRC MOD FOR 971217
        my $b = $dtransferred{$sfh}+$dgxferadd{$sfh};
        &tell("~!dcc~DCC GET read: $dfile{$rfh} who: $dnick{$sfh} bytes: $b"); # KSIRC MOD FOR 971217
        print $rfh $buf;
        print $sfh pack("N", $b); # used to be just $dtransfered but most seem to want xfet + offset        
      } else {
	&dgsclose($sfh, $rfh, "GET", "OK");
      }
    }
  }
  foreach $sfh (keys (%dsrfh)) {
    local($rfh)=$dsrfh{$sfh};
    if (vec($rout, fileno($sfh), 1) || !$dtransferred{$sfh}) {
      local($ack, $csa, $buf, $b, $l, $w)=(0, '', '');
      if ($dtransferred{$sfh}) {
          &dgsclose($sfh, $rfh, "SEND", "Protocol Error"), next if sysread($sfh, $b, 4)!=4;
          $ack=unpack("N", $b);
      }
      if($ack > ($dtransferred{$sfh} + $dsoffset{$sfh})){
          my $v = $dtransferred{$sfh} + $dsoffset{$sfh};
          &tell("*\cbD\cb* DCC transfer protocol failure! $ack $dtransferred{$sfh} $dsoffset{$sfh} $v");
          &dgsclose($sfh, $rfh, "SEND", "Protocol Out of Sync");
          next;
      }
      #
      # When you do a dcc resume the ack value returned from the
      # remote client is not well defined.  Two different values
      # are used, the current number of bytes transfered, or
      # the current location in the file.  We try to detech
      # which type of ack we got and we adjust our math
      # according so we keep up nice packet sizes.
      # xchat can't seem to take > 4k packets after a resume
      # and it causes the backoff to ack a little funny, but
      # it's not our fault!
      #
      if($dsoffset{$sfh} && ($ack != 0) && ($dsresumedb{$sfh} == undef)) {
          if($ack > $dsoffset{$sfh}){
              $dsresumedb{$sfh} = 1;
          }
          else {
              $dsresumedb{$sfh} = 2;
          }
          #&print("*** Resume style is: $dsresumedb{$sfh}");
          
      }
      if($dsoffset{$sfh} && ($dsresumedb{$sfh} == 1)){
          $csa=$set{"SENDAHEAD"}-($dtransferred{$sfh}+$dsoffset{$sfh})+$ack;
      }
      else {
          $csa=$set{"SENDAHEAD"}-$dtransferred{$sfh}+$ack;
      }
      #&print("*** CSA is: $csa ack: $ack dt: $dtransferred{$sfh} $dsoffset{$sfh}");
      next if $csa<0;
      $l=read($rfh, $buf, 512+$csa);
      $w=syswrite($sfh, $buf, $l) if $l;
      &dohooks("dcc_send_status", $dfile{$rfh}, $dtransferred{$sfh}, $rfh);
      #      &tell("*\cbD\cb* DCC SEND write: $dfile{$rfh} bytes: $dtransferred{$sfh}"); # KSIRC MOD FOR 971218
      my $sz = $dtransferred{$sfh}+$dsoffset{$sfh};
      &tell("~!dcc~DCC SEND write: $dfile{$rfh} who: $dnick{$sfh} bytes: $sz"); # KSIRC MOD FOR 971218
      next if $l==0 && $ack<$dtransferred{$sfh};
      $dtransferred{$sfh}+=$w;
      &dgsclose($sfh, $rfh, "SEND", "OK"), next if ($w<$l || $l==0);
    }
  }
  while ($#timers>=0 && $timers[0]<=time) {
    shift (@timers);
    shift (@trefs);
    eval shift (@timeactions);
    $@ =~ s/\n$//, &tell("*\cbE\cb* error in timer: $@") if $@ ne '';
  }
  if (vec($rout, fileno(STDIN), 1) || ($leftover && $fh eq 'STDIN')) {
    &gl('STDIN') || next;
    &exit if $_ eq '';
    chop;
    $logging && print LOG "<- " . $_ . "\n";
    &douserline($_) if $_ ne '';
  }
  if ($connected && (($leftover && $fh eq $S) || vec($rout, fileno($S), 1))) {
    &gl($S) || next;
    if ($_ eq '') {
      &tell("*\cbE\cb* Connection to server lost");
      close($S);
      delete $buffer{$S};
      $connected=0;
      &dohooks("disconnect");
      &bindtoserver;
      next;
    }
    chop;
    $logging && print LOG ">> " . $_ . "\n";
    $serverline=$_;
    $_=$server." ".$_ unless /^:/;
    ($who, $cmd, $args)=split(/ /, $_, 3);
    $cmd =~ tr/a-z/A-Z/;
    $who =~ s/^://;
    $args =~ s/^://;
    $user=$host=$puh1=$puh2='';
    if ($who =~ /^([^!@ ]+)!([^@ ]+)@([^ ]+)$/) {
      ($who, $user, $host) = ($1, $2, $3);
      $puh1="!$user\@$host" if $set{"PRINTUH"} ne 'none';
      $puh2=$puh1 if $set{"PRINTUH"} eq 'all';
    }
    &dohooks("raw_irc", $cmd, $args);
    next if $skip;
    next if (($cmd eq 'PRIVMSG' || $cmd eq 'NOTICE') &&
		&ignored("$who!$user\@$host"));
    if ($cmd eq '001') {
      $connected=2;
      $myserver=$who;
      ($nick)=split(/ /, $args, 2);
    }
    if ($cmd =~ /^\d\d\d$/) {
      &dohooks("num_".$cmd, $args);
      next if $skip;
      &donumeric;
    } elsif ($cmd eq 'PING') {
      &sl("PONG $args");
    } elsif  ($cmd eq 'PRIVMSG') {
      &yetonearg;
      if ($args =~ /^\001([^\001]*)\001$/ && $set{'CTCP'} ne 'none') {
	&ctcp($newarg, $1);
      } elsif (!$printchan && &eq($newarg, $talkchannel)) {
	&dohooks("public", $newarg, $args);
	&tell("~${newarg}~<${who}> $args");  # MOD FOR KSIRC
      } elsif ($newarg =~ /^[\#\&\+]/) {
	&dohooks("public", $newarg, $args);
	&tell("~${newarg}~<${who}> $args");  # MOD FOR KSIRC
      } elsif (&eq ($newarg, $nick)) {
	print "`#ssfe#t/m $who \n" if $ssfe;
	print "`#ssfe#o[$who$puh1] $args\n" if $ssfe;
	&dohooks("msg", $args);
	&tell("~${who}~[\cb${who}\cb${puh1}] $args");   # MOD FOR KSIRC
      } else {
	&tell("~${who}~[\cb${who}\cb${puh1}\cb] $args");   # MOD FOR KSIRC
      }
    } elsif ($cmd eq 'NOTICE') {
      &yetonearg;
      if ($args =~ /^\001([^\001]*)\001$/) {
	&ctcpreply($newarg, $1);
      } elsif ($newarg =~ /^[\#\&\+]/) {
	&dohooks("notice", $newarg, $args);
	&tell("~${newarg}~-${who}- $args");  # MOD FOR KSIRC
      } elsif ($who =~ /\./) {
	&dohooks("server_notice", $args);
        $args="*** ".$args unless ($args =~ /^\*/);
	&tell($args);
      } elsif (&eq($newarg, $nick)) {
	&dohooks("notice", $newarg, $args);
	&tell("~${who}~-\cb${who}\cb${puh1}- $args");   # MOD FOR KSIRC
      } else {
	&dohooks("notice", $newarg, $args);
	&tell("~${who}~-\cb$who$puh1\cb- $args");  # MOD FOR KSIRC
      }
      $newarg =~ s/\cg.*//;	# ircnet kludge
    } elsif ($cmd eq 'KICK') {
      &yetonearg;
      local($channel)=$newarg;
      &yetonearg;
      $args=$who unless $args;
      if (&eq($nick, $newarg)) {
	&tell("~${channel}~*\cb<\cb* You have been kicked off channel $channel by $who$puh2 ($args)");   # MOD FOR KSIRC
	@channels=grep(!&eq($_, $channel), @channels);
	if (@channels) {
	  $talkchannel=$channels[$#channels];
	} else {
	  $talkchannel='';
	}
	$channel =~ tr/A-Z/a-z/;
	&dohooks("kick", $newarg, $channel, $args);
	delete $mode{$channel};
	delete $limit{$channel};
	delete $haveops{$channel};
	delete $chankey{$channel};
	$talkchannel && !$ssfe && &tell("*** Talking to $talkchannel now");
	&dostatus;
      } else {
	&dohooks("kick", $newarg, $channel, $args);
	&tell("~${channel}~*\cb<\cb* $newarg has been kicked off channel $channel by $who$puh2 ($args)");   # MOD FOR KSIRC
      }
    } elsif ($cmd eq 'PART') {
      &yetonearg;
      if (&eq($who, $nick)) {
        #&tell("~!all~*\cb<\cb* You have left channel $newarg");   # MOD FOR KSIRC
	@channels=grep(!&eq($_, $newarg), @channels);
	if (@channels) {
	  $talkchannel=$channels[$#channels];
	} else {
	  $talkchannel='';
	}
	$newarg =~ tr/A-Z/a-z/;
	delete $mode{$newarg};
	delete $limit{$newarg};
	delete $haveops{$newarg};
	delete $chankey{$newarg};
	&dohooks("leave", $newarg);
	$talkchannel && !$ssfe && &tell("*** Talking to $talkchannel now");
	&dostatus;
      } else {
	&dohooks("leave", $newarg);
	&tell("~${newarg}~*\cb<\cb* $who$puh2 has left channel $newarg");   # MOD FOR KSIRC
      }
    } elsif ($cmd eq 'JOIN') {
      &yetonearg;
      if (&eq($nick, $who)) {
        $newarg =~ tr/A-Z/a-z/;
	push(@channels, $newarg);
	$talkchannel=$newarg;
	&dohooks("join", $newarg);
	&dostatus;
        &tell("~${newarg}~*\cb>\cb* You have joined channel $newarg");   # MOD FOR KSIRC
	&sl("MODE $newarg");
      } else {
	&dohooks("join", $newarg);
	&tell("~${newarg}~*\cb>\cb* $who ($user\@$host) has joined channel $newarg");   # MOD FOR KSIRC
      }
      &appeared($who);
    } elsif ($cmd eq 'NICK') {
      &yetonearg;
      if (&eq($nick, $who)) {
	$oldnick = $nick;
	$nick=$newarg;
	&dohooks("nick", $newarg);
	$who=$newarg;
	&dostatus;
	&tell("~!all~*\cbN\cb* $oldnick is now known as $newarg");
      } else {
	&dohooks("nick", $newarg);
	&tell("~!all~*\cbN\cb* $who$puh2 is now known as $newarg");   # MOD FOR KSIRC
      }
    } elsif ($cmd eq 'MODE') {
      &yetonearg;
      $args =~ s/ $//;
      if ($newarg =~ /^[\#\&\+]/) {
	&modestripper($newarg, $args);
	&dohooks("mode", $newarg, $args);
	&dostatus;
	&tell("~${newarg}~*\cb+\cb* Mode change \"$args\" on channel $newarg by $who$puh2");   # MOD FOR KSIRC
      } else {
	local($towho)=$newarg;
	&yetonearg;
	&umodechange($newarg), &dostatus if &eq($towho, $nick);
	&dohooks("mode", $towho, $newarg);
	&tell("*\cb+\cb* Mode change \"$newarg\" for user $towho by $who");   # MOD FOR KSIRC
      }
    } elsif ($cmd eq 'KILL') {
      &yetonearg;
      local($n)=$newarg;
      $args || ($args=$who);
      &tell("~${newarg}~*\cb<\cb* $n got killed by $who$puh1 ($args)");   # MOD FOR KSIRC
    } elsif ($cmd eq 'INVITE') {
      &yetonearg;
      &yetonearg;
      &dohooks("invite", $newarg);
      $invited=$newarg;
      &tell("~!default~*\cbI\cb* $who$puh1 invites you to channel $newarg");  # MOD FOR KSIRC
    } elsif ($cmd eq 'TOPIC') {
      &yetonearg;
      &dohooks("topic", $newarg, $args);
      &tell("~${newarg}~*\cbT\cb* $who$puh2 has changed the topic on channel $newarg to \"$args\"");   # MOD FOR KSIRC
    } elsif ($cmd eq 'SILENCE') {
      &tell("*** Silence $args");
    } elsif ($cmd eq 'PONG') {
      &dohooks("pong", $args);
    } elsif ($cmd eq 'QUIT') {
      &dohooks("signoff", $args);
      &tell("~!all~*\cb<\cb* Signoff: $who$puh2 ($args)");   # MOD FOR KSIRC
      &disappeared($who);
    } elsif ($cmd eq 'WALLOPS') {
      &tell("!$who$puh2! ".$args);
    } elsif ($cmd eq 'RPONG') {
      local($n, $t, $ms, $ts)=split(/ +/, $args);
      $ts =~ s/^://;
      &tell("*** RPONG: $who - $t: $ms ms, ".time-$ts." sec");
    } else {
      &tell("*** The server says: $serverline");
    }
  }
}