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
|
/*
* commands.c -- Stuff needed to execute commands in ircII.
* Includes the bulk of the built in commands for ircII.
*
* Copyright (c) 1990 Michael Sandroff.
* Copyright (c) 1991, 1992 Troy Rollo.
* Copyright (c) 1992-1996 Matthew Green.
* Copyright 1995, 2015 EPIC Software Labs
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notices, the above paragraph (the one permitting redistribution),
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define __need_term_h__
#define __need_putchar_x__
#define __need_term_flush__
#include "irc.h"
#define __need_ArgList_t__
#include "alias.h"
#include "alist.h"
#include "list.h"
#include "sedcrypt.h"
#include "ctcp.h"
#include "dcc.h"
#include "commands.h"
#include "exec.h"
#include "files.h"
#include "hook.h"
#include "server.h"
#include "ifcmd.h"
#include "ignore.h"
#include "input.h"
#include "ircaux.h"
#include "keys.h"
#include "lastlog.h"
#include "log.h"
#include "names.h"
#include "notify.h"
#include "numbers.h"
#include "output.h"
#include "parse.h"
#include "queue.h"
#include "screen.h"
#include "status.h"
#include "stack.h"
#include "termx.h"
#include "timer.h"
#include "vars.h"
#include "window.h"
#include "who.h"
#include "newio.h"
#include "words.h"
#include "reg.h"
#include "extlang.h"
#include "elf.h"
/* used with input_move_cursor */
#define RIGHT 1
#define LEFT 0
/* The maximum number of recursive LOAD levels allowed */
#define MAX_LOAD_DEPTH 10
/* flags used by e_away */
#define AWAY_ONE 0
#define AWAY_ALL 1
/* Used to handle and catch breaks and continues */
int will_catch_break_exceptions = 0;
int will_catch_continue_exceptions = 0;
int will_catch_return_exceptions = 0;
int break_exception = 0;
int continue_exception = 0;
int return_exception = 0;
volatile sig_atomic_t system_exception = 0;
/* commands and whatnot */
static void abortcmd (const char *, char *, const char *);
static void away (const char *, char *, const char *);
static void beepcmd (const char *, char *, const char *);
static void blesscmd (const char *, char *, const char *);
static void botmodecmd (const char *, char *, const char *);
static void breakcmd (const char *, char *, const char *);
static void commentcmd (const char *, char *, const char *);
static void continuecmd (const char *, char *, const char *);
static void ctcp (const char *, char *, const char *);
void debuglogcmd (const char *, char *, const char *);
static void deop (const char *, char *, const char *);
static void send_to_channel_first (const char *, char *, const char *);
static void send_to_query_first (const char *, char *, const char *);
static void sendlinecmd (const char *, char *, const char *);
static void echocmd (const char *, char *, const char *);
static void funny_stuff (const char *, char *, const char *);
static void cd (const char *, char *, const char *);
static void defercmd (const char *, char *, const char *);
static void describe (const char *, char *, const char *);
static void e_call (const char *, char *, const char *);
static void e_clear (const char *, char *, const char *);
static void e_hostname (const char *, char *, const char *);
static void e_nick (const char *, char *, const char *);
static void e_privmsg (const char *, char *, const char *);
static void e_pause (const char *, char *, const char *);
static void e_quit (const char *, char *, const char *);
static void e_topic (const char *, char *, const char *);
static void e_wallop (const char *, char *, const char *);
static void evalcmd (const char *, char *, const char *);
static void flush (const char *, char *, const char *);
static void hookcmd (const char *, char *, const char *);
static void info (const char *, char *, const char *);
static void inputcmd (const char *, char *, const char *);
static void license (const char *, char *, const char *);
static void mecmd (const char *, char *, const char *);
static void oper (const char *, char *, const char *);
static void packagecmd (const char *, char *, const char *);
static void pingcmd (const char *, char *, const char *);
static void pop_cmd (const char *, char *, const char *);
static void pretend_cmd (const char *, char *, const char *);
static void push_cmd (const char *, char *, const char *);
static void query (const char *, char *, const char *);
static void quotecmd (const char *, char *, const char *);
static void redirect (const char *, char *, const char *);
static void returncmd (const char *, char *, const char *);
extern void rubycmd (const char *, char *, const char *);
static void send_2comm (const char *, char *, const char *);
static void send_comm (const char *, char *, const char *);
static void send_invite (const char *, char *, const char *);
static void send_kick (const char *, char *, const char *);
static void send_channel_com (const char *, char *, const char *);
static void setenvcmd (const char *, char *, const char *);
static void squitcmd (const char *, char *, const char *);
static void subpackagecmd (const char *, char *, const char *);
static void typecmd (const char *, char *, const char *);
static void usleepcmd (const char *, char *, const char *);
static void shift_cmd (const char *, char *, const char *);
static void sleepcmd (const char *, char *, const char *);
static void stackcmd (const char *, char *, const char *);
static void unshift_cmd (const char *, char *, const char *);
static void version (const char *, char *, const char *);
static void waitcmd (const char *, char *, const char *);
static void whois (const char *, char *, const char *);
static void xechocmd (const char *, char *, const char *);
static void xevalcmd (const char *, char *, const char *);
static void xtypecmd (const char *, char *, const char *);
static void allocdumpcmd (const char *, char *, const char *);
/* other */
static void eval_inputlist (char *, const char *);
static void parse_block (const char *, const char *, int interactive);
/* I hate typedefs, but they sure can be useful.. */
typedef void (*CmdFunc) (const char *, char *, const char *);
/* IrcCommand: structure for each command in the command table */
typedef struct
{
const char * name; /* what the user types */
CmdFunc func; /* function that is the command */
} IrcCommand;
/* Current command name, needed for $curcmd() */
const char *current_command = NULL;
/*
* irc_command: all the availble irc commands: Note that the first entry has
* a zero length string name and a null server command... this little trick
* makes "/ blah blah blah" to always send the arguments to a channel,
* bypassing queries, etc. Neato. This list MUST be sorted.
*/
static IrcCommand irc_command[] =
{
{ "", send_to_channel_first },
{ "#", commentcmd },
{ ":", commentcmd },
{ "ABORT", abortcmd },
{ "ADMIN", send_comm },
{ "ALIAS", aliascmd }, /* alias.c */
{ "ASSIGN", assigncmd }, /* alias.c */
{ "AWAY", away },
{ "BEEP", beepcmd },
{ "BIND", bindcmd }, /* keys.c */
{ "BLESS", blesscmd },
{ "BOTMODE", botmodecmd },
{ "BREAK", breakcmd },
{ "CALL", e_call },
{ "CD", cd },
{ "CHANNEL", e_channel },
{ "CLEAR", e_clear },
{ "COMMENT", commentcmd },
{ "CONNECT", send_comm },
{ "CONTINUE", continuecmd },
{ "CTCP", ctcp },
{ "DCC", dcc_cmd }, /* dcc.c */
{ "DEBUGLOG", debuglogcmd }, /* debuglog.c */
{ "DEFER", defercmd },
{ "DEOP", deop },
{ "DESCRIBE", describe },
{ "DIE", send_comm },
{ "DISCONNECT", disconnectcmd }, /* server.c */
{ "DO", docmd }, /* if.c */
{ "DUMP", dumpcmd }, /* alias.c */
{ "ECHO", echocmd },
{ "ENCODING", encoding }, /* recode.c */
{ "ENCRYPT", encrypt_cmd }, /* crypt.c */
{ "EVAL", evalcmd },
{ "EXEC", execcmd }, /* exec.c */
{ "EXIT", e_quit },
{ "FE", fe }, /* if.c */
{ "FEC", fe }, /* if.c */
{ "FLUSH", flush },
{ "FOR", forcmd }, /* if.c */
{ "FOREACH", foreach }, /* if.c */
{ "HOOK", hookcmd },
{ "HOSTNAME", e_hostname },
{ "IF", ifcmd }, /* if.c */
{ "IGNORE", ignore }, /* ignore.c */
{ "INFO", info },
{ "INPUT", inputcmd },
{ "INPUT_CHAR", inputcmd },
{ "INVITE", send_invite },
{ "ISON", isoncmd },
{ "JOIN", e_channel },
{ "KICK", send_kick },
{ "KILL", send_2comm },
{ "KNOCK", send_channel_com},
{ "LASTLOG", lastlog }, /* lastlog.c */
{ "LICENSE", license },
{ "LINKS", send_comm },
{ "LIST", funny_stuff },
{ "LOAD", load },
{ "LOCAL", localcmd }, /* alias.c */
{ "LOG", logcmd }, /* logfiles.c */
{ "LUSERS", send_comm },
{ "MAP", send_comm },
{ "ME", mecmd },
{ "MESG", extern_write },
{ "MODE", send_channel_com},
{ "MOTD", send_comm },
{ "MSG", e_privmsg },
{ "NAMES", funny_stuff },
{ "NICK", e_nick },
{ "NOTE", send_comm },
{ "NOTICE", e_privmsg },
{ "NOTIFY", notify }, /* notify.c */
{ "ON", oncmd }, /* hook.c */
{ "OPER", oper },
{ "PACKAGE", packagecmd },
{ "PARSEKEY", parsekeycmd },
{ "PART", send_2comm },
{ "PAUSE", e_pause },
#ifdef HAVE_PERL
{ "PERL", perlcmd }, /* perl.c */
#endif
{ "PING", pingcmd },
{ "POP", pop_cmd },
{ "PRETEND", pretend_cmd },
{ "PUSH", push_cmd },
#ifdef HAVE_PYTHON
{ "PYDIRECT", pydirect_cmd }, /* python.c */
{ "PYTHON", pythoncmd }, /* python.c */
#endif
{ "QUERY", query },
{ "QUEUE", queuecmd }, /* queue.c */
{ "QUIT", e_quit },
{ "QUOTE", quotecmd },
{ "RBIND", rbindcmd }, /* keys.c */
{ "RECONNECT", reconnectcmd }, /* server.c */
{ "REDIRECT", redirect },
{ "REHASH", send_comm },
{ "REPEAT", repeatcmd },
{ "RESTART", send_comm },
{ "RETURN", returncmd },
{ "RPING", send_comm },
#ifdef HAVE_RUBY
{ "RUBY", rubycmd }, /* ruby.c */
#endif
{ "SAY", send_to_channel_first },
{ "SEND", send_to_query_first },
{ "SENDLINE", sendlinecmd },
{ "SERVER", servercmd }, /* server.c */
{ "SERVLIST", send_comm },
{ "SET", setcmd }, /* vars.c */
{ "SETENV", setenvcmd },
{ "SHIFT", shift_cmd },
{ "SHOOK", shookcmd },
{ "SILENCE", send_comm },
{ "SLEEP", sleepcmd },
{ "SQUERY", send_2comm },
{ "SQUIT", squitcmd },
{ "STACK", stackcmd }, /* stack.c */
{ "STATS", send_comm },
{ "STUB", stubcmd }, /* alias.c */
{ "SUBPACKAGE", subpackagecmd },
{ "SWITCH", switchcmd }, /* if.c */
{ "TIME", send_comm },
{ "TIMER", timercmd },
{ "TOPIC", e_topic },
{ "TRACE", send_comm },
{ "TYPE", typecmd }, /* keys.c */
{ "UNCLEAR", e_clear },
{ "UNLESS", ifcmd }, /* if.c */
{ "UNLOAD", unloadcmd }, /* alias.c */
{ "UNSHIFT", unshift_cmd },
{ "UNTIL", whilecmd },
{ "UPING", send_comm },
{ "USERHOST", userhostcmd },
{ "USERIP", useripcmd },
{ "USLEEP", usleepcmd },
{ "USRIP", usripcmd },
{ "VERSION", version },
{ "WAIT", waitcmd },
{ "WALLCHOPS", send_2comm },
{ "WALLOPS", e_wallop },
{ "WHICH", load },
{ "WHILE", whilecmd }, /* if.c */
{ "WHO", whocmd }, /* who.c */
{ "WHOIS", whois },
{ "WHOWAS", whois },
{ "WINDOW", windowcmd }, /* window.c */
{ "XDEBUG", xdebugcmd }, /* debug.c */
{ "XECHO", xechocmd },
{ "XEVAL", xevalcmd },
{ "XQUOTE", quotecmd },
{ "XTYPE", xtypecmd },
{ NULL, commentcmd }
};
void init_commands (void)
{
int i;
for (i = 0; irc_command[i].name; i++)
add_builtin_cmd_alias(irc_command[i].name, irc_command[i].func);
}
/*
* Full scale abort.
*/
BUILT_IN_COMMAND(abortcmd)
{
abort();
}
/*
* away: the /AWAY command. Keeps track of the away message locally, and
* sends the command on to the server.
*/
BUILT_IN_COMMAND(away)
{
char *arg = NULL;
int flag = AWAY_ONE;
int i;
if (*args)
{
if ((*args == '-') || (*args == '/'))
{
arg = args;
while (*arg && !isspace(*arg))
arg++;
if (*arg)
*arg++ = 0;
if (0 == my_strnicmp(args+1, "ALL", 1)) /* all */
{
flag = AWAY_ALL;
args = arg;
}
else if (0 == my_strnicmp(args+1, "ONE", 1)) /* one */
{
flag = AWAY_ONE;
args = arg;
}
else if (0 == my_strnicmp(args+1, "-", 1)) /* stop */
args = arg;
else
{
say("AWAY: %s unknown flag", args);
return;
}
}
}
if (flag == AWAY_ALL)
{
for (i = 0; i < server_list_size(); i++)
if (is_server_valid(i))
set_server_away_message(i, args);
}
else
set_server_away_message(from_server, args);
update_all_status();
}
BUILT_IN_COMMAND(blesscmd)
{
bless_local_stack();
}
BUILT_IN_COMMAND(beepcmd)
{
term_beep();
}
BUILT_IN_COMMAND(cd)
{
char *arg;
Filename dir;
/* Hrm. Is it worse to do new_next_arg than next_arg? */
if ((arg = new_next_arg(args, &args)) != NULL)
{
if (normalize_filename(arg, dir))
say("CD: %s contains an invalid directory", arg);
else if (chdir(dir))
say("CD: %s", strerror(errno));
}
if ((getcwd(dir, sizeof(dir))))
say("Current directory: %s", dir);
else
say("The current directory is invalid.");
}
BUILT_IN_COMMAND(e_call)
{
dump_call_stack();
}
/* clear: the CLEAR command. Figure it out */
BUILT_IN_COMMAND(e_clear)
{
char *arg;
int all = 0,
visible = 0,
hidden = 0;
int clear = !strcmp(command, "CLEAR");
int force = 1;
while ((arg = next_arg(args, &args)) != NULL)
{
/* -ALL and ALL here becuase the help files used to be wrong */
if (!my_strnicmp(arg, "ALL", 1) || !my_strnicmp(arg+1, "ALL", 1))
visible = 0, hidden = 0, all = 1;
else if (!my_strnicmp(arg+1, "VISIBLE", 1))
visible = 1, hidden = 0, all = 1;
else if (!my_strnicmp(arg+1, "HIDDEN", 1))
visible = 0, hidden = 1, all = 1;
else if (!my_strnicmp(arg+1, "NOFORCE", 1))
force = 0;
else
say("Unknown flag: %s", arg);
}
if (all)
{
if (clear)
clear_all_windows(visible, hidden);
else
unclear_all_windows(visible, hidden, force);
}
else
{
if (clear)
clear_window_by_refnum(0);
else
unclear_window_by_refnum(0, force);
}
update_all_windows();
}
/* comment: does the /COMMENT command, useful in .ircrc */
BUILT_IN_COMMAND(commentcmd)
{
/* nothing to do... */
}
BUILT_IN_COMMAND(ctcp)
{
const char *to;
char * stag;
int request;
if ((to = next_arg(args, &args)) != NULL)
{
if (!strcmp(to, "*"))
if ((to = get_window_echannel(0)) == NULL)
to = zero;
if ((stag = next_arg(args, &args)) != NULL)
upper(stag);
else
stag = LOCAL_COPY("VERSION");
if (get_server_doing_notice(from_server) > 0)
{
say("You may not use the CTCP command from a NOTICE!");
return;
}
else if (get_server_doing_privmsg(from_server))
request = 0; /* XXX What about dcc chat? */
else
request = 1;
if (args && *args)
send_ctcp(request, to, stag, "%s", args);
else
send_ctcp(request, to, stag, NULL);
}
else
say("Usage: /CTCP <[=]nick|channel|*> [<request>]");
}
#include "debuglog.c"
struct defer {
char * cmds;
int servref;
char * subargs;
};
typedef struct defer Defer;
static Defer * defer_list = NULL;
static int defer_list_size = -1;
int need_defered_commands = 0;
void do_defered_commands (void)
{
int i;
if (defer_list)
{
int old_from_server = from_server;
int old_window = get_server_current_window(from_server);
for (i = 0; defer_list[i].cmds; i++)
{
from_server = defer_list[i].servref;
make_window_current_by_refnum(get_server_current_window(from_server));
call_lambda_command("deferred", defer_list[i].cmds,
defer_list[i].subargs);
new_free(&defer_list[i].cmds);
new_free(&defer_list[i].subargs);
}
from_server = old_from_server;
make_window_current_by_refnum(old_window);
}
defer_list_size = 1;
RESIZE(defer_list, Defer, defer_list_size);
defer_list[0].cmds = NULL;
defer_list[0].subargs = NULL;
need_defered_commands = 0;
}
BUILT_IN_COMMAND(defercmd)
{
/* Bootstrap the defer list */
if (defer_list_size <= 0)
{
defer_list_size = 1;
RESIZE(defer_list, Defer, defer_list_size);
defer_list[0].cmds = NULL;
defer_list[0].subargs = NULL;
}
defer_list_size++;
RESIZE(defer_list, Defer, defer_list_size);
defer_list[defer_list_size - 2].cmds = malloc_strdup(args);
defer_list[defer_list_size - 2].subargs = malloc_strdup(subargs);
defer_list[defer_list_size - 2].servref = from_server;
defer_list[defer_list_size - 1].cmds = NULL;
defer_list[defer_list_size - 1].subargs = NULL;
need_defered_commands++;
}
BUILT_IN_COMMAND(deop)
{
send_to_server("MODE %s -o", get_server_nickname(from_server));
}
BUILT_IN_COMMAND(describe)
{
const char *target;
target = next_arg(args, &args);
if (target && args && *args)
{
char *message;
int l;
if (!strcmp(target, "*"))
if ((target = get_window_echannel(0)) == NULL)
target = zero;
message = args;
l = message_from(target, LEVEL_ACTION);
send_ctcp(1, target, "ACTION", "%s", message);
if (do_hook(SEND_ACTION_LIST, "%s %s", target, message))
put_it("* -> %s: %s %s", target, get_server_nickname(from_server), message);
pop_message_from(l);
}
else
say("Usage: /DESCRIBE <[=]nick|channel|*> <action description>");
}
BUILT_IN_COMMAND(send_to_query_first)
{
const char *tmp;
tmp = get_window_target(0);
send_text(from_server, tmp, args, NULL, 1, 0);
}
BUILT_IN_COMMAND(send_to_channel_first)
{
const char *tmp;
if ((tmp = get_window_echannel(0)))
send_text(from_server, tmp, args, NULL, 1, 0);
else if ((tmp = get_window_target(0)))
send_text(from_server, tmp, args, NULL, 1, 0);
}
/*
* e_channel: does the channel command. I just added displaying your current
* channel if none is given
*/
BUILT_IN_COMMAND(e_channel)
{
int l;
l = message_from(NULL, LEVEL_OTHER);
if (args && *args)
windowcmd_rejoin(0, &args);
else
list_channels();
pop_message_from(l);
}
/*
* e_nick: does the /NICK command. Records the users current nickname and
* sends the command on to the server
*/
BUILT_IN_COMMAND(e_nick)
{
char *nick;
if (from_server == NOSERV)
{
say("I can't figure out what server to change your nickname for");
return;
}
if (!(nick = next_arg(args, &args)))
{
say("Your nickname on server %d is %s",
from_server,
get_server_nickname(from_server));
if (get_pending_nickname(from_server))
say("A nickname change on server %d to %s is pending.",
from_server,
get_pending_nickname(from_server));
return;
}
change_server_nickname(from_server, nick);
}
/*
* This is a quick and dirty hack (emphasis on dirty) that i whipped up
* just for the heck of it. I feel really dirty about using the add_timer
* call (bletch!) to fake a timeout for io(). The better answer would be
* for io() to take an argument specifying the maximum threshold for a
* timeout, but i didnt want to deal with that here. So i just add a
* dummy timer event that does nothing (wasting two function calls and
* about 20 bytes of memory), and call io() until the whole thing blows
* over. Nice and painless. You might want to try this instead of /sleep,
* since this is (obviously) non-blocking. This also calls time() for every
* io event, so that might also start adding up. Oh well, TIOLI.
*
* Without an argument, it waits for the user to press a key. Any key.
* and the key is accepted. Thats probably not right, ill work on that.
*/
static int e_pause_cb_throw = 0;
static void e_pause_cb (char *u1, const char *u2) { e_pause_cb_throw--; }
static int e_pause_callback (void *ignored) { return 0; }
BUILT_IN_COMMAND(e_pause)
{
char * sec;
double seconds;
Timeval start;
if (!(sec = next_arg(args, &args)))
{
int c_level = e_pause_cb_throw;
add_wait_prompt(empty_string, e_pause_cb,
NULL, WAIT_PROMPT_DUMMY, 0);
e_pause_cb_throw++;
while (e_pause_cb_throw > c_level)
io("pause");
return;
}
seconds = atof(sec);
get_time(&start);
start = time_add(start, double_to_timeval(seconds));
/*
* I use comment here simply becuase its not going to mess
* with the arguments.
*/
add_timer(0, empty_string, seconds, 1,
e_pause_callback,
NULL, NULL, GENERAL_TIMER, -1, 0, 0);
while (time_diff(get_time(NULL), start) > 0)
io("e_pause");
}
/*
* e_privmsg: The MSG command, displaying a message on the screen indicating
* the message was sent. Also, this works for the NOTICE command.
*/
BUILT_IN_COMMAND(e_privmsg)
{
const char *nick;
if (!strcmp(command, "MSG"))
command = "PRIVMSG"; /* *cough* */
if ((nick = next_arg(args, &args)) != NULL)
{
if (!strcmp(nick, "."))
{
if (!(nick = get_server_sent_nick(from_server)))
{
say("You have not sent a message to anyone yet.");
return;
}
}
else if (!strcmp(nick, ","))
{
if (!(nick = get_server_recv_nick(from_server)))
{
say("You have not received a message from anyone yet.");
return;
}
}
else if (!strcmp(nick, "*") && (!(nick = get_window_echannel(0))))
nick = zero;
send_text(from_server, nick, args, command, get_window_display(), 0);
set_server_sent_body(from_server, args);
}
else
{
if (!strcmp(command, "PRIVMSG"))
command = "MSG"; /* *cough* */
say("Usage: /%s <[=]nickname|channel|*> <text>", command);
}
}
/* e_quit: The /QUIT, /EXIT, etc command */
BUILT_IN_COMMAND(e_quit)
{
if (args && *args)
irc_exit(1, "%s", args);
else
irc_exit(1, NULL);
}
/*
* The TOPIC command.
*/
BUILT_IN_COMMAND(e_topic)
{
int clear_topic = 0;
const char *channel = get_window_echannel(0);
const char *arg;
char * args_copy;
const char *recode_text;
char * extra = NULL;
if (!args)
return;
if (*args == '-')
clear_topic = 1, args++;
/*
* You can do
* /TOPIC #chan blahblah blah
* or
* /TOPIC blahblah blah
* So we keep a copy just in case it's the latter.
*/
args_copy = LOCAL_COPY(args);
if (!(arg = next_arg(args, &args)))
arg = channel;
else if (!strcmp(arg, "*"))
arg = channel;
if (!arg)
{
say("You are not on a channel in this window.");
return;
}
if (is_channel(arg))
{
if ((args && *args) || clear_topic)
{
recode_text = outbound_recode(arg, from_server, args, &extra);
send_to_server_with_payload(recode_text, "TOPIC %s", arg);
new_free(&extra);
}
else
send_to_server("TOPIC %s", arg);
}
else if (channel)
{
recode_text = outbound_recode(channel, from_server, args_copy, &extra);
send_to_server_with_payload(recode_text, "TOPIC %s", channel);
new_free(&extra);
}
else
say("You are not on a channel in this window.");
}
/* e_wallop: used for WALLOPS (undernet only command) */
BUILT_IN_COMMAND(e_wallop)
{
int l;
l = message_from(NULL, LEVEL_WALLOP);
send_to_server("WALLOPS :%s", args);
pop_message_from(l);
}
/* Super simple, fast /ECHO */
BUILT_IN_COMMAND(echocmd)
{
int old;
old = swap_window_display(1);
put_echo(args);
swap_window_display(old);
}
/*
* xecho: simply displays the args to the screen, with some flags.
* XECHO <- dont delete this, i search for it. ;-)
*/
BUILT_IN_COMMAND(xechocmd)
{
unsigned display;
char *flag_arg;
int temp = 0;
int all_windows = 0;
int all_windows_for_server = 0;
int want_banner = 0;
char *stuff = NULL;
int nolog = 0;
int more = 1;
int xtended = 0;
int old_window_notify = do_window_notifies;
int old_mangler = display_line_mangler;
int to_window_refnum = get_to_window();
int old_inhibit_logging = inhibit_logging;
int old_output_expires_after = output_expires_after;
int to_level = get_who_level();
const char * to_from = get_who_from();
while (more && args && *args == '-')
{
switch (args[1])
{
case 'c':
case 'C': /* CURRENT (output to user's current window) */
{
next_arg(args, &args);
to_window_refnum = 0;
break;
}
case 'l':
case 'L':
{
flag_arg = next_arg(args, &args);
/* LINE (output to scratch window) */
if (flag_arg[2] == 'i' || flag_arg[2] == 'I')
{
int to_line = 0;
int display_lines;
if (to_window_refnum == -1)
{
yell("XECHO: -LINE only works if -WIN is specified first");
return;
}
/* This is checked below, anyways */
if (get_window_refnum(to_window_refnum) < 1)
break;
display_lines = get_window_display_lines(to_window_refnum);
to_line = my_atol(next_arg(args, &args));
if (to_line < 0 || to_line >= display_lines)
{
yell("XECHO: -LINE %d is out of range "
"for window (max %d)", to_line,
display_lines - 1);
return;
}
set_window_change_line(to_window_refnum, to_line);
}
/* LEVEL (use specified lastlog level) */
else
{
if (!(flag_arg = next_arg(args, &args)))
break;
/* XECHO -L overrules /query and channels! */
if ((temp = str_to_level(flag_arg)) > -1)
{
to_level = temp;
to_from = NULL;
}
}
break;
}
case 't':
case 'T':
{
/* Chew up the argument. */
next_arg(args, &args);
if (!(flag_arg = next_arg(args, &args)))
break;
to_from = flag_arg;
break;
}
case 'v':
case 'V': /* VISUAL (output to a visible window) */
{
/* Chew up the argument. */
/*flag_arg =*/ next_arg(args, &args);
if (get_window_screennum(0) >= 0)
to_window_refnum = get_window_refnum(0);
else if (last_input_screen >= 0 &&
get_screen_input_window(last_input_screen) > 0)
to_window_refnum = get_screen_input_window(last_input_screen);
else
to_window_refnum = lookup_any_visible_window();
break;
}
case 'w':
case 'W': /* WINDOW (output to specified window) */
{
int w;
next_arg(args, &args);
if (!(flag_arg = next_arg(args, &args)))
break;
if (((w = lookup_window(flag_arg)) < 1) &&
((w = get_channel_window(flag_arg, from_server)) < 1))
{
/*
* This is a special favor to Blackjac for
* backwards compatability with epic4 where
* /xecho -w $winchan() would output to the
* current window when $winchan() returned -1.
*/
if (!my_stricmp(flag_arg, "-1"))
w = 0;
else
return; /* No such window */
}
to_window_refnum = w;
break;
}
case 'e':
case 'E':
{
double timeout = 0;
next_arg(args, &args);
if (!(flag_arg = next_arg(args, &args)))
break;
/* Outputting here is bad. So just ignore it */
if (is_number(flag_arg))
{
timeout = atof(flag_arg);
output_expires_after = timeout;
}
break;
}
case 'a':
case 'A': /* ALL (output to all windows) */
case '*':
{
flag_arg = next_arg(args, &args);
if (flag_arg[2] == 's' || flag_arg[2] == 'S')
all_windows_for_server = 1;
else
all_windows = 1;
break;
}
case 'b':
case 'B': /* WITH BANNER */
{
next_arg(args, &args);
want_banner = 1;
break;
}
case 'r':
case 'R': /* RAW OUTPUT TO TERMINAL */
{
next_arg(args, &args);
/*
* Nuke reminded me of this. Just because to_window
* is set does not mean that tputs_x is going to
* put the string out to the screen on that window.
* So we have to make sure that output_screen is
* to_window->screen.
*/
if (to_window_refnum != -1 &&
get_window_screennum(to_window_refnum) >= 0)
output_screen = get_window_screennum(to_window_refnum);
else
output_screen = get_window_screennum(0);
tputs_x(args);
term_flush();
return;
}
case 'n':
case 'N': /* NOLOG (dont add to lastlog) */
{
next_arg(args, &args);
nolog = 1;
break;
}
case 's':
case 'S': /* SAY (dont output if suppressing output) */
{
next_arg(args, &args);
if (!get_window_display())
return;
break;
}
case 'x':
case 'X': /* X -- allow all attributes to be outputted */
{
next_arg(args, &args);
display_line_mangler = NORMALIZE;
xtended = 1;
break;
}
case 'f':
case 'F': /* DO not notify for hidden windows (%F) */
{
next_arg(args, &args);
do_window_notifies = 0;
break;
}
case '-': /* End of arg list */
{
next_arg(args, &args);
more = 0;
break;
}
default: /* Unknown */
{
/*
* Unknown flags are just spit out
* like normal. This is done so that
* people can do /xecho -> blah blah
* and not get throttled on the '->'
*/
more = 0;
break;
}
}
if (!args)
args = LOCAL_COPY(empty_string);
}
display = swap_window_display(1);
if (nolog)
inhibit_logging = 1;
if (want_banner == 1)
{
const char *b = banner();
if (b && *b)
malloc_sprintf(&stuff, "%s %s", b, args);
else
malloc_sprintf(&stuff, "%s", args);
args = stuff;
}
else if (want_banner != 0)
panic(1, "xecho: want_banner is %d", want_banner);
if (all_windows == 1 || all_windows_for_server == 1)
{
int win = 0;
while ((traverse_all_windows2(&win)))
{
int l;
if (all_windows == 0 && get_window_server(win) != from_server)
continue;
l = message_setall(win, to_from, to_level);
put_echo(args);
pop_message_from(l);
}
}
else if (all_windows != 0)
panic(1, "xecho: all_windows is %d", all_windows);
else if (all_windows_for_server != 0)
panic(1, "xecho: all_windows_for_server is %d",
all_windows_for_server);
else
{
int l = message_setall(to_window_refnum, to_from, to_level);
put_echo(args);
pop_message_from(l);
}
if (stuff)
new_free(&stuff);
if (xtended)
display_line_mangler = old_mangler;
do_window_notifies = old_window_notify;
output_expires_after = old_output_expires_after;
if (nolog)
inhibit_logging = old_inhibit_logging;
swap_window_display(display);
}
/*
* /xeval only shares one line of code with /eval, and so doing all of the
* overhead stuff is ridiculous when /eval is used. So i split them up.
* Sue me.
*/
BUILT_IN_COMMAND(xevalcmd)
{
char * flag;
int old_from_server = from_server;
int old_refnum = get_window_refnum(0);
int l = -1;
int old_window_display = get_window_display();
int old_inhibit_logging = inhibit_logging;
int nolog = 0;
while (args && (*args == '-' || *args == '/'))
{
flag = next_arg(args, &args);
if (!my_stricmp(flag, "--")) /* End of options */
break;
if (!my_strnicmp(flag + 1, "SERVER", 1)) /* SERVER */
{
char *s;
int val;
/* No argument means no commands means ignore it */
if (!(s = next_arg(args, &args)))
return;
val = str_to_servref(s);
if (is_server_registered(val))
from_server = val;
}
else if (!my_strnicmp(flag + 1, "WINDOW", 1)) /* WINDOW */
{
int win;
if ((win = lookup_window(next_arg(args, &args))) > 0)
{
l = message_setall(win, get_who_from(), get_who_level());
make_window_current_by_refnum(win);
}
}
/* This does the reverse of ^ */
else if (!my_strnicmp(flag + 1, "NOISY", 1)) /* NOISY */
set_window_display(1);
else if (!my_strnicmp(flag + 1, "NOLOG", 2)) /* NOLOG */
nolog = 1;
}
if (nolog)
inhibit_logging = 1;
runcmds(args, subargs);
if (l != -1)
pop_message_from(l);
make_window_current_by_refnum(old_refnum);
from_server = old_from_server;
set_window_display(old_window_display);
if (nolog)
inhibit_logging = old_inhibit_logging;
}
BUILT_IN_COMMAND(evalcmd)
{
runcmds(args, subargs);
}
/* flush: flushes all pending stuff coming from the server */
BUILT_IN_COMMAND(flush)
{
say("Standby, Flushing server output...");
flush_server(from_server);
say("Done");
}
BUILT_IN_COMMAND(funny_stuff)
{
char *arg;
const char *stuff;
int min = 0,
max = 0,
flags = 0,
ircu = 0;
stuff = empty_string;
while ((arg = next_arg(args, &args)) != NULL)
{
if (*arg == '/' || *arg == '-')
{
if (my_strnicmp(arg+1, "IRCU", 1) == 0) /* IRCU */
ircu = 1;
else if (my_strnicmp(arg+1, "MAX", 2) == 0) /* MAX */
{
if ((arg = next_arg(args, &args)) != NULL)
max = my_atol(arg);
}
else if (my_strnicmp(arg+1, "MIN", 2) == 0) /* MIN */
{
if ((arg = next_arg(args, &args)) != NULL)
min = my_atol(arg);
}
else if (my_strnicmp(arg+1, "ALL", 1) == 0) /* ALL */
flags &= ~(FUNNY_PUBLIC | FUNNY_PRIVATE);
else if (my_strnicmp(arg+1, "PUBLIC", 2) == 0) /* PUBLIC */
{
flags |= FUNNY_PUBLIC;
flags &= ~FUNNY_PRIVATE;
}
else if (my_strnicmp(arg+1, "PRIVATE", 2) == 0) /* PRIVATE */
{
flags |= FUNNY_PRIVATE;
flags &= ~FUNNY_PUBLIC;
}
else if (my_strnicmp(arg+1, "TOPIC", 1) == 0) /* TOPIC */
flags |= FUNNY_TOPIC;
else if (my_strnicmp(arg+1, "USERS", 1) == 0) /* USERS */
flags |= FUNNY_USERS;
else if (my_strnicmp(arg+1, "NAME", 1) == 0) /* NAME */
flags |= FUNNY_NAME;
else
stuff = arg;
}
else stuff = arg;
}
if (strcmp(stuff, "*") == 0)
if (!(stuff = get_window_echannel(0)))
stuff = empty_string;
/* Channel names can contain stars! */
if (strchr(stuff, '*') && !im_on_channel(stuff, from_server))
{
set_server_funny_stuff(from_server, min, max, flags, stuff);
if (min && ircu)
{
if (max)
send_to_server("%s >%d,<%d", command, min - 1, max + 1);
else
send_to_server("%s >%d", command, min - 1);
}
else if (max && ircu)
send_to_server("%s <%d", command, max + 1);
else
send_to_server("%s %s", command, empty_string);
}
else
{
set_server_funny_stuff(from_server, min, max, flags, NULL);
if (min && ircu)
{
if (max)
send_to_server("%s >%d,<%d", command, min - 1, max + 1);
else
send_to_server("%s >%d", command, min - 1);
}
else if (max && ircu)
send_to_server("%s <%d", command, max + 1);
else
send_to_server("%s %s", command, stuff);
}
}
BUILT_IN_COMMAND(hookcmd)
{
if (*args)
do_hook(HOOK_LIST, "%s", args);
else
say("Usage: /HOOK [text]");
}
/*
* Modified /hostname by Thomas Morgan (tmorgan@pobox.com)
*/
BUILT_IN_COMMAND(e_hostname)
{
if (args && *args)
{
char *s;
if (!strcmp(args, "-"))
args = NULL;
s = switch_hostname(args);
say("%s", s);
new_free(&s);
}
else
say("Local Host name is (IPv4: %s, IPv6: %s)",
LocalIPv4HostName ? LocalIPv4HostName : "<default>",
LocalIPv6HostName ? LocalIPv6HostName : "<default>");
}
/*
* info: does the /INFO command. I just added some credits
* I updated most of the text -phone, feb 1993.
*/
BUILT_IN_COMMAND(info)
{
if (!args || !*args)
{
say("IRC II: Originally written by Michael Sandrof");
say("\tCopyright 1990-1991 Michael Sandrof");
say("Versions 2.1 to 2.2pre7 by Troy Rollo");
say("\tCopyright 1991-1992 Troy Rollo");
say("Versions 2.2pre8 through 2.8.2 by Matthew Green");
say("\tCopyright 1992-1995 Matthew Green");
say("All EPIC versions by EPIC Software Labs");
say("\tCopyright 1993-2013 EPIC Software Labs");
say(" ");
say(" Contact the EPIC project (%s)", EMAIL_CONTACT);
say(" for problems with this or any other EPIC client");
say(" ");
say("EPIC Software Labs (in alphabetical order):");
say(" \tBrian Hauber <bhauber@epicsol.org>");
say(" \tChip Norkus <wd@epicsol.org>");
say(" \tCrazyEddy <crazyed@epicsol.org>");
say(" \tDennis Moore <nimh@epicsol.org>");
say(" \tErlend B. Mikkelsen <howl@epicsol.org>");
say(" \tJason Brand <kitambi@epicsol.org>");
say(" \tJeremy Nelson <jnelson@epicsol.org>");
say(" \tStanislaw Halik <sthalik@epicsol.org>");
say(" \tWilliam Rockwood <wjr@epicsol.org>");
say(" \tXavier <jak@epicsol.org>");
say(" ");
say("The EPIC Project:");
say("There are far too many people in the EPIC project to ");
say("thank them properly here, so we set up a web page:");
say("\t\thttp://www.epicsol.org/?page=credits");
say(" ");
say("A special thank you to all who rabidly use and support");
say("the EPIC client and what it stands for");
say(" ");
say(" In memory of Jeffrey Zabek, 1973 - 2000 ");
say(" In memory of Matthew Lumberto, 2013 ");
say(" ");
say("ircii contributors");
say(" ");
say(" \tMichael Sandrof Mark T. Dameu");
say(" \tStellan Klebom Carl v. Loesch");
say(" \tTroy Rollo Martin Friedrich");
say(" \tMichael Weber Bill Wisner");
say(" \tRiccardo Facchetti Stephen van den Berg");
say(" \tVolker Paulsen Kare Pettersson");
say(" \tIan Frechette Charles Hannum");
say(" \tMatthew Green Christopher Williams");
say(" \tJonathan Lemon Brian Koehmstedt");
say(" \tNicolas Pioch Brian Fehdrau");
say(" \tDarren Reed Jeff Grills");
say(" \tChris Williams");
}
send_to_server("INFO %s", args?args:empty_string);
}
/*
* eval_inputlist: Cute little wrapper that calls runcmds() when we
* get an input prompt ..
*/
static void eval_inputlist (char *args, const char *line)
{
char *tmp;
if (args[0]=='(') {
if (!(tmp = next_expr(&args, '('))) {
yell("INPUT: syntax error with arglist");
return;
}
runcmds_with_arglist(args, tmp, line);
} else {
/* traditional behavior */
runcmds(args, line);
}
}
/*
* inputcmd: the INPUT command. Takes a couple of arguements...
* the first surrounded in double quotes, and the rest makes up
* a normal ircII command. The command is evalutated, with $*
* being the line that you input. Used add_wait_prompt() to prompt
* the user... -phone, jan 1993.
*/
BUILT_IN_COMMAND(inputcmd)
{
char *prompt;
int wait_type;
char *argument;
int echo = 1;
while (*args == '-')
{
argument = next_arg(args, &args);
if (!my_stricmp(argument, "-noecho"))
echo = 0;
if (!my_stricmp(argument, "--"))
break;
}
if (!(prompt = new_next_arg(args, &args)))
{
say("Usage: %s \"prompt\" { commands }", command);
return;
}
if (!strcmp(command, "INPUT"))
wait_type = WAIT_PROMPT_LINE;
else
wait_type = WAIT_PROMPT_KEY;
while (my_isspace(*args))
args++;
add_wait_prompt(prompt, eval_inputlist, args, wait_type, echo);
}
/*
* I put this here for legal/logistical reasons. The problem is that each
* file in the EPIC source distribution does not have the actual license
* information, but instead a pointer to where the license is at. This is
* confusing as all heck and doesnt result in the license information
* actually making its way into any compiled binaries. Because there are
* people who are distributing EPIC as a binary package, this command
* allows the resulting binary to have the copyright/license information
* in it, which (IMO) is probably a good idea.
*/
BUILT_IN_COMMAND(license)
{
yell("This is the original license for this software.");
yell(" ");
yell("Copyright (c) 1990 Michael Sandroff.");
yell("Copyright (c) 1991, 1992 Troy Rollo.");
yell("Copyright (c) 1992-1996 Matthew Green.");
yell("Copyright 1994 Jake Khuon.");
yell("Coypright 1993, 2013 EPIC Software Labs.");
yell("All rights reserved");
yell(" ");
yell("Redistribution and use in source and binary forms, with or");
yell("modification, are permitted provided that the following");
yell("conditions are met:");
yell("1. Redistributions of source code must retain the above");
yell(" copyright notice, this list of conditions and the following");
yell(" disclaimer.");
yell("2. Redistributions in binary form must reproduce the above");
yell(" copyright notice, the above paragraph (the one permitting");
yell(" redistribution), this list of conditions, and the following");
yell(" disclaimer in the documentation and/or other materials ");
yell(" provided with the distribution.");
yell("3. The names of the author(s) may not be used to endorse or ");
yell(" promote products derived from this software without specific");
yell(" prior written permission");
yell(" ");
yell("THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY");
yell("EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,");
yell("THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ");
yell("PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ");
yell("AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL");
yell("EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED");
yell("TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,");
yell("DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED");
yell("AND ON ANY THEORY OF LIABIILTY, WHETHER IN CONTRACT, STRICT");
yell("LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING");
yell("IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF");
yell("THE POSSIBILITY OF SUCH DAMAGE.");
}
struct load_info
{
char *filename;
char *package;
const char *loader;
int package_set_here;
int line;
int start_line;
struct stat sb;
} load_level[MAX_LOAD_DEPTH];
int load_depth = -1;
void dump_load_stack (int onelevel)
{
int i = load_depth;
if (i == -1)
return;
yell("Right before line [%d] of file [%s]", load_level[i].line,
load_level[i].filename);
if (!onelevel)
{
while (--i >= 0)
{
yell("Loaded right before line [%d] of file [%s]",
load_level[i].line, load_level[i].filename);
}
}
return;
}
const char *current_filename (void)
{
if (load_depth == -1)
return empty_string;
else if (load_level[load_depth].filename)
return load_level[load_depth].filename;
else
return empty_string;
}
const char *current_loader (void)
{
if (load_depth == -1)
return empty_string;
else if (load_level[load_depth].loader)
return load_level[load_depth].loader;
else
return empty_string;
}
int current_line (void)
{
if (load_depth == -1)
return -1;
else
return load_level[load_depth].line;
}
const char *current_package (void)
{
if (load_depth == -1)
return NULL;
else if (load_level[load_depth].package)
return load_level[load_depth].package;
else
return empty_string;
}
BUILT_IN_COMMAND(packagecmd)
{
if (load_depth == -1)
return;
else
malloc_strcpy(&load_level[load_depth].package, args);
}
BUILT_IN_COMMAND(subpackagecmd)
{
if (load_depth == -1)
return;
else if (load_depth > 0)
{
malloc_strcpy(&load_level[load_depth].package,
load_level[load_depth-1].package);
malloc_strcat(&load_level[load_depth].package, "::");
}
else
new_free(&load_level[load_depth].package);
malloc_strcat(&load_level[load_depth].package, args);
}
static void loader_which (const char *file_contents, off_t file_contents_size, const char *filename, const char *args, struct load_info *);
static void loader_std (const char *file_contents, off_t file_contents_size, const char *filename, const char *args, struct load_info *);
static void loader_pf (const char *file_contents, off_t file_contents_size, const char *filename, const char *args, struct load_info *);
/*
* load: the /LOAD command. Reads the named file, parsing each line as
* though it were typed in (passes each line to parse_statement).
* ---loadcmd---
*/
BUILT_IN_COMMAND(load)
{
char * filename;
char * sargs;
const char * use_path;
char * expanded;
struct epic_loadfile * elf;
int display;
int do_one_more = 0;
void (*loader) (const char *, off_t, const char *, const char *, struct load_info *);
char * file_contents = NULL;
off_t file_contents_size = 0;
/* This should default to /SET DEFAULT_SCRIPT_ENCODING */
const char * declared_encoding = NULL;
if (++load_depth == MAX_LOAD_DEPTH)
{
load_depth--;
dump_load_stack(0);
say("No more than %d levels of LOADs allowed", MAX_LOAD_DEPTH);
return;
}
/* Make sure no old (bogus) values are lying around. */
load_level[load_depth].filename = NULL;
load_level[load_depth].package = NULL;
load_level[load_depth].loader = NULL;
load_level[load_depth].package_set_here = 0;
load_level[load_depth].line = 0;
load_level[load_depth].start_line = 0;
/* What to do with load_level[load_depth].sb? */
display = swap_window_display(0);
permit_status_update(0); /* No updates to the status bar! */
/*
* Default loader: "std" for /load, "which" for /which
*/
if (command && *command == 'W')
loader = loader_which;
else
loader = loader_std;
declared_encoding = find_recoding("scripts", NULL, NULL);
/*
* We iterate over the whole list -- if we use the -args flag, the
* we will make a note to exit the loop at the bottom after we've
* gone through it once...
*/
while (args && *args && (filename = next_arg(args, &args)))
{
if (do_one_more)
{
sargs = args;
args = NULL;
}
else if (my_strnicmp(filename, "-pf", strlen(filename)) == 0)
{
loader = loader_pf;
continue;
}
else if (my_strnicmp(filename, "-std", strlen(filename)) == 0)
{
loader = loader_std;
continue;
}
/*
* If we use the args flag, then we will get the next
* filename (via continue) but at the bottom of the loop
* we will exit the loop
*/
else if (my_strnicmp(filename, "-args", strlen(filename)) == 0)
{
do_one_more = 1;
continue; /* Pick up the filename */
}
else if (my_strnicmp(filename, "-encoding", strlen(filename)) == 0)
{
declared_encoding = next_arg(args, &args);
continue;
}
else
sargs = NULL;
/* Locate the file */
if (!(use_path = get_string_var(LOAD_PATH_VAR)))
{
say("LOAD_PATH has not been set");
continue;
}
/*
* uzfopen emits an error if the file is not found, so we dont.
* uzfopen() also frees 'expanded' for us on error.
*/
expanded = malloc_strdup(filename);
/* Read the file into a string */
if (!(elf = uzfopen(&expanded, use_path, 1,
&load_level[load_depth].sb)))
continue;
if (slurp_elf_file(elf, &file_contents, &file_contents_size) > 0)
{
if (invalid_utf8str(file_contents))
{
size_t really;
really = file_contents_size;
if ((off_t)really != file_contents_size)
privileged_yell("Loading a non-utf8 file whose size is greater than size_t will probably have problems");
say("Recoding %s using encoding %s",
expanded, declared_encoding);
recode_with_iconv(declared_encoding, NULL,
&file_contents, &really);
file_contents_size = (off_t)really;
}
}
epic_fclose(elf);
new_free(&elf);
/* If no file resulted, then we're done. */
if (!file_contents || !*file_contents)
continue;
/* Now process the file */
load_level[load_depth].filename = expanded;
load_level[load_depth].line = 1;
if (load_depth > 0 && load_level[load_depth - 1].package)
malloc_strcpy(&load_level[load_depth].package,
load_level[load_depth-1].package);
will_catch_return_exceptions++;
loader(file_contents, file_contents_size, expanded,
sargs, &load_level[load_depth]);
will_catch_return_exceptions--;
return_exception = 0;
new_free(&load_level[load_depth].filename);
new_free(&load_level[load_depth].package);
new_free(&file_contents);
}
/*
* Restore some sanity
*/
/*
* XXX This looks "clever", but it's not.
* In other places in the client, we would just unconditionally
* restore the value of 'set_window_display(display)', but there
* is a historical practice where users expect to be able to do
* /SET DISPLAY OFF in their scripts, and expect it to stay off.
* Because we turned the display off (above), honoring
* /set display off is as simple as not restoring the original
* display value.
*/
if (get_int_var(DISPLAY_VAR))
set_window_display(display);
permit_status_update(1);
update_all_status();
load_depth--;
}
/* The "WHICH" loader */
static void loader_which (const char *file_contents, off_t file_contents_size, const char *filename, const char *subargs, struct load_info *loadinfo)
{
loadinfo->loader = "which";
yell("%s", filename);
}
/* The "Standard" (legacy) loader */
static void loader_std (const char *file_contents, off_t file_contents_size, const char *filename, const char *subargs, struct load_info *loadinfo)
{
int in_comment, comment_line, no_semicolon;
int paste_level, paste_line;
char *start, *real_start, *current_row;
#define MAX_LINE_SIZE BIG_BUFFER_SIZE * 5
char buffer[MAX_LINE_SIZE * 2 + 1];
loadinfo->loader = "std";
in_comment = 0;
comment_line = -1;
paste_level = 0;
paste_line = -1;
no_semicolon = 1;
real_start = NULL;
current_row = NULL;
for (;;loadinfo->line++)
{
int len;
char *ptr;
if (!string_fgets(buffer, MAX_LINE_SIZE, &file_contents, &file_contents_size))
break;
if (loadinfo->line == 1 && loadinfo->sb.st_mode & 0111 &&
(buffer[0] != '#' || buffer[1] != '!'))
{
yell("Caution -- %s is marked as an executable; "
"loading binaries results in undefined behavior.",
loadinfo->filename);
}
for (start = buffer; my_isspace(*start); start++)
;
if (!*start || *start == '#')
continue;
len = strlen(start);
/*
* Original idea to allow \'s in scripts for continued
* lines by Stargazer <spz@specklec.mpifr-bonn.mpg.de>
*
* If we have \\ at the end of the line, that
* should indicate that we DONT want the slash to
* escape the newline
*
* We cant just do start[len-2] because we cant say
* what will happen if len = 1... (a blank line)
*
* SO....
* If the line ends in a newline, and
* If there are at least 2 characters in the line
* and the 2nd to the last one is a \ and,
* If there are EITHER 2 characters on the line or
* the 3rd to the last character is NOT a \ and,
* If the line isnt too big yet and,
* If we can read more from the file,
* THEN -- adjust the length of the string
*/
while ( (start[len-1] == '\n') &&
(len >= 2 && start[len-2] == '\\') &&
(len < 3 || start[len-3] != '\\') &&
(len < MAX_LINE_SIZE) &&
(string_fgets(&(start[len-2]), MAX_LINE_SIZE - len, &file_contents, &file_contents_size)))
{
len = strlen(start);
loadinfo->line++;
}
if (start[len-1] == '\n')
start[--len] = 0;
real_start = start;
while (start && *start)
{
char *optr = start;
/* Skip slashed brackets */
while ((ptr = strpbrk(optr, "{};/")) &&
ptr != optr && ptr[-1] == '\\')
optr = ptr + 1;
/*
* if no_semicolon is set, we will not attempt
* to parse this line, but will continue
* grabbing text
*/
if (no_semicolon)
no_semicolon = 0;
else if ((!ptr || (ptr != start || *ptr == '/')) &&
current_row)
{
if (!paste_level)
{
parse_statement(current_row, 0, NULL);
new_free(¤t_row);
if (return_exception)
return;
}
else if (!in_comment)
malloc_strcat(¤t_row, ";");
}
if (ptr)
{
char c = *ptr;
*ptr = 0;
if (!in_comment)
malloc_strcat(¤t_row, start);
*ptr = c;
switch (c)
{
case '/' :
{
no_semicolon = 1;
/*
* If we're in a comment, any slashes that arent
* preceeded by a star is just ignored (cause its
* in a comment, after all >;)
*/
if (in_comment)
{
/* oops! cant do ptr[-1] if ptr == optr. doh! */
if ((ptr > start) && (ptr[-1] == '*'))
{
in_comment = 0;
comment_line = -1;
}
break;
}
/* We're not in a comment... should we start one? */
/*
* COMMENT_HACK (at the request of Kanan) determines
* whether C-like comments will be honored only at the
* beginning of a line (if ON) or anywhere (if OFF).
* This is needed because some older scripts (phoenix,
* textbox, etc) may use slash-star in some ascii
* graphics.
*/
if ((ptr[1] == '*') &&
(!get_int_var(COMMENT_HACK_VAR) ||
ptr == real_start))
{
/* Yep. its the start of a comment. */
in_comment = 1;
comment_line = loadinfo->line;
}
else
{
/* Its NOT a comment. Tack it on the end */
malloc_strcat(¤t_row, "/");
/* Is the / is at the EOL? */
if (!ptr[1])
{
/* If we are NOT in a block alias, */
if (paste_level == 0)
{
parse_statement(current_row, 0, NULL);
new_free(¤t_row);
if (return_exception)
return;
}
/* Just add semicolon and keep going */
else
no_semicolon = 0;
}
}
break;
}
/* switch statement tabbed back */
case '{' :
{
if (in_comment)
break;
/*
* If we are opening a brand new {} pair,
* remember the line
*/
if (!paste_level)
paste_line = loadinfo->line;
paste_level++;
if (ptr == start)
malloc_strcat(¤t_row, " {");
else
malloc_strcat(¤t_row, "{");
no_semicolon = 1;
break;
}
case '}' :
{
if (in_comment)
break;
if (!paste_level)
{
my_error("Unexpected } in %s, line %d",
filename, loadinfo->line);
break;
}
paste_level--;
/*
* If we're back to "level 0", then reset
* the paste line
*/
if (!paste_level)
paste_line = -1;
malloc_strcat(¤t_row, "}");
no_semicolon = ptr[1] ? 1 : 0;
break;
}
case ';':
{
if (in_comment)
break;
malloc_strcat(¤t_row, ";");
/* Semicolon at the end of line, not within {}s */
if (ptr[1] == 0 && !paste_level)
{
parse_statement(current_row, 0, NULL);
new_free(¤t_row);
if (return_exception)
return;
}
/* Semicolon at end of line, within {}s */
else if (ptr[1] == 0 && paste_level)
no_semicolon = 0;
/* Semicolon in the middle of a line */
else
no_semicolon = 1;
break;
}
} /* End of switch */
start = ptr+1;
}
else /* if (!ptr) -- eg, no special chars*/
{
if (!in_comment)
malloc_strcat(¤t_row, start);
start = NULL;
}
} /* End of while (start && *start) */
} /* End of for(;;line++) */
if (in_comment)
my_error("File %s ended with an unterminated comment in line %d",
filename, comment_line);
if (current_row)
{
if (paste_level)
{
my_error("Unexpected EOF in %s trying to match '{' at line %d",
filename, paste_line);
new_free(¤t_row);
}
else
{
parse_statement(current_row, 0, NULL);
new_free(¤t_row);
if (return_exception)
return;
}
}
}
/* The "Pre-Formatted" (new) loader */
/*
* The idea behind the pre-formatted loader was two-fold:
* 1) Eliminate the irregular syntax rules that applied only to /load'ed
* scripts and were not used anywhere else in ircII
* 2) Make loading scripts faster by reducing the processing needed to get
* them ready for /eval'ing.
*
* Normal loader scripts are taken to be a collection of newline-separated
* blocks of statements. By custom, each block contained one statement.
* When people first wanted to break statements over multiple lines, the
* \ continued-line marker was added. Later, when the perl-esque syntax
* was introduced, a fancy {-and-} counting algorithm was added that allowed
* the automatic continuation of a statement across lines without having to
* use \'s. Further, semicolons had to be added between statements that
* were within {..}'s. The result is a file that bears little resemblance
* to what ircII code actually looks like, and requires a heavyweight parser
* to convert a script into something that can be executed.
*
* The rules regarding the placement and matching of {'s and }'s, and the
* insertion of semicolons are, to be kind, irregular. This irregularity
* inspired the PF loader.
*
* The PF stands for "Pre-Formatted" and this means that your script is
* expected to be well-formed ircII code. The PF loader does not do any
* processing on your code, except to remove comments and leading spaces.
* The whole of the file should be a block, that is, a set of semicolon-
* separated statements.
*
* This creates several advantages. Because you are required to put in the
* semicolons yourself, the PF loader does not inexplicably insert semicolons
* in places you don't want them. The PF loader does not treat each line
* as a separate statement so you can break up your statements however it
* pleases you.
*
* Because of the lack of processing overhead, the pf loader is lightweight
* and is "signficantly" faster than the standard loader. This can be a
* big win for large scripts.
*/
static void loader_pf (const char *file_contents, off_t file_contents_size, const char *filename, const char *subargs, struct load_info *loadinfo)
{
char * buffer;
int bufsize, pos;
int this_char, my_newline, comment, shebang;
loadinfo->loader = "pf";
bufsize = 8192;
buffer = new_malloc(bufsize);
pos = 0;
my_newline = 1;
comment = 0;
shebang = 0;
this_char = string_fgetc(&file_contents, &file_contents_size);
/*
* If the file is +x, and starts with #!, it's ok.
* Otherwise, refuse to load it.
*/
if (loadinfo->sb.st_mode & 0111)
{
/* Special code to support #! scripts. */
if (this_char == '#' && !string_feof(file_contents, file_contents_size))
{
this_char = string_fgetc(&file_contents, &file_contents_size);
if (this_char == '!')
shebang = 1;
}
if (shebang == 0)
{
yell("Cannot open %s -- executable file",
loadinfo->filename);
new_free(&buffer);
return;
}
comment = 1;
}
while (!string_feof(file_contents, file_contents_size))
{
do
{
/* At a newline, turn on eol handling, turn off comment. */
if (this_char == '\n') {
my_newline = 1;
comment = 0;
break;
}
/* If we are in a comment, ignore this character. */
if (comment)
break;
/* If we last saw an eol, ignore any following spaces */
if (my_newline && isspace(this_char))
break;
/* If we last saw an eol, a # starts a one-line comment. */
if (my_newline && this_char == '#') {
comment = 1;
break;
}
/* If the last thing we saw was a newline, put a space here */
if (my_newline && pos > 0)
buffer[pos++] = ' ';
/* We are no longer at a newline */
my_newline = 0;
/* Append this character to the buffer */
buffer[pos++] = this_char;
} while (0);
if (pos >= bufsize - 20) {
void *buffer_ = buffer;
bufsize *= 2;
new_realloc(&buffer_, bufsize);
buffer = buffer_;
}
this_char = string_fgetc(&file_contents, &file_contents_size);
}
buffer[pos] = 0;
call_lambda_command("LOAD", buffer, subargs);
new_free(&buffer);
}
/*
* The /me command. Does CTCP ACTION. Dont ask me why this isnt the
* same as /describe...
*/
BUILT_IN_COMMAND(mecmd)
{
if (args && *args)
{
const char *target;
int l;
if ((target = get_window_target(0)) != NULL)
{
send_ctcp(1, target, "ACTION", "%s", args);
l = message_from(target, LEVEL_ACTION);
if (do_hook(SEND_ACTION_LIST, "%s %s", target, args))
put_it("* %s %s", get_server_nickname(from_server), args);
pop_message_from(l);
}
else
say("No target, neither channel nor query");
}
else
say("Usage: /ME <action description>");
}
static void oper_password_received (char *data, const char *line)
{
send_to_server("OPER %s %s", data, line);
}
/* oper: the OPER command. */
BUILT_IN_COMMAND(oper)
{
char *password;
char *nick;
oper_command = 1;
if (!(nick = next_arg(args, &args)))
nick = nickname;
if (!(password = next_arg(args, &args)))
{
add_wait_prompt("Operator Password:",
oper_password_received, nick, WAIT_PROMPT_LINE, 0);
return;
}
send_to_server("OPER %s %s", nick, password);
}
/* pingcmd: ctcp ping, duh - phone, jan 1993. */
BUILT_IN_COMMAND(pingcmd)
{
Timeval t;
char buffer[64];
get_time(&t);
snprintf(buffer, 63, "%s PING %ld %ld", args,
(long)t.tv_sec, (long)t.tv_usec);
ctcp(NULL, buffer, empty_string);
}
BUILT_IN_COMMAND(pop_cmd)
{
extern char *function_pop(char *);
char *blah = function_pop(args);
new_free(&blah);
}
BUILT_IN_COMMAND(pretend_cmd)
{
char *args_copy;
int s = from_server;
if (!is_server_open(s))
{
say("Server %d is not connected", s);
return;
}
if (!(args_copy = alloca(IO_BUFFER_SIZE + 1)))
return;
strlcpy(args_copy, args, IO_BUFFER_SIZE);
parse_server(args_copy, IO_BUFFER_SIZE);
from_server = s;
}
BUILT_IN_COMMAND(push_cmd)
{
extern char *function_push(char *);
char *blah = function_push(args);
new_free(&blah);
}
/*
* query: the /QUERY command. Works much like the /MSG, I'll let you figure
* it out.
*/
BUILT_IN_COMMAND(query)
{
windowcmd_query(0, &args);
}
/*
* quote: handles the QUOTE command. args are a direct server command which
* is simply send directly to the server
*/
BUILT_IN_COMMAND(quotecmd)
{
int refnum = from_server;
int urlencoded = 0;
if (*command == 'X')
{
while (args && (*args == '-' || *args == '/'))
{
char *flag = next_arg(args, &args);
if (!my_strnicmp(flag + 1, "SERVER", 1)) /* SERVER */
{
char *s;
int sval;
/* No argument means no commands means ignore it. */
if (!(s = next_arg(args, &args)))
return;
sval = str_to_servref(s);
if (!is_server_open(sval))
{
say("XQUOTE: Server %d is not connected", sval);
return;
}
refnum = sval;
}
else if (!my_strnicmp(flag + 1, "URL", 1)) /* URL quoting */
urlencoded++;
else if (!my_strnicmp(flag + 1, "ALL", 1)) /* ALL */
{
int i;
if (args && *args)
{
for (i = 0; i < server_list_size(); i++)
{
if (is_server_registered(i))
send_to_aserver(i, "%s", args);
}
}
return;
}
else /* End option processing on unknown arg. */
break;
}
}
if (urlencoded)
{
char * dest;
size_t destlen;
if (!(dest = transform_string_dyn("-URL", args, 0, &destlen)))
{
yell("XQUOTE -U: Could not urldecode [%s]", args);
return; /* It failed. bail. */
}
send_to_aserver_raw(refnum, destlen, dest);
new_free(&dest);
}
else if (args && *args)
send_to_aserver(refnum, "%s", args);
}
BUILT_IN_COMMAND(redirect)
{
const char *who;
if ((who = next_arg(args, &args)) == NULL)
{
say("%s", "Usage: /REDIRECT <nick|channel|=dcc|%process|/command|@filedescriptor|\"|0> <cmd>");
return;
}
if (from_server == NOSERV)
{
say("You may not use /REDIRECT here.");
return;
}
if (!strcmp(who, "*") && !(who = get_window_echannel(0)))
{
say("Must be on a channel to redirect to '*'");
return;
}
if (is_me(from_server, who))
{
say("You may not redirect output to yourself");
return;
}
/*
* Added by Chaos: Fixes the problem with /redirect when
* redirecting to a dcc chat sessions that isn't active or
* doesn't exist.
*/
if ((*who == '=') && !is_number(who + 1) && !dcc_chat_active(who + 1))
{
say("You don't have an active DCC CHAT to %s",who + 1);
return;
}
/*
* Turn on redirect, and do the thing.
*/
set_server_redirect(from_server, who);
set_server_sent(from_server, 0);
runcmds(args, subargs);
/*
* If we've queried the server, then we wait for it to
* reply, otherwise we're done.
*/
if (get_server_sent(from_server))
send_to_server("***%s", who);
else
set_server_redirect(from_server, NULL);
}
BUILT_IN_COMMAND(squitcmd)
{
char *server = NULL;
char *reason = NULL;
if (!(server = new_next_arg(args, &reason)))
{
say("Usage: /SQUIT <server> [<reason>]");
return;
}
if (reason && *reason)
send_to_server("SQUIT %s :%s", server, reason);
else
send_to_server("SQUIT %s", server);
}
BUILT_IN_COMMAND(send_2comm)
{
const char *target;
char *reason = NULL;
if (!(target = next_arg(args, &reason)))
target = empty_string;
if (!reason || !*reason)
reason = LOCAL_COPY(empty_string);
if (!target || !*target || !strcmp(target, "*"))
{
target = get_window_echannel(0);
if (!target || !*target)
target = "*"; /* what-EVER */
}
if (reason && *reason)
{
const char *recode_text;
char *extra = NULL;
recode_text = outbound_recode(target, from_server, reason, &extra);
send_to_server_with_payload(recode_text, "%s %s", command, target);
new_free(&extra);
}
else
send_to_server("%s %s", command, target);
}
/*
* send_comm: the generic command function. Uses the full command name found
* in 'command', combines it with the 'args', and sends it to the server
*/
BUILT_IN_COMMAND(send_comm)
{
if (args && *args)
send_to_server("%s %s", command, args);
else
send_to_server("%s", command);
}
/*
* Usage: INVITE #channel nickname1 nickname2 nickname3
* INVITE nickname #channel1 #channel2 #channel3
* INVITE nickname
*/
BUILT_IN_COMMAND(send_invite)
{
const char *arg;
const char *nick = NULL;
const char *channel = NULL;
const char *currchan;
int invites = 0;
currchan = get_window_echannel(0);
if (!currchan || !*currchan)
currchan = "*"; /* what-EVER */
while ((arg = next_arg(args, &args)))
{
if (!strcmp(arg, "*"))
channel = currchan;
else if (is_channel(arg))
channel = arg;
else
nick = arg;
if (channel && nick)
{
send_to_server("%s %s %s", command, nick, channel);
invites++;
}
}
if (!invites && nick)
{
if (strcmp(currchan, "*"))
{
send_to_server("%s %s %s", command, nick, currchan);
invites++;
}
else
say("You are not on a channel");
}
if (!invites)
say("Usage: %s [#channel] nickname", command);
}
/*
* send_kick: sends a kick message to the server. Works properly with
* kick comments.
*/
BUILT_IN_COMMAND(send_kick)
{
char *kickee;
const char *comment;
const char *channel;
const char * recode_text;
char * extra = NULL;
char usage[] = "Usage: KICK <channel|*> <nickname> [comment]";
if (!(channel = next_arg(args, &args)))
{
yell("%s", usage);
return;
}
if (!(kickee = next_arg(args, &args)))
{
yell("%s", usage);
return;
}
comment = args?args:empty_string;
if (!strcmp(channel, "*"))
channel = get_window_echannel(0);
recode_text = outbound_recode(channel, from_server, comment, &extra);
send_to_server_with_payload(recode_text, "KICK %s %s", channel, kickee);
new_free(&extra);
}
/*
* send_channel_com: does the same as send_com for command where the first
* argument is a channel name. If the first argument is *, it is expanded
* to the current channel (a la /WHO).
*/
BUILT_IN_COMMAND(send_channel_com)
{
char *ptr;
const char *s;
char usage[] = "Usage: %s <*|#channel> [arguments]";
ptr = next_arg(args, &args);
if (ptr && !strcmp(ptr, "*"))
{
if ((s = get_window_echannel(0)) != NULL)
send_to_server("%s %s %s", command, s, args?args:empty_string);
else
say("%s * is not valid since you are not on a channel", command);
}
else if (ptr)
send_to_server("%s %s %s", command, ptr, args?args:empty_string);
else
yell(usage, command);
}
/* The SENDLINE command.. */
BUILT_IN_COMMAND(sendlinecmd)
{
int server;
int display;
server = from_server;
display = swap_window_display(1);
parse_statement(args, 1, NULL);
update_input(-1, UPDATE_ALL);
swap_window_display(display);
from_server = server;
}
BUILT_IN_COMMAND(setenvcmd)
{
char *env_var;
if ((env_var = next_arg(args, &args)) != NULL)
{
if (*env_var == '-' && empty(args))
unsetenv(env_var + 1);
else
{
unsetenv(env_var);
setenv(env_var, args, 1);
}
}
else
say("Usage: SETENV [-]<var-name> [<value>]");
}
BUILT_IN_COMMAND(shift_cmd)
{
extern char *function_shift(char *);
char *blah = function_shift(args);
new_free(&blah);
}
BUILT_IN_COMMAND(sleepcmd)
{
char *arg;
float nms;
if ((arg = next_arg(args, &args)) != NULL)
{
nms = atof(arg);
my_sleep(nms);
}
else
say("Usage: SLEEP <seconds>");
}
BUILT_IN_COMMAND(stackcmd)
{
char *arg;
int len, type;
if ((arg = next_arg(args, &args)) != NULL)
{
len = strlen(arg);
if (!my_strnicmp(arg, "PUSH", len))
type = STACK_PUSH;
else if (!my_strnicmp(arg, "POP", len))
type = STACK_POP;
else if (!my_strnicmp(arg, "LIST", len))
type = STACK_LIST;
else
{
say("%s is unknown stack verb", arg);
return;
}
}
else
{
say("Need operation for STACK");
return;
}
if ((arg = next_arg(args, &args)) != NULL)
{
char *n;
len = strlen(arg);
if (!my_strnicmp(arg, "ON", len))
do_stack_on(type, args);
else if (!my_strnicmp(arg, "ALIAS", len))
{
n = remove_brackets(args, subargs);
if (type == STACK_PUSH)
{
if (stack_push_cmd_alias(n))
say("Can't push ALIAS %s", n);
}
else if (type == STACK_POP)
{
if (stack_pop_cmd_alias(n))
say("Can't pop ALIAS %s", n);
}
else
{
if (stack_list_cmd_alias(n))
say("Can't list ALIAS %s", n);
}
new_free(&n);
}
else if (!my_strnicmp(arg, "ASSIGN", len))
{
n = remove_brackets(args, subargs);
if (type == STACK_PUSH)
{
if (stack_push_var_alias(n))
say("Can't push ASSIGN %s", n);
}
else if (type == STACK_POP)
{
if (stack_pop_var_alias(n))
say("Can't pop ASSIGN %s", n);
}
else
{
if (stack_list_var_alias(n))
say("Can't list ASSIGN %s", n);
}
new_free(&n);
}
else if (!my_strnicmp(arg, "SET", len))
{
n = remove_brackets(args, subargs);
if (type == STACK_PUSH)
{
if (stack_push_builtin_var_alias(n))
say("Can't push SET %s", n);
}
else if (type == STACK_POP)
{
if (stack_pop_builtin_var_alias(n))
say("Can't pop SET %s", n);
}
else
{
if (stack_list_builtin_var_alias(n))
say("Can't list SET %s", n);
}
new_free(&n);
}
else if (!my_strnicmp(arg, "BIND", len))
do_stack_bind(type, args);
else
{
say("%s is not a valid STACK type", arg);
return;
}
}
else
{
say("Need stack type for STACK");
return;
}
}
/* timercmd moved to 'timer.c' */
BUILT_IN_COMMAND(unshift_cmd)
{
extern char *function_unshift(char *);
char *blah = function_unshift(args);
new_free(&blah);
}
BUILT_IN_COMMAND(usleepcmd)
{
char * arg;
double interval;
if ((arg = next_arg(args, &args)))
{
interval = my_atol(arg) / 1000000.0;
my_sleep(interval);
}
else
say("Usage: USLEEP <usec>");
}
/* version: does the /VERSION command with some IRCII version stuff */
BUILT_IN_COMMAND(version)
{
char *host;
if ((host = next_arg(args, &args)) != NULL)
send_to_server("VERSION %s", host);
else
{
say ("Client: ircII %s (Commit Id: %ld) (Internal Version: %s)", irc_version, commit_id, internal_version);
send_to_server("VERSION");
}
}
BUILT_IN_COMMAND(waitcmd)
{
char *ctl_arg = next_arg(args, &args);
if (ctl_arg && !my_strnicmp(ctl_arg, "-cmd", 2))
server_passive_wait(from_server, args);
else if (ctl_arg && !my_strnicmp(ctl_arg, "for", 3))
{
set_server_sent(from_server, 0);
lock_stack_frame();
runcmds(args, subargs);
unlock_stack_frame();
if (get_server_sent(from_server))
server_hard_wait(from_server);
set_server_sent(from_server, 0); /* Reset it again */
}
else if (ctl_arg && *ctl_arg == '%')
{
if (is_valid_process(ctl_arg))
{
if (args && *args)
{
if (!my_strnicmp(args, "-cmd ", 4))
next_arg(args, &args);
add_process_wait(ctl_arg, args);
}
else
{
char reason[1024];
snprintf(reason, 1024, "WAIT on EXEC %s",
ctl_arg);
lock_stack_frame();
while (is_valid_process(ctl_arg))
io(reason);
unlock_stack_frame();
}
}
}
else if (ctl_arg && *ctl_arg == '=')
{
ctl_arg++;
wait_for_dcc(ctl_arg);
}
else if (ctl_arg)
yell("Unknown argument to /WAIT");
else
{
server_hard_wait(from_server);
set_server_sent(from_server, 0);
}
}
/*
* whois: the WHOIS and WHOWAS commands.
*/
BUILT_IN_COMMAND(whois)
{
char *stuff = NULL;
if (!strcmp(command, "WHOWAS"))
{
char *word_one = next_arg(args, &args);
if (!is_string_empty(args))
malloc_sprintf(&stuff, "%s %s", word_one, args);
else if (!is_string_empty(word_one))
malloc_sprintf(&stuff, "%s", word_one);
else
malloc_sprintf(&stuff, "%s", get_server_nickname(from_server));
send_to_server("WHOWAS %s", stuff);
new_free(&stuff);
}
else /* whois command */
send_to_server("WHOIS %s", is_string_empty(args) ?
get_server_nickname(from_server) :
args);
}
/*
* type: The TYPE command. This parses the given string and treats each
* character as though it were typed in by the user. Thus key bindings
* are used for each character parsed. Special case characters are control
* character sequences, specified by a ^ follow by a legal control key.
* Thus doing "/TYPE ^B" will be as tho ^B were hit at the keyboard,
* probably moving the cursor backward one character.
*
* This was moved from keys.c, because it certainly does not belong there,
* and this seemed a reasonable place for it to go for now.
*/
BUILT_IN_COMMAND(typecmd)
{
int c;
char key;
for (; *args; args++)
{
if (*args == '^')
{
args++;
if (*args == '?')
key = '\177';
else if (*args)
{
c = *args;
if (islower(c))
c = toupper(c);
if (c < 64)
{
say("Invalid key sequence: ^%c", c);
return;
}
key = c - 64;
}
else
break;
}
else
{
if (*args == '\\')
args++;
if (*args)
key = *args;
else
break;
}
translate_user_input(key);
}
}
BUILT_IN_COMMAND(xtypecmd)
{
char *arg;
int code_point;
ptrdiff_t offset;
if (*args == '-' || *args == '/')
{
int saved = (int)*args;
args++;
if ((arg = next_arg(args, &args)) != NULL)
{
if (!my_strnicmp(arg, "LITERAL", 1))
{
while ((code_point = next_code_point2(args, &offset, 1)))
{
args += offset;
input_add_character(code_point, NULL);
}
}
else
say("Unknown flag -%s to XTYPE", arg);
return;
}
input_add_character(saved, NULL);
}
else
typecmd(command, args, empty_string);
return;
}
/*
*
* The rest of this file is the stuff that is not a command but is used
* by the commands in this file. These are considered to be "overhead".
*
*/
/*
* Returns 1 if the given built in command exists,
* Returns 0 if not.
*/
int command_exist (char *command)
{
char *name;
void * args = NULL;
void (*func) (const char *, char *, const char *) = NULL;
if (!command || !*command)
return 0;
name = LOCAL_COPY(command);
upper(name);
get_cmd_alias(name, &args, &func);
if (func == NULL)
return 0;
return 1;
}
int redirect_text (int to_server, const char *nick_list, const char *text, char *command, int hook)
{
static int recursion = 0;
int old_from_server = from_server;
int allow = 0;
int retval;
from_server = to_server;
if (recursion++ == 0)
allow = do_hook(REDIRECT_LIST, "%s %s", nick_list, text);
/* Suppress output */
if (strcmp(nick_list, "0") == 0 || *nick_list == '@' || *nick_list == '/')
retval = 1;
else
retval = 0;
/*
* Dont hook /ON REDIRECT if we're being called recursively
*/
if (allow)
send_text(from_server, nick_list, text, command, hook, 0);
recursion--;
from_server = old_from_server;
return retval;
}
struct target_type
{
char *nick_list;
const char *message;
int hook_type;
const char *command;
const char *format;
int mask;
};
/*
* The whole shebang.
*
* The message targets are parsed and collected into one of 4 buckets.
* This is not too dissimilar to what was done before, except now i
* feel more comfortable about how the code works.
*
* Bucket 0 -- Unencrypted PRIVMSGs to nicknames
* Bucket 1 -- Unencrypted PRIVMSGs to channels
* Bucket 2 -- Unencrypted NOTICEs to nicknames
* Bucket 3 -- Unencrypted NOTICEs to channels
*
* All other messages (encrypted, and DCC CHATs) are dispatched
* immediately, and seperately from all others. All messages that
* end up in one of the above mentioned buckets get sent out all
* at once.
*
* XXXX --- Its super super important that you *never* call yell(),
* put_it(), or anything like that which would end up calling add_to_window().
* add_to_window() can get in an infinite cycle with send_text() if the
* user is /redirect'ing. send_text() tries to prevent the user from being
* able to send stuff to the window by never hooking /ONs if we're already
* recursing -- so whoever is hacking on this code, its also up to you to
* make sure that YOU dont send anything to the screen without checking first!
*/
/* SENDTEXT -- Don't delete this, I search for it! */
void send_text (int server, const char *nick_list, const char *text, const char *command, int hook, int already_encoded)
{
int i,
old_server;
char *current_nick,
*next_nick,
*line;
List *key;
int old_window_display;
int old_from_server;
static int recursion = 0;
char * extra = NULL;
const char *recode_text;
/*
* XXXX - Heaven help us.
*/
struct target_type target[4] =
{
{NULL, NULL, SEND_MSG_LIST, "PRIVMSG", "*%s*> %s" , LEVEL_MSG },
{NULL, NULL, SEND_PUBLIC_LIST, "PRIVMSG", "%s> %s" , LEVEL_PUBLIC },
{NULL, NULL, SEND_NOTICE_LIST, "NOTICE", "-%s-> %s" , LEVEL_NOTICE },
{NULL, NULL, SEND_NOTICE_LIST, "NOTICE", "-%s-> %s" , LEVEL_NOTICE }
};
if (!nick_list || !text)
return;
/*
* If we are called recursively, it is because the user has
* /redirect'ed the output, or the user is sending something from
* their /on send_*. In both of these cases, an infinite loop
* could occur. To defeat the /redirect, we do not output to the
* screen. To defeat the /on send_*, we do not offer /on events.
*
* XXX - The 'hook == -1' hack is there until I write something
* more decent than recursion to handle /msg -<server>/<target>.
*
* I've gotten into too many scrapes with infinite recursion here.
* So, if you recurse twice, then something is seriously wrong
* and we will just flat out refuse to send the message.
*/
if (hook == -1)
hook = 1;
else if (recursion == 1)
hook = 0;
else if (recursion >= 2)
return;
recursion++;
old_from_server = from_server;
from_server = server;
old_window_display = swap_window_display(hook);
next_nick = LOCAL_COPY(nick_list);
if (command && !strcmp(command, "MSG"))
command = "PRIVMSG"; /* XXX */
while ((current_nick = next_nick))
{
if ((next_nick = strchr(current_nick, ',')))
*next_nick++ = 0;
if (!*current_nick)
continue;
/*
* The OUTBOUND text is ALWAYS recoded.
* In theory, 'text' should always be in utf8..
* But CTCPs are pre-encoded, so don't double recode those.
*/
if (!already_encoded)
recode_text = outbound_recode(current_nick, from_server, text, &extra);
else
recode_text = text;
if (*current_nick == '%')
text_to_process(current_nick, recode_text, 1);
/*
* Blank lines may be sent to /exec'd processes, but
* not to any other targets. Once we know that the target
* is not an exec'd process, we make sure the line is not
* blank.
*/
else if (!text || !*text)
;
/*
* Target 0 (an invalid irc target) is the "sink": messages
* go in but they don't go out.
*/
else if (!strcmp(current_nick, "0"))
;
/*
* Targets that start with @ are numbers that refer to
* $open() files. This used to be used for DCC TALK but
* we haven't supported that for 5 years.
*/
else if (*current_nick == '@' && is_number(current_nick + 1))
target_file_write(current_nick, text);
else if (*current_nick == '@'
&& (current_nick[1] == 'W' || current_nick[1] == 'w')
&& is_number(current_nick + 2))
target_file_write(current_nick, text);
else if (*current_nick == '@'
&& (current_nick[1] == 'L' || current_nick[1] == 'l')
&& is_number(current_nick + 2))
target_file_write(current_nick, text);
else if (*current_nick == '@'
&& (current_nick[1] == 'E' || current_nick[1] == 'e'))
{
/* XXX this is probably cheating. */
char *ptr = NULL;
malloc_sprintf(&ptr, "-W %s %s",
current_nick + 2, text);
xechocmd("XECHO", ptr, NULL);
new_free(&ptr);
}
else if (*current_nick == '"')
send_to_server("%s", text);
else if (*current_nick == '/')
{
line = malloc_strdup3(current_nick, space, text);
parse_statement(line, 0, empty_string);
new_free(&line);
}
else if (*current_nick == '=')
{
if (!is_number(current_nick + 1) &&
!dcc_chat_active(current_nick + 1))
{
yell("No DCC CHAT connection open to %s",
current_nick + 1);
new_free(&extra);
continue;
}
if ((key = is_crypted(current_nick, -1, NULL)) != 0)
{
char *breakage = LOCAL_COPY(recode_text);
line = crypt_msg(breakage, key);
}
else
line = malloc_strdup(recode_text);
old_server = from_server;
from_server = NOSERV;
dcc_chat_transmit(current_nick + 1, line, text, command, hook);
from_server = old_server;
set_server_sent_nick(from_server, current_nick);
new_free(&line);
}
else if (*current_nick == '-' && strchr(current_nick + 1, '/'))
{
int servref;
char * servername;
char * msgtarget;
servername = current_nick + 1;
msgtarget = strchr(servername, '/');
*msgtarget++ = 0;
if ((servref = str_to_servref(servername)) == NOSERV)
{
yell("Unknown server [%s] in target", servername);
new_free(&extra);
continue;
}
/* XXX -- Recursion is a hack. (but it works) */
send_text(servref, msgtarget, text, command, hook ? -1 : 0, already_encoded);
new_free(&extra);
continue;
}
else
{
char * copy = NULL;
/* XXX Should we check for invalid from_server here? */
if (get_server_doing_notice(from_server) > 0)
{
say("You cannot send a message from within ON NOTICE");
new_free(&extra);
continue;
}
i = is_channel(current_nick);
if (get_server_doing_privmsg(from_server) ||
(command && !strcmp(command, "NOTICE")))
i += 2;
if ((key = is_crypted(current_nick, from_server, NULL)))
{
int l;
l = message_from(current_nick, target[i].mask);
if (hook && do_hook(target[i].hook_type, "%s %s",
current_nick, text))
put_it(target[i].format, current_nick, text);
copy = LOCAL_COPY(recode_text);
line = crypt_msg(copy, key);
send_to_server_with_payload(line, "%s %s",
target[i].command, current_nick);
set_server_sent_nick(from_server, current_nick);
new_free(&line);
pop_message_from(l);
}
else if (extra) /* Message was transcoded */
{
int l;
l = message_from(current_nick, target[i].mask);
/*
* TEXT is the original UTF8 string.
* Recode_text is converted FOR IRC USE ONLY.
*/
if (hook && do_hook(target[i].hook_type, "%s %s",
current_nick, text))
put_it(target[i].format, current_nick, text);
send_to_server_with_payload(recode_text, "%s %s",
target[i].command, current_nick);
set_server_sent_nick(from_server, current_nick);
pop_message_from(l);
}
else
{
if (i == 0)
set_server_sent_nick(from_server, current_nick);
if (target[i].nick_list)
malloc_strcat(&target[i].nick_list, ",");
malloc_strcat(&target[i].nick_list, current_nick);
if (!target[i].message)
target[i].message = text;
}
}
new_free(&extra);
}
for (i = 0; i < 4; i++)
{
int l;
if (!target[i].message)
continue;
l = message_from(target[i].nick_list, target[i].mask);
if (hook && do_hook(target[i].hook_type, "%s %s",
target[i].nick_list, target[i].message))
put_it(target[i].format, target[i].nick_list,
target[i].message);
send_to_server_with_payload(target[i].message,
"%s %s",
target[i].command,
target[i].nick_list);
new_free(&target[i].nick_list);
target[i].message = NULL;
pop_message_from(l);
}
swap_window_display(old_window_display);
from_server = old_from_server;
recursion--;
}
/***************************************************************************/
static char * parse_line_alias_special (const char *name, const char *what, const char *args, void *arglist, int function);
/*
* Execute a block of ircII code (``what'') as a lambda function.
* The block will be run in its own private local name space, and will be
* given its own $FUNCTION_RETURN variable, which it will return.
*
* name - If name is not NULL, this lambda function will be treated as
* an atomic scope, and will not be able to change the enclosing
* scope's local variables. If name is NULL, it will have access
* to the enclosing local variables.
* what - The lambda function itself; a block of ircII code.
* args - The value of $* for the lambda function
*/
char * call_lambda_function (const char *name, const char *what, const char *args)
{
/*
* Explanation for why 'args' is (const char *) and not (char *).
* 'args' is $*, and is passed in from above. $* might be changed
* if we were executing an alias that had a parameter list, and so
* parse_line_alias_special must take a (char *). But the only time
* that ``args'' would be changed is if the ``arglist'' param was
* not NULL. Since we hardcode ``arglist == NULL'' it is absolutely
* guaranteed that ``args'' will not be touched, and so, it can safely
* be treated as (const char *) by whoever called us. Unfortunately,
* C does not allow you to treat a pointer as conditionally const, so
* we just use the cast to hide that. This is absolutely safe.
*/
return parse_line_alias_special(name, what, (char *)
#ifdef HAVE_INTPTR_T
(intptr_t)
#endif
args, NULL, 1);
}
/*
* Execute a block of ircII code as a lambda command, but do not set up a
* new FUNCTION_RETURN value and do not return that value.
*/
void call_lambda_command (const char *name, const char *what, const char *args)
{
parse_line_alias_special(name, what, (char *)
#ifdef HAVE_INTPTR_T
(intptr_t)
#endif
args, NULL, 0);
}
/************************************************************************/
/*
* Execute a named user alias block of ircII code as a function. Set up a
* new FUNCTION_RETURN value and return it.
*/
char *call_user_function (const char *alias_name, const char *alias_stuff, char *args, void *arglist)
{
return parse_line_alias_special(alias_name, alias_stuff, args,
arglist, 1);
}
/*
* Execute a named user alias block of ircII code as a command. Do not set
* up a new FUNCTION_RETURN and do not return that value.
*/
void call_user_command (const char *alias_name, const char *alias_stuff, char *args, void *arglist)
{
parse_line_alias_special(alias_name, alias_stuff, args,
arglist, 0);
}
/*
* parse_line_alias_special -- Run a generalized block of code
* Arguments:
* name - If non-NULL, the name of a new atomic scope.
* If NULL, this is not a new atomic scope.
* what - The block of code to execute
* args - The value of $*
* arglist - Local variables to auto-assign, shifting off of $*
* If NULL, do not change $*
* function - If 1, this is a function call. Set up new $function_return
* and return that value. CALLER MUST FREE THIS VALUE.
* If 0, this is not a function call. Return value is NULL.
*
* Special note -- This function is really only suitable for running new atomic
* scopes, because it set up a new /RETURNable block. If you
* just want to run a block of code inside an existing scope
* then use the runcmds() function.
*/
static char * parse_line_alias_special (const char *name, const char *what, const char *orig_subargs, void *arglist, int function)
{
int old_last_function_call_level = last_function_call_level;
char * result = NULL;
int localvars = name ? 1 : 0; /* 'name' could be free()d */
char * subargs;
if (!orig_subargs)
subargs = LOCAL_COPY(empty_string);
else
subargs = LOCAL_COPY(orig_subargs);
if (localvars && !make_local_stack(name))
{
yell("Could not run (%s) [%s]; too much recursion",
name ? name : "<unnamed>", what);
if (function)
return malloc_strdup(empty_string);
else
return NULL;
}
if (arglist)
prepare_alias_call(arglist, &subargs);
if (function)
{
last_function_call_level = wind_index;
add_local_alias("FUNCTION_RETURN", empty_string, 0);
}
will_catch_return_exceptions++;
parse_block(what, subargs, 0);
will_catch_return_exceptions--;
return_exception = 0;
if (function)
{
result = get_variable("FUNCTION_RETURN");
last_function_call_level = old_last_function_call_level;
}
if (localvars)
destroy_local_stack();
return result;
}
void runcmds (const char *what, const char *subargs)
{
if (!subargs)
subargs = empty_string;
parse_block(what, subargs, 0);
}
void runcmds_with_arglist (const char *what, char *args, const char *subargs)
{
ArgList *arglist = NULL;
if (!subargs)
subargs = LOCAL_COPY(empty_string);
arglist = parse_arglist(args);
parse_line_alias_special(NULL, what, subargs, arglist, 0);
destroy_arglist(&arglist);
}
/*
* parse_block: execute a block of ircII statements (in a C string)
*
* Arguments:
* org_line - A block of ircII statements. They will be run in sequence.
* args - The value of $* to use for this block.
* - (DEPRECATED) May be NULL if 'orig_line' is a single
* statement that must not be subject to $-expansion
* (old /load, /sendline, /on input, dumb mode input)
* *** IF args IS NULL, name MUST ALSO BE NULL! ***
* interactive - (DEPRECATED) 1 if this block came from user input.
* 0 if this block came from anywhere else.
* (This is used in parse_statement to decide how to handle /s)
*
* If args is NULL, the 'org_line' argument is passed straight through to
* parse_statement().
*
* If args is not NULL, the 'orig_line' argument is parsed, statement by
* statement, and each statement is passed through to parse_statement().
*/
static void parse_block (const char *org_line, const char *args, int interactive)
{
char *line = NULL;
ssize_t span;
/*
* Explicit statements (from /load or /on input or /sendline)
* are passed straight through to parse_statement without mangling.
*/
if (args == NULL)
{
parse_statement(org_line, interactive, args);
return;
}
/*
* We will be mangling 'org_line', so we make a copy to work with.
*/
if (!org_line)
panic(1, "org_line is NULL and it shouldn't be.");
line = LOCAL_COPY(org_line);
/*
* Otherwise, if the command has arguments, then:
* * It is being run as part of an alias/on/function
* * It is being /LOADed while /set input_aliases ON
* * It is typed at the input prompt while /set input_aliases ON
*/
while (line && *line)
{
/*
* Now we expand the first command in this set, so as to
* include any variables or argument-expandos. The "line"
* pointer is set to the first character of the next command
* (if any).
*/
if ((span = next_statement(line)) < 0)
break;
if (line[span] == ';')
line[span++] = 0;
/*
* Now we run the command.
*/
parse_statement(line, interactive, args);
/*
* If an exception was thrown by this command, then we just
* stop processing and let the exception be caught by whoever
* is catching it above us.
*/
if ((will_catch_break_exceptions && break_exception) ||
(will_catch_return_exceptions && return_exception) ||
(will_catch_continue_exceptions && continue_exception) ||
system_exception)
break;
/* Willfully ignore spaces after semicolons. */
line += span;
while (line && *line && isspace(*line))
line++;
/*
* We repeat this as long as we have more commands to parse.
*/
}
return;
}
/*
* parse_statement: Execute a single statement. Each statement is either a
* block statement (surrounded by {}s), an expression statement (surrounded by
* ()s or starts with a @), or a command statement (anything else).
*
* Args:
* line - A single ircII statement
* interactive - 1 if the statement is run because of user input
* 0 if the statement is run for any other reason
* subargs - The value of $* to use for this statement
* (DEPRECATED) May be NULL if $-expansion is suppressed.
*
* You usually don't want to run this function unless you *know for sure*
* that what you have in hand is a statement and nothing else. If you are
* not sure, better to call runcmds() and let it figure it out.
*
* Known legitimate callers:
* parse_block() (which breaks a block into statements)
* /LOAD -STD (which breaks a file into statements)
* SEND_LINE key binding (which processes the input line as a statement)
* Input in dumb mode (ditto)
* /SENDLINE (which simulates processing of the input line)
*/
int parse_statement (const char *stmt, int interactive, const char *subargs)
{
static unsigned level = 0;
unsigned old_window_display;
int old_display_var;
int cmdchar_used = 0;
int quiet = 0;
char * this_stmt;
if (!stmt || !*stmt)
return 0;
this_stmt = LOCAL_COPY(stmt);
set_current_command(this_stmt);
old_window_display = get_window_display();
old_display_var = get_int_var(DISPLAY_VAR);
if (get_int_var(DEBUG_VAR) & DEBUG_COMMANDS)
privileged_yell("Executing [%d] %s", level, stmt);
level++;
/*
* Once and for all i hope i fixed this. What does this do?
* well, at the beginning of your input line, it looks to see
* if youve used any ^s or /s. You can use up to one ^ and up
* to two /s. When any character is found that is not one of
* these characters, it stops looking.
*/
for (; *stmt; stmt++)
{
/*
* ^ turns off window_display for this statement.
* The user must do /^ at the input line for this.
*/
if (*stmt == '^' && (!interactive || cmdchar_used))
{
if (quiet++ > 1)
break;
}
/* / is the command char. 1 or 2 are allowed */
else if (*stmt == '/')
{
if (cmdchar_used++ > 2)
break;
}
else
break;
}
if (quiet)
set_window_display(0);
/*
* Statement in interactive mode w/o command chars sends the
* statement to the current target.
*/
if (interactive && cmdchar_used == 0)
send_text(from_server, get_window_target(0), stmt, NULL, 1, 0);
/*
* Statement that looks like () {} is an block-with-arglist statement
*/
else if (*stmt == '(')
{
char *arglist;
char *block;
char *copy;
/*
* If any syntax errors are encountered, fall down to
* the expression handler (which handles it in a backwards
* compatable way
*/
copy = LOCAL_COPY(stmt);
if (!(arglist = next_expr(©, '(')))
goto expression_statement;
while (*copy && my_isspace(*copy))
copy++;
if (*copy != '{')
goto expression_statement;
if (!(block = next_expr(©, '{')))
goto expression_statement;
runcmds_with_arglist(block, arglist, subargs);
}
/*
* Statement that starts with a { is a block statement.
*/
else if (*stmt == '{')
{
char *stuff;
char *copy;
copy = LOCAL_COPY(stmt);
if (!(stuff = next_expr(©, '{')))
{
privileged_yell("Unmatched { around [%-.20s]", copy);
stuff = copy + 1;
}
parse_block(stuff, subargs, interactive);
}
/*
* Statement that starts with @ or surrounded by ()s is an
* expression statement.
*/
else if ((*stmt == '@') || (*stmt == '('))
{
/*
* The expression parser wants to mangle the string it's given.
* Normally we would copy the statement as part of the
* expand_alias() step, but since we don't expand expressions,
* we just make a local copy.
*/
char *my_stmt, *tmp;
expression_statement:
my_stmt = LOCAL_COPY(stmt);
/* Expressions can start with @ or be surrounded by ()s */
if (*my_stmt == '(')
{
ssize_t span;
if ((span = MatchingBracket(my_stmt + 1, '(', ')')) >= 0)
my_stmt[1 + span] = 0;
}
if ((tmp = parse_inline(my_stmt + 1, subargs)))
new_free(&tmp);
}
/*
* Everything else whatsoever is a command statement.
*/
else
{
char *cmd, *args;
const char *alias = NULL;
void *arglist = NULL;
void (*builtin) (const char *, char *, const char *) = NULL;
const char *prevcmd = NULL;
if (subargs != NULL)
cmd = expand_alias(stmt, subargs);
else
cmd = malloc_strdup(stmt);
args = cmd;
while (*args && !isspace(*args))
args++;
if (*args)
*args++ = 0;
upper(cmd);
alias = get_cmd_alias(cmd, &arglist, &builtin);
if (cmdchar_used >= 2)
alias = NULL; /* Unconditionally */
if (alias || builtin) {
prevcmd = current_command;
current_command = cmd;
}
if (alias) {
call_user_command(cmd, alias, args, arglist);
}
else if (builtin)
builtin(cmd, args, subargs);
else if (get_int_var(DISPATCH_UNKNOWN_COMMANDS_VAR))
send_to_server("%s %s", cmd, args);
else if (do_hook(UNKNOWN_COMMAND_LIST, "%s%s %s", cmdchar_used >= 2 ? "//" : "", cmd, args))
say("Unknown command: %s", cmd);
if (alias || builtin) {
current_command = prevcmd;
}
new_free(&cmd);
}
/*
* Just as in /LOAD -- if the user just did /set display,
* honor their new value; otherwise, put everything back
* the way we found it.
*/
if (old_display_var != get_int_var(DISPLAY_VAR))
set_window_display(get_int_var(DISPLAY_VAR));
else
set_window_display(old_window_display);
level--;
unset_current_command();
return 0;
}
/***********************************************************************/
BUILT_IN_COMMAND(breakcmd)
{
if (!will_catch_break_exceptions)
say("Cannot BREAK here.");
else
break_exception++;
}
BUILT_IN_COMMAND(continuecmd)
{
if (!will_catch_continue_exceptions)
say("Cannot CONTINUE here.");
else
continue_exception++;
}
BUILT_IN_COMMAND(returncmd)
{
if (!will_catch_return_exceptions)
say("Cannot RETURN here.");
else
{
if (args && *args)
add_local_alias("FUNCTION_RETURN", args, 0);
return_exception++;
}
}
BUILT_IN_COMMAND(botmodecmd)
{
#if defined(NO_BOTS)
yell("This client was configured not to allow bots. Bummer.");
#else
if (dumb_mode) {
use_input = 0;
background = 1;
my_signal(SIGHUP, SIG_IGN);
if (!freopen("/dev/null", "w", stdout))
{
yell("Could not redirect stdout to /dev/null. Can't become a bot.");
return;
}
new_close(0);
if (fork())
_exit(0);
} else {
say("Bot mode can only be entered from Dumb mode.");
}
#endif
}
void help_topics_commands (FILE *f)
{
int x;
for (x = 0; irc_command[x].name; x++)
fprintf(f, "command %s\n", irc_command[x].name);
}
|