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