/usr/bin/email2trac is in trac-email2trac 2.8.8-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python
#
# This file is part of the email2trac utils
#
# Copyright 2002 SURFsara
#
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For vi/emacs or other use tabstop=4 (vi: set ts=4)
#
"""
email2trac.py -- Email -> TRAC tickets
A MTA filter to create Trac tickets from inbound emails.
first proof of concept from:
Copyright 2005, Daniel Lundin <daniel@edgewall.com>
Copyright 2005, Edgewall Software
Authors:
Bas van der Vlies <bas.vandervlies@surfsara.nl>
Walter de Jong <walter.dejong@surfsara.nl>
How to use
----------
* See https://oss.trac.surfsara.nl/email2trac/
* Commandline opions:
-h,--help
-A, --agilo
-B, --bh_product <name>
-d, --debug
-E, --virtualenv <path>
-f,--file <configuration file>
-n,--dry-run
-p, --project <project name>
-t, --ticket_prefix <name>
-v, --verbose
SVN Info:
$Id: email2trac.py.in 664 2015-04-28 06:58:37Z bas $
"""
import os
import sys
import string
import getopt
import time
import email
import email.Iterators
import email.Header
import re
import urllib
import unicodedata
import mimetypes
import traceback
import logging
import logging.handlers
import UserDict
import tempfile
from datetime import timedelta, datetime
from stat import *
## Some global variables
#
m = None
class SaraDict(UserDict.UserDict):
def __init__(self, dictin = None):
UserDict.UserDict.__init__(self)
self.name = None
if dictin:
if dictin.has_key('name'):
self.name = dictin['name']
del dictin['name']
self.data = dictin
def get_value(self, name):
if self.has_key(name):
return self[name]
else:
return None
def __repr__(self):
return repr(self.data)
def __str__(self):
return str(self.data)
def __getattr__(self, name):
"""
override the class attribute get method. Return the value
from the dictionary
"""
if self.data.has_key(name):
return self.data[name]
else:
return None
def __setattr__(self, name, value):
"""
override the class attribute set method only when the UserDict
has set its class attribute
"""
if self.__dict__.has_key('data'):
self.data[name] = value
else:
self.__dict__[name] = value
def __iter__(self):
return iter(self.data.keys())
class TicketEmailParser(object):
env = None
comment = '> '
def __init__(self, env, parameters, logger, version):
self.env = env
# Database connection
#
self.db = None
# Save parameters
#
self.parameters = parameters
self.logger = logger
# Some useful mail constants
#
self.email_name = None
self.email_addr = None
self.email_from = None
self.author = None
self.id = None
## Will be set to True if the user has ben registered
#
self.allow_registered_user = False
self.STRIP_CONTENT_TYPES = list()
## fields properties via body_text
#
self.properties = dict()
self.VERSION = version
self.get_config = self.env.config.get
## init function ##
#
self.setup_parameters()
def setup_parameters(self):
if self.parameters.umask:
os.umask(self.parameters.umask)
if not self.parameters.spam_level:
self.parameters.spam_level = 0
if not self.parameters.spam_header:
self.parameters.spam_header = 'X-Spam-Score'
if not self.parameters.email_quote:
self.parameters.email_quote = '^> .*'
self.parameters.skip_line_regex = '^> .*'
if not self.parameters.ticket_update_by_subject_lookback:
self.parameters.ticket_update_by_subject_lookback = 30
if self.parameters.verbatim_format == None:
self.parameters.verbatim_format = 1
if self.parameters.reflow == None:
self.parameters.reflow = 1
if self.parameters.binhex:
self.STRIP_CONTENT_TYPES.append('application/mac-binhex40')
if self.parameters.applesingle:
self.STRIP_CONTENT_TYPES.append('application/applefile')
if self.parameters.appledouble:
self.STRIP_CONTENT_TYPES.append('application/applefile')
if self.parameters.strip_content_types:
items = self.parameters.strip_content_types.split(',')
for item in items:
self.STRIP_CONTENT_TYPES.append(item.strip())
if self.parameters.tmpdir:
self.parameters.tmpdir = os.path.normcase(str(self.parameters['tmpdir']))
else:
self.parameters.tmpdir = os.path.normcase('/tmp')
if self.parameters.email_triggers_workflow == None:
self.parameters.email_triggers_workflow = 1
if not self.parameters.subject_field_separator:
self.parameters.subject_field_separator = '&'
else:
self.parameters.subject_field_separator = self.parameters.subject_field_separator.strip()
if not self.parameters.strip_signature_regex:
self.parameters.strip_signature_regex = '^-- $'
self.parameters.cut_line_regex = '^-- $'
self.trac_smtp_from = self.get_config('notification', 'smtp_from')
self.smtp_default_domain = self.get_config('notification', 'smtp_default_domain')
self.smtp_replyto = self.get_config('notification', 'smtp_replyto')
self.trac_smtp_always_cc = self.get_config('notification', 'smtp_always_cc')
self.trac_smtp_always_bcc = self.get_config('notification', 'smtp_always_bcc')
self.system = None
########## Email Header Functions ###########################################################
def spam(self, message):
"""
# X-Spam-Score: *** (3.255) BAYES_50,DNS_FROM_AHBL_RHSBL,HTML_
# Note if Spam_level then '*' are included
"""
self.logger.debug('function spam')
spam = False
if message.has_key(self.parameters.spam_header):
spam_l = string.split(message[self.parameters.spam_header])
#self.logger.info('Spam header: %s' %(message[self.parameters.spam_header]))
try:
number = spam_l[0].count('*')
except IndexError, detail:
number = 0
if number >= self.parameters.spam_level:
spam = True
## treat virus mails as spam
#
elif message.has_key('X-Virus-found'):
spam = True
## Astaro firewall spam handling
#
elif message.get('X-Spam-Flag') == "YES" and message.get('X-Spam-Result') == "Spam":
spam = True
## How to handle SPAM messages
#
if self.parameters.drop_spam and spam:
self.logger.info('Message is a SPAM. Automatic ticket insertion refused (SPAM level > %d)' %self.parameters.spam_level)
return 'drop'
elif spam:
return 'Spam'
else:
return False
def email_header_acl(self, keyword, message_field, default):
"""
This function wil check if the email address is allowed or denied
to send mail to the ticket list
"""
self.logger.debug('function email_header_acl: %s' %keyword)
try:
mail_addresses = self.parameters[keyword]
# Check if we have an empty string
#
if not mail_addresses:
return default
except KeyError, detail:
self.logger.debug('\t %s not defined, all messages are allowed.' %(keyword))
return default
mail_addresses = string.split(mail_addresses, ',')
message_addresses = string.split(message_field, ',')
for entry in mail_addresses:
entry = entry.strip()
TO_RE = re.compile(entry, re.VERBOSE|re.IGNORECASE)
for addr in message_addresses:
addr = addr.strip()
if self.parameters.compare_function_list in [ 'matches', 'match']:
s = '\t%s matches %s' %(addr, entry)
result = TO_RE.match(addr)
else:
s = '\t%s contains %s' %(addr, entry)
result = TO_RE.search(addr)
if result:
self.logger.debug(s)
return True
return False
def email_header_txt(self, m):
"""
Display To and CC addresses in description field
"""
s = ''
if m['To'] and len(m['To']) > 0:
s = "'''To:''' %s\r\n" %(m['To'])
if m['Cc'] and len(m['Cc']) > 0:
s = "%s'''Cc:''' %s\r\n" % (s, m['Cc'])
return self.email_to_unicode(s)
def get_sender_info(self, message):
"""
Get the default author name and email address from the message
"""
self.logger.debug('function get_sender_info')
to_addrs = email.Utils.getaddresses( message.get_all('to', []) )
self.email_to_addrs = list()
for n,e in to_addrs:
self.email_to_addrs.append(e)
self.email_to_addrs = ','.join(self.email_to_addrs)
self.email_name, self.email_addr = email.Utils.parseaddr(message['from'])
## decode email name can contain charset
#
self.email_name = self.email_to_unicode(self.email_name)
dstr = '\t email name: %s, email address: %s' %(self.email_name, self.email_addr)
self.logger.debug(dstr)
## Trac can not handle author's name that contains spaces
#
if self.email_addr.lower() == self.trac_smtp_from.lower():
if self.email_name:
self.author = self.email_name
else:
self.author = "email2trac"
else:
self.author = self.email_addr
if self.parameters.ignore_trac_user_settings:
return
# Is this a registered user, use email address as search key:
# result:
# u : login name
# n : Name that the user has set in the settings tab
# e : email address that the user has set in the settings tab
#
users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
if (
(e and (e.lower() == self.email_addr.lower())) or
(u and (u.lower() + '@' + self.smtp_default_domain.lower() == self.email_addr.lower()))
)
]
if len(users) >= 1:
self.email_from = users[0][2]
self.author = users[0][0]
self.logger.debug('\tget_sender_info: found registered user: %s' %(self.author) )
if self.parameters.white_list_registered_users:
self.logger.debug('\tget_sender_info: added registered user to white list')
self.allow_registered_user = True
def set_cc_fields(self, ticket, message, update=False):
"""
Set all the right fields for a new ticket
"""
self.logger.debug('function set_cc_fields')
# Put all CC-addresses in ticket CC field
#
if self.parameters.reply_all:
ticket_cc = ''
msg_cc_addrs = email.Utils.getaddresses( message.get_all('cc', []) )
if not msg_cc_addrs:
return
if update:
self.logger.debug("\tupdate ticket cc-field")
ticket_cc = ticket['cc']
ticket_cc_list = ticket_cc.split(',')
for name,addr in msg_cc_addrs:
## Prevent mail loop
#
if addr == self.trac_smtp_from:
self.logger.debug("\tSkipping %s email address for CC-field, same as smtp_from address in trac.ini " %(addr))
continue
## Alwyas remove reporter email address
#
elif addr == self.email_addr:
self.logger.debug("\tSkipping reporter email address for CC-field")
continue
## Always remove the always_cc address
#
elif addr == self.trac_smtp_always_cc:
self.logger.debug("\tSkipping smtp_always_cc email address for CC-field")
continue
## Always remove the always_bcc address
#
elif addr == self.trac_smtp_always_bcc:
self.logger.debug("\tSkipping smtp_always_bcc email address for CC-field")
continue
## Skip also addr in cc_black_list (email2trac.conf)
#
if self.email_header_acl('cc_black_list', addr, False):
self.logger.debug("\tSkipping address for CC-field due to cc_black_list")
continue
else:
## On update, prevent adding duplicates
#
if update:
if addr in ticket_cc_list:
continue
if ticket_cc:
ticket_cc = '%s, %s' %(ticket_cc, addr)
else:
ticket_cc = addr
if ticket_cc:
self.logger.debug('\tCC fields: %s' %ticket_cc)
ticket['cc'] = self.email_to_unicode(ticket_cc)
def acl_list_from_file(self, f, keyword):
"""
Read the email address from a file
"""
self.logger.debug('function acl_list_from_file %s : %s' %(f, keyword))
if not os.path.isfile(f):
self.logger.error('%s_file: %s does not exists' %(keyword, f) )
else:
## read whole file and replace '\n' with ''
#
addr_l = open(f, 'r').readlines()
s = ','.join(addr_l).replace('\n','')
try:
self.parameters[keyword] = "%s,%s" %(self.parameters[keyword], s)
except KeyError, detail:
self.parameters[keyword] = s
########## DEBUG functions ###########################################################
def debug_body(self, message_body, temp_file):
"""
"""
self.logger.debug('function debug_body:')
body_file = "%s.body" %(temp_file)
fx = open(body_file, 'wb')
if self.parameters.dry_run:
self.logger.info('DRY-RUN: not saving body to %s' %(body_file))
return
self.logger.debug('writing body to %s' %(body_file))
if not message_body:
message_body = '(None)'
message_body = message_body.encode('utf-8')
fx.write(message_body)
fx.close()
try:
os.chmod(body_file,S_IRWXU|S_IRWXG|S_IRWXO)
except OSError:
pass
def debug_attachments(self, message_parts, temp_file):
"""
"""
self.logger.debug('function debug_attachments')
n = 0
for item in message_parts:
## Skip inline text parts
#
if not isinstance(item, tuple):
continue
(original, filename, part) = item
self.logger.debug('\t part%d: Content-Type: %s' % (n, part.get_content_type()) )
dummy_filename, ext = os.path.splitext(filename)
n = n + 1
part_name = 'part%d%s' %(n, ext)
part_file = "%s.%s" %(temp_file, part_name)
s = 'writing %s: filename: %s' %(part_file, filename)
self.print_unicode(s)
## Forbidden chars, just write part files instead of names
#
#filename = filename.replace('\\', '_')
#filename = filename.replace('/', '_')
#filename = filename + '.att_email2trac'
# part_file = os.path.join(self.parameters.tmpdir, filename)
#part_file = util.text.unicode_quote(part_file)
#self.print_unicode(part_file)
if self.parameters.dry_run:
self.logger.info('DRY_RUN: NOT saving attachments')
continue
fx = open(part_file, 'wb')
text = part.get_payload(decode=1)
if not text:
text = '(None)'
fx.write(text)
fx.close()
try:
os.chmod(part_file,S_IRWXU|S_IRWXG|S_IRWXO)
except OSError:
pass
def save_email_for_debug(self, message, project_name, create_tempfile=False):
if self.parameters.dry_run:
self.logger.debug('DRY_RUN: NOT saving email message')
return
if create_tempfile:
(fd, tmp_file) = tempfile.mkstemp('.%s.email2trac' %project_name)
fx = os.fdopen(fd, 'wb')
else:
tmp_file = os.path.join(self.parameters.tmpdir, 'msg.%s.email2trac' %project_name)
fx = open(tmp_file, 'wb')
self.logger.debug('saving email to %s' %(tmp_file))
fx.write('%s' % message)
fx.close()
try:
os.chmod(tmp_file, S_IRWXU|S_IRWXG|S_IRWXO)
except OSError:
pass
message_parts = self.get_message_parts(message)
message_parts = self.unique_attachment_names(message_parts)
body_text = self.get_body_text(message_parts)
self.debug_body(body_text, tmp_file)
self.debug_attachments(message_parts, tmp_file)
########## Conversion functions ###########################################################
def email_to_unicode(self, message_str):
"""
Email has 7 bit ASCII code, convert it to unicode with the charset
that is encoded in 7-bit ASCII code and encode it as utf-8 so Trac
understands it.
"""
self.logger.debug("function email_to_unicode")
self.logger.debug("\t repr:%s type:%s" %(repr(message_str), type(message_str)))
## Skip unicode strings, there are already converted
#
if type(message_str) is unicode:
return message_str
results = email.Header.decode_header(message_str)
s = None
for text,format in results:
if format:
try:
temp = unicode(text, format)
except UnicodeError, detail:
# This always works
#
msg = 'ERROR: Could not find charset: %s, please install' %format
self.logger.error(msg)
temp = unicode(text, 'iso-8859-15')
except LookupError, detail:
msg = 'ERROR: Could not find charset: %s, please install' %format
self.logger.error(msg)
#temp = unicode(text, 'iso-8859-15')
temp = message_str
else:
temp = string.strip(text)
temp = unicode(text, 'iso-8859-15')
if s:
s = '%s %s' %(s, temp)
else:
s = '%s' %temp
return s
def str_to_dict(self, s):
"""
Transfrom a string of the form [<key>=<value>]+ to dict[<key>] = <value>
"""
self.logger.debug("function str_to_dict")
fields = string.split(s, self.parameters.subject_field_separator)
result = dict()
for field in fields:
try:
index, value = string.split(field, '=')
# We can not change the description of a ticket via the subject
# line. The description is the body of the email
#
if index.lower() in ['description']:
continue
if value:
result[index.lower()] = value
except ValueError:
pass
return result
def print_unicode(self,s):
"""
This function prints unicode strings if possible else it will quote it
"""
try:
self.logger.debug(s)
except UnicodeEncodeError, detail:
self.logger.debug(util.text.unicode_quote(s))
def html_2_txt(self, data):
"""
Various routines to convert html syntax to valid trac wiki syntax
"""
self.logger.debug('function html_2_txt')
## This routine make an safe html that can be include
# in trac, but no further text processing can be done
#
# try:
# from lxml.html.clean import Cleaner
# tags_rm = list()
# tags_rm.append('body')
#
# cleaner = Cleaner(remove_tags=tags_rm )
# parsed_data = cleaner.clean_html(data)
# parsed_data = '\n{{{\n#!html\n' + parsed_data + '\n}}}\n'
#
# return parsed_data
#
# except ImportError::
# pass
parsed_data = None
if self.parameters.html2text_cmd:
(fd, tmp_file) = tempfile.mkstemp('email2trac.html')
f = os.fdopen(fd, 'w')
cmd = '%s %s' %(self.parameters.html2text_cmd, tmp_file)
self.logger.debug('\t html2text conversion %s'%(cmd))
if self.parameters.dry_run:
self.logger.info('DRY_RUN: html2text conversion command: %s\n' %(cmd))
else:
f.write(data)
f.close()
lines = os.popen(cmd).readlines()
parsed_data = ''.join(lines)
os.unlink(tmp_file)
else:
self.logger.debug('\t No html2text conversion tool specified in email2trac.conf')
return parsed_data
def check_filename_length(self, name):
"""
To bypass a bug in Trac
check if the filename length is not larger then OS limit.
yes : return truncated filename
no : return unmodified filename
"""
self.logger.debug('function check_filename_length: ')
if not name:
return 'None'
dummy_filename, ext = os.path.splitext(name)
## Trac uses this format
#
try:
quote_format = util.text.unicode_quote(dummy_filename)
except UnicodeDecodeError, detail:
## last resort convert to unicode
#
dummy_filename = util.text.to_unicode(dummy_filename)
quote_format = util.text.unicode_quote(dummy_filename)
## Determine max filename length
#
try:
filemax_length = os.pathconf('/', 'PC_NAME_MAX')
except AttributeError, detail:
filemax_length = 240
if len(quote_format) <= filemax_length:
return name
else:
## Truncate file to filemax_length and reserve room for extension
# We must break on a boundry
#
length = filemax_length - 6
for i in range(0,10):
truncated = quote_format[ : (length - i)]
try:
unqoute_truncated = util.text.unicode_unquote(truncated)
unqoute_truncated = unqoute_truncated + ext
self.print_unicode('\t ' + unqoute_truncated)
break
except UnicodeDecodeError, detail:
continue
return unqoute_truncated
########## TRAC ticket functions ###########################################################
def mail_workflow(self, tkt):
"""
"""
self.logger.debug('function mail_workflow: ')
req = Mock(authname=self.author, perm=MockPerm(), args={})
ticket_system = TicketSystem(self.env)
try:
workflow = self.parameters['workflow_%s' %tkt['status'].lower()]
except KeyError:
## fallback for compability (Will be deprecated)
# workflow can be none.
#
workflow = None
if tkt['status'] in ['closed']:
workflow = self.parameters.workflow
if workflow:
## process all workflow implementations
#
tkt_module = TicketModule(self.env)
field_changes, problems = tkt_module.get_ticket_changes(req, tkt, workflow)
for field in field_changes.keys():
## We have already processed these fields
#
if not field in ['summary', 'description']:
s = 'workflow : %s, field %s : %s, by %s' \
%(workflow, field, field_changes[field]['new'],field_changes[field]['by'] )
self.logger.debug(s)
tkt[field] = field_changes[field]['new']
return True
else:
return False
def check_permission_participants(self, tkt, action):
"""
Check if the mailer is allowed to update the ticket
"""
self.logger.debug('function check_permission_participants %s')
if tkt['reporter'].lower() in [self.author.lower(), self.email_addr.lower()]:
self.logger.debug('ALLOW, %s is the ticket reporter' %(self.email_addr))
return True
perm = PermissionSystem(self.env)
if perm.check_permission(action, self.author):
self.logger.debug('ALLOW, %s has trac permission to update the ticket' %(self.author))
return True
# Is the updater in the CC?
try:
cc_list = tkt['cc'].split(',')
for cc in cc_list:
if self.email_addr.lower() in cc.lower().strip():
self.logger.debug('ALLOW, %s is in the CC' %(self.email_addr))
return True
except KeyError:
pass
return False
def check_permission(self, tkt, action):
"""
check if the reporter has the right permission for the action:
- TICKET_CREATE
- TICKET_MODIFY
- TICKET_APPEND
- TICKET_CHGPROP
There are three models:
- None : no checking at all
- trac : check the permission via trac permission model
- email2trac: ....
"""
self.logger.debug("function check_permission: %s" %(action))
if self.parameters.ticket_permission_system in ['trac']:
perm = PermissionCache(self.env, self.author)
if perm.has_permission(action):
return True
else:
return False
elif self.parameters.ticket_permission_system in ['update_restricted_to_participants']:
return (self.check_permission_participants(tkt, action))
## Default is to allow everybody ticket updates and ticket creation
#
else:
return True
def update_ticket_fields(self, ticket, user_dict, new=None):
"""
This will update the ticket fields. It will check if the
given fields are known and if the right values are specified
It will only update the ticket field value:
- If the field is known
- If the value supplied is valid for the ticket field.
If not then there are two options:
1) Skip the value (new=None)
2) Set default value for field (new=1)
"""
self.logger.debug("function update_ticket_fields")
if self.parameters.bh_product:
if 'product' in user_dict:
if user_dict['product'] != ticket.env.product.prefix:
self.logging.warning("bloodhound products cannot be changed "
"- ignoring")
user_dict.pop('product')
## Check only permission model on ticket updates
#
if not new:
if self.parameters.ticket_permission_system:
if not self.check_permission(ticket, 'TICKET_CHGPROP'):
self.logger.info('Reporter: %s has no permission to change ticket properties' %self.author)
return False
## Build a system dictionary from the ticket fields
# with field as index and option as value
#
sys_dict = dict()
for field in ticket.fields:
try:
sys_dict[field['name']] = field['options']
except KeyError:
sys_dict[field['name']] = None
pass
## Check user supplied fields an compare them with the
# system one's
#
for field,value in user_dict.items():
s = 'user_field\t %s = %s' %(field,value)
self.print_unicode(s)
if not field in sys_dict.keys():
self.logger.debug('%s is not a valid field for tickets' %(field))
continue
## To prevent mail loop
#
if field == 'cc':
cc_list = user_dict['cc'].split(',')
if self.trac_smtp_from in cc_list:
self.logger.debug('MAIL LOOP: %s is not allowed as CC address' %(self.trac_smtp_from))
cc_list.remove(self.trac_smtp_from)
value = ','.join(cc_list)
## Check if every value is allowed for this field
#
if sys_dict[field]:
if value in sys_dict[field]:
ticket[field] = value
else:
## Must we set a default if value is not allowed
#
if new:
value = self.get_config('ticket', 'default_%s' %(field) )
else:
ticket[field] = value
s = 'ticket_field\t %s = %s' %(field, ticket[field])
self.print_unicode(s)
def ticket_update(self, m, id, spam):
"""
If the current email is a reply to an existing ticket, this function
will append the contents of this email to that ticket, instead of
creating a new one.
"""
self.logger.debug("function ticket_update")
if not self.parameters.ticket_update:
self.logger.debug("ticket_update disabled")
return False
## Must we update ticket fields
#
update_fields = dict()
try:
id, keywords = string.split(id, '?')
update_fields = self.str_to_dict(keywords)
## Strip '#'
#
self.id = int(id[1:])
except ValueError:
## Strip '#'
#
self.id = int(id[1:])
self.logger.debug("\tticket id: %s" %id)
## When is the change committed
#
if self.VERSION < 0.11:
when = int(time.time())
else:
when = datetime.now(util.datefmt.utc)
try:
tkt = Ticket(self.env, self.id, self.db)
except util.TracError, detail:
## Not a valid ticket
#
self.logger.info("\tCreating a new ticket, ticket id: %s does not exists" %id)
self.id = None
return False
## Check the permission of the reporter
#
if self.parameters.ticket_permission_system:
if not self.check_permission(tkt, 'TICKET_APPEND'):
self.logger.info('Reporter: %s has no permission to add comments or attachments to tickets' %self.author)
return False
## How many changes has this ticket
#
grouped = TicketModule(self.env).grouped_changelog_entries(tkt, self.db)
cnum = sum(1 for e in grouped) + 1
## reopen the ticket if it is was closed
# We must use the ticket workflow framework
#
if self.parameters.email_triggers_workflow and (self.VERSION >= 0.11):
if not self.mail_workflow(tkt):
if tkt['status'] in ['closed']:
tkt['status'] = 'reopened'
tkt['resolution'] = ''
## Old pre 0.11 situation
#
elif self.parameters.email_triggers_workflow:
self.logger.debug('\temail triggers workflow pre trac 0.11')
if tkt['status'] in ['closed']:
tkt['status'] = 'reopened'
tkt['resolution'] = ''
else:
self.logger.debug('\temail triggers workflow disabled')
## Must we update some ticket fields properties via subject line
#
if update_fields:
self.update_ticket_fields(tkt, update_fields)
message_parts = self.get_message_parts(m)
message_parts = self.unique_attachment_names(message_parts)
## Must we update some ticket fields properties via inline comments
# in body_text
#
if self.properties:
self.update_ticket_fields(tkt, self.properties)
## Must we update the CC ticket field
#
self.set_cc_fields(tkt, m, update=True)
if self.parameters.email_header:
message_parts.insert(0, self.email_header_txt(m))
body_text = self.get_body_text(message_parts)
error_with_attachments = self.attach_attachments(message_parts)
if body_text.strip() or update_fields or self.properties:
if self.parameters.dry_run:
s = 'DRY_RUN: tkt.save_changes(self.author, body_text, ticket_change_number) %s %s' %(self.author, cnum)
self.logger.info(s)
else:
if error_with_attachments:
body_text = '%s\\%s' %(error_with_attachments, body_text)
self.logger.debug('\ttkt.save_changes(%s, %d)' %(self.author, cnum))
tkt.save_changes(self.author, body_text, when, None, str(cnum))
if not spam:
self.notify(tkt, False, when)
return True
def set_ticket_fields(self, ticket):
"""
set the ticket fields to value specified
- /etc/email2trac.conf with <prefix>_<field>
- trac default values, trac.ini
"""
self.logger.debug('function set_ticket_fields')
user_dict = dict()
for field in ticket.fields:
name = field['name']
## default trac value
#
CUSTOM_FIELD = False
if not field.get('custom'):
value = self.get_config('ticket', 'default_%s' %(name) )
## skip this field can only be set by email2trac.conf
#
if name in ['resolution']:
value = None
else:
## Else get the default value for custom fields
#
CUSTOM_FIELD = True
value = field.get('value')
options = field.get('options')
if value and options and (value not in options):
value = options[int(value)]
s = 'trac[%s] = %s' %(name, value)
self.print_unicode(s)
## email2trac.conf settings
#
prefix = self.parameters.ticket_prefix
try:
value = self.parameters['%s_%s' %(prefix, name)]
s = 'email2trac[%s] = %s ' %(name, value)
self.print_unicode(s)
except KeyError, detail:
pass
if value:
user_dict[name] = value
s = 'used %s = %s' %(name, value)
self.print_unicode(s)
else:
## custom fields need some initialisation
#
if CUSTOM_FIELD:
user_dict[name] = ''
self.update_ticket_fields(ticket, user_dict, new=1)
if 'status' not in user_dict.keys():
ticket['status'] = 'new'
def ticket_update_by_subject(self, subject):
"""
This list of Re: prefixes is probably incomplete. Taken from
wikipedia. Here is how the subject is matched
- Re: <subject>
- Re: (<Mail list label>:)+ <subject>
So we must have the last column
"""
self.logger.debug('function ticket_update_by_subject')
found_id = None
if self.parameters.ticket_update and self.parameters.ticket_update_by_subject:
SUBJECT_RE = re.compile(r'^(?:(?:RE|AW|VS|SV|FW|FWD):\s*)+(.*)', re.IGNORECASE)
result = SUBJECT_RE.search(subject)
if result:
## This is a reply
#
orig_subject = result.group(1)
self.logger.debug('subject search string: %s' %(orig_subject))
cursor = self.db.cursor()
summaries = [orig_subject, '%%: %s' % orig_subject]
## Time resolution is in micoseconds
#
search_date = datetime.now(util.datefmt.utc) - timedelta(days=self.parameters.ticket_update_by_subject_lookback)
if self.VERSION < 0.12:
lookback = util.datefmt.to_timestamp(search_date)
else:
lookback = util.datefmt.to_utimestamp(search_date)
for summary in summaries:
self.logger.debug('Looking for summary matching: "%s"' % summary)
sql = """SELECT id, reporter FROM ticket
WHERE changetime >= %s AND summary LIKE %s
ORDER BY changetime DESC"""
cursor.execute(sql, [lookback, summary.strip()])
for row in cursor:
(matched_id, sql_reporter) = row
## Save first entry.
#
if not found_id:
found_id = matched_id
## If subject and reporter are the same. The we certainly have found the right ticket
#
if sql_reporter == self.author:
self.logger.debug('Found matching reporter: %s with ticket id: %d' %(sql_reporter, matched_id))
found_id = matched_id
break
if found_id:
self.logger.debug('Found matching ticket id: %d' % found_id)
found_id = '#%d' % found_id
return (found_id, orig_subject)
return (found_id, subject)
def new_ticket(self, msg, subject, spam, set_fields = None):
"""
Create a new ticket
"""
self.logger.debug('function new_ticket')
tkt = Ticket(self.env)
## self.author can be email address of an username
#
tkt['reporter'] = self.author
self.set_cc_fields(tkt, msg)
self.set_ticket_fields(tkt)
## Check the permission of the reporter
#
if self.parameters.ticket_permission_system:
if not self.check_permission(tkt, 'TICKET_CREATE'):
self.logger.info('Reporter: %s has no permission to create tickets' %self.author)
return False
## Old style setting for component, will be removed
#
if spam:
tkt['component'] = 'Spam'
elif self.parameters.has_key('component'):
tkt['component'] = self.parameters['component']
if not msg['Subject']:
tkt['summary'] = u'(No subject)'
else:
tkt['summary'] = subject
if set_fields:
rest, keywords = string.split(set_fields, '?')
if keywords:
update_fields = self.str_to_dict(keywords)
self.update_ticket_fields(tkt, update_fields)
message_parts = self.get_message_parts(msg, True)
## Must we update some ticket fields properties via body_text
#
if self.properties:
self.update_ticket_fields(tkt, self.properties)
message_parts = self.unique_attachment_names(message_parts)
## produce e-mail like header
#
head = ''
if self.parameters.email_header:
head = self.email_header_txt(msg)
message_parts.insert(0, head)
body_text = self.get_body_text(message_parts)
tkt['description'] = body_text
## When is the change committed
#
if self.VERSION < 0.11:
when = int(time.time())
else:
when = datetime.now(util.datefmt.utc)
if self.parameters.dry_run:
self.logger.info('DRY_RUN: tkt.insert()')
else:
self.id = tkt.insert()
changed = False
comment = ''
## some routines in trac are dependend on ticket id
# like alternate notify template
#
if self.parameters.alternate_notify_template:
tkt['id'] = self.id
changed = True
## Rewrite the description if we have mailto enabled
#
if self.parameters.mailto_link:
changed = True
comment = u'\nadded mailto line\n'
#mailto = self.html_mailto_link( m['Subject'])
mailto = self.html_mailto_link(subject)
tkt['description'] = u'%s\r\n%s%s\r\n' \
%(head, mailto, body_text)
## Save the attachments to the ticket
#
error_with_attachments = self.attach_attachments(message_parts)
if error_with_attachments:
changed = True
comment = '%s\n%s\n' %(comment, error_with_attachments)
if self.parameters.email_triggers_workflow and (self.VERSION >= 0.11):
if self.mail_workflow(tkt):
changed = True
if changed:
if self.parameters.dry_run:
s = 'DRY_RUN: tkt.save_changes(%s, comment) real reporter = %s' %( tkt['reporter'], self.author)
self.logger.info(s)
else:
tkt.save_changes(tkt['reporter'], comment)
if not spam:
self.notify(tkt, True)
def attach_attachments(self, message_parts, update=False):
'''
save any attachments as files in the ticket's directory
'''
self.logger.debug('function attach_attachments()')
if self.parameters.dry_run:
self.logger.debug("DRY_RUN: no attachments attached to tickets")
return ''
count = 0
## Get Maxium attachment size
#
max_size = int(self.get_config('attachment', 'max_size'))
status = None
for item in message_parts:
## Skip body parts
#
if not isinstance(item, tuple):
continue
(original, filename, part) = item
## We have to determine the size so we use this temporary solution.
#
path, fd = util.create_unique_file(os.path.join(self.parameters.tmpdir, 'email2trac_tmp.att'))
text = part.get_payload(decode=1)
if not text:
text = '(None)'
fd.write(text)
fd.close()
## get the file_size
#
stats = os.lstat(path)
file_size = stats[ST_SIZE]
## Check if the attachment size is allowed
#
if (max_size != -1) and (file_size > max_size):
status = '%s\nFile %s is larger then allowed attachment size (%d > %d)\n\n' \
%(status, original, file_size, max_size)
os.unlink(path)
continue
else:
count = count + 1
## Insert the attachment
#
fd = open(path, 'rb')
if self.system == 'discussion':
att = attachment.Attachment(self.env, 'discussion', 'topic/%s' % (self.id,))
elif self.system == 'blog':
att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,))
else:
s = 'Attach %s to ticket %d' %(filename, self.id)
self.print_unicode(s)
att = attachment.Attachment(self.env, 'ticket', self.id)
## This will break the ticket_update system, the body_text is vaporized
# ;-(
#
if not update:
att.author = self.author
att.description = self.email_to_unicode('Added by email2trac')
try:
self.logger.debug('Insert atachment')
att.insert(filename, fd, file_size)
except OSError, detail:
self.logger.info('%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail))
status = '%s\nFilename %s could not be saved, problem: %s' %(status, filename, detail)
## Remove the created temporary filename
#
fd.close()
os.unlink(path)
## return error
#
return status
########## Fullblog functions #################################################
def blog(self, msg, subject, id, params):
"""
The blog create/update function
"""
## import the modules
#
from tracfullblog.core import FullBlogCore
from tracfullblog.model import BlogPost, BlogComment
## instantiate blog core
#
blog = FullBlogCore(self.env)
req = Mock(authname='anonymous', perm=MockPerm(), args={})
## parameters from the subject
#
params = self.str_to_dict((params or '').lstrip('?'))
## preferably get the time from the email date header, or else
# use current date and time
date = email.Utils.parsedate_tz(msg.get('date'))
if date:
dt = util.datefmt.to_datetime(email.Utils.mktime_tz(date), util.datefmt.utc)
else:
self.logger.warn("No valid date header found")
dt = util.datefmt.to_datetime(None, util.datefmt.utc)
## blog entry affected
#
self.id = id or util.datefmt.format_datetime(dt, "%Y%m%d%H%M%S", util.datefmt.utc)
## check wether a blog post exists
#
post = BlogPost(self.env, self.id)
force_update = self.properties.get('update', params.get('update'))
## message parts
#
message_parts = self.get_message_parts(msg)
message_parts = self.unique_attachment_names(message_parts)
if post.get_versions() and not force_update:
## add comment to blog entry
#
comment = BlogComment(self.env, self.id)
comment.author = self.properties.get('author', params.get('author', self.author))
comment.comment = self.get_body_text(message_parts)
comment.time = dt
if self.parameters.dry_run:
self.logger.info('DRY-RUN: not adding comment for blog entry "%s"' % id)
return
warnings = blog.create_comment(req, comment)
else:
## create or update blog entry
#
post.author = self.properties.get('author', params.get('author', self.author))
post.categories = self.properties.get('categories', params.get('categories', ''))
post.title = subject.strip()
post.publish_time = dt
post.body = self.get_body_text(message_parts)
if self.parameters.dry_run:
self.logger.info('DRY-RUN: not creating blog entry "%s"' % post.title)
return
warnings = blog.create_post(req, post, self.author, u'Created by email2trac', False)
## check for problems
#
if warnings:
raise TracError(', '.join('blog:%s:%s' % (w[0], w[1]) for w in warnings))
## all seems well, attach attachments
#
self.attach_attachments(message_parts)
########## Discussion functions ##############################################
def discussion_topic(self, content, subject):
## Import modules.
#
from tracdiscussion.api import DiscussionApi
from trac.util.datefmt import to_timestamp, utc
self.logger.debug('Creating a new topic in forum:', self.id)
## Get dissussion API component.
#
api = self.env[DiscussionApi]
context = self._create_context(content, subject)
## Get forum for new topic.
#
forum = api.get_forum(context, self.id)
if not forum:
self.logger.error("ERROR: Replied forum doesn't exist")
## Prepare topic.
#
topic = {'forum' : forum['id'],
'subject' : context.subject,
'time': to_timestamp(datetime.now(utc)),
'author' : self.author,
'subscribers' : [self.email_addr],
'body' : self.get_body_text(context.content_parts)}
## Add topic to DB and commit it.
#
self._add_topic(api, context, topic)
self.db.commit()
def discussion_topic_reply(self, content, subject):
## Import modules.
#
from tracdiscussion.api import DiscussionApi
from trac.util.datefmt import to_timestamp, utc
self.logger.debug('Replying to discussion topic', self.id)
## Get dissussion API component.
#
api = self.env[DiscussionApi]
context = self._create_context(content, subject)
## Get replied topic.
#
topic = api.get_topic(context, self.id)
if not topic:
self.logger.error("ERROR: Replied topic doesn't exist")
## Prepare message.
#
message = {'forum' : topic['forum'],
'topic' : topic['id'],
'replyto' : -1,
'time' : to_timestamp(datetime.now(utc)),
'author' : self.author,
'body' : self.get_body_text(context.content_parts)}
## Add message to DB and commit it.
#
self._add_message(api, context, message)
self.db.commit()
def discussion_message_reply(self, content, subject):
## Import modules.
#
from tracdiscussion.api import DiscussionApi
from trac.util.datefmt import to_timestamp, utc
self.logger.debug('Replying to discussion message', self.id)
## Get dissussion API component.
#
api = self.env[DiscussionApi]
context = self._create_context(content, subject)
## Get replied message.
#
message = api.get_message(context, self.id)
if not message:
self.logger.error("ERROR: Replied message doesn't exist")
## Prepare message.
#
message = {'forum' : message['forum'],
'topic' : message['topic'],
'replyto' : message['id'],
'time' : to_timestamp(datetime.now(utc)),
'author' : self.author,
'body' : self.get_body_text(context.content_parts)}
## Add message to DB and commit it.
#
self._add_message(api, context, message)
self.db.commit()
def _create_context(self, content, subject):
## Import modules.
#
from trac.mimeview import Context
from trac.web.api import Request
from trac.perm import PermissionCache
## TODO: Read server base URL from config.
# Create request object to mockup context creation.
#
environ = {'SERVER_PORT' : 80,
'SERVER_NAME' : 'test',
'REQUEST_METHOD' : 'POST',
'wsgi.url_scheme' : 'http',
'wsgi.input' : sys.stdin}
chrome = {'links': {},
'scripts': [],
'ctxtnav': [],
'warnings': [],
'notices': []}
if self.env.base_url_for_redirect:
environ['trac.base_url'] = self.env.base_url
req = Request(environ, None)
req.chrome = chrome
req.tz = 'missing'
req.authname = self.author
req.perm = PermissionCache(self.env, self.author)
req.locale = None
## Create and return context.
#
context = Context.from_request(req)
context.realm = 'discussion-email2trac'
context.cursor = self.db.cursor()
context.content = content
context.subject = subject
## Read content parts from content.
#
context.content_parts = self.get_message_parts(content)
context.content_parts = self.unique_attachment_names(
context.content_parts)
return context
def _add_topic(self, api, context, topic):
context.req.perm.assert_permission('DISCUSSION_APPEND')
## Filter topic.
#
for discussion_filter in api.discussion_filters:
accept, topic_or_error = discussion_filter.filter_topic(
context, topic)
if accept:
topic = topic_or_error
else:
raise TracError(topic_or_error)
## Add a new topic.
#
api.add_topic(context, topic)
## Get inserted topic with new ID.
#
topic = api.get_topic_by_time(context, topic['time'])
## Attach attachments.
#
self.id = topic['id']
self.attach_attachments(context.content_parts, True)
## Notify change listeners.
#
for listener in api.topic_change_listeners:
listener.topic_created(context, topic)
def _add_message(self, api, context, message):
context.req.perm.assert_permission('DISCUSSION_APPEND')
## Filter message.
#
for discussion_filter in api.discussion_filters:
accept, message_or_error = discussion_filter.filter_message(
context, message)
if accept:
message = message_or_error
else:
raise TracError(message_or_error)
## Add message.
#
api.add_message(context, message)
## Get inserted message with new ID.
#
message = api.get_message_by_time(context, message['time'])
## Attach attachments.
#
self.attach_attachments(context.content_parts, True)
## Notify change listeners.
#
for listener in api.message_change_listeners:
listener.message_created(context, message)
########## MAIN function ######################################################
def parse(self, fp):
"""
"""
self.logger.debug('Main function parse')
global m
m = email.message_from_file(fp)
if not m:
self.logger.debug('This is not a valid email message format')
return
## Work around lack of header folding in Python; see http://bugs.python.org/issue4696
#
try:
m.replace_header('Subject', m['Subject'].replace('\r', '').replace('\n', ''))
except AttributeError, detail:
pass
if self.parameters.debug: # save the entire e-mail message text
self.save_email_for_debug(m, self.parameters.project_name, True)
if self.VERSION <= 0.11:
self.db = self.env.get_db_cnx()
else:
self.db = self.env.get_read_db()
self.get_sender_info(m)
if self.parameters.white_list_file:
self.acl_list_from_file(self.parameters.white_list_file, 'white_list')
if not ( self.email_header_acl('white_list', self.email_addr, True) or self.allow_registered_user ) :
self.logger.info('Message rejected : %s not in white list' %(self.email_addr))
return False
if self.email_header_acl('black_list', self.email_addr, False):
self.logger.info('Message rejected : %s in black list' %(self.email_addr))
return False
if not self.email_header_acl('recipient_list', self.email_to_addrs, True):
self.logger.info('Message rejected : %s not in recipient list' %(self.email_to_addrs))
return False
## If spam drop the message
#
if self.spam(m) == 'drop':
return False
elif self.spam(m) == 'spam':
spam_msg = True
else:
spam_msg = False
if not m['Subject']:
subject = 'No Subject'
else:
subject = self.email_to_unicode(m['Subject'])
## Bug in python logging module <2.6
#
self.logger.info('subject: %s' %repr(subject))
## First try the new method and then fall back to old method
#
if not self.parse_delivered_to_field(m, subject, spam_msg):
self.parse_subject_field(m, subject, spam_msg)
def parse_delivered_to_field(self, m, subject, spam_msg):
"""
"""
self.logger.debug('function parse_delivered_to_field')
## Ticket id is in Delivered-To Field. Trac notify must send this
# eg: is+390@surfsara.nl
## Only run if this parameter is se
#
if not self.parameters.recipient_delimiter:
return False
try:
#print self.parameters.project_name
self.logger.debug('Delivered To: %s' %m['Delivered-To'])
id = m['Delivered-To']
id = id.split(self.parameters.recipient_delimiter)[1]
id = id.split('@')[0]
self.logger.debug('\t Found ticket id: %s' %id)
if not self.ticket_update(m, id, spam_msg):
return False
except KeyError, detail:
pass
except IndexError, detail:
pass
return False
def parse_subject_field(self, m, subject, spam_msg):
"""
"""
self.logger.debug('function parse_subject_header')
## [hic] #1529: Re: LRZ
# [hic] #1529?owner=bas,priority=medium: Re: LRZ
#
ticket_regex = r'''
(?P<new_fields>[#][?].*)
|(?P<reply>(?P<id>[#][\d]+)(?P<fields>\?[^:]*)?:)
'''
## Check if FullBlogPlugin is installed
#
blog_enabled = None
blog_regex = ''
if self.get_config('components', 'tracfullblog.*') in ['enabled']:
self.logger.debug('Trac BLOG support enabled')
blog_enabled = True
blog_regex = '''|(?P<blog>blog(?P<blog_params>[?][^:]*)?:(?P<blog_id>\S*))'''
## Check if DiscussionPlugin is installed
#
discussion_enabled = None
discussion_regex = ''
if self.get_config('components', 'tracdiscussion.api.discussionapi') in ['enabled']:
self.logger.debug('Trac Discussion support enabled')
discussion_enabled = True
discussion_regex = r'''
|(?P<forum>Forum[ ][#](?P<forum_id>\d+)[ ]-[ ]?)
|(?P<topic>Topic[ ][#](?P<topic_id>\d+)[ ]-[ ]?)
|(?P<message>Message[ ][#](?P<message_id>\d+)[ ]-[ ]?)
'''
regex_str = ticket_regex + blog_regex + discussion_regex
SYSTEM_RE = re.compile(regex_str, re.VERBOSE)
## Find out if this is a ticket, a blog or a discussion
#
result = SYSTEM_RE.search(subject)
if result:
## update ticket + fields
#
if result.group('reply'):
self.system = 'ticket'
## Skip the last ':' character
#
if not self.ticket_update(m, result.group('reply')[:-1], spam_msg):
self.new_ticket(m, subject, spam_msg)
## New ticket + fields
#
elif result.group('new_fields'):
self.system = 'ticket'
self.new_ticket(m, subject[:result.start('new_fields')], spam_msg, result.group('new_fields'))
if blog_enabled:
if result.group('blog'):
self.system = 'blog'
self.blog(m, subject[result.end('blog'):], result.group('blog_id'), result.group('blog_params'))
if discussion_enabled:
## New topic.
#
if result.group('forum'):
self.system = 'discussion'
self.id = int(result.group('forum_id'))
self.discussion_topic(m, subject[result.end('forum'):])
## Reply to topic.
#
elif result.group('topic'):
self.system = 'discussion'
self.id = int(result.group('topic_id'))
self.discussion_topic_reply(m, subject[result.end('topic'):])
## Reply to topic message.
#
elif result.group('message'):
self.system = 'discussion'
self.id = int(result.group('message_id'))
self.discussion_message_reply(m, subject[result.end('message'):])
else:
self.system = 'ticket'
(matched_id, subject) = self.ticket_update_by_subject(subject)
if matched_id:
if not self.ticket_update(m, matched_id, spam_msg):
self.new_ticket(m, subject, spam_msg)
else:
## No update by subject, so just create a new ticket
#
self.new_ticket(m, subject, spam_msg)
########## BODY TEXT functions ###########################################################
def strip_signature(self, text):
"""
Strip signature from message, inspired by Mailman software
"""
self.logger.debug('function strip_signature: %s' %self.parameters.strip_signature_regex)
body = []
STRIP_RE = re.compile( self.parameters.strip_signature_regex )
for line in text.splitlines():
match = STRIP_RE.match(line)
if match:
self.logger.debug('\t"%s " matched, skiping rest of message' %line)
break
body.append(line)
return ('\n'.join(body))
def reflow(self, text, delsp = 0):
"""
Reflow the message based on the format="flowed" specification (RFC 3676)
"""
flowedlines = []
quotelevel = 0
prevflowed = 0
for line in text.splitlines():
from re import match
## Figure out the quote level and the content of the current line
#
m = match('(>*)( ?)(.*)', line)
linequotelevel = len(m.group(1))
line = m.group(3)
## Determine whether this line is flowed
#
if line and line != '-- ' and line[-1] == ' ':
flowed = 1
else:
flowed = 0
if flowed and delsp and line and line[-1] == ' ':
line = line[:-1]
## If the previous line is flowed, append this line to it
#
if prevflowed and line != '-- ' and linequotelevel == quotelevel:
flowedlines[-1] += line
## Otherwise, start a new line
#
else:
flowedlines.append('>' * linequotelevel + line)
prevflowed = flowed
return '\n'.join(flowedlines)
def strip_quotes(self, text):
"""
Strip quotes from message by Nicolas Mendoza
"""
self.logger.debug('function strip_quotes: %s' %self.parameters.email_quote)
body = []
STRIP_RE = re.compile( self.parameters.email_quote )
for line in text.splitlines():
try:
match = STRIP_RE.match(line)
if match:
self.logger.debug('\t"%s " matched, skipping rest of message' %line)
continue
except UnicodeDecodeError:
tmp_line = self.email_to_unicode(line)
match = STRIP_RE.match(tmp_line)
if match:
self.logger.debug('\t"%s " matched, skipping rest of message' %line)
continue
body.append(line)
return ('\n'.join(body))
def inline_properties(self, text):
"""
Parse text if we use inline keywords to set ticket fields
"""
self.logger.debug('function inline_properties')
properties = dict()
body = list()
INLINE_EXP = re.compile('\s*[@]\s*(\w+)\s*:(.*)$')
for line in text.splitlines():
match = INLINE_EXP.match(line)
if match:
keyword, value = match.groups()
if self.parameters.inline_properties_first_wins:
if keyword in self.properties.keys():
continue
self.properties[keyword] = value.strip()
self.logger.debug('\tinline properties: %s : %s' %(keyword,value))
else:
body.append(line)
return '\n'.join(body)
def wrap_text(self, text, replace_whitespace = False):
"""
Will break a lines longer then given length into several small
lines of size given length
"""
import textwrap
LINESEPARATOR = '\n'
reformat = ''
for s in text.split(LINESEPARATOR):
tmp = textwrap.fill(s, self.parameters.use_textwrap)
if tmp:
reformat = '%s\n%s' %(reformat,tmp)
else:
reformat = '%s\n' %reformat
return reformat
# Python2.4 and higher
#
#return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR))
#
########## EMAIL attachements functions ###########################################################
def inline_part(self, part):
"""
"""
self.logger.debug('function inline_part()')
return part.get_param('inline', None, 'Content-Disposition') == '' or not part.has_key('Content-Disposition')
def get_message_parts(self, msg, new_email=False):
"""
parses the email message and returns a list of body parts and attachments
body parts are returned as strings, attachments are returned as tuples of (filename, Message object)
"""
self.logger.debug('function get_message_parts()')
message_parts = list()
ALTERNATIVE_MULTIPART = False
for part in msg.walk():
content_maintype = part.get_content_maintype()
content_type = part.get_content_type()
self.logger.debug('\t Message part: Main-Type: %s' % content_maintype)
self.logger.debug('\t Message part: Content-Type: %s' % content_type)
## Check content type
#
if content_type in self.STRIP_CONTENT_TYPES:
self.logger.debug("\t A %s attachment named '%s' was skipped" %(content_type, part.get_filename()))
continue
## Catch some mulitpart execptions
#
if content_type == 'multipart/alternative':
ALTERNATIVE_MULTIPART = True
continue
## Skip multipart containers
#
if content_maintype == 'multipart':
self.logger.debug("\t Skipping multipart container")
continue
## Check if this is an inline part. It's inline if there is co Cont-Disp header,
# or if there is one and it says "inline"
#
inline = self.inline_part(part)
## Drop HTML message
#
if ALTERNATIVE_MULTIPART and self.parameters.drop_alternative_html_version:
if content_type == 'text/html':
self.logger.debug('\t Skipping alternative HTML message')
ALTERNATIVE_MULTIPART = False
continue
#if self.VERSION < 1.0:
# filename = part.get_filename()
## convert 7 bit filename to 8 bit unicode
#
raw_filename = part.get_filename()
filename = self.email_to_unicode(raw_filename);
s = '\t unicode filename: %s' %(filename)
self.print_unicode(s)
self.logger.debug('\t raw filename: %s' %repr(raw_filename))
if self.VERSION < 1.0:
filename = self.check_filename_length(filename)
## Save all non plain text message as attachment
#
if not content_type in ['text/plain']:
message_parts.append( (filename, part) )
## We only convert html messages
#
if not content_type == 'text/html':
self.logger.debug('\t Appending %s (%s)' %(repr(filename), content_type))
continue
## We have an text or html message
#
if not inline:
self.logger.debug('\t Appending %s (%s), not an inline messsage part' %(repr(filename), content_type))
message_parts.append( (filename, part) )
continue
## Try to decode message part. We have a html or plain text messafe
#
body_text = part.get_payload(decode=1)
if not body_text:
body_text = part.get_payload(decode=0)
## Try to convert html message
#
if content_type == 'text/html':
body_text = self.html_2_txt(body_text)
if not body_text:
continue
format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower()
delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower()
if self.parameters.reflow and not self.parameters.verbatim_format and format == 'flowed':
body_text = self.reflow(body_text, delsp == 'yes')
if new_email and self.parameters.only_strip_on_update:
self.logger.debug('Skip signature/quote stripping for new messages')
else:
if self.parameters.strip_signature:
body_text = self.strip_signature(body_text)
if self.parameters.strip_quotes:
body_text = self.strip_quotes(body_text)
if self.parameters.inline_properties:
body_text = self.inline_properties(body_text)
if self.parameters.use_textwrap:
body_text = self.wrap_text(body_text)
## Get contents charset (iso-8859-15 if not defined in mail headers)
#
charset = part.get_content_charset()
if not charset:
charset = 'iso-8859-15'
try:
ubody_text = unicode(body_text, charset)
except UnicodeError, detail:
ubody_text = unicode(body_text, 'iso-8859-15')
except LookupError, detail:
ubody_text = 'ERROR: Could not find charset: %s, please install' %(charset)
if self.parameters.verbatim_format:
message_parts.append('{{{\r\n%s\r\n}}}' %ubody_text)
else:
message_parts.append('%s' %ubody_text)
return message_parts
def unique_attachment_names(self, message_parts):
"""
Make sure we have unique names attachments:
- check if it contains illegal characters
- Rename "None" filenames to "untitled-part"
"""
self.logger.debug('function unique_attachment_names()')
renamed_parts = []
attachment_names = set()
for item in message_parts:
## If not an attachment, leave it alone
#
if not isinstance(item, tuple):
renamed_parts.append(item)
continue
(filename, part) = item
## If filename = None, use a default one
#
if filename in [ 'None']:
filename = 'untitled-part'
self.logger.info('\t Rename filename "None" to: %s' %filename)
## Guess the extension from the content type, use non strict mode
# some additional non-standard but commonly used MIME types
# are also recognized
#
ext = mimetypes.guess_extension(part.get_content_type(), False)
if not ext:
ext = '.bin'
filename = '%s%s' % (filename, ext)
## Discard relative paths for windows/unix in attachment names
#
filename = filename.replace('\\', '_')
filename = filename.replace('/', '_')
## remove linefeed char
#
for forbidden_char in ['\r', '\n']:
filename = filename.replace(forbidden_char,'')
## We try to normalize the filename to utf-8 NFC if we can.
# Files uploaded from OS X might be in NFD.
# Check python version and then try it
#
#if sys.version_info[0] > 2 or (sys.version_info[0] == 2 and sys.version_info[1] >= 3):
# try:
# filename = unicodedata.normalize('NFC', unicode(filename, 'utf-8')).encode('utf-8')
# except TypeError:
# pass
## Make the filename unique for this ticket
#
num = 0
unique_filename = filename
dummy_filename, ext = os.path.splitext(filename)
while (unique_filename in attachment_names) or self.attachment_exists(unique_filename):
num += 1
unique_filename = "%s-%s%s" % (dummy_filename, num, ext)
s = '\t Attachment with filename %s will be saved as %s' % (filename, unique_filename)
self.print_unicode(s)
attachment_names.add(unique_filename)
renamed_parts.append((filename, unique_filename, part))
return renamed_parts
def attachment_exists(self, filename):
self.logger.debug("function attachment_exists")
s = '\t check if attachment already exists: Id : %s, Filename : %s' %(self.id, filename)
self.print_unicode(s)
## Do we have a valid ticket id
#
if not self.id:
return False
try:
if self.system == 'discussion':
att = attachment.Attachment(self.env, 'discussion', 'ticket/%s' % (self.id,), filename)
elif self.system == 'blog':
att = attachment.Attachment(self.env, 'blog', '%s' % (self.id,), filename)
else:
att = attachment.Attachment(self.env, 'ticket', self.id, filename)
return True
except attachment.ResourceNotFound:
return False
########## TRAC Ticket Text ###########################################################
def get_body_text(self, message_parts):
"""
"""
self.logger.debug('function get_body_text()')
body_text = []
for part in message_parts:
## Plain text part, append it
#
if not isinstance(part, tuple):
body_text.extend(part.strip().splitlines())
body_text.append("")
continue
(original, filename, part) = part
inline = self.inline_part(part)
## Skip generation of attachment link if html is converted to text
#
if part.get_content_type() == 'text/html' and self.parameters.html2text_cmd and inline:
s = 'Skipping attachment link for html part: %s' %(filename)
self.print_unicode(s)
continue
if part.get_content_maintype() == 'image' and inline:
if self.system != 'discussion':
s = 'wiki image link for: %s' %(filename)
self.print_unicode(s)
body_text.append('[[Image(%s)]]' % filename)
body_text.append("")
else:
if self.system != 'discussion':
s = 'wiki attachment link for: %s' %(filename)
self.print_unicode(s)
body_text.append('[attachment:"%s"]' % filename)
body_text.append("")
## Convert list body_texts to string
#
body_text = '\r\n'.join(body_text)
return body_text
def html_mailto_link(self, subject):
"""
This function returns a HTML mailto tag with the ticket id and author email address
"""
self.logger.debug("function html_mailto_link")
if not self.author:
author = self.email_addr
else:
author = self.author
if not self.parameters.mailto_cc:
self.parameters.mailto_cc = ''
## Bug in urllib.quote function
#
if isinstance(subject, unicode):
subject = subject.encode('utf-8')
## use urllib to escape the chars
#
s = '%s?Subject=%s&cc=%s' %(
urllib.quote(self.email_addr),
urllib.quote('Re: #%s: %s' %(self.id, subject)),
urllib.quote(self.parameters.mailto_cc)
)
if self.VERSION in [ 0.10 ]:
s = '\r\n{{{\r\n#!html\r\n<a\r\n href="mailto:%s">Reply to: %s\r\n</a>\r\n}}}\r\n' %(s, author)
else:
s = '[mailto:"%s" Reply to: %s]' %(s, author)
self.logger.debug("\tmailto link %s" %s)
return s
########## TRAC notify section ###########################################################
def notify(self, tkt, new=True, modtime=0):
"""
A wrapper for the TRAC notify function. So we can use templates
"""
self.logger.debug('function notify()')
class Email2TracNotifyEmail(TicketNotifyEmail):
def __init__(self, env):
TicketNotifyEmail.__init__(self, env)
self.email2trac_notify_reporter = None
self.email2trac_replyto = None
def send(self, torcpts, ccrcpts):
#print 'Yes this works'
dest = self.reporter or 'anonymous'
hdrs = {}
hdrs['Message-ID'] = self.get_message_id(dest, self.modtime)
hdrs['X-Trac-Ticket-ID'] = str(self.ticket.id)
hdrs['X-Trac-Ticket-URL'] = self.data['ticket']['link']
if not self.newticket:
msgid = self.get_message_id(dest)
hdrs['In-Reply-To'] = msgid
hdrs['References'] = msgid
if self.email2trac_notify_reporter:
if not self.email2trac_notify_reporter in torcpts:
torcpts.append(self.email2trac_notify_reporter)
if self.email2trac_replyto:
# use to rewrite reply to
# hdrs does not work, multiple reply addresses
#hdrs['Reply-To'] = 'bas.van.der.vlies@gmail.com'
self.replyto_email = self.email2trac_replyto
NotifyEmail.send(self, torcpts, ccrcpts, hdrs)
if self.parameters.dry_run :
self.logger.info('DRY_RUN: self.notify(tkt, True) reporter = %s' %tkt['reporter'])
return
try:
## create false {abs_}href properties, to trick Notify()
#
if self.VERSION in [0.10]:
self.env.abs_href = Href(self.get_config('project', 'url'))
self.env.href = Href(self.get_config('project', 'url'))
tn = Email2TracNotifyEmail(self.env)
## additionally append sender (regardeless of settings in trac.ini)
#
if self.parameters.notify_reporter:
self.logger.debug('\t Notify reporter set')
if not self.email_header_acl('notify_reporter_black_list', self.email_addr, False):
tn.email2trac_notify_reporter = self.email_addr
if self.parameters.notify_replyto_rewrite:
self.logger.debug('\t Notify replyto rewrite set')
action, value = self.parameters.notify_replyto_rewrite.split(':')
if action in ['use_mail_domain']:
self.logger.debug('\t\t use_mail_domain:%s' %value)
tn.email2trac_replyto = '%s@%s' %(self.id, value)
elif action in ['use_trac_smtp_replyto']:
self.logger.debug('\t\t use_trac_smtp_replyto delimiter:%s' %value)
dummy = self.smtp_replyto.split('@')
if len(dummy) > 1:
tn.email2trac_replyto = '%s%s%s@%s' %(dummy[0], value, self.id, dummy[1])
else:
tn.email2trac_replyto = '%s%s%s' %(dummy[0], value, self.id)
if self.parameters.alternate_notify_template:
if self.VERSION >= 0.11:
from trac.web.chrome import Chrome
if self.parameters.alternate_notify_template_update and not new:
tn.template_name = self.parameters.alternate_notify_template_update
else:
tn.template_name = self.parameters.alternate_notify_template
tn.template = Chrome(tn.env).load_template(tn.template_name, method='text')
else:
tn.template_name = self.parameters.alternate_notify_template
tn.notify(tkt, new, modtime)
except Exception, e:
self.logger.error('Failure sending notification on creation of ticket #%s: %s' %(self.id, e))
########## END Class Definition ########################################################
########## Parse Config File ###########################################################
def ReadConfig(file, name):
"""
Parse the config file
"""
if not os.path.isfile(file):
print 'File %s does not exist' %file
sys.exit(1)
config = trac_config.Configuration(file)
parentdir = config.get('DEFAULT', 'parentdir')
sections = config.sections()
## use some trac internals to get the defaults
#
tmp = config.parser.defaults()
project = SaraDict()
for option, value in tmp.items():
try:
project[option] = int(value)
except ValueError:
project[option] = value
if name:
if name in sections:
project = SaraDict()
for option, value in config.options(name):
try:
project[option] = int(value)
except ValueError:
project[option] = value
elif not parentdir:
print "Not a valid project name: %s, valid names are: %s" %(name, sections)
print "or set parentdir in the [DEFAULT] section"
sys.exit(1)
## If parentdir then set project dir to parentdir + name
#
if not project.has_key('project'):
if not parentdir:
print "You must set project or parentdir in your configuration file"
sys.exit(1)
elif not name:
print "You must configure a project section in your configuration file"
else:
project['project'] = os.path.join(parentdir, name)
##
# Save the project name
#
project['project_name'] = os.path.basename(project['project'])
return project
########## Setup Logging ###############################################################
def setup_log(parameters, project_name, interactive=None):
"""
Setup logging
Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form
would be interpreted by the ConfigParser itself.
"""
logger = logging.getLogger('email2trac %s' %project_name)
if interactive:
parameters.log_type = 'stderr'
if not parameters.log_type:
if sys.platform in ['win32', 'cygwin']:
parameters.log_type = 'eventlog'
else:
parameters.log_type = 'syslog'
if parameters.log_type == 'file':
if not parameters.log_file:
parameters.log_file = 'email2trac.log'
if not os.path.isabs(parameters.log_file):
parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file)
log_handler = logging.FileHandler(parameters.log_file)
elif parameters.log_type in ('winlog', 'eventlog', 'nteventlog'):
## Requires win32 extensions
#
logid = "email2trac"
log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application')
elif parameters.log_type in ('syslog', 'unix'):
log_handler = logging.handlers.SysLogHandler('/dev/log')
elif parameters.log_type in ('stderr'):
log_handler = logging.StreamHandler(sys.stderr)
else:
log_handler = logging.handlers.BufferingHandler(0)
if parameters.log_format:
parameters.log_format = parameters.log_format.replace('$(', '%(')
else:
parameters.log_format = '%(name)s: %(message)s'
if parameters.log_type in ('file', 'stderr'):
parameters.log_format = '%(asctime)s ' + parameters.log_format
log_formatter = logging.Formatter(parameters.log_format)
log_handler.setFormatter(log_formatter)
logger.addHandler(log_handler)
if (parameters.log_level in ['DEBUG', 'ALL']) or (parameters.debug > 0):
logger.setLevel(logging.DEBUG)
parameters.debug = 1
elif parameters.log_level in ['INFO'] or parameters.verbose:
logger.setLevel(logging.INFO)
elif parameters.log_level in ['WARNING']:
logger.setLevel(logging.WARNING)
elif parameters.log_level in ['ERROR']:
logger.setLevel(logging.ERROR)
elif parameters.log_level in ['CRITICAL']:
logger.setLevel(logging.CRITICAL)
else:
logger.setLevel(logging.INFO)
return logger
########## Own TicketNotifyEmail class ###############################################################
if __name__ == '__main__':
## Default config file
#
agilo = False
bh_product = None
configfile = '/etc/email2trac.conf'
project = ''
component = ''
ticket_prefix = 'default'
dry_run = None
verbose = None
debug_interactive = None
virtualenv = ''
SHORT_OPT = 'AB:cdE:hf:np:t:v'
LONG_OPT = ['agilo', 'bh_product=', 'component=', 'debug',
'dry-run', 'help', 'file=', 'project=', 'ticket_prefix=',
'virtualenv=', 'verbose']
try:
opts, args = getopt.getopt(sys.argv[1:], SHORT_OPT, LONG_OPT)
except getopt.error,detail:
print __doc__
print detail
sys.exit(1)
project_name = None
for opt,value in opts:
if opt in [ '-h', '--help']:
print __doc__
sys.exit(0)
elif opt in ['-A', '--agilo']:
agilo = True
elif opt in ['-B', '--bh_product']:
bh_product = value
elif opt in ['-c', '--component']:
component = value
elif opt in ['-d', '--debug']:
debug_interactive = 1
elif opt in ['-E', '--virtualenv']:
virtualenv = value
elif opt in ['-f', '--file']:
configfile = value
elif opt in ['-n', '--dry-run']:
dry_run = True
elif opt in ['-p', '--project']:
project_name = value
elif opt in ['-t', '--ticket_prefix']:
ticket_prefix = value
elif opt in ['-v', '--verbose']:
verbose = True
if virtualenv and os.path.exists(virtualenv):
activate_this = os.path.join(virtualenv, 'bin/activate_this.py')
if os.path.exists(activate_this):
execfile(activate_this, dict(__file__=activate_this))
try:
from trac import __version__ as trac_version
from trac import config as trac_config
except ImportError, detail:
print detail
print "Can not find a a valid trac installation, solutions could be:"
print "\tset PYTHONPATH"
print "\tuse the --virtualenv <dir> option"
sys.exit(1)
settings = ReadConfig(configfile, project_name)
## The default prefix for ticket values in email2trac.conf
#
settings.ticket_prefix = ticket_prefix
settings.dry_run = dry_run
settings.verbose = verbose
if bh_product:
settings.bh_product = bh_product
if not settings.debug and debug_interactive:
settings.debug = debug_interactive
if not settings.project:
print __doc__
print 'No Trac project is defined in the email2trac config file.'
sys.exit(1)
logger = setup_log(settings, os.path.basename(settings.project), debug_interactive)
if component:
settings['component'] = component
## We are only interested in the major versions
# 0.12.3 --> 0.12
# 1.0.2 --> 1.0
#
l = trac_version.split('.')
version = '.'.join(l[0:2])
logger.debug("Found trac version: %s" %(version))
try:
if version == '0.10':
from trac import attachment
from trac.env import Environment
from trac.ticket import Ticket
from trac.web.href import Href
from trac import util
from trac.ticket.web_ui import TicketModule
#
# return util.text.to_unicode(str)
#
# see http://projects.edgewall.com/trac/changeset/2799
from trac.ticket.notification import TicketNotifyEmail
from trac import config as trac_config
from trac.core import TracError
elif version in ['0.11', '0.12', '0.13', '1.0', '1.1']:
from trac import attachment
from trac import config as trac_config
from trac import util
from trac.core import TracError
from trac.env import Environment
from trac.perm import PermissionSystem
from trac.perm import PermissionCache
from trac.test import Mock, MockPerm
from trac.ticket.api import TicketSystem
from trac.ticket.web_ui import TicketModule
from trac.web.href import Href
try:
import pkg_resources
pkg = pkg_resources.get_distribution('BloodhoundMultiProduct')
bloodhound = pkg.version.split()[:2]
except pkg_resources.DistributionNotFound:
# assume no bloodhound
bloodhound = None
if bloodhound:
from multiproduct.env import Environment, ProductEnvironment
from multiproduct.ticket.web_ui import (ProductTicketModule
as TicketModule)
logger.debug("Found Bloodhound Distribution")
if not settings.bh_product:
print __doc__
print 'No Bloodhound project defined (bh_project) in section:%s email2trac config file.' %(settings.project)
sys.exit(1)
if agilo:
try:
from agilo.ticket.model import AgiloTicket as Ticket
except ImportError, detail:
try:
from agilo.ticket.model import Ticket
except ImportError, detail:
logger.error('Could not find Trac Agilo environemnt')
sys.exit(0)
else:
from trac.ticket import Ticket
#
# return util.text.to_unicode(str)
#
# see http://projects.edgewall.com/trac/changeset/2799
from trac.ticket.notification import TicketNotifyEmail
from trac.notification import NotifyEmail
else:
logger.error('TRAC version %s is not supported' %version)
sys.exit(0)
## Must be set before environment is created
#
if settings.has_key('python_egg_cache'):
python_egg_cache = str(settings['python_egg_cache'])
os.environ['PYTHON_EGG_CACHE'] = python_egg_cache
if settings.debug > 0:
logger.debug('Loading environment %s', settings.project)
try:
env = Environment(settings['project'], create=0)
if bloodhound:
## possibly overkill testing if the multiproduct schema is a
# new enough version
#
from multiproduct.env import MultiProductSystem
mps = MultiProductSystem(env)
if mps.get_version() > 4:
try:
env = ProductEnvironment(env, settings.bh_product, create=0)
except LookupError:
logger.error('%s is not a valid Bloodhound' %settings.bh_product)
sys.exit(0)
except IOError, detail:
logger.error("trac error: %s" %detail)
sys.exit(0)
except TracError, detail:
logger.error("trac error: %s" %detail)
sys.exit(0)
tktparser = TicketEmailParser(env, settings, logger, float(version))
tktparser.parse(sys.stdin)
## Catch all errors and use the logging module
#
except Exception, error:
etype, evalue, etb = sys.exc_info()
for e in traceback.format_exception(etype, evalue, etb):
logger.critical(e)
if m:
tktparser.save_email_for_debug(m, settings.project_name, True)
sys.exit(1)
# EOB
|