File: server.c

package info (click to toggle)
ircii 20210328-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,644 kB
  • sloc: ansic: 42,273; makefile: 860; sh: 524; perl: 348
file content (3188 lines) | stat: -rw-r--r-- 80,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
/*
 * server.c: Things dealing with server connections, etc. 
 *
 * Written By Michael Sandrof
 *
 * Copyright (c) 1990 Michael Sandrof.
 * Copyright (c) 1991, 1992 Troy Rollo.
 * Copyright (c) 1992-2018 Matthew R. Green.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "irc.h"
IRCII_RCSID("@(#)$eterna: server.c,v 1.250 2021/03/24 01:40:12 mrg Exp $");

#ifdef HAVE_SYS_UN_H
# include <sys/un.h>

int	connect_to_unix(int, u_char *);
#endif /* HAVE_SYS_UN_H */

#include "server.h"
#include "screen.h"
#include "ircaux.h"
#include "whois.h"
#include "lastlog.h"
#include "exec.h"
#include "window.h"
#include "output.h"
#include "names.h"
#include "parse.h"
#include "list.h"
#include "newio.h"
#include "vars.h"
#include "hook.h"
#include "icb.h"
#include "server.h"
#include "notice.h"
#include "ssl.h"
#include "sl_irc.h"

#include <assert.h>

/* Server: a structure for the server_list */
typedef	struct
{
	u_char	*name;			/* the name of the server */
	u_char	*itsname;		/* the server's idea of its name */
	u_char	*password;		/* password for that server */
	int	port;			/* port number on that server */
	u_char	*proxy_name;		/* HTTP proxy server to use */
	int	proxy_port;		/* port number for proxy server */
	u_char	*nickname;		/* nickname for this server */
	u_char	*away;			/* away message for this server */
	int	operator;		/* true if operator */
	int	oper_command;		/* true just after an oper() command is
					   given.  Used to tell the difference
					   between an incorrect password
					   generated by an oper() command and
					   one generated when connecting to a
					   new server */
	int	version;		/* the version of the server -
					   defined above */
	u_char	*version_string;	/* what is says */
	int	whois;			/* true if server sends numeric 318 */
	int	flags;			/* Various flags */
	int	connected;		/* true if connection is assured */
	int	write;			/* write descriptor */
	int	read;			/* read descriptior */
	pid_t	pid;			/* process id of server */
	int	eof;			/* eof flag for server */
	int	motd;			/* motd flag (used in notice.c) */
	int	sent;			/* set if something has been sent,
					   used for redirect */
	u_char	*buffer;		/* buffer of what dgets() doesn't get */
	WhoisQueue *WQ_head;		/* WHOIS Queue head */
	WhoisQueue *WQ_tail;		/* WHOIS Queue tail */
	WhoisStuff whois_stuff;		/* Whois Queue current collection buf */
	WhoInfo *who_info;		/* /who command info */
	int	close_serv;		/* Server to close when LOGGED_IN */
	CtcpFlood *ctcp_flood;		/* flood info for CTCP */
	u_char	*group;			/* ICB group */
	u_char	*icbmode;		/* ICB initial mode */
	ChannelList *chan_list;		/* list of channels for this server */
	void	(*parse_server)(u_char *); /* pointer to parser for this
					      server */
	int	server_group;		/* group this server belongs to */
	int	attempting_to_connect;	/* are we trying to connect this
					   server? */
	struct	addrinfo *res, *res0;	/* current lookup; for non blocking
					   support */
	SOCKADDR_STORAGE *localaddr;	/* currently bound local port */
	int 	localaddrlen;		/* length of above */
	SslInfo	*ssl_info;		/* handle for ssl routines */
	server_ssl_level ssl_level;	/* what sort of SSL to do */
	void	*server_private;	/* private data per protocol */
	server_private_cb_type server_private_cb; /* callback to free
					   server_private */
}	Server;

/* default SSL for IRC connections */
static	server_ssl_level irc_ssl_level = SSL_OFF;	

/* SGroup: a structure for server groups. */
typedef struct ser_group_list SGroup;
struct ser_group_list
{
	SGroup	*next;
	u_char	*name;
	int	number;
};

static	void	add_to_server_buffer(int, u_char *);
static	void	login_to_server(int);
static	int	connect_to_server_direct(u_char *, int, u_char *, int);
static	int	connect_to_server_process(u_char *, int, u_char *, int);
static	void	irc2_login_to_server(int);
static	void	server_group_get_connected_next(int);
static	int	reconnect_to_server(int, int);
static	void	parse_server(u_char *);
static	ssl_init_status	server_check_ssl(int);
static	void	reestablish_close_server(int, int);

/* server_list: the list of servers that the user can connect to,etc */
static	Server	*server_list = NULL;

/* number_of_servers_count: in the server list */
static	int	number_of_servers_count = 0;

/* server_group_list:  list of server groups */
static	SGroup	*server_group_list = NULL;

static	int	primary_server = -1;

/*
 * from_server: server we're currently interested in; set for window
 * operations or pretending to be on another server.
 */
static	int	from_server = -1;

static	int	never_connected_local = 1;	/* true until first connection
						 * is made */
static	int	connected_to_server_local = 0;	/* true when connection is
						 * confirmed */
static	int	parsing_server_index = -1;	/* set to the server we last
						   got a message from */

static	u_char	*default_proxy_name;
static	int	default_proxy_port;

#define DEFAULT_SERVER_VERSION Server2_8

#define DEFAULT_PROXY_PORT	3128

/*
 * close_server: Given an index into the server list, this closes the
 * connection to the corresponding server.  It does no checking on the
 * validity of the index.  It also first sends a "QUIT" to the server being
 * closed 
 */
void
close_server(int server_index, u_char *message)
{
	int	i,
		min,
		max;

	Debug(DB_SERVER, "entered. server %d: '%s'", server_index, message);
	if (server_index == -1)
	{
		min = 0;
		max = number_of_servers();
	}
	else
	{
		min = server_index;
		max = server_index + 1;
	}
	for (i = min; i < max; i++)
	{
		int	old_server = from_server;

		Debug(DB_SERVER, "server %d: '%s'", i, message);
		if (server_list[i].flags & CLOSE_PENDING)
			continue;
			
		if (i == primary_server)
			clean_whois_queue();

		from_server = -1;
		mark_not_connected(i);
		from_server = old_server;

		if (server_list[i].server_private)
			server_list[i].server_private_cb(
				&server_list[i].server_private);

		server_list[i].operator = 0;
		server_list[i].connected = 0;
		server_list[i].buffer = NULL;
		server_list[i].flags = SERVER_2_6_2;
		if (-1 != server_list[i].write)
		{
			if (message && *message)
			{
				u_char	*buffer = NULL;

				malloc_snprintf(&buffer, "QUIT :%s\n", message);
				/* XXX retval */
				ssl_write(server_list[i].ssl_info,
					  server_list[i].write, CP(buffer),
					  my_strlen(buffer));
				new_free(&buffer);
			}
			new_close(server_list[i].write);
			if (server_list[i].write == server_list[i].read)
				server_list[i].read = -1;
			server_list[i].write = -1;
		}

		if (server_list[i].ssl_info)
		{
			dgets_clear_ssl_info(server_list[i].read);
			ssl_close_connection(&server_list[i].ssl_info);
		}

		if (-1 != server_list[i].read)
		{
			new_close(server_list[i].read);
			server_list[i].read = -1;
		}
		if (-1 != server_list[i].pid)
		{
			kill(server_list[i].pid, SIGKILL);
			server_list[i].pid = (pid_t) -1;
		}
	}
}

/*
 * server_set_bits: Sets the proper bits in the fd_set structure according to
 * which servers in the server list have currently active read descriptors.  
 */

void
server_set_bits(fd_set *rd, fd_set *wd)
{
	int	i;

	for (i = 0; i < number_of_servers(); i++)
	{
		if (server_list[i].read != -1)
			FD_SET(server_list[i].read, rd);
#ifdef NON_BLOCKING_CONNECTS
		if (server_list[i].write != -1 &&
		    !(server_list[i].flags & (LOGGED_IN|CLOSE_PENDING|CONNECTED)))
			FD_SET(server_list[i].write, wd);
#endif /* NON_BLOCKING_CONNECTS */
	}
}

static int
reconnect_to_server(int si, int fi)
{
	return connect_to_server(server_list[si].name, server_list[si].port, server_list[si].nickname, fi);
}

static void
reestablish_close_server(int server, int old_server)
{
	if (server_list[old_server].flags & CLOSE_PENDING)
	{
		Win_Trav wt;
		Window *tmp;

		say("Connection to server %s resumed...", server_list[old_server].name);
		server_list[server].close_serv = -1;
		server_list[old_server].flags &= ~(CLOSE_PENDING|CLEAR_PENDING);
		server_list[old_server].flags |= LOGGED_IN;
		server_list[old_server].connected = 1;
		wt.init = 1;
		while ((tmp = window_traverse(&wt)))
			if (window_get_server(tmp) == server)
			{
				window_set_server(window_get_refnum(tmp), old_server, WIN_ALL);
				break;
			}
	}
	window_check_servers();
}

/*
 * do_server: check the given fd_set against the currently open servers in
 * the server list.  If one have information available to be read, it is read
 * and and parsed appropriately.  If an EOF is detected from an open server,
 * one of two things occurs. 1) If the server was the primary server,
 * get_connected() is called to maintain the connection status of the user.
 * 2) If the server wasn't a primary server, connect_to_server() is called to
 * try to keep that connection alive. 
 */
