File: rt-process.c

package info (click to toggle)
trn4 4.0-test77-11
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 3,640 kB
  • ctags: 3,719
  • sloc: ansic: 48,331; sh: 6,817; tcl: 1,696; yacc: 660; perl: 108; makefile: 24
file content (598 lines) | stat: -rw-r--r-- 15,267 bytes parent folder | download | duplicates (6)
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
/* rt-process.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */


#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "intrp.h"
#include "trn.h"
#include "hash.h"
#include "cache.h"
#include "bits.h"
#include "final.h"
#include "ng.h"
#include "ngdata.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "nntp.h"
#include "rcln.h"
#include "util.h"
#include "util2.h"
#include "kfile.h"
#include "rthread.h"
#include "rt-select.h"
#include "INTERN.h"
#include "rt-process.h"
#include "rt-process.ih"

/* This depends on art being set to the current article number.
*/
ARTICLE*
allocate_article(artnum)
ART_NUM artnum;
{
    register ARTICLE* article;

    /* create an new article */
    if (artnum >= absfirst)
	article = article_ptr(artnum);
    else {
	article = (ARTICLE*)safemalloc(sizeof (ARTICLE));
	bzero((char*)article, sizeof (ARTICLE));
	article->flags |= AF_FAKE|AF_TMPMEM;
    }
    return article;
}

static void
fix_msgid(msgid)
char* msgid;
{
    register char* cp;

    if ((cp = index(msgid, '@')) != NULL) {
	while (*++cp) {
	    if (isupper(*cp)) {
		*cp = tolower(*cp);	/* lower-case domain portion */
	    }
	}
    }
}

int
msgid_cmp(key, keylen, data)
char* key;
int keylen;
HASHDATUM data;
{
    /* We already know that the lengths are equal, just compare the strings */
    if (data.dat_len)
	return bcmp(key, data.dat_ptr, keylen);
    return bcmp(key, ((ARTICLE*)data.dat_ptr)->msgid, keylen);
}

SUBJECT* fake_had_subj; /* the fake-turned-real article had this subject */

bool
valid_article(article)
ARTICLE* article;
{
    ARTICLE* ap;
    ARTICLE* fake_ap;
    char* msgid = article->msgid;
    HASHDATUM data;

    if (msgid) {
	fix_msgid(msgid);
	data = hashfetch(msgid_hash, msgid, strlen(msgid));
	if (data.dat_len) {
	    safefree0(data.dat_ptr);
	    article->autofl = data.dat_len & (AUTO_SELS | AUTO_KILLS);
	    if ((data.dat_len & KF_AGE_MASK) == 0)
		article->autofl |= AUTO_OLD;
	    else
		kf_changethd_cnt++;
	    data.dat_len = 0;
	}
	if ((fake_ap = (ARTICLE*)data.dat_ptr) == NULL) {
	    data.dat_ptr = (char*)article;
	    hashstorelast(data);
	    fake_had_subj = NULL;
	    return TRUE;
	}
	if (fake_ap == article) {
	    fake_had_subj = NULL;
	    return TRUE;
	}

	/* Whenever we replace a fake art with a real one, it's a lot of work
	** cleaning up the references.  Fortunately, this is not often. */
	if (fake_ap && (fake_ap->flags & AF_TMPMEM)) {
	    article->parent = fake_ap->parent;
	    article->child1 = fake_ap->child1;
	    article->sibling = fake_ap->sibling;
	    fake_had_subj = fake_ap->subj;
	    if (fake_ap->autofl) {
		article->autofl |= fake_ap->autofl;
		kf_state |= kfs_thread_change_set;
	    }
	    if (curr_artp == fake_ap) {
		curr_artp = article;
		curr_art = article_num(article);
	    }
	    if (recent_artp == fake_ap) {
		recent_artp = article;
		recent_art = article_num(article);
	    }
	    if ((ap = article->parent) != NULL) {
		if (ap->child1 == fake_ap)
		    ap->child1 = article;
		else {
		    ap = ap->child1;
		    /* This sibling-search code is duplicated below */
		    while (ap->sibling) {
			if (ap->sibling == fake_ap) {
			    ap->sibling = article;
			    break;
			}
			ap = ap->sibling;
		    }
		    /* End of slibling-search code */
		}
	    } else if (fake_had_subj) {
		register SUBJECT* sp = fake_had_subj;
		if ((ap = sp->thread) == fake_ap) {
		    do {
			sp->thread = article;
			sp = sp->thread_link;
		    } while (sp != fake_had_subj);
		} else {
		    /* This sibling-search code is duplicated above */
		    while (ap->sibling) {
			if (ap->sibling == fake_ap) {
			    ap->sibling = article;
			    break;
			}
			ap = ap->sibling;
		    }
		    /* End of slibling-search code */
		}
	    }
	    for (ap = article->child1; ap; ap = ap->sibling)
		ap->parent = article;
	    clear_article(fake_ap);
	    free((char*)fake_ap);
	    data.dat_ptr = (char*)article;
	    hashstorelast(data);
	    return TRUE;
	}
    }
    /* Forget about the duplicate message-id or bogus article. */
    uncache_article(article,TRUE);
    return FALSE;
}

