summaryrefslogtreecommitdiffstats
path: root/src/plugins/irc/irc-config.c
blob: 4cf44d175ed594044f1662aac575c6048c42caa9 (plain) (blame)
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
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
/*
 * irc-config.c - IRC configuration options (file irc.conf)
 *
 * Copyright (C) 2003-2019 Sébastien Helleu <flashcode@flashtux.org>
 *
 * This file is part of WeeChat, the extensible chat client.
 *
 * WeeChat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * WeeChat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WeeChat.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <pwd.h>

#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-config.h"
#include "irc-buffer.h"
#include "irc-channel.h"
#include "irc-ctcp.h"
#include "irc-ignore.h"
#include "irc-msgbuffer.h"
#include "irc-nick.h"
#include "irc-notify.h"
#include "irc-server.h"


struct t_config_file *irc_config_file = NULL;
struct t_config_section *irc_config_section_msgbuffer = NULL;
struct t_config_section *irc_config_section_ctcp = NULL;
struct t_config_section *irc_config_section_server_default = NULL;
struct t_config_section *irc_config_section_server = NULL;

int irc_config_loading = 0;

/* IRC config, look section */

struct t_config_option *irc_config_look_buffer_open_before_autojoin;
struct t_config_option *irc_config_look_buffer_open_before_join;
struct t_config_option *irc_config_look_buffer_switch_autojoin;
struct t_config_option *irc_config_look_buffer_switch_join;
struct t_config_option *irc_config_look_color_nicks_in_names;
struct t_config_option *irc_config_look_color_nicks_in_nicklist;
struct t_config_option *irc_config_look_color_nicks_in_server_messages;
struct t_config_option *irc_config_look_color_pv_nick_like_channel;
struct t_config_option *irc_config_look_ctcp_time_format;
struct t_config_option *irc_config_look_display_away;
struct t_config_option *irc_config_look_display_ctcp_blocked;
struct t_config_option *irc_config_look_display_ctcp_reply;
struct t_config_option *irc_config_look_display_ctcp_unknown;
struct t_config_option *irc_config_look_display_host_join;
struct t_config_option *irc_config_look_display_host_join_local;
struct t_config_option *irc_config_look_display_host_quit;
struct t_config_option *irc_config_look_display_join_message;
struct t_config_option *irc_config_look_display_old_topic;
struct t_config_option *irc_config_look_display_pv_away_once;
struct t_config_option *irc_config_look_display_pv_back;
struct t_config_option *irc_config_look_highlight_channel;
struct t_config_option *irc_config_look_highlight_pv;
struct t_config_option *irc_config_look_highlight_server;
struct t_config_option *irc_config_look_highlight_tags_restrict;
struct t_config_option *irc_config_look_item_channel_modes_hide_args;
struct t_config_option *irc_config_look_item_display_server;
struct t_config_option *irc_config_look_item_nick_modes;
struct t_config_option *irc_config_look_item_nick_prefix;
struct t_config_option *irc_config_look_join_auto_add_chantype;
struct t_config_option *irc_config_look_msgbuffer_fallback;
struct t_config_option *irc_config_look_new_channel_position;
struct t_config_option *irc_config_look_new_pv_position;
struct t_config_option *irc_config_look_nick_completion_smart;
struct t_config_option *irc_config_look_nick_mode;
struct t_config_option *irc_config_look_nick_mode_empty;
struct t_config_option *irc_config_look_nicks_hide_password;
struct t_config_option *irc_config_look_notice_as_pv;
struct t_config_option *irc_config_look_notice_welcome_redirect;
struct t_config_option *irc_config_look_notice_welcome_tags;
struct t_config_option *irc_config_look_notify_tags_ison;
struct t_config_option *irc_config_look_notify_tags_whois;
struct t_config_option *irc_config_look_part_closes_buffer;
struct t_config_option *irc_config_look_pv_buffer;
struct t_config_option *irc_config_look_pv_tags;
struct t_config_option *irc_config_look_raw_messages;
struct t_config_option *irc_config_look_server_buffer;
struct t_config_option *irc_config_look_smart_filter;
struct t_config_option *irc_config_look_smart_filter_chghost;
struct t_config_option *irc_config_look_smart_filter_delay;
struct t_config_option *irc_config_look_smart_filter_join;
struct t_config_option *irc_config_look_smart_filter_join_unmask;
struct t_config_option *irc_config_look_smart_filter_mode;
struct t_config_option *irc_config_look_smart_filter_nick;
struct t_config_option *irc_config_look_smart_filter_quit;
struct t_config_option *irc_config_look_temporary_servers;
struct t_config_option *irc_config_look_topic_strip_colors;

/* IRC config, color section */

struct t_config_option *irc_config_color_input_nick;
struct t_config_option *irc_config_color_item_channel_modes;
struct t_config_option *irc_config_color_item_lag_counting;
struct t_config_option *irc_config_color_item_lag_finished;
struct t_config_option *irc_config_color_item_nick_modes;
struct t_config_option *irc_config_color_message_join;
struct t_config_option *irc_config_color_message_chghost;
struct t_config_option *irc_config_color_message_quit;
struct t_config_option *irc_config_color_mirc_remap;
struct t_config_option *irc_config_color_nick_prefixes;
struct t_config_option *irc_config_color_notice;
struct t_config_option *irc_config_color_reason_quit;
struct t_config_option *irc_config_color_topic_current;
struct t_config_option *irc_config_color_topic_new;
struct t_config_option *irc_config_color_topic_old;

/* IRC config, network section */

struct t_config_option *irc_config_network_autoreconnect_delay_growing;
struct t_config_option *irc_config_network_autoreconnect_delay_max;
struct t_config_option *irc_config_network_ban_mask_default;
struct t_config_option *irc_config_network_channel_encode;
struct t_config_option *irc_config_network_colors_receive;
struct t_config_option *irc_config_network_colors_send;
struct t_config_option *irc_config_network_lag_check;
struct t_config_option *irc_config_network_lag_max;
struct t_config_option *irc_config_network_lag_min_show;
struct t_config_option *irc_config_network_lag_reconnect;
struct t_config_option *irc_config_network_lag_refresh_interval;
struct t_config_option *irc_config_network_notify_check_ison;
struct t_config_option *irc_config_network_notify_check_whois;
struct t_config_option *irc_config_network_sasl_fail_unavailable;
struct t_config_option *irc_config_network_send_unknown_commands;
struct t_config_option *irc_config_network_whois_double_nick;

/* IRC config, server section */

struct t_config_option *irc_config_server_default[IRC_SERVER_NUM_OPTIONS];

struct t_hook *irc_config_hook_config_nick_color_options = NULL;
struct t_hook *irc_config_hook_config_chat_nick_colors = NULL;
struct t_hashtable *irc_config_hashtable_display_join_message = NULL;
struct t_hashtable *irc_config_hashtable_nick_prefixes = NULL;
struct t_hashtable *irc_config_hashtable_color_mirc_remap = NULL;
char **irc_config_nicks_hide_password = NULL;
int irc_config_num_nicks_hide_password = 0;

int irc_config_write_temp_servers = 0;


/*
 * Gets server pointer with name of option.
 */

struct t_irc_server *
irc_config_get_server_from_option_name (const char *name)
{
    struct t_irc_server *ptr_server;
    char *pos_option, *server_name;

    ptr_server = NULL;

    if (name)
    {
        pos_option = strrchr (name, '.');
        if (pos_option)
        {
            server_name = weechat_strndup (name, pos_option - name);
            if (server_name)
            {
                ptr_server = irc_server_search (server_name);
                free (server_name);
            }
        }
    }

    return ptr_server;
}

/*
 * Computes nick colors for all servers and channels.
 */

void
irc_config_compute_nick_colors ()
{
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    struct t_irc_nick *ptr_nick;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            for (ptr_nick = ptr_channel->nicks; ptr_nick;
                 ptr_nick = ptr_nick->next_nick)
            {
                if (irc_server_strcasecmp (ptr_server, ptr_nick->name,
                                           ptr_server->nick) != 0)
                {
                    if (ptr_nick->color)
                        free (ptr_nick->color);
                    ptr_nick->color = strdup (irc_nick_find_color (ptr_nick->name));
                }
            }
            if (ptr_channel->pv_remote_nick_color)
            {
                free (ptr_channel->pv_remote_nick_color);
                ptr_channel->pv_remote_nick_color = NULL;
            }
        }
    }

    /* if colors are displayed for nicks in nicklist, refresh them */
    if (weechat_config_boolean (irc_config_look_color_nicks_in_nicklist))
        irc_nick_nicklist_set_color_all ();
}

/*
 * Checks if channel modes arguments must be displayed or hidden
 * (according to option irc.look.item_channel_modes_hide_args).
 *
 * Returns:
 *   1: channel modes arguments must be displayed
 *   0: channel modes arguments must be hidden
 */

int
irc_config_display_channel_modes_arguments (const char *modes)
{
    char *pos_space, *pos;
    const char *ptr_mode;

    pos_space = strchr (modes, ' ');
    if (!pos_space)
        return 1;

    ptr_mode = weechat_config_string (irc_config_look_item_channel_modes_hide_args);
    if (!ptr_mode)
        return 1;

    /* "*" means hide all arguments */
    if (strcmp (ptr_mode, "*") == 0)
        return 0;

    while (ptr_mode[0])
    {
        pos = strchr (modes, ptr_mode[0]);
        if (pos && (pos < pos_space))
            return 0;
        ptr_mode++;
    }

    /* arguments are displayed by default */
    return 1;
}

/*
 * Callback for changes on options changing nick colors.
 */

int
irc_config_change_nick_colors_cb (const void *pointer, void *data,
                                  const char *option, const char *value)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;
    (void) value;

    irc_config_compute_nick_colors ();

    return WEECHAT_RC_OK;
}

/*
 * Callback for changes on option "irc.look.color_nicks_in_nicklist".
 */

void
irc_config_change_look_color_nicks_in_nicklist (const void *pointer,
                                                void *data,
                                                struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    irc_nick_nicklist_set_color_all ();
}

/*
 * Callback for changes on option "irc.look.display_away".
 */