void
do_server(fd_set *rd, fd_set *wd)
{
	u_char	lbuf[BIG_BUFFER_SIZE];
	int	des, j;
	static	int	times = 0;
	int	old_timeout;
	u_char	*close_msg;

	for (j = 0; j < number_of_servers(); j++)
	{
		close_msg = empty_string();
#ifdef NON_BLOCKING_CONNECTS
		/*
		 *	deraadt@theos.com suggests that every fd awaiting connection
		 *	should be run at this point.
		 */
		if ((des = server_list[j].write) != -1 && /*FD_ISSET(des, wd) &&*/
		    !(server_list[j].flags & (LOGGED_IN|CONNECTED))) {
			SOCKADDR_STORAGE sa;
			socklen_t salen = sizeof sa;

			if (getpeername(server_list[j].write, (struct sockaddr *) &sa, &salen) != -1)
				login_to_server((from_server = j));
		}
#endif /* NON_BLOCKING_CONNECTS */
		if ((des = server_list[j].read) != -1 && FD_ISSET(des, rd))
		{
			int	junk;
			u_char 	*bufptr;
			u_char	*s;
			int	i = j; /* i is always j? */
			int	old_sep = -1;
			int	is_icb;
			size_t	len;

			from_server = i;
			is_icb = server_get_version(from_server) == ServerICB;

			if (!(server_list[j].flags & LOGGED_IN))
			{
				/* If we're not logged in, try again, or keep going. */
				login_to_server((from_server = j));
				if (!(server_list[j].flags & LOGGED_IN))
					goto real_continue;
			}
#if 0
			Debug(DB_SERVER, "ssl_info[%d] = %p (des = %d)", j,
			      server_list[j].ssl_info, des);
#endif

			if (is_icb)
				old_sep = dgets_set_separator('\0');

			old_timeout = dgets_timeout(1);
			s = server_list[from_server].buffer;
			bufptr = lbuf;
			if (s && *s)
			{
				len = my_strlen(s);
				my_strncpy(lbuf, s, len);
				bufptr += len;
			}
			else
				len = 0;
			if (len >= sizeof(lbuf))
				goto buffer_is_full_hack;	/* XXX */
			junk = dgets(bufptr, (sizeof(lbuf) - len), des);
			(void) dgets_timeout(old_timeout);

			switch (junk)
			{
			case -2:
				/* SSL retry */
				goto real_continue;
			case -1:
				add_to_server_buffer(from_server, lbuf);
				goto real_continue;
			case 0:
			{
#ifdef NON_BLOCKING_CONNECTS
				int	old_serv = server_list[i].close_serv;
			/* Get this here before close_server() clears it -Sol */
				int	logged_in = server_list[i].flags & LOGGED_IN;
#endif /* NON_BLOCKING_CONNECTS */

				close_server(i, close_msg);
				say("Connection closed from %s: %s", server_list[i].name,
					dgets_errno() == -1 ? "Remote end closed connection" : strerror(dgets_errno()));
#ifdef NON_BLOCKING_CONNECTS
				if (!logged_in && server_list[i].res0)
				{
					say("Trying next IP address for %s...", server_list[i].name);
					if (reconnect_to_server(i, -1)) {
						say("Connection to server %s failed...", server_list[i].name);
						clean_whois_queue();
						window_check_servers();
					}
					goto real_continue;
				}

				if (!logged_in && old_serv != -1)
				{
					if (old_serv == i)	/* a hack?  you bet */
						goto a_hack;
					reestablish_close_server(i, old_serv);
					break;
				}
a_hack:
#endif /* NON_BLOCKING_CONNECTS */
				if (i == primary_server)
				{
					if (server_list[i].eof)
					{
						say("Unable to connect to server %s",
							server_list[i].name);
						if (i == number_of_servers() - 1)
  						{
							clean_whois_queue();
							window_check_servers();
							if (!connected_to_server())
								say("Use /SERVER to connect to a server");
							times = 0;
						}
						else
							server_group_get_connected_next(i);
					}
					else
					{
						if (times++ > 1)
						{
							clean_whois_queue();
							window_check_servers();
							if (!connected_to_server())
								say("Use /SERVER to connect to a server");
							times = 0;
  						}
						else
							get_connected(i);
					}
				}
				else if (server_list[i].eof)
				{
					say("Connection to server %s lost.", server_list[i].name);
					clean_whois_queue();
					window_check_servers();
				}
				else
				{
					if (reconnect_to_server(i, -1)) {
						say("Connection to server %s lost.", server_list[i].name);
						clean_whois_queue();
						window_check_servers();
					}
				}
				server_list[i].eof = 1;
				break;
			}
			default:
buffer_is_full_hack:
				{
					int	old_psi = parsing_server_index;

					parsing_server_index = i;
					parse_server(lbuf);
					new_free(&server_list[i].buffer);
					parsing_server_index = old_psi;
					break;
				}
			}
real_continue:
			from_server = primary_server;
			if (is_icb && old_sep != -1)
				(void)dgets_set_separator(old_sep);
		}
	}
}

/*
 * find_in_server_list: given a server name, this tries to match it against
 * names in the server list, returning the index into the list if found, or
 * -1 if not found 
 */
int
find_in_server_list(u_char *server, int port, u_char *nick)
{
	int	i, maybe = -1;
	size_t	len;

	len = my_strlen(server);
	for (i = 0; i < number_of_servers(); i++)
	{
		if (port && server_list[i].port &&
		    port != server_list[i].port)
			continue;

		if (my_strnicmp(server, server_list[i].name, len) != 0)
			continue;

		if (nick)
		{
			if (server_list[i].nickname == NULL)
			{
				maybe = i;
				continue;
			}
			if (my_stricmp(server_list[i].nickname, nick))
				continue;
		}
		maybe = i;
		break;
	}
	return (maybe);
}

/*
 * parse_server_index:  given a string, this checks if it's a number, and if
 * so checks it validity as a server index.  Otherwise -1 is returned 
 */
int
parse_server_index(u_char *str)
{
	int	i;

	if (is_number(str))
	{
		i = my_atoi(str);
		if (i >= 0 && i < number_of_servers())
			return i;
	}
	return -1;
}

/*
 * add_to_server_list: adds the given server to the server_list.  If the
 * server is already in the server list it is not re-added... however, if the
 * SL_ADD_OVERWRITE flag is true, the port and passwords are updated to the
 * values passes.  If the server is not on the list, it is added to the end.
 * In either case, the server is made the current server. 
 */
void
add_to_server_list(u_char *server, int port, u_char *proxy_name, int proxy_port,
		   u_char *password, u_char *nick,
		   int group, int type, int flags)
{
	Debug(DB_SERVER, "server '%s' port %d pass '%s' nick '%s' group %d "
			 "type %d flags %x proxy %s:%d", server, port, password,
			 nick, group, type, flags,
			 proxy_name ? proxy_name : UP("<>"), proxy_port);
	if (port == -1)
		port = CHOOSE_PORT(type);
	if ((from_server = find_in_server_list(server, port, nick)) == -1)
	{
		from_server = number_of_servers_count++;
		if (server_list)
			server_list = new_realloc(server_list, number_of_servers_count * sizeof(*server_list));
		else
			server_list = new_malloc(number_of_servers_count * sizeof(*server_list));
		server_list[from_server].name = NULL;
		server_list[from_server].proxy_name = NULL;
		server_list[from_server].itsname = NULL;
		server_list[from_server].password = NULL;
		server_list[from_server].away = NULL;
		server_list[from_server].version_string = NULL;
		server_list[from_server].operator = 0;
		server_list[from_server].read = -1;
		server_list[from_server].write = -1;
		server_list[from_server].pid = -1;
		server_list[from_server].whois = 0;
		server_list[from_server].flags = SERVER_2_6_2;
		server_list[from_server].nickname = NULL;
		server_list[from_server].connected = 0;
		server_list[from_server].eof = 0;
		server_list[from_server].motd = 1;
		server_list[from_server].group = NULL;
		server_list[from_server].icbmode = NULL;
		server_list[from_server].chan_list = NULL;
		malloc_strcpy(&server_list[from_server].name, server);
		if (password && *password)
			malloc_strcpy(&server_list[from_server].password, password);
		if (nick && *nick)
			malloc_strcpy(&server_list[from_server].nickname, nick);
		server_list[from_server].port = port;
		if (proxy_name)
			malloc_strcpy(&server_list[from_server].proxy_name, proxy_name);
		server_list[from_server].proxy_port = proxy_port;
		server_list[from_server].WQ_head = NULL;
		server_list[from_server].WQ_tail = NULL;
		server_list[from_server].whois_stuff.nick = NULL;
		server_list[from_server].whois_stuff.user = NULL;
		server_list[from_server].whois_stuff.host = NULL;
		server_list[from_server].whois_stuff.channel = NULL;
		server_list[from_server].whois_stuff.channels = NULL;
		server_list[from_server].whois_stuff.name = NULL;
		server_list[from_server].whois_stuff.server = NULL;
		server_list[from_server].whois_stuff.server_stuff = NULL;
		server_list[from_server].whois_stuff.away = NULL;
		server_list[from_server].whois_stuff.oper = 0;
		server_list[from_server].whois_stuff.chop = 0;
		server_list[from_server].whois_stuff.not_on = 0;
		server_list[from_server].who_info = alloc_who_info();
		server_list[from_server].buffer = NULL;
		server_list[from_server].close_serv = -1;
		server_list[from_server].localaddr = 0;
		server_list[from_server].localaddrlen = 0;
		server_list[from_server].ssl_info = NULL;
		server_list[from_server].server_private = NULL;
		if (flags & SL_ADD_DO_SSL_VERIFY)
			server_list[from_server].ssl_level = SSL_VERIFY;
		else if (flags & SL_ADD_DO_SSL)
			server_list[from_server].ssl_level = SSL_ON;
		else
			server_list[from_server].ssl_level = SSL_OFF;
		switch (type)
		{
		case ServerICB:
			server_list[from_server].parse_server = icb_parse_server;
			break;
		case -1:
			/* default */
			if (client_default_is_icb())
			{
				type = ServerICB;
				server_list[from_server].parse_server = icb_parse_server;
				break;
			}
			type = DEFAULT_SERVER_VERSION;
			/* FALLTHROUGH */
		default:
			server_list[from_server].parse_server = irc2_parse_server;
		}
		server_list[from_server].version = type;

		server_list[from_server].ctcp_flood = ctcp_new_flood();

		if (group == -1)
			server_list[from_server].server_group = 0;
		else
			server_list[from_server].server_group = group;
		server_list[from_server].res = 0;
		server_list[from_server].res0 = 0;
	}
	else
	{
		if (flags & SL_ADD_OVERWRITE)
		{
			server_list[from_server].port = port;
			if (password)
			{
				if (*password)
					malloc_strcpy(&(server_list[from_server].password), password);
				else
					new_free(&(server_list[from_server].password));
			}
			if (nick && *nick)
				malloc_strcpy(&(server_list[from_server].nickname), nick);
			if (group != -1)
				server_list[from_server].server_group = group;
		}
		if (server_list[from_server].res0)
		{
			freeaddrinfo(server_list[from_server].res0);
			server_list[from_server].res0 = 0;
		}
		server_list[from_server].res = 0;
		if ((int) my_strlen(server) > (int) my_strlen(server_list[from_server].name))
			malloc_strcpy(&(server_list[from_server].name), server);
	}
}

void
ctcp_reply_backlog_change(int size)
{
	int	i;

	if (size <= 0)
		size = 1;
	for (i = 0; i < number_of_servers(); i++)
		ctcp_refresh_flood(server_list[i].ctcp_flood, size);
}