/* Take a message-id and see if we already know about it.  If so, return
** the article, otherwise create a fake one.
*/
ARTICLE*
get_article(msgid)
char* msgid;
{
    register ARTICLE* article;
    HASHDATUM data;

    fix_msgid(msgid);

    data = hashfetch(msgid_hash, msgid, strlen(msgid));
    if (data.dat_len) {
	article = allocate_article(0);
	article->autofl = data.dat_len & (AUTO_SELS | AUTO_KILLS);
	if ((data.dat_len & KF_AGE_MASK) == 0)
	    article->autofl |= AUTO_OLD;
	else
	    kf_changethd_cnt++;
	article->msgid = data.dat_ptr;
	data.dat_ptr = (char*)article;
	data.dat_len = 0;
	hashstorelast(data);
    }
    else if (!(article = (ARTICLE*)data.dat_ptr)) {
	article = allocate_article(0);
	data.dat_ptr = (char*)article;
	article->msgid = savestr(msgid);
	hashstorelast(data);
    }
    return article;
}

/* Take all the data we've accumulated about the article and shove it into
** the article tree at the best place we can deduce.
*/
void
thread_article(article,references)
ARTICLE* article;
char* references;
{
    register ARTICLE* ap;
    register ARTICLE* prev;
    register char* cp;
    register char* end;
    int chain_autofl = (article->autofl
	| (article->subj->articles? article->subj->articles->autofl : 0));
    int thread_autofl, subj_autofl = 0;
    int rethreading = article->flags & AF_THREADED;

    /* We're definitely not a fake anymore */
    article->flags = (article->flags & ~AF_FAKE) | AF_THREADED;

    /* If the article was already part of an existing thread, unlink it
    ** to try to put it in the best possible spot.
    */
    if (fake_had_subj) {
	ARTICLE* stopper;
	if (fake_had_subj->thread != article->subj->thread)
	    merge_threads(fake_had_subj, article->subj);
	/* Check for a real or shared-fake parent */
	ap = article->parent;
	while (ap && (ap->flags & AF_FAKE) && !ap->child1->sibling) {
	    prev = ap;
	    ap = ap->parent;
	}
	stopper = ap;
	unlink_child(article);
	/* We'll assume that this article has as good or better references
	** than the child that faked us initially.  Free the fake reference-
	** chain and process our references as usual.
	*/
	for (ap = article->parent; ap != stopper; ap = prev) {
	    unlink_child(ap);
	    prev = ap->parent;
	    ap->date = 0;
	    ap->subj = 0;
	    ap->parent = 0;
	    /* don't free it until group exit since we probably re-use it */
	}
	article->parent = NULL;		/* neaten up */
	article->sibling = NULL;
    }

    /* If we have references, process them from the right end one at a time
    ** until we either run into somebody, or we run out of references.
    */
    if (references && *references) {
	prev = article;
	ap = NULL;
	if ((cp = rindex(references, '<')) == NULL
	 || (end = index(cp+1, ' ')) == NULL)
	    end = references + strlen(references) - 1;
	while (cp) {
	    while (end >= cp && end > references
	     && (*(unsigned char*)end <= ' ' || *end == ',')) {
		end--;
	    }
	    if (end <= cp)
		break;
	    end[1] = '\0';
	    /* Quit parsing references if this one is garbage. */
	    if (!(end = valid_message_id(cp, end)))
		break;
	    /* Dump all domains that end in '.', such as "..." & "1@DEL." */
	    if (end[-1] == '.')
		break;
	    ap = get_article(cp);
	    *cp = '\0';
	    chain_autofl |= ap->autofl;
	    if (ap->subj == article->subj)
		subj_autofl |= ap->autofl;

	    /* Check for duplicates on the reference line.  Brand-new data has
	    ** no date.  Data we just allocated earlier on this line has a
	    ** date but no subj.  Special-case the article itself, since it
	    ** does have a subj.
	    */
	    if ((ap->date && !ap->subj) || ap == article) {
		if ((ap = prev) == article)
		    ap = NULL;
		goto next;
	    }

	    /* When we're doing late processing of In-Reply-To: lines, we may
	    ** have to move an article from an old position.
	    */
	    if (rethreading && prev->subj)
		unlink_child(prev);
	    prev->parent = ap;
	    link_child(prev);
	    if (ap->subj)
		break;

	    ap->date = article->date;
	    prev = ap;
	  next:
	    if (cp > references)
	        end = cp-1;
	    else
	        end = cp;
	    cp = rindex(references, '<');
	}
	if (!ap)
	    goto no_references;

	/* Check if we ran into anybody that was already linked.  If so, we
	** just use their thread.
	*/
	if (ap->subj) {
	    /* See if this article spans the gap between what we thought
	    ** were two different threads.
	    */
	    if (article->subj->thread != ap->subj->thread)
		merge_threads(ap->subj, article->subj);
	} else {
	    /* We didn't find anybody we knew, so either create a new thread
	    ** or use the article's thread if it was previously faked.
	    */
	    ap->subj = article->subj;
	    link_child(ap);
	}
	/* Set the subj of faked articles we created as references. */
	for (ap = article->parent; ap && !ap->subj; ap = ap->parent)
	    ap->subj = article->subj;

	/* Make sure we didn't circularly link to a child article(!), by
	** ensuring that we run off the top before we run into ourself.
	*/
	while (ap && ap->parent != article)
	    ap = ap->parent;
	if (ap) {
	    /* Ugh.  Someone's tweaked reference line with an incorrect
	    ** article-order arrived first, and one of our children is
	    ** really one of our ancestors. Cut off the bogus child branch
	    ** right where we are and link it to the thread.
	    */
	    unlink_child(ap);
	    ap->parent = NULL;
	    link_child(ap);
	}
    } else {
      no_references:
	/* The article has no references.  Either turn it into a new thread
	** or re-attach the fleshed-out article to its old thread.  Don't
	** touch it at all unless this is the first attempt at threading it.
	*/
	if (!rethreading)
	    link_child(article);
    }
    if (!(article->flags & AF_CACHED))
	cache_article(article);
    thread_autofl = chain_autofl;
    if (sel_mode == SM_THREAD) {
	SUBJECT* sp = article->subj->thread_link;
	while (sp != article->subj) {
	    if (sp->articles)
		thread_autofl |= sp->articles->autofl;
	    sp = sp->thread_link;
	}
    }
    subj_autofl |= article->subj->articles->autofl;

    perform_auto_flags(article, thread_autofl, subj_autofl, chain_autofl);
}

