home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B90.ZIP / NEWS.CPP < prev    next >
Text File  |  1998-05-31  |  59KB  |  2,164 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 <malloc.h>
  16. #include <mem.h>
  17. #include <sys\stat.h>
  18. #include <math.h>
  19.  
  20. extern "C" {
  21.   #include "tcp.h"
  22. }
  23.  
  24. #include "net.h"
  25. #include "vardec.h"
  26. #include "version.h"
  27. #include "retcode.h"
  28.  
  29. #define SIZE_OF_EMULATOR 415
  30. #define FILL_CHAR 0xFF
  31.  
  32. extern unsigned _stklen = 12000U;
  33.  
  34. #define SHARE_LEVEL 10
  35. #define WAIT_TIME 10
  36. #define TRIES 100
  37.  
  38. struct ts_os_ver {
  39.   int maj;
  40.   int min;
  41. };
  42.  
  43. #define DOS     0
  44. #define OS2     1
  45. #define DV      2
  46. #define WINS    3
  47. #define WIN3    4
  48.  
  49. #define MT_DOS  0x01
  50. #define MT_OS2  0x02
  51. #define MT_DV   0x04
  52. #define MT_WINS 0x08
  53. #define MT_WIN3 0x10
  54.  
  55. struct ts_os_ver t_os_ver[5];
  56. int t_os_type;
  57. int t_os;
  58. char t_os_name[41];
  59.  
  60. #define BIGSTR 2048
  61. #define STRING 513
  62. #define STR 129
  63. #define WAIT_TIME 10
  64. #define TRIES 100
  65. #define NUL '\0'
  66. #define LAST(s) s[strlen(s)-1]
  67. #define NNTP_PORT 119
  68.  
  69. typedef struct {
  70.   tcp_Socket *sock;
  71. } Mail_Socket;
  72.  
  73. configrec syscfg;
  74.  
  75. char *version = "Freeware PPP Project News Retrieval " VERSION;
  76.  
  77. static char _temp_buffer[STRING];
  78. char POPNAME[25], POPDOMAIN[45];
  79. char cur_path[STRING], cur_from[STRING], cur_subject[STRING], cur_replyto[STRING];
  80. char cur_newsgroups[STRING], cur_message_ID[STRING], cur_organization[STRING];
  81. char cur_articleid[STRING], cur_references[STRING], cur_lines[STR], cur_date[STRING];
  82. static char maindir[MAXPATH], net_data[MAXPATH];
  83. int instance, crossposts, newsrc_upd, binxpost, MOREINFO;
  84. static int spooltodisk, NNTP_stat;
  85. unsigned long cur_daten, reparticle, cur_numa, cur_first, cur_last;
  86. unsigned short reply, sy;
  87. static char NEWSNAME[STRING], NEWSPASS[STRING];
  88.  
  89. typedef struct {
  90.   char groupname[60];
  91.   unsigned long lastread;
  92.   char subtype[8];
  93. } GROUPFILEREC;
  94.  
  95. GROUPFILEREC *grouprec;
  96. int ngroups;
  97.  
  98. #define SOCK_READ_ERR(PROTOCOL) sock_err:                                 \
  99.   switch (PROTOCOL##_stat) {                                              \
  100.     case 1 :                                                              \
  101.       fprintf(stderr, "\n ! Session error : %s",                          \
  102.           sockerr(PROTOCOL##_sock->sock));                                \
  103.       if (grouprec != NULL) write_groups(1);                              \
  104.       if (PROTOCOL##_sock->sock != NULL)                                  \
  105.         farfree(PROTOCOL##_sock->sock);                                   \
  106.       if (PROTOCOL##_sock != NULL)                                        \
  107.         farfree(PROTOCOL##_sock);                                         \
  108.       fcloseall();                                                        \
  109.       cursor('R');                                                        \
  110.       exit(EXIT_FAILURE);                                                 \
  111.     case -1:                                                              \
  112.       fprintf(stderr, "\n ! Timeout : %s",                                \
  113.           sockerr(PROTOCOL##_sock->sock));                                \
  114.       if (grouprec != NULL) write_groups(1);                              \
  115.       if (PROTOCOL##_sock->sock != NULL)                                  \
  116.         farfree(PROTOCOL##_sock->sock);                                   \
  117.       if (PROTOCOL##_sock != NULL)                                        \
  118.         farfree(PROTOCOL##_sock);                                         \
  119.       fcloseall();                                                        \
  120.       cursor('R');                                                        \
  121.       exit(EXIT_FAILURE);                                                 \
  122.   }                                                                       \
  123.  
  124. char *texth[] = {"th", "st", "nd", "rd"};
  125.  
  126. char *ordinal_text(int number)
  127. {
  128.   if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
  129.     number = 0;
  130.   return texth[number];
  131. }
  132.  
  133. static unsigned cursize;
  134.  
  135. void cursor(int tmp)
  136. {
  137.   union REGS inregs, outregs;
  138.  
  139.   switch (toupper(tmp)) {
  140.     case 'S':                               /* Save */
  141.       inregs.h.ah = 3;
  142.       inregs.h.bh = 0;
  143.       int86(0x10, &inregs, &outregs);
  144.       cursize = outregs.x.cx;
  145.       break;
  146.     case 'R':                               /* Restore */
  147.       inregs.h.ah = 1;
  148.       inregs.x.cx = cursize;
  149.       int86(0x10, &inregs, &outregs);
  150.       break;
  151.     case 'H':                               /* Hide */
  152.       inregs.h.ah = 1;
  153.       inregs.h.ch = 0x20;
  154.       int86(0x10, &inregs, &outregs);
  155.       break;
  156.     case 'N':                               /* Normal */
  157.       inregs.h.ah = 1;
  158.       inregs.h.ch = 6;
  159.       inregs.h.cl = 7;
  160.       int86(0x10, &inregs, &outregs);
  161.       break;
  162.   }
  163. }
  164.  
  165. int detect_multitask(void)
  166. {
  167.   union REGS t_regs;
  168.  
  169.   t_os_type = 0;
  170.   t_os = 0;
  171.  
  172.   if (_osmajor < 10) {
  173.     t_os_ver[DOS].maj = _osmajor;
  174.     t_os_ver[DOS].min = _osminor;
  175.     t_os_type = t_os_type | MT_DOS;
  176.     strcpy(t_os_name, "DOS");
  177.   } else {
  178.     t_os_type = t_os_type | MT_OS2;
  179.     t_os_ver[OS2].maj = _osmajor / 10;
  180.     t_os_ver[OS2].min = _osminor;
  181.     if (t_os_ver[OS2].maj == 3) {
  182.       strcpy(t_os_name, "OS/2 Warp");
  183.     } else {
  184.       strcpy(t_os_name, "OS/2");
  185.     }
  186.   }
  187.  
  188.   t_regs.x.ax = 0x4680;
  189.   int86(0x2F, &t_regs, &t_regs);
  190.  
  191.   if (t_regs.x.ax == 0x0000) {
  192.     t_os_ver[WINS].maj = 3;
  193.     t_os_ver[WINS].min = 0;
  194.     t_os_type = t_os_type | MT_WINS;
  195.   } else {
  196.     t_regs.x.ax = 0x1600;
  197.     int86(0x2F, &t_regs, &t_regs);
  198.     switch (t_regs.h.al) {
  199.       case 0x00:
  200.       case 0x80:
  201.       case 0x01:
  202.       case 0xFF:
  203.         break;
  204.       default:
  205.         t_os_type = t_os_type | MT_WIN3;
  206.         t_os_ver[WIN3].maj = t_regs.h.al;
  207.         t_os_ver[WIN3].min = t_regs.h.ah;
  208.         if (t_os_ver[WIN3].maj == 4) {
  209.           strcpy(t_os_name, "Windows 95");
  210.           t_os_ver[WIN3].maj = t_os_ver[WIN3].maj - 3;
  211.         } else {
  212.           strcpy(t_os_name, "Windows");
  213.         }
  214.         break;
  215.     }
  216.   }
  217.  
  218.   t_regs.x.cx = 0x4445;
  219.   t_regs.x.dx = 0x5351;
  220.   t_regs.x.ax = 0x2B01;
  221.  
  222.   intdos(&t_regs, &t_regs);
  223.   if (t_regs.h.al != 0xFF) {
  224.     t_os_type = t_os_type | MT_DV;
  225.     t_os_ver[DV].maj = t_regs.h.bh;
  226.     t_os_ver[DV].min = t_regs.h.bl;
  227.     strcpy(t_os_name, "DESQview");
  228.   }
  229.   if (t_os_type & MT_DOS)
  230.     t_os = DOS;
  231.   if (t_os_type & MT_DV)
  232.     t_os = DV;
  233.   if (t_os_type & MT_WINS)
  234.     t_os = WINS;
  235.   if (t_os_type & MT_WIN3)
  236.     t_os = WIN3;
  237.   if (t_os_type & MT_OS2)
  238.     t_os = OS2;
  239.   return (t_os - 1);
  240. }
  241.  
  242. void giveup_timeslice(void)
  243. {
  244.   union REGS t_regs;
  245.  
  246.   switch (t_os) {
  247.     case DOS:
  248.       break;
  249.     case OS2:
  250.     case WIN3:
  251.     case WINS:
  252.       t_regs.x.ax = 0x1680;
  253.       int86(0x2f, &t_regs, &t_regs);
  254.       break;
  255.     case DV:
  256.       t_regs.x.ax = 0x1000;
  257.       int86(0x15, &t_regs, &t_regs);
  258.       break;
  259.   }
  260. }
  261.  
  262.  
  263. void output(char *fmt,...)
  264. {
  265.   va_list v;
  266.   char s[255];
  267.  
  268.   va_start(v, fmt);
  269.   vsprintf(s, fmt, v);
  270.   va_end(v);
  271.   fputs(s, stderr);
  272. }
  273.  
  274. void backline(void)
  275. {
  276.   int i;
  277.  
  278.   output(" ");
  279.   for (i = wherex(); i > 0; i--)
  280.     output("\b \b");
  281. }
  282.  
  283. void cd_to(char *s)
  284. {
  285.   char *s1;
  286.   int i, db;
  287.  
  288.   s1 = s;
  289.   i = strlen(s1) - 1;
  290.   db = (s1[i] == '\\');
  291.   if (i == 0)
  292.     db = 0;
  293.   if ((i == 2) && (s1[1] == ':'))
  294.     db = 0;
  295.   if (db)
  296.     s1[i] = 0;
  297.   chdir(s1);
  298.   if (s[1] == ':')
  299.     setdisk(s[0] - 'A');
  300. }
  301.  
  302. void get_dir(char *s, int be)
  303. {
  304.   strcpy(s, "X:\\");
  305.   s[0] = 'A' + getdisk();
  306.   getcurdir(0, s + 3);
  307.   if (be) {
  308.     if (s[strlen(s) - 1] != '\\')
  309.       strcat(s, "\\");
  310.   }
  311. }
  312.  
  313. int sh_write(int handle, void *buffer, unsigned long length)
  314. {
  315.   if (handle == -1) {
  316.     return (-1);
  317.   }
  318.   return (write(handle, buffer, (unsigned) length));
  319. }
  320.  
  321. int sh_open(char *path, int file_access, unsigned fmode)
  322. {
  323.   int handle, count, share;
  324.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  325.  
  326.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  327.     share = SH_DENYRW;
  328.   } else {
  329.     share = SH_DENYWR;
  330.   }
  331.   handle = open(path, file_access | share, fmode);
  332.   if (handle < 0) {
  333.     count = 1;
  334.     fnsplit(path, drive, dir, file, ext);
  335.     if (access(path, 0) != -1) {
  336.       delay(WAIT_TIME);
  337.       handle = open(path, file_access | share, fmode);
  338.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  339.         if (count % 2)
  340.           delay(WAIT_TIME);
  341.         else
  342.           giveup_timeslice();
  343.         count++;
  344.         handle = open(path, file_access | share, fmode);
  345.       }
  346.     }
  347.   }
  348.   return (handle);
  349. }
  350.  
  351. int sh_open1(char *path, int access)
  352. {
  353.   unsigned fmode;
  354.  
  355.   fmode = 0;
  356.   if ((access & O_RDWR) || (access & O_WRONLY))
  357.     fmode |= S_IWRITE;
  358.   if ((access & O_RDWR) || (access & O_RDONLY))
  359.     fmode |= S_IREAD;
  360.   return (sh_open(path, access, fmode));
  361. }
  362.  
  363. int sh_close(int f)
  364. {
  365.   if (f != -1)
  366.     close(f);
  367.   return (-1);
  368. }
  369.  
  370. int sh_read(int handle, void *buf, unsigned length)
  371. {
  372.   if (handle == -1) {
  373.     return (-1);
  374.   }
  375.   return (read(handle, buf, length));
  376. }
  377.  
  378. long sh_lseek(int handle, long offset, int fromwhere)
  379. {
  380.   if (handle == -1) {
  381.     return (-1L);
  382.   }
  383.   return (lseek(handle, offset, fromwhere));
  384. }
  385.  
  386. FILE *fsh_open(char *path, char *fmode)
  387. {
  388.   FILE *f;
  389.   int count, share, md, fd;
  390.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  391.  
  392.   share = SH_DENYWR;
  393.   md = 0;
  394.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  395.     share = SH_DENYRD;
  396.     md = O_RDWR | O_CREAT | O_TRUNC;
  397.   } else
  398.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  399.     share = SH_DENYRD;
  400.     md = O_RDWR | O_CREAT;
  401.   } else {
  402.     md = O_RDONLY;
  403.   }
  404.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  405.     md |= O_BINARY;
  406.   }
  407.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  408.     md &= ~O_RDONLY;
  409.     md |= O_RDWR;
  410.     share = SH_DENYRD;
  411.   }
  412.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  413.   if (fd < 0) {
  414.     count = 1;
  415.     fnsplit(path, drive, dir, file, ext);
  416.     if ((access(path, 0)) != -1) {
  417.       delay(WAIT_TIME);
  418.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  419.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  420.         delay(WAIT_TIME);
  421.         count++;
  422.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  423.       }
  424.     }
  425.   }
  426.   if (fd > 0) {
  427.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  428.       sh_lseek(fd, 0L, SEEK_END);
  429.     f = fdopen(fd, fmode);
  430.     if (!f) {
  431.       close(fd);
  432.     }
  433.   } else
  434.     f = 0;
  435.   return (f);
  436. }
  437.  
  438. size_t fsh_write(void *ptr, size_t size, size_t n, FILE * stream)
  439. {
  440.  
  441.   if (stream == NULL) {
  442.     return (0);
  443.   }
  444.   return (fwrite(ptr, size, n, stream));
  445. }
  446.  
  447. char *stristr(char *string, char *pattern)
  448. {
  449.   char *pptr, *sptr, *start;
  450.   unsigned int slen, plen;
  451.  
  452.   for (start = string, pptr = pattern, slen = strlen(string),
  453.        plen = strlen(pattern); slen >= plen; start++, slen--) {
  454.     while (toupper(*start) != toupper(*pattern)) {
  455.       start++;
  456.       slen--;
  457.       if (slen < plen)
  458.         return (NULL);
  459.     }
  460.     sptr = start;
  461.     pptr = pattern;
  462.     while (toupper(*sptr) == toupper(*pptr)) {
  463.       sptr++;
  464.       pptr++;
  465.       if ('\0' == *pptr)
  466.         return (start);
  467.     }
  468.   }
  469.   return (NULL);
  470. }
  471.  
  472. unsigned char *trimstr1(unsigned char *s)
  473. {
  474.   int i;
  475.   static char *whitespace = " \r\n\t";
  476.  
  477.   i = strlen(s);
  478.   while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
  479.     --i;
  480.   while ((i > 0) && (_fstrchr(whitespace, *s))) {
  481.     memmove(s, s + 1, --i);
  482.   }
  483.   s[i] = 0;
  484.   return (s);
  485. }
  486.  
  487.  
  488.  
  489. int log_it(int display, char *fmt,...)
  490. {
  491.   va_list v;
  492.   char s[255], fn[MAXPATH];
  493.   FILE *fp;
  494.  
  495.   sprintf(fn, "%sNEWS.LOG", net_data);
  496.   if ((fp = fsh_open(fn, "at")) == NULL)
  497.     return 1;
  498.   va_start(v, fmt);
  499.   vsprintf(s, fmt, v);
  500.   va_end(v);
  501.   fputs(s, fp);
  502.   fclose(fp);
  503.   if (display)
  504.     fputs(s, stderr);
  505.   return 0;
  506. }
  507.  
  508.  
  509. int count_lines(char *s)
  510. {
  511.   FILE *fp;
  512.   char fn[201];
  513.   unsigned int nl = 0;
  514.   const int NEWLINE = '\n';
  515.   int c;
  516.  
  517.   strcpy(fn, s);
  518.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  519.     output("\n ■ Cannot open %s", fn);
  520.     return 0;
  521.   }
  522.   while ((c = getc(fp)) != EOF) {
  523.     if (c == NEWLINE)
  524.       ++nl;
  525.   }
  526.   fclose(fp);
  527.   return nl;
  528. }
  529.  
  530. int exist(char *s)
  531. {
  532.   int i;
  533.   struct ffblk ff;
  534.  
  535.   i = findfirst(s, &ff, 0);
  536.   if (i)
  537.     return (0);
  538.   else
  539.     return (1);
  540. }
  541.  
  542. void rename_pend(char *file)
  543. {
  544.   char s[181], s1[181];
  545.   int i, ok;
  546.  
  547.   sprintf(s, "%s%s", net_data, file);
  548.   ok = 0;
  549.   for (i = 0; i < 1000 && !ok; i++) {
  550.     sprintf(s1, "%sP0-%u.NET", net_data, i);
  551.     if (!exist(s1)) {
  552.       rename(s, s1);
  553.       ok = 1;
  554.     }
  555.   }
  556. }
  557.  
  558. void check_packets(void)
  559. {
  560.   char s[MAXPATH], s1[MAXPATH];
  561.   int f1;
  562.   struct ffblk ff;
  563.  
  564.   sprintf(s, "%sP0-*.%3.3d", net_data, instance);
  565.   f1 = findfirst(s, &ff, 0);
  566.   while (f1 == 0) {
  567.     if (ff.ff_fsize == 0L) {
  568.       sprintf(s1, "%s%s", net_data, ff.ff_name);
  569.       if (unlink(s1) != 0)
  570.         rename_pend(ff.ff_name);
  571.     } else
  572.       rename_pend(ff.ff_name);
  573.     f1 = findnext(&ff);
  574.   }
  575. }
  576.  
  577.  
  578. void write_groups(int display)
  579. {
  580.   int i;
  581.   char fn[160];
  582.   FILE *groupfp;
  583.  
  584.   sprintf(fn, "%sNEWS.RC", net_data);
  585.  
  586.   if ((groupfp = fsh_open(fn, "wt+")) == NULL) {
  587.     output("\n ■ Unable to open %s!", fn);
  588.     return;
  589.   } else {
  590.     if ((display) && (MOREINFO))
  591.       output("\n ■ Updating message pointers..");
  592.   }
  593.   for (i = 0; i < ngroups; i++)
  594.     if ((*grouprec[i].groupname) && (stricmp(grouprec[i].groupname, "newsrc") != 0))
  595.       fprintf(groupfp, "%s %lu %s\n", grouprec[i].groupname,
  596.               grouprec[i].lastread, grouprec[i].subtype);
  597.   if (groupfp != NULL)
  598.     fclose(groupfp);
  599. }
  600.  
  601. void nntp_shutdown(Mail_Socket * NNTP_sock)
  602. {
  603.   if (NNTP_sock->sock != NULL) {
  604.     sock_puts(NNTP_sock->sock, "QUIT");
  605.     sock_close(NNTP_sock->sock);
  606.     sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  607.   }
  608. sock_err:
  609.   return;
  610. }
  611.  
  612. void read_groups(void)
  613. {
  614.   int i = 0, ok;
  615.   unsigned short sn;
  616.   char *ss, fn[101], fn1[160], tmp[121];
  617.   FILE *groupfp;
  618.  
  619.   sprintf(fn1, "%sNEWS.RC", net_data);
  620.   ngroups = count_lines(fn1);
  621.   if (!ngroups)
  622.     return;
  623.   if (grouprec != NULL)
  624.     farfree(grouprec);
  625.   grouprec = NULL;
  626.   grouprec = (GROUPFILEREC *) farmalloc((ngroups + 1) * sizeof(GROUPFILEREC));
  627.   if (!grouprec) {
  628.     ngroups = 0;
  629.     output("\n ■ Insufficient memory for %d groups in %s.", ngroups, fn1);
  630.     return;
  631.   }
  632.   if ((groupfp = fsh_open(fn1, "rt")) == NULL) {
  633.     if (grouprec != NULL)
  634.       farfree(grouprec);
  635.     ngroups = 0;
  636.     return;
  637.   }
  638.   while (fgets(tmp, 120, groupfp)) {
  639.     if (*tmp) {
  640.       if (strncmpi(tmp, "newsrc", 6) == 0) {
  641.         strcpy(grouprec[i].groupname, "newsrc");
  642.         ++i;
  643.       } else {
  644.         ss = strtok(tmp, " ");
  645.         strcpy(grouprec[i].groupname, ss);
  646.         ss = strtok(NULL, " ");
  647.         if (ss) {
  648.           grouprec[i].lastread = atol(ss);
  649.           ss = strtok(NULL, " \n");
  650.           if (ss) {
  651.             strcpy(grouprec[i].subtype, strupr(ss));
  652.             ++i;
  653.           }
  654.         }
  655.       }
  656.     }
  657.   }
  658.   ngroups = i;
  659.   if (groupfp != NULL)
  660.     fclose(groupfp);
  661.   for (i = 0; i < ngroups; i++) {
  662.     if ((grouprec[i].subtype) && (atoi(grouprec[i].subtype) != 0)) {
  663.       sprintf(fn, "%sN%s.NET", net_data, grouprec[i].subtype);
  664.       if (exist(fn)) {
  665.         ok = 0;
  666.         if ((groupfp = fsh_open(fn, "rt")) != NULL) {
  667.           while ((fgets(tmp, 25, groupfp)) && !ok) {
  668.             sn = atoi(tmp);
  669.             if (sn == 32767)
  670.               ok = 1;
  671.           }
  672.           fclose(groupfp);
  673.         }
  674.         if (!ok)
  675.           log_it(1, "\n ■ @32767 not listed as a subscriber in N%s.NET!",
  676.                  grouprec[i].subtype);
  677.       }
  678.     }
  679.   }
  680. }
  681.  
  682. int sendauthinfo(Mail_Socket * NNTP_sock)
  683. {
  684.   char s[MAXPATH], buf[STR];
  685.  
  686.   *_temp_buffer = 0;
  687.  
  688.   log_it(MOREINFO, "\n ■ Server requested authentication information.");
  689.  
  690.   while (1) {
  691.     sprintf(buf, "authinfo user %s", NEWSNAME);
  692.     sock_printf(NNTP_sock->sock, buf);
  693.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  694.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  695.     sscanf(_temp_buffer, "%hu %s", &reply, s);
  696.     if (reply == 381) {
  697.       sprintf(buf, "authinfo pass %s", NEWSPASS);
  698.       sock_printf(NNTP_sock->sock, buf);
  699.       sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  700.       sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  701.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  702.       if (reply == 281) {
  703.         output("\n ■ Authentication accepted.  Continuing news retrieval session.");
  704.         return 1;
  705.       } else {
  706.         output("\n ■ Authentication failed.  Aborting news session.");
  707.         return 0;
  708.       }
  709.     } else {
  710.       output("\n ■ Unknown AUTHINFO response %s.", _temp_buffer);
  711.       return 0;
  712.     }
  713.   }
  714.   SOCK_READ_ERR(NNTP);
  715.   return 0;
  716. }
  717.  
  718. int cgroup(Mail_Socket * NNTP_sock, char *s)
  719. {
  720.   char junk[STRING];
  721.  
  722.   *_temp_buffer = 0;
  723.  
  724.   sock_printf(NNTP_sock->sock, "GROUP %s", s);
  725.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  726.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  727.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  728.   switch (reply) {
  729.     case 411:
  730.       return 0;
  731.     case 211:
  732.       sscanf(_temp_buffer, "%hu %lu %lu %lu %s",
  733.              &reply, &cur_numa, &cur_first, &cur_last, junk);
  734.       return 1;
  735.     case 480:
  736.       return (-1);
  737.     default:
  738.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  739.       log_it(MOREINFO, "\n ■ Unknown GROUP response : %s", _temp_buffer);
  740.       break;
  741.   }
  742.   SOCK_READ_ERR(NNTP);
  743.   return 0;
  744. }
  745.  
  746. char *treat(char *string)
  747. {
  748.   char *s;
  749.  
  750.   if (!string || !*string)
  751.     return NULL;
  752.  
  753.   for (s = string; *s; ++s) {
  754.     if ((*s < 32) || (*s > 126)) {
  755.       if ((*s != 9) && (*s != 10)) {
  756.         memmove(s, s + 1, strlen(s) + 1);
  757.         --s;
  758.       }
  759.     }
  760.   }
  761.  
  762.   return string;
  763. }
  764.  
  765. char *name_packet(char *pktname)
  766. {
  767.   int i;
  768.  
  769.   for (i = 0; i < 1000; i++) {
  770.     sprintf(pktname, "%sP0-%5.5d.%3.3d", net_data, i, instance);
  771.     if (!exist(pktname))
  772.       break;
  773.   }
  774.   return pktname;
  775. }
  776.  
  777. int jgets(char *s, int maxlen)
  778. {
  779.   char temp[500];
  780.   int done = 0, pos = 0, length = 0, i, c, zeroflag;
  781.  
  782.   while (!done) {
  783.     zeroflag = 0;
  784.     if ((c = getch()) == 0) {
  785.       zeroflag = 1;
  786.       c = getch();
  787.     }
  788.     switch (c) {
  789.       case 27:
  790.         return 0;
  791.       case 8:
  792.         if (c == 8) {
  793.           if (pos == 0)
  794.             break;
  795.           if (pos != length) {
  796.             for (i = pos - 1; i < length; i++)
  797.               temp[i] = temp[i + 1];
  798.             pos--;
  799.             length--;
  800.             putch(8);
  801.             for (i = pos; i < length; i++)
  802.               putch(temp[i]);
  803.             for (i = length; i >= pos; i--)
  804.               putch(8);
  805.           } else {
  806.             putch(8);
  807.             pos = --length;
  808.           }
  809.           break;
  810.         }
  811.       case 13:
  812.         if (c == 13) {
  813.           done = 1;
  814.           break;
  815.         }
  816.       default:
  817.         if (zeroflag)
  818.           break;
  819.         if (c == 0 || pos == maxlen)
  820.           break;
  821.         if (pos == length) {
  822.           temp[pos++] = c;
  823.           if (pos > length)
  824.             length++;
  825.           putch(c);
  826.         } else {
  827.           for (i = length++; i >= pos; i--)
  828.             temp[i + 1] = temp[i];
  829.           temp[pos++] = c;
  830.           putch(c);
  831.           for (i = pos; i < length; i++)
  832.             putch(temp[i]);
  833.           for (i = length; i > pos; i--)
  834.             putch(8);
  835.         }
  836.     }
  837.   }
  838.   temp[length] = '\0';
  839.   strcpy(s, temp);
  840.   return length;
  841. }
  842.  
  843.  
  844. int savebody(Mail_Socket * NNTP_sock, char *fn, int cug, unsigned long cur_article, int *abort)
  845. {
  846.   int i, f, place, length, curpos, count, part;
  847.   char _big_temp_buffer[BIGSTR];
  848.   char ch, spin[15], buf[STRING];
  849.   unsigned long msgsize;
  850.  
  851.   place = count = 0;
  852.   *_temp_buffer = 0;
  853.   part = 1;
  854.  
  855.   strcpy(spin, "||//--\\\\");
  856.   length = strlen(spin);
  857.  
  858.   if (spooltodisk)
  859.     f = sh_open(fn, O_RDWR | O_APPEND | O_TEXT | O_CREAT, S_IREAD | S_IWRITE);
  860.   else
  861.     f = sh_open(fn, O_RDWR | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE);
  862.  
  863.   if (f < 0) {
  864.     log_it(MOREINFO, "\n ■ Unable to write to %s.", fn);
  865.     return 0;
  866.   }
  867.  
  868.   sock_printf(NNTP_sock->sock, "BODY %ld", cur_article);
  869.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  870.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  871.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  872.   switch (reply) {
  873.     case 423:
  874.       return 0;
  875.     case 222:
  876.       sh_write(f, "\n", sizeof(char));
  877.       if (spooltodisk) {
  878.         if (MOREINFO)
  879.           output("\n ■ NEWS%d.UUE - [Esc], [Space], [Tab], ] = +10, } = +100, + = User Input [-]", cug);
  880.         for (i = 0; i < 79; i++)
  881.           sh_write(f, "-", sizeof(char));
  882.         sh_write(f, "\n", sizeof(char));
  883.         sprintf(buf, "Art  : %lu\n", cur_article);
  884.         sh_write(f, buf, strlen(buf));
  885.         if (*cur_lines) {
  886.           if ((atol(cur_lines) > 0L) && (atol(cur_lines) < 99999L))
  887.             sprintf(buf, "Lines: %s\n", cur_lines);
  888.           else
  889.             sprintf(buf, "Lines: Unknown\n");
  890.         }
  891.         sh_write(f, buf, strlen(buf));
  892.         sprintf(buf, "Group: %s\n", grouprec[cug].groupname);
  893.         sh_write(f, buf, strlen(buf));
  894.         if (*cur_date)
  895.           sprintf(buf, "Date : %s\n", cur_date);
  896.         else
  897.           sprintf(buf, "Date : Unknown\n");
  898.         sh_write(f, buf, strlen(buf));
  899.         if (*cur_replyto)
  900.           sprintf(buf, "From : %s\n", cur_replyto);
  901.         else
  902.           sprintf(buf, "From : Unknown\n");
  903.         sh_write(f, buf, strlen(buf));
  904.         if (*cur_subject)
  905.           sprintf(buf, "Subj : %s\n\n", cur_subject);
  906.         else
  907.           sprintf(buf, "Subj : Unknown\n");
  908.         sh_write(f, buf, strlen(buf));
  909.       } else {
  910.         if (*cur_replyto) {
  911.           sprintf(buf, "0RReply-To: %s\n", cur_replyto);
  912.           sh_write(f, buf, strlen(buf));
  913.         }
  914.         if (*cur_message_ID) {
  915.           sprintf(buf, "0RMessage-ID: %s\n", cur_message_ID);
  916.           sh_write(f, buf, strlen(buf));
  917.         }
  918.         if (*cur_references) {
  919.           sprintf(buf, "0RReferences: %s\n", cur_references);
  920.           sh_write(f, buf, strlen(buf));
  921.         }
  922.         if (MOREINFO)
  923.           output("\n ■ Receiving message - [Esc] Abort - [Space] Skip Group - [Tab] Catch Up [-]");
  924.         else
  925.           output(" [-]");
  926.       }
  927.       msgsize = 0L;
  928.       curpos = 0L;
  929.       while (1) {
  930.         while (kbhit()) {
  931.           ch = (getch());
  932.           strcpy(spin, "..oOOOo.");
  933.           length = strlen(spin);
  934.           switch (ch) {
  935.             case '+':
  936.               if (spooltodisk) {
  937.                 if (MOREINFO) {
  938.                   backline();
  939.                   log_it(MOREINFO, "\r ■ Jumping forward... wait until message is completed [.]");
  940.                 }
  941.                 *abort = 6;
  942.               }
  943.               break;
  944.             case '}':
  945.               if (spooltodisk) {
  946.                 if (MOREINFO) {
  947.                   backline();
  948.                   log_it(MOREINFO, "\r ■ Jumping 100 articles... wait until message is completed [.]");
  949.                 }
  950.                 *abort = 5;
  951.               }
  952.               break;
  953.             case ']':
  954.               if (spooltodisk) {
  955.                 if (MOREINFO) {
  956.                   backline();
  957.                   log_it(MOREINFO, "\r ■ Jumping 10 articles... wait until message is completed [.]");
  958.                 }
  959.                 *abort = 4;
  960.               }
  961.               break;
  962.             case 9:
  963.               if (MOREINFO) {
  964.                 backline();
  965.                 log_it(MOREINFO, "\r ■ Catching up on group... wait until message is completed [.]");
  966.               }
  967.               *abort = 3;
  968.               break;
  969.             case 32:
  970.               if (MOREINFO) {
  971.                 backline();
  972.                 log_it(MOREINFO, "\r ■ Skipping group... wait until message is completed [.]");
  973.               }
  974.               *abort = 2;
  975.               break;
  976.             case 27:
  977.               if (MOREINFO) {
  978.                 backline();
  979.                 log_it(MOREINFO, "\r ■ Aborting session... wait until message is completed [.]");
  980.               }
  981.               *abort = 1;
  982.               break;
  983.             default:
  984.               break;
  985.           }
  986.         }
  987.         if (count++ > 5) {
  988.           output("\b\b%c]", spin[place++]);
  989.           if (length)
  990.             place %= length;
  991.           count = 0;
  992.         }
  993.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  994.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  995.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0)
  996.           break;
  997.         else {
  998.           treat(_big_temp_buffer);
  999.           if (*_big_temp_buffer) {
  1000.             strcat(_big_temp_buffer, "\n");
  1001.             curpos = strlen(_big_temp_buffer);
  1002.             sh_write(f, _big_temp_buffer, strlen(_big_temp_buffer));
  1003.             msgsize += curpos;
  1004.           }
  1005.         }
  1006.         if ((msgsize > 30000L) && (!spooltodisk)) {
  1007.           strcpy(buf, "\nContinued in next message...\n");
  1008.           sh_write(f, buf, strlen(buf));
  1009.           sh_close(f);
  1010.           sprintf(fn, "%sSPOOL\\INPUT%d.MSG", net_data, ++part);
  1011.           unlink(fn);
  1012.           if ((f = sh_open(fn, O_WRONLY | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE)) < 0) {
  1013.             log_it(MOREINFO, "\n ■ Unable to create %s", fn);
  1014.             sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1015.             return 0;
  1016.           }
  1017.           if (MOREINFO) {
  1018.             backline();
  1019.             output("\r ■ Breaking into %d%s part [ ]", (part), ordinal_text(part));
  1020.           }
  1021.           strcpy(buf, "\nContinued from previous message...\n");
  1022.           sh_write(f, buf, strlen(buf));
  1023.           msgsize = strlen(buf);
  1024.         }
  1025.       }
  1026.       sh_close(f);
  1027.       output("\b\bX]");
  1028.       return 1;
  1029.     default:
  1030.       log_it(MOREINFO, "\n ■ Unknown BODY error : %s", _temp_buffer);
  1031.       break;
  1032.   }
  1033.   sh_close(f);
  1034.   SOCK_READ_ERR(NNTP);
  1035.   return 0;
  1036. }
  1037.  
  1038. int extract(char *to, char *key, char *from)
  1039. {
  1040.   if (!strnicmp(from, key, strlen(key))) {
  1041.     from += strlen(key);
  1042.     while (*from == ' ')
  1043.       from++;
  1044.     strcpy(to, from);
  1045.     return 1;
  1046.   } else
  1047.     return 0;
  1048. }
  1049.  
  1050.  
  1051. int chead(Mail_Socket * NNTP_sock, unsigned long which)
  1052. {
  1053.   char _big_temp_buffer[BIGSTR];
  1054.  
  1055.   *cur_path = 0;
  1056.   *cur_from = 0;
  1057.   *cur_replyto = 0;
  1058.   *cur_subject = 0;
  1059.   *cur_newsgroups = 0;
  1060.   *cur_message_ID = 0;
  1061.   *cur_references = 0;
  1062.   *cur_date = 0;
  1063.   *_temp_buffer = 0;
  1064.  
  1065.   sock_printf(NNTP_sock->sock, "HEAD %lu", which);
  1066.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1067.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1068.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  1069.   switch (reply) {
  1070.     case 423:
  1071.       output("%-55.55s", "Expired article.");
  1072.       return 0;
  1073.     case 221:
  1074.       while (1) {
  1075.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1076.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1077.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0) {
  1078.           if (cur_replyto[0] == 0)
  1079.             strcpy(cur_replyto, cur_from);
  1080.           if (cur_subject[0] == 0)
  1081.             strcpy(cur_subject, "No Subject");
  1082.           return 1;
  1083.         }
  1084.         if ((strlen(_big_temp_buffer)) > STRING)
  1085.           _big_temp_buffer[STRING] = '\0';
  1086.         if (extract(cur_path, "Path:", _big_temp_buffer) ||
  1087.              extract(cur_from, "From:", _big_temp_buffer) ||
  1088.              extract(cur_subject, "Subject:", _big_temp_buffer) ||
  1089.              extract(cur_replyto, "Reply-To:", _big_temp_buffer) ||
  1090.              extract(cur_newsgroups, "Newsgroups:", _big_temp_buffer) ||
  1091.              extract(cur_organization, "Organization:", _big_temp_buffer) ||
  1092.              extract(cur_message_ID, "Message-ID:", _big_temp_buffer) ||
  1093.              extract(cur_references, "References:", _big_temp_buffer) ||
  1094.              extract(cur_lines, "Lines:", _big_temp_buffer) ||
  1095.              extract(cur_date, "Date:", _big_temp_buffer))
  1096.          continue;
  1097.       }
  1098.     default:
  1099.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1100.       log_it(MOREINFO, "\n ■ Unknown chead error : %s", _big_temp_buffer);
  1101.       break;
  1102.   }
  1103.   SOCK_READ_ERR(NNTP);
  1104.   return 0;
  1105. }
  1106.  
  1107. Mail_Socket *netsocket(char *host)
  1108. {
  1109.   Mail_Socket *NNTP_sock = NULL;
  1110.   longword h;
  1111.  
  1112.   sock_init();
  1113.  
  1114.   if (!(h = resolve(host))) {
  1115.     if (!(h = resolve(host))) {
  1116.       log_it(1, "\n ■ Error : Cannot resolve host %s", host);
  1117.       return NULL;
  1118.     }
  1119.   }
  1120.   if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  1121.     log_it(1, "\n ■ Error: Insufficient memory to create socket... aborting.");
  1122.     return NULL;
  1123.   }
  1124.   if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  1125.     log_it(1, "\n ■ Error : Insufficient memory to create socket... aborting.");
  1126.     farfree(NNTP_sock);
  1127.     return NULL;
  1128.   }
  1129.   if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
  1130.     log_it(1, "\n ■ Error : Unable to connect to to %s.", host);
  1131.     farfree(NNTP_sock);
  1132.     return NULL;
  1133.   }
  1134.   sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
  1135.   sock_wait_established(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1136.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1137.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1138.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1139.   switch (reply) {
  1140.     case 200:
  1141.       log_it(MOREINFO, "\n ■ Connection to %s accepted...", host);
  1142.       return (NNTP_sock);
  1143.     case 502:
  1144.       log_it(1, "\n ■ Connection to %s refused... try again later.", host);
  1145.       break;
  1146.     case 503:
  1147.       log_it(1, "\n ■ NNTP service unavailable. Connection to %s refused.", host);
  1148.       break;
  1149.     default:
  1150.       log_it(1, "\n ■ Unknown NNTP error. Connection to %s failed.", host);
  1151.       break;
  1152.   }
  1153.   SOCK_READ_ERR(NNTP);
  1154.   return 0;
  1155. }
  1156.  
  1157. int cnext(Mail_Socket * NNTP_sock)
  1158. {
  1159.   char *p, *q;
  1160.  
  1161.   *_temp_buffer = 0;
  1162.  
  1163.   sock_printf(NNTP_sock->sock, "NEXT");
  1164.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1165.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1166.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1167.   switch (reply) {
  1168.     case 421:
  1169.       return 0;
  1170.     case 223:
  1171.       p = _temp_buffer;
  1172.       q = cur_articleid;
  1173.       while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  1174.         p++;
  1175.       while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  1176.         *q++ = *p++;
  1177.       *q++ = '>';
  1178.       *q++ = '\0';
  1179.       return 1;
  1180.     default:
  1181.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1182.       log_it(MOREINFO, "\n ■ Unknown NEXT error : %s", _temp_buffer);
  1183.       break;
  1184.   }
  1185.   SOCK_READ_ERR(NNTP);
  1186.   return 0;
  1187. }
  1188.  
  1189. int cslave(Mail_Socket * NNTP_sock)
  1190. {
  1191.   char junk[STRING];
  1192.  
  1193.   *_temp_buffer = 0;
  1194.  
  1195.   sock_printf(NNTP_sock->sock, "SLAVE");
  1196.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1197.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1198.   sscanf(_temp_buffer, "%hu %s", &reply, junk);
  1199.   SOCK_READ_ERR(NNTP);
  1200.   return 0;
  1201. }
  1202.  
  1203. char *stripspace(char *str)
  1204. {
  1205.   char *obuf, *nbuf;
  1206.  
  1207.   if (str) {
  1208.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  1209.       if (!isspace(*obuf))
  1210.         *nbuf++ = *obuf;
  1211.     }
  1212.     *nbuf = NULL;
  1213.   }
  1214.   return (str);
  1215. }
  1216.  
  1217.  
  1218. int saveactive(Mail_Socket * NNTP_sock, int forced)
  1219. {
  1220.   char s[181], fn[MAXPATH], group[256], act;
  1221.   unsigned long to, from;
  1222.   unsigned fnday, fnmonth, fnyear, fnhour, fnminute, fnsecond;
  1223.   int f, done, update;
  1224.   unsigned long count;
  1225.   FILE *fp;
  1226.   struct ftime ft;
  1227.   struct date d;
  1228.  
  1229.   *_temp_buffer = 0;
  1230.  
  1231.   update = 0;
  1232.  
  1233.   sprintf(fn, "%sNEWSRC", net_data);
  1234.  
  1235.   f = sh_open1(fn, O_RDONLY);
  1236.   if (f < 0)
  1237.     forced = 1;
  1238.   else {
  1239.     getftime(f, &ft);
  1240.     sh_close(f);
  1241.   }
  1242.  
  1243.   getdate(&d);
  1244.   if ((ft.ft_month == 12) && ((d.da_day == 1) && (d.da_mon == 1))) {
  1245.     unlink(fn);
  1246.     forced = 1;
  1247.   }
  1248.  
  1249.   if (!forced) {
  1250.     fnsecond = 2 * ft.ft_tsec;
  1251.     fnminute = ft.ft_min;
  1252.     fnhour = ft.ft_hour;
  1253.     fnday = ft.ft_day;
  1254.     fnmonth = ft.ft_month;
  1255.     fnyear = ft.ft_year + 80;
  1256.     log_it(1, "\n ■ NEWSRC last updated %02u/%02u/%02u %.2u:%.2u:%.2u... ",
  1257.         fnmonth, fnday, fnyear, fnhour, fnminute, fnsecond);
  1258.     if ((d.da_day == ft.ft_day) && (d.da_mon == ft.ft_month) &&
  1259.         (d.da_year == (ft.ft_year + 1980))) {
  1260.       log_it(1, "no update required.");
  1261.       return 0;
  1262.     } else
  1263.       log_it(1, "updating.");
  1264.     if ((d.da_mon == ft.ft_month) && (d.da_day != ft.ft_day))
  1265.       update = 1;
  1266.   }
  1267.  
  1268.   if (update)
  1269.     fp = fsh_open(fn, "a+");
  1270.   else
  1271.     fp = fsh_open(fn, "w+");
  1272.  
  1273.   if (fp == NULL) {
  1274.     perror(fn);
  1275.     return 0;
  1276.   }
  1277.   done = 0;
  1278.  
  1279.   while (!done) {
  1280.     if (update) {
  1281.       sprintf(s, "NEWGROUPS %02.02u%02.02u%02.02u %02.02u%02.02u%02.02u",
  1282.             fnyear, fnmonth, fnday, fnhour, fnminute, fnsecond);
  1283.       sock_printf(NNTP_sock->sock, s);
  1284.       output("\n ■ Refreshing NEWSRC newsgroup listing :           ");
  1285.     } else {
  1286.       sock_printf(NNTP_sock->sock, "LIST");
  1287.       output("\n ■ Retrieving current newsgroup listing :           ");
  1288.     }
  1289.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1290.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1291.     sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1292.     if ((reply == 215) || (reply == 231)) {
  1293.       count = 0L;
  1294.       while (1) {
  1295.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1296.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1297.         if (*_temp_buffer == '.' && _temp_buffer[1] == 0)
  1298.           break;
  1299.         if (strlen(_temp_buffer) < 180) {
  1300.           if (count++ % 25 == 24L)
  1301.             output("\b\b\b\b\b\b\b%-7lu", count);
  1302.           sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
  1303.           sprintf(s, "%s %lu\n", group, to);
  1304.           fsh_write(s, sizeof(char), strlen(s), fp);
  1305.         }
  1306.       }
  1307.       if (update) {
  1308.         if (count == 0) {
  1309.           log_it(1, "\b\b\b\b\b\b\bNo new groups.");
  1310.         } else
  1311.           log_it(1, "\b\b\b\b\b\b\b%lu group%s added to NEWSRC.", count,
  1312.                 count == 1 ? "" : "s");
  1313.       } else
  1314.         output("\b\b\b\b\b\b\b%lu total groups.", count);
  1315.  
  1316.       if (fp != NULL)
  1317.         fclose(fp);
  1318.       if (update) {
  1319.         f = sh_open1(fn, O_RDWR);
  1320.         ft.ft_day = d.da_day;
  1321.         setftime(f, &ft);
  1322.         sh_close(f);
  1323.       }
  1324.       return 1;
  1325.     } else {
  1326.       if (reply == 480) {
  1327.         if (fp != NULL)
  1328.           fclose(fp);
  1329.         return 0;
  1330.       } else {
  1331.         log_it(MOREINFO, "\n ■ Unknown saveactive error : %s", _temp_buffer);
  1332.         if (fp != NULL)
  1333.           fclose(fp);
  1334.         done = 1;
  1335.       }
  1336.     }
  1337.   }
  1338.   if (fp != NULL)
  1339.     fclose(fp);
  1340.   SOCK_READ_ERR(NNTP);
  1341.   return 0;
  1342. }
  1343.  
  1344. int checkx(int cug)
  1345. {
  1346.   char *ptr, *ptr2, *ptr3, fn[MAXPATH], buf[256], tmp[81];
  1347.   int i, spam, max, ok;
  1348.   FILE *fp;
  1349.  
  1350.   if ((stristr(grouprec[cug].groupname, "binaries")) && (binxpost))
  1351.     return 1;
  1352.  
  1353.   sprintf(buf, "%s!%s", POPNAME, POPDOMAIN);
  1354.   if (stristr(cur_path, buf) != 0) {
  1355.     sprintf(buf, "Local post - %s - skipped.", cur_subject);
  1356.     output("%-55.55s", buf);
  1357.     return 0;
  1358.   }
  1359.  
  1360.   ok = 1;
  1361.   spam = 0;
  1362.   sprintf(fn, "%sNOSPAM.TXT", net_data);
  1363.   if ((fp = fsh_open(fn, "r")) != NULL) {
  1364.     while ((!feof(fp)) && (!spam)) {
  1365.       fgets(buf, 80, fp);
  1366.       trimstr1(buf);
  1367.       if (strlen(buf) > 2) {
  1368.         if (buf[0] == '\"') {
  1369.           strcpy(tmp, &(buf[1]));
  1370.           LAST(tmp) = '\0';
  1371.           strcpy(buf, tmp);
  1372.         }
  1373.         if (buf[0] == '[') {
  1374.           if ((strnicmp(buf, "[GLOBAL]", 8) == 0) || (strnicmp(buf, "[NEWS]", 6) == 0))
  1375.             ok = 1;
  1376.           else
  1377.             ok = 0;
  1378.         }
  1379.         if ((ok) && ((stristr(cur_subject, buf)) || (stristr(cur_from, buf)))) {
  1380.           sprintf(tmp, "SPAM - %s - skipped.", buf);
  1381.           output("%-55.55s", tmp);
  1382.           spam = 1;
  1383.         }
  1384.       }
  1385.     }
  1386.     fclose(fp);
  1387.     if (spam)
  1388.       return 0;
  1389.   }
  1390.  
  1391.   ptr = cur_newsgroups;
  1392.  
  1393.   while (*ptr == ' ')
  1394.     ptr++;
  1395.   max = 0;
  1396.   while (1) {
  1397.     if (*ptr == 0)
  1398.       break;
  1399.     if (*ptr == ',') {
  1400.       ptr++;
  1401.       ++max;
  1402.     }
  1403.     while (*ptr == ' ')
  1404.       ptr++;
  1405.     ptr2 = ptr;
  1406.     ptr3 = buf;
  1407.     while (*ptr2 != 0 && *ptr2 != ',')
  1408.       *ptr3++ = *ptr2++;
  1409.     *ptr3 = 0;
  1410.     while (*(--ptr3) == ' ')
  1411.       *ptr3 = 0;
  1412.     ptr = ptr2;
  1413.     for (i = 0; i < cug; i++) {
  1414.       if (strcmpi(buf, grouprec[i].groupname) == 0) {
  1415.         output("Already posted in %s.", grouprec[i].groupname);
  1416.         return 0;
  1417.       }
  1418.     }
  1419.     if ((crossposts > 0) && (max > crossposts)) {
  1420.       sprintf(buf, "Crossposted to more than %d newsgroups.", crossposts);
  1421.       output("%-55.55s", buf);
  1422.       return 0;
  1423.     }
  1424.   }
  1425.   return 1;
  1426. }
  1427.  
  1428. unsigned char *strrep(char *str, char oldstring, char newstring)
  1429. {
  1430.   int i;
  1431.  
  1432.   for (i = 0; str[i]; i++)
  1433.     if (str[i] == oldstring)
  1434.       str[i] = newstring;
  1435.   return (str);
  1436. }
  1437.  
  1438.  
  1439.  
  1440. int postnews(Mail_Socket * NNTP_sock, int cug)
  1441. {
  1442.   char s[160], s1[12], s2[5], fn[MAXPATH], *ss;
  1443.   int f1, nlines;
  1444.   FILE *fp;
  1445.   struct ffblk ff;
  1446.  
  1447.   *_temp_buffer = 0;
  1448.  
  1449.   sprintf(fn, "%sOUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
  1450.   f1 = findfirst(fn, &ff, 0);
  1451.   if (f1 != 0) {
  1452.     log_it(MOREINFO, "\n ■ No outbound news articles to post...");
  1453.     return 1;
  1454.   }
  1455.   while (f1 == 0) {
  1456.     sock_printf(NNTP_sock->sock, "POST");
  1457.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1458.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1459.     if (*_temp_buffer != '3') {
  1460.       if (atoi(_temp_buffer) == 440)
  1461.         log_it(MOREINFO, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
  1462.       else
  1463.         log_it(1, "\n ■ Remote error: %s", _temp_buffer);
  1464.       return 0;
  1465.     }
  1466.     if (MOREINFO)
  1467.       output("\n");
  1468.     else
  1469.       backline();
  1470.     output(" ■ Posting article to %s\n", grouprec[cug].groupname);
  1471.     sprintf(s, "%sOUTBOUND\\%s", net_data, ff.ff_name);
  1472.     if ((fp = fsh_open(s, "r")) == NULL) {
  1473.       log_it(MOREINFO, "\n ■ Error reading %s.", s);
  1474.       return 1;
  1475.     }
  1476.     nlines = 0;
  1477.     while (!feof(fp)) {
  1478.       fgets(_temp_buffer, sizeof(_temp_buffer), fp);
  1479.       strrep(_temp_buffer, '\n', '\0');
  1480.       strrep(_temp_buffer, '\r', '\0');
  1481.       if (*_temp_buffer == '.') {
  1482.         movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
  1483.         *_temp_buffer = '.';
  1484.       }
  1485.       sock_puts(NNTP_sock->sock, _temp_buffer);
  1486.       if (MOREINFO) {
  1487.         backline();
  1488.         output("\r ■ Lines sent : %d", ++nlines);
  1489.       }
  1490.     }
  1491.     fclose(fp);
  1492.     sock_puts(NNTP_sock->sock, ".");
  1493.     if (MOREINFO)
  1494.       output("\n ■ Awaiting acknowledgement - may take several minutes...");
  1495.     log_it(MOREINFO, "\n ■ Server response : ");
  1496.     sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
  1497.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1498.     log_it(MOREINFO, "%s", _temp_buffer);
  1499.     if (*_temp_buffer != '2') {
  1500.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  1501.       if (reply == 441) {
  1502.         ss = strtok(_temp_buffer, " ");
  1503.         ss = strtok(NULL, " ");
  1504.         if (atoi(ss) == 435)
  1505.           unlink(s);
  1506.         fnsplit(s, NULL, NULL, s1, s2);
  1507.         log_it(MOREINFO, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
  1508.         if (unlink(s) == 0)
  1509.           log_it(MOREINFO, "\n ■ Deleted message - %s", s);
  1510.       } else
  1511.         log_it(MOREINFO, "\n ■ Remote error: %s", _temp_buffer);
  1512.     } else {
  1513.       if (unlink(s) == 0)
  1514.         log_it(MOREINFO, "\n ■ Deleted sent message - %s", s);
  1515.     }
  1516.     f1 = findnext(&ff);
  1517.   }
  1518.   SOCK_READ_ERR(NNTP);
  1519.   return 0;
  1520. }
  1521.  
  1522. void get_subtype(int sub, char *where)
  1523. {
  1524.   int ok, which;
  1525.   char fn[181], s[81], net_name[21], *ss;
  1526.   FILE *fp;
  1527.  
  1528.   where[0] = 0;
  1529.   which = sub;
  1530.   sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
  1531.   if ((fp = fsh_open(fn, "r")) == NULL)
  1532.     return;
  1533.   ok = 0;
  1534.   while (fgets(s, 80, fp)) {
  1535.     if (*s == '!') {
  1536.       if (which == atoi(&s[1]))
  1537.         ok = 1;
  1538.     }
  1539.     if (ok && (*s == '$')) {
  1540.       ss = strtok(s, " ");
  1541.       strcpy(net_name, &ss[1]);
  1542.       ss = strtok(NULL, " ");
  1543.       if (fp != NULL)
  1544.         fclose(fp);
  1545.       if ((stricmp(net_name, "FILENET") == 0)) {
  1546.         trimstr1(ss);
  1547.         strcpy(where, ss);
  1548.         return;
  1549.       } else
  1550.         return;
  1551.     }
  1552.   }
  1553.   if (fp != NULL)
  1554.     fclose(fp);
  1555. }
  1556.  
  1557. unsigned long max_on_sub(int cug)
  1558. {
  1559.   int i, num_subs;
  1560.   char fn[MAXPATH], subtype[12];
  1561.   unsigned long max;
  1562.   subboardrec sub;
  1563.   FILE *fp;
  1564.  
  1565.   max = 0;
  1566.   if ((strlen(grouprec[cug].subtype) == 1) && (*grouprec[cug].subtype == '0'))
  1567.     return max;
  1568.   if (MOREINFO)
  1569.     output("\n ■ Max articles -     ");
  1570.   else
  1571.     output(" -     ");
  1572.   sprintf(fn, "%sSUBS.DAT", syscfg.datadir);
  1573.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  1574.     log_it(MOREINFO, "\n ■ Unable to read %s.", fn);
  1575.     return max;
  1576.   } else {
  1577.     fseek(fp, 0L, SEEK_END);
  1578.     num_subs = (int) (ftell(fp) / sizeof(subboardrec));
  1579.     for (i = 0; i < num_subs; i++) {
  1580.       output("\b\b\b\b%-4d", i);
  1581.       fseek(fp, (long) i * sizeof(subboardrec), SEEK_SET);
  1582.       fread(&sub, sizeof(subboardrec), 1, fp);
  1583.       get_subtype(i, subtype);
  1584.       if (stricmp(subtype, grouprec[cug].subtype) == 0) {
  1585.         max = (unsigned long)sub.maxmsgs;
  1586.         if (MOREINFO)
  1587.           output("\b\b\b\b%s (%s) - %lu posts.", sub.name, subtype, max);
  1588.         else
  1589.           output("\b\b\b\b [%lu max]", max);
  1590.         break;
  1591.       }
  1592.     }
  1593.   }
  1594.   fclose(fp);
  1595.   return max;
  1596. }
  1597.  
  1598.  
  1599. int getnews(Mail_Socket * NNTP_sock)
  1600. {
  1601.   int f1, nup, nug, cug, jug, abort, ok, done, firstrun, parts, skipped, bogus;
  1602.   char *p, fn[MAXPATH], mailname[121], reline[121], pktname[MAXPATH];
  1603.   char ch, orig_subj[STRING], buf[STR];
  1604.   unsigned int text_len;
  1605.   unsigned short temptype;
  1606.   unsigned long new_articles, max_articles, skip_articles;
  1607.   net_header_rec nh;
  1608.   struct ffblk ff;
  1609.   FILE *fp;
  1610.  
  1611.   abort = cug = bogus = 0;
  1612.  
  1613.   if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
  1614.     if (!saveactive(NNTP_sock, 1)) {
  1615.       if (sendauthinfo(NNTP_sock))
  1616.         saveactive(NNTP_sock, 1);
  1617.     }
  1618.     ++cug;
  1619.   } else {
  1620.     if (newsrc_upd)
  1621.       saveactive(NNTP_sock, 0);
  1622.   }
  1623.  
  1624.   nug = jug = nup = 1;
  1625.   skipped = 0;
  1626.   cslave(NNTP_sock);
  1627.   name_packet(pktname);
  1628.   if (!MOREINFO)
  1629.     output("\n ■ [Esc] aborts session - [Space] skips group - [Tab] catches up group");
  1630.   while ((cug < ngroups) && (!abort)) {
  1631.     if (*grouprec[cug].groupname == ';') {
  1632.       log_it(1, "\n ■ Skipping commented group - %s", &(grouprec[cug].groupname[1]));
  1633.       ++cug;
  1634.       jug = 1;
  1635.       continue;
  1636.     }
  1637.     if (*grouprec[cug].subtype == '0')
  1638.       spooltodisk = 1;
  1639.     else
  1640.       spooltodisk = 0;
  1641.     if (nup) {
  1642.       nup = 0;
  1643.       write_groups(1);
  1644.       if (*grouprec[cug].subtype != '0')
  1645.         name_packet(pktname);
  1646.     }
  1647.     if (nug) {
  1648.       jug = 1;
  1649.       nug = 0;
  1650.       while (1) {
  1651.         if (!cgroup(NNTP_sock, grouprec[cug].groupname)) {
  1652.           if (cgroup(NNTP_sock, grouprec[cug].groupname) == -1) {
  1653.             if (sendauthinfo(NNTP_sock))
  1654.               continue;
  1655.             else
  1656.               return 1;
  1657.           }
  1658.           if (++bogus > 10) {
  1659.             log_it(1, "\n ■ Too many invalid newsgroups... NEWS.RC error?");
  1660.             return 1;
  1661.           }
  1662.           log_it(1, "\n ■ Error accessing - %s", grouprec[cug].groupname);
  1663.           nug = 1;
  1664.           break;
  1665.         } else
  1666.           break;
  1667.       }
  1668.       if (!nug) {
  1669.         log_it(1, "\n ■ Requesting - %s - ", grouprec[cug].groupname);
  1670.         if (grouprec[cug].lastread < cur_first)
  1671.           grouprec[cug].lastread = cur_first;
  1672.         if (grouprec[cug].lastread >= cur_last) {
  1673.           grouprec[cug].lastread = cur_last;
  1674.           log_it(1, "[No new articles]");
  1675.           nug = jug = 1;
  1676.         } else {
  1677.           new_articles = cur_last - grouprec[cug].lastread;
  1678.           if (new_articles) {
  1679.             log_it(1, "[%lu new article%s]", new_articles,
  1680.                    new_articles == 1 ? "" : "s");
  1681.           } else {
  1682.             log_it(1, "[No new articles]");
  1683.             nug = jug = 1;
  1684.           }
  1685.           if (new_articles > 250L) {
  1686.             max_articles = max_on_sub(cug);
  1687.             if (max_articles && (new_articles > max_articles)) {
  1688.               log_it(MOREINFO, "\n ■ Requesting most recent %lu articles", max_articles);
  1689.               grouprec[cug].lastread = cur_last - max_articles;
  1690.             }
  1691.           } else
  1692.             ++grouprec[cug].lastread;
  1693.         }
  1694.         postnews(NNTP_sock, cug);
  1695.         if (!nug) {
  1696.           if (cur_numa == 0) {
  1697.             log_it(MOREINFO, "\n ■ No articles in %s...", grouprec[cug].groupname);
  1698.             nug = jug = 1;
  1699.             continue;
  1700.           }
  1701.         }
  1702.       }
  1703.     } else {
  1704.       if ((!cnext(NNTP_sock)) || (grouprec[cug].lastread > cur_last)) {
  1705.         log_it(MOREINFO, "\n ■ End of articles in %s", grouprec[cug].groupname);
  1706.         nug = jug = 1;
  1707.         write_groups(0);
  1708.       }
  1709.     }
  1710.     if (!nug) {
  1711.       if (skipped)
  1712.         backline();
  1713.       else
  1714.         output("\n");
  1715.  
  1716.       output(" ■ [%lu/%lu] : ", grouprec[cug].lastread, cur_last);
  1717.       if ((chead(NNTP_sock, grouprec[cug].lastread)) && (checkx(cug))) {
  1718.         skipped = 0;
  1719.         if (*cur_subject) {
  1720.           if (strlen(cur_subject) > 78)
  1721.             cur_subject[78] = 0;
  1722.           treat(cur_subject);
  1723.         }
  1724.         strcpy(orig_subj, cur_subject);
  1725.         if (strlen(orig_subj) > 70)
  1726.           orig_subj[70] = 0;
  1727.         if (*cur_from) {
  1728.           treat(cur_from);
  1729.           if (strlen(cur_from) > 45)
  1730.             cur_from[45] = 0;
  1731.         } else
  1732.           strcpy(cur_from, "Unknown");
  1733.         if (*cur_replyto) {
  1734.           if (strlen(cur_replyto) > 45)
  1735.             cur_replyto[45] = 0;
  1736.         } else
  1737.           strcpy(cur_replyto, "Unknown");
  1738.         output("%-50.50s", orig_subj);
  1739.         if (spooltodisk)
  1740.           sprintf(fn, "%sSPOOL\\NEWS%d.UUE", net_data, cug);
  1741.         else
  1742.           sprintf(fn, "%sSPOOL\\INPUT1.MSG", net_data);
  1743.         abort = 0;
  1744.         fcloseall();
  1745.         ok = (savebody(NNTP_sock, fn, cug, grouprec[cug].lastread, &abort));
  1746.         if (ok && (*grouprec[cug].subtype != '0')) {
  1747.           if ((fp = fsh_open(pktname, "ab+")) == NULL) {
  1748.             log_it(MOREINFO, "\n ■ Unable to create %s!", pktname);
  1749.             return 1;
  1750.           }
  1751.           sprintf(fn, "%sSPOOL\\INPUT*.MSG", net_data);
  1752.           done = 1;
  1753.           parts = 0;
  1754.           if (findfirst(fn, &ff, 0) == 0) {
  1755.             parts = 1;
  1756.             done = 0;
  1757.           }
  1758.           firstrun = 1;
  1759.           while (!done) {
  1760.             sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1761.             f1 = sh_open1(fn, O_RDONLY | O_BINARY);
  1762.             text_len = (unsigned int) filelength(f1);
  1763.             if (text_len > 32000L) {
  1764.               log_it(MOREINFO, "\n ■ Truncating %lu bytes from input file",
  1765.                      text_len - 32000L);
  1766.               text_len = 32000;
  1767.             }
  1768.             if ((p = (char *) malloc(32767)) == NULL) {
  1769.               log_it(MOREINFO, "\n ■ Insufficient memory to read entire message");
  1770.               return 1;
  1771.             }
  1772.             sh_read(f1, (void *) p, text_len);
  1773.             sh_close(f1);
  1774.             temptype = atoi(grouprec[cug].subtype);
  1775.             nh.tosys = sy;
  1776.             nh.touser = 0;
  1777.             nh.fromsys = 32767;
  1778.             nh.fromuser = 0;
  1779.             if (!temptype) {
  1780.               nh.main_type = main_type_new_post;
  1781.               nh.minor_type = 0;
  1782.             } else {
  1783.               nh.main_type = main_type_pre_post;
  1784.               nh.minor_type = temptype;
  1785.             }
  1786.             nh.list_len = 0;
  1787.             ++cur_daten;
  1788.             nh.daten = cur_daten;
  1789.             nh.method = 0;
  1790.             if (parts > 1) {
  1791.               sprintf(buf, "%d%s ", parts, ordinal_text(parts));
  1792.               strncat(buf, orig_subj, 72);
  1793.               strcpy(cur_subject, buf);
  1794.               cur_subject[72] = '\0';
  1795.             }
  1796.             nh.length = text_len + strlen(cur_subject) + 1;
  1797.             strncpy(mailname, cur_replyto, 46);
  1798.             strcat(mailname, "\r\n");
  1799.             nh.length += strlen(mailname);
  1800.             if (nh.main_type == main_type_new_post)
  1801.               nh.length += strlen(grouprec[cug].subtype) + 1;
  1802.             if (firstrun) {
  1803.               firstrun = 0;
  1804.               if (strncmpi(cur_articleid, "re: ", 4) == 0) {
  1805.                 strncpy(reline, cur_articleid, 60);
  1806.                 sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
  1807.               }
  1808.             }
  1809.             if (!*cur_date) {
  1810.               strncpy(cur_date, ctime(&(time_t) nh.daten), 24);
  1811.               cur_date[24] = '\0';
  1812.             }
  1813.             strcat(cur_date, "\r\n");
  1814.             nh.length += strlen(cur_date);
  1815.             nh.length += strlen(cur_articleid);
  1816.             fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  1817.             if (nh.main_type == main_type_new_post)
  1818.               fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
  1819.             fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
  1820.             fsh_write(mailname, sizeof(char), strlen(mailname), fp);
  1821.             fsh_write(cur_date, sizeof(char), strlen(cur_date), fp);
  1822.             fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
  1823.             fsh_write(p, sizeof(char), text_len, fp);
  1824.             if (p)
  1825.               free(p);
  1826.             unlink(fn);
  1827.             if (findnext(&ff) == 0) {
  1828.               done = 0;
  1829.               ++parts;
  1830.             } else
  1831.               done = 1;
  1832.           }
  1833.           if (filelength(fileno(fp)) > 250000L)
  1834.             nup = 1;
  1835.           if (fp != NULL)
  1836.             fclose(fp);
  1837.         }
  1838.       } else
  1839.         skipped = 1;
  1840.       ++grouprec[cug].lastread;
  1841.       while (kbhit()) {
  1842.         ch = (getch());
  1843.         switch (ch) {
  1844.           case '+':
  1845.             if (spooltodisk) {
  1846.               if (MOREINFO)
  1847.                 backline();
  1848.               abort = 6;
  1849.             }
  1850.             break;
  1851.           case '}':
  1852.             if (spooltodisk) {
  1853.               if (MOREINFO)
  1854.                 backline();
  1855.               abort = 5;
  1856.             }
  1857.             break;
  1858.           case ']':
  1859.             if (spooltodisk) {
  1860.               if (MOREINFO)
  1861.                 backline();
  1862.               abort = 4;
  1863.             }
  1864.             break;
  1865.           case 9:
  1866.             abort = 3;
  1867.             break;
  1868.           case 32:
  1869.             abort = 2;
  1870.             break;
  1871.           case 27:
  1872.             abort = 1;
  1873.             break;
  1874.           default:
  1875.             break;
  1876.         }
  1877.       }
  1878.       switch (abort) {
  1879.         case 6:
  1880.           backline();
  1881.           output(" ? Skip how many articles? ");
  1882.           if (jgets(buf, 10)) {
  1883.             skip_articles = atol(buf);
  1884.             if ((grouprec[cug].lastread += skip_articles) < cur_last) {
  1885.               abort = 0;
  1886.               break;
  1887.             }
  1888.           } else
  1889.             break;
  1890.         case 5:
  1891.           if ((grouprec[cug].lastread += 100) < cur_last) {
  1892.             abort = 0;
  1893.             break;
  1894.           }
  1895.         case 4:
  1896.           if ((grouprec[cug].lastread += 10) < cur_last) {
  1897.             abort = 0;
  1898.             break;
  1899.           }
  1900.         case 3:
  1901.           grouprec[cug].lastread = cur_last;
  1902.         case 2:
  1903.           nug = jug = 1;
  1904.           abort = 0;
  1905.           break;
  1906.         default:
  1907.           break;
  1908.       }
  1909.     }
  1910.     if (nug) {
  1911.       ++cug;
  1912.       if (*grouprec[cug].subtype == '0')
  1913.         nup = 1;
  1914.     }
  1915.   }
  1916.   write_groups(1);
  1917.   nntp_shutdown(NNTP_sock);
  1918.   if (abort)
  1919.     log_it(1, "\n ■ Session aborted from keyboard");
  1920.   return 0;
  1921. }
  1922.  
  1923. int copyfile(char *input, char *output)
  1924. {
  1925.   int f1, f2, i;
  1926.   char *b;
  1927.   struct ftime ft;
  1928.  
  1929.   if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
  1930.     if ((b = (char *) malloc(16400)) == NULL)
  1931.       return 0;
  1932.     f1 = sh_open1(input, O_RDONLY | O_BINARY);
  1933.     if (!f1) {
  1934.       free(b);
  1935.       b = NULL;
  1936.       return 0;
  1937.     }
  1938.     getftime(f1, &ft);
  1939.  
  1940.     f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1941.     if (!f2) {
  1942.       free(b);
  1943.       b = NULL;
  1944.       f1 = sh_close(f1);
  1945.       return 0;
  1946.     }
  1947.     i = read(f1, (void *) b, 16384);
  1948.     while (i > 0) {
  1949.       sh_write(f2, (void *) b, i);
  1950.       i = read(f1, (void *) b, 16384);
  1951.     }
  1952.     f1 = sh_close(f1);
  1953.     setftime(f2, &ft);
  1954.     f2 = sh_close(f2);
  1955.     free(b);
  1956.     b = NULL;
  1957.   }
  1958.   return 1;
  1959. }
  1960.  
  1961.  
  1962.  
  1963. main(int argc, char *argv[])
  1964. {
  1965.   int ok, f, bAuth;
  1966.   char s[160], fn[MAXPATH], tempfn[MAXPATH], serverhost[50], *ss;
  1967.   FILE *fp;
  1968.   struct date dt;
  1969.   struct time tm;
  1970.   Mail_Socket *nntp_sock = NULL;
  1971.  
  1972.   crossposts = 10;
  1973.   binxpost = 0;
  1974.   newsrc_upd = 0;
  1975.   MOREINFO = 1;
  1976.   POPNAME[0] = 0;
  1977.   POPDOMAIN[0] = 0;
  1978.  
  1979.   cursor('S');
  1980.   cursor('H');
  1981.   output("\n ■ %s", version);
  1982.   if (argc != 4) {
  1983.     output("\n ■ Invalid arguments for %s\n", argv[0]);
  1984.     cursor('R');
  1985.     return EXIT_FAILURE;
  1986.   }
  1987.   strcpy(net_data, argv[1]);
  1988.   strcat(net_data, "\\");
  1989.   strcpy(serverhost, argv[2]);
  1990.   sy = atoi(argv[3]);
  1991.  
  1992.   get_dir(maindir, 0);
  1993.  
  1994.   detect_multitask();
  1995.  
  1996.   gettime(&tm);
  1997.   getdate(&dt);
  1998.   cur_daten = dostounix(&dt, &tm);
  1999.  
  2000.   ss = getenv("WWIV_INSTANCE");
  2001.   if (ss) {
  2002.     instance = atoi(ss);
  2003.     if ((instance <= 0) || (instance >= 1000)) {
  2004.       log_it(1, "\n ■ WWIV_INSTANCE set to %d.  Can only be 1..999!", instance);
  2005.       instance = 1;
  2006.     }
  2007.   } else
  2008.     instance = 1;
  2009.  
  2010.   sprintf(fn, "%s\\CONFIG.DAT", maindir);
  2011.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  2012.   if (f < 0) {
  2013.     output("\n ■ %s NOT FOUND.", fn);
  2014.     cursor('R');
  2015.     return EXIT_FAILURE;
  2016.   }
  2017.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  2018.   sh_close(f);
  2019.  
  2020.   read_groups();
  2021.   if (ngroups == 0) {
  2022.     output("\n ■ Unable to access newsgroup file NEWS.RC!");
  2023.     cursor('R');
  2024.     return EXIT_FAILURE;
  2025.   } else {
  2026.     sprintf(fn, "%sNEWS.RC", net_data);
  2027.     sprintf(tempfn, "%sNEWS.BAK", net_data);
  2028.     if (exist(tempfn))
  2029.       unlink(tempfn);
  2030.     copyfile(fn, tempfn);
  2031.   }
  2032.  
  2033.   if (stricmp(grouprec[0].groupname, "newsrc") == 0)
  2034.     log_it(1, "\n ■ Retrieving current newsgroup listing from %s.",
  2035.            serverhost);
  2036.   else
  2037.     log_it(MOREINFO, "\n ■ %u newsgroup%s defined in NEWS.RC.", ngroups,
  2038.            ngroups == 1 ? "" : "s");
  2039.  
  2040.   sprintf(fn, "%s\\NET.INI", maindir);
  2041.   if ((fp = fsh_open(fn, "rt")) == NULL)
  2042.     output("\n ■ Unable to read %s", fn);
  2043.   else {
  2044.     while (fgets(s, 100, fp)) {
  2045.       stripspace(s);
  2046.       if ((*s == ';') || (*s == '\n') || (*s == '['))
  2047.         continue;
  2048.       if (strlen(s) == 0)
  2049.         continue;
  2050.       if ((strnicmp(s, "POPNAME", 7) == 0) && (POPNAME[0] == 0)) {
  2051.         ss = strtok(s, "=");
  2052.         if (ss) {
  2053.           ss = strtok(NULL, "\n");
  2054.           trimstr1(ss);
  2055.           strcpy(POPNAME, ss);
  2056.         }
  2057.       }
  2058.       if (strnicmp(s, "FWDNAME", 7) == 0) {
  2059.         ss = strtok(s, "=");
  2060.         if (ss) {
  2061.           ss = strtok(NULL, "\n");
  2062.           trimstr1(ss);
  2063.           strcpy(POPNAME, ss);
  2064.         }
  2065.       }
  2066.       if ((strnicmp(s, "DOMAIN", 6) == 0) && (POPDOMAIN[0] == 0)) {
  2067.         ss = strtok(s, "=");
  2068.         if (ss) {
  2069.           ss = strtok(NULL, "\n");
  2070.           trimstr1(ss);
  2071.           strcpy(POPDOMAIN, ss);
  2072.         }
  2073.       }
  2074.       if (strnicmp(s, "FWDDOM", 6) == 0) {
  2075.         ss = strtok(s, "=");
  2076.         if (ss) {
  2077.           ss = strtok(NULL, "\n");
  2078.           trimstr1(ss);
  2079.           strcpy(POPDOMAIN, ss);
  2080.         }
  2081.       }
  2082.       if (strnicmp(s, "XPOSTS", 6) == 0) {
  2083.         ss = strtok(s, "=");
  2084.         if (ss) {
  2085.           ss = strtok(NULL, "\n");
  2086.           trimstr1(ss);
  2087.           crossposts = atoi(ss);
  2088.           if (crossposts > 99)
  2089.             crossposts = 10;
  2090.         }
  2091.       }
  2092.       if (strnicmp(s, "MORENEWS", 8) == 0) {
  2093.         ss = strtok(s, "=");
  2094.         if (ss) {
  2095.           ss = strtok(NULL, "\n");
  2096.           trimstr1(ss);
  2097.           if ((ss[0] == 'n') || (ss[0] == 'N'))
  2098.             MOREINFO = 0;
  2099.         }
  2100.       }
  2101.       if (strnicmp(s, "BINXPOST", 8) == 0) {
  2102.         ss = strtok(s, "=");
  2103.         if (ss) {
  2104.           ss = strtok(NULL, "\n");
  2105.           trimstr1(ss);
  2106.           if ((ss[0] == 'y') || (ss[0] == 'Y'))
  2107.             binxpost = 1;
  2108.         }
  2109.       }
  2110.       if (strnicmp(s, "NEWSRC_UPD", 9) == 0) {
  2111.         ss = strtok(s, "=");
  2112.         if (ss) {
  2113.           ss = strtok(NULL, "\n");
  2114.           trimstr1(ss);
  2115.           if ((ss[0] == 'y') || (ss[0] == 'Y'))
  2116.             newsrc_upd = 1;
  2117.         }
  2118.       }
  2119.       if (strnicmp(s, "NEWSNAME", 8) == 0) {
  2120.         ss = strtok(s, "=");
  2121.         if (ss) {
  2122.           ss = strtok(NULL, "\n");
  2123.           trimstr1(ss);
  2124.           strcpy(NEWSNAME, ss);
  2125.         }
  2126.       }
  2127.       if (strnicmp(s, "NEWSPASS", 8) == 0) {
  2128.         ss = strtok(s, "=");
  2129.         if (ss) {
  2130.           ss = strtok(NULL, "\n");
  2131.           trimstr1(ss);
  2132.           strcpy(NEWSPASS, ss);
  2133.         }
  2134.       }
  2135.       ss = NULL;
  2136.     }
  2137.     if (fp != NULL)
  2138.       fclose(fp);
  2139.   }
  2140.   ok = 1;
  2141.   if ((nntp_sock = netsocket(serverhost)) != NULL) {
  2142.     bAuth = 1;
  2143.     if (NEWSNAME[0]) {
  2144.       if (!sendauthinfo(nntp_sock))
  2145.         bAuth = 0;
  2146.     }
  2147.     if (bAuth)
  2148.       ok = getnews(nntp_sock);
  2149.     if (ok)
  2150.       log_it(1, "\n ■ NEWS exited with an error.");
  2151.     else
  2152.       log_it(1, "\n ■ Normal NEWS completion.");
  2153.   }
  2154.   cd_to(maindir);
  2155.   log_it(1, "\n ■ NEWS completed processing %d newsgroups", ngroups);
  2156.   if (grouprec)
  2157.     farfree(grouprec);
  2158.   grouprec = NULL;
  2159.   check_packets();
  2160.   fcloseall();
  2161.   cursor('R');
  2162.   return ok;
  2163. }
  2164.