void
remove_from_server_list(int i)
{
	int	old_server = from_server;

	from_server = i;
	clean_whois_queue();
	from_server = old_server;

	close_server(i, NULL);

	if (server_list[i].name)
		new_free(&server_list[i].name);
	if (server_list[i].itsname)
		new_free(&server_list[i].itsname);
	if (server_list[i].password)
		new_free(&server_list[i].password);
	if (server_list[i].away)
		new_free(&server_list[i].away);
	if (server_list[i].version_string)
		new_free(&server_list[i].version_string);
	if (server_list[i].nickname)
		new_free(&server_list[i].nickname);
	if (server_list[i].group)
		new_free(&server_list[i].group);
	if (server_list[i].icbmode)
		new_free(&server_list[i].icbmode);
	if (server_list[i].whois_stuff.nick)
		new_free(&server_list[i].whois_stuff.nick);
	if (server_list[i].whois_stuff.user)
		new_free(&server_list[i].whois_stuff.user);
	if (server_list[i].whois_stuff.host)
		new_free(&server_list[i].whois_stuff.host);
	if (server_list[i].whois_stuff.channel)
		new_free(&server_list[i].whois_stuff.channel);
	if (server_list[i].whois_stuff.channels)
		new_free(&server_list[i].whois_stuff.channels);
	if (server_list[i].whois_stuff.name)
		new_free(&server_list[i].whois_stuff.name);
	if (server_list[i].whois_stuff.server)
		new_free(&server_list[i].whois_stuff.server);
	if (server_list[i].whois_stuff.server_stuff)
		new_free(&server_list[i].whois_stuff.server_stuff);
	if (server_list[i].ctcp_flood)
		ctcp_clear_flood(&server_list[i].ctcp_flood);
	if (server_list[i].res0)
		freeaddrinfo(server_list[i].res0);

	/* update all the structs with server in them */
	window_server_delete(i);
	channel_server_delete(i);	/* fix `higher' servers */
	clear_channel_list(i);
	exec_server_delete(i);
	if (i < primary_server)
		--primary_server;
	if (i < from_server)
		--from_server;

	memmove(&server_list[i], &server_list[i + 1], (number_of_servers() - i - 1) * sizeof(*server_list));
	server_list = new_realloc(server_list, --number_of_servers_count * sizeof(*server_list));

	if (from_server >= number_of_servers_count)
		from_server = -1;
}

int
ssl_level_to_sa_flags(server_ssl_level level)
{
	assert(level != SSL_UNKNOWN);
	if (level == SSL_VERIFY)
		return SL_ADD_DO_SSL_VERIFY;
	else if (level == SSL_ON)
		return SL_ADD_DO_SSL;
	return 0;
}

static void
server_split_proxy_port(u_char *proxy_and_port, u_char **name, int *port)
{
	u_char	*sport;

	if ((sport = my_index(proxy_and_port, ':')) == NULL)
	{
		*port = DEFAULT_PROXY_PORT;
	}
	else
	{
		*sport++ = '\0';
		*port = my_atoi(sport);
	}

	*name = proxy_and_port;
}

/*
 * parse_server_info:  This parses a single string of the form
 * "server:portnum:password:nickname[:icbgroup]".  It the points port to the portnum
 * portion and password to the password portion.  This chews up the original
 * string, so upon return, name will only point the the name.  If portnum
 * or password are missing or empty,  their respective returned value will
 * point to null.  if extra is non NULL, it is set to anything after the
 * final : after the nickname..
 *
 * Note:  this will set *type if it sees the IRC/ or ICB/ at the start of
 * the "name".  The server group name will be set by prepending ":group:" to
 * the server, so any of these is valid:
 *
 *	:group:server:portnum:...
 *	ICB/:group:server:portnum:...
 *	server:portnum:...
 *	ICB/server:portnum:...
 *
 * SSLIRC/ and SSLIRCNOCHECK/ prefixes also enable using an SSL connection
 * to the server, the latter version does not perform any certificate
 * verification.
 *
 * In addition, the PROXY/proxy.host:port/ and NOPROXY/ prefixes may be
 * present (currently, only *before* any other prefix.)
 */
void
parse_server_info(u_char **name, u_char **port, u_char **password,
		  u_char **nick, u_char **group, u_char **extra,
		  int *type, server_ssl_level *level,
		  u_char **proxy_name, int *proxy_port)
{
	u_char *ptr, *ename, *savename = NULL;
	u_char *epname, *pname;

	Debug(DB_SERVER, "got %s", *name);
	Debug(DB_PROXY, "got %s port %d", *proxy_name ? *proxy_name : UP("<>"), *proxy_port);
	*port = *password = *nick = *extra = NULL;

	if (my_strncmp(*name, "PROXY/", 6) == 0)
	{
		pname = (*name) + 6;
		if ((epname = my_index(pname, '/')) == NULL)
		{
			yell("--- Unable to parse server info: %s", *name);
			return;
		}
		*epname++ = '\0';

		server_split_proxy_port(pname, proxy_name, proxy_port);
		Debug(DB_PROXY, "set proxy_name %s port %d", *proxy_name, *proxy_port);
		*name = epname;
	}
	else
	if (my_strncmp(*name, "NO_PROXY/", 9) == 0)
	{
		*proxy_port = -1;
		*name += 9;
	}

	if (my_strncmp(*name, "IRC/", 4) == 0)
	{
		*type = DEFAULT_SERVER_VERSION;
		*level = SSL_OFF;
		*name += 4;
	}
	else
	if (my_strncmp(*name, "SSLIRC/", 7) == 0)
	{
		*type = DEFAULT_SERVER_VERSION;
		*level = SSL_VERIFY;
		*name += 7;
	}
	else
	if (my_strncmp(*name, "SSLIRCNOCHECK/", 14) == 0)
	{
		*type = DEFAULT_SERVER_VERSION;
		*level = SSL_ON;
		*name += 14;
	}
	else
	if (my_strncmp(*name, "ICB/", 4) == 0)
	{
		*type = ServerICB;
		*level = SSL_OFF;
		*name += 4;
	}
	else
	if (*level == SSL_UNKNOWN)
		*level = irc_ssl_level;

	/* check for :group: processing */
	if (**name == ':')
	{
		if ((ename = my_index((*name)+1, ':')))
		{
			*ename = '\0';
			if (group)
				*group = *name + 1;
			*name = ename + 1;	/* now points to empty or : we hope */
		}
	}

	/* check for [i:p:v:6]:port style */
	if (**name == '[')
	{
		if ((ename = my_index((*name)+1, ']')))
		{
			*ename = '\0';
			savename = *name + 1;
			*name = ename + 1;	/* now points to empty or : we hope */
		}
	}

	if ((ptr = my_index(*name, ':')) != NULL)
	{
		*(ptr++) = '\0';
		if (my_strlen(ptr) == 0)
			*port = NULL;
		else
		{
			*port = ptr;
			if ((ptr = my_index(ptr, ':')) != NULL)
			{
				*(ptr++) = '\0';
				if (my_strlen(ptr) == 0)
					*password = NULL;
				else
				{
					*password = ptr;
					if ((ptr = my_index(ptr, ':'))
							!= NULL)
					{
						*(ptr++) = '\0';
						if (!my_strlen(ptr))
							*nick = NULL;
						else
						{
							*nick = ptr;
							if (extra && (ptr = my_index(ptr, ':'))
									!= NULL)
							{
								*(ptr++) = '\0';
								if (!my_strlen(ptr))
									*extra = NULL;
								else
									*extra = ptr;
							}
						}
					}
				}
			}
		}
	}
	if (savename)
		*name = savename;
	Debug(DB_PROXY, "got %s port %d", *proxy_name ? *proxy_name : UP("<>"), *proxy_port);
}

/*
 * build_server_list: given a whitespace separated list of server names this
 * builds a list of those servers using add_to_server_list().  Since
 * add_to_server_list() is used to added each server specification, this can
 * be called many many times to add more servers to the server list.  Each
 * element in the server list case have one of the following forms: 
 *
 * servername 
 *
 * servername:port 
 *
 * servername:port:password 
 *
 * servername::password 
 *
 * Note also that this routine mucks around with the server string passed to it,
 * so make sure this is ok.
 *
 * A new format for ICB and more support is:
 *
 *	type/<type-specifc-format>
 *
 * eg:
 *	IRC/server:port:pass:nick:#foo:#bar:&baz
 * means connect to server on port port with pass and nick, and then to join
 * channels #foo, #bar and &baz.  this is not implemented beyond the nick...
 *
 * or
 *	ICB/[:group:]server:port:pass:nick:group:mode
 * which is all the things needed at connection startup.  this is done.
 */
void
build_server_list(u_char *servers)
{
	u_char	*host,
		*rest,
		*extra,
		*mode,
		*password = NULL,
		*port = NULL,
		*group = NULL,
		*nick = NULL,
		*proxy_name = NULL;
	int	port_num,
		proxy_port = 0,
		type = -1;

	if (servers == NULL)
		return;
	while (servers)
	{
		if ((rest = my_index(servers, '\n')) != NULL)
			*rest++ = '\0';
		while ((host = next_arg(servers, &servers)) != NULL)
		{
			server_ssl_level level = SSL_UNKNOWN;

			parse_server_info(&host, &port, &password, &nick,
					  &group, &extra, &type, &level,
					  &proxy_name, &proxy_port);
			if (port && *port)
			{
				port_num = my_atoi(port);
				if (!port_num)
					port_num = CHOOSE_PORT(type);
			}
			else
				port_num = CHOOSE_PORT(type);
			if (!nick)
				nick = my_nickname();

			add_to_server_list(host, port_num, proxy_name, proxy_port,
					   password, nick,
					   find_server_group(group, 1), type,
					   ssl_level_to_sa_flags(level));
			if (extra)
			{
				switch (type)
				{
				case ServerICB:
					if ((mode = my_index(extra, ':')) && mode[1])
						*mode++ = 0;
					else
						mode = NULL;
					server_set_icbgroup(from_server, extra);
					server_set_icbmode(from_server, mode);
					break;
				default:
					break;
					/* nothing yet */
				}
			}
		}
		servers = rest;
	}
}

/*
 * connect_to_server_direct: handles the tcp connection to a server.  If
 * successful, the user is disconnected from any previously connected server,
 * the new server is added to the server list, and the user is registered on
 * the new server.  If connection to the server is not successful,  the
 * reason for failure is displayed and the previous server connection is
 * resumed uniterrupted. 
 *
 * This version of connect_to_server() connects directly to a server 
 */
