File: articles.c

package info (click to toggle)
nn 6.7.3-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,508 kB
  • ctags: 3,199
  • sloc: ansic: 32,035; sh: 1,491; awk: 138; makefile: 98
file content (662 lines) | stat: -rw-r--r-- 15,036 bytes parent folder | download | duplicates (9)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Basic article access and management
 */

#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "global.h"
#include "articles.h"
#include "db.h"
#include "kill.h"
#include "match.h"
#include "news.h"
#include "newsrc.h"
#include "regexp.h"
#include "sort.h"
#include "nn_term.h"

/* articles.c */

static void     new_thunk(thunk * t, char *ptr, long size);

#ifdef ART_GREP
static int      grep_article(group_header * gh, article_header * ah, char *pattern, int (*fcn) ());
#endif				/* ART_GREP */

int             seq_cross_filtering = 1;
int             select_leave_next = 0;	/* ask to select left over art. */
int             ignore_re = 0;
int             body_search_header = 0;
int             cross_post_limit = 0;

extern int      ignore_fancy_select;
extern int      killed_articles;

extern attr_type test_article();

/*
 * memory management
 */

static          thunk
                dummy_str_t = {
    NULL,
    NULL,
    0L
},

                dummy_art_t = {
    NULL,
    NULL,
    0L
};


static thunk   *first_str_t = &dummy_str_t;
static thunk   *current_str_t = &dummy_str_t;
static thunk   *first_art_t = &dummy_art_t;
static thunk   *current_art_t = &dummy_art_t;
static long     cur_str_size = 0, cur_art_size = 0;
static char    *next_str;
static article_header *next_art;
static article_header **art_array = NULL;

static article_number max_articles = 0, mem_offset = 0;

/*
 * allocate one article header
 */

#ifndef ART_THUNK_SIZE
#define ART_THUNK_SIZE	127
#endif

static void
new_thunk(thunk * t, char *ptr, long size)
{
    thunk          *new;

    new = newobj(thunk, 1);

    new->next_thunk = t->next_thunk;
    t->next_thunk = new;

    new->this_thunk = ptr;
    new->thunk_size = size;
}


article_header *
alloc_art(void)
{
    if (cur_art_size == 0) {
	if (current_art_t->next_thunk == NULL)
	    new_thunk(current_art_t,
		      (char *) newobj(article_header, ART_THUNK_SIZE),
		      (long) ART_THUNK_SIZE);

	current_art_t = current_art_t->next_thunk;
	next_art = (article_header *) current_art_t->this_thunk;
	cur_art_size = current_art_t->thunk_size;
    }
    cur_art_size--;
    return next_art++;
}

/*
 * allocate a string of length 'len'
 */

#ifndef STR_THUNK_SIZE
#define STR_THUNK_SIZE	((1<<14) - 32)	/* leave room for malloc header */
#endif

char           *
alloc_str(int len)
{
    /* allow space for '\0', and align on word boundary */
    int             size = (len + sizeof(int)) & ~(sizeof(int) - 1);
    char           *ret;

    if (cur_str_size < size) {
	if (current_str_t->next_thunk == NULL)
	    new_thunk(current_str_t,
		      newstr(STR_THUNK_SIZE), (long) STR_THUNK_SIZE);

	current_str_t = current_str_t->next_thunk;
	next_str = current_str_t->this_thunk;
	cur_str_size = current_str_t->thunk_size;
    }
    /* XXX else should do something reasonable */

    ret = next_str;
    cur_str_size -= size;
    next_str += size;

    ret[len] = NUL;		/* ensure string is null terminated */
    return ret;
}

/*
 * "free" the allocated memory
 */

void
free_memory(void)
{
    current_str_t = first_str_t;
    current_art_t = first_art_t;
    cur_str_size = 0;
    cur_art_size = 0;
    n_articles = 0;
}


/*
 * mark/release memory
 */


void
mark_str(string_marker * str_marker)
{
    str_marker->sm_cur_t = current_str_t;
    str_marker->sm_size = cur_str_size;
    str_marker->sm_next = next_str;
}

void
release_str(string_marker * str_marker)
{
    current_str_t = str_marker->sm_cur_t;
    cur_str_size = str_marker->sm_size;
    next_str = str_marker->sm_next;
}


void
mark_memory(memory_marker * mem_marker)
{
    mark_str(&(mem_marker->mm_string));

    mem_marker->mm_cur_t = current_art_t;
    mem_marker->mm_size = cur_art_size;
    mem_marker->mm_next = next_art;

    mem_marker->mm_nart = n_articles;
    mem_offset += n_articles;

    n_articles = 0;
    articles = art_array + mem_offset;
}

