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