static	int
connect_to_server_direct(u_char *server_name, int port, u_char *nick, int server_index)
{
	int	new_des;
	struct	addrinfo *r = 0, *r0 = 0;
	u_char	*connect_name;
	int	connect_port;
	u_char	*proxy_name = NULL;
	int	proxy_port = 0;

	if (server_index >= 0)
		server_list[server_index].oper_command = 0;
	errno = 0;

	connect_name = server_name;
	connect_port = port;

	/* does this server use a proxy, or is there a non-ignored default? */
	if (server_index >= 0 &&
	    server_index < number_of_servers() &&
	    server_list[server_index].proxy_port != -1)
	{
		if (server_list[server_index].proxy_name)
		{
			connect_name = server_list[server_index].proxy_name;
			connect_port = server_list[server_index].proxy_port;
			proxy_name = connect_name;
			proxy_port = connect_port;
		}
		else if (default_proxy_name)
		{
			connect_name = default_proxy_name;
			connect_port = default_proxy_port;
			proxy_name = connect_name;
			proxy_port = connect_port;
		}
	}

#ifdef HAVE_SYS_UN_H
	if (*server_name == '/')
		new_des = connect_to_unix(port, server_name);
	else
#endif /* HAVE_SYS_UN_H */
	{
		if (server_index >= 0 &&
		    server_list[server_index].res &&
		    server_list[server_index].res0)
		{
			new_des = connect_by_number(connect_port, connect_name,
					1, &server_list[server_index].res,
					&server_list[server_index].res0);
		}
		else
		{
			new_des = connect_by_number(connect_port, connect_name,
					1, &r, &r0);
		}
	}
	if (new_des < 0)
	{
		int	error = errno;

		char *e = NULL;
		switch (new_des)
		{
		default:
		case -2:
			error = 0;
		case -1:
			e = "Unknown host";
			break;
		case -3:
			e = "socket";
			break;
		case -4:
			e = "connect";
			break;
		}
			
		if (proxy_name)
			say("Unable to connect to port %d of server %s "
			    "(proxy %s:%d): %s%s%s",
			    port, server_name,
			    proxy_name, proxy_port,
			    e, error ? ": " : "", error ? strerror(error) : "");
		else
			say("Unable to connect to port %d of server %s: %s%s%s",
			    port, server_name,
			    e, error ? ": " : "", error ? strerror(error) : "");
		if (is_server_open(from_server))
			say("Connection to server %s resumed...", server_list[from_server].name);
		if (r0)
			freeaddrinfo(r0);
		return (-1);
	}

	update_all_status();
	add_to_server_list(server_name, port, proxy_name, proxy_port, NULL,
			   nick, -1, server_get_version(from_server),
			   SL_ADD_OVERWRITE);

	if (server_list[from_server].localaddr)
		new_free(&server_list[from_server].localaddr);
	server_list[from_server].localaddr = 0;

#ifdef HAVE_SYS_UN_H
	if (*server_name == '/')
	{
		server_list[from_server].localaddr = 0;
		server_list[from_server].localaddrlen = 0;
	}
	else
#endif /* HAVE_SYS_UN_H */
	{
		SOCKADDR_STORAGE *localaddr = new_malloc(sizeof *localaddr);
		socklen_t address_len = sizeof *localaddr;

		if (getsockname(new_des, (struct sockaddr *) localaddr, &address_len)
		    >= 0)
		{
			server_list[from_server].localaddr = localaddr;
			server_list[from_server].localaddrlen = address_len;
		}
		else
		{
			close(new_des);
			say("Could not getsockname(): %s", strerror(errno));
			new_free(&localaddr);
			return -1;
		}
	}
	if (port)
	{
		server_list[from_server].read = new_des;
		server_list[from_server].write = new_des;
	}
	else
		server_list[from_server].read = new_des;
	if (!server_list[from_server].res0 && r && r0)
	{
		server_list[from_server].res = r;
		server_list[from_server].res0 = r0;
	}
	else if (r0)
		freeaddrinfo(r0);

	server_list[from_server].operator = 0;
	return (0);
}

/*
 * connect_to_server_process: handles the tcp connection to a server.  If
 * successful, the user is disconnected from any previously connected server,
 * the new server is added to the server list, and the user is registered on
 * the new server.  If connection to the server is not successful,  the
 * reason for failure is displayed and the previous server connection is
 * resumed uniterrupted. 
 *
 * This version of connect_to_server() uses the ircio process to talk to a
 * server 
 */
static	int
connect_to_server_process(u_char *server_name, int port, u_char *nick, int server_index)
{
	int	write_des[2],
		read_des[2],
		pid,
		c;
	u_char	*path,
		*name = NULL,
		*s;
	u_char	buffer[BIG_BUFFER_SIZE];
	int	old_timeout;

	path = UP(IRCIO_PATH);
	if ((s = my_rindex(path, '/')) != NULL)
		malloc_strcpy(&name, s + 1);
	if (!name)
		name = path;
	if (*path == '\0')
		return (connect_to_server_direct(server_name, port, nick, server_index));
	if (server_index >= 0)
		server_list[server_index].oper_command = 0;
	write_des[0] = -1;
	write_des[1] = -1;
	if (pipe(write_des) || pipe(read_des))
	{
		if (write_des[0] != -1)
		{
			new_close(write_des[0]);
			new_close(write_des[1]);
		}
		say("Couldn't start new process: %s", strerror(errno));
		return (connect_to_server_direct(server_name, port, nick, server_index));
	}
	switch (pid = fork())
	{
	case -1:
		say("Couldn't start new process: %s\n", strerror(errno));
		return (-1);
	case 0:
		(void) MY_SIGNAL(SIGINT, (sigfunc *)SIG_IGN, 0);
		dup2(read_des[1], 1);
		dup2(write_des[0], 0);
		new_close(read_des[0]);
		new_close(read_des[1]);
		new_close(write_des[0]);
		new_close(write_des[1]);
		snprintf(CP(buffer), sizeof buffer, "%u", port);
		(void)setuid(getuid());
		execl(CP(path), CP(name), server_name, buffer, NULL);
		printf("-5 0\n"); /* -1 - -4 returned by connect_by_number() */
		fflush(stdout);
		_exit(1);
	default:
		new_close(read_des[1]);
		new_close(write_des[0]);
		break;
	}
	old_timeout = dgets_timeout(3);
	c = dgets(buffer, sizeof buffer, read_des[0]);
	(void) dgets_timeout(old_timeout);
	if ((c == 0) || ((c = my_atoi(buffer)) != 0))
	{
		if (c == -5)
			return (connect_to_server_direct(server_name, port, nick, server_index));
		else
		{
			u_char *ptr;

			if ((ptr = my_index(buffer, ' ')) != NULL)
			{
				ptr++;
				if (my_atoi(ptr) > 0)
		say("Unable to connect to port %d of server %s: %s",
			port, server_name, strerror(my_atoi(ptr)));
				else
		say("Unable to connect to port %d of server %s: Unknown host",
							port, server_name);
			}
			else
		say("Unable to connect to port %d of server %s: Unknown host",
							port, server_name);
			if (is_server_open(from_server))
				say("Connection to server %s resumed...",
						server_list[from_server].name);
			new_close(read_des[0]);
			new_close(write_des[1]);
			return (-1);
		}
	}
	update_all_status();
	add_to_server_list(server_name, port, NULL/*proxy*/, 0/*proxy port*/,
			   NULL, nick, -1, server_get_version(from_server),
			   SL_ADD_OVERWRITE);
	server_list[from_server].read = read_des[0];
	server_list[from_server].write = write_des[1];
	server_list[from_server].pid = pid;
	server_list[from_server].operator = 0;
	return (0);
}

/*
 * connect_to_server: Given a name and portnumber, this will attempt to
 * connect to that server using either a direct connection or process
 * connection, depending on the value of using_ircio().  If connection
 * is successful, the proper NICK, USER, and PASS commands are sent to the
 * server.  If the c_server parameter is not -1, then the server with that
 * index will be closed upon successful connection here. Also, if connection
 * is successful, the attempting_to_connect variable is incremented.  This is
 * checked in the notice.c routines to make sure that connection was truely
 * successful (and not closed immediately by the server). 
 */
int
connect_to_server(u_char *server_name, int port, u_char *nick, int c_server)
{
	int	server_index;
	SOCKADDR_STORAGE	sa;
	socklen_t salen = sizeof sa;
	int rv;

	save_message_from();
	message_from(NULL, LOG_CURRENT);
	server_index = find_in_server_list(server_name, port, nick);
	if (server_index < 0)
	{
		yell("connect_to_server: server_index returned -1 from find_in_server_list()");
		yell("aborting!");
		abort();
	}
	server_list[server_index].attempting_to_connect = 1;
	/*
	 * check if the server doesn't exist, or that we're not already
	 * connected to it.  note that "connected" also means logged in.
	 */
	if (!is_server_connected(server_index))
	{
		if (is_server_open(server_index))
			close_server(server_index, empty_string());
		if (port == -1)
			port = server_list[server_index].port;
		if (port == -1)
			port = CHOOSE_PORT(server_list[server_index].version);
		say("Connecting to port %d of server %s", port, server_name);
		
		if (!ignore_ircrc())
			load_ircquick();
		
		if (using_ircio())
			rv = connect_to_server_process(server_name, port, nick, server_index);
		else
			rv = connect_to_server_direct(server_name, port, nick, server_index);
		if (rv)
		{
			server_list[server_index].attempting_to_connect = 0;
			restore_message_from();
			return -1;
		}
		if ((c_server != -1) && (c_server != from_server))
		{
#ifdef NON_BLOCKING_CONNECTS
#if defined(GKM)
			say("--- server %s will be closed when we connect", server_list[c_server].name);
			if (server_list[c_server].flags & CLOSE_PENDING)
				say("--- why are we flagging this for closing a second time?");
#endif /* GKM */
			server_list[from_server].close_serv = c_server;
			server_list[c_server].flags |= CLOSE_PENDING;
			server_list[c_server].connected = 0;
#else
			close_server(c_server, empty_string());
#endif /* NON_BLOCKING_CONNECTS */
		}
		else
		{
			server_list[from_server].close_serv = -1;
		}
		if (server_list[from_server].nickname == NULL)
			malloc_strcpy(&server_list[from_server].nickname, my_nickname());
		server_list[from_server].flags &= ~LOGGED_IN;
		/*
		 * this used to be an ifndef NON_BLOCKING_CONNECTS .. we want to do this
		 * whenever the connection is valid, it's possible for a connect to be
		 * "immediate".
		 */
		if (is_server_open(from_server) &&
		    (using_ircio() ||
		    getpeername(server_list[from_server].read, (struct sockaddr *) &sa, &salen) != -1))
			login_to_server(from_server);
	}
	else
	{
		if (port == -1)
		{
			if (server_index != -1)
				port = server_list[server_index].port;
			else
				port = CHOOSE_PORT(server_get_version(server_index));
		}
		say("Connected to port %d of server %s", port, server_name);
		from_server = server_index;
		if ((c_server != -1) && (c_server != from_server))
			close_server(c_server, empty_string());
	}
	update_all_status();
	restore_message_from();
	return 0;
}

