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