forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
1480 lines (1311 loc) · 38.2 KB
/
thread.c
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Create/manipulate threading in emails
*
* @authors
* Copyright (C) 1996-2002 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mutt.h"
#include "thread.h"
#include "body.h"
#include "context.h"
#include "envelope.h"
#include "globals.h"
#include "header.h"
#include "lib/lib.h"
#include "list.h"
#include "options.h"
#include "protos.h"
#include "sort.h"
#define VISIBLE(hdr, ctx) \
(hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
/**
* is_descendant - Is one thread a descendant of another
*/
static int is_descendant(struct MuttThread *a, struct MuttThread *b)
{
while (a)
{
if (a == b)
return 1;
a = a->parent;
}
return 0;
}
/**
* need_display_subject - Determines whether to display a message's subject
*/
static int need_display_subject(struct Context *ctx, struct Header *hdr)
{
struct MuttThread *tmp = NULL, *tree = hdr->thread;
/* if the user disabled subject hiding, display it */
if (!option(OPT_HIDE_THREAD_SUBJECT))
return 1;
/* if our subject is different from our parent's, display it */
if (hdr->subject_changed)
return 1;
/* if our subject is different from that of our closest previously displayed
* sibling, display the subject */
for (tmp = tree->prev; tmp; tmp = tmp->prev)
{
hdr = tmp->message;
if (hdr && VISIBLE(hdr, ctx))
{
if (hdr->subject_changed)
return 1;
else
break;
}
}
/* if there is a parent-to-child subject change anywhere between us and our
* closest displayed ancestor, display the subject */
for (tmp = tree->parent; tmp; tmp = tmp->parent)
{
hdr = tmp->message;
if (hdr)
{
if (VISIBLE(hdr, ctx))
return 0;
else if (hdr->subject_changed)
return 1;
}
}
/* if we have no visible parent or previous sibling, display the subject */
return 1;
}
static void linearize_tree(struct Context *ctx)
{
struct MuttThread *tree = ctx->tree;
struct Header **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
while (tree)
{
while (!tree->message)
tree = tree->child;
*array = tree->message;
array += Sort & SORT_REVERSE ? -1 : 1;
if (tree->child)
tree = tree->child;
else
{
while (tree)
{
if (tree->next)
{
tree = tree->next;
break;
}
else
tree = tree->parent;
}
}
}
}
/**
* calculate_visibility - Are tree nodes visible
*
* this calculates whether a node is the root of a subtree that has visible
* nodes, whether a node itself is visible, whether, if invisible, it has
* depth anyway, and whether any of its later siblings are roots of visible
* subtrees. while it's at it, it frees the old thread display, so we can
* skip parts of the tree in mutt_draw_tree() if we've decided here that we
* don't care about them any more.
*/
static void calculate_visibility(struct Context *ctx, int *max_depth)
{
struct MuttThread *tmp = NULL, *tree = ctx->tree;
int hide_top_missing = option(OPT_HIDE_TOP_MISSING) && !option(OPT_HIDE_MISSING);
int hide_top_limited = option(OPT_HIDE_TOP_LIMITED) && !option(OPT_HIDE_LIMITED);
int depth = 0;
/* we walk each level backwards to make it easier to compute next_subtree_visible */
while (tree->next)
tree = tree->next;
*max_depth = 0;
while (true)
{
if (depth > *max_depth)
*max_depth = depth;
tree->subtree_visible = 0;
if (tree->message)
{
FREE(&tree->message->tree);
if (VISIBLE(tree->message, ctx))
{
tree->deep = true;
tree->visible = true;
tree->message->display_subject = need_display_subject(ctx, tree->message);
for (tmp = tree; tmp; tmp = tmp->parent)
{
if (tmp->subtree_visible)
{
tmp->deep = true;
tmp->subtree_visible = 2;
break;
}
else
tmp->subtree_visible = 1;
}
}
else
{
tree->visible = false;
tree->deep = !option(OPT_HIDE_LIMITED);
}
}
else
{
tree->visible = false;
tree->deep = !option(OPT_HIDE_MISSING);
}
tree->next_subtree_visible =
tree->next && (tree->next->next_subtree_visible || tree->next->subtree_visible);
if (tree->child)
{
depth++;
tree = tree->child;
while (tree->next)
tree = tree->next;
}
else if (tree->prev)
tree = tree->prev;
else
{
while (tree && !tree->prev)
{
depth--;
tree = tree->parent;
}
if (!tree)
break;
else
tree = tree->prev;
}
}
/* now fix up for the OPTHIDETOP* options if necessary */
if (hide_top_limited || hide_top_missing)
{
tree = ctx->tree;
while (true)
{
if (!tree->visible && tree->deep && tree->subtree_visible < 2 &&
((tree->message && hide_top_limited) || (!tree->message && hide_top_missing)))
tree->deep = false;
if (!tree->deep && tree->child && tree->subtree_visible)
tree = tree->child;
else if (tree->next)
tree = tree->next;
else
{
while (tree && !tree->next)
tree = tree->parent;
if (!tree)
break;
else
tree = tree->next;
}
}
}
}
/**
* mutt_draw_tree - Draw a tree of threaded emails
*
* Since the graphics characters have a value >255, I have to resort to using
* escape sequences to pass the information to print_enriched_string(). These
* are the macros MUTT_TREE_* defined in mutt.h.
*
* ncurses should automatically use the default ASCII characters instead of
* graphics chars on terminals which don't support them (see the man page for
* curs_addch).
*/
void mutt_draw_tree(struct Context *ctx)
{
char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree = NULL;
char corner = (Sort & SORT_REVERSE) ? MUTT_TREE_ULCORNER : MUTT_TREE_LLCORNER;
char vtee = (Sort & SORT_REVERSE) ? MUTT_TREE_BTEE : MUTT_TREE_TTEE;
int depth = 0, start_depth = 0, max_depth = 0, width = option(OPT_NARROW_TREE) ? 1 : 2;
struct MuttThread *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
/* Do the visibility calculations and free the old thread chars.
* From now on we can simply ignore invisible subtrees
*/
calculate_visibility(ctx, &max_depth);
pfx = safe_malloc(width * max_depth + 2);
arrow = safe_malloc(width * max_depth + 2);
while (tree)
{
if (depth)
{
myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
if (depth && start_depth == depth)
myarrow[0] = nextdisp ? MUTT_TREE_LTEE : corner;
else if (parent->message && !option(OPT_HIDE_LIMITED))
myarrow[0] = MUTT_TREE_HIDDEN;
else if (!parent->message && !option(OPT_HIDE_MISSING))
myarrow[0] = MUTT_TREE_MISSING;
else
myarrow[0] = vtee;
if (width == 2)
myarrow[1] = pseudo ? MUTT_TREE_STAR :
(tree->duplicate_thread ? MUTT_TREE_EQUALS : MUTT_TREE_HLINE);
if (tree->visible)
{
myarrow[width] = MUTT_TREE_RARROW;
myarrow[width + 1] = 0;
new_tree = safe_malloc((2 + depth * width));
if (start_depth > 1)
{
strncpy(new_tree, pfx, (start_depth - 1) * width);
strfcpy(new_tree + (start_depth - 1) * width, arrow,
(1 + depth - start_depth) * width + 2);
}
else
strfcpy(new_tree, arrow, 2 + depth * width);
tree->message->tree = new_tree;
}
}
if (tree->child && depth)
{
mypfx = pfx + (depth - 1) * width;
mypfx[0] = nextdisp ? MUTT_TREE_VLINE : MUTT_TREE_SPACE;
if (width == 2)
mypfx[1] = MUTT_TREE_SPACE;
}
parent = tree;
nextdisp = NULL;
pseudo = NULL;
do
{
if (tree->child && tree->subtree_visible)
{
if (tree->deep)
depth++;
if (tree->visible)
start_depth = depth;
tree = tree->child;
/* we do this here because we need to make sure that the first child thread
* of the old tree that we deal with is actually displayed if any are,
* or we might set the parent variable wrong while going through it. */
while (!tree->subtree_visible && tree->next)
tree = tree->next;
}
else
{
while (!tree->next && tree->parent)
{
if (tree == pseudo)
pseudo = NULL;
if (tree == nextdisp)
nextdisp = NULL;
if (tree->visible)
start_depth = depth;
tree = tree->parent;
if (tree->deep)
{
if (start_depth == depth)
start_depth--;
depth--;
}
}
if (tree == pseudo)
pseudo = NULL;
if (tree == nextdisp)
nextdisp = NULL;
if (tree->visible)
start_depth = depth;
tree = tree->next;
if (!tree)
break;
}
if (!pseudo && tree->fake_thread)
pseudo = tree;
if (!nextdisp && tree->next_subtree_visible)
nextdisp = tree;
} while (!tree->deep);
}
FREE(&pfx);
FREE(&arrow);
}
/**
* make_subject_list - Create a list of all subjects in a thread
*
* since we may be trying to attach as a pseudo-thread a MuttThread that
* has no message, we have to make a list of all the subjects of its
* most immediate existing descendants. we also note the earliest
* date on any of the parents and put it in *dateptr.
*/
static void make_subject_list(struct ListHead *subjects, struct MuttThread *cur, time_t *dateptr)
{
struct MuttThread *start = cur;
struct Envelope *env = NULL;
time_t thisdate;
int rc = 0;
while (true)
{
while (!cur->message)
cur = cur->child;
if (dateptr)
{
thisdate = option(OPT_THREAD_RECEIVED) ? cur->message->received :
cur->message->date_sent;
if (!*dateptr || thisdate < *dateptr)
*dateptr = thisdate;
}
env = cur->message->env;
if (env->real_subj && ((env->real_subj != env->subject) || (!option(OPT_SORT_RE))))
{
struct ListNode *np;
STAILQ_FOREACH(np, subjects, entries)
{
rc = mutt_strcmp(env->real_subj, np->data);
if (rc >= 0)
break;
}
if (!np)
mutt_list_insert_head(subjects, env->real_subj);
else if (rc > 0)
mutt_list_insert_after(subjects, np, env->real_subj);
}
while (!cur->next && cur != start)
{
cur = cur->parent;
}
if (cur == start)
break;
cur = cur->next;
}
}
/**
* find_subject - Find the best possible match for a parent based on subject
*
* If there are multiple matches, the one which was sent the latest, but before
* the current message, is used.
*/
static struct MuttThread *find_subject(struct Context *ctx, struct MuttThread *cur)
{
struct HashElem *ptr = NULL;
struct MuttThread *tmp = NULL, *last = NULL;
struct ListHead subjects = STAILQ_HEAD_INITIALIZER(subjects);
time_t date = 0;
make_subject_list(&subjects, cur, &date);
struct ListNode *np;
STAILQ_FOREACH(np, &subjects, entries)
{
for (ptr = hash_find_bucket(ctx->subj_hash, np->data); ptr; ptr = ptr->next)
{
tmp = ((struct Header *) ptr->data)->thread;
if (tmp != cur && /* don't match the same message */
!tmp->fake_thread && /* don't match pseudo threads */
tmp->message->subject_changed && /* only match interesting replies */
!is_descendant(tmp, cur) && /* don't match in the same thread */
(date >= (option(OPT_THREAD_RECEIVED) ? tmp->message->received :
tmp->message->date_sent)) &&
(!last || (option(OPT_THREAD_RECEIVED) ?
(last->message->received < tmp->message->received) :
(last->message->date_sent < tmp->message->date_sent))) &&
tmp->message->env->real_subj &&
(mutt_strcmp(np->data, tmp->message->env->real_subj) == 0))
{
last = tmp; /* best match so far */
}
}
}
mutt_list_clear(&subjects);
return last;
}
/**
* unlink_message - Break the message out of the thread
*
* Remove cur and its descendants from their current location. Also make sure
* ancestors of cur no longer are sorted by the fact that cur is their
* descendant.
*/
static void unlink_message(struct MuttThread **old, struct MuttThread *cur)
{
struct MuttThread *tmp = NULL;
if (cur->prev)
cur->prev->next = cur->next;
else
*old = cur->next;
if (cur->next)
cur->next->prev = cur->prev;
if (cur->sort_key)
{
for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key; tmp = tmp->parent)
tmp->sort_key = NULL;
}
}
/**
* insert_message - Insert a message into a thread
*
* add cur as a prior sibling of *new, with parent newparent
*/
static void insert_message(struct MuttThread **new,
struct MuttThread *newparent, struct MuttThread *cur)
{
if (*new)
(*new)->prev = cur;
cur->parent = newparent;
cur->next = *new;
cur->prev = NULL;
*new = cur;
}
static struct Hash *make_subj_hash(struct Context *ctx)
{
struct Header *hdr = NULL;
struct Hash *hash = NULL;
hash = hash_create(ctx->msgcount * 2, MUTT_HASH_ALLOW_DUPS);
for (int i = 0; i < ctx->msgcount; i++)
{
hdr = ctx->hdrs[i];
if (hdr->env->real_subj)
hash_insert(hash, hdr->env->real_subj, hdr);
}
return hash;
}
/**
* pseudo_threads - Thread messages by subject
*
* Thread by subject things that didn't get threaded by message-id
*/
static void pseudo_threads(struct Context *ctx)
{
struct MuttThread *tree = ctx->tree, *top = tree;
struct MuttThread *tmp = NULL, *cur = NULL, *parent = NULL, *curchild = NULL,
*nextchild = NULL;
if (!ctx->subj_hash)
ctx->subj_hash = make_subj_hash(ctx);
while (tree)
{
cur = tree;
tree = tree->next;
if ((parent = find_subject(ctx, cur)) != NULL)
{
cur->fake_thread = true;
unlink_message(&top, cur);
insert_message(&parent->child, parent, cur);
parent->sort_children = true;
tmp = cur;
while (true)
{
while (!tmp->message)
tmp = tmp->child;
/* if the message we're attaching has pseudo-children, they
* need to be attached to its parent, so move them up a level.
* but only do this if they have the same real subject as the
* parent, since otherwise they rightly belong to the message
* we're attaching. */
if (tmp == cur || (mutt_strcmp(tmp->message->env->real_subj,
parent->message->env->real_subj) == 0))
{
tmp->message->subject_changed = false;
for (curchild = tmp->child; curchild;)
{
nextchild = curchild->next;
if (curchild->fake_thread)
{
unlink_message(&tmp->child, curchild);
insert_message(&parent->child, parent, curchild);
}
curchild = nextchild;
}
}
while (!tmp->next && tmp != cur)
{
tmp = tmp->parent;
}
if (tmp == cur)
break;
tmp = tmp->next;
}
}
}
ctx->tree = top;
}
void mutt_clear_threads(struct Context *ctx)
{
for (int i = 0; i < ctx->msgcount; i++)
{
/* mailbox may have been only partially read */
if (ctx->hdrs[i])
{
ctx->hdrs[i]->thread = NULL;
ctx->hdrs[i]->threaded = false;
}
}
ctx->tree = NULL;
if (ctx->thread_hash)
hash_destroy(&ctx->thread_hash, *free);
}
static int compare_threads(const void *a, const void *b)
{
static sort_t *sort_func = NULL;
if (a && b)
return ((*sort_func)(&(*((struct MuttThread **) a))->sort_key,
&(*((struct MuttThread **) b))->sort_key));
/* a hack to let us reset sort_func even though we can't
* have extra arguments because of qsort
*/
else
{
sort_func = mutt_get_sort_func(Sort);
return (sort_func ? 1 : 0);
}
}
struct MuttThread *mutt_sort_subthreads(struct MuttThread *thread, int init)
{
struct MuttThread **array = NULL, *sort_key = NULL, *top = NULL, *tmp = NULL;
struct Header *oldsort_key = NULL;
int i, array_size, sort_top = 0;
/* we put things into the array backwards to save some cycles,
* but we want to have to move less stuff around if we're
* resorting, so we sort backwards and then put them back
* in reverse order so they're forwards
*/
Sort ^= SORT_REVERSE;
if (!compare_threads(NULL, NULL))
return thread;
top = thread;
array = safe_calloc((array_size = 256), sizeof(struct MuttThread *));
while (true)
{
if (init || !thread->sort_key)
{
thread->sort_key = NULL;
if (thread->parent)
thread->parent->sort_children = true;
else
sort_top = 1;
}
if (thread->child)
{
thread = thread->child;
continue;
}
else
{
/* if it has no children, it must be real. sort it on its own merits */
thread->sort_key = thread->message;
if (thread->next)
{
thread = thread->next;
continue;
}
}
while (!thread->next)
{
/* if it has siblings and needs to be sorted, sort it... */
if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top))
{
/* put them into the array */
for (i = 0; thread; i++, thread = thread->prev)
{
if (i >= array_size)
safe_realloc(&array, (array_size *= 2) * sizeof(struct MuttThread *));
array[i] = thread;
}
qsort((void *) array, i, sizeof(struct MuttThread *), *compare_threads);
/* attach them back together. make thread the last sibling. */
thread = array[0];
thread->next = NULL;
array[i - 1]->prev = NULL;
if (thread->parent)
thread->parent->child = array[i - 1];
else
top = array[i - 1];
while (--i)
{
array[i - 1]->prev = array[i];
array[i]->next = array[i - 1];
}
}
if (thread->parent)
{
tmp = thread;
thread = thread->parent;
if (!thread->sort_key || thread->sort_children)
{
/* make sort_key the first or last sibling, as appropriate */
sort_key = (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->child : tmp;
/* we just sorted its children */
thread->sort_children = false;
oldsort_key = thread->sort_key;
thread->sort_key = thread->message;
if (Sort & SORT_LAST)
{
if (!thread->sort_key ||
((((Sort & SORT_REVERSE) ? 1 : -1) *
compare_threads((void *) &thread, (void *) &sort_key)) > 0))
thread->sort_key = sort_key->sort_key;
}
else if (!thread->sort_key)
thread->sort_key = sort_key->sort_key;
/* if its sort_key has changed, we need to resort it and siblings */
if (oldsort_key != thread->sort_key)
{
if (thread->parent)
thread->parent->sort_children = true;
else
sort_top = 1;
}
}
}
else
{
Sort ^= SORT_REVERSE;
FREE(&array);
return top;
}
}
thread = thread->next;
}
}
static void check_subjects(struct Context *ctx, int init)
{
struct Header *cur = NULL;
struct MuttThread *tmp = NULL;
for (int i = 0; i < ctx->msgcount; i++)
{
cur = ctx->hdrs[i];
if (cur->thread->check_subject)
cur->thread->check_subject = false;
else if (!init)
continue;
/* figure out which messages have subjects different than their parents' */
tmp = cur->thread->parent;
while (tmp && !tmp->message)
{
tmp = tmp->parent;
}
if (!tmp)
cur->subject_changed = true;
else if (cur->env->real_subj && tmp->message->env->real_subj)
cur->subject_changed =
(mutt_strcmp(cur->env->real_subj, tmp->message->env->real_subj) != 0) ? true : false;
else
cur->subject_changed =
(cur->env->real_subj || tmp->message->env->real_subj) ? true : false;
}
}
void mutt_sort_threads(struct Context *ctx, int init)
{
struct Header *cur = NULL;
int i, oldsort, using_refs = 0;
struct MuttThread *thread = NULL, *new = NULL, *tmp = NULL, top;
memset(&top, 0, sizeof(top));
struct ListNode *ref = NULL;
/* set Sort to the secondary method to support the set sort_aux=reverse-*
* settings. The sorting functions just look at the value of
* SORT_REVERSE
*/
oldsort = Sort;
Sort = SortAux;
if (!ctx->thread_hash)
init = 1;
if (init)
ctx->thread_hash = hash_create(ctx->msgcount * 2, MUTT_HASH_ALLOW_DUPS);
/* we want a quick way to see if things are actually attached to the top of the
* thread tree or if they're just dangling, so we attach everything to a top
* node temporarily */
top.parent = top.next = top.prev = NULL;
top.child = ctx->tree;
for (thread = ctx->tree; thread; thread = thread->next)
thread->parent = ⊤
/* put each new message together with the matching messageless MuttThread if it
* exists. otherwise, if there is a MuttThread that already has a message, thread
* new message as an identical child. if we didn't attach the message to a
* MuttThread, make a new one for it. */
for (i = 0; i < ctx->msgcount; i++)
{
cur = ctx->hdrs[i];
if (!cur->thread)
{
if ((!init || option(OPT_DUP_THREADS)) && cur->env->message_id)
thread = hash_find(ctx->thread_hash, cur->env->message_id);
else
thread = NULL;
if (thread && !thread->message)
{
/* this is a message which was missing before */
thread->message = cur;
cur->thread = thread;
thread->check_subject = true;
/* mark descendants as needing subject_changed checked */
for (tmp = (thread->child ? thread->child : thread); tmp != thread;)
{
while (!tmp->message)
tmp = tmp->child;
tmp->check_subject = true;
while (!tmp->next && tmp != thread)
tmp = tmp->parent;
if (tmp != thread)
tmp = tmp->next;
}
if (thread->parent)
{
/* remove threading info above it based on its children, which we'll
* recalculate based on its headers. make sure not to leave
* dangling missing messages. note that we haven't kept track
* of what info came from its children and what from its siblings'
* children, so we just remove the stuff that's definitely from it */
do
{
tmp = thread->parent;
unlink_message(&tmp->child, thread);
thread->parent = NULL;
thread->sort_key = NULL;
thread->fake_thread = false;
thread = tmp;
} while (thread != &top && !thread->child && !thread->message);
}
}
else
{
new = (option(OPT_DUP_THREADS) ? thread : NULL);
thread = safe_calloc(1, sizeof(struct MuttThread));
thread->message = cur;
thread->check_subject = true;
cur->thread = thread;
hash_insert(ctx->thread_hash,
cur->env->message_id ? cur->env->message_id : "", thread);
if (new)
{
if (new->duplicate_thread)
new = new->parent;
thread = cur->thread;
insert_message(&new->child, new, thread);
thread->duplicate_thread = true;
thread->message->threaded = true;
}
}
}
else
{
/* unlink pseudo-threads because they might be children of newly
* arrived messages */
thread = cur->thread;
for (new = thread->child; new;)
{
tmp = new->next;
if (new->fake_thread)
{
unlink_message(&thread->child, new);
insert_message(&top.child, &top, new);
new->fake_thread = false;
}
new = tmp;
}
}
}
/* thread by references */
for (i = 0; i < ctx->msgcount; i++)
{
cur = ctx->hdrs[i];
if (cur->threaded)
continue;
cur->threaded = true;
thread = cur->thread;
using_refs = 0;
while (true)
{
if (using_refs == 0)
{
/* look at the beginning of in-reply-to: */
if ((ref = STAILQ_FIRST(&cur->env->in_reply_to)) != NULL)
using_refs = 1;
else
{
ref = STAILQ_FIRST(&cur->env->references);
using_refs = 2;
}
}
else if (using_refs == 1)
{
/* if there's no references header, use all the in-reply-to:
* data that we have. otherwise, use the first reference
* if it's different than the first in-reply-to, otherwise use
* the second reference (since at least eudora puts the most
* recent reference in in-reply-to and the rest in references)
*/
if (STAILQ_EMPTY(&cur->env->references))
ref = STAILQ_NEXT(ref, entries);
else
{
if (mutt_strcmp(ref->data, STAILQ_FIRST(&cur->env->references)->data) != 0)
ref = STAILQ_FIRST(&cur->env->references);
else
ref = STAILQ_NEXT(STAILQ_FIRST(&cur->env->references), entries);
using_refs = 2;
}
}
else
ref = STAILQ_NEXT(ref, entries); /* go on with references */
if (!ref)
break;
if ((new = hash_find(ctx->thread_hash, ref->data)) == NULL)
{
new = safe_calloc(1, sizeof(struct MuttThread));
hash_insert(ctx->thread_hash, ref->data, new);
}
else
{
if (new->duplicate_thread)
new = new->parent;
if (is_descendant(new, thread)) /* no loops! */
continue;
}
if (thread->parent)
unlink_message(&top.child, thread);
insert_message(&new->child, new, thread);
thread = new;
if (thread->message || (thread->parent && thread->parent != &top))
break;
}
if (!thread->parent)
insert_message(&top.child, &top, thread);
}
/* detach everything from the temporary top node */
for (thread = top.child; thread; thread = thread->next)
{
thread->parent = NULL;
}
ctx->tree = top.child;
check_subjects(ctx, init);
if (!option(OPT_STRICT_THREADS))
pseudo_threads(ctx);
if (ctx->tree)
{
ctx->tree = mutt_sort_subthreads(ctx->tree, init);
/* restore the oldsort order. */
Sort = oldsort;
/* Put the list into an array. */
linearize_tree(ctx);