static void
login_to_server_nonblocking(int server)
{
#ifdef NON_BLOCKING_CONNECTS
	int	old_serv = server_list[server].close_serv;

	/* clean up after ourselves */
	if (server_list[server].res0)
	{
		freeaddrinfo(server_list[server].res0);
		server_list[server].res0 = 0;
	}
	server_list[server].res = 0;
	if (old_serv != -1)
	{
#if defined(GKM)
		say("--- closing server %s - changing servers", server_list[server_list[server].close_serv].name);
		if (!(server_list[server_list[server].close_serv].flags & CLOSE_PENDING))
			say("--- uh oh. closing a server that wasn't CLOSE_PENDING");
#endif /* GKM */
		if (server_list[old_serv].flags & CLEAR_PENDING)
			clear_channel_list(old_serv);   /* Channels were
							   transfered -Sol */
		server_list[old_serv].flags &= ~(CLOSE_PENDING|CLEAR_PENDING);
		close_server(old_serv, empty_string());
		server_list[server].close_serv = -1;
		/* should we pause here to let the net catch up with us? */
	}
#if defined(GKM)
	else
	{
		say("--- no server to close in login_to_server()");
	}
#endif /* GKM */
#endif /* NON_BLOCKING_CONNECTS */
}

/*
 * Check if we should attempt to initiate an SSL connection or not.
 * Returns SSL_INIT_OK if there was no error, either no SSL or SSL
 * started OK.  Returns SSL_INIT_FAIL if SSL failed, either to work
 * at all or if certificate verification failed.  Returns
 * SSL_INIT_PENDING if the connection establishment is still ongoing.
 */
static	ssl_init_status
server_check_ssl(int server)
{
	ssl_init_status	status;

	if (server_list[server].ssl_level == SSL_OFF)
		return SSL_INIT_OK;
	
	if (server_list[server].flags & SSL_DONE)
		return SSL_INIT_OK;

	status = ssl_init_connection(server, server_list[server].read,
				     &server_list[server].ssl_info);
	Debug(DB_SERVER, "server = %d; status = %d; set: %p", server, status,
	      server_list[server].ssl_info);
	if (status == SSL_INIT_FAIL)
	{
		if (server_list[server].ssl_level == SSL_OFF)
		{
			yell("SSL off, why did we try to allocate one?");
			Debug(DB_SERVER, "SSL off");
		}
		else
		{
			Debug(DB_SERVER, "ssl_init_connection failed");
			yell("-- SSL init failed; closing server %s",
			     server_list[server].name);
		}
	}
	else if (status == SSL_INIT_OK)
	{
		server_list[server].flags |= SSL_DONE;
		dgets_set_ssl_info(server_list[server].read,
				   server_list[server].ssl_info);
	}

	return status;
}

/*
 * we're looking for eg:
 *
 * HTTP/1.1 200 Connection established
 * Proxy-Agent: Privoxy/3.0.19
 * <blank line>
 *
 * return values:
 *   -1  -  proxy login not completed, retry 
 *    0  -  proxy login complete
 *    1  -  failed some how; close shop.
 */
static int
server_check_http_response(int server, u_char *lbuf, size_t buflen)
{
	Debug(DB_PROXY, "server %d: buf \"%s\"", server, lbuf);
	if ((server_list[server].flags & PROXY_REPLY) == 0)
	{
		u_char	*arg, *args = lbuf;

		arg = next_arg(args, &args);

		if (arg &&
		    ((my_stricmp(UP("HTTP/1.0"), arg) == 0) ||
		     (my_stricmp(UP("HTTP/1.1"), arg) == 0)))
		{
			arg = next_arg(args, &args);

			if (my_stricmp(UP("200"), arg) == 0)
			{
				server_list[server].flags |= PROXY_REPLY;
				Debug(DB_PROXY, "got HTTP 200 reply!");
				return -1;
			}
		}
		Debug(DB_PROXY, "did not get HTTP 200 reply.");
		yell("-- proxy reply when we expected 200 was: \"%s\"", lbuf);
		return 1;
	}
	/* be generous */
	if (lbuf[0] == '\0' ||
	    (lbuf[0] == '\n' && lbuf[1] == '\0') ||
	    (lbuf[0] == '\n' && lbuf[1] == '\r' && lbuf[2] == '\0') ||
	    (lbuf[0] == '\r' && lbuf[1] == '\n' && lbuf[2] == '\0'))
	{
		Debug(DB_PROXY, "got empty string, end of http reply! done!");
		return 0;
	}

	/* probably just a header, we could check? */
	Debug(DB_PROXY, "not empty, keep going.  should be http header.");
	return -1;
}

static	void
login_to_server(int server)
{
	ssl_init_status status;
	u_char	*proxy_name;

	Debug(DB_SERVER, "server %d", server);

	if ((server_list[server].flags & CONNECTED) == 0)
	{
		/*
		 * If we're CONNECTED, we've gone through the non-blocking
		 * connect.  If we're LOGGED_IN, that means that we've sent
		 * the login sequence, and for SSL that means SSL has
		 * successfully initialised.
		 */
		server_list[server].flags |= CONNECTED;

#ifdef NON_BLOCKING_CONNECTS
		if (using_ircio() == 0)
		{
			set_blocking(server_list[server].read);
			if (server_list[server].read != server_list[server].write)
				set_blocking(server_list[server].write);
		}
#endif /* NON_BLOCKING_CONNECTS */
	}

	proxy_name = server_get_proxy_name(server, 1);
	if (proxy_name &&
	    (server_list[server].flags & PROXY_CONNECT) == 0)
	{
		u_char	*buf = NULL;
		int	len, rv;

		malloc_snprintf(&buf, "CONNECT %s:%d HTTP/1.1\nHost:\n\n",
		         server_list[server].name,
		         server_list[server].port);
		len = my_strlen(buf);
		rv = write(server_list[server].write, CP(buf), len);
		new_free(&buf);
		if (rv != len)
		{
			yell("--- proxy login failed.");
			Debug(DB_PROXY, "proxy write failed, closing.");
			goto failed_recover;
		}

		server_list[server].flags |= PROXY_CONNECT;
		Debug(DB_PROXY, "proxy: send CONNECT %s:%d (from_server = %d)",
		      server_list[server].name, server_list[server].port, from_server);

		/* XXX need to eat HTTP reply, before doing final login */
		return;
	}

	/*
	 * if we've sent proxy stuff, but haven't completed it yet, we
	 * need to eat the HTTP reply.
	 */
	if ((server_list[server].flags & PROXY_CONNECT) != 0 &&
	    (server_list[server].flags & PROXY_DONE) == 0)
	{
		u_char	lbuf[BIG_BUFFER_SIZE];
		u_char 	*bufptr;
		u_char	*s;
		int	old_timeout;
		int	junk;
			size_t	len;

		old_timeout = dgets_timeout(1);
		s = server_list[server].buffer;
		bufptr = lbuf;
		if (s && *s)
		{
			len = my_strlen(s);
			if (len >= sizeof(lbuf))
				goto buffer_is_full_hack;
			my_strncpy(lbuf, s, len);
			bufptr += len;
		}
		else
			len = 0;
		junk = dgets(bufptr, sizeof(lbuf) - len, server_list[server].read);
		(void) dgets_timeout(old_timeout);

		switch (junk)
		{
		case -2:
			/* shouldn't happen! SSL retry */
			yell("--- proxy http response got -2 / SSL retry!?");
			Debug(DB_PROXY, "proxy http response got -2 / SSL retry, closing.");
			goto failed_recover;
		case -1:
			Debug(DB_PROXY, "added \"%s\" to the server buffer for proxy.", lbuf);
			add_to_server_buffer(server, lbuf);
			return;
		case 0:
			goto failed_recover;
		default:
buffer_is_full_hack:
			switch (server_check_http_response(server, lbuf, sizeof lbuf))
			{
			case -1:
				Debug(DB_PROXY, "got retry on http response server %d", server);
				return;
			case 0:
				Debug(DB_PROXY, "proxy setup complete, server %d!", server);
				server_list[server].flags |= PROXY_DONE;
				return;
			case 1:
				yell("--- proxy http login failed, closing");
				Debug(DB_PROXY, "proxy http login failed, closing server %d", server);
				goto failed_recover;
			default:
				yell("--- help! this shouldn't happen.  not -1/0/1");
				goto failed_recover;
			}
		}
	}

	status = server_check_ssl(server);
	if (status == SSL_INIT_PENDING)
	{
		return;
	}
	if (status == SSL_INIT_FAIL)
	{
		int	old_serv;
		int	logged_in;

		Debug(DB_SERVER, "SSL failed, closing.");
failed_recover:
		old_serv = server_list[server].close_serv;
		logged_in = server_list[server].flags & LOGGED_IN;

		close_server(server, UP("proxy/SSL Failed."));
		if (!logged_in && old_serv != -1 && old_serv != server)
			reestablish_close_server(server, old_serv);
		return;
	}
	server_list[server].flags |= LOGGED_IN;
	login_to_server_nonblocking(server);
	if (server_get_version(server) == ServerICB)
		icb_login_to_server(server);
	else
		irc2_login_to_server(server);
	window_copy_prev_server(server);
}

static	void
irc2_login_to_server(int server)
{
	if (server_get_version(server) == ServerICB)
	{
		yell("--- ICB called irc2_login_to_server???");
		return;
	}

	if (server_list[server].password)
		send_to_server("PASS %s", server_list[server].password);
	send_to_server("NICK %s", server_list[server].nickname);
	send_to_server("USER %s %s %s :%s", my_username(), irc_umode(),
		server_list[server].name, my_realname());
}

/*
 * get_connected: This function connects the primary server for IRCII.  It
 * attempts to connect to the given server.  If this isn't possible, it
 * traverses the server list trying to keep the user connected at all cost.  
 * oldconn is set if this connection is really an old connection being
 * resurected (eg. connection to server failed).
 */
void
get_connected(int server)
{
	int	s,
		ret = -1;

	if (server_list)
	{
		int	already_connected = 0;

		if (server == number_of_servers())
			server = 0;
		else if (server < 0)
			server = number_of_servers() - 1;
		s = server;
		if (reconnect_to_server(server, primary_server))
		{
			while (server_list[server].read == -1)
			{
				server++;
				if (server == number_of_servers())
					server = 0;
				if (server == s)
				{
					clean_whois_queue();
					say("Use /SERVER to connect to a server");
					break;
				}
				from_server = server;
				already_connected = is_server_connected(server);
				ret = reconnect_to_server(server, primary_server);
			}
			if (!ret)
				from_server = server;
			else
				from_server = -1;
		}
		if (from_server != -1) {
			int flags;

			flags = (already_connected ? 0 : WIN_TRANSFER);
			window_set_server(-1, from_server, flags);
		}
	}
	else
	{
		clean_whois_queue();
		say("Use /SERVER to connect to a server");
	}
}

/*
 * read_server_file: reads hostname:portnum:password server information from
 * a file and adds this stuff to the server list.  See build_server_list()/ 
 */
