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