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