home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B72.ZIP / NEWS.CPP < prev    next >
C/C++ Source or Header  |  1997-11-21  |  55KB  |  2,059 lines

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