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