1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362
|
/* ========================================================================
* Copyright 1988-2008 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* ========================================================================
*/
/*
* Program: Mailbox Access routines
*
* Author: Mark Crispin
* UW Technology
* University of Washington
* Seattle, WA 98195
* Internet: MRC@Washington.EDU
*
* Date: 22 November 1989
* Last Edited: 15 April 2008
*/
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include "c-client.h"
char *UW_copyright = "Copyright 1988-2007 University of Washington\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n";
/* c-client global data */
/* version of this library */
static char *mailcclientversion = CCLIENTVERSION;
/* list of mail drivers */
static DRIVER *maildrivers = NIL;
/* list of authenticators */
static AUTHENTICATOR *mailauthenticators = NIL;
/* SSL driver pointer */
static NETDRIVER *mailssldriver = NIL;
/* pointer to alternate gets function */
static mailgets_t mailgets = NIL;
/* pointer to read progress function */
static readprogress_t mailreadprogress = NIL;
/* mail cache manipulation function */
static mailcache_t mailcache = mm_cache;
/* RFC-822 output generator */
static rfc822out_t mail822out = NIL;
/* RFC-822 output generator (new style) */
static rfc822outfull_t mail822outfull = NIL;
/* SMTP verbose callback */
static smtpverbose_t mailsmtpverbose = mm_dlog;
/* proxy copy routine */
static mailproxycopy_t mailproxycopy = NIL;
/* RFC-822 external line parse */
static parseline_t mailparseline = NIL;
/* RFC-822 external phrase parser */
static parsephrase_t mailparsephrase = NIL;
static kinit_t mailkinit = NIL; /* application kinit callback */
/* note network sent command */
static sendcommand_t mailsendcommand = NIL;
/* newsrc file name decision function */
static newsrcquery_t mailnewsrcquery = NIL;
/* ACL results callback */
static getacl_t mailaclresults = NIL;
static getannotation_t mailannotationresults = NIL;
/* list rights results callback */
static listrights_t maillistrightsresults = NIL;
/* my rights results callback */
static myrights_t mailmyrightsresults = NIL;
/* quota results callback */
static quota_t mailquotaresults = NIL;
/* quota root results callback */
static quotaroot_t mailquotarootresults = NIL;
/* sorted results callback */
static sortresults_t mailsortresults = NIL;
/* threaded results callback */
static threadresults_t mailthreadresults = NIL;
/* COPY UID results */
static copyuid_t mailcopyuid = NIL;
/* APPEND UID results */
static appenduid_t mailappenduid = NIL;
/* free elt extra stuff callback */
static freeeltsparep_t mailfreeeltsparep = NIL;
/* free envelope extra stuff callback */
static freeenvelopesparep_t mailfreeenvelopesparep = NIL;
/* free body extra stuff callback */
static freebodysparep_t mailfreebodysparep = NIL;
/* free stream extra stuff callback */
static freestreamsparep_t mailfreestreamsparep = NIL;
/* SSL start routine */
static sslstart_t mailsslstart = NIL;
/* SSL certificate query */
static sslcertificatequery_t mailsslcertificatequery = NIL;
/* SSL client certificate */
static sslclientcert_t mailsslclientcert = NIL;
/* SSL client private key */
static sslclientkey_t mailsslclientkey = NIL;
/* SSL failure notify */
static sslfailure_t mailsslfailure = NIL;
/* snarf interval */
static long mailsnarfinterval = 60;
/* snarf preservation */
static long mailsnarfpreserve = NIL;
/* newsrc name uses canonical host */
static long mailnewsrccanon = LONGT;
/* supported threaders */
static THREADER mailthreadordsub = {
"ORDEREDSUBJECT",mail_thread_orderedsubject,NIL
};
static THREADER mailthreadlist = {
"REFERENCES",mail_thread_references,&mailthreadordsub
};
/* server name */
static char *servicename = "unknown";
/* server externally-set authentication ID */
static char *externalauthid = NIL;
static int expungeatping = T; /* mail_ping() may call mm_expunged() */
static int trysslfirst = NIL; /* always try SSL first */
static int notimezones = NIL; /* write timezones in "From " header */
static int trustdns = T; /* do DNS canonicalization */
static int saslusesptrname = T; /* SASL uses name from DNS PTR lookup */
/* trustdns also must be set */
static int debugsensitive = NIL;/* debug telemetry includes sensitive data */
/* Default mail cache handler
* Accepts: pointer to cache handle
* message number
* caching function
* Returns: cache data
*/
void *mm_cache (MAILSTREAM *stream,unsigned long msgno,long op)
{
size_t n;
void *ret = NIL;
unsigned long i;
switch ((int) op) { /* what function? */
case CH_INIT: /* initialize cache */
if (stream->cache) { /* flush old cache contents */
while (stream->cachesize) {
mm_cache (stream,stream->cachesize,CH_FREE);
mm_cache (stream,stream->cachesize--,CH_FREESORTCACHE);
}
fs_give ((void **) &stream->cache);
fs_give ((void **) &stream->sc);
stream->nmsgs = 0; /* can't have any messages now */
}
break;
case CH_SIZE: /* (re-)size the cache */
if (!stream->cache) { /* have a cache already? */
/* no, create new cache */
n = (stream->cachesize = msgno + CACHEINCREMENT) * sizeof (void *);
stream->cache = (MESSAGECACHE **) memset (fs_get (n),0,n);
stream->sc = (SORTCACHE **) memset (fs_get (n),0,n);
}
/* is existing cache size large neough */
else if (msgno > stream->cachesize) {
i = stream->cachesize; /* remember old size */
n = (stream->cachesize = msgno + CACHEINCREMENT) * sizeof (void *);
fs_resize ((void **) &stream->cache,n);
fs_resize ((void **) &stream->sc,n);
while (i < stream->cachesize) {
stream->cache[i] = NIL;
stream->sc[i++] = NIL;
}
}
break;
case CH_MAKEELT: /* return elt, make if necessary */
if (!stream->cache[msgno - 1])
stream->cache[msgno - 1] = mail_new_cache_elt (msgno);
/* falls through */
case CH_ELT: /* return elt */
ret = (void *) stream->cache[msgno - 1];
break;
case CH_SORTCACHE: /* return sortcache entry, make if needed */
if (!stream->sc[msgno - 1]) stream->sc[msgno - 1] =
(SORTCACHE *) memset (fs_get (sizeof (SORTCACHE)),0,sizeof (SORTCACHE));
ret = (void *) stream->sc[msgno - 1];
break;
case CH_FREE: /* free elt */
mail_free_elt (&stream->cache[msgno - 1]);
break;
case CH_FREESORTCACHE:
if (stream->sc[msgno - 1]) {
if (stream->sc[msgno - 1]->from)
fs_give ((void **) &stream->sc[msgno - 1]->from);
if (stream->sc[msgno - 1]->to)
fs_give ((void **) &stream->sc[msgno - 1]->to);
if (stream->sc[msgno - 1]->cc)
fs_give ((void **) &stream->sc[msgno - 1]->cc);
if (stream->sc[msgno - 1]->subject)
fs_give ((void **) &stream->sc[msgno - 1]->subject);
if (stream->sc[msgno - 1]->unique &&
(stream->sc[msgno - 1]->unique != stream->sc[msgno - 1]->message_id))
fs_give ((void **) &stream->sc[msgno - 1]->unique);
if (stream->sc[msgno - 1]->message_id)
fs_give ((void **) &stream->sc[msgno - 1]->message_id);
if (stream->sc[msgno - 1]->references)
mail_free_stringlist (&stream->sc[msgno - 1]->references);
fs_give ((void **) &stream->sc[msgno - 1]);
}
break;
case CH_EXPUNGE: /* expunge cache slot */
for (i = msgno - 1; msgno < stream->nmsgs; i++,msgno++) {
if (stream->cache[i] = stream->cache[msgno])
stream->cache[i]->msgno = msgno;
stream->sc[i] = stream->sc[msgno];
}
stream->cache[i] = NIL; /* top of cache goes away */
stream->sc[i] = NIL;
break;
default:
fatal ("Bad mm_cache op");
break;
}
return ret;
}
/* Dummy string driver for complete in-memory strings */
static void mail_string_init (STRING *s,void *data,unsigned long size);
static char mail_string_next (STRING *s);
static void mail_string_setpos (STRING *s,unsigned long i);
STRINGDRIVER mail_string = {
mail_string_init, /* initialize string structure */
mail_string_next, /* get next byte in string structure */
mail_string_setpos /* set position in string structure */
};
/* Initialize mail string structure for in-memory string
* Accepts: string structure
* pointer to string
* size of string
*/
static void mail_string_init (STRING *s,void *data,unsigned long size)
{
/* set initial string pointers */
s->chunk = s->curpos = (char *) (s->data = data);
/* and sizes */
s->size = s->chunksize = s->cursize = size;
s->data1 = s->offset = 0; /* never any offset */
}
/* Get next character from string
* Accepts: string structure
* Returns: character, string structure chunk refreshed
*/
static char mail_string_next (STRING *s)
{
return *s->curpos++; /* return the last byte */
}
/* Set string pointer position
* Accepts: string structure
* new position
*/
static void mail_string_setpos (STRING *s,unsigned long i)
{
s->curpos = s->chunk + i; /* set new position */
s->cursize = s->chunksize - i;/* and new size */
}
/* Mail routines
*
* mail_xxx routines are the interface between this module and the outside
* world. Only these routines should be referenced by external callers.
*
* Note that there is an important difference between a "sequence" and a
* "message #" (msgno). A sequence is a string representing a sequence in
* {"n", "n:m", or combination separated by commas} format, whereas a msgno
* is a single integer.
*
*/
/* Mail version check
* Accepts: version
*/
void mail_versioncheck (char *version)
{
/* Skip unnecessarily strict version check. */
;
}
/* Mail link driver
* Accepts: driver to add to list
*/
void mail_link (DRIVER *driver)
{
DRIVER **d = &maildrivers;
while (*d) d = &(*d)->next; /* find end of list of drivers */
*d = driver; /* put driver at the end */
driver->next = NIL; /* this driver is the end of the list */
}
/* Mail manipulate driver parameters
* Accepts: mail stream
* function code
* function-dependent value
* Returns: function-dependent return value
*/
void *mail_parameters (MAILSTREAM *stream,long function,void *value)
{
void *r,*ret = NIL;
DRIVER *d;
AUTHENTICATOR *a;
switch ((int) function) {
case SET_INBOXPATH:
fatal ("SET_INBOXPATH not permitted");
case GET_INBOXPATH:
if ((stream || (stream = mail_open (NIL,"INBOX",OP_PROTOTYPE))) &&
stream->dtb) ret = (*stream->dtb->parameters) (function,value);
break;
case SET_THREADERS:
fatal ("SET_THREADERS not permitted");
case GET_THREADERS: /* use stream dtb instead of global */
ret = (stream && stream->dtb) ?
/* KLUDGE ALERT: note stream passed as value */
(*stream->dtb->parameters) (function,stream) : (void *) &mailthreadlist;
break;
case SET_NAMESPACE:
fatal ("SET_NAMESPACE not permitted");
break;
case SET_NEWSRC: /* too late on open stream */
if (stream && stream->dtb && (stream != ((*stream->dtb->open) (NIL))))
fatal ("SET_NEWSRC not permitted");
else ret = env_parameters (function,value);
break;
case GET_NAMESPACE:
case GET_NEWSRC: /* use stream dtb instead of environment */
ret = (stream && stream->dtb) ?
/* KLUDGE ALERT: note stream passed as value */
(*stream->dtb->parameters) (function,stream) :
env_parameters (function,value);
break;
case ENABLE_DEBUG:
fatal ("ENABLE_DEBUG not permitted");
case DISABLE_DEBUG:
fatal ("DISABLE_DEBUG not permitted");
case SET_DIRFMTTEST:
fatal ("SET_DIRFMTTEST not permitted");
case GET_DIRFMTTEST:
if (!(stream && stream->dtb &&
(ret = (*stream->dtb->parameters) (function,NIL))))
fatal ("GET_DIRFMTTEST not permitted");
break;
case SET_DRIVERS:
fatal ("SET_DRIVERS not permitted");
case GET_DRIVERS: /* always return global */
ret = (void *) maildrivers;
break;
case SET_DRIVER:
fatal ("SET_DRIVER not permitted");
case GET_DRIVER:
for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
d = d->next);
ret = (void *) d;
break;
case ENABLE_DRIVER:
for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
d = d->next);
if (ret = (void *) d) d->flags &= ~DR_DISABLE;
break;
case DISABLE_DRIVER:
for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
d = d->next);
if (ret = (void *) d) d->flags |= DR_DISABLE;
break;
case ENABLE_AUTHENTICATOR:
for (a = mailauthenticators;/* scan authenticators */
a && compare_cstring (a->name,(char *) value); a = a->next);
if (ret = (void *) a) a->flags &= ~AU_DISABLE;
break;
case DISABLE_AUTHENTICATOR:
for (a = mailauthenticators;/* scan authenticators */
a && compare_cstring (a->name,(char *) value); a = a->next);
if (ret = (void *) a) a->flags |= AU_DISABLE;
break;
case UNHIDE_AUTHENTICATOR:
for (a = mailauthenticators;/* scan authenticators */
a && compare_cstring (a->name,(char *) value); a = a->next);
if (ret = (void *) a) a->flags &= ~AU_HIDE;
break;
case HIDE_AUTHENTICATOR:
for (a = mailauthenticators;/* scan authenticators */
a && compare_cstring (a->name,(char *) value); a = a->next);
if (ret = (void *) a) a->flags |= AU_HIDE;
break;
case SET_EXTERNALAUTHID:
if (value) { /* setting external authentication ID */
externalauthid = cpystr ((char *) value);
mail_parameters (NIL,UNHIDE_AUTHENTICATOR,"EXTERNAL");
}
else { /* clearing external authentication ID */
if (externalauthid) fs_give ((void **) &externalauthid);
mail_parameters (NIL,HIDE_AUTHENTICATOR,"EXTERNAL");
}
case GET_EXTERNALAUTHID:
ret = (void *) externalauthid;
break;
case SET_GETS:
mailgets = (mailgets_t) value;
case GET_GETS:
ret = (void *) mailgets;
break;
case SET_READPROGRESS:
mailreadprogress = (readprogress_t) value;
case GET_READPROGRESS:
ret = (void *) mailreadprogress;
break;
case SET_CACHE:
mailcache = (mailcache_t) value;
case GET_CACHE:
ret = (void *) mailcache;
break;
case SET_RFC822OUTPUT:
mail822out = (rfc822out_t) value;
case GET_RFC822OUTPUT:
ret = (void *) mail822out;
break;
case SET_RFC822OUTPUTFULL:
mail822outfull = (rfc822outfull_t) value;
case GET_RFC822OUTPUTFULL:
ret = (void *) mail822outfull;
break;
case SET_SMTPVERBOSE:
mailsmtpverbose = (smtpverbose_t) value;
case GET_SMTPVERBOSE:
ret = (void *) mailsmtpverbose;
break;
case SET_MAILPROXYCOPY:
mailproxycopy = (mailproxycopy_t) value;
case GET_MAILPROXYCOPY:
ret = (void *) mailproxycopy;
break;
case SET_PARSELINE:
mailparseline = (parseline_t) value;
case GET_PARSELINE:
ret = (void *) mailparseline;
break;
case SET_PARSEPHRASE:
mailparsephrase = (parsephrase_t) value;
case GET_PARSEPHRASE:
ret = (void *) mailparsephrase;
break;
case SET_NEWSRCQUERY:
mailnewsrcquery = (newsrcquery_t) value;
case GET_NEWSRCQUERY:
ret = (void *) mailnewsrcquery;
break;
case SET_NEWSRCCANONHOST:
mailnewsrccanon = (long) value;
case GET_NEWSRCCANONHOST:
ret = (void *) mailnewsrccanon;
break;
case SET_COPYUID:
mailcopyuid = (copyuid_t) value;
case GET_COPYUID:
ret = (void *) mailcopyuid;
break;
case SET_APPENDUID:
mailappenduid = (appenduid_t) value;
case GET_APPENDUID:
ret = (void *) mailappenduid;
break;
case SET_FREEENVELOPESPAREP:
mailfreeenvelopesparep = (freeenvelopesparep_t) value;
case GET_FREEENVELOPESPAREP:
ret = (void *) mailfreeenvelopesparep;
break;
case SET_FREEELTSPAREP:
mailfreeeltsparep = (freeeltsparep_t) value;
case GET_FREEELTSPAREP:
ret = (void *) mailfreeeltsparep;
break;
case SET_FREESTREAMSPAREP:
mailfreestreamsparep = (freestreamsparep_t) value;
case GET_FREESTREAMSPAREP:
ret = (void *) mailfreestreamsparep;
break;
case SET_FREEBODYSPAREP:
mailfreebodysparep = (freebodysparep_t) value;
case GET_FREEBODYSPAREP:
ret = (void *) mailfreebodysparep;
break;
case SET_SSLSTART:
mailsslstart = (sslstart_t) value;
case GET_SSLSTART:
ret = (void *) mailsslstart;
break;
case SET_SSLCERTIFICATEQUERY:
mailsslcertificatequery = (sslcertificatequery_t) value;
case GET_SSLCERTIFICATEQUERY:
ret = (void *) mailsslcertificatequery;
break;
case SET_SSLCLIENTCERT:
mailsslclientcert = (sslclientcert_t) value;
case GET_SSLCLIENTCERT:
ret = (void *) mailsslclientcert;
break;
case SET_SSLCLIENTKEY:
mailsslclientkey = (sslclientkey_t) value;
case GET_SSLCLIENTKEY:
ret = (void *) mailsslclientkey;
break;
case SET_SSLFAILURE:
mailsslfailure = (sslfailure_t) value;
case GET_SSLFAILURE:
ret = (void *) mailsslfailure;
break;
case SET_KINIT:
mailkinit = (kinit_t) value;
case GET_KINIT:
ret = (void *) mailkinit;
break;
case SET_SENDCOMMAND:
mailsendcommand = (sendcommand_t) value;
case GET_SENDCOMMAND:
ret = (void *) mailsendcommand;
break;
case SET_SERVICENAME:
servicename = (char *) value;
case GET_SERVICENAME:
ret = (void *) servicename;
break;
case SET_EXPUNGEATPING:
expungeatping = (value ? T : NIL);
case GET_EXPUNGEATPING:
ret = (void *) (expungeatping ? VOIDT : NIL);
break;
case SET_SORTRESULTS:
mailsortresults = (sortresults_t) value;
case GET_SORTRESULTS:
ret = (void *) mailsortresults;
break;
case SET_THREADRESULTS:
mailthreadresults = (threadresults_t) value;
case GET_THREADRESULTS:
ret = (void *) mailthreadresults;
break;
case SET_SSLDRIVER:
mailssldriver = (NETDRIVER *) value;
case GET_SSLDRIVER:
ret = (void *) mailssldriver;
break;
case SET_TRYSSLFIRST:
trysslfirst = (value ? T : NIL);
case GET_TRYSSLFIRST:
ret = (void *) (trysslfirst ? VOIDT : NIL);
break;
case SET_NOTIMEZONES:
notimezones = (value ? T : NIL);
case GET_NOTIMEZONES:
ret = (void *) (notimezones ? VOIDT : NIL);
break;
case SET_TRUSTDNS:
trustdns = (value ? T : NIL);
case GET_TRUSTDNS:
ret = (void *) (trustdns ? VOIDT : NIL);
break;
case SET_SASLUSESPTRNAME:
saslusesptrname = (value ? T : NIL);
case GET_SASLUSESPTRNAME:
ret = (void *) (saslusesptrname ? VOIDT : NIL);
break;
case SET_DEBUGSENSITIVE:
debugsensitive = (value ? T : NIL);
case GET_DEBUGSENSITIVE:
ret = (void *) (debugsensitive ? VOIDT : NIL);
break;
case SET_ANNOTATION:
mailannotationresults = (getannotation_t) value;
case GET_ANNOTATION:
ret = (void *) mailannotationresults;
break;
case SET_ACL:
mailaclresults = (getacl_t) value;
case GET_ACL:
ret = (void *) mailaclresults;
break;
case SET_LISTRIGHTS:
maillistrightsresults = (listrights_t) value;
case GET_LISTRIGHTS:
ret = (void *) maillistrightsresults;
break;
case SET_MYRIGHTS:
mailmyrightsresults = (myrights_t) value;
case GET_MYRIGHTS:
ret = (void *) mailmyrightsresults;
break;
case SET_QUOTA:
mailquotaresults = (quota_t) value;
case GET_QUOTA:
ret = (void *) mailquotaresults;
break;
case SET_QUOTAROOT:
mailquotarootresults = (quotaroot_t) value;
case GET_QUOTAROOT:
ret = (void *) mailquotarootresults;
break;
case SET_SNARFINTERVAL:
mailsnarfinterval = (long) value;
case GET_SNARFINTERVAL:
ret = (void *) mailsnarfinterval;
break;
case SET_SNARFPRESERVE:
mailsnarfpreserve = (long) value;
case GET_SNARFPRESERVE:
ret = (void *) mailsnarfpreserve;
break;
case SET_SNARFMAILBOXNAME:
if (stream) { /* have a stream? */
if (stream->snarf.name) fs_give ((void **) &stream->snarf.name);
stream->snarf.name = cpystr ((char *) value);
}
else fatal ("SET_SNARFMAILBOXNAME with no stream");
case GET_SNARFMAILBOXNAME:
if (stream) ret = (void *) stream->snarf.name;
break;
default:
if (r = smtp_parameters (function,value)) ret = r;
if (r = env_parameters (function,value)) ret = r;
if (r = tcp_parameters (function,value)) ret = r;
if (stream && stream->dtb) {/* if have stream, do for its driver only */
if (r = (*stream->dtb->parameters) (function,value)) ret = r;
}
/* else do all drivers */
else for (d = maildrivers; d; d = d->next)
if (r = (d->parameters) (function,value)) ret = r;
break;
}
return ret;
}
/* Mail validate mailbox name
* Accepts: MAIL stream
* mailbox name
* purpose string for error message
* Return: driver factory on success, NIL on failure
*/
DRIVER *mail_valid (MAILSTREAM *stream,char *mailbox,char *purpose)
{
char tmp[MAILTMPLEN];
DRIVER *factory = NIL;
/* never allow names with newlines */
if (strpbrk (mailbox,"\015\012")) {
if (purpose) { /* if want an error message */
sprintf (tmp,"Can't %s with such a name",purpose);
MM_LOG (tmp,ERROR);
}
return NIL;
}
/* validate name, find driver factory */
if (strlen (mailbox) < (NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50))
for (factory = maildrivers; factory &&
((factory->flags & DR_DISABLE) ||
((factory->flags & DR_LOCAL) && (*mailbox == '{')) ||
!(*factory->valid) (mailbox));
factory = factory->next);
/* validate factory against non-dummy stream */
if (factory && stream && stream->dtb && (stream->dtb != factory) &&
strcmp (stream->dtb->name,"dummy"))
/* factory invalid; if dummy, use stream */
factory = strcmp (factory->name,"dummy") ? NIL : stream->dtb;
if (!factory && purpose) { /* if want an error message */
sprintf (tmp,"Can't %s %.80s: %s",purpose,mailbox,(*mailbox == '{') ?
"invalid remote specification" : "no such mailbox");
MM_LOG (tmp,ERROR);
}
return factory; /* return driver factory */
}
/* Mail validate network mailbox name
* Accepts: mailbox name
* mailbox driver to validate against
* pointer to where to return host name if non-NIL
* pointer to where to return mailbox name if non-NIL
* Returns: driver on success, NIL on failure
*/
DRIVER *mail_valid_net (char *name,DRIVER *drv,char *host,char *mailbox)
{
NETMBX mb;
if (!mail_valid_net_parse (name,&mb) || strcmp (mb.service,drv->name))
return NIL;
if (host) strcpy (host,mb.host);
if (mailbox) strcpy (mailbox,mb.mailbox);
return drv;
}
/* Mail validate network mailbox name
* Accepts: mailbox name
* NETMBX structure to return values
* Returns: T on success, NIL on failure
*/
long mail_valid_net_parse (char *name,NETMBX *mb)
{
return mail_valid_net_parse_work (name,mb,"imap");
}
/* Mail validate network mailbox name worker routine
* Accepts: mailbox name
* NETMBX structure to return values
* default service
* Returns: T on success, NIL on failure
*/
long mail_valid_net_parse_work (char *name,NETMBX *mb,char *service)
{
int i,j;
char c,*s,*t,*v,tmp[MAILTMPLEN],arg[MAILTMPLEN];
/* initialize structure */
memset (mb,'\0',sizeof (NETMBX));
/* must have host specification */
if (*name++ != '{') return NIL;
if (*name == '[') { /* if domain literal, find its ending */
if (!((v = strpbrk (name,"]}")) && (*v++ == ']'))) return NIL;
}
/* find end of host name */
else if (!(v = strpbrk (name,"/:}"))) return NIL;
/* validate length, find mailbox part */
if (!((i = v - name) && (i < NETMAXHOST) && (t = strchr (v,'}')) &&
((j = t - v) < MAILTMPLEN) && (strlen (t+1) < (size_t) NETMAXMBX)))
return NIL; /* invalid mailbox */
strncpy (mb->host,name,i); /* set host name */
strncpy (mb->orighost,name,i);
mb->host[i] = mb->orighost[i] = '\0';
strcpy (mb->mailbox,t+1); /* set mailbox name */
if (t - v) { /* any switches or port specification? */
strncpy (t = tmp,v,j); /* copy it */
tmp[j] = '\0'; /* tie it off */
c = *t++; /* get first delimiter */
do switch (c) { /* act based upon the character */
case ':': /* port specification */
if (mb->port || !(mb->port = strtoul (t,&t,10))) return NIL;
c = t ? *t++ : '\0'; /* get delimiter, advance pointer */
break;
case '/': /* switch */
/* find delimiter */
if (t = strpbrk (s = t,"/:=")) {
c = *t; /* remember delimiter for later */
*t++ = '\0'; /* tie off switch name */
}
else c = '\0'; /* no delimiter */
if (c == '=') { /* parse switches which take arguments */
if (*t == '"') { /* quoted string? */
for (v = arg,i = 0,++t; (c = *t++) != '"';) {
if (!c) return NIL; /* unterminated string */
/* quote next character */
if (c == '\\') c = *t++;
if (!c) return NIL; /* can't quote NUL either */
arg[i++] = c;
}
c = *t++; /* remember delimiter for later */
arg[i] = '\0'; /* tie off argument */
}
else { /* non-quoted argument */
if (t = strpbrk (v = t,"/:")) {
c = *t; /* remember delimiter for later */
*t++ = '\0'; /* tie off switch name */
}
else c = '\0'; /* no delimiter */
i = strlen (v); /* length of argument */
}
if (!compare_cstring (s,"service") && (i < NETMAXSRV) && !*mb->service)
lcase (strcpy (mb->service,v));
else if (!compare_cstring (s,"user") && (i < NETMAXUSER) && !*mb->user)
strcpy (mb->user,v);
else if (!compare_cstring (s,"authuser") && (i < NETMAXUSER) &&
!*mb->authuser) strcpy (mb->authuser,v);
else return NIL;
}
else { /* non-argument switch */
if (!compare_cstring (s,"anonymous")) mb->anoflag = T;
else if (!compare_cstring (s,"debug")) mb->dbgflag = T;
else if (!compare_cstring (s,"readonly")) mb->readonlyflag = T;
else if (!compare_cstring (s,"secure")) mb->secflag = T;
else if (!compare_cstring (s,"norsh")) mb->norsh = T;
else if (!compare_cstring (s,"loser")) mb->loser = T;
else if (!compare_cstring (s,"tls") && !mb->notlsflag)
mb->tlsflag = T;
else if (!compare_cstring (s,"tls-sslv23") && !mb->notlsflag)
mb->tlssslv23 = mb->tlsflag = T;
else if (!compare_cstring (s,"notls") && !mb->tlsflag)
mb->notlsflag = T;
else if (!compare_cstring (s,"tryssl"))
mb->trysslflag = mailssldriver? T : NIL;
else if (mailssldriver && !compare_cstring (s,"ssl") && !mb->tlsflag)
mb->sslflag = mb->notlsflag = T;
else if (mailssldriver && !compare_cstring (s,"novalidate-cert"))
mb->novalidate = T;
/* hack for compatibility with the past */
else if (mailssldriver && !compare_cstring (s,"validate-cert"));
/* service switches below here */
else if (*mb->service) return NIL;
else if (!compare_cstring (s,"imap") ||
!compare_cstring (s,"nntp") ||
!compare_cstring (s,"pop3") ||
!compare_cstring (s,"smtp") ||
!compare_cstring (s,"submit"))
lcase (strcpy (mb->service,s));
else if (!compare_cstring (s,"imap2") ||
!compare_cstring (s,"imap2bis") ||
!compare_cstring (s,"imap4") ||
!compare_cstring (s,"imap4rev1"))
strcpy (mb->service,"imap");
else if (!compare_cstring (s,"pop"))
strcpy (mb->service,"pop3");
else return NIL; /* invalid non-argument switch */
}
break;
default: /* anything else is bogus */
return NIL;
} while (c); /* see if anything more to parse */
}
/* default mailbox name */
if (!*mb->mailbox) strcpy (mb->mailbox,"INBOX");
/* default service name */
if (!*mb->service) strcpy (mb->service,service);
/* /norsh only valid if imap */
if (mb->norsh && strcmp (mb->service,"imap")) return NIL;
return T;
}
/* Mail scan mailboxes for string
* Accepts: mail stream
* reference
* pattern to search
* contents to search
*/
void mail_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
{
int remote = ((*pat == '{') || (ref && *ref == '{'));
DRIVER *d;
if (ref && (strlen (ref) > NETMAXMBX)) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LIST reference specification: %.80s",ref);
MM_LOG (tmp,ERROR);
return;
}
if (strlen (pat) > NETMAXMBX) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LIST pattern specification: %.80s",pat);
MM_LOG (tmp,ERROR);
return;
}
if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
if (stream) { /* if have a stream, do it for that stream */
if ((d = stream->dtb) && d->scan &&
!(((d->flags & DR_LOCAL) && remote)))
(*d->scan) (stream,ref,pat,contents);
}
/* otherwise do for all DTB's */
else for (d = maildrivers; d; d = d->next)
if (d->scan && !((d->flags & DR_DISABLE) ||
((d->flags & DR_LOCAL) && remote)))
(d->scan) (NIL,ref,pat,contents);
}
/* Mail list mailboxes
* Accepts: mail stream
* reference
* pattern to search
*/
void mail_list (MAILSTREAM *stream,char *ref,char *pat)
{
int remote = ((*pat == '{') || (ref && *ref == '{'));
DRIVER *d = maildrivers;
if (ref && (strlen (ref) > NETMAXMBX)) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LIST reference specification: %.80s",ref);
MM_LOG (tmp,ERROR);
return;
}
if (strlen (pat) > NETMAXMBX) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LIST pattern specification: %.80s",pat);
MM_LOG (tmp,ERROR);
return;
}
if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
if (stream && stream->dtb) { /* if have a stream, do it for that stream */
if (!(((d = stream->dtb)->flags & DR_LOCAL) && remote))
(*d->list) (stream,ref,pat);
}
/* otherwise do for all DTB's */
else do if (!((d->flags & DR_DISABLE) ||
((d->flags & DR_LOCAL) && remote)))
(d->list) (NIL,ref,pat);
while (d = d->next); /* until at the end */
}
/* Mail list subscribed mailboxes
* Accepts: mail stream
* pattern to search
*/
void mail_lsub (MAILSTREAM *stream,char *ref,char *pat)
{
int remote = ((*pat == '{') || (ref && *ref == '{'));
DRIVER *d = maildrivers;
if (ref && (strlen (ref) > NETMAXMBX)) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LSUB reference specification: %.80s",ref);
MM_LOG (tmp,ERROR);
return;
}
if (strlen (pat) > NETMAXMBX) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Invalid LSUB pattern specification: %.80s",pat);
MM_LOG (tmp,ERROR);
return;
}
if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
if (stream && stream->dtb) { /* if have a stream, do it for that stream */
if (!(((d = stream->dtb)->flags & DR_LOCAL) && remote))
(*d->lsub) (stream,ref,pat);
}
/* otherwise do for all DTB's */
else do if (!((d->flags & DR_DISABLE) ||
((d->flags & DR_LOCAL) && remote)))
(d->lsub) (NIL,ref,pat);
while (d = d->next); /* until at the end */
}
/* Mail subscribe to mailbox
* Accepts: mail stream
* mailbox to add to subscription list
* Returns: T on success, NIL on failure
*/
long mail_subscribe (MAILSTREAM *stream,char *mailbox)
{
DRIVER *factory = mail_valid (stream,mailbox,"subscribe to mailbox");
return factory ?
(factory->subscribe ?
(*factory->subscribe) (stream,mailbox) : sm_subscribe (mailbox)) : NIL;
}
/* Mail unsubscribe to mailbox
* Accepts: mail stream
* mailbox to delete from subscription list
* Returns: T on success, NIL on failure
*/
long mail_unsubscribe (MAILSTREAM *stream,char *mailbox)
{
DRIVER *factory = mail_valid (stream,mailbox,NIL);
return (factory && factory->unsubscribe) ?
(*factory->unsubscribe) (stream,mailbox) : sm_unsubscribe (mailbox);
}
/* Mail create mailbox
* Accepts: mail stream
* mailbox name to create
* Returns: T on success, NIL on failure
*/
long mail_create (MAILSTREAM *stream,char *mailbox)
{
MAILSTREAM *ts;
char *s,*t,tmp[MAILTMPLEN];
size_t i;
DRIVER *d;
/* never allow names with newlines */
if (s = strpbrk (mailbox,"\015\012")) {
MM_LOG ("Can't create mailbox with such a name",ERROR);
return NIL;
}
if (strlen (mailbox) >= (NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50)) {
sprintf (tmp,"Can't create %.80s: %s",mailbox,(*mailbox == '{') ?
"invalid remote specification" : "no such mailbox");
MM_LOG (tmp,ERROR);
return NIL;
}
/* create of INBOX invalid */
if (!compare_cstring (mailbox,"INBOX")) {
MM_LOG ("Can't create INBOX",ERROR);
return NIL;
}
/* validate name */
if (s = mail_utf7_valid (mailbox)) {
sprintf (tmp,"Can't create %s: %.80s",s,mailbox);
MM_LOG (tmp,ERROR);
return NIL;
}
/* see if special driver hack */
if ((mailbox[0] == '#') && ((mailbox[1] == 'd') || (mailbox[1] == 'D')) &&
((mailbox[2] == 'r') || (mailbox[2] == 'R')) &&
((mailbox[3] == 'i') || (mailbox[3] == 'I')) &&
((mailbox[4] == 'v') || (mailbox[4] == 'V')) &&
((mailbox[5] == 'e') || (mailbox[5] == 'E')) &&
((mailbox[6] == 'r') || (mailbox[6] == 'R')) && (mailbox[7] == '.')) {
/* copy driver until likely delimiter */
if ((s = strpbrk (t = mailbox+8,"/\\:")) && (i = s - t)) {
strncpy (tmp,t,i);
tmp[i] = '\0';
}
else {
sprintf (tmp,"Can't create mailbox %.80s: bad driver syntax",mailbox);
MM_LOG (tmp,ERROR);
return NIL;
}
for (d = maildrivers; d && strcmp (d->name,tmp); d = d->next);
if (d) mailbox = ++s; /* skip past driver specification */
else {
sprintf (tmp,"Can't create mailbox %.80s: unknown driver",mailbox);
MM_LOG (tmp,ERROR);
return NIL;
}
}
/* use stream if one given or deterministic */
else if ((stream && stream->dtb) ||
(((*mailbox == '{') || (*mailbox == '#')) &&
(stream = mail_open (NIL,mailbox,OP_PROTOTYPE | OP_SILENT))))
d = stream->dtb;
else if ((*mailbox != '{') && (ts = default_proto (NIL))) d = ts->dtb;
else { /* failed utterly */
sprintf (tmp,"Can't create mailbox %.80s: indeterminate format",mailbox);
MM_LOG (tmp,ERROR);
return NIL;
}
return (*d->create) (stream,mailbox);
}
/* Mail delete mailbox
* Accepts: mail stream
* mailbox name to delete
* Returns: T on success, NIL on failure
*/
long mail_delete (MAILSTREAM *stream,char *mailbox)
{
DRIVER *dtb = mail_valid (stream,mailbox,"delete mailbox");
if (!dtb) return NIL;
if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) &&
((mailbox[1] == 'N') || (mailbox[1] == 'n')) &&
((mailbox[2] == 'B') || (mailbox[2] == 'b')) &&
((mailbox[3] == 'O') || (mailbox[3] == 'o')) &&
((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5]) {
MM_LOG ("Can't delete INBOX",ERROR);
return NIL;
}
return SAFE_DELETE (dtb,stream,mailbox);
}
/* Mail rename mailbox
* Accepts: mail stream
* old mailbox name
* new mailbox name
* Returns: T on success, NIL on failure
*/
long mail_rename (MAILSTREAM *stream,char *old,char *newname)
{
char *s,tmp[MAILTMPLEN];
DRIVER *dtb = mail_valid (stream,old,"rename mailbox");
if (!dtb) return NIL;
/* validate name */
if (s = mail_utf7_valid (newname)) {
sprintf (tmp,"Can't rename to %s: %.80s",s,newname);
MM_LOG (tmp,ERROR);
return NIL;
}
if ((*old != '{') && (*old != '#') && mail_valid (NIL,newname,NIL)) {
sprintf (tmp,"Can't rename %.80s: mailbox %.80s already exists",
old,newname);
MM_LOG (tmp,ERROR);
return NIL;
}
return SAFE_RENAME (dtb,stream,old,newname);
}
/* Validate mailbox as Modified UTF-7
* Accepts: candidate mailbox name
* Returns: error string if error, NIL if valid
*/
char *mail_utf7_valid (char *mailbox)
{
char *s;
for (s = mailbox; *s; s++) { /* make sure valid name */
/* reserved for future use with UTF-8 */
if (*s & 0x80) return "mailbox name with 8-bit octet";
/* validate modified UTF-7 */
else if (*s == '&') while (*++s != '-') switch (*s) {
case '\0':
return "unterminated modified UTF-7 name";
case '+': /* valid modified BASE64 */
case ',':
break; /* all OK so far */
default: /* must be alphanumeric */
if (!isalnum (*s)) return "invalid modified UTF-7 name";
break;
}
}
return NIL; /* all OK */
}
/* Mail status of mailbox
* Accepts: mail stream if open on this mailbox
* mailbox name
* status flags
* Returns: T on success, NIL on failure
*/
long mail_status (MAILSTREAM *stream,char *mbx,long flags)
{
DRIVER *dtb = mail_valid (stream,mbx,"get status of mailbox");
if (!dtb) return NIL; /* only if valid */
if (stream && ((dtb != stream->dtb) ||
((dtb->flags & DR_LOCAL) && strcmp (mbx,stream->mailbox) &&
strcmp (mbx,stream->original_mailbox))))
stream = NIL; /* stream not suitable */
return SAFE_STATUS (dtb,stream,mbx,flags);
}
/* Mail status of mailbox default handler
* Accepts: mail stream
* mailbox name
* status flags
* Returns: T on success, NIL on failure
*/
long mail_status_default (MAILSTREAM *stream,char *mbx,long flags)
{
MAILSTATUS status;
unsigned long i;
MAILSTREAM *tstream = NIL;
/* make temporary stream (unless this mbx) */
if (!stream && !(stream = tstream =
mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
status.flags = flags; /* return status values */
status.messages = stream->nmsgs;
status.recent = stream->recent;
if (flags & SA_UNSEEN) /* must search to get unseen messages */
for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
if (!mail_elt (stream,i)->seen) status.unseen++;
status.uidnext = stream->uid_last + 1;
status.uidvalidity = stream->uid_validity;
MM_STATUS(stream,mbx,&status);/* pass status to main program */
if (tstream) mail_close (tstream);
return T; /* success */
}
/* Mail open
* Accepts: candidate stream for recycling
* mailbox name
* open options
* Returns: stream to use on success, NIL on failure
*/
MAILSTREAM *mail_open (MAILSTREAM *stream,char *name,long options)
{
int i;
char c,*s,tmp[MAILTMPLEN];
NETMBX mb;
DRIVER *d;
switch (name[0]) { /* see if special handling */
case '#': /* possible special hacks */
if (((name[1] == 'M') || (name[1] == 'm')) &&
((name[2] == 'O') || (name[2] == 'o')) &&
((name[3] == 'V') || (name[3] == 'v')) &&
((name[4] == 'E') || (name[4] == 'e')) && (c = name[5]) &&
(s = strchr (name+6,c)) && (i = s - (name + 6)) && (i < MAILTMPLEN)) {
if (stream = mail_open (stream,s+1,options)) {
strncpy (tmp,name+6,i); /* copy snarf mailbox name */
tmp[i] = '\0'; /* tie off name */
mail_parameters (stream,SET_SNARFMAILBOXNAME,(void *) tmp);
stream->snarf.options = options;
mail_ping (stream); /* do initial snarf */
/* punt if can't do initial snarf */
if (!stream->snarf.time) stream = mail_close (stream);
}
return stream;
}
/* special POP hack */
else if (((name[1] == 'P') || (name[1] == 'p')) &&
((name[2] == 'O') || (name[2] == 'o')) &&
((name[3] == 'P') || (name[3] == 'p')) &&
mail_valid_net_parse_work (name+4,&mb,"pop3") &&
!strcmp (mb.service,"pop3") && !mb.anoflag && !mb.readonlyflag) {
if (stream = mail_open (stream,mb.mailbox,options)) {
sprintf (tmp,"{%.255s",mb.host);
if (mb.port) sprintf (tmp + strlen (tmp),":%lu",mb.port);
if (mb.user[0]) sprintf (tmp + strlen (tmp),"/user=%.64s",mb.user);
if (mb.dbgflag) strcat (tmp,"/debug");
if (mb.secflag) strcat (tmp,"/secure");
if (mb.tlsflag) strcat (tmp,"/tls");
if (mb.notlsflag) strcat (tmp,"/notls");
if (mb.sslflag) strcat (tmp,"/ssl");
if (mb.trysslflag) strcat (tmp,"/tryssl");
if (mb.novalidate) strcat (tmp,"/novalidate-cert");
strcat (tmp,"/pop3/loser}");
mail_parameters (stream,SET_SNARFMAILBOXNAME,(void *) tmp);
mail_ping (stream); /* do initial snarf */
}
return stream; /* return local mailbox stream */
}
else if ((options & OP_PROTOTYPE) &&
((name[1] == 'D') || (name[1] == 'd')) &&
((name[2] == 'R') || (name[2] == 'r')) &&
((name[3] == 'I') || (name[3] == 'i')) &&
((name[4] == 'V') || (name[4] == 'v')) &&
((name[5] == 'E') || (name[5] == 'e')) &&
((name[6] == 'R') || (name[6] == 'r')) && (name[7] == '.')) {
sprintf (tmp,"%.80s",name+8);
/* tie off name at likely delimiter */
if (s = strpbrk (tmp,"/\\:")) *s++ = '\0';
else {
sprintf (tmp,"Can't resolve mailbox %.80s: bad driver syntax",name);
MM_LOG (tmp,ERROR);
return mail_close (stream);
}
for (d = maildrivers; d && compare_cstring (d->name,tmp); d = d->next);
if (d) return (*d->open) (NIL);
sprintf (tmp,"Can't resolve mailbox %.80s: unknown driver",name);
MM_LOG (tmp,ERROR);
return mail_close (stream);
}
/* fall through to default case */
default: /* not special hack (but could be # name */
d = mail_valid (NIL,name,(options & OP_SILENT) ?
(char *) NIL : "open mailbox");
}
return d ? mail_open_work (d,stream,name,options) : stream;
}
/* Mail open worker routine
* Accepts: factory
* candidate stream for recycling
* mailbox name
* open options
* Returns: stream to use on success, NIL on failure
*/
MAILSTREAM *mail_open_work (DRIVER *d,MAILSTREAM *stream,char *name,
long options)
{
int i;
char tmp[MAILTMPLEN];
NETMBX mb;
if (options & OP_PROTOTYPE) return (*d->open) (NIL);
/* name is copied here in case the caller does a re-open using
* stream->mailbox or stream->original_mailbox as the argument.
*/
name = cpystr (name); /* make copy of name */
if (stream) { /* recycling requested? */
if ((stream->dtb == d) && (d->flags & DR_RECYCLE) &&
((d->flags & DR_HALFOPEN) || !(options & OP_HALFOPEN)) &&
mail_usable_network_stream (stream,name)) {
/* yes, checkpoint if needed */
if (d->flags & DR_XPOINT) mail_check (stream);
mail_free_cache (stream); /* clean up stream */
if (stream->mailbox) fs_give ((void **) &stream->mailbox);
if (stream->original_mailbox)
fs_give ((void **) &stream->original_mailbox);
/* flush user flags */
for (i = 0; i < NUSERFLAGS; i++)
if (stream->user_flags[i]) fs_give ((void **) &stream->user_flags[i]);
}
else { /* stream not recycleable, babble if net */
if (!stream->silent && stream->dtb && !(stream->dtb->flags&DR_LOCAL) &&
mail_valid_net_parse (stream->mailbox,&mb)) {
sprintf (tmp,"Closing connection to %.80s",mb.host);
MM_LOG (tmp,(long) NIL);
}
/* flush the old stream */
stream = mail_close (stream);
}
}
/* check if driver does not support halfopen */
else if ((options & OP_HALFOPEN) && !(d->flags & DR_HALFOPEN)) {
fs_give ((void **) &name);
return NIL;
}
/* instantiate new stream if not recycling */
if (!stream) (*mailcache) (stream = (MAILSTREAM *)
memset (fs_get (sizeof (MAILSTREAM)),0,
sizeof (MAILSTREAM)),(long) 0,CH_INIT);
stream->dtb = d; /* set dispatch */
/* set mailbox name */
stream->mailbox = cpystr (stream->original_mailbox = name);
/* initialize stream flags */
stream->inbox = stream->lock = NIL;
stream->debug = (options & OP_DEBUG) ? T : NIL;
stream->rdonly = (options & OP_READONLY) ? T : NIL;
stream->anonymous = (options & OP_ANONYMOUS) ? T : NIL;
stream->scache = (options & OP_SHORTCACHE) ? T : NIL;
stream->silent = (options & OP_SILENT) ? T : NIL;
stream->halfopen = (options & OP_HALFOPEN) ? T : NIL;
stream->secure = (options & OP_SECURE) ? T : NIL;
stream->tryssl = (options & OP_TRYSSL) ? T : NIL;
stream->mulnewsrc = (options & OP_MULNEWSRC) ? T : NIL;
stream->nokod = (options & OP_NOKOD) ? T : NIL;
stream->sniff = (options & OP_SNIFF) ? T : NIL;
stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
stream->perm_answered = stream->perm_draft = stream->kwd_create = NIL;
stream->uid_nosticky = (d->flags & DR_NOSTICKY) ? T : NIL;
stream->uid_last = 0; /* default UID validity */
stream->uid_validity = (unsigned long) time (0);
/* have driver open, flush if failed */
return ((*d->open) (stream)) ? stream : mail_close (stream);
}
/* Mail close
* Accepts: mail stream
* close options
* Returns: NIL, always
*/
MAILSTREAM *mail_close_full (MAILSTREAM *stream,long options)
{
int i;
if (stream) { /* make sure argument given */
/* do the driver's close action */
if (stream->dtb) (*stream->dtb->close) (stream,options);
stream->dtb = NIL; /* resign driver */
if (stream->mailbox) fs_give ((void **) &stream->mailbox);
if (stream->original_mailbox)
fs_give ((void **) &stream->original_mailbox);
if (stream->snarf.name) fs_give ((void **) &stream->snarf.name);
stream->sequence++; /* invalidate sequence */
/* flush user flags */
for (i = 0; i < NUSERFLAGS; i++)
if (stream->user_flags[i]) fs_give ((void **) &stream->user_flags[i]);
mail_free_cache (stream); /* finally free the stream's storage */
if (mailfreestreamsparep && stream->sparep)
(*mailfreestreamsparep) (&stream->sparep);
if (!stream->use) fs_give ((void **) &stream);
}
return NIL;
}
/* Mail make handle
* Accepts: mail stream
* Returns: handle
*
* Handles provide a way to have multiple pointers to a stream yet allow the
* stream's owner to nuke it or recycle it.
*/
MAILHANDLE *mail_makehandle (MAILSTREAM *stream)
{
MAILHANDLE *handle = (MAILHANDLE *) fs_get (sizeof (MAILHANDLE));
handle->stream = stream; /* copy stream */
/* and its sequence */
handle->sequence = stream->sequence;
stream->use++; /* let stream know another handle exists */
return handle;
}
/* Mail release handle
* Accepts: Mail handle
*/
void mail_free_handle (MAILHANDLE **handle)
{
MAILSTREAM *s;
if (*handle) { /* only free if exists */
/* resign stream, flush unreferenced zombies */
if ((!--(s = (*handle)->stream)->use) && !s->dtb) fs_give ((void **) &s);
fs_give ((void **) handle); /* now flush the handle */
}
}
/* Mail get stream handle
* Accepts: Mail handle
* Returns: mail stream or NIL if stream gone
*/
MAILSTREAM *mail_stream (MAILHANDLE *handle)
{
MAILSTREAM *s = handle->stream;
return (s->dtb && (handle->sequence == s->sequence)) ? s : NIL;
}
/* Mail fetch cache element
* Accepts: mail stream
* message # to fetch
* Returns: cache element of this message
* Can also be used to create cache elements for new messages.
*/
MESSAGECACHE *mail_elt (MAILSTREAM *stream,unsigned long msgno)
{
if (msgno < 1 || msgno > stream->nmsgs) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Bad msgno %lu in mail_elt, nmsgs = %lu, mbx=%.80s",
msgno,stream->nmsgs,stream->mailbox ? stream->mailbox : "???");
fatal (tmp);
}
return (MESSAGECACHE *) (*mailcache) (stream,msgno,CH_MAKEELT);
}
/* Mail fetch fast information
* Accepts: mail stream
* sequence
* option flags
*
* Generally, mail_fetch_structure is preferred
*/
void mail_fetch_fast (MAILSTREAM *stream,char *sequence,long flags)
{
/* do the driver's action */
if (stream->dtb && stream->dtb->fast)
(*stream->dtb->fast) (stream,sequence,flags);
}
/* Mail fetch flags
* Accepts: mail stream
* sequence
* option flags
*/
void mail_fetch_flags (MAILSTREAM *stream,char *sequence,long flags)
{
/* do the driver's action */
if (stream->dtb && stream->dtb->msgflags)
(*stream->dtb->msgflags) (stream,sequence,flags);
}
/* Mail fetch message overview
* Accepts: mail stream
* UID sequence to fetch
* pointer to overview return function
*/
void mail_fetch_overview (MAILSTREAM *stream,char *sequence,overview_t ofn)
{
if (stream->dtb && mail_uid_sequence (stream,sequence) &&
!(stream->dtb->overview && (*stream->dtb->overview) (stream,ofn)) &&
mail_ping (stream))
mail_fetch_overview_default (stream,ofn);
}
/* Mail fetch message overview using sequence numbers instead of UIDs
* Accepts: mail stream
* sequence to fetch
* pointer to overview return function
*/
void mail_fetch_overview_sequence (MAILSTREAM *stream,char *sequence,
overview_t ofn)
{
if (stream->dtb && mail_sequence (stream,sequence) &&
!(stream->dtb->overview && (*stream->dtb->overview) (stream,ofn)) &&
mail_ping (stream))
mail_fetch_overview_default (stream,ofn);
}
/* Mail fetch message overview default handler
* Accepts: mail stream with sequence bits lit
* pointer to overview return function
*/
void mail_fetch_overview_default (MAILSTREAM *stream,overview_t ofn)
{
MESSAGECACHE *elt;
ENVELOPE *env;
OVERVIEW ov;
unsigned long i;
ov.optional.lines = 0;
ov.optional.xref = NIL;
for (i = 1; i <= stream->nmsgs; i++)
if (((elt = mail_elt (stream,i))->sequence) &&
(env = mail_fetch_structure (stream,i,NIL,NIL)) && ofn) {
ov.subject = env->subject;
ov.from = env->from;
ov.date = env->date;
ov.message_id = env->message_id;
ov.references = env->references;
ov.optional.octets = elt->rfc822_size;
(*ofn) (stream,mail_uid (stream,i),&ov,i);
}
}
/* Mail fetch message structure
* Accepts: mail stream
* message # to fetch
* pointer to return body
* option flags
* Returns: envelope of this message, body returned in body value
*
* Fetches the "fast" information as well
*/
ENVELOPE *mail_fetch_structure (MAILSTREAM *stream,unsigned long msgno,
BODY **body,long flags)
{
ENVELOPE **env;
BODY **b;
MESSAGECACHE *elt;
char c,*s,*hdr;
unsigned long hdrsize;
STRING bs;
/* do the driver's action if specified */
if (stream->dtb && stream->dtb->structure)
return (*stream->dtb->structure) (stream,msgno,body,flags);
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return NIL; /* must get UID/msgno map first */
}
elt = mail_elt (stream,msgno);/* get elt for real message number */
if (stream->scache) { /* short caching */
if (msgno != stream->msgno){/* garbage collect if not same message */
mail_gc (stream,GC_ENV | GC_TEXTS);
stream->msgno = msgno; /* this is the current message now */
}
env = &stream->env; /* get pointers to envelope and body */
b = &stream->body;
}
else { /* get pointers to elt envelope and body */
env = &elt->private.msg.env;
b = &elt->private.msg.body;
}
if (stream->dtb && ((body && !*b) || !*env || (*env)->incomplete)) {
mail_free_envelope (env); /* flush old envelope and body */
mail_free_body (b);
/* see if need to fetch the whole thing */
if (body || !elt->rfc822_size) {
s = (*stream->dtb->header) (stream,msgno,&hdrsize,flags & ~FT_INTERNAL);
/* make copy in case body fetch smashes it */
hdr = (char *) memcpy (fs_get ((size_t) hdrsize+1),s,(size_t) hdrsize);
hdr[hdrsize] = '\0'; /* tie off header */
(*stream->dtb->text) (stream,msgno,&bs,(flags & ~FT_INTERNAL) | FT_PEEK);
if (!elt->rfc822_size) elt->rfc822_size = hdrsize + SIZE (&bs);
if (body) /* only parse body if requested */
rfc822_parse_msg (env,b,hdr,hdrsize,&bs,BADHOST,stream->dtb->flags);
else
rfc822_parse_msg (env,NIL,hdr,hdrsize,NIL,BADHOST,stream->dtb->flags);
fs_give ((void **) &hdr); /* flush header */
}
else { /* can save memory doing it this way */
hdr = (*stream->dtb->header) (stream,msgno,&hdrsize,flags | FT_INTERNAL);
if (hdrsize) { /* in case null header */
c = hdr[hdrsize]; /* preserve what's there */
hdr[hdrsize] = '\0'; /* tie off header */
rfc822_parse_msg (env,NIL,hdr,hdrsize,NIL,BADHOST,stream->dtb->flags);
hdr[hdrsize] = c; /* restore in case cached data */
}
else *env = mail_newenvelope ();
}
}
/* if need date, have date in envelope? */
if (!elt->day && *env && (*env)->date) mail_parse_date (elt,(*env)->date);
/* sigh, fill in bogus default */
if (!elt->day) elt->day = elt->month = 1;
if (body) *body = *b; /* return the body */
return *env; /* return the envelope */
}
/* Mail mark single message (internal use only)
* Accepts: mail stream
* elt to mark
* fetch flags
*/
static void markseen (MAILSTREAM *stream,MESSAGECACHE *elt,long flags)
{
unsigned long i;
char sequence[20];
MESSAGECACHE *e;
/* non-peeking and needs to set \Seen? */
if (!(flags & FT_PEEK) && !elt->seen) {
if (stream->dtb->flagmsg){ /* driver wants per-message call? */
elt->valid = NIL; /* do pre-alteration driver call */
(*stream->dtb->flagmsg) (stream,elt);
/* set seen, do post-alteration driver call */
elt->seen = elt->valid = T;
(*stream->dtb->flagmsg) (stream,elt);
}
if (stream->dtb->flag) { /* driver wants one-time call? */
/* better safe than sorry, save seq bits */
for (i = 1; i <= stream->nmsgs; i++) {
e = mail_elt (stream,i);
e->private.sequence = e->sequence;
}
/* call driver to set the message */
sprintf (sequence,"%lu",elt->msgno);
(*stream->dtb->flag) (stream,sequence,"\\Seen",ST_SET);
/* restore sequence bits */
for (i = 1; i <= stream->nmsgs; i++) {
e = mail_elt (stream,i);
e->sequence = e->private.sequence;
}
}
/* notify mail program of flag change */
MM_FLAGS (stream,elt->msgno);
}
}
/* Mail fetch message
* Accepts: mail stream
* message # to fetch
* pointer to returned length
* flags
* Returns: message text
*/
char *mail_fetch_message (MAILSTREAM *stream,unsigned long msgno,
unsigned long *len,long flags)
{
GETS_DATA md;
SIZEDTEXT *t;
STRING bs;
MESSAGECACHE *elt;
char *s,*u;
unsigned long i,j;
if (len) *len = 0; /* default return size */
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return ""; /* must get UID/msgno map first */
}
/* initialize message data identifier */
INIT_GETS (md,stream,msgno,"",0,0);
/* is data already cached? */
if ((t = &(elt = mail_elt (stream,msgno))->private.msg.full.text)->data) {
markseen (stream,elt,flags);/* mark message seen */
return mail_fetch_text_return (&md,t,len);
}
if (!stream->dtb) return ""; /* not in cache, must have live driver */
if (stream->dtb->msgdata) return
((*stream->dtb->msgdata) (stream,msgno,"",0,0,NIL,flags) && t->data) ?
mail_fetch_text_return (&md,t,len) : "";
/* ugh, have to do this the crufty way */
u = mail_fetch_header (stream,msgno,NIL,NIL,&i,flags);
/* copy in case text method stomps on it */
s = (char *) memcpy (fs_get ((size_t) i),u,(size_t) i);
if ((*stream->dtb->text) (stream,msgno,&bs,flags)) {
t = &stream->text; /* build combined copy */
if (t->data) fs_give ((void **) &t->data);
t->data = (unsigned char *) fs_get ((t->size = i + SIZE (&bs)) + 1);
if (!elt->rfc822_size) elt->rfc822_size = t->size;
else if (elt->rfc822_size != t->size) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Calculated RFC822.SIZE (%lu) != reported size (%lu)",
t->size,elt->rfc822_size);
mm_log (tmp,WARN); /* bug trap */
}
memcpy (t->data,s,(size_t) i);
for (u = (char *) t->data + i, j = SIZE (&bs); j;) {
memcpy (u,bs.curpos,bs.cursize);
u += bs.cursize; /* update text */
j -= bs.cursize;
bs.curpos += (bs.cursize -1);
bs.cursize = 0;
(*bs.dtb->next) (&bs); /* advance to next buffer's worth */
}
*u = '\0'; /* tie off data */
u = mail_fetch_text_return (&md,t,len);
}
else u = "";
fs_give ((void **) &s); /* finished with copy of header */
return u;
}
/* Mail fetch message header
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* list of lines to fetch
* pointer to returned length
* flags
* Returns: message header in RFC822 format
*
* Note: never calls a mailgets routine
*/
char *mail_fetch_header (MAILSTREAM *stream,unsigned long msgno,char *section,
STRINGLIST *lines,unsigned long *len,long flags)
{
STRING bs;
BODY *b = NIL;
SIZEDTEXT *t = NIL,rt;
MESSAGE *m = NIL;
MESSAGECACHE *elt;
char tmp[MAILTMPLEN];
if (len) *len = 0; /* default return size */
if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return ""; /* must get UID/msgno map first */
}
elt = mail_elt (stream,msgno);/* get cache data */
if (section && *section) { /* nested body header wanted? */
if (!((b = mail_body (stream,msgno,section)) &&
(b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
return ""; /* lose if no body or not MESSAGE/RFC822 */
m = b->nested.msg; /* point to nested message */
}
/* else top-level message header wanted */
else m = &elt->private.msg;
if (m->header.text.data && mail_match_lines (lines,m->lines,flags)) {
if (lines) textcpy (t = &stream->text,&m->header.text);
else t = &m->header.text; /* in cache, and cache is valid */
markseen (stream,elt,flags);/* mark message seen */
}
else if (stream->dtb) { /* not in cache, has live driver? */
if (stream->dtb->msgdata) { /* has driver section fetch? */
/* build driver section specifier */
if (section && *section) sprintf (tmp,"%s.HEADER",section);
else strcpy (tmp,"HEADER");
if ((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,lines,flags)) {
t = &m->header.text; /* fetch data */
/* don't need to postprocess lines */
if (m->lines) lines = NIL;
else if (lines) textcpy (t = &stream->text,&m->header.text);
}
}
else if (b) { /* nested body wanted? */
if (stream->private.search.text) {
rt.data = (unsigned char *) stream->private.search.text +
b->nested.msg->header.offset;
rt.size = b->nested.msg->header.text.size;
t = &rt;
}
else if ((*stream->dtb->text) (stream,msgno,&bs,flags & ~FT_INTERNAL)) {
if ((bs.dtb->next == mail_string_next) && !lines) {
rt.data = (unsigned char *) bs.curpos + b->nested.msg->header.offset;
rt.size = b->nested.msg->header.text.size;
if (stream->private.search.string)
stream->private.search.text = bs.curpos;
t = &rt; /* special hack to avoid extra copy */
}
else textcpyoffstring (t = &stream->text,&bs,
b->nested.msg->header.offset,
b->nested.msg->header.text.size);
}
}
else { /* top-level header fetch */
/* mark message seen */
markseen (stream,elt,flags);
if (rt.data = (unsigned char *)
(*stream->dtb->header) (stream,msgno,&rt.size,flags)) {
/* make a safe copy if need to filter */
if (lines) textcpy (t = &stream->text,&rt);
else t = &rt; /* top level header */
}
}
}
if (!t || !t->data) return "";/* error if no string */
/* filter headers if requested */
if (lines) t->size = mail_filter ((char *) t->data,t->size,lines,flags);
if (len) *len = t->size; /* return size if requested */
return (char *) t->data; /* and text */
}
/* Mail fetch message text
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* pointer to returned length
* flags
* Returns: message text
*/
char *mail_fetch_text (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long *len,long flags)
{
GETS_DATA md;
PARTTEXT *p;
STRING bs;
MESSAGECACHE *elt;
BODY *b = NIL;
char tmp[MAILTMPLEN];
unsigned long i;
if (len) *len = 0; /* default return size */
memset (&stream->private.string,NIL,sizeof (STRING));
if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return ""; /* must get UID/msgno map first */
}
elt = mail_elt (stream,msgno);/* get cache data */
if (section && *section) { /* nested body text wanted? */
if (!((b = mail_body (stream,msgno,section)) &&
(b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
return ""; /* lose if no body or not MESSAGE/RFC822 */
p = &b->nested.msg->text; /* point at nested message */
/* build IMAP-format section specifier */
sprintf (tmp,"%s.TEXT",section);
flags &= ~FT_INTERNAL; /* can't win with this set */
}
else { /* top-level message text wanted */
p = &elt->private.msg.text;
strcpy (tmp,"TEXT");
}
/* initialize message data identifier */
INIT_GETS (md,stream,msgno,section,0,0);
if (p->text.data) { /* is data already cached? */
markseen (stream,elt,flags);/* mark message seen */
return mail_fetch_text_return (&md,&p->text,len);
}
if (!stream->dtb) return ""; /* not in cache, must have live driver */
if (stream->dtb->msgdata) return
((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,NIL,flags) && p->text.data)?
mail_fetch_text_return (&md,&p->text,len) : "";
if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return "";
if (section && *section) { /* nested is more complex */
SETPOS (&bs,p->offset);
i = p->text.size; /* just want this much */
}
else i = SIZE (&bs); /* want entire text */
return mail_fetch_string_return (&md,&bs,i,len,flags);
}
/* Mail fetch message body part MIME headers
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* pointer to returned length
* flags
* Returns: message text
*/
char *mail_fetch_mime (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long *len,long flags)
{
PARTTEXT *p;
STRING bs;
BODY *b;
char tmp[MAILTMPLEN];
if (len) *len = 0; /* default return size */
if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return ""; /* must get UID/msgno map first */
}
flags &= ~FT_INTERNAL; /* can't win with this set */
if (!(section && *section && (b = mail_body (stream,msgno,section))))
return ""; /* not valid section */
/* in cache? */
if ((p = &b->mime)->text.data) {
/* mark message seen */
markseen (stream,mail_elt (stream,msgno),flags);
if (len) *len = p->text.size;
return (char *) p->text.data;
}
if (!stream->dtb) return ""; /* not in cache, must have live driver */
if (stream->dtb->msgdata) { /* has driver fetch? */
/* build driver section specifier */
sprintf (tmp,"%s.MIME",section);
if ((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,NIL,flags) &&
p->text.data) {
if (len) *len = p->text.size;
return (char *) p->text.data;
}
else return "";
}
if (len) *len = b->mime.text.size;
if (!b->mime.text.size) { /* empty MIME header -- mark seen anyway */
markseen (stream,mail_elt (stream,msgno),flags);
return "";
}
/* have to get it from offset */
if (stream->private.search.text)
return stream->private.search.text + b->mime.offset;
if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) {
if (len) *len = 0;
return "";
}
if (bs.dtb->next == mail_string_next) {
if (stream->private.search.string) stream->private.search.text = bs.curpos;
return bs.curpos + b->mime.offset;
}
return textcpyoffstring (&stream->text,&bs,b->mime.offset,b->mime.text.size);
}
/* Mail fetch message body part
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* pointer to returned length
* flags
* Returns: message body
*/
char *mail_fetch_body (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long *len,long flags)
{
GETS_DATA md;
PARTTEXT *p;
STRING bs;
BODY *b;
SIZEDTEXT *t;
char *s,tmp[MAILTMPLEN];
memset (&stream->private.string,NIL,sizeof (STRING));
if (!(section && *section)) /* top-level text wanted? */
return mail_fetch_message (stream,msgno,len,flags);
else if (strlen (section) > (MAILTMPLEN - 20)) {
if (len) *len = 0;
return "";
}
flags &= ~FT_INTERNAL; /* can't win with this set */
/* initialize message data identifier */
INIT_GETS (md,stream,msgno,section,0,0);
/* kludge for old section 0 header */
if (!strcmp (s = strcpy (tmp,section),"0") ||
((s = strstr (tmp,".0")) && !s[2])) {
SIZEDTEXT ht;
*s = '\0'; /* tie off section */
/* this silly way so it does mailgets */
ht.data = (unsigned char *) mail_fetch_header (stream,msgno,
tmp[0] ? tmp : NIL,NIL,
&ht.size,flags);
/* may have UIDs here */
md.flags = (flags & FT_UID) ? MG_UID : NIL;
return mail_fetch_text_return (&md,&ht,len);
}
if (len) *len = 0; /* default return size */
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return ""; /* must get UID/msgno map first */
}
/* must have body */
if (!(b = mail_body (stream,msgno,section))) return "";
/* have cached text? */
if ((t = &(p = &b->contents)->text)->data) {
/* mark message seen */
markseen (stream,mail_elt (stream,msgno),flags);
return mail_fetch_text_return (&md,t,len);
}
if (!stream->dtb) return ""; /* not in cache, must have live driver */
if (stream->dtb->msgdata) return
((*stream->dtb->msgdata)(stream,msgno,section,0,0,NIL,flags) && t->data) ?
mail_fetch_text_return (&md,t,len) : "";
if (len) *len = t->size;
if (!t->size) { /* empty body part -- mark seen anyway */
markseen (stream,mail_elt (stream,msgno),flags);
return "";
}
/* copy body from stringstruct offset */
if (stream->private.search.text)
return stream->private.search.text + p->offset;
if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) {
if (len) *len = 0;
return "";
}
if (bs.dtb->next == mail_string_next) {
if (stream->private.search.string) stream->private.search.text = bs.curpos;
return bs.curpos + p->offset;
}
SETPOS (&bs,p->offset);
return mail_fetch_string_return (&md,&bs,t->size,len,flags);
}
/* Mail fetch partial message text
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* offset of first designed byte or 0 to start at beginning
* maximum number of bytes or 0 for all bytes
* flags
* Returns: T if successful, else NIL
*/
long mail_partial_text (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long first,unsigned long last,long flags)
{
GETS_DATA md;
PARTTEXT *p = NIL;
MESSAGECACHE *elt;
STRING bs;
BODY *b;
char tmp[MAILTMPLEN];
unsigned long i;
if (!mailgets) fatal ("mail_partial_text() called without a mailgets!");
if (section && (strlen (section) > (MAILTMPLEN - 20))) return NIL;
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return NIL; /* must get UID/msgno map first */
}
elt = mail_elt (stream,msgno);/* get cache data */
flags &= ~FT_INTERNAL; /* bogus if this is set */
if (section && *section) { /* nested body text wanted? */
if (!((b = mail_body (stream,msgno,section)) &&
(b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
return NIL; /* lose if no body or not MESSAGE/RFC822 */
p = &b->nested.msg->text; /* point at nested message */
/* build IMAP-format section specifier */
sprintf (tmp,"%s.TEXT",section);
}
else { /* else top-level message text wanted */
p = &elt->private.msg.text;
strcpy (tmp,"TEXT");
}
/* initialize message data identifier */
INIT_GETS (md,stream,msgno,tmp,first,last);
if (p->text.data) { /* is data already cached? */
INIT (&bs,mail_string,p->text.data,i = p->text.size);
markseen (stream,elt,flags);/* mark message seen */
}
else { /* else get data from driver */
if (!stream->dtb) return NIL;
if (stream->dtb->msgdata) /* driver will handle this */
return (*stream->dtb->msgdata) (stream,msgno,tmp,first,last,NIL,flags);
if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return NIL;
if (section && *section) { /* nexted if more complex */
SETPOS (&bs,p->offset); /* offset stringstruct to data */
i = p->text.size; /* maximum size of data */
}
else i = SIZE (&bs); /* just want this much */
}
if (i <= first) i = first = 0;/* first byte is beyond end of text */
/* truncate as needed */
else { /* offset and truncate */
SETPOS (&bs,first + GETPOS (&bs));
i -= first; /* reduced size */
if (last && (i > last)) i = last;
}
/* do the mailgets thing */
(*mailgets) (mail_read,&bs,i,&md);
return T; /* success */
}
/* Mail fetch partial message body part
* Accepts: mail stream
* message # to fetch
* MIME section specifier (#.#.#...#)
* offset of first designed byte or 0 to start at beginning
* maximum number of bytes or 0 for all bytes
* flags
* Returns: T if successful, else NIL
*/
long mail_partial_body (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long first,unsigned long last,long flags)
{
GETS_DATA md;
PARTTEXT *p;
STRING bs;
BODY *b;
SIZEDTEXT *t;
unsigned long i;
if (!(section && *section)) /* top-level text wanted? */
return mail_partial_text (stream,msgno,NIL,first,last,flags);
if (!mailgets) fatal ("mail_partial_body() called without a mailgets!");
if (flags & FT_UID) { /* UID form of call */
if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
else return NIL; /* must get UID/msgno map first */
}
/* must have body */
if (!(b = mail_body (stream,msgno,section))) return NIL;
flags &= ~FT_INTERNAL; /* bogus if this is set */
/* initialize message data identifier */
INIT_GETS (md,stream,msgno,section,first,last);
/* have cached text? */
if ((t = &(p = &b->contents)->text)->data) {
/* mark message seen */
markseen (stream,mail_elt (stream,msgno),flags);
INIT (&bs,mail_string,t->data,i = t->size);
}
else { /* else get data from driver */
if (!stream->dtb) return NIL;
if (stream->dtb->msgdata) /* driver will handle this */
return (*stream->dtb->msgdata) (stream,msgno,section,first,last,NIL,
flags);
if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return NIL;
if (section && *section) { /* nexted if more complex */
SETPOS (&bs,p->offset); /* offset stringstruct to data */
i = t->size; /* maximum size of data */
}
else i = SIZE (&bs); /* just want this much */
}
if (i <= first) i = first = 0;/* first byte is beyond end of text */
else { /* offset and truncate */
SETPOS (&bs,first + GETPOS (&bs));
i -= first; /* reduced size */
if (last && (i > last)) i = last;
}
/* do the mailgets thing */
(*mailgets) (mail_read,&bs,i,&md);
return T; /* success */
}
/* Mail return message text
* Accepts: identifier data
* sized text
* pointer to returned length
* Returns: text
*/
char *mail_fetch_text_return (GETS_DATA *md,SIZEDTEXT *t,unsigned long *len)
{
STRING bs;
if (len) *len = t->size; /* return size */
if (t->size && mailgets) { /* have to do the mailgets thing? */
/* silly but do it anyway for consistency */
INIT (&bs,mail_string,t->data,t->size);
return (*mailgets) (mail_read,&bs,t->size,md);
}
return t->size ? (char *) t->data : "";
}
/* Mail return message string
* Accepts: identifier data
* stringstruct
* text length
* pointer to returned length
* flags
* Returns: text, or NIL if stringstruct returned
*/
char *mail_fetch_string_return (GETS_DATA *md,STRING *bs,unsigned long i,
unsigned long *len,long flags)
{
char *ret = NIL;
if (len) *len = i; /* return size */
/* return stringstruct hack */
if (flags & FT_RETURNSTRINGSTRUCT) {
memcpy (&md->stream->private.string,bs,sizeof (STRING));
SETPOS (&md->stream->private.string,GETPOS (&md->stream->private.string));
}
/* have to do the mailgets thing? */
else if (mailgets) ret = (*mailgets) (mail_read,bs,i,md);
/* special hack to avoid extra copy */
else if (bs->dtb->next == mail_string_next) ret = bs->curpos;
/* make string copy in memory */
else ret = textcpyoffstring (&md->stream->text,bs,GETPOS (bs),i);
return ret;
}
/* Read data from stringstruct
* Accepts: stringstruct
* size of data to read
* buffer to read into
* Returns: T, always, stringstruct updated
*/
long mail_read (void *stream,unsigned long size,char *buffer)
{
unsigned long i;
STRING *s = (STRING *) stream;
while (size) { /* until satisfied */
memcpy (buffer,s->curpos,i = min (s->cursize,size));
buffer += i; /* update buffer */
size -= i; /* note that we read this much */
s->curpos += --i; /* advance that many spaces minus 1 */
s->cursize -= i;
SNX (s); /* now use SNX to advance the last byte */
}
return T;
}
/* Mail fetch UID
* Accepts: mail stream
* message number
* Returns: UID or zero if dead stream
*/
unsigned long mail_uid (MAILSTREAM *stream,unsigned long msgno)
{
unsigned long uid = mail_elt (stream,msgno)->private.uid;
return uid ? uid :
(stream->dtb && stream->dtb->uid) ? (*stream->dtb->uid) (stream,msgno) : 0;
}
/* Mail fetch msgno from UID
* Accepts: mail stream
* UID
* Returns: msgno or zero if failed
*/
unsigned long mail_msgno (MAILSTREAM *stream,unsigned long uid)
{
unsigned long msgno,delta,first,firstuid,last,lastuid,middle,miduid;
if (stream->dtb) { /* active stream? */
if (stream->dtb->msgno) /* direct way */
return (*stream->dtb->msgno) (stream,uid);
else if (stream->dtb->uid) {/* indirect way */
/* Placeholder for now, since currently there are no drivers which
* have a uid method but not a msgno method
*/
for (msgno = 1; msgno <= stream->nmsgs; msgno++)
if ((*stream->dtb->uid) (stream,msgno) == uid) return msgno;
}
/* binary search since have full map */
else for (first = 1,last = stream->nmsgs, delta = (first <= last) ? 1 : 0;
delta &&
(uid >= (firstuid = mail_elt (stream,first)->private.uid)) &&
(uid <= (lastuid = mail_elt (stream,last)->private.uid));) {
/* done if match at an endpoint */
if (uid == firstuid) return first;
if (uid == lastuid) return last;
/* have anything between endpoints? */
if (delta = ((last - first) / 2)) {
if ((miduid = mail_elt (stream,middle = first + delta)->private.uid)
== uid)
return middle; /* found match in middle */
else if (uid < miduid) last = middle - 1;
else first = middle + 1;
}
}
}
else { /* dead stream, do linear search for UID */
for (msgno = 1; msgno <= stream->nmsgs; msgno++)
if (mail_elt (stream,msgno)->private.uid == uid) return msgno;
}
return 0; /* didn't find the UID anywhere */
}
/* Mail fetch From string for menu
* Accepts: destination string
* mail stream
* message # to fetch
* desired string length
* Returns: string of requested length
*/
void mail_fetchfrom (char *s,MAILSTREAM *stream,unsigned long msgno,
long length)
{
char *t;
char tmp[MAILTMPLEN];
ENVELOPE *env = mail_fetchenvelope (stream,msgno);
ADDRESS *adr = env ? env->from : NIL;
memset (s,' ',(size_t)length);/* fill it with spaces */
s[length] = '\0'; /* tie off with null */
/* get first from address from envelope */
while (adr && !adr->host) adr = adr->next;
if (adr) { /* if a personal name exists use it */
if (!(t = adr->personal))
sprintf (t = tmp,"%.256s@%.256s",adr->mailbox,adr->host);
memcpy (s,t,(size_t) min (length,(long) strlen (t)));
}
}
/* Mail fetch Subject string for menu
* Accepts: destination string
* mail stream
* message # to fetch
* desired string length
* Returns: string of no more than requested length
*/
void mail_fetchsubject (char *s,MAILSTREAM *stream,unsigned long msgno,
long length)
{
ENVELOPE *env = mail_fetchenvelope (stream,msgno);
memset (s,'\0',(size_t) length+1);
/* copy subject from envelope */
if (env && env->subject) strncpy (s,env->subject,(size_t) length);
else *s = ' '; /* if no subject then just a space */
}
/* Mail modify flags
* Accepts: mail stream
* sequence
* flag(s)
* option flags
*/
void mail_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
{
MESSAGECACHE *elt;
unsigned long i,uf;
long f;
short nf;
if (!stream->dtb) return; /* no-op if no stream */
if ((stream->dtb->flagmsg || !stream->dtb->flag) &&
((flags & ST_UID) ? mail_uid_sequence (stream,sequence) :
mail_sequence (stream,sequence)) &&
((f = mail_parse_flags (stream,flag,&uf)) || uf))
for (i = 1,nf = (flags & ST_SET) ? T : NIL; i <= stream->nmsgs; i++)
if ((elt = mail_elt (stream,i))->sequence) {
struct { /* old flags */
unsigned int valid : 1;
unsigned int seen : 1;
unsigned int deleted : 1;
unsigned int flagged : 1;
unsigned int answered : 1;
unsigned int draft : 1;
unsigned long user_flags;
} old;
old.valid = elt->valid; old.seen = elt->seen;
old.deleted = elt->deleted; old.flagged = elt->flagged;
old.answered = elt->answered; old.draft = elt->draft;
old.user_flags = elt->user_flags;
elt->valid = NIL; /* prepare for flag alteration */
if (stream->dtb->flagmsg) (*stream->dtb->flagmsg) (stream,elt);
if (f&fSEEN) elt->seen = nf;
if (f&fDELETED) elt->deleted = nf;
if (f&fFLAGGED) elt->flagged = nf;
if (f&fANSWERED) elt->answered = nf;
if (f&fDRAFT) elt->draft = nf;
/* user flags */
if (flags & ST_SET) elt->user_flags |= uf;
else elt->user_flags &= ~uf;
elt->valid = T; /* flags now altered */
if ((old.valid != elt->valid) || (old.seen != elt->seen) ||
(old.deleted != elt->deleted) || (old.flagged != elt->flagged) ||
(old.answered != elt->answered) || (old.draft != elt->draft) ||
(old.user_flags != elt->user_flags))
MM_FLAGS (stream,elt->msgno);
if (stream->dtb->flagmsg) (*stream->dtb->flagmsg) (stream,elt);
}
/* call driver once */
if (stream->dtb->flag) (*stream->dtb->flag) (stream,sequence,flag,flags);
}
/* Mail search for messages
* Accepts: mail stream
* character set
* search program
* option flags
* Returns: T if successful, NIL if dead stream, NIL searchpgm or bad charset
*/
long mail_search_full (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,
long flags)
{
unsigned long i;
long ret = NIL;
if (!(flags & SE_RETAIN)) /* clear search vector unless retaining */
for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->searched = NIL;
if (pgm && stream->dtb) /* must have a search program and driver */
ret = (*(stream->dtb->search ? stream->dtb->search : mail_search_default))
(stream,charset,pgm,flags);
/* flush search program if requested */
if (flags & SE_FREE) mail_free_searchpgm (&pgm);
return ret;
}
/* Mail search for messages default handler
* Accepts: mail stream
* character set
* search program
* option flags
* Returns: T if successful, NIL if bad charset
*/
long mail_search_default (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,
long flags)
{
unsigned long i;
char *msg;
/* make sure that charset is good */
if (msg = utf8_badcharset (charset)) {
MM_LOG (msg,ERROR); /* output error */
fs_give ((void **) &msg);
return NIL;
}
utf8_searchpgm (pgm,charset);
for (i = 1; i <= stream->nmsgs; ++i)
if (mail_search_msg (stream,i,NIL,pgm)) {
if (flags & SE_UID) mm_searched (stream,mail_uid (stream,i));
else { /* mark as searched, notify mail program */
mail_elt (stream,i)->searched = T;
if (!stream->silent) mm_searched (stream,i);
}
}
return LONGT; /* search completed */
}
/* Mail ping mailbox
* Accepts: mail stream
* Returns: stream if still open else NIL
*/
long mail_ping (MAILSTREAM *stream)
{
unsigned long i,n,uf,len;
char *s,*f,tmp[MAILTMPLEN],flags[MAILTMPLEN];
MAILSTREAM *snarf;
MESSAGECACHE *elt;
STRING bs;
long ret;
/* do driver action */
if ((ret = ((stream && stream->dtb) ? (stream->dtb->ping) (stream) : NIL)) &&
stream->snarf.name && /* time to snarf? */
/* prohibit faster than once/min */
(time (0) > (time_t) (stream->snarf.time + min(60,mailsnarfinterval))) &&
(snarf = mail_open (NIL,stream->snarf.name,
stream->snarf.options | OP_SILENT))) {
if ((n = snarf->nmsgs) && /* yes, have messages to snarf? */
mail_search_full (snarf,NIL,mail_criteria ("UNDELETED"),SE_FREE)) {
for (i = 1; ret && (i <= n); i++) /* for each message */
if ((elt = mail_elt (snarf,i))->searched &&
(s = mail_fetch_message (snarf,i,&len,FT_PEEK)) && len) {
INIT (&bs,mail_string,s,len);
if (mailsnarfpreserve) {
/* yes, make sure have fast data */
if (!elt->valid || !elt->day) {
sprintf (tmp,"%lu",n);
mail_fetch_fast (snarf,tmp,NIL);
}
/* initialize flag string */
memset (flags,0,MAILTMPLEN);
/* output system flags except \Deleted */
if (elt->seen) strcat (flags," \\Seen");
if (elt->flagged) strcat (flags," \\Flagged");
if (elt->answered) strcat (flags," \\Answered");
if (elt->draft) strcat (flags," \\Draft");
/* any user flags? */
for (uf = elt->user_flags,s = flags + strlen (flags);
uf && (f = stream->user_flags[find_rightmost_bit (&uf)]) &&
((MAILTMPLEN - (s - tmp)) > (long) (2 + strlen (f)));
s += strlen (s)) sprintf (s," %s",f);
ret = mail_append_full (stream,stream->mailbox,flags + 1,
mail_date (tmp,elt),&bs);
}
else ret = mail_append (stream,stream->mailbox,&bs);
if (ret) { /* did snarf succeed? */
/* driver has per-message (or no) flag call */
if (snarf->dtb->flagmsg || !snarf->dtb->flag) {
elt->valid = NIL; /* prepare for flag alteration */
if (snarf->dtb->flagmsg) (*snarf->dtb->flagmsg) (snarf,elt);
/* flags now altered */
elt->deleted = elt->seen = elt->valid = T;
if (snarf->dtb->flagmsg) (*snarf->dtb->flagmsg) (snarf,elt);
}
/* driver has one-time flag call */
if (snarf->dtb->flag) {
sprintf (tmp,"%lu",i);
(*snarf->dtb->flag) (snarf,tmp,"\\Deleted \\Seen",ST_SET);
}
}
else { /* copy failed */
sprintf (tmp,"Unable to move message %lu from %s mailbox",
i,snarf->dtb->name);
mm_log (tmp,WARN);
}
}
}
/* expunge the messages */
mail_close_full (snarf,n ? CL_EXPUNGE : NIL);
stream->snarf.time = (unsigned long) time (0);
/* Even if the snarf failed, we don't want to return NIL if the stream
* is still alive. Or at least that's what we currently think.
*/
/* redo the driver's action */
ret = stream->dtb ? (*stream->dtb->ping) (stream) : NIL;
}
return ret;
}
/* Mail check mailbox
* Accepts: mail stream
*/
void mail_check (MAILSTREAM *stream)
{
/* do the driver's action */
if (stream->dtb) (*stream->dtb->check) (stream);
}
/* Mail expunge mailbox
* Accepts: mail stream
* sequence to expunge if non-NIL
* expunge options
* Returns: T on success, NIL on failure
*/
long mail_expunge_full (MAILSTREAM *stream,char *sequence,long options)
{
/* do the driver's action */
return stream->dtb ? (*stream->dtb->expunge) (stream,sequence,options) : NIL;
}
/* Mail copy message(s)
* Accepts: mail stream
* sequence
* destination mailbox
* flags
*/
long mail_copy_full (MAILSTREAM *stream,char *sequence,char *mailbox,
long options)
{
return stream->dtb ?
SAFE_COPY (stream->dtb,stream,sequence,mailbox,options) : NIL;
}
/* Append data package to use for old single-message mail_append() interface */
typedef struct mail_append_package {
char *flags; /* initial flags */
char *date; /* message internal date */
STRING *message; /* stringstruct of message */
} APPENDPACKAGE;
/* Single append message string
* Accepts: mail stream
* package pointer (cast as a void *)
* pointer to return initial flags
* pointer to return message internal date
* pointer to return stringstruct of message to append
* Returns: T, always
*/
static long mail_append_single (MAILSTREAM *stream,void *data,char **flags,
char **date,STRING **message)
{
APPENDPACKAGE *ap = (APPENDPACKAGE *) data;
*flags = ap->flags; /* get desired data from the package */
*date = ap->date;
*message = ap->message;
ap->message = NIL; /* so next callback puts a stop to it */
return LONGT; /* always return success */
}
/* Mail append message string
* Accepts: mail stream
* destination mailbox
* initial flags
* message internal date
* stringstruct of message to append
* Returns: T on success, NIL on failure
*/
long mail_append_full (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
STRING *message)
{
APPENDPACKAGE ap;
ap.flags = flags; /* load append package */
ap.date = date;
ap.message = message;
return mail_append_multiple (stream,mailbox,mail_append_single,(void *) &ap);
}
/* Mail append message(s)
* Accepts: mail stream
* destination mailbox
* append data callback
* arbitrary data for callback use
* Returns: T on success, NIL on failure
*/
long mail_append_multiple (MAILSTREAM *stream,char *mailbox,append_t af,
void *data)
{
char *s,tmp[MAILTMPLEN];
DRIVER *d = NIL;
long ret = NIL;
/* never allow names with newlines */
if (strpbrk (mailbox,"\015\012"))
MM_LOG ("Can't append to mailbox with such a name",ERROR);
else if (strlen (mailbox) >=
(NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50)) {
sprintf (tmp,"Can't append %.80s: %s",mailbox,(*mailbox == '{') ?
"invalid remote specification" : "no such mailbox");
MM_LOG (tmp,ERROR);
}
/* special driver hack? */
else if (!strncmp (lcase (strcpy (tmp,mailbox)),"#driver.",8)) {
/* yes, tie off name at likely delimiter */
if (!(s = strpbrk (tmp+8,"/\\:"))) {
sprintf (tmp,"Can't append to mailbox %.80s: bad driver syntax",mailbox);
MM_LOG (tmp,ERROR);
return NIL;
}
*s++ = '\0'; /* tie off at delimiter */
if (!(d = (DRIVER *) mail_parameters (NIL,GET_DRIVER,tmp+8))) {
sprintf (tmp,"Can't append to mailbox %.80s: unknown driver",mailbox);
MM_LOG (tmp,ERROR);
}
else ret = SAFE_APPEND (d,stream,mailbox + (s - tmp),af,data);
}
else if (d = mail_valid (stream,mailbox,NIL))
ret = SAFE_APPEND (d,stream,mailbox,af,data);
/* No driver, try for TRYCREATE if no stream. Note that we use the
* createProto here, not the appendProto, since the dummy driver already
* took care of the appendProto case. Otherwise, if appendProto is set to
* NIL, we won't get a TRYCREATE.
*/
else if (!stream && (stream = default_proto (NIL)) && stream->dtb &&
SAFE_APPEND (stream->dtb,stream,mailbox,af,data))
/* timing race? */
MM_NOTIFY (stream,"Append validity confusion",WARN);
/* generate error message */
else mail_valid (stream,mailbox,"append to mailbox");
return ret;
}
/* Mail garbage collect stream
* Accepts: mail stream
* garbage collection flags
*/
void mail_gc (MAILSTREAM *stream,long gcflags)
{
MESSAGECACHE *elt;
unsigned long i;
/* do the driver's action first */
if (stream->dtb && stream->dtb->gc) (*stream->dtb->gc) (stream,gcflags);
stream->msgno = 0; /* nothing cached now */
if (gcflags & GC_ENV) { /* garbage collect envelopes? */
if (stream->env) mail_free_envelope (&stream->env);
if (stream->body) mail_free_body (&stream->body);
}
if (gcflags & GC_TEXTS) { /* free texts */
if (stream->text.data) fs_give ((void **) &stream->text.data);
stream->text.size = 0;
}
/* garbage collect per-message stuff */
for (i = 1; i <= stream->nmsgs; i++)
if (elt = (MESSAGECACHE *) (*mailcache) (stream,i,CH_ELT))
mail_gc_msg (&elt->private.msg,gcflags);
}
/* Mail garbage collect message
* Accepts: message structure
* garbage collection flags
*/
void mail_gc_msg (MESSAGE *msg,long gcflags)
{
if (gcflags & GC_ENV) { /* garbage collect envelopes? */
mail_free_envelope (&msg->env);
mail_free_body (&msg->body);
}
if (gcflags & GC_TEXTS) { /* garbage collect texts */
if (msg->full.text.data) fs_give ((void **) &msg->full.text.data);
if (msg->header.text.data) {
mail_free_stringlist (&msg->lines);
fs_give ((void **) &msg->header.text.data);
}
if (msg->text.text.data) fs_give ((void **) &msg->text.text.data);
/* now GC all body components */
if (msg->body) mail_gc_body (msg->body);
}
}
/* Mail garbage collect texts in BODY structure
* Accepts: BODY structure
*/
void mail_gc_body (BODY *body)
{
PART *part;
switch (body->type) { /* free contents */
case TYPEMULTIPART: /* multiple part */
for (part = body->nested.part; part; part = part->next)
mail_gc_body (&part->body);
break;
case TYPEMESSAGE: /* encapsulated message */
if (body->subtype && !strcmp (body->subtype,"RFC822")) {
mail_free_stringlist (&body->nested.msg->lines);
mail_gc_msg (body->nested.msg,GC_TEXTS);
}
break;
default:
break;
}
if (body->mime.text.data) fs_give ((void **) &body->mime.text.data);
if (body->contents.text.data) fs_give ((void **) &body->contents.text.data);
}
/* Mail get body part
* Accepts: mail stream
* message number
* section specifier
* Returns: pointer to body
*/
BODY *mail_body (MAILSTREAM *stream,unsigned long msgno,unsigned char *section)
{
BODY *b = NIL;
PART *pt;
unsigned long i;
/* make sure have a body */
if (section && *section && mail_fetchstructure (stream,msgno,&b) && b)
while (*section) { /* find desired section */
if (isdigit (*section)) { /* get section specifier */
/* make sure what follows is valid */
if (!(i = strtoul (section,(char **) §ion,10)) ||
(*section && ((*section++ != '.') || !*section))) return NIL;
/* multipart content? */
if (b->type == TYPEMULTIPART) {
/* yes, find desired part */
if (pt = b->nested.part) while (--i && (pt = pt->next));
if (!pt) return NIL; /* bad specifier */
b = &pt->body; /* note new body */
}
/* otherwise must be section 1 */
else if (i != 1) return NIL;
/* need to go down further? */
if (*section) switch (b->type) {
case TYPEMULTIPART: /* multipart */
break;
case TYPEMESSAGE: /* embedded message */
if (!strcmp (b->subtype,"RFC822")) {
b = b->nested.msg->body;
break;
}
default: /* bogus subpart specification */
return NIL;
}
}
else return NIL; /* unknown section specifier */
}
return b;
}
/* Mail output date from elt fields
* Accepts: character string to write into
* elt to get data data from
* Returns: the character string
*/
const char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char *mail_date (char *string,MESSAGECACHE *elt)
{
sprintf (string,"%2d-%s-%d %02d:%02d:%02d %c%02d%02d",
elt->day ? elt->day : 1,
months[elt->month ? (elt->month - 1) : 0],
elt->year + BASEYEAR,elt->hours,elt->minutes,elt->seconds,
elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes);
return string;
}
/* Mail output extended-ctime format date from elt fields
* Accepts: character string to write into
* elt to get data data from
* Returns: the character string
*/
char *mail_cdate (char *string,MESSAGECACHE *elt)
{
char *fmt = "%s %s %2d %02d:%02d:%02d %4d %s%02d%02d\n";
int d = elt->day ? elt->day : 1;
int m = elt->month ? (elt->month - 1) : 0;
int y = elt->year + BASEYEAR;
const char *s = months[m];
if (m < 2) { /* if before March, */
m += 10; /* January = month 10 of previous year */
y--;
}
else m -= 2; /* March is month 0 */
sprintf (string,fmt,days[(int) (d + 2 + ((7 + 31 * m) / 12)
#ifndef USEJULIANCALENDAR
#ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
+ (y / 400)
#ifdef Y4KBUGFIX
- (y / 4000)
#endif
#else /* Orthodox calendar */
+ (2 * (y / 900)) + ((y % 900) >= 200)
+ ((y % 900) >= 600)
#endif
- (y / 100)
#endif
+ y + (y / 4)) % 7],
s,d,elt->hours,elt->minutes,elt->seconds,elt->year + BASEYEAR,
elt->zoccident ? "-" : "+",elt->zhours,elt->zminutes);
return string;
}
/* Mail parse date into elt fields
* Accepts: elt to write into
* date string to parse
* Returns: T if parse successful, else NIL
* This routine parses dates as follows:
* . leading three alphas followed by comma and space are ignored
* . date accepted in format: mm/dd/yy, mm/dd/yyyy, dd-mmm-yy, dd-mmm-yyyy,
* dd mmm yy, dd mmm yyyy, yyyy-mm-dd, yyyymmdd
* . two and three digit years interpreted according to RFC 2822 rules
* . mandatory end of string if yyyy-mm-dd or yyyymmdd; otherwise optional
* space followed by time:
* . time accepted in format hh:mm:ss or hh:mm
* . end of string accepted
* . timezone accepted: hyphen followed by symbolic timezone, or space
* followed by signed numeric timezone or symbolic timezone
* Examples of normal input:
* . IMAP date-only (SEARCH):
* dd-mmm-yyyy
* . IMAP date-time (INTERNALDATE):
* dd-mmm-yyyy hh:mm:ss +zzzz
* . RFC-822:
* www, dd mmm yy hh:mm:ss zzz
* . RFC-2822:
* www, dd mmm yyyy hh:mm:ss +zzzz
*/
long mail_parse_date (MESSAGECACHE *elt,unsigned char *s)
{
unsigned long d,m,y;
int mi,ms;
struct tm *t;
time_t tn;
char tmp[MAILTMPLEN];
static unsigned long maxyear = 0;
if (!maxyear) { /* know the end of time yet? */
MESSAGECACHE tmpelt;
memset (&tmpelt,0xff,sizeof (MESSAGECACHE));
maxyear = BASEYEAR + tmpelt.year;
}
/* clear elt */
elt->zoccident = elt->zhours = elt->zminutes =
elt->hours = elt->minutes = elt->seconds =
elt->day = elt->month = elt->year = 0;
/* make a writeable uppercase copy */
if (s && *s && (strlen (s) < (size_t)MAILTMPLEN)) s = ucase (strcpy (tmp,s));
else return NIL;
/* skip over possible day of week */
if (isalpha (*s) && isalpha (s[1]) && isalpha (s[2]) && (s[3] == ',') &&
(s[4] == ' ')) s += 5;
while (*s == ' ') s++; /* parse first number (probable month) */
if (!(m = strtoul (s,(char **) &s,10))) return NIL;
switch (*s) { /* different parse based on delimiter */
case '/': /* mm/dd/yy format */
if (isdigit (*++s) && (d = strtoul (s,(char **) &s,10)) &&
(*s == '/') && isdigit (*++s)) {
y = strtoul (s,(char **) &s,10);
if (*s == '\0') break; /* must end here */
}
return NIL; /* bogon */
case ' ': /* dd mmm yy format */
while (s[1] == ' ') s++; /* slurp extra whitespace */
case '-':
if (isdigit (s[1])) { /* possible ISO 8601 date format? */
y = m; /* yes, first number is year */
/* get month and day */
if ((m = strtoul (s+1,(char **) &s,10)) && (*s++ == '-') &&
(d = strtoul (s,(char **) &s,10)) && !*s) break;
return NIL; /* syntax error or time present */
}
d = m; /* dd-mmm-yy[yy], so first number is a day */
/* make sure string long enough! */
if (strlen (s) < (size_t) 5) return NIL;
/* Some compilers don't allow `<<' and/or longs in case statements. */
/* slurp up the month string */
ms = ((s[1] - 'A') * 1024) + ((s[2] - 'A') * 32) + (s[3] - 'A');
switch (ms) { /* determine the month */
case (('J'-'A') * 1024) + (('A'-'A') * 32) + ('N'-'A'): m = 1; break;
case (('F'-'A') * 1024) + (('E'-'A') * 32) + ('B'-'A'): m = 2; break;
case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('R'-'A'): m = 3; break;
case (('A'-'A') * 1024) + (('P'-'A') * 32) + ('R'-'A'): m = 4; break;
case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('Y'-'A'): m = 5; break;
case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('N'-'A'): m = 6; break;
case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('L'-'A'): m = 7; break;
case (('A'-'A') * 1024) + (('U'-'A') * 32) + ('G'-'A'): m = 8; break;
case (('S'-'A') * 1024) + (('E'-'A') * 32) + ('P'-'A'): m = 9; break;
case (('O'-'A') * 1024) + (('C'-'A') * 32) + ('T'-'A'): m = 10; break;
case (('N'-'A') * 1024) + (('O'-'A') * 32) + ('V'-'A'): m = 11; break;
case (('D'-'A') * 1024) + (('E'-'A') * 32) + ('C'-'A'): m = 12; break;
default: return NIL; /* unknown month */
}
if (s[4] == *s) s += 5; /* advance to year */
else { /* first three were OK, possibly full name */
mi = *s; /* note delimiter, skip alphas */
for (s += 4; isalpha (*s); s++);
/* error if delimiter not here */
if (mi != *s++) return NIL;
}
while (*s == ' ') s++; /* parse year */
if (isdigit (*s)) { /* must be a digit here */
y = strtoul (s,(char **) &s,10);
if (*s == '\0' || *s == ' ') break;
}
case '\0': /* ISO 8601 compact date */
if (m < (BASEYEAR * 10000)) return NIL;
y = m / 10000; /* get year */
d = (m %= 10000) % 100; /* get day */
m /= 100; /* and month */
break;
default:
return NIL; /* unknown date format */
}
/* minimal validity check of date */
if ((d > 31) || (m > 12)) return NIL;
if (y < 49) y += 2000; /* RFC 2282 rules for two digit years 00-49 */
else if (y < 999) y += 1900; /* 2-digit years 50-99 and 3-digit years */
/* reject prehistoric and far future years */
if ((y < BASEYEAR) || (y > maxyear)) return NIL;
/* set values in elt */
elt->day = d; elt->month = m; elt->year = y - BASEYEAR;
ms = '\0'; /* initially no time zone string */
if (*s) { /* time specification present? */
/* parse time */
d = strtoul (s+1,(char **) &s,10);
if (*s != ':') return NIL;
m = strtoul (++s,(char **) &s,10);
y = (*s == ':') ? strtoul (++s,(char **) &s,10) : 0;
/* validity check time */
if ((d > 23) || (m > 59) || (y > 60)) return NIL;
/* set values in elt */
elt->hours = d; elt->minutes = m; elt->seconds = y;
switch (*s) { /* time zone specifier? */
case ' ': /* numeric time zone */
while (s[1] == ' ') s++; /* slurp extra whitespace */
if (!isalpha (s[1])) { /* treat as '-' case if alphabetic */
/* test for sign character */
if ((elt->zoccident = (*++s == '-')) || (*s == '+')) s++;
/* validate proper timezone */
if (isdigit(*s) && isdigit(s[1]) && isdigit(s[2]) && (s[2] < '6') &&
isdigit(s[3])) {
elt->zhours = (*s - '0') * 10 + (s[1] - '0');
elt->zminutes = (s[2] - '0') * 10 + (s[3] - '0');
}
return T; /* all done! */
}
/* falls through */
case '-': /* symbolic time zone */
if (!(ms = *++s)) ms = 'Z';
else if (*++s) { /* multi-character? */
ms -= 'A'; ms *= 1024; /* yes, make compressed three-byte form */
ms += ((*s++ - 'A') * 32);
if (*s) ms += *s++ - 'A';
if (*s) ms = '\0'; /* more than three characters */
}
default: /* ignore anything else */
break;
}
}
/* This is not intended to be a comprehensive list of all possible
* timezone strings. Such a list would be impractical. Rather, this
* listing is intended to incorporate all military, North American, and
* a few special cases such as Japan and the major European zone names,
* such as what might be expected to be found in a Tenex format mailbox
* and spewed from an IMAP server. The trend is to migrate to numeric
* timezones which lack the flavor but also the ambiguity of the names.
*
* RFC-822 only recognizes UT, GMT, 1-letter military timezones, and the
* 4 CONUS timezones and their summer time variants. [Sorry, Canadian
* Atlantic Provinces, Alaska, and Hawaii.]
*/
switch (ms) { /* determine the timezone */
/* Universal */
case (('U'-'A')*1024)+(('T'-'A')*32):
#ifndef STRICT_RFC822_TIMEZONES
case (('U'-'A')*1024)+(('T'-'A')*32)+'C'-'A':
#endif
/* Greenwich */
case (('G'-'A')*1024)+(('M'-'A')*32)+'T'-'A':
case 'Z': elt->zhours = 0; break;
/* oriental (from Greenwich) timezones */
#ifndef STRICT_RFC822_TIMEZONES
/* Middle Europe */
case (('M'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
#endif
#ifdef BRITISH_SUMMER_TIME
/* British Summer */
case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
case 'A': elt->zhours = 1; break;
#ifndef STRICT_RFC822_TIMEZONES
/* Eastern Europe */
case (('E'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
#endif
case 'B': elt->zhours = 2; break;
case 'C': elt->zhours = 3; break;
case 'D': elt->zhours = 4; break;
case 'E': elt->zhours = 5; break;
case 'F': elt->zhours = 6; break;
case 'G': elt->zhours = 7; break;
case 'H': elt->zhours = 8; break;
#ifndef STRICT_RFC822_TIMEZONES
/* Japan */
case (('J'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
case 'I': elt->zhours = 9; break;
case 'K': elt->zhours = 10; break;
case 'L': elt->zhours = 11; break;
case 'M': elt->zhours = 12; break;
/* occidental (from Greenwich) timezones */
case 'N': elt->zoccident = 1; elt->zhours = 1; break;
case 'O': elt->zoccident = 1; elt->zhours = 2; break;
#ifndef STRICT_RFC822_TIMEZONES
case (('A'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
#endif
case 'P': elt->zoccident = 1; elt->zhours = 3; break;
#ifdef NEWFOUNDLAND_STANDARD_TIME
/* Newfoundland */
case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
elt->zoccident = 1; elt->zhours = 3; elt->zminutes = 30; break;
#endif
#ifndef STRICT_RFC822_TIMEZONES
/* Atlantic */
case (('A'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
/* CONUS */
case (('E'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
case 'Q': elt->zoccident = 1; elt->zhours = 4; break;
/* Eastern */
case (('E'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
case (('C'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
case 'R': elt->zoccident = 1; elt->zhours = 5; break;
/* Central */
case (('C'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
case (('M'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
case 'S': elt->zoccident = 1; elt->zhours = 6; break;
/* Mountain */
case (('M'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
case (('P'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
case 'T': elt->zoccident = 1; elt->zhours = 7; break;
/* Pacific */
case (('P'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#ifndef STRICT_RFC822_TIMEZONES
case (('Y'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
#endif
case 'U': elt->zoccident = 1; elt->zhours = 8; break;
#ifndef STRICT_RFC822_TIMEZONES
/* Yukon */
case (('Y'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
case 'V': elt->zoccident = 1; elt->zhours = 9; break;
#ifndef STRICT_RFC822_TIMEZONES
/* Hawaii */
case (('H'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
case 'W': elt->zoccident = 1; elt->zhours = 10; break;
/* Nome/Bering/Samoa */
#ifdef NOME_STANDARD_TIME
case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
#ifdef BERING_STANDARD_TIME
case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
#ifdef SAMOA_STANDARD_TIME
case (('S'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
#endif
case 'X': elt->zoccident = 1; elt->zhours = 11; break;
case 'Y': elt->zoccident = 1; elt->zhours = 12; break;
default: /* unknown time zones treated as local */
tn = time (0); /* time now... */
t = localtime (&tn); /* get local minutes since midnight */
mi = t->tm_hour * 60 + t->tm_min;
ms = t->tm_yday; /* note Julian day */
if (t = gmtime (&tn)) { /* minus UTC minutes since midnight */
mi -= t->tm_hour * 60 + t->tm_min;
/* ms can be one of:
* 36x local time is December 31, UTC is January 1, offset -24 hours
* 1 local time is 1 day ahead of UTC, offset +24 hours
* 0 local time is same day as UTC, no offset
* -1 local time is 1 day behind UTC, offset -24 hours
* -36x local time is January 1, UTC is December 31, offset +24 hours
*/
if (ms -= t->tm_yday) /* correct offset if different Julian day */
mi += ((ms < 0) == (abs (ms) == 1)) ? -24*60 : 24*60;
if (mi < 0) { /* occidental? */
mi = abs (mi); /* yup, make positive number */
elt->zoccident = 1; /* and note west of UTC */
}
elt->zhours = mi / 60; /* now break into hours and minutes */
elt->zminutes = mi % 60;
}
break;
}
return T;
}
/* Mail n messages exist
* Accepts: mail stream
* number of messages
*/
void mail_exists (MAILSTREAM *stream,unsigned long nmsgs)
{
char tmp[MAILTMPLEN];
if (nmsgs > MAXMESSAGES) {
sprintf (tmp,"Mailbox has more messages (%lu) exist than maximum (%lu)",
nmsgs,MAXMESSAGES);
mm_log (tmp,ERROR);
nmsgs = MAXMESSAGES; /* cap to maximum */
/* probably will crash in mail_elt() soon enough... */
}
/* make sure cache is large enough */
(*mailcache) (stream,nmsgs,CH_SIZE);
stream->nmsgs = nmsgs; /* update stream status */
/* notify main program of change */
if (!stream->silent) MM_EXISTS (stream,nmsgs);
}
/* Mail n messages are recent
* Accepts: mail stream
* number of recent messages
*/
void mail_recent (MAILSTREAM *stream,unsigned long recent)
{
char tmp[MAILTMPLEN];
if (recent <= stream->nmsgs) stream->recent = recent;
else {
sprintf (tmp,"Non-existent recent message(s) %lu, nmsgs=%lu",
recent,stream->nmsgs);
mm_log (tmp,ERROR);
}
}
/* Mail message n is expunged
* Accepts: mail stream
* message #
*/
void mail_expunged (MAILSTREAM *stream,unsigned long msgno)
{
char tmp[MAILTMPLEN];
MESSAGECACHE *elt;
if (msgno > stream->nmsgs) {
sprintf (tmp,"Expunge of non-existent message %lu, nmsgs=%lu",
msgno,stream->nmsgs);
mm_log (tmp,ERROR);
}
else {
elt = (MESSAGECACHE *) (*mailcache) (stream,msgno,CH_ELT);
/* notify main program of change */
if (!stream->silent) MM_EXPUNGED (stream,msgno);
if (elt) { /* if an element is there */
elt->msgno = 0; /* invalidate its message number and free */
(*mailcache) (stream,msgno,CH_FREE);
(*mailcache) (stream,msgno,CH_FREESORTCACHE);
}
/* expunge the slot */
(*mailcache) (stream,msgno,CH_EXPUNGE);
--stream->nmsgs; /* update stream status */
if (stream->msgno) { /* have stream pointers? */
/* make sure the short cache is nuked */
if (stream->scache) mail_gc (stream,GC_ENV | GC_TEXTS);
else stream->msgno = 0; /* make sure invalidated in any case */
}
}
}
/* Mail stream status routines */
/* Mail lock stream
* Accepts: mail stream
*/
void mail_lock (MAILSTREAM *stream)
{
if (stream->lock) {
char tmp[MAILTMPLEN];
sprintf (tmp,"Lock when already locked, mbx=%.80s",
stream->mailbox ? stream->mailbox : "???");
fatal (tmp);
}
else stream->lock = T; /* lock stream */
}
/* Mail unlock stream
* Accepts: mail stream
*/
void mail_unlock (MAILSTREAM *stream)
{
if (!stream->lock) fatal ("Unlock when not locked");
else stream->lock = NIL; /* unlock stream */
}
/* Mail turn on debugging telemetry
* Accepts: mail stream
*/
void mail_debug (MAILSTREAM *stream)
{
stream->debug = T; /* turn on debugging telemetry */
if (stream->dtb) (*stream->dtb->parameters) (ENABLE_DEBUG,stream);
}
/* Mail turn off debugging telemetry
* Accepts: mail stream
*/
void mail_nodebug (MAILSTREAM *stream)
{
stream->debug = NIL; /* turn off debugging telemetry */
if (stream->dtb) (*stream->dtb->parameters) (DISABLE_DEBUG,stream);
}
/* Mail log to debugging telemetry
* Accepts: message
* flag that data is "sensitive"
*/
void mail_dlog (char *string,long flag)
{
mm_dlog ((debugsensitive || !flag) ? string : "<suppressed>");
}
/* Mail parse UID sequence
* Accepts: mail stream
* sequence to parse
* Returns: T if parse successful, else NIL
*/
long mail_uid_sequence (MAILSTREAM *stream,unsigned char *sequence)
{
unsigned long i,j,k,x,y;
for (i = 1; i <= stream->nmsgs; i++) mail_elt (stream,i)->sequence = NIL;
while (sequence && *sequence){/* while there is something to parse */
if (*sequence == '*') { /* maximum message */
i = stream->nmsgs ? mail_uid (stream,stream->nmsgs) : stream->uid_last;
sequence++; /* skip past * */
}
/* parse and validate message number */
/* parse and validate message number */
else if (!isdigit (*sequence)) {
MM_LOG ("Syntax error in sequence",ERROR);
return NIL;
}
else if (!(i = strtoul (sequence,(char **) &sequence,10))) {
MM_LOG ("UID may not be zero",ERROR);
return NIL;
}
switch (*sequence) { /* see what the delimiter is */
case ':': /* sequence range */
if (*++sequence == '*') { /* maximum message */
j = stream->nmsgs ? mail_uid (stream,stream->nmsgs) : stream->uid_last;
sequence++; /* skip past * */
}
/* parse end of range */
else if (!(j = strtoul (sequence,(char **) &sequence,10))) {
MM_LOG ("UID sequence range invalid",ERROR);
return NIL;
}
if (*sequence && *sequence++ != ',') {
MM_LOG ("UID sequence range syntax error",ERROR);
return NIL;
}
if (i > j) { /* swap the range if backwards */
x = i; i = j; j = x;
}
x = mail_msgno (stream,i);/* get msgnos */
y = mail_msgno (stream,j);/* for both UIDS (don't && it) */
/* easy if both UIDs valid */
if (x && y) while (x <= y) mail_elt (stream,x++)->sequence = T;
/* start UID valid, end is not */
else if (x) while ((x <= stream->nmsgs) && (mail_uid (stream,x) <= j))
mail_elt (stream,x++)->sequence = T;
/* end UID valid, start is not */
else if (y) for (x = 1; x <= y; x++) {
if (mail_uid (stream,x) >= i) mail_elt (stream,x)->sequence = T;
}
/* neither is valid, ugh */
else for (x = 1; x <= stream->nmsgs; x++)
if (((k = mail_uid (stream,x)) >= i) && (k <= j))
mail_elt (stream,x)->sequence = T;
break;
case ',': /* single message */
++sequence; /* skip the delimiter, fall into end case */
case '\0': /* end of sequence, mark this message */
if (x = mail_msgno (stream,i)) mail_elt (stream,x)->sequence = T;
break;
default: /* anything else is a syntax error! */
MM_LOG ("UID sequence syntax error",ERROR);
return NIL;
}
}
return T; /* successfully parsed sequence */
}
/* Mail see if line list matches that in cache
* Accepts: candidate line list
* cached line list
* matching flags
* Returns: T if match, NIL if no match
*/
long mail_match_lines (STRINGLIST *lines,STRINGLIST *msglines,long flags)
{
unsigned long i;
unsigned char *s,*t;
STRINGLIST *m;
if (!msglines) return T; /* full header is in cache */
/* need full header but filtered in cache */
if ((flags & FT_NOT) || !lines) return NIL;
do { /* make sure all present & accounted for */
for (m = msglines; m; m = m->next) if (lines->text.size == m->text.size) {
for (s = lines->text.data,t = m->text.data,i = lines->text.size;
i && !compare_uchar (*s,*t); s++,t++,i--);
if (!i) break; /* this line matches */
}
if (!m) return NIL; /* didn't find in the list */
}
while (lines = lines->next);
return T; /* all lines found */
}
/* Mail filter text by header lines
* Accepts: text to filter, with trailing null
* length of text
* list of lines
* fetch flags
* Returns: new text size, text overwritten
*/
unsigned long mail_filter (char *text,unsigned long len,STRINGLIST *lines,
long flags)
{
STRINGLIST *hdrs;
int notfound;
unsigned long i;
char c,*s,*e,*t,tmp[MAILTMPLEN];
char *src = text;
char *dst = src;
char *end = text + len;
text[len] = '\012'; /* guard against running off buffer */
while (src < end) { /* process header */
/* slurp header line name */
for (s = src,e = s + MAILTMPLEN - 1,e = (e < end ? e : end),t = tmp;
(s < e) && ((c = (*s ? *s : (*s = ' '))) != ':') &&
((c > ' ') ||
((c != ' ') && (c != '\t') && (c != '\015') && (c != '\012')));
*t++ = *s++);
*t = '\0'; /* tie off */
notfound = T; /* not found yet */
if (i = t - tmp) /* see if found in header */
for (hdrs = lines; hdrs && notfound; hdrs = hdrs->next)
if ((hdrs->text.size == i) && !compare_csizedtext (tmp,&hdrs->text))
notfound = NIL;
/* skip header line if not wanted */
if (i && ((flags & FT_NOT) ? !notfound : notfound))
while (((*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012')) || ((*src == ' ') || (*src == '\t')));
else if (src == dst) { /* copy to self */
while (((*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
(*src++ != '\012')) || ((*src == ' ') || (*src == '\t')));
dst = src; /* update destination */
}
else { /* copy line and any continuation line */
while ((((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012'))||
((*src == ' ') || (*src == '\t')));
/* in case hit the guard LF */
if (src > end) dst -= (src - end);
}
}
*dst = '\0'; /* tie off destination */
return dst - text;
}
/* Local mail search message
* Accepts: MAIL stream
* message number
* optional section specification
* search program
* Returns: T if found, NIL otherwise
*/
long mail_search_msg (MAILSTREAM *stream,unsigned long msgno,char *section,
SEARCHPGM *pgm)
{
unsigned short d;
char tmp[MAILTMPLEN];
MESSAGECACHE *elt = mail_elt (stream,msgno);
SEARCHHEADER *hdr;
SEARCHOR *or;
SEARCHPGMLIST *not;
unsigned long now = (unsigned long) time (0);
if (pgm->msgno || pgm->uid) { /* message set searches */
SEARCHSET *set;
/* message sequences */
if (pgm->msgno) { /* inside this message sequence set */
for (set = pgm->msgno; set; set = set->next)
if (set->last ? ((set->first <= set->last) ?
((msgno >= set->first) && (msgno <= set->last)) :
((msgno >= set->last) && (msgno <= set->first))) :
msgno == set->first) break;
if (!set) return NIL; /* not found within sequence */
}
if (pgm->uid) { /* inside this unique identifier set */
unsigned long uid = mail_uid (stream,msgno);
for (set = pgm->uid; set; set = set->next)
if (set->last ? ((set->first <= set->last) ?
((uid >= set->first) && (uid <= set->last)) :
((uid >= set->last) && (uid <= set->first))) :
uid == set->first) break;
if (!set) return NIL; /* not found within sequence */
}
}
/* Fast data searches */
/* need to fetch fast data? */
if ((!elt->rfc822_size && (pgm->larger || pgm->smaller)) ||
(!elt->year && (pgm->before || pgm->on || pgm->since ||
pgm->older || pgm->younger)) ||
(!elt->valid && (pgm->answered || pgm->unanswered ||
pgm->deleted || pgm->undeleted ||
pgm->draft || pgm->undraft ||
pgm->flagged || pgm->unflagged ||
pgm->recent || pgm->old ||
pgm->seen || pgm->unseen ||
pgm->keyword || pgm->unkeyword))) {
unsigned long i;
MESSAGECACHE *ielt;
for (i = elt->msgno; /* find last unloaded message in range */
(i < stream->nmsgs) && (ielt = mail_elt (stream,i+1)) &&
((!ielt->rfc822_size && (pgm->larger || pgm->smaller)) ||
(!ielt->year && (pgm->before || pgm->on || pgm->since ||
pgm->older || pgm->younger)) ||
(!ielt->valid && (pgm->answered || pgm->unanswered ||
pgm->deleted || pgm->undeleted ||
pgm->draft || pgm->undraft ||
pgm->flagged || pgm->unflagged ||
pgm->recent || pgm->old ||
pgm->seen || pgm->unseen ||
pgm->keyword || pgm->unkeyword))); ++i);
if (i == elt->msgno) sprintf (tmp,"%lu",elt->msgno);
else sprintf (tmp,"%lu:%lu",elt->msgno,i);
mail_fetch_fast (stream,tmp,NIL);
}
/* size ranges */
if ((pgm->larger && (elt->rfc822_size <= pgm->larger)) ||
(pgm->smaller && (elt->rfc822_size >= pgm->smaller))) return NIL;
/* message flags */
if ((pgm->answered && !elt->answered) ||
(pgm->unanswered && elt->answered) ||
(pgm->deleted && !elt->deleted) ||
(pgm->undeleted && elt->deleted) ||
(pgm->draft && !elt->draft) ||
(pgm->undraft && elt->draft) ||
(pgm->flagged && !elt->flagged) ||
(pgm->unflagged && elt->flagged) ||
(pgm->recent && !elt->recent) ||
(pgm->old && elt->recent) ||
(pgm->seen && !elt->seen) ||
(pgm->unseen && elt->seen)) return NIL;
/* keywords */
if ((pgm->keyword && !mail_search_keyword (stream,elt,pgm->keyword,LONGT)) ||
(pgm->unkeyword && !mail_search_keyword (stream,elt,pgm->unkeyword,NIL)))
return NIL;
/* internal date ranges */
if (pgm->before || pgm->on || pgm->since) {
d = mail_shortdate (elt->year,elt->month,elt->day);
if (pgm->before && (d >= pgm->before)) return NIL;
if (pgm->on && (d != pgm->on)) return NIL;
if (pgm->since && (d < pgm->since)) return NIL;
}
if (pgm->older || pgm->younger) {
unsigned long msgd = mail_longdate (elt);
if (pgm->older && msgd > (now - pgm->older)) return NIL;
if (pgm->younger && msgd < (now - pgm->younger)) return NIL;
}
/* envelope searches */
if (pgm->sentbefore || pgm->senton || pgm->sentsince ||
pgm->bcc || pgm->cc || pgm->from || pgm->to || pgm->subject ||
pgm->return_path || pgm->sender || pgm->reply_to || pgm->in_reply_to ||
pgm->message_id || pgm->newsgroups || pgm->followup_to ||
pgm->references) {
ENVELOPE *env;
MESSAGECACHE delt;
if (section) { /* use body part envelope */
BODY *body = mail_body (stream,msgno,section);
env = (body && (body->type == TYPEMESSAGE) && body->subtype &&
!strcmp (body->subtype,"RFC822")) ? body->nested.msg->env : NIL;
}
else { /* use top level envelope if no section */
if (pgm->header && !stream->scache && !(stream->dtb->flags & DR_LOCAL))
mail_fetch_header(stream,msgno,NIL,NIL,NIL,FT_PEEK|FT_SEARCHLOOKAHEAD);
env = mail_fetchenvelope (stream,msgno);
}
if (!env) return NIL; /* no envelope obtained */
/* sent date ranges */
if ((pgm->sentbefore || pgm->senton || pgm->sentsince) &&
(!mail_parse_date (&delt,env->date) ||
!(d = mail_shortdate (delt.year,delt.month,delt.day)) ||
(pgm->sentbefore && (d >= pgm->sentbefore)) ||
(pgm->senton && (d != pgm->senton)) ||
(pgm->sentsince && (d < pgm->sentsince)))) return NIL;
/* search headers */
if ((pgm->bcc && !mail_search_addr (env->bcc,pgm->bcc)) ||
(pgm->cc && !mail_search_addr (env->cc,pgm->cc)) ||
(pgm->from && !mail_search_addr (env->from,pgm->from)) ||
(pgm->to && !mail_search_addr (env->to,pgm->to)) ||
(pgm->subject && !mail_search_header_text (env->subject,pgm->subject)))
return NIL;
/* These criteria are not supported by IMAP and have to be emulated */
if ((pgm->return_path &&
!mail_search_addr (env->return_path,pgm->return_path)) ||
(pgm->sender && !mail_search_addr (env->sender,pgm->sender)) ||
(pgm->reply_to && !mail_search_addr (env->reply_to,pgm->reply_to)) ||
(pgm->in_reply_to &&
!mail_search_header_text (env->in_reply_to,pgm->in_reply_to)) ||
(pgm->message_id &&
!mail_search_header_text (env->message_id,pgm->message_id)) ||
(pgm->newsgroups &&
!mail_search_header_text (env->newsgroups,pgm->newsgroups)) ||
(pgm->followup_to &&
!mail_search_header_text (env->followup_to,pgm->followup_to)) ||
(pgm->references &&
!mail_search_header_text (env->references,pgm->references)))
return NIL;
}
/* search header lines */
for (hdr = pgm->header; hdr; hdr = hdr->next) {
char *t,*e,*v;
SIZEDTEXT s;
STRINGLIST sth,stc;
sth.next = stc.next = NIL; /* only one at a time */
sth.text.data = hdr->line.data;
sth.text.size = hdr->line.size;
/* get the header text */
if ((t = mail_fetch_header (stream,msgno,NIL,&sth,&s.size,
FT_INTERNAL | FT_PEEK |
(section ? NIL : FT_SEARCHLOOKAHEAD))) &&
strchr (t,':')) {
if (hdr->text.size) { /* anything matches empty search string */
/* non-empty, copy field data */
s.data = (unsigned char *) fs_get (s.size + 1);
/* for each line */
for (v = (char *) s.data, e = t + s.size; t < e;) switch (*t) {
default: /* non-continuation, skip leading field name */
while ((t < e) && (*t++ != ':'));
if ((t < e) && (*t == ':')) t++;
case '\t': case ' ': /* copy field data */
while ((t < e) && (*t != '\015') && (*t != '\012')) *v++ = *t++;
*v++ = '\n'; /* tie off line */
while (((*t == '\015') || (*t == '\012')) && (t < e)) t++;
}
/* calculate true size */
s.size = v - (char *) s.data;
*v = '\0'; /* tie off results */
stc.text.data = hdr->text.data;
stc.text.size = hdr->text.size;
/* search header */
if (mail_search_header (&s,&stc)) fs_give ((void **) &s.data);
else { /* search failed */
fs_give ((void **) &s.data);
return NIL;
}
}
}
else return NIL; /* no matching header text */
}
/* search strings */
if ((pgm->text && !mail_search_text (stream,msgno,section,pgm->text,LONGT))||
(pgm->body && !mail_search_text (stream,msgno,section,pgm->body,NIL)))
return NIL;
/* logical conditions */
for (or = pgm->or; or; or = or->next)
if (!(mail_search_msg (stream,msgno,section,or->first) ||
mail_search_msg (stream,msgno,section,or->second))) return NIL;
for (not = pgm->not; not; not = not->next)
if (mail_search_msg (stream,msgno,section,not->pgm)) return NIL;
return T;
}
/* Mail search message header null-terminated text
* Accepts: header text
* strings to search
* Returns: T if search found a match
*/
long mail_search_header_text (char *s,STRINGLIST *st)
{
SIZEDTEXT h;
/* have any text? */
if (h.data = (unsigned char *) s) {
h.size = strlen (s); /* yes, get its size */
return mail_search_header (&h,st);
}
return NIL;
}
/* Mail search message header
* Accepts: header as sized text
* strings to search
* Returns: T if search found a match
*/
long mail_search_header (SIZEDTEXT *hdr,STRINGLIST *st)
{
SIZEDTEXT h;
long ret = LONGT;
/* make UTF-8 version of header */
utf8_mime2text (hdr,&h,U8T_CANONICAL);
while (h.size && ((h.data[h.size-1]=='\015') || (h.data[h.size-1]=='\012')))
--h.size; /* slice off trailing newlines */
do if (h.size ? /* search non-empty string */
!ssearch (h.data,h.size,st->text.data,st->text.size) : st->text.size)
ret = NIL;
while (ret && (st = st->next));
if (h.data != hdr->data) fs_give ((void **) &h.data);
return ret;
}
/* Mail search message body
* Accepts: MAIL stream
* message number
* optional section specification
* string list
* flags
* Returns: T if search found a match
*/
long mail_search_text (MAILSTREAM *stream,unsigned long msgno,char *section,
STRINGLIST *st,long flags)
{
BODY *body;
long ret = NIL;
STRINGLIST *s = mail_newstringlist ();
mailgets_t omg = mailgets;
if (stream->dtb->flags & DR_LOWMEM) mailgets = mail_search_gets;
/* strings to search */
for (stream->private.search.string = s; st;) {
s->text.data = st->text.data;
s->text.size = st->text.size;
if (st = st->next) s = s->next = mail_newstringlist ();
}
stream->private.search.text = NIL;
if (flags) { /* want header? */
SIZEDTEXT s,t;
s.data = (unsigned char *)
mail_fetch_header (stream,msgno,section,NIL,&s.size,FT_INTERNAL|FT_PEEK);
utf8_mime2text (&s,&t,U8T_CANONICAL);
ret = mail_search_string_work (&t,&stream->private.search.string);
if (t.data != s.data) fs_give ((void **) &t.data);
}
if (!ret) { /* still looking for match? */
/* no section, get top-level body */
if (!section) mail_fetchstructure (stream,msgno,&body);
/* get body of nested message */
else if ((body = mail_body (stream,msgno,section)) &&
(body->type == TYPEMULTIPART) && body->subtype &&
!strcmp (body->subtype,"RFC822")) body = body->nested.msg->body;
if (body) ret = mail_search_body (stream,msgno,body,NIL,1,flags);
}
mailgets = omg; /* restore former gets routine */
/* clear searching */
for (s = stream->private.search.string; s; s = s->next) s->text.data = NIL;
mail_free_stringlist (&stream->private.search.string);
stream->private.search.text = NIL;
return ret;
}
/* Mail search message body text parts
* Accepts: MAIL stream
* message number
* current body pointer
* hierarchical level prefix
* position at current hierarchical level
* string list
* flags
* Returns: T if search found a match
*/
long mail_search_body (MAILSTREAM *stream,unsigned long msgno,BODY *body,
char *prefix,unsigned long section,long flags)
{
long ret = NIL;
unsigned long i;
char *s,*t,sect[MAILTMPLEN];
SIZEDTEXT st,h;
PART *part;
PARAMETER *param;
if (prefix && (strlen (prefix) > (MAILTMPLEN - 20))) return NIL;
sprintf (sect,"%s%lu",prefix ? prefix : "",section++);
if (flags && prefix) { /* want to search MIME header too? */
st.data = (unsigned char *) mail_fetch_mime (stream,msgno,sect,&st.size,
FT_INTERNAL | FT_PEEK);
if (stream->dtb->flags & DR_LOWMEM) ret = stream->private.search.result;
else {
/* make UTF-8 version of header */
utf8_mime2text (&st,&h,U8T_CANONICAL);
ret = mail_search_string_work (&h,&stream->private.search.string);
if (h.data != st.data) fs_give ((void **) &h.data);
}
}
if (!ret) switch (body->type) {
case TYPEMULTIPART:
/* extend prefix if not first time */
s = prefix ? strcat (sect,".") : "";
for (i = 1,part = body->nested.part; part && !ret; i++,part = part->next)
ret = mail_search_body (stream,msgno,&part->body,s,i,flags);
break;
case TYPEMESSAGE:
if (!strcmp (body->subtype,"RFC822")) {
if (flags) { /* want to search nested message header? */
st.data = (unsigned char *)
mail_fetch_header (stream,msgno,sect,NIL,&st.size,
FT_INTERNAL | FT_PEEK);
if (stream->dtb->flags & DR_LOWMEM) ret =stream->private.search.result;
else {
/* make UTF-8 version of header */
utf8_mime2text (&st,&h,U8T_CANONICAL);
ret = mail_search_string_work (&h,&stream->private.search.string);
if (h.data != st.data) fs_give ((void **) &h.data);
}
}
if (body = body->nested.msg->body)
ret = (body->type == TYPEMULTIPART) ?
mail_search_body (stream,msgno,body,(prefix ? prefix : ""),
section - 1,flags) :
mail_search_body (stream,msgno,body,strcat (sect,"."),1,flags);
break;
}
/* non-MESSAGE/RFC822 falls into text case */
case TYPETEXT:
s = mail_fetch_body (stream,msgno,sect,&i,FT_INTERNAL | FT_PEEK);
if (stream->dtb->flags & DR_LOWMEM) ret = stream->private.search.result;
else {
for (t = NIL,param = body->parameter; param && !t; param = param->next)
if (!strcmp (param->attribute,"CHARSET")) t = param->value;
switch (body->encoding) { /* what encoding? */
case ENCBASE64:
if (st.data = (unsigned char *)
rfc822_base64 ((unsigned char *) s,i,&st.size)) {
ret = mail_search_string (&st,t,&stream->private.search.string);
fs_give ((void **) &st.data);
}
break;
case ENCQUOTEDPRINTABLE:
if (st.data = rfc822_qprint ((unsigned char *) s,i,&st.size)) {
ret = mail_search_string (&st,t,&stream->private.search.string);
fs_give ((void **) &st.data);
}
break;
default:
st.data = (unsigned char *) s;
st.size = i;
ret = mail_search_string (&st,t,&stream->private.search.string);
break;
}
}
break;
}
return ret;
}
/* Mail search text
* Accepts: sized text to search
* character set of sized text
* string list of search keys
* Returns: T if search found a match
*/
long mail_search_string (SIZEDTEXT *s,char *charset,STRINGLIST **st)
{
SIZEDTEXT u;
long ret;
STRINGLIST **sc = st;
/* convert to UTF-8 as best we can */
if (!utf8_text (s,charset,&u,U8T_CANONICAL))
utf8_text (s,NIL,&u,U8T_CANONICAL);
ret = mail_search_string_work (&u,st);
if (u.data != s->data) fs_give ((void **) &u.data);
return ret;
}
/* Mail search text worker routine
* Accepts: sized text to search
* string list of search keys
* Returns: T if search found a match
*/
long mail_search_string_work (SIZEDTEXT *s,STRINGLIST **st)
{
void *t;
STRINGLIST **sc = st;
while (*sc) { /* run down criteria list */
if (ssearch (s->data,s->size,(*sc)->text.data,(*sc)->text.size)) {
t = (void *) (*sc); /* found one, need to flush this */
*sc = (*sc)->next; /* remove it from the list */
fs_give (&t); /* flush the buffer */
}
else sc = &(*sc)->next; /* move to next in list */
}
return *st ? NIL : LONGT;
}
/* Mail search keyword
* Accepts: MAIL stream
* elt to get flags from
* keyword list
* T for keyword search, NIL for unkeyword search
* Returns: T if search found a match
*/
long mail_search_keyword (MAILSTREAM *stream,MESSAGECACHE *elt,STRINGLIST *st,
long flag)
{
int i,j;
unsigned long f = 0;
unsigned long tf;
do {
for (i = 0; (j = (i < NUSERFLAGS) && stream->user_flags[i]); ++i)
if (!compare_csizedtext (stream->user_flags[i],&st->text)) {
f |= (1 << i);
break;
}
if (flag && !j) return NIL;
} while (st = st->next);
tf = elt->user_flags & f; /* get set flags which match */
return flag ? (f == tf) : !tf;
}
/* Mail search an address list
* Accepts: address list
* string list
* Returns: T if search found a match
*/
#define SEARCHBUFLEN (size_t) 2000
#define SEARCHBUFSLOP (size_t) 5
long mail_search_addr (ADDRESS *adr,STRINGLIST *st)
{
ADDRESS *a,tadr;
SIZEDTEXT txt;
char tmp[SENDBUFLEN + 1];
size_t i = SEARCHBUFLEN;
size_t k;
long ret = NIL;
if (adr) {
txt.data = (unsigned char *) fs_get (i + SEARCHBUFSLOP);
/* never an error or next */
tadr.error = NIL,tadr.next = NIL;
/* write address list */
for (txt.size = 0,a = adr; a; a = a->next) {
k = (tadr.mailbox = a->mailbox) ? 4 + 2*strlen (a->mailbox) : 3;
if (tadr.personal = a->personal) k += 3 + 2*strlen (a->personal);
if (tadr.adl = a->adl) k += 3 + 2*strlen (a->adl);
if (tadr.host = a->host) k += 3 + 2*strlen (a->host);
if (tadr.personal || tadr.adl) k += 2;
if (k < (SENDBUFLEN-10)) {/* ignore ridiculous addresses */
tmp[0] = '\0';
rfc822_write_address (tmp,&tadr);
/* resize buffer if necessary */
if (((k = strlen (tmp)) + txt.size) > i)
fs_resize ((void **) &txt.data,SEARCHBUFSLOP + (i += SEARCHBUFLEN));
/* add new address */
memcpy (txt.data + txt.size,tmp,k);
txt.size += k;
/* another address follows */
if (a->next) txt.data[txt.size++] = ',';
}
}
txt.data[txt.size] = '\0'; /* tie off string */
ret = mail_search_header (&txt,st);
fs_give ((void **) &txt.data);
}
return ret;
}
/* Get string for low-memory searching
* Accepts: readin function pointer
* stream to use
* number of bytes
* gets data packet
* mail stream
* message number
* descriptor string
* option flags
* Returns: NIL, always
*/
#define SEARCHSLOP 128
char *mail_search_gets (readfn_t f,void *stream,unsigned long size,
GETS_DATA *md)
{
unsigned long i;
char tmp[MAILTMPLEN+SEARCHSLOP+1];
SIZEDTEXT st;
/* better not be called unless searching */
if (!md->stream->private.search.string) {
sprintf (tmp,"Search botch, mbx = %.80s, %s = %lu[%.80s]",
md->stream->mailbox,
(md->flags & FT_UID) ? "UID" : "msg",md->msgno,md->what);
fatal (tmp);
}
/* initially no match for search */
md->stream->private.search.result = NIL;
/* make sure buffer clear */
memset (st.data = (unsigned char *) tmp,'\0',
(size_t) MAILTMPLEN+SEARCHSLOP+1);
/* read first buffer */
(*f) (stream,st.size = i = min (size,(long) MAILTMPLEN),tmp);
/* search for text */
if (mail_search_string (&st,NIL,&md->stream->private.search.string))
md->stream->private.search.result = T;
else if (size -= i) { /* more to do, blat slop down */
memmove (tmp,tmp+MAILTMPLEN-SEARCHSLOP,(size_t) SEARCHSLOP);
do { /* read subsequent buffers one at a time */
(*f) (stream,i = min (size,(long) MAILTMPLEN),tmp+SEARCHSLOP);
st.size = i + SEARCHSLOP;
if (mail_search_string (&st,NIL,&md->stream->private.search.string))
md->stream->private.search.result = T;
else memmove (tmp,tmp+MAILTMPLEN,(size_t) SEARCHSLOP);
}
while ((size -= i) && !md->stream->private.search.result);
}
if (size) { /* toss out everything after that */
do (*f) (stream,i = min (size,(long) MAILTMPLEN),tmp);
while (size -= i);
}
return NIL;
}
/* Mail parse search criteria
* Accepts: criteria
* Returns: search program if parse successful, else NIL
*/
SEARCHPGM *mail_criteria (char *criteria)
{
SEARCHPGM *pgm = NIL;
char *criterion,*r,tmp[MAILTMPLEN];
int f;
if (criteria) { /* only if criteria defined */
/* make writeable copy of criteria */
criteria = cpystr (criteria);
/* for each criterion */
for (pgm = mail_newsearchpgm (), criterion = strtok_r (criteria," ",&r);
criterion; (criterion = strtok_r (NIL," ",&r))) {
f = NIL; /* init then scan the criterion */
switch (*ucase (criterion)) {
case 'A': /* possible ALL, ANSWERED */
if (!strcmp (criterion+1,"LL")) f = T;
else if (!strcmp (criterion+1,"NSWERED")) f = pgm->answered = T;
break;
case 'B': /* possible BCC, BEFORE, BODY */
if (!strcmp (criterion+1,"CC"))
f = mail_criteria_string (&pgm->bcc,&r);
else if (!strcmp (criterion+1,"EFORE"))
f = mail_criteria_date (&pgm->before,&r);
else if (!strcmp (criterion+1,"ODY"))
f = mail_criteria_string (&pgm->body,&r);
break;
case 'C': /* possible CC */
if (!strcmp (criterion+1,"C")) f = mail_criteria_string (&pgm->cc,&r);
break;
case 'D': /* possible DELETED */
if (!strcmp (criterion+1,"ELETED")) f = pgm->deleted = T;
break;
case 'F': /* possible FLAGGED, FROM */
if (!strcmp (criterion+1,"LAGGED")) f = pgm->flagged = T;
else if (!strcmp (criterion+1,"ROM"))
f = mail_criteria_string (&pgm->from,&r);
break;
case 'K': /* possible KEYWORD */
if (!strcmp (criterion+1,"EYWORD"))
f = mail_criteria_string (&pgm->keyword,&r);
break;
case 'N': /* possible NEW */
if (!strcmp (criterion+1,"EW")) f = pgm->recent = pgm->unseen = T;
break;
case 'O': /* possible OLD, ON */
if (!strcmp (criterion+1,"LD")) f = pgm->old = T;
else if (!strcmp (criterion+1,"N"))
f = mail_criteria_date (&pgm->on,&r);
break;
case 'R': /* possible RECENT */
if (!strcmp (criterion+1,"ECENT")) f = pgm->recent = T;
break;
case 'S': /* possible SEEN, SINCE, SUBJECT */
if (!strcmp (criterion+1,"EEN")) f = pgm->seen = T;
else if (!strcmp (criterion+1,"INCE"))
f = mail_criteria_date (&pgm->since,&r);
else if (!strcmp (criterion+1,"UBJECT"))
f = mail_criteria_string (&pgm->subject,&r);
break;
case 'T': /* possible TEXT, TO */
if (!strcmp (criterion+1,"EXT"))
f = mail_criteria_string (&pgm->text,&r);
else if (!strcmp (criterion+1,"O"))
f = mail_criteria_string (&pgm->to,&r);
break;
case 'U': /* possible UN* */
if (criterion[1] == 'N') {
if (!strcmp (criterion+2,"ANSWERED")) f = pgm->unanswered = T;
else if (!strcmp (criterion+2,"DELETED")) f = pgm->undeleted = T;
else if (!strcmp (criterion+2,"FLAGGED")) f = pgm->unflagged = T;
else if (!strcmp (criterion+2,"KEYWORD"))
f = mail_criteria_string (&pgm->unkeyword,&r);
else if (!strcmp (criterion+2,"SEEN")) f = pgm->unseen = T;
}
break;
default: /* we will barf below */
break;
}
if (!f) { /* if can't identify criterion */
sprintf (tmp,"Unknown search criterion: %.30s",criterion);
MM_LOG (tmp,ERROR);
mail_free_searchpgm (&pgm);
break;
}
}
/* no longer need copy of criteria */
fs_give ((void **) &criteria);
}
return pgm;
}
/* Parse a date
* Accepts: pointer to date integer to return
* pointer to strtok state
* Returns: T if successful, else NIL
*/
int mail_criteria_date (unsigned short *date,char **r)
{
STRINGLIST *s = NIL;
MESSAGECACHE elt;
/* parse the date and return fn if OK */
int ret = (mail_criteria_string (&s,r) &&
mail_parse_date (&elt,(char *) s->text.data) &&
(*date = mail_shortdate (elt.year,elt.month,elt.day))) ?
T : NIL;
if (s) mail_free_stringlist (&s);
return ret;
}
/* Calculate shortdate from elt values
* Accepts: year (0 = BASEYEAR)
* month (1 = January)
* day
* Returns: shortdate
*/
unsigned short mail_shortdate (unsigned int year,unsigned int month,
unsigned int day)
{
return (year << 9) + (month << 5) + day;
}
/* Parse a string
* Accepts: pointer to stringlist
* pointer to strtok state
* Returns: T if successful, else NIL
*/
int mail_criteria_string (STRINGLIST **s,char **r)
{
unsigned long n;
char e,*d,*end = " ",*c = strtok_r (NIL,"",r);
if (!c) return NIL; /* missing argument */
switch (*c) { /* see what the argument is */
case '{': /* literal string */
n = strtoul (c+1,&d,10); /* get its length */
if ((*d++ == '}') && (*d++ == '\015') && (*d++ == '\012') &&
(!(*(c = d + n)) || (*c == ' '))) {
e = *--c; /* store old delimiter */
*c = '\377'; /* make sure not a space */
strtok_r (c," ",r); /* reset the strtok mechanism */
*c = e; /* put character back */
break;
}
case '\0': /* catch bogons */
case ' ':
return NIL;
case '"': /* quoted string */
if (strchr (c+1,'"')) end = "\"";
else return NIL; /* falls through */
default: /* atomic string */
if (d = strtok_r (c,end,r)) n = strlen (d);
else return NIL;
break;
}
while (*s) s = &(*s)->next; /* find tail of list */
*s = mail_newstringlist (); /* make new entry */
/* return the data */
(*s)->text.data = (unsigned char *) cpystr (d);
(*s)->text.size = n;
return T;
}
/* Mail parse set from string
* Accepts: string to parse
* pointer to updated string pointer for return
* Returns: set with pointer updated, or NIL if error
*/
SEARCHSET *mail_parse_set (char *s,char **ret)
{
SEARCHSET *cur;
SEARCHSET *set = NIL;
while (isdigit (*s)) {
if (!set) cur = set = mail_newsearchset ();
else cur = cur->next = mail_newsearchset ();
/* parse value */
if (!(cur->first = strtoul (s,&s,10)) ||
((*s == ':') && !(isdigit (*++s) && (cur->last = strtoul (s,&s,10)))))
break; /* bad value or range */
if (*s == ',') ++s; /* point to next value if more */
else { /* end of set */
*ret = s; /* set return pointer */
return set; /* return set */
}
}
mail_free_searchset (&set); /* failure, punt partial set */
return NIL;
}
/* Mail append to set
* Accepts: head of search set or NIL to do nothing
* message to add
* Returns: tail of search set or NIL if did nothing
*/
SEARCHSET *mail_append_set (SEARCHSET *set,unsigned long msgno)
{
if (set) { /* find tail */
while (set->next) set = set->next;
/* start of set if no first member */
if (!set->first) set->first = msgno;
else if (msgno == (set->last ? set->last : set->first) + 1)
set->last = msgno; /* extend range if 1 past current */
else (set = set->next = mail_newsearchset ())->first = msgno;
}
return set;
}
/* Mail sort messages
* Accepts: mail stream
* character set
* search program
* sort program
* option flags
* Returns: vector of sorted message sequences or NIL if error
*/
unsigned long *mail_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
SORTPGM *pgm,long flags)
{
unsigned long *ret = NIL;
if (stream->dtb) /* do the driver's action */
ret = (*(stream->dtb->sort ? stream->dtb->sort : mail_sort_msgs))
(stream,charset,spg,pgm,flags);
/* flush search/sort programs if requested */
if (spg && (flags & SE_FREE)) mail_free_searchpgm (&spg);
if (flags & SO_FREE) mail_free_sortpgm (&pgm);
return ret;
}
/* Mail sort messages work routine
* Accepts: mail stream
* character set
* search program
* sort program
* option flags
* Returns: vector of sorted message sequences or NIL if error
*/
unsigned long *mail_sort_msgs (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
SORTPGM *pgm,long flags)
{
unsigned long i;
SORTCACHE **sc;
unsigned long *ret = NIL;
if (spg) { /* only if a search needs to be done */
int silent = stream->silent;
stream->silent = T; /* don't pass up mm_searched() events */
/* search for messages */
mail_search_full (stream,charset,spg,NIL);
stream->silent = silent; /* restore silence state */
}
/* initialize progress counters */
pgm->nmsgs = pgm->progress.cached = 0;
/* pass 1: count messages to sort */
for (i = 1; i <= stream->nmsgs; ++i)
if (mail_elt (stream,i)->searched) pgm->nmsgs++;
if (pgm->nmsgs) { /* pass 2: sort cache */
sc = mail_sort_loadcache (stream,pgm);
/* pass 3: sort messages */
if (!pgm->abort) ret = mail_sort_cache (stream,pgm,sc,flags);
fs_give ((void **) &sc); /* don't need sort vector any more */
}
/* empty sort results */
else ret = (unsigned long *) memset (fs_get (sizeof (unsigned long)),0,
sizeof (unsigned long));
/* also return via callback if requested */
if (mailsortresults) (*mailsortresults) (stream,ret,pgm->nmsgs);
return ret; /* return sort results */
}
/* Mail sort sortcache vector
* Accepts: mail stream
* sort program
* sortcache vector
* option flags
* Returns: vector of sorted message sequences or NIL if error
*/
unsigned long *mail_sort_cache (MAILSTREAM *stream,SORTPGM *pgm,SORTCACHE **sc,
long flags)
{
unsigned long i,*ret;
/* pass 3: sort messages */
qsort ((void *) sc,pgm->nmsgs,sizeof (SORTCACHE *),mail_sort_compare);
/* optional post sorting */
if (pgm->postsort) (*pgm->postsort) ((void *) sc);
/* pass 4: return results */
ret = (unsigned long *) fs_get ((pgm->nmsgs+1) * sizeof (unsigned long));
if (flags & SE_UID) /* UID or msgno? */
for (i = 0; i < pgm->nmsgs; i++) ret[i] = mail_uid (stream,sc[i]->num);
else for (i = 0; i < pgm->nmsgs; i++) ret[i] = sc[i]->num;
ret[pgm->nmsgs] = 0; /* tie off message list */
return ret;
}
/* Mail load sortcache
* Accepts: mail stream, already searched
* sort program
* Returns: vector of sortcache pointers matching search
*/
static STRINGLIST maildateline = {{(unsigned char *) "date",4},NIL};
static STRINGLIST mailrnfromline = {{(unsigned char *) ">from",5},NIL};
static STRINGLIST mailfromline = {{(unsigned char *) "from",4},
&mailrnfromline};
static STRINGLIST mailtonline = {{(unsigned char *) "to",2},NIL};
static STRINGLIST mailccline = {{(unsigned char *) "cc",2},NIL};
static STRINGLIST mailsubline = {{(unsigned char *) "subject",7},NIL};
SORTCACHE **mail_sort_loadcache (MAILSTREAM *stream,SORTPGM *pgm)
{
char *t,*v,*x,tmp[MAILTMPLEN];
SORTPGM *pg;
SORTCACHE *s,**sc;
MESSAGECACHE *elt,telt;
ENVELOPE *env;
ADDRESS *adr = NIL;
unsigned long i = (pgm->nmsgs) * sizeof (SORTCACHE *);
sc = (SORTCACHE **) memset (fs_get ((size_t) i),0,(size_t) i);
/* see what needs to be loaded */
for (i = 1; !pgm->abort && (i <= stream->nmsgs); i++)
if ((elt = mail_elt (stream,i))->searched) {
sc[pgm->progress.cached++] =
s = (SORTCACHE *) (*mailcache) (stream,i,CH_SORTCACHE);
s->pgm = pgm; /* note sort program */
s->num = i;
/* get envelope if cached */
if (stream->scache) env = (i == stream->msgno) ? stream->env : NIL;
else env = elt->private.msg.env;
for (pg = pgm; pg; pg = pg->next) switch (pg->function) {
case SORTARRIVAL: /* sort by arrival date */
if (!s->arrival) {
/* internal date unknown but can get? */
if (!elt->day && !(stream->dtb->flags & DR_NOINTDATE)) {
sprintf (tmp,"%lu",i);
mail_fetch_fast (stream,tmp,NIL);
}
/* wrong thing before 3-Jan-1970 */
s->arrival = elt->day ? mail_longdate (elt) : 1;
s->dirty = T;
}
break;
case SORTSIZE: /* sort by message size */
if (!s->size) {
if (!elt->rfc822_size) {
sprintf (tmp,"%lu",i);
mail_fetch_fast (stream,tmp,NIL);
}
s->size = elt->rfc822_size ? elt->rfc822_size : 1;
s->dirty = T;
}
break;
case SORTDATE: /* sort by date */
if (!s->date) {
if (env) t = env->date;
else if ((t = mail_fetch_header (stream,i,NIL,&maildateline,NIL,
FT_INTERNAL | FT_PEEK)) &&
(t = strchr (t,':')))
for (x = ++t; x = strpbrk (x,"\012\015"); x++)
switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
case ' ': /* erase continuation newlines */
case '\t':
memmove (x,v,strlen (v));
break;
default: /* tie off extraneous text */
*x = x[1] = '\0';
}
/* skip leading whitespace */
if (t) while ((*t == ' ') || (*t == '\t')) t++;
/* parse date from Date: header */
if (!(t && mail_parse_date (&telt,t) &&
(s->date = mail_longdate (&telt)))) {
/* failed, use internal date */
if (!(s->date = s->arrival)) {
/* internal date unknown but can get? */
if (!elt->day && !(stream->dtb->flags & DR_NOINTDATE)) {
sprintf (tmp,"%lu",i);
mail_fetch_fast (stream,tmp,NIL);
}
/* wrong thing before 3-Jan-1970 */
s->date = (s->arrival = elt->day ? mail_longdate (elt) : 1);
}
}
s->dirty = T;
}
break;
case SORTFROM: /* sort by first from */
if (!s->from) {
if (env) s->from = env->from && env->from->mailbox ?
cpystr (env->from->mailbox) : NIL;
else if ((t = mail_fetch_header (stream,i,NIL,&mailfromline,NIL,
FT_INTERNAL | FT_PEEK)) &&
(t = strchr (t,':'))) {
for (x = ++t; x = strpbrk (x,"\012\015"); x++)
switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
case ' ': /* erase continuation newlines */
case '\t':
memmove (x,v,strlen (v));
break;
case 'f': /* continuation but with extra "From:" */
case 'F':
if (v = strchr (v,':')) {
memmove (x,v+1,strlen (v+1));
break;
}
default: /* tie off extraneous text */
*x = x[1] = '\0';
}
if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
s->from = adr->mailbox;
adr->mailbox = NIL;
mail_free_address (&adr);
}
}
if (!s->from) s->from = cpystr ("");
s->dirty = T;
}
break;
case SORTTO: /* sort by first to */
if (!s->to) {
if (env) s->to = env->to && env->to->mailbox ?
cpystr (env->to->mailbox) : NIL;
else if ((t = mail_fetch_header (stream,i,NIL,&mailtonline,NIL,
FT_INTERNAL | FT_PEEK)) &&
(t = strchr (t,':'))) {
for (x = ++t; x = strpbrk (x,"\012\015"); x++)
switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
case ' ': /* erase continuation newlines */
case '\t':
memmove (x,v,strlen (v));
break;
case 't': /* continuation but with extra "To:" */
case 'T':
if (v = strchr (v,':')) {
memmove (x,v+1,strlen (v+1));
break;
}
default: /* tie off extraneous text */
*x = x[1] = '\0';
}
if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
s->to = adr->mailbox;
adr->mailbox = NIL;
mail_free_address (&adr);
}
}
if (!s->to) s->to = cpystr ("");
s->dirty = T;
}
break;
case SORTCC: /* sort by first cc */
if (!s->cc) {
if (env) s->cc = env->cc && env->cc->mailbox ?
cpystr (env->cc->mailbox) : NIL;
else if ((t = mail_fetch_header (stream,i,NIL,&mailccline,NIL,
FT_INTERNAL | FT_PEEK)) &&
(t = strchr (t,':'))) {
for (x = ++t; x = strpbrk (x,"\012\015"); x++)
switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
case ' ': /* erase continuation newlines */
case '\t':
memmove (x,v,strlen (v));
break;
case 't': /* continuation but with extra "To:" */
case 'T':
if (v = strchr (v,':')) {
memmove (x,v+1,strlen (v+1));
break;
}
default: /* tie off extraneous text */
*x = x[1] = '\0';
}
if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
s->cc = adr->mailbox;
adr->mailbox = NIL;
mail_free_address (&adr);
}
}
if (!s->cc) s->cc = cpystr ("");
s->dirty = T;
}
break;
case SORTSUBJECT: /* sort by subject */
if (!s->subject) {
/* get subject from envelope if have one */
if (env) t = env->subject ? env->subject : "";
/* otherwise snarf from header text */
else if ((t = mail_fetch_header (stream,i,NIL,&mailsubline,
NIL,FT_INTERNAL | FT_PEEK)) &&
(t = strchr (t,':')))
for (x = ++t; x = strpbrk (x,"\012\015"); x++)
switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
case ' ': /* erase continuation newlines */
case '\t':
memmove (x,v,strlen (v));
break;
default: /* tie off extraneous text */
*x = x[1] = '\0';
}
else t = ""; /* empty subject */
/* strip and cache subject */
s->refwd = mail_strip_subject (t,&s->subject);
s->dirty = T;
}
break;
default:
fatal ("Unknown sort function");
}
}
return sc;
}
/* Strip subjects of extra spaces and leading and trailing cruft for sorting
* Accepts: unstripped subject
* pointer to return stripped subject, in cpystr form
* Returns: T if subject had a re/fwd, NIL otherwise
*/
unsigned int mail_strip_subject (char *t,char **ret)
{
SIZEDTEXT src,dst;
unsigned long i,slen;
char c,*s,*x;
unsigned int refwd = NIL;
if (src.size = strlen (t)) { /* have non-empty subject? */
src.data = (unsigned char *) t;
/* Step 1 */
/* make copy, convert MIME2 if needed */
*ret = s = (utf8_mime2text (&src,&dst,U8T_CANONICAL) &&
(src.data != dst.data)) ? (char *) dst.data : cpystr (t);
/* convert spaces to tab, strip extra spaces */
for (x = t = s, c = 'x'; *t; t++) {
if (c != ' ') c = *x++ = ((*t == '\t') ? ' ' : *t);
else if ((*t != '\t') && (*t != ' ')) c = *x++ = *t;
}
*x = '\0'; /* tie off string */
/* Step 2 */
for (slen = dst.size; s; slen = strlen (s)) {
for (t = s + slen; t > s; ) switch (t[-1]) {
case ' ': case '\t': /* WSP */
*--t = '\0'; /* just remove it */
break;
case ')': /* possible "(fwd)" */
if ((t >= (s + 5)) && (t[-5] == '(') &&
((t[-4] == 'F') || (t[-4] == 'f')) &&
((t[-3] == 'W') || (t[-3] == 'w')) &&
((t[-2] == 'D') || (t[-2] == 'd'))) {
*(t -= 5) = '\0'; /* remove "(fwd)" */
refwd = T; /* note a re/fwd */
break;
}
default: /* not a subj-trailer */
t = s;
break;
}
/* Steps 3-5 */
for (t = s; t; ) switch (*s) {
case ' ': case '\t': /* WSP */
s = t = mail_strip_subject_wsp (s + 1);
break;
case 'r': case 'R': /* possible "re" */
if (((s[1] == 'E') || (s[1] == 'e')) &&
(t = mail_strip_subject_wsp (s + 2)) &&
(t = mail_strip_subject_blob (t)) && (*t == ':')) {
s = ++t; /* found "re" */
refwd = T; /* definitely a re/fwd at this point */
}
else t = NIL; /* found subj-middle */
break;
case 'f': case 'F': /* possible "fw" or "fwd" */
if (((s[1] == 'w') || (s[1] == 'W')) &&
(((s[2] == 'd') || (s[2] == 'D')) ?
(t = mail_strip_subject_wsp (s + 3)) :
(t = mail_strip_subject_wsp (s + 2))) &&
(t = mail_strip_subject_blob (t)) && (*t == ':')) {
s = ++t; /* found "fwd" */
refwd = T; /* definitely a re/fwd at this point */
}
else t = NIL; /* found subj-middle */
break;
case '[': /* possible subj-blob */
if ((t = mail_strip_subject_blob (s)) && *t) s = t;
else t = NIL; /* found subj-middle */
break;
default:
t = NIL; /* found subj-middle */
break;
}
/* Step 6 */
/* Netscape-style "[Fwd: ...]"? */
if ((*s == '[') && ((s[1] == 'F') || (s[1] == 'f')) &&
((s[2] == 'W') || (s[2] == 'w')) &&
((s[3] == 'D') || (s[3] == 'd')) && (s[4] == ':') &&
(s[i = strlen (s) - 1] == ']')) {
s[i] = '\0'; /* flush closing "]" */
s += 5; /* and leading "[Fwd:" */
refwd = T; /* definitely a re/fwd at this point */
}
else break; /* don't need to loop back to step 2 */
}
if (s != (t = *ret)) { /* removed leading text? */
s = *ret = cpystr (s); /* yes, make a fresh return copy */
fs_give ((void **) &t); /* flush old copy */
}
}
else *ret = cpystr (""); /* empty subject */
return refwd; /* return re/fwd state */
}
/* Strip subject wsp helper routine
* Accepts: text
* Returns: pointer to text after blob
*/
char *mail_strip_subject_wsp (char *s)
{
while ((*s == ' ') || (*s == '\t')) s++;
return s;
}
/* Strip subject blob helper routine
* Accepts: text
* Returns: pointer to text after any blob, NIL if blob-like but not blob
*/
char *mail_strip_subject_blob (char *s)
{
if (*s != '[') return s; /* not a blob, ignore */
/* search for end of blob */
while (*++s != ']') if ((*s == '[') || !*s) return NIL;
return mail_strip_subject_wsp (s + 1);
}
/* Sort compare messages
* Accept: first message sort cache element
* second message sort cache element
* Returns: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
*/
int mail_sort_compare (const void *a1,const void *a2)
{
int i = 0;
SORTCACHE *s1 = *(SORTCACHE **) a1;
SORTCACHE *s2 = *(SORTCACHE **) a2;
SORTPGM *pgm = s1->pgm;
if (!s1->sorted) { /* this one sorted yet? */
s1->sorted = T;
pgm->progress.sorted++; /* another sorted message */
}
if (!s2->sorted) { /* this one sorted yet? */
s2->sorted = T;
pgm->progress.sorted++; /* another sorted message */
}
do {
switch (pgm->function) { /* execute search program */
case SORTDATE: /* sort by date */
i = compare_ulong (s1->date,s2->date);
break;
case SORTARRIVAL: /* sort by arrival date */
i = compare_ulong (s1->arrival,s2->arrival);
break;
case SORTSIZE: /* sort by message size */
i = compare_ulong (s1->size,s2->size);
break;
case SORTFROM: /* sort by first from */
i = compare_cstring (s1->from,s2->from);
break;
case SORTTO: /* sort by first to */
i = compare_cstring (s1->to,s2->to);
break;
case SORTCC: /* sort by first cc */
i = compare_cstring (s1->cc,s2->cc);
break;
case SORTSUBJECT: /* sort by subject */
i = compare_cstring (s1->subject,s2->subject);
break;
}
if (pgm->reverse) i = -i; /* flip results if necessary */
}
while (pgm = i ? NIL : pgm->next);
/* return result, avoid 0 if at all possible */
return i ? i : compare_ulong (s1->num,s2->num);
}
/* Return message date as an unsigned long seconds since time began
* Accepts: message cache pointer
* Returns: unsigned long of date
*
* This routine, like most UNIX systems, is clueless about leap seconds.
* Thus, it treats 23:59:60 as equivalent to 00:00:00 the next day.
*
* This routine forces any early hours on 1-Jan-1970 in oriental timezones
* to be 1-Jan-1970 00:00:00 UTC, so as to avoid negative longdates.
*/
unsigned long mail_longdate (MESSAGECACHE *elt)
{
unsigned long m = elt->month ? elt->month : 1;
unsigned long yr = elt->year + BASEYEAR;
/* number of days since time began */
unsigned long ret = (elt->day ? (elt->day - 1) : 0)
+ 30 * (m - 1) + ((m + (m > 8)) / 2)
#ifndef USEJULIANCALENDAR
#ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
+ ((yr / 400) - (BASEYEAR / 400)) - ((yr / 100) - (BASEYEAR / 100))
#ifdef Y4KBUGFIX
- ((yr / 4000) - (BASEYEAR / 4000))
#endif
- ((m < 3) ?
!(yr % 4) && ((yr % 100) || (!(yr % 400)
#ifdef Y4KBUGFIX
&& (yr % 4000)
#endif
)) : 2)
#else /* Orthodox calendar */
+ ((2*(yr / 900)) - (2*(BASEYEAR / 900)))
+ (((yr % 900) >= 200) - ((BASEYEAR % 900) >= 200))
+ (((yr % 900) >= 600) - ((BASEYEAR % 900) >= 600))
- ((yr / 100) - (BASEYEAR / 100))
- ((m < 3) ?
!(yr % 4) && ((yr % 100) || ((yr % 900) == 200) || ((yr % 900) == 600))
: 2)
#endif
#endif
+ elt->year * 365 + (((unsigned long) (elt->year + (BASEYEAR % 4))) / 4);
ret *= 24; ret += elt->hours; /* date value in hours */
ret *= 60; ret +=elt->minutes;/* date value in minutes */
yr = (elt->zhours * 60) + elt->zminutes;
if (elt->zoccident) ret += yr;/* occidental timezone, make UTC */
else if (ret < yr) return 0; /* still 31-Dec-1969 in UTC */
else ret -= yr; /* oriental timezone, make UTC */
ret *= 60; ret += elt->seconds;
return ret;
}
/* Mail thread messages
* Accepts: mail stream
* thread type
* character set
* search program
* option flags
* Returns: thread node tree or NIL if error
*/
THREADNODE *mail_thread (MAILSTREAM *stream,char *type,char *charset,
SEARCHPGM *spg,long flags)
{
THREADNODE *ret = NIL;
if (stream->dtb) /* must have a live driver */
ret = stream->dtb->thread ? /* do driver's action if available */
(*stream->dtb->thread) (stream,type,charset,spg,flags) :
mail_thread_msgs (stream,type,charset,spg,flags,mail_sort_msgs);
/* flush search/sort programs if requested */
if (spg && (flags & SE_FREE)) mail_free_searchpgm (&spg);
return ret;
}
/* Mail thread messages
* Accepts: mail stream
* thread type
* character set
* search program
* option flags
* sorter routine
* Returns: thread node tree or NIL if error
*/
THREADNODE *mail_thread_msgs (MAILSTREAM *stream,char *type,char *charset,
SEARCHPGM *spg,long flags,sorter_t sorter)
{
THREADER *t;
for (t = &mailthreadlist; t; t = t->next)
if (!compare_cstring (type,t->name)) {
THREADNODE *ret = (*t->dispatch) (stream,charset,spg,flags,sorter);
if (mailthreadresults) (*mailthreadresults) (stream,ret);
return ret;
}
MM_LOG ("No such thread type",ERROR);
return NIL;
}
/* Mail thread ordered subject
* Accepts: mail stream
* character set
* search program
* option flags
* sorter routine
* Returns: thread node tree
*/
THREADNODE *mail_thread_orderedsubject (MAILSTREAM *stream,char *charset,
SEARCHPGM *spg,long flags,
sorter_t sorter)
{
THREADNODE *thr = NIL;
THREADNODE *cur,*top,**tc;
SORTPGM pgm,pgm2;
SORTCACHE *s;
unsigned long i,j,*lst,*ls;
/* sort by subject+date */
memset (&pgm,0,sizeof (SORTPGM));
memset (&pgm2,0,sizeof (SORTPGM));
pgm.function = SORTSUBJECT;
pgm.next = &pgm2;
pgm2.function = SORTDATE;
if (lst = (*sorter) (stream,charset,spg,&pgm,flags & ~(SE_FREE | SE_UID))){
if (*(ls = lst)) { /* create thread */
/* note first subject */
cur = top = thr = mail_newthreadnode
((SORTCACHE *) (*mailcache) (stream,*ls++,CH_SORTCACHE));
/* note its number */
cur->num = (flags & SE_UID) ? mail_uid (stream,*lst) : *lst;
i = 1; /* number of threads */
while (*ls) { /* build tree */
/* subjects match? */
s = (SORTCACHE *) (*mailcache) (stream,*ls++,CH_SORTCACHE);
if (compare_cstring (top->sc->subject,s->subject)) {
i++; /* have a new thread */
top = top->branch = cur = mail_newthreadnode (s);
}
/* start a child of the top */
else if (cur == top) cur = cur->next = mail_newthreadnode (s);
/* sibling of child */
else cur = cur->branch = mail_newthreadnode (s);
/* set to msgno or UID as needed */
cur->num = (flags & SE_UID) ? mail_uid (stream,s->num) : s->num;
}
/* make threadnode cache */
tc = (THREADNODE **) fs_get (i * sizeof (THREADNODE *));
/* load threadnode cache */
for (j = 0, cur = thr; cur; cur = cur->branch) tc[j++] = cur;
if (i != j) fatal ("Threadnode cache confusion");
qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
tc[j]->branch = NIL; /* end of root */
thr = tc[0]; /* head of data */
fs_give ((void **) &tc);
}
fs_give ((void **) &lst);
}
return thr;
}
/* Mail thread references
* Accepts: mail stream
* character set
* search program
* option flags
* sorter routine
* Returns: thread node tree
*/
#define REFHASHSIZE 1009 /* arbitrary prime for hash table size */
/* Reference threading container, as described in Jamie Zawinski's web page
* (http://www.jwz.org/doc/threading.html) for this algorithm. These are
* stored as extended data in the hash table (called "id_table" in JWZ's
* document) and are maintained by the hash table routines. The hash table
* routines implement extended data as additional void* words at the end of
* each bucket, hence these strange macros instead of a struct which would
* have been more straightforward.
*/
#define THREADLINKS 3 /* number of thread links */
#define CACHE(data) ((SORTCACHE *) (data)[0])
#define PARENT(data) ((container_t) (data)[1])
#define SETPARENT(data,value) ((container_t) (data[1] = value))
#define SIBLING(data) ((container_t) (data)[2])
#define SETSIBLING(data,value) ((container_t) (data[2] = value))
#define CHILD(data) ((container_t) (data)[3])
#define SETCHILD(data,value) ((container_t) (data[3] = value))
THREADNODE *mail_thread_references (MAILSTREAM *stream,char *charset,
SEARCHPGM *spg,long flags,sorter_t sorter)
{
MESSAGECACHE *elt,telt;
ENVELOPE *env;
SORTCACHE *s;
STRINGLIST *st;
HASHENT *he;
THREADNODE **tc,*cur,*lst,*nxt,*sis,*msg;
container_t con,nxc,prc,sib;
void **sub;
char *t,tmp[MAILTMPLEN];
unsigned long j,nmsgs;
unsigned long i = stream->nmsgs * sizeof (SORTCACHE *);
SORTCACHE **sc = (SORTCACHE **) memset (fs_get ((size_t) i),0,(size_t) i);
HASHTAB *ht = hash_create (REFHASHSIZE);
THREADNODE *root = NIL;
if (spg) { /* only if a search needs to be done */
int silent = stream->silent;
stream->silent = T; /* don't pass up mm_searched() events */
/* search for messages */
mail_search_full (stream,charset,spg,NIL);
stream->silent = silent; /* restore silence state */
}
/* create SORTCACHE vector of requested msgs */
for (i = 1, nmsgs = 0; i <= stream->nmsgs; ++i)
if (mail_elt (stream,i)->searched)
(sc[nmsgs++] = (SORTCACHE *)(*mailcache)(stream,i,CH_SORTCACHE))->num =i;
/* separate pass so can do overview fetch lookahead */
for (i = 0; i < nmsgs; ++i) { /* for each requested message */
/* is anything missing in its SORTCACHE? */
if (!((s = sc[i])->date && s->subject && s->message_id && s->references)) {
/* driver has an overview mechanism? */
if (stream->dtb && stream->dtb->overview) {
/* yes, find following unloaded entries */
for (j = i + 1; (j < nmsgs) && !sc[j]->references; ++j);
sprintf (tmp,"%lu",mail_uid (stream,s->num));
if (i != --j) /* end of range different? */
sprintf (tmp + strlen (tmp),":%lu",mail_uid (stream,sc[j]->num));
/* load via overview mechanism */
mail_fetch_overview (stream,tmp,mail_thread_loadcache);
}
/* still missing data? */
if (!s->date || !s->subject || !s->message_id || !s->references) {
/* try to load data from envelope */
if (env = mail_fetch_structure (stream,s->num,NIL,NIL)) {
if (!s->date && env->date && mail_parse_date (&telt,env->date))
s->date = mail_longdate (&telt);
if (!s->subject && env->subject)
s->refwd =
mail_strip_subject (env->subject,&s->subject);
if (!s->message_id && env->message_id && *env->message_id)
s->message_id = mail_thread_parse_msgid (env->message_id,NIL);
if (!s->references && /* use References: or In-Reply-To: */
!(s->references =
mail_thread_parse_references (env->references,T)))
s->references = mail_thread_parse_references(env->in_reply_to,NIL);
}
/* last resort */
if (!s->date && !(s->date = s->arrival)) {
/* internal date unknown but can get? */
if (!(elt = mail_elt (stream,s->num))->day &&
!(stream->dtb->flags & DR_NOINTDATE)) {
sprintf (tmp,"%lu",s->num);
mail_fetch_fast (stream,tmp,NIL);
}
/* wrong thing before 3-Jan-1970 */
s->date = (s->arrival = elt->day ? mail_longdate (elt) : 1);
}
if (!s->subject) s->subject = cpystr ("");
if (!s->references) s->references = mail_newstringlist ();
s->dirty = T;
}
}
/* Step 1 (preliminary) */
/* generate unique string */
sprintf (tmp,"%s.%lx.%lx@%s",stream->mailbox,stream->uid_validity,
mail_uid (stream,s->num),mylocalhost ());
/* flush old unique string if not message-id */
if (s->unique && (s->unique != s->message_id))
fs_give ((void **) &s->unique);
s->unique = s->message_id ? /* don't permit Message ID duplicates */
(hash_lookup (ht,s->message_id) ? cpystr (tmp) : s->message_id) :
(s->message_id = cpystr (tmp));
/* add unique string to hash table */
hash_add (ht,s->unique,s,THREADLINKS);
}
/* Step 1 */
for (i = 0; i < nmsgs; ++i) { /* for each message in sortcache */
/* Step 1A */
if ((st = (s = sc[i])->references) && st->text.data)
for (con = hash_lookup_and_add (ht,(char *) st->text.data,NIL,
THREADLINKS); st = st->next; con = nxc) {
nxc = hash_lookup_and_add (ht,(char *) st->text.data,NIL,THREADLINKS);
/* only if no parent & won't introduce loop */
if (!PARENT (nxc) && !mail_thread_check_child (con,nxc)) {
SETPARENT (nxc,con); /* establish parent/child link */
/* other children become sibling of this one */
SETSIBLING (nxc,CHILD (con));
SETCHILD (con,nxc); /* set as child of parent */
}
}
else con = NIL; /* else message has no ancestors */
/* Step 1B */
if ((prc = PARENT ((nxc = hash_lookup (ht,s->unique)))) &&
(prc != con)) { /* break links if have a different parent */
SETPARENT (nxc,NIL); /* easy if direct child */
if (nxc == CHILD (prc)) SETCHILD (prc,SIBLING (nxc));
else { /* otherwise hunt through sisters */
for (sib = CHILD (prc); nxc != SIBLING (sib); sib = SIBLING (sib));
SETSIBLING (sib,SIBLING (nxc));
}
SETSIBLING (nxc,NIL); /* no more little sisters either */
prc = NIL; /* no more parent set */
}
/* need to set parent, and parent is good? */
if (!prc && !mail_thread_check_child (con,nxc)) {
SETPARENT (nxc,con); /* establish parent/child link */
if (con) { /* if non-root parent, set parent's child */
if (CHILD (con)) { /* have a child already */
/* find youngest daughter */
for (con = CHILD (con); SIBLING (con); con = SIBLING (con));
SETSIBLING (con,nxc); /* add new baby sister */
}
else SETCHILD (con,nxc);/* set as only child */
}
}
}
fs_give ((void **) &sc); /* finished with sortcache vector */
/* Step 2 */
/* search hash table for parentless messages */
for (i = 0, prc = con = NIL; i < ht->size; i++)
for (he = ht->table[i]; he; he = he->next)
if (!PARENT ((nxc = he->data))) {
/* sibling of previous parentless message */
if (con) con = SETSIBLING (con,nxc);
else prc = con = nxc; /* first parentless message */
}
/* Once the dummy containers are pruned, we no longer need the parent
* information, so we can convert the containers to THREADNODEs. Since
* we don't need the id_table any more either, we can reset the hash table
* and reuse it as a subject_table. Resetting the hash table will also
* destroy the containers.
*/
/* Step 3 */
/* prune dummies, convert to threadnode */
root = mail_thread_c2node (stream,mail_thread_prune_dummy (prc,NIL),flags);
/* Step 4 */
/* make buffer for sorting */
tc = (THREADNODE **) fs_get (nmsgs * sizeof (THREADNODE *));
/* load threadcache and count nodes to sort */
for (i = 0, cur = root; cur ; cur = cur->branch) tc[i++] = cur;
if (i > 1) { /* only if need to sort */
qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
/* relink siblings */
for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
tc[j]->branch = NIL; /* end of root */
root = tc[0]; /* establish new root */
}
/* Step 5A */
hash_reset (ht); /* discard containers, reset ht */
/* Step 5B */
for (cur = root; cur; cur = cur->branch)
if ((t = (nxt = (cur->sc ? cur : cur->next))->sc->subject) && *t) {
/* add new subject to hash table */
if (!(sub = hash_lookup (ht,t))) hash_add (ht,t,cur,0);
/* if one in table not dummy and */
else if ((s = (lst = (THREADNODE *) sub[0])->sc) &&
/* current dummy, or not re/fwd and table is */
(!cur->sc || (!nxt->sc->refwd && s->refwd)))
sub[0] = (void *) cur; /* replace with this message */
}
/* Step 5C */
for (cur = root, sis = NIL; cur; cur = msg) {
/* do nothing if current message or no sub */
if (!(t = (cur->sc ? cur : cur->next)->sc->subject) || !*t ||
((lst = (THREADNODE *) (sub = hash_lookup (ht,t))[0]) == cur))
msg = (sis = cur)->branch;
else if (!lst->sc) { /* is message in the table a dummy? */
/* find youngest daughter of msg in table */
for (msg = lst->next; msg->branch; msg = msg->branch);
if (!cur->sc) { /* current message a dummy? */
msg->branch = cur->next;/* current's daughter now dummy's youngest */
msg = cur->branch; /* continue scan at younger sister */
/* now delete this node */
cur->branch = cur->next = NIL;
mail_free_threadnode (&cur);
}
else { /* current message not a dummy */
msg->branch = cur; /* append as youngest daughter */
msg = cur->branch; /* continue scan at younger sister */
cur->branch = NIL; /* lose our younger sisters */
}
}
else { /* no dummies, is current re/fwd, table not? */
if (cur->sc->refwd && !lst->sc->refwd) {
if (lst->next) { /* find youngest daughter of msg in table */
for (msg = lst->next; msg->branch; msg = msg->branch);
msg->branch = cur; /* append as youngest daughter */
}
else lst->next = cur; /* no children, so make the eldest daughter */
}
else { /* no re/fwd, create a new dummy */
msg = mail_newthreadnode (NIL);
if (lst == root) { /* msg in table is root? */
root = lst->branch; /* younger sister becomes new root */
/* no longer older sister either */
if (lst == sis) sis = NIL;
}
else { /* find older sister of msg in table */
for (nxt = root; lst != nxt->branch; nxt = nxt->branch);
/* remove from older sister */
nxt->branch = lst->branch;
}
msg->next = lst; /* msg in table becomes child */
lst->branch = cur; /* current now little sister of msg in table */
if (sis) { /* have an elder sister? */
if (sis == lst) /* rescan if lost her */
for (sis = root; cur != sis->branch; sis = sis->branch);
sis->branch = msg; /* make dummy younger sister of big sister */
}
else root = msg; /* otherwise this is the new root */
sub[0] = sis = msg; /* set new msg in table and new big sister */
}
msg = cur->branch; /* continue scan at younger sister */
cur->branch = NIL; /* lose our younger sisters */
}
if (sis) sis->branch = msg; /* older sister gets this as younger sister */
else root = msg; /* otherwise this is the new root */
}
hash_destroy (&ht); /* finished with hash table */
/* Step 6 */
/* sort threads */
root = mail_thread_sort (root,tc);
fs_give ((void **) &tc); /* finished with sort buffer */
return root; /* return sorted list */
}
/* Fetch overview callback to load sortcache for threading
* Accepts: MAIL stream
* UID of this message
* overview of this message
* msgno of this message
*/
void mail_thread_loadcache (MAILSTREAM *stream,unsigned long uid,OVERVIEW *ov,
unsigned long msgno)
{
if (msgno && ov) { /* just in case */
MESSAGECACHE telt;
SORTCACHE *s = (SORTCACHE *) (*mailcache) (stream,msgno,CH_SORTCACHE);
if (!s->subject && ov->subject) {
s->refwd = mail_strip_subject (ov->subject,&s->subject);
s->dirty = T;
}
if (!s->from && ov->from && ov->from->mailbox) {
s->from = cpystr (ov->from->mailbox);
s->dirty = T;
}
if (!s->date && ov->date && mail_parse_date (&telt,ov->date)) {
s->date = mail_longdate (&telt);
s->dirty = T;
}
if (!s->message_id && ov->message_id) {
s->message_id = mail_thread_parse_msgid (ov->message_id,NIL);
s->dirty = T;
}
if (!s->references &&
!(s->references = mail_thread_parse_references (ov->references,T))) {
/* don't do In-Reply-To with NNTP mailboxes */
s->references = mail_newstringlist ();
s->dirty = T;
}
if (!s->size && ov->optional.octets) {
s->size = ov->optional.octets;
s->dirty = T;
}
}
}
/* Thread parse Message ID
* Accepts: pointer to purported Message ID
* pointer to return pointer
* Returns: Message ID or NIL, return pointer updated
*/
char *mail_thread_parse_msgid (char *s,char **ss)
{
char *ret = NIL;
char *t = NIL;
ADDRESS *adr;
if (s) { /* only for non-NIL strings */
rfc822_skipws (&s); /* skip whitespace */
/* ignore phrases */
if (((*s == '<') || (s = rfc822_parse_phrase (s))) &&
(adr = rfc822_parse_routeaddr (s,&t,BADHOST))) {
/* make return msgid */
if (adr->mailbox && adr->host)
sprintf (ret = (char *) fs_get (strlen (adr->mailbox) +
strlen (adr->host) + 2),"%s@%s",
adr->mailbox,adr->host);
mail_free_address (&adr); /* don't need temporary address */
}
}
if (ss) *ss = t; /* update return pointer */
return ret;
}
/* Thread parse references
* Accepts: pointer to purported references
* parse multiple references flag
* Returns: references or NIL
*/
STRINGLIST *mail_thread_parse_references (char *s,long flag)
{
char *t;
STRINGLIST *ret = NIL;
STRINGLIST *cur;
/* found first reference? */
if (t = mail_thread_parse_msgid (s,&s)) {
(ret = mail_newstringlist ())->text.data = (unsigned char *) t;
ret->text.size = strlen (t);
if (flag) /* parse subsequent references */
for (cur = ret; t = mail_thread_parse_msgid (s,&s); cur = cur->next) {
(cur->next = mail_newstringlist ())->text.data = (unsigned char *) t;
cur->next->text.size = strlen (t);
}
}
return ret;
}
/* Prune dummy messages
* Accepts: candidate container to prune
* older sibling of container, if any
* Returns: container in this position, possibly pruned
* All children and younger siblings are also pruned
*/
container_t mail_thread_prune_dummy (container_t msg,container_t ane)
{
/* prune container and children */
container_t ret = msg ? mail_thread_prune_dummy_work (msg,ane) : NIL;
/* prune all younger sisters */
if (ret) for (ane = ret; ane && (msg = SIBLING (ane)); ane = msg)
msg = mail_thread_prune_dummy_work (msg,ane);
return ret;
}
/* Prune dummy messages worker routine
* Accepts: candidate container to prune
* older sibling of container, if any
* Returns: container in this position, possibly pruned
* All children are also pruned
*/
container_t mail_thread_prune_dummy_work (container_t msg,container_t ane)
{
container_t cur;
/* get children, if any */
container_t nxt = mail_thread_prune_dummy (CHILD (msg),NIL);
/* just update children if container has msg */
if (CACHE (msg)) SETCHILD (msg,nxt);
else if (!nxt) { /* delete dummy with no children */
nxt = SIBLING (msg); /* get younger sister */
if (ane) SETSIBLING (ane,nxt);
/* prune younger sister if exists */
msg = nxt ? mail_thread_prune_dummy_work (nxt,ane) : NIL;
}
/* not if parent root & multiple children */
else if ((cur = PARENT (msg)) || !SIBLING (nxt)) {
/* OK to promote, try younger sister of aunt */
if (ane) SETSIBLING (ane,nxt);
/* otherwise promote to child of grandmother */
else if (cur) SETCHILD (cur,nxt);
SETPARENT (nxt,cur); /* set parent as well */
/* look for end of siblings in new container */
for (cur = nxt; SIBLING (cur); cur = SIBLING (cur));
/* reattach deleted container's siblings */
SETSIBLING (cur,SIBLING (msg));
/* prune and return new container */
msg = mail_thread_prune_dummy_work (nxt,ane);
}
else SETCHILD (msg,nxt); /* in case child pruned */
return msg; /* return this message */
}
/* Test that purported mother is not a child of purported daughter
* Accepts: mother
* purported daugher
* Returns: T if circular parentage exists, else NIL
*/
long mail_thread_check_child (container_t mother,container_t daughter)
{
if (mother) { /* only if mother non-NIL */
if (mother == daughter) return T;
for (daughter = CHILD (daughter); daughter; daughter = SIBLING (daughter))
if (mail_thread_check_child (mother,daughter)) return T;
}
return NIL;
}
/* Generate threadnodes from containers
* Accepts: Mail stream
* container
* flags
* Return: threadnode list
*/
THREADNODE *mail_thread_c2node (MAILSTREAM *stream,container_t con,long flags)
{
THREADNODE *ret,*cur;
SORTCACHE *s;
container_t nxt;
/* for each container */
for (ret = cur = NIL; con; con = SIBLING (con)) {
s = CACHE (con); /* yes, get its sortcache */
/* create node for it */
if (ret) cur = cur->branch = mail_newthreadnode (s);
else ret = cur = mail_newthreadnode (s);
/* attach sequence or UID for non-dummy */
if (s) cur->num = (flags & SE_UID) ? mail_uid (stream,s->num) : s->num;
/* attach the children */
if (nxt = CHILD (con)) cur->next = mail_thread_c2node (stream,nxt,flags);
}
return ret;
}
/* Sort thread tree by date
* Accepts: thread tree to sort
* qsort vector to sort
* Returns: sorted thread tree
*/
THREADNODE *mail_thread_sort (THREADNODE *thr,THREADNODE **tc)
{
unsigned long i,j;
THREADNODE *cur;
/* sort children of each thread */
for (cur = thr; cur; cur = cur->branch)
if (cur->next) cur->next = mail_thread_sort (cur->next,tc);
/* Must do this in a separate pass since recursive call will clobber tc */
/* load threadcache and count nodes to sort */
for (i = 0, cur = thr; cur; cur = cur->branch) tc[i++] = cur;
if (i > 1) { /* only if need to sort */
qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
/* relink root siblings */
for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
tc[j]->branch = NIL; /* end of root */
}
return i ? tc[0] : NIL; /* return new head of list */
}
/* Thread compare date
* Accept: first message sort cache element
* second message sort cache element
* Returns: -1 if a1 < a2, 1 if a1 > a2
*
* This assumes that a sort cache element is either a message (with a
* sortcache entry) or a dummy with a message (with sortcache entry) child.
* This is true of both the ORDEREDSUBJECT (no dummies) and REFERENCES
* (dummies only at top-level, and with non-dummy children).
*
* If a new algorithm allows a dummy parent to have a dummy child, this
* routine must be changed if it is to be used by that algorithm.
*
* Messages with bogus dates are always sorted at the top.
*/
int mail_thread_compare_date (const void *a1,const void *a2)
{
THREADNODE *t1 = *(THREADNODE **) a1;
THREADNODE *t2 = *(THREADNODE **) a2;
SORTCACHE *s1 = t1->sc ? t1->sc : t1->next->sc;
SORTCACHE *s2 = t2->sc ? t2->sc : t2->next->sc;
int ret = compare_ulong (s1->date,s2->date);
/* use number as final tie-breaker */
return ret ? ret : compare_ulong (s1->num,s2->num);
}
/* Mail parse sequence
* Accepts: mail stream
* sequence to parse
* Returns: T if parse successful, else NIL
*/
long mail_sequence (MAILSTREAM *stream,unsigned char *sequence)
{
unsigned long i,j,x;
for (i = 1; i <= stream->nmsgs; i++) mail_elt (stream,i)->sequence = NIL;
while (sequence && *sequence){/* while there is something to parse */
if (*sequence == '*') { /* maximum message */
if (stream->nmsgs) i = stream->nmsgs;
else {
MM_LOG ("No messages, so no maximum message number",ERROR);
return NIL;
}
sequence++; /* skip past * */
}
/* parse and validate message number */
else if (!isdigit (*sequence)) {
MM_LOG ("Syntax error in sequence",ERROR);
return NIL;
}
else if (!(i = strtoul (sequence,(char **) &sequence,10)) ||
(i > stream->nmsgs)) {
MM_LOG ("Sequence out of range",ERROR);
return NIL;
}
switch (*sequence) { /* see what the delimiter is */
case ':': /* sequence range */
if (*++sequence == '*') { /* maximum message */
if (stream->nmsgs) j = stream->nmsgs;
else {
MM_LOG ("No messages, so no maximum message number",ERROR);
return NIL;
}
sequence++; /* skip past * */
}
/* parse end of range */
else if (!(j = strtoul (sequence,(char **) &sequence,10)) ||
(j > stream->nmsgs)) {
MM_LOG ("Sequence range invalid",ERROR);
return NIL;
}
if (*sequence && *sequence++ != ',') {
MM_LOG ("Sequence range syntax error",ERROR);
return NIL;
}
if (i > j) { /* swap the range if backwards */
x = i; i = j; j = x;
}
/* mark each item in the sequence */
while (i <= j) mail_elt (stream,j--)->sequence = T;
break;
case ',': /* single message */
++sequence; /* skip the delimiter, fall into end case */
case '\0': /* end of sequence, mark this message */
mail_elt (stream,i)->sequence = T;
break;
default: /* anything else is a syntax error! */
MM_LOG ("Sequence syntax error",ERROR);
return NIL;
}
}
return T; /* successfully parsed sequence */
}
/* Parse flag list
* Accepts: MAIL stream
* flag list as a character string
* pointer to user flags to return
* Returns: system flags
*/
long mail_parse_flags (MAILSTREAM *stream,char *flag,unsigned long *uf)
{
char *t,*n,*s,tmp[MAILTMPLEN],msg[MAILTMPLEN];
short f = 0;
long i,j;
*uf = 0; /* initially no user flags */
if (flag && *flag) { /* no-op if no flag string */
/* check if a list and make sure valid */
if (((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) ||
(strlen (flag) >= MAILTMPLEN)) {
MM_LOG ("Bad flag list",ERROR);
return NIL;
}
/* copy the flag string w/o list construct */
strncpy (n = tmp,flag+i,(j = strlen (flag) - (2*i)));
tmp[j] = '\0';
while ((t = n) && *t) { /* parse the flags */
/* find end of flag */
if (n = strchr (t,' ')) *n++ = '\0';
if (*t == '\\') { /* system flag? */
if (!compare_cstring (t+1,"SEEN")) f |= fSEEN;
else if (!compare_cstring (t+1,"DELETED")) f |= fDELETED;
else if (!compare_cstring (t+1,"FLAGGED")) f |= fFLAGGED;
else if (!compare_cstring (t+1,"ANSWERED")) f |= fANSWERED;
else if (!compare_cstring (t+1,"DRAFT")) f |= fDRAFT;
else {
sprintf (msg,"Unsupported system flag: %.80s",t);
MM_LOG (msg,WARN);
}
}
else { /* keyword flag */
for (i = j = 0; /* user flag, search through table */
!i && (j < NUSERFLAGS) && (s = stream->user_flags[j]); ++j)
if (!compare_cstring (t,s)) *uf |= i = 1 << j;
if (!i) { /* flag not found, can it be created? */
if (stream->kwd_create && (j < NUSERFLAGS) && *t &&
(strlen (t) <= MAXUSERFLAG)) {
for (s = t; t && *s; s++) switch (*s) {
default: /* all other characters */
/* SPACE, CTL, or not CHAR */
if ((*s > ' ') && (*s < 0x7f)) break;
case '*': case '%': /* list_wildcards */
case '"': case '\\':/* quoted-specials */
/* atom_specials */
case '(': case ')': case '{':
case ']': /* resp-specials */
sprintf (msg,"Invalid flag: %.80s",t);
MM_LOG (msg,WARN);
t = NIL;
}
if (t) { /* only if valid */
*uf |= 1 << j; /* set the bit */
stream->user_flags[j] = cpystr (t);
/* if out of user flags */
if (j == NUSERFLAGS - 1) stream->kwd_create = NIL;
}
}
else {
if (*t) sprintf (msg,"Unknown flag: %.80s",t);
else strcpy (msg,"Empty flag invalid");
MM_LOG (msg,WARN);
}
}
}
}
}
return f;
}
/* Mail check network stream for usability with new name
* Accepts: MAIL stream
* candidate new name
* Returns: T if stream can be used, NIL otherwise
*/
long mail_usable_network_stream (MAILSTREAM *stream,char *name)
{
NETMBX smb,nmb,omb;
return (stream && stream->dtb && !(stream->dtb->flags & DR_LOCAL) &&
mail_valid_net_parse (name,&nmb) &&
mail_valid_net_parse (stream->mailbox,&smb) &&
mail_valid_net_parse (stream->original_mailbox,&omb) &&
((!compare_cstring (smb.host,
trustdns ? tcp_canonical (nmb.host) : nmb.host)&&
!strcmp (smb.service,nmb.service) &&
(!nmb.port || (smb.port == nmb.port)) &&
(nmb.anoflag == stream->anonymous) &&
(!nmb.user[0] || !strcmp (smb.user,nmb.user))) ||
(!compare_cstring (omb.host,nmb.host) &&
!strcmp (omb.service,nmb.service) &&
(!nmb.port || (omb.port == nmb.port)) &&
(nmb.anoflag == stream->anonymous) &&
(!nmb.user[0] || !strcmp (omb.user,nmb.user))))) ? LONGT : NIL;
}
/* Mail data structure instantiation routines */
/* Mail instantiate cache elt
* Accepts: initial message number
* Returns: new cache elt
*/
MESSAGECACHE *mail_new_cache_elt (unsigned long msgno)
{
MESSAGECACHE *elt = (MESSAGECACHE *) memset (fs_get (sizeof (MESSAGECACHE)),
0,sizeof (MESSAGECACHE));
elt->lockcount = 1; /* initially only cache references it */
elt->msgno = msgno; /* message number */
return elt;
}
/* Mail instantiate envelope
* Returns: new envelope
*/
ENVELOPE *mail_newenvelope (void)
{
return (ENVELOPE *) memset (fs_get (sizeof (ENVELOPE)),0,sizeof (ENVELOPE));
}
/* Mail instantiate address
* Returns: new address
*/
ADDRESS *mail_newaddr (void)
{
return (ADDRESS *) memset (fs_get (sizeof (ADDRESS)),0,sizeof (ADDRESS));
}
/* Mail instantiate body
* Returns: new body
*/
BODY *mail_newbody (void)
{
return mail_initbody ((BODY *) fs_get (sizeof (BODY)));
}
/* Mail initialize body
* Accepts: body
* Returns: body
*/
BODY *mail_initbody (BODY *body)
{
memset ((void *) body,0,sizeof (BODY));
body->type = TYPETEXT; /* content type */
body->encoding = ENC7BIT; /* content encoding */
return body;
}
/* Mail instantiate body parameter
* Returns: new body part
*/
PARAMETER *mail_newbody_parameter (void)
{
return (PARAMETER *) memset (fs_get (sizeof(PARAMETER)),0,sizeof(PARAMETER));
}
/* Mail instantiate body part
* Returns: new body part
*/
PART *mail_newbody_part (void)
{
PART *part = (PART *) memset (fs_get (sizeof (PART)),0,sizeof (PART));
mail_initbody (&part->body); /* initialize the body */
return part;
}
/* Mail instantiate body message part
* Returns: new body message part
*/
MESSAGE *mail_newmsg (void)
{
return (MESSAGE *) memset (fs_get (sizeof (MESSAGE)),0,sizeof (MESSAGE));
}
/* Mail instantiate string list
* Returns: new string list
*/
STRINGLIST *mail_newstringlist (void)
{
return (STRINGLIST *) memset (fs_get (sizeof (STRINGLIST)),0,
sizeof (STRINGLIST));
}
/* Mail instantiate new search program
* Returns: new search program
*/
SEARCHPGM *mail_newsearchpgm (void)
{
return (SEARCHPGM *) memset (fs_get (sizeof(SEARCHPGM)),0,sizeof(SEARCHPGM));
}
/* Mail instantiate new search program
* Accepts: header line name
* Returns: new search program
*/
SEARCHHEADER *mail_newsearchheader (char *line,char *text)
{
SEARCHHEADER *hdr = (SEARCHHEADER *) memset (fs_get (sizeof (SEARCHHEADER)),
0,sizeof (SEARCHHEADER));
hdr->line.size = strlen ((char *) (hdr->line.data =
(unsigned char *) cpystr (line)));
hdr->text.size = strlen ((char *) (hdr->text.data =
(unsigned char *) cpystr (text)));
return hdr;
}
/* Mail instantiate new search set
* Returns: new search set
*/
SEARCHSET *mail_newsearchset (void)
{
return (SEARCHSET *) memset (fs_get (sizeof(SEARCHSET)),0,sizeof(SEARCHSET));
}
/* Mail instantiate new search or
* Returns: new search or
*/
SEARCHOR *mail_newsearchor (void)
{
SEARCHOR *or = (SEARCHOR *) memset (fs_get (sizeof (SEARCHOR)),0,
sizeof (SEARCHOR));
or->first = mail_newsearchpgm ();
or->second = mail_newsearchpgm ();
return or;
}
/* Mail instantiate new searchpgmlist
* Returns: new searchpgmlist
*/
SEARCHPGMLIST *mail_newsearchpgmlist (void)
{
SEARCHPGMLIST *pgl = (SEARCHPGMLIST *)
memset (fs_get (sizeof (SEARCHPGMLIST)),0,sizeof (SEARCHPGMLIST));
pgl->pgm = mail_newsearchpgm ();
return pgl;
}
/* Mail instantiate new sortpgm
* Returns: new sortpgm
*/
SORTPGM *mail_newsortpgm (void)
{
return (SORTPGM *) memset (fs_get (sizeof (SORTPGM)),0,sizeof (SORTPGM));
}
/* Mail instantiate new threadnode
* Accepts: sort cache for thread node
* Returns: new threadnode
*/
THREADNODE *mail_newthreadnode (SORTCACHE *sc)
{
THREADNODE *thr = (THREADNODE *) memset (fs_get (sizeof (THREADNODE)),0,
sizeof (THREADNODE));
if (sc) thr->sc = sc; /* initialize sortcache */
return thr;
}
/* Mail instantiate new acllist
* Returns: new acllist
*/
ACLLIST *mail_newacllist (void)
{
return (ACLLIST *) memset (fs_get (sizeof (ACLLIST)),0,sizeof (ACLLIST));
}
ANNOTATION *mail_newannotation (void)
{
return (ANNOTATION *) memset (fs_get (sizeof (ANNOTATION)),0,sizeof(ANNOTATION));
}
ANNOTATION_VALUES *mail_newannotationvalue (void)
{
return (ANNOTATION_VALUES *) memset (fs_get (sizeof (ANNOTATION_VALUES)),0,sizeof(ANNOTATION_VALUES));
}
/* Mail instantiate new quotalist
* Returns: new quotalist
*/
QUOTALIST *mail_newquotalist (void)
{
return (QUOTALIST *) memset (fs_get (sizeof (QUOTALIST)),0,
sizeof (QUOTALIST));
}
/* Mail garbage collection routines */
/* Mail garbage collect body
* Accepts: pointer to body pointer
*/
void mail_free_body (BODY **body)
{
if (*body) { /* only free if exists */
mail_free_body_data (*body);/* free its data */
fs_give ((void **) body); /* return body to free storage */
}
}
/* Mail garbage collect body data
* Accepts: body pointer
*/
void mail_free_body_data (BODY *body)
{
switch (body->type) { /* free contents */
case TYPEMULTIPART: /* multiple part */
mail_free_body_part (&body->nested.part);
break;
case TYPEMESSAGE: /* encapsulated message */
if (body->subtype && !strcmp (body->subtype,"RFC822")) {
mail_free_stringlist (&body->nested.msg->lines);
mail_gc_msg (body->nested.msg,GC_ENV | GC_TEXTS);
}
if (body->nested.msg) fs_give ((void **) &body->nested.msg);
break;
default:
break;
}
if (body->subtype) fs_give ((void **) &body->subtype);
mail_free_body_parameter (&body->parameter);
if (body->id) fs_give ((void **) &body->id);
if (body->description) fs_give ((void **) &body->description);
if (body->disposition.type) fs_give ((void **) &body->disposition.type);
if (body->disposition.parameter)
mail_free_body_parameter (&body->disposition.parameter);
if (body->language) mail_free_stringlist (&body->language);
if (body->location) fs_give ((void **) &body->location);
if (body->mime.text.data) fs_give ((void **) &body->mime.text.data);
if (body->contents.text.data) fs_give ((void **) &body->contents.text.data);
if (body->md5) fs_give ((void **) &body->md5);
if (mailfreebodysparep && body->sparep)
(*mailfreebodysparep) (&body->sparep);
}
/* Mail garbage collect body parameter
* Accepts: pointer to body parameter pointer
*/
void mail_free_body_parameter (PARAMETER **parameter)
{
if (*parameter) { /* only free if exists */
if ((*parameter)->attribute) fs_give ((void **) &(*parameter)->attribute);
if ((*parameter)->value) fs_give ((void **) &(*parameter)->value);
/* run down the list as necessary */
mail_free_body_parameter (&(*parameter)->next);
/* return body part to free storage */
fs_give ((void **) parameter);
}
}
/* Mail garbage collect body part
* Accepts: pointer to body part pointer
*/
void mail_free_body_part (PART **part)
{
if (*part) { /* only free if exists */
mail_free_body_data (&(*part)->body);
/* run down the list as necessary */
mail_free_body_part (&(*part)->next);
fs_give ((void **) part); /* return body part to free storage */
}
}
/* Mail garbage collect message cache
* Accepts: mail stream
*
* The message cache is set to NIL when this function finishes.
*/
void mail_free_cache (MAILSTREAM *stream)
{
/* do driver specific stuff first */
mail_gc (stream,GC_ELT | GC_ENV | GC_TEXTS);
/* flush the cache */
(*mailcache) (stream,(long) 0,CH_INIT);
}
/* Mail garbage collect cache element
* Accepts: pointer to cache element pointer
*/
void mail_free_elt (MESSAGECACHE **elt)
{
/* only free if exists and no sharers */
if (*elt && !--(*elt)->lockcount) {
mail_gc_msg (&(*elt)->private.msg,GC_ENV | GC_TEXTS);
if (mailfreeeltsparep && (*elt)->sparep)
(*mailfreeeltsparep) (&(*elt)->sparep);
fs_give ((void **) elt);
}
else *elt = NIL; /* else simply drop pointer */
}
/* Mail garbage collect envelope
* Accepts: pointer to envelope pointer
*/
void mail_free_envelope (ENVELOPE **env)
{
if (*env) { /* only free if exists */
if ((*env)->remail) fs_give ((void **) &(*env)->remail);
mail_free_address (&(*env)->return_path);
if ((*env)->date) fs_give ((void **) &(*env)->date);
mail_free_address (&(*env)->from);
mail_free_address (&(*env)->sender);
mail_free_address (&(*env)->reply_to);
if ((*env)->subject) fs_give ((void **) &(*env)->subject);
mail_free_address (&(*env)->to);
mail_free_address (&(*env)->cc);
mail_free_address (&(*env)->bcc);
if ((*env)->in_reply_to) fs_give ((void **) &(*env)->in_reply_to);
if ((*env)->message_id) fs_give ((void **) &(*env)->message_id);
if ((*env)->newsgroups) fs_give ((void **) &(*env)->newsgroups);
if ((*env)->followup_to) fs_give ((void **) &(*env)->followup_to);
if ((*env)->references) fs_give ((void **) &(*env)->references);
if (mailfreeenvelopesparep && (*env)->sparep)
(*mailfreeenvelopesparep) (&(*env)->sparep);
fs_give ((void **) env); /* return envelope to free storage */
}
}
/* Mail garbage collect address
* Accepts: pointer to address pointer
*/
void mail_free_address (ADDRESS **address)
{
if (*address) { /* only free if exists */
if ((*address)->personal) fs_give ((void **) &(*address)->personal);
if ((*address)->adl) fs_give ((void **) &(*address)->adl);
if ((*address)->mailbox) fs_give ((void **) &(*address)->mailbox);
if ((*address)->host) fs_give ((void **) &(*address)->host);
if ((*address)->error) fs_give ((void **) &(*address)->error);
if ((*address)->orcpt.type) fs_give ((void **) &(*address)->orcpt.type);
if ((*address)->orcpt.addr) fs_give ((void **) &(*address)->orcpt.addr);
mail_free_address (&(*address)->next);
fs_give ((void **) address);/* return address to free storage */
}
}
/* Mail garbage collect stringlist
* Accepts: pointer to stringlist pointer
*/
void mail_free_stringlist (STRINGLIST **string)
{
if (*string) { /* only free if exists */
if ((*string)->text.data) fs_give ((void **) &(*string)->text.data);
mail_free_stringlist (&(*string)->next);
fs_give ((void **) string); /* return string to free storage */
}
}
/* Mail garbage collect searchpgm
* Accepts: pointer to searchpgm pointer
*/
void mail_free_searchpgm (SEARCHPGM **pgm)
{
if (*pgm) { /* only free if exists */
mail_free_searchset (&(*pgm)->msgno);
mail_free_searchset (&(*pgm)->uid);
mail_free_searchor (&(*pgm)->or);
mail_free_searchpgmlist (&(*pgm)->not);
mail_free_searchheader (&(*pgm)->header);
mail_free_stringlist (&(*pgm)->bcc);
mail_free_stringlist (&(*pgm)->body);
mail_free_stringlist (&(*pgm)->cc);
mail_free_stringlist (&(*pgm)->from);
mail_free_stringlist (&(*pgm)->keyword);
mail_free_stringlist (&(*pgm)->subject);
mail_free_stringlist (&(*pgm)->text);
mail_free_stringlist (&(*pgm)->to);
fs_give ((void **) pgm); /* return program to free storage */
}
}
/* Mail garbage collect searchheader
* Accepts: pointer to searchheader pointer
*/
void mail_free_searchheader (SEARCHHEADER **hdr)
{
if (*hdr) { /* only free if exists */
if ((*hdr)->line.data) fs_give ((void **) &(*hdr)->line.data);
if ((*hdr)->text.data) fs_give ((void **) &(*hdr)->text.data);
mail_free_searchheader (&(*hdr)->next);
fs_give ((void **) hdr); /* return header to free storage */
}
}
/* Mail garbage collect searchset
* Accepts: pointer to searchset pointer
*/
void mail_free_searchset (SEARCHSET **set)
{
if (*set) { /* only free if exists */
mail_free_searchset (&(*set)->next);
fs_give ((void **) set); /* return set to free storage */
}
}
/* Mail garbage collect searchor
* Accepts: pointer to searchor pointer
*/
void mail_free_searchor (SEARCHOR **orl)
{
if (*orl) { /* only free if exists */
mail_free_searchpgm (&(*orl)->first);
mail_free_searchpgm (&(*orl)->second);
mail_free_searchor (&(*orl)->next);
fs_give ((void **) orl); /* return searchor to free storage */
}
}
/* Mail garbage collect search program list
* Accepts: pointer to searchpgmlist pointer
*/
void mail_free_searchpgmlist (SEARCHPGMLIST **pgl)
{
if (*pgl) { /* only free if exists */
mail_free_searchpgm (&(*pgl)->pgm);
mail_free_searchpgmlist (&(*pgl)->next);
fs_give ((void **) pgl); /* return searchpgmlist to free storage */
}
}
/* Mail garbage collect namespace
* Accepts: poiner to namespace
*/
void mail_free_namespace (NAMESPACE **n)
{
if (*n) {
fs_give ((void **) &(*n)->name);
mail_free_namespace (&(*n)->next);
mail_free_body_parameter (&(*n)->param);
fs_give ((void **) n); /* return namespace to free storage */
}
}
/* Mail garbage collect sort program
* Accepts: pointer to sortpgm pointer
*/
void mail_free_sortpgm (SORTPGM **pgm)
{
if (*pgm) { /* only free if exists */
mail_free_sortpgm (&(*pgm)->next);
fs_give ((void **) pgm); /* return sortpgm to free storage */
}
}
/* Mail garbage collect thread node
* Accepts: pointer to threadnode pointer
*/
void mail_free_threadnode (THREADNODE **thr)
{
if (*thr) { /* only free if exists */
mail_free_threadnode (&(*thr)->branch);
mail_free_threadnode (&(*thr)->next);
fs_give ((void **) thr); /* return threadnode to free storage */
}
}
/* Mail garbage collect acllist
* Accepts: pointer to acllist pointer
*/
void mail_free_acllist (ACLLIST **al)
{
if (*al) { /* only free if exists */
if ((*al)->identifier) fs_give ((void **) &(*al)->identifier);
if ((*al)->rights) fs_give ((void **) &(*al)->rights);
mail_free_acllist (&(*al)->next);
fs_give ((void **) al); /* return acllist to free storage */
}
}
static void mail_free_annotation_values(ANNOTATION_VALUES **val)
{
if (*val) {
if ((*val)->attr) fs_give ((void**) &(*val)->attr);
if ((*val)->value) fs_give ((void**) &(*val)->value);
mail_free_annotation_values (&(*val)->next);
fs_give ((void **) val);
}
}
void mail_free_annotation(ANNOTATION **al)
{
if (*al) {
if((*al)->mbox) fs_give ((void**) &(*al)->mbox);
if((*al)->entry) fs_give ((void**) &(*al)->entry);
if((*al)->values)
mail_free_annotation_values(&(*al)->values);
fs_give ((void **) al);
}
}
/* Mail garbage collect quotalist
* Accepts: pointer to quotalist pointer
*/
void mail_free_quotalist (QUOTALIST **ql)
{
if (*ql) { /* only free if exists */
if ((*ql)->name) fs_give ((void **) &(*ql)->name);
mail_free_quotalist (&(*ql)->next);
fs_give ((void **) ql); /* return quotalist to free storage */
}
}
/* Link authenicator
* Accepts: authenticator to add to list
*/
void auth_link (AUTHENTICATOR *auth)
{
if (!auth->valid || (*auth->valid) ()) {
AUTHENTICATOR **a = &mailauthenticators;
while (*a) a = &(*a)->next; /* find end of list of authenticators */
*a = auth; /* put authenticator at the end */
auth->next = NIL; /* this authenticator is the end of the list */
}
}
/* Authenticate access
* Accepts: mechanism name
* responder function
* argument count
* argument vector
* Returns: authenticated user name or NIL
*/
char *mail_auth (char *mechanism,authresponse_t resp,int argc,char *argv[])
{
AUTHENTICATOR *auth;
for (auth = mailauthenticators; auth; auth = auth->next)
if (auth->server && !compare_cstring (auth->name,mechanism))
return (!(auth->flags & AU_DISABLE) &&
((auth->flags & AU_SECURE) ||
!mail_parameters (NIL,GET_DISABLEPLAINTEXT,NIL))) ?
(*auth->server) (resp,argc,argv) : NIL;
return NIL; /* no authenticator found */
}
/* Lookup authenticator index
* Accepts: authenticator index
* Returns: authenticator, or 0 if not found
*/
AUTHENTICATOR *mail_lookup_auth (unsigned long i)
{
AUTHENTICATOR *auth = mailauthenticators;
while (auth && --i) auth = auth->next;
return auth;
}
/* Lookup authenticator name
* Accepts: authenticator name
* required authenticator flags
* Returns: index in authenticator chain, or 0 if not found
*/
unsigned int mail_lookup_auth_name (char *mechanism,long flags)
{
int i;
AUTHENTICATOR *auth;
for (i = 1, auth = mailauthenticators; auth; i++, auth = auth->next)
if (auth->client && !(flags & ~auth->flags) &&
!(auth->flags & AU_DISABLE) && !compare_cstring (auth->name,mechanism))
return i;
return 0;
}
/* Standard TCP/IP network driver */
static NETDRIVER tcpdriver = {
tcp_open, /* open connection */
tcp_aopen, /* open preauthenticated connection */
tcp_getline, /* get a line */
tcp_getbuffer, /* get a buffer */
tcp_soutr, /* output pushed data */
tcp_sout, /* output string */
tcp_close, /* close connection */
tcp_host, /* return host name */
tcp_remotehost, /* return remote host name */
tcp_port, /* return port number */
tcp_localhost /* return local host name */
};
/* Network open
* Accepts: NETMBX specifier to open
* default network driver
* default port
* SSL driver
* SSL service name
* SSL driver port
* Returns: Network stream if success, else NIL
*/
NETSTREAM *net_open (NETMBX *mb,NETDRIVER *dv,unsigned long port,
NETDRIVER *ssld,char *ssls,unsigned long sslp)
{
NETSTREAM *stream = NIL;
char tmp[MAILTMPLEN];
unsigned long flags = mb->novalidate ? NET_NOVALIDATECERT : 0;
if (strlen (mb->host) >= NETMAXHOST) {
sprintf (tmp,"Invalid host name: %.80s",mb->host);
MM_LOG (tmp,ERROR);
}
/* use designated driver if given */
else if (dv) stream = net_open_work (dv,mb->host,mb->service,port,mb->port,
flags);
else if (mb->sslflag && ssld) /* use ssl if sslflag lit */
stream = net_open_work (ssld,mb->host,ssls,sslp,mb->port,flags);
/* if trysslfirst and can open ssl... */
else if ((mb->trysslflag || trysslfirst) && ssld &&
(stream = net_open_work (ssld,mb->host,ssls,sslp,mb->port,
flags | NET_SILENT | NET_TRYSSL))) {
if (net_sout (stream,"",0)) mb->sslflag = T;
else {
net_close (stream); /* flush fake SSL stream */
stream = NIL;
}
}
/* default to TCP driver */
else stream = net_open_work (&tcpdriver,mb->host,mb->service,port,mb->port,
flags);
return stream;
}
/* Network open worker routine
* Accepts: network driver
* host name
* service name to look up port
* port number if service name not found
* port number to override service name
* flags (passed on top of port)
* Returns: Network stream if success, else NIL
*/
NETSTREAM *net_open_work (NETDRIVER *dv,char *host,char *service,
unsigned long port,unsigned long portoverride,
unsigned long flags)
{
NETSTREAM *stream = NIL;
void *tstream;
if (service && (*service == '*')) {
flags |= NET_NOOPENTIMEOUT; /* mark that no timeout is desired */
++service; /* no longer need the no timeout indicator */
}
if (portoverride) { /* explicit port number? */
service = NIL; /* yes, override service name */
port = portoverride; /* use that instead of default port */
}
if (tstream = (*dv->open) (host,service,port | flags)) {
stream = (NETSTREAM *) fs_get (sizeof (NETSTREAM));
stream->stream = tstream;
stream->dtb = dv;
}
return stream;
}
/* Network authenticated open
* Accepts: network driver
* NETMBX specifier
* service specifier
* return user name buffer
* Returns: Network stream if success else NIL
*/
NETSTREAM *net_aopen (NETDRIVER *dv,NETMBX *mb,char *service,char *user)
{
NETSTREAM *stream = NIL;
void *tstream;
if (!dv) dv = &tcpdriver; /* default to TCP driver */
if (tstream = (*dv->aopen) (mb,service,user)) {
stream = (NETSTREAM *) fs_get (sizeof (NETSTREAM));
stream->stream = tstream;
stream->dtb = dv;
}
return stream;
}
/* Network receive line
* Accepts: Network stream
* Returns: text line string or NIL if failure
*/
char *net_getline (NETSTREAM *stream)
{
return (*stream->dtb->getline) (stream->stream);
}
/* Network receive buffer
* Accepts: Network stream (must be void * for use as readfn_t)
* size in bytes
* buffer to read into
* Returns: T if success, NIL otherwise
*/
long net_getbuffer (void *st,unsigned long size,char *buffer)
{
NETSTREAM *stream = (NETSTREAM *) st;
return (*stream->dtb->getbuffer) (stream->stream,size,buffer);
}
/* Network send null-terminated string
* Accepts: Network stream
* string pointer
* Returns: T if success else NIL
*/
long net_soutr (NETSTREAM *stream,char *string)
{
return (*stream->dtb->soutr) (stream->stream,string);
}
/* Network send string
* Accepts: Network stream
* string pointer
* byte count
* Returns: T if success else NIL
*/
long net_sout (NETSTREAM *stream,char *string,unsigned long size)
{
return (*stream->dtb->sout) (stream->stream,string,size);
}
/* Network close
* Accepts: Network stream
*/
void net_close (NETSTREAM *stream)
{
if (stream->stream) (*stream->dtb->close) (stream->stream);
fs_give ((void **) &stream);
}
/* Network get host name
* Accepts: Network stream
* Returns: host name for this stream
*/
char *net_host (NETSTREAM *stream)
{
return (*stream->dtb->host) (stream->stream);
}
/* Network get remote host name
* Accepts: Network stream
* Returns: host name for this stream
*/
char *net_remotehost (NETSTREAM *stream)
{
return (*stream->dtb->remotehost) (stream->stream);
}
/* Network return port for this stream
* Accepts: Network stream
* Returns: port number for this stream
*/
unsigned long net_port (NETSTREAM *stream)
{
return (*stream->dtb->port) (stream->stream);
}
/* Network get local host name
* Accepts: Network stream
* Returns: local host name
*/
char *net_localhost (NETSTREAM *stream)
{
return (*stream->dtb->localhost) (stream->stream);
}
|