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