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
|
#!/usr/bin/perl -w
# CLI client for the F*EX service (send, list, delete)
#
# see also: fexget
#
# Author: Ulli Horlacher <framstag@belwue.de>
#
# Perl Artistic Licence
# BEGIN { $SIG{__WARN__} = sub { die @_ } }
use 5.010;
use strict qw(vars subs);
use Encode;
use Config;
use Socket;
use IO::Handle;
use IO::Socket::INET;
use Getopt::Std;
use File::Basename;
use Term::ReadLine;
use Sys::Hostname;
use Cwd qw(abs_path getcwd);
use Fcntl qw(:flock :mode);
use Digest::MD5 qw(md5_hex);
use Time::HiRes qw(time);
# use Smart::Comments;
use constant kB => 2**10;
use constant MB => 2**20;
our $CTYPE = 'ISO-8859-1';
$ENV{LANGUAGE} = 'en';
$ENV{LC_MESSAGES} = 'C';
eval 'use Net::INET6Glue::INET_is_INET6';
eval {
local $^W = 0;
require I18N::Langinfo;
I18N::Langinfo->import(qw'langinfo CODESET');
$CTYPE = langinfo(CODESET());
};
$| = 1;
our ($SH,$fexhome,$idf,$fextmp,$useragent,$editor,$nomail);
our ($windoof,$cygwin,$macos);
our ($transferfile,$share_);
our ($anonymous,$public);
our ($frecipient);
our ($FEXID,$FEXXX,$HOME);
our (%alias);
our $version = 20200429;
our $_0 = $0;
our $prg = $0;
our $FEXOPT = $ENV{FEXOPT}||'';
our $nmtime = 0;
our $command = '';
our ($fexsend,$fexget);
our %SSL = (SSL_version => 'TLSv1_2');
my $sigpipe;
my $chunksize = 0;
my $sleepwait = 1;
my $xn = '\d{8}_\d{6}';
my ($xlist,$xp);
unless ($version) {
my @d = localtime((stat $0)[9]);
$version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
}
$version .= '_sw' if abs_path($0) =~ m:/sw/:;
&update if "@ARGV" eq 'UPDATE';
if ($Config{osname} =~ /^mswin/i) {
# http://slu.livejournal.com/17395.html
$windoof = $Config{osname};
$HOME = $ENV{USERPROFILE};
$fexhome = $ENV{FEXHOME} || $HOME.'\fex';
$fextmp = $ENV{FEXTMP} || "$fexhome\\tmp";
$idf = "$fexhome\\id";
$editor = $ENV{EDITOR} || 'notepad.exe';
$useragent = sprintf("fexsend-$version (%s %s)",
$Config{osname},$Config{archname});
$SSL{SSL_verify_mode} = 0;
} else {
$prg =~ s:(.*/):: or die "$0: cannot find path\n";
$fexsend = $1.'fexsend';
$fexget = $1.'fexget';
$0 = "$prg @ARGV";
if ($Config{osname} =~ /^darwin/i or $ENV{MACOS}) {
# https://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
$ENV{COPYFILE_DISABLE} = 1;
# http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window
$macos = $Config{osname};
$HOME = (getpwuid($<))[7]||$ENV{HOME};
$fexhome = $HOME.'/.fex';
$fextmp = $ENV{FEXTMP} || "$fexhome/tmp";
$fextmp =~ s:/$::;
$idf = "$fexhome/id";
chmod 0600,$idf;
$editor = $ENV{EDITOR} || 'open -W -n -e';
$_ = `sw_vers -productVersion 2>/dev/null`||'';
chomp;
$useragent = "fexsend-$version (MacOS $_)";
} else {
$HOME = (getpwuid($<))[7]||$ENV{HOME};
$fexhome = $HOME.'/.fex';
$fextmp = $ENV{FEXTMP} || "$fexhome/tmp";
$idf = "$fexhome/id";
chmod 0600,$idf;
$editor = $ENV{EDITOR} || 'vi';
my ($os,$osp,$osn,$osv);
if (open my $osr,'/etc/os-release') {
while (<$osr>) {
$osp = $1 if /^PRETTY_NAME="(.+)"/;
$osn = $1 if /^NAME="(.+)"/;
$osv = $1 if /^VERSION="(.+)"/;
}
close $osr;
}
if ($osp and $osp =~ /\d/) {
$os = $osp;
} elsif ($osn and $osv) {
$os = "$osn $osv";
} else {
$os = `(lsb_release -d||uname -a)2>/dev/null`||'';
chomp $os;
$os =~ s/^Description:\s+//;
}
$useragent = "fexsend-$version ($os)";
}
}
if ($Config{osname} eq 'cygwin' or $ENV{CYGWIN}) {
# https://cygwin.com/faq/faq.html#faq.using.fixing-fork-failures
$cygwin = 1;
$SSL{SSL_verify_mode} = 0;
}
if (-f ($_ = '/etc/fex/config.pl')) {
eval { require } or warn $@;
}
if (my $fua = $ENV{FUA}) {
if ($useragent =~ /(\S+)/ and $fua !~ /\Q$1/) {
$useragent = "$fua/$useragent";
} else {
$useragent = $fua;
}
}
$ENV{FUA} = $useragent;
$ENV{FUA} =~ s/ .*//;
# force tar format (SUSE uses pax!)
$ENV{TAR_OPTIONS} .= ' --format=gnu';
my $from = '';
my $to = '';
my $id = '';
my $skey = '';
my $gkey = '';
my $okey = '';
my $pkey = '';
my $share = '';
my $atype = ''; # archive type
my $fexcgi; # F*EX CGI URL
my @files; # files to send
my @ifiles; # files to send from index file
my %AB = (); # server based address book
my ($server,$port,$sid,$https);
my $proxy = '';
my $proxy_prefix = '';
my $proxy_authorization = '';
my $features = '';
my $timeout = 30; # server timeout
my $fexlist = "$fextmp/fexlist";
my ($usage,$hints);
my $xx = $prg =~ /\bxx$/;
my $xxx = $prg =~ /\bxxx$/;
if ($xx) {
$usage = "
usage:
send file(s): xx FILE...
send all files including .*: xx ./DIRECTORY
send STDIN: xx -
send pipe: ... | xx
get file(s) or STDIN: xx
get file(s) no-questions: xx --
options:
-v verbose mode
-m KBS limit throughput (KBS kB/s)
-p SIZE:WAIT send file in chunks (SIZE MB, WAIT seconds)
examples:
dmesg | xx
xx project
xx --
";
} elsif ($xxx) {
$usage = "
$prg: internet clipboard with versioning
file upload: $prg [-m LIMIT] [-k DAYS] [-w] [-q] [-p] [-z] [-Z] FILE...
pipe upload: ... | $prg [-m LIMIT] [-k DAYS] [-w] [-q] [-t] [COMMENT]
download: $prg [-m LIMIT] [-o] [DATE_TIME|URL|-]
delete: $prg -d [DATE_TIME|URL|REGEXP|-]
other: $prg [-l] [-w] [-u] [-x] [-X] [-k DAYS DATE_TIME|URL]
options:
-m limit throughput (kB/s)
-k keep DAYS (default: 1 day)
-w show wget download command
-q quiet mode: no transfer status info
-p add path for single file argument
-z force compression
-0 force no compression
-Z use zip
-T use tar (default for UNIX)
-t text mode (URL content displayble by webbrowser)
-o overwrite existing files
-d delete
-l list
-u generate upload function to be used by other users
-x show xxx propagation shell code (with your F*EX ID!)
-X same as -x, but generate URL shell command
upload arguments:
.* files are default excluded, to include use .FILE or ./DIRECTORY
download or delete argument:
- last upload (in overwrite mode for download)
examples:
$prg *jpg # upload all jpg files
$prg -m 1000 -k 5 . # upload current directory with 1000 kB/s and keep 5 days
ls -l | $prg -t # upload directory listing in text format
$prg -d - # delete last upload
$prg # interactive usage: download, list, delete
see also: fexsend, fexget, fexpack, fexstore, fexsync
";
$hints = '
xxx is the extension of the F*EX clipboard xx with versioning.
With xxx you can store files or directories or pipe data of any size.
Files or directories beginning with a "." will not be transfered unless you use
an argument with such a name.
".del" and ".snapshot*" are always excluded!
The default keep time is 1 day, then the upload will expire.
If you need a longer keep time, then use option -k
For verbose mode you can use option -v
xxx autodetermines whether compression is suggestive by using container taz,
see: fexsend -H
You can use different F*EX accounts with fexsend and xxx:
xxx uses the [xx] F*EX id (env $FEXXX) if it is there, otherwise the regular
F*EX id (env $FEXID).
';
} else {
$usage = "
usage:
$prg [options] FILE[...] [@] RECIPIENT[,...]
$prg [special option]
$prg -l ['RECIPIENT-REGEXP']
$prg -f NUMBER RECIPIENT[,...]
$prg -x NUMBER [-C -k -D -K]
options:
-v verbose mode
-a ARCHIVE put files in archive (.zip .7z .tar .tgz) without .* files
-A ARCHIVE put files in archive (.zip .7z .tar .tgz) with .* files
-# FILELIST exclude files (# is list separator) from archive
-0 do not compress files for .zip and .7z archives
-d delete file on fex server
-o overwrite mode, do not try to resume
-m KBS limit throughput (KBS kB/s)
-p SIZE:WAIT send file in chunks (SIZE MB, WAIT seconds)
-i ACCOUNT use ID data [ACCOUNT] from ID file
-C 'COMMENT' add COMMENT to notification email
-k MAX keep file MAX days on fex server
-D delay auto-delete after download
-K no auto-delete after download
-M MIME-file (to be displayed in recipient's webbrowser)
-s STREAM read data from pipe and upload it with STREAM name
-r ADDRESS use Reply-To ADDRESS in notification email
-n send no notification email, just show the download URL
-/ do not upload the file, but tell the server to link it
-= ALIAS use ALIAS as filename
-^ add date_time to filename
-q quiet mode
-+ undocumented feature - test it :-)
special options:
-I initialize ID file or show ID
-I ACCOUNT add alternate ID data (secondary logins) to ID file
-l list sent files (numbers needed for -f -x -d -N)
-u list download URLs of sent files (show NUMBERs)
-f NUMBER forward already uploaded file to another recipient
-x NUMBER use -C -k -D -K for already uploaded file
-d NUMBER delete file on fex server
-N NUMBER resend notification email
-Q check quotas
-U generate upload URL with bash function (-U+ for archives)
-T UP:DOWN test internet speed with upload and download MBs
-@ edit server address book (aliases)
-S show server/user settings and auth-ID
-V show version and installation/upgrade hint
-H show hints and examples and more options
examples:
$prg visualization.mp4 framstag\@rus.uni-stuttgart.de
$prg -a images.zip *.jpg webmaster\@belwue.de,metoo
lshw | $prg -s hardware.list admin\@belwue.de
";
# usage: $prg -R FEX-URL email
# -R FEX mail self-register your email address at FEX server
$hints = '
fexsend hints and more options:
usage: fexsend [options] file recipient(s)
Recipient can be a comma separated address list. Example:
fexsend big.file framstag@rus.uni-stuttgart.de,webmaster@belwue.de
Recipient can be an alias from your server address book
(use "fexsend -@" to edit it). Example:
fexsend big.file framstag
Recipient can be a SKEY URL, which you have received from a regular F*EX user.
When using this URL you are a subuser of this full user and the file will be
sent to him. Example:
fexsend big.file http://fex.rus.uni-stuttgart.de/fup?skey=4285f8cdd881626524fba686d5f0a83a
Recipient can be a GKEY URL, which you have received from a regular F*EX user.
Using this URL you are a member of his group and the file will be sent to all
members of this group. Example:
fexsend big.file http://fex.rus.uni-stuttgart.de/fup?gkey=50d26547b1e8c1110beb8748fc1d9444
Recipient can be a OKEY URL, which you have received from a regular F*EX user.
Using this URL you can send exactly one file. Example:
fexsend big.file http://fex.rus.uni-stuttgart.de/fup?to=framstag@rus.uni-stuttgart.de?okey=HZ6UEkCQ
When you use "FEX-URL/anonymous" as recipient and your F*EX administrator has
allowed anonymous upload for your IP address then no auth-ID is needed.
"." as recipient means fex to yourself and show the download URL after upload
without sending a notification email. Example:
fexsend software.tar .
"+" as recipient means fex to yourself and show the download URL immediatelly.
With this URL the file can be downloaded while the upload is still in progress!
Example:
fexsend big_VM.tar +
"//" as recipient means fex to yourself and create extra short download URL.
Example:
fexsend software.tar //
If you want a Bcc of the notification email then add \'!bcc!\' to the comment:
fexsend -C \'!bcc! for me and you\' ...
To manage your subuser and groups or forward or redirect files, use a
webbrowser with the URL from "fexsend -U", e.g.: firefox $(fexsend -U)
If you want to copy-forward an already uploaded file to another recipient,
then you first have to query the file number with:
fexsend -l
and then copy-forward it with:
fexsend -b # other@address
Where # is the file number.
You can list an uploaded file in more detail with
fexsend -l #
Where # is the file number.
If you want to modify the keep time, comment or auto-delete behaviour of an
already uploaded file then you first have to query the file number with:
fexsend -l
and then for example set the keep time to 30 days with:
fexsend -x # -k 30
Where # is the file number.
To list files sent only to yourself, use:
fexsend -l .
To list files sent to others, but yourself, use:
fexsend -l -
To show the content of your id file, use:
fexsend -I -
With option -. the server sends a short instead of a detailed notification
email to the recipient.
With option -a (excludes .* files) or -A (includes .* files) you can send
several files or whole directories within a single archive file.
".del" and ".snapshot*" are always excluded!
To keep the full path inside the archive, add a trailing / to the directory
name, example: fexsend -a example.tar keep/this/directory/path/ .
With option -O USER you can generate an onetime upload URL
(USER must be an email address).
Option -# uses shell pattern to exclude files.
The archive types tar and tgz are build on-the-fly (streaming) whereas archive
types zip and 7z need a temporary archive file in $HOME/.fex/tmp/ (disk space!)
When you use the pseudo archive type taz, then fexsend will determine whether
compression is suggestive by inspecting all file names and then uses either
tar or tgz archive. Suggestive means: total size must be less than 2 GB and at
least 20% must be compressable.
With option -s you can send any data coming from a pipe (STDIN) as a file
without wasting local disc space.
With option -p SIZE:WAIT you can send your file in chunks.
This may be necessary if you have a firewall/gateway with a tcp transfer
limitation (e.g. Arbor). The option parameter SIZE:WAIT specifies the chunk
size in MB and the time to wait between the chunks in seconds. Examples:
fexsend -p 100 data.tar someone@some.where # 100 MB chunks
fexsend -p 10:3 data.tar someone@some.where # 10 MB chunks with 3 s wait time
With option -X you can specify any URL parameter, e.g.:
fexsend -X autodelete=yes ...
fexsend -X \'autodelete=no&locale=german\' ...
With option -F you activate female mode.
For HTTPS you can set the environment variables:
SSLVERIFY=1 # activate server identity verification
SSLVERSION=TLSv1_2 # this is the default
SSLCAPATH=/etc/ssl/certs # path to trusted (root) certificates
SSLCAFILE=/etc/ssl/cert.pem # file with trusted (root) certificates
SSLCIPHERLIST=HIGH:!3DES # see "man ciphers" from openssl
Partner programs:
fexget is for downloading. See: fexget -h
fexpush is for archive sharing. See: fexpush -h
fexpull is for archive sharing. See: fexpull -h
xx is an internet clipboard. See: xx -h
xxx is an internet clipboard with versioning. See: xxx -h
fexpack is a named internet clipboard. See: fexpack -h
fexsend stores the login data (server, user and auth-ID) in the file
$HOME/.fex/id
The format of this file is ([data] is optional):
server-URL[!proxy[:port[:chunk-size]]
email-address
auth-ID
For temporary usage of a HTTP proxy use:
fexsend -P your_proxy:port:chunksize_in_MB file recipient
Example:
fexsend -P wwwproxy.uni-stuttgart.de.de:8080:1024 4GB.tar .
For temporary usage of an alternative F*EX server or user use:
FEXID="FEXSERVER USER AUTHID" fexsend file recipient
Example:
FEXID="fex.belwue.de gaga@belwue.de blubb" fexsend big.file framstag@belwue.de
When using http (and not https), then your auth-ID will be sent encrypted with
a one-time session ID, offered by the F*EX server. This will no happen, when
you use an http proxy, because most proxys do not support connection keep alive.
You can define aliases (and optional fexsend options) in $HOME/.fex/config.pl:
%alias = (
\'alias1\' => \'user1@domain1.org\',
\'alias2\' => \'user2@domain2.org\',
\'both\' => \'user1@domain1.org,user2@domain2.org\',
\'extra\' => \'extra@special.net:-i other -K -k 30\',
);
fexsend also respects aliases in $HOME/.mutt/aliases
The alias priority is (descending):
$HOME/.fex/config.pl
$HOME/.mutt/aliases
fexserver address book
In $HOME/.fex/config.pl you can also set the SSL* environment variables and the
$opt_* variables, e.g.:
$ENV{SSLVERSION} = \'TLSv1_2\';
${\'opt_+\'} = 1; # equivalent to option -+
$opt_0 = 1; # equivalent to option -0
$opt_m = 200; # equivalent to option -m 200
';
}
$usage =~ s/^\n//;
my @rcamel = (
'[A
_ _ c*_)
/ \/ \//
*=( __ /
\\\\/\\\\/
',
"[A \\\\/\\\\/ \n",
"[A //\\\\//\\\\\n"
);
my @rrcamel = (
'[A
(_*p _ _
\\\\/ \/ \\
\ __ )=*
//\\\\//\\\\
',
"[A \\\\/\\\\/ \n",
"[A //\\\\//\\\\\n"
);
autoflush STDOUT;
autoflush STDERR;
if ($windoof and not @ARGV and not $ENV{PROMPT}) {
# restart with cmd.exe to have mouse cut+paste
exec qw'cmd /k',$prg,'-W';
exit;
}
unless (-d $fexhome) {
mkdir $fexhome or die "$prg: cannot create $fexhome - $!\n";
chmod 0700,$fexhome;
}
unless (-d $fextmp) {
mkdir $fextmp or die "$prg: cannot create $fextmp - $!\n";
chmod 0700,$fextmp;
}
if (-d $fextmp and my @s = stat($fextmp)) {
if ($< ne $s[4]) {
die "$prg: not owner of $fextmp\n";
}
my $yt = time-24*60*60;
foreach my $tmp (glob "$fextmp/*") {
if (@s = stat $tmp) {
unlink $tmp if $yt > $s[9];
}
}
} else {
die "$prg: $fextmp is not a directory\n";
}
my @_ARGV = @ARGV; # save arguments
our ($opt_q,$opt_h,$opt_H,$opt_v,$opt_m,$opt_c,$opt_k,$opt_d,$opt_l,$opt_I,
$opt_K,$opt_D,$opt_u,$opt_f,$opt_a,$opt_C,$opt_R,$opt_M,$opt_L,$opt_Q,
$opt_A,$opt_i,$opt_z,$opt_Z,$opt_b,$opt_P,$opt_x,$opt_X,$opt_V,$opt_U,
$opt_s,$opt_o,$opt_g,$opt_F,$opt_n,$opt_r,$opt_S,$opt_N,$opt_T,$opt_O,
$opt_p,$opt_t,$opt_w,$opt_0);
if ($xx) {
$opt_q = 1 if @ARGV and $ARGV[-1] eq '--' and pop @ARGV or not -t STDOUT;
$opt_h = $opt_v = $opt_I = 0;
$opt_X = $opt_p = $opt_m = $opt_i = '';
$_ = "$fexhome/config.pl"; require if -f;
getopts('hvI.m:p:i:') or die $usage;
} elsif ($xxx) {
$opt_h = $opt_H = $opt_v = $opt_g = $opt_d = $opt_I = $opt_u = 0;
$opt_l = $opt_L = $opt_x = $opt_X = $opt_o = $opt_t = $opt_w = 0;
$opt_k = $opt_z = $opt_Z = $opt_T = $opt_q = $opt_p = $opt_0 = 0;
$opt_m = '';
getopts('hHvlLdIuxXopzZTtwq.0m:k:') or die $usage;
if ($opt_g+$opt_d+$opt_u+$opt_l+$opt_L+$opt_x+$opt_X > 1) {
die "$prg: you cannot mix options -d -g -u -l -x -X\n";
}
$opt_x ||= $opt_X;
@ARGV = ('-') unless -t STDOUT or @ARGV;
$opt_g = 1 if -t STDIN and not @ARGV and
not $opt_l||$opt_L||$opt_u||$opt_x||$opt_d||$opt_I;
if ($opt_w) {
$ENV{opt_w} = $opt_w;
$opt_l = $opt_w if -t STDIN and not @ARGV;
}
} else {
if ($macos and not @ARGV) {
&ask_file;
}
$opt_h = $opt_v = $opt_m = $opt_c = $opt_d = $opt_l = $opt_I = 0;
$opt_H = $opt_K = $opt_D = $opt_R = $opt_M = $opt_L = $opt_Q = $opt_0 = 0;
$opt_x = $opt_o = $opt_g = $opt_V = $opt_U = $opt_F = $opt_n = $opt_q = 0;
$opt_S = $opt_N = $opt_u = 0;
${'opt_@'} = ${'opt_!'} = ${'opt_+'} = ${'opt_.'} = ${'opt_/'} = 0;
${'opt_^'} = 0;
${'opt_='} = ${'opt_~'} = ${'opt_#'} = ${'opt_?'} = '';
$opt_k = $opt_f = $opt_C = $opt_i = $opt_b = $opt_P = $opt_X = '';
$opt_a = $opt_A = $opt_s = $opt_r = $opt_T = $opt_p = $opt_O = '';
$_ = "$fexhome/config.pl"; require if -f;
unshift @ARGV,split if $_ = $ENV{FEXSEND_OPT};
getopts('hHvdonVDKlILuURWMFzZqQS@0!+./^?~:r:m:k:f:a:A:s:C:i:b:O:p:P:x:X:N:T:=:#:')
or die $usage;
if ($opt_V) {
$useragent =~ /fexsend-(.+)/;
print "Version: $1\n";
if ("@ARGV" ne '.' and abs_path($_0) !~ m:/sw/:) {
print "Upgrade: wget fex.belwue.de/download/fex.pl && perl fex.pl\n";
}
exit;
if (not @ARGV and -w $_0) {
print "Update fexsend? "; # ex "upgrade fexsend"
$_ = <STDIN>||'';
if (/^y/i) {
my $url = 'http://fex.belwue.de/download/fexsend';
warn "$prg: downloading $url\n";
my $new = `wget -qO- $url`;
if ($new !~ /update fexsend/) { die "fexsend: bad update\n" }
if ($new =~ /version = (\d+)/ and $1 le $version) {
warn "$prg: version $version is uptodate\n";
exit;
}
$_0 = abs_path($_0);
system "vv -vs '$_0' 2>/dev/null || rsync -av '$_0' '$_0~'";
exit $? if $?;
open $_0,'>',$_0 or die "$prg: cannot write $_0 - $!\n";
print {$_0} $new;
close $_0;
exec $_0,qw'-V .';
}
}
exit;
}
if ($opt_K and $opt_D) {
die "$prg: you cannot use both options -D and -K\n";
}
if ($opt_a and $opt_A) {
die "$prg: you cannot use both options -a and -A\n";
}
$opt_a = $opt_A if $opt_A;
$opt_l = $opt_u if $opt_u;
if ($opt_a and $opt_c) {
$opt_a =~ s/\.tar$/.tgz/;
}
if ($opt_a and $opt_s) {
die "$prg: you cannot use both options -a and -s\n";
}
if ($opt_g and $opt_c) {
$opt_c = 0;
}
if ($opt_A) {
$xlist = ${'opt_#'} . '#.del#.snapshot*#.fex/tmp';
} else {
$xlist = ${'opt_#'} . '#.*';
}
$xlist =~ s/^#//;
$xp = $xlist;
$xp =~ s/\./\\./g;
$xp =~ s/\+/\\+/g;
$xp =~ s/\?/./g;
$xp =~ s/\*/.*/g;
$xp =~ s/\#/|/g;
# $xp = "#|$xp";
$opt_f ||= $opt_b;
if ($opt_f and $opt_f !~ /^\d+$/) {
die "$prg: option -f needs a number, see $prg -l\n";
}
if ($opt_I and $opt_R) {
die "$prg: you cannot use both options -I and -R\n";
}
$command =
($opt_d) ? 'DELETE':
($opt_l or $opt_L) ? 'LIST':
($opt_Q) ? 'CHECKQUOTA':
($opt_S) ? 'LISTSETTINGS':
($opt_Z) ? 'RECEIVEDLOG':
($opt_z) ? 'SENDLOG':
(${'opt_!'}) ? 'FOPLOG':
'';
$opt_D =
($opt_D) ? 'DELAY':
($opt_K) ? 'NO':
$opt_D;
}
die $usage if $opt_m and $opt_m !~ /^\d+/;
if ($opt_h) {
female_mode("show help?") if $opt_F;
print $usage;
exit;
}
if ($opt_H) {
if (0 and -t STDOUT) {
exec "$prg -H |".($ENV{PAGER}||'less');
} else {
print $hints;
}
exit;
}
if ($opt_R) {
®ister;
exit;
}
if ($opt_I) {
if ($xx or $xxx) { &show_id }
else { &init_id }
exit;
}
if ($xx or $xxx) {
# alternativ ID?
if ($opt_i) {
$proxy = $proxy_prefix = '';
open $idf,$idf or die "$prg: cannot open $idf - $!\n";
while (<$idf>) {
if (/^\[$opt_i\]/) {
get_id($idf);
last;
}
}
close $idf;
die "$prg: no [$opt_i] in $idf\n" unless $_;
} elsif ($FEXXX = $ENV{FEXXX}) {
$FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
($fexcgi,$from,$id) = split(/\s+/,$FEXXX);
warn "using ID from \$FEXXX\n" if $opt_v;
} elsif (open $idf,$idf) {
while (<$idf>) {
if (/^\[xx\]/) {
$proxy = $proxy_prefix = '';
get_id($idf);
warn "using ID from $idf [xx]\n" if $opt_v;
last;
}
}
close $idf;
}
unless ($id) {
if ($FEXID = $ENV{FEXID}) {
$FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
($fexcgi,$from,$id) = split(/\s+/,$FEXID);
warn "using ID from \$FEXID\n" if $opt_v;
} else {
if (open $idf,$idf) {
get_id($idf);
close $idf;
warn "using ID from $idf\n" if $opt_v;
}
}
}
unless ($id) {
die "$prg: no $idf or \$FEXID or \$FEXXX available\n";
}
} else {
# alternativ ID?
if ($opt_i) {
$proxy = $proxy_prefix = '';
open $idf,$idf or die "$prg: cannot open $idf - $!\n";
while (<$idf>) {
if (/^\[$opt_i\]/) {
get_id($idf);
last;
}
}
close $idf;
die "$prg: no [$opt_i] in $idf\n" unless $_;
} else {
if ($ENV{FEXXX} and not $ENV{FEXID} and not -f $idf) {
$ENV{FEXID} = $ENV{FEXXX};
}
if ($FEXID = $ENV{FEXID}) {
$FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
($fexcgi,$from,$id) = split(/\s+/,$FEXID);
die "$prg: no id in \$FEXID\n" unless $id;
$id =~ s/^PKEY:// and $pkey = $id;
warn "using ID from \$FEXID\n" if $opt_v;
} else {
if ($windoof and not -f $idf) { &init_id }
if (open $idf,$idf) {
get_id($idf);
close $idf;
warn "using ID from $idf\n" if $opt_v;
}
}
}
}
if (0 and not $proxy and $proxy = $ENV{http_proxy}) {
if ($proxy =~ s{(http://)(\S+:\S+)@}{$1}) {
$proxy_authorization = encode_b64($2);
}
if ($proxy =~ m{(http://\S+)(:(\d+))?}) {
$proxy_prefix = $1.($2||'');
}
}
if ($xxx) {
&xxx;
exit;
}
if ($opt_P) {
if ($opt_P =~ /^([\w.-]+:\d+)(:(\d+))?/) {
$proxy = $1;
$chunksize = $3 || 0;
} else {
die "$prg: proxy must be: SERVER:PORT\n";
}
} elsif ($opt_p =~ /^(\d+)$/) {
$chunksize = $1;
$sleepwait = 1;
} elsif ($opt_p =~ /^(\d+):(\d+)$/) {
$chunksize = $1;
$sleepwait = $2;
}
$chunksize *= MB;
if ($opt_T) {
my ($up,$down);
$usage = "usage: $prg -T MB_up[:MB_down] [fexserver]\n";
if ($opt_T =~ /^(\d+)$/) {
$up = $down = $1;
} elsif ($opt_T =~ /^(\d+):(\d+)$/) {
$up = $1;
$down = $2;
} else {
die $usage;
}
if (@ARGV) {
nettest($ARGV[0],$up,$down);
} elsif ($fexcgi) {
nettest($fexcgi,$up,$down);
} else {
nettest('fex.belwue.de',$up,$down);
}
exit;
}
if (@ARGV > 1 and $ARGV[-1] =~ /(^|\/)anonymous/) {
$fexcgi = $1 if $ARGV[-1] =~ s:(.+)/::;
die "usage: $prg [options] file FEXSERVER/anonymous\n" unless $fexcgi;
$anonymous = $from = 'anonymous';
$sid = $id = 'ANONYMOUS';
} elsif (@ARGV > 1 and $id eq 'PUBLIC') {
$public = $sid = $id;
} elsif (@ARGV > 1 and $ARGV[-1] =~ m{^(https?://[\w.-]+(:\d+)?/fup.*\?[sgo]key=\w+)}) {
$fexcgi = $1;
if ($fexcgi =~ /skey=(\w+)/) {
$skey = $1;
if ($command) {
die "$prg: command $command not allowed for subuser\n";
}
}
if ($fexcgi =~ /gkey=(\w+)/) {
$gkey = $1;
if ($command) {
die "$prg: command $command not allowed for groupuser\n";
}
}
if ($fexcgi =~ /okey=(\w+)/) {
$okey = $1;
if ($command) {
die "$prg: command $command not allowed for onetime user\n";
}
}
} else {
if ($ENV{FEXSERVER} and not $ENV{GATEWAY_INTERFACE} and not $opt_i) {
$fexcgi = $ENV{FEXSERVER};
}
if (not -e $idf and not ($fexcgi and $from and $id)) {
die "$prg: no ID file $idf found, use \"fexsend -I\" to create it\n";
}
unless ($fexcgi) {
die "$prg: no FEX URL found, use \"$prg -u URL\" or \"$prg -I\"\n";
}
unless ($from and $id) {
die "$prg: no sender found, use \"$prg -f FROM:ID\" or \"$prg -I\"\n";
}
if ($fexcgi !~ /^http/) {
if ($fexcgi =~ /:443/) { $fexcgi = "https://$fexcgi" }
else { $fexcgi = "http://$fexcgi" }
}
}
$server = $fexcgi;
$port = 80;
$port = 443 if $server =~ s{https://}{};
$port = $1 if $server =~ s/:(\d+)//;
if ($port == 443) {
# $opt_s and die "$prg: cannot use -s with https due to stunnel bug\n";
# $opt_g and die "$prg: cannot use -g with https due to stunnel bug\n";
$https = $port;
}
$server =~ s{http://}{};
$server =~ s{/.*}{};
if ($proxy) {
if ($port == 80) { $proxy_prefix = "http://$server" }
elsif ($port != 443) { $proxy_prefix = "http://$server:$port" }
}
# xx: special file exchange between own accounts
if ($xx) {
if ($opt_v) {
print "server: $server:$port\n";
print "user: $from\n";
# printf "FEXXX=%s\n",encode_b64($FEXXX);
}
$transferfile = "$fextmp/STDFEX";
open my $lock,'>>',$transferfile
or die "$prg: cannot write $transferfile - $!\n";
flock($lock,LOCK_EX|LOCK_NB)
or die "$prg: $transferfile is locked by another process\n";
truncate $transferfile,0;
if (not @ARGV and -t) {
&get_xx($transferfile);
} else {
&send_xx($transferfile);
}
exit;
}
# regular fexsend
if (${'opt_?'} or
($windoof or $cygwin) and not @ARGV and $useragent !~ /^\w+\// and not
($opt_l or $opt_L or $opt_Q or $opt_A or $opt_U or $opt_I or
$opt_f or $opt_x or $opt_N or ${'opt_~'}))
{ &inquire }
unless (length $opt_C) {
$opt_C = $ENV{FEXCOMMENT}||'';
}
if (${'opt_.'}) {
$opt_C = "!SHORTMAIL! $opt_C";
}
if ($opt_n or $opt_C =~ /^(NOMAIL|!#!)/) {
$opt_C = $nomail = 'NOMAIL';
}
if ((not ($opt_q||$skey||$gkey||$okey||$anonymous) and ($opt_i||${'opt_/'}
or $ENV{FEXID}) and $useragent !~ m:/:))
{ print "server/user: $fexcgi/$from\n" }
my $A = "@ARGV";
if ($opt_f) { &forward }
elsif ($opt_x) { &modify }
elsif ($opt_N) { &renotify }
elsif ($opt_Q) { &query_quotas }
elsif ($opt_S) { &query_settings }
elsif ($opt_l or $opt_L) { &list }
elsif ($opt_U) { command('GENUKEY',@ARGV) }
elsif ($opt_O) { command("GENOKEY:$opt_O") }
elsif ($opt_z or $opt_Z or ${'opt_!'}) { &get_log }
elsif (${'opt_@'}) { edit_address_book($from) }
elsif (${'opt_~'}) { command(${'opt_~'}) }
elsif ($opt_d and $anonymous) { &purge }
elsif ($opt_d and $A =~ /^[\d ]+$/) { &delete_file_number }
elsif ($opt_d and $A =~ m:^http.*/fop/:) { &delete_file_url }
elsif ($opt_V||$opt_v and not @ARGV) { exit }
else { &send_fex }
exit;
# initialize ID file or show ID
sub init_id {
my $tag;
my $proxy = '';
if ($opt_I) {
$tag = shift @ARGV;
die $usage if @ARGV;
}
$fexcgi = $from = $id = '';
unless (-d $fexhome) {
mkdir $fexhome or die "$prg: cannot create FEXHOME $fexhome - $!\n";
chmod 0700,$fexhome;
}
# show ID
if (not $tag and open $idf,$idf) {
if ($opt_i) {
while (<$idf>) {
last if /^\[$opt_i\]/;
}
}
$fexcgi = <$idf>;
$from = <$idf>;
$id = <$idf>;
close $idf;
if ($id) {
chomp($fexcgi,$from,$id);
$FEXID = encode_b64("$fexcgi $from $id");
if (-t STDIN) {
print "# server: $fexcgi\n";
print "# user: $from\n";
print "# hint: to edit the ID file \$HOME/.fex/id use \"$prg -I .\" #\n";
print "export FEXID=$FEXID;history -d \$((HISTCMD-1))\n";
} else {
print "FEXID=$FEXID\n";
}
exit;
} else {
die "$prg: no ID data found\n";
}
}
if ($tag) {
if ($tag eq '.') {
exec $ENV{EDITOR}||'vi',$idf;
}
if ($tag eq '-') {
open $idf,$idf or die "$prg: cannot open $idf - $!\n";
print while <$idf>;
exit;
}
if ($tag =~ /^FEXID=(\S+)$/) {
$FEXID = $1;
if (-f $idf) {
die "$prg: $idf does already exist\n".
"$prg: use \"$prg -I .\" to edit it\n";
}
($fexcgi,$from,$id) = split(/\s+/,decode_b64($FEXID));
unless ($id) {
die "$prg: $FEXID is not a valid FEXID\n";
}
if (open $idf,'>',$idf) {
print {$idf} "$fexcgi\n",
"$from\n",
"$id\n";
close $idf;
print "data written to $idf\n";
exit;
} else {
die "$prg: cannot write to $idf - $!\n";
}
}
}
if ($tag) { print "F*EX server URL for [$tag]: " }
else { print "F*EX server URL: " }
$fexcgi = <STDIN>;
$fexcgi =~ s/[\s\n]//g;
die "you MUST provide a FEX-URL!\n" unless $fexcgi;
if ($fexcgi =~ /\?/) {
$from = $1 if $fexcgi =~ /\bfrom=(.+?)(&|$)/i;
$id = $1 if $fexcgi =~ /\bid=(.+?)(&|$)/i;
# $skey = $1 if $fexcgi =~ /\bskey=(.+?)(&|$)/i;
# $gkey = $1 if $fexcgi =~ /\bgkey=(.+?)(&|$)/i;
die "$prg: cannot use GKEY URL in ID file\n" if $fexcgi =~ /gkey=/i;
die "$prg: cannot use SKEY URL in ID file\n" if $fexcgi =~ /skey=/i;
$fexcgi =~ s/\?.*//;
}
unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
die "\"$fexcgi\" is not a legal FEX-URL!\n";
}
$fexcgi =~ s:/fup/*$::;
print "proxy address (hostname:port or empty if none): ";
$proxy = <STDIN>;
$proxy =~ s/[\s\n]//g;
if ($proxy =~ /^[\w.-]+:\d+$/) {
$proxy = "!$proxy";
} elsif ($proxy =~ /\S/) {
die "wrong proxy address format\n";
} else {
$proxy = "";
}
if ($proxy) {
print "proxy POST limit in MB (use 2048 if unknown): ";
$_ = <STDIN>;
if (/(\d+)/) {
$proxy .= "[$1]";
}
}
if ($skey) {
$from = 'SUBUSER';
$id = $skey;
} elsif ($gkey) {
$from = 'GROUPMEMBER';
$id = $gkey;
} elsif ($okey) {
$from = 'ONETIMEUSER';
$id = $okey;
} else {
unless ($from) {
print "Your email address as registered at $fexcgi: ";
$from = <STDIN>;
$from =~ s/[\s\n]//g;
die "you MUST provide your email address!\n" unless $from;
}
unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
die "\"$from\" is not a legal email address!\n";
}
unless ($id) {
print "Your auth-ID for $from at $fexcgi: ";
$id = <STDIN>;
$id =~ s/[\s\n]//g;
die "you MUST provide your ID!\n" unless $id;
}
}
if (open $idf,'>>',$idf) {
print {$idf} "\n[$tag]\n" if $tag and -s $idf;
print {$idf} "$fexcgi$proxy\n",
"$from\n",
"$id\n";
close $idf;
print "data written to $idf\n";
} else {
die "$prg: cannot write to $idf - $!\n";
}
}
sub show_id {
my ($fexcgi,$from,$id);
if (open $idf,$idf) {
$fexcgi = <$idf>;
# $fexcgi = <$idf> if $fexcgi =~ /^\[.+\]/;
$from = <$idf>;
$id = <$idf>;
while (<$idf>) {
if (/^\[xx\]/) {
$fexcgi = <$idf>;
$from = <$idf>;
$id = <$idf>;
}
}
close $idf;
die "$prg: too few data in $idf" unless defined $id;
chomp($fexcgi);
chomp($from);
chomp($id);
$FEXXX = encode_b64("$fexcgi $from $id");
if (-t STDIN) {
print "# server: $fexcgi\n";
print "# user: $from\n";
print "export FEXXX=$FEXXX;history -d \$((HISTCMD-1))\n";
} else {
print "FEXXX=$FEXXX\n";
}
} else {
die "$prg: cannot read $idf - $!\n";
}
}
sub register {
my $fs = shift @ARGV or die $usage;
my $mail = shift @ARGV or die $usage;
my $port;
my ($server,$user,$id);
die "$prg: $idf does already exist\n" if -e $idf;
if ($fs =~ /^https/) {
die "$prg: cannot handle https at this time\n";
}
$fs =~ s{^http://}{};
$fs =~ s{/.*}{};
if ($fs =~ s/:(\d+)//) { $port = $1 }
else { $port = 80 }
tcpconnect($fs,$port);
sendheader("$fs:$port","GET $proxy_prefix/fur?user=$mail&verify=no HTTP/1.1");
http_response();
# header
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^\s*$/;
}
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
if (m{http://(.*)/fup\?from=(.+)&ID=(.+)}) {
$server = $1;
$user = $2;
$id = $3;
if (open F,">$idf") {
print F "$server\n",
"$user\n",
"$id\n";
close F;
chmod 0600,$idf;
print "user data written to $idf\n";
print "you can now fex!\n";
exit;
} else {
die "$prg: cannot write to $idf - $!\n";
}
}
}
die "$prg: no account data received from F*EX server\n";
}
# menu for MacOS users
sub menu {
my $key;
my $new;
local $_;
system 'clear';
print "\n";
print "fexsend-$version\n";
for (;;) {
if (open $idf,$idf) {
$fexcgi = getline($idf) and
$from = getline($idf) and
$id = getline($idf);
close $idf;
last if $id;
}
&set_ID;
}
print "\n";
print "$from on $fexcgi\n";
print "\n";
for (;;) {
print "\n";
print "[s] send a file or directory\n";
# print "[u] update fexsend\n";
print "[l] change login data (user, server, auth-ID)\n";
print "[h] help\n";
print "[q] quit\n";
print "\n";
print "your choice: ";
$key = ReadKey(0);
if ($key eq 'q') {
print "$key\n";
print "\n";
print "Type [Cmd]W to close this window.\n";
exit;
}
if ($key eq 'h') {
print "$key\n";
print
"\n".
"With fexsend you can send files of any size to any email address.\n".
"\n".
"At the recipient or file prompt [ENTER] brings you to this option menu.\n".
"\n".
"To send more than one file:\n".
"When you enter * at the file prompt, you will be first asked for an archive name\n".
"and then you can drag+drop multiple files.\n".
"\n".
"Do not forget to terminate each input line with [ENTER].\n".
"\n".
"See http://fex.rus.uni-stuttgart.de/ for more information.\n";
next;
}
if (0 and $key eq 'u') {
print "$key\n";
if ($prg =~ m:(^/client/|/sw/):) {
print "\n";
print "use swupdate to update fexsend!\n";
next;
}
$new = $prg.'.new';
system qw'wget -nv -O',$new,'http://fex.belwue.de/download/fexsend';
chmod 0755,$new;
system qw'perl -c',$new;
if ($? == 0) {
rename $new,$prg;
exec $prg;
} else {
print "\n";
print "cannot install new fexsend\n";
}
next;
}
if ($key eq 'l') {
print "$key\n";
system 'clear';
&set_ID;
next;
}
if ($key eq 's' or $key eq "\n") {
print "s\n";
&ask_file;
next;
}
}
exit;
}
# for MacOS
sub ask_file {
my ($file,$comment,$recipient,$archive,$size,$cmd,$key);
my @files;
my $qfiles;
local $_;
system 'clear';
&set_ID unless -s $idf;
print "\n";
print "Enter [ENTER] after each input line.\n";
print "\n";
for (;;) {
print "Recipient(s): ";
$recipient = <STDIN>;
chomp $recipient;
$recipient =~ s/^\s+//;
$recipient =~ s/\s+$//;
$recipient =~ s/[\s;,]+/,/g;
&menu unless $recipient;
last if $recipient =~ /\w/ or $recipient eq '.';
}
for (;;) {
print "\n";
print "Drag a file into this window or hit [ENTER] ";
print $archive ? "to continue.\n" : "for menu options.\n";
print "File to send: ";
$file = <STDIN>||'';
chomp $file;
$file =~ s/^\s+//;
$file =~ s/ $// if $file !~ /\\ $/;
&menu unless $file or $archive;
if ($file eq '*') {
print "Archive name: ";
$archive = <STDIN>||'';
chomp $archive;
next unless $archive;
$archive =~ s/^\s+//g;
$archive =~ s/\s+$//g;
next;
}
if ($file) {
unless (-e $file) {
$file =~ s/\\\\/\000/g;
$file =~ s/\\//g;
$file =~ s/\000/\\/g;
}
unless (-r $file) {
print "\"$file\" is not readable\n";
next;
}
my $qf = shellquote($file);
if (`du -ms $qf` =~ /^(\d+)/) {
$size += $1;
printf "%d MB\n",$1;
}
if ($archive) {
push @files,$file;
next;
}
}
if ($archive) {
$archive =~ s/[^\w=.+-]/_/g;
$archive =~ s/^\./_./g;
next unless @files;
$qfiles = join(' ',map(shellquote($_),@files));
if ($size < 2048) {
$archive .= '.zip';
} else {
$archive .= '.tar';
}
}
print "\n";
print "Comment: ";
$comment = <STDIN>||'';
chomp $comment;
print "\n";
if ($comment =~ s/^:\s*-/-/) {
$cmd = shellquote($_0)." $comment ";
if ($archive) {
$cmd .= '-A '.shellquote($archive).' '.$qfiles;
} else {
$cmd .= shellquote($file);
}
$cmd .= ' '.shellquote($recipient);
print $cmd,"\n";
system $cmd;
} else {
print shellquote($prg)." -C '$comment' ";
if ($archive) {
printf "-A %s %s %s\n",shellquote($archive),$qfiles,$recipient;
system $prg,'-C',$comment,'-A',$archive,@files,$recipient;
} else {
printf "%s %s\n",shellquote($file),$recipient;
system $prg,'-C',$comment,$file,$recipient;
}
}
print "\n";
print "[s] send another file to $recipient\n";
print "[n] send another file to another recipient\n";
print "[q] quit\n";
print "\n";
print "your choice: ";
for (;;) {
$key = ReadKey(0);
&ask_file if $key eq 'n';
if ($key eq 's' or $key eq "\n") {
print "s\n";
last;
}
if ($key eq 'q') {
print "$key\n";
exit;
}
}
$file = $comment = $archive = '';
@files = ();
}
}
sub set_ID {
my ($server,$port,$user,$logo);
local $_;
print "\n";
for (;;) {
print "F*EX server URL: ";
$server = <STDIN>;
$server =~ s/[\s\n]//g;
if ($server =~ s:/fup/(\w+)$::) {
$_ = decode_b64($1);
if (/(from|user)=(.+)&id=(.+)/) {
$user = $2;
$id = $3;
}
}
$server =~ s:/fup.*::;
$server =~ s:/+$::;
next if $server !~ /\w/;
if ($server =~ s/^https:..// or $server =~ /:443/) {
$server =~ s/:.*//;
$port = 443;
eval "use IO::Socket::SSL";
if ($@) {
print "\nno perl SSL modules installed - cannot use https\n\n";
next;
}
$SH = IO::Socket::SSL->new(
PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
%SSL
);
} else {
$server =~ s:^http.//::;
if ($server =~ s/:(\d+)//) {
$port = $1;
} else {
$port = 80;
}
$SH = IO::Socket::INET->new(
PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
);
}
unless ($SH) {
print "\ncannot connect to $server:$port - $!\n\n";
next;
}
sendheader(
"$server:$port",
"GET /logo.jpg HTTP/1.0",
"Connection: close",
);
$_ = <$SH>||'';
unless (/HTTP.1.1 200/) {
print "\nbad server reply: $_\n";
next;
}
while (<$SH>) { last if /^\s*$/ }
local $/;
$logo = <$SH>||'';
close $SH;
if (length $logo < 9999) {
print "\n$server is not a F*EX server!\n\n";
next;
}
open $logo,">$fextmp/fex.jpg";
print {$logo} $logo;
close $logo;
last;
}
for (;;) {
last if $user;
print "Your login (email address): ";
$user = <STDIN>;
$user =~ s/[\s\n]//g;
if ($user !~ /.@[\w.-]+$/) {
print "\"$user\" is not a valid email address!\n";
next;
}
}
for (;;) {
last if $id;
print "Your auth-ID for this account: ";
$id = <STDIN>;
$id =~ s/[\s\n]//g;
}
open $idf,'>',$idf or die "$prg: cannot write to $idf - $!\n";
print {$idf} "$server\n",
"$user\n",
"$id\n";
close $idf;
print "\n";
print "Login data written to $idf\n\n";
print "fexing test file to $user:\n\n";
system "$prg -o -M -C test $fextmp/fex.jpg $user";
print "\n";
if ($? != 0) {
print "fexsend failed, login data is invalid, try again\n";
&set_ID;
} else {
print "fexsend test succeeded!\n";
sleep 3;
}
}
sub nettest {
my $url = shift;
my $up = shift;
my $down = shift;
my $bs = 2**16;
my ($length,$t0,$t1,$t2,$tt,$tb,$tc,$B,$kBs,$bt);
my $nettest = $sid = 'nettest';
$port ||= 80;
if ($url =~ s:^https.//::) {
$https = $port = 443;
} else {
$url =~ s:^http.//::;
$port = $1 if $url =~ s/:(\d+)//;
}
$url =~ s/[\/:].*//;
$server = $url;
if ($up) {
serverconnect($server,$port);
checkrecipient($nettest,$nettest);
warn "$prg: send to $server:$port\n";
formdatapost(
from => $nettest,
to => $nettest,
id => $nettest,
file => $nettest,
size => $up*MB,
comment => 'NOSTORE',
);
}
if ($down) {
serverconnect($server,$port);
warn "$prg: receive from $server:$port\n";
sendheader("$server:$port","GET $proxy_prefix/ddd/$down HTTP/1.0");
$_ = <$SH>;
die "$prg: no response from fex server $server\n" unless $_;
s/\r//;
if (/^HTTP\/[\d.]+ 2/) {
warn "<-- $_" if $opt_v;
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^$/;
$length = $1 if /^Content-Length:\s*(\d+)/i;
}
} else {
s/HTTP\/[\d.]+ \d+ //;
die "$prg: bad server reply: $_";
}
unless ($length) {
die "$prg: no Content-Length header in server reply\n";
}
if (${'opt_+'}) {
print $rrcamel[0];
$tc = 0;
}
$t0 = $t1 = $t2 = int(time);
$B = 0;
while ($B < $length) {
$b = read $SH,$_,$bs or die "$prg: cannot read after $B bytes - $!\n";
# defined($_ = <$SH>) or die "$prg: cannot read after $B bytes - $!\n";
# $b = length;
$B += $b;
$bt += $b;
$t2 = time;
if (${'opt_+'} and int($t2*10)>$tc) {
print $rrcamel[$tc%2+1];
$tc = int($t2*10);
}
if (int($t2) > $t1) {
$kBs = int($bt/kB/($t2-$t1));
$t1 = $t2;
$bt = 0;
printf STDERR "nettest: %d MB (%d%%) %d kB/s \r",
int($B/MB),int(100*$B/$length),$kBs;
}
}
close $SH;
$tt = $t2-$t0;
$kBs = int($B/kB/($tt||1));
if (${'opt_+'}) {
print $rrcamel[1];
print $rrcamel[2];
}
printf STDERR "nettest: %d MB in %d s = %d kB/s \n",
int($B/MB),$tt,$kBs;
}
}
# read one key from terminal in raw mode
sub ReadKey {
my $key;
local $SIG{INT} = sub { stty('reset'); exit };
stty('raw');
# loop necessary for ESXi support
$key = getc(STDIN) while not defined $key;
stty('reset');
return $key;
}
sub stty {
if (shift eq 'raw') {
system qw'stty -echo -icanon eol',"\001";
} else {
system qw'stty echo icanon eol',"\000";
}
}
sub send_xx {
my $transferfile = shift;
my $file = '';
my (@r,@tar,$dir);
local $_;
$SIG{PIPE} = $SIG{INT} = sub {
unlink $transferfile;
exit 3;
};
@tar = qw'tar -cvz';
push @tar,qw'--exclude=.snapshot* --exclude=.fex/tmp';
if (-t) {
if ("@ARGV" eq '-') {
# store STDIN to transfer file
shelldo("gzip >> $transferfile");
} elsif (@ARGV) {
# local $opt_v = 1; # show tar command
# print "making tar transfer file $transferfile :\n";
# single file? then add this directly
if (scalar @ARGV == 1) {
# strip path if not ending with /
if ($ARGV[0] =~ m:(.+)/(.+): and $2 !~ m:/$:) {
($dir,$file) = ($1,$2);
chdir $dir or die "$prg: $dir - $!\n";
} else {
$file = $ARGV[0];
}
if (-l $file) {
shelldo(@tar,qw'--dereference -f',$transferfile,$file);
} else {
shelldo(@tar,'-f',$transferfile,$file);
}
} else {
# remove common path
my $dir = dirname($ARGV[0]);
foreach (@ARGV) {
unless (/^\Q$dir\E\/./) {
undef $dir;
last;
}
}
if (defined($dir) and chdir $dir) {
@ARGV = grep { s:^\Q$dir\E\/::; } @ARGV;
}
shelldo(@tar,'-f',$transferfile,@ARGV);
}
if ($?) {
unlink $transferfile;
if ($? == 2) {
die "$prg: interrupted making tar transfer file\n";
} else {
die "$prg: error while making tar transfer file\n";
}
}
}
} else {
# write input from pipe to transfer file
shelldo("gzip >> $transferfile");
}
die "$prg: no transfer file\n" unless -s $transferfile;
serverconnect($server,$port);
query_sid($server,$port);
@r = formdatapost(
from => $from,
to => $from,
id => $sid,
file => $transferfile,
comment => 'NOMAIL',
autodelete => $transferfile =~ /STDFEX/ ? 'NO' : 'DELAY',
);
# open P,'|w3m -T text/html -dump' or die "$prg: w3m - $!\n";
# print P @r;
http_response(@r);
if ($transferfile =~ /:/ and $prg ne 'xxx') {
if ("@r" =~ /\s(X-)?Location: (http.*?)\s/) {
print "wget -qO - $2 | tar xvzf -\n";
}
}
unlink $transferfile;
}
sub query_quotas {
my (@r,$r);
local $_;
female_mode("query quotas?") if $opt_F;
@r = formdatapost(
from => $from,
to => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
print "sender quota (used): $1 ($2) MB\n";
} else {
print "sender quota: unlimited\n";
}
if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
print "recipient quota (used): $1 ($2) MB\n";
} else {
print "recipient quota: unlimited\n";
}
}
sub command {
my (@r,$r);
my ($psp,$owner,$share,$archive,$avt,$dkey);
my $command = shift;
local $_;
if ($port == 80) { $psp = "http://$server" }
elsif ($port == 443) { $psp = "https://$server" }
else { $psp = "http://$server:$port" }
if ($command eq 'FUPWATCH') {
warn "$prg: querying $psp/$from\n";
@r = formdatapost(
from => $from,
to => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
die "$prg: server does not support FUPWATCH\n" if $features !~ /FUPWATCH/;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
while (<$SH>) {
warn "<-- $_" if $opt_v;
if (/^(\S+) (\S+) (\S+) (\w+) (\d+) (".*")$/) {
printf "%s %d %s/fop/%s/%s %s\n",$1,$5,$psp,$4,$3,$6;
}
}
die "$prg: server has closed the connection\n";
}
if ($command eq 'GENUKEY') {
@r = formdatapost(
from => $from,
to => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
if (grep /^Content-Type: text\/html/,@r) {
die "$prg: no $command support on $server:$port\n";
}
my $ukey = '';
while (shift @r) {
$ukey = $1 if /^X-UKEY: (\w+)/;
}
$ukey ||= shift @r;
if ($ukey) {
if ($ukey =~ /_$/) {
# xxx / xup
print "FUP=$psp/fup?ukey=$ukey\n";
exit;
}
if ($ENV{FUA} =~ /fexpack/) {
print "# upload URL and bash function for copy&paste\n";
print "# upload key is valid for one day!\n";
print "FUP=$psp/fup?ukey=$ukey\n";
print
'fup(){ ',
'local a t;',
'a=$(basename "$1");',
'[ -r "$1" ]||{ echo "usage: fup FILE...";return; };',
'[ -z "$2" ]||{ echo -n "archive name: ";read a; };',
't="${TMPDIR:-/tmp}/fup_$a.tar";',
'echo $t:;',
'tar cvf "$t" "$@"&&curl',
' -F "keep=1"',
' -F "comment=[$(logname)@$(hostname)]"',
' -F "file=@$t"',
' $FUP|cat;',
'rm "$t";',
} else {
print "# upload URL and bash function for others to send you files\n";
print "# upload key is valid for one day!\n";
print "FUP=$psp/fup?ukey=$ukey#$from\n";
print 'fup(){ ';
if ("@ARGV" eq '+' or ${'opt_+'}) {
print
'local a t c;',
'a=$(basename "$1");',
'[ -r "$1" ]||{ echo "usage: fup FILE...";return; };',
'[ -z "$2" ]||{ echo -n "archive name: ";read a; };',
'echo -n "comment: ";read -e c;',
't="${TMPDIR:-/tmp}/fup_$a.tar";',
'echo $t:;',
'tar cvf "$t" "$@"&&curl',
' -F "comment=[$(logname)@$(hostname)] $c"',
' -F "file=@$t"',
' $FUP|cat;',
'rm "$t";',
} else {
print
'[ -f "$1" ]&&{ printf "comment: ";read -e c;',
'curl -F "file=@$1" "$FUP?comment=$c"|cat;}||',
'{ echo usage: fup FILE;};',
}
}
print "};fup\n";
} else {
die "$prg: no UKEY reply from $server\n";
}
exit;
}
if ($command =~ /^GENOKEY:\S/) {
@r = formdatapost(
from => $from,
to => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
unless (grep /^Content-Type: text\/plain/,@r) {
die "$prg: no $command support on $server:$port\n[@r]";
}
foreach (@r) {
if (/okey=/) {
print "$_\n";
exit;
}
}
die "$prg: no okey in server reply\n";
}
if ($command eq 'LISTSHARE') {
if ("@ARGV" =~ /^(\S+):share=(\S+)$/) {
$owner = $1;
$share = $2;
} else {
die "usage: $prg -~ LISTSHARE OWNER:share=SHARE\n"
}
@r = formdatapost(
from => $from,
to => "@ARGV",
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
if (grep /^Content-Type: text\/html/,@r) {
die "$prg: no archive sharing support on $server:$port\n";
}
# skip HTTP header
while (shift @r) { }
my $sx = '';
if ($pkey) {
$sx = md5_hex("$1:$pkey") if $id =~ /^MD5H:(\w+)/;
} else {
$sx = md5_hex("$1:$id") if $sid =~ /^MD5H:(\w+)/;
}
foreach my $line (@r) {
if ($line =~ /^(\S+):owner:-$/) {
$owner = $1;
next;
} elsif ($line =~ /^(\S+\@\S+):([a-z]+):(\w+)$/) {
my $suser = $1;
my $access = $2;
my $pkey = $3;
$pkey = pack('H*',$pkey) ^ $sx if $sx;
$line = "$psp/fas/$owner/$share/$suser/$pkey (access=$access)";
} elsif ($line =~ s/^((\S+) (\S+)(.*\" ))(\w+)$/$1/) {
$archive = $2;
$avt = $3;
$dkey = $5;
$dkey = pack('H*',$dkey) ^ substr($sx,0,8) if $sx;
$line .= sprintf "%s/fop/%s/%s_%s",$psp,$dkey,$archive,$avt;
}
print $line,"\n";
}
exit;
}
if ($command eq 'LISTSHARES') {
@r = formdatapost(
from => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: $server response: $_\n";
}
if (grep /^Content-Type: text\/html/,@r) {
die "$prg: no archive sharing support on $server:$port\n";
}
# skip HTTP header
while (shift @r) { }
foreach (@r) { print "$_\n" }
exit;
}
if ($command =~ /^SHAREUSER:(\S+\@[\w.-]+):[a-z]+$/) {
my $suser = $1;
if ("@ARGV" =~ /^(\S+):share=(\S+)$/) {
$owner = $1;
$share = $2;
} else {
die "usage: $prg -~ SHAREUSER:USER:ACCESS OWNER:share=SHARE\n"
}
@r = formdatapost(
from => $from,
to => "@ARGV",
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: $server response: $_\n";
}
if (grep /^Content-Type: text\/html/,@r) {
die "$prg: no archive sharing support on $server:$port\n";
}
my $sx = '';
if ($pkey) {
$sx = md5_hex("$1:$pkey") if $id =~ /^MD5H:(\w+)/;
} else {
$sx = md5_hex("$1:$id") if $sid =~ /^MD5H:(\w+)/;
}
# skip HTTP header
while (shift @r) { }
$_ = shift @r;
if (s/^pkey=//) {
if (/MD5H:(\w+)/) {
$pkey = pack('H*',$1) ^ $sx;
} else {
$pkey = $_;
}
print "$psp/fas/$owner/$share/$suser/$pkey\n";
} else {
print "$_\n";
}
exit;
}
if ($command =~ /^COPYARCHIVE:\S+:\S+:\S+:\S+:\S+/) {
if ("@ARGV" =~ /^(\S+)$/) {
$from = $1;
} else {
die "usage: $prg -~ COPYARCHIVE:SHARE:ARCHIVE:VERSION.CONTAINER:NEWSHARE:NEWARCHIVE OWNER\n";
}
@r = formdatapost(
from => $from,
to => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: $server response: $_\n";
}
if (grep /^Content-Type: text\/html/,@r) {
die "$prg: no archive sharing support on $server:$port\n";
}
while (@r) {
$_ = shift @r;
last if /^$/;
}
print "$_\n" foreach @r;
exit;
}
if ($command =~ /^DOX(SYNC|DEL|LIST):./ or $command eq 'DOXLIST') {
# die "$prg: fexserver does not support DOX\n" if $features !~ /DOX/;
@r = formdatapost(
from => $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: $server response: $_\n";
}
unless (/^HTTP.* 222/) {
die "$prg: no DOX support on $server\n";
}
while (<$SH>) {
warn "<-- $_" if $opt_v;
if ($command =~ /DOXSYNC/) {
s/^\s+//;
} elsif ($command eq 'DOXLIST') {
if (/^(\d+) (\d+) (.+)/) {
my @d = localtime($1);
$_ = sprintf("%d-%02d-%02d %02d:%02d:%02d %7d MB %s\n",
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],$2,$3);
}
}
print;
}
exit;
}
die "$prg: unknown command $command\n";
}
sub query_settings {
my (@r,$r);
local $_;
female_mode("query settings?") if $opt_F;
if ($FEXID) {
print "ID data from \$FEXID\n";
} elsif (-f $idf) {
print "ID data from $idf\n";
} else {
die "$prg: found no ID\n";
}
print "server: $fexcgi\n";
print "user: $from\n";
print "auth-ID: $id\n";
print "login URL: ";
&show_URL;
@r = formdatapost(
from => $from,
to => $from,
id => $sid,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 2/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
if (($_) = grep(/^X-Autodelete/,@r) and /:\s+(\w+)/) {
print "autodelete: $1\n";
}
if (($_) = grep(/^X-Default-Keep/,@r) and /(\d+)/) {
print "default keep: $1 days\n";
}
if (($_) = grep(/^X-Default-Keep-Archives/,@r) and /(\d+)/) {
print "default archive keep: $1 days\n";
}
if (($_) = grep(/^X-Default-Locale/,@r) and /:\s+(\w+)/) {
print "default locale: $1\n";
}
if (($_) = grep(/^X-MIME/,@r) and /:\s+(\w+)/) {
print "display file with browser: $1\n";
}
if (($_) = grep(/^X-Sender-Quota/,@r) and /(\d+)\s+(\d+)/) {
print "sender quota (used): $1 ($2) MB\n";
} else {
print "sender quota: unlimited\n";
}
if (($_) = grep(/^X-Recipient-Quota/,@r) and /(\d+)\s+(\d+)/) {
print "recipient quota (used): $1 ($2) MB\n";
} else {
print "recipient quota: unlimited\n";
}
}
# list spool
sub list {
my (@r,$r);
my ($data,$dkey,$to);
my $n = 0;
my $s = 1;
my $a = shift @ARGV || '@';
local $_;
female_mode("list spooled files?") if $opt_F;
if ($opt_l) {
if ($a =~ /^\d+$/) {
open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
while (<$fexlist>) {
if (/#(\d+) (\w+) (.+)/ and $1 eq $a) {
serverconnect($server,$port) unless $SH;
sendheader(
"$server:$port",
"GET $proxy_prefix/fop/$2/$2?LIST HTTP/1.1",
);
$_ = <$SH>||'';
s/\r//;
warn "<-- $_" if $opt_v;
if (/^HTTP.* 200/) {
warn "<-- $_" if $opt_v;
while (<$SH>) {
s/\r//;
if (/^\n/) {
print;
print while <$SH>;
}
}
} elsif (s:HTTP/[\d\. ]+::) {
die "$prg: server response: $_";
} else {
die "$prg: no response from fex server $server\n";
}
exit;
}
}
die "$prg: file \#$a not found in fexlist\n";
}
}
@r = formdatapost(
from => $from,
to => $opt_l ? '*' : $from,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 200/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
# list sent files
if ($opt_l) {
open $fexlist,'>',$fexlist or die "$prg: cannot write $fexlist - $!\n";
foreach (@r) {
last if m:</pre>:;
next unless /<pre>/ or $data;
$data = 1;
if (/<a href=".*dkey=(\w+).*?">/) { $dkey = $1 }
else { $dkey = '' }
# $_ = decode_utf8($_);
# $_ = locale($_);
s/<.*?>//g;
s/"/\"/g;
s/</</g;
s/&/&/g;
if (/^(to (.+) :)/) {
$to = $2;
if ($a eq '.') {
$s = $to eq $from ? 1 : 0;
} elsif ($a eq '-') {
$s = $to eq $from ? 0 : 1;
} else {
$s = $to =~ /$a/;
}
if ($opt_u) {
s/to/for/;
print "\n$_\n" if $s;
} else {
print "\n$_\n" if $s;
print {$fexlist} "\n$_\n";
}
} elsif (s/.* (\d+) MB //) {
my $size = $1;
$n++;
if ($s) {
if ($opt_u and /( [+-] )(\S+)/) {
my $file = $2;
$file =~ s/[^\w.,@~^=+-]/_/g;
printf " %s/fop/%s/%s\n",$fexcgi,$dkey,$file;
} else {
printf "%4s %8d MB %s\n","#$n",$size,$_;
}
}
printf {$fexlist} "%4s %s %s\n","#$n",$dkey,$_ unless $opt_u;
}
}
close $fexlist;
}
# list received files
if ($opt_L) {
my $show = 1;
my $user;
foreach (@r) {
s/"/\"/g;
s/'/\'/g;
s/</</g;
s/&/&/g;
# locale must be english!
if ($a eq '.' and /Files for (\S+)/) {
# only files from = to = user
$user = $1;
}
next unless /<pre>/ or $data;
$data = 1;
last if m:</pre>:;
if (/(from (\S+) :)/) {
if ($user) {
$show = $2 eq $user ? 1 : 0;
}
print "\n$1\n" if $show;
}
if ($show and
m{(\d+) (MB.*)<a href="(https?://.*/fop/\w+/.+)">(.+)</a>( ".*")?}) {
printf "%8d %s%s%s\n",$1,$2,$3,($5||'');
}
}
}
}
sub show_URL {
printf "%s/fup/%s\n",$fexcgi,encode_b64("from=$from&id=$id");
}
sub get_log {
my (@r);
local $_;
@r = formdatapost(
from => $from,
to => $from,
id => $sid,
command => $command,
);
die "$prg: no response from fex server $server\n" unless @r;
$_ = shift @r;
unless (/^HTTP.* 200/) {
s:HTTP/[\d\. ]+::;
die "$prg: server response: $_\n";
}
while (shift @r) {}
foreach (@r) { print "$_\n" }
exit;
}
sub show_address_book {
my (%AB,@r);
my $alias;
local $_;
%AB = query_address_book($server,$port,$from);
foreach $alias (sort keys %AB) {
next if $alias eq 'ADDRESS_BOOK';
$_ = sprintf "%s = %s (%s) # %s\n",
$alias,
$AB{$alias},
$AB{$alias}->{options},
$AB{$alias}->{comment};
s/ \(\)//;
s/ \# $//;
print;
}
}
sub purge {
die "$prg: not yet implemented\n";
}
sub delete_file_number {
my ($to);
while (@ARGV) {
$opt_d = shift @ARGV;
die "usage: $prg -d #\n" if $opt_d !~ /^\d+$/;
open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
while (<$fexlist>) {
if (/^to (.+\@.+) :/) {
$to = $1;
} elsif (/#(\d+) (\w+) .*? [+-] (\S+)/ and $1 eq $opt_d) {
delete_file_dkey($2,$3);
last;
}
}
close $fexlist;
sleep 1; # do not overrun server
}
exit;
}
sub delete_file_url {
if (scalar(@ARGV) != 1) {
die "usage: $prg -d FEXURL\n";
}
if ("@ARGV" =~ m:^http.*/fop/(\w+)/(.+):) {
delete_file_dkey($1,$2);
} else {
die "$prg: @ARGV is not a FEXURL\n";
}
exit;
}
sub delete_file_dkey {
my ($dkey,$file) = @_;
local $_;
serverconnect($server,$port) unless $SH;
query_sid($server,$port);
sendheader(
"$server:$port",
"GET $proxy_prefix/fop/$dkey/$file?DELETE&id=$sid HTTP/1.1",
);
$_ = <$SH>||'';
s/\r//;
warn "<-- $_" if $opt_v;
if (/^HTTP.* 200/) {
while (<$SH>) {
s/\r//;
last if /^\n/; # ignore HTML output
warn "<-- $_" if $opt_v;
if (/^X-File: (.+)\/(.+)\/(.+)/) {
if ($1 eq $2) {
printf "%s deleted\n",decode_utf8(urldecode($3));
} else {
printf "%s for %s deleted\n",decode_utf8(urldecode($3)),$1;
}
}
}
undef $SH;
} elsif (s:HTTP/[\d\. ]+::) {
die "$prg: server response: $_";
} else {
die "$prg: no response from fex server $server\n";
}
}
sub delete_file {
my ($from,$to,$file) = @_;
local $_;
unless ($SH) {
serverconnect($server,$port);
query_sid($server,$port) unless $anonymous;
}
$to = $from if $to eq '.';
$file = urlencode($file);
sendheader(
"$server:$port",
"GET $proxy_prefix/fop/$to/$from/$file?id=$sid&DELETE HTTP/1.1",
);
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^\s*$/;
}
}
sub urlencode {
local $_ = shift;
# $_ = encode_utf8($_) if utf8::is_utf8($_);
# s/([^~^_:,;+*.!$#<>(){}\[\]\@\w\-])/'%'.uc(unpack("H2",$1))/ge;
s/([^~^_:,;+*.!#\@\w\-])/'%'.uc(unpack("H2",$1))/ge;
return $_;
}
sub send_fex {
my @to;
my $file = '';
my @files = ();
my ($data,$aname,$alias);
my (@r,$r);
my $t0 = time;
my @transferfiles;
my $vp = '\d{8}_\d{6}'; # fexpush version pattern
local $_;
if ($from =~ /^SUBUSER|GROUPMEMBER$/) {
$to = '_';
} else {
# look for single @ in arguments
for (my $i=1; $i<$#ARGV; $i++) {
if ($ARGV[$i] eq '@') {
$ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
$#ARGV = $i;
last;
}
}
$to = pop @ARGV or die $usage;
if ($to eq '.' or $to eq '+') {
$nomail ||= $to;
$opt_M ||= $to;
}
if ($to eq ':') {
$to = $from;
$nomail = $opt_C ||= 'NOMAIL';
}
if ($opt_g and $to =~ /,/) {
die "$prg: encryption is supported to only one recipient\n";
}
if ($to =~ m{^https?://.*/fup\?skey=(\w+)}) {
$from = 'SUBUSER';
$to = '_';
$id = $1;
}
if ($to =~ m{^https?://.*/fup\?gkey=(\w+)}) {
$from = 'GROUPMEMBER';
$to = '_';
$id = $1;
}
if ($to =~ m{^https?://.*/fup\?to=(\S+)\?okey=(\w+)}) {
$from = 'ONETIMEUSER';
$to = $1;
$id = $2;
}
}
@to = split(',',$to);
die $usage unless @ARGV or $opt_a or $opt_s;
die $usage if $opt_s and @ARGV;
if ($cygwin and scalar(@ARGV) == 1 and not $opt_a) {
my $a = $ARGV[0];
$opt_a = basename($a).'.zip' if -d $a;
}
if (not $opt_a and not $opt_d and scalar(@ARGV) == 1) {
$_ = $ARGV[0];
if (not -e and /(.+)\.(tar|tgz|zip|7z)$/ and -e $1) {
@ARGV = ($1);
s:.*/::;
s/[^\w_.+-]/_/g;
$opt_a = $_;
}
}
if ($opt_a and scalar(@ARGV) == 1) {
my $a = $ARGV[0];
if (-d $a and $a !~ m:/$:) {
my $dir = abs_path($a);
@ARGV = (basename($dir));
$dir = dirname($dir);
chdir $dir or die "$prg: cannot cd $dir - $!\n";
warn "\$ cd $dir\n" if $opt_v;
}
}
if ($opt_a and not $opt_A) {
my @A = ();
foreach my $a (@ARGV) {
if ($a eq '.') {
push @A,glob('*');
} elsif ($a =~ m:^\.|/\.:) {
die "$prg: cannot send $a with option -a, use -A instead\n";
push @A,glob('*');
} else {
push @A,$a;
}
}
@ARGV = @A;
}
# early serverconnect necessary for X-Features info
serverconnect($server,$port);
if ($anonymous) {
my $aok;
sendheader("$server:$port","GET /SID HTTP/1.1");
$_ = <$SH>||'';
s/\r//;
die "$prg: no response from fex server $server\n" unless $_;
warn "<-- $_" if $opt_v;
if (/^HTTP.* [25]01/) {
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last unless /\w/;
$aok = $_ if /X-Features:.*ANONYMOUS/;
}
die "$prg: no anonymous support on server $server\n" unless $aok;
} else {
die "$prg: bad response from server $server : $_\n";
}
} elsif ($public) {
} else {
query_sid($server,$port);
if ($from eq 'SUBUSER') {
$skey = $sid;
# die "skey=$skey\nid=$id\nsid=$sid\n";
}
if ($from eq 'GROUPMEMBER') {
$gkey = $sid;
}
if ($to eq '.' or $to eq '+') {
# @to = ($from);
} elsif ($to =~ /(.+):share=(.+)/) {
$frecipient = $1;
$share = $2;
} elsif ($to =~ m:^(//.*):) {
my $xkey = $1;
if ($features =~ /XKEY/) {
@to = ($from);
$opt_C = $xkey;
} else {
die "$prg: server does not support XKEY\n";
}
} elsif (grep /^[^@]*$/,@to and not ($skey or $gkey or $okey or $share)) {
%AB = query_address_book($server,$port,$from);
if ($proxy) {
serverconnect($server,$port);
query_sid($server,$port);
}
foreach $to (@to) {
# alias in local config?
if ($alias{$to}) {
if ($alias{$to} =~ /(.+?):(.+)/) {
my $ato = $1;
my $opt = $2;
my @argv = @_ARGV;
pop @argv;
# special extra upload
system $prg,split(/\s/,$opt),@argv,$ato;
$to = '';
} else {
$to = $alias{$to};
}
}
# alias in server address book?
elsif ($AB{$to}) {
# do not substitute alias with expanded addresses because then
# keep and autodelete options from address book will get lost
# $to = $AB{$to};
}
# look for mutt aliases
elsif ($to !~ /@/ and $to ne $from) {
$to = get_mutt_alias($to);
}
}
}
$to = join(',',grep /./,@to) or exit;
$share_ = $to =~ /:share=_$/;
if (
not ($skey or $gkey or $okey or $share)
and $from ne $to
and $to ne '.'
and $to ne '+'
and $features =~ /CHECKRECIPIENT/
and $command !~ /^(DELETE|LIST|RECEIVEDLOG|SENDLOG|FOPLOG)$/
) {
checkrecipient($from,$to);
if ($proxy) {
serverconnect($server,$port);
query_sid($server,$port);
}
}
}
if (@ARGV > 1 and not ($opt_a or $opt_s or $opt_d)) {
$opt_a = inputline("Archive name (name.tar, name.tgz or name.zip) or ".
"[ENTER] to send file for file:\n");
$opt_a =~ s/^\s+//;
$opt_a =~ s/\s+$//;
$opt_a =~ s/\//_/g;
}
if ($macos and not $opt_a and -d "@ARGV") {
my $dir = "@ARGV";
my $qdir = shellquote($dir);
if (`du -s $qdir` =~ /^(\d+)/ and $1 < 2**21) {
$opt_a = "$dir.zip";
} else {
$opt_a = "$dir.tar";
}
}
if ($opt_s) {
$opt_s =~ s/^=//;
$opt_s =~ s:.*/::;
$opt_s =~ s/[^\w_.+-]/_/g;
@files = ($opt_s);
} elsif ($opt_a) {
$opt_a =~ s/^=//;
$opt_a =~ s:/+$::;
if ($opt_a =~ /(.+)\.(zip|tar|tgz|taz|7z)$/) {
$aname = $1;
$atype = $2;
# no file argument left?
unless (@ARGV) {
# use filename as archive name
push @ARGV,$aname;
}
$aname =~ s:.*/::;
$aname =~ s/[^\w_.+-]/_/g;
} else {
die "$prg: archive name must be one of ".
"$opt_a.tar $opt_a.tgz $opt_a.taz $opt_a.zip\n";
}
if ("@ARGV" =~ /^\[(\S+)\]$/) {
die "$prg: cannot read \"$1\"\n" unless -r $1;
} else {
foreach my $file (@ARGV) {
die "$prg: cannot read \"$file\"\n" unless -l $file or -r $file;
}
}
$opt_a =~ s:.*/::;
$opt_a =~ s/[^\w_.+-]/_/g;
$opt_a .= ".$atype" if $opt_a !~ /\.$atype$/;
if (0 and $useragent !~ /\// and scalar(@ARGV) > 1) {
# remove common path
my $dir = dirname($ARGV[0]);
foreach (@ARGV) {
unless (/^\Q$dir\E\/./) {
undef $dir;
last;
}
}
if (defined($dir) and chdir $dir) {
@ARGV = grep { s:^\Q$dir\E\/::; } @ARGV;
}
}
if ($useragent =~ /fexsync/) {
if ($features !~ /CCCSYNC/) {
die "$prg: fexserver does not support fexsync\n";
}
if ($opt_C =~/^DOX/ and $features !~ /DOX/) {
die "$prg: fexserver does not support fexdox\n";
}
}
if (${'opt_^'}) {
nmtime(@ARGV);
my @d = localtime $nmtime;
my $dt .= sprintf("_%d%02d%02d_%02d%02d%02d",
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
$opt_a =~ s/(.+)\./$1$dt./;
}
$transferfile = "$fextmp/$opt_a";
unlink $transferfile;
if ($atype eq 'zip') {
print "Making fex archive ($opt_a):\n" unless $opt_q;
if ($windoof) {
system(qw'7z a -tzip',$transferfile,@ARGV);
@files = ($transferfile);
} elsif ($macos and scalar(@ARGV) == 1) {
## ditto-zip is now handled by formdatapost()
system 'true';
@files = ($opt_a);
} else {
# zip archives must be < 2 GB, so split as necessary
# @files = zipsplit($transferfile,@ARGV);
# if (scalar(@files) == 1) {
# $transferfile = $files[0];
# $transferfile =~ s/_1.zip$/.zip/;
# rename $files[0],$transferfile;
# @files = ($transferfile);
# }
@files = zip($transferfile,@ARGV);
}
@transferfiles = @files;
} elsif ($atype eq '7z') {
print "Making fex archive ($opt_a):\n" unless $opt_q;
# http://www.7-zip.org/
my @sz = qw'7z a -t7z';
push @sz,'-mx0' if $opt_0 or not compressable(@ARGV);
foreach my $x (split('#',$xlist)) { push @sz,"-xr!$x" }
vsystem(@sz,$transferfile,@ARGV);
@transferfiles = @files = ($transferfile);
} elsif ($atype eq 'tar') {
if ($windoof) {
print "Making fex archive ($opt_a):\n" unless $opt_q;
system(qw'7z a -ttar',$transferfile,@ARGV);
@transferfiles = @files = ($transferfile);
} else {
## tar is handled by formdatapost()
# system(qw'tar cvf',$transferfile,@ARGV);
@files = ($opt_a);
}
} elsif ($atype eq 'tgz') {
if ($windoof) {
die "$prg: archive type tgz not available, use tar, zip or 7z\n";
} else {
## tgz is handled by formdatapost()
# system(qw'tar cvzf',$transferfile,@ARGV);
@files = ($opt_a);
}
} elsif ($atype eq 'taz') {
if ($windoof) {
die "$prg: archive type taz not available, use tar, zip or 7z\n";
} else {
## taz is handled by formdatapost()
@files = ($opt_a);
}
} else {
die "$prg: unknown archive format \"$atype\"\n";
}
if (@transferfiles) {
# error in making transfer archive?
if ($?) {
unlink @transferfiles;
die "$prg: $! - aborting upload\n";
}
# maybe timeout, so make new connect
if (time-$t0 >= $timeout) {
serverconnect($server,$port);
query_sid($server,$port) unless $anonymous;
}
}
} else {
unless (@ARGV) {
if ($windoof) {
&inquire;
} else {
die $usage;
}
}
foreach (@ARGV) {
my $file = $_;
unless ($opt_d) {
unless (-f $file) {
if (-e $file) {
die "$prg: \"$file\" is not a regular file, try option -a\n"
} else {
die "$prg: \"$file\" does not exist\n";
}
}
die "$prg: cannot read \"$file\"\n" unless -r $file;
}
push @files,$file;
}
}
if (${'opt_/'}) {
foreach my $file (@files) {
my @s = stat($file);
unless (@s and ($s[2] & S_IROTH) and -r $file) {
die "$prg: \"$file\" is not world readable\n";
}
}
}
my $nextfile = 0;
foreach my $file (@files) {
if ($nextfile) {
warn "\n";
} else {
sleep 1; # do not overrun server!
$nextfile++
}
unless (-s $file or $opt_d or $opt_a or $opt_s) {
die "$prg: cannot send empty file \"$file\"\n";
}
female_mode("send file $file?") if $opt_F;
if (basename($file) =~ /^ARCHIVE_SHARES?$/ and $features !~ /\bFAS\b/) {
die "$prg: server $server:$port has no archive sharing support\n";
}
@r = formdatapost(
from => $from,
to => $to,
replyto => $opt_r,
id => $sid,
file => $file,
keep => $opt_k,
comment => $opt_C,
command => $command,
autodelete => $opt_D,
);
if ($opt_s) {
if (@r) {
if (($from eq $to or $to eq '.') and not $opt_q) {
my $xxx = $useragent =~ /xxx/;
foreach (@r) {
if ($xxx and /Location: (http\S+)/) {
my $url = $1;
warn $_;
if ($url =~ /xxx.bash/) {
print "# copy this bash command to your other account to temporary\n";
print "# install the F*EX clients (including xxx) with your F*EX ID\n";
print "eval \$(wget -qO - $url)\n";
exit;
}
if ($url =~ /($xn)/) {
print "xxx $1\n";
if ($ENV{opt_w}) {
if (/Location: .*\.txt$/) {
print "wget -qO- $url\n";
} elsif (/Location: .*\.gz$/) {
print "wget -qO- $url | gunzip\n";
}
}
}
}
if (/Location: (http.*\.gz$)/) {
my $url = $1;
if ($share_ and $url =~ /\/__$vp/) {
print "wget -qO- $url | gunzip\n";
}
} elsif (/Location:/) {
print;
}
}
}
exit;
} else {
die "$prg: no server response\n";
}
}
if (not @r or not grep /\w/,@r) {
die "$prg: no response from server\n";
}
next if "@r" eq '0';
# already transfered
if ("@r" =~ /^Location: (http.*)/) {
foreach (@r) {
print "Location: $1\n" if /^Location: (http.*)/;
}
# my $url = $1;
# if ($useragent =~ /xxx/) {
# if ($cygwin) {
# print "Location: $url\n";
# }
# print "fexget $1\n";
# } elsif ($useragent =~ /zip|autofex/ or $cygwin) {
# print "Location: $url\n";
# } else {
# print "FOP=$url\n";
# if ($url =~ /\.tar$/) {
# print "wget -qO - \$FOP | tar -xvf -\n";
# } elsif ($url =~ /\.(tgz|tar\.gz)$/) {
# print "wget -qO - \$FOP | tar -xvzf -\n";
# } elsif ($url =~ /\.tar\.bz2$/) {
# print "FOP=$url\n";
# print "wget -qO - \$FOP | bunzip2 | tar -xvf -\n";
# } elsif ($url =~ m:.*/(.+):) {
# print "rm -f $1; ";
# print "wget -c \$FOP\n";
# }
# }
next;
}
if ("@r" =~ /INTERNAL ERROR.*<pre>(.+)<\/pre>/s) {
die "$prg: server error: $1\n";
}
if ($r[0] =~ /^HTTP.* 201 (.+)/) {
print "$1\n";
exit;
}
if (($r) = grep /^ERROR:/,@r) {
if ($anonymous and $r =~ /purge it/) {
die "$prg: file is already on server for $to - use another anonymous recipent\n";
} elsif ($r =~ /timeout/i) {
close $SH;
retry("timed out");
} else {
$r =~ s/.*?:\s*//;
$r =~ s/<.+?>//g;
$r =~ s/^HTTP\/1.. \d+ //;
die "$prg: server error: $r\n";
}
}
unless ($opt_d) {
if (scalar(@r) == 1) {
die "$prg: server error: @r\n";
} else {
if ($r[0] !~ /HTTP.1.. 2/) {
if ($r[0] =~ /HTTP.[\s\d.]+(.+)/) {
die "$prg: server error: $1\n";
} else {
die "$prg: server error:\n".join("\n",@r)."\n";
}
}
}
}
if ("@r" =~ /<h3>(.*?)</) {
print "$1\n";
}
if ($useragent =~ /fexsync/) {
if ($opt_C =~ /^DOX/) {
foreach (@r) {
print "$1\n" if /^Location: (.+)/;
}
}
exit;
}
if ($opt_a !~ /^afex_\d+\.tar$/ and $file !~ /afex_\d+\.tar$/) {
# print grep({s/^(X-Recipient:.*\((.+)\))/Parameters: $2\n/i} @r);
my $nonot = 0;
my $recipient = '';
my @location = ();
foreach (@r) {
if ($useragent =~ /dox/) {
if (/^(X-)?(Recipient: \S+)/i) {
# $recipient = $2."\n";
}
if (m:^(X-)?(Location.*/dox/.*):i) {
push @location,$2."\n";
}
} else {
if (/^(X-)?(Recipient.*)/i) {
$recipient = $2."\n";
if (/notification=no/i) { $nonot = 1 }
else { $nonot = 0 }
}
if (/^(X-)?(Location.*)/i) {
push @location,$2."\n";
}
}
}
if ($from eq $to or $from =~ /^\Q$to\E@/i
or $nomail or $anonymous or $nonot)
{
if ($id =~ /^PKEY:/) {
$recipient =~ s/ \(.*//;;
print $recipient;
} else {
print $recipient if $useragent !~ /xxx/;
print @location if $to ne '.' and $to ne '+';
}
}
if (grep /\/__$vp/,@location) {
} elsif ($to eq '.') {
if ($useragent =~ /autofex|zip/) {
print @location;
exit;
} elsif ($useragent =~ /xxx/) {
if ("@location" =~ /(http\S+xxx_($xn)\S+)/) {
print "xxx -o $2\n";
}
if ($ENV{opt_w}) {
my $cmd;
foreach (@location) {
if (/(http\S+xxx_($xn)\S+)/) {
my $url = $1;
if ($url =~ /\.tar$/) {
print "FOP=$url\n";
$cmd = "wget -qO - \$FOP | tar -xvf -\n";
} elsif ($url =~ /\.(tar\.gz|tgz)$/) {
print "FOP=$url\n";
$cmd = "wget -qO - \$FOP | tar -xvzf -\n";
} elsif ($url =~ /.*\/(.+\.zip)$/) {
print "FOP=$url\n";
$cmd = "wget -c $url && unzip $1 && rm $1\n";
} elsif ($url =~ /\.txt$/) {
$cmd = "wget -qO - $url\n";
} else {
$cmd = "wget -c $url\n";
}
}
}
print $cmd;
}
exit;
} elsif (@location) {
print @location;
# my $cmd;
# foreach (@location) {
# if (/(http.*\.(tar|tg(z))$)/) {
# my $z = $3||'';
# print "FOP=$1\n";
# $cmd = "wget -qO - \$FOP | tar -xv$z -f -\n";
# } elsif (m:(http.*/(.+)):) {
# print "FOP=$1\n";
# $cmd = "rm -f $2; wget \$FOP\n";
# }
# }
# print $cmd;
}
} elsif ($share_) {
print @location;
}
foreach (@r) {
if (/<!-- %W: (.+) -->/) {
$_ = $1;
s/</</g;
s/>/>/g;
warn "Warning: $_\n";
}
}
print @location if $to eq '+';
}
}
if (@r and $from =~ /^fexmaster@/ and $from ne $to and $to ne '.' and
$useragent !~ m:/:)
{
foreach (@r) {
print "$1\n" if /^X-(Recipient: .+)/;
print "$1\n" if /^X-(Location: http.+)/;
}
}
# delete transfer tmp file
unlink $transferfile if $transferfile;
}
sub forward {
my (@r);
my ($to,$n,$dkey,$file,$req);
my ($status,$fp);
local $_;
# look for single @ in arguments
for (my $i=1; $i<$#ARGV; $i++) {
if ($ARGV[$i] eq '@') {
$ARGV[$i] = join(',',@ARGV[$i+1 .. $#ARGV]);
$#ARGV = $i;
last;
}
}
# if ($windoof and not @ARGV) { &inquire }
$to = pop @ARGV or die $usage;
$to = $from if $to eq '.';
if ($to !~ /@/ and $to ne $from) {
$to = get_mutt_alias($to);
}
open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
while (<$fexlist>) {
if (/#(\d+) (\w+) .\s*\d+ d. ([+-] )?(.+)/ and $1 eq $opt_f) {
$n = $1;
$dkey = $2;
$file = $4;
if ($file =~ s/ "(.*)"$//) {
$opt_C ||= $1 if $1 ne 'NOMAIL';
}
last;
}
}
close $fexlist;
unless ($n) {
die "$prg: file #$opt_f not found in fexlist\n";
}
female_mode("forward file #$opt_f?") if $opt_F;
serverconnect($server,$port);
query_sid($server,$port);
$req = "GET $proxy_prefix/fup?"
."from=$from&ID=$sid&to=$to&dkey=$dkey&command=FORWARD";
$req .= "&comment=$opt_C" if $opt_C;
$req .= "&keep=$opt_k" if $opt_k;
$req .= "&autodelete=$opt_D" if $opt_D;
$req .= "&$opt_X" if $opt_X;
$req .= " HTTP/1.1";
sendheader("$server:$port",$req);
http_response();
$fp = $file;
$fp =~ s/[^\w_.-]/.+/g; # because of UTF8 filename
$status = 1;
while (<$SH>) {
warn "<-- $_" if $opt_v;
if (/copy-forwarded to/) {
$status = 0;
print;
}
}
if ($status) {
die "$prg: server failed, rerun command with option -v\n";
}
exit;
}
sub renotify {
my (@r);
my ($to,$n,$dkey,$file,$req,$recipient);
local $_;
die $usage if @ARGV;
open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
while (<$fexlist>) {
if (/#(\d+) (\w+) .\s*\d+ d. (.+)/ and $1 eq $opt_N) {
$n = $1;
$dkey = $2;
last;
}
}
close $fexlist;
unless ($n) {
die "$prg: file #$opt_N not found in fexlist\n";
}
female_mode("resend notification for file #$opt_N?") if $opt_F;
serverconnect($server,$port);
query_sid($server,$port);
$req = "GET $proxy_prefix/fup?"
."from=$from&ID=$sid&dkey=$dkey&command=RENOTIFY"
." HTTP/1.1";
sendheader("$server:$port",$req);
http_response();
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^\s*$/;
if (/^X-Notify: (.+)\/(.+)\/(.+)/) {
$recipient = $1;
$file = $3;
}
}
if ($file) {
print "notification email for $file has been resent to $recipient\n";
} else {
if ($opt_v) {
die "$prg: server failed\n";
} else {
die "$prg: server failed, rerun command with option -v\n";
}
}
exit;
}
sub modify {
my (@r);
my ($n,$dkey,$file,$req);
local $_;
die $usage if @ARGV;
die $usage unless $opt_C or $opt_k or $opt_D;
open $fexlist,$fexlist or die "$prg: $fexlist - $!\n";
while (<$fexlist>) {
if ($opt_x =~ /^\d+$/) {
if (/#(\d+) (\w+) .\s*\d+ d. . (.+)/ and $1 eq $opt_x) {
$n = $1;
$dkey = $2;
$file = $3;
$file =~ s/ "(.*)"$//;
last;
}
} else {
if (/#(\d+) (\w+) .\s*\d+ d. . (.+)/ and $2 eq $opt_x) {
$n = $1;
$dkey = $2;
$file = $3;
$file =~ s/ "(.*)"$//;
last;
}
}
}
close $fexlist;
unless ($n) {
die "$prg: file #$opt_x not found in fexlist\n";
}
female_mode("modify file #$opt_x?") if $opt_F;
serverconnect($server,$port);
query_sid($server,$port);
$req = "GET $proxy_prefix/fup?"
."from=$from&ID=$sid&dkey=$dkey&command=MODIFY";
$req .= "&comment=$opt_C" if $opt_C;
$req .= "&keep=$opt_k" if $opt_k;
$req .= "&autodelete=$opt_D" if $opt_D;
$req .= " HTTP/1.1";
sendheader("$server:$port",$req);
http_response();
while (<$SH>) {
s/\n*$/\n/;
warn "<-- $_" if $opt_v;
print if /\Q$file/;
}
exit;
}
sub get_xx {
my $transferfile = shift;
my $ft = '';
local $_;
# get transfer file from FEX server
unless ($SH) {
serverconnect($server,$port);
query_sid($server,$port);
}
xxget($from,$sid,$transferfile);
# empty file?
unless (-s $transferfile) {
unlink $transferfile;
exit;
}
if ($ft = `file $transferfile 2>/dev/null`) {
if ($ft =~ /compressed/) {
rename $transferfile,"$transferfile.gz";
shelldo(ws("gunzip $transferfile.gz"));
}
$ft = `file $transferfile`;
}
# file command failed, so we look ourself into the file...
elsif (open $transferfile,$transferfile) {
read $transferfile,$_,4;
close $transferfile;
# gzip magic?
if (/\x1F\x8B\x08\x00/) {
rename $transferfile,"$transferfile.gz";
shelldo(ws("gunzip $transferfile.gz"));
# assuming tar
$ft = 'tar archive';
}
}
if ($ft =~ /tar archive/ and -t STDOUT) {
rename $transferfile,"$transferfile.tar";
$transferfile .= '.tar';
if ($opt_q) {
$_ = 'y';
} else {
print "Files in transfer-container:\n\n";
shelldo(ws("tar tvf $transferfile"));
print "\nExtract these files? [Yn] ";
$_ = <STDIN>;
}
if (/^n/i) {
print "keeping $transferfile\n";
} else {
my $untar = "tar xvf";
# if ($> == 0 and `tar --help 2>&1` =~ /gnu/) {
# $untar = "tar --no-same-owner -xvf";
# }
vsystem("$untar $transferfile && rm $transferfile");
die "$prg: error while untaring, see $transferfile\n" if -f $transferfile;
}
} else {
exec 'cat',$transferfile;
}
exit;
}
sub formdatapost {
my %P = @_;
my ($boundary,$filename,$length,$buf,$file,$fpsize,$resume,$seek,$nettest);
my ($flink);
my (@hh,@hb,@r,@pv,$to);
my ($bytes,$tbytes,$b,$t,$bt);
my ($t0,$t1,$t2,$tt,$tc);
my $readahead; # flag if pipe is read ahead
my $bs = 2**16; # blocksize for reading and sending file
my $fileid = int(time);
my $chunk = 0;
my $filesize = 0;
my $connection = '';
my $pct = '';
my $dittodir = '.';
my ($tar,$ditto,$aname,$atype,$list,$error,$location,$checkrecipient);
local $_;
$_ = $P{command}||'';
$checkrecipient = $_ if /CHECKRECIPIENT/i;
if (defined($file = $P{file})) {
$to = $AB{$P{to}} || $P{to}; # for gpg
if ($_ = $P{comment}) {
$_ = encode_utf8($_);
s/%/%25/g;
s/([^ -~])/urlencode($1)/eg;
$P{comment} = $_;
}
# special file: stream from STDIN
if ($opt_s) {
$filename = encode_utf8($file);
$filesize = -1;
} elsif (not $opt_a) {
# $opt_c and $opt_g are no longer active, because cannot resume
if ($opt_c) {
my ($if,$of);
$if = shellquote($file);
$transferfile = $fextmp . '/' . basename($file) . '.gz';
$of = shellquote($transferfile);
shelldo("gzip <$if>$of");
$filesize = -s $transferfile;
die "$prg: cannot gzip \"$file\"\n" unless $filesize;
$file = $transferfile;
} elsif ($opt_g) {
my ($if,$of);
$if = shellquote($file);
$transferfile = $fextmp . '/' . basename($file) . '.gpg';
$of = shellquote($transferfile);
shelldo("gpg -e -r $to <$if>$of");
$filesize = -s $transferfile;
die "$prg: cannot gpg \"$file\"\n" unless $filesize;
$file = $transferfile;
}
}
# special file: tar-on-the-fly
if (not $windoof and $opt_a and $file =~ /(.+)\.(tar|tgz|taz)$/) {
$aname = $1;
$atype = $2;
$list = "$fextmp/$aname.list";
$error = "$fextmp/$aname.error";
$tar = 'tar -c';
$tar .= 'v' unless $opt_q;
if (-t STDOUT and not $opt_q and
`tar --help 2>/dev/null` =~ /--index-file/)
{ $tar .= " --index-file=$list" }
foreach my $x (split('#',$xlist)) {
if ($x eq '.') {
$tar .= " --exclude='.*'";
} else {
$x =~ s/\'/\\\'/g;
$tar .= " --exclude='$x'";
}
}
$tar .= " -f-";
if ("@ARGV" =~ /^\[(\S+)\]$/) {
# [fileslist]
$tar .= ' -T '.shellquote($1);
} else {
foreach (@ARGV) {
s:/+$::;
$_ = '/' if /^$/;
$tar .= ' '.shellquote($_);
}
}
if ($atype eq 'taz') {
if (compressable(@ARGV)) {
$atype = 'tgz';
} else {
$atype = 'tar';
}
$opt_a =~ s/taz$/$atype/;
}
$tar =~ s/-cv/-cvz/ if $atype eq 'tgz';
# print "calculating archive size... ";
print "Making fex archive ($opt_a):\n" unless $opt_q;
warn "\$ $tar\n" if $opt_v;
open $tar,"$tar 2>$error|" or die "$prg: cannot run tar - $!\n";
$t0 = int(time) if -t STDOUT;
while ($b = read $tar,$_,$bs) {
$filesize += $b;
if ($t0) {
$t1 = int(time);
if (not $opt_q and -t STDOUT and $t1>$t0) {
printf "Archive size: %d MB\r",int($filesize/MB);
$t0 = $t1;
}
}
}
printf "Archive size: %d MB\n",int($filesize/MB) if -t STDOUT and not $opt_q;
unless (close $tar) {
$_ = '';
if (open $error,$error) {
local $/;
$_ = <$error>;
close $error;
}
unlink $list,$error;
die "$prg: tar error:\n$_";
}
unlink $error;
$file = "$aname.$atype";
$filename = encode_utf8($file);
undef $SH; # force reconnect (timeout!)
}
# special file: ditto-zip-on-the-fly
# ditto: Can't archive multiple sources
elsif ($macos and $opt_a and $file =~ /(.+)\.(zip)$/ and scalar(@ARGV) == 1)
{
$aname = $1;
$atype = $2;
$list = "$fextmp/$aname.list";
$error = "$fextmp/$aname.error";
$ditto = 'ditto -c -k --sequesterRsrc --keepParent';
if (-d "@ARGV" and "@ARGV" =~ m:^(.+)/(.+):) {
$dittodir = $1;
$file = $2;
$file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
$ditto .= ' '.$file;
} else {
foreach (@ARGV) {
$file = $_;
$file =~ s/([^\w\-\@\#%,.=+_:])/\\$1/g;
$ditto .= ' '.$file;
}
}
# print "calculating archive size... ";
debug("cd $dittodir;$ditto -");
open $ditto,"cd $dittodir;$ditto - 2>$error|"
or die "$prg: cannot run ditto - $!\n";
$t0 = int(time) if -t STDOUT;
while ($b = read $ditto,$_,$bs) {
$filesize += $b;
if ($t0) {
$t1 = int(time);
if (not $opt_q and -t STDOUT and $t1>$t0) {
printf "Archive size: %d MB\r",int($filesize/MB);
$t0 = $t1;
}
}
}
printf "Archive size: %d MB\n",int($filesize/MB) if -t STDOUT and not $opt_q;
unless (close $ditto) {
$_ = '';
if (-s $error and open $error,$error) {
local $/;
$_ = <$error>;
close $error;
}
unlink $list,$error;
die "$prg: ditto-zip error:\n$_";
}
unlink $error;
$file = "$aname.$atype";
$filename = encode_utf8($file);
undef $SH; # force reconnect (timeout!)
}
elsif ($P{to} eq 'nettest') {
$filename = $nettest = 'nettest';
$filesize = $P{size};
$fileid = 0;
}
# single file
elsif (not $opt_s) {
if (${'opt_^'} and my @s = stat $file) {
my @d = localtime($s[9]);
my $dt = sprintf('_%d%02d%02d_%02d%02d%02d',
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
${'opt_='} = $file;
${'opt_='} =~ s/(.+?)\./$1$dt./ or ${'opt_='} .= $dt;
}
$filename = encode_utf8(${'opt_='} || $file);
# $filename = urlencode($filename);
if ($windoof) {
$filename =~ s/^[a-z]://;
$filename =~ s/.*\\//;
}
$filename =~ s:.*/::;
$filename =~ s:[\r\n]+: :g;
if ($opt_d) {
$filesize = 0;
} elsif (not $opt_s) {
$filesize = -s $file or die "$prg: \"$file\" is empty or not readable\n";
}
}
unless ($opt_d or $nettest) {
if (0 and $opt_g) {
$filesize = -1;
$fileid = int(time);
} else {
if ($opt_a) {
# index file?
if ($tar and "@ARGV" =~ /^\[(\S+)\]$/) {
my $if = $1;
open $if,$if or die "$prg: cannot open $if - $!\n";
while (<$if>) {
chomp;
push @ifiles,$_;
}
close $if;
if (@ifiles) {
$fileid = md5_hex(fmd(@ifiles));
} else {
$fileid = int(time);
}
} else {
$fileid = md5_hex(fmd(@ARGV));
}
} elsif ($opt_s) {
$fileid = randstring(32);
} else {
$fileid = fileid($file);
}
}
}
} else {
$file = $filename = '';
$filesize = 0;
}
FORMDATAPOST:
@hh = (); # HTTP header
@hb = (); # HTTP body
@r = (); # HTTP reply
$seek = 0;
$resume = '';
$chunk++;
if (not $SH or $transferfile) {
serverconnect($server,$port);
query_sid($server,$port) unless $anonymous or $nettest;
}
$P{id} = $sid; # ugly hack!
$filename =~ s/\\/_/g; # \ is a illegal character for fexsrv
if ($file and not $xx and not $nettest) {
if ($opt_o and not $opt_d and $P{to} ne '+') {
# delete before overwrite
delete_file($from,$to,$filename);
serverconnect($server,$port);
query_sid($server,$port) unless $anonymous;
$P{id} = $sid; # ugly hack!
} elsif (not($opt_s or $opt_d or $opt_l or $opt_L or ${'opt_/'})) {
# ask server if this file has been already sent
($seek,$location) = query_file($server,$port,
$frecipient||$P{to},$P{from},$P{id},$filename,$fileid);
$seek = 0 if $P{to} eq '+' and $opt_o;
if ($filesize == $seek) {
unlink $list if $list;
if ($share) {
warn "$prg: archive $file is already in share $share\n";
exit;
} else {
warn "$prg: $file has been already transferred\n";
}
if ($location) {
if ($nomail and not $pkey or $share_ or $P{from} eq $P{to}) {
return "Location: $location\n";
}
if ($from =~ /^fexmaster@/) {
print "Location: $location\n";
}
return 0;
}
} elsif ($seek and $seek < $filesize) {
$resume = " (resuming at byte $seek)";
} elsif ($filesize <= $seek) {
$seek = 0;
}
if ($location and $P{to} eq '+') {
print "Location: $location\n";
}
}
if ($proxy) {
sleep 1; # do not overrun proxy
serverconnect($server,$port);
}
}
# file part size
if ($chunksize and ($proxy and $port != 443 or not $proxy)
and $filesize - $seek > $chunksize - $bs) {
if ($features !~ /MULTIPOST/) {
die "$prg: server does not support chunked multi-POST\n";
}
$opt_o = 0; # no overwriting mode for next chunks
$fpsize = $chunksize - $bs;
} else {
$fpsize = $filesize - $seek;
}
$boundary = randstring(48);
$P{seek} = $seek;
$P{filesize} = $filesize;
$P{keep} = '00' if defined $P{keep} and $P{keep} eq '0';
# send HTTP POST variables
if ($skey) {
$P{skey} = $skey;
@pv = qw'from to skey keep autodelete comment seek filesize';
} elsif ($gkey) {
$P{gkey} = $gkey;
@pv = qw'from to gkey keep autodelete comment seek filesize';
} elsif ($okey) {
$P{okey} = $okey;
@pv = qw'from to okey keep autodelete comment seek filesize';
} elsif ($pkey) {
$P{pkey} = $sid;
@pv = qw'from to pkey comment command seek filesize';
} else {
@pv = qw'from to id replyto keep autodelete comment command seek filesize';
}
foreach my $v (@pv) {
if ($P{$v}) {
my $name = uc($v);
push @hb,"--$boundary";
push @hb,"Content-Disposition: form-data; name=\"$name\"";
push @hb,"";
# push @hb,encode_utf8($P{$v});
push @hb,$P{$v};
}
}
# at last, POST the file
if ($file) {
push @hb,"--$boundary";
push @hb,"Content-Disposition: form-data; ".
qq'name="FILE"; filename="$filename"';
unless ($opt_d) {
if ($opt_M) { push @hb,"Content-Type: application/x-mime" }
else { push @hb,"Content-Type: application/octet-stream" }
if (${'opt_/'}) {
$flink = abs_path($file);
push @hb,"Content-Location: $flink";
} else {
push @hb,"Content-Length: $fpsize"; # optional header! NOT filesize!
push @hb,"X-File-ID: $fileid";
}
push @hb,"";
}
push @hb,"";
# prevent proxy chunked mode reply
$connection = "close";
}
push @hb,"--$boundary--";
if ($fpsize < 0) {
$length = $fpsize;
} else {
$length = length(join('',@hb)) + scalar(@hb)*2 + $fpsize;
}
if ($file and not $opt_d) {
if ($flink) { $hb[-2] = $flink }
else { $hb[-2] = '(file content)' }
}
# any other extra URL arguments
my $opt_X = '';
$opt_X = "?LOCALE=english" if $P{command};
$opt_X = "?$::opt_X" if $::opt_X and ($file or $checkrecipient);
# HTTP header
push @hh,"POST $proxy_prefix/fup$opt_X HTTP/1.1";
push @hh,"Host: $server:$port";
push @hh,"User-Agent: $useragent";
push @hh,"Content-Length: $length";
push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
push @hh,"Connection: $connection" if $connection;
push @hh,'';
$SIG{PIPE} = \&sigpipehandler;
# foreach $sig (keys %SIG) {
# eval '$SIG{$sig} = sub { print "\n!!! SIGNAL '.$sig.' !!!\n"; exit; }';
# }
if ($file) {
pop @hb;
pop @hb unless $flink;
nvtsend(@hh,@hb) or do {
warn "$prg: server has closed the connection, reconnecting...\n";
sleep 3;
undef $SH;
goto FORMDATAPOST; # necessary: new $sid ==> new @hh
};
if ($opt_v and not $opt_d) {
if ($flink) { warn "--> $flink\n" }
else { warn "--> (file content)\n" }
}
unless ($opt_d or $flink) {
$t0 = $t2 = int(time);
$tt = $t0-1;
$t1 = 0;
$tc = 0;
if ($opt_s) {
if ($opt_g) {
open $file,"gpg -e -r $to|" or die "$prg: cannot run gpg - $!\n";
} else {
open $file,'>&=STDIN' or die "$prg: cannot open STDIN - $!\n";
}
} elsif ($tar) {
unless ($readahead) {
if ($opt_g) {
warn "\$ $tar|gpg -e -r $to|\n" if $opt_v;
open $file,"$tar|gpg -e -r $to|" or die "$prg: cannot run tar&gpg - $!\n";
} else {
warn "\$ $tar|\n" if $opt_v;
open $file,"$tar|" or die "$prg: cannot run tar - $!\n";
}
if ($seek) {
print "Fast forward to byte $seek (resuming)\n";
readahead($file,$seek);
$readahead = 1;
# reconnect because of possible server timeout
shutdown($SH,2);
undef $SH;
sleep 1;
goto FORMDATAPOST; # necessary: new $sid ==> new @hh
}
}
} elsif ($ditto) {
unless ($readahead) {
$ditto =~ s/ditto/ditto -V/;
open $file,"cd $dittodir;$ditto -|" or die "$prg: cannot run ditto - $!\n";
if ($seek and not $readahead) {
print "Fast forward to byte $seek (resuming)\n";
readahead($file,$seek);
$readahead = 1;
# reconnect because of possible server timeout
shutdown($SH,2);
undef $SH;
sleep 1;
goto FORMDATAPOST; # necessary: new $sid ==> new @hh
}
}
} elsif ($nettest) {
#
} else {
if ($opt_g and not $tar) {
# my $fileq = shellquote($file);
# open $file,"gpg -e -r $to <$fileq|" or die "$prg: cannot run gpg - $!\n";
open $file,$file or die "$prg: cannot read \"$file\" - $!\n";
seek $file,$seek,0;
} else {
open $file,$file or die "$prg: cannot read \"$file\" - $!\n";
seek $file,$seek,0;
}
binmode $file;
}
$readahead = 0;
$bytes = 0;
$tbytes = $seek;
autoflush $SH 0;
print $rcamel[0] if ${'opt_+'};
$buf = '#' x $bs if $nettest;
$SIG{ALRM} = sub { retry("timed out") };
while ($bytes < $fpsize or $opt_s) {
if ($nettest) {
$b = $bs;
} else {
$b = read($file,$buf,$bs)||0;
last if $b == 0;
}
alarm($timeout*2);
if ($https) {
print {$SH} $buf or &sigpipehandler;
} else {
syswrite $SH,$buf or &sigpipehandler;
}
alarm(0);
$bytes += $b;
$tbytes += $b;
if (not $nettest and $filesize > 0 and $tbytes > $filesize) {
die "$prg: \"$file\" filesize has grown while uploading\n";
}
$bt += $b;
$t2 = time;
if (${'opt_+'} and int($t2*10)>$tc) {
print $rcamel[$tc%2+1];
$tc = int($t2*10);
}
if (int($t2)>$t1) {
&sigpipehandler unless $SH->connected;
show_tarindex($list,$pct);
if (not $opt_q and -t STDOUT) {
# smaller block size is better on slow links
$bs = 4096 if $t1 and $bs>4096 and $bytes/($t2-$t0)<65536;
if ($filesize > 0) {
$pct = sprintf "(%d%%)",int($tbytes/$filesize*100);
}
if ($tbytes>2*MB and $bs>4096) {
printf STDERR "%s: %d MB of %d MB %s %d kB/s \r",
$opt_s||$opt_a||$file,
int($tbytes/MB),
int($filesize/MB),
$pct,
int($bt/kB/($t2-$tt));
} else {
printf STDERR "%s: %d kB of %d MB %s %d kB/s \r",
$opt_s||$opt_a||$file,
int($tbytes/kB),
int($filesize/MB),
$pct,
int($bt/kB/($t2-$tt));
}
}
$t1 = $t2;
# time window for transfer rate calculation
if ($t2-$tt>10) {
$bt = 0;
$tt = $t2;
}
}
last if $filesize > 0 and $bytes >= $fpsize;
sleep 1 while ($opt_m and $bytes/kB/(time-$t0||1) > $opt_m);
}
show_tarindex($list,$pct);
warn "\n--> --$boundary--\n" if $opt_v;
$tt = ($t2-$t0)||1;
print $rcamel[2] if ${'opt_+'};
if ($useragent !~ /fexsync/ and not $opt_s and $fileid =~ /[a-z]/) {
if ($opt_a) {
my @files = @ARGV;
@files = @ifiles if @ifiles;
if ($fileid ne md5_hex(fmd(@files))) {
print "\n" unless $opt_q;
die "$prg: files have been modified while uploading\n";
}
} else {
if ($fileid ne fileid($file)) {
print "\n" unless $opt_q;
die "$prg: file has been modified while uploading\n";
}
}
}
if (not $chunksize and $tbytes < $filesize and $useragent !~ /fexsync/) {
die "$prg: \"$file\" filesize has shrunk while uploading\n";
}
unless ($opt_q) {
if ($seek or $chunksize and $chunksize < $filesize) {
if ($fpsize>2*MB) {
printf STDERR "%s: %d MB in %d s = %d kB/s",
$opt_s||$opt_a||$file,
int($bytes/MB),
int($tt)||1,
int($bytes/kB/$tt)||1;
if ($tbytes == $filesize) {
printf STDERR ", total %d MB\n",int($filesize/MB);
} else {
printf STDERR ", chunk #%d : %d MB\n",
$chunk,int($tbytes/MB);
}
} else {
printf STDERR "%s: %d kB in %d s = %d kB/s",
$opt_s||$opt_a||$file,
int($bytes/kB)||1,
int($tt)||1,
int($bytes/kB/$tt)||1;
if ($tbytes == $filesize) {
printf STDERR ", total %d kB\n",int($filesize/kB)||1;
} else {
printf STDERR ", chunk #%d : %d kB\n",
$chunk,int($tbytes/kB)||1;
}
}
} else {
if ($tbytes>2*MB) {
printf STDERR "%s: %d MB in %d s = %d kB/s \n",
$opt_s||$opt_a||$file,
int($bytes/MB),
int($tt)||1,
int($bytes/kB/$tt)||1;
} else {
printf STDERR "%s: %d kB in %d s = %d kB/s \n",
$opt_s||$opt_a||$file,
int($bytes/kB)||1,
int($tt)||1,
int($bytes/kB/$tt)||1;
}
}
}
}
autoflush $SH 1;
print {$SH} "\r\n--$boundary--\r\n";
# return if $nettest;
# special handling of streaming file because of stunnel tcp shutdown bug
if ($opt_s or 0 and $opt_g) {
print "waiting for server response" if -t STDOUT and not $opt_q;
for (1..3) {
shutdown($SH,2);
close $SH;
sleep 2;
print "." if -t STDOUT and not $opt_q;
serverconnect($server,$port);
query_sid($server,$port) unless $anonymous;
($seek,$location) =
query_file($server,$port,$P{to},$P{from},$sid,$filename,$fileid);
if ($seek == $bytes) {
print "\r \r" if -t STDOUT and not $opt_q;
return "Location: $location\n";
}
}
if ($location) {
die "$prg: streamed $bytes bytes but server received $seek bytes\n";
}
print "\r \r" if -t STDOUT and not $opt_q;
return '';
}
if ($flink) {
$bytes = -s $flink;
if ($bytes>2*MB) {
printf STDERR "%s: %d MB\n",$flink,int($bytes/MB);
} else {
printf STDERR "%s: %d kB\n",$flink,int($bytes/kB);
}
}
} else {
autoflush $SH 1;
nvtsend(@hh,@hb) or die "$prg: server has closed the connection\n";
}
# SuSe: Can't locate object method "BINMODE" via package "IO::Socket::SSL::SSL_HANDLE"
# binmode $SH,':utf8';
# if (not $opt_q and $file and -t STDOUT) {
# print STDERR "\r \r";
# }
my $cl = '';
if (-t STDOUT and not ($opt_s or $nettest) or $useragent =~ /fexsync/) {
unless ($opt_q) {
print STDERR "waiting for server ok...";
$cl = "\r \r";
}
}
while (<$SH>) {
s/[\r\n]+//g;
print STDERR $cl; $cl = '';
warn "<-- $_\n" if $opt_v;
if (@r) {
last if $r[0] =~ / (204|222) / and /^$/ or /<\/html>/i;
last if /^$/ and grep /^Connection: close/,@r and grep /^X-Location/,@r;
}
push @r,decode_utf8($_);
}
print STDERR $cl; $cl = '';
if ($file) {
close $SH;
undef $SH;
if ($chunksize and $tbytes < $filesize) {
if ($sleepwait) {
my $wait = $sleepwait;
while ($wait) {
print STDERR "next connect in $wait s \r" if -t STDOUT;
sleep 1;
$wait--;
}
}
$readahead = 1;
goto FORMDATAPOST;
}
close $file if fileno $file; # unless $nettest or $opt_d or $flink;
}
return @r;
}
sub show_tarindex {
my $list = shift;
my $pct = shift;
my $nl = '';
local $_;
if ($list) {
unless (fileno $list) {
open $list,$list and unlink $list;
}
if (fileno $list) {
$nl = "\n" if $pct;
while (<$list>) {
print $nl,$_;
$nl = '';
}
}
}
}
sub randstring {
my $n = shift;
my @rc = ('A'..'Z','a'..'z',0..9 );
my $rn = @rc;
my $rs;
for (1..$n) { $rs .= $rc[int(rand($rn))] };
return $rs;
}
sub zip {
no strict 'refs';
my $zip = shift;
my $du = 0;
my ($cmd);
my @q;
local $_;
warn "\$ find -type f @_\n" if $opt_v;
if (not $opt_q and open my $F,'-|','find',@_,qw'-type f') {
my $xp = '';
foreach my $x (split('#',$xlist)) {
if ($x eq '.') {
$xp .= '|.*/\.';
} else {
$x = quotemeta $x;
$x =~ s/\\\?/./g;
$x =~ s/\\\*/.*/g;
$xp .= "|$x|.*/$x";
}
}
$xp =~ s/.//;
while (<$F>) {
chomp;
unless ($xp and /^$xp$/) {
$du += -s;
}
}
close $F;
$du = int($du/1024/1024);
print "$du MB\n";
if ($du >= 2048) {
print "zip cannot handle more than 2 GB correctly, better use 7z\n";
print "continue anyway? ";
$_ = <STDIN>||'';
exit 1 if /^n/i;
}
}
unlink $zip;
# if ($opt_c) { $cmd = "zip -@ $zip" }
# else { $cmd = "zip -0 -@ $zip" }
$cmd = "zip -r -y -@ $zip -x";
$cmd =~ s/ / -0 / if $opt_0 or not compressable(@_);
foreach my $x (split('#',$xlist)) {
if ($x eq '.') {
$cmd .= " '*/.*' '*/.*/*'";
} else {
$x =~ s/\'/'"\'"'/g;
$cmd .= " '$x' '*/$x' '*/$x/*'";
}
}
warn "\$ $cmd\n" if $opt_v;
open $cmd,"|$cmd" or die "$prg: cannot create $zip - $!\n";
foreach (@_) {
print {$cmd} $_."\n";
warn " $_\n" if $opt_v;
}
close $cmd or warn "$prg: zip failed - $!\n";
return $zip;
}
sub getline {
my $file = shift;
local $_;
while (<$file>) {
chomp;
s/^#.*//;
s/\s+#.*//;
s/^\s+//;
s/\s+$//;
return $_ if length($_);
}
return '';
}
sub query_file {
my ($server,$port,$to,$from,$qid,$filename,$fileid) = @_;
my ($head,$response,$fexsrv,$cc);
my $seek = 0;
my $qfileid = '';
my $location = '';
local $_;
$to =~ s/[,:].*//;
$to = $AB{$to} if $AB{$to};
$to = $from if $to eq '.';
$filename = urlencode($filename);
if ($skey) {
$head = "HEAD $proxy_prefix/fop/$to/$from/$filename??SKEY=$qid HTTP/1.1";
} elsif ($gkey) {
$head = "HEAD $proxy_prefix/fop/$to/$from/$filename??GKEY=$qid HTTP/1.1";
} elsif ($okey) {
$head = "HEAD $proxy_prefix/fop/$to/$from/$filename??OKEY=$qid HTTP/1.1";
} elsif ($share) {
my $qid = $pkey || $id;
$head = sprintf "HEAD $proxy_prefix/fop/$to/$share/$from/%s/%s HTTP/1.1",
md5_hex("$filename:$qid"),$filename;
# die "$filename:$qid\n$head";
} else {
$head = "HEAD $proxy_prefix/fop/$to/$from/$filename??ID=$qid HTTP/1.1";
}
sendheader("$server:$port",$head);
$_ = <$SH>;
unless (defined $_ and /\w/) {
die "$prg: no response from server\n";
}
s/\r//;
warn "<-- $_" if $opt_v;
unless (/^HTTP.* 200/) {
s:HTTP/[\d\. ]+::;
$response = $_;
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
$fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
last if /^\s*$/;
}
die "$prg: no fexserver at $server:$port\n" unless $fexsrv;
chomp $response;
if ($share) {
die "$prg: no share support on $server:$port ($response)\n";
} else {
die "$prg: response from $server:$port : $response\n";
}
}
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^$/;
if (/^Content-Length:\s+(\d+)/) { $seek = $1 }
if (/^X-File-ID:\s+(.+)/) { $qfileid = $1 }
if (/^X-Features:\s+(.+)/) { $features = $1 }
if (/^X-Location:\s+(.+)/) { $location = $1 }
if (/^Connection: close/) { $cc = $_ }
}
# return true seek only if file is identified
$seek = 0 if $qfileid and $qfileid ne $fileid;
if ($cc) {
serverconnect($server,$port);
$sid = $id;
}
return ($seek,$location);
}
sub edit_address_book {
my ($user) = @_;
my $alias;
my $ab = "$fexhome/ADDRESS_BOOK";
my (%AB,@r);
local $_;
die "$prg: address book not available for subusers\n" if $skey;
die "$prg: address book not available for onetime users\n" if $okey;
die "$prg: address book not available for group members\n" if $gkey;
die "$prg: address book not available for share users\n" if $pkey;
unless (-t STDOUT) {
&show_address_book;
exit;
}
female_mode("edit your address book?") if $opt_F;
%AB = query_address_book($server,$port,$user);
if ($AB{ADDRESS_BOOK} !~ /\w/) {
$AB{ADDRESS_BOOK} =
"# Format: alias email-address # Comment\n".
"# Example:\n".
"framstag framstag\@rus.uni-stuttgart.de\n";
}
open $ab,">$ab" or die "$prg: cannot write to $ab - $!\n";
print {$ab} $AB{ADDRESS_BOOK};
close $ab;
system "$editor $ab";
exit unless -s $ab;
$opt_o = $opt_A;
serverconnect($server,$port);
query_sid($server,$port);
@r = formdatapost(
from => $user,
to => $user,
id => $sid,
file => $ab,
);
unlink $ab,$ab.'~';
}
sub query_address_book {
my ($server,$port,$user) = @_;
my ($req,$alias,$address,$options,$comment,$cl,$ab,$b);
my %AB;
local $_;
unless ($SH) {
serverconnect($server,$port);
query_sid($server,$port);
}
$req = "GET $proxy_prefix/fop/$user/$user/ADDRESS_BOOK?ID=$sid HTTP/1.1";
sendheader("$server:$port",$req);
$_ = <$SH>;
unless (defined $_ and /\w/) {
die "$prg: no response from server\n";
}
s/\r//;
warn "<-- $_" if $opt_v;
unless (/^HTTP.* 200/) {
if (/^HTTP.* 404/) {
while (<$SH>) { last if /^\r?\n/ }
return;
} else {
# s:HTTP/[\d\. ]+::;
# die "$prg: server response: $_";
close $SH;
undef $SH;
return ();
}
}
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
last if /^$/;
$cl = $1 if /^Content-Length: (\d+)/;
}
if ($cl) {
while (<$SH>) {
$b += length;
$ab .= $_;
s/[\r\n]//g;
s/^\s+//;
s/\s+$//;
warn "<-- $_\n" if $opt_v;
s/\s*#\s*(.*)//;
if ($_) {
$comment = $1||'';
($alias,$address,$options) = split;
if ($address) {
if ($options) { $options =~ s/[()]//g }
else { $options = '' }
$AB{$alias} = $address;
$AB{$alias}->{options} = $options||'';
$AB{$alias}->{comment} = $comment||'';
if ($options and $options =~ /keep=(\d+)/i) {
$AB{$alias}->{keep} = $1;
}
if ($options and $options =~ /autodelete=(\w+)/i) {
$AB{$alias}->{autodelete} = $1;
}
}
}
last if $b >= $cl;
}
}
$AB{ADDRESS_BOOK} = $ab;
return %AB;
}
# sets global $sid $features $timeout # ugly hack! :-}
sub query_sid {
my ($server,$port) = @_;
my ($req,$fexsrv,$cc);
local $_;
$sid = $id;
if ($port eq 443 or $proxy) {
return if $opt_d;
return if $features; # early return if we know enough
$req = "OPTIONS /FEX HTTP/1.1"; # does not work with (some) proxies
$req = "GET /SID HTTP/1.1"; # needed as FEATURES query
} else {
$req = "GET /SID HTTP/1.1";
}
sendheader("$server:$port",$req,"X-User: $from");
$_ = <$SH>;
unless (defined $_ and /\w/) {
warn "\n" if $opt_v;
die "$prg: no response from server\n";
}
s/\r//;
warn "<-- $_" if $opt_v;
if (/^HTTP.* [25]0[01] /) {
if ($port ne 443 and not $okey and $FEXOPT !~ /\bNOSID\b/i) {
if (/^HTTP.* 201 (.+)/ and not $proxy) {
$sid = 'MD5H:'.md5_hex($id.$1);
} else {
warn "$prg: no SID available, sending auth-ID unencrypted\n";
}
}
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
$features = $1 if /^X-Features: (.+)/;
$timeout = $1 if /^X-Timeout: (\d+)/;
$cc = $_ if /^Connection: close/;
last if /^\n/;
}
if ($cc) {
serverconnect($server,$port);
$sid = $id;
}
} elsif (/^HTTP.* 301 /) {
while (<$SH>) { last if /Location/ }
die "$prg: cannot use $server:$port because server has a redirection to\n".$_;
} else {
# no SID support - perhaps transparent web proxy?
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
$fexsrv = $_ if /^(Server: fexsrv|X-Features:)/;
last if /^\s*$/;
}
die "$prg: no fexserver at $server:$port\n" unless $fexsrv;
serverconnect($server,$port);
$sid = $id;
}
# warn "proxy: $proxy\n";
if ($proxy) {
serverconnect($server,$port);
$sid = $id;
}
}
sub xxget {
my ($from,$id,$save) = @_;
my $bs = 4096;
my $xx = $save;
my ($url,$B,$b,$t0,$t1,$cl);
my ($ts,$tso);
local $_;
$xx =~ s:.*/::;
$url = "$proxy_prefix/fop/$from/$from/$xx?ID=$id";
sendheader("$server:$port","GET $url HTTP/1.0");
http_response();
while (<$SH>) {
s/\r//;
warn "<-- $_" if $opt_v;
$cl = $1 if /^Content-Length:\s(\d+)/;
# $ft = $1 if /^X-File-Type:\s(.+)/;
last if /^$/;
}
die "$prg: no Content-Length in server-reply\n" unless $cl;
open $save,">$save" or die "$prg: cannot write to $save - $!\n";
binmode $save;
$t0 = $t1 = int(time);
$tso = '';
while ($b = read($SH,$_,$bs)) {
$B += $b;
print {$save} $_;
if (int(time) > $t1) {
$t1 = int(time);
$ts = ts($B,$cl);
if ($ts ne $tso) {
print STDERR $ts,"\r";
$tso = $ts;
}
}
sleep 1 while ($opt_m and $B/kB/(time-$t0||1) > $opt_m);
}
print STDERR ts($B,$cl),"\n";
close $save;
}
# transfer status
sub ts {
my ($b,$tb) = @_;
return sprintf("transferred: %d MB (%d%%)",int($b/MB),int($b/$tb*100));
}
sub sigpipehandler {
retry("died");
}
sub retry {
my $reason = shift;
local $SIG{ALRM} = sub { };
if (fileno $SH) {
alarm(1);
my @r = <$SH>;
alarm(0);
if (@r and $opt_v) {
die "\n$prg: ($$) server error: @r\n";
}
if (@r and $r[0] =~ /^HTTP.* \d+ (.*)/) {
die "\n$prg: server error: $1\n";
}
}
$timeout *= 2;
warn "\n$prg: connection to $server $reason\n";
if ($opt_s) { exit 3 }
warn "retrying after $timeout seconds...\n";
sleep $timeout;
if ($windoof) { exec $^X,$prg,@_ARGV }
else { exec $_0,@_ARGV }
die $!;
}
sub checkrecipient {
my ($from,$to) = @_;
my @r;
local $_;
@r = formdatapost(
from => $from,
to => $to,
id => $sid,
autodelete => $opt_D,
keep => $opt_k,
command => 'CHECKRECIPIENT',
);
$_ = shift @r or die "$prg: no reply from server\n";
if (/ 2\d\d /) {
return if $to eq 'nettest';
foreach (@r) {
last if /^$/;
if (s/X-(Recipient: .+)/$1\n/) {
s/autodelete=\w+/autodelete=$opt_D/ if $opt_D;
s/keep=\d+/keep=$opt_k/ if $opt_k;
print unless $opt_q;
$frecipient ||= (split)[1];
}
}
} else {
http_response($_,@r);
}
}
# get ID data from ID file
sub get_id {
my $idf = shift;
$fexcgi = getline($idf) || die "$prg: no FEX-URL in $idf\n";
$from = getline($idf) || die "$prg: no FROM in $idf\n";
$id = getline($idf) || die "$prg: no ID in $idf\n";
if ($fexcgi =~ s/!([\w.-]+:\d+)(:(\d+))?//) {
$proxy = $1;
$chunksize = $3 || 0;
}
unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
die "$prg: illegal FEX-URL \"$fexcgi\" in $idf\n";
}
unless ($from =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
die "$prg: illegal FROM \"$from\" in $idf\n";
}
$fexcgi =~ s:/+$::;
}
# for windows
sub inquire {
my ($file,$to);
unless (@ARGV) {
for (;;) {
$file = inputline("file or directory to send: ");
exit unless length $file;
$file =~ s/^'(.+)'$/$1/;
last if -e $file;
warn "$file does not exist\n";
}
if (-d $file) {
my $dir = dirname($file);
$file = basename($file);
chdir $dir or die "$prg: $dir - $!\n";
$opt_a ||= "$file.zip";
}
@ARGV = ($file);
}
$to = inputline("recipient (email address): ");
unless (length $to) {
$opt_n = $to = '.';
}
$to =~ s/\s+/,/g;
$to =~ s/,,+/,/g;
$to =~ s/^,//;
$to =~ s/,$//;
push @ARGV,$to;
unless ($opt_n or $opt_C) {
$opt_C = inputline("comment: ");
}
}
sub shelldo {
warn "\$ @_\n" if $opt_v;
if (system(@_) < 0) { die "failed: @_\n" }
}
# emulate seek on a pipe
sub readahead {
my $fh = shift; # filehandle
my $ba = shift; # bytes ahead
my $bs = 2**16;
my $s = 0;
my $n;
local $_;
while ($s < $ba) {
$n = $ba-$s;
$n = $bs if $n > $bs;
$s += read $fh,$_,$n;
}
}
sub fileid {
my $file = shift;
my $dirmode = shift;
my @s = $dirmode ? lstat($file) : stat($file);
if (@s) {
return md5_hex($file.$s[0].$s[1].$s[7].$s[9]);
} else {
warn "$prg: $file - $!\n";
return int(time);
}
}
sub get_mutt_alias {
my $to = shift;
my $ma = $HOME.'/.mutt/aliases';
my ($alias,$options);
local $_;
$to =~ s/(:.+)// and $options = $1;
open $ma,$ma or return $to;
while (<$ma>) {
if (/^alias \Q$to\E\s/i) {
chomp;
s/\s*#.*//;
s/\(.*?\)//;
s/\s+$//;
s/.*\s+//;
s/[<>]//g;
if (/,/) {
warn "$prg: ignoring mutt multi-alias $to = $_\n";
last;
}
if (/@/) {
$alias = $_;
warn "$prg: found mutt alias $to = $alias\n";
$alias .= $options if $options;
last;
}
}
}
close $ma;
$to = "$to:$options" if $options;
return ($alias||$to);
}
# collect (hashed) file meta data
sub fmd {
my @files = @_;
my ($file,$dir);
my $fmd = '';
foreach $file (@files) {
if (not -l $file and -d $file) {
$dir = $file;
next if $dir =~ m:/\.fex/tmp$:;
if (opendir $dir,$dir) {
while (defined($file = readdir($dir))) {
next if $file eq '..';
next if $xp and $file =~ /^($xp)$/;
if ($file eq '.') {
$fmd .= fileid($dir);
} elsif (-l "$dir/$file") {
# hack for dangling symlinks: do not raise an error
$fmd .= fileid("$dir/$file",'dirmode');
} else {
$fmd .= fmd("$dir/$file");
}
}
closedir $dir;
}
} else {
$fmd .= fileid($file,'dirmode');
}
}
return $fmd;
}
sub female_mode {
local $_;
if (open my $tty,'/dev/tty') {
print "@_\n";
print " [y] yes\n",
" [n] no\n",
" [p] perhaps - don't know\n",
"your choice: ";
$_ = <$tty> || '';
close $tty;
if (/^y/i) { return }
if (/^n/i) { exit }
if (/^p/i) { int(rand(2)) ? return : exit }
female_mode(@_);
}
}
sub http_response {
local $_ = shift || <$SH>;
my @r = @_;
my $error;
$_ = <$SH> unless $_;
unless (defined $_ and /\w/) {
die "$prg: no response from server\n";
}
s/\r?\n//;
# warn "<-- $_\n" if $opt_v;
# CGI fatalsToBrowser
if (/^HTTP.* 500/) {
@r = <$SH> unless @r;
@r = () unless @r;
die "$prg: server error: $_\n@r\n";
}
unless (/^HTTP.* 200/) {
$error = $_;
$error =~ s/HTTP.[\s\d.]+//;
if (defined($SH)) {
@r = <$SH> unless @r;
@r = () unless @r;
}
foreach (@r) {
chomp;
$error .= "\n".$_ if /^Location/;
warn "<-- $_\n" if $opt_v;
}
die "$prg: server error: $error\n";
}
if ("@r" =~ /INTERNAL ERROR.*<pre>(.+)<\/pre>/s) {
die "$prg: server error: $1\n";
}
return $_;
}
# check (heuristcally) if files should be compressed
sub compressable {
my $uf = 1;
my $cf = 1;
my $cfx = 'gif|jpg|png|avi|mp\d|flv|m4v|ogg|tar|t?gz|zip|7z|bz2|rar|iso';
local $_;
if ("@_" =~ /^\[(.+)\]$/) {
my $flist = $1;
if (open $flist,$flist) {
while (<$flist>) {
chomp;
if (-f) {
if (/\.($cfx)$/i) {
$cf += -s;
} else {
$uf += -s;
}
}
}
close $flist;
}
} else {
warn "\$ find @_ -type f|\n" if $opt_v;
my @find = ('find',@_,qw'-type f');
foreach my $x (split('#',$xlist)) {
$x = '.*' if $x eq '.';
$_ = "( ! -path */$x -prune )";
push @find,split;
}
if (open my $find,'-|',@find) {
while (<$find>) {
chomp;
if (/\.($cfx)$/i) {
$cf += -s;
} else {
$uf += -s;
}
}
close $find;
}
}
if ($uf+$cf < 2**31 and $uf/$cf > 0.2) {
return 1;
} else {
return 0;
}
}
sub ws {
local $_ = shift;
return split;
}
sub update {
my $cfb = '### common functions ###';
my $cfc;
local $/;
open $prg,$prg or die "cannot read $prg - $!\n";
$cfc = <$prg>;
close $prg;
$cfc =~ s/.*\n$cfb\n//s;
my @p = qw(fexget sexsend);
foreach my $p (@p) {
if (open $p,$p) {
$_ = <$p>;
close $p;
s/\n$cfb.*/\n$cfb\n$cfc/s;
system "vv -s $p";
open $p,'>',$p or die "cannot write $p - $!\n";
print {$p} $_;
close $p;
} else {
warn "cannot read $p - $!\n";
}
}
exec "l fexsend @p";
exit;
}
# newest modification time
sub nmtime {
my ($file,$dir);
my @files;
my @s;
local $_;
if (scalar(@_) == 1 and not -d $_[0]) {
if (@s = stat $_[0]) {
$nmtime = $s[9] if $s[9] > $nmtime;
}
} else {
foreach $file (@_) {
@files = ();
if (-d $file and opendir $dir,$file) {
while (defined($_ = readdir $dir)) {
next if /^\.\.?$/;
next if $xp and /^($xp)$/;
push @files, "$file/$_";
}
closedir $dir;
nmtime(@files);
} else {
if (@s = lstat $file) {
$nmtime = $s[9] if $s[9] > $nmtime;
}
}
}
}
}
sub xxx {
my (%fp,%list);
my $clear = `clear`;
my $first = 1;
$ENV{FUA} = 'xxx';
my @fexget = ($fexget);
push @fexget,'-o' if $opt_o;
push @fexget,'-m',$opt_m if $opt_m;
unless ($FEXID = $ENV{FEXXX}||$ENV{FEXID}) {
if (open $idf,$idf) {
get_id($idf);
while (<$idf>) {
if (/^\[xx\]/) {
get_id($idf);
last;
}
}
close $idf;
$FEXID = encode_b64("$fexcgi $from $id");
}
# $FEXID = $1 if `xx -I</dev/null` =~ /FEXXX=(.+)/;
}
if ($opt_I) {
if ($FEXID) {
print "export FEXXX='$FEXID';history -d \$((HISTCMD-1))\n";
} else {
die "$prg: no FEXID\n";
}
exit;
}
if ($FEXID) {
if (($ENV{FEXID}||'') ne $FEXID) {
warn "\$ export FEXID='$FEXID'\n" if $opt_v;
$ENV{FEXID} = $FEXID;
}
} else {
die "$prg: no FEXID\n";
}
if ($opt_x) {
my $xi;
# "USER=\$(id|sed 's/).*//;s/.*(//')",
$xi .= "$_\n" foreach (
'FEXBIN=/tmp/$USER/.fex/tmp;',
'mkdir -p $FEXBIN && cd $FEXBIN &&',
'wget -qO - fex.belwue.de/download/xxx.tgz | tar xzf -;',
'test "$UID" = 0 && chown -h root:root $FEXBIN/*;',
'PATH="$FEXBIN:$PATH";',
"export FEXID='$FEXID';",
'ls -l'
);
my $fx = "| $fexsend -k 1 -s xxx.bash .";
warn "$fx\n" if $opt_v;
if ($opt_X and open $fx,$fx) {
warn $xi if $opt_v;
print {$fx} $xi;
close $fx;
} else {
print "# copy these bash commands to your other account to temporary\n";
print "# install the F*EX clients (including xxx) with your F*EX ID\n";
$xi =~ s/;\n/\n/g;
$xi =~ s/(FEXID=.*)/$1;history -d \$((HISTCMD-1))/;
print $xi;
}
exit;
}
if ($opt_u) {
$_ = vsystem("$fexsend -~ GENUKEY|");
if ($? == 0 and /FUP=(http.+)/) {
my $fup = $1;
print "# to get (one day valid) upload function, copy&paste:\n";
print "eval \$(curl -s $fup)\n";
print if $opt_v and /\n/;
}
exit $?;
}
if ($opt_l or $opt_L) {
my @xxx = qw'
which xxx || xxx() {
case $1 in
*.tgz) wget -qO - $1|tar xvzf -;;
*.tar) wget -qO - $1|tar xvf -;;
*.gz) wget -qO - $1|gunzip;;
*.txt) wget -qO - $1;;
esac;
}
';
warn "\$ $fexsend -L .|\n" if $opt_v;
open my $p,"$fexsend -L .|" or exit 1;
while (<$p>) {
if (my ($size,$keep,$url,$n,$container,$comment) =
m:(\d+ MB) \(\s*(\d+ d)\) (http.*/xxx_($xn)\.(\S+)) "(.+)":)
{
if (not $opt_w and @xxx) {
print "@xxx\n";
@xxx = ();
}
print "\n$comment ($size, $keep)\n";
if ($opt_w) {
# print "xxx $n\n";
print "xxx=$url\n";
if ($container eq 'txt') {
print "wget -qO- \$xxx\n";
} elsif ($container eq 'gz') {
print "wget -qO- \$xxx | gunzip\n";
} elsif ($container eq 'tar') {
print "wget -qO- \$xxx | tar xvf -\n";
} elsif ($container eq 'tgz' or $container eq 'tar.gz') {
print "wget -qO- \$xxx | tar xvzf -\n";
} elsif ($container eq 'zip') {
my $zip = "xxx_$n.zip";
print "wget -c \$xxx && unzip $zip && rm $zip\n";
} else {
print "wget -c \$xxx\n";
}
} else {
print "xxx $url\n";
}
}
}
exit;
}
if ($opt_k and "@ARGV" =~ /($xn)/) {
my $x = $1;
my $n = 0;
warn "\$ $fexsend -l|\n" if $opt_v;
open my $p,"$fexsend -l|" or exit 1;
while (<$p>) {
if (/#(\d+).* xxx_($x)/) {
$n = $1;
}
}
close $p;
if ($n) {
vexec($fexsend,'-x',$n,'-k',$opt_k);
exit $?;
} else {
die "$prg: archive not found\n";
}
}
# get or delete named archive
if ("@ARGV" =~ /^($xn|-)$|^(http.*xxx_$xn\S+)/) {
my $n = $1||'';
my $url = $2;
my $fpl;
unless ($url) {
warn "\$ $fexsend -L .|\n" if $opt_v;
open my $p,"$fexsend -L .|" or exit 1;
while (<$p>) {
$url = $1 if m:(http\S+/xxx_$n\.\S+):;
$fpl = $_ if m:(http\S+/xxx_$xn\.\S+):;
}
close $p;
if ($n eq '-' and $fpl =~ m:(\d+ MB).*?(http\S+/xxx_$xn\.\S+) (".+"):) {
$url = $2;
}
}
if ($url) {
if ($opt_d) {
vexec($fexget[0],'-d',$url);
} else {
if ($url =~ /xxx_$xn\.txt$/) {
vexec("@fexget -s- $url");
} elsif ($url =~ /xxx_$xn\.gz$/) {
vexec("@fexget -s- $url | gunzip");
} else {
if (-t STDOUT) {
push @fexget,'-o' if $n eq '-';
} else {
push @fexget,'-s-';
}
vexec(@fexget,$url);
}
}
exit $?;
} else {
die "$prg: no such archive\n";
}
}
# delete archive with regexp
if ($opt_d and "@ARGV" =~ /(.+)/) {
my $x = $1;
my (%url,%comment);
warn "\$ $fexsend -L .|\n" if $opt_v;
open my $p,"$fexsend -L .|" or exit 1;
while (<$p>) {
if (/(http\S+\/xxx_($xn)\.\w+) "(.*$x.*)"/) {
$url{$2} = $1;
$comment{$2} = $3;
}
}
close $p;
if (%url) {
foreach (sort keys %url) {
if (/(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)/) {
print "$1-$2-$3 $4:$5:$6 $comment{$_}\n";
}
}
print "delete? ";
for (;;) {
my $k = ReadKey(0);
if ($k eq 'n') {
print "no\n";
exit;
}
if ($k eq 'y') {
print "yes\n";
last;
}
}
foreach my $url (sort keys %url) {
print "\n";
vsystem($fexget[0],'-d',$url{$url});
sleep 1;
}
}
exit;
}
if ($opt_g or $opt_d) {
die $usage if @ARGV or not -t STDIN;
my $long = 0;
my $showurl = 0;
my $dl = "\r".(" "x60)."\r";
for (;;) {
my $n = 0;
my $i = 0;
my $k = '';
my $last = '';
my $input = '';
my %fp = ();
my %dkey = ();
my %list = ();
my @xxx = ();
warn "\$ $fexsend -L .|\n" if $opt_v;
open my $p,"$fexsend -L .|" or exit 1;
while (<$p>) {
chomp;
if (m:http\S+/xxx_$xn:) {
s/ //;
s/\(//;
s/\)//;
s/\"//g;
push @xxx,$_;
}
}
close $p;
$i = $n = scalar(@xxx) or die "$prg: no stored archives found\n";
print "\n";
foreach (@xxx) {
if (s:((http\S+)/fop/(\w+)/xxx_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)\.(\w+)) ::) {
$fp{$i} = $1;
$dkey{$i} = $3;
$list{$i} = $_ = sprintf "%3s $4-$5-$6 $7:$8:$8 %s\n","#$i",$_;
s/\d\d\d\d-.*?\[/\[/ unless $long;
print;
printf " %s\n",$fp{$i} if $showurl;
$i--;
}
}
for (;;) {
print $dl;
print "get" if $opt_g;
print "delete" if $opt_d;
print "list" if $opt_l;
print "keep" if $opt_k;
if ($input) {
print " #$input";
} else {
print " # (or enter h for help): ";
}
last if $last;
$k = ReadKey(0);
if ($k eq "\n") {
$input ||= 'q' if $first;
last if $input;
redo;
}
$first = 0;
unless ($input) {
# first input character
if ($k eq ' ') {
$input = $k;
last;
}
if ($k eq 'q') {
print "q\n";
exit;
}
if ($k eq "h" or $k eq "?") {
print $dl;
if ($opt_g) {
print "#- get archive # and overwrite existing files\n";
print "[v] toggle view format\n";
print "[u] toggle show URL\n";
print "[d] delete\n";
print "[k] keep\n";
print "[l] list content\n";
}
if ($opt_d) {
print "#-# delete archives from # to #\n";
print "[v] toggle view format\n";
print "[u] toggle show URL\n";
print "[g] get\n";
print "[k] keep\n";
print "[l] list content\n";
}
if ($opt_k) {
print "# set new keep time for #\n";
print "[v] toggle view format\n";
print "[u] toggle show URL\n";
print "[g] get\n";
print "[d] delete\n";
print "[l] list content\n";
}
if ($opt_l) {
print "[v] toggle view format\n";
print "[u] toggle show URL\n";
print "[g] get\n";
print "[d] delete\n";
print "[k] keep\n";
}
print "[q] quit\n";
print "[SPACE] reload overview\n";
redo;
}
if ($opt_g and $k eq "-") {
$last = $input = 1;
push @fexget,'-o';
redo;
}
if ($k eq "v" or $k eq "u") {
# print "\r",' 'x40,"\n";
print $clear;
$long = not $long if $k eq "v";
$showurl = not $showurl if $k eq "u";
for (my $i=$n;$i;$i--) {
$_ = $list{$i};
s/\d\d\d\d-.*?\[/\[/ unless $long;
print;
printf " %s\n",$fp{$i} if $showurl;
}
redo;
}
if ($k eq "d") {
$opt_d = 1;
$opt_g = 0;
$opt_l = 0;
$opt_k = 0;
redo;
}
if ($k eq "g") {
$opt_g = 1;
$opt_d = 0;
$opt_l = 0;
$opt_k = 0;
redo;
}
if ($k eq "l") {
$opt_l = 1;
$opt_d = 0;
$opt_g = 0;
$opt_k = 0;
redo;
}
if ($k eq "k") {
$opt_k = 1;
$opt_d = 0;
$opt_g = 0;
$opt_l = 0;
$long = 1;
$input = ' ';
last;
}
}
if ($opt_g and $k eq "-") {
push @fexget,'-o';
$input ||= 1;
$last = $input;
redo;
}
if (ord($k) == 4 or ord($k) == 127) {
$input =~ s/.$//;
} elsif ($k =~ /\d/) {
$input .= $k;
} elsif ($opt_d and $input =~ /^\d+$/) {
$input .= $k if $k eq ' ';
$input .= $k if $k eq '-';
}
}
if ($input eq ' ') {
print $clear;
redo;
}
if ($input eq '') {
print "\r",' 'x72,"\r";
exit;
}
print "\n";
exit if $input !~ /\d/;
$input =~ s/-$//;
if ($input =~ s/^(\d+)-(\d+)$//) {
for ($1..$2) {
$input .= $_.' ';
}
}
foreach my $i (split(' ',$input)) {
print "\n";
if ($i =~ /^\d+$/) {
if (my $durl = $fp{$i}) {
$_ = $list{$i};
s/ *#\d+ //;
s/ +( \d+ MB)/$1/;
print;
if ($opt_g) {
my $pp;
if ($durl =~ /\d+\.gz$/) {
my $pipe = inputline("pipe data to: ");
$pipe = '| '.$pipe if $pipe and $pipe !~ /^>/;
vexec("@fexget -s- $durl | gunzip $pipe");
} elsif ($durl =~ /\d+\.txt$/) {
vexec("@fexget -s- $durl");
} else {
close STDIN;
if (($windoof or $cygwin) and getcwd() =~ /Downloads$/) {
system(@fexget,$durl);
system qw'explorer .';
exit;
} else {
vexec(@fexget,$durl);
}
}
exit 1;
}
if ($opt_d) {
vsystem($fexsend,'-d',basename($durl),'.');
sleep 1;
}
if ($opt_k) {
my $keep = inputline("keep days: ");
# update fexlist
vsystem("$fexsend -l >/dev/null");
vsystem($fexsend,'-x',$dkey{$i},'-k',$keep);
sleep 1;
}
if ($opt_l) {
if ($durl =~ /\.(gz|txt)$/) {
print "\nis not an archive, but pipe data\n";
} else {
vsystem($fexget[0],'-t',$durl);
sleep 1;
}
}
} else {
print "#$i does not exist\n" if $i;
}
}
}
print "\nhit [SPACE] to continue, [q] to quit";
$k = ReadKey(0);
if ($k eq "q") {
print "\r",' 'x72,"\r";
exit;
}
print $clear;
}
}
# upload
my @d = localtime time;
my $fp = sprintf('xxx_%d%02d%02d_%02d%02d%02d',
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
my $comment = sprintf('[%s@%s]',scalar(getpwuid($<)),hostname());
$opt_k ||= 1;
$fexsend .= " -q" if $opt_q;
unless (-t STDIN) {
$fexsend .= " -k $opt_k";
$comment .= ' -';
if (@ARGV) {
$comment .= " # @ARGV";
} elsif (0 and my $pipe = readlink '/proc/self/fd/0') {
# search for pipe STDIN process
# does not work reliable, because STDIN process may has already terminated
foreach my $fd1 (glob '/proc/*/fd/1') {
if ((readlink($fd1)||'') eq $pipe and $fd1 =~ m:(/proc/\d+):) {
my $cmdline = "$1/cmdline";
if (open $cmdline,$cmdline) {
local $/;
$_ = <$cmdline>;
close $cmdline;
s/\000$//;
s/\000/ /g;
$comment .= " # $_";
}
}
}
}
$comment = shellquote($comment);
if ($opt_t) {
vsystem("cat|$fexsend -M -C $comment -s $fp.txt .");
} else {
vsystem("gzip|$fexsend -C $comment -s $fp.gz .");
}
$fp =~ s/_/ /;
# print "$fp\n" if $? == 0;
exit $?;
}
my @fexsend = ($fexsend);
push @fexsend,'-0' if $opt_0;
push @fexsend,('-k',$opt_k) if $opt_k;
push @fexsend,('-m',$opt_m) if $opt_m;
my $ao = '-a';
grep { /^\..|\/\./ and $ao = '-A' } @ARGV;
map { s:/+$:/: } @ARGV;
my @files = @ARGV;
if ("@ARGV" eq '.') {
die "$prg: . is empty\n" unless glob '*';
$comment .= ' '.abs_path("@ARGV").'/';
} else {
foreach (@ARGV) {
$comment .= " $_";
$comment =~ s:/*$:/: if -d and not -l;
}
if (scalar(@ARGV) == 1 and not $opt_p) {
my $a = $ARGV[0];
die "$prg: cannot read $a\n" unless -r $a;
if ($a !~ /\/$/) {
$a = abs_path($a);
die "$prg: cannot send /\n" if $a eq '/';
if ($a =~ m:(.+)/(.+):) {
chdir $1 or die "$prg: cannot cd $1 - $!\n";
warn "\$ cd $1\n" if $opt_v;
@files = ($2);
} else {
chdir '/';
warn "\$ cd /\n" if $opt_v;
@files = ($a);
}
$comment =~ s/ .*/ @files/;
}
}
}
my $container = 'taz';
if ($opt_Z or ($windoof or $cygwin) and not $opt_T) {
$container = 'zip';
} elsif ($opt_z) {
$container = 'tgz';
} elsif ($opt_0) {
$container = 'tar';
}
vsystem(@fexsend,'-C',$comment,$ao,"$fp.$container",@files,'.');
if ($ENV{RUNCMD}) {
exec 'xxx';
}
exit $?;
}
### common functions ###
sub mtime {
my @d = localtime((stat shift)[9]);
return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
}
sub urldecode {
local $_ = shift;
s/\%([a-f\d]{2})/chr(hex($1))/ige;
return $_;
}
sub serverconnect {
my ($server,$port) = @_;
my $connect = "CONNECT $server:$port HTTP/1.1";
local $_;
if ($proxy) {
tcpconnect(split(':',$proxy));
if ($port == 443) {
nvtsend($connect,"");
$_ = <$SH>;
s/\r//;
warn "<-- $_"if $opt_v;
unless (/^HTTP.1.. 200/) {
die "$prg: proxy error : $_";
}
&enable_ssl;
$SH = IO::Socket::SSL->start_SSL($SH,%SSL);
}
} else {
tcpconnect($server,$port);
}
}
# set up tcp/ip connection
sub tcpconnect {
my ($server,$port) = @_;
if ($SH) {
close $SH;
undef $SH;
}
if ($port == 443) {
# eval "use IO::Socket::SSL qw(debug3)";
&enable_ssl;
$SH = IO::Socket::SSL->new(
PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
%SSL
);
} else {
$SH = IO::Socket::INET->new(
PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
);
}
die "$prg: $@\n" if $@;
if ($SH) {
autoflush $SH 1;
binmode $SH;
} else {
die "$prg: cannot connect $server:$port - $@\n";
}
warn "TCPCONNECT to $server:$port\n" if $opt_v;
}
sub sendheader {
my $sp = shift;
my @head = @_;
my $head;
push @head,"Host: $sp";
push @head,"User-Agent: $useragent";
foreach $head (@head) {
chomp $head;
warn "--> $head\n" if $opt_v;
print {$SH} $head,"\r\n";
}
warn "-->\n" if $opt_v;
print {$SH} "\r\n";
}
sub nvtsend {
local $SIG{PIPE} = sub { $sigpipe = "@_" };
$sigpipe = '';
die "$prg: internal error: no active network handle\n" unless $SH;
die "$prg: remote host has closed the link\n" unless $SH->connected;
foreach my $line (@_) {
warn "--> $line\n" if $opt_v;
print {$SH} $line,"\r\n";
if ($sigpipe) {
undef $SH;
return 0;
}
}
return 1;
}
sub shellquote {
local $_ = shift;
s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
return $_;
}
sub debug {
print "## DEBUG: @_\n" if $FEXOPT =~ /\bDEBUG\b/i;
}
sub locale {
my $string = shift;
# my @x = Encode->encodings(':all'); die "$CTYPE - @x";
if ($CTYPE) {
if ($CTYPE =~ /UTF-?8/i) {
return $string;
} elsif (grep { $CTYPE =~ /^$_$/i } Encode->encodings(':all')) {
Encode::from_to($string,'UTF8',$CTYPE,Encode::FB_WARN);
# return encode($CTYPE,decode('UTF8',$string,Encode::FB_WARN),Encode::FB_WARN);
} else {
Encode::from_to($string,'UTF8','ISO-8859-1',Encode::FB_WARN);
# return encode('ISO-8859-1',decode('UTF8',$string));
}
return $string;
}
return $string;
}
sub enable_ssl {
local $_;
eval "use IO::Socket::SSL";
die "$prg: cannot load IO::Socket::SSL\n" if $@;
# needed for CentOS (Redhat, too?)
foreach (qw'/etc/ssl/cert.pem /etc/ssl/certs/ca-bundle.crt') {
$SSL{SSL_ca_file} = $_ if -f;
}
&get_ssl_env;
eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
if ($opt_v) {
foreach my $v (keys %SSL) {
printf STDERR "%s => %s\n",$v,$SSL{$v};
}
}
}
sub get_ssl_env {
# set SSL/TLS options
if (defined($ENV{SSLVERIFY})) {
$SSL{SSL_verify_mode} = $ENV{SSLVERIFY};
}
foreach my $opt (qw(
SSL_version
SSL_cipher_list
SSL_verify_mode
SSL_ca_path
SSL_ca_file)
) {
my $env = uc($opt);
$env =~ s/_//g;
$SSL{$opt} = $ENV{$env} if defined($ENV{$env});
}
if ($SSL{SSL_verify_mode} and $SSL{SSL_verify_mode} ne 'SSL_VERIFY_NONE') {
&search_ca;
unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
die "$prg: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
}
} elsif (defined($SSL{SSL_verify_mode})) {
# user has set SSLVERIFY=0 !
} else {
&search_ca;
$SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
}
}
sub search_ca {
local $_;
return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
if (-f) {
$SSL{SSL_ca_file} = $_;
return;
}
}
foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
if (-d) {
$SSL{SSL_ca_path} = $_;
return;
}
}
}
# from MIME::Base64::Perl
sub encode_b64 {
my $res = "";
my $eol = "\n";
my $padding;
pos($_[0]) = 0;
$res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
$res =~ tr|\` -_|AA-Za-z0-9+/|;
$padding = (3-length($_[0])%3)%3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
return $res;
}
# from MIME::Base64::Perl
sub decode_b64 {
local $_ = shift;
my $uu = '';
my ($i,$l);
tr|A-Za-z0-9+=/||cd;
s/=+$//;
tr|A-Za-z0-9+/| -_|;
return "" unless length;
$l = (length)-60;
for ($i = 0; $i <= $l; $i += 60) {
$uu .= "M" . substr($_,$i,60);
}
$_ = substr($_,$i);
if (length) {
$uu .= chr(32+(length)*3/4) . $_;
}
return unpack("u",$uu);
}
sub inputline {
my $prompt = shift;
my $default = shift||'';
my $term = new Term::ReadLine $prg;
$term->ornaments(0) unless $ENV{PERL_RL};
return $term->readline($prompt,$default)||'';
}
sub vexec {
vsystem(@_);
exit $?;
}
sub vsystem {
my @cmd = @_;
my $cmd;
my $shellmeta = '[\\\'"`*?&|<>(){};]';
local $_;
if ($opt_v) {
if (-t STDIN) { print STDERR '$ ' }
else { print STDERR '| ' }
if (scalar(@cmd) == 1) {
warn "@cmd\n";
} else {
my @w;
foreach (@cmd) {
if (/\'/) {
push @w,shellquote($_);
} elsif (/[^\w\/:=~^%@,.+-]/) {
push @w,"'$_'";
} else {
push @w,$_;
}
}
warn "@w\n";
}
}
$_ = "@cmd";
if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) {
@cmd = split;
}
if (scalar(@cmd) == 1) {
$cmd = "@cmd";
$cmd =~ s/\|&$/ 2>&1|/;
if ($cmd =~ s/\|$//) {
my @a = `$cmd`;
if (wantarray) {
map { chomp } @a;
return @a;
} else {
chomp $a[0] if scalar(@a) == 1;
return join('',@a);
}
} else {
system $cmd;
}
} else {
system @cmd;
}
}
|