home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B33.ZIP / NEWS.C < prev    next >
C/C++ Source or Header  |  1997-07-20  |  52KB  |  1,896 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 (send_ssm)
  533.     ssm(s);
  534.   return 0;
  535. }
  536.  
  537.  
  538. int count_lines(char *s)
  539. {
  540.   FILE *fp;
  541.   char fn[201];
  542.   unsigned int nl = 0;
  543.   const int NEWLINE = '\n';
  544.   int c;
  545.  
  546.   strcpy(fn, s);
  547.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  548.     output("\n ■ Cannot open %s", fn);
  549.     return 0;
  550.   }
  551.   while ((c = getc(fp)) != EOF) {
  552.     if (c == NEWLINE)
  553.       ++nl;
  554.   }
  555.   if (fp != NULL)
  556.     fclose(fp);
  557.   return nl;
  558. }
  559.  
  560. static unsigned char *trimstr1(unsigned char *s)
  561. {
  562.   int i;
  563.   static char *whitespace = " \r\n\t";
  564.  
  565.   i = strlen(s);
  566.   while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
  567.     --i;
  568.   while ((i > 0) && (_fstrchr(whitespace, *s))) {
  569.     memmove(s, s + 1, --i);
  570.   }
  571.   s[i] = 0;
  572.   return (s);
  573. }
  574.  
  575. void usleep(void)
  576. {
  577.   tcp_tick(NULL);                           /* occasionally process something */
  578. }
  579.  
  580. int exist(char *s)
  581. {
  582.   int i;
  583.   struct ffblk ff;
  584.  
  585.   i = findfirst(s, &ff, 0);
  586.   if (i)
  587.     return (0);
  588.   else
  589.     return (1);
  590. }
  591.  
  592. void rename_pend(char *dir, char *file)
  593. {
  594.   char s[181], s1[181];
  595.   int i, ok;
  596.  
  597.   sprintf(s, "%s\\%s", dir, file);
  598.   ok = 0;
  599.   for (i = 0; i < 1000 && !ok; i++) {
  600.     sprintf(s1, "%s\\P0-%u.NET", dir, i);
  601.     if (!exist(s1)) {
  602.       rename(s, s1);
  603.       ok = 1;
  604.     }
  605.   }
  606. }
  607.  
  608. void check_packets(void)
  609. {
  610.   char s[181];
  611.   int f1;
  612.   struct ffblk ff;
  613.  
  614.   sprintf(s, "%s\\P0-*.%3.3d", net_data, instance);
  615.   f1 = findfirst(s, &ff, 0);
  616.   while (f1 == 0) {
  617.     rename_pend(net_data, ff.ff_name);
  618.     f1 = findnext(&ff);
  619.   }
  620. }
  621.  
  622.  
  623. void err_exit(int exitlevel, char *fmt,...)
  624. {
  625.   va_list v;
  626.   char s[255];
  627.  
  628.   va_start(v, fmt);
  629.   vsprintf(s, fmt, v);
  630.   va_end(v);
  631.   fputs(s, stderr);
  632.   check_packets();
  633.   cursor('R');
  634.   exit(exitlevel);
  635. }
  636.  
  637. void write_groups(int display)
  638. {
  639.   int i;
  640.   FILE *groupfp;
  641.  
  642.   if ((groupfp = fsh_open(newsrc, "wt")) == NULL) {
  643.     output("\n ■ Unable to open %s!", newsrc);
  644.     return;
  645.   } else {
  646.     if (display)
  647.       output("\n ■ Updating pointers in %s...", newsrc);
  648.   }
  649.   for (i = 0; i < ngroups; i++) {
  650.     if ((grouprec[i].groupname[0]) && (stricmp(grouprec[i].groupname, "newsrc") != 0)) {
  651.       fprintf(groupfp, "%s %ld %s\n", grouprec[i].groupname,
  652.               grouprec[i].lastread, grouprec[i].subtype);
  653.     }
  654.   }
  655.   if (groupfp != NULL)
  656.     fclose(groupfp);
  657. }
  658.  
  659. void far *mallocx(unsigned long l)
  660. {
  661.   void *x;
  662.   char huge *xx;
  663.  
  664.   if (!l)
  665.     l = 1;
  666.   x = farmalloc(l);
  667.   if (!x) {
  668.     output("\n ■ Unable to allocate %lu bytes of memory", l);
  669.     cursor('R');
  670.     exit(EXIT_FAILURE);
  671.   }
  672.   xx = (char huge *) x;
  673.   while (l) {
  674.     if (l > 32768L) {
  675.       memset((void *) xx, 0, (size_t) 32768L);
  676.       l -= 32768L;
  677.       xx += 32768L;
  678.     } else {
  679.       memset((void *) xx, 0, (size_t) l);
  680.       break;
  681.     }
  682.   }
  683.   return (x);
  684. }
  685.  
  686.  
  687. void far *malloca(unsigned long nbytes)
  688. {
  689.   void *buf;
  690.  
  691.   buf = malloc((int) nbytes + 1);
  692.   if (buf == NULL)
  693.     output("\n ■ Unable to allocate sufficient memory (%ld bytes)", nbytes);
  694.   return (buf);
  695. }
  696.  
  697. void nntp_shutdown(Mail_Socket * NNTP_sock)
  698. {
  699.   sock_printf(NNTP_sock->sock, "QUIT");
  700.   sock_close(NNTP_sock->sock);
  701.   sock_wait_closed(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  702. sock_err:
  703.   return;
  704. }
  705.  
  706.  
  707. void read_groups(void)
  708. {
  709.   int i = 0, ok;
  710.   unsigned short sn;
  711.   char *ss, fn[101], tmp[101];
  712.   FILE *groupfp;
  713.  
  714.   if (grouprec)
  715.     free((void *) grouprec);
  716.   grouprec = NULL;
  717.   ngroups = count_lines(newsrc);
  718.   if (!ngroups)
  719.     return;
  720.   grouprec = (GROUPFILEREC *) mallocx((ngroups + 1) * sizeof(GROUPFILEREC));
  721.   if (!grouprec) {
  722.     ngroups = 0;
  723.     output("\n ■ Unable to allocate memory for reading NEWS.RC!");
  724.     return;
  725.   }
  726.   if ((groupfp = fsh_open(newsrc, "rt")) == NULL) {
  727.     free((void *) grouprec);
  728.     ngroups = 0;
  729.     return;
  730.   }
  731.   while (fgets(tmp, 120, groupfp)) {
  732.     if (tmp[0]) {
  733.       if (strncmpi(tmp, "newsrc", 6) == 0) {
  734.         strcpy(grouprec[i].groupname, "newsrc");
  735.         ++i;
  736.       } else {
  737.         ss = strtok(tmp, " ");
  738.         strcpy(grouprec[i].groupname, ss);
  739.         ss = strtok(NULL, " ");
  740.         if (ss != NULL) {
  741.           grouprec[i].lastread = atol(ss);
  742.           ss = strtok(NULL, " \n");
  743.           if (ss != NULL) {
  744.             strcpy(grouprec[i].subtype, strupr(ss));
  745.             ++i;
  746.           }
  747.         }
  748.       }
  749.     }
  750.   }
  751.   ngroups = i;
  752.   if (groupfp != NULL)
  753.     fclose(groupfp);
  754.   for (i = 0; i < ngroups; i++) {
  755.     if ((grouprec[i].subtype) && (atoi(grouprec[i].subtype) != 0)) {
  756.       sprintf(fn, "%s\\N%s.NET", net_data, grouprec[i].subtype);
  757.       if (exist(fn)) {
  758.         groupfp = fsh_open(fn, "rt");
  759.         ok = 0;
  760.         while ((fgets(tmp, 25, groupfp)) && !ok) {
  761.           sn = atoi(tmp);
  762.           if (sn == 32767)
  763.             ok = 1;
  764.         }
  765.         if (groupfp != NULL)
  766.           fclose(groupfp);
  767.         if (!ok)
  768.           log_it(1, "\n ■ @32767 not list as a subscriber in N%s.NET!",
  769.                  grouprec[i].subtype);
  770.       }
  771.     }
  772.   }
  773. }
  774.  
  775. int cgroup(Mail_Socket * NNTP_sock, char *s)
  776. {
  777.   static char _temp_buffer[STRING];
  778.  
  779.   sock_printf(NNTP_sock->sock, "GROUP %s", s);
  780.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  781.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  782.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  783.   if (reply == 411)
  784.     return 1;
  785.   else {
  786.     if (reply == 211) {
  787.       sscanf(_temp_buffer, "%hu %lu %lu %lu %s",
  788.              &reply, &cur_numa, &cur_first, &cur_last, &cur_gname);
  789.       return 0;
  790.     } else {
  791.       if (reply == 480)
  792.         return 2;
  793.       else
  794.         log_it(0, "\n ■ Unknown cgroup response : %s", _temp_buffer);
  795.       return 1;
  796.     }
  797.   }
  798.   SOCK_READ_ERR(NNTP_sock);
  799.   return -1;
  800. }
  801.  
  802. int cstat(Mail_Socket * NNTP_sock, unsigned long i)
  803. {
  804.   char *p, *q;
  805.   static char _temp_buffer[STRING];
  806.  
  807.   sock_printf(NNTP_sock->sock, "STAT %lu", i);
  808.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  809.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  810.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  811.   if (reply == 423)
  812.     return -1;
  813.   else
  814.     if (reply == 223) {
  815.     cur_article = reparticle;
  816.     p = _temp_buffer;
  817.     q = cur_articleid;
  818.     while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  819.       p++;
  820.     while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  821.       *q++ = *p++;
  822.     *q++ = '>';
  823.     *q++ = '\0';
  824.     return 0;
  825.     }
  826.   else
  827.     log_it(0, "\n ■ Unknown cstat error : %s", _temp_buffer);
  828.   SOCK_READ_ERR(NNTP_sock);
  829.   return -1;
  830. }
  831.  
  832. void qexit(Mail_Socket * NNTP_sock, int n)
  833. {
  834.   static char _temp_buffer[STR];
  835.  
  836.   if (n >= 10)
  837.     log_it(0, "\n ! Fatal NEWS error: check configuration!");
  838.   fflush(stdout);
  839.   sock_printf(NNTP_sock->sock, "QUIT");
  840.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  841.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  842.   if (*_temp_buffer != '2')
  843.     log_it(0, "\n ■ Tried to quit host but failed");
  844. sock_err:
  845.   cursor('R');
  846.   check_packets();
  847.   fcloseall();
  848.   free_Mail_Socket(NNTP_sock);
  849.   exit(n);
  850. }
  851.  
  852. void treat(char *str)
  853. {
  854.   char *obuf, *nbuf;
  855.  
  856.   if (*str) {
  857.     for (obuf = str, nbuf = str; *obuf && obuf; ++obuf) {
  858.       if (((*obuf >= 32) && (*obuf <= 126)) || (*obuf == 10) || (*obuf == 9)) {
  859.         *nbuf++ = *obuf;
  860.       }
  861.     }
  862.   }
  863.   *nbuf = 0;
  864. }
  865.  
  866. void name_packet(char *pktname)
  867. {
  868.   int ok;
  869.   struct stat info;
  870.   unsigned i;
  871.  
  872.   ok = 0;
  873.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  874.     sprintf(pktname, "%s\\P0-%u.%3.3d", net_data, i, instance);
  875.     if (stat(pktname, &info) == -1)
  876.       ok = 1;
  877.   }
  878. }
  879.  
  880. int savebody(Mail_Socket * NNTP_sock, int cug, int *abort)
  881. {
  882.   char s1[21], s2[21], ch;
  883.   unsigned int curpos, count, part;
  884.   char spin[26];
  885.   int i, place, len, spooltodisk;
  886.   unsigned long msgsize;
  887.   struct stat statbuf;
  888.   FILE *tpktfp = NULL;
  889.   static char _temp_buffer[BIGSTR];
  890.  
  891.   strcpy(spin, "||//--\\\\");
  892.   place = count = spooltodisk = 0;
  893.   len = strlen(spin);
  894.  
  895.   if ((strlen(grouprec[cug].subtype) == 1) &&
  896.       (grouprec[cug].subtype[0] == '0'))
  897.     spooltodisk = 1;
  898.   part = 1;
  899.   if (spooltodisk)
  900.     sprintf(tpktname, "%s\\NEWS%d.UUE", tmpdir, cug);
  901.   else {
  902.     sprintf(tpktname, "%s\\INPUT%d.MSG", tmpdir, part++);
  903.     if (stat(tpktname, &statbuf) == 0)
  904.       unlink(tpktname);
  905.   }
  906.   sock_printf(NNTP_sock->sock, "BODY %lu", cur_article);
  907.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  908.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  909.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  910.   if (reply == 423)
  911.     return -1;
  912.   else {
  913.     if (reply == 222) {
  914.       if (!spooltodisk) {
  915.         if ((tpktfp = fsh_open(tpktname, "wt+")) == NULL) {
  916.           log_it(0, "\n ■ Unable to create temporary packet %s", tpktname);
  917.           qexit(NNTP_sock, 1);
  918.         } else {
  919.           strcpy(_temp_buffer, "\n");
  920.           fputs(_temp_buffer, tpktfp);
  921.         }
  922.       } else {
  923.         if (tpktfp != NULL)
  924.           fclose(tpktfp);
  925.         if ((tpktfp = fsh_open(tpktname, "at+")) == NULL) {
  926.           log_it(0, "\n ■ Unable to create temporary packet %s", tpktname);
  927.           qexit(NNTP_sock, 1);
  928.         } else {
  929.           fputs("\n", tpktfp);
  930.           for (i = 0; i < 79; i++)
  931.             fputs("-", tpktfp);
  932.           fputs("\n", tpktfp);
  933.           sprintf(_temp_buffer, "Art  : %lu\n", cur_article);
  934.           fputs(_temp_buffer, tpktfp);
  935.           sprintf(_temp_buffer, "Date : %s\n", cur_date);
  936.           fputs(_temp_buffer, tpktfp);
  937.           sprintf(_temp_buffer, "From : %s\n", cur_replyto);
  938.           fputs(_temp_buffer, tpktfp);
  939.           sprintf(_temp_buffer, "Subj : %s\n\n", cur_subject);
  940.           fputs(_temp_buffer, tpktfp);
  941.         }
  942.       }
  943.       fnsplit(tpktname, NULL, NULL, s1, s2);
  944.       if (!spooltodisk)
  945.         output("\n ■ Receiving message - <Esc> aborts - <Space> skips group [-]");
  946.       else
  947.         output("\n ■ Appending to %s%s - <Esc> aborts - <Space> skips group [-]", s1, s2);
  948.       msgsize = 0L;
  949.       curpos = 0L;
  950.       while (1) {
  951.         if (kbhit()) {
  952.           ch = (getch());
  953.           switch (ch) {
  954.             case 27:
  955.               output("\r ■ Abort detected... please wait until message is completed [-]");
  956.               *abort = 1;
  957.               break;
  958.             case 32:
  959.               output("\r ■ Skipping group... please wait until message is completed [-]");
  960.               *abort = 2;
  961.               break;
  962.             default:
  963.               break;
  964.           }
  965.         }
  966.         sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  967.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  968.         if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
  969.           break;
  970.         } else {
  971.           treat(_temp_buffer);
  972.           strcat(_temp_buffer, "\n");
  973.           curpos = strlen(_temp_buffer);
  974.           fputs(_temp_buffer, tpktfp);
  975.           msgsize += curpos;
  976.         }
  977.         if (count++ > 5) {
  978.           output("\b\b%c]", spin[place++]);
  979.           place %= len;
  980.           count = 0;
  981.         }
  982.         if ((msgsize > 30000L) && (!spooltodisk)) {
  983.           sprintf(_temp_buffer, "\nContinued in next message...\n");
  984.           fputs(_temp_buffer, tpktfp);
  985.           if (tpktfp != NULL)
  986.             fclose(tpktfp);
  987.           sprintf(tpktname, "%s\\INPUT%d.MSG", tmpdir, part++);
  988.           if (stat(tpktname, &statbuf) == 0)
  989.             unlink(tpktname);
  990.           if ((tpktfp = fsh_open(tpktname, "wt+")) == NULL) {
  991.             log_it(0, "\n ■ Unable to create temporary packet %s", tpktname);
  992.             qexit(NNTP_sock, 1);
  993.           }
  994.           backline();
  995.           output("\r ■ Breaking into %d%s part [ ]", (
  996.                                          part - 1), ordinal_text(part - 1));
  997.           sprintf(_temp_buffer, "\nContinued from previous message...\n");
  998.           fputs(_temp_buffer, tpktfp);
  999.           msgsize = strlen(_temp_buffer);
  1000.         }
  1001.       }
  1002.       if (tpktfp != NULL)
  1003.         fclose(tpktfp);
  1004.       output("\b\bX]");
  1005.       return 1;
  1006.     }
  1007.   }
  1008.   if (tpktfp != NULL)
  1009.     fclose(tpktfp);
  1010.   SOCK_READ_ERR(NNTP_sock);
  1011.   return 0;
  1012. }
  1013.  
  1014. int extract(char *to, char *key, char *from)
  1015. {
  1016.   if (!strnicmp(from, key, strlen(key))) {
  1017.     from += strlen(key);
  1018.     while (*from == ' ')
  1019.       from++;
  1020.     strcpy(to, from);
  1021.     return 1;
  1022.   } else
  1023.     return 0;
  1024. }
  1025.  
  1026.  
  1027. int chead(Mail_Socket * NNTP_sock)
  1028. {
  1029.   static char _temp_buffer[BIGSTR];
  1030.  
  1031.   *cur_path = 0;
  1032.   *cur_from = 0;
  1033.   *cur_replyto = 0;
  1034.   *cur_subject = 0;
  1035.   *cur_newsgroups = 0;
  1036.   *cur_message_ID = 0;
  1037.   *cur_references = 0;
  1038.   *cur_date = 0;
  1039.  
  1040.   sock_printf(NNTP_sock->sock, "HEAD %lu", cur_article);
  1041.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1042.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1043.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1044.   if (reply == 423)
  1045.     return 0;
  1046.   else
  1047.     if (reply == 221)
  1048.     while (1) {
  1049.       sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1050.       sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1051.       if (strlen(_temp_buffer) > 1024)
  1052.         _temp_buffer[1024] = 0;
  1053.       if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
  1054.         if (cur_replyto[0] == 0)
  1055.           strcpy(cur_replyto, cur_from);
  1056.         return 1;
  1057.       }
  1058.       extract(cur_path, "Path:", _temp_buffer) ||
  1059.           extract(cur_from, "From:", _temp_buffer) ||
  1060.           extract(cur_subject, "Subject:", _temp_buffer) ||
  1061.           extract(cur_replyto, "Reply-To:", _temp_buffer) ||
  1062.           extract(cur_newsgroups, "Newsgroups:", _temp_buffer) ||
  1063.           extract(cur_organization, "Organization:", _temp_buffer) ||
  1064.           extract(cur_message_ID, "Message-ID:", _temp_buffer) ||
  1065.           extract(cur_references, "References:", _temp_buffer) ||
  1066.           extract(cur_lines, "Lines:", _temp_buffer) ||
  1067.           extract(cur_date, "Date:", _temp_buffer);
  1068.     }
  1069.   else {
  1070.     log_it(0, "\n ■ Unknown chead error : %s", _temp_buffer);
  1071.   }
  1072.   SOCK_READ_ERR(NNTP_sock);
  1073.   return 0;
  1074. }
  1075.  
  1076. Mail_Socket *netsocket(char *host)
  1077. {
  1078.   Mail_Socket *NNTP_sock = NULL;
  1079.   longword h;
  1080.   static char _temp_buffer[STR];
  1081.  
  1082.   if (!(h = resolve(host)))
  1083.     err_exit(EXIT_FAILURE, "\n ■ Could not resolve %s... aborting", host);
  1084.   if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL)
  1085.     err_exit(EXIT_FAILURE, "\n ■ Insufficient memory for socket... aborting.");
  1086.   if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  1087.     farfree(NNTP_sock);
  1088.     err_exit(EXIT_FAILURE, "\n ■ Insufficient memory for socket... aborting.");
  1089.   }
  1090.   if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
  1091.     free_Mail_Socket(NNTP_sock);
  1092.     err_exit(EXIT_FAILURE, "\n ■ TCP socket open failed... aborting");
  1093.   }
  1094.   sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
  1095.   sock_wait_established(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1096.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1097.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1098.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1099.   switch (reply) {
  1100.     case 200:
  1101.       log_it(0, "\n ■ Connection to %s accepted...", host);
  1102.       break;
  1103.     case 502:
  1104.       free_Mail_Socket(NNTP_sock);
  1105.       err_exit(EXIT_FAILURE, "\n ■ Connection to %s refused... try again later.", host);
  1106.       break;
  1107.     case 503:
  1108.       free_Mail_Socket(NNTP_sock);
  1109.       err_exit(EXIT_FAILURE, "\n ■ NNTP service unavailable. Connection to %s refused.", host);
  1110.       break;
  1111.     default:
  1112.       free_Mail_Socket(NNTP_sock);
  1113.       err_exit(EXIT_FAILURE, "\n ■ Unknown NNTP error. Connection to %s failed.", host);
  1114.       break;
  1115.   }
  1116.   SOCK_READ_ERR(NNTP_sock);
  1117.   return (NNTP_sock);
  1118. }
  1119.  
  1120. int cnext(Mail_Socket * NNTP_sock)
  1121. {
  1122.   char *p, *q;
  1123.   static char _temp_buffer[STRING];
  1124.  
  1125.   sock_printf(NNTP_sock->sock, "NEXT");
  1126.   sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1127.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1128.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1129.   if (reply == 421)
  1130.     return -1;
  1131.   else
  1132.     if (reply == 223) {
  1133.     cur_article = reparticle;
  1134.     p = _temp_buffer;
  1135.     q = cur_articleid;
  1136.     while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  1137.       p++;
  1138.     while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  1139.       *q++ = *p++;
  1140.     *q++ = '>';
  1141.     *q++ = '\0';
  1142.     return 0;
  1143.     }
  1144.   else {
  1145.     log_it(0, "\n ■ Unknown cnext error : %s", _temp_buffer);
  1146.   }
  1147.   SOCK_READ_ERR(NNTP_sock);
  1148.   return -1;
  1149. }
  1150.  
  1151. char *stripspace(char *str)
  1152. {
  1153.   char *obuf, *nbuf;
  1154.  
  1155.   if (str) {
  1156.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  1157.       if (!isspace(*obuf))
  1158.         *nbuf++ = *obuf;
  1159.     }
  1160.     *nbuf = NULL;
  1161.   }
  1162.   return (str);
  1163. }
  1164.  
  1165.  
  1166. int sendauthinfo(Mail_Socket * NNTP_sock)
  1167. {
  1168.   char s[MAXPATH], line[121], name[21], pass[31], *ss;
  1169.   static char _temp_buffer[BIGSTR];
  1170.   FILE *fp;
  1171.  
  1172.   output("\n ■ Server requested authentication information.");
  1173.   sprintf(s, "%s\\NET.INI", maindir);
  1174.   if ((fp = fsh_open(s, "rt")) == NULL) {
  1175.     output("\n ■ Unable to open %s.", s);
  1176.     return 1;
  1177.   }
  1178.   while (fgets(line, 80, fp)) {
  1179.     ss = NULL;
  1180.     stripspace(line);
  1181.     if ((line[0] == ';') || (line[0] == '\n') || (line[0] == '['))
  1182.       continue;
  1183.     if (strlen(line) == 0)
  1184.       continue;
  1185.     if (strnicmp(line, "NEWSNAME", 8) == 0) {
  1186.       ss = strtok(line, "=");
  1187.       if (ss) {
  1188.         ss = strtok(NULL, "\n");
  1189.         trimstr1(ss);
  1190.         name[0] = 0;
  1191.         strcpy(name, ss);
  1192.       }
  1193.     }
  1194.     if (strnicmp(line, "NEWSPASS", 8) == 0) {
  1195.       ss = strtok(line, "=");
  1196.       if (ss) {
  1197.         ss = strtok(NULL, "\n");
  1198.         trimstr1(ss);
  1199.         pass[0] = 0;
  1200.         strcpy(pass, ss);
  1201.       }
  1202.     }
  1203.   }
  1204.   if ((name[0] == 0) || (pass[0] == 0)) {
  1205.     output("\n ■ NEWSNAME or NEWSPASS not properly defined!");
  1206.     return 1;
  1207.   }
  1208.   while (1) {
  1209.     sprintf(_temp_buffer, "authinfo user %s", name);
  1210.     sock_printf(NNTP_sock->sock, _temp_buffer);
  1211.     sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1212.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1213.     sscanf(_temp_buffer, "%hu %s", &reply, s);
  1214.     if (reply == 381) {
  1215.       sprintf(_temp_buffer, "authinfo pass %s", pass);
  1216.       sock_printf(NNTP_sock->sock, _temp_buffer);
  1217.       sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1218.       sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1219.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  1220.       if (reply == 281) {
  1221.         output("\n ■ Authentication accepted.  Continuing news retrieval session.");
  1222.         return 0;
  1223.       } else {
  1224.         output("\n ■ Authentication failed.  Aborting news retrieval session.");
  1225.         return 1;
  1226.       }
  1227.     } else {
  1228.       output("\n ■ Unknown response \"%hu\" from server.", reply);
  1229.       return 1;
  1230.     }
  1231.   }
  1232.   SOCK_READ_ERR(NNTP_sock);
  1233.   if (fp != NULL)
  1234.     fclose(fp);
  1235.   return -1;
  1236. }
  1237.  
  1238. int saveactive(Mail_Socket * NNTP_sock, char *fn)
  1239. {
  1240.   char s[181], group[256], act;
  1241.   unsigned long to, from;
  1242.   int count, done;
  1243.   FILE *fp;
  1244.   static char _temp_buffer[BIGSTR];
  1245.  
  1246.   if ((fp = fsh_open(fn, "w")) == NULL) {
  1247.     perror(fn);
  1248.     return -1;
  1249.   }
  1250.   done = 0;
  1251.   while (!done) {
  1252.     sock_printf(NNTP_sock->sock, "LIST");
  1253.     sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1254.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1255.     sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1256.     if (reply == 215) {
  1257.       count = 0;
  1258.       while (1) {
  1259.         if (count++ % 25 == 0)
  1260.           output("\b\b\b\b\b\b\b%-7d", count);
  1261.         sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1262.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1263.         if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
  1264.           break;
  1265.         sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
  1266.         sprintf(s, "%s %lu\n", group, to);
  1267.         fsh_write(s, sizeof(char), strlen(s), fp);
  1268.       }
  1269.       log_it(0, "\n ■ %d total newsgroups available on %s", count, serverhost);
  1270.       if (fp != NULL)
  1271.         fclose(fp);
  1272.       return 0;
  1273.     } else {
  1274.       if (reply == 480) {
  1275.         if (fp != NULL)
  1276.           fclose(fp);
  1277.         return 1;
  1278.       } else {
  1279.         log_it(0, "\n ■ Unknown saveactive error : %s", _temp_buffer);
  1280.         if (fp != NULL)
  1281.           fclose(fp);
  1282.         done = 1;
  1283.       }
  1284.     }
  1285.   }
  1286.   SOCK_READ_ERR(NNTP_sock);
  1287.   return -1;
  1288. }
  1289.  
  1290. int checkx(int cug)
  1291. {
  1292.   char *ptr, *ptr2, *ptr3, buf[256];
  1293.   int i, max;
  1294.  
  1295.   ptr = strtok(cur_organization, "*");
  1296.   trimstr1(ptr);
  1297.   if (strncmpi(ptr, syscfg.systemname, strlen(syscfg.systemname)) == 0) {
  1298.     log_it(0, "\n ■ Skipping message - originally posted on %s.", syscfg.systemname);
  1299.     return 0;
  1300.   }
  1301.   ptr = cur_newsgroups;
  1302.   while (*ptr == ' ')
  1303.     ptr++;
  1304.   max = 0;
  1305.   while (1) {
  1306.     if (*ptr == 0)
  1307.       break;
  1308.     if (*ptr == ',') {
  1309.       ptr++;
  1310.       ++max;
  1311.     }
  1312.     while (*ptr == ' ')
  1313.       ptr++;
  1314.     ptr2 = ptr;
  1315.     ptr3 = buf;
  1316.     while (*ptr2 != 0 && *ptr2 != ',')
  1317.       *ptr3++ = *ptr2++;
  1318.     *ptr3 = 0;
  1319.     while (*(--ptr3) == ' ')
  1320.       *ptr3 = 0;
  1321.     ptr = ptr2;
  1322.     for (i = 0; i < cug; i++) {
  1323.       if (strcmpi(buf, grouprec[i].groupname) == 0) {
  1324.         output("\n ■ Skipping message - already posted in %s.",
  1325.                 grouprec[i].groupname);
  1326.         return 0;
  1327.       }
  1328.     }
  1329.     if ((crossposts > 0) && (max > crossposts)) {
  1330.       output("\n ■ Skipping message - crossposted to more than %hu newsgroups.",
  1331.           crossposts);
  1332.       return 0;
  1333.     }
  1334.   }
  1335.   return 1;
  1336. }
  1337.  
  1338. void good_name(char *name)
  1339. {
  1340.   char *ss;
  1341.   int i;
  1342.  
  1343.   trimstr1(name);
  1344.   if ((strcspn(name, " ") == strlen(name)) && (strcspn(name, "(") == strlen(name)))
  1345.     if (_fstrchr(name, '@') != NULL)
  1346.       return;
  1347.   if (strcspn(name, "<") != strlen(name)) {
  1348.     ss = strtok(name, "<");
  1349.     if (ss)
  1350.       ss = strtok(NULL, ">");
  1351.     strcpy(name, ss);
  1352.     if (name[0] == 0)
  1353.       strcpy(name, "Unknown");
  1354.     return;
  1355.   }
  1356.   if ((i = strcspn(name, "(")) != strlen(name)) {
  1357.     if (name[i - 1] == 32) {
  1358.       ss = strtok(name, " ");
  1359.       if (ss)
  1360.         strcpy(name, ss);
  1361.     } else {
  1362.       ss = strtok(name, "(");
  1363.       if (ss)
  1364.         strcpy(name, ss);
  1365.     }
  1366.     if (name[0] == 0)
  1367.       strcpy(name, "Unknown");
  1368.     return;
  1369.   }
  1370. }
  1371.  
  1372. int postnews(Mail_Socket * NNTP_sock, int cug)
  1373. {
  1374.   char s[181], s1[12], s2[5], fn[201], *ss;
  1375.   int f1, nlines, len, tlen;
  1376.   FILE *fp;
  1377.   static char _temp_buffer[STR];
  1378.   struct ffblk ff;
  1379.  
  1380.   sprintf(fn, "%s\\OUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
  1381.   f1 = findfirst(fn, &ff, 0);
  1382.   if (f1 != 0) {
  1383.     log_it(0, "\n ■ No outbound news articles to post...");
  1384.     return 1;
  1385.   }
  1386.   while (f1 == 0) {
  1387.     sock_printf(NNTP_sock->sock, "POST");
  1388.     sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
  1389.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1390.     if (*_temp_buffer != '3') {
  1391.       if (atoi(_temp_buffer) == 440)
  1392.         log_it(1, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
  1393.       else
  1394.         log_it(0, "\n ■ Remote error: %s", _temp_buffer);
  1395.       return 0;
  1396.     }
  1397.     output("\n ■ Posting article to %s\n", grouprec[cug].groupname);
  1398.     sprintf(s, "%s\\OUTBOUND\\%s", net_data, ff.ff_name);
  1399.     fp = fsh_open(s, "rb");
  1400.     nlines = 0;
  1401.     while (fgets(_temp_buffer, 120, fp) != NULL) {
  1402.       if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
  1403.         _temp_buffer[1] = '.';
  1404.         _temp_buffer[2] = 0;
  1405.       }
  1406.       tlen = 0;
  1407.       len = strlen(_temp_buffer);
  1408.       while (tlen < len) {
  1409.         if (!tcp_tick(NNTP_sock->sock)) {
  1410.           if (fp != NULL)
  1411.             fclose(fp);
  1412.           output("\n ■ Connection reset by host");
  1413.           return 1;
  1414.         }
  1415.         tlen += sock_fastwrite(NNTP_sock->sock, &_temp_buffer[tlen], len - tlen);
  1416.       }
  1417.       backline();
  1418.       output("\r ■ Lines sent : %d", ++nlines);
  1419.     }
  1420.     if (fp != NULL)
  1421.       fclose(fp);
  1422.     sock_printf(NNTP_sock->sock, ".\n");
  1423.     output("\n ■ Awaiting acknowledgement - may take several minutes...");
  1424.     log_it(0, "\n ■ Server response : ");
  1425.     sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
  1426.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1427.     log_it(0, "%s", _temp_buffer);
  1428.     if (*_temp_buffer != '2') {
  1429.       if (atoi(_temp_buffer) == 441) {
  1430.         ss = strtok(_temp_buffer, " ");
  1431.         ss = strtok(NULL, " ");
  1432.         if (atoi(ss) == 435)
  1433.           unlink(s);
  1434.         fnsplit(s, NULL, NULL, s1, s2);
  1435.         log_it(0, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
  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_sock);
  1445.   return 0;
  1446. }
  1447.  
  1448. void get_subtype(int sub, char *where)
  1449. {
  1450.   int ok;
  1451.   char fn[181], s[81], net_name[21], *ss;
  1452.   FILE *fp;
  1453.  
  1454.   where[0] = 0;
  1455.   sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
  1456.   if ((fp = fsh_open(fn, "r")) == NULL)
  1457.     return;
  1458.   ok = 0;
  1459.   while (fgets(s, 80, fp)) {
  1460.     if (s[0] == '!') {
  1461.       if (sub == atoi(&s[1]))
  1462.         ok = 1;
  1463.     }
  1464.     if (ok && (s[0] == '$')) {
  1465.       ss = strtok(s, " ");
  1466.       strcpy(net_name, &ss[1]);
  1467.       ss = strtok(NULL, " ");
  1468.       if (fp != NULL)
  1469.         fclose(fp);
  1470.       if ((stricmp(net_name, "FILENET") == 0)) {
  1471.         trimstr1(ss);
  1472.         strcpy(where, ss);
  1473.         return;
  1474.       } else
  1475.         return;
  1476.     }
  1477.   }
  1478.   if (fp != NULL)
  1479.     fclose(fp);
  1480. }
  1481.  
  1482. unsigned int max_on_sub(int cug)
  1483. {
  1484.   int i, sub, f, max_subs, num_subs;
  1485.   char s[121], subtype[12];
  1486.   unsigned int max;
  1487.  
  1488.   if ((strlen(grouprec[cug].subtype) == 1) &&
  1489.       (grouprec[cug].subtype[0] == '0'))
  1490.     return 0;
  1491.   max_subs = syscfg.max_subs;
  1492.   subboards = (subboardrec *) mallocx(max_subs * sizeof(subboardrec));
  1493.   if (!subboards) {
  1494.     output("\b\b\b\bUnable to allocate %ld bytes.",
  1495.             max_subs * sizeof(subboardrec));
  1496.     return 0;
  1497.   }
  1498.   sprintf(s, "%sSUBS.DAT", syscfg.datadir);
  1499.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1500.   if (f < 0) {
  1501.     output("\n ■ %s NOT FOUND", s);
  1502.     return 0;
  1503.   }
  1504.   num_subs = (sh_read(f, subboards, (max_subs * sizeof(subboardrec)))) /
  1505.       sizeof(subboardrec);
  1506.   f = sh_close(f);
  1507.   for (i = 0; i < num_subs; i++) {
  1508.     sub = i;
  1509.     output("\b\b\b\b%-4d", sub);
  1510.     get_subtype(sub, subtype);
  1511.     if (stricmp(subtype, grouprec[cug].subtype) == 0) {
  1512.       max = subboards[i].maxmsgs;
  1513.       output("\b\b\b\b%s - %hu posts.", subtype, max);
  1514.       if (subboards)
  1515.         free(subboards);
  1516.       return max;
  1517.     }
  1518.   }
  1519.   if (subboards)
  1520.     free(subboards);
  1521.   return 0;
  1522. }
  1523.  
  1524. int getnews(char *hostname)
  1525. {
  1526.   int f1, nup, nug, cug, abort, ok, done, firstrun, max_articles, parts;
  1527.   char *p, fn[201], mailname[121], reline[121], s[21], s1[21], pktname[121];
  1528.   char orig_subj[STRING];
  1529.   unsigned int text_len;
  1530.   unsigned short temptype;
  1531.   unsigned long new_articles;
  1532.   net_header_rec nh;
  1533.   struct date dt;
  1534.   struct time tm;
  1535.   struct ffblk ff;
  1536.   static FILE *fp;
  1537.   Mail_Socket *nntp_sock;
  1538.   static char _temp_buffer[STR];
  1539.  
  1540.   abort = cug = 0;
  1541.   sock_init();
  1542.   nntp_sock = netsocket(hostname);
  1543.  
  1544.   if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
  1545.     grouprec[0].groupname[0] = 0;
  1546.     write_groups(0);
  1547.     if (grouprec)
  1548.       free(grouprec);
  1549.     sprintf(fn, "%s\\NEWSRC", net_data);
  1550.     log_it(0, "\n ■ Creating newsgroup list : %s...         ", fn);
  1551.     if (saveactive(nntp_sock, fn) == 1) {
  1552.       if (sendauthinfo(nntp_sock) == 0)
  1553.         saveactive(nntp_sock, fn);
  1554.     }
  1555.     nntp_shutdown(nntp_sock);
  1556.     free_Mail_Socket(nntp_sock);
  1557.     return 0;
  1558.   }
  1559.   nug = nup = 1;
  1560.   while ((cug < ngroups) && (!abort)) {
  1561.     if (nup) {
  1562.       nup = 0;
  1563.       if (grouprec[cug].subtype) {
  1564.         if (fp != NULL)
  1565.           fclose(fp);
  1566.         write_groups(0);
  1567.         name_packet(pktname);
  1568.         if ((fp = fsh_open(pktname, "wb")) == NULL) {
  1569.           log_it(0, "\n ■ Unable to create %s!", pktname);
  1570.           qexit(nntp_sock, 1);
  1571.         } else {
  1572.           fnsplit(pktname, NULL, NULL, s, s1);
  1573.           output("\n ■ Creating new packet : %s%s", s, s1);
  1574.         }
  1575.       } else {
  1576.         if (fp != NULL)
  1577.           fclose(fp);
  1578.       }
  1579.     }
  1580.     if (nug) {
  1581.       cur_article = ++grouprec[cug].lastread;
  1582.       nug = 0;
  1583.       while (1) {
  1584.         if (cgroup(nntp_sock, grouprec[cug].groupname)) {
  1585.           if (cgroup(nntp_sock, grouprec[cug].groupname) == 2) {
  1586.             if (sendauthinfo(nntp_sock) == 0)
  1587.               continue;
  1588.             else
  1589.               return 1;
  1590.           }
  1591.           log_it(1, "\n ■ Error accessing newsgroup \"%s\".",
  1592.                  grouprec[cug].groupname);
  1593.           if (++bogus >= 10) {
  1594.             log_it(0, "\n ■ More than 10 invalid newsgroups... possible NEWS.RC error!");
  1595.             qexit(nntp_sock, 10);
  1596.           }
  1597.           nug = 1;
  1598.           break;
  1599.         } else
  1600.           break;
  1601.       }
  1602.       if (!nug) {
  1603.         log_it(0, "\n ■ Requesting %s... ", grouprec[cug].groupname);
  1604.         if (grouprec[cug].lastread < cur_first)
  1605.           grouprec[cug].lastread = cur_first;
  1606.         if (grouprec[cug].lastread > cur_last) {
  1607.           grouprec[cug].lastread = cur_last;
  1608.           log_it(0, "no new articles.");
  1609.           nug = 1;
  1610.         } else {
  1611.           new_articles = cur_last - grouprec[cug].lastread + 1;
  1612.           log_it(0, "%lu new article%s.        ",
  1613.                  cur_last - grouprec[cug].lastread + 1,
  1614.                  new_articles == 1 ? "" : "s");
  1615.           if (new_articles > 250) {
  1616.             output("\n ■ Maximum posts allowed on subtype     ");
  1617.             max_articles = max_on_sub(cug);
  1618.             if (max_articles && (new_articles > max_articles)) {
  1619.               log_it(0, "\n ■ Requesting most recent %d articles", max_articles);
  1620.               grouprec[cug].lastread = (cur_last - max_articles);
  1621.             }
  1622.           }
  1623.         }
  1624.         postnews(nntp_sock, cug);
  1625.         if (!nug) {
  1626.           if (cur_numa == 0) {
  1627.             log_it(0, "\n ■ No articles available in %s...",
  1628.                    grouprec[cug].groupname);
  1629.             nug = 1;
  1630.             continue;
  1631.           } else {
  1632.             if (cstat(nntp_sock, grouprec[cug].lastread) &&
  1633.                 cstat(nntp_sock, cur_last))
  1634.               nug = 1;
  1635.           }
  1636.         }
  1637.       }
  1638.     } else {
  1639.       if (cnext(nntp_sock)) {
  1640.         output("\n ■ End of new articles in %s",
  1641.                 grouprec[cug].groupname);
  1642.         nug = 1;
  1643.         write_groups(1);
  1644.       }
  1645.     }
  1646.     if (kbhit()) {
  1647.       if (getch() == 27) {
  1648.         log_it(0,"\n ■ Aborting news retrieval session...");
  1649.         if (fp != NULL)
  1650.           fclose(fp);
  1651.         nntp_shutdown(nntp_sock);
  1652.         free_Mail_Socket(nntp_sock);
  1653.         return 1;
  1654.       }
  1655.     }
  1656.     if (!nug) {
  1657.       if ((chead(nntp_sock)) && (checkx(cug))) {
  1658.         if (cur_subject[0]) {
  1659.           treat(cur_subject);
  1660.           if (strlen(cur_subject) > 65)
  1661.             cur_subject[65] = '\0';
  1662.         } else
  1663.           strcpy(cur_subject, "No subject");
  1664.         strcpy(orig_subj, cur_subject);
  1665.         if (cur_from[0]) {
  1666.           treat(cur_from);
  1667.           if (strlen(cur_from) > 45)
  1668.             cur_from[45] = 0;
  1669.         } else
  1670.           strcpy(cur_from, "Unknown");
  1671.         if (cur_replyto[0]) {
  1672.           if (strlen(cur_replyto) > 45)
  1673.             cur_replyto[45] = 0;
  1674.         }
  1675.         strncpy(_temp_buffer, orig_subj, 55);
  1676.         _temp_buffer[55] = '\0';
  1677.         output("\n ■ [%lu/%lu] : %s ", cur_article,
  1678.                 cur_last, _temp_buffer);
  1679.         ok = savebody(nntp_sock, cug, &abort);
  1680.         if (ok && grouprec[cug].subtype) {
  1681.           strcpy(msgarea, grouprec[cug].groupname);
  1682.           sprintf(msgto, "All");
  1683.           sprintf(fn, "%s\\INPUT*.MSG", tmpdir);
  1684.           done = 1;
  1685.           parts = 0;
  1686.           if (findfirst(fn, &ff, 0) == 0) {
  1687.             parts = 1;
  1688.             done = 0;
  1689.           }
  1690.           firstrun = 1;
  1691.           while (!done) {
  1692.             sprintf(fn, "%s\\%s", tmpdir, ff.ff_name);
  1693.             f1 = sh_open1(fn, O_RDONLY | O_BINARY);
  1694.             text_len = (unsigned int) filelength(f1);
  1695.             if (text_len > 32000L) {
  1696.               output("\n ■ Truncating %lu bytes from input file",
  1697.                       text_len - 32000L);
  1698.               text_len = 32000;
  1699.             }
  1700.             p = (char *) malloca(32767);
  1701.             if (!p) {
  1702.               log_it(0, "\n ■ Insufficient memory to read entire message");
  1703.               qexit(nntp_sock, 1);
  1704.             }
  1705.             sh_read(f1, (void *) p, text_len);
  1706.             sh_close(f1);
  1707.             temptype = atoi(grouprec[cug].subtype);
  1708.             nh.tosys = sy;
  1709.             nh.touser = 0;
  1710.             nh.fromsys = 32767;
  1711.             nh.fromuser = 0;
  1712.             if (!temptype) {
  1713.               nh.main_type = main_type_new_post;
  1714.               nh.minor_type = 0;
  1715.             } else {
  1716.               nh.main_type = main_type_pre_post;
  1717.               nh.minor_type = temptype;
  1718.             }
  1719.             nh.list_len = 0;
  1720.             gettime(&tm);
  1721.             getdate(&dt);
  1722.             nh.daten = dostounix(&dt, &tm);
  1723.             nh.method = 0;
  1724.             if (parts > 1) {
  1725.               sprintf(_temp_buffer, "%d%s ", parts, ordinal_text(parts));
  1726.               strcat(_temp_buffer, orig_subj);
  1727.               strcpy(cur_subject, _temp_buffer);
  1728.               if (strlen(cur_subject) > 72)
  1729.                 cur_subject[72] = '\0';
  1730.               else
  1731.                 cur_subject[strlen(cur_subject)] = '\0';
  1732.             }
  1733.             nh.length = text_len + strlen(cur_subject) + 1;
  1734.             strncpy(mailname, cur_replyto, 46);
  1735.             strcat(mailname, "\r\n");
  1736.             nh.length += strlen(mailname);
  1737.             nh.length += 24 + 2;
  1738.             if (nh.main_type == main_type_new_post)
  1739.               nh.length += strlen(grouprec[cug].subtype) + 1;
  1740.             if (firstrun) {
  1741.               firstrun = 0;
  1742.               if (strncmpi(cur_articleid, "re: ", 4) == 0) {
  1743.                 strncpy(reline, cur_articleid, 60);
  1744.                 sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
  1745.               }
  1746.             }
  1747.             nh.length += strlen(cur_articleid);
  1748.             fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  1749.             if (nh.main_type == main_type_new_post)
  1750.               fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
  1751.             fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
  1752.             fsh_write(mailname, sizeof(char), strlen(mailname), fp);
  1753.             strncpy(_temp_buffer, ctime(&(time_t) nh.daten), 24);
  1754.             _temp_buffer[24] = '\0';
  1755.             strcat(_temp_buffer, "\r\n");
  1756.             fsh_write(_temp_buffer, sizeof(char), strlen(_temp_buffer), fp);
  1757.             sprintf(_temp_buffer, "%s\n\n", cur_articleid);
  1758.             fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
  1759.             fsh_write(p, sizeof(char), text_len, fp);
  1760.             if (p)
  1761.               free(p);
  1762.             unlink(fn);
  1763.             if (findnext(&ff) == 0) {
  1764.               done = 0;
  1765.               ++parts;
  1766.             } else
  1767.               done = 1;
  1768.           }
  1769.         }
  1770.         grouprec[cug].lastread = cur_article;
  1771.       } else
  1772.         grouprec[cug].lastread = cur_article + 1;
  1773.       if ((filelength(fileno(fp)) > 250000L) && grouprec[cug].subtype)
  1774.         nup = 1;
  1775.       if (abort == 2) {
  1776.         write_groups(1);
  1777.         nug = 1;
  1778.         abort = 0;
  1779.       }
  1780.     }
  1781.     if (nug) {
  1782.       ++cug;
  1783.       if (((strlen(grouprec[cug].subtype) == 1) &&
  1784.            (grouprec[cug].subtype[0] == '0')) && (grouprec[cug].subtype)) {
  1785.         nup = 1;
  1786.       } else {
  1787.         if ((grouprec[cug - 1].subtype) &&
  1788.             ((strlen(grouprec[cug].subtype) == 1) &&
  1789.              (grouprec[cug].subtype[0] == '0'))) {
  1790.           if (fp != NULL)
  1791.             fclose(fp);
  1792.         }
  1793.       }
  1794.     }
  1795.   }
  1796.   if (fp != NULL)
  1797.     fclose(fp);
  1798.   if (abort)
  1799.     log_it(0, "\n ■ Session aborted from keyboard");
  1800.   write_groups(1);
  1801.   nntp_shutdown(nntp_sock);
  1802.   free_Mail_Socket(nntp_sock);
  1803.   return 0;
  1804. }
  1805.  
  1806. void main(int argc, char *argv[])
  1807. {
  1808.   int f, done;
  1809.   char s[201], fn[MAXPATH], *ss;
  1810.   FILE *fp;
  1811.   struct tm *time_now;
  1812.   time_t some;
  1813.  
  1814.   cursor('S');
  1815.   cursor('H');
  1816.   output("\n ■ %s", version);
  1817.   if (argc != 4)
  1818.     err_exit(EXIT_FAILURE, "\n ■ Invalid arguments for %s", argv[0]);
  1819.   ss = getenv("WWIV_INSTANCE");
  1820.   if (ss) {
  1821.     instance = atoi(ss);
  1822.     if ((instance <= 0) || (instance >= 1000)) {
  1823.       log_it(0, "\n ■ WWIV_INSTANCE set to %d.  Can only be 1..999!", instance);
  1824.       instance = 1;
  1825.     }
  1826.   } else
  1827.     instance = 1;
  1828.   SOCK_DELAY = 120;
  1829.   get_dir(maindir, 0);
  1830.   strcpy(net_data, argv[1]);
  1831.   strcpy(serverhost, argv[2]);
  1832.   detect_multitask();
  1833.   time(&some);
  1834.   time_now = localtime(&some);
  1835.   strftime(s, 80, "\n\nNEWS session beginning on %A, %B %d, %Y at %H:%M %p",
  1836.            time_now);
  1837.   sprintf(fn, "%s\\NEWS.LOG", net_data);
  1838.   if ((fp = fsh_open(fn, "at")) != NULL) {
  1839.     fprintf(fp, s);
  1840.     fclose(fp);
  1841.   }
  1842.   sprintf(tmpdir, "%s\\SPOOL", net_data);
  1843.   sy = atoi(argv[3]);
  1844.   sprintf(s, "%s\\CONFIG.DAT", maindir);
  1845.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1846.   if (f < 0)
  1847.     err_exit(EXIT_FAILURE, "\n ■ %s NOT FOUND.", s);
  1848.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1849.   sh_close(f);
  1850.   sprintf(newsrc, "%s\\NEWS.RC", net_data);
  1851.   read_groups();
  1852.   if (ngroups == 0)
  1853.     err_exit(EXIT_FAILURE, "\n ■ Unable to access newsgroup file: %s!", newsrc);
  1854.   if (stricmp(grouprec[0].groupname, "newsrc") == 0)
  1855.     log_it(0, "\n ■ Retrieving current newsgroup listing from %s",
  1856.            serverhost);
  1857.   else
  1858.     log_it(0, "\n ■ %u newsgroup%s defined in %s", ngroups,
  1859.            ngroups == 1 ? "" : "s", strlwr(newsrc));
  1860.   crossposts = 10;
  1861.   sprintf(fn, "%s\\NET.INI", maindir);
  1862.   if ((fp = fsh_open(fn, "rt")) == NULL)
  1863.     output("\n ■ Unable to read %s", fn);
  1864.   else {
  1865.     done = 0;
  1866.     while ((fgets(s, 80, fp)) && (!done)) {
  1867.       ss = NULL;
  1868.       stripspace(s);
  1869.       if ((s[0] == ';') || (s[0] == '\n') || (s[0]== '['))
  1870.         continue;
  1871.       if (strlen(s) == 0)
  1872.         continue;
  1873.       if (strnicmp(s, "XPOSTS", 6) == 0) {
  1874.         ss = strtok(s, "=");
  1875.         if (ss) {
  1876.           ss = strtok(NULL, "\n");
  1877.           trimstr1(ss);
  1878.           crossposts = atoi(ss);
  1879.           if (crossposts > 99)
  1880.             crossposts = 10;
  1881.           done = 1;
  1882.         }
  1883.       }
  1884.     }
  1885.   }
  1886.   if (getnews(serverhost))
  1887.     err_exit(EXIT_FAILURE, "\n ■ Exiting program.");
  1888.   if (grouprec)
  1889.     free(grouprec);
  1890.   grouprec = NULL;
  1891.   cd_to(maindir);
  1892.   log_it(0, "\n ■ NEWS succesfully completed processing %d newsgroups", ngroups);
  1893.   trim_log();
  1894.   err_exit(EXIT_SUCCESS, "\n ■ Normal program termination");
  1895. }
  1896.