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