File: news.c

package info (click to toggle)
nn 6.7.3-16.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 32,039; sh: 1,491; awk: 138; makefile: 80
file content (454 lines) | stat: -rw-r--r-- 8,913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Article header parsing.
 */

#include <stdlib.h>
#include <ctype.h>
#include "config.h"
#include "global.h"
#include "digest.h"
#include "more.h"
#include "news.h"
#include "nntp.h"

#ifndef SUNOS4
#include <strings.h>
#else
#include <string.h>
#endif

/* news.c */

struct news_header news;

static char   **art_hdr_field(register char *lp, int all);


int             retry_on_error = 0;

char           *
parse_header(FILE * f, char **(*hdr_field) (), int modes, news_header_buffer hdrbuf)
{
    register char  *bp, *cp, **fptr;
    int             siz, all, date_only;
    long            pos;

    pos = ftell(f);

/* read first NEWS_HEADER_BUFFER bytes (should be more than enough) */

    all = modes & GET_ALL_FIELDS;
    date_only = modes & GET_DATE_ONLY;

    siz = fread(hdrbuf, sizeof(char), NEWS_HEADER_BUFFER, f);
    if (siz <= 0) {
	hdrbuf[0] = NUL;
	return hdrbuf;
    }
    bp = hdrbuf;
    bp[siz - 1] = NUL;

    /* decode subarticle header */
    while (*bp) {

	if (*bp == NL) {	/* empty line following header */
	    ++bp;
	    fseek(f, pos + (bp - hdrbuf), 0);
	    return bp;
	}
	if (bp[0] == SP && bp[1] == SP) {	/* An ugly hack so that NN
						 * can read */
	    bp += 2;		/* it's own FAQ... :-) (sorry Bill) */
	    continue;
	}
	if (date_only && *bp != 'D')
	    fptr = NULL;
	else if ((fptr = (*hdr_field) (bp, all))) {
	    while (*bp && *bp != ':' && isascii(*bp) && !isspace(*bp))
		bp++;
	    if (*bp)
		bp++;
	    while (*bp && isascii(*bp) && isspace(*bp) && *bp != NL)
		bp++;
	    *fptr = bp;
	}

#ifdef NO_HEADER_SEPARATION_HACK
	else {
	    for (cp = bp; *cp && *cp != ':'; cp++) {
		if (!isascii(*cp))
		    break;
		if (*cp == '_' || *cp == '-')
		    continue;
		if (isalnum(*cp))
		    continue;
		break;
	    }
	    if (*cp != ':') {
		*bp = NL;
		pos--;
		continue;
	    }
	}
#endif

	while (*bp && *bp != NL)
	    bp++;

	/* Assume that continued lines are never empty! */
	if (fptr && bp == *fptr)
	    *fptr = NULL;

	while (*bp) {		/* look for continued lines */
	    cp = bp + 1;

	    if (!(*cp && isascii(*cp) && isspace(*cp) && *cp != NL)) {
		/* next line is empty or not indented */
		*bp++ = NUL;
		break;
	    }
	    *bp = SP;		/* substitute NL with SPACE */
	    bp = cp;
	    while (*bp && *bp != NL)
		bp++;
	}
    }

    return bp;
}