int
read_server_file(void)
{
	FILE *fp;
	u_char format[11];
	u_char *file_path;
	u_char *free_path = NULL;
	u_char	buffer[FS_BUFFER_SIZE];

	if ((file_path = my_getenv("IRCSERVERSFILE")) == NULL)
	{
		malloc_strcpy(&free_path, my_irc_lib());
		malloc_strcat(&free_path, UP(SERVERS_FILE));
		file_path = free_path;
	}
	snprintf(CP(format), sizeof format, "%%%ds", (int)sizeof buffer);
	fp = fopen(CP(file_path), "r");
	new_free(&free_path);
	if (NULL != fp)
	{
		while (fscanf(fp, CP(format), buffer) != EOF)
			build_server_list(buffer);
		fclose(fp);
		return (0);
	}
	return (1);
}

/* display_server_list: just guess what this does */
void
display_server_list(void)
{
	int	i;

	if (from_server >= number_of_servers())
		from_server = -1;

	if (server_list)
	{
		if (from_server != -1)
			say("Current server: %s %d",
					server_list[from_server].name,
					server_list[from_server].port);
		else
			say("Current server: <None>");
		if (primary_server != -1)
			say("Primary server: %s %d",
				server_list[primary_server].name,
				server_list[primary_server].port);
		else
			say("Primary server: <None>");
		if (client_default_is_icb())
			say("Using ICB connections by default");
		if (default_proxy_name)
			say("Using proxy server: %s port %d by default",
			    default_proxy_name, default_proxy_port);
		say("Server list:");
		for (i = 0; i < number_of_servers(); i++)
		{
			u_char	*proxy_msg = NULL;
			u_char	*icb_msg;
			u_char	*group_msg = NULL;
			u_char	*pname = NULL;
			const u_char	*ssl_val;
			int	pport = 0;

			switch (server_list[i].ssl_level) {
			case SSL_ON:
				ssl_val = UP(" (with unchecked TLS)");
				break;
			case SSL_VERIFY:
				ssl_val = UP(" (with TLS)");
				break;
			default:
				ssl_val = empty_string();
				break;
			}

			icb_msg = server_list[i].version == ServerICB ?
				    (u_char *) " (ICB connection)" : empty_string();
			if (server_list[i].server_group)
				malloc_snprintf(&group_msg, " [group: %s]",
				    find_server_group_name(server_list[i].server_group));
			else
				malloc_strcat(&group_msg, empty_string());

			pport = server_get_proxy_port(i, 0);
			if (pport == -1)
				malloc_snprintf(&proxy_msg, " (no proxy)");
			else if ((pname = server_get_proxy_name(i, 0)))
				malloc_snprintf(&proxy_msg,
					 " (proxy on server %s port %d)",
					 pname, pport);
			else
				malloc_strcat(&proxy_msg, empty_string());

			if (!server_list[i].nickname)
			{
				say("\t%d) %s %d%s%s%s%s%s", i,
					server_list[i].name,
					server_list[i].port,
					server_list[i].read == -1 ?
					    UP(" (not connected)") : empty_string(),
					group_msg,
					icb_msg,
					proxy_msg,
					ssl_val);
			}
			else
			{
				say("\t%d) %s %d (%s%s)%s%s%s%s", i,
					server_list[i].name,
					server_list[i].port,
					(server_list[i].read == -1) ?
					    UP("was ") : empty_string(),
					server_list[i].nickname,
					group_msg,
					icb_msg,
					proxy_msg,
					ssl_val);
			}
			new_free(&proxy_msg);
			new_free(&group_msg);
#ifdef GKM
			say("\t\tflags: %s%s%s%s%s%s%s",
				server_list[i].flags & SERVER_2_6_2 ? UP("SERVER_2_6_2 ") : empty_string(),
				server_list[i].flags & USER_MODE_I ? UP("USER_MODE_I ") : empty_string(),
				server_list[i].flags & USER_MODE_W ? UP("USER_MODE_W ") : empty_string(),
				server_list[i].flags & USER_MODE_S ? UP("USER_MODE_S ") : empty_string(),
				server_list[i].flags & CLOSE_PENDING ? UP("CLOSE_PENDING ") : empty_string(),
				server_list[i].flags & CLEAR_PENDING ? UP("CLEAR_PENDING ") : empty_string(),
				server_list[i].flags & LOGGED_IN ? UP("LOGGED_IN ") : empty_string() );
			say("\t\tclose_serv=%d, connected=%d, read=%d, eof=%d", server_list[i].close_serv, server_list[i].connected, server_list[i].read, server_list[i].eof);
#endif /* GKM */
		}
	}
	else
		say("The server list is empty");
}

void
mark_all_away(u_char *command, u_char *message)
{
	int	old_server;

	old_server = from_server;
	for (from_server = 0; from_server < number_of_servers(); from_server++)
	{
		if (is_server_connected(from_server))
			send_to_server("%s :%s", command, message);
	}
	from_server = old_server;
}


/*
 * server_set_password: this sets the password for the server with the given
 * index.  If password is null, the password for the given server is returned 
 */
u_char	*
server_set_password(int server_index, u_char *password)
{

	if (server_list)
	{
		if (password)
			malloc_strcpy(&(server_list[server_index].password), password);
		return (server_list[server_index].password);
	}
	else
		return (NULL);
}

/*
 * ICB support
 */
void
server_set_icbgroup(int server_index, u_char *group)
{

	malloc_strcpy(&server_list[server_index].group, group);
}

void
server_set_icbmode(int server_index, u_char *mode)
{

	malloc_strcpy(&server_list[server_index].icbmode, mode);
}

/*
 * server: the /SERVER command. Read the SERVER help page about 
 */
void
servercmd(u_char *command, u_char *args, u_char *subargs)
{
	u_char	*server,
		*port,
		*proxy_name = NULL,
		*proxy_port_str = NULL,
		*extra,
		*newmode,
		*password = NULL,
		*nick = NULL,
		*group = NULL;
	int	port_num,
		proxy_port = 0,
		i,
		new_server_flags,
		type = -1;
	server_ssl_level level = SSL_UNKNOWN;

	if ((server = next_arg(args, &args)) != NULL)
	{
		while (*server == '-')
		{
			size_t	len;

			/*
			 * old usage of `/server -' handled here.
			 */
			if (*++server == '\0')
			{
				get_connected(primary_server - 1);
				return;
			}
			upper(server);
			len = my_strlen(server);
			/*
			 * just don't return if you want to perform some action in one of
			 * the flag handling sections.
			 */
			if (!my_strncmp(server, "ICB", len))
				type = ServerICB;
			else if (!my_strncmp(server, "IRC", len))
				type = DEFAULT_SERVER_VERSION;
			else if (!my_strncmp(server, "SSL", len))
				level = SSL_VERIFY;
			else if (!my_strncmp(server, "SSLNOCHECK", len))
				level = SSL_ON;
			else if (!my_strncmp(server, "NOSSL", len))
				level = SSL_OFF;
			else if (!my_strncmp(server, "DELETE", len))
			{
				if ((server = next_arg(args, &args)) != NULL)
				{
					if ((i = parse_server_index(server)) == -1)
					{
						if (-1 == (i = find_in_server_list(server, 0, 0)))
						{
							say("No such server in list");
							return;
						}
					}
					if (server_list[i].connected)
					{
						say("Can not delete server that is already open");
						return;
					}
					remove_from_server_list(i);
					return;
				}
				say("Need server number for -DELETE");
				return;
			}
			else if (!my_strncmp(server, "GROUP", len))
			{
				if ((group = next_arg(args, &args)) == NULL)
				{
					say("SERVER -GROUP needs <group> and <server>");
					return;
				}
			}
			else if (!my_strncmp(server, "PROXY", len))
			{
				if ((proxy_name = next_arg(args, &args)) == NULL ||
				    (proxy_port_str = next_arg(args, &args)) == NULL)
				{
					say("SERVER -PROXY needs <proxy> and <host>");
					return;
				}
				proxy_port = my_atoi(proxy_port_str);
			}
			else
			{
				say("SERVER: %s is an unknown flag", server);
				return;
			}
			if ((server = next_arg(args, &args)) == NULL)
			{
				say("SERVER: need a server name");
				return;
			}
		}

		if (my_index(server, ':') != NULL)
		{
			parse_server_info(&server, &port, &password, &nick,
			    group ? 0 : &group, &extra, &type, &level,
			    &proxy_name, &proxy_port);
			if (!my_strlen(server))
			{
				say("Server name required");
				return;
			}
			if (port && *port) {
				port_num = my_atoi(port);
				if (!port_num)
					port_num = CHOOSE_PORT(type);
			} else
				/* consider port = next_arg() */
				port_num = CHOOSE_PORT(type);
		}
		else
		{
			if ((port = next_arg(args, &args)) != NULL)
			{
				port_num = my_atoi(port);
				if (!port_num)
					port_num = CHOOSE_PORT(type);
				if ((password = next_arg(args, &args)) != NULL)
					nick = next_arg(args, &args);
			}
			else
				port_num = CHOOSE_PORT(type);

			if (level == SSL_UNKNOWN)
				level = irc_ssl_level;
			extra = NULL;
		}

		/*
		 * We need to not do this in the case "server" is really a
		 * valid server number.
		 */
		if (parse_server_index(server) == -1)
			add_to_server_list(server, port_num,
					   proxy_name, proxy_port, password,
					   nick, -1, type,
					   ssl_level_to_sa_flags(level));

		if (group && *group)
			server_list[from_server].server_group = find_server_group(group, 1);

		if (extra && type == ServerICB)
		{
			if ((newmode = my_index(extra, ':')))
			{
				*newmode++ = 0;
				malloc_strcpy(&(server_list[from_server].icbmode), newmode);
			}
			malloc_strcpy(&(server_list[from_server].group), extra);
		}

		if (*server == '+' || *server == '=' || *server == '~')
		{
			if (group)
				add_server_to_server_group(
				    window_get_server(curr_scr_win), group);

			if (*(server+1))
			{
				u_char	servinfo[INPUT_BUFFER_SIZE+1];

				if (*server == '+')
					server++;
				/* Reconstitute whole server info so
				  window_get_connected can parse it -Sol */
				snprintf(CP(servinfo), sizeof servinfo, "%s:%d:%s:%s",
					server, port_num,
					password ? password : empty_string(),
					nick ? nick : empty_string());
				window_get_connected(curr_scr_win, servinfo,
				    -1, NULL, proxy_name, proxy_port,
				    group, type, level);
			}
			else
				get_connected(primary_server + 1);
			return;
		}
		/*
		 * work in progress.. window_get_prev_server() needs to be set for
		 * all windows that used to be associated with a server as it
		 * switches [successfully] to a new server.
		 * this'll be fun since that can happen in server.c and
		 * window.c and non-blocking-connects will throw yet another
		 * wrench into things since we only want it to happen on
		 * a successful connect. - gkm
		 */
		else if (*server == '.')
		{
			if (*(++server))
			{
				say("syntax error - nothing may be specified after the '.'");
				return;
			}
			if (get_current_screen() && curr_scr_win &&
			    (i = window_get_prev_server(curr_scr_win)) != -1)
			{
				if (group)
					add_server_to_server_group(i, group);

				window_restore_server(i);
				if (!proxy_name && server_list[i].proxy_name)
					proxy_name = server_list[i].proxy_name;
				if (proxy_port == 0 && server_list[i].proxy_port)
					proxy_port = server_list[i].proxy_port;
				window_get_connected(curr_scr_win, NULL,
				    window_get_server(curr_scr_win), NULL,
				    proxy_name, proxy_port,
				    group, type, level);
			}
			else
				say("No server previously in use in this window");
			return;
		}
		if ((i = parse_server_index(server)) != -1)
		{
			server = server_list[i].name;
			if (server_list[i].port != -1)
				port_num = server_list[i].port;
			if (server_list[i].nickname && !nick)
				nick = server_list[i].nickname;
		}
		else
			i = find_in_server_list(server, port_num, nick);

		if (group)
			add_server_to_server_group(i, group);

		if (is_server_connected(i))
		{
			/*
			 * We reset the log level only if the "new" server
			 * already has windows associated with it : here it's
			 * equivalent to its already being connected. -Sol
			 */
			new_server_flags = 0;
		}
		else
			new_server_flags = WIN_TRANSFER;
		if (connect_to_server(server, port_num, nick, primary_server) != -1)
		{
			if (primary_server > -1 && from_server != primary_server &&
			    !server_list[from_server].away &&
			    server_list[primary_server].away)
				malloc_strcpy(&server_list[from_server].away,
				    server_list[primary_server].away);
			window_set_server(-1, from_server, new_server_flags);
		}
	}
	else
		display_server_list();
}

