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