void
irc_config_change_look_display_away (const void *pointer, void *data,
                                     struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (!irc_config_loading
        && (weechat_config_integer (irc_config_look_display_away) == IRC_CONFIG_DISPLAY_AWAY_CHANNEL))
    {
        weechat_printf (
            NULL,
            _("%sWARNING: the value \"channel\" for option "
              "\"irc.look.display_away\" will send all your away changes to "
              "the channels, which is often considered as spam; therefore you "
              "could be banned from channels, you are warned!"),
            weechat_prefix ("error"));
    }
}

/*
 * Callback for changes on option "irc.look.display_join_message".
 */

void
irc_config_change_look_display_join_message (const void *pointer, void *data,
                                             struct t_config_option *option)
{
    char **items;
    int num_items, i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (!irc_config_hashtable_display_join_message)
    {
        irc_config_hashtable_display_join_message = weechat_hashtable_new (
            32,
            WEECHAT_HASHTABLE_STRING,
            WEECHAT_HASHTABLE_STRING,
            NULL, NULL);
    }
    else
        weechat_hashtable_remove_all (irc_config_hashtable_display_join_message);

    items = weechat_string_split (
        weechat_config_string (irc_config_look_display_join_message),
        ",",
        WEECHAT_STRING_SPLIT_STRIP_LEFT
        | WEECHAT_STRING_SPLIT_STRIP_RIGHT
        | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
        0,
        &num_items);
    if (items)
    {
        for (i = 0; i < num_items; i++)
        {
            weechat_hashtable_set (irc_config_hashtable_display_join_message,
                                   items[i], "1");
        }
        weechat_string_free_split (items);
    }
}

/*
 * Callback for changes on option "irc.look.server_buffer".
 */

void
irc_config_change_look_server_buffer (const void *pointer, void *data,
                                      struct t_config_option *option)
{
    struct t_irc_server *ptr_server;
    struct t_gui_buffer *ptr_buffer;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    /* first unmerge all IRC server buffers */
    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->buffer)
            weechat_buffer_unmerge (ptr_server->buffer, -1);
    }

    /* merge IRC server buffers with core buffer or another buffer */
    if ((weechat_config_integer (irc_config_look_server_buffer) ==
         IRC_CONFIG_LOOK_SERVER_BUFFER_MERGE_WITH_CORE)
        || (weechat_config_integer (irc_config_look_server_buffer) ==
            IRC_CONFIG_LOOK_SERVER_BUFFER_MERGE_WITHOUT_CORE))
    {
        ptr_buffer =
            (weechat_config_integer (irc_config_look_server_buffer) ==
             IRC_CONFIG_LOOK_SERVER_BUFFER_MERGE_WITH_CORE) ?
            weechat_buffer_search_main () : irc_buffer_search_server_lowest_number ();

        if (ptr_buffer)
        {
            for (ptr_server = irc_servers; ptr_server;
                 ptr_server = ptr_server->next_server)
            {
                if (ptr_server->buffer && (ptr_server->buffer != ptr_buffer))
                    weechat_buffer_merge (ptr_server->buffer, ptr_buffer);
            }
        }
    }
}

/*
 * Callback for changes on option "irc.look.pv_buffer".
 */

void
irc_config_change_look_pv_buffer (const void *pointer, void *data,
                                  struct t_config_option *option)
{
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    struct t_gui_buffer *ptr_buffer;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    /* first unmerge all IRC private buffers */
    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            if ((ptr_channel->type == IRC_CHANNEL_TYPE_PRIVATE)
                && ptr_channel->buffer)
            {
                weechat_buffer_unmerge (ptr_channel->buffer, -1);
            }
        }
    }

    /* merge IRC private buffers */
    if ((weechat_config_integer (irc_config_look_pv_buffer) == IRC_CONFIG_LOOK_PV_BUFFER_MERGE_BY_SERVER)
        || (weechat_config_integer (irc_config_look_pv_buffer) == IRC_CONFIG_LOOK_PV_BUFFER_MERGE_ALL))
    {
        for (ptr_server = irc_servers; ptr_server;
             ptr_server = ptr_server->next_server)
        {
            for (ptr_channel = ptr_server->channels; ptr_channel;
                 ptr_channel = ptr_channel->next_channel)
            {
                if ((ptr_channel->type == IRC_CHANNEL_TYPE_PRIVATE)
                    && ptr_channel->buffer)
                {
                    ptr_buffer = NULL;
                    switch (weechat_config_integer (irc_config_look_pv_buffer))
                    {
                        case IRC_CONFIG_LOOK_PV_BUFFER_MERGE_BY_SERVER:
                            /* merge private buffers by server */
                            ptr_buffer = irc_buffer_search_private_lowest_number (ptr_server);
                            break;
                        case IRC_CONFIG_LOOK_PV_BUFFER_MERGE_ALL:
                            /* merge *ALL* private buffers */
                            ptr_buffer = irc_buffer_search_private_lowest_number (NULL);
                            break;
                    }
                    if (ptr_buffer && (ptr_channel->buffer != ptr_buffer))
                        weechat_buffer_merge (ptr_channel->buffer, ptr_buffer);
                }
            }
        }
    }
}

/*
 * Callback for changes on option "irc.look.item_channel_modes_hide_args".
 */

void
irc_config_change_look_item_channel_modes_hide_args (const void *pointer,
                                                     void *data,
                                                     struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("buffer_modes");
}

/*
 * Callback for changes on option "irc.look.highlight_tags_restrict".
 */

void
irc_config_change_look_highlight_tags_restrict (const void *pointer, void *data,
                                                struct t_config_option *option)
{
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->buffer)
        {
            weechat_buffer_set (
                ptr_server->buffer, "highlight_tags_restrict",
                weechat_config_string (irc_config_look_highlight_tags_restrict));
        }
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            if (ptr_channel->buffer)
            {
                weechat_buffer_set (
                    ptr_channel->buffer, "highlight_tags_restrict",
                    weechat_config_string (irc_config_look_highlight_tags_restrict));
            }
        }
    }
}

/*
 * Callback for changes on option "irc.look.item_display_server".
 */

void
irc_config_change_look_item_display_server (const void *pointer, void *data,
                                            struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("buffer_plugin");
    weechat_bar_item_update ("buffer_name");
    weechat_bar_item_update ("buffer_short_name");
}

/*
 * Callback for changes on option "irc.look.nicks_hide_password".
 */

void
irc_config_change_look_nicks_hide_password (const void *pointer, void *data,
                                            struct t_config_option *option)
{
    const char *nicks_hide_password;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (irc_config_nicks_hide_password)
    {
        weechat_string_free_split (irc_config_nicks_hide_password);
        irc_config_nicks_hide_password = NULL;
    }
    irc_config_num_nicks_hide_password = 0;

    nicks_hide_password = weechat_config_string (irc_config_look_nicks_hide_password);
    if (nicks_hide_password && nicks_hide_password[0])
    {
        irc_config_nicks_hide_password = weechat_string_split (
            nicks_hide_password,
            ",",
            WEECHAT_STRING_SPLIT_STRIP_LEFT
            | WEECHAT_STRING_SPLIT_STRIP_RIGHT
            | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
            0,
            &irc_config_num_nicks_hide_password);
    }
}

/*
 * Callback for changes on option "irc.look.topic_strip_colors".
 */

void
irc_config_change_look_topic_strip_colors (const void *pointer, void *data,
                                           struct t_config_option *option)
{
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            if (ptr_channel->buffer)
                irc_channel_set_buffer_title (ptr_channel);
        }
    }
}

/*
 * Callback for changes on an option affecting bar item "input_prompt".
 */

void
irc_config_change_bar_item_input_prompt (const void *pointer, void *data,
                                         struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("input_prompt");
}

/*
 * Callback for changes on option "irc.color.item_channel_modes".
 */

void
irc_config_change_color_item_channel_modes (const void *pointer, void *data,
                                            struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("buffer_modes");
}

/*
 * Callback for changes on options "irc.color.item_lag_counting" and
 * "irc.color.item_lag_finished".
 */

void
irc_config_change_color_item_lag (const void *pointer, void *data,
                                  struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("lag");
}

/*
 * Callback for changes on option "irc.color.item_nick_modes".
 */

void
irc_config_change_color_item_nick_modes (const void *pointer, void *data,
                                         struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("input_prompt");
    weechat_bar_item_update ("irc_nick_modes");
}

/*
 * Callback for changes on option "irc.color.mirc_remap".
 */

void
irc_config_change_color_mirc_remap (const void *pointer, void *data,
                                    struct t_config_option *option)
{
    char **items, *pos;
    int num_items, i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (!irc_config_hashtable_color_mirc_remap)
    {
        irc_config_hashtable_color_mirc_remap = weechat_hashtable_new (
            32,
            WEECHAT_HASHTABLE_STRING,
            WEECHAT_HASHTABLE_STRING,
            NULL, NULL);
    }
    else
        weechat_hashtable_remove_all (irc_config_hashtable_color_mirc_remap);

    items = weechat_string_split (
        weechat_config_string (irc_config_color_mirc_remap),
        ";",
        WEECHAT_STRING_SPLIT_STRIP_LEFT
        | WEECHAT_STRING_SPLIT_STRIP_RIGHT
        | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
        0,
        &num_items);
    if (items)
    {
        for (i = 0; i < num_items; i++)
        {
            pos = strchr (items[i], ':');
            if (pos)
            {
                pos[0] = '\0';
                weechat_hashtable_set (irc_config_hashtable_color_mirc_remap,
                                       items[i],
                                       pos + 1);
            }
        }
        weechat_string_free_split (items);
    }
}

/*
 * Callback for changes on option "irc.color.nick_prefixes".
 */

void
irc_config_change_color_nick_prefixes (const void *pointer, void *data,
                                       struct t_config_option *option)
{
    char **items, *pos;
    int num_items, i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (!irc_config_hashtable_nick_prefixes)
    {
        irc_config_hashtable_nick_prefixes = weechat_hashtable_new (
            32,
            WEECHAT_HASHTABLE_STRING,
            WEECHAT_HASHTABLE_STRING,
            NULL, NULL);
    }
    else
        weechat_hashtable_remove_all (irc_config_hashtable_nick_prefixes);

