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
|
/* Channel-handling routines.
*
* (C) 2003-2014 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*
*/
#include "services.h"
#include "language.h"
Channel *chanlist[1024];
#define HASH(chan) ((chan)[1] ? ((chan)[1]&31)<<5 | ((chan)[2]&31) : 0)
/**************************** External Calls *****************************/
/*************************************************************************/
void chan_deluser(User * user, Channel * c)
{
struct c_userlist *u;
if (c->ci)
update_cs_lastseen(user, c->ci);
for (u = c->users; u && u->user != user; u = u->next);
if (!u)
return;
if (u->ud) {
if (u->ud->lastline)
free(u->ud->lastline);
free(u->ud);
}
if (u->next)
u->next->prev = u->prev;
if (u->prev)
u->prev->next = u->next;
else
c->users = u->next;
free(u);
c->usercount--;
if (s_BotServ && c->ci && c->ci->bi && c->usercount == BSMinUsers - 1) {
anope_cmd_part(c->ci->bi->nick, c->name, NULL);
}
/* Channel is permament and shouldn't be deleted */
if (anope_get_permchan_mode() && c->mode & anope_get_permchan_mode())
return;
if (!c->users)
chan_delete(c);
}
/*************************************************************************/
/* Returns a fully featured binary modes string. If complete is 0, the
* eventual parameters won't be added to the string.
*/
char *chan_get_modes(Channel * chan, int complete, int plus)
{
static char res[BUFSIZE];
char *end = res;
if (chan->mode) {
int n = 0;
CBModeInfo *cbmi = cbmodeinfos;
do {
if (chan->mode & cbmi->flag)
*end++ = cbmi->mode;
} while ((++cbmi)->mode != 0 && ++n < sizeof(res) - 1);
if (complete) {
cbmi = cbmodeinfos;
do {
if (cbmi->getvalue && (chan->mode & cbmi->flag) &&
(plus || !(cbmi->flags & CBM_MINUS_NO_ARG))) {
char *value = cbmi->getvalue(chan);
if (value) {
*end++ = ' ';
while (*value)
*end++ = *value++;
}
}
} while ((++cbmi)->mode != 0 && ++n < sizeof(res) - 1);
}
}
*end = 0;
return res;
}
/*************************************************************************/
/* Retrieves the status of an user on a channel */
int chan_get_user_status(Channel * chan, User * user)
{
struct u_chanlist *uc;
for (uc = user->chans; uc; uc = uc->next)
if (uc->chan == chan)
return uc->status;
return 0;
}
/*************************************************************************/
/* Has the given user the given status on the given channel? :p */
int chan_has_user_status(Channel * chan, User * user, int16 status)
{
struct u_chanlist *uc;
for (uc = user->chans; uc; uc = uc->next) {
if (uc->chan == chan) {
if (debug) {
alog("debug: chan_has_user_status wanted %d the user is %d", status, uc->status);
}
return (uc->status & status);
}
}
return 0;
}
/*************************************************************************/
/* Remove the status of an user on a channel */
void chan_remove_user_status(Channel * chan, User * user, int16 status)
{
struct u_chanlist *uc;
if (debug >= 2)
alog("debug: removing user status (%d) from %s for %s", status,
user->nick, chan->name);
for (uc = user->chans; uc; uc = uc->next) {
if (uc->chan == chan) {
uc->status &= ~status;
break;
}
}
}
/*************************************************************************/
void chan_set_modes(const char *source, Channel * chan, int ac, char **av,
int check)
{
int add = 1;
char *modes = av[0], mode;
CBMode *cbm;
CMMode *cmm;
CUMode *cum;
unsigned char botmode = 0;
BotInfo *bi;
User *u, *user;
int i, real_ac = ac;
char **real_av = av;
if (debug)
alog("debug: Changing modes for %s to %s", chan->name,
merge_args(ac, av));
if (UseTS6 && ircd->ts6) {
u = find_byuid(source);
if (!u) u = finduser(source);
} else
u = finduser(source);
if (u && (chan_get_user_status(chan, u) & CUS_DEOPPED)) {
char *s;
if (debug)
alog("debug: Removing instead of setting due to DEOPPED flag");
/* Swap adding and removing of the modes */
for (s = av[0]; *s; s++) {
if (*s == '+')
*s = '-';
else if (*s == '-')
*s = '+';
}
/* Set the resulting mode buffer */
anope_cmd_mode(whosends(chan->ci), chan->name, merge_args(ac, av));
chan_set_modes(whosends(chan->ci), chan, ac, av, check);
return;
}
ac--;
while ((mode = *modes++)) {
switch (mode) {
case '+':
add = 1;
continue;
case '-':
add = 0;
continue;
}
if (((int) mode) < 0) {
if (debug)
alog("Debug: Malformed mode detected on %s.", chan->name);
continue;
}
if ((cum = &cumodes[(int) mode])->status != 0) {
if (ac == 0) {
alog("channel: mode %c%c with no parameter (?) for channel %s", add ? '+' : '-', mode, chan->name);
continue;
}
ac--;
av++;
if ((cum->flags & CUF_PROTECT_BOTSERV) && !add) {
if ((bi = findbot(*av))) {
if (!botmode || botmode != mode) {
anope_cmd_mode(bi->nick, chan->name, "+%c %s",
mode, bi->nick);
botmode = mode;
continue;
} else {
botmode = mode;
continue;
}
}
} else {
if ((bi = findbot(*av))) {
continue;
}
}
if (UseTS6 && ircd->ts6) {
user = find_byuid(*av);
if (!user) user = finduser(*av);
} else
user = finduser(*av);
if (!user) {
if (debug)
alog("debug: MODE %s %c%c for nonexistent user %s",
chan->name, (add ? '+' : '-'), mode, *av);
continue;
}
if (debug)
alog("debug: Setting %c%c on %s for %s", (add ? '+' : '-'),
mode, chan->name, user->nick);
if (add) {
chan_set_user_status(chan, user, cum->status);
/* If this does +o or +h, remove any DEOPPED flag */
if (cum->status & CUS_OP || cum->status & CUS_HALFOP)
chan_remove_user_status(chan, user, CUS_DEOPPED);
} else {
chan_remove_user_status(chan, user, cum->status);
}
} else if ((cbm = &cbmodes[(int) mode])->flag != 0) {
if (check >= 0) {
if (add)
chan->mode |= cbm->flag;
else
chan->mode &= ~cbm->flag;
}
if (cbm->setvalue) {
if (add || !(cbm->flags & CBM_MINUS_NO_ARG)) {
if (ac == 0) {
alog("channel: mode %c%c with no parameter (?) for channel %s", add ? '+' : '-', mode, chan->name);
continue;
}
ac--;
av++;
}
cbm->setvalue(chan, add ? *av : NULL);
}
if (check < 0) {
if (add)
chan->mode |= cbm->flag;
else
chan->mode &= ~cbm->flag;
}
} else if ((cmm = &cmmodes[(int) mode])->addmask) {
if (ac == 0) {
alog("channel: mode %c%c with no parameter (?) for channel %s", add ? '+' : '-', mode, chan->name);
continue;
}
ac--;
av++;
if (add)
cmm->addmask(chan, *av);
else
cmm->delmask(chan, *av);
}
}
/* Don't bounce modes from u:lined clients or servers, bug #1004. *
* We can get UUIDs as well.. don not assume nick ~ Viper */
if (UseTS6 && ircd->ts6) {
user = find_byuid(source);
if (!user) user = finduser(source);
} else
user = finduser(source);
if ((user && is_ulined(user->server->name)) || is_ulined((char *)source))
return;
if (check > 0) {
check_modes(chan);
if ((check < 2) || (check == 3)) {
/* Walk through all users we've set modes for and see if they are
* valid. Invalid modes (like +o with SECUREOPS on) will be removed
*/
real_ac--;
real_av++;
for (i = 0; i < real_ac; i++) {
if (UseTS6 && ircd->ts6) {
user = find_byuid(*real_av);
if (!user) user = finduser(*real_av);
} else
user = finduser(*real_av);
if (!user && UseTS6 && ircd->ts6) user = find_byuid(*real_av);
if (user && is_on_chan(chan, user)) {
if (check < 2)
chan_set_correct_modes(user, chan, 0);
else if ((chan->ci->flags) && (chan->ci->flags & CI_SECUREOPS))
chan_set_correct_modes(user, chan, 0);
}
real_av++;
}
}
}
}
/*************************************************************************/
/* Set the status of an user on a channel */
void chan_set_user_status(Channel * chan, User * user, int16 status)
{
struct u_chanlist *uc;
if (debug >= 2)
alog("debug: setting user status (%d) on %s for %s", status,
user->nick, chan->name);
if (HelpChannel && ircd->supporthelper && (status & CUS_OP)
&& (stricmp(chan->name, HelpChannel) == 0)
&& (!chan->ci || check_access(user, chan->ci, CA_AUTOOP))) {
if (debug) {
alog("debug: %s being given +h for having %d status in %s",
user->nick, status, chan->name);
}
common_svsmode(user, "+h", NULL);
}
for (uc = user->chans; uc; uc = uc->next) {
if (uc->chan == chan) {
uc->status |= status;
break;
}
}
}
/*************************************************************************/
/* Return the Channel structure corresponding to the named channel, or NULL
* if the channel was not found. chan is assumed to be non-NULL and valid
* (i.e. pointing to a channel name of 2 or more characters). */
Channel *findchan(const char *chan)
{
Channel *c;
if (!chan || !*chan) {
if (debug) {
alog("debug: findchan() called with NULL values");
}
return NULL;
}
if (debug >= 3)
alog("debug: findchan(%p)", chan);
c = chanlist[HASH(chan)];
while (c) {
if (stricmp(c->name, chan) == 0) {
if (debug >= 3)
alog("debug: findchan(%s) -> %p", chan, (void *) c);
return c;
}
c = c->next;
}
if (debug >= 3)
alog("debug: findchan(%s) -> %p", chan, (void *) c);
return NULL;
}
/*************************************************************************/
/* Iterate over all channels in the channel list. Return NULL at end of
* list.
*/
static Channel *current;
static int next_index;
Channel *firstchan(void)
{
next_index = 0;
while (next_index < 1024 && current == NULL)
current = chanlist[next_index++];
if (debug >= 3)
alog("debug: firstchan() returning %s",
current ? current->name : "NULL (end of list)");
return current;
}
Channel *nextchan(void)
{
if (current)
current = current->next;
if (!current && next_index < 1024) {
while (next_index < 1024 && current == NULL)
current = chanlist[next_index++];
}
if (debug >= 3)
alog("debug: nextchan() returning %s",
current ? current->name : "NULL (end of list)");
return current;
}
/*************************************************************************/
/* Return statistics. Pointers are assumed to be valid. */
void get_channel_stats(long *nrec, long *memuse)
{
long count = 0, mem = 0;
Channel *chan;
struct c_userlist *cu;
BanData *bd;
int i;
for (i = 0; i < 1024; i++) {
for (chan = chanlist[i]; chan; chan = chan->next) {
count++;
mem += sizeof(*chan);
if (chan->topic)
mem += strlen(chan->topic) + 1;
if (chan->key)
mem += strlen(chan->key) + 1;
if (ircd->fmode) {
if (chan->flood)
mem += strlen(chan->flood) + 1;
}
if (ircd->Lmode) {
if (chan->redirect)
mem += strlen(chan->redirect) + 1;
}
if (ircd->jmode) {
if (chan->throttle)
mem += strlen(chan->throttle) + 1;
}
mem += get_memuse(chan->bans);
if (ircd->except) {
mem += get_memuse(chan->excepts);
}
if (ircd->invitemode) {
mem += get_memuse(chan->invites);
}
for (cu = chan->users; cu; cu = cu->next) {
mem += sizeof(*cu);
if (cu->ud) {
mem += sizeof(*cu->ud);
if (cu->ud->lastline)
mem += strlen(cu->ud->lastline) + 1;
}
}
for (bd = chan->bd; bd; bd = bd->next) {
if (bd->mask)
mem += strlen(bd->mask) + 1;
mem += sizeof(*bd);
}
}
}
*nrec = count;
*memuse = mem;
}
/*************************************************************************/
/* Is the given nick on the given channel? */
int is_on_chan(Channel * c, User * u)
{
struct u_chanlist *uc;
for (uc = u->chans; uc; uc = uc->next)
if (uc->chan == c)
return 1;
return 0;
}
/*************************************************************************/
/* Is the given nick on the given channel?
This function supports links. */
User *nc_on_chan(Channel * c, NickCore * nc)
{
struct c_userlist *u;
if (!c || !nc)
return NULL;
for (u = c->users; u; u = u->next) {
if (u->user->na && u->user->na->nc == nc
&& nick_recognized(u->user))
return u->user;
}
return NULL;
}
/*************************************************************************/
/*************************** Message Handling ****************************/
/*************************************************************************/
/* Handle a JOIN command.
* av[0] = channels to join
*/
void do_join(const char *source, int ac, char **av)
{
User *user;
Channel *chan;
char *s, *t;
struct u_chanlist *c, *nextc;
char *channame;
time_t ts = time(NULL);
if (UseTS6 && ircd->ts6) {
user = find_byuid(source);
if (!user)
user = finduser(source);
} else {
user = finduser(source);
}
if (!user) {
if (debug) {
alog("debug: JOIN from nonexistent user %s: %s", source,
merge_args(ac, av));
}
return;
}
t = av[0];
while (*(s = t)) {
t = s + strcspn(s, ",");
if (*t)
*t++ = 0;
if (*s == '0') {
c = user->chans;
while (c) {
nextc = c->next;
channame = sstrdup(c->chan->name);
send_event(EVENT_PART_CHANNEL, 3, EVENT_START, user->nick,
channame);
chan_deluser(user, c->chan);
send_event(EVENT_PART_CHANNEL, 3, EVENT_STOP, user->nick,
channame);
free(channame);
free(c);
c = nextc;
}
user->chans = NULL;
continue;
}
/* how about not triggering the JOIN event on an actual /part :) -certus */
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_START, source, s);
/* Make sure check_kick comes before chan_adduser, so banned users
* don't get to see things like channel keys. */
/* If channel already exists, check_kick() will use correct TS.
* Otherwise, we lose. */
if (check_kick(user, s, ts))
continue;
if (ac == 2) {
ts = strtoul(av[1], NULL, 10);
if (debug) {
alog("debug: recieved a new TS for JOIN: %ld",
(long int) ts);
}
}
chan = findchan(s);
chan = join_user_update(user, chan, s, ts);
chan_set_correct_modes(user, chan, 1);
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_STOP, source, s);
}
}
/*************************************************************************/
/* Handle a KICK command.
* av[0] = channel
* av[1] = nick(s) being kicked
* av[2] = reason
*/
void do_kick(const char *source, int ac, char **av)
{
BotInfo *bi;
ChannelInfo *ci;
User *user;
char *s, *t;
struct u_chanlist *c;
Uid *uid;
t = av[1];
while (*(s = t)) {
t = s + strcspn(s, ",");
if (*t)
*t++ = 0;
if (ircd->ts6 && UseTS6)
{
uid = find_nickuid(s);
if (uid)
s = uid->nick;
}
/* If it is the bot that is being kicked, we make it rejoin the
* channel and stop immediately.
* --lara
*/
if (s_BotServ && (bi = findbot(s)) && (ci = cs_findchan(av[0]))) {
bot_join(ci);
continue;
}
if (UseTS6 && ircd->ts6) {
user = find_byuid(s);
if (!user) {
user = finduser(s);
}
} else {
user = finduser(s);
}
if (!user) {
if (debug) {
alog("debug: KICK for nonexistent user %s on %s: %s", s,
av[0], merge_args(ac - 2, av + 2));
}
continue;
}
if (debug) {
alog("debug: kicking %s from %s", user->nick, av[0]);
}
for (c = user->chans; c && stricmp(av[0], c->chan->name) != 0;
c = c->next);
if (c) {
send_event(EVENT_CHAN_KICK, 2, user->nick, av[0]);
chan_deluser(user, c->chan);
if (c->next)
c->next->prev = c->prev;
if (c->prev)
c->prev->next = c->next;
else
user->chans = c->next;
free(c);
}
}
}
/*************************************************************************/
/* Handle a PART command.
* av[0] = channels to leave
* av[1] = reason (optional)
*/
void do_part(const char *source, int ac, char **av)
{
User *user;
char *s, *t;
struct u_chanlist *c;
char *channame;
if (UseTS6 && ircd->ts6) {
user = find_byuid(source);
if (!user)
user = finduser(source);
} else {
user = finduser(source);
}
if (!user) {
if (debug) {
alog("debug: PART from nonexistent user %s: %s", source,
merge_args(ac, av));
}
return;
}
t = av[0];
while (*(s = t)) {
t = s + strcspn(s, ",");
if (*t)
*t++ = 0;
if (debug)
alog("debug: %s leaves %s", source, s);
for (c = user->chans; c && stricmp(s, c->chan->name) != 0;
c = c->next);
if (c) {
if (!c->chan) {
alog("user: BUG parting %s: channel entry found but c->chan NULL", s);
return;
}
channame = sstrdup(c->chan->name);
send_event(EVENT_PART_CHANNEL, (ac >= 2 ? 4 : 3), EVENT_START,
user->nick, channame, (ac >= 2 ? av[1] : ""));
chan_deluser(user, c->chan);
if (c->next)
c->next->prev = c->prev;
if (c->prev)
c->prev->next = c->next;
else
user->chans = c->next;
free(c);
send_event(EVENT_PART_CHANNEL, (ac >= 2 ? 4 : 3), EVENT_STOP,
user->nick, channame, (ac >= 2 ? av[1] : ""));
free(channame);
}
}
}
/*************************************************************************/
/* Handle a SJOIN command.
On channel creation, syntax is:
av[0] = timestamp
av[1] = channel name
av[2|3|4] = modes \ depends of whether the modes k and l
av[3|4|5] = users / are set or not.
When a single user joins an (existing) channel, it is:
av[0] = timestamp
av[1] = user
============================================================
Unreal SJOIN
On Services connect there is
SJOIN !11LkOb #ircops +nt :@Trystan &*!*@*.aol.com "*@*.home.com
av[0] = time stamp (base64)
av[1] = channel
av[2] = modes
av[3] = users + bans + exceptions
On Channel Creation or a User joins an existing
Luna.NomadIrc.Net SJOIN !11LkW9 #akill :@Trystan
Luna.NomadIrc.Net SJOIN !11LkW9 #akill :Trystan`
av[0] = time stamp (base64)
av[1] = channel
av[2] = users
*/
void do_sjoin(const char *source, int ac, char **av)
{
Channel *c;
User *user;
Server *serv = NULL;
struct c_userlist *cu;
char *s = NULL;
char *end, cubuf[7], *end2, *cumodes[6], *buf;
int is_sqlined = 0;
int ts = 0;
int is_created = 0;
int keep_their_modes = 1;
if (UseTS6 && ircd->ts6)
serv = findserver_uid(servlist, source);
if (!serv)
serv = findserver(servlist, source);
if (ircd->sjb64) {
ts = base64dects(av[0]);
} else {
ts = strtoul(av[0], NULL, 10);
}
c = findchan(av[1]);
if (c != NULL) {
if (c->creation_time == 0 || ts == 0)
c->creation_time = 0;
else if (c->creation_time > ts) {
c->creation_time = ts;
for (cu = c->users; cu; cu = cu->next) {
/* XXX */
cumodes[0] = "-ov";
cumodes[1] = cu->user->nick;
cumodes[2] = cu->user->nick;
chan_set_modes(source, c, 3, cumodes, 2);
}
if (c->ci)
{
if (c->ci->bi)
{
/* This is ugly, but it always works */
anope_cmd_part(c->ci->bi->nick, c->name, "TS reop");
bot_join(c->ci);
}
/* Make sure +r is set */
if (ircd->chanreg && ircd->regmode)
{
c->mode |= ircd->regmode;
anope_cmd_mode(whosends(c->ci), c->name, "+r");
}
}
/* XXX simple modes and bans */
} else if (c->creation_time < ts)
keep_their_modes = 0;
} else
is_created = 1;
/* Double check to avoid unknown modes that need parameters */
if (ac >= 4) {
if (ircd->chansqline) {
if (!c)
is_sqlined = check_chan_sqline(av[1]);
}
cubuf[0] = '+';
cumodes[0] = cubuf;
/* We make all the users join */
s = av[ac - 1]; /* Users are always the last element */
while (*s) {
end = strchr(s, ' ');
if (end)
*end = 0;
end2 = cubuf + 1;
if (ircd->sjoinbanchar) {
if (*s == ircd->sjoinbanchar && keep_their_modes) {
buf = myStrGetToken(s, ircd->sjoinbanchar, 1);
add_ban(c, buf);
if (buf)
free(buf);
if (!end)
break;
s = end + 1;
continue;
}
}
if (ircd->sjoinexchar) {
if (*s == ircd->sjoinexchar && keep_their_modes) {
buf = myStrGetToken(s, ircd->sjoinexchar, 1);
add_exception(c, buf);
if (buf)
free(buf);
if (!end)
break;
s = end + 1;
continue;
}
}
if (ircd->sjoininvchar) {
if (*s == ircd->sjoininvchar && keep_their_modes) {
buf = myStrGetToken(s, ircd->sjoininvchar, 1);
add_invite(c, buf);
if (buf)
free(buf);
if (!end)
break;
s = end + 1;
continue;
}
}
while (csmodes[(int) *s] != 0)
*end2++ = csmodes[(int) *s++];
*end2 = 0;
if (UseTS6 && ircd->ts6) {
user = find_byuid(s);
if (!user)
user = finduser(s);
} else {
user = finduser(s);
}
if (!user) {
if (debug) {
alog("debug: SJOIN for nonexistent user %s on %s", s,
av[1]);
}
return;
}
if (is_sqlined && !is_oper(user)) {
anope_cmd_kick(s_OperServ, av[1], s, "Q-Lined");
} else {
if (!check_kick(user, av[1], ts)) {
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_START,
user->nick, av[1]);
/* Make the user join; if the channel does not exist it
* will be created there. This ensures that the channel
* is not created to be immediately destroyed, and
* that the locked key or topic is not shown to anyone
* who joins the channel when empty.
*/
c = join_user_update(user, c, av[1], ts);
/* We update user mode on the channel */
if (end2 - cubuf > 1 && keep_their_modes) {
int i;
for (i = 1; i < end2 - cubuf; i++)
cumodes[i] = user->nick;
chan_set_modes(source, c, 1 + (end2 - cubuf - 1),
cumodes, 2);
}
if (c->ci && (!serv || is_sync(serv))
&& !c->topic_sync)
restore_topic(c->name);
chan_set_correct_modes(user, c, 1);
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_STOP,
user->nick, av[1]);
}
}
if (!end)
break;
s = end + 1;
}
if (c && keep_their_modes) {
/* We now update the channel mode. */
chan_set_modes(source, c, ac - 3, &av[2], 2);
}
/* Unreal just had to be different */
} else if (ac == 3 && !ircd->ts6) {
if (ircd->chansqline) {
if (!c)
is_sqlined = check_chan_sqline(av[1]);
}
cubuf[0] = '+';
cumodes[0] = cubuf;
/* We make all the users join */
s = av[2]; /* Users are always the last element */
while (*s) {
end = strchr(s, ' ');
if (end)
*end = 0;
end2 = cubuf + 1;
while (csmodes[(int) *s] != 0)
*end2++ = csmodes[(int) *s++];
*end2 = 0;
if (UseTS6 && ircd->ts6) {
user = find_byuid(s);
if (!user)
user = finduser(s);
} else {
user = finduser(s);
}
if (!user) {
if (debug) {
alog("debug: SJOIN for nonexistent user %s on %s", s,
av[1]);
}
return;
}
if (is_sqlined && !is_oper(user)) {
anope_cmd_kick(s_OperServ, av[1], s, "Q-Lined");
} else {
if (!check_kick(user, av[1], ts)) {
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_START,
user->nick, av[1]);
/* Make the user join; if the channel does not exist it
* will be created there. This ensures that the channel
* is not created to be immediately destroyed, and
* that the locked key or topic is not shown to anyone
* who joins the channel when empty.
*/
c = join_user_update(user, c, av[1], ts);
/* We update user mode on the channel */
if (end2 - cubuf > 1 && keep_their_modes) {
int i;
for (i = 1; i < end2 - cubuf; i++)
cumodes[i] = user->nick;
chan_set_modes(source, c, 1 + (end2 - cubuf - 1),
cumodes, 2);
}
chan_set_correct_modes(user, c, 1);
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_STOP,
user->nick, av[1]);
}
}
if (!end)
break;
s = end + 1;
}
} else if (ac == 3 && ircd->ts6) {
if (ircd->chansqline) {
if (!c)
is_sqlined = check_chan_sqline(av[1]);
}
cubuf[0] = '+';
cumodes[0] = cubuf;
/* We make all the users join */
s = sstrdup(source); /* Users are always the last element */
while (*s) {
end = strchr(s, ' ');
if (end)
*end = 0;
end2 = cubuf + 1;
while (csmodes[(int) *s] != 0)
*end2++ = csmodes[(int) *s++];
*end2 = 0;
if (UseTS6 && ircd->ts6) {
user = find_byuid(s);
if (!user)
user = finduser(s);
} else {
user = finduser(s);
}
if (!user) {
if (debug) {
alog("debug: SJOIN for nonexistent user %s on %s", s,
av[1]);
}
free(s);
return;
}
if (is_sqlined && !is_oper(user)) {
anope_cmd_kick(s_OperServ, av[1], s, "Q-Lined");
} else {
if (!check_kick(user, av[1], ts)) {
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_START,
user->nick, av[1]);
/* Make the user join; if the channel does not exist it
* will be created there. This ensures that the channel
* is not created to be immediately destroyed, and
* that the locked key or topic is not shown to anyone
* who joins the channel when empty.
*/
c = join_user_update(user, c, av[1], ts);
/* We update user mode on the channel */
if (end2 - cubuf > 1 && keep_their_modes) {
int i;
for (i = 1; i < end2 - cubuf; i++)
cumodes[i] = user->nick;
chan_set_modes(source, c, 1 + (end2 - cubuf - 1),
cumodes, 2);
}
chan_set_correct_modes(user, c, 1);
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_STOP,
user->nick, av[1]);
}
}
if (!end)
break;
s = end + 1;
}
free(s);
} else if (ac == 2) {
if (UseTS6 && ircd->ts6) {
user = find_byuid(source);
if (!user)
user = finduser(source);
} else {
user = finduser(source);
}
if (!user) {
if (debug) {
alog("debug: SJOIN for nonexistent user %s on %s", source,
av[1]);
}
return;
}
if (check_kick(user, av[1], ts))
return;
if (ircd->chansqline) {
if (!c)
is_sqlined = check_chan_sqline(av[1]);
}
if (is_sqlined && !is_oper(user)) {
anope_cmd_kick(s_OperServ, av[1], user->nick, "Q-Lined");
} else {
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_START, user->nick,
av[1]);
c = join_user_update(user, c, av[1], ts);
if (is_created && c->ci)
restore_topic(c->name);
chan_set_correct_modes(user, c, 1);
send_event(EVENT_JOIN_CHANNEL, 3, EVENT_STOP, user->nick,
av[1]);
}
}
}
/*************************************************************************/
/* Handle a channel MODE command. */
void do_cmode(const char *source, int ac, char **av)
{
Channel *chan;
ChannelInfo *ci = NULL;
int i;
char *t;
if (ircdcap->tsmode) {
/* TSMODE for bahamut - leave this code out to break MODEs. -GD */
/* if they don't send it in CAPAB check if we just want to enable it */
if (uplink_capab & ircdcap->tsmode || UseTSMODE) {
for (i = 0; i < strlen(av[1]); i++) {
if (!isdigit(av[1][i]))
break;
}
if (av[1][i] == '\0') {
/* We have a valid TS field in av[1] now, so we can strip it off */
/* After we swap av[0] and av[1] ofcourse to not break stuff! :) */
t = av[0];
av[0] = av[1];
av[1] = t;
ac--;
av++;
} else {
alog("TSMODE enabled but MODE has no valid TS");
}
}
}
/* :42XAAAAAO TMODE 1106409026 #ircops +b *!*@*.aol.com */
if (UseTS6 && ircd->ts6) {
if (isdigit(av[0][0])) {
ac--;
av++;
}
}
chan = findchan(av[0]);
if (!chan) {
if (debug) {
ci = cs_findchan(av[0]);
if (!(ci && (ci->flags & CI_VERBOTEN)))
alog("debug: MODE %s for nonexistent channel %s",
merge_args(ac - 1, av + 1), av[0]);
}
return;
}
/* This shouldn't trigger on +o, etc. */
if (strchr(source, '.') && !av[1][strcspn(av[1], "bovahq")]) {
if (time(NULL) != chan->server_modetime) {
chan->server_modecount = 0;
chan->server_modetime = time(NULL);
}
chan->server_modecount++;
}
ac--;
av++;
chan_set_modes(source, chan, ac, av, 1);
}
/*************************************************************************/
/* Handle a TOPIC command. */
void do_topic(const char *source, int ac, char **av)
{
Channel *c = findchan(av[0]);
ChannelInfo *ci;
int ts;
time_t topic_time;
char *topicsetter;
if (ircd->sjb64) {
ts = base64dects(av[2]);
if (debug) {
alog("debug: encoded TOPIC TS %s converted to %d", av[2], ts);
}
} else {
ts = strtoul(av[2], NULL, 10);
}
topic_time = ts;
if (!c) {
if (debug) {
alog("debug: TOPIC %s for nonexistent channel %s",
merge_args(ac - 1, av + 1), av[0]);
}
return;
}
/* We can be sure that the topic will be in sync here -GD */
c->topic_sync = 1;
ci = c->ci;
/* For Unreal, cut off the ! and any futher part of the topic setter.
* This way, nick!ident@host setters will only show the nick. -GD
*/
topicsetter = myStrGetToken(av[1], '!', 0);
/* If the current topic we have matches the last known topic for this
* channel exactly, there's no need to update anything and we can as
* well just return silently without updating anything. -GD
* But we still need to update the topic internally for the channel - Adam
*/
if ((ac > 3) && *av[3] && ci && ci->last_topic
&& (strcmp(av[3], ci->last_topic) == 0)
&& (strcmp(topicsetter, ci->last_topic_setter) == 0)) {
if (c->topic)
free(c->topic);
c->topic = sstrdup(av[3]);
strscpy(c->topic_setter, topicsetter, sizeof(c->topic_setter));
c->topic_time = topic_time;
free(topicsetter);
return;
}
if (check_topiclock(c, topic_time)) {
free(topicsetter);
return;
}
if (c->topic) {
free(c->topic);
c->topic = NULL;
}
if (ac > 3 && *av[3]) {
c->topic = sstrdup(av[3]);
}
strscpy(c->topic_setter, topicsetter, sizeof(c->topic_setter));
c->topic_time = topic_time;
free(topicsetter);
record_topic(av[0]);
/* Only call events if we are synced with the uplink */
if (serv_uplink && is_sync(serv_uplink)) {
if (ci && ci->last_topic) {
send_event(EVENT_TOPIC_UPDATED, 2, av[0], ci->last_topic);
} else {
send_event(EVENT_TOPIC_UPDATED, 2, av[0], "");
}
}
}
/*************************************************************************/
/**************************** Internal Calls *****************************/
/*************************************************************************/
void add_ban(Channel * chan, char *mask)
{
Entry *ban;
/* check for NULL values otherwise we will segfault */
if (!chan || !mask) {
if (debug) {
alog("debug: add_ban called with NULL values");
}
return;
}
/* Check if the list already exists, if not create it.
* Create a new ban and add it to the list.. ~ Viper */
if (!chan->bans)
chan->bans = list_create();
ban = entry_add(chan->bans, mask);
if (!ban)
fatal("Creating new ban entry failed");
/* Check whether it matches a botserv bot after adding internally
* and parsing it through cidr support. ~ Viper */
if (s_BotServ && BSSmartJoin && chan->ci && chan->ci->bi
&& chan->usercount >= BSMinUsers) {
BotInfo *bi = chan->ci->bi;
if (entry_match(ban, bi->nick, bi->user, bi->host, 0)) {
anope_cmd_mode(bi->nick, chan->name, "-b %s", mask);
entry_delete(chan->bans, ban);
return;
}
}
if (debug)
alog("debug: Added ban %s to channel %s", mask, chan->name);
}
/*************************************************************************/
void add_exception(Channel * chan, char *mask)
{
Entry *exception;
if (!chan || !mask) {
if (debug)
alog("debug: add_exception called with NULL values");
return;
}
/* Check if the list already exists, if not create it.
* Create a new exception and add it to the list.. ~ Viper */
if (!chan->excepts)
chan->excepts = list_create();
exception = entry_add(chan->excepts, mask);
if (!exception)
fatal("Creating new exception entry failed");
if (debug)
alog("debug: Added except %s to channel %s", mask, chan->name);
}
/*************************************************************************/
void add_invite(Channel * chan, char *mask)
{
Entry *invite;
if (!chan || !mask) {
if (debug)
alog("debug: add_invite called with NULL values");
return;
}
/* Check if the list already exists, if not create it.
* Create a new invite and add it to the list.. ~ Viper */
if (!chan->invites)
chan->invites = list_create();
invite = entry_add(chan->invites, mask);
if (!invite)
fatal("Creating new exception entry failed");
if (debug)
alog("debug: Added invite %s to channel %s", mask, chan->name);
}
/*************************************************************************/
/**
* Set the correct modes, or remove the ones granted without permission,
* for the specified user on ths specified channel. This doesn't give
* modes to ignored users, but does remove them if needed.
* @param user The user to give/remove modes to/from
* @param c The channel to give/remove modes on
* @param give_modes Set to 1 to give modes, 0 to not give modes
* @return void
**/
void chan_set_correct_modes(User * user, Channel * c, int give_modes)
{
char *tmp;
char modebuf[BUFSIZE];
char userbuf[BUFSIZE];
int status;
int add_modes = 0;
int rem_modes = 0;
ChannelInfo *ci;
if (!c || !(ci = c->ci))
return;
if ((ci->flags & CI_VERBOTEN) || (*(c->name) == '+'))
return;
status = chan_get_user_status(c, user);
if (debug)
alog("debug: Setting correct user modes for %s on %s (current status: %d, %sgiving modes)", user->nick, c->name, status, (give_modes ? "" : "not "));
/* Changed the second line of this if a bit, to make sure unregistered
* users can always get modes (IE: they always have autoop enabled). Before
* this change, you were required to have a registered nick to be able
* to receive modes. I wonder who added that... *looks at Rob* ;) -GD
*/
if (give_modes && (get_ignore(user->nick) == NULL)
&& (!user->na || !(user->na->nc->flags & NI_AUTOOP))) {
if (ircd->owner && is_founder(user, ci))
add_modes |= CUS_OWNER;
else if ((ircd->protect || ircd->admin)
&& check_access(user, ci, CA_AUTOPROTECT))
add_modes |= CUS_PROTECT;
if (check_access(user, ci, CA_AUTOOP))
add_modes |= CUS_OP;
else if (ircd->halfop && check_access(user, ci, CA_AUTOHALFOP))
add_modes |= CUS_HALFOP;
else if (check_access(user, ci, CA_AUTOVOICE))
add_modes |= CUS_VOICE;
}
/* We check if every mode they have is legally acquired here, and remove
* the modes that they're not allowed to have. But only if SECUREOPS is
* on, because else every mode is legal. -GD
* Unless the channel has just been created. -heinz
* Or the user matches CA_AUTODEOP... -GD
*/
if (((ci->flags & CI_SECUREOPS) || (c->usercount == 1 && is_sync(user->server))
|| check_access(user, ci, CA_AUTODEOP))
&& !is_ulined(user->server->name)) {
if (ircd->owner && (status & CUS_OWNER) && !is_founder(user, ci))
rem_modes |= CUS_OWNER;
if ((ircd->protect || ircd->admin) && (status & CUS_PROTECT)
&& !check_access(user, ci, CA_AUTOPROTECT)
&& !check_access(user, ci, CA_PROTECTME))
rem_modes |= CUS_PROTECT;
if ((status & CUS_OP) && !check_access(user, ci, CA_AUTOOP)
&& !check_access(user, ci, CA_OPDEOPME))
rem_modes |= CUS_OP;
if (ircd->halfop && (status & CUS_HALFOP)
&& !check_access(user, ci, CA_AUTOHALFOP)
&& !check_access(user, ci, CA_HALFOPME))
rem_modes |= CUS_HALFOP;
}
/* No modes to add or remove, exit function -GD */
if (!add_modes && !rem_modes)
return;
/* No need for strn* functions for modebuf, as every possible string
* will always fit in. -GD
*/
strcpy(modebuf, "");
strcpy(userbuf, "");
if (add_modes > 0) {
strcat(modebuf, "+");
if ((add_modes & CUS_OWNER) && !(status & CUS_OWNER)) {
tmp = stripModePrefix(ircd->ownerset);
strcat(modebuf, tmp);
free(tmp);
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
} else {
add_modes &= ~CUS_OWNER;
}
if ((add_modes & CUS_PROTECT) && !(status & CUS_PROTECT)) {
tmp = stripModePrefix(ircd->adminset);
strcat(modebuf, tmp);
free(tmp);
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
} else {
add_modes &= ~CUS_PROTECT;
}
if ((add_modes & CUS_OP) && !(status & CUS_OP)) {
strcat(modebuf, "o");
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
rem_modes |= CUS_DEOPPED;
} else {
add_modes &= ~CUS_OP;
}
if ((add_modes & CUS_HALFOP) && !(status & CUS_HALFOP)) {
strcat(modebuf, "h");
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
/* Halfops are ops too, having a halfop with CUS_DEOPPED is not good - Adam */
rem_modes |= CUS_DEOPPED;
} else {
add_modes &= ~CUS_HALFOP;
}
if ((add_modes & CUS_VOICE) && !(status & CUS_VOICE)) {
strcat(modebuf, "v");
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
} else {
add_modes &= ~CUS_VOICE;
}
}
if (rem_modes > 0) {
strcat(modebuf, "-");
if (rem_modes & CUS_OWNER) {
tmp = stripModePrefix(ircd->ownerset);
strcat(modebuf, tmp);
free(tmp);
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
}
if (rem_modes & CUS_PROTECT) {
tmp = stripModePrefix(ircd->adminset);
strcat(modebuf, tmp);
free(tmp);
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
}
if (rem_modes & CUS_OP) {
strcat(modebuf, "o");
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
/* Do not mark a user as deopped if they are halfopd - Adam */
if (!(add_modes & CUS_HALFOP) && !(status & CUS_HALFOP))
add_modes |= CUS_DEOPPED;
}
if (rem_modes & CUS_HALFOP) {
strcat(modebuf, "h");
strcat(userbuf, " ");
strcat(userbuf, GET_USER(user));
/* Do not mark a user as deopped if they are opped - Adam */
if (!(add_modes & CUS_OP) && !(status & CUS_OP))
add_modes |= CUS_DEOPPED;
}
}
/* Here, both can be empty again due to the "isn't it set already?"
* checks above. -GD
*/
if (!add_modes && !rem_modes)
return;
anope_cmd_mode(whosends(ci), c->name, "%s%s", modebuf, userbuf);
if (add_modes > 0)
chan_set_user_status(c, user, add_modes);
if (rem_modes > 0)
chan_remove_user_status(c, user, rem_modes);
}
/*************************************************************************/
/* Add/remove a user to/from a channel, creating or deleting the channel as
* necessary. If creating the channel, restore mode lock and topic as
* necessary. Also check for auto-opping and auto-voicing.
*/
void chan_adduser2(User * user, Channel * c)
{
struct c_userlist *u;
u = scalloc(sizeof(struct c_userlist), 1);
u->next = c->users;
if (c->users)
c->users->prev = u;
c->users = u;
u->user = user;
c->usercount++;
if (get_ignore(user->nick) == NULL) {
if (c->ci && (check_access(user, c->ci, CA_MEMO))
&& (c->ci->memos.memocount > 0)) {
if (c->ci->memos.memocount == 1) {
notice_lang(s_MemoServ, user, MEMO_X_ONE_NOTICE,
c->ci->memos.memocount, c->ci->name);
} else {
notice_lang(s_MemoServ, user, MEMO_X_MANY_NOTICE,
c->ci->memos.memocount, c->ci->name);
}
}
/* Added channelname to entrymsg - 30.03.2004, Certus */
/* Also, don't send the entrymsg when bursting -GD */
if (c->ci && c->ci->entry_message && is_sync(user->server))
notice_user(whosends(c->ci), user, "[%s] %s", c->name,
c->ci->entry_message);
}
/**
* We let the bot join even if it was an ignored user, as if we don't,
* and the ignored user dosnt just leave, the bot will never
* make it into the channel, leaving the channel botless even for
* legit users - Rob
**/
if (s_BotServ && c->ci && c->ci->bi) {
if (c->usercount == BSMinUsers)
bot_join(c->ci);
if (c->usercount >= BSMinUsers && (c->ci->botflags & BS_GREET)
&& user->na && user->na->nc->greet
&& check_access(user, c->ci, CA_GREET)) {
/* Only display the greet if the main uplink we're connected
* to has synced, or we'll get greet-floods when the net
* recovers from a netsplit. -GD
*/
if (is_sync(user->server)) {
anope_cmd_privmsg(c->ci->bi->nick, c->name, "[%s] %s",
user->na->nick, user->na->nc->greet);
c->ci->bi->lastmsg = time(NULL);
}
}
}
}
/*************************************************************************/
/* This creates the channel structure (was originally in
chan_adduser, but splitted to make it more efficient to use for
SJOINs). */
Channel *chan_create(char *chan, time_t ts)
{
Channel *c;
Channel **list;
if (debug)
alog("debug: Creating channel %s", chan);
/* Allocate pre-cleared memory */
c = scalloc(sizeof(Channel), 1);
strscpy(c->name, chan, sizeof(c->name));
list = &chanlist[HASH(c->name)];
c->next = *list;
if (*list)
(*list)->prev = c;
*list = c;
c->creation_time = ts;
/* Store ChannelInfo pointer in channel record */
c->ci = cs_findchan(chan);
if (c->ci)
c->ci->c = c;
/* Restore locked modes and saved topic */
if (c->ci) {
check_modes(c);
stick_all(c->ci);
}
if (serv_uplink && is_sync(serv_uplink) && (!(c->topic_sync))) {
restore_topic(chan);
}
return c;
}
/*************************************************************************/
/* This destroys the channel structure, freeing everything in it. */
void chan_delete(Channel * c)
{
BanData *bd, *next;
if (debug)
alog("debug: Deleting channel %s", c->name);
for (bd = c->bd; bd; bd = next) {
if (bd->mask)
free(bd->mask);
next = bd->next;
free(bd);
}
if (c->ci)
c->ci->c = NULL;
if (c->topic)
free(c->topic);
if (c->key)
free(c->key);
if (ircd->fmode) {
if (c->flood)
free(c->flood);
}
if (ircd->Lmode) {
if (c->redirect)
free(c->redirect);
}
if (ircd->jmode) {
if (c->throttle)
free (c->throttle);
}
if (c->bans && c->bans->count) {
while (c->bans->entries) {
entry_delete(c->bans, c->bans->entries);
}
free(c->bans);
}
if (ircd->except) {
if (c->excepts && c->excepts->count) {
while (c->excepts->entries) {
entry_delete(c->excepts, c->excepts->entries);
}
free(c->excepts);
}
}
if (ircd->invitemode) {
if (c->invites && c->invites->count) {
while (c->invites->entries) {
entry_delete(c->invites, c->invites->entries);
}
free(c->invites);
}
}
if (c->next)
c->next->prev = c->prev;
if (c->prev)
c->prev->next = c->next;
else
chanlist[HASH(c->name)] = c->next;
free(c);
}
/*************************************************************************/
void del_ban(Channel * chan, char *mask)
{
AutoKick *akick;
Entry *ban;
/* Sanity check as it seems some IRCD will just send -b without a mask */
if (!mask || !chan->bans || (chan->bans->count == 0))
return;
ban = elist_find_mask(chan->bans, mask);
if (ban) {
entry_delete(chan->bans, ban);
if (chan->bans->count == 0) {
free(chan->bans);
chan->bans = NULL;
}
if (debug)
alog("debug: Deleted ban %s from channel %s", mask,
chan->name);
}
if (chan->ci && (akick = is_stuck(chan->ci, mask)))
stick_mask(chan->ci, akick);
}
/*************************************************************************/
void del_exception(Channel * chan, char *mask)
{
Entry *exception;
/* Sanity check as it seems some IRCD will just send -e without a mask */
if (!mask || !chan->excepts || (chan->excepts->count == 0))
return;
exception = elist_find_mask(chan->excepts, mask);
if (exception) {
entry_delete(chan->excepts, exception);
if (chan->excepts->count == 0) {
free(chan->excepts);
chan->excepts = NULL;
}
if (debug)
alog("debug: Deleted except %s to channel %s", mask,
chan->name);
}
}
/*************************************************************************/
void del_invite(Channel * chan, char *mask)
{
Entry *invite;
/* Sanity check as it seems some IRCD will just send -I without a mask */
if (!mask || !chan->invites || (chan->invites->count == 0)) {
return;
}
invite = elist_find_mask(chan->invites, mask);
if (invite) {
entry_delete(chan->invites, invite);
if(chan->invites->count == 0) {
free(chan->invites);
chan->invites = NULL;
}
if (debug)
alog("debug: Deleted invite %s to channel %s", mask,
chan->name);
}
}
/*************************************************************************/
char *get_flood(Channel * chan)
{
return chan->flood;
}
/*************************************************************************/
char *get_throttle(Channel * chan)
{
return chan->throttle;
}
/*************************************************************************/
char *get_key(Channel * chan)
{
return chan->key;
}
/*************************************************************************/
char *get_limit(Channel * chan)
{
static char limit[16];
if (chan->limit == 0)
return NULL;
snprintf(limit, sizeof(limit), "%lu", (unsigned long int) chan->limit);
return limit;
}
/*************************************************************************/
char *get_redirect(Channel * chan)
{
return chan->redirect;
}
/*************************************************************************/
/* This is a dummy function part of making anope accept modes
* it does actively parse.. ~ Viper */
char *get_unkwn(Channel * chan)
{
return NULL;
}
/*************************************************************************/
Channel *join_user_update(User * user, Channel * chan, char *name,
time_t chants)
{
struct u_chanlist *c;
/* If it's a new channel, so we need to create it first. */
if (!chan)
chan = chan_create(name, chants);
else
{
/* Check chants against 0, as not every ircd sends JOIN with a TS. */
if (chan->creation_time > chants && chants != 0)
{
struct c_userlist *cu;
char *modes[6];
chan->creation_time = chants;
for (cu = chan->users; cu; cu = cu->next)
{
/* XXX */
modes[0] = "-ov";
modes[1] = cu->user->nick;
modes[2] = cu->user->nick;
chan_set_modes(s_OperServ, chan, 3, modes, 2);
}
if (chan->ci)
{
if (chan->ci->bi)
{
/* This is ugly, but it always works */
anope_cmd_part(chan->ci->bi->nick, chan->name, "TS reop");
bot_join(chan->ci);
}
/* Make sure +r is set */
if (ircd->chanreg && ircd->regmode)
{
chan->mode |= ircd->regmode;
anope_cmd_mode(whosends(chan->ci), chan->name, "+r");
}
}
/* XXX simple modes and bans */
}
}
if (debug)
alog("debug: %s joins %s", user->nick, chan->name);
c = scalloc(sizeof(*c), 1);
c->next = user->chans;
if (user->chans)
user->chans->prev = c;
user->chans = c;
c->chan = chan;
chan_adduser2(user, chan);
return chan;
}
/*************************************************************************/
void set_flood(Channel * chan, char *value)
{
if (chan->flood)
free(chan->flood);
chan->flood = value ? sstrdup(value) : NULL;
if (debug)
alog("debug: Flood mode for channel %s set to %s", chan->name,
chan->flood ? chan->flood : "no flood settings");
}
/*************************************************************************/
void chan_set_throttle(Channel * chan, char *value)
{
if (chan->throttle)
free(chan->throttle);
chan->throttle = value ? sstrdup(value) : NULL;
if (debug)
alog("debug: Throttle mode for channel %s set to %s", chan->name,
chan->throttle ? chan->throttle : "none");
}
/*************************************************************************/
void chan_set_key(Channel * chan, char *value)
{
if (chan->key)
free(chan->key);
chan->key = value ? sstrdup(value) : NULL;
if (debug)
alog("debug: Key of channel %s set to %s", chan->name,
chan->key ? chan->key : "no key");
}
/*************************************************************************/
void set_limit(Channel * chan, char *value)
{
chan->limit = value ? strtoul(value, NULL, 10) : 0;
if (debug)
alog("debug: Limit of channel %s set to %u", chan->name,
chan->limit);
}
/*************************************************************************/
void set_redirect(Channel * chan, char *value)
{
if (chan->redirect)
free(chan->redirect);
chan->redirect = value ? sstrdup(value) : NULL;
if (debug)
alog("debug: Redirect of channel %s set to %s", chan->name,
chan->redirect ? chan->redirect : "no redirect");
}
/*************************************************************************/
/* This is a dummy function to make anope parse a param for a mode,
* yet we don't use that param internally.. ~ Viper */
void set_unkwn(Channel *chan, char *value)
{
/* Do nothing.. */
}
/*************************************************************************/
void do_mass_mode(char *modes)
{
int ac;
char **av;
Channel *c;
char *myModes;
if (!modes) {
return;
}
/* Prevent modes being altered by split_buf */
myModes = sstrdup(modes);
ac = split_buf(myModes, &av, 1);
for (c = firstchan(); c; c = nextchan()) {
if (c->bouncy_modes) {
free(av);
free(myModes);
return;
} else {
anope_cmd_mode(s_OperServ, c->name, "%s", modes);
chan_set_modes(s_OperServ, c, ac, av, 1);
}
}
free(av);
free(myModes);
}
/*************************************************************************/
void restore_unsynced_topics(void)
{
Channel *c;
for (c = firstchan(); c; c = nextchan()) {
if (!(c->topic_sync))
restore_topic(c->name);
}
}
/*************************************************************************/
/**
* This handles creating a new Entry.
* This function destroys and free's the given mask as a side effect.
* @param mask Host/IP/CIDR mask to convert to an entry
* @return Entry struct for the given mask, NULL if creation failed
*/
Entry *entry_create(char *mask)
{
Entry *entry;
char *nick = NULL, *user, *host, *cidrhost;
uint32 ip, cidr;
entry = scalloc(1, sizeof(Entry));
entry->type = ENTRYTYPE_NONE;
entry->prev = NULL;
entry->next = NULL;
entry->nick = NULL;
entry->user = NULL;
entry->host = NULL;
entry->mask = sstrdup(mask);
host = strchr(mask, '@');
if (host) {
*host++ = '\0';
/* If the user is purely a wildcard, ignore it */
if (str_is_pure_wildcard(mask))
user = NULL;
else {
/* There might be a nick too */
user = strchr(mask, '!');
if (user) {
*user++ = '\0';
/* If the nick is purely a wildcard, ignore it */
if (str_is_pure_wildcard(mask))
nick = NULL;
else
nick = mask;
} else {
nick = NULL;
user = mask;
}
}
} else {
/* It is possibly an extended ban/invite mask, but we do
* not support these at this point.. ~ Viper */
/* If there's no user in the mask, assume a pure wildcard */
user = NULL;
host = mask;
}
if (nick) {
entry->nick = sstrdup(nick);
/* Check if we have a wildcard user */
if (str_is_wildcard(nick))
entry->type |= ENTRYTYPE_NICK_WILD;
else
entry->type |= ENTRYTYPE_NICK;
}
if (user) {
entry->user = sstrdup(user);
/* Check if we have a wildcard user */
if (str_is_wildcard(user))
entry->type |= ENTRYTYPE_USER_WILD;
else
entry->type |= ENTRYTYPE_USER;
}
/* Only check the host if it's not a pure wildcard */
if (*host && !str_is_pure_wildcard(host)) {
if (ircd->cidrchanbei && str_is_cidr(host, &ip, &cidr, &cidrhost)) {
entry->cidr_ip = ip;
entry->cidr_mask = cidr;
entry->type |= ENTRYTYPE_CIDR4;
host = cidrhost;
} else if (ircd->cidrchanbei && strchr(host, '/')) {
/* Most IRCd's don't enforce sane bans therefore it is not
* so unlikely we will encounter this.
* Currently we only support strict CIDR without taking into
* account quirks of every single ircd (nef) that ignore everything
* after the first /cidr. To add this, sanitaze before sending to
* str_is_cidr() as this expects a standard cidr.
* Add it to the internal list (so it is included in for example clear)
* but do not use if during matching.. ~ Viper */
entry->type = ENTRYTYPE_NONE;
} else {
entry->host = sstrdup(host);
if (str_is_wildcard(host))
entry->type |= ENTRYTYPE_HOST_WILD;
else
entry->type |= ENTRYTYPE_HOST;
}
}
free(mask);
return entry;
}
/**
* Create an entry and add it at the beginning of given list.
* @param list The List the mask should be added to
* @param mask The mask to parse and add to the list
* @return Pointer to newly added entry. NULL if it fails.
*/
Entry *entry_add(EList * list, char *mask)
{
Entry *e;
char *hostmask;
hostmask = sstrdup(mask);
e = entry_create(hostmask);
if (!e)
return NULL;
e->next = list->entries;
e->prev = NULL;
if (list->entries)
list->entries->prev = e;
list->entries = e;
list->count++;
return e;
}
/**
* Delete the given entry from a given list.
* @param list Linked list from which entry needs to be removed.
* @param e The entry to be deleted, must be member of list.
*/
void entry_delete(EList * list, Entry * e)
{
if (!list || !e)
return;
if (e->next)
e->next->prev = e->prev;
if (e->prev)
e->prev->next = e->next;
if (list->entries == e)
list->entries = e->next;
if (e->nick)
free(e->nick);
if (e->user)
free(e->user);
if (e->host)
free(e->host);
free(e->mask);
free(e);
list->count--;
}
/**
* Create and initialize a new entrylist
* @return Pointer to the created EList object
**/
EList *list_create()
{
EList *list;
list = scalloc(1, sizeof(EList));
list->entries = NULL;
list->count = 0;
return list;
}
/**
* Match the given Entry to the given user/host and optional IP addy
* @param e Entry struct to match against
* @param nick Nick to match against
* @param user User to match against
* @param host Host to match against
* @param ip IP to match against, set to 0 to not match this
* @return 1 for a match, 0 for no match
*/
int entry_match(Entry * e, char *nick, char *user, char *host, uint32 ip)
{
/* If we don't get an entry, or it s an invalid one, no match ~ Viper */
if (!e || e->type == ENTRYTYPE_NONE)
return 0;
if (ircd->cidrchanbei && (e->type & ENTRYTYPE_CIDR4) &&
(!ip || (ip && ((ip & e->cidr_mask) != e->cidr_ip))))
return 0;
if ((e->type & ENTRYTYPE_NICK)
&& (!nick || stricmp(e->nick, nick) != 0))
return 0;
if ((e->type & ENTRYTYPE_USER)
&& (!user || stricmp(e->user, user) != 0))
return 0;
if ((e->type & ENTRYTYPE_HOST)
&& (!host || stricmp(e->host, host) != 0))
return 0;
if ((e->type & ENTRYTYPE_NICK_WILD)
&& !match_wild_nocase(e->nick, nick))
return 0;
if ((e->type & ENTRYTYPE_USER_WILD)
&& !match_wild_nocase(e->user, user))
return 0;
if ((e->type & ENTRYTYPE_HOST_WILD)
&& !match_wild_nocase(e->host, host))
return 0;
return 1;
}
/**
* Match the given Entry to the given hostmask and optional IP addy.
* @param e Entry struct to match against
* @param mask Hostmask to match against
* @param ip IP to match against, set to 0 to not match this
* @return 1 for a match, 0 for no match
*/
int entry_match_mask(Entry * e, char *mask, uint32 ip)
{
char *hostmask, *nick, *user, *host;
int res;
hostmask = sstrdup(mask);
host = strchr(hostmask, '@');
if (host) {
*host++ = '\0';
user = strchr(hostmask, '!');
if (user) {
*user++ = '\0';
nick = hostmask;
} else {
nick = NULL;
user = hostmask;
}
} else {
nick = NULL;
user = NULL;
host = hostmask;
}
res = entry_match(e, nick, user, host, ip);
/* Free the destroyed mask. */
free(hostmask);
return res;
}
/**
* Match a nick, user, host, and ip to a list entry
* @param e List that should be matched against
* @param nick The nick to match
* @param user The user to match
* @param host The host to match
* @param ip The ip to match
* @return Returns the first matching entry, if none, NULL is returned.
*/
Entry *elist_match(EList * list, char *nick, char *user, char *host,
uint32 ip)
{
Entry *e;
if (!list || !list->entries)
return NULL;
for (e = list->entries; e; e = e->next) {
if (entry_match(e, nick, user, host, ip))
return e;
}
/* We matched none */
return NULL;
}
/**
* Match a mask and ip to a list.
* @param list EntryList that should be matched against
* @param mask The nick!user@host mask to match
* @param ip The ip to match
* @return Returns the first matching entry, if none, NULL is returned.
*/
Entry *elist_match_mask(EList * list, char *mask, uint32 ip)
{
char *hostmask, *nick, *user, *host;
Entry *res;
if (!list || !list->entries || !mask)
return NULL;
hostmask = sstrdup(mask);
host = strchr(hostmask, '@');
if (host) {
*host++ = '\0';
user = strchr(hostmask, '!');
if (user) {
*user++ = '\0';
nick = hostmask;
} else {
nick = NULL;
user = hostmask;
}
} else {
nick = NULL;
user = NULL;
host = hostmask;
}
res = elist_match(list, nick, user, host, ip);
/* Free the destroyed mask. */
free(hostmask);
return res;
}
/**
* Check if a user matches an entry on a list.
* @param list EntryList that should be matched against
* @param user The user to match against the entries
* @param full true to match against real host and real IP
* @return Returns the first matching entry, if none, NULL is returned.
*/
static Entry *_elist_match_user(EList * list, User * u, boolean full)
{
Entry *res = NULL;
char *host;
uint32 ip = 0;
if (!list || !list->entries || !u)
return NULL;
if (u->hostip == NULL) {
host = host_resolve(u->host);
/* we store the just resolved hostname so we don't
* need to do this again */
if (host) {
u->hostip = sstrdup(host);
}
} else {
host = sstrdup(u->hostip);
}
/* Convert the host to an IP.. */
if (host)
ip = str_is_ip(host);
/* Match what we ve got against the lists.. */
if (full)
res = elist_match(list, u->nick, u->username, u->host, ip);
if (!res)
res = elist_match(list, u->nick, u->vident, u->vhost, 0);
if (!res)
res = elist_match(list, u->nick, u->username, u->chost, 0);
if (host)
free(host);
return res;
}
Entry *elist_match_user(EList *list, User *u)
{
return _elist_match_user(list, u, false);
}
Entry *elist_match_user_full(EList *list, User *u, boolean full)
{
return _elist_match_user(list, u, full);
}
/**
* Find a entry identical to the given mask..
* @param list EntryList that should be matched against
* @param mask The *!*@* mask to match
* @return Returns the first matching entry, if none, NULL is returned.
*/
Entry *elist_find_mask(EList * list, char *mask)
{
Entry *e;
if (!list || !list->entries || !mask)
return NULL;
for (e = list->entries; e; e = e->next) {
if (!stricmp(e->mask, mask))
return e;
}
return NULL;
}
/**
* Gets the total memory use of an entrylit.
* @param list The list we should estimate the mem use of.
* @return Returns the memory useage of the given list.
*/
long get_memuse(EList * list)
{
Entry *e;
long mem = 0;
if (!list)
return 0;
mem += sizeof(EList *);
mem += sizeof(Entry *) * list->count;
if (list->entries) {
for (e = list->entries; e; e = e->next) {
if (e->nick)
mem += strlen(e->nick) + 1;
if (e->user)
mem += strlen(e->user) + 1;
if (e->host)
mem += strlen(e->host) + 1;
if (e->mask)
mem += strlen(e->mask) + 1;
}
}
return mem;
}
/*************************************************************************/
|