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