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