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