home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part03 / addng.c next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  8.3 KB  |  340 lines

  1. /* $Id: addng.c,v 4.4.2.1 1991/12/01 18:05:42 sob PATCH_2 sob $
  2.  *
  3.  * $Log: addng.c,v $
  4.  * Revision 4.4.2.1  1991/12/01  18:05:42  sob
  5.  * Patchlevel 2 changes
  6.  *
  7.  * Revision 4.4  1991/09/09  20:18:23  sob
  8.  * release 4.4
  9.  *
  10.  *
  11.  * 
  12.  */
  13. /* This software is Copyright 1991 by Stan Barber. 
  14.  *
  15.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  16.  * use this software as long as: there is no monetary profit gained
  17.  * specifically from the use or reproduction of this software, it is not
  18.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  19.  * included prominently in any copy made. 
  20.  *
  21.  * The author make no claims as to the fitness or correctness of this software
  22.  * for any use whatsoever, and it is provided as is. Any use of this software
  23.  * is at the user's own risk. 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "rn.h"
  29. #include "ngdata.h"
  30. #include "last.h"
  31. #include "util.h"
  32. #include "intrp.h"
  33. #include "only.h"
  34. #include "rcstuff.h"
  35. #ifdef SERVER
  36. #include "server.h"
  37. #endif
  38. #include "final.h"
  39. #include "INTERN.h"
  40. #include "addng.h"
  41.  
  42. #ifdef TZSET
  43. #include <time.h>
  44. #else
  45. #include <sys/time.h>
  46. #include <sys/timeb.h>
  47. #endif
  48.  
  49. void
  50. addng_init()
  51. {
  52.     ;
  53. }
  54.  
  55. #ifdef FINDNEWNG
  56. /* generate a list of new newsgroups from active file */
  57.  
  58. bool
  59. newlist(munged,checkinlist)
  60. bool_int munged;            /* are we scanning the whole file? */
  61. bool_int checkinlist;
  62. {
  63.     char *tmpname;
  64.     register char *s, *status;
  65.     register NG_NUM ngnum;
  66. #ifndef ACTIVE_TIMES
  67.     long birthof();
  68. #endif
  69.  
  70.     tmpname = filexp(RNEWNAME);
  71.     tmpfp = fopen(tmpname,"w+");
  72.     if (tmpfp == Nullfp) {
  73.     printf(cantcreate,tmpname) FLUSH;
  74.     return FALSE;
  75.     }
  76.     UNLINK(tmpname);            /* be nice to the world */
  77.  
  78.     while (fgets(buf,LBUFLEN,actfp) != Nullch) {
  79.     /* Check if they want to break out of the new newsgroups search */
  80.     if (int_count) {
  81.         int_count = 0;
  82.         fclose(tmpfp);
  83.         return FALSE;
  84.     }
  85.     if (s = index(buf,' ')) {
  86.         status=s;
  87.         while (isdigit(*status) || isspace(*status)) status++;
  88.         *s++ = '\0';
  89.         if (strnEQ(buf,"to.",3) || *status == 'x' || *status == '=')
  90.             /* since = groups are refiling to another group, just
  91.            ignore their existence */
  92.         continue;
  93. #ifdef ACTIVE_TIMES
  94.         if (inlist(buf) && ((ngnum = find_ng(buf)) == nextrcline
  95.                 || toread[ngnum] == TR_UNSUB)
  96. #else
  97.         if (checkinlist ?
  98.         (inlist(buf) && ((ngnum = find_ng(buf)) == nextrcline
  99.                  || toread[ngnum] == TR_UNSUB))
  100.           : (find_ng(buf) == nextrcline
  101.          && birthof(buf,(ART_NUM)atol(s)) > lasttime)
  102. #endif
  103.         ) {
  104.                     /* if not in .newsrc and younger */
  105.                     /* than the last time we checked */
  106.         fprintf(tmpfp,"%s\n",buf);
  107.                     /* then remember said newsgroup */
  108.         }
  109. #ifdef FASTNEW
  110.         else {            /* not really a new group */
  111.         if (!munged) {        /* did we assume not munged? */
  112.             fclose(tmpfp);    /* then go back, knowing that */
  113.             return TRUE;    /* active file was indeed munged */
  114.         }
  115.         }
  116. #endif
  117.     }
  118. #ifdef DEBUGGING
  119.     else
  120.         printf("Bad active record: %s\n",buf) FLUSH;
  121. #endif
  122.     }
  123.  
  124.     /* we have successfully generated the list */
  125.  
  126.     fseek(tmpfp,0L,0);            /* rewind back to the beginning */
  127.     while (fgets(buf,LBUFLEN,tmpfp) != Nullch) {
  128.     buf[strlen(buf)-1] = '\0';
  129.     get_ng(buf,TRUE);        /* add newsgroup, maybe */
  130.     }
  131.     fclose(tmpfp);            /* be nice to ourselves */
  132.     return FALSE;            /* do not call us again */
  133. }
  134.  
  135. #ifdef ACTIVE_TIMES
  136. #ifdef SERVER
  137.  
  138. bool
  139. find_new_groups()
  140. {
  141.     char *tmpname;
  142.     register char *s;
  143.     struct tm *ts;
  144.     long now;
  145.     NG_NUM oldnext = nextrcline;    /* remember # lines in newsrc */
  146.  
  147.     tmpname = filexp(RNEWNAME);
  148.     tmpfp = fopen(tmpname,"w+");
  149.     if (tmpfp == Nullfp) {
  150.     printf(cantcreate,tmpname) FLUSH;
  151.     return FALSE;
  152.     }
  153.     UNLINK(tmpname);            /* be nice to the world */
  154.  
  155.     time(&now);
  156.     ts = gmtime(&lastnewtime);
  157.     sprintf(ser_line, "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT",
  158.     ts->tm_year % 100, ts->tm_mon+1, ts->tm_mday,
  159.     ts->tm_hour, ts->tm_min, ts->tm_sec);
  160. #ifdef DEBUGGING
  161.     if (debug & DEB_NNTP)
  162.     printf(">%s\n", ser_line) FLUSH;
  163. #endif
  164.     put_server(ser_line);
  165.     nntp_get(ser_line, sizeof(ser_line));
  166. #ifdef DEBUGGING
  167.     if (debug & DEB_NNTP)
  168.     printf("<%s\n", ser_line) FLUSH;
  169. #endif
  170.     if (*ser_line != CHAR_OK) {        /* and then see if that's ok */
  171.   error_exit:
  172.     fclose(tmpfp);
  173.     printf("Can't get new groups from server:\n%s\n", ser_line);
  174.     return FALSE;
  175.     }
  176.  
  177.     while (1) {
  178.     if (nntp_get(ser_line, sizeof(ser_line)) < 0)
  179.         goto error_exit;
  180. #ifdef DEBUGGING
  181.     if (debug & DEB_NNTP)
  182.         printf("<%s\n", ser_line) FLUSH;
  183. #endif
  184.     if (ser_line[0] == '.')
  185.         break;
  186.     if ((s = index(ser_line, ' ')) != Nullch)
  187.         *s = '\0';
  188.     fprintf(tmpfp,"%s\n",ser_line);
  189.     }
  190.  
  191.     /* we have successfully generated the list */
  192.  
  193.     if (ftell(tmpfp)) {
  194.     fputs("\nFinding new newsgroups:\n",stdout) FLUSH;
  195.  
  196.     fseek(tmpfp,0L,0);        /* rewind back to the beginning */
  197.     while (fgets(buf,LBUFLEN,tmpfp) != Nullch) {
  198.         buf[strlen(buf)-1] = '\0';
  199.         get_ng(buf,FALSE);        /* add newsgroup, maybe */
  200.     }
  201.     lastnewtime = now;        /* remember when we found new groups */
  202.     }                    /* (ends up back in .rnlast) */
  203.     fclose(tmpfp);            /* be nice to ourselves */
  204.  
  205.     return oldnext != nextrcline;
  206. }
  207. #else /* not SERVER */
  208.  
  209. bool
  210. find_new_groups()
  211. {
  212.     register char *s;
  213.     long lastone;
  214.     NG_NUM oldnext = nextrcline;    /* remember # lines in newsrc */
  215.  
  216.     fstat(fileno(actfp),&filestat);    /* find active file size */
  217.     lastactsiz = filestat.st_size;    /* just to save it in .rnlast */
  218.  
  219.     stat(ACTIVE_TIMES,&filestat);    /* did active.times file grow? */
  220.     if (filestat.st_size == lastnewsize)
  221.     return FALSE;
  222.     lastnewsize = filestat.st_size;
  223.  
  224.     fputs("\nChecking for new newsgroups...\n",stdout) FLUSH;
  225.  
  226.     s = filexp(ACTIVE_TIMES);
  227.     tmpfp = fopen(s,"r");
  228.     if (tmpfp == Nullfp) {
  229.     printf(cantopen,s) FLUSH;
  230.     return FALSE;
  231.     }
  232.     lastone = time(Null(time_t*)) - 24L * 60 * 60 - 1;
  233.     while (fgets(buf,LBUFLEN,tmpfp) != Nullch) {
  234.     if ((s = index(buf, ' ')) != Nullch)
  235.         if ((lastone = atol(s+1)) >= lastnewtime) {
  236.         char tmpbuf[80];
  237.         *s = '\0';
  238.         if (findact(tmpbuf, buf, s - buf, 0L) >= 0)
  239.             get_ng(buf,FALSE);    /* add newsgroup, maybe */
  240.         }
  241.     }
  242.     fclose(tmpfp);
  243.     lastnewtime = lastone+1;        /* remember time of last new group */
  244.                     /* (ends up back in .rnlast) */
  245.     return oldnext != nextrcline;
  246. }
  247. #endif /* SERVER */
  248. #else /* not ACTIVE_TIMES */
  249.  
  250. bool
  251. find_new_groups()
  252. {
  253.     long oldactsiz = lastactsiz;
  254.     NG_NUM oldnext = nextrcline;    /* remember # lines in newsrc */
  255.  
  256.     fstat(fileno(actfp),&filestat);    /* did active file grow? */
  257.  
  258.     if (filestat.st_size == lastactsiz)
  259.     return FALSE;
  260.     lastactsiz = filestat.st_size;    /* remember new size */
  261.  
  262. #ifdef VERBOSE
  263.     IF(verbose)
  264.     fputs("\nChecking active file for new newsgroups...\n",stdout) FLUSH;
  265.     ELSE
  266. #endif
  267. #ifdef TERSE
  268.     fputs("\nNew newsgroups:\n",stdout) FLUSH;
  269. #endif
  270.  
  271. #ifdef FASTNEW                /* bad soft ptrs -> edited active */
  272.     if (!writesoft && oldactsiz) {    /* maybe just do tail of file? */
  273.     fseek(actfp,oldactsiz-NL_SIZE,0);
  274.     fgets(buf,LBUFLEN,actfp);
  275.     if (*buf == '\n' && !newlist(FALSE,FALSE))
  276.         goto bugout;
  277.     }
  278. #endif
  279.     fseek(actfp,0L,0);        /* rewind active file */
  280.     newlist(TRUE,FALSE);        /* sure hope they use hashing... */
  281. bugout:
  282.     return oldnext != nextrcline;
  283. }
  284.  
  285. /* return creation time of newsgroup */
  286.  
  287. long
  288. birthof(ngnam,ngsize)
  289. char *ngnam;
  290. ART_NUM ngsize;
  291. {
  292.     char tst[128];
  293.     long time();
  294.  
  295. #ifdef SERVER        /* ngsize not used */
  296.     int x,tot,min,max;
  297.     sprintf(tst,"GROUP %s",ngnam);
  298. #ifdef DEBUGGING
  299.     if (debug & DEB_NNTP)
  300.     printf(">%s\n", tst) FLUSH;
  301. #endif
  302.     put_server(tst);
  303.     (void) nntp_get(tst, sizeof(tst));
  304. #ifdef DEBUGGING
  305.     if (debug & DEB_NNTP)
  306.     printf("<%s\n", tst) FLUSH;
  307. #endif
  308.     if (*tst != CHAR_OK) return(0); /* not a real group */
  309.     (void) sscanf(tst,"%d%d%d%d",&x,&tot,&min,&max);
  310.     if (tot > 0) return(time(Null(long *)));
  311.     else return(0);
  312. #else /* not SERVER */
  313.  
  314.     sprintf(tst, ngsize ? "%s/%s/1" : "%s/%s" ,spool,getngdir(ngnam));
  315.     if (stat(tst,&filestat) < 0)
  316.     return (ngsize ? 0L : time(Null(long *)));
  317.     /* not there, assume something good */
  318.     else
  319.     return filestat.st_mtime;
  320.  
  321. #endif
  322. }
  323. #endif /* ACTIVE_TIMES */
  324.  
  325. bool
  326. scanactive()
  327. {
  328.     NG_NUM oldnext = nextrcline;    /* remember # lines in newsrc */
  329.  
  330.     fseek(actfp,0L,0);
  331.     newlist(TRUE,TRUE);
  332.     if (nextrcline != oldnext) {    /* did we add any new groups? */
  333.     return TRUE;
  334.     }
  335.     return FALSE;
  336. }
  337.  
  338. #endif
  339.  
  340.