/*
 * flush_server: eats all output from server, until there is at least a
 * second delay between bits of servers crap... useful to abort a /links. 
 */
void
flush_server(void)
{
	fd_set rd;
	struct timeval time_out;
	int	flushing = 1;
	int	des;
	int	old_timeout;
	u_char	buffer[BIG_BUFFER_SIZE];

	if ((des = server_list[from_server].read) == -1)
		return;
	time_out.tv_usec = 0;
	time_out.tv_sec = 1;
	old_timeout = dgets_timeout(1);
	while (flushing)
	{
		FD_ZERO(&rd);
		FD_SET(des, &rd);
		switch (new_select(&rd, NULL, &time_out))
		{
		case -1:
		case 0:
			flushing = 0;
			break;
		default:
			if (FD_ISSET(des, &rd))
			{
				switch (dgets(buffer, sizeof buffer, des))
				{
				case -2:
					/* SSL retry */
					break;
				case -1:
				case 0:
					flushing = 0;
					break;
				default:
					break;
				}
			}
			break;
		}
	}
	/* make sure we've read a full line from server */
	FD_ZERO(&rd);
	FD_SET(des, &rd);
	if (new_select(&rd, NULL, &time_out) > 0)
		dgets(buffer, sizeof buffer, des);
	(void) dgets_timeout(old_timeout);
}

/*
 * server_set_whois: sets the whois value for the given server index.  If the
 * whois value is 0, it assumes the server doesn't send End of WHOIS commands
 * and the whois.c routines use the old fashion way of getting whois info. If
 * the whois value is non-zero, then the server sends End of WHOIS and things
 * can be done more effienciently 
 */
void
server_set_whois(int server_index, int value)
{
	server_list[server_index].whois = value;
}

/* server_get_whois: Returns the whois value for the given server index */
int
server_get_whois(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	return (server_list[server_index].whois);
}


void
server_set_2_6_2(int server_index, int value)
{
	server_set_flag(server_index, SERVER_2_6_2, value);
}

int
server_get_2_6_2(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	return (server_get_flag(server_index, SERVER_2_6_2));
}

void
server_set_flag(int server_index, int flag, int value)
{
	if (server_index == -1)
		server_index = primary_server;
	if (value)
		server_list[server_index].flags |= flag;
	else
		server_list[server_index].flags &= ~flag;
}

int
server_get_flag(int server_index, int value)
{
	if (server_index == -1)
		server_index = primary_server;
	return server_list[server_index].flags & value;
}

/* get ICB group */
u_char *
server_get_icbgroup(int server_index)
{
	u_char	*group;

	if (server_index == -1)
		server_index = primary_server;
	group = server_list[server_index].group ? server_list[server_index].group : empty_string();
	return (group);
}

/* get ICB mode */
u_char *
server_get_icbmode(int server_index)
{
	u_char	*mode;

	if (server_index == -1)
		server_index = primary_server;
	mode = server_list[server_index].icbmode ? server_list[server_index].icbmode : empty_string();
	return (mode);
}

/*
 * server_get_password: get the passwor for this server.
 */
u_char *
server_get_password(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	return (server_list[server_index].password);
}

/*
 * server_set_version: Sets the server version for the given server type.  A
 * zero version means pre 2.6, a one version means 2.6 aso. (look server.h
 * for typedef)
 */
void
server_set_version(int server_index, int version)
{
	if (server_index == -1)
		server_index = primary_server;
	server_list[server_index].version = version;
}

/*
 * server_get_version: returns the server version value for the given server
 * index 
 */
int
server_get_version(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	if (server_index == -1)
		return DEFAULT_SERVER_VERSION;
	else
		return (server_list[server_index].version);
}

/* server_get_name: returns the name for the given server index */
u_char	*
server_get_name(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	return (server_list[server_index].name);
}

/* server_set_itsname: returns the server's idea of its name */
u_char	*
server_get_itsname(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	if (server_list[server_index].itsname)
		return server_list[server_index].itsname;
	else if (server_list[server_index].name)
		return server_list[server_index].name;
	else
		return UP("<None>");
}

void
server_set_itsname(int server_index, u_char *name)
{
	if (server_index == -1)
		server_index = primary_server;
	malloc_strcpy(&server_list[server_index].itsname, name);
}

/*
 * is_server_open: Returns true if the given server index represents a server
 * with a live connection, returns false otherwise 
 */
int
is_server_open(int server_index)
{
	if (server_index < 0)
		return (0);
	return (server_list[server_index].read != -1);
}

/*
 * is_server_connected: returns true if the given server is connected.  This
 * means that both the tcp connection is open and the user is properly
 * registered 
 */
int
is_server_connected(int server_index)
{
	if (server_index < 0)
		return (0);
	return (server_list[server_index].connected && (server_list[server_index].flags & LOGGED_IN));
}

/* server_get_port: Returns the connection port for the given server index */
int
server_get_port(int server_index)
{
	if (server_index == -1)
		server_index = primary_server;
	return (server_list[server_index].port);
}

/*
 * server_get_nickname: returns the current nickname for the given server
 * index 
 */
u_char	*
server_get_nickname(int server_index)
{
	if ((server_index != -1) && server_list[server_index].nickname)
		return (server_list[server_index].nickname);
	else
		return my_nickname();
}



/* server_get_qhead - get the head of the whois queue */
WhoisQueue *
server_get_qhead(int server_index)
{
	if (server_index >= 0)
		return server_list[server_index].WQ_head;
	return NULL;
}

/* server_get_whois_stuff */
WhoisStuff *
server_get_whois_stuff(int server_index)
{
	if (server_index >= 0)
		return &server_list[server_index].whois_stuff;
	return NULL;
}

/* server_get_qtail - get the tail of the whois queue */
WhoisQueue *
server_get_qtail(int server_index)
{
	if (server_index >= 0)
		return server_list[server_index].WQ_tail;
	return NULL;
}


/* server_set_qhead - set the head of the whois queue */
void
server_set_qhead(int server_index, WhoisQueue *value)
{
	if (server_index >= 0)
		server_list[server_index].WQ_head = value;
}

/* server_set_qtail - set the tail of the whois queue */
void
server_set_qtail(int server_index, WhoisQueue *value)
{
	if (server_index >= 0)
		server_list[server_index].WQ_tail = value;
}


/*
 * server_get_operator: returns true if the user has op privs on the server,
 * false otherwise 
 */
int
server_get_operator(int server_index)
{
	return (server_list[server_index].operator);
}

/*
 * server_set_operator: If flag is non-zero, marks the user as having op
 * privs on the given server.  
 */
void
server_set_operator(int server_index, int flag)
{
	server_list[server_index].operator = flag;
}

/*
 * server_set_nickname: sets the nickname for the given server to nickname.
 * This nickname is then used for all future connections to that server
 * (unless changed with NICK while connected to the server 
 */
void
server_set_nickname(int server_index, u_char *nick)
{
	if (server_index >= 0)
	{
		malloc_strcpy(&(server_list[server_index].nickname), nick);
		if (server_index == primary_server)
			set_nickname(nick);
	}
	update_all_status();
}

void
server_set_motd(int server_index, int flag)
{
	if (server_index >= 0)
		server_list[server_index].motd = flag;
}

int
server_get_motd(int server_index)
{
	if (server_index >= 0)
		return(server_list[server_index].motd);
	return (0);
}

void
server_is_connected(int server_index, int value)
{
	server_list[server_index].connected = value;
	if (value)
		server_list[server_index].eof = 0;
}

/* send_to_server: sends the given info the the server */
void
send_to_server(const char *format, ...)
{
	static	int	in_send_to_server = 0;
	u_char	lbuf[BIG_BUFFER_SIZE];	/* make this buffer *much*
					 * bigger than needed */
	u_char	*buf = lbuf;
	int	des;
	size_t	len;
	int	server = from_server;
	va_list vlist;

	va_start(vlist, format);

	if (in_send_to_server)
		return;
	//memset(lbuf, 0, sizeof(lbuf));
	in_send_to_server = 1;
	if (server == -1)
		server = primary_server;
	if (server != -1 && ((des = server_list[server].write) != -1) &&
	    (server_list[server].flags & LOGGED_IN) )
	{
		/* save space for the packet length */
		if (server_get_version(server) == ServerICB)
			buf++;
		server_list[server].sent = 1;
		vsnprintf(CP(buf), sizeof lbuf, format, vlist);
		va_end(vlist);
		len = my_strlen(buf);
		if (len > (IRCD_BUFFER_SIZE - 2))
			lbuf[IRCD_BUFFER_SIZE - 2] = '\0';
		/*
		 * for ICB, we send a final nul, and for IRC, we have
		 * a final newline.
		 */
		len++;
		if (do_hook(RAW_SEND_LIST, "%s", lbuf))
		{
			if (server_get_version(server) == ServerICB)
			{
				/*
				 * we depend on our caller to split things
				 * up for the ICB server
				 */
				if (len > 254)
					len = 254;
				lbuf[len] = 0;
				lbuf[0] = (u_char)len;
				lbuf[++len] = 0;
			}
			else
				my_strmcat(buf, "\n", IRCD_BUFFER_SIZE);

			/* XXX retval */
			ssl_write(server_list[server].ssl_info,
				  server_list[server].write, CP(lbuf), len);
		}
	}
	else if (!in_redirect() && !connected_to_server())
		say("You are not connected to a server, use /SERVER to connect.");
	in_send_to_server = 0;
}

