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