home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B61.ZIP / NEWS.CPP < prev    next >
C/C++ Source or Header  |  1997-11-03  |  54KB  |  1,990 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <ctype.h>
  4. #include <process.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <io.h>
  12. #include <dos.h>
  13. #include <share.h>
  14. #include <dir.h>
  15. #include <alloc.h>
  16. #include <mem.h>
  17. #include <sys\stat.h>
  18. #include <math.h>
  19.  
  20. extern "C" {
  21.   #include "tcp.h"
  22. }
  23.  
  24. #include "net.h"
  25. #include "vardec.h"
  26. #include "version.h"
  27. #include "retcode.h"
  28.  
  29. extern unsigned _stklen = 6000U;
  30.  
  31. #define SHARE_LEVEL 10
  32. #define WAIT_TIME 10
  33. #define TRIES 100
  34.  
  35. #define MT_DESQVIEW 0x01
  36. #define MT_WINDOWS  0x02
  37. #define MT_OS2      0x04
  38. #define MT_NB       0x40
  39.  
  40. #define BIGSTR 2048
  41. #define STRING 513
  42. #define STR 129
  43. #define WAIT_TIME 10
  44. #define TRIES 100
  45. #define NUL '\0'
  46. #define LAST(s) s[strlen(s)-1]
  47. #define NNTP_PORT 119
  48. #define MAX_LOG 800
  49.  
  50. typedef struct {
  51.   tcp_Socket *sock;
  52. } Mail_Socket;
  53.  
  54. configrec syscfg;
  55.  
  56. char *version = "Freeware PPP Project News Retrieval " VERSION;
  57.  
  58. static char _temp_buffer[STRING];
  59. char POPNAME[25], POPDOMAIN[45];
  60. char cur_path[STRING], cur_from[STRING], cur_subject[STRING], cur_replyto[STRING];
  61. char cur_newsgroups[STRING], cur_message_ID[STRING], cur_organization[STRING];
  62. char cur_articleid[STRING], cur_references[STRING], cur_lines[STR], cur_date[STRING];
  63. static char maindir[MAXPATH], net_data[MAXPATH];
  64. int instance, crossposts, binxpost, multitasker = 0;
  65. static int spooltodisk, NNTP_stat;
  66. unsigned long cur_daten, reparticle, cur_numa, cur_first, cur_last;
  67. unsigned short reply, sy;
  68. static char NEWSNAME[STRING], NEWSPASS[STRING];
  69.  
  70. typedef struct {
  71.   char groupname[60];
  72.   unsigned long lastread;
  73.   char subtype[8];
  74. } GROUPFILEREC;
  75.  
  76. GROUPFILEREC *grouprec;
  77. int ngroups;
  78.  
  79. #define SOCK_READ_ERR(PROTOCOL) sock_err:                                 \
  80.   switch (PROTOCOL##_stat) {                                              \
  81.     case 1 :                                                              \
  82.       fprintf(stderr, "\n ! Session error : %s",                          \
  83.           sockerr(PROTOCOL##_sock->sock));                                \
  84.       fcloseall();                                                        \
  85.       if (PROTOCOL##_sock->sock != NULL)                                  \
  86.         farfree(PROTOCOL##_sock->sock);                                   \
  87.       if (PROTOCOL##_sock != NULL)                                        \
  88.         farfree(PROTOCOL##_sock);                                         \
  89.       cursor('R');                                                        \
  90.       exit(EXIT_FAILURE);                                                 \
  91.     case -1:                                                              \
  92.       fprintf(stderr, "\n ! Timeout : %s",                                \
  93.           sockerr(PROTOCOL##_sock->sock));                                \
  94.       fcloseall();                                                        \
  95.       if (PROTOCOL##_sock->sock != NULL)                                  \
  96.         farfree(PROTOCOL##_sock->sock);                                   \
  97.       if (PROTOCOL##_sock != NULL)                                        \
  98.         farfree(PROTOCOL##_sock);                                         \
  99.       cursor('R');                                                        \
  100.       exit(EXIT_FAILURE);                                                 \
  101.   }                                                                       \
  102.  
  103.  
  104. char *texth[] = {"th", "st", "nd", "rd"};
  105.  
  106. char *ordinal_text(int number)
  107. {
  108.   if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
  109.     number = 0;
  110.   return texth[number];
  111. }
  112.  
  113. static unsigned cursize;
  114.  
  115. void cursor(int tmp)
  116. {
  117.   union REGS inregs, outregs;
  118.  
  119.   switch (toupper(tmp)) {
  120.     case 'S':                               /* Save */
  121.       inregs.h.ah = 3;
  122.       inregs.h.bh = 0;
  123.       int86(0x10, &inregs, &outregs);
  124.       cursize = outregs.x.cx;
  125.       break;
  126.     case 'R':                               /* Restore */
  127.       inregs.h.ah = 1;
  128.       inregs.x.cx = cursize;
  129.       int86(0x10, &inregs, &outregs);
  130.       break;
  131.     case 'H':                               /* Hide */
  132.       inregs.h.ah = 1;
  133.       inregs.h.ch = 0x20;
  134.       int86(0x10, &inregs, &outregs);
  135.       break;
  136.     case 'N':                               /* Normal */
  137.       inregs.h.ah = 1;
  138.       inregs.h.ch = 6;
  139.       inregs.h.cl = 7;
  140.       int86(0x10, &inregs, &outregs);
  141.       break;
  142.   }
  143. }
  144.  
  145. void dv_pause(void)
  146. {
  147.   __emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
  148.   __emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
  149.   __emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
  150. }
  151.  
  152. void win_pause(void)
  153. {
  154.   __emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
  155. }
  156.  
  157. int get_dos_version(void)
  158. {
  159.   _AX = 0x3000;
  160.   geninterrupt(0x21);
  161.   if (_AX % 256 >= 10) {
  162.     multitasker |= MT_OS2;
  163.   }
  164.   return (_AX);
  165. }
  166.  
  167. int get_dv_version(void)
  168. {
  169.   int v;
  170.  
  171.   if (multitasker & MT_OS2)
  172.     return 0;
  173.   _AX = 0x2b01;
  174.   _CX = 0x4445;
  175.   _DX = 0x5351;
  176.   geninterrupt(0x21);
  177.   if (_AL == 0xff) {
  178.     return 0;
  179.   } else {
  180.     v = _BX;
  181.     multitasker |= MT_DESQVIEW;
  182.     return v;
  183.   }
  184. }
  185.  
  186. int get_win_version(void)
  187. {
  188.   int v = 0;
  189.  
  190.   __emit__(0x55, 0x06, 0x53);
  191.   _AX = 0x352f;
  192.   geninterrupt(0x21);
  193.   _AX = _ES;
  194.   if (_AX | _BX) {
  195.     _AX = 0x1600;
  196.     geninterrupt(0x2f);
  197.     v = _AX;
  198.     if (v % 256 <= 1)
  199.       v = 0;
  200.   }
  201.   __emit__(0x5b, 0x07, 0x5d);
  202.   if (v != 0)
  203.     multitasker |= MT_WINDOWS;
  204.   return (v);
  205. }
  206.  
  207. int get_nb_version(void)
  208. {
  209.   _AX = 0;
  210.   geninterrupt(0x2A);
  211.   return (_AH);
  212. }
  213.  
  214. void detect_multitask(void)
  215. {
  216.   get_dos_version();
  217.   get_win_version();
  218.   get_dv_version();
  219.   if (multitasker < 2)
  220.     if (get_nb_version())
  221.       multitasker = MT_NB;
  222. }
  223.  
  224. void giveup_timeslice(void)
  225. {
  226.   if (multitasker) {
  227.     switch (multitasker) {
  228.  case 1: 
  229.  case 3: 
  230.         dv_pause();
  231.         break;
  232.       case 2:
  233.       case 4:
  234.       case 5:
  235.       case 6:
  236.       case 7:
  237.         win_pause();
  238.         break;
  239.       default:
  240.         break;
  241.     }
  242.   }
  243. }
  244.  
  245. void output(char *fmt,...)
  246. {
  247.   va_list v;
  248.   char s[255];
  249.  
  250.   va_start(v, fmt);
  251.   vsprintf(s, fmt, v);
  252.   va_end(v);
  253.   fputs(s, stderr);
  254. }
  255.  
  256. void backline(void)
  257. {
  258.   int i;
  259.  
  260.   output(" ");
  261.   for (i = wherex(); i > 0; i--)
  262.     output("\b \b");
  263. }
  264.  
  265. void cd_to(char *s)
  266. {
  267.   char *s1;
  268.   int i, db;
  269.  
  270.   s1 = s;
  271.   i = strlen(s1) - 1;
  272.   db = (s1[i] == '\\');
  273.   if (i == 0)
  274.     db = 0;
  275.   if ((i == 2) && (s1[1] == ':'))
  276.     db = 0;
  277.   if (db)
  278.     s1[i] = 0;
  279.   chdir(s1);
  280.   if (s[1] == ':')
  281.     setdisk(s[0] - 'A');
  282. }
  283.  
  284. void get_dir(char *s, int be)
  285. {
  286.   strcpy(s, "X:\\");
  287.   s[0] = 'A' + getdisk();
  288.   getcurdir(0, s + 3);
  289.   if (be) {
  290.     if (s[strlen(s) - 1] != '\\')
  291.       strcat(s, "\\");
  292.   }
  293. }
  294.  
  295. int sh_write(int handle, void *buffer, unsigned long len)
  296. {
  297.   if (handle == -1) {
  298.     return (-1);
  299.   }
  300.   return (write(handle, buffer, (unsigned) len));
  301. }
  302.  
  303. int sh_open(char *path, int file_access, unsigned fmode)
  304. {
  305.   int handle, count, share;
  306.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  307.  
  308.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  309.     share = SH_DENYRW;
  310.   } else {
  311.     share = SH_DENYWR;
  312.   }
  313.   handle = open(path, file_access | share, fmode);
  314.   if (handle < 0) {
  315.     count = 1;
  316.     fnsplit(path, drive, dir, file, ext);
  317.     if (access(path, 0) != -1) {
  318.       delay(WAIT_TIME);
  319.       handle = open(path, file_access | share, fmode);
  320.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  321.         if (count % 2)
  322.           delay(WAIT_TIME);
  323.         else
  324.           giveup_timeslice();
  325.         count++;
  326.         handle = open(path, file_access | share, fmode);
  327.       }
  328.     }
  329.   }
  330.   return (handle);
  331. }
  332.  
  333. int sh_open1(char *path, int access)
  334. {
  335.   unsigned fmode;
  336.  
  337.   fmode = 0;
  338.   if ((access & O_RDWR) || (access & O_WRONLY))
  339.     fmode |= S_IWRITE;
  340.   if ((access & O_RDWR) || (access & O_RDONLY))
  341.     fmode |= S_IREAD;
  342.   return (sh_open(path, access, fmode));
  343. }
  344.  
  345. int sh_close(int f)
  346. {
  347.   if (f != -1)
  348.     close(f);
  349.   return (-1);
  350. }
  351.  
  352. int sh_read(int handle, void *buf, unsigned len)
  353. {
  354.   if (handle == -1) {
  355.     return (-1);
  356.   }
  357.   return (read(handle, buf, len));
  358. }
  359.  
  360. long sh_lseek(int handle, long offset, int fromwhere)
  361. {
  362.   if (handle == -1) {
  363.     return (-1L);
  364.   }
  365.   return (lseek(handle, offset, fromwhere));
  366. }
  367.  
  368. FILE *fsh_open(char *path, char *fmode)
  369. {
  370.   FILE *f;
  371.   int count, share, md, fd;
  372.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  373.  
  374.   share = SH_DENYWR;
  375.   md = 0;
  376.   if (((char *) strchr(fmode, 'w')) != NULL) {
  377.     share = SH_DENYRD;
  378.     md = O_RDWR | O_CREAT | O_TRUNC;
  379.   } else
  380.     if (((char *) strchr(fmode, 'a')) != NULL) {
  381.     share = SH_DENYRD;
  382.     md = O_RDWR | O_CREAT;
  383.   } else {
  384.     md = O_RDONLY;
  385.   }
  386.   if (((char *) strchr(fmode, 'b')) != NULL) {
  387.     md |= O_BINARY;
  388.   }
  389.   if (((char *) strchr(fmode, '+')) != NULL) {
  390.     md &= ~O_RDONLY;
  391.     md |= O_RDWR;
  392.     share = SH_DENYRD;
  393.   }
  394.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  395.   if (fd < 0) {
  396.     count = 1;
  397.     fnsplit(path, drive, dir, file, ext);
  398.     if ((access(path, 0)) != -1) {
  399.       delay(WAIT_TIME);
  400.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  401.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  402.         delay(WAIT_TIME);
  403.         count++;
  404.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  405.       }
  406.     }
  407.   }
  408.   if (fd > 0) {
  409.     if (((char *) strchr(fmode, 'a')) != NULL)
  410.       sh_lseek(fd, 0L, SEEK_END);
  411.     f = fdopen(fd, fmode);
  412.     if (!f) {
  413.       close(fd);
  414.     }
  415.   } else
  416.     f = 0;
  417.   return (f);
  418. }
  419.  
  420. size_t fsh_write(void *ptr, size_t size, size_t n, FILE * stream)
  421. {
  422.  
  423.   if (stream == NULL) {
  424.     return (0);
  425.   }
  426.   return (fwrite(ptr, size, n, stream));
  427. }
  428.  
  429. char *stristr(char *string, char *pattern)
  430. {
  431.   char *pptr, *sptr, *start;
  432.   unsigned int slen, plen;
  433.  
  434.   for (start = string, pptr = pattern, slen = strlen(string),
  435.        plen = strlen(pattern); slen >= plen; start++, slen--) {
  436.     while (toupper(*start) != toupper(*pattern)) {
  437.       start++;
  438.       slen--;
  439.       if (slen < plen)
  440.         return (NULL);
  441.     }
  442.     sptr = start;
  443.     pptr = pattern;
  444.     while (toupper(*sptr) == toupper(*pptr)) {
  445.       sptr++;
  446.       pptr++;
  447.       if ('\0' == *pptr)
  448.         return (start);
  449.     }
  450.   }
  451.   return (NULL);
  452. }
  453.  
  454.  
  455.  
  456. void trim_log(void)
  457. {
  458.   int num_lines, total_lines, kill_lines;
  459.   FILE *old_log, *new_log;
  460.   char ol[101], nl[101], s[121];
  461.  
  462.   sprintf(ol, "%sNEWS.LOG", net_data);
  463.   sprintf(nl, "%sNEWS.ZZZ", net_data);
  464.  
  465.   old_log = fopen(ol, "r");
  466.   new_log = fopen(nl, "a");
  467.  
  468.   total_lines = 0;
  469.   if (old_log != NULL) {
  470.     while (!(fgets(s, 100, old_log) == NULL))
  471.       ++total_lines;
  472.     rewind(old_log);
  473.     if (total_lines < MAX_LOG) {
  474.       fclose(old_log);
  475.       if (new_log != NULL)
  476.         fclose(new_log);
  477.       unlink(nl);
  478.       return;
  479.     }
  480.     kill_lines = total_lines - MAX_LOG;
  481.     num_lines = 0;
  482.     while ((fgets(s, 80, old_log)) && (num_lines < kill_lines))
  483.       num_lines++;
  484.     while ((strstr(s, "NEWS session") == NULL) && (num_lines < total_lines)) {
  485.       fgets(s, 80, old_log);
  486.       num_lines++;
  487.     }
  488.     fputs(s, new_log);
  489.     while ((!(fgets(s, 80, old_log) == NULL)))
  490.       fputs(s, new_log);
  491.   }
  492.   if (old_log != NULL)
  493.     fclose(old_log);
  494.   if (new_log != NULL)
  495.     fclose(new_log);
  496.   unlink(ol);
  497.   rename(nl, ol);
  498. }
  499.  
  500. void ssm(char *s)
  501. {
  502.   int f, i, i1;
  503.   char s1[161];
  504.   shortmsgrec sm;
  505.   configrec syscfg;
  506.  
  507.   sprintf(s1, "CONFIG.DAT");
  508.   f = sh_open1(s1, O_RDONLY | O_BINARY);
  509.   if (f < 0)
  510.     return;
  511.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  512.   f = sh_close(f);
  513.   sprintf(s1, "%sSMW.DAT", syscfg.datadir);
  514.   f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  515.   if (f < 0)
  516.     return;
  517.   i = (int) (filelength(f) / sizeof(shortmsgrec));
  518.   i1 = i - 1;
  519.   if (i1 >= 0) {
  520.     sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  521.     sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  522.     while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
  523.       --i1;
  524.       sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  525.       sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  526.     }
  527.     if ((sm.tosys) || (sm.touser))
  528.       ++i1;
  529.   } else
  530.     i1 = 0;
  531.   sm.tosys = 0;
  532.   sm.touser = 1;
  533.   strncpy(sm.message, s, 80);
  534.   sm.message[80] = 0;
  535.   sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  536.   sh_write(f, (void *) &sm, sizeof(shortmsgrec));
  537.   sh_close(f);
  538. }
  539.  
  540. unsigned char *trimstr1(unsigned char *s)
  541. {
  542.   int i;
  543.   static char *whitespace = " \r\n\t";
  544.  
  545.   i = strlen(s);
  546.   while ((i > 0) && (strchr(whitespace, s[i - 1])))
  547.     --i;
  548.   while ((i > 0) && (strchr(whitespace, *s))) {
  549.     memmove(s, s + 1, --i);
  550.   }
  551.   s[i] = 0;
  552.   return (s);
  553. }
  554.  
  555.  
  556.  
  557. int log_it(int send_ssm, char *fmt,...)
  558. {
  559.   va_list v;
  560.   char s[255], fn[MAXPATH];
  561.   FILE *fp;
  562.  
  563.   sprintf(fn, "%sNEWS.LOG", net_data);
  564.   if ((fp = fsh_open(fn, "at")) == NULL)
  565.     return 1;
  566.   va_start(v, fmt);
  567.   vsprintf(s, fmt, v);
  568.   va_end(v);
  569.   fputs(s, fp);
  570.   fclose(fp);
  571.   fputs(s, stderr);
  572.   if (send_ssm) {
  573.     trimstr1(s);
  574.     ssm(s);
  575.   }
  576.   return 0;
  577. }
  578.  
  579.  
  580. int count_lines(char *s)
  581. {
  582.   FILE *fp;
  583.   char fn[201];
  584.   unsigned int nl = 0;
  585.   const int NEWLINE = '\n';
  586.   int c;
  587.  
  588.   strcpy(fn, s);
  589.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  590.     output("\n ■ Cannot open %s", fn);
  591.     return 0;
  592.   }
  593.   while ((c = getc(fp)) != EOF) {
  594.     if (c == NEWLINE)
  595.       ++nl;
  596.   }
  597.   fclose(fp);
  598.   return nl;
  599. }
  600.  
  601. int exist(char *s)
  602. {
  603.   int i;
  604.   struct ffblk ff;
  605.  
  606.   i = findfirst(s, &ff, 0);
  607.   if (i)
  608.     return (0);
  609.   else
  610.     return (1);
  611. }
  612.  
  613. void rename_pend(char *file)
  614. {
  615.   char s[181], s1[181];
  616.   int i, ok;
  617.  
  618.   sprintf(s, "%s%s", net_data, file);
  619.   ok = 0;
  620.   for (i = 0; i < 1000 && !ok; i++) {
  621.     sprintf(s1, "%sP0-%u.NET", net_data, i);
  622.     if (!exist(s1)) {
  623.       rename(s, s1);
  624.       ok = 1;
  625.     }
  626.   }
  627. }
  628.  
  629. void check_packets(void)
  630. {
  631.   char s[MAXPATH], s1[MAXPATH];
  632.   int f1;
  633.   struct ffblk ff;
  634.  
  635.   sprintf(s, "%sP0-*.%3.3d", net_data, instance);
  636.   f1 = findfirst(s, &ff, 0);
  637.   while (f1 == 0) {
  638.     if (ff.ff_fsize == 0L) {
  639.       sprintf(s1, "%s%s", net_data, ff.ff_name);
  640.       if (unlink(s1) != 0)
  641.         rename_pend(ff.ff_name);
  642.     } else
  643.       rename_pend(ff.ff_name);
  644.     f1 = findnext(&ff);
  645.   }
  646. }
  647.  
  648.  
  649. void write_groups(int display)
  650. {
  651.   int i;
  652.   char fn[160];
  653.   FILE *groupfp;
  654.  
  655.   sprintf(fn, "%sNEWS.RC", net_data);
  656.  
  657.   if ((groupfp = fsh_open(fn, "wt+")) == NULL) {
  658.     output("\n ■ Unable to open %s!", fn);
  659.     return;
  660.   } else {
  661.     if (display)
  662.       output("\n ■ Updating message pointers..");
  663.   }
  664.   for (i = 0; i < ngroups; i++)
  665.     if ((*grouprec[i].groupname) && (stricmp(grouprec[i].groupname, "newsrc") != 0))
  666.       fprintf(groupfp, "%s %lu %s\n", grouprec[i].groupname,
  667.               grouprec[i].lastread, grouprec[i].subtype);
  668.   if (groupfp != NULL)
  669.     fclose(groupfp);
  670. }
  671.  
  672. void nntp_shutdown(Mail_Socket * NNTP_sock)
  673. {
  674.   if (NNTP_sock->sock != NULL) {
  675.     sock_puts(NNTP_sock->sock, "QUIT");
  676.     sock_close(NNTP_sock->sock);
  677.     sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  678.   }
  679. sock_err:
  680.   return;
  681. }
  682.  
  683. void read_groups(void)
  684. {
  685.   int i = 0, ok;
  686.   unsigned short sn;
  687.   char *ss, fn[101], fn1[160], tmp[121];
  688.   FILE *groupfp;
  689.  
  690.   sprintf(fn1, "%sNEWS.RC", net_data);
  691.   ngroups = count_lines(fn1);
  692.   if (!ngroups)
  693.     return;
  694.   if (grouprec != NULL)
  695.     farfree(grouprec);
  696.   grouprec = NULL;
  697.   grouprec = (GROUPFILEREC *) farmalloc((ngroups + 1) * sizeof(GROUPFILEREC));
  698.   if (!grouprec) {
  699.     ngroups = 0;
  700.     output("\n ■ Insufficient memory for %d groups in %s.", ngroups, fn1);
  701.     return;
  702.   }
  703.   if ((groupfp = fsh_open(fn1, "rt")) == NULL) {
  704.     if (grouprec != NULL)
  705.       farfree(grouprec);
  706.     ngroups = 0;
  707.     return;
  708.   }
  709.   while (fgets(tmp, 120, groupfp)) {
  710.     if (*tmp) {
  711.       if (strncmpi(tmp, "newsrc", 6) == 0) {
  712.         strcpy(grouprec[i].groupname, "newsrc");
  713.         ++i;
  714.       } else {
  715.         ss = strtok(tmp, " ");
  716.         strcpy(grouprec[i].groupname, ss);
  717.         ss = strtok(NULL, " ");
  718.         if (ss) {
  719.           grouprec[i].lastread = atol(ss);
  720.           ss = strtok(NULL, " \n");
  721.           if (ss) {
  722.             strcpy(grouprec[i].subtype, strupr(ss));
  723.             ++i;
  724.           }
  725.         }
  726.       }
  727.     }
  728.   }
  729.   ngroups = i;
  730.   if (groupfp != NULL)
  731.     fclose(groupfp);
  732.   for (i = 0; i < ngroups; i++) {
  733.     if ((grouprec[i].subtype) && (atoi(grouprec[i].subtype) != 0)) {
  734.       sprintf(fn, "%sN%s.NET", net_data, grouprec[i].subtype);
  735.       if (exist(fn)) {
  736.         ok = 0;
  737.         if ((groupfp = fsh_open(fn, "rt")) != NULL) {
  738.           while ((fgets(tmp, 25, groupfp)) && !ok) {
  739.             sn = atoi(tmp);
  740.             if (sn == 32767)
  741.               ok = 1;
  742.           }
  743.           fclose(groupfp);
  744.         }
  745.         if (!ok)
  746.           log_it(1, "\n ■ @32767 not listed as a subscriber in N%s.NET!",
  747.                  grouprec[i].subtype);
  748.       }
  749.     }
  750.   }
  751. }
  752.  
  753. int sendauthinfo(Mail_Socket * NNTP_sock)
  754. {
  755.   char s[MAXPATH], buf[STR];
  756.  
  757.   *_temp_buffer = 0;
  758.  
  759.   output("\n ■ Server requested authentication information.");
  760.  
  761.   while (1) {
  762.     sprintf(buf, "authinfo user %s", NEWSNAME);
  763.     sock_printf(NNTP_sock->sock, buf);
  764.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  765.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  766.     sscanf(_temp_buffer, "%hu %s", &reply, s);
  767.     if (reply == 381) {
  768.       sprintf(buf, "authinfo pass %s", NEWSPASS);
  769.       sock_printf(NNTP_sock->sock, buf);
  770.       sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  771.       sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  772.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  773.       if (reply == 281) {
  774.         output("\n ■ Authentication accepted.  Continuing news retrieval session.");
  775.         return 1;
  776.       } else {
  777.         output("\n ■ Authentication failed.  Aborting news session.");
  778.         return 0;
  779.       }
  780.     } else {
  781.       output("\n ■ Unknown AUTHINFO response %s.", _temp_buffer);
  782.       return 0;
  783.     }
  784.   }
  785.   SOCK_READ_ERR(NNTP);
  786.   return 0;
  787. }
  788.  
  789. int cgroup(Mail_Socket * NNTP_sock, char *s)
  790. {
  791.   char junk[STRING];
  792.  
  793.   *_temp_buffer = 0;
  794.  
  795.   sock_printf(NNTP_sock->sock, "GROUP %s", s);
  796.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  797.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  798.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  799.   switch (reply) {
  800.     case 411:
  801.       return 0;
  802.     case 211:
  803.       sscanf(_temp_buffer, "%hu %lu %lu %lu %s",
  804.              &reply, &cur_numa, &cur_first, &cur_last, junk);
  805.       return 1;
  806.     case 480:
  807.       return (-1);
  808.     default:
  809.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  810.       log_it(0, "\n ■ Unknown GROUP response : %s", _temp_buffer);
  811.       break;
  812.   }
  813.   SOCK_READ_ERR(NNTP);
  814.   return 0;
  815. }
  816.  
  817. void treat(char *str)
  818. {
  819.   char *obuf, *nbuf;
  820.  
  821.   if (*str) {
  822.     for (obuf = str, nbuf = str; *obuf && obuf; ++obuf) {
  823.       if (((*obuf >= 32) && (*obuf <= 126)) || (*obuf == 10) || (*obuf == 9)) {
  824.         *nbuf++ = *obuf;
  825.       }
  826.     }
  827.     *nbuf = 0;
  828.   }
  829. }
  830.  
  831. char *name_packet(char *pktname)
  832. {
  833.   int i;
  834.  
  835.   for (i = 0; i < 1000; i++) {
  836.     sprintf(pktname, "%sP0-%5.5d.%3.3d", net_data, i, instance);
  837.     if (!exist(pktname))
  838.       break;
  839.   }
  840.   return pktname;
  841. }
  842.  
  843.  
  844. int savebody(Mail_Socket * NNTP_sock, char *fn, int cug, unsigned long cur_article, int *abort)
  845. {
  846.   int i, f, place, len, curpos, count, part;
  847.   char _big_temp_buffer[BIGSTR];
  848.   char ch, spin[15], buf[180];
  849.   unsigned long msgsize;
  850.  
  851.   place = count = 0;
  852.   *_temp_buffer = 0;
  853.   part = 1;
  854.  
  855.   strcpy(spin, "||//--\\\\");
  856.   len = strlen(spin);
  857.  
  858.   if (spooltodisk)
  859.     f = sh_open(fn, O_RDWR | O_APPEND | O_TEXT | O_CREAT, S_IREAD | S_IWRITE);
  860.   else
  861.     f = sh_open(fn, O_RDWR | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE);
  862.  
  863.   if (f < 0) {
  864.     output("\n ■ Unable to write to %s.", fn);
  865.     return 0;
  866.   }
  867.  
  868.   sock_printf(NNTP_sock->sock, "BODY %ld", cur_article);
  869.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  870.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  871.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  872.   switch (reply) {
  873.     case 423:
  874.       return 0;
  875.     case 222:
  876.       sh_write(f, "\n", sizeof(char));
  877.       if (spooltodisk) {
  878.         output("\n ■ Writing NEWS%d.UUE", cug);
  879.         for (i = 0; i < 79; i++)
  880.           sh_write(f, "-", sizeof(char));
  881.         sh_write(f, "\n", sizeof(char));
  882.         sprintf(buf, "Art  : %lu\n", cur_article);
  883.         sh_write(f, buf, strlen(buf));
  884.         if (*cur_date)
  885.           sprintf(buf, "Date : %s\n", cur_date);
  886.         else
  887.           sprintf(buf, "Date : Unknown\n");
  888.         sh_write(f, buf, strlen(buf));
  889.         if (*cur_replyto)
  890.           sprintf(buf, "From : %s\n", cur_replyto);
  891.         else
  892.           sprintf(buf, "From : Unknown\n");
  893.         sh_write(f, buf, strlen(buf));
  894.         if (*cur_subject)
  895.           sprintf(buf, "Subj : %s\n\n", cur_subject);
  896.         else
  897.           sprintf(buf, "Subj : Unknown\n");
  898.         sh_write(f, buf, strlen(buf));
  899.       } else
  900.         output("\n ■ Receiving message");
  901.       output(" - [Esc] Abort - [Space] Skip Group - [Tab] Catch Up [-]");
  902.       msgsize = 0L;
  903.       curpos = 0L;
  904.       while (1) {
  905.         while (kbhit()) {
  906.           ch = (getch());
  907.           switch (ch) {
  908.             case 9:
  909.               backline();
  910.               log_it(0, "\r ■ Catching up on group... wait until message is completed [-]");
  911.               *abort = 3;
  912.               break;
  913.             case 32:
  914.               backline();
  915.               log_it(0, "\r ■ Skipping group... wait until message is completed [-]");
  916.               *abort = 2;
  917.               break;
  918.             case 27:
  919.               backline();
  920.               log_it(0, "\r ■ Aborting session... wait until message is completed [-]");
  921.               *abort = 1;
  922.               break;
  923.             default:
  924.               break;
  925.           }
  926.         }
  927.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  928.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  929.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0)
  930.           break;
  931.         else {
  932.           treat(_big_temp_buffer);
  933.           strcat(_big_temp_buffer, "\n");
  934.           curpos = strlen(_big_temp_buffer);
  935.           sh_write(f, _big_temp_buffer, strlen(_big_temp_buffer));
  936.           msgsize += curpos;
  937.         }
  938.         if (count++ > 5) {
  939.           output("\b\b%c]", spin[place++]);
  940.           if (len)
  941.             place %= len;
  942.           count = 0;
  943.         }
  944.         if ((msgsize > 30000L) && (!spooltodisk)) {
  945.           strcpy(buf, "\nContinued in next message...\n");
  946.           sh_write(f, buf, strlen(buf));
  947.           sh_close(f);
  948.           sprintf(fn, "%sSPOOL\\INPUT%d.MSG", net_data, ++part);
  949.           unlink(fn);
  950.           if ((f = sh_open(fn, O_WRONLY | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE)) < 0) {
  951.             log_it(0, "\n ■ Unable to create %s", fn);
  952.             sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  953.             return 0;
  954.           }
  955.           backline();
  956.           output("\r ■ Breaking into %d%s part [ ]", (part), ordinal_text(part));
  957.           strcpy(buf, "\nContinued from previous message...\n");
  958.           sh_write(f, buf, strlen(buf));
  959.           msgsize = strlen(buf);
  960.         }
  961.       }
  962.       sh_close(f);
  963.       output("\b\bX]");
  964.       return 1;
  965.     default:
  966.       output("\n ■ Unknown BODY error : %s", _temp_buffer);
  967.       break;
  968.   }
  969.   sh_close(f);
  970.   SOCK_READ_ERR(NNTP);
  971.   return 0;
  972. }
  973.  
  974. int extract(char *to, char *key, char *from)
  975. {
  976.   if (!strnicmp(from, key, strlen(key))) {
  977.     from += strlen(key);
  978.     while (*from == ' ')
  979.       from++;
  980.     strcpy(to, from);
  981.     return 1;
  982.   } else
  983.     return 0;
  984. }
  985.  
  986.  
  987. int chead(Mail_Socket * NNTP_sock, unsigned long which)
  988. {
  989.   char _big_temp_buffer[BIGSTR];
  990.  
  991.   *cur_path = 0;
  992.   *cur_from = 0;
  993.   *cur_replyto = 0;
  994.   *cur_subject = 0;
  995.   *cur_newsgroups = 0;
  996.   *cur_message_ID = 0;
  997.   *cur_references = 0;
  998.   *cur_date = 0;
  999.   *_temp_buffer = 0;
  1000.  
  1001.   sock_printf(NNTP_sock->sock, "HEAD %lu", which);
  1002.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1003.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1004.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  1005.   switch (reply) {
  1006.     case 423:
  1007.       output("%-55.55s", "Expired article.");
  1008.       return 0;
  1009.     case 221:
  1010.       while (1) {
  1011.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1012.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1013.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0) {
  1014.           if (cur_replyto[0] == 0)
  1015.             strcpy(cur_replyto, cur_from);
  1016.           return 1;
  1017.         }
  1018.         if ((strlen(_big_temp_buffer)) > STRING)
  1019.           _big_temp_buffer[STRING] = '\0';
  1020.         if (extract(cur_path, "Path:", _big_temp_buffer) ||
  1021.              extract(cur_from, "From:", _big_temp_buffer) ||
  1022.              extract(cur_subject, "Subject:", _big_temp_buffer) ||
  1023.              extract(cur_replyto, "Reply-To:", _big_temp_buffer) ||
  1024.              extract(cur_newsgroups, "Newsgroups:", _big_temp_buffer) ||
  1025.              extract(cur_organization, "Organization:", _big_temp_buffer) ||
  1026.              extract(cur_message_ID, "Message-ID:", _big_temp_buffer) ||
  1027.              extract(cur_references, "References:", _big_temp_buffer) ||
  1028.              extract(cur_lines, "Lines:", _big_temp_buffer) ||
  1029.              extract(cur_date, "Date:", _big_temp_buffer))
  1030.          continue;
  1031.       }
  1032.     default:
  1033.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1034.       log_it(0, "\n ■ Unknown chead error : %s", _big_temp_buffer);
  1035.       break;
  1036.   }
  1037.   SOCK_READ_ERR(NNTP);
  1038.   return 0;
  1039. }
  1040.  
  1041. Mail_Socket *netsocket(char *host)
  1042. {
  1043.   Mail_Socket *NNTP_sock = NULL;
  1044.   longword h;
  1045.  
  1046.   sock_init();
  1047.  
  1048.   if (!(h = resolve(host))) {
  1049.     if (!(h = resolve(host))) {
  1050.       output("\n ■ Error : Cannot resolve host %s", host);
  1051.       return NULL;
  1052.     }
  1053.   }
  1054.   if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  1055.     output("\n ■ Error: Insufficient memory to create socket... aborting.");
  1056.     return NULL;
  1057.   }
  1058.   if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  1059.     output("\n ■ Error : Insufficient memory to create socket... aborting.");
  1060.     farfree(NNTP_sock);
  1061.     return NULL;
  1062.   }
  1063.   if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
  1064.     output("\n ■ Error : Unable to connect to to %s.", host);
  1065.     farfree(NNTP_sock);
  1066.     return NULL;
  1067.   }
  1068.   sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
  1069.   sock_wait_established(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1070.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1071.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1072.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1073.   switch (reply) {
  1074.     case 200:
  1075.       log_it(0, "\n ■ Connection to %s accepted...", host);
  1076.       return (NNTP_sock);
  1077.     case 502:
  1078.       output("\n ■ Connection to %s refused... try again later.", host);
  1079.       break;
  1080.     case 503:
  1081.       output("\n ■ NNTP service unavailable. Connection to %s refused.", host);
  1082.       break;
  1083.     default:
  1084.       output("\n ■ Unknown NNTP error. Connection to %s failed.", host);
  1085.       break;
  1086.   }
  1087.   SOCK_READ_ERR(NNTP);
  1088.   return 0;
  1089. }
  1090.  
  1091. int cnext(Mail_Socket * NNTP_sock)
  1092. {
  1093.   char *p, *q;
  1094.  
  1095.   *_temp_buffer = 0;
  1096.  
  1097.   sock_printf(NNTP_sock->sock, "NEXT");
  1098.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1099.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1100.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1101.   switch (reply) {
  1102.     case 421:
  1103.       return 0;
  1104.     case 223:
  1105.       p = _temp_buffer;
  1106.       q = cur_articleid;
  1107.       while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  1108.         p++;
  1109.       while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  1110.         *q++ = *p++;
  1111.       *q++ = '>';
  1112.       *q++ = '\0';
  1113.       return 1;
  1114.     default:
  1115.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1116.       log_it(0, "\n ■ Unknown NEXT error : %s", _temp_buffer);
  1117.       break;
  1118.   }
  1119.   SOCK_READ_ERR(NNTP);
  1120.   return 0;
  1121. }
  1122.  
  1123. int cslave(Mail_Socket * NNTP_sock)
  1124. {
  1125.   char junk[STRING];
  1126.  
  1127.   *_temp_buffer = 0;
  1128.  
  1129.   sock_printf(NNTP_sock->sock, "SLAVE");
  1130.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1131.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1132.   sscanf(_temp_buffer, "%hu %s", &reply, junk);
  1133.   SOCK_READ_ERR(NNTP);
  1134.   return 0;
  1135. }
  1136.  
  1137. char *stripspace(char *str)
  1138. {
  1139.   char *obuf, *nbuf;
  1140.  
  1141.   if (str) {
  1142.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  1143.       if (!isspace(*obuf))
  1144.         *nbuf++ = *obuf;
  1145.     }
  1146.     *nbuf = NULL;
  1147.   }
  1148.   return (str);
  1149. }
  1150.  
  1151.  
  1152. int saveactive(Mail_Socket * NNTP_sock, int forced)
  1153. {
  1154.   char s[181], fn[MAXPATH], group[256], act;
  1155.   unsigned long to, from;
  1156.   unsigned fnday, fnmonth, fnyear, fnhour, fnminute, fnsecond;
  1157.   int f, done, update;
  1158.   unsigned long count;
  1159.   FILE *fp;
  1160.   struct ftime ft;
  1161.   struct date d;
  1162.  
  1163.   *_temp_buffer = 0;
  1164.  
  1165.   update = 0;
  1166.  
  1167.   sprintf(fn, "%sNEWSRC", net_data);
  1168.  
  1169.   f = sh_open1(fn, O_RDONLY);
  1170.   if (f < 0)
  1171.     forced = 1;
  1172.   else {
  1173.     getftime(f, &ft);
  1174.     sh_close(f);
  1175.   }
  1176.  
  1177.   if (!forced) {
  1178.     fnsecond = 2 * ft.ft_tsec;
  1179.     fnminute = ft.ft_min;
  1180.     fnhour = ft.ft_hour;
  1181.     fnday = ft.ft_day;
  1182.     fnmonth = ft.ft_month;
  1183.     fnyear = ft.ft_year + 80;
  1184.     output("\n ■ NEWSRC last updated %02u/%02u/%02u %.2u:%.2u:%.2u... ",
  1185.         fnmonth, fnday, fnyear, fnhour, fnminute, fnsecond);
  1186.     getdate(&d);
  1187.     if ((d.da_day == ft.ft_day) && (d.da_mon == ft.ft_month) &&
  1188.         (d.da_year == (ft.ft_year + 1980))) {
  1189.       output("no update required.");
  1190.       return 0;
  1191.     } else
  1192.       output("updating.");
  1193.     if ((d.da_mon == ft.ft_month) && (d.da_day != ft.ft_day))
  1194.       update = 1;
  1195.   }
  1196.  
  1197.   if (update)
  1198.     fp = fsh_open(fn, "a");
  1199.   else
  1200.     fp = fsh_open(fn, "w");
  1201.  
  1202.   if (fp == NULL) {
  1203.     perror(fn);
  1204.     return 0;
  1205.   }
  1206.   done = 0;
  1207.  
  1208.   while (!done) {
  1209.     if (update) {
  1210.       sprintf(s, "NEWGROUPS %02.02u%02.02u%02.02u %02.02u%02.02u%02.02u",
  1211.             fnyear, fnmonth, fnday, fnhour, fnminute, fnsecond);
  1212.       sock_printf(NNTP_sock->sock, s);
  1213.       output("\n ■ Refreshing NEWSRC newsgroup listing :           ");
  1214.     } else {
  1215.       sock_printf(NNTP_sock->sock, "LIST");
  1216.       output("\n ■ Retrieving current newsgroup listing :           ");
  1217.     }
  1218.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1219.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1220.     sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1221.     if ((reply == 215) || (reply == 231)) {
  1222.       count = 0L;
  1223.       while (1) {
  1224.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1225.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1226.         if (*_temp_buffer == '.' && _temp_buffer[1] == 0)
  1227.           break;
  1228.         if (count++ % 25 == 0L)
  1229.           output("\b\b\b\b\b\b\b%-7lu", count);
  1230.         sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
  1231.         sprintf(s, "%s %lu\n", group, to);
  1232.         fsh_write(s, sizeof(char), strlen(s), fp);
  1233.       }
  1234.       if (update) {
  1235.         if (count == 0) {
  1236.           output("\b\b\b\b\b\b\bNo new groups.");
  1237.         } else
  1238.           output("\b\b\b\b\b\b\b%lu group%s added to NEWSRC.", count,
  1239.                 count == 1 ? "" : "s");
  1240.       } else
  1241.         output("\b\b\b\b\b\b\b%lu total groups.", count);
  1242.  
  1243.       if (fp != NULL)
  1244.         fclose(fp);
  1245.       if (update) {
  1246.         f = sh_open1(fn, O_RDWR);
  1247.         ft.ft_day = d.da_day;
  1248.         setftime(f, &ft);
  1249.         sh_close(f);
  1250.       }
  1251.       return 1;
  1252.     } else {
  1253.       if (reply == 480) {
  1254.         if (fp != NULL)
  1255.           fclose(fp);
  1256.         return 0;
  1257.       } else {
  1258.         log_it(0, "\n ■ Unknown saveactive error : %s", _temp_buffer);
  1259.         if (fp != NULL)
  1260.           fclose(fp);
  1261.         done = 1;
  1262.       }
  1263.     }
  1264.   }
  1265.   if (fp != NULL)
  1266.     fclose(fp);
  1267.   SOCK_READ_ERR(NNTP);
  1268.   return 0;
  1269. }
  1270.  
  1271. int checkx(int cug)
  1272. {
  1273.   char *ptr, *ptr2, *ptr3, fn[MAXPATH], buf[256], tmp[81];
  1274.   int i, spam, max;
  1275.   FILE *fp;
  1276.  
  1277.   if ((stristr(grouprec[cug].groupname, "binaries")) && (binxpost))
  1278.     return 1;
  1279.  
  1280.   sprintf(buf, "%s!%s", POPNAME, POPDOMAIN);
  1281.   if (stristr(cur_path, buf) != 0) {
  1282.     sprintf(buf, "Local post - %s - skipped.", cur_subject);
  1283.     output("%-55.55s", buf);
  1284.     return 0;
  1285.   }
  1286.  
  1287.   spam = 0;
  1288.   sprintf(fn, "%sNOSPAM.TXT", net_data);
  1289.   if ((fp = fsh_open(fn, "r")) != NULL) {
  1290.     while ((!feof(fp)) && (!spam)) {
  1291.       fgets(buf, 80, fp);
  1292.       trimstr1(buf);
  1293.       if (strlen(buf) > 2) {
  1294.         if ((stristr(cur_subject, buf)) || (stristr(cur_from, buf))) {
  1295.           sprintf(tmp, "SPAM - %s - skipped.", buf);
  1296.           output("%-55.55s", tmp);
  1297.           spam = 1;
  1298.         }
  1299.       }
  1300.     }
  1301.     fclose(fp);
  1302.     if (spam)
  1303.       return 0;
  1304.   }
  1305.  
  1306.   ptr = cur_newsgroups;
  1307.  
  1308.   while (*ptr == ' ')
  1309.     ptr++;
  1310.   max = 0;
  1311.   while (1) {
  1312.     if (*ptr == 0)
  1313.       break;
  1314.     if (*ptr == ',') {
  1315.       ptr++;
  1316.       ++max;
  1317.     }
  1318.     while (*ptr == ' ')
  1319.       ptr++;
  1320.     ptr2 = ptr;
  1321.     ptr3 = buf;
  1322.     while (*ptr2 != 0 && *ptr2 != ',')
  1323.       *ptr3++ = *ptr2++;
  1324.     *ptr3 = 0;
  1325.     while (*(--ptr3) == ' ')
  1326.       *ptr3 = 0;
  1327.     ptr = ptr2;
  1328.     for (i = 0; i < cug; i++) {
  1329.       if (strcmpi(buf, grouprec[i].groupname) == 0) {
  1330.         output("Already posted in %s.", grouprec[i].groupname);
  1331.         return 0;
  1332.       }
  1333.     }
  1334.     if ((crossposts > 0) && (max > crossposts)) {
  1335.       sprintf(buf, "Crossposted to more than %d newsgroups.", crossposts);
  1336.       output("%-55.55s", buf);
  1337.       return 0;
  1338.     }
  1339.   }
  1340.   return 1;
  1341. }
  1342.  
  1343. int postnews(Mail_Socket * NNTP_sock, int cug)
  1344. {
  1345.   char s[160], s1[12], s2[5], fn[MAXPATH], *ss;
  1346.   int f1, nlines, len, tlen;
  1347.   FILE *fp;
  1348.   struct ffblk ff;
  1349.  
  1350.   *_temp_buffer = 0;
  1351.  
  1352.   sprintf(fn, "%sOUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
  1353.   f1 = findfirst(fn, &ff, 0);
  1354.   if (f1 != 0) {
  1355.     log_it(0, "\n ■ No outbound news articles to post...");
  1356.     return 1;
  1357.   }
  1358.   while (f1 == 0) {
  1359.     sock_printf(NNTP_sock->sock, "POST");
  1360.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1361.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1362.     if (*_temp_buffer != '3') {
  1363.       if (atoi(_temp_buffer) == 440)
  1364.         log_it(1, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
  1365.       else
  1366.         log_it(0, "\n ■ Remote error: %s", _temp_buffer);
  1367.       return 0;
  1368.     }
  1369.     output("\n ■ Posting article to %s\n", grouprec[cug].groupname);
  1370.     sprintf(s, "%sOUTBOUND\\%s", net_data, ff.ff_name);
  1371.     if ((fp = fsh_open(s, "r")) == NULL) {
  1372.       log_it(1, "\n ■ Error reading %s.", s);
  1373.       return 1;
  1374.     }
  1375.     nlines = 0;
  1376.     while (!feof(fp)) {
  1377.       fgets(_temp_buffer, sizeof(_temp_buffer), fp);
  1378.       if (*_temp_buffer == '.') {
  1379.         movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
  1380.         *_temp_buffer = '.';
  1381.       }
  1382.       tlen = 0;
  1383.       len = strlen(_temp_buffer);
  1384.       while (tlen < len)
  1385.         tlen += sock_fastwrite(NNTP_sock->sock, &_temp_buffer[tlen], len - tlen);
  1386.       backline();
  1387.       output("\r ■ Lines sent : %d", ++nlines);
  1388.       tcp_tick(NNTP_sock->sock);
  1389.     }
  1390.     sock_puts(NNTP_sock->sock, ".");
  1391.     output("\n ■ Awaiting acknowledgement - may take several minutes...");
  1392.     fclose(fp);
  1393.     log_it(0, "\n ■ Server response : ");
  1394.     sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
  1395.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1396.     log_it(0, "%s", _temp_buffer);
  1397.     if (*_temp_buffer != '2') {
  1398.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  1399.       if (reply == 441) {
  1400.         ss = strtok(_temp_buffer, " ");
  1401.         ss = strtok(NULL, " ");
  1402.         if (atoi(ss) == 435)
  1403.           unlink(s);
  1404.         fnsplit(s, NULL, NULL, s1, s2);
  1405.         log_it(0, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
  1406.         if (unlink(s) == 0)
  1407.           log_it(0, "\n ■ Deleted message - %s", s);
  1408.       } else
  1409.         log_it(0, "\n ■ Remote error: %s", _temp_buffer);
  1410.     } else {
  1411.       if (unlink(s) == 0)
  1412.         log_it(0, "\n ■ Deleted sent message - %s", s);
  1413.     }
  1414.     f1 = findnext(&ff);
  1415.   }
  1416.   SOCK_READ_ERR(NNTP);
  1417.   return 0;
  1418. }
  1419.  
  1420. void get_subtype(int sub, char *where)
  1421. {
  1422.   int ok, which;
  1423.   char fn[181], s[81], net_name[21], *ss;
  1424.   FILE *fp;
  1425.  
  1426.   where[0] = 0;
  1427.   which = sub;
  1428.   sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
  1429.   if ((fp = fsh_open(fn, "r")) == NULL)
  1430.     return;
  1431.   ok = 0;
  1432.   while (fgets(s, 80, fp)) {
  1433.     if (*s == '!') {
  1434.       if (which == atoi(&s[1]))
  1435.         ok = 1;
  1436.     }
  1437.     if (ok && (*s == '$')) {
  1438.       ss = strtok(s, " ");
  1439.       strcpy(net_name, &ss[1]);
  1440.       ss = strtok(NULL, " ");
  1441.       if (fp != NULL)
  1442.         fclose(fp);
  1443.       if ((stricmp(net_name, "FILENET") == 0)) {
  1444.         trimstr1(ss);
  1445.         strcpy(where, ss);
  1446.         return;
  1447.       } else
  1448.         return;
  1449.     }
  1450.   }
  1451.   if (fp != NULL)
  1452.     fclose(fp);
  1453. }
  1454.  
  1455. unsigned long max_on_sub(int cug)
  1456. {
  1457.   int i, num_subs;
  1458.   char fn[MAXPATH], subtype[12];
  1459.   unsigned long max;
  1460.   subboardrec sub;
  1461.   FILE *fp;
  1462.  
  1463.   max = 0;
  1464.   if ((strlen(grouprec[cug].subtype) == 1) && (*grouprec[cug].subtype == '0'))
  1465.     return max;
  1466.   output("\n ■ Maximum posts -     ");
  1467.   sprintf(fn, "%sSUBS.DAT", syscfg.datadir);
  1468.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  1469.     output("\n ■ Unable to read %s.", fn);
  1470.     return max;
  1471.   } else {
  1472.     fseek(fp, 0L, SEEK_END);
  1473.     num_subs = (int) (ftell(fp) / sizeof(subboardrec));
  1474.     for (i = 0; i < num_subs; i++) {
  1475.       output("\b\b\b\b%-4d", i);
  1476.       fseek(fp, (long) i * sizeof(subboardrec), SEEK_SET);
  1477.       fread(&sub, sizeof(subboardrec), 1, fp);
  1478.       get_subtype(i, subtype);
  1479.       if (stricmp(subtype, grouprec[cug].subtype) == 0) {
  1480.         max = (unsigned long)sub.maxmsgs;
  1481.         output("\b\b\b\b%s (%s) - %lu posts.", sub.name, subtype, max);
  1482.         break;
  1483.       }
  1484.     }
  1485.   }
  1486.   fclose(fp);
  1487.   return max;
  1488. }
  1489.  
  1490.  
  1491. int getnews(Mail_Socket * NNTP_sock)
  1492. {
  1493.   int f1, nup, nug, cug, abort, ok, done, firstrun, parts, skipped, bogus;
  1494.   char *p, fn[MAXPATH], mailname[121], reline[121], pktname[MAXPATH];
  1495.   char ch, orig_subj[STRING], buf[STR];
  1496.   unsigned int text_len;
  1497.   unsigned short temptype;
  1498.   unsigned long new_articles, max_articles;
  1499.   net_header_rec nh;
  1500.   struct ffblk ff;
  1501.   FILE *fp;
  1502.  
  1503.   abort = cug = bogus = 0;
  1504.  
  1505.   if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
  1506.     if (!saveactive(NNTP_sock, 1)) {
  1507.       if (sendauthinfo(NNTP_sock))
  1508.         saveactive(NNTP_sock, 1);
  1509.     }
  1510.   } else
  1511.     saveactive(NNTP_sock, 0);
  1512.  
  1513.   nug = nup = 1;
  1514.   skipped = 0;
  1515.   cslave(NNTP_sock);
  1516.   while ((cug < ngroups) && (!abort)) {
  1517.     if (*grouprec[cug].subtype == '0')
  1518.       spooltodisk = 1;
  1519.     else
  1520.       spooltodisk = 0;
  1521.     if (nup) {
  1522.       nup = 0;
  1523.       write_groups(1);
  1524.       if (*grouprec[cug].subtype != '0')
  1525.         name_packet(pktname);
  1526.     }
  1527.     if (nug) {
  1528.       nug = 0;
  1529.       while (1) {
  1530.         if (!cgroup(NNTP_sock, grouprec[cug].groupname)) {
  1531.           if (cgroup(NNTP_sock, grouprec[cug].groupname) == -1) {
  1532.             if (sendauthinfo(NNTP_sock))
  1533.               continue;
  1534.             else
  1535.               return 1;
  1536.           }
  1537.           if (++bogus > 10) {
  1538.             log_it(0, "\n ■ Too many invalid newsgroups... NEWS.RC error?");
  1539.             return 1;
  1540.           }
  1541.           log_it(1, "\n ■ Error accessing newsgroup \"%s\".", grouprec[cug].groupname);
  1542.           nug = 1;
  1543.           break;
  1544.         } else
  1545.           break;
  1546.       }
  1547.       if (!nug) {
  1548.         log_it(0, "\n ■ Requesting %s... ", grouprec[cug].groupname);
  1549.         if (grouprec[cug].lastread < cur_first)
  1550.           grouprec[cug].lastread = cur_first;
  1551.         if (grouprec[cug].lastread >= cur_last) {
  1552.           grouprec[cug].lastread = cur_last;
  1553.           log_it(0, "no new articles.");
  1554.           nug = 1;
  1555.         } else {
  1556.           new_articles = cur_last - grouprec[cug].lastread;
  1557.           if (new_articles) {
  1558.             log_it(0, "%lu new article%s.", new_articles,
  1559.                    new_articles == 1 ? "" : "s");
  1560.           } else {
  1561.             log_it(0, "no new articles.");
  1562.             nug = 1;
  1563.           }
  1564.           if (new_articles > 250L) {
  1565.             max_articles = max_on_sub(cug);
  1566.             if (max_articles && (new_articles > max_articles)) {
  1567.               log_it(0, "\n ■ Requesting most recent %lu articles", max_articles);
  1568.               grouprec[cug].lastread = cur_last - max_articles;
  1569.             }
  1570.           } else
  1571.             ++grouprec[cug].lastread;
  1572.         }
  1573.         postnews(NNTP_sock, cug);
  1574.         if (!nug) {
  1575.           if (cur_numa == 0) {
  1576.             log_it(0, "\n ■ No articles in %s...", grouprec[cug].groupname);
  1577.             nug = 1;
  1578.             continue;
  1579.           }
  1580.         }
  1581.       }
  1582.     } else {
  1583.       if ((!cnext(NNTP_sock)) || (grouprec[cug].lastread > cur_last)) {
  1584.         output("\n ■ End of articles in %s", grouprec[cug].groupname);
  1585.         nug = 1;
  1586.       }
  1587.     }
  1588.     if (!nug) {
  1589.       if (skipped)
  1590.         output("\r");
  1591.       else
  1592.         output("\n");
  1593.       output(" ■ [%lu/%lu] : ", grouprec[cug].lastread, cur_last);
  1594.       if ((chead(NNTP_sock, grouprec[cug].lastread)) && (checkx(cug))) {
  1595.         skipped = 0;
  1596.         if (*cur_subject)
  1597.           treat(cur_subject);
  1598.         else
  1599.           strcpy(cur_subject, "No subject");
  1600.         strcpy(orig_subj, cur_subject);
  1601.         if (*cur_from) {
  1602.           treat(cur_from);
  1603.           if (strlen(cur_from) > 45)
  1604.             cur_from[45] = 0;
  1605.         } else
  1606.           strcpy(cur_from, "Unknown");
  1607.         if (*cur_replyto) {
  1608.           if (strlen(cur_replyto) > 45)
  1609.             cur_replyto[45] = 0;
  1610.         } else
  1611.           strcpy(cur_replyto, "Unknown");
  1612.         output("%-55.55s", orig_subj);
  1613.         if (spooltodisk)
  1614.           sprintf(fn, "%sSPOOL\\NEWS%d.UUE", net_data, cug);
  1615.         else
  1616.           sprintf(fn, "%sSPOOL\\INPUT1.MSG", net_data);
  1617.         abort = 0;
  1618.         fcloseall();
  1619.         ok = (savebody(NNTP_sock, fn, cug, grouprec[cug].lastread, &abort));
  1620.         if (ok && (*grouprec[cug].subtype != '0')) {
  1621.           if ((fp = fsh_open(pktname, "ab+")) == NULL) {
  1622.             log_it(0, "\n ■ Unable to create %s!", pktname);
  1623.             return 1;
  1624.           }
  1625.           sprintf(fn, "%sSPOOL\\INPUT*.MSG", net_data);
  1626.           done = 1;
  1627.           parts = 0;
  1628.           if (findfirst(fn, &ff, 0) == 0) {
  1629.             parts = 1;
  1630.             done = 0;
  1631.           }
  1632.           firstrun = 1;
  1633.           while (!done) {
  1634.             sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1635.             f1 = sh_open1(fn, O_RDONLY | O_BINARY);
  1636.             text_len = (unsigned int) filelength(f1);
  1637.             if (text_len > 32000L) {
  1638.               output("\n ■ Truncating %lu bytes from input file",
  1639.                      text_len - 32000L);
  1640.               text_len = 32000L;
  1641.             }
  1642.             if ((p = (char *) malloc(32767L)) == NULL) {
  1643.               log_it(0, "\n ■ Insufficient memory to read entire message");
  1644.               return 1;
  1645.             }
  1646.             sh_read(f1, (void *) p, text_len);
  1647.             sh_close(f1);
  1648.             temptype = atoi(grouprec[cug].subtype);
  1649.             nh.tosys = sy;
  1650.             nh.touser = 0;
  1651.             nh.fromsys = 32767;
  1652.             nh.fromuser = 0;
  1653.             if (!temptype) {
  1654.               nh.main_type = main_type_new_post;
  1655.               nh.minor_type = 0;
  1656.             } else {
  1657.               nh.main_type = main_type_pre_post;
  1658.               nh.minor_type = temptype;
  1659.             }
  1660.             nh.list_len = 0;
  1661.             ++cur_daten;
  1662.             nh.daten = cur_daten;
  1663.             nh.method = 0;
  1664.             if (parts > 1) {
  1665.               sprintf(buf, "%d%s ", parts, ordinal_text(parts));
  1666.               strncat(buf, orig_subj, 72);
  1667.               strcpy(cur_subject, buf);
  1668.               cur_subject[72] = '\0';
  1669.             }
  1670.             nh.length = text_len + strlen(cur_subject) + 1;
  1671.             strncpy(mailname, cur_replyto, 46);
  1672.             strcat(mailname, "\r\n");
  1673.             nh.length += strlen(mailname);
  1674.             if (nh.main_type == main_type_new_post)
  1675.               nh.length += strlen(grouprec[cug].subtype) + 1;
  1676.             if (firstrun) {
  1677.               firstrun = 0;
  1678.               if (strncmpi(cur_articleid, "re: ", 4) == 0) {
  1679.                 strncpy(reline, cur_articleid, 60);
  1680.                 sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
  1681.               }
  1682.             }
  1683.             if (!*cur_date) {
  1684.               strncpy(cur_date, ctime(&(time_t) nh.daten), 24);
  1685.               cur_date[24] = '\0';
  1686.             }
  1687.             strcat(cur_date, "\r\n");
  1688.             nh.length += strlen(cur_date);
  1689.             nh.length += strlen(cur_articleid);
  1690.             fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  1691.             if (nh.main_type == main_type_new_post)
  1692.               fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
  1693.             fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
  1694.             fsh_write(mailname, sizeof(char), strlen(mailname), fp);
  1695.             fsh_write(cur_date, sizeof(char), strlen(cur_date), fp);
  1696.             fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
  1697.             fsh_write(p, sizeof(char), text_len, fp);
  1698.             if (p)
  1699.               free(p);
  1700.             unlink(fn);
  1701.             if (findnext(&ff) == 0) {
  1702.               done = 0;
  1703.               ++parts;
  1704.             } else
  1705.               done = 1;
  1706.           }
  1707.           if (filelength(fileno(fp)) > 250000L)
  1708.             nup = 1;
  1709.           if (fp != NULL)
  1710.             fclose(fp);
  1711.         }
  1712.       } else
  1713.         skipped = 1;
  1714.       ++grouprec[cug].lastread;
  1715.       while (kbhit()) {
  1716.         ch = (getch());
  1717.         switch (ch) {
  1718.           case 9:
  1719.             log_it(0, "\n ■ Catching up on unread articles...");
  1720.             abort = 3;
  1721.             break;
  1722.           case 32:
  1723.             output("\n ■ Skipping group %s... ", grouprec[cug].groupname);
  1724.             abort = 2;
  1725.             break;
  1726.           case 27:
  1727.             log_it(0, "\n ■ Aborting news retrieval session...");
  1728.             abort = 1;
  1729.             break;
  1730.           default:
  1731.             break;
  1732.         }
  1733.       }
  1734.       switch (abort) {
  1735.         case 3:
  1736.           grouprec[cug].lastread = cur_last;
  1737.         case 2:
  1738.           nug = 1;
  1739.           abort = 0;
  1740.           break;
  1741.         default:
  1742.           break;
  1743.       }
  1744.     }
  1745.     if (nug) {
  1746.       ++cug;
  1747.       if (*grouprec[cug].subtype == '0')
  1748.         nup = 1;
  1749.     }
  1750.   }
  1751.   nntp_shutdown(NNTP_sock);
  1752.   if (abort)
  1753.     log_it(0, "\n ■ Session aborted from keyboard");
  1754.   write_groups(1);
  1755.   return 0;
  1756. }
  1757.  
  1758. int copyfile(char *input, char *output)
  1759. {
  1760.   int f1, f2, i;
  1761.   char *b;
  1762.   struct ftime ft;
  1763.  
  1764.   if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
  1765.     if ((b = (char *) malloc(16400)) == NULL)
  1766.       return 0;
  1767.     f1 = sh_open1(input, O_RDONLY | O_BINARY);
  1768.     if (!f1) {
  1769.       free(b);
  1770.       b = NULL;
  1771.       return 0;
  1772.     }
  1773.     getftime(f1, &ft);
  1774.  
  1775.     f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1776.     if (!f2) {
  1777.       free(b);
  1778.       b = NULL;
  1779.       f1 = sh_close(f1);
  1780.       return 0;
  1781.     }
  1782.     i = read(f1, (void *) b, 16384);
  1783.     while (i > 0) {
  1784.       sh_write(f2, (void *) b, i);
  1785.       i = read(f1, (void *) b, 16384);
  1786.     }
  1787.     f1 = sh_close(f1);
  1788.     setftime(f2, &ft);
  1789.     f2 = sh_close(f2);
  1790.     free(b);
  1791.     b = NULL;
  1792.   }
  1793.   return 1;
  1794. }
  1795.  
  1796.  
  1797.  
  1798. main(int argc, char *argv[])
  1799. {
  1800.   int ok, f, bAuth;
  1801.   char s[160], fn[MAXPATH], tempfn[MAXPATH], serverhost[50], *ss;
  1802.   FILE *fp;
  1803.   struct tm *time_now;
  1804.   struct date dt;
  1805.   struct time tm;
  1806.   time_t some;
  1807.   Mail_Socket *nntp_sock = NULL;
  1808.  
  1809.   crossposts = 10;
  1810.   binxpost = 0;
  1811.   POPNAME[0] = 0;
  1812.   POPDOMAIN[0] = 0;
  1813.  
  1814.   cursor('S');
  1815.   cursor('H');
  1816.   output("\n ■ %s", version);
  1817.   if (argc != 4) {
  1818.     output("\n ■ Invalid arguments for %s\n", argv[0]);
  1819.     cursor('R');
  1820.     return EXIT_FAILURE;
  1821.   }
  1822.   strcpy(net_data, argv[1]);
  1823.   strcat(net_data, "\\");
  1824.   strcpy(serverhost, argv[2]);
  1825.   sy = atoi(argv[3]);
  1826.  
  1827.   get_dir(maindir, 0);
  1828.  
  1829.   detect_multitask();
  1830.  
  1831.   gettime(&tm);
  1832.   getdate(&dt);
  1833.   cur_daten = dostounix(&dt, &tm);
  1834.   time(&some);
  1835.   time_now = localtime(&some);
  1836.   strftime(s, 80, "\n\nNEWS session beginning on %A, %B %d, %Y at %H:%M %p",
  1837.            time_now);
  1838.   sprintf(fn, "%sNEWS.LOG", net_data);
  1839.   if ((fp = fsh_open(fn, "at")) != NULL) {
  1840.     fprintf(fp, s);
  1841.     fclose(fp);
  1842.   }
  1843.   ss = getenv("WWIV_INSTANCE");
  1844.   if (ss) {
  1845.     instance = atoi(ss);
  1846.     if ((instance <= 0) || (instance >= 1000)) {
  1847.       log_it(0, "\n ■ WWIV_INSTANCE set to %d.  Can only be 1..999!", instance);
  1848.       instance = 1;
  1849.     }
  1850.   } else
  1851.     instance = 1;
  1852.  
  1853.   sprintf(fn, "%s\\CONFIG.DAT", maindir);
  1854.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  1855.   if (f < 0) {
  1856.     output("\n ■ %s NOT FOUND.", fn);
  1857.     cursor('R');
  1858.     return EXIT_FAILURE;
  1859.   }
  1860.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1861.   sh_close(f);
  1862.  
  1863.   read_groups();
  1864.   if (ngroups == 0) {
  1865.     output("\n ■ Unable to access newsgroup file NEWS.RC!");
  1866.     cursor('R');
  1867.     return EXIT_FAILURE;
  1868.   } else {
  1869.     sprintf(fn, "%sNEWS.RC", net_data);
  1870.     sprintf(tempfn, "%sNEWS.BAK", net_data);
  1871.     if (exist(tempfn))
  1872.       unlink(tempfn);
  1873.     copyfile(fn, tempfn);
  1874.   }
  1875.  
  1876.   if (stricmp(grouprec[0].groupname, "newsrc") == 0)
  1877.     log_it(0, "\n ■ Retrieving current newsgroup listing from %s.",
  1878.            serverhost);
  1879.   else
  1880.     log_it(0, "\n ■ %u newsgroup%s defined in NEWS.RC.", ngroups,
  1881.            ngroups == 1 ? "" : "s");
  1882.  
  1883.   sprintf(fn, "%s\\NET.INI", maindir);
  1884.   if ((fp = fsh_open(fn, "rt")) == NULL)
  1885.     output("\n ■ Unable to read %s", fn);
  1886.   else {
  1887.     while (fgets(s, 100, fp)) {
  1888.       stripspace(s);
  1889.       if ((*s == ';') || (*s == '\n') || (*s == '['))
  1890.         continue;
  1891.       if (strlen(s) == 0)
  1892.         continue;
  1893.       if ((strnicmp(s, "POPNAME", 7) == 0) && (POPNAME[0] == 0)) {
  1894.         ss = strtok(s, "=");
  1895.         if (ss) {
  1896.           ss = strtok(NULL, "\n");
  1897.           trimstr1(ss);
  1898.           strcpy(POPNAME, ss);
  1899.         }
  1900.       }
  1901.       if (strnicmp(s, "FWDNAME", 7) == 0) {
  1902.         ss = strtok(s, "=");
  1903.         if (ss) {
  1904.           ss = strtok(NULL, "\n");
  1905.           trimstr1(ss);
  1906.           strcpy(POPNAME, ss);
  1907.         }
  1908.       }
  1909.       if ((strnicmp(s, "DOMAIN", 6) == 0) && (POPDOMAIN[0] == 0)) {
  1910.         ss = strtok(s, "=");
  1911.         if (ss) {
  1912.           ss = strtok(NULL, "\n");
  1913.           trimstr1(ss);
  1914.           strcpy(POPDOMAIN, ss);
  1915.         }
  1916.       }
  1917.       if (strnicmp(s, "FWDDOM", 6) == 0) {
  1918.         ss = strtok(s, "=");
  1919.         if (ss) {
  1920.           ss = strtok(NULL, "\n");
  1921.           trimstr1(ss);
  1922.           strcpy(POPDOMAIN, ss);
  1923.         }
  1924.       }
  1925.       if (strnicmp(s, "XPOSTS", 6) == 0) {
  1926.         ss = strtok(s, "=");
  1927.         if (ss) {
  1928.           ss = strtok(NULL, "\n");
  1929.           trimstr1(ss);
  1930.           crossposts = atoi(ss);
  1931.           if (crossposts > 99)
  1932.             crossposts = 10;
  1933.         }
  1934.       }
  1935.       if (strnicmp(s, "BINXPOST", 8) == 0) {
  1936.         ss = strtok(s, "=");
  1937.         if (ss) {
  1938.           ss = strtok(NULL, "\n");
  1939.           trimstr1(ss);
  1940.           if ((ss[0] == 'y') || (ss[0] == 'Y'))
  1941.             binxpost = 1;
  1942.         }
  1943.       }
  1944.       if (strnicmp(s, "NEWSNAME", 8) == 0) {
  1945.         ss = strtok(s, "=");
  1946.         if (ss) {
  1947.           ss = strtok(NULL, "\n");
  1948.           trimstr1(ss);
  1949.           strcpy(NEWSNAME, ss);
  1950.         }
  1951.       }
  1952.       if (strnicmp(s, "NEWSPASS", 8) == 0) {
  1953.         ss = strtok(s, "=");
  1954.         if (ss) {
  1955.           ss = strtok(NULL, "\n");
  1956.           trimstr1(ss);
  1957.           strcpy(NEWSPASS, ss);
  1958.         }
  1959.       }
  1960.       ss = NULL;
  1961.     }
  1962.     if (fp != NULL)
  1963.       fclose(fp);
  1964.   }
  1965.   ok = 1;
  1966.   if ((nntp_sock = netsocket(serverhost)) != NULL) {
  1967.     bAuth = 1;
  1968.     if (NEWSNAME[0]) {
  1969.       if (!sendauthinfo(nntp_sock))
  1970.         bAuth = 0;
  1971.     }
  1972.     if (bAuth)
  1973.       ok = getnews(nntp_sock);
  1974.     if (ok)
  1975.       log_it(1, "\n ■ NEWS exited with an error.");
  1976.     else
  1977.       log_it(0, "\n ■ Normal NEWS completion.");
  1978.   }
  1979.   cd_to(maindir);
  1980.   log_it(0, "\n ■ NEWS completed processing %d newsgroups", ngroups);
  1981.   if (grouprec)
  1982.     farfree(grouprec);
  1983.   grouprec = NULL;
  1984.   check_packets();
  1985.   trim_log();
  1986.   fcloseall();
  1987.   cursor('R');
  1988.   return ok;
  1989. }
  1990.