void
rover_thread(article, s)
ARTICLE* article;
char* s;
{
    ARTICLE* prev = article;
    char* end;
    char ch;

    for (;;) {
	while (*++s == ' ') ;
	if (isdigit(*s)) {
	    article = article_ptr(atol(s));
	    prev->parent = article;
	    link_child(prev);
	    break;
	}
	end = index(s, '>');
	if (!end)
	    return;				/* Impossible! */
	ch = end[1];
	end[1] = '\0';
	article = get_article(s);
	prev->parent = article;
	link_child(prev);
	if (!ch)
	    break;
	end[1] = ch;
	s = end;
    }
}

/* Check if the string we've found looks like a valid message-id reference.
*/
static char*
valid_message_id(start, end)
register char* start;
register char* end;
{
    char* mid;

    if (start == end)
	return 0;

    if (*end != '>') {
	/* Compensate for space cadets who include the header in their
	** subsitution of all '>'s into another citation character.
	*/
	if (*end == '<' || *end == '-' || *end == '!' || *end == '%'
	 || *end == ')' || *end == '|' || *end == ':' || *end == '}'
	 || *end == '*' || *end == '+' || *end == '#' || *end == ']'
	 || *end == '@' || *end == '$') {
	    *end = '>';
	}
    } else if (end[-1] == '>') {
	*(end--) = '\0';
    }
    /* Id must be "<...@...>" */
    if (*start != '<' || *end != '>' || (mid = index(start, '@')) == NULL
     || mid == start+1 || mid+1 == end) {
	return 0;
    }
    return end;
}