void
release_memory(memory_marker * mem_marker)
{
    release_str(&(mem_marker->mm_string));

    current_art_t = mem_marker->mm_cur_t;
    cur_art_size = mem_marker->mm_size;
    next_art = mem_marker->mm_next;

    n_articles = mem_marker->mm_nart;

    mem_offset -= n_articles;
    articles = art_array + mem_offset;
}

/*
 * merge all memory chunks into one.
 */

void
merge_memory(void)
{
    n_articles += mem_offset;
    mem_offset = 0;
    articles = art_array;
}


/*
 * save article header in 'articles' array
 * 'articles' is enlarged if too small
 */

#define	FIRST_ART_ARRAY_SIZE	500	/* malloc header */
#define	NEXT_ART_ARRAY_SIZE	512

void
add_article(article_header * art)
{
    if ((n_articles + mem_offset) == max_articles) {
	/* must increase size of 'articles' */

	if (max_articles == 0) {
	    /* allocate initial 'articles' array */
	    max_articles = FIRST_ART_ARRAY_SIZE;
	} else {
	    max_articles += NEXT_ART_ARRAY_SIZE;
	}
	art_array = resizeobj(art_array, article_header *, max_articles);
	articles = art_array + mem_offset;
    }
    articles[n_articles] = art;
    n_articles++;
}


