home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B77.ZIP / NEWS.CPP < prev    next >
Text File  |  1997-12-14  |  58KB  |  2,123 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 = (9*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 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, 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. unsigned char *trimstr1(unsigned 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. void treat(char *str)
  732. {
  733.   char *obuf, *nbuf;
  734.  
  735.   if (*str) {
  736.     for (obuf = str, nbuf = str; *obuf && obuf; ++obuf) {
  737.       if (((*obuf >= 32) && (*obuf <= 126)) || (*obuf == 10) || (*obuf == 9)) {
  738.         *nbuf++ = *obuf;
  739.       }
  740.     }
  741.     *nbuf = 0;
  742.   }
  743. }
  744.  
  745. char *name_packet(char *pktname)
  746. {
  747.   int i;
  748.  
  749.   for (i = 0; i < 1000; i++) {
  750.     sprintf(pktname, "%sP0-%5.5d.%3.3d", net_data, i, instance);
  751.     if (!exist(pktname))
  752.       break;
  753.   }
  754.   return pktname;
  755. }
  756.  
  757. int jgets(char *s, int maxlen)
  758. {
  759.   char temp[500];
  760.   int done = 0, pos = 0, len = 0, i, c, zeroflag;
  761.  
  762.   while (!done) {
  763.     zeroflag = 0;
  764.     if ((c = getch()) == 0) {
  765.       zeroflag = 1;
  766.       c = getch();
  767.     }
  768.     switch (c) {
  769.       case 27:
  770.         return 0;
  771.       case 8:
  772.         if (c == 8) {
  773.           if (pos == 0)
  774.             break;
  775.           if (pos != len) {
  776.             for (i = pos - 1; i < len; i++)
  777.               temp[i] = temp[i + 1];
  778.             pos--;
  779.             len--;
  780.             putch(8);
  781.             for (i = pos; i < len; i++)
  782.               putch(temp[i]);
  783.             for (i = len; i >= pos; i--)
  784.               putch(8);
  785.           } else {
  786.             putch(8);
  787.             pos = --len;
  788.           }
  789.           break;
  790.         }
  791.       case 13:
  792.         if (c == 13) {
  793.           done = 1;
  794.           break;
  795.         }
  796.       default:
  797.         if (zeroflag)
  798.           break;
  799.         if (c == 0 || pos == maxlen)
  800.           break;
  801.         if (pos == len) {
  802.           temp[pos++] = c;
  803.           if (pos > len)
  804.             len++;
  805.           putch(c);
  806.         } else {
  807.           for (i = len++; i >= pos; i--)
  808.             temp[i + 1] = temp[i];
  809.           temp[pos++] = c;
  810.           putch(c);
  811.           for (i = pos; i < len; i++)
  812.             putch(temp[i]);
  813.           for (i = len; i > pos; i--)
  814.             putch(8);
  815.         }
  816.     }
  817.   }
  818.   temp[len] = '\0';
  819.   strcpy(s, temp);
  820.   return len;
  821. }
  822.  
  823.  
  824. int savebody(Mail_Socket * NNTP_sock, char *fn, int cug, unsigned long cur_article, int *abort)
  825. {
  826.   int i, f, place, len, curpos, count, part;
  827.   char _big_temp_buffer[BIGSTR];
  828.   char ch, spin[15], buf[STRING];
  829.   unsigned long msgsize;
  830.  
  831.   place = count = 0;
  832.   *_temp_buffer = 0;
  833.   part = 1;
  834.  
  835.   strcpy(spin, "||//--\\\\");
  836.   len = strlen(spin);
  837.  
  838.   if (spooltodisk)
  839.     f = sh_open(fn, O_RDWR | O_APPEND | O_TEXT | O_CREAT, S_IREAD | S_IWRITE);
  840.   else
  841.     f = sh_open(fn, O_RDWR | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE);
  842.  
  843.   if (f < 0) {
  844.     log_it(MOREINFO, "\n ■ Unable to write to %s.", fn);
  845.     return 0;
  846.   }
  847.  
  848.   sock_printf(NNTP_sock->sock, "BODY %ld", cur_article);
  849.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  850.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  851.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  852.   switch (reply) {
  853.     case 423:
  854.       return 0;
  855.     case 222:
  856.       sh_write(f, "\n", sizeof(char));
  857.       if (spooltodisk) {
  858.         if (MOREINFO)
  859.           output("\n ■ NEWS%d.UUE - [Esc], [Space], [Tab], ] = +10, } = +100, + = User Input [-]", cug);
  860.         for (i = 0; i < 79; i++)
  861.           sh_write(f, "-", sizeof(char));
  862.         sh_write(f, "\n", sizeof(char));
  863.         sprintf(buf, "Art  : %lu\n", cur_article);
  864.         sh_write(f, buf, strlen(buf));
  865.         if (*cur_lines) {
  866.           if ((atol(cur_lines) > 0L) && (atol(cur_lines) < 99999L))
  867.             sprintf(buf, "Lines: %s\n", cur_lines);
  868.           else
  869.             sprintf(buf, "Lines: Unknown\n");
  870.         }
  871.         sh_write(f, buf, strlen(buf));
  872.         sprintf(buf, "Group: %s\n", grouprec[cug].groupname);
  873.         sh_write(f, buf, strlen(buf));
  874.         if (*cur_date)
  875.           sprintf(buf, "Date : %s\n", cur_date);
  876.         else
  877.           sprintf(buf, "Date : Unknown\n");
  878.         sh_write(f, buf, strlen(buf));
  879.         if (*cur_replyto)
  880.           sprintf(buf, "From : %s\n", cur_replyto);
  881.         else
  882.           sprintf(buf, "From : Unknown\n");
  883.         sh_write(f, buf, strlen(buf));
  884.         if (*cur_subject)
  885.           sprintf(buf, "Subj : %s\n\n", cur_subject);
  886.         else
  887.           sprintf(buf, "Subj : Unknown\n");
  888.         sh_write(f, buf, strlen(buf));
  889.       } else {
  890.         if (*cur_replyto) {
  891.           sprintf(buf, "0RReply-To: %s\n", cur_replyto);
  892.           sh_write(f, buf, strlen(buf));
  893.         }
  894.         if (*cur_message_ID) {
  895.           sprintf(buf, "0RMessage-ID: %s\n", cur_message_ID);
  896.           sh_write(f, buf, strlen(buf));
  897.         }
  898.         if (*cur_references) {
  899.           sprintf(buf, "0RReferences: %s\n", cur_references);
  900.           sh_write(f, buf, strlen(buf));
  901.         }
  902.         if (MOREINFO)
  903.           output("\n ■ Receiving message - [Esc] Abort - [Space] Skip Group - [Tab] Catch Up [-]");
  904.         else
  905.           output(" [-]");
  906.       }
  907.       msgsize = 0L;
  908.       curpos = 0L;
  909.       while (1) {
  910.         while (kbhit()) {
  911.           ch = (getch());
  912.           strcpy(spin, "..oOOOo.");
  913.           len = strlen(spin);
  914.           switch (ch) {
  915.             case '+':
  916.               if (spooltodisk) {
  917.                 if (MOREINFO) {
  918.                   backline();
  919.                   log_it(MOREINFO, "\r ■ Jumping forward... wait until message is completed [.]");
  920.                 }
  921.                 *abort = 6;
  922.               }
  923.               break;
  924.             case '}':
  925.               if (spooltodisk) {
  926.                 if (MOREINFO) {
  927.                   backline();
  928.                   log_it(MOREINFO, "\r ■ Jumping 100 articles... wait until message is completed [.]");
  929.                 }
  930.                 *abort = 5;
  931.               }
  932.               break;
  933.             case ']':
  934.               if (spooltodisk) {
  935.                 if (MOREINFO) {
  936.                   backline();
  937.                   log_it(MOREINFO, "\r ■ Jumping 10 articles... wait until message is completed [.]");
  938.                 }
  939.                 *abort = 4;
  940.               }
  941.               break;
  942.             case 9:
  943.               if (MOREINFO) {
  944.                 backline();
  945.                 log_it(MOREINFO, "\r ■ Catching up on group... wait until message is completed [.]");
  946.               }
  947.               *abort = 3;
  948.               break;
  949.             case 32:
  950.               if (MOREINFO) {
  951.                 backline();
  952.                 log_it(MOREINFO, "\r ■ Skipping group... wait until message is completed [.]");
  953.               }
  954.               *abort = 2;
  955.               break;
  956.             case 27:
  957.               if (MOREINFO) {
  958.                 backline();
  959.                 log_it(MOREINFO, "\r ■ Aborting session... wait until message is completed [.]");
  960.               }
  961.               *abort = 1;
  962.               break;
  963.             default:
  964.               break;
  965.           }
  966.         }
  967.         if (count++ > 5) {
  968.           output("\b\b%c]", spin[place++]);
  969.           if (len)
  970.             place %= len;
  971.           count = 0;
  972.         }
  973.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  974.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  975.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0)
  976.           break;
  977.         else {
  978.           treat(_big_temp_buffer);
  979.           strcat(_big_temp_buffer, "\n");
  980.           curpos = strlen(_big_temp_buffer);
  981.           sh_write(f, _big_temp_buffer, strlen(_big_temp_buffer));
  982.           msgsize += curpos;
  983.         }
  984.         if ((msgsize > 30000L) && (!spooltodisk)) {
  985.           strcpy(buf, "\nContinued in next message...\n");
  986.           sh_write(f, buf, strlen(buf));
  987.           sh_close(f);
  988.           sprintf(fn, "%sSPOOL\\INPUT%d.MSG", net_data, ++part);
  989.           unlink(fn);
  990.           if ((f = sh_open(fn, O_WRONLY | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE)) < 0) {
  991.             log_it(MOREINFO, "\n ■ Unable to create %s", fn);
  992.             sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  993.             return 0;
  994.           }
  995.           if (MOREINFO) {
  996.             backline();
  997.             output("\r ■ Breaking into %d%s part [ ]", (part), ordinal_text(part));
  998.           }
  999.           strcpy(buf, "\nContinued from previous message...\n");
  1000.           sh_write(f, buf, strlen(buf));
  1001.           msgsize = strlen(buf);
  1002.         }
  1003.       }
  1004.       sh_close(f);
  1005.       output("\b\bX]");
  1006.       return 1;
  1007.     default:
  1008.       log_it(MOREINFO, "\n ■ Unknown BODY error : %s", _temp_buffer);
  1009.       break;
  1010.   }
  1011.   sh_close(f);
  1012.   SOCK_READ_ERR(NNTP);
  1013.   return 0;
  1014. }
  1015.  
  1016. int extract(char *to, char *key, char *from)
  1017. {
  1018.   if (!strnicmp(from, key, strlen(key))) {
  1019.     from += strlen(key);
  1020.     while (*from == ' ')
  1021.       from++;
  1022.     strcpy(to, from);
  1023.     return 1;
  1024.   } else
  1025.     return 0;
  1026. }
  1027.  
  1028.  
  1029. int chead(Mail_Socket * NNTP_sock, unsigned long which)
  1030. {
  1031.   char _big_temp_buffer[BIGSTR];
  1032.  
  1033.   *cur_path = 0;
  1034.   *cur_from = 0;
  1035.   *cur_replyto = 0;
  1036.   *cur_subject = 0;
  1037.   *cur_newsgroups = 0;
  1038.   *cur_message_ID = 0;
  1039.   *cur_references = 0;
  1040.   *cur_date = 0;
  1041.   *_temp_buffer = 0;
  1042.  
  1043.   sock_printf(NNTP_sock->sock, "HEAD %lu", which);
  1044.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1045.   sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1046.   sscanf(_big_temp_buffer, "%hu %lu", &reply, &reparticle);
  1047.   switch (reply) {
  1048.     case 423:
  1049.       output("%-55.55s", "Expired article.");
  1050.       return 0;
  1051.     case 221:
  1052.       while (1) {
  1053.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1054.         sock_gets(NNTP_sock->sock, _big_temp_buffer, sizeof(_big_temp_buffer));
  1055.         if (_big_temp_buffer[0] == '.' && _big_temp_buffer[1] == 0) {
  1056.           if (cur_replyto[0] == 0)
  1057.             strcpy(cur_replyto, cur_from);
  1058.           return 1;
  1059.         }
  1060.         if ((strlen(_big_temp_buffer)) > STRING)
  1061.           _big_temp_buffer[STRING] = '\0';
  1062.         if (extract(cur_path, "Path:", _big_temp_buffer) ||
  1063.              extract(cur_from, "From:", _big_temp_buffer) ||
  1064.              extract(cur_subject, "Subject:", _big_temp_buffer) ||
  1065.              extract(cur_replyto, "Reply-To:", _big_temp_buffer) ||
  1066.              extract(cur_newsgroups, "Newsgroups:", _big_temp_buffer) ||
  1067.              extract(cur_organization, "Organization:", _big_temp_buffer) ||
  1068.              extract(cur_message_ID, "Message-ID:", _big_temp_buffer) ||
  1069.              extract(cur_references, "References:", _big_temp_buffer) ||
  1070.              extract(cur_lines, "Lines:", _big_temp_buffer) ||
  1071.              extract(cur_date, "Date:", _big_temp_buffer))
  1072.          continue;
  1073.       }
  1074.     default:
  1075.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1076.       log_it(MOREINFO, "\n ■ Unknown chead error : %s", _big_temp_buffer);
  1077.       break;
  1078.   }
  1079.   SOCK_READ_ERR(NNTP);
  1080.   return 0;
  1081. }
  1082.  
  1083. Mail_Socket *netsocket(char *host)
  1084. {
  1085.   Mail_Socket *NNTP_sock = NULL;
  1086.   longword h;
  1087.  
  1088.   sock_init();
  1089.  
  1090.   if (!(h = resolve(host))) {
  1091.     if (!(h = resolve(host))) {
  1092.       log_it(1, "\n ■ Error : Cannot resolve host %s", host);
  1093.       return NULL;
  1094.     }
  1095.   }
  1096.   if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  1097.     log_it(1, "\n ■ Error: Insufficient memory to create socket... aborting.");
  1098.     return NULL;
  1099.   }
  1100.   if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  1101.     log_it(1, "\n ■ Error : Insufficient memory to create socket... aborting.");
  1102.     farfree(NNTP_sock);
  1103.     return NULL;
  1104.   }
  1105.   if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
  1106.     log_it(1, "\n ■ Error : Unable to connect to to %s.", host);
  1107.     farfree(NNTP_sock);
  1108.     return NULL;
  1109.   }
  1110.   sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
  1111.   sock_wait_established(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1112.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1113.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1114.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1115.   switch (reply) {
  1116.     case 200:
  1117.       log_it(MOREINFO, "\n ■ Connection to %s accepted...", host);
  1118.       return (NNTP_sock);
  1119.     case 502:
  1120.       log_it(1, "\n ■ Connection to %s refused... try again later.", host);
  1121.       break;
  1122.     case 503:
  1123.       log_it(1, "\n ■ NNTP service unavailable. Connection to %s refused.", host);
  1124.       break;
  1125.     default:
  1126.       log_it(1, "\n ■ Unknown NNTP error. Connection to %s failed.", host);
  1127.       break;
  1128.   }
  1129.   SOCK_READ_ERR(NNTP);
  1130.   return 0;
  1131. }
  1132.  
  1133. int cnext(Mail_Socket * NNTP_sock)
  1134. {
  1135.   char *p, *q;
  1136.  
  1137.   *_temp_buffer = 0;
  1138.  
  1139.   sock_printf(NNTP_sock->sock, "NEXT");
  1140.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1141.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1142.   sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1143.   switch (reply) {
  1144.     case 421:
  1145.       return 0;
  1146.     case 223:
  1147.       p = _temp_buffer;
  1148.       q = cur_articleid;
  1149.       while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
  1150.         p++;
  1151.       while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
  1152.         *q++ = *p++;
  1153.       *q++ = '>';
  1154.       *q++ = '\0';
  1155.       return 1;
  1156.     default:
  1157.       sock_wait_closed(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1158.       log_it(MOREINFO, "\n ■ Unknown NEXT error : %s", _temp_buffer);
  1159.       break;
  1160.   }
  1161.   SOCK_READ_ERR(NNTP);
  1162.   return 0;
  1163. }
  1164.  
  1165. int cslave(Mail_Socket * NNTP_sock)
  1166. {
  1167.   char junk[STRING];
  1168.  
  1169.   *_temp_buffer = 0;
  1170.  
  1171.   sock_printf(NNTP_sock->sock, "SLAVE");
  1172.   sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1173.   sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1174.   sscanf(_temp_buffer, "%hu %s", &reply, junk);
  1175.   SOCK_READ_ERR(NNTP);
  1176.   return 0;
  1177. }
  1178.  
  1179. char *stripspace(char *str)
  1180. {
  1181.   char *obuf, *nbuf;
  1182.  
  1183.   if (str) {
  1184.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  1185.       if (!isspace(*obuf))
  1186.         *nbuf++ = *obuf;
  1187.     }
  1188.     *nbuf = NULL;
  1189.   }
  1190.   return (str);
  1191. }
  1192.  
  1193.  
  1194. int saveactive(Mail_Socket * NNTP_sock, int forced)
  1195. {
  1196.   char s[181], fn[MAXPATH], group[256], act;
  1197.   unsigned long to, from;
  1198.   unsigned fnday, fnmonth, fnyear, fnhour, fnminute, fnsecond;
  1199.   int f, done, update;
  1200.   unsigned long count;
  1201.   FILE *fp;
  1202.   struct ftime ft;
  1203.   struct date d;
  1204.  
  1205.   *_temp_buffer = 0;
  1206.  
  1207.   update = 0;
  1208.  
  1209.   sprintf(fn, "%sNEWSRC", net_data);
  1210.  
  1211.   f = sh_open1(fn, O_RDONLY);
  1212.   if (f < 0)
  1213.     forced = 1;
  1214.   else {
  1215.     getftime(f, &ft);
  1216.     sh_close(f);
  1217.   }
  1218.  
  1219.   if (!forced) {
  1220.     fnsecond = 2 * ft.ft_tsec;
  1221.     fnminute = ft.ft_min;
  1222.     fnhour = ft.ft_hour;
  1223.     fnday = ft.ft_day;
  1224.     fnmonth = ft.ft_month;
  1225.     fnyear = ft.ft_year + 80;
  1226.     log_it(1, "\n ■ NEWSRC last updated %02u/%02u/%02u %.2u:%.2u:%.2u... ",
  1227.         fnmonth, fnday, fnyear, fnhour, fnminute, fnsecond);
  1228.     getdate(&d);
  1229.     if ((d.da_day == ft.ft_day) && (d.da_mon == ft.ft_month) &&
  1230.         (d.da_year == (ft.ft_year + 1980))) {
  1231.       log_it(1, "no update required.");
  1232.       return 0;
  1233.     } else
  1234.       log_it(1, "updating.");
  1235.     if ((d.da_mon == ft.ft_month) && (d.da_day != ft.ft_day))
  1236.       update = 1;
  1237.   }
  1238.  
  1239.   if (update)
  1240.     fp = fsh_open(fn, "a+");
  1241.   else
  1242.     fp = fsh_open(fn, "w+");
  1243.  
  1244.   if (fp == NULL) {
  1245.     perror(fn);
  1246.     return 0;
  1247.   }
  1248.   done = 0;
  1249.  
  1250.   while (!done) {
  1251.     if (update) {
  1252.       sprintf(s, "NEWGROUPS %02.02u%02.02u%02.02u %02.02u%02.02u%02.02u",
  1253.             fnyear, fnmonth, fnday, fnhour, fnminute, fnsecond);
  1254.       sock_printf(NNTP_sock->sock, s);
  1255.       output("\n ■ Refreshing NEWSRC newsgroup listing :           ");
  1256.     } else {
  1257.       sock_printf(NNTP_sock->sock, "LIST");
  1258.       output("\n ■ Retrieving current newsgroup listing :           ");
  1259.     }
  1260.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1261.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1262.     sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
  1263.     if ((reply == 215) || (reply == 231)) {
  1264.       count = 0L;
  1265.       while (1) {
  1266.         sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1267.         sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1268.         if (*_temp_buffer == '.' && _temp_buffer[1] == 0)
  1269.           break;
  1270.         if (strlen(_temp_buffer) < 180) {
  1271.           if (count++ % 25 == 24L)
  1272.             output("\b\b\b\b\b\b\b%-7lu", count);
  1273.           sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
  1274.           sprintf(s, "%s %lu\n", group, to);
  1275.           fsh_write(s, sizeof(char), strlen(s), fp);
  1276.         }
  1277.       }
  1278.       if (update) {
  1279.         if (count == 0) {
  1280.           log_it(1, "\b\b\b\b\b\b\bNo new groups.");
  1281.         } else
  1282.           log_it(1, "\b\b\b\b\b\b\b%lu group%s added to NEWSRC.", count,
  1283.                 count == 1 ? "" : "s");
  1284.       } else
  1285.         output("\b\b\b\b\b\b\b%lu total groups.", count);
  1286.  
  1287.       if (fp != NULL)
  1288.         fclose(fp);
  1289.       if (update) {
  1290.         f = sh_open1(fn, O_RDWR);
  1291.         ft.ft_day = d.da_day;
  1292.         setftime(f, &ft);
  1293.         sh_close(f);
  1294.       }
  1295.       return 1;
  1296.     } else {
  1297.       if (reply == 480) {
  1298.         if (fp != NULL)
  1299.           fclose(fp);
  1300.         return 0;
  1301.       } else {
  1302.         log_it(MOREINFO, "\n ■ Unknown saveactive error : %s", _temp_buffer);
  1303.         if (fp != NULL)
  1304.           fclose(fp);
  1305.         done = 1;
  1306.       }
  1307.     }
  1308.   }
  1309.   if (fp != NULL)
  1310.     fclose(fp);
  1311.   SOCK_READ_ERR(NNTP);
  1312.   return 0;
  1313. }
  1314.  
  1315. int checkx(int cug)
  1316. {
  1317.   char *ptr, *ptr2, *ptr3, fn[MAXPATH], buf[256], tmp[81];
  1318.   int i, spam, max, ok;
  1319.   FILE *fp;
  1320.  
  1321.   if ((stristr(grouprec[cug].groupname, "binaries")) && (binxpost))
  1322.     return 1;
  1323.  
  1324.   sprintf(buf, "%s!%s", POPNAME, POPDOMAIN);
  1325.   if (stristr(cur_path, buf) != 0) {
  1326.     sprintf(buf, "Local post - %s - skipped.", cur_subject);
  1327.     output("%-55.55s", buf);
  1328.     return 0;
  1329.   }
  1330.  
  1331.   ok = 1;
  1332.   spam = 0;
  1333.   sprintf(fn, "%sNOSPAM.TXT", net_data);
  1334.   if ((fp = fsh_open(fn, "r")) != NULL) {
  1335.     while ((!feof(fp)) && (!spam)) {
  1336.       fgets(buf, 80, fp);
  1337.       trimstr1(buf);
  1338.       if (strlen(buf) > 2) {
  1339.         if (buf[0] == '\"') {
  1340.           strcpy(tmp, &(buf[1]));
  1341.           LAST(tmp) = '\0';
  1342.           strcpy(buf, tmp);
  1343.         }
  1344.         if (buf[0] == '[') {
  1345.           if ((strnicmp(buf, "[GLOBAL]", 8) == 0) || (strnicmp(buf, "[NEWS]", 6) == 0))
  1346.             ok = 1;
  1347.           else
  1348.             ok = 0;
  1349.         }
  1350.         if ((ok) && ((stristr(cur_subject, buf)) || (stristr(cur_from, buf)))) {
  1351.           sprintf(tmp, "SPAM - %s - skipped.", buf);
  1352.           output("%-55.55s", tmp);
  1353.           spam = 1;
  1354.         }
  1355.       }
  1356.     }
  1357.     fclose(fp);
  1358.     if (spam)
  1359.       return 0;
  1360.   }
  1361.  
  1362.   ptr = cur_newsgroups;
  1363.  
  1364.   while (*ptr == ' ')
  1365.     ptr++;
  1366.   max = 0;
  1367.   while (1) {
  1368.     if (*ptr == 0)
  1369.       break;
  1370.     if (*ptr == ',') {
  1371.       ptr++;
  1372.       ++max;
  1373.     }
  1374.     while (*ptr == ' ')
  1375.       ptr++;
  1376.     ptr2 = ptr;
  1377.     ptr3 = buf;
  1378.     while (*ptr2 != 0 && *ptr2 != ',')
  1379.       *ptr3++ = *ptr2++;
  1380.     *ptr3 = 0;
  1381.     while (*(--ptr3) == ' ')
  1382.       *ptr3 = 0;
  1383.     ptr = ptr2;
  1384.     for (i = 0; i < cug; i++) {
  1385.       if (strcmpi(buf, grouprec[i].groupname) == 0) {
  1386.         output("Already posted in %s.", grouprec[i].groupname);
  1387.         return 0;
  1388.       }
  1389.     }
  1390.     if ((crossposts > 0) && (max > crossposts)) {
  1391.       sprintf(buf, "Crossposted to more than %d newsgroups.", crossposts);
  1392.       output("%-55.55s", buf);
  1393.       return 0;
  1394.     }
  1395.   }
  1396.   return 1;
  1397. }
  1398.  
  1399. unsigned char *strrep(char *str, char old, char New)
  1400. {
  1401.   int i;
  1402.  
  1403.   for (i = 0; str[i]; i++)
  1404.     if (str[i] == old)
  1405.       str[i] = New;
  1406.   return (str);
  1407. }
  1408.  
  1409.  
  1410.  
  1411. int postnews(Mail_Socket * NNTP_sock, int cug)
  1412. {
  1413.   char s[160], s1[12], s2[5], fn[MAXPATH], *ss;
  1414.   int f1, nlines;
  1415.   FILE *fp;
  1416.   struct ffblk ff;
  1417.  
  1418.   *_temp_buffer = 0;
  1419.  
  1420.   sprintf(fn, "%sOUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
  1421.   f1 = findfirst(fn, &ff, 0);
  1422.   if (f1 != 0) {
  1423.     log_it(MOREINFO, "\n ■ No outbound news articles to post...");
  1424.     return 1;
  1425.   }
  1426.   while (f1 == 0) {
  1427.     sock_printf(NNTP_sock->sock, "POST");
  1428.     sock_wait_input(NNTP_sock->sock, sock_delay, NULL, &NNTP_stat);
  1429.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1430.     if (*_temp_buffer != '3') {
  1431.       if (atoi(_temp_buffer) == 440)
  1432.         log_it(MOREINFO, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
  1433.       else
  1434.         log_it(1, "\n ■ Remote error: %s", _temp_buffer);
  1435.       return 0;
  1436.     }
  1437.     if (MOREINFO)
  1438.       output("\n");
  1439.     else
  1440.       backline();
  1441.     output(" ■ Posting article to %s\n", grouprec[cug].groupname);
  1442.     sprintf(s, "%sOUTBOUND\\%s", net_data, ff.ff_name);
  1443.     if ((fp = fsh_open(s, "r")) == NULL) {
  1444.       log_it(MOREINFO, "\n ■ Error reading %s.", s);
  1445.       return 1;
  1446.     }
  1447.     nlines = 0;
  1448.     while (!feof(fp)) {
  1449.       fgets(_temp_buffer, sizeof(_temp_buffer), fp);
  1450.       strrep(_temp_buffer, '\n', '\0');
  1451.       strrep(_temp_buffer, '\r', '\0');
  1452.       if (*_temp_buffer == '.') {
  1453.         movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
  1454.         *_temp_buffer = '.';
  1455.       }
  1456.       sock_puts(NNTP_sock->sock, _temp_buffer);
  1457.       if (MOREINFO) {
  1458.         backline();
  1459.         output("\r ■ Lines sent : %d", ++nlines);
  1460.       }
  1461.     }
  1462.     fclose(fp);
  1463.     sock_puts(NNTP_sock->sock, ".");
  1464.     if (MOREINFO)
  1465.       output("\n ■ Awaiting acknowledgement - may take several minutes...");
  1466.     log_it(MOREINFO, "\n ■ Server response : ");
  1467.     sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
  1468.     sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1469.     log_it(MOREINFO, "%s", _temp_buffer);
  1470.     if (*_temp_buffer != '2') {
  1471.       sscanf(_temp_buffer, "%hu %s", &reply, s);
  1472.       if (reply == 441) {
  1473.         ss = strtok(_temp_buffer, " ");
  1474.         ss = strtok(NULL, " ");
  1475.         if (atoi(ss) == 435)
  1476.           unlink(s);
  1477.         fnsplit(s, NULL, NULL, s1, s2);
  1478.         log_it(MOREINFO, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
  1479.         if (unlink(s) == 0)
  1480.           log_it(MOREINFO, "\n ■ Deleted message - %s", s);
  1481.       } else
  1482.         log_it(MOREINFO, "\n ■ Remote error: %s", _temp_buffer);
  1483.     } else {
  1484.       if (unlink(s) == 0)
  1485.         log_it(MOREINFO, "\n ■ Deleted sent message - %s", s);
  1486.     }
  1487.     f1 = findnext(&ff);
  1488.   }
  1489.   SOCK_READ_ERR(NNTP);
  1490.   return 0;
  1491. }
  1492.  
  1493. void get_subtype(int sub, char *where)
  1494. {
  1495.   int ok, which;
  1496.   char fn[181], s[81], net_name[21], *ss;
  1497.   FILE *fp;
  1498.  
  1499.   where[0] = 0;
  1500.   which = sub;
  1501.   sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
  1502.   if ((fp = fsh_open(fn, "r")) == NULL)
  1503.     return;
  1504.   ok = 0;
  1505.   while (fgets(s, 80, fp)) {
  1506.     if (*s == '!') {
  1507.       if (which == atoi(&s[1]))
  1508.         ok = 1;
  1509.     }
  1510.     if (ok && (*s == '$')) {
  1511.       ss = strtok(s, " ");
  1512.       strcpy(net_name, &ss[1]);
  1513.       ss = strtok(NULL, " ");
  1514.       if (fp != NULL)
  1515.         fclose(fp);
  1516.       if ((stricmp(net_name, "FILENET") == 0)) {
  1517.         trimstr1(ss);
  1518.         strcpy(where, ss);
  1519.         return;
  1520.       } else
  1521.         return;
  1522.     }
  1523.   }
  1524.   if (fp != NULL)
  1525.     fclose(fp);
  1526. }
  1527.  
  1528. unsigned long max_on_sub(int cug)
  1529. {
  1530.   int i, num_subs;
  1531.   char fn[MAXPATH], subtype[12];
  1532.   unsigned long max;
  1533.   subboardrec sub;
  1534.   FILE *fp;
  1535.  
  1536.   max = 0;
  1537.   if ((strlen(grouprec[cug].subtype) == 1) && (*grouprec[cug].subtype == '0'))
  1538.     return max;
  1539.   if (MOREINFO)
  1540.     output("\n ■ Max articles -     ");
  1541.   else
  1542.     output(" -     ");
  1543.   sprintf(fn, "%sSUBS.DAT", syscfg.datadir);
  1544.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  1545.     log_it(MOREINFO, "\n ■ Unable to read %s.", fn);
  1546.     return max;
  1547.   } else {
  1548.     fseek(fp, 0L, SEEK_END);
  1549.     num_subs = (int) (ftell(fp) / sizeof(subboardrec));
  1550.     for (i = 0; i < num_subs; i++) {
  1551.       output("\b\b\b\b%-4d", i);
  1552.       fseek(fp, (long) i * sizeof(subboardrec), SEEK_SET);
  1553.       fread(&sub, sizeof(subboardrec), 1, fp);
  1554.       get_subtype(i, subtype);
  1555.       if (stricmp(subtype, grouprec[cug].subtype) == 0) {
  1556.         max = (unsigned long)sub.maxmsgs;
  1557.         if (MOREINFO)
  1558.           output("\b\b\b\b%s (%s) - %lu posts.", sub.name, subtype, max);
  1559.         else
  1560.           output("\b\b\b\b [%lu max]", max);
  1561.         break;
  1562.       }
  1563.     }
  1564.   }
  1565.   fclose(fp);
  1566.   return max;
  1567. }
  1568.  
  1569.  
  1570. int getnews(Mail_Socket * NNTP_sock)
  1571. {
  1572.   int f1, nup, nug, cug, jug, abort, ok, done, firstrun, parts, skipped, bogus;
  1573.   char *p, fn[MAXPATH], mailname[121], reline[121], pktname[MAXPATH];
  1574.   char ch, orig_subj[STRING], buf[STR];
  1575.   unsigned int text_len;
  1576.   unsigned short temptype;
  1577.   unsigned long new_articles, max_articles, skip_articles;
  1578.   net_header_rec nh;
  1579.   struct ffblk ff;
  1580.   FILE *fp;
  1581.  
  1582.   abort = cug = bogus = 0;
  1583.  
  1584.   if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
  1585.     if (!saveactive(NNTP_sock, 1)) {
  1586.       if (sendauthinfo(NNTP_sock))
  1587.         saveactive(NNTP_sock, 1);
  1588.     }
  1589.   } else
  1590.     saveactive(NNTP_sock, 0);
  1591.  
  1592.   nug = jug = nup = 1;
  1593.   skipped = 0;
  1594.   cslave(NNTP_sock);
  1595.   name_packet(pktname);
  1596.   if (!MOREINFO)
  1597.     output("\n ■ [Esc] aborts session - [Space] skips group - [Tab] catches up group");
  1598.   while ((cug < ngroups) && (!abort)) {
  1599.     if (*grouprec[cug].groupname == ';') {
  1600.       log_it(1, "\n ■ Skipping commented group - %s", &(grouprec[cug].groupname[1]));
  1601.       ++cug;
  1602.       jug = 1;
  1603.       continue;
  1604.     }
  1605.     if (*grouprec[cug].subtype == '0')
  1606.       spooltodisk = 1;
  1607.     else
  1608.       spooltodisk = 0;
  1609.     if (nup) {
  1610.       nup = 0;
  1611.       write_groups(1);
  1612.       if (*grouprec[cug].subtype != '0')
  1613.         name_packet(pktname);
  1614.     }
  1615.     if (nug) {
  1616.       jug = 1;
  1617.       nug = 0;
  1618.       while (1) {
  1619.         if (!cgroup(NNTP_sock, grouprec[cug].groupname)) {
  1620.           if (cgroup(NNTP_sock, grouprec[cug].groupname) == -1) {
  1621.             if (sendauthinfo(NNTP_sock))
  1622.               continue;
  1623.             else
  1624.               return 1;
  1625.           }
  1626.           if (++bogus > 10) {
  1627.             log_it(1, "\n ■ Too many invalid newsgroups... NEWS.RC error?");
  1628.             return 1;
  1629.           }
  1630.           log_it(1, "\n ■ Error accessing - %s", grouprec[cug].groupname);
  1631.           nug = 1;
  1632.           break;
  1633.         } else
  1634.           break;
  1635.       }
  1636.       if (!nug) {
  1637.         log_it(1, "\n ■ Requesting - %s - ", grouprec[cug].groupname);
  1638.         if (grouprec[cug].lastread < cur_first)
  1639.           grouprec[cug].lastread = cur_first;
  1640.         if (grouprec[cug].lastread >= cur_last) {
  1641.           grouprec[cug].lastread = cur_last;
  1642.           log_it(1, "[No new articles]");
  1643.           nug = jug = 1;
  1644.         } else {
  1645.           new_articles = cur_last - grouprec[cug].lastread;
  1646.           if (new_articles) {
  1647.             log_it(1, "[%lu new article%s]", new_articles,
  1648.                    new_articles == 1 ? "" : "s");
  1649.           } else {
  1650.             log_it(1, "[No new articles]");
  1651.             nug = jug = 1;
  1652.           }
  1653.           if (new_articles > 250L) {
  1654.             max_articles = max_on_sub(cug);
  1655.             if (max_articles && (new_articles > max_articles)) {
  1656.               log_it(MOREINFO, "\n ■ Requesting most recent %lu articles", max_articles);
  1657.               grouprec[cug].lastread = cur_last - max_articles;
  1658.             }
  1659.           } else
  1660.             ++grouprec[cug].lastread;
  1661.         }
  1662.         postnews(NNTP_sock, cug);
  1663.         if (!nug) {
  1664.           if (cur_numa == 0) {
  1665.             log_it(MOREINFO, "\n ■ No articles in %s...", grouprec[cug].groupname);
  1666.             nug = jug = 1;
  1667.             continue;
  1668.           }
  1669.         }
  1670.       }
  1671.     } else {
  1672.       if ((!cnext(NNTP_sock)) || (grouprec[cug].lastread > cur_last)) {
  1673.         log_it(MOREINFO, "\n ■ End of articles in %s", grouprec[cug].groupname);
  1674.         nug = jug = 1;
  1675.         write_groups(0);
  1676.       }
  1677.     }
  1678.     if (!nug) {
  1679.       if (skipped)
  1680.         backline();
  1681.       else
  1682.         output("\n");
  1683.  
  1684.       output(" ■ [%lu/%lu] : ", grouprec[cug].lastread, cur_last);
  1685.       if ((chead(NNTP_sock, grouprec[cug].lastread)) && (checkx(cug))) {
  1686.         skipped = 0;
  1687.         if (*cur_subject) {
  1688.           if (strlen(cur_subject) > 78)
  1689.             cur_subject[78] = 0;
  1690.           treat(cur_subject);
  1691.         } else
  1692.           strcpy(cur_subject, "No subject");
  1693.         strcpy(orig_subj, cur_subject);
  1694.         if (strlen(orig_subj) > 70)
  1695.           orig_subj[70] = 0;
  1696.         if (*cur_from) {
  1697.           treat(cur_from);
  1698.           if (strlen(cur_from) > 45)
  1699.             cur_from[45] = 0;
  1700.         } else
  1701.           strcpy(cur_from, "Unknown");
  1702.         if (*cur_replyto) {
  1703.           if (strlen(cur_replyto) > 45)
  1704.             cur_replyto[45] = 0;
  1705.         } else
  1706.           strcpy(cur_replyto, "Unknown");
  1707.         output("%-50.50s", orig_subj);
  1708.         if (spooltodisk)
  1709.           sprintf(fn, "%sSPOOL\\NEWS%d.UUE", net_data, cug);
  1710.         else
  1711.           sprintf(fn, "%sSPOOL\\INPUT1.MSG", net_data);
  1712.         abort = 0;
  1713.         fcloseall();
  1714.         ok = (savebody(NNTP_sock, fn, cug, grouprec[cug].lastread, &abort));
  1715.         if (ok && (*grouprec[cug].subtype != '0')) {
  1716.           if ((fp = fsh_open(pktname, "ab+")) == NULL) {
  1717.             log_it(MOREINFO, "\n ■ Unable to create %s!", pktname);
  1718.             return 1;
  1719.           }
  1720.           sprintf(fn, "%sSPOOL\\INPUT*.MSG", net_data);
  1721.           done = 1;
  1722.           parts = 0;
  1723.           if (findfirst(fn, &ff, 0) == 0) {
  1724.             parts = 1;
  1725.             done = 0;
  1726.           }
  1727.           firstrun = 1;
  1728.           while (!done) {
  1729.             sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1730.             f1 = sh_open1(fn, O_RDONLY | O_BINARY);
  1731.             text_len = (unsigned int) filelength(f1);
  1732.             if (text_len > 32000L) {
  1733.               log_it(MOREINFO, "\n ■ Truncating %lu bytes from input file",
  1734.                      text_len - 32000L);
  1735.               text_len = 32000;
  1736.             }
  1737.             if ((p = (char *) malloc(32767)) == NULL) {
  1738.               log_it(MOREINFO, "\n ■ Insufficient memory to read entire message");
  1739.               return 1;
  1740.             }
  1741.             sh_read(f1, (void *) p, text_len);
  1742.             sh_close(f1);
  1743.             temptype = atoi(grouprec[cug].subtype);
  1744.             nh.tosys = sy;
  1745.             nh.touser = 0;
  1746.             nh.fromsys = 32767;
  1747.             nh.fromuser = 0;
  1748.             if (!temptype) {
  1749.               nh.main_type = main_type_new_post;
  1750.               nh.minor_type = 0;
  1751.             } else {
  1752.               nh.main_type = main_type_pre_post;
  1753.               nh.minor_type = temptype;
  1754.             }
  1755.             nh.list_len = 0;
  1756.             ++cur_daten;
  1757.             nh.daten = cur_daten;
  1758.             nh.method = 0;
  1759.             if (parts > 1) {
  1760.               sprintf(buf, "%d%s ", parts, ordinal_text(parts));
  1761.               strncat(buf, orig_subj, 72);
  1762.               strcpy(cur_subject, buf);
  1763.               cur_subject[72] = '\0';
  1764.             }
  1765.             nh.length = text_len + strlen(cur_subject) + 1;
  1766.             strncpy(mailname, cur_replyto, 46);
  1767.             strcat(mailname, "\r\n");
  1768.             nh.length += strlen(mailname);
  1769.             if (nh.main_type == main_type_new_post)
  1770.               nh.length += strlen(grouprec[cug].subtype) + 1;
  1771.             if (firstrun) {
  1772.               firstrun = 0;
  1773.               if (strncmpi(cur_articleid, "re: ", 4) == 0) {
  1774.                 strncpy(reline, cur_articleid, 60);
  1775.                 sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
  1776.               }
  1777.             }
  1778.             if (!*cur_date) {
  1779.               strncpy(cur_date, ctime(&(time_t) nh.daten), 24);
  1780.               cur_date[24] = '\0';
  1781.             }
  1782.             strcat(cur_date, "\r\n");
  1783.             nh.length += strlen(cur_date);
  1784.             nh.length += strlen(cur_articleid);
  1785.             fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  1786.             if (nh.main_type == main_type_new_post)
  1787.               fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
  1788.             fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
  1789.             fsh_write(mailname, sizeof(char), strlen(mailname), fp);
  1790.             fsh_write(cur_date, sizeof(char), strlen(cur_date), fp);
  1791.             fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
  1792.             fsh_write(p, sizeof(char), text_len, fp);
  1793.             if (p)
  1794.               free(p);
  1795.             unlink(fn);
  1796.             if (findnext(&ff) == 0) {
  1797.               done = 0;
  1798.               ++parts;
  1799.             } else
  1800.               done = 1;
  1801.           }
  1802.           if (filelength(fileno(fp)) > 250000L)
  1803.             nup = 1;
  1804.           if (fp != NULL)
  1805.             fclose(fp);
  1806.         }
  1807.       } else
  1808.         skipped = 1;
  1809.       ++grouprec[cug].lastread;
  1810.       while (kbhit()) {
  1811.         ch = (getch());
  1812.         switch (ch) {
  1813.           case '+':
  1814.             if (spooltodisk) {
  1815.               if (MOREINFO)
  1816.                 backline();
  1817.               abort = 6;
  1818.             }
  1819.             break;
  1820.           case '}':
  1821.             if (spooltodisk) {
  1822.               if (MOREINFO)
  1823.                 backline();
  1824.               abort = 5;
  1825.             }
  1826.             break;
  1827.           case ']':
  1828.             if (spooltodisk) {
  1829.               if (MOREINFO)
  1830.                 backline();
  1831.               abort = 4;
  1832.             }
  1833.             break;
  1834.           case 9:
  1835.             abort = 3;
  1836.             break;
  1837.           case 32:
  1838.             abort = 2;
  1839.             break;
  1840.           case 27:
  1841.             abort = 1;
  1842.             break;
  1843.           default:
  1844.             break;
  1845.         }
  1846.       }
  1847.       switch (abort) {
  1848.         case 6:
  1849.           backline();
  1850.           output(" ? Skip how many articles? ");
  1851.           if (jgets(buf, 10)) {
  1852.             skip_articles = atol(buf);
  1853.             if ((grouprec[cug].lastread += skip_articles) < cur_last) {
  1854.               abort = 0;
  1855.               break;
  1856.             }
  1857.           } else
  1858.             break;
  1859.         case 5:
  1860.           if ((grouprec[cug].lastread += 100) < cur_last) {
  1861.             abort = 0;
  1862.             break;
  1863.           }
  1864.         case 4:
  1865.           if ((grouprec[cug].lastread += 10) < cur_last) {
  1866.             abort = 0;
  1867.             break;
  1868.           }
  1869.         case 3:
  1870.           grouprec[cug].lastread = cur_last;
  1871.         case 2:
  1872.           nug = jug = 1;
  1873.           abort = 0;
  1874.           break;
  1875.         default:
  1876.           break;
  1877.       }
  1878.     }
  1879.     if (nug) {
  1880.       ++cug;
  1881.       if (*grouprec[cug].subtype == '0')
  1882.         nup = 1;
  1883.     }
  1884.   }
  1885.   write_groups(1);
  1886.   nntp_shutdown(NNTP_sock);
  1887.   if (abort)
  1888.     log_it(1, "\n ■ Session aborted from keyboard");
  1889.   return 0;
  1890. }
  1891.  
  1892. int copyfile(char *input, char *output)
  1893. {
  1894.   int f1, f2, i;
  1895.   char *b;
  1896.   struct ftime ft;
  1897.  
  1898.   if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
  1899.     if ((b = (char *) malloc(16400)) == NULL)
  1900.       return 0;
  1901.     f1 = sh_open1(input, O_RDONLY | O_BINARY);
  1902.     if (!f1) {
  1903.       free(b);
  1904.       b = NULL;
  1905.       return 0;
  1906.     }
  1907.     getftime(f1, &ft);
  1908.  
  1909.     f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1910.     if (!f2) {
  1911.       free(b);
  1912.       b = NULL;
  1913.       f1 = sh_close(f1);
  1914.       return 0;
  1915.     }
  1916.     i = read(f1, (void *) b, 16384);
  1917.     while (i > 0) {
  1918.       sh_write(f2, (void *) b, i);
  1919.       i = read(f1, (void *) b, 16384);
  1920.     }
  1921.     f1 = sh_close(f1);
  1922.     setftime(f2, &ft);
  1923.     f2 = sh_close(f2);
  1924.     free(b);
  1925.     b = NULL;
  1926.   }
  1927.   return 1;
  1928. }
  1929.  
  1930.  
  1931.  
  1932. main(int argc, char *argv[])
  1933. {
  1934.   int ok, f, bAuth;
  1935.   char s[160], fn[MAXPATH], tempfn[MAXPATH], serverhost[50], *ss;
  1936.   FILE *fp;
  1937.   struct date dt;
  1938.   struct time tm;
  1939.   Mail_Socket *nntp_sock = NULL;
  1940.  
  1941.   crossposts = 10;
  1942.   binxpost = 0;
  1943.   MOREINFO = 1;
  1944.   POPNAME[0] = 0;
  1945.   POPDOMAIN[0] = 0;
  1946.  
  1947.   cursor('S');
  1948.   cursor('H');
  1949.   output("\n ■ %s", version);
  1950.   if (argc != 4) {
  1951.     output("\n ■ Invalid arguments for %s\n", argv[0]);
  1952.     cursor('R');
  1953.     return EXIT_FAILURE;
  1954.   }
  1955.   strcpy(net_data, argv[1]);
  1956.   strcat(net_data, "\\");
  1957.   strcpy(serverhost, argv[2]);
  1958.   sy = atoi(argv[3]);
  1959.  
  1960.   get_dir(maindir, 0);
  1961.  
  1962.   detect_multitask();
  1963.  
  1964.   gettime(&tm);
  1965.   getdate(&dt);
  1966.   cur_daten = dostounix(&dt, &tm);
  1967.  
  1968.   ss = getenv("WWIV_INSTANCE");
  1969.   if (ss) {
  1970.     instance = atoi(ss);
  1971.     if ((instance <= 0) || (instance >= 1000)) {
  1972.       log_it(1, "\n ■ WWIV_INSTANCE set to %d.  Can only be 1..999!", instance);
  1973.       instance = 1;
  1974.     }
  1975.   } else
  1976.     instance = 1;
  1977.  
  1978.   sprintf(fn, "%s\\CONFIG.DAT", maindir);
  1979.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  1980.   if (f < 0) {
  1981.     output("\n ■ %s NOT FOUND.", fn);
  1982.     cursor('R');
  1983.     return EXIT_FAILURE;
  1984.   }
  1985.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1986.   sh_close(f);
  1987.  
  1988.   read_groups();
  1989.   if (ngroups == 0) {
  1990.     output("\n ■ Unable to access newsgroup file NEWS.RC!");
  1991.     cursor('R');
  1992.     return EXIT_FAILURE;
  1993.   } else {
  1994.     sprintf(fn, "%sNEWS.RC", net_data);
  1995.     sprintf(tempfn, "%sNEWS.BAK", net_data);
  1996.     if (exist(tempfn))
  1997.       unlink(tempfn);
  1998.     copyfile(fn, tempfn);
  1999.   }
  2000.  
  2001.   if (stricmp(grouprec[0].groupname, "newsrc") == 0)
  2002.     log_it(1, "\n ■ Retrieving current newsgroup listing from %s.",
  2003.            serverhost);
  2004.   else
  2005.     log_it(MOREINFO, "\n ■ %u newsgroup%s defined in NEWS.RC.", ngroups,
  2006.            ngroups == 1 ? "" : "s");
  2007.  
  2008.   sprintf(fn, "%s\\NET.INI", maindir);
  2009.   if ((fp = fsh_open(fn, "rt")) == NULL)
  2010.     output("\n ■ Unable to read %s", fn);
  2011.   else {
  2012.     while (fgets(s, 100, fp)) {
  2013.       stripspace(s);
  2014.       if ((*s == ';') || (*s == '\n') || (*s == '['))
  2015.         continue;
  2016.       if (strlen(s) == 0)
  2017.         continue;
  2018.       if ((strnicmp(s, "POPNAME", 7) == 0) && (POPNAME[0] == 0)) {
  2019.         ss = strtok(s, "=");
  2020.         if (ss) {
  2021.           ss = strtok(NULL, "\n");
  2022.           trimstr1(ss);
  2023.           strcpy(POPNAME, ss);
  2024.         }
  2025.       }
  2026.       if (strnicmp(s, "FWDNAME", 7) == 0) {
  2027.         ss = strtok(s, "=");
  2028.         if (ss) {
  2029.           ss = strtok(NULL, "\n");
  2030.           trimstr1(ss);
  2031.           strcpy(POPNAME, ss);
  2032.         }
  2033.       }
  2034.       if ((strnicmp(s, "DOMAIN", 6) == 0) && (POPDOMAIN[0] == 0)) {
  2035.         ss = strtok(s, "=");
  2036.         if (ss) {
  2037.           ss = strtok(NULL, "\n");
  2038.           trimstr1(ss);
  2039.           strcpy(POPDOMAIN, ss);
  2040.         }
  2041.       }
  2042.       if (strnicmp(s, "FWDDOM", 6) == 0) {
  2043.         ss = strtok(s, "=");
  2044.         if (ss) {
  2045.           ss = strtok(NULL, "\n");
  2046.           trimstr1(ss);
  2047.           strcpy(POPDOMAIN, ss);
  2048.         }
  2049.       }
  2050.       if (strnicmp(s, "XPOSTS", 6) == 0) {
  2051.         ss = strtok(s, "=");
  2052.         if (ss) {
  2053.           ss = strtok(NULL, "\n");
  2054.           trimstr1(ss);
  2055.           crossposts = atoi(ss);
  2056.           if (crossposts > 99)
  2057.             crossposts = 10;
  2058.         }
  2059.       }
  2060.       if (strnicmp(s, "MORENEWS", 8) == 0) {
  2061.         ss = strtok(s, "=");
  2062.         if (ss) {
  2063.           ss = strtok(NULL, "\n");
  2064.           trimstr1(ss);
  2065.           if ((ss[0] == 'n') || (ss[0] == 'N'))
  2066.             MOREINFO = 0;
  2067.         }
  2068.       }
  2069.       if (strnicmp(s, "BINXPOST", 8) == 0) {
  2070.         ss = strtok(s, "=");
  2071.         if (ss) {
  2072.           ss = strtok(NULL, "\n");
  2073.           trimstr1(ss);
  2074.           if ((ss[0] == 'y') || (ss[0] == 'Y'))
  2075.             binxpost = 1;
  2076.         }
  2077.       }
  2078.       if (strnicmp(s, "NEWSNAME", 8) == 0) {
  2079.         ss = strtok(s, "=");
  2080.         if (ss) {
  2081.           ss = strtok(NULL, "\n");
  2082.           trimstr1(ss);
  2083.           strcpy(NEWSNAME, ss);
  2084.         }
  2085.       }
  2086.       if (strnicmp(s, "NEWSPASS", 8) == 0) {
  2087.         ss = strtok(s, "=");
  2088.         if (ss) {
  2089.           ss = strtok(NULL, "\n");
  2090.           trimstr1(ss);
  2091.           strcpy(NEWSPASS, ss);
  2092.         }
  2093.       }
  2094.       ss = NULL;
  2095.     }
  2096.     if (fp != NULL)
  2097.       fclose(fp);
  2098.   }
  2099.   ok = 1;
  2100.   if ((nntp_sock = netsocket(serverhost)) != NULL) {
  2101.     bAuth = 1;
  2102.     if (NEWSNAME[0]) {
  2103.       if (!sendauthinfo(nntp_sock))
  2104.         bAuth = 0;
  2105.     }
  2106.     if (bAuth)
  2107.       ok = getnews(nntp_sock);
  2108.     if (ok)
  2109.       log_it(1, "\n ■ NEWS exited with an error.");
  2110.     else
  2111.       log_it(1, "\n ■ Normal NEWS completion.");
  2112.   }
  2113.   cd_to(maindir);
  2114.   log_it(1, "\n ■ NEWS completed processing %d newsgroups", ngroups);
  2115.   if (grouprec)
  2116.     farfree(grouprec);
  2117.   grouprec = NULL;
  2118.   check_packets();
  2119.   fcloseall();
  2120.   cursor('R');
  2121.   return ok;
  2122. }
  2123.