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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: thread.c 942 2008-03-04 18:21:33Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2013-2017 Eduardo Chappa
* Copyright 2006-2008 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
#include "../pith/headers.h"
#include "../pith/thread.h"
#include "../pith/flag.h"
#include "../pith/icache.h"
#include "../pith/mailindx.h"
#include "../pith/msgno.h"
#include "../pith/sort.h"
#include "../pith/pineelt.h"
#include "../pith/status.h"
#include "../pith/news.h"
#include "../pith/search.h"
#include "../pith/mailcmd.h"
#include "../pith/ablookup.h"
/*
* Internal prototypes
*/
long *sort_thread_flatten(THREADNODE *, MAILSTREAM *, long *,
char *, long, PINETHRD_S *, unsigned);
void make_thrdflags_consistent(MAILSTREAM *, MSGNO_S *, PINETHRD_S *, int);
THREADNODE *collapse_threadnode_tree(THREADNODE *);
THREADNODE *collapse_threadnode_tree_sorted(THREADNODE *);
THREADNODE *sort_threads_and_collapse(THREADNODE *);
THREADNODE *insert_tree_in_place(THREADNODE *, THREADNODE *);
unsigned long branch_greatest_num(THREADNODE *, int);
long calculate_visible_threads(MAILSTREAM *);
PINETHRD_S *
fetch_thread(MAILSTREAM *stream, long unsigned int rawno)
{
MESSAGECACHE *mc;
PINELT_S *pelt;
PINETHRD_S *thrd = NULL;
if(stream && rawno > 0L && rawno <= stream->nmsgs
&& !sp_need_to_rethread(stream)){
mc = (rawno > 0L && stream && rawno <= stream->nmsgs)
? mail_elt(stream, rawno) : NULL;
if(mc && (pelt = (PINELT_S *) mc->sparep))
thrd = pelt->pthrd;
}
return(thrd);
}
PINETHRD_S *
fetch_head_thread(MAILSTREAM *stream)
{
unsigned long rawno;
PINETHRD_S *thrd = NULL;
if(stream){
/* first find any thread */
for(rawno = 1L; !thrd && rawno <= stream->nmsgs; rawno++)
thrd = fetch_thread(stream, rawno);
if(thrd && thrd->head)
thrd = fetch_thread(stream, thrd->head);
}
return(thrd);
}
/*
* Set flag f to v for all messages in thrd.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately.
* Ok to call it on top-level thread which has no branch already.
*/
void
set_flags_for_thread(MAILSTREAM *stream, MSGNO_S *msgmap, int f, PINETHRD_S *thrd, int v)
{
PINETHRD_S *nthrd, *bthrd;
if(!(stream && thrd && msgmap))
return;
set_lflag(stream, msgmap, mn_raw2m(msgmap, thrd->rawno), f, v);
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
set_flags_for_thread(stream, msgmap, f, nthrd, v);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
set_flags_for_thread(stream, msgmap, f, bthrd, v);
}
}
void
erase_threading_info(MAILSTREAM *stream, MSGNO_S *msgmap)
{
unsigned long n;
MESSAGECACHE *mc;
PINELT_S *peltp;
if(!(stream && stream->spare))
return;
ps_global->view_skipped_index = 0;
sp_set_viewing_a_thread(stream, 0);
if(THRD_INDX())
setup_for_thread_index_screen();
else
setup_for_index_index_screen();
stream->spare = 0;
for(n = 1L; n <= stream->nmsgs; n++){
set_lflag(stream, msgmap, mn_raw2m(msgmap, n),
MN_COLL | MN_CHID | MN_CHID2, 0);
mc = mail_elt(stream, n);
if(mc && mc->sparep){
peltp = (PINELT_S *) mc->sparep;
if(peltp->pthrd)
fs_give((void **) &peltp->pthrd);
}
}
}
void
sort_thread_callback(MAILSTREAM *stream, THREADNODE *tree)
{
THREADNODE *collapsed_tree = NULL;
PINETHRD_S *thrd = NULL;
unsigned long msgno, rawno;
int un_view_thread = 0;
long raw_current;
char *dup_chk = NULL;
dprint((2, "sort_thread_callback\n"));
g_sort.msgmap->max_thrdno = 0L;
/*
* Eliminate dummy nodes from tree and collapse the tree in a logical
* way. If the dummy node is at the top-level, then its children are
* promoted to the top-level as separate threads.
*/
if(F_ON(F_THREAD_SORTS_BY_ARRIVAL, ps_global))
collapsed_tree = collapse_threadnode_tree_sorted(tree);
else
collapsed_tree = collapse_threadnode_tree(tree);
/* dup_chk is like sort with an origin of 1 */
dup_chk = (char *) fs_get((mn_get_nmsgs(g_sort.msgmap)+1) * sizeof(char));
memset(dup_chk, 0, (mn_get_nmsgs(g_sort.msgmap)+1) * sizeof(char));
memset(&g_sort.msgmap->sort[1], 0, mn_get_total(g_sort.msgmap) * sizeof(long));
(void) sort_thread_flatten(collapsed_tree, stream,
&g_sort.msgmap->sort[1],
dup_chk, mn_get_nmsgs(g_sort.msgmap),
NULL, THD_TOP);
/* reset the inverse array */
msgno_reset_isort(g_sort.msgmap);
if(dup_chk)
fs_give((void **) &dup_chk);
if(collapsed_tree)
mail_free_threadnode(&collapsed_tree);
if(any_lflagged(g_sort.msgmap, MN_HIDE))
g_sort.msgmap->visible_threads = calculate_visible_threads(stream);
else
g_sort.msgmap->visible_threads = g_sort.msgmap->max_thrdno;
raw_current = mn_m2raw(g_sort.msgmap, mn_get_cur(g_sort.msgmap));
sp_set_need_to_rethread(stream, 0);
/*
* Set appropriate bits to start out collapsed if desired. We use the
* stream spare bit to tell us if we've done this before for this
* stream.
*/
if(!stream->spare
&& (COLL_THRDS() || SEP_THRDINDX())
&& mn_get_total(g_sort.msgmap) > 1L){
collapse_threads(stream, g_sort.msgmap, NULL);
}
else if(stream->spare){
/*
* If we're doing auto collapse then new threads need to have
* their collapse bit set. This happens below if we're in the
* thread index, but if we're in the regular index with auto
* collapse we have to look for these.
*/
if(any_lflagged(g_sort.msgmap, MN_USOR)){
if(COLL_THRDS()){
for(msgno = 1L; msgno <= mn_get_total(g_sort.msgmap); msgno++){
rawno = mn_m2raw(g_sort.msgmap, msgno);
if(get_lflag(stream, NULL, rawno, MN_USOR)){
thrd = fetch_thread(stream, rawno);
/*
* Node is new, unsorted, top-level thread,
* and we're using auto collapse.
*/
if(thrd && !thrd->parent)
set_lflag(stream, g_sort.msgmap, msgno, MN_COLL, 1);
/*
* If a parent is collapsed, clear that parent's
* index cache entry. This is only necessary if
* the parent's index display can depend on its
* children, of course.
*/
if(thrd && thrd->parent){
thrd = fetch_thread(stream, thrd->parent);
while(thrd){
long t;
if(get_lflag(stream, NULL, thrd->rawno, MN_COLL)
&& (t = mn_raw2m(g_sort.msgmap,
(long) thrd->rawno)))
clear_index_cache_ent(stream, t, 0);
if(thrd->parent)
thrd = fetch_thread(stream, thrd->parent);
else
thrd = NULL;
}
}
}
}
}
set_lflags(stream, g_sort.msgmap, MN_USOR, 0);
}
if(sp_viewing_a_thread(stream)){
if(any_lflagged(g_sort.msgmap, MN_CHID2)){
/* current should be part of viewed thread */
if(get_lflag(stream, NULL, raw_current, MN_CHID2)){
thrd = fetch_thread(stream, raw_current);
if(thrd && thrd->top && thrd->top != thrd->rawno)
thrd = fetch_thread(stream, thrd->top);
if(thrd){
/*
* For messages that are part of thread set MN_CHID2
* and for messages that aren't part of the thread
* clear MN_CHID2. Easiest is to just do it instead
* of checking if it is true first.
*/
set_lflags(stream, g_sort.msgmap, MN_CHID2, 0);
set_thread_lflags(stream, thrd, g_sort.msgmap,
MN_CHID2, 1);
/*
* Outside of the viewed thread everything else
* should be collapsed at the top-levels.
*/
collapse_threads(stream, g_sort.msgmap, thrd);
/*
* Inside of the thread, the top of the thread
* can't be hidden, the rest are hidden if a
* parent somewhere above them is collapsed.
* There can be collapse points that are hidden
* inside of the tree. They remain collapsed even
* if the parent above them uncollapses.
*/
msgno = mn_raw2m(g_sort.msgmap, (long) thrd->rawno);
if(msgno)
set_lflag(stream, g_sort.msgmap, msgno, MN_CHID, 0);
if(thrd->next){
PINETHRD_S *nthrd;
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
make_thrdflags_consistent(stream, g_sort.msgmap,
nthrd,
get_lflag(stream, NULL,
thrd->rawno,
MN_COLL));
}
}
else
un_view_thread++;
}
else
un_view_thread++;
}
else
un_view_thread++;
if(un_view_thread){
set_lflags(stream, g_sort.msgmap, MN_CHID2, 0);
unview_thread(ps_global, stream, g_sort.msgmap);
}
else{
mn_reset_cur(g_sort.msgmap,
mn_raw2m(g_sort.msgmap, raw_current));
view_thread(ps_global, stream, g_sort.msgmap, 0);
}
}
else if(SEP_THRDINDX()){
set_lflags(stream, g_sort.msgmap, MN_CHID2, 0);
collapse_threads(stream, g_sort.msgmap, NULL);
}
else{
thrd = fetch_head_thread(stream);
while(thrd){
/*
* The top-level threads aren't hidden by collapse.
*/
msgno = mn_raw2m(g_sort.msgmap, thrd->rawno);
if(msgno)
set_lflag(stream, g_sort.msgmap, msgno, MN_CHID, 0);
if(thrd->next){
PINETHRD_S *nthrd;
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
make_thrdflags_consistent(stream, g_sort.msgmap,
nthrd,
get_lflag(stream, NULL,
thrd->rawno,
MN_COLL));
}
if(thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else
thrd = NULL;
}
}
}
stream->spare = 1;
dprint((2, "sort_thread_callback done\n"));
}
void
collapse_threads(MAILSTREAM *stream, MSGNO_S *msgmap, PINETHRD_S *not_this_thread)
{
PINETHRD_S *thrd = NULL, *nthrd;
unsigned long msgno;
dprint((9, "collapse_threads\n"));
thrd = fetch_head_thread(stream);
while(thrd){
if(thrd != not_this_thread){
msgno = mn_raw2m(g_sort.msgmap, thrd->rawno);
/* set collapsed bit */
if(msgno){
set_lflag(stream, g_sort.msgmap, msgno, MN_COLL, 1);
set_lflag(stream, g_sort.msgmap, msgno, MN_CHID, 0);
}
/* hide its children */
if(thrd->next && (nthrd = fetch_thread(stream, thrd->next)))
set_thread_subtree(stream, nthrd, msgmap, 1, MN_CHID);
}
if(thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else
thrd = NULL;
}
dprint((9, "collapse_threads done\n"));
}
void
make_thrdflags_consistent(MAILSTREAM *stream, MSGNO_S *msgmap, PINETHRD_S *thrd,
int a_parent_is_collapsed)
{
PINETHRD_S *nthrd, *bthrd;
unsigned long msgno;
if(!thrd)
return;
msgno = mn_raw2m(msgmap, thrd->rawno);
if(a_parent_is_collapsed){
/* if some parent is collapsed, we should be hidden */
if(msgno)
set_lflag(stream, msgmap, msgno, MN_CHID, 1);
}
else{
/* no parent is collapsed so we are not hidden */
if(msgno)
set_lflag(stream, msgmap, msgno, MN_CHID, 0);
}
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
make_thrdflags_consistent(stream, msgmap, nthrd,
a_parent_is_collapsed
? a_parent_is_collapsed
: get_lflag(stream, NULL, thrd->rawno,
MN_COLL));
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
make_thrdflags_consistent(stream, msgmap, bthrd,
a_parent_is_collapsed);
}
}
long
calculate_visible_threads(MAILSTREAM *stream)
{
PINETHRD_S *thrd = NULL;
long vis = 0L;
thrd = fetch_head_thread(stream);
while(thrd){
vis += (thread_has_some_visible(stream, thrd) ? 1 : 0);
if(thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else
thrd = NULL;
}
return(vis);
}
/*
* This routine does a couple things. The input is the THREADNODE node
* that we get from c-client because of the THREAD command. The rest of
* the arguments are used to help guide this function through its
* recursive steps. One thing it does is to place the sort order in
* the array initially pointed to by the entry argument. All it is doing
* is walking the tree in the next then branch order you see below and
* incrementing the entry number one for each node. The other thing it
* is doing at the same time is to create a PINETHRD_S tree from the
* THREADNODE tree. The two trees are completely equivalent but the
* PINETHRD_S version has additional back pointers and parent pointers
* and so on to make it easier for alpine to deal with it. Each node
* of that tree is tied to the data associated with a particular message
* by the msgno_thread_info() call, so that we can go from a message
* number to the place in the thread tree that message sits.
*/
long *
sort_thread_flatten(THREADNODE *node, MAILSTREAM *stream,
long *entry, char *dup_chk, long maxno,
PINETHRD_S *thrd, unsigned int flags)
{
PINETHRD_S *newthrd = NULL;
if(node){
if(node->num > 0L && node->num <= maxno){ /* holes happen */
if(!dup_chk[node->num]){ /* not a duplicate */
*entry = node->num;
dup_chk[node->num] = 1;
/*
* Build a richer threading structure that will help us paint
* and operate on threads and subthreads.
*/
newthrd = msgno_thread_info(stream, node->num, thrd, flags);
if(newthrd){
entry++;
if(node->next)
entry = sort_thread_flatten(node->next, stream,
entry, dup_chk, maxno,
newthrd, THD_NEXT);
if(node->branch)
entry = sort_thread_flatten(node->branch, stream,
entry, dup_chk, maxno,
newthrd,
(flags == THD_TOP) ? THD_TOP
: THD_BRANCH);
}
}
}
}
return(entry);
}
/*
* Make a copy of c-client's THREAD tree while eliminating dummy nodes.
*/
THREADNODE *
collapse_threadnode_tree(THREADNODE *tree)
{
THREADNODE *newtree = NULL;
if(tree){
if(tree->num){
newtree = mail_newthreadnode(NULL);
newtree->num = tree->num;
if(tree->next)
newtree->next = collapse_threadnode_tree(tree->next);
if(tree->branch)
newtree->branch = collapse_threadnode_tree(tree->branch);
}
else{
if(tree->next)
newtree = collapse_threadnode_tree(tree->next);
if(tree->branch){
if(newtree){
THREADNODE *last_branch = NULL;
/*
* Next moved up to replace "tree" in the tree.
* If next has no branches, then we want to branch off
* of next. If next has branches, we want to branch off
* of the last of those branches instead.
*/
last_branch = newtree;
while(last_branch->branch)
last_branch = last_branch->branch;
last_branch->branch = collapse_threadnode_tree(tree->branch);
}
else
newtree = collapse_threadnode_tree(tree->branch);
}
}
}
return(newtree);
}
/*
* Like collapse_threadnode_tree, we collapse the dummy nodes.
* In addition we rearrange the threads by order of arrival of
* the last message in the thread, rather than the first message
* in the thread.
*/
THREADNODE *
collapse_threadnode_tree_sorted(THREADNODE *tree)
{
THREADNODE *sorted_tree = NULL;
sorted_tree = sort_threads_and_collapse(tree);
/*
* We used to eliminate top-level dummy nodes here so that
* orphans would still get sorted together, but we changed
* to sort the orphans themselves as top-level threads.
*
* It might be a matter of choice how they get sorted, but
* we'll try doing it this way and not add another feature.
*/
return(sorted_tree);
}
/*
* Recurse through the tree, sorting each top-level branch by the
* greatest num in the thread.
*/
THREADNODE *
sort_threads_and_collapse(THREADNODE *tree)
{
THREADNODE *newtree = NULL, *newbranchtree = NULL, *newtreefree = NULL;
if(tree){
newtree = mail_newthreadnode(NULL);
newtree->num = tree->num;
/*
* Only sort at the top level. Individual threads can
* rely on collapse_threadnode_tree
*/
if(tree->next)
newtree->next = collapse_threadnode_tree(tree->next);
if(tree->branch){
/*
* This recursive call returns an already re-sorted tree.
* With that, we can loop through and inject ourselves
* where we fit in with that sort, and pass back to the
* caller to inject themselves.
*/
newbranchtree = sort_threads_and_collapse(tree->branch);
}
if(newtree->num)
newtree = insert_tree_in_place(newtree, newbranchtree);
else{
/*
* If top node is a dummy, here is where we collapse it.
*/
newtreefree = newtree;
newtree = insert_tree_in_place(newtree->next, newbranchtree);
newtreefree->next = NULL;
mail_free_threadnode(&newtreefree);
}
}
return(newtree);
}
/*
* Recursively insert each of the top-level nodes in newtree in their place
* in tree according to which tree has the most recent arrival
*/
THREADNODE *
insert_tree_in_place(THREADNODE *newtree, THREADNODE *tree)
{
THREADNODE *node = NULL;
unsigned long newtree_greatest_num = 0;
if(newtree->branch){
node = newtree->branch;
newtree->branch = NULL;
tree = insert_tree_in_place(node, tree);
}
newtree_greatest_num = branch_greatest_num(newtree, 0);
if(tree){
/*
* Since tree is already sorted, we can insert when we find something
* newtree is less than
*/
if(newtree_greatest_num < branch_greatest_num(tree, 0))
newtree->branch = tree;
else {
for(node = tree; node->branch; node = node->branch){
if(newtree_greatest_num < branch_greatest_num(node->branch, 0)){
newtree->branch = node->branch;
node->branch = newtree;
break;
}
}
if(!node->branch)
node->branch = newtree;
newtree = tree;
}
}
return(newtree);
}
/*
* Given a thread, return the greatest num in the tree.
* is_subthread tells us not to recurse through branches, so
* we can split the top level into threads.
*/
unsigned long
branch_greatest_num(THREADNODE *tree, int is_subthread)
{
unsigned long ret, branch_ret;
ret = tree->num;
if(tree->next && (branch_ret = branch_greatest_num(tree->next, 1)) > ret)
ret = branch_ret;
if(is_subthread && tree->branch &&
(branch_ret = branch_greatest_num(tree->branch, 1)) > ret)
ret = branch_ret;
return ret;
}
/*
* Args stream -- the usual
* rawno -- the raw msg num associated with this new node
* attached_to_thrd -- the PINETHRD_S node that this is either a next or branch
* off of
* flags --
*/
PINETHRD_S *
msgno_thread_info(MAILSTREAM *stream, long unsigned int rawno,
PINETHRD_S *attached_to_thrd, unsigned int flags)
{
PINELT_S **peltp;
MESSAGECACHE *mc;
if(!stream || rawno < 1L || rawno > stream->nmsgs)
return NULL;
/*
* any private elt data yet?
*/
if((mc = mail_elt(stream, rawno))
&& (*(peltp = (PINELT_S **) &mc->sparep) == NULL)){
*peltp = (PINELT_S *) fs_get(sizeof(PINELT_S));
memset(*peltp, 0, sizeof(PINELT_S));
}
if((*peltp)->pthrd == NULL)
(*peltp)->pthrd = (PINETHRD_S *) fs_get(sizeof(PINETHRD_S));
memset((*peltp)->pthrd, 0, sizeof(PINETHRD_S));
(*peltp)->pthrd->rawno = rawno;
if(attached_to_thrd)
(*peltp)->pthrd->head = attached_to_thrd->head;
else
(*peltp)->pthrd->head = (*peltp)->pthrd->rawno; /* it's me */
if(flags == THD_TOP){
/*
* We can tell this thread is a top-level thread because it doesn't
* have a parent.
*/
(*peltp)->pthrd->top = (*peltp)->pthrd->rawno; /* I am a top */
if(attached_to_thrd){
attached_to_thrd->nextthd = (*peltp)->pthrd->rawno;
(*peltp)->pthrd->prevthd = attached_to_thrd->rawno;
(*peltp)->pthrd->thrdno = attached_to_thrd->thrdno + 1L;
}
else
(*peltp)->pthrd->thrdno = 1L; /* 1st thread */
g_sort.msgmap->max_thrdno = (*peltp)->pthrd->thrdno;
}
else if(flags == THD_NEXT){
if(attached_to_thrd){
attached_to_thrd->next = (*peltp)->pthrd->rawno;
(*peltp)->pthrd->parent = attached_to_thrd->rawno;
(*peltp)->pthrd->top = attached_to_thrd->top;
}
}
else if(flags == THD_BRANCH){
if(attached_to_thrd){
attached_to_thrd->branch = (*peltp)->pthrd->rawno;
(*peltp)->pthrd->parent = attached_to_thrd->parent;
(*peltp)->pthrd->top = attached_to_thrd->top;
}
}
return((*peltp)->pthrd);
}
/*
* Collapse or expand a threading subtree. Not called from separate thread
* index.
*/
void
collapse_or_expand(struct pine *state, MAILSTREAM *stream, MSGNO_S *msgmap,
long unsigned int msgno)
{
int collapsed, adjust_current = 0;
PINETHRD_S *thrd = NULL, *nthrd;
unsigned long rawno;
if(!stream)
return;
/*
* If msgno is a good msgno, then we collapse or expand the subthread
* which begins at msgno. If msgno is 0, we collapse or expand the
* entire current thread.
*/
if(msgno > 0L && msgno <= mn_get_total(msgmap)){
rawno = mn_m2raw(msgmap, msgno);
if(rawno)
thrd = fetch_thread(stream, rawno);
}
else if(msgno == 0L){
rawno = mn_m2raw(msgmap, mn_get_cur(msgmap));
if(rawno)
thrd = fetch_thread(stream, rawno);
if(thrd && thrd->top != thrd->rawno){
adjust_current++;
thrd = fetch_thread(stream, thrd->top);
/*
* Special case. If the user is collapsing the entire thread
* (msgno == 0), and we are in a Zoomed view, and the top of
* the entire thread is not part of the Zoomed view, then watch
* out. If we were to collapse the entire thread it would just
* disappear, because the top is not in the Zoom. Therefore,
* don't allow it. Do what the user probably wants, which is to
* collapse the thread at that point instead of the entire thread,
* leaving behind the top of the subthread to expand if needed.
* In other words, treat it as if they didn't have the
* F_SLASH_COLL_ENTIRE feature set.
*/
collapsed = get_lflag(stream, NULL, thrd->rawno, MN_COLL)
&& thrd->next;
if(!collapsed && get_lflag(stream, NULL, thrd->rawno, MN_HIDE))
thrd = fetch_thread(stream, rawno);
}
}
if(!thrd)
return;
collapsed = get_lflag(stream, NULL, thrd->rawno, MN_COLL) && thrd->next;
if(collapsed){
msgno = mn_raw2m(msgmap, thrd->rawno);
if(msgno > 0L && msgno <= mn_get_total(msgmap)){
set_lflag(stream, msgmap, msgno, MN_COLL, 0);
if(thrd->next){
if((nthrd = fetch_thread(stream, thrd->next)) != NULL)
set_thread_subtree(stream, nthrd, msgmap, 0, MN_CHID);
clear_index_cache_ent(stream, msgno, 0);
}
}
}
else if(thrd && thrd->next){
msgno = mn_raw2m(msgmap, thrd->rawno);
if(msgno > 0L && msgno <= mn_get_total(msgmap)){
set_lflag(stream, msgmap, msgno, MN_COLL, 1);
if((nthrd = fetch_thread(stream, thrd->next)) != NULL)
set_thread_subtree(stream, nthrd, msgmap, 1, MN_CHID);
clear_index_cache_ent(stream, msgno, 0);
}
}
else
q_status_message(SM_ORDER, 0, 1,
_("No thread to collapse or expand on this line"));
/* if current is hidden, adjust */
if(adjust_current)
adjust_cur_to_visible(stream, msgmap);
}
/*
* Select the messages in a subthread. If all of the messages are already
* selected, unselect them. This routine is a bit strange because it
* doesn't set the MN_SLCT bit. Instead, it sets MN_STMP in apply_command
* and then thread_command copies the MN_STMP messages back to MN_SLCT.
*/
void
select_thread_stmp(struct pine *state, MAILSTREAM *stream, MSGNO_S *msgmap)
{
PINETHRD_S *thrd = NULL;
unsigned long rawno, in_thread, set_in_thread, save_branch;
/* ugly bit means the same thing as return of 1 from individual_select */
state->ugly_consider_advancing_bit = 0;
if(!(stream && msgmap))
return;
rawno = mn_m2raw(msgmap, mn_get_cur(msgmap));
if(rawno)
thrd = fetch_thread(stream, rawno);
if(!thrd)
return;
/* run through thrd to see if it is all selected */
save_branch = thrd->branch;
thrd->branch = 0L;
if((set_in_thread = count_lflags_in_thread(stream, thrd, msgmap, MN_STMP))
== (in_thread = count_lflags_in_thread(stream, thrd, msgmap, MN_NONE))){
/*
* If everything is selected, the first unselect should cause
* an autozoom. In order to trigger the right code in
* thread_command()
* copy_lflags()
* we set the MN_HIDE bit on the current message here.
*/
if(F_ON(F_AUTO_ZOOM, state) && !any_lflagged(msgmap, MN_HIDE)
&& any_lflagged(msgmap, MN_STMP) == mn_get_total(msgmap))
set_lflag(stream, msgmap, mn_get_cur(msgmap), MN_HIDE, 1);
set_thread_lflags(stream, thrd, msgmap, MN_STMP, 0);
}
else{
set_thread_lflags(stream, thrd, msgmap, MN_STMP, 1);
state->ugly_consider_advancing_bit = 1;
}
thrd->branch = save_branch;
if(set_in_thread == in_thread)
q_status_message1(SM_ORDER, 0, 3, _("Unselected %s messages in thread"),
comatose((long) in_thread));
else if(set_in_thread == 0)
q_status_message1(SM_ORDER, 0, 3, _("Selected %s messages in thread"),
comatose((long) in_thread));
else
q_status_message1(SM_ORDER, 0, 3,
_("Selected %s more messages in thread"),
comatose((long) (in_thread-set_in_thread)));
}
/*
* Count how many of this system flag in this thread subtree.
* If flags == 0 count the messages in the thread.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately.
* Ok to call it on top-level thread which has no branch already.
*/
unsigned long
count_flags_in_thread(MAILSTREAM *stream, PINETHRD_S *thrd, long int flags)
{
unsigned long count = 0;
PINETHRD_S *nthrd, *bthrd;
MESSAGECACHE *mc;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return count;
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
count += count_flags_in_thread(stream, nthrd, flags);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
count += count_flags_in_thread(stream, bthrd, flags);
}
mc = (thrd && thrd->rawno > 0L && stream && thrd->rawno <= stream->nmsgs)
? mail_elt(stream, thrd->rawno) : NULL;
if(mc && mc->valid && FLAG_MATCH(flags, mc, stream))
count++;
return count;
}
/*
* Count how many of this local flag in this thread subtree.
* If flags == MN_NONE then we just count the messages instead of whether
* the messages have a flag set.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately.
* Ok to call it on top-level thread which has no branch already.
*/
unsigned long
count_lflags_in_thread(MAILSTREAM *stream, PINETHRD_S *thrd, MSGNO_S *msgmap, int flags)
{
unsigned long count = 0;
PINETHRD_S *nthrd, *bthrd;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return count;
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
count += count_lflags_in_thread(stream, nthrd, msgmap, flags);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
count += count_lflags_in_thread(stream, bthrd, msgmap,flags);
}
if(flags == MN_NONE)
count++;
else
count += get_lflag(stream, msgmap, mn_raw2m(msgmap, thrd->rawno), flags);
return count;
}
/*
* Special-purpose for performance improvement.
*/
int
thread_has_some_visible(MAILSTREAM *stream, PINETHRD_S *thrd)
{
PINETHRD_S *nthrd, *bthrd;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return 0;
if(get_lflag(stream, NULL, thrd->rawno, MN_HIDE) == 0)
return 1;
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd && thread_has_some_visible(stream, nthrd))
return 1;
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd && thread_has_some_visible(stream, bthrd))
return 1;
}
return 0;
}
int
mark_msgs_in_thread(MAILSTREAM *stream, PINETHRD_S *thrd, MSGNO_S *msgmap)
{
int count = 0;
PINETHRD_S *nthrd, *bthrd;
MESSAGECACHE *mc;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return count;
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
count += mark_msgs_in_thread(stream, nthrd, msgmap);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
count += mark_msgs_in_thread(stream, bthrd, msgmap);
}
if(stream && thrd->rawno >= 1L && thrd->rawno <= stream->nmsgs &&
(mc = mail_elt(stream,thrd->rawno))
&& !mc->sequence
&& !mc->private.msg.env){
mc->sequence = 1;
count++;
}
return count;
}
/*
* This sets or clears flags for the messages at this node and below in
* a tree.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately.
* Ok to call it on top-level thread which has no branch already.
*/
void
set_thread_lflags(MAILSTREAM *stream, PINETHRD_S *thrd, MSGNO_S *msgmap, int flags, int v)
/* flags to set or clear */
/* set or clear? */
{
unsigned long msgno;
PINETHRD_S *nthrd, *bthrd;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return;
msgno = mn_raw2m(msgmap, thrd->rawno);
set_lflag(stream, msgmap, msgno, flags, v);
/*
* Careful, performance hack. This should logically be a separate
* operation on the thread but it is convenient to stick it in here.
*
* When we back out of viewing a thread to the separate-thread-index
* we may leave behind some cached hlines that aren't quite right
* because they were collapsed. In particular, the plus_col character
* may be wrong. Instead of trying to figure out what it should be just
* clear the cache entries for the this thread when we come back in
* to view it again.
*/
if(msgno > 0L && flags == MN_CHID2 && v == 1)
clear_index_cache_ent(stream, msgno, 0);
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
set_thread_lflags(stream, nthrd, msgmap, flags, v);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
set_thread_lflags(stream, bthrd, msgmap, flags, v);
}
}
char
status_symbol_for_thread(MAILSTREAM *stream, PINETHRD_S *thrd, IndexColType type)
{
char status = ' ';
unsigned long save_branch, cnt, tot_in_thrd;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return status;
save_branch = thrd->branch;
thrd->branch = 0L; /* branch is a sibling, not part of thread */
/*
* This is D if all of thread is deleted,
* else A if all of thread is answered,
* else F if all of thread is forwarded,
* else N if any unseen and not deleted,
* else blank.
*/
if(type == iStatus){
tot_in_thrd = count_flags_in_thread(stream, thrd, F_NONE);
/* all deleted */
if(count_flags_in_thread(stream, thrd, F_DEL) == tot_in_thrd)
status = 'D';
/* all answered */
else if(count_flags_in_thread(stream, thrd, F_ANS) == tot_in_thrd)
status = 'A';
/* all forwarded */
else if(count_flags_in_thread(stream, thrd, F_FWD) == tot_in_thrd)
status = 'F';
/* or any new and not deleted */
else if((!IS_NEWS(stream)
|| F_ON(F_FAKE_NEW_IN_NEWS, ps_global))
&& count_flags_in_thread(stream, thrd, F_UNDEL|F_UNSEEN))
status = 'N';
}
else if(type == iFStatus){
if(!IS_NEWS(stream) || F_ON(F_FAKE_NEW_IN_NEWS, ps_global)){
tot_in_thrd = count_flags_in_thread(stream, thrd, F_NONE);
cnt = count_flags_in_thread(stream, thrd, F_UNSEEN);
if(cnt)
status = (cnt == tot_in_thrd) ? 'N' : 'n';
}
}
else if(type == iIStatus || type == iSIStatus){
tot_in_thrd = count_flags_in_thread(stream, thrd, F_NONE);
/* unseen and recent */
cnt = count_flags_in_thread(stream, thrd, F_RECENT|F_UNSEEN);
if(cnt)
status = (cnt == tot_in_thrd) ? 'N' : 'n';
else{
/* unseen and !recent */
cnt = count_flags_in_thread(stream, thrd, F_UNSEEN);
if(cnt)
status = (cnt == tot_in_thrd) ? 'U' : 'u';
else{
/* seen and recent */
cnt = count_flags_in_thread(stream, thrd, F_RECENT|F_SEEN);
if(cnt)
status = (cnt == tot_in_thrd) ? 'R' : 'r';
}
}
}
thrd->branch = save_branch;
return status;
}
/*
* Symbol is * if some message in thread is important,
* + if some message is to us,
* - if mark-for-cc and some message is cc to us, else blank.
*/
char
to_us_symbol_for_thread(MAILSTREAM *stream, PINETHRD_S *thrd, int consider_flagged)
{
char to_us = ' ';
char branch_to_us = ' ';
PINETHRD_S *nthrd, *bthrd;
MESSAGECACHE *mc;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return to_us;
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
to_us = to_us_symbol_for_thread(stream, nthrd, consider_flagged);
}
if(((consider_flagged && to_us != '*') || (!consider_flagged && to_us != '+'))
&& thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
branch_to_us = to_us_symbol_for_thread(stream, bthrd, consider_flagged);
/* use branch to_us symbol if it has higher priority than what we have so far */
if(to_us == ' '){
if(branch_to_us == '-' || branch_to_us == '+' || branch_to_us == '*')
to_us = branch_to_us;
}
else if(to_us == '-'){
if(branch_to_us == '+' || branch_to_us == '*')
to_us = branch_to_us;
}
else if(to_us == '+'){
if(branch_to_us == '*')
to_us = branch_to_us;
}
}
if((consider_flagged && to_us != '*') || (!consider_flagged && to_us != '+')){
if(consider_flagged && thrd && thrd->rawno > 0L
&& stream && thrd->rawno <= stream->nmsgs
&& (mc = mail_elt(stream, thrd->rawno))
&& FLAG_MATCH(F_FLAG, mc, stream))
to_us = '*';
else if(to_us != '+' && !IS_NEWS(stream)){
INDEXDATA_S idata;
MESSAGECACHE *mc;
ADDRESS *addr;
memset(&idata, 0, sizeof(INDEXDATA_S));
idata.stream = stream;
idata.rawno = thrd->rawno;
idata.msgno = mn_raw2m(sp_msgmap(stream), idata.rawno);
if(idata.rawno > 0L && stream && idata.rawno <= stream->nmsgs
&& (mc = mail_elt(stream, idata.rawno))){
idata.size = mc->rfc822_size;
index_data_env(&idata,
pine_mail_fetchenvelope(stream, idata.rawno));
}
else
idata.bogus = 2;
for(addr = fetch_to(&idata); addr; addr = addr->next)
if(address_is_us(addr, ps_global)){
to_us = '+';
break;
}
if(to_us != '+' && resent_to_us(&idata))
to_us = '+';
if(to_us == ' ' && F_ON(F_MARK_FOR_CC,ps_global))
for(addr = fetch_cc(&idata); addr; addr = addr->next)
if(address_is_us(addr, ps_global)){
to_us = '-';
break;
}
}
}
return to_us;
}
/*
* This sets or clears flags for the messages at this node and below in
* a tree. It doesn't just blindly do it, perhaps it should. Instead,
* when un-hiding a subtree it leaves the sub-subtree hidden if a node
* is collapsed.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately.
* Ok to call it on top-level thread which has no branch already.
*/
void
set_thread_subtree(MAILSTREAM *stream, PINETHRD_S *thrd, MSGNO_S *msgmap, int v, int flags)
/* set or clear? */
/* flags to set or clear */
{
int hiding;
unsigned long msgno;
PINETHRD_S *nthrd, *bthrd;
hiding = (flags == MN_CHID) && v;
if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
return;
msgno = mn_raw2m(msgmap, thrd->rawno);
set_lflag(stream, msgmap, msgno, flags, v);
if(thrd->next && (hiding || !get_lflag(stream,NULL,thrd->rawno,MN_COLL))){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
set_thread_subtree(stream, nthrd, msgmap, v, flags);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
set_thread_subtree(stream, bthrd, msgmap, v, flags);
}
}
/*
* View a thread. Move from the thread index screen to a message index
* screen for the current thread.
*
* set_lflags - Set the local flags appropriately to start viewing
* the thread. We would not want to set this if we are
* already viewing the thread (and expunge or new mail
* happened) and we want to preserve the collapsed state
* of the subthreads.
*/
int
view_thread(struct pine *state, MAILSTREAM *stream, MSGNO_S *msgmap, int set_lflags)
{
PINETHRD_S *thrd = NULL;
unsigned long rawno, cur;
if(!any_messages(msgmap, NULL, "to View"))
return 0;
if(!(stream && msgmap))
return 0;
rawno = mn_m2raw(msgmap, mn_get_cur(msgmap));
if(rawno)
thrd = fetch_thread(stream, rawno);
if(thrd && thrd->top && thrd->top != thrd->rawno)
thrd = fetch_thread(stream, thrd->top);
if(!thrd)
return 0;
/*
* Clear hidden and collapsed flag for this thread.
* And set CHID2.
* Don't have to worry about there being a branch because
* this is a toplevel thread.
*/
if(set_lflags){
set_thread_lflags(stream, thrd, msgmap, MN_COLL | MN_CHID, 0);
set_thread_lflags(stream, thrd, msgmap, MN_CHID2, 1);
}
/*
* If this is one of those wacky users who like to sort backwards
* they would probably prefer that the current message be the last
* one in the thread (the one highest up the screen).
*/
if(mn_get_revsort(msgmap)){
cur = mn_get_cur(msgmap);
while(cur > 1L && get_lflag(stream, msgmap, cur-1L, MN_CHID2))
cur--;
if(cur != mn_get_cur(msgmap))
mn_set_cur(msgmap, cur);
}
/* first message in thread might be hidden if zoomed */
if(any_lflagged(msgmap, MN_HIDE)){
cur = mn_get_cur(msgmap);
while(get_lflag(stream, msgmap, cur, MN_HIDE))
cur++;
if(cur != mn_get_cur(msgmap))
mn_set_cur(msgmap, cur);
}
msgmap->top = mn_get_cur(msgmap);
sp_set_viewing_a_thread(stream, 1);
state->mangled_screen = 1;
setup_for_index_index_screen();
return 1;
}
int
unview_thread(struct pine *state, MAILSTREAM *stream, MSGNO_S *msgmap)
{
PINETHRD_S *thrd = NULL, *topthrd = NULL;
unsigned long rawno;
if(!(stream && msgmap))
return 0;
rawno = mn_m2raw(msgmap, mn_get_cur(msgmap));
if(rawno)
thrd = fetch_thread(stream, rawno);
if(thrd && thrd->top)
topthrd = fetch_thread(stream, thrd->top);
if(!topthrd)
return 0;
/* hide this thread */
set_thread_lflags(stream, topthrd, msgmap, MN_CHID, 1);
/* clear special CHID2 flags for this thread */
set_thread_lflags(stream, topthrd, msgmap, MN_CHID2, 0);
/* clear CHID for top-level message and set COLL */
set_lflag(stream, msgmap, mn_raw2m(msgmap, topthrd->rawno), MN_CHID, 0);
set_lflag(stream, msgmap, mn_raw2m(msgmap, topthrd->rawno), MN_COLL, 1);
mn_set_cur(msgmap, mn_raw2m(msgmap, topthrd->rawno));
sp_set_viewing_a_thread(stream, 0);
setup_for_thread_index_screen();
return 1;
}
PINETHRD_S *
find_thread_by_number(MAILSTREAM *stream, MSGNO_S *msgmap, long int target, PINETHRD_S *startthrd)
{
PINETHRD_S *thrd = NULL;
if(!(stream && msgmap))
return(thrd);
thrd = startthrd;
if(!thrd || !(thrd->prevthd || thrd->nextthd))
thrd = fetch_thread(stream, mn_m2raw(msgmap, mn_get_cur(msgmap)));
if(thrd && !(thrd->prevthd || thrd->nextthd) && thrd->head)
thrd = fetch_thread(stream, thrd->head);
if(thrd){
/* go forward from here */
if(thrd->thrdno < target){
while(thrd){
if(thrd->thrdno == target)
break;
if(mn_get_revsort(msgmap) && thrd->prevthd)
thrd = fetch_thread(stream, thrd->prevthd);
else if(!mn_get_revsort(msgmap) && thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else
thrd = NULL;
}
}
/* back up from here */
else if(thrd->thrdno > target
&& (mn_get_revsort(msgmap)
|| (thrd->thrdno - target) < (target - 1L))){
while(thrd){
if(thrd->thrdno == target)
break;
if(mn_get_revsort(msgmap) && thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else if(!mn_get_revsort(msgmap) && thrd->prevthd)
thrd = fetch_thread(stream, thrd->prevthd);
else
thrd = NULL;
}
}
/* go forward from head */
else if(thrd->thrdno > target){
if(thrd->head){
thrd = fetch_thread(stream, thrd->head);
while(thrd){
if(thrd->thrdno == target)
break;
if(thrd->nextthd)
thrd = fetch_thread(stream, thrd->nextthd);
else
thrd = NULL;
}
}
}
}
return(thrd);
}
/*
* Set search bit for every message in a thread.
*
* Watch out when calling this. The thrd->branch is not part of thrd.
* Branch is a sibling to thrd, not a child. Zero out branch before calling
* or call on thrd->next and worry about thrd separately. Top-level threads
* already have a branch equal to zero.
*
* If msgset is non-NULL, then only set the search bit for a message if that
* message is included in the msgset.
*/
void
set_search_bit_for_thread(MAILSTREAM *stream, PINETHRD_S *thrd, SEARCHSET **msgset)
{
PINETHRD_S *nthrd, *bthrd;
if(!(stream && thrd))
return;
if(thrd->rawno > 0L && thrd->rawno <= stream->nmsgs
&& (!(msgset && *msgset) || in_searchset(*msgset, thrd->rawno)))
mm_searched(stream, thrd->rawno);
if(thrd->next){
nthrd = fetch_thread(stream, thrd->next);
if(nthrd)
set_search_bit_for_thread(stream, nthrd, msgset);
}
if(thrd->branch){
bthrd = fetch_thread(stream, thrd->branch);
if(bthrd)
set_search_bit_for_thread(stream, bthrd, msgset);
}
}
|