int
access_group(register group_header * gh, article_number first_article, article_number last_article, register flag_type flags, char *mask)
{
    register article_header *ah;
    int             skip_digest, n;
    group_header   *cpgh;

#ifdef NOV
    int             dbstatus;
#else				/* NOV */
    FILE           *data;
    long            data_offset, data_size;
#endif				/* NOV */

    cross_post_number cross_post;
    attr_type       leave_attr;
    memory_marker   mem_marker;
    static regexp  *rexp = NULL;
    static char     subptext[80];

    if (first_article < gh->first_db_article)
	first_article = gh->first_db_article;

    if (last_article > gh->last_db_article)
	last_article = gh->last_db_article;

    if (last_article == 0 || first_article > last_article)
	return 0;

#ifdef NOV
    db_read_group(gh, first_article, last_article);
#else
    data = open_data_file(gh, 'd', OPEN_READ);
    if (data == NULL)
	return -10;

    if ((data_offset = get_data_offset(gh, first_article)) == -1)
	return -11;
#endif				/* NOV */

    if (mask == NULL) {
	if (rexp != NULL) {
	    freeobj(rexp);
	    rexp = NULL;
	}
    } else {
	if (*mask == '/') {
	    mask++;
	    if (rexp != NULL) {
		if (strncmp(mask, subptext, 80) != 0) {
		    freeobj(rexp);
		    rexp = NULL;
		}
	    }
	    if (rexp == NULL) {
		strncpy(subptext, mask, 80);
		rexp = regcomp(mask);
		if (rexp == NULL)
		    return -1;
	    }
	    /* notice mask is still non-NULL */
	}
    }

    if ((flags & (ACC_ALSO_READ_ARTICLES | ACC_ONLY_READ_ARTICLES)))
	leave_attr = 0;
    else if (select_leave_next)
	leave_attr = A_READ;	/* will prompt */
    else
	leave_attr = A_LEAVE_NEXT;

    if (!(flags & ACC_SPEW_MODE))
	use_newsrc(gh, (flags & ACC_MERGED_NEWSRC) ? 2 :
		   (flags & ACC_ORIG_NEWSRC) ? 1 : 0);

    if ((flags & ACC_EXTRA_ARTICLES) == 0)
	mark_memory(&mem_marker);

    ah = alloc_art();

    skip_digest = 0;

#ifdef NOV
    /* XXX: db_read_art takes a FILE * in nnmaster version */
    while ((dbstatus = db_read_art((FILE *) - 1)) > 0) {

#ifdef ART_GREP
	if (s_keyboard || s_hangup)
	    break;		/* add to allow interrupt of GREP */
#endif

	if (db_hdr.dh_number < first_article)
	    continue;
#else
    fseek(data, data_offset, 0);

    while (data_offset < gh->data_write_offset) {

#ifdef ART_GREP
	if (s_keyboard || s_hangup)
	    break;		/* add to allow interrupt of GREP */
#endif

	data_size = db_read_art(data);
	if (data_size <= 0) {
	    fclose(data);
	    if ((flags & ACC_EXTRA_ARTICLES) == 0)
		release_memory(&mem_marker);
	    return -2;
	}
	data_offset += data_size;
#endif				/* NOV */

	if (db_hdr.dh_lpos == (off_t) 0)
	    continue;		/* article not accessible */

	if (db_hdr.dh_number > gh->last_db_article
	    || db_hdr.dh_number < gh->first_db_article)
	    goto data_error;

	if (skip_digest && db_data.dh_type == DH_SUB_DIGEST)
	    continue;

	skip_digest = 0;

	if (cross_post_limit && db_hdr.dh_cross_postings >= cross_post_limit)
	    continue;

	if (db_hdr.dh_cross_postings && !(flags & ACC_ALSO_CROSS_POSTINGS)) {
	    for (n = 0; n < (int) db_hdr.dh_cross_postings; n++) {
		cross_post = NETW_CROSS_INT(db_data.dh_cross[n]);
		if (cross_post < 0 || cross_post >= master.number_of_groups)
		    continue;
		cpgh = ACTIVE_GROUP(cross_post);
		if (cpgh == gh) {
		    if (seq_cross_filtering)
			continue;
		    n = db_hdr.dh_cross_postings;
		    break;
		}
		if (!(flags & ACC_ALSO_UNSUB_GROUPS))
		    if (cpgh->group_flag & G_UNSUBSCRIBED)
			continue;

		if (!seq_cross_filtering)
		    break;
		if (cpgh->preseq_index > 0 &&
		    cpgh->preseq_index < gh->preseq_index)
		    break;
	    }

	    if (n < (int) db_hdr.dh_cross_postings) {
		if (db_data.dh_type == DH_DIGEST_HEADER)
		    skip_digest++;
		continue;
	    }
	}
	ah->flag = 0;

	switch (db_data.dh_type) {
	    case DH_DIGEST_HEADER:
		if (flags & ACC_DONT_SPLIT_DIGESTS)
		    skip_digest++;
		else if ((flags & ACC_ALSO_FULL_DIGEST) == 0)
		    continue;	/* don't want the full digest when split */
		ah->flag |= A_FULL_DIGEST;
		break;
	    case DH_SUB_DIGEST:
		ah->flag |= A_DIGEST;
		break;
	}

	ah->a_number = db_hdr.dh_number;
	if (ah->a_number > last_article)
	    break;

	if (flags & ACC_SPEW_MODE) {
	    fold_string(db_data.dh_subject);
	    tprintf("%x:%s\n", (int) (gh->group_num), db_data.dh_subject);
	    continue;
	}
	ah->hpos = db_hdr.dh_hpos;
	ah->fpos = ah->hpos + (off_t) (db_hdr.dh_fpos);
	ah->lpos = db_hdr.dh_lpos;

	ah->attr = test_article(ah);

	if (flags & ACC_MERGED_NEWSRC) {
	    if (ah->attr == A_KILL)
		continue;
	} else if (ah->attr != A_READ && (flags & ACC_ONLY_READ_ARTICLES))
	    continue;

#ifdef ART_GREP
	if ((flags & ACC_ON_GREP_UNREAD) && (ah->attr == A_READ))
	    continue;
#endif				/* ART_GREP */

	if (rexp != NULL) {
	    if (flags & ACC_ON_SUBJECT)
		if (regexec_cf(rexp, db_data.dh_subject))
		    goto match_ok;
	    if (flags & ACC_ON_SENDER)
		if (regexec_cf(rexp, db_data.dh_sender))
		    goto match_ok;

#ifdef ART_GREP
	    if (flags & (ACC_ON_GREP_UNREAD | ACC_ON_GREP_ALL))
		if (grep_article(gh, ah, (char *) rexp, regexec_cf))
		    goto match_ok;
#endif				/* ART_GREP */

	    continue;
	} else if (mask != NULL) {
	    if (flags & ACC_ON_SUBJECT)
		if (strmatch_cf(mask, db_data.dh_subject))
		    goto match_ok;
	    if (flags & ACC_ON_SENDER)
		if (strmatch_cf(mask, db_data.dh_sender))
		    goto match_ok;

#ifdef ART_GREP
	    if (flags & (ACC_ON_GREP_UNREAD | ACC_ON_GREP_ALL))
		if (grep_article(gh, ah, mask, strmatch_cf))
		    goto match_ok;
#endif				/* ART_GREP */

	    continue;
	}
match_ok:

attr_again:

	switch (ah->attr) {
	    case A_LEAVE:
		if (mask) {
		    ah->attr = 0;
		    break;
		}
		if (leave_attr == A_READ) {
		    clrdisp();
		    prompt("Select left over articles in group %s? ", gh->group_name);
		    leave_attr = yes(0) > 0 ? A_SELECT : A_LEAVE_NEXT;
		}
		ah->attr = leave_attr;
		goto attr_again;

	    case A_SELECT:
		if (mask)
		    ah->attr = 0;
		break;

	    case A_READ:
		if (flags & ACC_MERGED_NEWSRC)
		    break;

		if (!(flags & (ACC_ALSO_READ_ARTICLES | ACC_ONLY_READ_ARTICLES)))
		    if (ah->a_number > gh->last_article)
			continue;

		/* FALLTHROUGH */
	    case A_SEEN:
	    case 0:
		if (flags & ACC_DO_KILL) {
		    ah->replies = db_hdr.dh_replies;
		    ah->sender = db_data.dh_sender;
		    ah->subject = db_data.dh_subject;
		    if (kill_article(ah))
			continue;
		}
		/* The 'P' command to a read group must show articles as read */
		if (gh->unread_count <= 0)
		    ah->attr = A_READ;
		break;

	    default:
		break;
	}

	if (db_hdr.dh_sender_length) {
	    ah->name_length = db_hdr.dh_sender_length;
	    ah->sender = alloc_str((int) db_hdr.dh_sender_length);
	    strcpy(ah->sender, db_data.dh_sender);
	} else
	    ah->sender = "";

	if (db_hdr.dh_subject_length) {
	    ah->subj_length = db_hdr.dh_subject_length;
	    ah->subject = alloc_str((int) db_hdr.dh_subject_length);
	    strcpy(ah->subject, db_data.dh_subject);
	} else
	    ah->subject = "";

	ah->replies = db_hdr.dh_replies;
	ah->lines = db_hdr.dh_lines;
	ah->t_stamp = db_hdr.dh_date;

	ah->a_group = (flags & ACC_MERGED_MENU) ? current_group : NULL;

	add_article(ah);
	ah = alloc_art();
    }

#ifdef NOV
    if (dbstatus < 0) {		/* Error reading DB? */
	if ((flags & ACC_EXTRA_ARTICLES) == 0)
	    release_memory(&mem_marker);
	return -2;
    }
    /* else EOF reading DB */
#else
    fclose(data);
#endif				/* NOV */

    if ((flags & ACC_DONT_SORT_ARTICLES) == 0)
	sort_articles(-1);
    else
	no_sort_articles();

    if (ignore_re && !ignore_fancy_select && !(flags & ACC_ALSO_READ_ARTICLES)) {
	int             nexta, roota, killa, newa;
	for (nexta = killa = newa = roota = 0; nexta < n_articles; nexta++) {
	    ah = articles[nexta];
	    if (ah->flag & A_ROOT_ART)
		roota = ah->replies & 127;
	    if (roota && ah->replies && !auto_select_article(ah, 1))
		killa++;
	    else
		articles[newa++] = ah;
	}
	n_articles -= killa;
	killed_articles += killa;
    }
    return n_articles > 0 ? 1 : 0;

data_error:
    log_entry('E', "%s: data inconsistency", gh->group_name);

#ifndef NOV
    fclose(data);
#endif				/* NOV */

    if ((flags & ACC_EXTRA_ARTICLES) == 0)
	release_memory(&mem_marker);
    return -12;
}

#ifdef ART_GREP
/* GREP ARTICLE -- search the article for the pattern using the specified function
   Return 1 if pattern occurs in the article else 0
   Also return 0 if any malloc or file open or read error occurs
   Global data_header db_hdr is set by caller
todo:
   doesn't work for folders (hangs)
*/

static int
grep_article(group_header * gh, article_header * ah, char *pattern, int (*fcn) ())
{
    char           *line, *end;
    FILE           *art;
    news_header_buffer buffer1, buffer2;
    static char    *buf;
    static int      bufsize, count;
    size_t          size;

    count++;
    art = open_news_article(ah, FILL_OFFSETS |
		  (body_search_header ? 0 : SKIP_HEADER), buffer1, buffer2);
    if (!art) {
	/* msg("Cannot open article"); */
	return 0;
    }
    size = ah->lpos - ftell(art) + 1;
    if (bufsize < size) {
	if (buf)
	    free(buf);
	buf = (char *) malloc(size + 10);
	if (!buf) {
	    msg("Cannot malloc %d bytes", size);
	    bufsize = 0;
	    return 0;
	}
	bufsize = size;
    }
    if (fread(buf, size - 1, 1, art) != 1) {
	fclose(art);
	/* msg("Cannot read article"); */
	return 0;
    }
    fclose(art);
    /* print status message every so often */
    if (!(count % 27))
	msg("Searching %d of %d",
	    ah->a_number - gh->first_db_article,
	    gh->last_db_article - gh->first_db_article);

    buf[size] = 0;		/* make buf a giant string so strchr below
				 * terminates */
    line = buf;
    while ((end = strchr(line, '\n'))) {
	*end++ = 0;		/* make the line a nul terminated string */
	if ((*fcn) (pattern, line))
	    return 1;
	line = end;
    }
    return 0;
}

#endif				/* ART_GREP */