2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * Mail -- a mail program
53 * Open addressing is used for Message-IDs because the maximum number of
54 * messages in the table is known in advance (== msgCount).
57 struct message
*mi_data
;
70 static unsigned mhash(const char *cp
, int mprime
);
71 static struct mitem
*mlook(char *id
, struct mitem
*mt
, struct message
*mdata
,
73 static void adopt(struct message
*parent
, struct message
*child
, int dist
);
74 static struct message
*interlink(struct message
*m
, long count
, int newmail
);
75 static void finalize(struct message
*mp
);
76 static int mlonglt(const void *a
, const void *b
);
77 static int mfloatlt(const void *a
, const void *b
);
78 static int mcharlt(const void *a
, const void *b
);
79 static void lookup(struct message
*m
, struct mitem
*mi
, int mprime
);
80 static void makethreads(struct message
*m
, long count
, int newmail
);
81 static char *skipre(const char *cp
);
82 static int colpt(int *msgvec
, int cl
);
83 static void colps(struct message
*b
, int cl
);
84 static void colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
87 * Return the hash value for a message id modulo mprime, or mprime
88 * if the passed string does not look like a message-id.
91 mhash(const char *cp
, int mprime
)
94 unsigned h
= 0, g
, at
= 0;
99 * Pay attention not to hash characters which are
100 * irrelevant for Message-ID semantics.
103 cp
= skip_comment(&cp
[1]) - 1;
106 if (*cp
== '"' || *cp
== '\\')
110 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
& 0377);
111 if ((g
= h
& 0xf0000000) != 0) {
116 return at
? h
% (unsigned int)mprime
: (unsigned int)mprime
;
119 #define NOT_AN_ID ((struct mitem *)-1)
122 * Look up a message id. Returns NOT_AN_ID if the passed string does
123 * not look like a message-id.
125 static struct mitem
*
126 mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, int mprime
)
129 unsigned h
, c
, n
= 0;
131 if (id
== NULL
&& (id
= hfield("message-id", mdata
)) == NULL
)
133 if (mdata
&& mdata
->m_idhash
)
134 h
= ~mdata
->m_idhash
;
136 h
= mhash(id
, mprime
);
137 if (h
== (unsigned int)mprime
)
141 while (mp
->mi_id
!= NULL
) {
142 if (msgidcmp(mp
->mi_id
, id
) == 0)
144 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
146 while (c
>= (unsigned int)mprime
)
150 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
153 mdata
->m_idhash
= ~h
;
155 return mp
->mi_id
? mp
: NULL
;
159 * Child is to be adopted by parent. A thread tree is structured
162 * ------ m_child ------ m_child
163 * | |-------------------->| |------------------------> . . .
164 * | |<--------------------| |<----------------------- . . .
165 * ------ m_parent ------ m_parent
167 * | \____ m_younger | |
171 * | m_parent ---- | |
176 * | | |------------------------> . . .
177 * | | |<----------------------- . . .
180 * \----- m_younger | |
189 * | |------------------------> . . .
190 * | |<----------------------- . . .
195 * The base message of a thread does not have a m_parent link. Elements
196 * connected by m_younger/m_elder links are replies to the same message,
197 * which is connected to them by m_parent links. The first reply to a
198 * message gets the m_child link.
201 adopt(struct message
*parent
, struct message
*child
, int dist
)
203 struct message
*mp
, *mq
;
205 for (mp
= parent
; mp
; mp
= mp
->m_parent
)
208 child
->m_level
= dist
; /* temporarily store distance */
209 child
->m_parent
= parent
;
210 if (parent
->m_child
!= NULL
) {
212 for (mp
= parent
->m_child
; mp
; mp
= mp
->m_younger
) {
213 if (mp
->m_date
>= child
->m_date
) {
215 mp
->m_elder
->m_younger
= child
;
216 child
->m_elder
= mp
->m_elder
;
218 child
->m_younger
= mp
;
219 if (mp
== parent
->m_child
)
220 parent
->m_child
= child
;
225 mq
->m_younger
= child
;
228 parent
->m_child
= child
;
232 * Connect all messages on the lowest thread level with m_younger/m_elder
235 static struct message
*
236 interlink(struct message
*m
, long count
, int newmail
)
241 struct message
*root
;
242 int autocollapse
= !newmail
&& !(inhook
&2) &&
243 value("autocollapse") != NULL
;
245 ms
= smalloc(sizeof *ms
* count
);
246 for (n
= 0, i
= 0; i
< count
; i
++) {
247 if (m
[i
].m_parent
== NULL
) {
250 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
256 qsort(ms
, n
, sizeof *ms
, mlonglt
);
257 root
= &m
[ms
[0].ms_n
];
258 for (i
= 1; i
< n
; i
++) {
259 m
[ms
[i
-1].ms_n
].m_younger
= &m
[ms
[i
].ms_n
];
260 m
[ms
[i
].ms_n
].m_elder
= &m
[ms
[i
-1].ms_n
];
269 finalize(struct message
*mp
)
273 for (n
= 0; mp
; mp
= next_in_thread(mp
)) {
274 mp
->m_threadpos
= ++n
;
275 mp
->m_level
= mp
->m_parent
?
276 mp
->m_level
+ mp
->m_parent
->m_level
: 0;
281 mlonglt(const void *a
, const void *b
)
285 i
= ((struct msort
*)a
)->ms_u
.ms_long
-
286 ((struct msort
*)b
)->ms_u
.ms_long
;
288 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
293 mfloatlt(const void *a
, const void *b
)
297 i
= ((struct msort
*)a
)->ms_u
.ms_float
-
298 ((struct msort
*)b
)->ms_u
.ms_float
;
300 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
301 return i
> 0 ? 1 : i
< 0 ? -1 : 0;
305 mcharlt(const void *a
, const void *b
)
309 i
= strcoll(((struct msort
*)a
)->ms_u
.ms_char
,
310 ((struct msort
*)b
)->ms_u
.ms_char
);
312 i
= ((struct msort
*)a
)->ms_n
- ((struct msort
*)b
)->ms_n
;
318 lookup(struct message
*m
, struct mitem
*mi
, int mprime
)
325 if (m
->m_flag
& MHIDDEN
)
328 if ((cp
= hfield("in-reply-to", m
)) != NULL
) {
329 if ((np
= extract(cp
, GREF
)) != NULL
)
331 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
332 != NULL
&& ip
!= NOT_AN_ID
) {
333 adopt(ip
->mi_data
, m
, 1);
336 } while ((np
= np
->n_flink
) != NULL
);
338 if ((cp
= hfield("references", m
)) != NULL
) {
339 if ((np
= extract(cp
, GREF
)) != NULL
) {
340 while (np
->n_flink
!= NULL
)
343 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
346 continue; /* skip dist++ */
347 adopt(ip
->mi_data
, m
, dist
);
351 } while ((np
= np
->n_blink
) != NULL
);
357 makethreads(struct message
*m
, long count
, int newmail
)
365 mprime
= nextprime(count
);
366 mt
= scalloc(mprime
, sizeof *mt
);
367 for (i
= 0; i
< count
; i
++) {
368 if ((m
[i
].m_flag
&MHIDDEN
) == 0) {
369 mlook(NULL
, mt
, &m
[i
], mprime
);
370 if (m
[i
].m_date
== 0) {
371 if ((cp
= hfield("date", &m
[i
])) != NULL
)
372 m
[i
].m_date
= rfctime(cp
);
375 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
=
376 m
[i
].m_parent
= NULL
;
378 if (!newmail
&& !(inhook
&2))
379 m
[i
].m_collapsed
= 0;
382 * Most folders contain the eldest messages first. Traversing
383 * them in descending order makes it more likely that younger
384 * brothers are found first, so elder ones can be prepended to
385 * the brother list, which is faster. The worst case is still
386 * in O(n^2) and occurs when all but one messages in a folder
387 * are replies to the one message, and are sorted such that
388 * youngest messages occur first.
390 for (i
= count
-1; i
>= 0; i
--)
391 lookup(&m
[i
], mt
, mprime
);
392 threadroot
= interlink(m
, count
, newmail
);
393 finalize(threadroot
);
401 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void *)-1) {
402 if (mb
.mb_type
== MB_IMAP
)
403 imap_getheaders(1, msgCount
);
404 makethreads(message
, msgCount
, vp
== (void *)-1);
406 mb
.mb_sorted
= sstrdup("thread");
408 if (vp
&& vp
!= (void *)-1 && !inhook
&& value("header"))
421 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
423 if (vp
&& !inhook
&& value("header"))
429 next_in_thread(struct message
*mp
)
434 return mp
->m_younger
;
435 while (mp
->m_parent
) {
436 if (mp
->m_parent
->m_younger
)
437 return mp
->m_parent
->m_younger
;
444 prev_in_thread(struct message
*mp
)
448 while (mp
->m_child
) {
450 while (mp
->m_younger
)
459 this_in_thread(struct message
*mp
, long n
)
463 if (n
== -1) { /* find end of thread */
469 mq
= next_in_thread(mp
);
470 if (mq
== NULL
|| mq
->m_threadpos
< mp
->m_threadpos
)
476 while (mp
&& mp
->m_threadpos
< n
) {
477 if (mp
->m_younger
&& mp
->m_younger
->m_threadpos
<= n
) {
481 mp
= next_in_thread(mp
);
483 return mp
&& mp
->m_threadpos
== n
? mp
: NULL
;
487 * Sorted mode is internally just a variant of threaded mode with all
488 * m_parent and m_child links being NULL.
505 enum method me_method
;
506 int (*me_func
)(const void *, const void *);
508 { "date", SORT_DATE
, mlonglt
},
509 { "from", SORT_FROM
, mcharlt
},
510 { "to", SORT_TO
, mcharlt
},
511 { "subject", SORT_SUBJECT
, mcharlt
},
512 { "size", SORT_SIZE
, mlonglt
},
513 { "status", SORT_STATUS
, mlonglt
},
514 { "score", SORT_SCORE
, mfloatlt
},
515 { "thread", SORT_THREAD
, NULL
},
518 char **args
= (char **)vp
, *cp
, *_args
[2];
519 int (*func
)(const void *, const void *);
523 int showname
= value("showname") != NULL
;
526 msgvec
[0] = dot
- &message
[0] + 1;
528 if (vp
== NULL
|| vp
== (void *)-1) {
529 _args
[0] = savestr(mb
.mb_sorted
);
532 } else if (args
[0] == NULL
) {
533 printf("Current sorting criterion is: %s\n",
534 mb
.mb_sorted
? mb
.mb_sorted
: "unsorted");
537 for (i
= 0; methnames
[i
].me_name
; i
++)
538 if (*args
[0] && is_prefix(args
[0], methnames
[i
].me_name
))
540 if (methnames
[i
].me_name
== NULL
) {
541 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
544 method
= methnames
[i
].me_method
;
545 func
= methnames
[i
].me_func
;
547 mb
.mb_sorted
= sstrdup(args
[0]);
548 if (method
== SORT_THREAD
)
549 return thread(vp
&& vp
!= (void *)-1 ? msgvec
: vp
);
550 ms
= ac_alloc(sizeof *ms
* msgCount
);
556 if (mb
.mb_type
== MB_IMAP
)
557 imap_getheaders(1, msgCount
);
562 for (n
= 0, i
= 0; i
< msgCount
; i
++) {
564 if ((mp
->m_flag
&MHIDDEN
) == 0) {
567 if (mp
->m_date
== 0 &&
568 (cp
= hfield("date", mp
)) != 0)
569 mp
->m_date
= rfctime(cp
);
570 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
573 if (mp
->m_flag
& MDELETED
)
574 ms
[n
].ms_u
.ms_long
= 1;
575 else if ((mp
->m_flag
&(MNEW
|MREAD
)) == MNEW
)
576 ms
[n
].ms_u
.ms_long
= 90;
577 else if (mp
->m_flag
& MFLAGGED
)
578 ms
[n
].ms_u
.ms_long
= 85;
579 else if ((mp
->m_flag
&(MNEW
|MBOX
)) == MBOX
)
580 ms
[n
].ms_u
.ms_long
= 70;
581 else if (mp
->m_flag
& MNEW
)
582 ms
[n
].ms_u
.ms_long
= 80;
583 else if (mp
->m_flag
& MREAD
)
584 ms
[n
].ms_u
.ms_long
= 40;
586 ms
[n
].ms_u
.ms_long
= 60;
589 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
592 ms
[n
].ms_u
.ms_float
= mp
->m_score
;
596 if ((cp
= hfield(method
== SORT_FROM
?
597 "from" : "to", mp
)) != NULL
) {
598 ms
[n
].ms_u
.ms_char
= showname
?
599 realname(cp
) : skin(cp
);
600 makelow(ms
[n
].ms_u
.ms_char
);
602 ms
[n
].ms_u
.ms_char
= "";
606 if ((cp
= hfield("subject", mp
)) != NULL
) {
609 mime_fromhdr(&in
, &out
, TD_ICONV
);
611 savestr(skipre(out
.s
));
613 makelow(ms
[n
].ms_u
.ms_char
);
615 ms
[n
].ms_u
.ms_char
= "";
620 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
625 qsort(ms
, n
, sizeof *ms
, func
);
626 threadroot
= &message
[ms
[0].ms_n
];
627 for (i
= 1; i
< n
; i
++) {
628 message
[ms
[i
-1].ms_n
].m_younger
= &message
[ms
[i
].ms_n
];
629 message
[ms
[i
].ms_n
].m_elder
= &message
[ms
[i
-1].ms_n
];
632 threadroot
= &message
[0];
633 finalize(threadroot
);
636 return vp
&& vp
!= (void *)-1 && !inhook
&&
637 value("header") ? headers(msgvec
) : 0;
641 skipre(const char *cp
)
643 if (lowerconv(cp
[0]&0377) == 'r' &&
644 lowerconv(cp
[1]&0377) == 'e' &&
646 spacechar(cp
[3]&0377)) {
648 while (spacechar(*cp
&0377))
667 colpt(int *msgvec
, int cl
)
671 if (mb
.mb_threaded
!= 1) {
672 puts("Not in threaded mode.");
675 for (ip
= msgvec
; *ip
!= 0; ip
++)
676 colps(&message
[*ip
-1], cl
);
681 colps(struct message
*b
, int cl
)
686 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
|MREAD
)) == MNEW
))
690 colpm(m
, cl
, &cc
, &uc
);
691 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
692 colpm(m
, cl
, &cc
, &uc
);
695 b
->m_collapsed
= -cc
;
696 for (m
= b
->m_parent
; m
; m
= m
->m_parent
)
697 if (m
->m_collapsed
<= -uc
) {
698 m
->m_collapsed
+= uc
;
702 if (b
->m_collapsed
> 0) {
706 for (m
= b
; m
; m
= m
->m_parent
)
707 if (m
->m_collapsed
<= -uc
) {
708 m
->m_collapsed
+= uc
;
715 colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
718 if (m
->m_collapsed
> 0)
720 if ((m
->m_flag
& (MNEW
|MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
722 if (m
->m_collapsed
> 0)
725 if (m
->m_collapsed
> 0) {
732 colpm(m
, cl
, cc
, uc
);
733 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
734 colpm(m
, cl
, cc
, uc
);
739 uncollapse1(struct message
*m
, int always
)
741 if (mb
.mb_threaded
== 1 && (always
|| m
->m_collapsed
> 0))