    items = weechat_string_split (
        weechat_config_string (irc_config_color_nick_prefixes),
        ";",
        WEECHAT_STRING_SPLIT_STRIP_LEFT
        | WEECHAT_STRING_SPLIT_STRIP_RIGHT
        | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
        0,
        &num_items);
    if (items)
    {
        for (i = 0; i < num_items; i++)
        {
            pos = strchr (items[i], ':');
            if (pos)
            {
                pos[0] = '\0';
                weechat_hashtable_set (irc_config_hashtable_nick_prefixes,
                                       items[i],
                                       pos + 1);
            }
        }
        weechat_string_free_split (items);
    }

    irc_nick_nicklist_set_prefix_color_all ();

    weechat_bar_item_update ("input_prompt");
}

/*
 * Callback for changes on option "irc.network.lag_check".
 */

void
irc_config_change_network_lag_check (const void *pointer, void *data,
                                     struct t_config_option *option)
{
    time_t time_next_check;
    struct t_irc_server *ptr_server;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    time_next_check = (weechat_config_integer (irc_config_network_lag_check) > 0) ?
        time (NULL) : 0;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->is_connected)
            ptr_server->lag_next_check = time_next_check;
    }
}

/*
 * Callback for changes on option "irc.network.lag_min_show".
 */

void
irc_config_change_network_lag_min_show (const void *pointer, void *data,
                                        struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    weechat_bar_item_update ("lag");
}

/*
 * Callback for changes on option "irc.network.notify_check_ison".
 */

void
irc_config_change_network_notify_check_ison (const void *pointer, void *data,
                                             struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    irc_notify_hook_timer_ison ();
}

/*
 * Callback for changes on option "irc.network.notify_check_whois".
 */

void
irc_config_change_network_notify_check_whois (const void *pointer, void *data,
                                              struct t_config_option *option)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    irc_notify_hook_timer_whois ();
}

/*
 * Callback for changes on option "irc.network.send_unknown_commands".
 */

void
irc_config_change_network_send_unknown_commands (const void *pointer,
                                                 void *data,
                                                 struct t_config_option *option)
{
    char value[2];
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    strcpy (value,
            (weechat_config_boolean (irc_config_network_send_unknown_commands)) ?
            "1" : "0");

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->buffer)
        {
            weechat_buffer_set (ptr_server->buffer,
                                "input_get_unknown_commands", value);
        }
    }

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            if (ptr_channel->buffer)
            {
                weechat_buffer_set (ptr_channel->buffer,
                                    "input_get_unknown_commands", value);
            }
        }
    }
}

/*
 * Callback called when a default server option is modified.
 */

void
irc_config_server_default_change_cb (const void *pointer, void *data,
                                     struct t_config_option *option)
{
    int index_option;
    struct t_irc_server *ptr_server;

    /* make C compiler happy */
    (void) data;

    index_option = irc_server_search_option (pointer);
    if (index_option >= 0)
    {
        for (ptr_server = irc_servers; ptr_server;
             ptr_server = ptr_server->next_server)
        {
            /*
             * when default value for a server option is changed, we apply it
             * on all servers where value is "null" (inherited from default
             * value)
             */
            if (weechat_config_option_is_null (ptr_server->options[index_option]))
            {
                switch (index_option)
                {
                    case IRC_SERVER_OPTION_ADDRESSES:
                        irc_server_set_addresses (ptr_server,
                                                  weechat_config_string (option));
                        break;
                    case IRC_SERVER_OPTION_NICKS:
                        irc_server_set_nicks (ptr_server,
                                              weechat_config_string (option));
                        break;
                    case IRC_SERVER_OPTION_AWAY_CHECK:
                    case IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS:
                        if (IRC_SERVER_OPTION_INTEGER(ptr_server, IRC_SERVER_OPTION_AWAY_CHECK) > 0)
                            irc_server_check_away (ptr_server);
                        else
                            irc_server_remove_away (ptr_server);
                        break;
                }
            }
        }
    }
}

/*
 * Checks string with GnuTLS priorities.
 *
 * Returns NULL if OK, or pointer to char with error in string.
 */

const char *
irc_config_check_gnutls_priorities (const char *priorities)
{
#ifdef HAVE_GNUTLS
    gnutls_priority_t priority_cache;
    const char *pos_error;
    int rc;

    if (!priorities || !priorities[0])
        return NULL;

    rc = gnutls_priority_init (&priority_cache, priorities, &pos_error);
    if (rc == GNUTLS_E_SUCCESS)
    {
        gnutls_priority_deinit (priority_cache);
        return NULL;
    }
    if (pos_error)
        return pos_error;
    return priorities;
#else
    /* make C compiler happy */
    (void) priorities;

    return NULL;
#endif /* HAVE_GNUTLS */
}

/*
 * Checks autojoin value, which must respect the IRC JOIN syntax:
 *   #chan1,#chan2
 *   #chan1,#chan2,#chan3 key1,key2
 *
 * Returns:
 *   1: value OK
 *   0: invalid value
 */

int
irc_config_check_autojoin (const char *autojoin)
{
    char *string, **items, **channels, **keys;
    int rc, num_items, num_channels, num_keys;

    rc = 0;
    string = NULL;
    items = NULL;
    channels = NULL;
    keys = NULL;
    num_items = 0;
    num_channels = 0;
    num_keys = 0;

    /* NULL or empty value is considered as OK */
    if (!autojoin || !autojoin[0])
        return 1;

    /* ignore spaces at beginning/end of string */
    string = weechat_string_strip (autojoin, 1, 1, " ");
    if (!string)
        goto end;

    /* no space allowed before or after a comma */
    if (strstr (string, ", ") || strstr (string, " ,"))
        goto end;

    items = weechat_string_split (string, " ",
                                  WEECHAT_STRING_SPLIT_STRIP_LEFT
                                  | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                                  | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                                  0, &num_items);
    if (!items || (num_items < 1) || (num_items > 2))
        goto end;

    channels = weechat_string_split (items[0], ",",
                                     WEECHAT_STRING_SPLIT_STRIP_LEFT
                                     | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                                     | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                                     0, &num_channels);

    if (num_items == 2)
        keys = weechat_string_split (items[1], ",",
                                     WEECHAT_STRING_SPLIT_STRIP_LEFT
                                     | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                                     | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                                     0, &num_keys);

    /* error if there are more keys than channels to join */
    if (num_keys > num_channels)
        goto end;

    rc = 1;

end:
    if (string)
        free (string);
    if (items)
        weechat_string_free_split (items);
    if (channels)
        weechat_string_free_split (channels);
    if (keys)
        weechat_string_free_split (keys);

    return rc;
}

/*
 * Callback called to check a server option when it is modified.
 */

int
irc_config_server_check_value_cb (const void *pointer, void *data,
                                  struct t_config_option *option,
                                  const char *value)
{
    int index_option, proxy_found;
    const char *pos_error, *proxy_name;
    char *error;
    long number;
    struct t_infolist *infolist;

    /* make C compiler happy */
    (void) data;
    (void) option;

    index_option = irc_server_search_option (pointer);
    if (index_option >= 0)
    {
        switch (index_option)
        {
            case IRC_SERVER_OPTION_PROXY:
                if (value && value[0])
                {
                    proxy_found = 0;
                    infolist = weechat_infolist_get ("proxy", NULL, value);
                    if (infolist)
                    {
                        while (weechat_infolist_next (infolist))
                        {
                            proxy_name = weechat_infolist_string (infolist,
                                                                  "name");
                            if (proxy_name && (strcmp (value, proxy_name) == 0))
                            {
                                proxy_found = 1;
                                break;
                            }
                        }
                        weechat_infolist_free (infolist);
                    }
                    if (!proxy_found)
                    {
                        weechat_printf (
                            NULL,
                            _("%s%s: warning: proxy \"%s\" does not exist "
                              "(you can add it with command /proxy)"),
                            weechat_prefix ("error"), IRC_PLUGIN_NAME, value);
                    }
                }
                break;
            case IRC_SERVER_OPTION_SSL_PRIORITIES:
                pos_error = irc_config_check_gnutls_priorities (value);
                if (pos_error)
                {
                    weechat_printf (
                        NULL,
                        _("%s%s: invalid priorities string, error at this "
                          "position in string: \"%s\""),
                        weechat_prefix ("error"), IRC_PLUGIN_NAME, pos_error);
                    return 0;
                }
                break;
            case IRC_SERVER_OPTION_AUTOJOIN:
                if (!value || !value[0])
                    break;
                if (!irc_config_check_autojoin (value))
                {
                    weechat_printf (
                        NULL,
                        _("%s%s: warning: invalid autojoin value \"%s\", see "
                          "/help %s.%s.%s"),
                        weechat_prefix ("error"),
                        IRC_PLUGIN_NAME,
                        value,
                        weechat_config_option_get_string (option, "config_name"),
                        weechat_config_option_get_string (option, "section_name"),
                        weechat_config_option_get_string (option, "name"));
                }
                break;
            case IRC_SERVER_OPTION_SPLIT_MSG_MAX_LENGTH:
                if (!value || !value[0])
                    break;
                error = NULL;
                number = strtol (value, &error, 10);
                if (!error || error[0])
                {
                    /*
                     * not a valid number, but we return 1 (OK) to let WeeChat
                     * display the appropriate error
                     */
                    return 1;
                }
                if ((number < 0)
                    || ((number > 0) && (number < 128))
                    || (number > 4096))
                {
                    weechat_printf (
                            NULL,
                            _("%s%s: invalid length for split, it must be "
                              "either 0 or any integer between 128 and 4096"),
                            weechat_prefix ("error"), IRC_PLUGIN_NAME);
                    return 0;
                }
                break;
        }
    }

    return 1;
}

/*
 * Callback called when a server option is modified.
 */

void
irc_config_server_change_cb (const void *pointer, void *data,
                             struct t_config_option *option)
{
    int index_option;
    char *name;
    struct t_irc_server *ptr_server;

    /* make C compiler happy */
    (void) data;