#ifdef HAVE_SYS_UN_H
/*
 * Connect to a UNIX domain socket. Only works for servers.
 * submitted by Avalon for use with server 2.7.2 and beyond.
 */
int
connect_to_unix(int port, u_char *path)
{
	struct	sockaddr_un un;
	int	    sock;

	sock = socket(AF_UNIX, SOCK_STREAM, 0);

	un.sun_family = AF_UNIX;
	if (snprintf(un.sun_path, sizeof un.sun_path, "%-.100s/%-.6d", path, port) != sizeof un.sun_path)
	{
		yell("--- sun_path truncated: wanted '%-.100s/%-.6d'", path, port);
	}

	if (connect(sock, (struct sockaddr *)&un, (int)my_strlen(path)+2) == -1)
	{
		new_close(sock);
		return -1;
	}
	return sock;
}
#endif /* HAVE_SYS_UN_H */

/*
 * close_all_server: Used whn creating new screens to close all the open
 * server connections in the child process...
 */
void
close_all_server(void)
{
	int	i;

	for (i = 0; i < number_of_servers(); i++)
	{
		if (server_list[i].read != -1)
			new_close(server_list[i].read);
		if (server_list[i].write != -1)
			new_close(server_list[i].write);
	}
}

u_char	*
create_server_list(void)
{
	int	i;
	u_char	*value = NULL;
	StringList *sl;

	sl = sl_init();
	for (i = 0; i < number_of_servers(); i++)
		if (server_list[i].read != -1)
			sl_add(sl, CP(server_get_itsname(i)));
	value = sl_concat(sl, UP(" "));
	sl_free(sl, 0);

	return value;
}

static	void
add_to_server_buffer(int server, u_char *buf)
{
	if (buf && *buf)
	{
		if (server_list[server].buffer)
			malloc_strcat(&server_list[server].buffer, buf);
		else
			malloc_strcpy(&server_list[server].buffer, buf);
	}
}

void
disconnectcmd(u_char *command, u_char *args, u_char *subargs)
{
	u_char	*server;
	u_char	*message;
	int	i;
	int	old_serv;

	if ((server = next_arg(args, &args)) != NULL && server[0] != '*' && server[1] != '\0')
	{
		i = parse_server_index(server);
		if (-1 == i)
		{
			say("No such server!");
			return;
		}
	}
	else
		i = get_window_server(0);
	/*
	 * XXX - this is a major kludge.  i should never equal -1 at
	 * this point.  we only do this because something has gotten
	 * *really* confused at this point.  .mrg.
	 */
	if (i == -1)
	{
		for (i = 0; i < number_of_servers(); i++)
		{
			server_list[i].eof = -1;
			server_list[i].connected = 0;
			new_close(server_list[i].read);
			new_close(server_list[i].write);
		}
		goto done;
	}
	if (!args || !*args)
		message = UP("Disconnecting");
	else
		message = args;
	if (-1 == server_list[i].write)
	{
		say("That server isn't connected!");
		return;
	}
	server = server_list[i].itsname ? server_list[i].itsname :
		server_list[i].name ? server_list[i].name : (u_char *) "unknown?";
	say("Disconnecting from server %s", server);
	old_serv = server_list[i].close_serv;
	close_server(i, message);
	server_list[i].eof = 1;
	if (old_serv != -1 && old_serv != i)
	{
		Window *tmp;
		Win_Trav wt;

		say("Connection to server %s resumed...", server_list[old_serv].name);
		server_list[i].close_serv = -1;
		server_list[old_serv].flags &= ~(CLOSE_PENDING|CLEAR_PENDING);
		server_list[old_serv].flags |= LOGGED_IN;
		server_list[old_serv].connected = 1;
		wt.init = 1;
		while ((tmp = window_traverse(&wt)))
			if (window_get_server(tmp) == i)
			{
				window_set_server(window_get_refnum(tmp), old_serv, WIN_ALL);
				break;
			}
	}
done:
	clean_whois_queue();
	window_check_servers();
	if (!connected_to_server())
		say("You are not connected to a server. Use /SERVER to connect.");
}

/*
 * parse_server: feed this line into the right server parser
 */
static void
parse_server(u_char *line)
{
	server_list[parsing_server_index].parse_server(line);
}

int
server_get_server_group(int server_index)
{
	if (server_index < 0 && server_index >= number_of_servers())
		server_index = from_server;

	return server_list[server_index].server_group;
}

void
server_set_server_group(int server_index, int group)
{
	if (server_index < 0 && server_index >= number_of_servers())
		server_index = from_server;

	server_list[server_index].server_group = group;
}

static void
server_group_get_connected_next(int si)
{
	int i, group;

	group = server_list[si].server_group;

	for (i = si + 1; i != si; i = (i + 1) % number_of_servers())
		if (server_list[i].server_group == group)
		{
			say("Ok, trying server %s...", server_get_itsname(si));
			get_connected(i);
			return;
		}
	say("No servers available.");
}

void
add_server_to_server_group(int server, u_char *group)
{
	int	i = find_server_group(group, 1);

	server_list[server].server_group = i;
	say("Server %s's server group is now %s", server_get_itsname(server), group);
}

int
find_server_group(u_char *group, int add)
{
	static	int	next = 1;
	SGroup *g;

	if (!group || !*group)
		return 0;

	g = (SGroup *) find_in_list((List **)(void *)&server_group_list, group, 0);
	if (g)
		goto end;

	if (!add)
		return 0;

	g = new_malloc(sizeof *g);
	g->name = NULL;
	malloc_strcpy(&g->name, group);
	g->number = next++;
	add_to_list((List **)(void *)&server_group_list, (List *) g);
end:
	return g->number;
}

u_char	*
find_server_group_name(int number)
{
	SGroup *g = server_group_list;

	for (; g; g = g->next)
		if (g->number == number)
			return g->name;
	return empty_string();
}

int
active_server_group(int sgroup)
{
	int i;

	/* kinda blah - returns first active server in that group */
	for (i = 0; i < number_of_servers(); i++)
		if (server_list[i].connected)
			return i;

	return -1;
}

void
server_set_version_string(int server, u_char *ver)
{
	if (ver)
		malloc_strcpy(&server_list[server].version_string, ver);
	else
		new_free(&server_list[server].version_string);
}

u_char *
server_get_version_string(int server)
{
	return server_list[server].version_string;
}

void
server_set_away(int server, u_char *away)
{
	if (away)
		malloc_strcpy(&server_list[server].away, away);
	else
		new_free(&server_list[server].away);
}

u_char *
server_get_away(int server)
{
	return server_list[server].away;
}

void
server_get_local_ip_info(int server, SOCKADDR_STORAGE **sa, socklen_t *salen)
{
	if (sa)
		*sa = server_list[from_server].localaddr;
	if (salen)
		*salen = server_list[from_server].localaddrlen;
}

void
server_set_chan_list(int server, ChannelList *chan)
{
	server_list[server].chan_list = chan;
}

ChannelList *
server_get_chan_list(int server)
{
	return server_list[server].chan_list;
}

void
server_set_attempting_to_connect(int server, int attempt)
{
	server_list[server].attempting_to_connect = attempt;
}

int
server_get_attempting_to_connect(int server)
{
	return server_list[server].attempting_to_connect;
}

void
server_set_sent(int server, int sent)
{
	server_list[server].sent = sent;
}

int
server_get_sent(int server)
{
	return server_list[server].sent;
}

CtcpFlood *
server_get_ctcp_flood(int server_index)
{
	if (server_index < 0 && server_index >= number_of_servers())
		server_index = from_server;

	return server_list[server_index].ctcp_flood;
}

int
number_of_servers(void)
{
	return number_of_servers_count;
}

void
unset_never_connected(void)
{
	never_connected_local = 0;
}

int
never_connected(void)
{
	return never_connected_local;
}

void
set_connected_to_server(int val)
{
	connected_to_server_local = val;
}

int
connected_to_server(void)
{
	return connected_to_server_local;
}

WhoInfo *
server_get_who_info(void)
{
	return server_list[from_server].who_info;
}

int
server_get_oper_command(void)
{
	return server_list[from_server].oper_command;
}

void
server_set_oper_command(int val)
{
	server_list[from_server].oper_command = val;
}

int
parsing_server(void)
{
	return parsing_server_index;
}

int
get_primary_server(void)
{
	return primary_server;
}

void
set_primary_server(int server)
{
	primary_server = server;
}

int
get_from_server(void)
{
	return from_server;
}

int
set_from_server(int server)
{
	int	old_from_server = from_server;

	from_server = server;
	return old_from_server;
}

server_ssl_level
server_do_ssl(int server)
{
	return server_list[server].ssl_level;
}

void
server_default_encryption(u_char *proto, u_char *type)
{
	if (my_stricmp(proto, UP("ICB")) == 0)
	{
		yell("No ICB SSL support yet.");
		return;
	}
	if (my_stricmp(proto, UP("IRC")) != 0)
	{
		yell("Only support IRC and ICB.");
		return;
	}

	/* OK, irc SSL here. */
	if (my_stricmp(type, UP("check")) == 0)
	{
		irc_ssl_level = SSL_VERIFY;
		return;
	}

	if (my_stricmp(type, UP("nocheck")) == 0)
	{
		irc_ssl_level = SSL_ON;
		return;
	}

	if (my_stricmp(type, UP("off")) == 0)
	{
		irc_ssl_level = SSL_OFF;
		return;
	}
}

void	*
server_get_server_private(int server)
{
	return server_list[server].server_private;
}

void
server_set_server_private(int server, void *data, server_private_cb_type cb)
{
	if (server < 0 && server >= number_of_servers())
		server = from_server;

	server_list[server].server_private = data;
	server_list[server].server_private_cb = cb;
}

u_char	*
server_get_proxy_name(int server, int use_default)
{
	u_char	*name;

	if (server < 0 && server >= number_of_servers())
		return NULL;
	name = server_list[server].proxy_name;
	if (server_list[server].proxy_port == -1)
		name = NULL;
	else if (name == NULL && use_default)
		name = default_proxy_name;

	Debug(DB_PROXY, "returning name '%s'", name ? name : UP("<>"));
	return name;
}

int
server_get_proxy_port(int server, int use_default)
{
	int port;

	if (server < 0 && server >= number_of_servers())
		return 0;
	port = server_list[server].proxy_port;
	if (port == 0 && use_default)
		port = default_proxy_port;

	Debug(DB_PROXY, "returning port %d", port);
	return port;
}

void
server_set_default_proxy(u_char *proxy_and_port)
{
	server_split_proxy_port(proxy_and_port,
				&default_proxy_name, &default_proxy_port);
	Debug(DB_PROXY, "set server '%s' port '%d'", default_proxy_name, default_proxy_port);
}