home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B34.ZIP / NEWS.C < prev    next >
C/C++ Source or Header  |  1997-08-04  |  52KB  |  1,898 lines

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