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