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 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382
|
/*
* IRC - Internet Relay Chat, ircd/channel.c
* Copyright (C) 1990 Jarkko Oikarinen and
* University of Oulu, Co Center
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef __GNUC__
#pragma implementation
#endif
#include "sys.h"
#include <stdlib.h>
#include "h.h"
#include "struct.h"
#include "channel.h"
#include "parse.h"
#include "whowas.h"
#include "send.h"
#include "s_err.h"
#include "numeric.h"
#include "ircd.h"
#include "match.h"
#include "list.h"
#include "hash.h"
#include "s_misc.h"
#include "s_user.h"
#include "s_conf.h"
#include "s_bsd.h"
#include "msg.h"
#include "common.h"
#include "s_serv.h"
#include "channel.h"
#include "support.h"
#include "numnicks.h"
#include "sprintf_irc.h"
RCSTAG_CC("$Id: channel.c,v 1.59 1997/11/22 20:07:56 carlo Exp $")
#ifdef EPATH
#define m_names n_names
#define m_list n_list
#define m_join n_join
#define m_mode n_mode
#endif
aChannel *channel = NullChn;
static void sendmodeto_one(aClient *cptr, char *from, char *name,
char *mode, char *param, time_t creationtime);
static void add_invite(aClient *, aChannel *);
static int add_banid(aClient *, aChannel *, char *, int, int);
static Link *next_overlapped_ban(void);
static Link *next_removed_overlapped_ban(void);
static int can_join(aClient *, aChannel *, char *);
static void channel_modes(aClient *, char *, char *, aChannel *);
static int del_banid(aClient *, aChannel *, char *, int);
static int is_banned(aClient *, aChannel *, Link *);
static int number_of_zombies(aChannel *);
static int is_deopped(aClient *, aChannel *);
static int set_mode(aClient *, aClient *, aChannel *, int,
char **, char *, char *, char *, int *);
static void sub1_from_channel(aChannel *);
static void send_hack_notice(aClient *, aClient *, int, char *[], int, int);
void clean_channelname(char *);
void del_invite(aClient *, aChannel *);
static char *PartFmt = ":%s PART %s";
/*
* some buffers for rebuilding channel/nick lists with ,'s
*/
static char nickbuf[BUFSIZE], buf[BUFSIZE];
static char modebuf[MODEBUFLEN], parabuf[MODEBUFLEN];
static char nparabuf[MODEBUFLEN];
/*
* Maximum acceptable lag time in seconds: A channel younger than
* this is not protected against hacking admins.
* Mainly here to check if the TS clocks really sync (otherwise this
* will start causing HACK notices.
* This value must be the same on all servers.
*/
#define TS_LAG_TIME ((time_t)1200)
/*
* A Magic TS that is used for channels that are created by JOIN,
* a channel with this TS accepts all TS without complaining that
* it might receive later via MODE or CREATE.
*/
#define MAGIC_REMOTE_JOIN_TS 1270080000
/*
* return the length (>=0) of a chain of links.
*/
static int list_length(Link *lp)
{
Reg2 int count = 0;
for (; lp; lp = lp->next)
count++;
return count;
}
/*
* find_chasing
*
* Find the client structure for a nick name (user) using history
* mechanism if necessary. If the client is not found, an error
* message (NO SUCH NICK) is generated. If the client was found
* through the history, chasing will be 1 and otherwise 0.
*/
static aClient *find_chasing(aClient *sptr, char *user, int *chasing)
{
Reg2 aClient *who = find_client(user, (aClient *)NULL);
if (chasing)
*chasing = 0;
if (who)
return who;
if (!(who = get_history(user, (long)KILLCHASETIMELIMIT)))
{
sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, sptr->name, user);
return NULL;
}
if (chasing)
*chasing = 1;
return who;
}
/*
* Fixes a string so that the first white space found becomes an end of
* string marker (`\-`). returns the 'fixed' string or "*" if the string
* was NULL length or a NULL pointer.
*/
static char *check_string(char *s)
{
static char star[2] = "*";
char *str = s;
if (BadPtr(s))
return star;
for (; *s; s++)
if (isspace(*s))
{
*s = '\0';
break;
}
return (BadPtr(str)) ? star : str;
}
/*
* Create a string of form "foo!bar@fubar" given foo, bar and fubar
* as the parameters. If NULL, they become "*".
*/
static char *make_nick_user_host(char *nick, char *name, char *host)
{
static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
register char *s = namebuf;
register size_t len;
nick = check_string(nick);
len = strlen(nick);
if (len > NICKLEN)
len = NICKLEN;
strncpy(namebuf, nick, len);
s += len;
*s++ = '!';
name = check_string(name);
len = strlen(name);
if (len > USERLEN)
len = USERLEN;
strncpy(s, name, len);
s += len;
*s++ = '@';
host = check_string(host);
len = strlen(host);
if (len > HOSTLEN)
len = HOSTLEN;
strncpy(s, host, len);
s += len;
*s = '\0';
return (namebuf);
}
/*
* Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
* IP-number as the parameters. If NULL, they become "*".
*/
static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
{
static char ipbuf[NICKLEN + USERLEN + 16 + 3];
register char *s = ipbuf;
register size_t len;
nick = check_string(nick);
len = strlen(nick);
if (len > NICKLEN)
len = NICKLEN;
strncpy(ipbuf, nick, len);
s += len;
*s++ = '!';
name = check_string(name);
len = strlen(name);
if (len > USERLEN)
len = USERLEN;
strncpy(s, name, len);
s += len;
*s++ = '@';
strcpy(s, inetntoa(ip));
return (ipbuf);
}
/*
* add_banid
*
* `cptr' must be the client adding the ban.
*
* If `change' is true then add `banid' to channel `chptr'.
* Returns 0 if the ban was added.
* Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
* Return -1 otherwise.
*
* Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
* when `change' is false, otherwise they will be removed from the banlist.
* Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
* respectively will return these bans until NULL is returned.
*
* If `firsttime' is true, the ban list as returned by next_overlapped_ban()
* is reset (unless a non-zero value is returned, in which case the
* CHFL_BAN_OVERLAPPED flag might not have been reset!).
*
* --Run
*/
static Link *next_ban, *prev_ban, *removed_bans_list;
static int add_banid(aClient *cptr, aChannel *chptr, char *banid,
int change, int firsttime)
{
Reg1 Link *ban, **banp;
Reg2 int cnt = 0, removed_bans = 0, len = strlen(banid);
if (firsttime)
{
next_ban = NULL;
if (prev_ban || removed_bans_list)
MyCoreDump; /* Memory leak */
}
if (MyClient(cptr))
collapse(banid);
for (banp = &chptr->banlist; *banp;)
{
len += strlen((*banp)->value.ban.banstr);
++cnt;
if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
{
if (!strcmp((*banp)->value.ban.banstr, banid))
{
(*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
return -2;
}
}
else if (!mmatch((*banp)->value.ban.banstr, banid))
return -1;
if (!mmatch(banid, (*banp)->value.ban.banstr))
{
Link *tmp = *banp;
if (change)
{
if (MyClient(cptr))
{
cnt--;
len -= strlen(tmp->value.ban.banstr);
}
*banp = tmp->next;
#if 0
/* Silently remove overlapping bans */
RunFree(tmp->value.ban.banstr);
RunFree(tmp->value.ban.who);
free_link(tmp);
#else
/* These will be sent to the user later as -b */
tmp->next = removed_bans_list;
removed_bans_list = tmp;
removed_bans = 1;
#endif
}
else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
{
tmp->flags |= CHFL_BAN_OVERLAPPED;
if (!next_ban)
next_ban = tmp;
banp = &tmp->next;
}
else
banp = &tmp->next;
}
else
{
if (firsttime)
(*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
banp = &(*banp)->next;
}
}
if (MyClient(cptr) && !removed_bans &&
(len > MAXBANLENGTH || (cnt >= MAXBANS)))
{
sendto_one(cptr, err_str(ERR_BANLISTFULL), me.name, cptr->name,
chptr->chname, banid);
return -1;
}
if (change)
{
char *ip_start;
ban = make_link();
ban->next = chptr->banlist;
ban->value.ban.banstr = (char *)RunMalloc(strlen(banid) + 1);
strcpy(ban->value.ban.banstr, banid);
ban->value.ban.who = (char *)RunMalloc(strlen(cptr->name) + 1);
strcpy(ban->value.ban.who, cptr->name);
ban->value.ban.when = now;
ban->flags = CHFL_BAN; /* This bit is never used I think... */
if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
ban->flags |= CHFL_BAN_IPMASK;
chptr->banlist = ban;
/* Erase ban-valid-bit */
for (ban = chptr->members; ban; ban = ban->next)
ban->flags &= ~CHFL_BANVALID; /* `ban' == channel member ! */
}
return 0;
}
static Link *next_overlapped_ban(void)
{
Reg1 Link *tmp = next_ban;
if (tmp)
{
Reg2 Link *ban;
for (ban = tmp->next; ban; ban = ban->next)
if ((ban->flags & CHFL_BAN_OVERLAPPED))
break;
next_ban = ban;
}
return tmp;
}
static Link *next_removed_overlapped_ban(void)
{
Reg1 Link *tmp = removed_bans_list;
if (prev_ban)
{
if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
RunFree(prev_ban->value.ban.banstr);
RunFree(prev_ban->value.ban.who);
free_link(prev_ban);
}
if (tmp)
removed_bans_list = removed_bans_list->next;
prev_ban = tmp;
return tmp;
}
/*
* del_banid
*
* If `change' is true, delete `banid' from channel `chptr'.
* Returns `false' if removal was (or would have been) successful.
*/
static int del_banid(aClient *sptr, aChannel *chptr, char *banid, int change)
{
Reg1 Link **ban;
Reg2 Link *tmp;
if (!banid)
return -1;
for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next))
if (mycmp(banid, (*ban)->value.ban.banstr) == 0)
{
tmp = *ban;
if (change)
{
*ban = tmp->next;
RunFree(tmp->value.ban.banstr);
RunFree(tmp->value.ban.who);
free_link(tmp);
/* Erase ban-valid-bit, for channel members that are banned */
for (tmp = chptr->members; tmp; tmp = tmp->next)
if ((tmp->flags & (CHFL_BANNED|CHFL_BANVALID)) ==
(CHFL_BANNED|CHFL_BANVALID))
tmp->flags &= ~CHFL_BANVALID; /* `tmp' == channel member */
}
return 0;
}
return -1;
}
/*
* IsMember - returns Link * if a person is joined and not a zombie
*/
Link *IsMember(aClient *cptr, aChannel *chptr)
{
Link *lp;
return (((lp = find_user_link(chptr->members, cptr)) &&
!(lp->flags & CHFL_ZOMBIE)) ? lp : NULL);
}
/*
* is_banned - a non-zero value if banned else 0.
*/
static int is_banned(aClient *cptr, aChannel *chptr, Link *member)
{
Reg1 Link *tmp;
char *s, *ip_s = NULL;
if (!IsPerson(cptr))
return 0;
if (member)
{
if ((member->flags & CHFL_BANVALID))
return (member->flags & CHFL_BANNED);
}
s = make_nick_user_host(cptr->name, cptr->user->username, cptr->user->host);
for (tmp = chptr->banlist; tmp; tmp = tmp->next)
{
if ((tmp->flags & CHFL_BAN_IPMASK))
{
if (!ip_s)
ip_s = make_nick_user_ip(cptr->name, cptr->user->username, cptr->ip);
if (match(tmp->value.ban.banstr, ip_s) == 0)
break;
}
else if (match(tmp->value.ban.banstr, s) == 0)
break;
}
if (member)
{
member->flags |= CHFL_BANVALID;
if (tmp)
{
member->flags |= CHFL_BANNED;
return 1;
}
else
{
member->flags &= ~CHFL_BANNED;
return 0;
}
}
return (tmp != NULL);
}
/*
* adds a user to a channel by adding another link to the channels member
* chain.
*/
static void add_user_to_channel(aChannel *chptr, aClient *who, int flags)
{
Reg1 Link *ptr;
if (who->user)
{
ptr = make_link();
ptr->value.cptr = who;
ptr->flags = flags;
ptr->next = chptr->members;
chptr->members = ptr;
chptr->users++;
ptr = make_link();
ptr->value.chptr = chptr;
ptr->next = who->user->channel;
who->user->channel = ptr;
who->user->joined++;
}
}
void remove_user_from_channel(aClient *sptr, aChannel *chptr)
{
Reg1 Link **curr;
Reg2 Link *tmp;
Reg3 Link *lp = chptr->members;
for (; lp && (lp->flags & CHFL_ZOMBIE || lp->value.cptr == sptr);
lp = lp->next);
for (;;)
{
for (curr = &chptr->members; (tmp = *curr); curr = &tmp->next)
if (tmp->value.cptr == sptr)
{
*curr = tmp->next;
free_link(tmp);
break;
}
for (curr = &sptr->user->channel; (tmp = *curr); curr = &tmp->next)
if (tmp->value.chptr == chptr)
{
*curr = tmp->next;
free_link(tmp);
break;
}
sptr->user->joined--;
if (lp)
break;
if (chptr->members)
sptr = chptr->members->value.cptr;
else
break;
sub1_from_channel(chptr);
}
sub1_from_channel(chptr);
}
int is_chan_op(aClient *cptr, aChannel *chptr)
{
Reg1 Link *lp;
if (chptr)
if ((lp = find_user_link(chptr->members, cptr)) &&
!(lp->flags & CHFL_ZOMBIE))
return (lp->flags & CHFL_CHANOP);
return 0;
}
static int is_deopped(aClient *cptr, aChannel *chptr)
{
Reg1 Link *lp;
if (chptr)
if ((lp = find_user_link(chptr->members, cptr)))
return (lp->flags & CHFL_DEOPPED);
return (IsPerson(cptr) ? 1 : 0);
}
int is_zombie(aClient *cptr, aChannel *chptr)
{
Reg1 Link *lp;
if (chptr)
if ((lp = find_user_link(chptr->members, cptr)))
return (lp->flags & CHFL_ZOMBIE);
return 0;
}
int has_voice(aClient *cptr, aChannel *chptr)
{
Reg1 Link *lp;
if (chptr)
if ((lp = find_user_link(chptr->members, cptr)) &&
!(lp->flags & CHFL_ZOMBIE))
return (lp->flags & CHFL_VOICE);
return 0;
}
int can_send(aClient *cptr, aChannel *chptr)
{
Reg1 Link *lp;
lp = IsMember(cptr, chptr);
if ((!lp || !(lp->flags & (CHFL_CHANOP | CHFL_VOICE)) ||
(lp->flags & CHFL_ZOMBIE)) && MyClient(cptr) &&
is_banned(cptr, chptr, lp))
return (MODE_BAN);
if (chptr->mode.mode & MODE_MODERATED &&
(!lp || !(lp->flags & (CHFL_CHANOP | CHFL_VOICE)) ||
(lp->flags & CHFL_ZOMBIE)))
return (MODE_MODERATED);
if (chptr->mode.mode & MODE_NOPRIVMSGS && !lp)
return (MODE_NOPRIVMSGS);
return 0;
}
aChannel *find_channel(char *chname, aChannel *chptr)
{
return hash_find_channel(chname, chptr);
}
/*
* write the "simple" list of channel modes for channel chptr onto buffer mbuf
* with the parameters in pbuf.
*/
static void channel_modes(aClient *cptr, char *mbuf, char *pbuf,
aChannel *chptr)
{
*mbuf++ = '+';
if (chptr->mode.mode & MODE_SECRET)
*mbuf++ = 's';
else if (chptr->mode.mode & MODE_PRIVATE)
*mbuf++ = 'p';
if (chptr->mode.mode & MODE_MODERATED)
*mbuf++ = 'm';
if (chptr->mode.mode & MODE_TOPICLIMIT)
*mbuf++ = 't';
if (chptr->mode.mode & MODE_INVITEONLY)
*mbuf++ = 'i';
if (chptr->mode.mode & MODE_NOPRIVMSGS)
*mbuf++ = 'n';
if (chptr->mode.limit)
{
*mbuf++ = 'l';
sprintf_irc(pbuf, *chptr->mode.key ? "%d " : "%d", chptr->mode.limit);
}
if (*chptr->mode.key)
{
*mbuf++ = 'k';
if (IsMember(cptr, chptr) || IsServer(cptr))
strcat(pbuf, chptr->mode.key);
}
*mbuf = '\0';
return;
}
static int send_mode_list(aClient *cptr, char *chname, time_t creationtime,
Link *top, int mask, char flag)
{
Reg1 Link *lp;
Reg2 char *cp, *name;
int count = 0, send = 0, sent = 0;
cp = modebuf + strlen(modebuf);
if (*parabuf) /* mode +l or +k xx */
count = 1;
for (lp = top; lp; lp = lp->next)
{
if (!(lp->flags & mask))
continue;
if (mask == CHFL_BAN)
name = lp->value.ban.banstr;
else
name = lp->value.cptr->name;
if (strlen(parabuf) + strlen(name) + 11 < (size_t) MODEBUFLEN)
{
strcat(parabuf, " ");
strcat(parabuf, name);
count++;
*cp++ = flag;
*cp = '\0';
}
else if (*parabuf)
send = 1;
if (count == 6)
send = 1;
if (send)
{
/* cptr is always a server! So we send creationtimes */
sendmodeto_one(cptr, me.name, chname, modebuf, parabuf, creationtime);
sent = 1;
send = 0;
*parabuf = '\0';
cp = modebuf;
*cp++ = '+';
if (count != 6)
{
strcpy(parabuf, name);
*cp++ = flag;
}
count = 0;
*cp = '\0';
}
}
return sent;
}
/*
* send "cptr" a full list of the modes for channel chptr.
*/
void send_channel_modes(aClient *cptr, aChannel *chptr)
{
int sent;
if (IsLocalChannel(chptr->chname))
return;
*modebuf = *parabuf = '\0';
channel_modes(cptr, modebuf, parabuf, chptr);
if (Protocol(cptr) < 10)
{
sent = send_mode_list(cptr, chptr->chname, chptr->creationtime,
chptr->members, CHFL_CHANOP, 'o');
if (!sent && chptr->creationtime)
sendto_one(cptr, ":%s MODE %s %s %s %lu", me.name,
chptr->chname, modebuf, parabuf, chptr->creationtime);
else if (modebuf[1] || *parabuf)
sendmodeto_one(cptr, me.name,
chptr->chname, modebuf, parabuf, chptr->creationtime);
*parabuf = '\0';
*modebuf = '+';
modebuf[1] = '\0';
send_mode_list(cptr, chptr->chname, chptr->creationtime,
chptr->banlist, CHFL_BAN, 'b');
if (modebuf[1] || *parabuf)
sendmodeto_one(cptr, me.name, chptr->chname, modebuf,
parabuf, chptr->creationtime);
*parabuf = '\0';
*modebuf = '+';
modebuf[1] = '\0';
send_mode_list(cptr, chptr->chname, chptr->creationtime,
chptr->members, CHFL_VOICE, 'v');
if (modebuf[1] || *parabuf)
sendmodeto_one(cptr, me.name, chptr->chname, modebuf,
parabuf, chptr->creationtime);
}
else
{
static int current_flags[4] =
{0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP};
int first = 1, full = 1, flag_cnt = 0, new_mode = 0;
size_t len, sblen;
Link *lp1 = chptr->members;
Link *lp2 = chptr->banlist;
for (first = 1; full; first = 0) /* Loop for multiple messages */
{
full = 0; /* Assume by default we get it
all in one message */
/* (Continued) prefix: "<Y> BURST <channel> <TS>" */
sprintf_irc(sendbuf, "%c BURST %s %lu", NumServ(&me),
chptr->chname, chptr->creationtime);
sblen = strlen(sendbuf);
if (first && modebuf[1]) /* Add simple modes (iklmnpst)
if first message */
{
/* prefix: "<Y> BURST <channel> <TS>[ <modes>[ <params>]]" */
sendbuf[sblen++] = ' ';
strcpy(sendbuf + sblen, modebuf);
sblen += strlen(modebuf);
if (*parabuf)
{
sendbuf[sblen++] = ' ';
strcpy(sendbuf + sblen, parabuf);
sblen += strlen(parabuf);
}
}
/* Attach nicks, comma seperated " nick[:modes],nick[:modes],..." */
/* Run 4 times over all members, to group the members with the
* same mode together */
for (first = 1; flag_cnt < 4;
lp1 = chptr->members, new_mode = 1, flag_cnt++)
{
for (; lp1; lp1 = lp1->next)
{
if ((lp1->flags & (CHFL_CHANOP | CHFL_VOICE)) !=
current_flags[flag_cnt])
continue; /* Skip members with different flags */
if (sblen + NUMNICKLEN + 4 > BUFSIZE - 3)
/* The 4 is a possible ",:ov"
* The -3 is for the "\r\n\0" that is added in send.c
*/
{
full = 1; /* Make sure we continue after
sending it so far */
break; /* Do not add this member to this message */
}
sendbuf[sblen++] = first ? ' ' : ',';
first = 0; /* From now on, us comma's to add new nicks */
/*
* This is equivalent to
* sprintf_irc(sendbuf + sblen, "%c%c%c",
* NumNick(lp1->value.cptr)); sblen += NUMNICKLEN;
* but slightly faster:
*/
sendbuf[sblen++] = *lp1->value.cptr->user->server->yxx;
sendbuf[sblen++] = lp1->value.cptr->yxx[0];
sendbuf[sblen++] = lp1->value.cptr->yxx[1];
if (new_mode) /* Do we have a nick with a new mode ? */
{
new_mode = 0;
sendbuf[sblen++] = ':';
if (lp1->flags & CHFL_CHANOP)
sendbuf[sblen++] = 'o';
if (lp1->flags & CHFL_VOICE)
sendbuf[sblen++] = 'v';
}
}
if (full)
break;
}
if (!full)
{
/* Attach all bans, space seperated " :%ban ban ..." */
for (first = 2; lp2; lp2 = lp2->next)
{
len = strlen(lp2->value.ban.banstr);
if (sblen + len + 1 + first > BUFSIZE - 3)
/* The +1 stands for the added ' '.
* The +first stands for the added ":%".
* The -3 is for the "\r\n\0" that is added in send.c
*/
{
full = 1;
break;
}
if (first)
{
first = 0;
sendbuf[sblen++] = ' ';
sendbuf[sblen++] = ':'; /* Will be last parameter */
sendbuf[sblen++] = '%'; /* To tell bans apart */
}
else
sendbuf[sblen++] = ' ';
strcpy(sendbuf + sblen, lp2->value.ban.banstr);
sblen += len;
}
}
sendbuf[sblen] = '\0';
sendbufto_one(cptr); /* Send this message */
} /* Continue when there was something
that didn't fit (full==1) */
}
}
/*
* m_mode
* parv[0] - sender
* parv[1] - channel
*/
int m_mode(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
int badop, sendts;
aChannel *chptr;
/* Now, try to find the channel in question */
if (parc > 1)
{
chptr = find_channel(parv[1], NullChn);
if (chptr == NullChn)
return m_umode(cptr, sptr, parc, parv);
}
else
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "MODE");
return 0;
}
sptr->flags &= ~FLAGS_TS8;
clean_channelname(parv[1]);
if (*parv[1] == '&' && !MyClient(sptr))
return 0;
if (parc < 3)
{
*modebuf = *parabuf = '\0';
modebuf[1] = '\0';
channel_modes(sptr, modebuf, parabuf, chptr);
sendto_one(sptr, rpl_str(RPL_CHANNELMODEIS), me.name, parv[0],
chptr->chname, modebuf, parabuf);
sendto_one(sptr, rpl_str(RPL_CREATIONTIME), me.name, parv[0],
chptr->chname, chptr->creationtime);
return 0;
}
if (IsModelessChannel(chptr->chname))
{
if (IsClient(sptr))
sendto_one(sptr, err_str(ERR_MODELESS), me.name, parv[0], chptr->chname);
return 0;
}
if (!(sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2,
modebuf, parabuf, nparabuf, &badop)))
{
sendto_one(sptr, err_str(IsMember(sptr, chptr) ? ERR_CHANOPRIVSNEEDED :
ERR_NOTONCHANNEL), me.name, parv[0], chptr->chname);
return 0;
}
if (badop >= 2)
send_hack_notice(cptr, sptr, parc, parv, badop, 1);
if (strlen(modebuf) > (size_t) 1 || sendts > 0)
{
if (badop != 2 && strlen(modebuf) > (size_t) 1)
sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s %s",
IsAnOper(sptr) ? me.name : parv[0],
chptr->chname, modebuf, parabuf);
if (IsLocalChannel(chptr->chname))
return 0;
/* We send a creationtime of 0, to mark it as a hack --Run */
if (IsServer(sptr) && (badop == 2 || sendts > 0))
{
if (*modebuf == '\0')
strcpy(modebuf, "+");
if (badop != 2)
{
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s %s %lu",
IsAnOper(sptr) ? me.name : parv[0],
chptr->chname, modebuf, parabuf,
(badop == 4) ? (time_t) 0 : chptr->creationtime);
sendto_highprot_butone(cptr, 10, ":%s MODE %s %s %s %lu",
IsAnOper(sptr) ? me.name : parv[0],
chptr->chname, modebuf, nparabuf,
(badop == 4) ? (time_t) 0 : chptr->creationtime);
}
}
else
{
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s %s",
IsAnOper(sptr) ? me.name : parv[0],
chptr->chname, modebuf, parabuf);
sendto_highprot_butone(cptr, 10, ":%s MODE %s %s %s",
IsAnOper(sptr) ? me.name : parv[0],
chptr->chname, modebuf, nparabuf);
}
}
return 0;
}
static int DoesOp(char *modebuf)
{
modebuf--; /* Is it possible that a mode
starts with o and not +o ? */
while (*++modebuf)
if (*modebuf == 'o' || *modebuf == 'v')
return (1);
return 0;
}
/* This function should be removed when all servers are 2.10 */
static void sendmodeto_one(aClient *cptr, char *from, char *name,
char *mode, char *param, time_t creationtime)
{
if (IsServer(cptr) && DoesOp(mode) && creationtime)
sendto_one(cptr, ":%s MODE %s %s %s %lu",
from, name, mode, param, creationtime);
else
sendto_one(cptr, ":%s MODE %s %s %s",
from, name, mode, param);
}
char *pretty_mask(char *mask)
{
Reg1 char *cp;
Reg2 char *user;
Reg3 char *host;
if ((user = strchr((cp = mask), '!')))
*user++ = '\0';
if ((host = strrchr(user ? user : cp, '@')))
{
*host++ = '\0';
if (!user)
return make_nick_user_host(NULL, cp, host);
}
else if (!user && strchr(cp, '.'))
return make_nick_user_host(NULL, NULL, cp);
return make_nick_user_host(cp, user, host);
}
static char bmodebuf[MODEBUFLEN], bparambuf[MODEBUFLEN];
static char nbparambuf[MODEBUFLEN]; /* "Numeric" Bounce Parameter Buffer */
/*
* Check and try to apply the channel modes passed in the parv array for
* the client ccptr to channel chptr. The resultant changes are printed
* into mbuf and pbuf (if any) and applied to the channel.
*/
static int set_mode(aClient *cptr, aClient *sptr, aChannel *chptr, int parc,
char *parv[], char *mbuf, char *pbuf, char *npbuf, int *badop)
{
static Link chops[MAXPARA - 2]; /* This size is only needed when a broken
server sends more then MAXMODEPARAMS
parameters */
static int flags[] =
{
MODE_PRIVATE, 'p', MODE_SECRET, 's',
MODE_MODERATED, 'm', MODE_NOPRIVMSGS, 'n',
MODE_TOPICLIMIT, 't', MODE_INVITEONLY, 'i',
MODE_VOICE, 'v', MODE_KEY, 'k',
0x0, 0x0};
Reg1 Link *lp;
Reg2 char *curr = parv[0], *cp = NULL;
Reg3 int *ip;
Link *member, *tmp = NULL;
u_int whatt = MODE_ADD, bwhatt = 0;
int limitset = 0, bounce, add_banid_called = 0;
int nusers = 0, new, len, nlen, blen, nblen, keychange = 0;
int opcnt = 0, banlsent = 0;
int doesdeop = 0, doesop = 0, hacknotice = 0, change, gotts = 0;
aClient *who;
Mode *mode, oldm;
static char numeric[16];
char *bmbuf = bmodebuf, *bpbuf = bparambuf, *nbpbuf = nbparambuf;
time_t newtime = (time_t) 0;
aConfItem *aconf;
*mbuf = *pbuf = *npbuf = *bmbuf = *bpbuf = *nbpbuf = '\0';
*badop = 0;
if (parc < 1)
return 0;
mode = &(chptr->mode);
memcpy(&oldm, mode, sizeof(Mode));
/*
* Mode is accepted when sptr is a channel operator
* but also when the mode is received from a server.
* At this point, let any member pass, so they are allowed
* to see the bans.
*/
if (!(IsServer(cptr) || (tmp = IsMember(sptr, chptr))))
return 0;
new = mode->mode;
while (curr && *curr)
{
switch (*curr)
{
case '+':
whatt = MODE_ADD;
break;
case '-':
whatt = MODE_DEL;
break;
case 'o':
case 'v':
if (--parc <= 0)
break;
parv++;
*parv = check_string(*parv);
if (MyClient(sptr) && opcnt >= MAXMODEPARAMS)
break;
/*
* Check for nickname changes and try to follow these
* to make sure the right client is affected by the
* mode change.
* Even if we find a nick with find_chasing() there
* is still a reason to ignore in a special case.
* We need to ignore the mode when:
* - It is part of a net.burst (from a server and
* a MODE_ADD). Ofcourse we don't ignore mode
* changes from Uworld.
* - The found nick is not on the right side off
* the net.junction.
* This fixes the bug that when someone (tries to)
* ride a net.break and does so with the nick of
* someone on the otherside, that he is nick collided
* (killed) but his +o still ops the other person.
*/
if (MyClient(sptr) || Protocol(cptr) < 10)
{
if (!(who = find_chasing(sptr, parv[0], NULL)))
break;
}
else
{
if (!(who = FindNClient(parv[0])))
break;
}
if (whatt == MODE_ADD && IsServer(sptr) && who->from != sptr->from &&
!IsAnOper(sptr) && !find_conf_host(cptr->confs, sptr->name, CONF_UWORLD))
break;
if (!(member = find_user_link(chptr->members, who)))
{
sendto_one(cptr, err_str(ERR_USERNOTINCHANNEL),
me.name, cptr->name, who->name, chptr->chname);
break;
}
/* if the user is +k, prevent a deop from local user */
if (whatt == MODE_DEL && IsChannelService(who) &&
MyClient(cptr) && *curr == 'o')
{
sendto_one(cptr, err_str(ERR_ISCHANSERVICE), me.name,
cptr->name, parv[0], chptr->chname);
break;
}
if (whatt == MODE_ADD)
{
lp = &chops[opcnt++];
lp->value.cptr = who;
if (IsServer(sptr) && (!(who->flags & FLAGS_TS8) || ((*curr == 'o') &&
!(member->flags & (CHFL_SERVOPOK | CHFL_CHANOP)))))
*badop = ((member->flags & CHFL_DEOPPED) && (*curr == 'o')) ? 2 : 3;
lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
lp->flags |= MODE_ADD;
}
else if (whatt == MODE_DEL)
{
lp = &chops[opcnt++];
lp->value.cptr = who;
doesdeop = 1; /* Also when -v */
lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
lp->flags |= MODE_DEL;
}
if (*curr == 'o')
doesop = 1;
break;
case 'k':
if (--parc <= 0)
break;
parv++;
/* check now so we eat the parameter if present */
if (keychange)
break;
*parv = check_string(*parv);
{
u_char *s1, *s2;
for (s1 = s2 = (u_char *)*parv; *s2; s2++)
if ((*s1 = *s2 & 0x7f) > (u_char)32 && *s1 != ':')
s1++;
*s1 = '\0';
}
if (!**parv) /* nothing left in key */
break;
if (MyClient(sptr) && opcnt >= MAXMODEPARAMS)
break;
if (whatt == MODE_ADD)
{
if (*mode->key && !IsServer(cptr))
sendto_one(cptr, err_str(ERR_KEYSET),
me.name, cptr->name, chptr->chname);
else if (!*mode->key || IsServer(cptr))
{
lp = &chops[opcnt++];
lp->value.cp = *parv;
if (strlen(lp->value.cp) > (size_t) KEYLEN)
lp->value.cp[KEYLEN] = '\0';
lp->flags = MODE_KEY | MODE_ADD;
keychange = 1;
}
}
else if (whatt == MODE_DEL)
{
if (mycmp(mode->key, *parv) == 0 || IsServer(cptr))
{
lp = &chops[opcnt++];
lp->value.cp = mode->key;
lp->flags = MODE_KEY | MODE_DEL;
keychange = 1;
}
}
break;
case 'b':
if (--parc <= 0)
{
if (banlsent) /* Only send it once */
break;
for (lp = chptr->banlist; lp; lp = lp->next)
sendto_one(cptr, rpl_str(RPL_BANLIST), me.name, cptr->name,
chptr->chname, lp->value.ban.banstr, lp->value.ban.who,
lp->value.ban.when);
sendto_one(cptr, rpl_str(RPL_ENDOFBANLIST), me.name, cptr->name,
chptr->chname);
banlsent = 1;
break;
}
parv++;
if (BadPtr(*parv))
break;
if (MyClient(sptr) && opcnt >= MAXMODEPARAMS)
break;
if (whatt == MODE_ADD)
{
lp = &chops[opcnt++];
lp->value.cp = *parv;
lp->flags = MODE_ADD | MODE_BAN;
}
else if (whatt == MODE_DEL)
{
lp = &chops[opcnt++];
lp->value.cp = *parv;
lp->flags = MODE_DEL | MODE_BAN;
}
break;
case 'l':
/*
* limit 'l' to only *1* change per mode command but
* eat up others.
*/
if (limitset)
{
if (whatt == MODE_ADD && --parc > 0)
parv++;
break;
}
if (whatt == MODE_DEL)
{
limitset = 1;
nusers = 0;
break;
}
if (--parc > 0)
{
if (BadPtr(*parv))
break;
if (MyClient(sptr) && opcnt >= MAXMODEPARAMS)
break;
if (!(nusers = atoi(*++parv)))
continue;
lp = &chops[opcnt++];
lp->flags = MODE_ADD | MODE_LIMIT;
limitset = 1;
break;
}
sendto_one(cptr, err_str(ERR_NEEDMOREPARAMS),
me.name, cptr->name, "MODE +l");
break;
case 'i': /* falls through for default case */
if (whatt == MODE_DEL)
while ((lp = chptr->invites))
del_invite(lp->value.cptr, chptr);
default:
for (ip = flags; *ip; ip += 2)
if (*(ip + 1) == *curr)
break;
if (*ip)
{
if (whatt == MODE_ADD)
{
if (*ip == MODE_PRIVATE)
new &= ~MODE_SECRET;
else if (*ip == MODE_SECRET)
new &= ~MODE_PRIVATE;
new |= *ip;
}
else
new &= ~*ip;
}
else if (!IsServer(cptr))
sendto_one(cptr, err_str(ERR_UNKNOWNMODE),
me.name, cptr->name, *curr);
break;
}
curr++;
/*
* Make sure mode strings such as "+m +t +p +i" are parsed
* fully.
*/
if (!*curr && parc > 0)
{
curr = *++parv;
parc--;
/* If this was from a server, and it is the last
* parameter and it starts with a digit, it must
* be the creationtime. --Run
*/
if (IsServer(sptr))
{
if (parc == 1 && isdigit(*curr))
{
newtime = atoi(curr);
if (newtime && chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
{
chptr->creationtime = newtime;
*badop = 0;
}
gotts = 1;
if (newtime == 0)
{
*badop = 2;
hacknotice = 1;
}
else if (newtime > chptr->creationtime)
{ /* It is a net-break ride if we have ops.
bounce modes if we have ops. --Run */
if (doesdeop)
*badop = 2;
else if (chptr->creationtime == 0)
{
if (chptr->creationtime == 0 || doesop)
chptr->creationtime = newtime;
*badop = 0;
}
/* Bounce: */
else
*badop = 1;
}
/*
* A legal *badop can occur when two
* people join simultaneously a channel,
* Allow for 10 min of lag (and thus hacking
* on channels younger then 10 min) --Run
*/
else if (*badop == 0 ||
chptr->creationtime > (TStime() - TS_LAG_TIME))
{
if (newtime < chptr->creationtime)
chptr->creationtime = newtime;
*badop = 0;
}
break;
}
}
else
*badop = 0;
}
} /* end of while loop for MODE processing */
/* Now reject non chan ops */
if (!IsPrivileged(cptr) && (!tmp || !(tmp->flags & CHFL_CHANOP)))
{
*badop = 0;
return (opcnt || new != mode->mode || limitset || keychange) ? 0 : -1;
}
if (doesop && newtime == 0 && IsPrivileged(sptr))
*badop = 2;
if (*badop >= 2 && (IsAnOper(sptr) ||
(aconf = find_conf_host(cptr->confs, sptr->name, CONF_UWORLD))))
*badop = 4;
bounce = (*badop == 1 || *badop == 2 || is_deopped(sptr, chptr)) ? 1 : 0;
if (IsAnOper(sptr)) bounce = 0;
whatt = 0;
for (ip = flags; *ip; ip += 2)
if ((*ip & new) && !(*ip & oldm.mode))
{
if (bounce)
{
if (bwhatt != MODE_DEL)
{
*bmbuf++ = '-';
bwhatt = MODE_DEL;
}
*bmbuf++ = *(ip + 1);
}
else
{
if (whatt != MODE_ADD)
{
*mbuf++ = '+';
whatt = MODE_ADD;
}
mode->mode |= *ip;
*mbuf++ = *(ip + 1);
}
}
for (ip = flags; *ip; ip += 2)
if ((*ip & oldm.mode) && !(*ip & new))
{
if (bounce)
{
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
*bmbuf++ = *(ip + 1);
}
else
{
if (whatt != MODE_DEL)
{
*mbuf++ = '-';
whatt = MODE_DEL;
}
mode->mode &= ~*ip;
*mbuf++ = *(ip + 1);
}
}
blen = nblen = 0;
if (limitset && !nusers && mode->limit)
{
if (bounce)
{
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
*bmbuf++ = 'l';
sprintf(numeric, "%-15d", mode->limit);
if ((cp = strchr(numeric, ' ')))
*cp = '\0';
strcat(bpbuf, numeric);
blen += strlen(numeric);
strcat(bpbuf, " ");
strcat(nbpbuf, numeric);
nblen += strlen(numeric);
strcat(nbpbuf, " ");
}
else
{
if (whatt != MODE_DEL)
{
*mbuf++ = '-';
whatt = MODE_DEL;
}
mode->mode &= ~MODE_LIMIT;
mode->limit = 0;
*mbuf++ = 'l';
}
}
/*
* Reconstruct "+bkov" chain.
*/
if (opcnt)
{
Reg1 int i = 0;
Reg2 char c = 0;
u_int prev_whatt = 0;
for (; i < opcnt; i++)
{
lp = &chops[i];
/*
* make sure we have correct mode change sign
*/
if (whatt != (lp->flags & (MODE_ADD | MODE_DEL)))
if (lp->flags & MODE_ADD)
{
*mbuf++ = '+';
prev_whatt = whatt;
whatt = MODE_ADD;
}
else
{
*mbuf++ = '-';
prev_whatt = whatt;
whatt = MODE_DEL;
}
len = strlen(pbuf);
nlen = strlen(npbuf);
/*
* get c as the mode char and tmp as a pointer to
* the parameter for this mode change.
*/
switch (lp->flags & MODE_WPARAS)
{
case MODE_CHANOP:
c = 'o';
cp = lp->value.cptr->name;
break;
case MODE_VOICE:
c = 'v';
cp = lp->value.cptr->name;
break;
case MODE_BAN:
/*
* I made this a bit more user-friendly (tm):
* nick = nick!*@*
* nick!user = nick!user@*
* user@host = *!user@host
* host.name = *!*@host.name --Run
*/
c = 'b';
cp = pretty_mask(lp->value.cp);
break;
case MODE_KEY:
c = 'k';
cp = lp->value.cp;
break;
case MODE_LIMIT:
c = 'l';
sprintf(numeric, "%-15d", nusers);
if ((cp = strchr(numeric, ' ')))
*cp = '\0';
cp = numeric;
break;
}
/* What could be added: cp+' '+' '+<TS>+'\0' */
if (len + strlen(cp) + 13 > (size_t) MODEBUFLEN ||
nlen + strlen(cp) + NUMNICKLEN + 12 > (size_t) MODEBUFLEN)
break;
switch (lp->flags & MODE_WPARAS)
{
case MODE_KEY:
if (strlen(cp) > (size_t) KEYLEN)
*(cp + KEYLEN) = '\0';
if ((whatt == MODE_ADD && (*mode->key == '\0' ||
mycmp(mode->key, cp) != 0)) ||
(whatt == MODE_DEL && (*mode->key != '\0')))
{
if (bounce)
{
if (*mode->key == '\0')
{
if (bwhatt != MODE_DEL)
{
*bmbuf++ = '-';
bwhatt = MODE_DEL;
}
strcat(bpbuf, cp);
blen += strlen(cp);
strcat(bpbuf, " ");
blen++;
strcat(nbpbuf, cp);
nblen += strlen(cp);
strcat(nbpbuf, " ");
nblen++;
}
else
{
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
strcat(bpbuf, mode->key);
blen += strlen(mode->key);
strcat(bpbuf, " ");
blen++;
strcat(nbpbuf, mode->key);
nblen += strlen(mode->key);
strcat(nbpbuf, " ");
nblen++;
}
*bmbuf++ = c;
mbuf--;
if (*mbuf != '+' && *mbuf != '-')
mbuf++;
else
whatt = prev_whatt;
}
else
{
*mbuf++ = c;
strcat(pbuf, cp);
len += strlen(cp);
strcat(pbuf, " ");
len++;
strcat(npbuf, cp);
nlen += strlen(cp);
strcat(npbuf, " ");
nlen++;
if (whatt == MODE_ADD)
strncpy(mode->key, cp, KEYLEN);
else
*mode->key = '\0';
}
}
break;
case MODE_LIMIT:
if (nusers && nusers != mode->limit)
{
if (bounce)
{
if (mode->limit == 0)
{
if (bwhatt != MODE_DEL)
{
*bmbuf++ = '-';
bwhatt = MODE_DEL;
}
}
else
{
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
sprintf(numeric, "%-15d", mode->limit);
if ((cp = strchr(numeric, ' ')))
*cp = '\0';
strcat(bpbuf, numeric);
blen += strlen(numeric);
strcat(bpbuf, " ");
blen++;
strcat(nbpbuf, numeric);
nblen += strlen(numeric);
strcat(nbpbuf, " ");
nblen++;
}
*bmbuf++ = c;
mbuf--;
if (*mbuf != '+' && *mbuf != '-')
mbuf++;
else
whatt = prev_whatt;
}
else
{
*mbuf++ = c;
strcat(pbuf, cp);
len += strlen(cp);
strcat(pbuf, " ");
len++;
strcat(npbuf, cp);
nlen += strlen(cp);
strcat(npbuf, " ");
nlen++;
mode->limit = nusers;
}
}
break;
case MODE_CHANOP:
case MODE_VOICE:
tmp = find_user_link(chptr->members, lp->value.cptr);
if (lp->flags & MODE_ADD)
{
change = (~tmp->flags) & CHFL_OVERLAP & lp->flags;
if (change && bounce)
{
if (lp->flags & MODE_CHANOP)
tmp->flags |= CHFL_DEOPPED;
if (bwhatt != MODE_DEL)
{
*bmbuf++ = '-';
bwhatt = MODE_DEL;
}
*bmbuf++ = c;
strcat(bpbuf, lp->value.cptr->name);
blen += strlen(lp->value.cptr->name);
strcat(bpbuf, " ");
blen++;
sprintf_irc(nbpbuf + nblen, "%c%c%c ", NumNick(lp->value.cptr));
nblen += NUMNICKLEN + 1;
change = 0;
}
else if (change)
{
tmp->flags |= lp->flags & CHFL_OVERLAP;
if (lp->flags & MODE_CHANOP)
{
tmp->flags &= ~CHFL_DEOPPED;
if (IsServer(sptr))
tmp->flags &= ~CHFL_SERVOPOK;
}
}
}
else
{
change = tmp->flags & CHFL_OVERLAP & lp->flags;
if (change && bounce)
{
if (lp->flags & MODE_CHANOP)
tmp->flags &= ~CHFL_DEOPPED;
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
*bmbuf++ = c;
strcat(bpbuf, lp->value.cptr->name);
blen += strlen(lp->value.cptr->name);
strcat(bpbuf, " ");
blen++;
sprintf_irc(nbpbuf + nblen, "%c%c%c ", NumNick(lp->value.cptr));
nblen += NUMNICKLEN + 1;
change = 0;
}
else
{
tmp->flags &= ~change;
if ((change & MODE_CHANOP) && IsServer(sptr))
tmp->flags |= CHFL_DEOPPED;
}
}
if (change || *badop == 2 || *badop == 4)
{
*mbuf++ = c;
strcat(pbuf, cp);
len += strlen(cp);
strcat(pbuf, " ");
len++;
/*
* sprintf_irc(npbuf + nlen, "%c%c%c ", NumNick(lp->value.cptr));
* nlen += NUMNICKLEN + 1;
* But this is slightly faster:
*/
npbuf[nlen++] = *(lp->value.cptr)->user->server->yxx;
npbuf[nlen++] = (lp->value.cptr)->yxx[0];
npbuf[nlen++] = (lp->value.cptr)->yxx[1];
npbuf[nlen++] = ' ';
npbuf[nlen] = 0;
}
else
{
mbuf--;
if (*mbuf != '+' && *mbuf != '-')
mbuf++;
else
whatt = prev_whatt;
}
break;
case MODE_BAN:
/*
* Only bans aren't bounced, it makes no sense to bounce last second
* bans while propagating bans done before the net.rejoin. The reason
* why I don't bounce net.rejoin bans is because it is too much
* work to take care of too long strings adding the necessary TS to
* net.burst bans -- RunLazy
* We do have to check for *badop==2 now, we don't want HACKs to take
* effect.
*
* Since BURST - I *did* implement net.rejoin ban bouncing. So now it
* certainly makes sense to also bounce 'last second' bans (bans done
* after the net.junction). -- RunHardWorker
*/
if ((change = (whatt & MODE_ADD) &&
!add_banid(sptr, chptr, cp, !bounce, !add_banid_called)))
add_banid_called = 1;
else
change = (whatt & MODE_DEL) && !del_banid(sptr, chptr, cp, !bounce);
if (bounce && change)
{
change = 0;
if ((whatt & MODE_ADD))
{
if (bwhatt != MODE_DEL)
{
*bmbuf++ = '-';
bwhatt = MODE_DEL;
}
}
else if ((whatt & MODE_DEL))
{
if (bwhatt != MODE_ADD)
{
*bmbuf++ = '+';
bwhatt = MODE_ADD;
}
}
*bmbuf++ = c;
strcat(bpbuf, cp);
blen += strlen(cp);
strcat(bpbuf, " ");
blen++;
strcat(nbpbuf, cp);
nblen += strlen(cp);
strcat(nbpbuf, " ");
nblen++;
}
if (change)
{
*mbuf++ = c;
strcat(pbuf, cp);
len += strlen(cp);
strcat(pbuf, " ");
len++;
strcat(npbuf, cp);
nlen += strlen(cp);
strcat(npbuf, " ");
nlen++;
}
else
{
mbuf--;
if (*mbuf != '+' && *mbuf != '-')
mbuf++;
else
whatt = prev_whatt;
}
break;
}
} /* for (; i < opcnt; i++) */
} /* if (opcnt) */
*mbuf++ = '\0';
*bmbuf++ = '\0';
/* Bounce here */
if (!hacknotice && *bmodebuf && chptr->creationtime)
{
if (IsAnOper(cptr) || Protocol(cptr) < 10)
sendto_one(cptr, ":%s MODE %s %s %s %lu",
me.name, chptr->chname, bmodebuf, bparambuf,
*badop == 2 ? (time_t) 0 : chptr->creationtime);
else
sendto_one(cptr, "%c MODE %s %s %s %lu",
NumServ(&me), chptr->chname, bmodebuf, nbparambuf,
*badop == 2 ? (time_t) 0 : chptr->creationtime);
}
/* If there are possibly bans to re-add, bounce them now */
if (add_banid_called && bounce)
{
Link *ban[6]; /* Max 6 bans at a time */
size_t len[6], sblen, total_len;
int cnt, delayed = 0;
while (delayed || (ban[0] = next_overlapped_ban()))
{
len[0] = strlen(ban[0]->value.ban.banstr);
cnt = 1; /* We already got one ban :) */
sblen = sprintf_irc(sendbuf, ":%s MODE %s +b",
me.name, chptr->chname) - sendbuf;
total_len = sblen + 1 + len[0]; /* 1 = ' ' */
/* Find more bans: */
delayed = 0;
while (cnt < 6 && (ban[cnt] = next_overlapped_ban()))
{
len[cnt] = strlen(ban[cnt]->value.ban.banstr);
if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
{
delayed = cnt + 1; /* != 0 */
break; /* Flush */
}
sendbuf[sblen++] = 'b';
total_len += 2 + len[cnt++]; /* 2 = "b " */
}
while (cnt--)
{
sendbuf[sblen++] = ' ';
strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr);
sblen += len[cnt];
}
sendbufto_one(cptr); /* Send bounce to uplink */
if (delayed)
ban[0] = ban[delayed - 1];
}
}
/* Send -b's of overlapped bans to clients to keep them synchronized */
if (add_banid_called && !bounce)
{
Link *ban;
char *banstr[6]; /* Max 6 bans at a time */
size_t len[6], sblen, psblen, total_len;
int cnt, delayed = 0;
Link *lp; aClient *acptr;
if (IsServer(sptr))
psblen = sprintf_irc(sendbuf, ":%s MODE %s -b",
sptr->name, chptr->chname) - sendbuf;
else /* We rely on IsRegistered(sptr) being true for MODE */
psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name,
sptr->user->username, sptr->user->host, chptr->chname) - sendbuf;
while (delayed || (ban = next_removed_overlapped_ban()))
{
if (!delayed)
{
len[0] = strlen((banstr[0] = ban->value.ban.banstr));
ban->value.ban.banstr = NULL;
}
cnt = 1; /* We already got one ban :) */
sblen = psblen;
total_len = sblen + 1 + len[0]; /* 1 = ' ' */
/* Find more bans: */
delayed = 0;
while (cnt < 6 && (ban = next_removed_overlapped_ban()))
{
len[cnt] = strlen((banstr[cnt] = ban->value.ban.banstr));
ban->value.ban.banstr = NULL;
if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
{
delayed = cnt + 1; /* != 0 */
break; /* Flush */
}
sendbuf[sblen++] = 'b';
total_len += 2 + len[cnt++]; /* 2 = "b " */
}
while (cnt--)
{
sendbuf[sblen++] = ' ';
strcpy(sendbuf + sblen, banstr[cnt]);
RunFree(banstr[cnt]);
sblen += len[cnt];
}
for (lp = chptr->members; lp; lp = lp->next)
if (MyConnect(acptr = lp->value.cptr) && !(lp->flags & CHFL_ZOMBIE))
sendbufto_one(acptr);
if (delayed)
{
banstr[0] = banstr[delayed - 1];
len[0] = len[delayed - 1];
}
}
}
return gotts ? 1 : -1;
}
/* We are now treating the <key> part of /join <channel list> <key> as a key
* ring; that is, we try one key against the actual channel key, and if that
* doesn't work, we try the next one, and so on. -Kev -Texaco
* Returns: 0 on match, 1 otherwise
* This version contributed by SeKs <intru@info.polymtl.ca>
*/
static int compall(char *key, char *keyring)
{
register char *p1;
top:
p1 = key; /* point to the key... */
while (*p1 && *p1 == *keyring)
{ /* step through the key and ring until they
don't match... */
p1++;
keyring++;
}
if (!*p1 && (!*keyring || *keyring == ','))
/* ok, if we're at the end of the and also at the end of one of the keys
in the keyring, we have a match */
return 0;
if (!*keyring) /* if we're at the end of the key ring, there
weren't any matches, so we return 1 */
return 1;
/* Not at the end of the key ring, so step
through to the next key in the ring: */
while (*keyring && *(keyring++) != ',');
goto top; /* and check it against the key */
}
static int can_join(aClient *sptr, aChannel *chptr, char *key)
{
Reg1 Link *lp;
/* Now a banned user CAN join if invited -- Nemesi */
/* Now a user CAN escape channel limit if invited -- bfriendly */
if ((chptr->mode.mode & MODE_INVITEONLY) || (is_banned(sptr, chptr, NULL)
|| (chptr->mode.limit && chptr->users >= chptr->mode.limit)))
{
for (lp = sptr->user->invited; lp; lp = lp->next)
if (lp->value.chptr == chptr)
break;
if (!lp)
{
if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
return (ERR_CHANNELISFULL);
/* This can return an "Invite only" msg instead of the "You are banned"
if _both_ conditions are true, but who can say what is more
appropriate ? checking again IsBanned would be _SO_ cpu-xpensive ! */
return ((chptr->mode.mode & MODE_INVITEONLY) ?
ERR_INVITEONLYCHAN : ERR_BANNEDFROMCHAN);
}
}
/* now using compall (above) to test against a whole key ring -Kev */
if (*chptr->mode.key && (BadPtr(key) || compall(chptr->mode.key, key)))
return (ERR_BADCHANNELKEY);
return 0;
}
/*
* Remove bells and commas from channel name
*/
void clean_channelname(char *cn)
{
for (; *cn; cn++)
if (*cn == '\007' || *cn == ' ' || *cn == ',')
{
*cn = '\0';
return;
}
}
/*
* Get Channel block for i (and allocate a new channel
* block, if it didn't exists before).
*/
static aChannel *get_channel(aClient *cptr, char *chname, int flag)
{
Reg1 aChannel *chptr;
int len;
if (BadPtr(chname))
return NULL;
len = strlen(chname);
if (MyClient(cptr) && len > CHANNELLEN)
{
len = CHANNELLEN;
*(chname + CHANNELLEN) = '\0';
}
if ((chptr = find_channel(chname, (aChannel *)NULL)))
return (chptr);
if (flag == CREATE)
{
chptr = (aChannel *)RunMalloc(sizeof(aChannel) + len);
memset(chptr, 0, sizeof(aChannel));
strcpy(chptr->chname, chname);
if (channel)
channel->prevch = chptr;
chptr->prevch = NULL;
chptr->nextch = channel;
chptr->creationtime = MyClient(cptr) ? TStime() : (time_t) 0;
channel = chptr;
add_to_channel_hash_table(chname, chptr);
}
return chptr;
}
static void add_invite(aClient *cptr, aChannel *chptr)
{
Reg1 Link *inv, **tmp;
del_invite(cptr, chptr);
/*
* Delete last link in chain if the list is max length
*/
if (list_length(cptr->user->invited) >= MAXCHANNELSPERUSER)
del_invite(cptr, cptr->user->invited->value.chptr);
/*
* Add client to channel invite list
*/
inv = make_link();
inv->value.cptr = cptr;
inv->next = chptr->invites;
chptr->invites = inv;
/*
* Add channel to the end of the client invite list
*/
for (tmp = &(cptr->user->invited); *tmp; tmp = &((*tmp)->next));
inv = make_link();
inv->value.chptr = chptr;
inv->next = NULL;
(*tmp) = inv;
}
/*
* Delete Invite block from channel invite list and client invite list
*/
void del_invite(aClient *cptr, aChannel *chptr)
{
Reg1 Link **inv, *tmp;
for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
if (tmp->value.cptr == cptr)
{
*inv = tmp->next;
free_link(tmp);
break;
}
for (inv = &(cptr->user->invited); (tmp = *inv); inv = &tmp->next)
if (tmp->value.chptr == chptr)
{
*inv = tmp->next;
free_link(tmp);
break;
}
}
/* List and skip all channels that are listen */
void list_next_channels(aClient *cptr, int nr)
{
aListingArgs *args = cptr->listing;
aChannel *chptr = args->chptr;
chptr->mode.mode &= ~MODE_LISTED;
while (is_listed(chptr) || --nr >= 0)
{
for (; chptr; chptr = chptr->nextch)
{
if (!cptr->user || (SecretChannel(chptr) && !IsMember(cptr, chptr)))
continue;
if (chptr->users > args->min_users && chptr->users < args->max_users &&
chptr->creationtime > args->min_time &&
chptr->creationtime < args->max_time &&
(!args->topic_limits || (*chptr->topic &&
chptr->topic_time > args->min_topic_time &&
chptr->topic_time < args->max_topic_time)))
{
sendto_one(cptr, rpl_str(RPL_LIST), me.name, cptr->name,
ShowChannel(cptr, chptr) ? chptr->chname : "*",
chptr->users, ShowChannel(cptr, chptr) ? chptr->topic : "");
chptr = chptr->nextch;
break;
}
}
if (!chptr)
{
RunFree(cptr->listing);
cptr->listing = NULL;
sendto_one(cptr, rpl_str(RPL_LISTEND), me.name, cptr->name);
break;
}
}
if (chptr)
{
cptr->listing->chptr = chptr;
chptr->mode.mode |= MODE_LISTED;
}
}
/*
* Subtract one user from channel i (and free channel
* block, if channel became empty).
*/
static void sub1_from_channel(aChannel *chptr)
{
Reg2 Link *tmp;
Link *obtmp;
if (--chptr->users <= 0) /* Can indeed become < 0,
it's called for an empty channel too */
{
if (is_listed(chptr))
{
int i;
for (i = 0; i <= highest_fd; i++)
{
aClient *acptr;
if ((acptr = loc_clients[i]) && acptr->listing &&
acptr->listing->chptr == chptr)
{
list_next_channels(acptr, 1);
break; /* Only one client can list a channel */
}
}
}
/*
* Now, find all invite links from channel structure
*/
while ((tmp = chptr->invites))
del_invite(tmp->value.cptr, chptr);
tmp = chptr->banlist;
while (tmp)
{
obtmp = tmp;
tmp = tmp->next;
RunFree(obtmp->value.ban.banstr);
RunFree(obtmp->value.ban.who);
free_link(obtmp);
}
if (chptr->prevch)
chptr->prevch->nextch = chptr->nextch;
else
channel = chptr->nextch;
if (chptr->nextch)
chptr->nextch->prevch = chptr->prevch;
del_from_channel_hash_table(chptr->chname, chptr);
RunFree((char *)chptr);
}
}
/*
* m_join
*
* parv[0] = sender prefix
* parv[1] = channel
* parv[2] = channel keys (client), or channel TS (server)
*/
int m_join(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
static char jbuf[BUFSIZE], mbuf[BUFSIZE];
Reg1 Link *lp;
Reg3 aChannel *chptr;
Reg4 char *name, *keysOrTS = NULL;
int i = 0, flags = 0, zombie = 0, jlen = 0, mlen = 0, sendcreate = 0;
int *buflen;
char *p = NULL, *bufptr;
if (parc < 2 || *parv[1] == '\0')
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "JOIN");
return 0;
}
for (p = parv[1]; *p; p++) /* find the last "JOIN 0" in the line -Kev */
if (*p == '0' && (*(p + 1) == ',' || *(p + 1) == '\0'))
{
/* If it's a single "0", remember the place; we will start parsing
the channels after the last 0 in the line -Kev */
parv[1] = p;
if (!*(p + 1))
break;
p++;
}
else
{ /* Step through to the next comma or until the
end of the line, in an attempt to save CPU
-Kev */
while (*p != ',' && *p != '\0')
p++;
if (!*p)
break;
}
keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
parv[2] needs to be NULL for the call to
m_names below -Kev */
parv[2] = p = NULL;
*jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
/*
* Rebuild list of channels joined to be the actual result of the
* JOIN. Note that "JOIN 0" is the destructive problem.
*/
for (name = strtoken(&p, parv[1], ","); name; name = strtoken(&p, NULL, ","))
{
size_t len;
clean_channelname(name);
if (*name == '&' && !MyConnect(sptr))
continue;
if (*name == '0' && *(name + 1) == '\0')
{
/* Remove the user from all his channels -Kev */
while ((lp = sptr->user->channel))
{
chptr = lp->value.chptr;
if (!is_zombie(sptr, chptr))
sendto_channel_butserv(chptr, sptr, PartFmt, parv[0], chptr->chname);
remove_user_from_channel(sptr, chptr);
}
}
else
{ /* not a /join 0, so treat it as
a /join #channel -Kev */
if (!IsChannelName(name))
{
if (MyClient(sptr))
sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
continue;
}
if (MyConnect(sptr))
{
/*
* Local client is first to enter previously nonexistant
* channel so make them (rightfully) the Channel Operator.
* This looks kind of ugly because we try to avoid calling the strlen()
*/
if (ChannelExists(name))
{
flags = CHFL_DEOPPED;
sendcreate = 0;
}
else if (strlen(name) > CHANNELLEN)
{
*(name + CHANNELLEN) = '\0';
if (ChannelExists(name))
{
flags = CHFL_DEOPPED;
sendcreate = 0;
}
else
{
flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
sendcreate = 1;
}
}
else
{
flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
sendcreate = 1;
}
if (sptr->user->joined >= MAXCHANNELSPERUSER)
{
sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
me.name, parv[0], name);
break; /* Can't return, else he won't get on ANY
channels! Break out of the for loop instead.
-Kev */
}
}
chptr = get_channel(sptr, name, CREATE);
if (chptr && (lp = find_user_link(chptr->members, sptr)))
{
if (lp->flags & CHFL_ZOMBIE)
{
zombie = 1;
flags = lp->flags & (CHFL_DEOPPED | CHFL_SERVOPOK);
remove_user_from_channel(sptr, chptr);
chptr = get_channel(sptr, name, CREATE);
}
else
continue;
}
if (!chptr->creationtime) /* A remote JOIN created this channel ? */
chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
if (parc > 2)
{
if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
chptr->creationtime = atoi(keysOrTS);
else
parc = 2; /* Don't pass it on */
}
if (!zombie)
{
if (!MyConnect(sptr))
flags = CHFL_DEOPPED;
if (sptr->flags & FLAGS_TS8)
flags |= CHFL_SERVOPOK;
}
if (!chptr || (MyConnect(sptr) && (i = can_join(sptr, chptr, keysOrTS))))
{
sendto_one(sptr, err_str(i), me.name, parv[0], name);
continue;
}
/*
* Complete user entry to the new channel (if any)
*/
add_user_to_channel(chptr, sptr, flags);
/*
* Notify all other users on the new channel
*/
sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
if (MyClient(sptr))
{
del_invite(sptr, chptr);
if (chptr->topic[0] != '\0')
{
sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
parv[0], name, chptr->topic);
sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
chptr->topic_nick, chptr->topic_time);
}
parv[1] = name;
m_names(cptr, sptr, 2, parv);
}
}
/* Select proper buffer; mbuf for creation, jbuf otherwise */
if (*name == '&')
continue; /* Head off local channels at the pass */
bufptr = (sendcreate == 0) ? jbuf : mbuf;
buflen = (sendcreate == 0) ? &jlen : &mlen;
len = strlen(name);
if (*buflen < BUFSIZE - len - 2)
{
if (*bufptr)
{
strcat(bufptr, ","); /* Add to join buf */
*buflen += 1;
}
strncat(bufptr, name, BUFSIZE - *buflen - 1);
*buflen += len;
}
sendcreate = 0; /* Reset sendcreate */
}
#ifndef NO_PROTOCOL9
if (*jbuf || *mbuf) /* Propagate joins to P09 servers */
sendto_lowprot_butone(cptr, 9, (*jbuf && *mbuf) ? ":%s JOIN %s,%s" :
":%s JOIN %s%s", parv[0], jbuf, mbuf);
#endif
if (*jbuf) /* Propgate joins to P10 servers */
#ifdef NO_PROTOCOL9
sendto_serv_butone(cptr,
parc > 2 ? ":%s JOIN %s %s" : ":%s JOIN %s", parv[0], jbuf, keysOrTS);
#else
sendto_highprot_butone(cptr, 10,
parc > 2 ? ":%s JOIN %s %s" : ":%s JOIN %s", parv[0], jbuf, keysOrTS);
#endif
if (*mbuf) /* and now creation events */
#ifdef NO_PROTOCOL9
sendto_serv_butone(cptr, "%c%c%c CREATE %s %lu",
NumNick(sptr), mbuf, TStime());
#else
sendto_highprot_butone(cptr, 10, "%c%c%c CREATE %s %lu",
NumNick(sptr), mbuf, TStime());
#endif
if (MyClient(sptr))
{ /* shouldn't ever set TS for remote JOIN's */
if (*jbuf)
{ /* check for channels that need TS's */
p = NULL;
for (name = strtoken(&p, jbuf, ","); name; name = strtoken(&p, NULL, ","))
{
chptr = get_channel(sptr, name, !CREATE);
if (chptr && chptr->mode.mode & MODE_SENDTS)
{ /* send a TS? */
sendto_serv_butone(cptr, ":%s MODE %s + %lu", me.name, name,
chptr->creationtime); /* ok, send TS */
chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */
}
}
}
if (*mbuf)
{ /* ok, send along modes for creation events to P9 */
p = NULL;
for (name = strtoken(&p, mbuf, ","); name; name = strtoken(&p, NULL, ","))
{
chptr = get_channel(sptr, name, !CREATE);
sendto_lowprot_butone(cptr, 9, ":%s MODE %s +o %s %lu", me.name,
name, parv[0], chptr->creationtime);
}
}
}
return 0;
}
/*
* m_create
*
* parv[0] = sender prefix
* parv[1] = channel names
* parv[2] = channel time stamp
*/
int m_create(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
char cbuf[BUFSIZE]; /* Buffer for list with channels
that `sptr' really creates */
time_t chanTS; /* Creation time for all channels
in the comma seperated list */
char *p, *name;
Reg5 aChannel *chptr;
int badop;
/* sanity checks: Only accept CREATE messages from servers */
if (!IsServer(cptr) || parc < 3 || *parv[2] == '\0')
return 0;
chanTS = atoi(parv[2]);
*cbuf = '\0'; /* Start with empty buffer */
/* For each channel in the comma seperated list: */
for (name = strtoken(&p, parv[1], ","); name; name = strtoken(&p, NULL, ","))
{
badop = 0; /* Default is to accept the op */
if ((chptr = find_channel(name, NullChn)))
{
if (TStime() - chanTS > TS_LAG_TIME)
{
/* A bounce would not be accepted anyway - if we get here something
is wrong with the TS clock syncing (or we have more then
TS_LAG_TIME lag, or an admin is hacking */
badop = 2;
/* This causes a HACK notice on all upstream servers: */
if (IsAnOper(cptr) || Protocol(cptr) < 10)
sendto_one(cptr, ":%s MODE %s -o %s 0",
me.name, chptr->chname, sptr->name);
else
sendto_one(cptr, ":%s MODE %s -o %c%c%c 0",
me.name, chptr->chname, NumNick(sptr));
/* This causes a WALLOPS on all downstream servers and a notice to our
own opers: */
parv[1] = name; /* Corrupt parv[1], it is not used anymore anyway */
send_hack_notice(cptr, sptr, parc, parv, badop, 2);
}
else if (chptr->creationtime && chanTS > chptr->creationtime &&
chptr->creationtime != MAGIC_REMOTE_JOIN_TS)
{
/* We (try) to bounce the mode, because the CREATE is used on an older
channel, probably a net.ride */
badop = 1;
/* Send a deop upstream: */
if (IsAnOper(cptr) || Protocol(cptr) < 10)
sendto_one(cptr, ":%s MODE %s -o %s %lu", me.name,
chptr->chname, sptr->name, chptr->creationtime);
else
sendto_one(cptr, ":%s MODE %s -o %c%c%c %lu", me.name,
chptr->chname, NumNick(sptr), chptr->creationtime);
}
}
else /* Channel doesn't exist: create it */
chptr = get_channel(sptr, name, CREATE);
/* Add and mark ops */
add_user_to_channel(chptr, sptr,
(badop || IsModelessChannel(name)) ? CHFL_DEOPPED : CHFL_CHANOP);
/* Send user join to the local clients (if any) */
sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
if (badop) /* handle badop: convert CREATE into JOIN */
sendto_serv_butone(cptr, ":%s JOIN %s %lu",
sptr->name, chptr->chname, chptr->creationtime);
else
{
/* Send the op to local clients:
(if any; extremely unlikely, but it CAN happen) */
if (!IsModelessChannel(name))
sendto_channel_butserv(chptr, sptr, ":%s MODE %s +o %s",
sptr->user->server->name, name, parv[0]);
/* Set/correct TS and add the channel to the
buffer for accepted channels: */
chptr->creationtime = chanTS;
if (*cbuf)
strcat(cbuf, ",");
strcat(cbuf, name);
}
}
if (*cbuf) /* Any channel accepted with ops ? */
{
#ifdef NO_PROTOCOL9
sendto_serv_butone(cptr, "%c%c%c CREATE %s %lu",
NumNick(sptr), cbuf, chanTS);
#else
/* send CREATEs to 2.10 servers */
sendto_highprot_butone(cptr, 10, "%c%c%c CREATE %s %lu",
NumNick(sptr), cbuf, chanTS);
/* And JOIN + MODE to 2.9 servers; following
is not needed after all are 2.10 */
sendto_lowprot_butone(cptr, 9, ":%s JOIN %s", parv[0], cbuf);
p = NULL;
for (name = strtoken(&p, cbuf, ","); name; name = strtoken(&p, NULL, ","))
sendto_lowprot_butone(cptr, 9, ":%s MODE %s +o %s %lu",
sptr->user->server->name, name, parv[0], chanTS);
#endif
}
return 0;
}
static size_t prefix_len;
static void add_token_to_sendbuf(aClient *cptr, aChannel *chptr, char *token,
size_t * sblenp, int *firstp, int *send_itp, char is_a_ban, int mode)
{
int first = *firstp;
/*
* Heh - we do not need to test if it still fits in the buffer, because
* this BURST message is reconstructed from another BURST message, and
* it only can become smaller. --Run
*/
if (*firstp) /* First token in this parameter ? */
{
*firstp = 0;
if (*send_itp == 0)
*send_itp = 1; /* Buffer contains data to be sent */
sendbuf[(*sblenp)++] = ' ';
if (is_a_ban)
{
sendbuf[(*sblenp)++] = ':'; /* Bans are always the last "parv" */
sendbuf[(*sblenp)++] = is_a_ban;
}
}
else /* Of course, 'send_it' is already set here */
/* Seperate banmasks with a space because
they can contain commas themselfs: */
sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ',';
strcpy(sendbuf + *sblenp, token);
*sblenp += strlen(token);
if (!is_a_ban) /* nick list ? Need to take care
of modes for nicks: */
{
static int last_mode = 0;
mode &= CHFL_CHANOP | CHFL_VOICE;
if (first)
last_mode = 0;
if (last_mode != mode) /* Append mode like ':ov' if changed */
{
last_mode = mode;
sendbuf[(*sblenp)++] = ':';
if (mode & CHFL_CHANOP)
sendbuf[(*sblenp)++] = 'o';
if (mode & CHFL_VOICE)
sendbuf[(*sblenp)++] = 'v';
}
sendbuf[*sblenp] = '\0';
}
}
static void cancel_mode(aClient *sptr, aChannel *chptr, char m,
const char *param, int *count)
{
static char *pb, *sbp, *sbpi;
int paramdoesntfit = 0;
if (*count == -1) /* initialize ? */
{
sbp = sbpi =
sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname);
pb = parabuf;
*count = 0;
}
/* m == 0 means flush */
if (m)
{
if (param)
{
size_t nplen = strlen(param);
if (pb - parabuf + nplen + 23 > MODEBUFLEN)
paramdoesntfit = 1;
else
{
*sbp++ = m;
*pb++ = ' ';
strcpy(pb, param);
pb += nplen;
++*count;
}
}
else
*sbp++ = m;
}
else if (*count == 0)
return;
if (*count == 6 || !m || paramdoesntfit)
{
#ifndef NO_PROTOCOL9
Dlink *lp;
char *sbe;
#endif
Link *member;
strcpy(sbp, parabuf);
#ifndef NO_PROTOCOL9
sbe = sbp + strlen(parabuf);
#endif
for (member = chptr->members; member; member = member->next)
if (MyClient(member->value.cptr))
sendbufto_one(member->value.cptr);
#ifndef NO_PROTOCOL9
sprintf_irc(sbe, " %lu", chptr->creationtime);
/* Send 'sendbuf' to all 2.9 downlinks: */
for (lp = me.serv->down; lp; lp = lp->next)
if (Protocol(lp->value.cptr) < 10)
sendbufto_one(lp->value.cptr);
#endif
sbp = sbpi;
pb = parabuf;
*count = 0;
}
if (paramdoesntfit)
{
*sbp++ = m;
*pb++ = ' ';
strcpy(pb, param);
pb += strlen(param);
++*count;
}
}
/*
* m_burst -- by Run carlo@runaway.xs4all.nl december 1995 till march 1997
*
* parv[0] = sender prefix
* parv[1] = channel name
* parv[2] = channel timestamp
* The meaning of the following parv[]'s depend on their first character:
* If parv[n] starts with a '+':
* Net burst, additive modes
* parv[n] = <mode>
* parv[n+1] = <param> (optional)
* parv[n+2] = <param> (optional)
* If parv[n] starts with a '%', then n will be parc-1:
* parv[n] = %<ban> <ban> <ban> ...
* If parv[n] starts with another character:
* parv[n] = <nick>[:<mode>],<nick>[:<mode>],...
* where <mode> is the channel mode (ov) of nick and all following nicks.
*
* Example:
* "S BURST #channel 87654321 +ntkl key 123 AAA,AAB:o,BAA,BAB:ov :%ban1 ban2"
*
* Anti net.ride code.
*
* When the channel already exist, and its TS is larger then
* the TS in the BURST message, then we cancel all existing modes.
* If its is smaller then the received BURST message is ignored.
* If it's equal, then the received modes are just added.
*/
int m_burst(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
Reg1 aChannel *chptr;
time_t timestamp;
int netride = 0, wipeout = 0, n;
int send_it = 0, add_banid_not_called = 1;
Mode *current_mode;
size_t sblen, mblen = 0;
int mblen2, pblen2, cnt;
int prev_mode;
char prev_key[KEYLEN + 1];
Link *lp;
#ifndef NO_PROTOCOL9
int ts_sent = 0;
#endif
/* BURST is only for servers and has at least 4 parameters */
if (!IsServer(cptr) || parc < 4)
return 0;
/* Find the channel, or create it - note that the creation time
* will be 0 if it has to be created */
chptr = get_channel(sptr, parv[1], CREATE);
current_mode = &chptr->mode;
prev_mode = chptr->mode.mode;
if (*chptr->mode.key)
{
prev_mode |= MODE_KEY;
strcpy(prev_key, chptr->mode.key);
}
if (chptr->mode.limit)
prev_mode |= MODE_LIMIT;
timestamp = atoi(parv[2]);
/* Copy the new TS when the received creationtime appears to be older */
if (!chptr->creationtime || chptr->creationtime > timestamp)
{
/* Set the new timestamp */
chptr->creationtime = timestamp;
send_it = 1; /* Make sure we pass on the different timestamp ! */
/* Mark all bans as needed to be wiped out */
for (lp = chptr->banlist; lp; lp = lp->next)
lp->flags |= CHFL_BURST_BAN_WIPEOUT;
/*
* Only the first BURST for this channel can have creationtime > timestamp,
* so at this moment ALL members are on OUR side, and thus all net.riders:
*/
wipeout = 1;
}
for (lp = chptr->members; lp; lp = lp->next)
lp->flags &= ~CHFL_BURST_JOINED; /* Set later for nicks in the BURST msg */
/* If `wipeout' is set then these will be deopped later. */
/* If the entering creationtime is younger, ignore the modes */
if (chptr->creationtime < timestamp)
netride = 1; /* Only pass on the nicks (so they JOIN) */
/* Prepare buffers to pass the message */
*bparambuf = *bmodebuf = *parabuf = '\0';
pblen2 = 0;
*modebuf = '+';
mblen2 = 1;
cnt = 0;
prefix_len = sblen = sprintf_irc(sendbuf, "%c BURST %s %lu", NumServ(sptr),
chptr->chname, chptr->creationtime) - sendbuf;
/* Run over all remaining parameters */
for (n = 3; n < parc; n++)
switch (*parv[n]) /* What type is it ? mode, nicks or bans ? */
{
case '+': /* modes */
{
char *p = parv[n];
while (*(++p)) /* Run over all mode characters */
{
switch (*p) /* which mode ? */
{
/*
* The following cases all do the following:
* - In case wipeout needed, reset 'prev_mode' to indicate this
* mode should not be cancelled.
* - If wipeout or (not netride and the new mode is a change),
* add it to bmodebuf and bparabuf for propagation.
* - Else ignore it.
* - Add it to modebuf and parabuf for propagation to the
* clients when not netride and the new mode is a change.
* Special cases:
* - If a +s is received, cancel a +p and sent a -p to the
* clients too (if +p was set).
* - If a +p is received and +s is set, ignore the +p.
*/
case 'i':
{
register int tmp;
prev_mode &= ~MODE_INVITEONLY;
if (!(tmp = netride ||
(current_mode->mode & MODE_INVITEONLY)) || wipeout)
{
bmodebuf[mblen++] = 'i';
current_mode->mode |= MODE_INVITEONLY;
}
if (!tmp)
modebuf[mblen2++] = 'i';
break;
}
case 'k':
{
register int tmp;
char *param = parv[++n];
prev_mode &= ~MODE_KEY;
if (!(tmp = netride || (*current_mode->key &&
(!strcmp(current_mode->key, param) ||
(!wipeout && strcmp(current_mode->key, param) < 0)))) ||
wipeout)
{
bmodebuf[mblen++] = 'k';
strcat(bparambuf, " ");
strcat(bparambuf, param);
strncpy(current_mode->key, param, KEYLEN);
}
if (!tmp)
{
modebuf[mblen2++] = 'k';
parabuf[pblen2++] = ' ';
strcpy(parabuf + pblen2, param);
pblen2 += strlen(param);
cnt++;
}
break;
}
case 'l':
{
register int tmp;
int param = atoi(parv[++n]);
prev_mode &= ~MODE_LIMIT;
if (!(tmp = netride || (current_mode->limit &&
(current_mode->limit == param ||
(!wipeout && current_mode->limit < param)))) || wipeout)
{
bmodebuf[mblen++] = 'l';
sprintf_irc(bparambuf + strlen(bparambuf), " %d", param);
current_mode->limit = param;
}
if (!tmp)
{
modebuf[mblen2++] = 'l';
pblen2 = sprintf_irc(parabuf + pblen2,
" %d", param) - parabuf;
cnt++;
}
break;
}
case 'm':
{
register int tmp;
prev_mode &= ~MODE_MODERATED;
if (!(tmp = netride ||
(current_mode->mode & MODE_MODERATED)) || wipeout)
{
bmodebuf[mblen++] = 'm';
current_mode->mode |= MODE_MODERATED;
}
if (!tmp)
modebuf[mblen2++] = 'm';
break;
}
case 'n':
{
register int tmp;
prev_mode &= ~MODE_NOPRIVMSGS;
if (!(tmp = netride ||
(current_mode->mode & MODE_NOPRIVMSGS)) || wipeout)
{
bmodebuf[mblen++] = 'n';
current_mode->mode |= MODE_NOPRIVMSGS;
}
if (!tmp)
modebuf[mblen2++] = 'n';
break;
}
case 'p':
{
register int tmp;
/* Special case: */
if (!netride && !wipeout &&
(current_mode->mode & MODE_SECRET))
break;
prev_mode &= ~MODE_PRIVATE;
if (!(tmp = netride ||
(current_mode->mode & MODE_PRIVATE)) || wipeout)
{
bmodebuf[mblen++] = 'p';
current_mode->mode |= MODE_PRIVATE;
}
if (!tmp)
modebuf[mblen2++] = 'p';
break;
}
case 's':
{
register int tmp;
prev_mode &= ~MODE_SECRET;
if (!(tmp = netride ||
(current_mode->mode & MODE_SECRET)) || wipeout)
{
bmodebuf[mblen++] = 's';
current_mode->mode |= MODE_SECRET;
}
if (!tmp)
modebuf[mblen2++] = 's';
/* Special case: */
if (!netride && !wipeout &&
(current_mode->mode & MODE_PRIVATE))
{
int i;
for (i = mblen2 - 1; i >= 0; --i)
modebuf[i + 2] = modebuf[i];
modebuf[0] = '-';
modebuf[1] = 'p';
mblen2 += 2;
current_mode->mode &= ~MODE_PRIVATE;
}
break;
}
case 't':
{
register int tmp;
prev_mode &= ~MODE_TOPICLIMIT;
if (!(tmp = netride ||
(current_mode->mode & MODE_TOPICLIMIT)) || wipeout)
{
bmodebuf[mblen++] = 't';
current_mode->mode |= MODE_TOPICLIMIT;
}
if (!tmp)
modebuf[mblen2++] = 't';
break;
}
}
} /* <-- while over all modes */
bmodebuf[mblen] = '\0';
sendbuf[sblen] = '\0';
if (mblen) /* Anything to send at all ? */
{
send_it = 1;
strcpy(sendbuf + sblen, " +");
sblen += 2;
strcpy(sendbuf + sblen, bmodebuf);
sblen += mblen;
strcpy(sendbuf + sblen, bparambuf);
sblen += strlen(bparambuf);
}
break; /* Done mode part */
}
case '%': /* bans */
{
char *pv, *p = NULL, *ban;
int first = 1;
if (netride)
break; /* Ignore bans */
/* Run over all bans */
for (pv = parv[n] + 1; (ban = strtoken(&p, pv, " ")); pv = NULL)
{
int ret;
/*
* The following part should do the following:
* - If the new (un)ban is not a _change_ it is ignored.
* - Else, add it to sendbuf for later use.
* - If sendbuf is full, send it, and prepare a new
* message in sendbuf.
*/
ret = add_banid(sptr, chptr, ban, 1, add_banid_not_called);
if (ret == 0)
{
add_banid_not_called = 0;
/* Mark this new ban so we can send it to the clients later */
chptr->banlist->flags |= CHFL_BURST_BAN;
}
if (ret != -1)
/* A new ban was added or an existing one needs to be passed on.
* Also add it to sendbuf: */
add_token_to_sendbuf(cptr, chptr, ban, &sblen, &first,
&send_it, '%', 0);
}
break; /* Done bans part */
}
default: /* nicks */
{
char *pv, *p = NULL, *nick, *ptr;
int first = 1;
/* Default mode: */
int default_mode = CHFL_DEOPPED;
/* Run over all nicks */
for (pv = parv[n]; (nick = strtoken(&p, pv, ",")); pv = NULL)
{
aClient *acptr;
if ((ptr = strchr(nick, ':'))) /* New default mode ? */
{
*ptr = '\0'; /* Fix 'nick' */
acptr = FindNClient(nick);
if (!netride)
{
/* Calculate new mode change: */
default_mode = CHFL_DEOPPED;
while (*(++ptr))
if (*ptr == 'o')
{
default_mode |= CHFL_CHANOP;
default_mode &= ~CHFL_DEOPPED;
}
else if (*ptr == 'v')
default_mode |= CHFL_VOICE;
else
break;
}
}
else
acptr = FindNClient(nick);
/*
* Note that at this point we already received a 'NICK' for any
* <nick> numeric that is joining (and possibly opped) here.
* Therefore we consider the following situations:
* - The <nick> numeric exists and is from the direction of cptr: ok
* - The <nick> numeric does not exist:
* Apparently this previous <nick> numeric was killed (upstream)
* or it collided with an existing <nick> name.
* - The <nick> numeric exists but is from another direction:
* Apparently this previous <nick> numeric was killed,
* and due to a reroute it signed on via another link (probably
* a nick [numeric] collision).
* Note that it can't be a QUIT or SQUIT, because a QUIT would
* come from the same direction as the BURST (cptr) while an
* upstream SQUIT removes the source (server) and we would thus
* have this BURST ignored already.
* This means that if we find the nick and it is from the correct
* direction, it joins. If it doesn't exist or is from another
* direction, we have to ignore it. If all nicks are ignored, we
* remove the channel again when it is empty and don't propagate
* the BURST message.
*/
if (acptr && acptr->from == cptr)
{
/*
* The following should do the following:
* - Add it to sendbuf for later use.
* - If sendbuf is full, send it, and prepare a new
* message in sendbuf.
*/
add_token_to_sendbuf(cptr, chptr, nick, &sblen, &first,
&send_it, 0, default_mode);
/* Let is take effect: (Note that in the case of a netride
* 'default_mode' is always CHFL_DEOPPED here). */
add_user_to_channel(chptr, acptr, default_mode);
chptr->members->flags |= CHFL_BURST_JOINED;
}
} /* <-- Next nick */
if (!chptr->members) /* All nicks collided and channel is empty ? */
{
sub1_from_channel(chptr);
return 0; /* Forget about the (rest of the) message... */
}
break; /* Done nicks part */
}
} /* <-- Next parameter if any */
if (!chptr->members) /* This message only contained bans (then the previous
* message only contained collided nicks, see above) */
{
sub1_from_channel(chptr);
if (!add_banid_not_called)
while (next_removed_overlapped_ban());
return 0; /* Forget about the (rest of the) message... */
}
/* The last (possibly only) message is always send here */
if (send_it) /* Anything (left) to send ? */
{
Dlink *lp;
Link *member;
/* send 'sendbuf' to all downlinks */
for (lp = me.serv->down; lp; lp = lp->next)
{
if (lp->value.cptr == cptr)
continue;
if (Protocol(lp->value.cptr) > 9)
sendbufto_one(lp->value.cptr);
}
/*
* Now we finally can screw sendbuf again...
* Send all changes to the local clients:
*
* First send all joins and op them, because 2.9 servers
* would protest with a HACK if we first de-opped people.
* However, we don't send the +b bans yes, because we
* DO first want to -b the old bans (otherwise it's confusing).
*/
/* Send all joins: */
for (member = chptr->members; member; member = member->next)
if (member->flags & CHFL_BURST_JOINED)
{
sendto_channel_butserv(chptr, member->value.cptr, ":%s JOIN :%s",
member->value.cptr->name, chptr->chname);
#ifndef NO_PROTOCOL9
/* And to 2.9 servers: */
sendto_lowprot_butone(cptr, 9, ":%s JOIN %s",
member->value.cptr->name, chptr->chname);
#endif
}
if (!netride)
{
/* Send all +o and +v modes: */
for (member = chptr->members; member; member = member->next)
{
if ((member->flags & CHFL_BURST_JOINED))
{
int mode = CHFL_CHANOP;
for (;;)
{
if ((member->flags & mode))
{
modebuf[mblen2++] = (mode == CHFL_CHANOP) ? 'o' : 'v';
parabuf[pblen2++] = ' ';
strcpy(parabuf + pblen2, member->value.cptr->name);
pblen2 += strlen(member->value.cptr->name);
if (6 == ++cnt)
{
modebuf[mblen2] = 0;
sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s%s",
parv[0], chptr->chname, modebuf, parabuf);
#ifndef NO_PROTOCOL9
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s%s %lu",
parv[0], chptr->chname, modebuf, parabuf,
chptr->creationtime);
ts_sent = 1;
#endif
*parabuf = 0;
pblen2 = 0;
mblen2 = 1;
cnt = 0;
}
}
if (mode == CHFL_CHANOP)
mode = CHFL_VOICE;
else
break;
}
}
}
/* Flush MODEs: */
if (cnt > 0 || mblen2 > 1)
{
modebuf[mblen2] = 0;
sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s%s",
parv[0], chptr->chname, modebuf, parabuf);
#ifndef NO_PROTOCOL9
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s%s %lu",
parv[0], chptr->chname, modebuf, parabuf, chptr->creationtime);
ts_sent = 1;
#endif
}
#ifndef NO_PROTOCOL9
else if (send_it && !ts_sent)
{
sendto_lowprot_butone(cptr, 9, ":%s MODE %s + %lu",
parv[0], chptr->chname, chptr->creationtime);
ts_sent = 1;
}
#endif
}
}
if (wipeout)
{
Link *lp;
Link **ban;
int mode;
char m;
int count = -1;
/* Now cancel all previous simple modes */
if ((prev_mode & MODE_SECRET))
cancel_mode(sptr, chptr, 's', NULL, &count);
if ((prev_mode & MODE_PRIVATE))
cancel_mode(sptr, chptr, 'p', NULL, &count);
if ((prev_mode & MODE_MODERATED))
cancel_mode(sptr, chptr, 'm', NULL, &count);
if ((prev_mode & MODE_TOPICLIMIT))
cancel_mode(sptr, chptr, 't', NULL, &count);
if ((prev_mode & MODE_INVITEONLY))
cancel_mode(sptr, chptr, 'i', NULL, &count);
if ((prev_mode & MODE_NOPRIVMSGS))
cancel_mode(sptr, chptr, 'n', NULL, &count);
if ((prev_mode & MODE_LIMIT))
{
current_mode->limit = 0;
cancel_mode(sptr, chptr, 'l', NULL, &count);
}
if ((prev_mode & MODE_KEY))
{
*current_mode->key = 0;
cancel_mode(sptr, chptr, 'k', prev_key, &count);
}
current_mode->mode &= ~prev_mode;
/* And deop and devoice all net.riders on my side */
mode = CHFL_CHANOP;
m = 'o';
for (;;)
{
for (lp = chptr->members; lp; lp = lp->next)
{
if ((lp->flags & CHFL_BURST_JOINED))
continue; /* This is not a net.rider from
* this side of the net.junction */
if ((lp->flags & mode))
{
lp->flags &= ~mode;
if (mode == CHFL_CHANOP)
lp->flags |= CHFL_DEOPPED;
cancel_mode(sptr, chptr, m, lp->value.cptr->name, &count);
}
}
if (mode == CHFL_VOICE)
break;
mode = CHFL_VOICE;
m = 'v';
}
/* And finally wipeout all bans that are left */
for (ban = &chptr->banlist; *ban;)
{
Link *tmp = *ban;
if ((tmp->flags & CHFL_BURST_BAN_WIPEOUT))
{
cancel_mode(sptr, chptr, 'b', tmp->value.ban.banstr, &count);
/* Copied from del_banid(): */
*ban = tmp->next;
RunFree(tmp->value.ban.banstr);
RunFree(tmp->value.ban.who);
free_link(tmp);
/* Erase ban-valid-bit, for channel members that are banned */
for (tmp = chptr->members; tmp; tmp = tmp->next)
if ((tmp->flags & (CHFL_BANNED|CHFL_BANVALID)) ==
(CHFL_BANNED|CHFL_BANVALID))
tmp->flags &= ~CHFL_BANVALID; /* `tmp' == channel member */
}
else
ban = &tmp->next;
}
/* Also wipeout overlapped bans */
if (!add_banid_not_called)
{
Link *ban;
while ((ban = next_removed_overlapped_ban()))
cancel_mode(sptr, chptr, 'b', ban->value.ban.banstr, &count);
}
cancel_mode(sptr, chptr, 0, NULL, &count); /* flush */
}
if (send_it && !netride)
{
Link *bl;
int deban;
if (add_banid_not_called || !(bl = next_removed_overlapped_ban()))
{
deban = 0;
bl = chptr->banlist;
*modebuf = '+';
}
else
{
deban = 1;
*modebuf = '-';
}
mblen2 = 1;
pblen2 = 0;
cnt = 0;
for (;;)
{
size_t nblen = 0;
if (bl)
nblen = strlen(bl->value.ban.banstr);
if (cnt == 6 || (!bl && cnt) ||
pblen2 + nblen + 12 > MODEBUFLEN) /* The last check is to make sure
that the receiving 2.9 will
still process this */
{
/* Time to send buffer */
modebuf[mblen2] = 0;
sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s%s",
parv[0], chptr->chname, modebuf, parabuf);
#ifndef NO_PROTOCOL9
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s%s",
parv[0], chptr->chname, modebuf, parabuf);
#endif
*modebuf = deban ? '-' : '+';
mblen2 = 1;
pblen2 = 0;
cnt = 0;
}
if (!bl) /* Done ? */
break;
if (deban || (bl->flags & CHFL_BURST_BAN))
{
/* Add ban to buffers and remove it */
modebuf[mblen2++] = 'b';
parabuf[pblen2++] = ' ';
strcpy(parabuf + pblen2, bl->value.ban.banstr);
pblen2 += nblen;
cnt++;
bl->flags &= ~CHFL_BURST_BAN;
}
if (deban)
{
if (!(bl = next_removed_overlapped_ban()))
{
deban = 0;
modebuf[mblen2++] = '+';
bl = chptr->banlist;
}
}
else
bl = bl->next;
}
/* Flush MODE [-b]+b ...: */
if (cnt > 0 || mblen2 > 1)
{
modebuf[mblen2] = 0;
sendto_channel_butserv(chptr, sptr, ":%s MODE %s %s%s",
parv[0], chptr->chname, modebuf, parabuf);
#ifndef NO_PROTOCOL9
sendto_lowprot_butone(cptr, 9, ":%s MODE %s %s%s %lu",
parv[0], chptr->chname, modebuf, parabuf, chptr->creationtime);
#endif
}
#ifndef NO_PROTOCOL9
else if (send_it && !ts_sent)
sendto_lowprot_butone(cptr, 9, ":%s MODE %s + %lu",
parv[0], chptr->chname, chptr->creationtime);
#endif
}
return 0;
}
/*
* m_part
*
* parv[0] = sender prefix
* parv[1] = channel
*/
int m_part(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
Reg1 aChannel *chptr;
Reg2 Link *lp;
char *p = NULL, *name, pbuf[BUFSIZE];
*pbuf = '\0'; /* Initialize the part buffer... -Kev */
sptr->flags &= ~FLAGS_TS8;
if (parc < 2 || parv[1][0] == '\0')
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "PART");
return 0;
}
for (; (name = strtoken(&p, parv[1], ",")); parv[1] = NULL)
{
chptr = get_channel(sptr, name, !CREATE);
if (!chptr)
{
sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL),
me.name, parv[0], name);
continue;
}
if (*name == '&' && !MyClient(sptr))
continue;
/* Do not use IsMember here: zombies must be able to part too */
if (!(lp = find_user_link(chptr->members, sptr)))
{
/* Normal to get when our client did a kick
for a remote client (who sends back a PART),
so check for remote client or not --Run */
if (MyClient(sptr))
sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0], name);
continue;
}
/*
* Remove user from the old channel (if any)
* Basically just removing the #ifdef V28PlusOnly, and using
* an internal array to store the info. We're recreating the
* /join list for sending out to all the servers... -Kev
*/
if (*name != '&')
{
if (*pbuf)
strcat(pbuf, ",");
strcat(pbuf, name);
}
if (!(lp->flags & CHFL_ZOMBIE))
sendto_channel_butserv(chptr, sptr, PartFmt, parv[0], name);
else if (MyClient(sptr))
sendto_one(sptr, PartFmt, parv[0], name);
remove_user_from_channel(sptr, chptr);
}
if (*pbuf) /* Send out the parts to all servers... -Kev */
sendto_serv_butone(cptr, PartFmt, parv[0], pbuf);
return 0;
}
/*
* m_kick
*
* parv[0] = sender prefix
* parv[1] = channel
* parv[2] = client to kick
* parv[3] = kick comment
*/
int m_kick(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
aClient *who;
aChannel *chptr;
char *comment;
Link *lp, *lp2;
sptr->flags &= ~FLAGS_TS8;
if (parc < 3 || *parv[1] == '\0')
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "KICK");
return 0;
}
if (IsServer(sptr))
send_hack_notice(cptr, sptr, parc, parv, 1, 3);
comment = (BadPtr(parv[3])) ? parv[0] : parv[3];
if (strlen(comment) > (size_t) TOPICLEN)
comment[TOPICLEN] = '\0';
*nickbuf = *buf = '\0';
chptr = get_channel(sptr, parv[1], !CREATE);
if (!chptr)
{
sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], parv[1]);
return 0;
}
if (*parv[1] == '&' && !MyClient(sptr))
return 0;
if (!IsServer(cptr) && !is_chan_op(sptr, chptr))
{
sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
me.name, parv[0], chptr->chname);
return 0;
}
lp2 = find_user_link(chptr->members, sptr);
if (MyClient(sptr) || IsAnOper(cptr) || Protocol(cptr) < 10)
{
if (!(who = find_chasing(sptr, parv[2], NULL)))
return 0; /* No such user left! */
}
else if (!(who = FindNClient(parv[2])))
return 0; /* No such user left! */
/* if the user is +k, prevent a kick from local user */
if (IsChannelService(who) && MyClient(sptr))
{
sendto_one(sptr, err_str(ERR_ISCHANSERVICE), me.name,
parv[0], parv[2], chptr->chname);
return 0;
}
if (((lp = find_user_link(chptr->members, who)) &&
!(lp->flags & CHFL_ZOMBIE)) || IsServer(sptr))
{
if (who->from != cptr &&
((lp2 && (lp2->flags & CHFL_DEOPPED)) || (!lp2 && IsPerson(sptr))))
{
/*
* Bounce here:
* cptr must be a server (or cptr == sptr and
* sptr->flags can't have DEOPPED set
* when CHANOP is set).
*/
sendto_one(cptr, ":%s JOIN %s", who->name, parv[1]);
if (lp->flags & CHFL_CHANOP)
{
if (IsAnOper(cptr) || Protocol(cptr) < 10)
sendto_one(cptr, ":%s MODE %s +o %s %lu",
me.name, parv[1], who->name, chptr->creationtime);
else
sendto_one(cptr, "%c MODE %s +o %c%c%c %lu",
NumServ(&me), parv[1], NumNick(who), chptr->creationtime);
}
if (lp->flags & CHFL_VOICE)
{
if (Protocol(cptr) < 10)
sendto_one(cptr, ":%s MODE %s +v %s %lu",
me.name, parv[1], who->name, chptr->creationtime);
else
sendto_one(cptr, "%c MODE %s +v %c%c%c %lu",
NumServ(&me), parv[1], NumNick(who), chptr->creationtime);
}
}
else
{
if (lp)
sendto_channel_butserv(chptr, sptr,
":%s KICK %s %s :%s", parv[0], parv[1], who->name, comment);
if (!IsLocalChannel(parv[1]))
{
sendto_lowprot_butone(cptr, 9, ":%s KICK %s %s :%s",
parv[0], parv[1], who->name, comment);
sendto_highprot_butone(cptr, 10, ":%s KICK %s %c%c%c :%s",
parv[0], parv[1], NumNick(who), comment);
}
if (lp)
{
/*
* Consider:
*
* client
* |
* c
* |
* X --a--> A --b--> B --d--> D
* |
* who
*
* Where `who' is being KICK-ed by a "KICK" message received by server 'A'
* via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
*
* a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
* Remove the user immedeately when no users are left on the channel.
* b) On server B : remove the user (who/lp) from the channel, send a
* PART upstream (to A) and pass on the KICK.
* c) KICKed by `client'; On server B : remove the user (who/lp) from the
* channel, and pass on the KICK.
* d) On server D : remove the user (who/lp) from the channel, and pass on
* the KICK.
*
* Note:
* - Setting the ZOMBIE flag never hurts, we either remove the
* client after that or we don't.
* - The KICK message was already passed on, as should be in all cases.
* - `who' is removed in all cases except case a) when users are left.
* - A PART is only sent upstream in case b).
*
* 2 aug 97:
*
* 6
* |
* 1 --- 2 --- 3 --- 4 --- 5
* | |
* kicker who
*
* We also need to turn 'who' into a zombie on servers 1 and 6,
* because a KICK from 'who' (kicking someone else in that direction)
* can arrive there afterwards - which should not be bounced itself.
* Therefore case a) also applies for servers 1 and 6.
*
* --Run
*/
/* Default for case a): */
lp->flags |= CHFL_ZOMBIE;
/* Case b) or c) ?: */
if (MyClient(who)) /* server 4 */
{
if (IsServer(cptr)) /* Case b) ? */
sendto_one(cptr, PartFmt, who->name, parv[1]);
remove_user_from_channel(who, chptr);
return 0;
}
if (who->from == cptr) /* True on servers 1, 5 and 6 */
{
aClient *acptr = IsServer(sptr) ? sptr : sptr->user->server;
for (; acptr != &me; acptr = acptr->serv->up)
if (acptr == who->user->server) /* Case d) (server 5) */
{
remove_user_from_channel(who, chptr);
return 0;
}
}
/* Case a) (servers 1, 2, 3 and 6) */
for (lp = chptr->members; lp; lp = lp->next)
if (!(lp->flags & CHFL_ZOMBIE))
break;
if (!lp)
remove_user_from_channel(who, chptr);
#ifdef GODMODE
else
sendto_op_mask(SNO_HACK2, "%s is now a zombie on %s",
who->name, chptr->chname);
#endif
}
}
}
else if (MyClient(sptr))
sendto_one(sptr, err_str(ERR_USERNOTINCHANNEL),
me.name, parv[0], parv[2], parv[1]);
return 0;
}
int count_channels(aClient *sptr)
{
Reg1 aChannel *chptr;
Reg2 int count = 0;
for (chptr = channel; chptr; chptr = chptr->nextch)
count++;
return (count);
}
/*
* m_topic
*
* parv[0] = sender prefix
* parv[1] = channel
* parv[parc - 1] = topic (if parc > 2)
*/
int m_topic(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
aChannel *chptr;
char *topic = NULL, *name, *p = NULL;
if (parc < 2)
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "TOPIC");
return 0;
}
if (parc > 2)
topic = parv[parc - 1];
for (; (name = strtoken(&p, parv[1], ",")); parv[1] = NULL)
{
if (IsModelessChannel(name))
{
sendto_one(sptr, err_str(ERR_MODELESS), me.name, parv[0], name);
continue;
}
chptr = NULL;
if (!IsChannelName(name) || !(chptr = find_channel(name, NullChn)) ||
((topic || SecretChannel(chptr)) && !IsMember(sptr, chptr)))
{
sendto_one(sptr, err_str(chptr ? ERR_NOTONCHANNEL : ERR_NOSUCHCHANNEL),
me.name, parv[0], name);
continue;
}
if (*name == '&' && !MyClient(sptr))
continue;
if (!topic) /* only asking for topic */
{
if (chptr->topic[0] == '\0')
sendto_one(sptr, rpl_str(RPL_NOTOPIC), me.name, parv[0], chptr->chname);
else
{
sendto_one(sptr, rpl_str(RPL_TOPIC),
me.name, parv[0], chptr->chname, chptr->topic);
sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME),
me.name, parv[0], chptr->chname,
chptr->topic_nick, chptr->topic_time);
}
}
else if (((chptr->mode.mode & MODE_TOPICLIMIT) == 0 ||
is_chan_op(sptr, chptr)) && topic)
{
/* setting a topic */
strncpy(chptr->topic, topic, TOPICLEN);
strncpy(chptr->topic_nick, sptr->name, NICKLEN);
chptr->topic_time = now;
sendto_match_servs(chptr, cptr, ":%s TOPIC %s :%s",
parv[0], chptr->chname, chptr->topic);
sendto_channel_butserv(chptr, sptr, ":%s TOPIC %s :%s",
parv[0], chptr->chname, chptr->topic);
}
else
sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
me.name, parv[0], chptr->chname);
}
return 0;
}
/*
* m_invite
* parv[0] - sender prefix
* parv[1] - user to invite
* parv[2] - channel name
*
* - INVITE now is accepted only if who does it is chanop (this of course
* implies that channel must exist and he must be on it).
*
* - On the other side it IS processed even if channel is NOT invite only
* leaving room for other enhancements like inviting banned ppl. -- Nemesi
*
*/
int m_invite(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
aClient *acptr;
aChannel *chptr;
if (parc < 3 || *parv[2] == '\0')
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name, parv[0], "INVITE");
return 0;
}
if (!(acptr = find_person(parv[1], (aClient *)NULL)))
{
sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], parv[1]);
return 0;
}
if (is_silenced(sptr, acptr))
return 0;
clean_channelname(parv[2]);
if (*parv[2] == '0' || !IsChannelName(parv[2]) ||
(IsLocalChannel(parv[2]) && !MyClient(sptr)))
return 0;
if (IsModelessChannel(parv[2]))
{
sendto_one(sptr, err_str(ERR_MODELESS), me.name, parv[0], parv[2]);
return 0;
}
if (!(chptr = find_channel(parv[2], NullChn)))
{
if (check_target_limit(sptr, acptr, acptr->name))
return 0;
if (MyConnect(sptr))
{
sendto_one(sptr, rpl_str(RPL_INVITING), me.name, parv[0],
acptr->name, parv[2]);
if (acptr->user->away)
sendto_one(sptr, rpl_str(RPL_AWAY), me.name, parv[0],
acptr->name, acptr->user->away);
}
sendto_prefix_one(acptr, sptr, ":%s INVITE %s :%s", parv[0],
acptr->name, parv[2]);
return 0;
}
if (!IsMember(sptr, chptr))
{
sendto_one(sptr, err_str(ERR_NOTONCHANNEL), me.name, parv[0], parv[2]);
return 0;
}
if (IsMember(acptr, chptr))
{
sendto_one(sptr, err_str(ERR_USERONCHANNEL),
me.name, parv[0], parv[1], parv[2]);
return 0;
}
if (MyConnect(sptr))
{
if (!is_chan_op(sptr, chptr))
{
sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
me.name, parv[0], chptr->chname);
return 0;
}
/* If we get here, it was a VALID and meaningful INVITE */
if (check_target_limit(sptr, acptr, acptr->name))
return 0;
sendto_one(sptr, rpl_str(RPL_INVITING), me.name, parv[0],
acptr->name, chptr->chname);
if (acptr->user->away)
sendto_one(sptr, rpl_str(RPL_AWAY), me.name, parv[0],
acptr->name, acptr->user->away);
}
if (MyConnect(acptr))
add_invite(acptr, chptr);
sendto_prefix_one(acptr, sptr, ":%s INVITE %s :%s", parv[0],
acptr->name, chptr->chname);
return 0;
}
static int number_of_zombies(aChannel *chptr)
{
Reg1 Link *lp;
Reg2 int count = 0;
for (lp = chptr->members; lp; lp = lp->next)
if (lp->flags & CHFL_ZOMBIE)
count++;
return count;
}
/*
* m_list
*
* parv[0] = sender prefix
* parv[1] = channel list or user/time limit
* parv[2...] = more user/time limits
*/
int m_list(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
aChannel *chptr;
char *name, *p = NULL;
int show_usage = 0, show_channels = 0, param;
aListingArgs args = {
-1, /* max_time */
0, /* min_time */
-1, /* max_users */
0, /* min_users */
0, /* topic_limits */
-1, /* max_topic_time */
0, /* min_topic_time */
NULL /* chptr */
};
if (parc < 2) /* No arguments given to /LIST ? */
{
#ifdef DEFAULT_LIST_PARAM
static char *defparv[MAXPARA + 1];
static int defparc = 0;
static char lp[] = DEFAULT_LIST_PARAM;
int i;
if (!defparc)
{
char *s = lp, *t;
defparc = 1;
defparv[defparc++] = t = strtok(s, " ");
while (t && defparc < MAXPARA)
{
if ((t = strtok(NULL, " ")))
defparv[defparc++] = t;
}
}
for (i = 1; i < defparc; i++)
parv[i] = defparv[i];
parv[i] = NULL;
parc = defparc;
#else
if (sptr->listing) /* Already listing ? */
{
sptr->listing->chptr->mode.mode &= ~MODE_LISTED;
RunFree(sptr->listing);
sptr->listing = NULL;
sendto_one(sptr, rpl_str(RPL_LISTEND), me.name, sptr->name);
return 0; /* Let LIST abort a listing. --Ensor */
}
#endif /* DEFAULT_LIST_PARAM */
}
/* Decode command */
for (param = 1; !show_usage && parv[param]; param++)
{
char *p = parv[param];
do
{
int is_time = 0;
switch (*p)
{
case 'T':
case 't':
is_time++;
args.topic_limits = 1;
/* Fall through */
case 'C':
case 'c':
is_time++;
p++;
if (*p != '<' && *p != '>')
{
show_usage = 1;
break;
}
/* Fall through */
case '<':
case '>':
{
p++;
if (!isdigit(*p))
show_usage = 1;
else
{
if (is_time)
{
time_t val = atoi(p);
if (p[-1] == '<')
{
if (val < 80000000) /* Toggle UTC/offset */
{
/*
* Demands that
* 'TStime() - chptr->creationtime < val * 60'
* Which equals
* 'chptr->creationtime > TStime() - val * 60'
*/
if (is_time == 1)
args.min_time = TStime() - val * 60;
else
args.min_topic_time = TStime() - val * 60;
}
else if (is_time == 1) /* Creation time in UTC was entered */
args.max_time = val;
else /* Topic time in UTC was entered */
args.max_topic_time = val;
}
else if (val < 80000000)
{
if (is_time == 1)
args.max_time = TStime() - val * 60;
else
args.max_topic_time = TStime() - val * 60;
}
else if (is_time == 1)
args.min_time = val;
else
args.min_topic_time = val;
}
else if (p[-1] == '<')
args.max_users = atoi(p);
else
args.min_users = atoi(p);
if ((p = strchr(p, ',')))
p++;
}
break;
}
default:
if (!IsChannelName(p))
{
show_usage = 1;
break;
}
if (parc != 2) /* Don't allow a mixture of channels with <,> */
show_usage = 1;
show_channels = 1;
p = NULL;
break;
}
}
while (!show_usage && p); /* p points after comma, or is NULL */
}
if (show_usage)
{
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
"Usage: \002/QUOTE LIST\002 \037parameters\037");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
"Where \037parameters\037 is a space or comma seperated "
"list of one or more of:");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002<\002\037max_users\037 ; Show all channels with less "
"than \037max_users\037.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002>\002\037min_users\037 ; Show all channels with more "
"than \037min_users\037.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002C<\002\037max_minutes\037 ; Channels that exist less "
"than \037max_minutes\037.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002C>\002\037min_minutes\037 ; Channels that exist more "
"than \037min_minutes\037.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002T<\002\037max_minutes\037 ; Channels with a topic last "
"set less than \037max_minutes\037 ago.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
" \002T>\002\037min_minutes\037 ; Channels with a topic last "
"set more than \037min_minutes\037 ago.");
sendto_one(sptr, rpl_str(RPL_LISTUSAGE), me.name, parv[0],
"Example: LIST <3,>1,C<10,T>0 ; 2 users, younger than 10 min., "
"topic set.");
return 0;
}
sendto_one(sptr, rpl_str(RPL_LISTSTART), me.name, parv[0]);
if (!show_channels)
{
if (args.max_users > args.min_users + 1 && args.max_time > args.min_time &&
args.max_topic_time > args.min_topic_time) /* Sanity check */
{
if ((sptr->listing = (aListingArgs *)RunMalloc(sizeof(aListingArgs))))
{
memcpy(sptr->listing, &args, sizeof(aListingArgs));
if ((sptr->listing->chptr = channel))
{
int m = channel->mode.mode & MODE_LISTED;
list_next_channels(sptr, 64);
channel->mode.mode |= m;
return 0;
}
RunFree(sptr->listing);
sptr->listing = NULL;
}
}
sendto_one(sptr, rpl_str(RPL_LISTEND), me.name, parv[0]);
return 0;
}
for (; (name = strtoken(&p, parv[1], ",")); parv[1] = NULL)
{
chptr = find_channel(name, NullChn);
if (chptr && ShowChannel(sptr, chptr) && sptr->user)
sendto_one(sptr, rpl_str(RPL_LIST), me.name, parv[0],
ShowChannel(sptr, chptr) ? name : "*",
chptr->users - number_of_zombies(chptr), chptr->topic);
}
sendto_one(sptr, rpl_str(RPL_LISTEND), me.name, parv[0]);
return 0;
}
/*
* m_names - Added by Jto 27 Apr 1989
*
* parv[0] = sender prefix
* parv[1] = channel
*/
int m_names(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
Reg1 aChannel *chptr;
Reg2 aClient *c2ptr;
Reg3 Link *lp;
aChannel *ch2ptr = NULL;
int idx, flag, len, mlen;
char *s, *para = parc > 1 ? parv[1] : NULL;
if (parc > 2 && hunt_server(1, cptr, sptr, ":%s NAMES %s %s", 2, parc, parv))
return 0;
mlen = strlen(me.name) + 10 + strlen(sptr->name);
if (!BadPtr(para))
{
s = strchr(para, ',');
if (s)
{
parv[1] = ++s;
m_names(cptr, sptr, parc, parv);
}
clean_channelname(para);
ch2ptr = find_channel(para, (aChannel *)NULL);
}
/*
* First, do all visible channels (public and the one user self is)
*/
for (chptr = channel; chptr; chptr = chptr->nextch)
{
if ((chptr != ch2ptr) && !BadPtr(para))
continue; /* -- wanted a specific channel */
if (!MyConnect(sptr) && BadPtr(para))
continue;
#ifndef GODMODE
if (!ShowChannel(sptr, chptr))
continue; /* -- users on this are not listed */
#endif
/* Find users on same channel (defined by chptr) */
strcpy(buf, "* ");
len = strlen(chptr->chname);
strcpy(buf + 2, chptr->chname);
strcpy(buf + 2 + len, " :");
if (PubChannel(chptr))
*buf = '=';
else if (SecretChannel(chptr))
*buf = '@';
idx = len + 4;
flag = 1;
for (lp = chptr->members; lp; lp = lp->next)
{
c2ptr = lp->value.cptr;
#ifndef GODMODE
if (sptr != c2ptr && IsInvisible(c2ptr) && !IsMember(sptr, chptr))
continue;
#endif
if (lp->flags & CHFL_ZOMBIE)
{
if (lp->value.cptr != sptr)
continue;
else
{
strcat(buf, "!");
idx++;
}
}
else if (lp->flags & CHFL_CHANOP)
{
strcat(buf, "@");
idx++;
}
else if (lp->flags & CHFL_VOICE)
{
strcat(buf, "+");
idx++;
}
strcat(buf, c2ptr->name);
strcat(buf, " ");
idx += strlen(c2ptr->name) + 1;
flag = 1;
#ifdef GODMODE
{
char yxx[4];
sprintf_irc(yxx, "%c%c%c", NumNick(c2ptr));
if (c2ptr != FindNClient(yxx))
MyCoreDump;
sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
idx += 6;
}
if (mlen + idx + NICKLEN + 11 > BUFSIZE)
#else
if (mlen + idx + NICKLEN + 5 > BUFSIZE)
#endif
/* space, modifier, nick, \r \n \0 */
{
sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf);
strcpy(buf, "* ");
strncpy(buf + 2, chptr->chname, len + 1);
buf[len + 2] = 0;
strcat(buf, " :");
if (PubChannel(chptr))
*buf = '=';
else if (SecretChannel(chptr))
*buf = '@';
idx = len + 4;
flag = 0;
}
}
if (flag)
sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf);
}
if (!BadPtr(para))
{
sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], para);
return (1);
}
/* Second, do all non-public, non-secret channels in one big sweep */
strcpy(buf, "* * :");
idx = 5;
flag = 0;
for (c2ptr = client; c2ptr; c2ptr = c2ptr->next)
{
aChannel *ch3ptr;
int showflag = 0, secret = 0;
#ifndef GODMODE
if (!IsPerson(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
#else
if (!IsPerson(c2ptr))
#endif
continue;
lp = c2ptr->user->channel;
/*
* Don't show a client if they are on a secret channel or when
* they are on a channel sptr is on since they have already
* been show earlier. -avalon
*/
while (lp)
{
ch3ptr = lp->value.chptr;
#ifndef GODMODE
if (PubChannel(ch3ptr) || IsMember(sptr, ch3ptr))
#endif
showflag = 1;
if (SecretChannel(ch3ptr))
secret = 1;
lp = lp->next;
}
if (showflag) /* Have we already shown them ? */
continue;
#ifndef GODMODE
if (secret) /* On any secret channels ? */
continue;
#endif
strcat(buf, c2ptr->name);
strcat(buf, " ");
idx += strlen(c2ptr->name) + 1;
flag = 1;
#ifdef GODMODE
{
char yxx[4];
sprintf_irc(yxx, "%c%c%c", NumNick(c2ptr));
if (c2ptr != FindNClient(yxx))
MyCoreDump;
sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
idx += 6;
}
#endif
#ifdef GODMODE
if (mlen + idx + NICKLEN + 9 > BUFSIZE)
#else
if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
#endif
{
sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf);
strcpy(buf, "* * :");
idx = 5;
flag = 0;
}
}
if (flag)
sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf);
sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*");
return (1);
}
void send_user_joins(aClient *cptr, aClient *user)
{
Reg1 Link *lp;
Reg2 aChannel *chptr;
Reg3 int cnt = 0, len = 0, clen;
char *mask;
*buf = ':';
strcpy(buf + 1, user->name);
strcat(buf, " JOIN ");
len = strlen(user->name) + 7;
for (lp = user->user->channel; lp; lp = lp->next)
{
chptr = lp->value.chptr;
if ((mask = strchr(chptr->chname, ':')))
if (match(++mask, cptr->name))
continue;
if (*chptr->chname == '&')
continue;
if (is_zombie(user, chptr))
continue;
clen = strlen(chptr->chname);
if (clen + 1 + len > BUFSIZE - 3)
{
if (cnt)
{
buf[len - 1] = '\0';
sendto_one(cptr, "%s", buf);
}
*buf = ':';
strcpy(buf + 1, user->name);
strcat(buf, " JOIN ");
len = strlen(user->name) + 7;
cnt = 0;
}
strcpy(buf + len, chptr->chname);
cnt++;
len += clen;
if (lp->next)
{
len++;
strcat(buf, ",");
}
}
if (*buf && cnt)
sendto_one(cptr, "%s", buf);
return;
}
/*
* send_hack_notice()
*
* parc & parv[] are the same as that of the calling function:
* mtype == 1 is from m_mode, 2 is from m_create, 3 is from m_kick.
*
* This function prepares sendbuf with the server notices and wallops
* to be sent for all hacks. -Ghostwolf 18-May-97
*/
static void send_hack_notice(aClient *cptr, aClient *sptr, int parc,
char *parv[], int badop, int mtype)
{
aChannel *chptr;
static char params[MODEBUFLEN];
int i = 3;
chptr = find_channel(parv[1], NullChn);
*params = '\0';
if (IsAnOper(cptr) || Protocol(cptr) < 10)
/* We don't get numeric nicks from P09 */
{ /* servers, so this can be sent "As Is" */
if (mtype == 1)
{
while (i < parc)
{
strcat(params, " ");
strcat(params, parv[i++]);
}
sprintf_irc(sendbuf,
":%s NOTICE * :*** Notice -- %sOPCOM %s MODE %s %s%s",
me.name, (badop == 3) ? "bounce or " : "",
parv[0], parv[1], parv[2], params);
sendbufto_op_mask((badop == 3) ? SNO_HACK3 :
(badop == 4) ? SNO_HACK4 : SNO_HACK2);
if ((IsServer(sptr)) && (badop == 2))
{
sprintf_irc(sendbuf, ":%s WALLOPS :HACK: %s MODE %s %s%s",
me.name, parv[0], parv[1], parv[2], params);
sendbufto_serv_butone(cptr);
}
}
else if (mtype == 3)
{
sprintf_irc(sendbuf,
":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
me.name, sptr->name, parv[1], parv[2], parv[3]);
sendbufto_op_mask(SNO_HACK4);
}
}
else /* P10 servers require numeric nick conversion before sending. */
{
switch(mtype)
{
case 1: /* Convert nicks for MODE HACKs here */
{
char *mode = parv[2];
while(i < parc)
{
while (*mode && *mode != 'o' && *mode != 'v')
++mode;
strcat(params, " ");
if (*mode == 'o' || *mode == 'v')
{
register aClient *acptr;
if((acptr = FindNClient(parv[i])) != NULL) /* Convert nicks here */
strcat(params, acptr->name);
else
{
strcat(params, "<");
strcat(params, parv[i]);
strcat(params, ">");
}
}
else /* If it isn't a numnick, send it 'as is' */
strcat(params, parv[i]);
i++;
}
sprintf_irc(sendbuf,
":%s NOTICE * :*** Notice -- %sHACK(%d): %s MODE %s %s%s [%lu]",
me.name, (badop == 3) ? "BOUNCE or " : "",
badop, parv[0], parv[1], parv[2], params, chptr->creationtime);
sendbufto_op_mask((badop == 3) ? SNO_HACK3 :
(badop == 4) ? SNO_HACK4 : SNO_HACK2);
if ((IsServer(sptr)) && (badop == 2))
{
sprintf_irc(sendbuf, ":%s WALLOPS :HACK: %s MODE %s %s%s",
me.name, parv[0], parv[1], parv[2], params);
sendbufto_serv_butone(cptr);
}
break;
}
case 2: /* No conversion is needed for CREATE; the only numnick is sptr */
{
sendto_serv_butone(cptr, ":%s WALLOPS :HACK: %s CREATE %s %s",
me.name, sptr->name, chptr->chname, parv[2]);
sendto_op_mask(SNO_HACK2, "HACK(2): %s CREATE %s %s",
sptr->name, chptr->chname, parv[2]);
break;
}
case 3: /* Convert nick in KICK message */
{
aClient *acptr;
if((acptr = FindNClient(parv[2])) != NULL) /* attempt to convert nick */
sprintf_irc(sendbuf,
":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
me.name, sptr->name, parv[1], acptr->name, parv[3]);
else /* if conversion fails, send it 'as is' in <>'s */
sprintf_irc(sendbuf,
":%s NOTICE * :*** Notice -- HACK: %s KICK %s <%s> :%s",
me.name, sptr->name, parv[1], parv[2], parv[3]);
sendbufto_op_mask(SNO_HACK4);
break;
}
}
}
}
|