/* Remove an article from its parent/siblings.  Leave parent pointer intact.
*/
static void
unlink_child(child)
register ARTICLE* child;
{
    register ARTICLE* last;

    if (!(last = child->parent)) {
	register SUBJECT* sp = child->subj;
	if ((last = sp->thread) == child) {
	    do {
		sp->thread = child->sibling;
		sp = sp->thread_link;
	    } while (sp != child->subj);
	} else
	    goto sibling_search;
    } else {
	if (last->child1 == child)
	    last->child1 = child->sibling;
	else {
	    last = last->child1;
	  sibling_search:
	    while (last && last->sibling != child)
		last = last->sibling;
	    if (last)
		last->sibling = child->sibling;
	}
    }
}

/* Helper function for comparing article dates.  If the dates are
** equal we compare article numbers.  This means that different
** articles always compare as having different dates, and makes the
** threading algorithm stable as to starting point.
** Returns what Perl calls (left <=> right) ie -1 if left is before
** right, +1 if after, 0 if same.
*/
static int
compare_article_dates(left, right)
register ARTICLE* left;
register ARTICLE* right;
{
    if (left->date < right->date) return -1;
    if (left->date > right->date) return +1;
    if (left->num < right->num) return -1;
    if (left->num > right->num) return +1;
    return 0;
}

/* Link an article to its parent article.  If its parent pointer is zero,
** link it to its thread.  Sorts siblings by date.
*/
void
link_child(child)
register ARTICLE* child;
{
    register ARTICLE* ap;

    if (!(ap = child->parent)) {
	register SUBJECT* sp = child->subj;
	ap = sp->thread;
	if (!ap || compare_article_dates(child, ap) < 0) {
	    do {
		sp->thread = child;
		sp = sp->thread_link;
	    } while (sp != child->subj);
	    child->sibling = ap;
	} else
	    goto sibling_search;
    } else {
	ap = ap->child1;
	if (!ap || compare_article_dates(child, ap) < 0) {
	    child->sibling = ap;
	    child->parent->child1 = child;
	} else {
	  sibling_search:
	    while (ap->sibling &&
                   compare_article_dates(ap->sibling, child) <= 0)
		ap = ap->sibling;
	    child->sibling = ap->sibling;
	    ap->sibling = child;
	}
    }
}

/* Merge all of s2's thread into s1's thread.
*/
void
merge_threads(s1, s2)
SUBJECT* s1;
SUBJECT* s2;
{
    register SUBJECT* sp;
    register ARTICLE* t1;
    register ARTICLE* t2;

    t1 = s1->thread;
    t2 = s2->thread;
    /* Change all of t2's thread pointers to a common lead article */
    sp = s2;
    do {
	sp->thread = t1;
	sp = sp->thread_link;
    } while (sp != s2);

    /* Join the two circular lists together */
    sp = s2->thread_link;
    s2->thread_link = s1->thread_link;
    s1->thread_link = sp;

    /* If thread mode is set, ensure the subjects are adjacent in the list. */
    /* Don't do this if the selector is active, because it gets messed up. */
    if (sel_mode == SM_THREAD && gmode != 's') {
	for (sp = s2; sp->prev && sp->prev->thread == t1; ) {
	    sp = sp->prev;
	    if (sp == s1)
		goto artlink;
	}
	while (s2->next && s2->next->thread == t1) {
	    s2 = s2->next;
	    if (s2 == s1)
		goto artlink;
	}
	/* Unlink the s2 chunk of subjects from the list */
	if (!sp->prev)
	    first_subject = s2->next;
	else
	    sp->prev->next = s2->next;
	if (!s2->next)
	    last_subject = sp->prev;
	else
	    s2->next->prev = sp->prev;
	/* Link the s2 chunk after s1 */
	sp->prev = s1;
	s2->next = s1->next;
	if (!s1->next)
	    last_subject = s2;
	else
	    s1->next->prev = s2;
	s1->next = sp;
    }

  artlink:
    /* Link each article that was attached to t2 to t1. */
    for (t1 = t2; t1; t1 = t2) {
	t2 = t2->sibling;
	link_child(t1);      /* parent is null, thread is newly set */
    }
}