static char   **
art_hdr_field(register char *lp, int all)
{

#define check(name, lgt, field) \
    if (isascii(lp[lgt]) && isspace(lp[lgt]) \
	&& strncasecmp(name, lp, lgt) == 0)\
	return &news.field

    switch (*lp++) {

	    case 'A':
	    case 'a':
	    if (!all)
		break;
	    check("pproved:", 8, ng_appr);
	    break;

	case 'B':
	case 'b':
	    check("ack-References:", 15, ng_bref);
	    break;

	case 'C':
	case 'c':
	    check("ontrol:", 7, ng_control);
	    check("omment-To:", 10, ng_comment);
	    break;

	case 'D':
	case 'd':
	    check("ate:", 4, ng_date);
	    if (!all)
		break;
	    check("ate-Received:", 13, ng_rdate);
	    check("istribution:", 12, ng_dist);
	    break;

	case 'F':
	case 'f':
	    check("rom:", 4, ng_from);
	    if (!all)
		break;
	    check("ollowup-To:", 11, ng_follow);
	    break;

	case 'K':
	case 'k':
	    if (!all)
		break;
	    check("eywords:", 8, ng_keyw);
	    break;

	case 'L':
	case 'l':
	    check("ines:", 5, ng_xlines);
	    break;

	case 'M':
	case 'm':
	    if (!all)
		break;
	    if (strncasecmp(lp, "essage-", 7))
		break;
	    lp += 7;
	    check("ID:", 3, ng_ident);
	    break;

	case 'N':
	case 'n':
	    check("ewsgroups:", 10, ng_groups);
	    break;

	case 'O':
	case 'o':
	    if (!all)
		break;
	    check("rganization:", 12, ng_org);
	    check("riginator:", 10, ng_origr);
	    break;

	case 'P':
	case 'p':
	    if (!all)
		break;
	    check("ath:", 4, ng_path);
	    break;

	case 'R':
	case 'r':
	    check("eferences:", 10, ng_ref);
	    check("eply-To:", 8, ng_reply);
	    break;

	case 'S':
	case 's':
	    check("ubject:", 7, ng_subj);
	    check("ender:", 6, ng_sender);
	    if (!all)
		break;
	    check("ummary:", 7, ng_summ);
	    break;

	case 'T':
	case 't':
	    check("itle:", 5, ng_subj);
	    break;

	case 'X':
	case 'x':
	    check("ref:", 4, ng_xref);
	    break;
    }

    return NULL;

#undef check
}

int
is_header_line(char *line)
{
    return art_hdr_field(line, 0) != (char **) NULL;
}