    index_option = irc_server_search_option (pointer);
    if (index_option >= 0)
    {
        name = weechat_config_option_get_pointer (option, "name");
        ptr_server = irc_config_get_server_from_option_name (name);
        if (ptr_server)
        {
            switch (index_option)
            {
                case IRC_SERVER_OPTION_ADDRESSES:
                    irc_server_set_addresses (
                        ptr_server,
                        IRC_SERVER_OPTION_STRING(ptr_server,
                                                 IRC_SERVER_OPTION_ADDRESSES));
                    break;
                case IRC_SERVER_OPTION_NICKS:
                    irc_server_set_nicks (
                        ptr_server,
                        IRC_SERVER_OPTION_STRING(ptr_server,
                                                 IRC_SERVER_OPTION_NICKS));
                    break;
                case IRC_SERVER_OPTION_AWAY_CHECK:
                case IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS:
                    if (IRC_SERVER_OPTION_INTEGER(ptr_server, IRC_SERVER_OPTION_AWAY_CHECK) > 0)
                        irc_server_check_away (ptr_server);
                    else
                        irc_server_remove_away (ptr_server);
                    break;
                case IRC_SERVER_OPTION_NOTIFY:
                    irc_notify_new_for_server (ptr_server);
                    break;
            }
        }
    }
}

/*
 * Callback called when "notify" option from "server_default" section is
 * changed.
 *
 * This function is used to reject any value in the option (this option is not
 * used, only values in servers are used for notify).
 *
 * Returns:
 *   1: value is not set
 *   0: value is set
 */

int
irc_config_server_default_check_notify (const void *pointer, void *data,
                                        struct t_config_option *option,
                                        const char *value)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) option;

    if (value && value[0])
        return 0;

    return 1;
}

/*
 * Reloads IRC configuration file.
 */

int
irc_config_reload (const void *pointer, void *data,
                   struct t_config_file *config_file)
{
    int rc;
    struct t_irc_server *ptr_server, *next_server;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        ptr_server->reloading_from_config = 1;
        ptr_server->reloaded_from_config = 0;
    }

    irc_ignore_free_all ();

    irc_config_loading = 1;
    rc = weechat_config_reload (config_file);
    irc_config_loading = 0;

    ptr_server = irc_servers;
    while (ptr_server)
    {
        next_server = ptr_server->next_server;

        /*
         * if server existed before reload, but was not read in irc.conf:
         * - if connected to server: display a warning, keep server in memory
         * - if not connected: delete server
         */
        if (ptr_server->reloading_from_config
            && !ptr_server->reloaded_from_config)
        {
            if (ptr_server->is_connected)
            {
                weechat_printf (
                    NULL,
                    _("%s%s: warning: server \"%s\" not found in "
                      "configuration file, not deleted in memory because it's "
                      "currently used"),
                    weechat_prefix ("error"), IRC_PLUGIN_NAME,
                    ptr_server->name);
            }
            else
                irc_server_free (ptr_server);
        }

        ptr_server = next_server;
    }

    return rc;
}

/*
 * Sets a message target buffer.
 */

