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
|
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#else
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/stat.h>
#include <math.h>
#include <errno.h>
#include <ctype.h>
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#include "suck_config.h"
#include "both.h"
#include "suck.h"
#include "suckutils.h"
#include "dedupe.h"
#include "phrases.h"
#include "killfile.h"
#include "timer.h"
#include "active.h"
#include "batch.h"
#include "xover.h"
#include "db.h"
#ifdef MYSIGNAL
#include <signal.h>
#endif
#ifdef CHECK_HISTORY
#include "chkhistory.h"
#endif
/* function prototypes */
int get_articles(PMaster);
int get_one_article(PMaster, int, long);
int do_supplemental(PMaster);
int restart_yn(PMaster);
int scan_args(PMaster, int, char *[]);
void do_cleanup(PMaster);
int do_authenticate(PMaster);
void load_phrases(PMaster);
void free_phrases(void);
int parse_args(PMaster, int, char *[]);
int get_group_number(PMaster, char *);
int do_supplemental(PMaster);
int do_sup_bynr(PMaster, char *);
int do_nodownload(PMaster);
#ifdef MYSIGNAL
RETSIGTYPE sighandler(int);
void pause_signal(int, PMaster);
enum {PAUSE_SETUP, PAUSE_DO};
/*------------------------------------------*/
int GotSignal = FALSE;
/* the only static variable allowed, it's */
/* the only graceful way to handle a signal */
/*------------------------------------------*/
#endif
/* set up for phrases */
char **both_phrases=default_both_phrases;
char **suck_phrases=default_suck_phrases;
char **timer_phrases=default_timer_phrases;
char **chkh_phrases=default_chkh_phrases;
char **dedupe_phrases=default_dedupe_phrases;
char **killf_reasons=default_killf_reasons;
char **killf_phrases=default_killf_phrases;
char **killp_phrases=default_killp_phrases;
char **sucku_phrases=default_sucku_phrases;
char **active_phrases=default_active_phrases;
char **batch_phrases=default_batch_phrases;
char **xover_phrases=default_xover_phrases;
char **xover_reasons=default_xover_reasons;
enum { STATUS_STDOUT, STATUS_STDERR };
enum { RESTART_YES, RESTART_NO, RESTART_ERROR };
enum {
ARG_NO_MATCH, ARG_ALWAYS_BATCH, ARG_BATCH_INN, ARG_BATCH_RNEWS, ARG_BATCH_LMOVE, \
ARG_BATCH_INNFEED, ARG_BATCH_POST, ARG_CLEANUP, ARG_DIR_TEMP, ARG_DIR_DATA, ARG_DIR_MSGS, \
ARG_DEF_ERRLOG, ARG_HOST, ARG_NO_POSTFIX, ARG_LANGUAGE, ARG_MULTIFILE, ARG_POSTFIX, \
ARG_QUIET, ARG_RNEWSSIZE, ARG_DEF_STATLOG, ARG_WAIT_SIG, ARG_ACTIVE, ARG_RECONNECT, \
ARG_DEBUG, ARG_ERRLOG, ARG_HISTORY, ARG_KILLFILE, ARG_KLOG_NONE, ARG_KLOG_SHORT, \
ARG_KLOG_LONG, ARG_MODEREADER, ARG_PORTNR, ARG_PASSWD, ARG_RESCAN, ARG_STATLOG, \
ARG_USERID, ARG_VERSION, ARG_WAIT, ARG_LOCALHOST, ARG_TIMEOUT, ARG_NRMODE, ARG_AUTOAUTH, \
ARG_NODEDUPE, ARG_NO_CHK_MSGID, ARG_READACTIVE, ARG_PREBATCH, ARG_SKIP_ON_RESTART, \
ARG_KLOG_NAME, ARG_USEGUI, ARG_XOVER, ARG_CONN_DEDUPE, ARG_POST_FILTER, ARG_CONN_ACTIVE, \
ARG_HIST_FILE, ARG_HEADER_ONLY, ARG_ACTIVE_LASTREAD, ARG_USEXOVER, ARG_RESETCOUNTER, \
ARG_LOW_READ, ARG_SHOW_GROUP, ARG_USE_SSL, ARG_LOCAL_SSL, ARG_BATCH_POST_NR, \
ARG_PASSWD_ENV,
};
typedef struct Arglist{
const char *sarg;
const char *larg;
int nr_params;
int flag;
int errmsg; /* this is an index into suck_phrases */
} Args, *Pargs;
const Args arglist[] = {
{"a", "always_batch", 0, ARG_ALWAYS_BATCH, -1},
{"bi", "batch_inn", 1, ARG_BATCH_INN, 40},
{"br", "batch_rnews", 1, ARG_BATCH_RNEWS, 40},
{"bl", "batch_lmove", 1, ARG_BATCH_LMOVE, 40},
{"bf", "batch_innfeed",1, ARG_BATCH_INNFEED, 40},
{"bp", "batch_post", 0, ARG_BATCH_POST, -1},
{"bP", "batch_post_nr", 1, ARG_BATCH_POST_NR, 72},
{"c", "cleanup", 0, ARG_CLEANUP, -1},
{"dt", "dir_temp", 1, ARG_DIR_TEMP, 37},
{"dd", "dir_data", 1, ARG_DIR_DATA, 37},
{"dm", "dir_msgs", 1, ARG_DIR_MSGS, 37},
{"e", "def_error_log", 0, ARG_DEF_ERRLOG, -1},
{"f", "reconnect_dedupe", 0, ARG_CONN_DEDUPE, -1},
{"g", "header_only", 0, ARG_HEADER_ONLY, -1},
{"h", "host", 1, ARG_HOST, 51},
{"hl", "localhost", 1, ARG_LOCALHOST, 50},
{"i", "default_activeread", 1, ARG_ACTIVE_LASTREAD, 65},
{"k", "kill_no_postfix", 0, ARG_NO_POSTFIX, -1},
{"l", "language_file", 1, ARG_LANGUAGE, 47},
{"lr", "low_read", 0, ARG_LOW_READ, -1},
{"m", "multifile", 0, ARG_MULTIFILE, -1},
{"n", "number_mode", 0, ARG_NRMODE, -1},
{"p", "postfix", 1, ARG_POSTFIX, 36},
{"q", "quiet", 0, ARG_QUIET, -1},
{"r", "rnews_size", 1, ARG_RNEWSSIZE, 35},
{"rc", "reset_counter", 0, ARG_RESETCOUNTER, -1},
{"s", "def_status_log", 0, ARG_DEF_STATLOG, -1},
{"sg", "show_group", 0, ARG_SHOW_GROUP, -1},
#ifdef HAVE_LIBSSL
{"ssl","use_ssl", 0, ARG_USE_SSL, -1},
#endif
{"u", "auto_authorization", 0, ARG_AUTOAUTH, -1},
{"w", "wait_signal", 2, ARG_WAIT_SIG, 46},
{"x", "no_chk_msgid", 0, ARG_NO_CHK_MSGID, -1},
{"y", "post_filter", 1, ARG_POST_FILTER, 62},
{"z", "no_dedupe", 0, ARG_NODEDUPE, -1},
{"A", "active", 0, ARG_ACTIVE, -1},
{"AL", "read_active", 1, ARG_READACTIVE, 56},
{"B", "pre-batch", 0, ARG_PREBATCH, -1},
{"C", "reconnect", 1, ARG_RECONNECT, 49},
{"D", "debug", 0, ARG_DEBUG, -1},
{"E", "error_log", 1, ARG_ERRLOG, 41},
{"F", "reconnect_active", 0, ARG_CONN_ACTIVE, -1},
{"G", "use_gui", 0, ARG_USEGUI, -1},
{"H", "no_history", 0, ARG_HISTORY, -1},
{"HF", "history_file", 1, ARG_HIST_FILE, 64},
{"K", "killfile", 0, ARG_KILLFILE, -1},
{"L", "kill_log_none", 0, ARG_KLOG_NONE, -1},
{"LF", "kill_log_name", 1, ARG_KLOG_NAME, 61},
{"LS", "kill_log_short", 0, ARG_KLOG_SHORT, -1},
{"LL", "kill_log_long", 0, ARG_KLOG_LONG, -1},
{"M", "mode_reader", 0, ARG_MODEREADER, -1},
{"N", "portnr", 1, ARG_PORTNR, 45},
{"O", "skip_on_restart", 0, ARG_SKIP_ON_RESTART, -1},
{"P", "password", 1, ARG_PASSWD, 44},
{"Q", "password_env", 0, ARG_PASSWD_ENV, -1},
{"R", "no_rescan", 0, ARG_RESCAN, -1},
{"S", "status_log", 1, ARG_STATLOG, 42},
#ifdef HAVE_LIBSSL
{"SSL" "local_use_ssl", 0, ARG_LOCAL_SSL, -1},
#endif
#ifdef TIMEOUT
{"T", "timeout", 1, ARG_TIMEOUT, 52},
#endif
{"U", "userid", 1, ARG_USERID, 43},
{"V", "version", 0, ARG_VERSION, -1},
{"W", "wait", 2, ARG_WAIT, 46},
{"X", "no_xover", 0, ARG_XOVER, -1},
{"Z", "use_xover", 0, ARG_USEXOVER, -1},
};
#define MAX_ARG_PARAMS 2 /* max nr of params with any arg */
#define NR_ARGS (sizeof(arglist)/sizeof(arglist[0]))
/*------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
struct stat sbuf;
Master master;
PList temp;
PGroups ptemp;
POverview pov;
char *inbuf, **args, **fargs = NULL;
int nr, resp, loop, fargc, retval = RETVAL_OK;
#ifdef LOCKFILE
const char *lockfile = NULL;
#endif
#ifdef MYSIGNAL
#ifdef HAVE_SIGACTION
struct sigaction sigs;
#endif
#endif
/* initialize master structure */
master.head = master.curr = NULL;
master.nritems = master.nrgot = 0;
master.MultiFile = FALSE;
master.msgs = stdout;
master.sockfd = -1;
master.status_file = FALSE; /* are status messages going to a file */
master.status_file_name = NULL;
master.do_killfile = TRUE;
master.do_chkhistory = TRUE;
master.do_modereader = FALSE;
master.always_batch = FALSE;
master.rnews_size = 0L;
master.batch = BATCH_FALSE;
master.batchfile = NULL;
master.cleanup = FALSE;
master.portnr = DEFAULT_NNRP_PORT;
master.host = getenv("NNTPSERVER"); /* the default */
master.pause_time = -1;
master.pause_nrmsgs = -1;
master.sig_pause_time = -1;
master.sig_pause_nrmsgs = -1;
master.killfile_log = KILL_LOG_LONG; /* do we log killed messages */
master.phrases = NULL;
master.errlog = NULL;
master.debug = FALSE;
master.rescan = TRUE; /* do we do rescan on a restart */
master.quiet = FALSE; /* do we display BPS and msg count */
master.killp = NULL; /* pointer to killfile structure */
master.kill_ignore_postfix = FALSE;
master.reconnect_nr = 0; /* how many x msgs do we disconnect and reconnect 0 = never */
master.innfeed = NULL;
master.do_active = FALSE; /* do we scan the local active list */
master.localhost = NULL;
master.groups = NULL;
master.nrmode = FALSE; /* use nrs or msgids to get article */
master.auto_auth = FALSE; /* do we do auto authorization */
master.passwd = NULL;
master.userid = NULL;
master.no_dedupe = FALSE;
master.chk_msgid = TRUE; /* do we check MsgID for trailing > */
master.activefile = NULL;
master.prebatch = FALSE; /* do we try to batch any left over articles b4 we start? */
master.grpnr = -1; /* what group number are we currently on */
master.skip_on_restart = FALSE;
master.kill_log_name = N_KILLLOG;
master.use_gui = FALSE;
master.do_xover = TRUE;
master.conn_dedupe = FALSE;
master.post_filter= NULL;
master.conn_active = FALSE;
master.history_file = HISTORY_FILE;
master.header_only = FALSE;
master.db = -1;
master.active_lastread = ACTIVE_DEFAULT_LASTREAD;
master.use_xover = FALSE;
master.xoverview = NULL;
master.resetcounter = FALSE;
master.low_read = FALSE;
master.show_group = FALSE;
master.do_ssl = FALSE;
master.ssl_struct = NULL;
master.local_ssl = FALSE;
master.local_ssl_struct = NULL;
master.batch_post_nr = 0;
master.passwd_env = FALSE;
/* have to do this next so if set on cmd line, overrides this */
#ifdef N_PHRASES /* in case someone nukes def */
if(stat(N_PHRASES, &sbuf) == 0 && S_ISREG(sbuf.st_mode)) {
/* we have a regular phrases file make it the default */
master.phrases = N_PHRASES;
}
#endif
/* allow no args, only the hostname, or hostname as first arg */
/* also have to do the file argument checking */
switch(argc) {
case 1:
break;
case 2:
/* the fargs == NULL test so only process first file name */
if(argv[1][0] == FILE_CHAR) {
if((fargs = build_args(&argv[1][1], &fargc)) != NULL) {
retval = scan_args(&master, fargc, fargs);
}
}
else if(argv[1][0] == '-') {
/* in case of suck -V */
retval = scan_args(&master, 1, &(argv[1]));
}
else{
master.host = argv[1];
}
break;
default:
for(loop=1;loop<argc && fargs == NULL;loop++) {
if(argv[loop][0] == FILE_CHAR) {
if((fargs = build_args(&argv[loop][1], &fargc)) != NULL) {
retval = scan_args(&master, fargc, fargs);
}
}
}
/* this is here so anything at command line overrides file */
if(argv[1][0] != '-' && argv[1][0] != FILE_CHAR) {
master.host = argv[1];
argc-= 2;
args = &(argv[2]);
}
else {
args = &(argv[1]);
argc--;
}
retval = scan_args(&master, argc, args);
break;
}
/* print out status stuff */
if(master.debug == TRUE) {
do_debug("Suck version %s\n",SUCK_VERSION);
do_debug("master.MultiFile = %d\n", master.MultiFile);
do_debug("master.status_file = %d\n",master.status_file);
do_debug("master.status_file_name = %s\n", null_str(master.status_file_name));
do_debug("master.do_killfile = %s\n", true_str(master.do_killfile));
do_debug("master.do_chkhistory = %s\n", true_str(master.do_chkhistory));
do_debug("master.do_modereader = %s\n", true_str(master.do_modereader));
do_debug("master.always_batch = %s\n", true_str(master.always_batch));
do_debug("master.rnews_size = %ld\n", master.rnews_size);
do_debug("master.batch = %d\n", master.batch);
do_debug("master.batchfile = %s\n", null_str(master.batchfile));
do_debug("master.cleanup = %s\n", true_str(master.cleanup));
do_debug("master.host = %s\n", null_str(master.host));
do_debug("master.portnr = %u\n", master.portnr);
do_debug("master.pause_time = %d\n", master.pause_time);
do_debug("master.pause_nrmsgs = %d\n", master.pause_nrmsgs);
do_debug("master.sig_pause_time = %d\n", master.sig_pause_time);
do_debug("master.sig_pause_nrmsgs = %d\n", master.sig_pause_nrmsgs);
do_debug("master.killfile_log = %d\n", master.killfile_log);
do_debug("master.phrases = %s\n", null_str(master.phrases));
do_debug("master.errlog = %s\n", null_str(master.errlog));
do_debug("master.rescan = %s\n", true_str(master.rescan == TRUE));
do_debug("master.quiet = %s\n", true_str(master.quiet));
do_debug("master.kill_ignore_postfix = %s\n", true_str(master.kill_ignore_postfix));
do_debug("master.reconnect_nr=%d\n", master.reconnect_nr);
do_debug("master.do_active = %s\n", true_str(master.do_active));
do_debug("master.localhost = %s\n", null_str(master.localhost));
do_debug("master.nrmode = %s\n", true_str(master.nrmode));
do_debug("master.auto_auth = %s\n", true_str(master.auto_auth));
do_debug("master.no_dedupe = %s\n", true_str(master.no_dedupe));
do_debug("master.chk_msgid = %s\n", true_str(master.chk_msgid));
do_debug("master.activefile = %s\n", null_str(master.activefile));
do_debug("master.prebatch = %s\n", true_str(master.prebatch));
do_debug("master.skip_on_restart = %s\n", true_str(master.skip_on_restart));
do_debug("master.kill_log_name = %s\n", null_str(master.kill_log_name));
do_debug("master.use_gui = %s\n", true_str(master.use_gui));
do_debug("master.do_xover = %s\n", true_str(master.do_xover));
do_debug("master.conn_dedupe = %s\n", true_str(master.conn_dedupe));
do_debug("master.post_filter = %s\n", null_str(master.post_filter));
do_debug("master.conn_active = %s\n", true_str(master.conn_active));
do_debug("master.history_file = %s\n", null_str(master.history_file));
do_debug("master.header_only = %s\n", true_str(master.header_only));
do_debug("master.active_lastread = %d\n", master.active_lastread);
do_debug("master.use_xover = %s\n", true_str(master.use_xover));
do_debug("master.do_ssl = %s\n", true_str(master.do_ssl));
do_debug("master.local_ssl = %s\n", true_str(master.local_ssl));
do_debug("master.batch_post_nr = %d\n",master.batch_post_nr);
do_debug("master.passwd_env = %s\n", true_str(master.passwd_env));
#ifdef TIMEOUT
do_debug("TimeOut = %d\n", TimeOut);
#endif
do_debug("master.debug = TRUE\n");
}
/* now do any final args checks needed */
/* check to see if we have enough info to scan the localhost active file */
if((master.do_active == TRUE || master.batch == BATCH_LIHAVE) && master.localhost == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[6], NULL);
}
else if(master.host == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[74], NULL);
}
/* okay now the main stuff */
if(retval == RETVAL_OK) {
if(master.status_file == FALSE) {
/* if not in multifile mode, all status msgs MUST go to stderr to not mess up articles */
/* this has to go before lockfile code, so lockfile prints msg to correct place. */
master.msgs = ( master.MultiFile == FALSE) ? stderr : stdout ;
}
#ifdef LOCKFILE
/* this has to be here since we use full_path() to get path for lock file */
/* and it isn't set up until we process the args above. */
if(do_lockfile(&master) != RETVAL_OK) {
exit(-1);
}
#endif
#ifdef MYSIGNAL
/* set up signal handlers */
#ifdef HAVE_SIGACTION
sigemptyset(&(sigs.sa_mask));
sigs.sa_handler = sighandler;
sigs.sa_flags = 0;
if(sigaction(MYSIGNAL, &sigs, NULL) == -1
|| sigaction(MYSIGNAL2, &sigs, NULL) == -1
|| sigaction(PAUSESIGNAL, &sigs, NULL) == -1) {
MyPerror(suck_phrases[67]);
}
else {
signal_block(MYSIGNAL_SETUP); /* set up sgetline() to block signal */
pause_signal(PAUSE_SETUP, &master); /* set up routine for pause swap if signal */
}
#else
/* the old-fashioned way */
signal(MYSIGNAL, sighandler);
signal(MYSIGNAL2, sighandler);
signal(PAUSESIGNAL, sighander);
pause_signal(PAUSE_SETUP, &master);
#endif
#endif
load_phrases(&master); /* this has to be here so rest prints okay */
/* set up status log, if none specified or unable to open status log */
/* then use stdout or stderr */
if(master.status_file_name != NULL) {
/* okay attempt to open it up */
if((master.msgs = fopen(master.status_file_name, "a")) == NULL) {
MyPerror(suck_phrases[0]);
master.msgs = stdout; /* reset to default */
}
else {
master.status_file = TRUE;
}
}
#ifdef HAVE_SETVBUF
setvbuf(master.msgs, NULL, _IOLBF, 0); /* set to line buffering */
#endif
/* do we batch up any lingering articles ? */
if(master.prebatch == TRUE) {
switch(master.batch) {
case BATCH_FALSE:
error_log(ERRLOG_REPORT, suck_phrases[58], NULL);
break;
case BATCH_INNXMIT:
do_innbatch(&master);
break;
case BATCH_RNEWS:
do_rnewsbatch(&master);
break;
case BATCH_LMOVE:
do_lmovebatch(&master);
break;
case BATCH_LIHAVE:
do_localpost(&master);
break;
}
}
/* now parse the killfile */
#ifdef KILLFILE
if(master.do_killfile == TRUE) {
master.killp = parse_killfile(KILL_KILLFILE, master.killfile_log, master.debug, master.kill_ignore_postfix);
}
#endif
/* now parse the xover killfile */
if(master.do_xover == TRUE) {
master.xoverp = parse_killfile(KILL_XOVER, master.killfile_log, master.debug, master.kill_ignore_postfix);
}
print_phrases(master.msgs, suck_phrases[1], master.host, NULL);
if(do_connect(&master, CONNECT_FIRST) != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
/* if we have XOVER killfiles, we need to get the format of em before any processing */
if(master.xoverp != NULL || master.use_xover == TRUE) {
get_xoverview(&master);
killprg_sendoverview(&master);
}
/* first, check for restart articles, */
/* then scan for any new articles */
if((loop = restart_yn(&master)) == RESTART_ERROR) {
retval = RETVAL_ERROR;
}
else if(loop == RESTART_NO || master.rescan == TRUE) {
/* we don't do scan if we had restart and option = FALSE */
/* do we scan the local active file? */
if(master.do_active == TRUE || master.activefile != NULL) {
if(get_message_index_active(&master) < RETVAL_OK) {
retval = RETVAL_ERROR;
}
}
else if(get_message_index(&master) < RETVAL_OK) {
retval = RETVAL_ERROR;
}
if(retval == RETVAL_OK) {
retval = do_supplemental(&master);
}
if(retval == RETVAL_OK) {
retval = do_nodownload(&master);
}
if(retval == RETVAL_OK && master.head != NULL && master.nritems > 0) {
/* if we don't have any messages, we don't need to do this */
if(master.no_dedupe == FALSE) {
dedupe_list(&master);
}
#ifdef CHECK_HISTORY
if(master.do_chkhistory == TRUE) {
chkhistory(&master);
}
#endif
print_phrases(master.msgs,suck_phrases[20], str_int(master.nritems), NULL);
}
}
if(retval == RETVAL_OK) {
if(master.nritems == 0) {
print_phrases(master.msgs,suck_phrases[3], NULL);
retval = RETVAL_NOARTICLES;
}
else {
/* write out restart db */
retval = db_write(&master);
/* reconnect after dedupe, in case of time out due to long dedupe time*/
if(retval == RETVAL_OK && master.conn_dedupe == TRUE) {
retval = do_connect(&master, CONNECT_AGAIN);
}
if(retval == RETVAL_OK) {
retval = get_articles(&master);
}
}
}
if(retval == RETVAL_OK) { /* if we got disconnected above, don't do this */
/* send quit, and get reply */
sputline(master.sockfd,"quit\r\n", master.do_ssl, master.ssl_struct);
if(master.debug == TRUE) {
do_debug("Sending command: quit\n");
}
do {
resp = sgetline(master.sockfd, &inbuf, master.do_ssl, master.ssl_struct);
if(resp>0) {
if(master.debug == TRUE) {
do_debug("Quitting GOT: %s", inbuf);
}
number(inbuf, &nr);
}
}while(nr != 205 && resp > 0);
}
}
if(master.sockfd >= 0) {
disconnect_from_nntphost(master.sockfd, master.do_ssl, &master.ssl_struct);
print_phrases(master.msgs,suck_phrases[4], master.host, NULL);
}
if(master.debug == TRUE) {
do_debug("retval=%d (RETVAL_OK=%d), m.nrgot=%d, m.batch=%d\n", retval, RETVAL_OK, master.nrgot, master.batch);
}
if((retval == RETVAL_OK || master.always_batch == TRUE) && master.nrgot > 0 && master.header_only == FALSE) {
switch(master.batch) {
case BATCH_INNXMIT:
do_post_filter(&master);
do_innbatch(&master);
break;
case BATCH_RNEWS:
do_post_filter(&master);
do_rnewsbatch(&master);
break;
case BATCH_LMOVE:
do_post_filter(&master);
do_lmovebatch(&master);
break;
case BATCH_LIHAVE:
do_post_filter(&master);
do_localpost(&master);
break;
default:
break;
}
}
if((retval == RETVAL_NOARTICLES || retval == RETVAL_OK) && master.cleanup == TRUE) {
print_phrases(master.msgs, suck_phrases[7], NULL);
do_cleanup(&master);
}
/* close out status log */
if(master.msgs != NULL && master.msgs != stdout && master.msgs != stderr) {
fclose(master.msgs);
}
if(master.head != NULL) {
/* clean up memory */
master.curr = master.head;
while(master.curr != NULL) {
temp = (master.curr)->next;
free_one_node(master.curr);
master.curr = temp;
}
}
/* free up group list */
while (master.groups != NULL) {
ptemp=(master.groups)->next;
free(master.groups);
master.groups = ptemp;
}
/* free up overview.fmt list */
while (master.xoverview != NULL) {
pov = (master.xoverview)->next;
if((master.xoverview)->header != NULL) {
free((master.xoverview)->header);
}
free(master.xoverview);
master.xoverview = pov;
}
#ifdef KILLFILE
free_killfile(master.killp);
#endif
free_killfile(master.xoverp);
if(fargs != NULL) {
free_args(fargc, fargs);
}
#ifdef LOCKFILE
lockfile = full_path(FP_GET, FP_TMPDIR, N_LOCKFILE);
if(lockfile != NULL) {
unlink(lockfile);
}
#endif
}
free_phrases(); /* do this last so everything is displayed correctly */
exit(retval);
}
/*------------------------------------------------------------*/
int do_connect(PMaster master, int which_time) {
char *inbuf;
int nr, resp, retval = RETVAL_OK;
FILE *fp;
if(which_time != CONNECT_FIRST) {
/* close down previous connection */
sputline(master->sockfd, "quit\r\n", master->do_ssl, master->ssl_struct);
do {
resp = sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct);
if(resp>0) {
if(master->debug == TRUE) {
do_debug("Reconnect GOT: %s", inbuf);
}
number(inbuf, &nr);
}
}while(nr != 205 && resp > 0);
disconnect_from_nntphost(master->sockfd, master->do_ssl, &master->ssl_struct);
/* now reset everything */
if(master->curr != NULL) {
(master->curr)->sentcmd = FALSE;
}
master->grpnr = -1;
}
if(master->debug == TRUE) {
do_debug("Connecting to %s on port %d\n", master->host, master->portnr);
}
fp = (which_time == CONNECT_FIRST) ? master->msgs : NULL;
master->sockfd = connect_to_nntphost( master->host, NULL, 0, fp, master->portnr, master->do_ssl, &master->ssl_struct);
if(master->sockfd < 0 ) {
retval = RETVAL_ERROR;
}
else if(sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct) < 0) {
/* Get the announcement line */
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("Got: %s", inbuf);
}
if(which_time == CONNECT_FIRST) {
fprintf(master->msgs ,"%s", inbuf );
}
/* check to see if we have to do authorization now */
number(inbuf, &resp);
if(resp == 480 ) {
retval = do_authenticate(master);
}
if(retval == RETVAL_OK && master->do_modereader == TRUE) {
retval = send_command(master, "mode reader\r\n", &inbuf, 0);
if(retval == RETVAL_OK) {
/* Again the announcement */
if(which_time == CONNECT_FIRST) {
fprintf(master->msgs ,"%s",inbuf);
}
}
}
if(master->auto_auth == TRUE) {
if(master->passwd == NULL || master->userid == NULL) {
error_log(ERRLOG_REPORT, suck_phrases[55], NULL);
retval = RETVAL_ERROR;
}
else {
/* auto authorize */
retval = do_authenticate(master);
}
}
}
return retval;
}
/*--------------------------------------------------------------------*/
int get_message_index(PMaster master) {
long lastread;
int nrread, retval, maxread;
char buf[MAXLINLEN], group[512];
FILE *ifp,*tmpfp;
retval = RETVAL_OK;
ifp = tmpfp = NULL;
TimerFunc(TIMER_START, 0, NULL);
if((ifp = fopen(full_path(FP_GET, FP_DATADIR, N_OLDRC), "r" )) == NULL) {
MyPerror(full_path(FP_GET, FP_DATADIR, N_OLDRC));
retval = RETVAL_ERROR;
}
else if((tmpfp = fopen(full_path(FP_GET, FP_TMPDIR, N_NEWRC), "w" )) == NULL) {
MyPerror(full_path(FP_GET, FP_TMPDIR, N_NEWRC));
retval = RETVAL_ERROR;
}
while(retval == RETVAL_OK && fgets(buf, MAXLINLEN-1, ifp) != NULL) {
if(buf[0] == SUCKNEWSRC_COMMENT_CHAR) {
/* skip this group */
fputs(buf, tmpfp);
print_phrases(master->msgs, suck_phrases[8], buf, NULL);
}
else {
maxread = -1; /* just in case */
nrread = sscanf(buf, "%s %ld %d\n", group, &lastread, &maxread);
if ( nrread < 2 || nrread > 3) {
error_log(ERRLOG_REPORT, suck_phrases[9], buf, NULL);
fputs(buf, tmpfp); /* rewrite the line */
}
else if(maxread == 0) {
/* just rewrite the line */
fputs(buf, tmpfp);
}
else {
retval = do_one_group(master, buf, group, tmpfp, lastread, maxread);
}
}
}
TimerFunc(TIMER_TIMEONLY, 0,master->msgs);
if(retval == RETVAL_OK) {
print_phrases(master->msgs, suck_phrases[16], str_int(master->nritems), NULL);
}
else if(ifp != NULL) {
/* this is in case we had to abort the above while loop (due to loss of pipe to server) and */
/* we hadn't finished writing out the suck.newrc, this finishes it up. */
do {
fputs(buf, tmpfp);
}
while(fgets(buf, MAXLINLEN-1, ifp) != NULL);
}
if(tmpfp != NULL) {
fclose(tmpfp);
}
if(ifp != NULL) {
fclose(ifp);
}
return retval;
}
/*-----------------------------------------------------------------------------------------------------*/
int do_one_group(PMaster master, char *buf, char *group, FILE *newrc, long lastread, int maxread) {
char *sp, *inbuf, cmd[MAXLINLEN];
long count,low,high;
int response,retval,i;
retval = RETVAL_OK;
sprintf(cmd,"group %s\r\n",group);
if(send_command(master,cmd,&inbuf,0) != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
sp = number(inbuf, &response);
if(response != 211) {
fputs(buf, newrc); /* rewrite line AS IS in newrc */
/* handle group not found */
switch(response) {
case 411:
error_log(ERRLOG_REPORT, suck_phrases[11], group, NULL);
break;
case 500:
error_log(ERRLOG_REPORT, suck_phrases[48], NULL);
break;
default:
error_log(ERRLOG_REPORT, suck_phrases[12],group,str_int(response),NULL);
retval = RETVAL_ERROR; /* bomb out on wacko errors */
break;
}
}
else {
sp = get_long(sp, &count);
sp = get_long(sp, &low);
sp = get_long(sp, &high);
fprintf(newrc, "%s %ld", group, high);
if(maxread > 0) {
fprintf(newrc, " %d", maxread);
}
fputs("\n", newrc);
/* add a sanity check in case remote host changes its numbering scheme */
/* the > 0 is needed, since if a nnrp site has no article it will reset */
/* the count to 0. So not an error */
if(lastread > high && high > 0) {
if(master->resetcounter == TRUE) {
/* reset lastread to low, so we pick up all articles in the group */
lastread = low;
print_phrases(master->msgs,suck_phrases[71],group,str_int(high),str_int(low),NULL);
}
else {
print_phrases(master->msgs,suck_phrases[13],group,str_int(high),NULL);
}
}
if(lastread < 0 ) {
/* if a neg number, get the last X nr of messages, handy for starting */
/* a new group off ,say -100 will get the last 100 messages */
lastread += high; /* this works since lastread is neg */
if(lastread < 0) {
lastread = 0; /* just to be on the safeside */
}
}
/* this has to be >= 0 since if there are no article on server high = 0 */
/* so if we write out 0, we must be able to handle zero as valid lastread */
/* the count > 0 so if no messages available we don't even try */
/* or if low > high no messages either */
if (low <= high && count > 0 && lastread < high && lastread >= 0) {
/* add group name to list of groups */
if(lastread < low) {
lastread = low - 1;
}
/* now set the max nr of messages to read */
if(maxread > 0 && high-maxread > lastread) {
if(master->low_read == TRUE) {
/* instead of limiting from the high-water mark (the latest articles) */
/* limit from the low-water mark (the oldest articles) */
high = (lastread+1) + maxread;
}
else {
lastread = high-maxread;
}
print_phrases(master->msgs, suck_phrases[14], group, str_int(maxread), NULL);
}
print_phrases(master->msgs, suck_phrases[15], group, str_long(high-lastread), str_long(lastread+1), str_long(high), NULL);
if(master->xoverp != NULL) {
/* do this via the xover killfile command */
retval = do_group_xover(master, group, lastread+1, high);
}
/* do we use xover or xhdr to get our article list */
if(master->use_xover == TRUE) {
retval = get_xover(master, group, lastread+1, high);
}
else if((master->xoverp == NULL) || (retval == RETVAL_NOXOVER)) {
/* do this the normal way */
sprintf(cmd, "xhdr Message-ID %ld-%ld\r\n", lastread+1,high);
i = send_command(master,cmd,&inbuf,221);
if(i != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
do {
if(sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct) < 0) {
retval = RETVAL_ERROR;
}
else if (*inbuf != '.' ) {
retval = allocnode(master, inbuf, MANDATORY_OPTIONAL, group, 0L);
}
} while (retval == RETVAL_OK && *inbuf != '.' && *(inbuf+1) != '\n');
} /* end if response */
} /* end if xover */
} /* end if lastread */
} /* end response */
} /* end else */
if(retval == RETVAL_ERROR) {
error_log(ERRLOG_REPORT, suck_phrases[59], NULL);
}
return retval;
}
/*-----------------------------------------------------------*/
int get_articles(PMaster master) {
int retval, logcount, i, grpnr, grplen;
long loop, downloaded;
PGroups grps;
const char *grpname;
const char *empty = "";
#ifdef HAVE_GETTIMEOFDAY
double bps;
#endif
#ifdef KILLFILE
int ( *get_message)(PMaster, int, long); /* which function will we use get_one_article or get_one_article_kill) */
grpnr = -1;
grps = NULL;
grpname = empty;
grplen = 0;
/* do we use killfiles? */
get_message = (master->killp == NULL) ? get_one_article : get_one_article_kill;
#endif
retval = RETVAL_OK;
downloaded = loop = 0; /* used to track how many downloaded, for reconnect option */
/* figure out how many digits wide the articleCount is for display purposes */
/* this used to be log10()+1, but that meant I needed the math library */
for(logcount=1, i=master->nritems; i > 9 ; logcount++) {
i /= 10;
}
if(master->MultiFile == TRUE && checkdir(full_path(FP_GET, FP_MSGDIR, NULL)) == FALSE) {
retval = RETVAL_ERROR;
}
else {
retval = db_open(master);
if(retval == RETVAL_OK) {
master->curr = master->head;
if(master->batch == BATCH_INNFEED) {
/* open up batch file for appending to */
if((master->innfeed = fopen(master->batchfile, "a")) == NULL) {
MyPerror(master->batchfile);
}
}
else if(master->batch == BATCH_LIHAVE) {
if((master->innfeed = fopen(full_path(FP_GET, FP_TMPDIR, master->batchfile), "a")) == NULL) {
MyPerror(full_path(FP_GET, FP_TMPDIR, master->batchfile));
retval = RETVAL_ERROR;
}
}
TimerFunc(TIMER_START, 0, NULL);
#ifdef MYSIGNAL
while(retval == RETVAL_OK && master->curr != NULL && GotSignal == FALSE) {
#else
while(retval == RETVAL_OK && master->curr != NULL) {
#endif
if(master->debug == TRUE) {
do_debug("Article nr = %s mandatory = %c\n", (master->curr)->msgnr, (master->curr)->mandatory);
}
loop++;
if((master->curr)->downloaded == FALSE) {
/* we haven't yet downloaded this article */
downloaded++;
/* to be polite to the server, lets allow for a pause every once in a while */
if(master->pause_time > 0 && master->pause_nrmsgs > 0) {
if((downloaded > 0) && (downloaded % master->pause_nrmsgs == 0)) {
sleep(master->pause_time);
}
}
if(master->status_file == FALSE && master->quiet == FALSE) {
/* if we are going to a file, we don't want all of these articles printed */
/* or if quiet flag is set */
/* this stuff doesn't go thru print_phrases so I can keep spacing right */
/* and I only print numbers and the BPS */
#ifdef HAVE_GETTIMEOFDAY
bps = TimerFunc(TIMER_GET_BPS, 0, master->msgs);
#endif
if(master->use_gui == TRUE) {
/* this stuff is formatted by the GUI so we put it out in a format it likes */
#ifndef HAVE_GETTIMEOFDAY
fprintf(master->msgs, "---%ld+++\n",master->nritems - loop);
#else
fprintf(master->msgs, "---%ld+++%f\n", master->nritems - loop, bps);
#endif /* HAVE_GETTIMEOFDAY */
}
else {
if(master->show_group == TRUE) {
/* add the group name (if available) to the line printed */
if((master->curr)->groupnr >= 0) {
if((master->curr)->groupnr != grpnr) {
grpname = empty;
grpnr = (master->curr)->groupnr;
/* new group name find it */
grps = master->groups;
while(grps != NULL && grps->nr != grpnr) {
grps = grps->next;
}
if(grps != NULL) {
grpname = grps->group;
/* calculate max len for format of printf*/
/* which erases the previous group name */
if(strlen(grpname) > grplen) {
grplen = strlen(grpname);
}
}
}
}
else {
grpnr = -1;
grpname = empty;
}
}
#ifndef HAVE_GETTIMEOFDAY
fprintf(master->msgs, "%5ld %-*s\r",master->nritems - loop, grplen, grpname);
#else
fprintf(master->msgs, "%5ld %9.1f %s %-*s\r",master->nritems-loop,bps,suck_phrases[5], grplen, grpname);
#endif /* HAVE_GETTIMEOFDAY */
}
fflush(master->msgs); /* so message gets printed now */
}
#ifdef KILLFILE
/* get one article */
retval = (*get_message)(master, logcount, loop);
#else
retval = get_one_article(master, logcount, loop);
#endif
if(retval == RETVAL_OK ) {
retval = db_mark_dled(master, master->curr);
}
}
master->curr = (master->curr)->next; /* get next article */
/* to be NOT polite to the server reconnect every X msgs to combat the INND */
/* LIKE_PULLERS=DONT. This is last in loop, so if problem can abort gracefully */
if((master->reconnect_nr > 0) && (downloaded > 0) && (downloaded % master->reconnect_nr == 0)) {
retval = do_connect(master, CONNECT_AGAIN);
if(master->curr != NULL) {
(master->curr)->sentcmd = FALSE; /* if we sent command force resend */
}
}
// do a local post every x number of articles
if(master->batch == BATCH_LIHAVE && master->batch_post_nr > 0 && master->nrgot >= master->batch_post_nr) {
fclose(master->innfeed);
master->innfeed = NULL;
do_post_filter(master);
retval = do_localpost(master);
if(retval == RETVAL_OK) {
master->nrgot = 0; // so if no more articles left we don't try to post in main routine
if((master->innfeed = fopen(full_path(FP_GET, FP_TMPDIR, master->batchfile), "a")) == NULL) {
MyPerror(full_path(FP_GET, FP_TMPDIR, master->batchfile));
retval = RETVAL_ERROR;
}
}
}
} /* end while */
db_close(master);
if(retval == RETVAL_OK && master->nritems == loop) {
db_delete(master);
}
if(retval == RETVAL_OK) {
TimerFunc(TIMER_TOTALS, 0, master->msgs);
}
if(master->innfeed != NULL) {
fclose(master->innfeed);
}
}
}
return retval;
}
/*-----------------------------------------------*/
/* add items from supplemental list to link list */
/* ----------------------------------------------*/
int do_supplemental(PMaster master) {
int retval, oldkept;
FILE *fp;
char linein[MAXLINLEN+1];
retval = RETVAL_OK;
oldkept = master->nritems;
if((fp = fopen(full_path(FP_GET, FP_DATADIR, N_SUPPLEMENTAL), "r")) != NULL) {
print_phrases(master->msgs, suck_phrases[17], NULL);
while(retval == RETVAL_OK && fgets(linein, MAXLINLEN, fp) != NULL) {
if(linein[0] == '!') {
retval = do_sup_bynr(master, linein);
}
else if(linein[0] == '<') {
retval = allocnode(master, linein, MANDATORY_YES, NULL, 0L);
}
else {
error_log(ERRLOG_REPORT, suck_phrases[18], linein, NULL);
}
}
print_phrases(master->msgs, suck_phrases[19], str_int(master->nritems-oldkept), \
str_int(master->nritems), NULL);
fclose(fp);
}
return retval;
}
/*------------------------------------------------------------------------------------------*/
int do_sup_bynr(PMaster master, char *linein) {
int retval = RETVAL_OK;
/* this routine takes in a line formated !group_name article_nr */
/* gets it's Msg-ID (via XHDR), then adds it to our list of articles */
/* as a mandatory article to download */
char grpname[MAX_GRP_LEN], cmd[MAXLINLEN], *resp;
int i, done;
long nrlow, nrhigh;
if(master->debug == TRUE) {
do_debug("supplemental adding %s", linein);
}
i = sscanf(linein, "!%s %ld-%ld", grpname, &nrlow, &nrhigh);
if(i < 2) {
error_log(ERRLOG_REPORT, suck_phrases[18], linein, NULL);
}
else {
sprintf(cmd,"group %s\r\n",grpname);
if(send_command(master,cmd,NULL,211) == RETVAL_OK) {
if(i == 2) {
sprintf(cmd,"xhdr Message-ID %ld\r\n",nrlow);
}
else {
sprintf(cmd,"xhdr Message-ID %ld-%ld\r\n",nrlow,nrhigh);
}
if(send_command(master,cmd,NULL,221) == RETVAL_OK) {
/* now have to get message-id and the . */
done = FALSE;
while( done == FALSE) {
if(sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct) < 0) {
retval = RETVAL_ERROR;
done = TRUE;
}
else {
if(master->debug == TRUE) {
do_debug("Got %s", resp);
}
if(*resp == '.') {
done = TRUE;
}
else {
retval = allocnode(master, resp, MANDATORY_YES, grpname, 0L);
}
}
}
}
}
}
return retval;
}
/*---------------------------------------------------------------------*/
int do_nodownload(PMaster master) {
int i, retval = RETVAL_OK;
FILE *fp;
PList item, prev;
char linein[MAXLINLEN+1];
long nrlines = 0, nrnuked = 0;
if((fp = fopen(full_path(FP_GET, FP_DATADIR, N_NODOWNLOAD), "r")) != NULL) {
print_phrases(master->msgs, suck_phrases[68], NULL);
while(retval == RETVAL_OK && fgets(linein, MAXLINLEN, fp) != NULL) {
nrlines++;
i = strlen(linein) - 1; /* so it points at last char */
/* strip off nl */
if(linein[i] == '\n') {
linein[i] = '\0';
i--;
}
/* strip off extra spaces on the end */
while(isspace(linein[i])){
linein[i] = '\0';
i--;
}
item = master->head;
prev = NULL;
/* find start of msgid */
if(linein[0] != '<') {
error_log(ERRLOG_REPORT, suck_phrases[69], linein, NULL);
}
else {
if(master->debug == TRUE) {
do_debug("Checking Nodownload - %s\n", linein);
}
while((item != NULL) && (cmp_msgid(linein, item->msgnr) == FALSE)) {
prev = item;
item = item->next;
}
if(item != NULL) {
/* found a match, remove from the list */
if(master->debug == TRUE) {
do_debug("Matched, nuking from list\n");
}
nrnuked++;
master->nritems--;
if(item == master->head) {
/* change top of list */
master->head = (master->head)->next;
}
else {
/* its not the end of the list */
prev->next = item->next;
free_one_node(item);
}
}
}
}
fclose(fp);
print_phrases(master->msgs, suck_phrases[70], str_long(nrlines), str_long(nrnuked), str_long(master->nritems), NULL);
}
return retval;
}
/*----------------------------------------------------------------------*/
int get_group_number(PMaster master, char *grpname) {
PGroups grps, gptr;
int groupnr = 0;
/* first, find out if it doesn't exist already */
gptr = master->groups;
while(gptr != NULL && groupnr == 0) {
if(strcmp(gptr->group, grpname) == 0) {
/* bingo */
groupnr = gptr->nr;
}
else {
gptr = gptr->next;
}
}
if(groupnr == 0) {
/* add group to group list and get group nr */
if((grps = malloc(sizeof(Groups))) == NULL) {
/* out of memory */
error_log(ERRLOG_REPORT, suck_phrases[22], NULL);
}
else {
grps->next = NULL;
strcpy(grps->group, grpname);
/* now add to list and count groupnr */
if(master->groups == NULL) {
grps->nr = 1;
master->groups = grps;
}
else {
gptr = master->groups;
while(gptr->next != NULL) {
gptr = gptr->next;
}
gptr->next = grps;
grps->nr = gptr->nr + 1;
}
groupnr = grps->nr;
if(master->debug == TRUE) {
do_debug("Adding to group list: %d %s\n", grps->nr, grps->group);
}
}
}
return groupnr;
}
/*-----------------------------------------------------------------------*/
int allocnode(PMaster master, char *linein, int mandatory, char *group, long msgnr_in) {
/* if msgnr_in is not filled in (0), then parse the msgnr off the linein */
/* if allocate memory here, must free in free_one_node */
PList ptr = NULL;
char *end_ptr, *st_ptr;
static PList curr = NULL; /* keep track of current end of list */
int groupnr = 0, retval = RETVAL_OK;
long msgnr = 0;
static int warned = FALSE;
/* get the article nr */
if(group == NULL) {
/* we're called by do_supplemental */
st_ptr = linein;
}
else {
if (msgnr_in > 0) {
/* we're prob called by xover code */
st_ptr = linein;
msgnr = msgnr_in;
}
else {
st_ptr = get_long(linein,&msgnr);
if(msgnr == 0 && warned == FALSE) {
warned = TRUE;
error_log(ERRLOG_REPORT, suck_phrases[53], NULL);
}
}
if(msgnr > 0 && group != NULL) {
groupnr = get_group_number(master, group);
}
}
if(retval == RETVAL_OK) {
/* find the message id */
while(*st_ptr != '<' && *st_ptr != '\0') {
st_ptr++;
}
end_ptr = st_ptr;
while(*end_ptr != '>' && *end_ptr != '\0') {
end_ptr++;
}
if((*st_ptr != '<') || ( master->chk_msgid == TRUE && *end_ptr != '>')) {
error_log(ERRLOG_REPORT, suck_phrases[21], linein, NULL);
}
else {
*(end_ptr+1) = '\0'; /* ensure null termination */
if((ptr = malloc(sizeof(List))) == NULL) {
error_log(ERRLOG_REPORT, suck_phrases[22], NULL);
retval = RETVAL_ERROR;
}
else if(strlen(st_ptr) >= MAX_MSGID_LEN) {
error_log(ERRLOG_REPORT, suck_phrases[63], group, msgnr, NULL);
free(ptr);
}
else {
strcpy(ptr->msgnr, st_ptr);
ptr->next = NULL;
ptr->mandatory = (char) mandatory;
ptr->sentcmd = FALSE; /* have we sent command to remote */
ptr->nr = msgnr;
ptr->groupnr = groupnr;
ptr->downloaded = FALSE;
ptr->dbnr = 0L;
ptr->delete = FALSE;
if(master->debug == TRUE) {
do_debug("MSGID %s NR %d GRP %d MANDATORY %c added\n",ptr->msgnr, ptr->nr, ptr->groupnr, ptr->mandatory);
}
/* now put on list */
if( curr == NULL) {
if(master->head == NULL) {
/* first node */
master->head = curr = ptr;
}
else {
/* came in with a restart, find end of list */
curr = master->head;
while(curr->next != NULL) {
curr = curr->next;
}
/* now add node on */
curr->next = ptr;
curr = ptr;
}
}
else {
curr->next = ptr;
curr = ptr;
}
master->nritems++;
}
}
}
return retval;
}
/*------------------------------------------------------------------------*/
void free_one_node(PList node) {
free(node);
}
/*----------------------------------------------------------------------------------*/
const char *build_command(PMaster master, const char *cmdstart, PList article) {
static char cmd[MAXLINLEN+1];
static int warned = FALSE;
char *resp;
char grpcmd[MAXLINLEN+1];
PGroups grps;
/* build command to get article/head/body */
/* if nrmode is on send group cmd if needed */
if(master->nrmode == TRUE) {
if(article->nr == 0) {
master->nrmode = FALSE;
}
else if(master->grpnr != article->groupnr) {
grps = master->groups;
while(grps != NULL && grps->nr != article->groupnr) {
grps = grps->next;
}
if(grps != NULL) {
/* okay send group command */
snprintf(grpcmd, MAXLINLEN, "GROUP %s\r\n", grps->group);
if(send_command(master, grpcmd, &resp, 211) != RETVAL_OK) {
master->nrmode = FALSE; /* can't chg groups turn it off */
}
else {
/* so don't redo group command on next one */
master->grpnr = grps->nr;
}
}
}
if(master->nrmode == TRUE) {
/* everything hunky dory, we've switched groups, and got a good nr */
snprintf(cmd, MAXLINLEN, "%s %ld\r\n", cmdstart, article->nr);
}
else if(warned == FALSE) {
/* tell of the switch to nonnr mode */
warned = TRUE;
error_log(ERRLOG_REPORT, suck_phrases[54], NULL);
}
}
/* the if is in case we changed it above */
if(master->nrmode == FALSE) {
snprintf(cmd, MAXLINLEN, "%s %s\r\n", cmdstart, article->msgnr);
}
return cmd;
}
/*----------------------------------------------------------------------------------*/
int get_one_article(PMaster master, int logcount, long itemon) {
int nr, len, retval = RETVAL_OK;
char buf[MAXLINLEN+1];
const char *cmd = "", *tname = NULL;
char fname[PATH_MAX+1];
char *resp;
PList plist;
FILE *fptr = stdout; /* the default */
fname[0] = '\0'; /* just in case */
/* first send command to get article if not already sent */
if((master->curr)->sentcmd == FALSE) {
cmd = (master->header_only == FALSE)
? build_command(master, "article", master->curr)
: build_command(master, "head", master->curr);
if(master->debug == TRUE) {
do_debug("Sending command: \"%s\"",cmd);
}
sputline(master->sockfd, cmd, master->do_ssl, master->ssl_struct);
(master->curr)->sentcmd = TRUE;
}
plist = (master->curr)->next;
if(plist != NULL && master->batch_post_nr == 0) {
/* if batch_post_nr > 0 we're doing periodic batch posts */
/* which confuses the remote server so we can't use pipelining */
if(master->nrmode == FALSE || plist->groupnr == (master->curr)->groupnr) {
/* can't use pipelining if nrmode on, cause of GROUP command I'll have to send*/
if(plist->sentcmd == FALSE) {
/* send next command */
cmd = (master->header_only == FALSE)
? build_command(master, "article", plist)
: build_command(master, "head", plist);
if(master->debug == TRUE) {
do_debug("Sending command: \"%s\"",cmd);
}
sputline(master->sockfd, cmd, master->do_ssl, master->ssl_struct);
plist->sentcmd = TRUE;
}
}
}
/* now while the remote is finding the article lets set up */
if(master->MultiFile == TRUE) {
/* open file */
/* file name will be ####-#### ex 001-166 (nron,total) */
sprintf(buf,"%0*ld-%d", logcount, itemon ,master->nritems);
/* the strcpy to avoid wiping out fname in second call to full_path */
strcpy(fname, full_path(FP_GET, FP_MSGDIR, buf));
strcat(buf, N_TMP_EXTENSION); /* add tmp extension */
tname = full_path(FP_GET, FP_TMPDIR, buf); /* temp file name */
if(master->debug == TRUE) {
do_debug("File name = \"%s\" temp = \"%s\"", fname, tname);
}
if((fptr = fopen(tname, "w")) == NULL) {
MyPerror(tname);
retval = RETVAL_ERROR;
}
}
/* okay hopefully by this time the remote end is ready */
if((len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct)) < 0) {
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
if(nr == 480) {
/* got to authorize, negate send-ahead for next msg */
if(plist != NULL) {
plist->sentcmd = FALSE; /* cause its gonna error out in do_auth() */
}
if(do_authenticate(master) == RETVAL_OK) {
/* resend command for current article */
cmd = build_command(master, "article", master->curr);
if(master->debug == TRUE) {
do_debug("Sending command: \"%s\"",cmd);
}
sputline(master->sockfd, cmd, master->do_ssl, master->ssl_struct);
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
}
else {
retval = RETVAL_ERROR;
}
}
/* 221 = header 220 = article */
if(nr == 220 || nr == 221 ) {
/* get the article */
if((retval = get_a_chunk(master, fptr)) == RETVAL_OK) {
master->nrgot++;
if (fptr == stdout) {
/* gracefully end the message in stdout version */
fputs(".\n", stdout);
}
}
}
else
{
/* don't return RETVAL_ERROR so we can get next article */
error_log(ERRLOG_REPORT, suck_phrases[29], cmd ,resp, NULL);
}
}
if(fptr != NULL && fptr != stdout) {
fclose(fptr);
}
if(master->MultiFile == TRUE) {
if(retval == RETVAL_ERROR || (nr != 221 && nr != 220)) {
unlink(tname);
}
/* now rename it to the permanent file name */
else {
move_file(tname, fname);
if((master->batch == BATCH_INNFEED || master->batch == BATCH_LIHAVE) && master->innfeed != NULL) {
/* write path name and msgid to file */
fprintf(master->innfeed, "%s %s\n", fname, (master->curr)->msgnr);
fflush(master->innfeed); /* so it gets written sooner */
}
}
}
return retval;
}
/*---------------------------------------------------------------------------*/
int get_a_chunk(PMaster master, FILE *fptr) {
int done, partial, len, retval;
char *inbuf;
size_t nr;
retval = RETVAL_OK;
done = FALSE;
partial = FALSE;
len = 0; /* just to get rid of a compiler warning */
/* partial = was the previous line a complete line or not */
/* this is needed to avoid a scenario where the line is MAXLINLEN+1 */
/* long and the last character is a ., which would make us think */
/* that we are at the end of the article when we actually aren't */
while(done == FALSE && (len = sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct)) >= 0) {
TimerFunc(TIMER_ADDBYTES, len, NULL);
if(inbuf[0] == '.' && partial == FALSE) {
if(len == 2 && inbuf[1] == '\n') {
done = TRUE;
}
else {
/* handle double dots IAW RFC977 2.4.1*/
inbuf++; /* move past first dot */
len--;
}
}
if(done == FALSE) {
if(retval == RETVAL_OK) {
nr = fwrite(inbuf, sizeof(inbuf[0]), len, fptr);
fflush(fptr);
if(nr != len) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[57], NULL);
}
}
partial= (len==MAXLINLEN&&inbuf[len-1]!='\n') ? TRUE : FALSE;
}
}
if(len < 0) {
retval = RETVAL_ERROR;
}
return retval;
}
/*-----------------------------------------------------------------------------*/
int restart_yn(PMaster master) {
int done, retval = RESTART_NO;
PList itemon;
long itemnr;
const char *fname;
struct stat buf;
fname = full_path(FP_GET, FP_TMPDIR, N_DBFILE);
if(stat(fname, &buf) == 0) {
/* restart file exists */
retval = db_read(master);
if(retval == RETVAL_OK) {
/* find out where we stopped */
itemon = master->head;
itemnr = 0L;
done = FALSE;
while(itemon != NULL && done == FALSE) {
itemnr++;
if(itemon->downloaded == FALSE) {
/* so that we don't think we already sent command */
itemon->sentcmd = FALSE;
if(master->skip_on_restart == TRUE) {
/* mark one more */
print_phrases(master->msgs, suck_phrases[60], NULL);
itemon->downloaded = TRUE;
}
else {
itemnr--; /* don't count this one */
}
done = TRUE;
}
itemon = itemon->next;
}
}
}
return retval;
}
/*-----------------------------------------------------------------*/
#ifdef MYSIGNAL
RETSIGTYPE sighandler(int what) {
switch(what) {
case PAUSESIGNAL:
pause_signal(PAUSE_DO, NULL);
/* if we don't do this, the next time called, we'll abort */
/* signal(PAUSESIGNAL, sighandler); */
/* signal_block(MYSIGNAL_SETUP); */ /* just to be on the safe side */
break;
case MYSIGNAL:
case MYSIGNAL2:
default:
error_log(ERRLOG_REPORT, suck_phrases[24], NULL);
GotSignal = TRUE;
}
}
/*----------------------------------------------------------------*/
void pause_signal(int action, PMaster master) {
int x, y;
static PMaster psave = NULL;
switch(action) {
case PAUSE_SETUP:
psave = master;
break;
case PAUSE_DO:
if(psave == NULL) {
error_log(ERRLOG_REPORT, suck_phrases[25], NULL);
}
else {
/* swap pause_time and pause_nrmsgs with the sig versions */
x = psave->pause_time;
y = psave->pause_nrmsgs;
psave->pause_time = psave->sig_pause_time;
psave->pause_nrmsgs = psave->sig_pause_nrmsgs;
psave->sig_pause_time = x;
psave->sig_pause_nrmsgs = y;
print_phrases(psave->msgs, suck_phrases[26], NULL);
}
}
}
#endif
/*----------------------------------------------------------------*/
int send_command(PMaster master, const char *cmd, char **ret_response, int good_response) {
/* this is needed so can do user authorization */
int len, retval = RETVAL_OK, nr;
char *resp;
if(master->debug == TRUE) {
do_debug("sending command: %s", cmd);
}
sputline(master->sockfd, cmd, master->do_ssl, master->ssl_struct);
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
if( len < 0) {
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
if(nr == 480 ) {
/* we must do authorization */
retval = do_authenticate(master);
if(retval == RETVAL_OK) {
/* resend command */
sputline(master->sockfd, cmd, master->do_ssl, master->ssl_struct);
if(master->debug == TRUE) {
do_debug("sending command: %s", cmd);
}
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
if( len < 0) {
retval = RETVAL_ERROR;
}
else {
number(resp,&nr);
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
}
}
}
if (good_response != 0 && nr != good_response) {
error_log(ERRLOG_REPORT, suck_phrases[29],cmd,resp,NULL);
retval = RETVAL_UNEXPECTEDANS;
}
}
if(ret_response != NULL) {
*ret_response = resp;
}
return retval;
}
/*------------------------------------------------------------------------------*/
/* 1. move N_OLDRC to N_OLD_OLDRC N_OLDRC might not exist */
/* 2. move N_NEWRC to N_OLDRC */
/* 3. rm N_SUPPLEMENTAL */
/*------------------------------------------------------------------------------*/
void do_cleanup(PMaster master) {
const char *oldptr;
char ptr[PATH_MAX+1];
struct stat buf;
int exist;
int okay = TRUE;
if(master->debug == TRUE) {
do_debug("checking for existance of suck.newrc\n");
}
if(stat(full_path(FP_GET, FP_TMPDIR, N_NEWRC), &buf) == 0) {
if(master->debug == TRUE) {
do_debug("found suck.newrc\n");
}
/* we do the above test, in case we were in a restart */
/* with -R which would cause no suck.newrc to be created */
/* since message_index() would be skipped */
/* since no suck.newrc, don't move any of the sucknewrcs around */
strcpy(ptr,full_path(FP_GET, FP_DATADIR, N_OLDRC));
/* must strcpy since full path overwrites itself everytime */
oldptr = full_path(FP_GET, FP_DATADIR, N_OLD_OLDRC);
/* does the sucknewsrc file exist ? */
exist = TRUE;
if(stat(ptr, &buf) != 0 && errno == ENOENT) {
exist = FALSE;
}
if(master->debug == TRUE) {
do_debug("sucknewsrc.old = %s sucknewsrc = %s exist = %s\n", oldptr, ptr, true_str(exist));
}
if(exist == TRUE && move_file(ptr, oldptr) != 0) {
MyPerror(suck_phrases[30]);
okay = FALSE;
}
else if(move_file(full_path(FP_GET, FP_TMPDIR, N_NEWRC), ptr) != 0) {
MyPerror(suck_phrases[31]);
okay = FALSE;
}
}
else if( errno != ENOENT) {
MyPerror(full_path(FP_GET, FP_DATADIR, N_NEWRC));
okay = FALSE;
}
if(okay == TRUE && unlink(full_path(FP_GET, FP_DATADIR, N_SUPPLEMENTAL)) != 0 && errno != ENOENT) {
/* ENOENT is not an error since this file may not always exist */
MyPerror(suck_phrases[33]);
}
}
/*--------------------------------------------------------------------------------------*/
int scan_args(PMaster master, int argc, char *argv[]) {
int loop, i, whicharg, arg, retval = RETVAL_OK;
for(loop = 0 ; loop < argc && retval == RETVAL_OK ; loop++) {
arg = ARG_NO_MATCH;
whicharg = -1;
if(master->debug == TRUE) {
do_debug("Checking arg #%d-%d: '%s'\n", loop, argc, argv[loop]);
}
if(argv[loop][0] == FILE_CHAR) {
/* totally skip these they are processed elsewhere */
continue;
}
if(argv[loop][0] == '-' && argv[loop][1] == '-') {
/* check long args */
for(i = 0 ; i < NR_ARGS && arg == ARG_NO_MATCH ; i++) {
if(arglist[i].larg != NULL) {
if(strcmp(&argv[loop][2], arglist[i].larg) == 0) {
arg = arglist[i].flag;
whicharg = i;
}
}
}
}
else if (argv[loop][0] == '-') {
/* check short args */
for(i = 0 ; i < NR_ARGS ; i++) {
if(arglist[i].sarg != NULL) {
if(strcmp(&argv[loop][1], arglist[i].sarg) == 0) {
arg = arglist[i].flag;
whicharg = i;
}
}
}
}
if(arg == ARG_NO_MATCH) {
/* we ignore the file_char arg, it is processed elsewhere */
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[34], argv[loop], NULL);
}
else {
/* need to check for valid nr of args and feed em down */
if(( loop + arglist[whicharg].nr_params) < argc) {
retval = parse_args(master, arg, &(argv[loop+1]));
/* skip em so they don't get processed as args */
loop += arglist[whicharg].nr_params;
}
else {
i = (arglist[whicharg].errmsg < 0) ? 34 : arglist[whicharg].errmsg;
error_log(ERRLOG_REPORT, suck_phrases[i], NULL);
retval = RETVAL_ERROR;
}
}
}
return retval;
}
/*---------------------------------------------------------------------------------------*/
int parse_args(PMaster master, int arg, char *argv[]) {
int x, retval = RETVAL_OK;
switch(arg) {
case ARG_ALWAYS_BATCH:
/* if we have downloaded at least one article, then batch up */
/* even on errors */
master->always_batch = TRUE;
break;
/* batch file implies MultiFile mode */
case ARG_BATCH_INN:
master->batch = BATCH_INNXMIT;
master->MultiFile = TRUE;
master->batchfile = argv[0];
break;
case ARG_BATCH_RNEWS:
master->batch = BATCH_RNEWS;
master->MultiFile = TRUE;
master->batchfile = argv[0];
break;
case ARG_BATCH_LMOVE:
master->batch = BATCH_LMOVE;
master->MultiFile = TRUE;
master->batchfile = argv[0];
break;
case ARG_BATCH_INNFEED:
master->batch = BATCH_INNFEED;
master->MultiFile = TRUE;
master->batchfile = argv[0];
break;
case ARG_BATCH_POST_NR:
master->batch_post_nr = atoi(argv[0]);
/* no break fall thru to the rest of batch_post*/
case ARG_BATCH_POST:
master->batch = BATCH_LIHAVE;
master->MultiFile = TRUE;
master->batchfile = N_POSTFILE;
break;
case ARG_CLEANUP: /* cleanup option */
master->cleanup = TRUE;
break;
case ARG_DIR_TEMP:
full_path(FP_SET, FP_TMPDIR, argv[0]);
break;
case ARG_DIR_DATA:
full_path(FP_SET, FP_DATADIR, argv[0]);
break;
case ARG_DIR_MSGS:
full_path(FP_SET, FP_MSGDIR, argv[0]);
break;
case ARG_DEF_ERRLOG: /* use default error log path */
error_log(ERRLOG_SET_FILE, ERROR_LOG, NULL);
master->errlog = ERROR_LOG;
break;
case ARG_HOST: /* host name */
master->host = argv[0];
break;
case ARG_NO_POSTFIX: /* kill files don't use the postfix */
master->kill_ignore_postfix = TRUE;
break;
case ARG_LANGUAGE: /* load language support */
master->phrases = argv[0];
break;
case ARG_MULTIFILE:
master->MultiFile = TRUE;
break;
case ARG_POSTFIX:
full_path(FP_SET_POSTFIX, FP_NONE, argv[0]);
break;
case ARG_QUIET: /* don't display BPS and message count */
master->quiet = TRUE;
break;
case ARG_RNEWSSIZE:
master->rnews_size = atol(argv[0]);
break;
case ARG_DEF_STATLOG: /* use default status log name */
master->status_file_name = STATUS_LOG;
break;
case ARG_WAIT_SIG: /* wait (sleep) x seconds between every y articles IF we get a signal */
master->sig_pause_time = atoi(argv[0]);
master->sig_pause_nrmsgs = atoi(argv[1]);
break;
case ARG_ACTIVE:
master->do_active = TRUE;
break;
case ARG_RECONNECT: /* how often do we drop and reconnect to battle INNS's LIKE_PULLER=DONT */
master->reconnect_nr = atoi(argv[0]);
break;
case ARG_DEBUG: /* debug */
master->debug = TRUE;
/* now set up our error log and MyPerror reporting so it goes to debug.suck */
error_log(ERRLOG_SET_DEBUG, NULL, NULL);
break;
case ARG_ERRLOG: /* error log path */
error_log(ERRLOG_SET_FILE, argv[0], NULL);
master->errlog = argv[0];
break;
case ARG_HISTORY:
master->do_chkhistory = FALSE;
break;
case ARG_KILLFILE:
master->do_killfile = FALSE;
break;
case ARG_KLOG_NONE:
master->killfile_log = KILL_LOG_NONE;
break;
case ARG_KLOG_SHORT:
master->killfile_log = KILL_LOG_SHORT;
break;
case ARG_KLOG_LONG:
master->killfile_log = KILL_LOG_LONG;
break;
case ARG_MODEREADER: /* mode reader */
master->do_modereader = TRUE;
break;
case ARG_PORTNR: /* override default portnr */
master->portnr = atoi(argv[0]);
break;
case ARG_PASSWD: /* passwd */
master->passwd = argv[0];
break;
case ARG_RESCAN: /* don't rescan on restart */
master->rescan = FALSE;
break;
case ARG_STATLOG: /* status log path */
master->status_file_name = argv[0];
break;
case ARG_USERID: /* userid */
master->userid = argv[0];
break;
case ARG_PASSWD_ENV: /* get userid/password from ENV */
master->userid = getenv("NNTP_USER");
master->passwd = getenv("NNTP_PASS");
master->passwd_env = TRUE;
break;
case ARG_VERSION: /* show version number */
error_log(ERRLOG_REPORT,"Suck version %v1%\n",SUCK_VERSION, NULL);
retval = RETVAL_VERNR; /* so we don't do anything else */
break;
case ARG_WAIT: /* wait (sleep) x seconds between every y articles */
master->pause_time = atoi(argv[0]);
master->pause_nrmsgs = atoi(argv[1]);
break;
case ARG_LOCALHOST:
master->localhost = argv[0];
break;
case ARG_NRMODE:
master->nrmode = TRUE;
break;
case ARG_AUTOAUTH:
master->auto_auth = TRUE;
break;
case ARG_NODEDUPE:
master->no_dedupe = TRUE;
break;
case ARG_READACTIVE:
master->activefile = argv[0];
break;
case ARG_PREBATCH:
master->prebatch = TRUE;
break;
case ARG_SKIP_ON_RESTART:
master->skip_on_restart = TRUE;
break;
case ARG_KLOG_NAME:
master->kill_log_name = argv[0];
break;
case ARG_USEGUI:
master->use_gui = TRUE;
break;
case ARG_XOVER:
master->do_xover = FALSE;
break;
case ARG_CONN_DEDUPE:
master->conn_dedupe = TRUE;
break;
case ARG_POST_FILTER:
master->post_filter = argv[0];
break;
case ARG_CONN_ACTIVE:
master->conn_active = TRUE;
break;
case ARG_HIST_FILE:
master->history_file = argv[0];
break;
case ARG_HEADER_ONLY:
master->header_only = TRUE;
break;
case ARG_ACTIVE_LASTREAD:
x = atoi(argv[0]);
if(x > 0) {
error_log(ERRLOG_REPORT, suck_phrases[66], NULL);
}
else {
master->active_lastread = atoi(argv[0]);
}
break;
case ARG_USEXOVER:
master->use_xover = TRUE;
break;
case ARG_RESETCOUNTER:
master->resetcounter = TRUE;
break;
case ARG_LOW_READ:
master->low_read = TRUE;
break;
case ARG_SHOW_GROUP:
master->show_group = TRUE;
break;
#ifdef TIMEOUT
case ARG_TIMEOUT:
TimeOut = atoi(argv[0]);
break;
#endif
#ifdef HAVE_LIBSSL
case ARG_USE_SSL:
master->do_ssl = TRUE;
master->portnr = DEFAULT_SSL_PORT;
break;
case ARG_LOCAL_SSL:
master->local_ssl = TRUE;
break;
#endif
}
return retval;
}
/*----------------------------------*/
/* authenticate when receive a 480 */
/*----------------------------------*/
int do_authenticate(PMaster master) {
int len, nr, retval = RETVAL_OK;
char *resp, buf[MAXLINLEN];
if(master->userid == NULL || master->passwd == NULL) {
error_log(ERRLOG_REPORT, suck_phrases[73], NULL);
retval = RETVAL_NOAUTH;
}
else {
/* we must do authorization */
sprintf(buf, "AUTHINFO USER %s\r\n", master->userid);
if(master->debug == TRUE) {
do_debug("sending command: %s", buf);
}
sputline(master->sockfd, buf, master->do_ssl, master->ssl_struct);
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
if( len < 0) {
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
if(nr == 480) {
/* this is because of the pipelining code */
/* we get the second need auth */
/* just ignore it and get the next line */
/* which should be the 381 we need */
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
}
if(nr != 381) {
error_log(ERRLOG_REPORT, suck_phrases[27], resp, NULL);
retval = RETVAL_NOAUTH;
}
else {
sprintf(buf, "AUTHINFO PASS %s\r\n", master->passwd);
sputline(master->sockfd, buf, master->do_ssl, master->ssl_struct);
if(master->debug == TRUE) {
do_debug("sending command: %s", buf);
}
len = sgetline(master->sockfd, &resp, master->do_ssl, master->ssl_struct);
if(len < 0) {
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("got answer: %s", resp);
}
TimerFunc(TIMER_ADDBYTES, len, NULL);
number(resp, &nr);
switch(nr) {
case 281: /* bingo */
retval = RETVAL_OK;
break;
case 502: /* permission denied */
retval = RETVAL_NOAUTH;
error_log(ERRLOG_REPORT, suck_phrases[28], NULL);
break;
default: /* wacko error */
error_log(ERRLOG_REPORT, suck_phrases[27], resp, NULL);
retval = RETVAL_NOAUTH;
break;
}
}
}
}
}
return retval;
}
/*--------------------------------------------------------------------------------*/
/* THE strings in this routine is the only one not in the arrays, since */
/* we are in the middle of reading the arrays, and they may or may not be valid. */
/*--------------------------------------------------------------------------------*/
void load_phrases(PMaster master) {
int error=TRUE;
FILE *fpi;
char buf[MAXLINLEN];
if(master->phrases != NULL) {
if((fpi = fopen(master->phrases, "r")) == NULL) {
MyPerror(master->phrases);
}
else {
fgets(buf, MAXLINLEN, fpi);
if(strncmp( buf, SUCK_VERSION, strlen(SUCK_VERSION)) != 0) {
error_log(ERRLOG_REPORT, "Invalid Phrase File, wrong version\n", NULL);
}
else if((both_phrases = read_array(fpi, NR_BOTH_PHRASES, TRUE)) != NULL) {
read_array(fpi, NR_RPOST_PHRASES, FALSE); /* skip these */
read_array(fpi, NR_TEST_PHRASES, FALSE);
if(( suck_phrases = read_array(fpi, NR_SUCK_PHRASES, TRUE)) != NULL &&
( timer_phrases= read_array(fpi, NR_TIMER_PHRASES,TRUE)) &&
( chkh_phrases= read_array(fpi, NR_CHKH_PHRASES,TRUE)) &&
(dedupe_phrases= read_array(fpi, NR_DEDUPE_PHRASES,TRUE)) &&
( killf_reasons= read_array(fpi, NR_KILLF_REASONS,TRUE)) &&
( killf_phrases= read_array(fpi, NR_KILLF_PHRASES,TRUE)) &&
( sucku_phrases= read_array(fpi, NR_SUCKU_PHRASES,TRUE)) &&
(read_array(fpi, NR_LMOVE_PHRASES, FALSE) == NULL) && /* skip this one */
( active_phrases=read_array(fpi, NR_ACTIVE_PHRASES,TRUE)) &&
( batch_phrases= read_array(fpi, NR_BATCH_PHRASES,TRUE)) &&
( xover_phrases= read_array(fpi, NR_XOVER_PHRASES,TRUE)) &&
( xover_reasons= read_array(fpi, NR_XOVER_REASONS,TRUE))) {
error = FALSE;
}
}
}
fclose(fpi);
if(error == TRUE) {
/* reset back to default */
error_log(ERRLOG_REPORT, "Using default Language phrases\n", NULL);
both_phrases = default_both_phrases;
suck_phrases=default_suck_phrases;
timer_phrases=default_timer_phrases;
chkh_phrases=default_chkh_phrases;
dedupe_phrases=default_dedupe_phrases;
killf_reasons=default_killf_reasons;
killf_phrases=default_killf_phrases;
killp_phrases=default_killp_phrases;
sucku_phrases=default_sucku_phrases;
active_phrases=default_active_phrases;
batch_phrases=default_batch_phrases;
xover_phrases=default_xover_phrases;
xover_reasons=default_xover_reasons;
}
}
}
/*--------------------------------------------------------------------------------*/
void free_phrases(void) {
/* free up the memory alloced in load_phrases() */
if(both_phrases != default_both_phrases) {
free_array(NR_BOTH_PHRASES, both_phrases);
}
if(suck_phrases != default_suck_phrases) {
free_array(NR_SUCK_PHRASES, suck_phrases);
}
if(timer_phrases != default_timer_phrases) {
free_array(NR_TIMER_PHRASES, timer_phrases);
}
if(chkh_phrases != default_chkh_phrases) {
free_array(NR_CHKH_PHRASES, chkh_phrases);
}
if(dedupe_phrases != default_dedupe_phrases) {
free_array(NR_DEDUPE_PHRASES, dedupe_phrases);
}
if(killf_reasons != default_killf_reasons) {
free_array(NR_KILLF_REASONS, killf_reasons);
}
if(killf_phrases != default_killf_phrases) {
free_array(NR_KILLF_PHRASES, killf_phrases);
}
if(killp_phrases != default_killp_phrases) {
free_array(NR_KILLP_PHRASES, killp_phrases);
}
if(sucku_phrases != default_sucku_phrases) {
free_array(NR_SUCKU_PHRASES, sucku_phrases);
}
if(active_phrases != default_active_phrases) {
free_array(NR_ACTIVE_PHRASES, active_phrases);
}
if(batch_phrases != default_batch_phrases) {
free_array(NR_BATCH_PHRASES, batch_phrases);
}
if(xover_phrases != default_xover_phrases) {
free_array(NR_XOVER_PHRASES, xover_phrases);
}
if(xover_reasons != default_xover_reasons) {
free_array(NR_XOVER_REASONS, xover_reasons);
}
}
|