home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B56.ZIP / NEWS.CPP < prev    next >
C/C++ Source or Header  |  1997-10-25  |  54KB  |  2,015 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.           tcp_tick(NULL);
  890.           while (kbhit()) {
  891.             ch = (getch());
  892.             switch (ch) {
  893.               case 32:
  894.                 backline();
  895.                 output("\r ■ Skipping group... please wait until this message is completed [-]");
  896.                 *abort = 2;
  897.                 break;
  898.               case 27:
  899.                 backline();
  900.                 output("\r ■ Abort detected... please wait until this message is completed [-]");
  901.                 *abort = 1;
  902.                 break;
  903.               default:
  904.                 break;
  905.             }
  906.           }
  907.           sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  908.           sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  909.           if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0)
  910.             break;
  911.           else {
  912.             treat(_big_temp_buffer);
  913.             strcat(_big_temp_buffer, "\n");
  914.             curpos = strlen(_big_temp_buffer);
  915.             fputs(_big_temp_buffer, tpktfp);
  916.             msgsize += curpos;
  917.           }
  918.           if (count++ > 5) {
  919.             output("\b\b%c]", spin[place++]);
  920.             if (len)
  921.               place %= len;
  922.             count = 0;
  923.           }
  924.           if ((msgsize > 30000L) && (!spooltodisk)) {
  925.             strcpy(buf, "\nContinued in next message...\n");
  926.             fputs(buf, tpktfp);
  927.             if (tpktfp != NULL)
  928.               fclose(tpktfp);
  929.             sprintf(fn, "%sSPOOL\\INPUT%d.MSG", net_data, ++part);
  930.             if (stat(fn, &statbuf) == 0)
  931.               unlink(fn);
  932.             if ((tpktfp = fsh_open(fn, "wt+")) == NULL) {
  933.               fprintf(stderr, "\n ■ Unable to create temporary packet 3 %s", fn);
  934.               sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  935.               *abort = 3;
  936.               return 0;
  937.             }
  938.             backline();
  939.             output("\r ■ Breaking into %d%s part [ ]", (part), ordinal_text(part));
  940.             strcpy(buf, "\nContinued from previous message...\n");
  941.             fputs(buf, tpktfp);
  942.             msgsize = strlen(buf);
  943.           }
  944.         }
  945.         if (tpktfp != NULL)
  946.           fclose(tpktfp);
  947.         output("\b\bX]");
  948.         return 1;
  949.       }
  950.       return 1;
  951.     default:
  952.       output("\n ■ Unknown BODY error : %s", _temp_buffer);
  953.       break;
  954.   }
  955.  
  956.   SOCK_READ_ERR(NNTP);
  957.   return 0;
  958. }
  959.  
  960. int extract(char *to, char *key, char *from)
  961. {
  962.   if (!strnicmp(from, key, strlen(key))) {
  963.     from += strlen(key);
  964.     while (*from == ' ')
  965.       from++;
  966.     strcpy(to, from);
  967.     return 1;
  968.   } else
  969.     return 0;
  970. }
  971.  
  972.  
  973. int chead(Mail_Socket * NNTP_sock, unsigned long cur_article)
  974. {
  975.   char _big_temp_buffer[BIGSTR];
  976.  
  977.   *cur_path = 0;
  978.   *cur_from = 0;
  979.   *cur_replyto = 0;
  980.   *cur_subject = 0;
  981.   *cur_newsgroups = 0;
  982.   *cur_message_ID = 0;
  983.   *cur_references = 0;
  984.   *cur_date = 0;
  985.   *_temp_buffer = 0;
  986.  
  987.   sock_printf(NNTP_sock->sock, "HEAD %lu", cur_article);
  988.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  989.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  990.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  991.   switch (reply) {
  992.     case 423:
  993.       output("%-55.55s", "Expired article.");
  994.       return 0;
  995.     case 221:
  996.       while (1) {
  997.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  998.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  999.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0) {
  1000.           if (cur_replyto[0] == 0)
  1001.             strcpy(cur_replyto, cur_from);
  1002.           return 1;
  1003.         }
  1004.         if ((strlen(_big_temp_buffer)) > STRING)
  1005.           _big_temp_buffer[STRING] = '\0';
  1006.         if (extract(cur_path, "Path:", _big_temp_buffer) ||
  1007.              extract(cur_from, "From:", _big_temp_buffer) ||
  1008.              extract(cur_subject, "Subject:", _big_temp_buffer) ||
  1009.              extract(cur_replyto, "Reply-To:", _big_temp_buffer) ||
  1010.              extract(cur_newsgroups, "Newsgroups:", _big_temp_buffer) ||
  1011.              extract(cur_organization, "Organization:", _big_temp_buffer) ||
  1012.              extract(cur_message_ID, "Message-ID:", _big_temp_buffer) ||
  1013.              extract(cur_references, "References:", _big_temp_buffer) ||
  1014.              extract(cur_lines, "Lines:", _big_temp_buffer) ||
  1015.              extract(cur_date, "Date:", _big_temp_buffer))
  1016.          continue;
  1017.       }
  1018.     default:
  1019.       log_it(0, "\n ■ Unknown chead error : %s", _big_temp_buffer);
  1020.       break;
  1021.   }
  1022.   SOCK_READ_ERR(NNTP);
  1023.   return 0;
  1024. }
  1025.  
  1026. Mail_Socket *netsocket(char *host)
  1027. {
  1028.   Mail_Socket *NNTP_sock = NULL;
  1029.   longword h;
  1030.  
  1031.   sock_init();
  1032.  
  1033.   if (!(h = resolve(host))) {
  1034.     if (!(h = resolve(host))) {
  1035.       output("\n ■ Error : Cannot resolve host %s", host);
  1036.       return NULL;
  1037.     }
  1038.   }
  1039.   if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  1040.     output("\n ■ Error: Insufficient memory to create socket... aborting.");
  1041.     return NULL;
  1042.   }
  1043.   if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  1044.     output("\n ■ Error : Insufficient memory to create socket... aborting.");
  1045.     farfree(NNTP_sock);
  1046.     return NULL;
  1047.   }
  1048.   if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
  1049.     output("\n ■ Error : Unable to connect to to %s.", host);
  1050.     farfree(NNTP_sock);
  1051.     return NULL;
  1052.   }
  1053.   sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
  1054.   sock_wait_established(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1055.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1056.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1057.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1058.   switch (reply) {
  1059.     case 200:
  1060.       log_it(0, "\n ■ Connection to %s accepted...", host);
  1061.       return (NNTP_sock);
  1062.     case 502:
  1063.       output("\n ■ Connection to %s refused... try again later.", host);
  1064.       break;
  1065.     case 503:
  1066.       output("\n ■ NNTP service unavailable. Connection to %s refused.", host);
  1067.       break;
  1068.     default:
  1069.       output("\n ■ Unknown NNTP error. Connection to %s failed.", host);
  1070.       break;
  1071.   }
  1072.   SOCK_READ_ERR(NNTP);
  1073.   return 0;
  1074. }
  1075.  
  1076. int cnext(Mail_Socket * NNTP_sock)
  1077. {
  1078.   char *p, *q;
  1079.  
  1080.   *_temp_buffer = 0;
  1081.  
  1082.   sock_printf(NNTP_sock->sock, "NEXT");
  1083.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1084.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1085.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1086.   switch (reply) {
  1087.     case 421:
  1088.       return 0;
  1089.     case 223:
  1090.       p = _temp_buffer;
  1091.       q = cur_articleid;
  1092.       while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  1093.         p++;
  1094.       while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  1095.         *q++ = *p++;
  1096.       *q++ = '>';
  1097.       *q++ = '\0';
  1098.       return 1;
  1099.     default:
  1100.       log_it(0, "\n ■ Unknown cnext error : %s", _temp_buffer);
  1101.       break;
  1102.   }
  1103.   SOCK_READ_ERR(NNTP);
  1104.   return 0;
  1105. }
  1106.  
  1107. int cslave(Mail_Socket * NNTP_sock)
  1108. {
  1109.   char junk[STRING];
  1110.  
  1111.   *_temp_buffer = 0;
  1112.  
  1113.   sock_printf(NNTP_sock->sock, "SLAVE");
  1114.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1115.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1116.   sscanf(_temp_buffer, "%hu %s", &reply, &junk);
  1117.   SOCK_READ_ERR(NNTP);
  1118.   return 0;
  1119. }
  1120.  
  1121. char *stripspace(char *str)
  1122. {
  1123.   char *obuf, *nbuf;
  1124.  
  1125.   if (str) {
  1126.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  1127.       if (!isspace(*obuf))
  1128.         *nbuf++ = *obuf;
  1129.     }
  1130.     *nbuf = NULL;
  1131.   }
  1132.   return (str);
  1133. }
  1134.  
  1135.  
  1136. int saveactive(Mail_Socket * NNTP_sock, int forced)
  1137. {
  1138.   char s[181], fn[MAXPATH], group[256], act;
  1139.   unsigned long to, from;
  1140.   unsigned fnday, fnmonth, fnyear, fnhour, fnminute, fnsecond;
  1141.   int f, done, update;
  1142.   unsigned long count;
  1143.   FILE *fp;
  1144.   struct ftime ft;
  1145.   struct date d;
  1146.  
  1147.   *_temp_buffer = 0;
  1148.  
  1149.   update = 0;
  1150.  
  1151.   sprintf(fn, "%sNEWSRC", net_data);
  1152.  
  1153.   f = sh_open1(fn, O_RDONLY);
  1154.   if (f < 0)
  1155.     forced = 1;
  1156.   else {
  1157.     getftime(f, &ft);
  1158.     sh_close(f);
  1159.   }
  1160.  
  1161.   if (!forced) {
  1162.     fnsecond = 2 * ft.ft_tsec;
  1163.     fnminute = ft.ft_min;
  1164.     fnhour = ft.ft_hour;
  1165.     fnday = ft.ft_day;
  1166.     fnmonth = ft.ft_month;
  1167.     fnyear = ft.ft_year + 80;
  1168.     output("\n ■ NEWSRC last updated %02u/%02u/%02u %.2u:%.2u:%.2u... ",
  1169.         fnmonth, fnday, fnyear, fnhour, fnminute, fnsecond);
  1170.     getdate(&d);
  1171.     if ((d.da_day == ft.ft_day) && (d.da_mon == ft.ft_month) &&
  1172.         (d.da_year == (ft.ft_year + 1980))) {
  1173.       output("no update required.");
  1174.       return 0;
  1175.     } else
  1176.       output("updating.");
  1177.     if ((d.da_mon == ft.ft_month) && (d.da_day != ft.ft_day))
  1178.       update = 1;
  1179.   }
  1180.  
  1181.   if (update)
  1182.     fp = fsh_open(fn, "a");
  1183.   else
  1184.     fp = fsh_open(fn, "w");
  1185.  
  1186.   if (fp == NULL) {
  1187.     perror(fn);
  1188.     return 0;
  1189.   }
  1190.   done = 0;
  1191.  
  1192.   while (!done) {
  1193.     if (update) {
  1194.       sprintf(s, "NEWGROUPS %02.02u%02.02u%02.02u %02.02u%02.02u%02.02u",
  1195.             fnyear, fnmonth, fnday, fnhour, fnminute, fnsecond);
  1196.       sock_printf(NNTP_sock->sock, s);
  1197.       output("\n ■ Refreshing NEWSRC newsgroup listing :           ");
  1198.     } else {
  1199.       sock_printf(NNTP_sock->sock, "LIST");
  1200.       output("\n ■ Retrieving current newsgroup listing :           ");
  1201.     }
  1202.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1203.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1204.     sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1205.     if ((reply == 215) || (reply == 231)) {
  1206.       count = 0L;
  1207.       while (1) {
  1208.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1209.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1210.         if (*_temp_buffer == '.' && _temp_buffer[1] == 0)
  1211.           break;
  1212.         if (count++ % 25 == 0L)
  1213.           output("\b\b\b\b\b\b\b%-7lu", count);
  1214.         sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
  1215.         sprintf(s, "%s %lu\n", group, to);
  1216.         fsh_write(s, sizeof(char), strlen(s), fp);
  1217.       }
  1218.       if (update) {
  1219.         if (count == 0) {
  1220.           output("\b\b\b\b\b\b\bNo new groups.");
  1221.         } else
  1222.           output("\b\b\b\b\b\b\b%lu group%s added to NEWSRC.", count,
  1223.                 count == 1 ? "" : "s");
  1224.       } else
  1225.         output("\b\b\b\b\b\b\b%lu total groups.", count);
  1226.  
  1227.       if (fp != NULL)
  1228.         fclose(fp);
  1229.       if (update) {
  1230.         f = sh_open1(fn, O_RDWR);
  1231.         ft.ft_day = d.da_day;
  1232.         setftime(f, &ft);
  1233.         sh_close(f);
  1234.       }
  1235.       return 1;
  1236.     } else {
  1237.       if (reply == 480) {
  1238.         if (fp != NULL)
  1239.           fclose(fp);
  1240.         return 0;
  1241.       } else {
  1242.         log_it(0, "\n ■ Unknown saveactive error : %s", _temp_buffer);
  1243.         if (fp != NULL)
  1244.           fclose(fp);
  1245.         done = 1;
  1246.       }
  1247.     }
  1248.   }
  1249.   SOCK_READ_ERR(NNTP);
  1250.   return 0;
  1251. }
  1252.  
  1253. char *stristr(char *string, char *pattern)
  1254. {
  1255.   char *pptr, *sptr, *start;
  1256.   unsigned int slen, plen;
  1257.  
  1258.   for (start = string, pptr = pattern, slen = strlen(string),
  1259.        plen = strlen(pattern); slen >= plen; start++, slen--) {
  1260.     while (toupper(*start) != toupper(*pattern)) {
  1261.       start++;
  1262.       slen--;
  1263.       if (slen < plen)
  1264.         return (NULL);
  1265.     }
  1266.     sptr = start;
  1267.     pptr = pattern;
  1268.     while (toupper(*sptr) == toupper(*pptr)) {
  1269.       sptr++;
  1270.       pptr++;
  1271.       if ('\0' == *pptr)
  1272.         return (start);
  1273.     }
  1274.   }
  1275.   return (NULL);
  1276. }
  1277.  
  1278.  
  1279. int checkx(int cug)
  1280. {
  1281.   char *ptr, *ptr2, *ptr3, fn[MAXPATH], buf[256], tmp[81];
  1282.   int i, max;
  1283.   FILE *fp;
  1284.  
  1285.   if ((strstr(grouprec[cug].groupname, "binaries") != NULL) && (binxpost))
  1286.     return 1;
  1287.  
  1288.   sprintf(buf, "%s!%s", POPNAME, POPDOMAIN);
  1289.   if (strstr(cur_path, buf) != 0) {
  1290.     sprintf(buf, "Local post - %s - skipped.", cur_subject);
  1291.     output("%-55.55s", buf);
  1292.     return 0;
  1293.   }
  1294.  
  1295.   sprintf(fn, "%sNOSPAM.TXT", net_data);
  1296.   if (((fp = fsh_open(fn, "r")) != NULL) && (!binxpost)) {
  1297.     while (fgets(buf, 80, fp)) {
  1298.       trimstr1(buf);
  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.     fclose(fp);
  1307.   }
  1308.  
  1309.   ptr = cur_newsgroups;
  1310.  
  1311.   while (*ptr == ' ')
  1312.     ptr++;
  1313.   max = 0;
  1314.   while (1) {
  1315.     if (*ptr == 0)
  1316.       break;
  1317.     if (*ptr == ',') {
  1318.       ptr++;
  1319.       ++max;
  1320.     }
  1321.     while (*ptr == ' ')
  1322.       ptr++;
  1323.     ptr2 = ptr;
  1324.     ptr3 = buf;
  1325.     while (*ptr2 != 0 && *ptr2 != ',')
  1326.       *ptr3++ = *ptr2++;
  1327.     *ptr3 = 0;
  1328.     while (*(--ptr3) == ' ')
  1329.       *ptr3 = 0;
  1330.     ptr = ptr2;
  1331.     for (i = 0; i < cug; i++) {
  1332.       if (strcmpi(buf, grouprec[i].groupname) == 0) {
  1333.         output("Already posted in %s.", grouprec[i].groupname);
  1334.         return 0;
  1335.       }
  1336.     }
  1337.     if ((crossposts > 0) && (max > crossposts)) {
  1338.       sprintf(buf, "Crossposted to more than %hu newsgroups.", crossposts);
  1339.       output("%-55.55s", buf);
  1340.       return 0;
  1341.     }
  1342.   }
  1343.   return 1;
  1344. }
  1345.  
  1346. int postnews(Mail_Socket * NNTP_sock, int cug)
  1347. {
  1348.   char s[181], s1[12], s2[5], fn[201], buf[STR], *ss;
  1349.   int f1, nlines, len, tlen;
  1350.   FILE *fp;
  1351.   struct ffblk ff;
  1352.  
  1353.   *_temp_buffer = 0;
  1354.  
  1355.   sprintf(fn, "%sOUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
  1356.   f1 = findfirst(fn, &ff, 0);
  1357.   if (f1 != 0) {
  1358.     log_it(0, "\n ■ No outbound news articles to post...");
  1359.     return 1;
  1360.   }
  1361.   while (f1 == 0) {
  1362.     sock_printf(NNTP_sock->sock, "POST");
  1363.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1364.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1365.     if (*_temp_buffer != '3') {
  1366.       if (atoi(_temp_buffer) == 440)
  1367.         log_it(1, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
  1368.       else
  1369.         log_it(0, "\n ■ Remote error: %s", _temp_buffer);
  1370.       return 0;
  1371.     }
  1372.     output("\n ■ Posting article to %s\n", grouprec[cug].groupname);
  1373.     sprintf(s, "%sOUTBOUND\\%s", net_data, ff.ff_name);
  1374.     fp = fsh_open(s, "rb");
  1375.     nlines = 0;
  1376.     while (fgets(buf, 120, fp) != NULL) {
  1377.       if (*buf == '.' && buf[1] == 0) {
  1378.         buf[1] = '.';
  1379.         buf[2] = 0;
  1380.       }
  1381.       tlen = 0;
  1382.       len = strlen(buf);
  1383.       while (tlen < len) {
  1384.         if (!tcp_tick(NNTP_sock->sock)) {
  1385.           if (fp != NULL)
  1386.             fclose(fp);
  1387.           output("\n ■ Connection reset by host");
  1388.           return 1;
  1389.         }
  1390.         tlen += sock_fastwrite(NNTP_sock->sock, &buf[tlen], len - tlen);
  1391.       }
  1392.       backline();
  1393.       output("\r ■ Lines sent : %d", ++nlines);
  1394.     }
  1395.     if (fp != NULL)
  1396.       fclose(fp);
  1397.     sock_printf(NNTP_sock->sock, ".\n");
  1398.     output("\n ■ Awaiting acknowledgement - may take several minutes...");
  1399.     log_it(0, "\n ■ Server response : ");
  1400.     sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
  1401.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1402.     log_it(0, "%s", _temp_buffer);
  1403.     if (*_temp_buffer != '2') {
  1404.       sscanf(_temp_buffer, "%hu %s", &reply, &buf);
  1405.       if (reply == 441) {
  1406.         ss = strtok(_temp_buffer, " ");
  1407.         ss = strtok(NULL, " ");
  1408.         if (atoi(ss) == 435)
  1409.           unlink(s);
  1410.         fnsplit(s, NULL, NULL, s1, s2);
  1411.         log_it(0, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
  1412.         if (unlink(s) == 0)
  1413.           log_it(0, "\n ■ Deleted message - %s", s);
  1414.       } else
  1415.         log_it(0, "\n ■ Remote error: %s", _temp_buffer);
  1416.     } else {
  1417.       if (unlink(s) == 0)
  1418.         log_it(0, "\n ■ Deleted sent message - %s", s);
  1419.     }
  1420.     f1 = findnext(&ff);
  1421.   }
  1422.   SOCK_READ_ERR(NNTP);
  1423.   return 0;
  1424. }
  1425.  
  1426. void get_subtype(int sub, char *where)
  1427. {
  1428.   int ok, which;
  1429.   char fn[181], s[81], net_name[21], *ss;
  1430.   FILE *fp;
  1431.  
  1432.   where[0] = 0;
  1433.   which = sub;
  1434.   sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
  1435.   if ((fp = fsh_open(fn, "r")) == NULL)
  1436.     return;
  1437.   ok = 0;
  1438.   while (fgets(s, 80, fp)) {
  1439.     if (*s == '!') {
  1440.       if (which == atoi(&s[1]))
  1441.         ok = 1;
  1442.     }
  1443.     if (ok && (*s == '$')) {
  1444.       ss = strtok(s, " ");
  1445.       strcpy(net_name, &ss[1]);
  1446.       ss = strtok(NULL, " ");
  1447.       if (fp != NULL)
  1448.         fclose(fp);
  1449.       if ((stricmp(net_name, "FILENET") == 0)) {
  1450.         trimstr1(ss);
  1451.         strcpy(where, ss);
  1452.         return;
  1453.       } else
  1454.         return;
  1455.     }
  1456.   }
  1457.   if (fp != NULL)
  1458.     fclose(fp);
  1459. }
  1460.  
  1461. unsigned long max_on_sub(int cug)
  1462. {
  1463.   int i, num_subs;
  1464.   char fn[MAXPATH], subtype[12];
  1465.   unsigned long max;
  1466.   subboardrec sub;
  1467.   FILE *fp;
  1468.  
  1469.   max = 0;
  1470.   if ((strlen(grouprec[cug].subtype) == 1) && (*grouprec[cug].subtype == '0'))
  1471.     return max;
  1472.   output("\n ■ Maximum posts -     ");
  1473.   sprintf(fn, "%sSUBS.DAT", syscfg.datadir);
  1474.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  1475.     output("\n ■ Unable to read %s.", fn);
  1476.     return max;
  1477.   } else {
  1478.     fseek(fp, 0L, SEEK_END);
  1479.     num_subs = (int) (ftell(fp) / sizeof(subboardrec));
  1480.     for (i = 0; i < num_subs; i++) {
  1481.       output("\b\b\b\b%-4d", i);
  1482.       fseek(fp, (long) i * sizeof(subboardrec), SEEK_SET);
  1483.       fread(&sub, sizeof(subboardrec), 1, fp);
  1484.       get_subtype(i, subtype);
  1485.       if (stricmp(subtype, grouprec[cug].subtype) == 0) {
  1486.         max = (unsigned long)sub.maxmsgs;
  1487.         output("\b\b\b\b%s (%s) - %lu posts.", sub.name, subtype, max);
  1488.         break;
  1489.       }
  1490.     }
  1491.   }
  1492.   if (fp != NULL)
  1493.     fclose(fp);
  1494.   return max;
  1495. }
  1496.  
  1497.  
  1498. int getnews(Mail_Socket * NNTP_sock)
  1499. {
  1500.   int f1, nup, nug, cug, abort, ok, done, firstrun, parts, skipped, bogus;
  1501.   char *p, fn[MAXPATH], mailname[121], reline[121], pktname[121];
  1502.   char ch, orig_subj[STRING], buf[STR];
  1503.   unsigned int text_len;
  1504.   unsigned short temptype;
  1505.   unsigned long new_articles, max_articles;
  1506.   net_header_rec nh;
  1507.   struct stat statbuf;
  1508.   struct ffblk ff;
  1509.   FILE *fp;
  1510.  
  1511.   abort = cug = bogus = 0;
  1512.   fp = NULL;
  1513.  
  1514.   if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
  1515.     if (!saveactive(NNTP_sock, 1)) {
  1516.       if (sendauthinfo(NNTP_sock))
  1517.         saveactive(NNTP_sock, 1);
  1518.     }
  1519.   } else
  1520.     saveactive(NNTP_sock, 0);
  1521.  
  1522.   nug = nup = 1;
  1523.   skipped = 0;
  1524.   cslave(NNTP_sock);
  1525.   while ((cug < ngroups) && (!abort)) {
  1526.     tcp_tick(NNTP_sock->sock);
  1527.     if (nup) {
  1528.       nup = 0;
  1529.       if (grouprec[cug].subtype) {
  1530.         if (fp != NULL)
  1531.           fclose(fp);
  1532.         name_packet(pktname);
  1533.         if ((fp = fsh_open(pktname, "wb")) == NULL) {
  1534.           log_it(0, "\n ■ Unable to create %s!", pktname);
  1535.           return 1;
  1536.         }
  1537.       } else {
  1538.         if (fp != NULL)
  1539.           fclose(fp);
  1540.       }
  1541.       write_groups(0);
  1542.     }
  1543.     if (nug) {
  1544.       nug = 0;
  1545.       while (1) {
  1546.         if (!cgroup(NNTP_sock, grouprec[cug].groupname)) {
  1547.           if (cgroup(NNTP_sock, grouprec[cug].groupname) == -1) {
  1548.             if (sendauthinfo(NNTP_sock))
  1549.               continue;
  1550.             else
  1551.               return 1;
  1552.           }
  1553.           if (++bogus > 10) {
  1554.             log_it(0, "\n ■ Too many invalid newsgroups... NEWS.RC error?");
  1555.             return 1;
  1556.           }
  1557.           log_it(1, "\n ■ Error accessing newsgroup \"%s\".", grouprec[cug].groupname);
  1558.           nug = 1;
  1559.           break;
  1560.         } else
  1561.           break;
  1562.       }
  1563.       if (!nug) {
  1564.         log_it(0, "\n ■ Requesting %s... ", grouprec[cug].groupname);
  1565.         if (grouprec[cug].lastread < cur_first)
  1566.           grouprec[cug].lastread = cur_first;
  1567.         if (grouprec[cug].lastread >= cur_last) {
  1568.           grouprec[cug].lastread = cur_last;
  1569.           log_it(0, "no new articles.");
  1570.           nug = 1;
  1571.         } else {
  1572.           new_articles = cur_last - grouprec[cug].lastread;
  1573.           if (new_articles) {
  1574.             log_it(0, "%lu new article%s.", new_articles,
  1575.                    new_articles == 1 ? "" : "s");
  1576.           } else {
  1577.             log_it(0, "no new articles.");
  1578.             nug = 1;
  1579.           }
  1580.           if (new_articles > 250L) {
  1581.             max_articles = max_on_sub(cug);
  1582.             if (max_articles && (new_articles > max_articles)) {
  1583.               log_it(0, "\n ■ Requesting most recent %lu articles", max_articles);
  1584.               grouprec[cug].lastread = cur_last - max_articles;
  1585.             }
  1586.           } else
  1587.             ++grouprec[cug].lastread;
  1588.         }
  1589.         postnews(NNTP_sock, cug);
  1590.         if (!nug) {
  1591.           if (cur_numa == 0) {
  1592.             log_it(0, "\n ■ No articles in %s...", grouprec[cug].groupname);
  1593.             nug = 1;
  1594.             continue;
  1595.           }
  1596.         }
  1597.       }
  1598.     } else {
  1599.       if ((!cnext(NNTP_sock)) || (grouprec[cug].lastread > cur_last)) {
  1600.         output("\n ■ End of articles in %s", grouprec[cug].groupname);
  1601.         nug = 1;
  1602.         write_groups(1);
  1603.       }
  1604.     }
  1605.     if (!nug) {
  1606.       if (skipped)
  1607.         output("\r");
  1608.       else
  1609.         output("\n");
  1610.       output(" ■ [%lu/%lu] : ", grouprec[cug].lastread, cur_last);
  1611.       if ((chead(NNTP_sock, grouprec[cug].lastread)) && (checkx(cug))) {
  1612.         skipped = 0;
  1613.         if (*cur_subject)
  1614.           treat(cur_subject);
  1615.         else
  1616.           strcpy(cur_subject, "No subject");
  1617.         strcpy(orig_subj, cur_subject);
  1618.         if (*cur_from) {
  1619.           treat(cur_from);
  1620.           if (strlen(cur_from) > 45)
  1621.             cur_from[45] = 0;
  1622.         } else
  1623.           strcpy(cur_from, "Unknown");
  1624.         if (*cur_replyto) {
  1625.           if (strlen(cur_replyto) > 45)
  1626.             cur_replyto[45] = 0;
  1627.         } else
  1628.           strcpy(cur_replyto, "Unknown");
  1629.         output("%-55.55s", orig_subj);
  1630.         if ((strlen(grouprec[cug].subtype) == 1) && (grouprec[cug].subtype[0] == '0')) {
  1631.           spooltodisk = 1;
  1632.           sprintf(fn, "%sSPOOL\\NEWS%d.UUE", net_data, cug);
  1633.         } else {
  1634.           spooltodisk = 0;
  1635.           sprintf(fn, "%sSPOOL\\INPUT1.MSG", net_data);
  1636.           if (stat(fn, &statbuf) == 0)
  1637.             unlink(fn);
  1638.         }
  1639.         abort = 0;
  1640.         ok = (savebody(NNTP_sock, fn, cug, grouprec[cug].lastread, &abort));
  1641.         if (ok && grouprec[cug].subtype) {
  1642.           sprintf(fn, "%sSPOOL\\INPUT*.MSG", net_data);
  1643.           done = 1;
  1644.           parts = 0;
  1645.           if (findfirst(fn, &ff, 0) == 0) {
  1646.             parts = 1;
  1647.             done = 0;
  1648.           }
  1649.           firstrun = 1;
  1650.           while (!done) {
  1651.             sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1652.             f1 = sh_open1(fn, O_RDONLY | O_BINARY);
  1653.             text_len = (unsigned int) filelength(f1);
  1654.             if (text_len > 32000L) {
  1655.               output("\n ■ Truncating %lu bytes from input file",
  1656.                      text_len - 32000L);
  1657.               text_len = 32000L;
  1658.             }
  1659.             if ((p = (char *) malloc(32767L)) == NULL) {
  1660.               log_it(0, "\n ■ Insufficient memory to read entire message");
  1661.               return 1;
  1662.             }
  1663.             sh_read(f1, (void *) p, text_len);
  1664.             sh_close(f1);
  1665.             temptype = atoi(grouprec[cug].subtype);
  1666.             nh.tosys = sy;
  1667.             nh.touser = 0;
  1668.             nh.fromsys = 32767;
  1669.             nh.fromuser = 0;
  1670.             if (!temptype) {
  1671.               nh.main_type = main_type_new_post;
  1672.               nh.minor_type = 0;
  1673.             } else {
  1674.               nh.main_type = main_type_pre_post;
  1675.               nh.minor_type = temptype;
  1676.             }
  1677.             nh.list_len = 0;
  1678.             ++cur_daten;
  1679.             nh.daten = cur_daten;
  1680.             nh.method = 0;
  1681.             if (parts > 1) {
  1682.               sprintf(buf, "%d%s ", parts, ordinal_text(parts));
  1683.               strncat(buf, orig_subj, 72);
  1684.               strcpy(cur_subject, buf);
  1685.               cur_subject[72] = '\0';
  1686.             }
  1687.             nh.length = text_len + strlen(cur_subject) + 1;
  1688.             strncpy(mailname, cur_replyto, 46);
  1689.             strcat(mailname, "\r\n");
  1690.             nh.length += strlen(mailname);
  1691.             if (nh.main_type == main_type_new_post)
  1692.               nh.length += strlen(grouprec[cug].subtype) + 1;
  1693.             if (firstrun) {
  1694.               firstrun = 0;
  1695.               if (strncmpi(cur_articleid, "re: ", 4) == 0) {
  1696.                 strncpy(reline, cur_articleid, 60);
  1697.                 sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
  1698.               }
  1699.             }
  1700.             if (!*cur_date) {
  1701.               strncpy(cur_date, ctime(&(time_t) nh.daten), 24);
  1702.               cur_date[24] = '\0';
  1703.             }
  1704.             strcat(cur_date, "\r\n");
  1705.             nh.length += strlen(cur_date);
  1706.             nh.length += strlen(cur_articleid);
  1707.             fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  1708.             if (nh.main_type == main_type_new_post)
  1709.               fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
  1710.             fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
  1711.             fsh_write(mailname, sizeof(char), strlen(mailname), fp);
  1712.             fsh_write(cur_date, sizeof(char), strlen(cur_date), fp);
  1713.             fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
  1714.             fsh_write(p, sizeof(char), text_len, fp);
  1715.             if (p)
  1716.               free(p);
  1717.             unlink(fn);
  1718.             if (findnext(&ff) == 0) {
  1719.               done = 0;
  1720.               ++parts;
  1721.             } else
  1722.               done = 1;
  1723.           }
  1724.         }
  1725.       } else
  1726.         skipped = 1;
  1727.       ++grouprec[cug].lastread;
  1728.       if ((filelength(fileno(fp)) > 250000L) && grouprec[cug].subtype)
  1729.         nup = 1;
  1730.       if (abort == 2) {
  1731.         write_groups(0);
  1732.         nug = 1;
  1733.         abort = 0;
  1734.       }
  1735.       if (abort == 3) {
  1736.         output("\n ■ Disk error - exiting.");
  1737.         write_groups(0);
  1738.         return 1;
  1739.       }
  1740.     }
  1741.     if (nug) {
  1742.       ++cug;
  1743.       if (((strlen(grouprec[cug].subtype) == 1) &&
  1744.            (grouprec[cug].subtype[0] == '0')) && (grouprec[cug].subtype)) {
  1745.         nup = 1;
  1746.       } else {
  1747.         if (((strlen(grouprec[cug - 1].subtype)) > 1) &&
  1748.             ((strlen(grouprec[cug].subtype) == 1) &&
  1749.              (grouprec[cug].subtype[0] == '0'))) {
  1750.           if (fp != NULL)
  1751.             fclose(fp);
  1752.         }
  1753.       }
  1754.     }
  1755.     while (kbhit()) {
  1756.       ch = (getch());
  1757.       switch (ch) {
  1758.         case 27:
  1759.           log_it(0, "\n ■ Aborting news retrieval session...");
  1760.           write_groups(0);
  1761.           abort = 1;
  1762.           break;
  1763.         case 32:
  1764.           output("\n ■ Skipping group %s... ", grouprec[cug].groupname);
  1765.           nug = 1;
  1766.           break;
  1767.         default:
  1768.           break;
  1769.       }
  1770.     }
  1771.   }
  1772.   if (fp != NULL)
  1773.     fclose(fp);
  1774.   nntp_shutdown(NNTP_sock);
  1775.   if (abort)
  1776.     log_it(0, "\n ■ Session aborted from keyboard");
  1777.   write_groups(0);
  1778.   return 0;
  1779. }
  1780.  
  1781. int copyfile(char *input, char *output)
  1782. {
  1783.   int f1, f2, i;
  1784.   char *b;
  1785.   struct ftime ft;
  1786.  
  1787.   if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
  1788.     if ((b = (char *) malloc(16400)) == NULL)
  1789.       return 0;
  1790.     f1 = sh_open1(input, O_RDONLY | O_BINARY);
  1791.     if (!f1) {
  1792.       free(b);
  1793.       b = NULL;
  1794.       return 0;
  1795.     }
  1796.     getftime(f1, &ft);
  1797.  
  1798.     f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1799.     if (!f2) {
  1800.       free(b);
  1801.       b = NULL;
  1802.       f1 = sh_close(f1);
  1803.       return 0;
  1804.     }
  1805.     i = read(f1, (void *) b, 16384);
  1806.     while (i > 0) {
  1807.       sh_write(f2, (void *) b, i);
  1808.       i = read(f1, (void *) b, 16384);
  1809.     }
  1810.     f1 = sh_close(f1);
  1811.     setftime(f2, &ft);
  1812.     f2 = sh_close(f2);
  1813.     free(b);
  1814.     b = NULL;
  1815.   }
  1816.   return 1;
  1817. }
  1818.  
  1819.  
  1820.  
  1821. main(int argc, char *argv[])
  1822. {
  1823.   int ok, f, bAuth;
  1824.   char s[160], fn[MAXPATH], tempfn[MAXPATH], *ss;
  1825.   FILE *fp;
  1826.   struct tm *time_now;
  1827.   struct date dt;
  1828.   struct time tm;
  1829.   time_t some;
  1830.   Mail_Socket *nntp_sock = NULL;
  1831.  
  1832.   crossposts = 10;
  1833.   binxpost = 0;
  1834.   POPNAME[0] = 0;
  1835.   POPDOMAIN[0] = 0;
  1836.  
  1837.   cursor('S');
  1838.   cursor('H');
  1839.   output("\n ■ %s", version);
  1840.   if (argc != 4) {
  1841.     output("\n ■ Invalid arguments for %s\n", argv[0]);
  1842.     cursor('R');
  1843.     return EXIT_FAILURE;
  1844.   }
  1845.   strcpy(net_data, argv[1]);
  1846.   strcat(net_data, "\\");
  1847.   strcpy(serverhost, argv[2]);
  1848.   sy = atoi(argv[3]);
  1849.  
  1850.   get_dir(maindir, 0);
  1851.  
  1852.   detect_multitask();
  1853.  
  1854.   gettime(&tm);
  1855.   getdate(&dt);
  1856.   cur_daten = dostounix(&dt, &tm);
  1857.   time(&some);
  1858.   time_now = localtime(&some);
  1859.   strftime(s, 80, "\n\nNEWS session beginning on %A, %B %d, %Y at %H:%M %p",
  1860.            time_now);
  1861.   sprintf(fn, "%sNEWS.LOG", net_data);
  1862.   if ((fp = fsh_open(fn, "at")) != NULL) {
  1863.     fprintf(fp, s);
  1864.     fclose(fp);
  1865.   }
  1866.   ss = getenv("WWIV_INSTANCE");
  1867.   if (ss) {
  1868.     instance = atoi(ss);
  1869.     if ((instance <= 0) || (instance >= 1000)) {
  1870.       log_it(0, "\n ■ WWIV_INSTANCE set to %d.  Can only be 1..999!", instance);
  1871.       instance = 1;
  1872.     }
  1873.   } else
  1874.     instance = 1;
  1875.  
  1876.   sprintf(fn, "%s\\CONFIG.DAT", maindir);
  1877.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  1878.   if (f < 0) {
  1879.     output("\n ■ %s NOT FOUND.", fn);
  1880.     cursor('R');
  1881.     return EXIT_FAILURE;
  1882.   }
  1883.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1884.   sh_close(f);
  1885.  
  1886.   read_groups();
  1887.   if (ngroups == 0) {
  1888.     output("\n ■ Unable to access newsgroup file NEWS.RC!");
  1889.     cursor('R');
  1890.     return EXIT_FAILURE;
  1891.   } else {
  1892.     sprintf(fn, "%sNEWS.RC", net_data);
  1893.     sprintf(tempfn, "%sNEWS.BAK", net_data);
  1894.     if (exist(tempfn))
  1895.       unlink(tempfn);
  1896.     copyfile(fn, tempfn);
  1897.   }
  1898.  
  1899.   if (stricmp(grouprec[0].groupname, "newsrc") == 0)
  1900.     log_it(0, "\n ■ Retrieving current newsgroup listing from %s.",
  1901.            serverhost);
  1902.   else
  1903.     log_it(0, "\n ■ %u newsgroup%s defined in NEWS.RC.", ngroups,
  1904.            ngroups == 1 ? "" : "s");
  1905.  
  1906.   sprintf(fn, "%s\\NET.INI", maindir);
  1907.   if ((fp = fsh_open(fn, "rt")) == NULL)
  1908.     output("\n ■ Unable to read %s", fn);
  1909.   else {
  1910.     while (fgets(s, 100, fp)) {
  1911.       stripspace(s);
  1912.       if ((*s == ';') || (*s == '\n') || (*s == '['))
  1913.         continue;
  1914.       if (strlen(s) == 0)
  1915.         continue;
  1916.       if ((strnicmp(s, "POPNAME", 7) == 0) && (POPNAME[0] == 0)) {
  1917.         ss = strtok(s, "=");
  1918.         if (ss) {
  1919.           ss = strtok(NULL, "\n");
  1920.           trimstr1(ss);
  1921.           strcpy(POPNAME, ss);
  1922.         }
  1923.       }
  1924.       if (strnicmp(s, "FWDNAME", 7) == 0) {
  1925.         ss = strtok(s, "=");
  1926.         if (ss) {
  1927.           ss = strtok(NULL, "\n");
  1928.           trimstr1(ss);
  1929.           strcpy(POPNAME, ss);
  1930.         }
  1931.       }
  1932.       if ((strnicmp(s, "DOMAIN", 6) == 0) && (POPDOMAIN[0] == 0)) {
  1933.         ss = strtok(s, "=");
  1934.         if (ss) {
  1935.           ss = strtok(NULL, "\n");
  1936.           trimstr1(ss);
  1937.           strcpy(POPDOMAIN, ss);
  1938.         }
  1939.       }
  1940.       if (strnicmp(s, "FWDDOM", 6) == 0) {
  1941.         ss = strtok(s, "=");
  1942.         if (ss) {
  1943.           ss = strtok(NULL, "\n");
  1944.           trimstr1(ss);
  1945.           strcpy(POPDOMAIN, ss);
  1946.         }
  1947.       }
  1948.       if (strnicmp(s, "XPOSTS", 6) == 0) {
  1949.         ss = strtok(s, "=");
  1950.         if (ss) {
  1951.           ss = strtok(NULL, "\n");
  1952.           trimstr1(ss);
  1953.           crossposts = atoi(ss);
  1954.           if (crossposts > 99)
  1955.             crossposts = 10;
  1956.         }
  1957.       }
  1958.       if (strnicmp(s, "BINXPOST", 9) == 0) {
  1959.         ss = strtok(s, "=");
  1960.         if (ss) {
  1961.           binxpost = 0;
  1962.           ss = strtok(NULL, "\n");
  1963.           trimstr1(ss);
  1964.           if ((ss[0] == 'y') || (ss[0] == 'Y'))
  1965.             binxpost = 1;
  1966.         }
  1967.       }
  1968.       if (strnicmp(s, "NEWSNAME", 8) == 0) {
  1969.         ss = strtok(s, "=");
  1970.         if (ss) {
  1971.           ss = strtok(NULL, "\n");
  1972.           trimstr1(ss);
  1973.           strcpy(NEWSNAME, ss);
  1974.         }
  1975.       }
  1976.       if (strnicmp(s, "NEWSPASS", 8) == 0) {
  1977.         ss = strtok(s, "=");
  1978.         if (ss) {
  1979.           ss = strtok(NULL, "\n");
  1980.           trimstr1(ss);
  1981.           strcpy(NEWSPASS, ss);
  1982.         }
  1983.       }
  1984.       ss = NULL;
  1985.     }
  1986.     if (fp != NULL)
  1987.       fclose(fp);
  1988.   }
  1989.   ok = 1;
  1990.   if ((nntp_sock = netsocket(serverhost)) != NULL) {
  1991.     bAuth = 1;
  1992.     if (NEWSNAME[0]) {
  1993.       if (!sendauthinfo(nntp_sock))
  1994.         bAuth = 0;
  1995.     }
  1996.     if (bAuth)
  1997.       ok = getnews(nntp_sock);
  1998.     if (ok)
  1999.       log_it(1, "\n ■ NEWS exited with an error.");
  2000.     else
  2001.       log_it(0, "\n ■ Normal NEWS completion.");
  2002.   }
  2003.   cd_to(maindir);
  2004.   log_it(0, "\n ■ NEWS completed processing %d newsgroups", ngroups);
  2005.   if (grouprec)
  2006.     farfree(grouprec);
  2007.   grouprec = NULL;
  2008.   check_packets();
  2009.   trim_log();
  2010.   free_Mail_Socket(nntp_sock);
  2011.   fcloseall();
  2012.   cursor('R');
  2013.   return ok;
  2014. }
  2015.