int
irc_config_msgbuffer_create_option (const void *pointer, void *data,
                                    struct t_config_file *config_file,
                                    struct t_config_section *section,
                                    const char *option_name, const char *value)
{
    struct t_config_option *ptr_option;
    int rc;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    rc = WEECHAT_CONFIG_OPTION_SET_ERROR;

    if (option_name)
    {
        ptr_option = weechat_config_search_option (config_file, section,
                                                   option_name);
        if (ptr_option)
        {
            if (value)
                rc = weechat_config_option_set (ptr_option, value, 1);
            else
            {
                weechat_config_option_free (ptr_option);
                rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
            }
        }
        else
        {
            if (value)
            {
                ptr_option = weechat_config_new_option (
                    config_file, section,
                    option_name, "integer",
                    _("buffer used to display message received from IRC "
                      "server"),
                    "weechat|server|current|private", 0, 0, value, value, 0,
                    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
                rc = (ptr_option) ?
                    WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE :
                    WEECHAT_CONFIG_OPTION_SET_ERROR;
            }
            else
                rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
        }
    }

    if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
    {
        weechat_printf (
            NULL,
            _("%s%s: error creating \"%s\" => \"%s\""),
            weechat_prefix ("error"), IRC_PLUGIN_NAME, option_name, value);
    }

    return rc;
}

/*
 * Sets a ctcp reply format.
 */

int
irc_config_ctcp_create_option (const void *pointer, void *data,
                               struct t_config_file *config_file,
                               struct t_config_section *section,
                               const char *option_name, const char *value)
{
    struct t_config_option *ptr_option;
    int rc;
    const char *default_value;
    static char empty_value[1] = { '\0' };
    const char *pos_name;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    rc = WEECHAT_CONFIG_OPTION_SET_ERROR;

    if (option_name)
    {
        ptr_option = weechat_config_search_option (config_file, section,
                                                   option_name);
        if (ptr_option)
        {
            if (value)
                rc = weechat_config_option_set (ptr_option, value, 1);
            else
            {
                weechat_config_option_free (ptr_option);
                rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
            }
        }
        else
        {
            if (value)
            {
                pos_name = strchr (option_name, '.');
                pos_name = (pos_name) ? pos_name + 1 : option_name;

                default_value = irc_ctcp_get_default_reply (pos_name);
                if (!default_value)
                    default_value = empty_value;

                ptr_option = weechat_config_new_option (
                    config_file, section,
                    option_name, "string",
                    _("format for CTCP reply or empty string for blocking "
                      "CTCP (no reply), following variables are replaced: "
                      "$version (WeeChat version), "
                      "$compilation (compilation date), "
                      "$osinfo (info about OS), "
                      "$site (WeeChat site), "
                      "$download (WeeChat site, download page), "
                      "$time (current date and time as text), "
                      "$username (username on server), "
                      "$realname (realname on server)"),
                    NULL, 0, 0, default_value, value, 0,
                    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
                rc = (ptr_option) ?
                    WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE :
                    WEECHAT_CONFIG_OPTION_SET_ERROR;
            }
            else
                rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
        }
    }

    if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
    {
        weechat_printf (
            NULL,
            _("%s%s: error creating CTCP \"%s\" => \"%s\""),
            weechat_prefix ("error"), IRC_PLUGIN_NAME, option_name, value);
    }

    return rc;
}

/*
 * Reads ignore option from configuration file.
 *
 * Returns:
 *   1: OK
 *   0: error
 */

int
irc_config_ignore_read_cb (const void *pointer, void *data,
                           struct t_config_file *config_file,
                           struct t_config_section *section,
                           const char *option_name, const char *value)
{
    char **argv, **argv_eol;
    int argc;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) config_file;
    (void) section;

    if (option_name)
    {
        if (value && value[0])
        {
            argv = weechat_string_split (
                value,
                ";",
                WEECHAT_STRING_SPLIT_STRIP_LEFT
                | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                0,
                &argc);
            argv_eol = weechat_string_split (
                value,
                ";",
                WEECHAT_STRING_SPLIT_STRIP_LEFT
                | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS
                | WEECHAT_STRING_SPLIT_KEEP_EOL,
                0,
                NULL);
            if (argv && argv_eol && (argc >= 3))
            {
                irc_ignore_new (argv_eol[2], argv[0], argv[1]);
            }
            if (argv)
                weechat_string_free_split (argv);
            if (argv_eol)
                weechat_string_free_split (argv_eol);
        }
    }

    return 1;
}

/*
 * Writes ignore section in IRC configuration file.
 */

int
irc_config_ignore_write_cb (const void *pointer, void *data,
                            struct t_config_file *config_file,
                            const char *section_name)
{
    struct t_irc_ignore *ptr_ignore;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    if (!weechat_config_write_line (config_file, section_name, NULL))
        return WEECHAT_CONFIG_WRITE_ERROR;

    for (ptr_ignore = irc_ignore_list; ptr_ignore;
         ptr_ignore = ptr_ignore->next_ignore)
    {
        if (!weechat_config_write_line (config_file,
                                        "ignore",
                                        "%s;%s;%s",
                                        (ptr_ignore->server) ? ptr_ignore->server : "*",
                                        (ptr_ignore->channel) ? ptr_ignore->channel : "*",
                                        ptr_ignore->mask))
            return WEECHAT_CONFIG_WRITE_ERROR;
    }

    return WEECHAT_CONFIG_WRITE_OK;
}

/*
 * Creates a new option for a server.
 *
 * Returns pointer to new option, NULL if error.
 */

struct t_config_option *
irc_config_server_new_option (struct t_config_file *config_file,
                              struct t_config_section *section,
                              int index_option,
                              const char *option_name,
                              const char *default_value,
                              const char *value,
                              int null_value_allowed,
                              int (*callback_check_value)(const void *pointer,
                                                          void *data,
                                                          struct t_config_option *option,
                                                          const char *value),
                              const void *callback_check_value_pointer,
                              void *callback_check_value_data,
                              void (*callback_change)(const void *pointer,
                                                      void *data,
                                                      struct t_config_option *option),
                              const void *callback_change_pointer,
                              void *callback_change_data)
{
    struct t_config_option *new_option;

    new_option = NULL;

    switch (index_option)
    {
        case IRC_SERVER_OPTION_ADDRESSES:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("list of hostname/port or IP/port for server (separated by "
                   "comma) "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_PROXY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("name of proxy used for this server (optional, proxy must "
                   "be defined with command /proxy)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_IPV6:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("use IPv6 protocol for server communication (try IPv6 then "
                   "fallback to IPv4); if disabled, only IPv4 is used"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("use SSL for server communication"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL_CERT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("SSL certificate file used to automatically identify your "
                   "nick (\"%h\" will be replaced by WeeChat home, "
                   "\"~/.weechat\" by default)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL_PRIORITIES:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("string with priorities for gnutls (for syntax, see "
                   "documentation of function gnutls_priority_init in gnutls "
                   "manual, common strings are: \"PERFORMANCE\", \"NORMAL\", "
                    "\"SECURE128\", \"SECURE256\", \"EXPORT\", \"NONE\")"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL_DHKEY_SIZE:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("size of the key used during the Diffie-Hellman Key "
                   "Exchange"),
                NULL, 0, INT_MAX,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL_FINGERPRINT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("fingerprint of certificate which is trusted and accepted "
                   "for the server; only hexadecimal digits are allowed (0-9, "
                   "a-f): 128 chars for SHA-512, 64 chars for SHA-256, "
                   "40 chars for SHA-1 (insecure, not recommended); many "
                   "fingerprints can be separated by commas; if this option "
                   "is set, the other checks on certificates are NOT "
                   "performed (option \"ssl_verify\") "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SSL_VERIFY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("check that the SSL connection is fully trusted"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_PASSWORD:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("password for server "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_CAPABILITIES:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                /* TRANSLATORS: please keep words "client capabilities" between brackets if translation is different (see fr.po) */
                N_("comma-separated list of client capabilities to enable for "
                   "server if they are available (see /help cap for a list of "
                   "capabilities supported by WeeChat) "
                   "(example: \"away-notify,multi-prefix\")"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_MECHANISM:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("mechanism for SASL authentication: "
                   "\"plain\" for plain text password, "
                   "\"ecdsa-nist256p-challenge\" for key-based "
                   "challenge authentication, "
                   "\"external\" for authentication using client side SSL "
                   "cert, "
                   "\"dh-blowfish\" for blowfish crypted password "
                   "(insecure, not recommended), "
                   "\"dh-aes\" for AES crypted password "
                   "(insecure, not recommended)"),
                "plain|ecdsa-nist256p-challenge|external|dh-blowfish|dh-aes",
                0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_USERNAME:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("username for SASL authentication; this option is not used "
                   "for mechanism \"external\" "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_PASSWORD:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("password for SASL authentication; this option is not used "
                   "for mechanisms \"ecdsa-nist256p-challenge\" and "
                   "\"external\" "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_KEY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("file with ECC private key for mechanism "
                   "\"ecdsa-nist256p-challenge\" "
                   "(\"%h\" will be replaced by WeeChat home, "
                   "\"~/.weechat\" by default)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_TIMEOUT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("timeout (in seconds) before giving up SASL "
                   "authentication"),
                NULL, 1, 3600,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SASL_FAIL:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("action to perform if SASL authentication fails: "
                   "\"continue\" to ignore the authentication problem, "
                   "\"reconnect\" to schedule a reconnection to the server, "
                   "\"disconnect\" to disconnect from server "
                   "(see also option irc.network.sasl_fail_unavailable)"),
                "continue|reconnect|disconnect", 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTOCONNECT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("automatically connect to server when WeeChat is starting"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTORECONNECT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("automatically reconnect to server when disconnected"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTORECONNECT_DELAY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("delay (in seconds) before trying again to reconnect to "
                   "server"),
                NULL, 1, 65535,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_NICKS:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("nicknames to use on server (separated by comma) "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_NICKS_ALTERNATE:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("get an alternate nick when all the declared nicks are "
                   "already used on server: add some \"_\" until the nick has "
                   "a length of 9, and then replace last char (or the two "
                   "last chars) by a number from 1 to 99, until we find "
                   "a nick not used on server"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_USERNAME:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("user name to use on server "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_REALNAME:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("real name to use on server "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_LOCAL_HOSTNAME:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("custom local hostname/IP for server (optional, if empty "
                   "local hostname is used)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_USERMODE:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("user mode(s) to set after connection to server and before "
                   "executing command and the auto-join of channels; examples: "
                   "\"+R\" (to set mode \"R\"), \"+R-i\" (to set mode \"R\" "
                   "and remove \"i\"); see /help mode for the complete mode "
                   "syntax "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_COMMAND:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("command(s) to run after connection to server and before "
                   "auto-join of channels (many commands can be separated by "
                   "\";\", use \"\\;\" for a semicolon, special variables "
                   "$nick, $channel and $server are replaced by their value) "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_COMMAND_DELAY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("delay (in seconds) after execution of command and before "
                   "auto-join of channels (example: give some time for "
                   "authentication before joining channels)"),
                NULL, 0, 3600,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTOJOIN:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("comma separated list of channels to join after connection "
                   "to server (and after executing command + delay if they are "
                   "set); the channels that require a key must be at beginning "
                   "of the list, and all the keys must be given after the "
                   "channels (separated by a space) (example: \"#channel1,"
                   "#channel2,#channel3 key1,key2\" where #channel1 and "
                   "#channel2 are protected by key1 and key2) "
                   "(note: content is evaluated, see /help eval; server "
                   "options are evaluated with ${irc_server.xxx} and "
                   "${server} is replaced by the server name)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTOREJOIN:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "boolean",
                N_("automatically rejoin channels after kick; you can define "
                   "a buffer local variable on a channel to override this "
                   "value (name of variable: \"autorejoin\", value: \"on\" or "
                   "\"off\")"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AUTOREJOIN_DELAY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("delay (in seconds) before autorejoin (after kick)"),
                NULL, 0, 3600*24,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_CONNECTION_TIMEOUT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("timeout (in seconds) between TCP connection to server and "
                   "message 001 received, if this timeout is reached before "
                   "001 message is received, WeeChat will disconnect from "
                   "server"),
                NULL, 1, 3600,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_ANTI_FLOOD_PRIO_HIGH:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("anti-flood for high priority queue: number of seconds "
                   "between two user messages or commands sent to IRC server "
                   "(0 = no anti-flood)"),
                NULL, 0, 60,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_ANTI_FLOOD_PRIO_LOW:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("anti-flood for low priority queue: number of seconds "
                   "between two messages sent to IRC server (messages like "
                   "automatic CTCP replies) (0 = no anti-flood)"),
                NULL, 0, 60,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AWAY_CHECK:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("interval between two checks for away (in minutes, "
                   "0 = never check)"),
                NULL, 0, 60 * 24 * 7,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("do not check away nicks on channels with high number of "
                   "nicks (0 = unlimited)"),
                NULL, 0, 1000000,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_MSG_KICK:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("default kick message used by commands \"/kick\" and "
                   "\"/kickban\" "
                   "(note: content is evaluated, see /help eval; special "
                   "variables ${nick}, ${channel} and ${server} are replaced "
                   "by their value)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_MSG_PART:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("default part message (leaving channel) "
                   "(note: content is evaluated, see /help eval; special "
                   "variables ${nick}, ${channel} and ${server} are replaced "
                   "by their value; \"%v\" is replaced by WeeChat version if "
                   "there is no ${...} in string)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_MSG_QUIT:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("default quit message (disconnecting from server) "
                   "(note: content is evaluated, see /help eval; special "
                   "variables ${nick}, ${channel} and ${server} are replaced "
                   "by their value; \"%v\" is replaced by WeeChat version if "
                   "there is no ${...} in string)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_NOTIFY:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "string",
                N_("notify list for server (you should not change this option "
                    "but use /notify command instead)"),
                NULL, 0, 0,
                default_value, value,
                null_value_allowed,
                (section == irc_config_section_server_default) ?
                &irc_config_server_default_check_notify : callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_OPTION_SPLIT_MSG_MAX_LENGTH:
            new_option = weechat_config_new_option (
                config_file, section,
                option_name, "integer",
                N_("split outgoing IRC messages to fit in this number of chars; "
                   "the default value is 512, this is a safe and recommended "
                   "value; "
                   "value 0 disables the split (not recommended, unless you "
                   "know what you do); allowed values are 0 or "
                   "any integer between 128 and 4096; "
                   "this option should be changed only on non-standard IRC "
                   "servers, for example gateways like bitlbee"),
                NULL, 0, 4096,
                default_value, value,
                null_value_allowed,
                callback_check_value,
                callback_check_value_pointer,
                callback_check_value_data,
                callback_change,
                callback_change_pointer,
                callback_change_data,
                NULL, NULL, NULL);
            break;
        case IRC_SERVER_NUM_OPTIONS:
            break;
    }

    return new_option;
}

/*
 * Reads server option in IRC configuration file.
 */

int
irc_config_server_read_cb (const void *pointer, void *data,
                           struct t_config_file *config_file,
                           struct t_config_section *section,
                           const char *option_name, const char *value)
{
    struct t_irc_server *ptr_server;
    int index_option, rc, i;
    char *pos_option, *server_name;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) config_file;
    (void) section;

    rc = WEECHAT_CONFIG_OPTION_SET_ERROR;

    if (option_name)
    {
        pos_option = strrchr (option_name, '.');
        if (pos_option)
        {
            server_name = weechat_strndup (option_name,
                                           pos_option - option_name);
            pos_option++;
            if (server_name)
            {
                index_option = irc_server_search_option (pos_option);
                if (index_option >= 0)
                {
                    ptr_server = irc_server_search (server_name);
                    if (!ptr_server)
                        ptr_server = irc_server_alloc (server_name);
                    if (ptr_server)
                    {
                        if (ptr_server->reloading_from_config
                            && !ptr_server->reloaded_from_config)
                        {
                            for (i = 0; i < IRC_SERVER_NUM_OPTIONS; i++)
                            {
                                weechat_config_option_set (
                                    ptr_server->options[i], NULL, 1);
                            }
                            ptr_server->reloaded_from_config = 1;
                        }
                        rc = weechat_config_option_set (
                            ptr_server->options[index_option], value, 1);
                    }
                    else
                    {
                        weechat_printf (
                            NULL,
                            _("%s%s: error adding server \"%s\""),
                            weechat_prefix ("error"), IRC_PLUGIN_NAME,
                            server_name);
                    }
                }
                free (server_name);
            }
        }
    }

    if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
    {
        weechat_printf (
            NULL,
            _("%s%s: error creating server option \"%s\""),
            weechat_prefix ("error"), IRC_PLUGIN_NAME, option_name);
    }

    return rc;
}

/*
 * Writes server section in IRC configuration file.
 */

int
irc_config_server_write_cb (const void *pointer, void *data,
                            struct t_config_file *config_file,
                            const char *section_name)
{
    struct t_irc_server *ptr_server;
    int i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    if (!weechat_config_write_line (config_file, section_name, NULL))
        return WEECHAT_CONFIG_WRITE_ERROR;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (!ptr_server->temp_server || irc_config_write_temp_servers)
        {
            for (i = 0; i < IRC_SERVER_NUM_OPTIONS; i++)
            {
                if (!weechat_config_write_option (config_file,
                                                  ptr_server->options[i]))
                    return WEECHAT_CONFIG_WRITE_ERROR;
            }
        }
    }

    return WEECHAT_CONFIG_WRITE_OK;
}

/*
 * Creates default options for servers.
 */

void
irc_config_server_create_default_options (struct t_config_section *section)
{
    int i, length;
    char *nicks, *username, *realname, *default_value;
    struct passwd *my_passwd;

    nicks = NULL;
    username = NULL;
    realname = strdup ("");

    /* Get the user's name from /etc/passwd */
    if ((my_passwd = getpwuid (geteuid ())) != NULL)
    {
        length = (strlen (my_passwd->pw_name) + 4) * 5;
        nicks = malloc (length);
        if (nicks)
        {
            snprintf (nicks, length, "%s,%s1,%s2,%s3,%s4",
                      my_passwd->pw_name,
                      my_passwd->pw_name,
                      my_passwd->pw_name,
                      my_passwd->pw_name,
                      my_passwd->pw_name);
        }
        username = strdup (my_passwd->pw_name);
    }
    else
    {
        /* default values if /etc/passwd can't be read */
        nicks = strdup (IRC_SERVER_DEFAULT_NICKS);
        username = strdup ("weechat");
    }

    for (i = 0; i < IRC_SERVER_NUM_OPTIONS; i++)
    {
        default_value = NULL;
        switch (i)
        {
            case IRC_SERVER_OPTION_NICKS:
                default_value = nicks;
                break;
            case IRC_SERVER_OPTION_USERNAME:
                default_value = username;
                break;
            case IRC_SERVER_OPTION_REALNAME:
                default_value = realname;
                break;
        }
        if (!default_value)
            default_value = irc_server_options[i][1];

        irc_config_server_default[i] = irc_config_server_new_option (
            irc_config_file,
            section,
            i,
            irc_server_options[i][0],
            irc_server_options[i][1],
            default_value,
            0,
            &irc_config_server_check_value_cb,
            irc_server_options[i][0],
            NULL,
            &irc_config_server_default_change_cb,
            irc_server_options[i][0],
            NULL);
    }

    if (nicks)
        free (nicks);
    if (username)
        free (username);
    if (realname)
        free (realname);
}

/*
 * Initializes IRC configuration file.
 *
 * Returns:
 *   1: OK
 *   0: error
 */

int
irc_config_init ()
{
    struct t_config_section *ptr_section;

    irc_config_hashtable_display_join_message = weechat_hashtable_new (
        32,
        WEECHAT_HASHTABLE_STRING,
        WEECHAT_HASHTABLE_STRING,
        NULL, NULL);
    irc_config_hashtable_nick_prefixes = weechat_hashtable_new (
        32,
        WEECHAT_HASHTABLE_STRING,
        WEECHAT_HASHTABLE_STRING,
        NULL, NULL);
    irc_config_hashtable_color_mirc_remap = weechat_hashtable_new (
        32,
        WEECHAT_HASHTABLE_STRING,
        WEECHAT_HASHTABLE_STRING,
        NULL, NULL);

    irc_config_file = weechat_config_new (IRC_CONFIG_NAME,
                                          &irc_config_reload, NULL, NULL);
    if (!irc_config_file)
        return 0;

    /* look */
    ptr_section = weechat_config_new_section (irc_config_file, "look",
                                              0, 0,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }

    irc_config_look_buffer_open_before_autojoin = weechat_config_new_option (
        irc_config_file, ptr_section,
        "buffer_open_before_autojoin", "boolean",
        N_("open channel buffer before the JOIN is received from server "
           "when it is auto joined (with server option \"autojoin\"); "
           "this is useful to open channels with always the same buffer "
           "numbers on startup"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_buffer_open_before_join = weechat_config_new_option (
        irc_config_file, ptr_section,
        "buffer_open_before_join", "boolean",
        N_("open channel buffer before the JOIN is received from server "
           "when it is manually joined (with /join command)"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_buffer_switch_autojoin = weechat_config_new_option (
        irc_config_file, ptr_section,
        "buffer_switch_autojoin", "boolean",
        N_("auto switch to channel buffer when it is auto joined (with "
           "server option \"autojoin\")"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_buffer_switch_join = weechat_config_new_option (
        irc_config_file, ptr_section,
        "buffer_switch_join", "boolean",
        N_("auto switch to channel buffer when it is manually joined "
           "(with /join command)"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_color_nicks_in_names = weechat_config_new_option (
        irc_config_file, ptr_section,
        "color_nicks_in_names", "boolean",
        N_("use nick color in output of /names (or list of nicks displayed "
           "when joining a channel)"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_color_nicks_in_nicklist = weechat_config_new_option (
        irc_config_file, ptr_section,
        "color_nicks_in_nicklist", "boolean",
        N_("use nick color in nicklist"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_color_nicks_in_nicklist, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_color_nicks_in_server_messages = weechat_config_new_option (
        irc_config_file, ptr_section,
        "color_nicks_in_server_messages", "boolean",
        N_("use nick color in messages from server"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_color_pv_nick_like_channel = weechat_config_new_option (
        irc_config_file, ptr_section,
        "color_pv_nick_like_channel", "boolean",
        N_("use same nick color for channel and private"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_ctcp_time_format = weechat_config_new_option (
        irc_config_file, ptr_section,
        "ctcp_time_format", "string",
        N_("time format used in answer to message CTCP TIME (see man strftime "
           "for date/time specifiers)"),
        NULL, 0, 0, "%a, %d %b %Y %T %z", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_away = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_away", "integer",
        N_("display message when (un)marking as away (off: do not display/send "
           "anything, local: display locally, channel: send action to channels)"),
        "off|local|channel", 0, 0, "local", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_display_away, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_display_ctcp_blocked = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_ctcp_blocked", "boolean",
        N_("display CTCP message even if it is blocked"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_ctcp_reply = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_ctcp_reply", "boolean",
        N_("display CTCP reply sent by WeeChat"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_ctcp_unknown = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_ctcp_unknown", "boolean",
        N_("display CTCP message even if it is unknown CTCP"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_host_join = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_host_join", "boolean",
        N_("display host in join messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_host_join_local = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_host_join_local", "boolean",
        N_("display host in join messages from local client"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_host_quit = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_host_quit", "boolean",
        N_("display host in part/quit messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_join_message = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_join_message", "string",
        N_("comma-separated list of messages to display after joining a "
           "channel: 324 = channel modes, 329 = channel creation date, "
           "332 = topic, 333 = nick/date for topic, 353 = names on channel, "
           "366 = names count"),
        NULL, 0, 0, "329,332,333,366", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_display_join_message, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_display_old_topic = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_old_topic", "boolean",
        N_("display old topic when channel topic is changed"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_pv_away_once = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_pv_away_once", "boolean",
        N_("display remote away message only once in private"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_display_pv_back = weechat_config_new_option (
        irc_config_file, ptr_section,
        "display_pv_back", "boolean",
        N_("display a message in private when user is back (after quit on "
           "server)"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_highlight_channel = weechat_config_new_option (
        irc_config_file, ptr_section,
        "highlight_channel", "string",
        N_("comma separated list of words to highlight in channel buffers "
           "(case insensitive, use \"(?-i)\" at beginning of words to "
           "make them case sensitive; special variables $nick, $channel and "
           "$server are replaced by their value), these words are added to "
           "buffer property \"highlight_words\" only when buffer is created "
           "(it does not affect current buffers), an empty string disables "
            "default highlight on nick, examples: \"$nick\", \"(?-i)$nick\""),
        NULL, 0, 0, "$nick", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_highlight_pv = weechat_config_new_option (
        irc_config_file, ptr_section,
        "highlight_pv", "string",
        N_("comma separated list of words to highlight in private buffers "
           "(case insensitive, use \"(?-i)\" at beginning of words to "
           "make them case sensitive; special variables $nick, $channel and "
           "$server are replaced by their value), these words are added to "
           "buffer property \"highlight_words\" only when buffer is created "
           "(it does not affect current buffers), an empty string disables "
            "default highlight on nick, examples: \"$nick\", \"(?-i)$nick\""),
        NULL, 0, 0, "$nick", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_highlight_server = weechat_config_new_option (
        irc_config_file, ptr_section,
        "highlight_server", "string",
        N_("comma separated list of words to highlight in server buffers "
           "(case insensitive, use \"(?-i)\" at beginning of words to "
           "make them case sensitive; special variables $nick, $channel and "
           "$server are replaced by their value), these words are added to "
           "buffer property \"highlight_words\" only when buffer is created "
           "(it does not affect current buffers), an empty string disables "
           "default highlight on nick, examples: \"$nick\", \"(?-i)$nick\""),
        NULL, 0, 0, "$nick", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_highlight_tags_restrict = weechat_config_new_option (
        irc_config_file, ptr_section,
        "highlight_tags_restrict", "string",
        N_("restrict highlights to these tags on irc buffers (to have "
           "highlight on user messages but not server messages); tags "
           "must be separated by a comma and \"+\" can be used to make a "
           "logical \"and\" between tags; wildcard \"*\" is allowed in tags; "
           "an empty value allows highlight on any tag"),
        NULL, 0, 0, "irc_privmsg,irc_notice", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_highlight_tags_restrict, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_item_channel_modes_hide_args = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_channel_modes_hide_args", "string",
        N_("hide channel modes arguments if at least one of these modes is in "
           "channel modes (\"*\" to always hide all arguments, empty value to "
           "never hide arguments); example: \"kf\" to hide arguments if \"k\" "
           "or \"f\" are in channel modes"),
        NULL, 0, 0, "k", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_item_channel_modes_hide_args, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_item_display_server = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_display_server", "integer",
        N_("name of bar item where IRC server is displayed (for status bar)"),
        "buffer_plugin|buffer_name", 0, 0, "buffer_plugin", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_item_display_server, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_item_nick_modes = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_nick_modes", "boolean",
        N_("display nick modes in bar item \"input_prompt\""),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_bar_item_input_prompt, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_item_nick_prefix = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_nick_prefix", "boolean",
        N_("display nick prefix in bar item \"input_prompt\""),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_bar_item_input_prompt, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_join_auto_add_chantype = weechat_config_new_option (
        irc_config_file, ptr_section,
        "join_auto_add_chantype", "boolean",
        N_("automatically add channel type in front of channel name on "
           "command /join if the channel name does not start with a valid "
           "channel type for the server; for example: \"/join weechat\" will "
           "in fact send: \"/join #weechat\""),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_msgbuffer_fallback = weechat_config_new_option (
        irc_config_file, ptr_section,
        "msgbuffer_fallback", "integer",
        N_("default target buffer for msgbuffer options when target is "
           "private and that private buffer is not found"),
        "current|server", 0, 0, "current", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_new_channel_position = weechat_config_new_option (
        irc_config_file, ptr_section,
        "new_channel_position", "integer",
        N_("force position of new channel in list of buffers "
           "(none = default position (should be last buffer), "
           "next = current buffer + 1, near_server = after last channel/pv "
           "of server)"),
        "none|next|near_server", 0, 0, "none", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_new_pv_position = weechat_config_new_option (
        irc_config_file, ptr_section,
        "new_pv_position", "integer",
        N_("force position of new private in list of buffers "
           "(none = default position (should be last buffer), "
           "next = current buffer + 1, near_server = after last channel/pv "
           "of server)"),
        "none|next|near_server", 0, 0, "none", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_nick_completion_smart = weechat_config_new_option (
        irc_config_file, ptr_section,
        "nick_completion_smart", "integer",
        N_("smart completion for nicks (completes first with last speakers): "
           "speakers = all speakers (including highlights), "
           "speakers_highlights = only speakers with highlight"),
        "off|speakers|speakers_highlights", 0, 0, "speakers", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_nick_mode = weechat_config_new_option (
        irc_config_file, ptr_section,
        "nick_mode", "integer",
        N_("display nick mode (op, voice, ...) before nick (none = never, "
           "prefix = in prefix only (default), action = in action messages "
           "only, both = prefix + action messages)"),
        "none|prefix|action|both", 0, 0, "prefix", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_nick_mode_empty = weechat_config_new_option (
        irc_config_file, ptr_section,
        "nick_mode_empty", "boolean",
        N_("display a space if nick mode is enabled but nick has no mode (not "
           "op, voice, ...)"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_bar_item_input_prompt, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_nicks_hide_password = weechat_config_new_option (
        irc_config_file, ptr_section,
        "nicks_hide_password", "string",
        N_("comma separated list of nicks for which passwords will be hidden "
           "when a message is sent, for example to hide password in message "
           "displayed by \"/msg nickserv identify password\", example: "
           "\"nickserv,nickbot\""),
        NULL, 0, 0, "nickserv", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_nicks_hide_password, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_notice_as_pv = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notice_as_pv", "integer",
        N_("display notices as private messages (if auto, use private buffer "
           "if found)"),
        "auto|never|always", 0, 0, "auto", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_notice_welcome_redirect = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notice_welcome_redirect", "boolean",
        N_("automatically redirect channel welcome notices to the channel "
           "buffer; such notices have the nick as target but a channel name "
           "in beginning of notice message, for example the ENTRYMSG notices "
           "sent by Atheme IRC Services which look like: "
           "\"[#channel] Welcome to this channel...\""),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_notice_welcome_tags = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notice_welcome_tags", "string",
        N_("comma separated list of tags used in a welcome notices redirected "
           "to a channel, for example: \"notify_private\""),
        NULL, 0, 0, "", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_notify_tags_ison = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notify_tags_ison", "string",
        N_("comma separated list of tags used in messages displayed by notify "
           "when a nick joins or quits server (result of command ison or "
           "monitor), for example: \"notify_message\", \"notify_private\" or "
           "\"notify_highlight\""),
        NULL, 0, 0, "notify_message", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_notify_tags_whois = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notify_tags_whois", "string",
        N_("comma separated list of tags used in messages displayed by notify "
           "when a nick away status changes (result of command whois), "
           "for example: \"notify_message\", \"notify_private\" or "
           "\"notify_highlight\""),
        NULL, 0, 0, "notify_message", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_part_closes_buffer = weechat_config_new_option (
        irc_config_file, ptr_section,
        "part_closes_buffer", "boolean",
        N_("close buffer when /part is issued on a channel"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_pv_buffer = weechat_config_new_option (
        irc_config_file, ptr_section,
        "pv_buffer", "integer",
        N_("merge private buffers"),
        "independent|merge_by_server|merge_all", 0, 0, "independent", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_pv_buffer, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_pv_tags = weechat_config_new_option (
        irc_config_file, ptr_section,
        "pv_tags", "string",
        N_("comma separated list of tags used in private messages, for example: "
           "\"notify_message\", \"notify_private\" or \"notify_highlight\""),
        NULL, 0, 0, "notify_private", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_raw_messages = weechat_config_new_option (
        irc_config_file, ptr_section,
        "raw_messages", "integer",
        N_("number of raw messages to save in memory when raw data buffer is "
           "closed (messages will be displayed when opening raw data buffer)"),
        NULL, 0, 65535, "256", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_server_buffer = weechat_config_new_option (
        irc_config_file, ptr_section,
        "server_buffer", "integer",
        N_("merge server buffers; this option has no effect if a layout "
           "is saved and is conflicting with this value (see /help layout)"),
        "merge_with_core|merge_without_core|independent", 0, 0, "merge_with_core",
        NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_server_buffer, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_look_smart_filter = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter", "boolean",
        N_("filter join/part/quit/nick messages for a nick if not speaking "
           "for some minutes on channel (you must create a filter on tag "
           "\"irc_smart_filter\")"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_chghost = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_chghost", "boolean",
        /* TRANSLATORS: please do not translate "chghost" */
        N_("enable smart filter for \"chghost\" messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_delay = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_delay", "integer",
        N_("delay for filtering join/part/quit messages (in minutes): if the "
           "nick did not speak during the last N minutes, the join/part/quit is "
           "filtered"),
        NULL, 1, 60*24*7, "5", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_join = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_join", "boolean",
        /* TRANSLATORS: please do not translate "join" */
        N_("enable smart filter for \"join\" messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_join_unmask = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_join_unmask", "integer",
        N_("delay for unmasking a join message that was filtered with tag "
           "\"irc_smart_filter\" (in minutes): if a nick has joined max N "
           "minutes ago and then says something on channel (message, notice or "
           "update on topic), the join is unmasked, as well as nick changes "
           "after this join (0 = disable: never unmask a join)"),
        NULL, 0, 60*24*7, "30", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_mode = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_mode", "string",
        /* TRANSLATORS: please do not translate "mode" */
        N_("enable smart filter for \"mode\" messages: \"*\" to filter all "
           "modes, \"+\" to filter all modes in server prefixes (for example "
           "\"ovh\"), \"xyz\" to filter only modes x/y/z, \"-xyz\" to filter "
           "all modes but not x/y/z; examples: \"ovh\": filter modes o/v/h, "
           "\"-bkl\": filter all modes but not b/k/l"),
        NULL, 0, 0, "+", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_nick = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_nick", "boolean",
        /* TRANSLATORS: please do not translate "nick" */
        N_("enable smart filter for \"nick\" messages (nick changes)"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_smart_filter_quit = weechat_config_new_option (
        irc_config_file, ptr_section,
        "smart_filter_quit", "boolean",
        /* TRANSLATORS: please do not translate "part" and "quit" */
        N_("enable smart filter for \"part\" and \"quit\" messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_temporary_servers = weechat_config_new_option (
        irc_config_file, ptr_section,
        "temporary_servers", "boolean",
        N_("enable automatic addition of temporary servers with command "
           "/connect"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_look_topic_strip_colors = weechat_config_new_option (
        irc_config_file, ptr_section,
        "topic_strip_colors", "boolean",
        N_("strip colors in topic (used only when displaying buffer title)"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_look_topic_strip_colors, NULL, NULL,
        NULL, NULL, NULL);

    /* color */
    ptr_section = weechat_config_new_section (irc_config_file, "color",
                                              0, 0,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }

    irc_config_color_input_nick = weechat_config_new_option (
        irc_config_file, ptr_section,
        "input_nick", "color",
        N_("color for nick in input bar"),
        NULL, -1, 0, "lightcyan", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_bar_item_input_prompt, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_item_channel_modes = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_channel_modes", "color",
        N_("color for channel modes, near channel name"),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_item_channel_modes, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_item_lag_counting = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_lag_counting", "color",
        N_("color for lag indicator, when counting (pong not received from "
           "server, lag is increasing)"),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_item_lag, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_item_lag_finished = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_lag_finished", "color",
        N_("color for lag indicator, when pong has been received from server"),
        NULL, -1, 0, "yellow", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_item_lag, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_item_nick_modes = weechat_config_new_option (
        irc_config_file, ptr_section,
        "item_nick_modes", "color",
        N_("color for nick modes in bar item \"input_prompt\""),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_item_nick_modes, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_message_join = weechat_config_new_option (
        irc_config_file, ptr_section,
        "message_join", "color",
        N_("color for text in join messages"),
        NULL, -1, 0, "green", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_message_chghost = weechat_config_new_option (
        irc_config_file, ptr_section,
        "message_chghost", "color",
        N_("color for text in chghost messages"),
        NULL, -1, 0, "brown", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_message_quit = weechat_config_new_option (
        irc_config_file, ptr_section,
        "message_quit", "color",
        N_("color for text in part/quit messages"),
        NULL, -1, 0, "red", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_mirc_remap = weechat_config_new_option (
        irc_config_file, ptr_section,
        "mirc_remap", "string",
        /* TRANSLATORS: please do not translate the list of WeeChat color names at the end of string */
        N_("remap mirc colors in messages using a hashtable: keys are \"fg,bg\" "
           "as integers between -1 (not specified) and 15, values are WeeChat "
           "color names or numbers (format is: \"1,-1:color1;2,7:color2\"), "
           "example: \"1,-1:darkgray;1,2:white,blue\" to remap black to "
           "\"darkgray\" and black on blue to \"white,blue\"; default "
           "WeeChat colors for IRC codes: 0=white, 1=black, 2=blue, 3=green, "
           "4=lightred, 5=red, 6=magenta, 7=brown, 8=yellow, 9=lightgreen, "
           "10=cyan, 11=lightcyan, 12=lightblue, 13=lightmagenta, "
           "14=darkgray, 15=gray"),
        NULL, 0, 0, "1,-1:darkgray", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_mirc_remap, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_nick_prefixes = weechat_config_new_option (
        irc_config_file, ptr_section,
        "nick_prefixes", "string",
        N_("color for nick prefixes using mode char (o=op, h=halfop, v=voice, "
           "..), format is: \"o:color1;h:color2;v:color3\" (if a mode is not "
           "found, WeeChat will try with next modes received from server "
           "(\"PREFIX\"); a special mode \"*\" can be used as default color "
           "if no mode has been found in list)"),
        NULL, 0, 0, "y:lightred;q:lightred;a:lightcyan;o:lightgreen;"
        "h:lightmagenta;v:yellow;*:lightblue", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_color_nick_prefixes, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_color_notice = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notice", "color",
        N_("color for text \"Notice\" in notices"),
        NULL, -1, 0, "green", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_reason_quit = weechat_config_new_option (
        irc_config_file, ptr_section,
        "reason_quit", "color",
        N_("color for reason in part/quit messages"),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_topic_current = weechat_config_new_option (
        irc_config_file, ptr_section,
        "topic_current", "color",
        N_("color for current channel topic (when joining a channel or "
           "using /topic)"),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_topic_new = weechat_config_new_option (
        irc_config_file, ptr_section,
        "topic_new", "color",
        N_("color for new channel topic (when topic is changed)"),
        NULL, -1, 0, "white", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_color_topic_old = weechat_config_new_option (
        irc_config_file, ptr_section,
        "topic_old", "color",
        N_("color for old channel topic (when topic is changed)"),
        NULL, -1, 0, "default", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

    /* network */
    ptr_section = weechat_config_new_section (irc_config_file, "network",
                                              0, 0,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL,
                                              NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }

    irc_config_network_autoreconnect_delay_growing = weechat_config_new_option (
        irc_config_file, ptr_section,
        "autoreconnect_delay_growing", "integer",
        N_("growing factor for autoreconnect delay to server (1 = always same "
           "delay, 2 = delay*2 for each retry, etc.)"),
        NULL, 1, 100, "2", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_autoreconnect_delay_max = weechat_config_new_option (
        irc_config_file, ptr_section,
        "autoreconnect_delay_max", "integer",
        N_("maximum autoreconnect delay to server (in seconds, 0 = no maximum)"),
        NULL, 0, 3600 * 24 * 7, "600", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_ban_mask_default = weechat_config_new_option (
        irc_config_file, ptr_section,
        "ban_mask_default", "string",
        N_("default ban mask for commands /ban, /unban and /kickban; variables "
           "$nick, $user, $ident and $host are replaced by their values "
           "(extracted from \"nick!user@host\"); $ident is the same as $user if "
           "$user does not start with \"~\", otherwise it is set to \"*\"; this "
            "default mask is used only if WeeChat knows the host for the nick"),
        NULL, 0, 0, "*!$ident@$host", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_channel_encode = weechat_config_new_option (
        irc_config_file, ptr_section,
        "channel_encode", "boolean",
        N_("decode/encode channel name inside messages using charset options; "
           "it is recommended to keep that off if you use only UTF-8 in "
           "channel names; you can enable this option if you are using an "
           "exotic charset like ISO in channel names"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_colors_receive = weechat_config_new_option (
        irc_config_file, ptr_section,
        "colors_receive", "boolean",
        N_("when off, colors codes are ignored in incoming messages"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_colors_send = weechat_config_new_option (
        irc_config_file, ptr_section,
        "colors_send", "boolean",
        N_("allow user to send colors with special codes (ctrl-c + a code and "
           "optional color: b=bold, cxx=color, cxx,yy=color+background, "
           "i=italic, o=disable color/attributes, r=reverse, u=underline)"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_lag_check = weechat_config_new_option (
        irc_config_file, ptr_section,
        "lag_check", "integer",
        N_("interval between two checks for lag (in seconds, 0 = never "
           "check)"),
        NULL, 0, 3600 * 24 * 7, "60", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_network_lag_check, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_network_lag_max = weechat_config_new_option (
        irc_config_file, ptr_section,
        "lag_max", "integer",
        N_("maximum lag (in seconds): if this lag is reached, WeeChat will "
           "consider that the answer from server (pong) will never be received "
           "and will give up counting the lag (0 = never give up)"),
        NULL, 0, 3600 * 24 * 7, "1800", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_lag_min_show = weechat_config_new_option (
        irc_config_file, ptr_section,
        "lag_min_show", "integer",
        N_("minimum lag to show (in milliseconds)"),
        NULL, 0, 1000 * 3600 * 24, "500", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_network_lag_min_show, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_network_lag_reconnect = weechat_config_new_option (
        irc_config_file, ptr_section,
        "lag_reconnect", "integer",
        N_("reconnect to server if lag is greater than or equal to this value "
           "(in seconds, 0 = never reconnect); this value must be less than or "
           "equal to irc.network.lag_max"),
        NULL, 0, 3600 * 24 * 7, "300", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_lag_refresh_interval = weechat_config_new_option (
        irc_config_file, ptr_section,
        "lag_refresh_interval", "integer",
        N_("interval between two refreshes of lag item, when lag is "
           "increasing (in seconds)"),
        NULL, 1, 3600, "1", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_notify_check_ison = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notify_check_ison", "integer",
        N_("interval between two checks for notify with IRC command \"ison\" "
           "(in minutes)"),
        NULL, 1, 60 * 24 * 7, "1", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_network_notify_check_ison, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_network_notify_check_whois = weechat_config_new_option (
        irc_config_file, ptr_section,
        "notify_check_whois", "integer",
        N_("interval between two checks for notify with IRC command \"whois\" "
           "(in minutes)"),
        NULL, 1, 60 * 24 * 7, "5", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_network_notify_check_whois, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_network_sasl_fail_unavailable = weechat_config_new_option (
        irc_config_file, ptr_section,
        "sasl_fail_unavailable", "boolean",
        N_("cause SASL authentication failure when SASL is requested but "
           "unavailable on the server; when this option is enabled, it has "
           "effect only if option \"sasl_fail\" is set to \"reconnect\" or "
           "\"disconnect\" in the server"),
        NULL, 0, 0, "on", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    irc_config_network_send_unknown_commands = weechat_config_new_option (
        irc_config_file, ptr_section,
        "send_unknown_commands", "boolean",
        N_("send unknown commands to server"),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL,
        &irc_config_change_network_send_unknown_commands, NULL, NULL,
        NULL, NULL, NULL);
    irc_config_network_whois_double_nick = weechat_config_new_option (
        irc_config_file, ptr_section,
        "whois_double_nick", "boolean",
        N_("double the nick in /whois command (if only one nick is given), to "
           "get idle time in answer; for example: \"/whois nick\" will send "
           "\"whois nick nick\""),
        NULL, 0, 0, "off", NULL, 0,
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

    /* msgbuffer */
    ptr_section = weechat_config_new_section (
        irc_config_file, "msgbuffer",
        1, 1,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        &irc_config_msgbuffer_create_option, NULL, NULL,
        NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }
    irc_config_section_msgbuffer = ptr_section;

    /* CTCP */
    ptr_section = weechat_config_new_section (
        irc_config_file, "ctcp",
        1, 1,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        &irc_config_ctcp_create_option, NULL, NULL,
        NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }
    irc_config_section_ctcp = ptr_section;

    /* ignore */
    ptr_section = weechat_config_new_section (
        irc_config_file, "ignore",
        0, 0,
        &irc_config_ignore_read_cb, NULL, NULL,
        &irc_config_ignore_write_cb, NULL, NULL,
        &irc_config_ignore_write_cb, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }

    /* server_default */
    ptr_section = weechat_config_new_section (
        irc_config_file, "server_default",
        0, 0,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }
    irc_config_section_server_default = ptr_section;

    irc_config_server_create_default_options (ptr_section);

    /* server */
    ptr_section = weechat_config_new_section (
        irc_config_file, "server",
        0, 0,
        &irc_config_server_read_cb, NULL, NULL,
        &irc_config_server_write_cb, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL,
        NULL, NULL, NULL);
    if (!ptr_section)
    {
        weechat_config_free (irc_config_file);
        irc_config_file = NULL;
        return 0;
    }
    irc_config_section_server = ptr_section;

    irc_config_hook_config_nick_color_options = weechat_hook_config (
        "weechat.look.nick_color_*",
        &irc_config_change_nick_colors_cb, NULL, NULL);
    irc_config_hook_config_chat_nick_colors = weechat_hook_config (
        "weechat.color.chat_nick_colors",
        &irc_config_change_nick_colors_cb, NULL, NULL);

    return 1;
}

/*
 * Reads IRC configuration file.
 */

int
irc_config_read ()
{
    int rc;

    irc_config_loading = 1;
    rc = weechat_config_read (irc_config_file);
    irc_config_loading = 0;

    if (rc == WEECHAT_CONFIG_READ_OK)
    {
        irc_notify_new_for_all_servers ();
        irc_config_change_look_display_join_message (NULL, NULL, NULL);
        irc_config_change_look_nicks_hide_password (NULL, NULL, NULL);
        irc_config_change_color_nick_prefixes (NULL, NULL, NULL);
        irc_config_change_color_mirc_remap (NULL, NULL, NULL);
        irc_config_change_network_notify_check_ison (NULL, NULL, NULL);
        irc_config_change_network_notify_check_whois (NULL, NULL, NULL);
    }

    return rc;
}

/*
 * Writes IRC configuration file.
 */

int
irc_config_write (int write_temp_servers)
{
    irc_config_write_temp_servers = write_temp_servers;

    return weechat_config_write (irc_config_file);
}

/*
 * Frees IRC configuration.
 */

void
irc_config_free ()
{
    weechat_config_free (irc_config_file);

    if (irc_config_hook_config_nick_color_options)
    {
        weechat_unhook (irc_config_hook_config_nick_color_options);
        irc_config_hook_config_nick_color_options = NULL;
    }

    if (irc_config_hook_config_chat_nick_colors)
    {
        weechat_unhook (irc_config_hook_config_chat_nick_colors);
        irc_config_hook_config_chat_nick_colors = NULL;
    }

    if (irc_config_nicks_hide_password)
    {
        weechat_string_free_split (irc_config_nicks_hide_password);
        irc_config_nicks_hide_password = NULL;
        irc_config_num_nicks_hide_password = 0;
    }

    if (irc_config_hashtable_display_join_message)
    {
        weechat_hashtable_free (irc_config_hashtable_display_join_message);
        irc_config_hashtable_display_join_message = NULL;
    }

    if (irc_config_hashtable_nick_prefixes)
    {
        weechat_hashtable_free (irc_config_hashtable_nick_prefixes);
        irc_config_hashtable_nick_prefixes = NULL;
    }

    if (irc_config_hashtable_color_mirc_remap)
    {
        weechat_hashtable_free (irc_config_hashtable_color_mirc_remap);
        irc_config_hashtable_color_mirc_remap = NULL;
    }
}