FILE           *
open_news_article(article_header * art, int modes, news_header_buffer buffer1, news_header_buffer buffer2)
{

    char           *digest_buffer;
    int             retry;
    FILE           *f;
    struct stat     statb;

#ifndef DONT_COUNT_LINES
    int             c;
    off_t           digest_artlen = 0;
#endif				/* DONT_COUNT_LINES */

#ifdef NNTP
    int             lazy = 0;
#endif				/* NNTP */

#ifndef DONT_COUNT_LINES
#ifdef NNTP
    long            fpos;
#endif				/* NNTP */
#endif				/* DONT_COUNT_LINES */

    if (art->flag & A_FOLDER) {
	f = open_file(group_path_name, OPEN_READ);
	if (f == NULL)
	    return NULL;
	fseek(f, art->hpos, 0);

#ifndef DONT_COUNT_LINES
	digest_artlen = art->lpos - art->fpos;
#endif				/* DONT_COUNT_LINES */
    }

#ifdef NNTP
    else if (use_nntp) {
	lazy = (current_group->master_flag & M_ALWAYS_DIGEST) == 0
	    && (modes & LAZY_BODY) ? 1 : 0;
	f = nntp_get_article(art->a_number, lazy);
	if (f == NULL)
	    return NULL;
    }
#endif				/* NNTP */

    else {
	sprintf(group_file_name, "%ld", art->a_number);

	retry = retry_on_error;
	while ((f = open_file(group_path_name, OPEN_READ)) == NULL)
	    if (--retry < 0)
		return NULL;

	/* necessary because empty files wreak havoc */
	if (fstat(fileno(f), &statb) < 0 ||

#ifdef NOV
	    (art->lpos = statb.st_size, statb.st_size <= (off_t) 0)) {
#else
	    statb.st_size < art->lpos || statb.st_size <= (off_t) 0) {
#endif				/* NOV */

	    fclose(f);
	    return who_am_i == I_AM_MASTER ? (FILE *) 1 : NULL;
	}
    }

    digest_buffer = buffer1;

    if (modes & FILL_NEWS_HEADER) {

	news.ng_from = NULL;
	news.ng_reply = NULL;
	news.ng_name = NULL;
	news.ng_subj = NULL;
	news.ng_groups = NULL;
	news.ng_ref = NULL;
	news.ng_bref = NULL;
	news.ng_sender = NULL;

	news.ng_xlines = NULL;
	news.ng_xref = NULL;

	if (modes & GET_ALL_FIELDS) {
	    news.ng_path = NULL;
	    news.ng_reply = NULL;
	    news.ng_ident = NULL;
	    news.ng_follow = NULL;
	    news.ng_keyw = NULL;
	    news.ng_dist = NULL;
	    news.ng_org = NULL;
	    news.ng_appr = NULL;
	    news.ng_summ = NULL;
	    news.ng_control = NULL;
	    news.ng_date = NULL;
	    news.ng_rdate = NULL;
	    news.ng_comment = NULL;
	    news.ng_origr = NULL;
	}
	if (modes & GET_DATE_ONLY)
	    news.ng_date = NULL;

	(void) parse_header(f, art_hdr_field, modes, buffer1);

	if (news.ng_from == NULL)
	    news.ng_from = news.ng_sender;

#ifdef NOV
	/* fill in article positions..  new style.. */
	if ((art->flag & (A_FOLDER | A_DIGEST)) == 0) {
	    setpos(art, f);
	    news.ng_fpos = art->fpos;
	}
#else				/* NOV */
	if (modes & FILL_OFFSETS)	/* used only by old DB code */
	    art->fpos = news.ng_fpos = ftell(f);
#endif				/* NOV */

	if (news.ng_xlines)
	    news.ng_lines = atoi(news.ng_xlines);
	else {

#ifndef DONT_COUNT_LINES

#ifdef NNTP
	    if (use_nntp && lazy && !(art->flag & (A_DIGEST | A_FOLDER))) {
		fpos = ftell(f);
		fclose(f);
		f = nntp_get_article(art->a_number, 2);
		if (f == NULL)
		    return NULL;
		lazy = 0;
		fseek(f, fpos, 0);
	    }
#endif				/* NNTP */

	    news.ng_lines = 0;
	    while ((c = getc(f)) != EOF) {
		if (c == '\n')
		    news.ng_lines++;
		if (digest_artlen && --digest_artlen == 0)
		    break;
	    }
#else				/* DONT_COUNT_LINES */
	    news.ng_lines = -1;
#endif				/* DONT_COUNT_LINES */
	}

	if (modes & FILL_OFFSETS) {
	    fseek(f, 0, 2);
	    news.ng_lpos = ftell(f);
	}

#ifdef NNTP
	else if (use_nntp && (art->flag & (A_DIGEST | A_FOLDER)) == 0) {
	    fseek(f, 0, 2);
	    art->lpos = ftell(f);
	}
#endif

	news.ng_flag = 0;

	if (news.ng_appr)
	    news.ng_flag |= N_MODERATED;

	if (modes & DIGEST_CHECK && is_digest(news.ng_subj))
	    news.ng_flag |= N_DIGEST;

#ifdef NNTP
	if (use_nntp && lazy && news.ng_flag & N_DIGEST) {
	    fclose(f);
	    f = nntp_get_article(art->a_number, 2);
	    if (f == NULL)
		return NULL;
	}
#endif

	digest_buffer = buffer2;
    }

#ifdef NNTP
    else if (use_nntp && (art->flag & (A_DIGEST | A_FOLDER)) == 0) {
	fseek(f, 0, 2);
	art->lpos = ftell(f);
    }
#endif

    if (modes & FILL_DIGEST_HEADER) {
	fseek(f, art->hpos, 0);
	parse_digest_header(f, modes & GET_ALL_FIELDS, digest_buffer);
    }

#ifdef NOV
    else {
	/* fill in article positions..  new style.. */
	if ((art->flag & (A_FOLDER | A_DIGEST)) == 0) {
	    setpos(art, f);
	    news.ng_fpos = art->fpos;
	}
    }
#endif				/* NOV */

    fseek(f, (modes & SKIP_HEADER) ? art->fpos : art->hpos, 0);

    return f;
}