2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
8 * Gunnar Ritter. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Gunnar Ritter
21 * and his contributors.
22 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid
[] = "@(#)thread.c 1.57 (gritter) 3/4/06";
52 * Mail -- a mail program
58 * Open addressing is used for Message-IDs because the maximum number of
59 * messages in the table is known in advance (== msgCount).
62 struct message
*mi_data
;
75 static unsigned mhash(const char *cp
, int mprime
);
76 static struct mitem
*mlook(char *id
, struct mitem
*mt
, struct message
*mdata
,
78 static void adopt(struct message
*parent
, struct message
*child
, int dist
);
79 static struct message
*interlink(struct message
*m
, long count
, int newmail
);
80 static void finalize(struct message
*mp
);
81 static int mlonglt(const void *a
, const void *b
);
82 static int mfloatlt(const void *a
, const void *b
);
83 static int mcharlt(const void *a
, const void *b
);
84 static void lookup(struct message
*m
, struct mitem
*mi
, int mprime
);
85 static void makethreads(struct message
*m
, long count
, int newmail
);
86 static char *skipre(const char *cp
);
87 static int colpt(int *msgvec
, int cl
);
88 static void colps(struct message
*b
, int cl
);
89 static void colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
92 * Return the hash value for a message id modulo mprime, or mprime
93 * if the passed string does not look like a message-id.
96 mhash(const char *cp
, int mprime
)
99 unsigned h
= 0, g
, at
= 0;
104 * Pay attention not to hash characters which are
105 * irrelevant for Message-ID semantics.
108 cp
= skip_comment(&cp
[1]) - 1;
111 if (*cp
== '"' || *cp
== '\\')
115 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
& 0377);
116 if ((g
= h
& 0xf0000000) != 0) {
121 return at
? h
% mprime
: mprime
;
124 #define NOT_AN_ID ((struct mitem *)-1)
127 * Look up a message id. Returns NOT_AN_ID if the passed string does
128 * not look like a message-id.
130 static struct mitem
*
131 mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, int mprime
)
134 unsigned h
, c
, n
= 0;
136 if (id
== NULL
&& (id
= hfield("message-id", mdata
)) == NULL
)
138 if (mdata
&& mdata
->m_idhash
)
139 h
= ~mdata
->m_idhash
;
141 h
= mhash(id
, mprime
);
146 while (mp
->mi_id
!= NULL
) {
147 if (msgidcmp(mp
->mi_id
, id
) == 0)
149 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
155 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
158 mdata
->m_idhash
= ~h
;
160 return mp
->mi_id
? mp
: NULL
;
164 * Child is to be adopted by parent. A thread tree is structured
167 * ------ m_child ------ m_child
168 * | |-------------------->| |------------------------> . . .
169 * | |<--------------------| |<----------------------- . . .
170 * ------ m_parent ------ m_parent
172 * | \____ m_younger | |
176 * | m_parent ---- | |
181 * | | |------------------------> . . .
182 * | | |<----------------------- . . .
185 * \----- m_younger | |
194 * | |------------------------> . . .
195 * | |<----------------------- . . .
200 * The base message of a thread does not have a m_parent link. Elements
201 * connected by m_younger/m_elder links are replies to the same message,
202 * which is connected to them by m_parent links. The first reply to a
203 * message gets the m_child link.
206 adopt(struct message
*parent
, struct message
*child
, int dist
)
208 struct message
*mp
, *mq
;
210 for (mp
= parent
; mp
; mp
= mp
->m_parent
)
213 child
->m_level
= dist
; /* temporarily store distance */
214 child
->m_parent
= parent
;
215 if (parent
->m_child
!= NULL
) {
217 for (mp
= parent
->m_child
; mp
; mp
= mp
->m_younger
) {
218 if (mp
->m_date
>= child
->m_date
) {
220 mp
->m_elder
->m_younger
= child
;
221 child
->m_elder
= mp
->m_elder
;
223 child
->m_younger
= mp
;
224 if (mp
== parent
->m_child
)
225 parent
->m_child
= child
;
230 mq
->m_younger
= child
;
233 parent
->m_child
= child
;
237 * Connect all messages on the lowest thread level with m_younger/m_elder
240 static struct message
*
241 interlink(struct message
*m
, long count
, int newmail
)
246 struct message
*root
;
247 int autocollapse
= !newmail
&& !(inhook
&2) &&
248 value("autocollapse") != NULL
;
250 ms
= smalloc(sizeof *ms
* count
);
251 for (n
= 0, i
= 0; i
< count
; i
++) {
252 if (m
[i
].m_parent
== NULL
) {
255 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
261 qsort(ms
, n
, sizeof *ms
, mlonglt
);
262 root
= &m
[ms
[0].ms_n
];
263 for (i
= 1; i
< n
; i
++) {
264 m
[ms
[i
-1].ms_n
].m_younger
= &m
[ms
[i
].ms_n
];
265 m
[ms
[i
].ms_n
].m_elder
= &m
[ms
[i
-1].ms_n
];
274 finalize(struct message
*mp
)
278 for (n
= 0; mp
; mp
= next_in_thread(mp
)) {
279 mp
->m_threadpos
= ++n
;
280 mp
->m_level
= mp
->m_parent
?
281 mp
->m_level
+ mp
->m_parent
->m_level
: 0;
286 mlonglt(const void *a
, const void *b
)
290 i
= ((struct msort
*)a
)->ms_u
.ms_long
-
291 ((struct msort
*)b
)->ms_u
.ms_long
;
293 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
298 mfloatlt(const void *a
, const void *b
)
302 i
= ((struct msort
*)a
)->ms_u
.ms_float
-
303 ((struct msort
*)b
)->ms_u
.ms_float
;
305 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
306 return i
> 0 ? 1 : i
< 0 ? -1 : 0;
310 mcharlt(const void *a
, const void *b
)
314 i
= strcoll(((struct msort
*)a
)->ms_u
.ms_char
,
315 ((struct msort
*)b
)->ms_u
.ms_char
);
317 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
323 lookup(struct message
*m
, struct mitem
*mi
, int mprime
)
330 if (m
->m_flag
& MHIDDEN
)
333 if ((cp
= hfield("in-reply-to", m
)) != NULL
) {
334 if ((np
= extract(cp
, GREF
)) != NULL
)
336 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
337 != NULL
&& ip
!= NOT_AN_ID
) {
338 adopt(ip
->mi_data
, m
, 1);
341 } while ((np
= np
->n_flink
) != NULL
);
343 if ((cp
= hfield("references", m
)) != NULL
) {
344 if ((np
= extract(cp
, GREF
)) != NULL
) {
345 while (np
->n_flink
!= NULL
)
348 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
351 continue; /* skip dist++ */
352 adopt(ip
->mi_data
, m
, dist
);
356 } while ((np
= np
->n_blink
) != NULL
);
362 makethreads(struct message
*m
, long count
, int newmail
)
370 mprime
= nextprime(count
);
371 mt
= scalloc(mprime
, sizeof *mt
);
372 for (i
= 0; i
< count
; i
++) {
373 if ((m
[i
].m_flag
&MHIDDEN
) == 0) {
374 mlook(NULL
, mt
, &m
[i
], mprime
);
375 if (m
[i
].m_date
== 0) {
376 if ((cp
= hfield("date", &m
[i
])) != NULL
)
377 m
[i
].m_date
= rfctime(cp
);
380 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
=
381 m
[i
].m_parent
= NULL
;
383 if (!newmail
&& !(inhook
&2))
384 m
[i
].m_collapsed
= 0;
387 * Most folders contain the eldest messages first. Traversing
388 * them in descending order makes it more likely that younger
389 * brothers are found first, so elder ones can be prepended to
390 * the brother list, which is faster. The worst case is still
391 * in O(n^2) and occurs when all but one messages in a folder
392 * are replies to the one message, and are sorted such that
393 * youngest messages occur first.
395 for (i
= count
-1; i
>= 0; i
--)
396 lookup(&m
[i
], mt
, mprime
);
397 threadroot
= interlink(m
, count
, newmail
);
398 finalize(threadroot
);
406 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void *)-1) {
407 if (mb
.mb_type
== MB_IMAP
)
408 imap_getheaders(1, msgCount
);
409 makethreads(message
, msgCount
, vp
== (void *)-1);
411 mb
.mb_sorted
= sstrdup("thread");
413 if (vp
&& vp
!= (void *)-1 && !inhook
&& value("header"))
426 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
428 if (vp
&& !inhook
&& value("header"))
434 next_in_thread(struct message
*mp
)
439 return mp
->m_younger
;
440 while (mp
->m_parent
) {
441 if (mp
->m_parent
->m_younger
)
442 return mp
->m_parent
->m_younger
;
449 prev_in_thread(struct message
*mp
)
453 while (mp
->m_child
) {
455 while (mp
->m_younger
)
464 this_in_thread(struct message
*mp
, long n
)
468 if (n
== -1) { /* find end of thread */
474 mq
= next_in_thread(mp
);
475 if (mq
== NULL
|| mq
->m_threadpos
< mp
->m_threadpos
)
481 while (mp
&& mp
->m_threadpos
< n
) {
482 if (mp
->m_younger
&& mp
->m_younger
->m_threadpos
<= n
) {
486 mp
= next_in_thread(mp
);
488 return mp
&& mp
->m_threadpos
== n
? mp
: NULL
;
492 * Sorted mode is internally just a variant of threaded mode with all
493 * m_parent and m_child links being NULL.
510 enum method me_method
;
511 int (*me_func
)(const void *, const void *);
513 { "date", SORT_DATE
, mlonglt
},
514 { "from", SORT_FROM
, mcharlt
},
515 { "to", SORT_TO
, mcharlt
},
516 { "subject", SORT_SUBJECT
, mcharlt
},
517 { "size", SORT_SIZE
, mlonglt
},
518 { "status", SORT_STATUS
, mlonglt
},
519 { "score", SORT_SCORE
, mfloatlt
},
520 { "thread", SORT_THREAD
, NULL
},
523 char **args
= (char **)vp
, *cp
, *_args
[2];
524 int (*func
)(const void *, const void *);
528 int showname
= value("showname") != NULL
;
531 msgvec
[0] = dot
- &message
[0] + 1;
533 if (vp
== NULL
|| vp
== (void *)-1) {
534 _args
[0] = savestr(mb
.mb_sorted
);
537 } else if (args
[0] == NULL
) {
538 printf("Current sorting criterion is: %s\n",
539 mb
.mb_sorted
? mb
.mb_sorted
: "unsorted");
542 for (i
= 0; methnames
[i
].me_name
; i
++)
543 if (*args
[0] && is_prefix(args
[0], methnames
[i
].me_name
))
545 if (methnames
[i
].me_name
== NULL
) {
546 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
549 method
= methnames
[i
].me_method
;
550 func
= methnames
[i
].me_func
;
552 mb
.mb_sorted
= sstrdup(args
[0]);
553 if (method
== SORT_THREAD
)
554 return thread(vp
&& vp
!= (void *)-1 ? msgvec
: vp
);
555 ms
= ac_alloc(sizeof *ms
* msgCount
);
561 if (mb
.mb_type
== MB_IMAP
)
562 imap_getheaders(1, msgCount
);
567 for (n
= 0, i
= 0; i
< msgCount
; i
++) {
569 if ((mp
->m_flag
&MHIDDEN
) == 0) {
572 if (mp
->m_date
== 0 &&
573 (cp
= hfield("date", mp
)) != 0)
574 mp
->m_date
= rfctime(cp
);
575 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
578 if (mp
->m_flag
& MDELETED
)
579 ms
[n
].ms_u
.ms_long
= 1;
580 else if ((mp
->m_flag
&(MNEW
|MREAD
)) == MNEW
)
581 ms
[n
].ms_u
.ms_long
= 90;
582 else if (mp
->m_flag
& MFLAGGED
)
583 ms
[n
].ms_u
.ms_long
= 85;
584 else if ((mp
->m_flag
&(MNEW
|MBOX
)) == MBOX
)
585 ms
[n
].ms_u
.ms_long
= 70;
586 else if (mp
->m_flag
& MNEW
)
587 ms
[n
].ms_u
.ms_long
= 80;
588 else if (mp
->m_flag
& MREAD
)
589 ms
[n
].ms_u
.ms_long
= 40;
591 ms
[n
].ms_u
.ms_long
= 60;
594 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
597 ms
[n
].ms_u
.ms_float
= mp
->m_score
;
601 if ((cp
= hfield(method
== SORT_FROM
?
602 "from" : "to", mp
)) != NULL
) {
603 ms
[n
].ms_u
.ms_char
= showname
?
604 realname(cp
) : skin(cp
);
605 makelow(ms
[n
].ms_u
.ms_char
);
607 ms
[n
].ms_u
.ms_char
= "";
611 if ((cp
= hfield("subject", mp
)) != NULL
) {
614 mime_fromhdr(&in
, &out
, TD_ICONV
);
616 savestr(skipre(out
.s
));
618 makelow(ms
[n
].ms_u
.ms_char
);
620 ms
[n
].ms_u
.ms_char
= "";
625 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
630 qsort(ms
, n
, sizeof *ms
, func
);
631 threadroot
= &message
[ms
[0].ms_n
];
632 for (i
= 1; i
< n
; i
++) {
633 message
[ms
[i
-1].ms_n
].m_younger
= &message
[ms
[i
].ms_n
];
634 message
[ms
[i
].ms_n
].m_elder
= &message
[ms
[i
-1].ms_n
];
637 threadroot
= &message
[0];
638 finalize(threadroot
);
641 return vp
&& vp
!= (void *)-1 && !inhook
&&
642 value("header") ? headers(msgvec
) : 0;
646 skipre(const char *cp
)
648 if (lowerconv(cp
[0]&0377) == 'r' &&
649 lowerconv(cp
[1]&0377) == 'e' &&
651 spacechar(cp
[3]&0377)) {
653 while (spacechar(*cp
&0377))
672 colpt(int *msgvec
, int cl
)
676 if (mb
.mb_threaded
!= 1) {
677 puts("Not in threaded mode.");
680 for (ip
= msgvec
; *ip
!= 0; ip
++)
681 colps(&message
[*ip
-1], cl
);
686 colps(struct message
*b
, int cl
)
691 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
|MREAD
)) == MNEW
))
695 colpm(m
, cl
, &cc
, &uc
);
696 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
697 colpm(m
, cl
, &cc
, &uc
);
700 b
->m_collapsed
= -cc
;
701 for (m
= b
->m_parent
; m
; m
= m
->m_parent
)
702 if (m
->m_collapsed
<= -uc
) {
703 m
->m_collapsed
+= uc
;
707 if (b
->m_collapsed
> 0) {
711 for (m
= b
; m
; m
= m
->m_parent
)
712 if (m
->m_collapsed
<= -uc
) {
713 m
->m_collapsed
+= uc
;
720 colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
723 if (m
->m_collapsed
> 0)
725 if ((m
->m_flag
& (MNEW
|MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
727 if (m
->m_collapsed
> 0)
730 if (m
->m_collapsed
> 0) {
737 colpm(m
, cl
, cc
, uc
);
738 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
739 colpm(m
, cl
, cc
, uc
);
744 uncollapse1(struct message
*m
, int always
)
746 if (mb
.mb_threaded
== 1 && (always
|| m
->m_collapsed
> 0))