1 /*@ 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 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
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 * Open addressing is used for Message-IDs because the maximum number of
48 * messages in the table is known in advance (== msgCount).
51 struct message
*mi_data
;
66 static unsigned mhash(const char *cp
, int mprime
);
67 static struct mitem
*mlook(char *id
, struct mitem
*mt
, struct message
*mdata
,
69 static void adopt(struct message
*parent
, struct message
*child
, int dist
);
70 static struct message
*interlink(struct message
*m
, long cnt
, int nmail
);
71 static void finalize(struct message
*mp
);
73 static int muilt(void const *a
, void const *b
);
75 static int mlonglt(const void *a
, const void *b
);
76 static int mcharlt(const void *a
, const void *b
);
77 static void lookup(struct message
*m
, struct mitem
*mi
, int mprime
);
78 static void makethreads(struct message
*m
, long cnt
, int nmail
);
79 static char const *skipre(char const *cp
);
80 static int colpt(int *msgvec
, int cl
);
81 static void colps(struct message
*b
, int cl
);
82 static void colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
85 * Return the hash value for a message id modulo mprime, or mprime
86 * if the passed string does not look like a message-id.
89 mhash(const char *cp
, int mprime
)
92 unsigned h
= 0, g
, at
= 0;
97 * Pay attention not to hash characters which are
98 * irrelevant for Message-ID semantics.
101 cp
= skip_comment(&cp
[1]) - 1;
104 if (*cp
== '"' || *cp
== '\\')
108 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
& 0377);
109 if ((g
= h
& 0xf0000000) != 0) {
114 return at
? h
% (unsigned int)mprime
: (unsigned int)mprime
;
117 #define NOT_AN_ID ((struct mitem *)-1)
120 * Look up a message id. Returns NOT_AN_ID if the passed string does
121 * not look like a message-id.
123 static struct mitem
*
124 mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, int mprime
)
127 unsigned h
, c
, n
= 0;
129 if (id
== NULL
&& (id
= hfield1("message-id", mdata
)) == NULL
)
131 if (mdata
&& mdata
->m_idhash
)
132 h
= ~mdata
->m_idhash
;
134 h
= mhash(id
, mprime
);
135 if (h
== (unsigned int)mprime
)
139 while (mp
->mi_id
!= NULL
) {
140 if (msgidcmp(mp
->mi_id
, id
) == 0)
142 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
144 while (c
>= (unsigned int)mprime
)
148 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
151 mdata
->m_idhash
= ~h
;
153 return mp
->mi_id
? mp
: NULL
;
157 * Child is to be adopted by parent. A thread tree is structured
160 * ------ m_child ------ m_child
161 * | |-------------------->| |------------------------> . . .
162 * | |<--------------------| |<----------------------- . . .
163 * ------ m_parent ------ m_parent
165 * | \____ m_younger | |
169 * | m_parent ---- | |
174 * | | |------------------------> . . .
175 * | | |<----------------------- . . .
178 * \----- m_younger | |
187 * | |------------------------> . . .
188 * | |<----------------------- . . .
193 * The base message of a thread does not have a m_parent link. Elements
194 * connected by m_younger/m_elder links are replies to the same message,
195 * which is connected to them by m_parent links. The first reply to a
196 * message gets the m_child link.
199 adopt(struct message
*parent
, struct message
*child
, int dist
)
201 struct message
*mp
, *mq
;
203 for (mp
= parent
; mp
; mp
= mp
->m_parent
)
206 child
->m_level
= dist
; /* temporarily store distance */
207 child
->m_parent
= parent
;
208 if (parent
->m_child
!= NULL
) {
210 for (mp
= parent
->m_child
; mp
; mp
= mp
->m_younger
) {
211 if (mp
->m_date
>= child
->m_date
) {
213 mp
->m_elder
->m_younger
= child
;
214 child
->m_elder
= mp
->m_elder
;
216 child
->m_younger
= mp
;
217 if (mp
== parent
->m_child
)
218 parent
->m_child
= child
;
223 mq
->m_younger
= child
;
226 parent
->m_child
= child
;
230 * Connect all messages on the lowest thread level with m_younger/m_elder
233 static struct message
*
234 interlink(struct message
*m
, long cnt
, int nmail
)
239 struct message
*root
;
240 int autocollapse
= !nmail
&& !(inhook
&2) &&
241 value("autocollapse") != NULL
;
243 ms
= smalloc(sizeof *ms
* cnt
);
244 for (n
= 0, i
= 0; i
< cnt
; i
++) {
245 if (m
[i
].m_parent
== NULL
) {
248 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
254 qsort(ms
, n
, sizeof *ms
, mlonglt
);
255 root
= &m
[ms
[0].ms_n
];
256 for (i
= 1; i
< n
; i
++) {
257 m
[ms
[i
-1].ms_n
].m_younger
= &m
[ms
[i
].ms_n
];
258 m
[ms
[i
].ms_n
].m_elder
= &m
[ms
[i
-1].ms_n
];
267 finalize(struct message
*mp
)
271 for (n
= 0; mp
; mp
= next_in_thread(mp
)) {
272 mp
->m_threadpos
= ++n
;
273 mp
->m_level
= mp
->m_parent
?
274 mp
->m_level
+ mp
->m_parent
->m_level
: 0;
280 muilt(void const *a
, void const *b
)
282 struct msort
const *xa
= a
, *xb
= b
;
285 i
= (int)(xa
->ms_u
.ms_ui
- xb
->ms_u
.ms_ui
);
287 i
= xa
->ms_n
- xb
->ms_n
;
293 mlonglt(const void *a
, const void *b
)
295 struct msort
const *xa
= a
, *xb
= b
;
298 i
= (int)(xa
->ms_u
.ms_long
- xb
->ms_u
.ms_long
);
300 i
= xa
->ms_n
- xb
->ms_n
;
305 mcharlt(const void *a
, const void *b
)
307 struct msort
const *xa
= a
, *xb
= b
;
310 i
= strcoll(xa
->ms_u
.ms_char
, xb
->ms_u
.ms_char
);
312 i
= xa
->ms_n
- xb
->ms_n
;
317 lookup(struct message
*m
, struct mitem
*mi
, int mprime
)
324 if (m
->m_flag
& MHIDDEN
)
327 if ((cp
= hfield1("in-reply-to", m
)) != NULL
) {
328 if ((np
= extract(cp
, GREF
)) != NULL
)
330 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
331 != NULL
&& ip
!= NOT_AN_ID
) {
332 adopt(ip
->mi_data
, m
, 1);
335 } while ((np
= np
->n_flink
) != NULL
);
337 if ((cp
= hfield1("references", m
)) != NULL
) {
338 if ((np
= extract(cp
, GREF
)) != NULL
) {
339 while (np
->n_flink
!= NULL
)
342 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
345 continue; /* skip dist++ */
346 adopt(ip
->mi_data
, m
, dist
);
350 } while ((np
= np
->n_blink
) != NULL
);
356 makethreads(struct message
*m
, long cnt
, int nmail
)
364 mprime
= nextprime(cnt
);
365 mt
= scalloc(mprime
, sizeof *mt
);
366 for (i
= 0; i
< cnt
; i
++) {
367 if ((m
[i
].m_flag
&MHIDDEN
) == 0) {
368 mlook(NULL
, mt
, &m
[i
], mprime
);
369 if (m
[i
].m_date
== 0) {
370 if ((cp
= hfield1("date", &m
[i
])) != NULL
)
371 m
[i
].m_date
= rfctime(cp
);
374 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
=
375 m
[i
].m_parent
= NULL
;
377 if (!nmail
&& !(inhook
&2))
378 m
[i
].m_collapsed
= 0;
381 * Most folders contain the eldest messages first. Traversing
382 * them in descending order makes it more likely that younger
383 * brothers are found first, so elder ones can be prepended to
384 * the brother list, which is faster. The worst case is still
385 * in O(n^2) and occurs when all but one messages in a folder
386 * are replies to the one message, and are sorted such that
387 * youngest messages occur first.
389 for (i
= cnt
-1; i
>= 0; i
--)
390 lookup(&m
[i
], mt
, mprime
);
391 threadroot
= interlink(m
, cnt
, nmail
);
392 finalize(threadroot
);
400 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void *)-1) {
402 if (mb
.mb_type
== MB_IMAP
)
403 imap_getheaders(1, msgCount
);
405 makethreads(message
, msgCount
, vp
== (void *)-1);
406 if (mb
.mb_sorted
!= NULL
)
408 mb
.mb_sorted
= sstrdup("thread");
410 if (vp
&& vp
!= (void *)-1 && !inhook
&& value("header"))
423 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
425 if (vp
&& !inhook
&& value("header"))
431 next_in_thread(struct message
*mp
)
436 return mp
->m_younger
;
437 while (mp
->m_parent
) {
438 if (mp
->m_parent
->m_younger
)
439 return mp
->m_parent
->m_younger
;
446 prev_in_thread(struct message
*mp
)
450 while (mp
->m_child
) {
452 while (mp
->m_younger
)
461 this_in_thread(struct message
*mp
, long n
)
465 if (n
== -1) { /* find end of thread */
471 mq
= next_in_thread(mp
);
472 if (mq
== NULL
|| mq
->m_threadpos
< mp
->m_threadpos
)
478 while (mp
&& mp
->m_threadpos
< n
) {
479 if (mp
->m_younger
&& mp
->m_younger
->m_threadpos
<= n
) {
483 mp
= next_in_thread(mp
);
485 return mp
&& mp
->m_threadpos
== n
? mp
: NULL
;
489 * Sorted mode is internally just a variant of threaded mode with all
490 * m_parent and m_child links being NULL.
509 enum method me_method
;
510 int (*me_func
)(const void *, const void *);
512 { "date", SORT_DATE
, mlonglt
},
513 { "from", SORT_FROM
, mcharlt
},
514 { "to", SORT_TO
, mcharlt
},
515 { "subject", SORT_SUBJECT
, mcharlt
},
516 { "size", SORT_SIZE
, mlonglt
},
518 { "spam", SORT_SPAM
, muilt
},
520 { "status", SORT_STATUS
, mlonglt
},
521 { "thread", SORT_THREAD
, NULL
},
524 char **args
= (char **)vp
, *cp
, *_args
[2];
525 int (*func
)(const void *, const void *);
529 int showname
= value("showname") != NULL
;
532 msgvec
[0] = dot
- &message
[0] + 1;
534 if (vp
== NULL
|| vp
== (void *)-1) {
535 _args
[0] = savestr(mb
.mb_sorted
);
538 } else if (args
[0] == NULL
) {
539 printf("Current sorting criterion is: %s\n",
540 mb
.mb_sorted
? mb
.mb_sorted
: "unsorted");
543 for (i
= 0; methnames
[i
].me_name
; i
++)
544 if (*args
[0] && is_prefix(args
[0], methnames
[i
].me_name
))
546 if (methnames
[i
].me_name
== NULL
) {
547 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
550 method
= methnames
[i
].me_method
;
551 func
= methnames
[i
].me_func
;
553 mb
.mb_sorted
= sstrdup(args
[0]);
554 if (method
== SORT_THREAD
)
555 return thread(vp
&& vp
!= (void *)-1 ? msgvec
: vp
);
556 ms
= ac_alloc(sizeof *ms
* msgCount
);
563 if (mb
.mb_type
== MB_IMAP
)
564 imap_getheaders(1, msgCount
);
570 for (n
= 0, i
= 0; i
< msgCount
; i
++) {
572 if ((mp
->m_flag
&MHIDDEN
) == 0) {
575 if (mp
->m_date
== 0 &&
576 (cp
= hfield1("date", mp
)) != 0)
577 mp
->m_date
= rfctime(cp
);
578 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
581 if (mp
->m_flag
& MDELETED
)
582 ms
[n
].ms_u
.ms_long
= 1;
583 else if ((mp
->m_flag
&(MNEW
|MREAD
)) == MNEW
)
584 ms
[n
].ms_u
.ms_long
= 90;
585 else if (mp
->m_flag
& MFLAGGED
)
586 ms
[n
].ms_u
.ms_long
= 85;
587 else if ((mp
->m_flag
&(MNEW
|MBOX
)) == MBOX
)
588 ms
[n
].ms_u
.ms_long
= 70;
589 else if (mp
->m_flag
& MNEW
)
590 ms
[n
].ms_u
.ms_long
= 80;
591 else if (mp
->m_flag
& MREAD
)
592 ms
[n
].ms_u
.ms_long
= 40;
594 ms
[n
].ms_u
.ms_long
= 60;
597 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
601 ms
[n
].ms_u
.ms_ui
= mp
->m_spamscore
;
606 if ((cp
= hfield1(method
== SORT_FROM
?
607 "from" : "to", mp
)) != NULL
) {
608 ms
[n
].ms_u
.ms_char
= showname
?
609 realname(cp
) : skin(cp
);
610 makelow(ms
[n
].ms_u
.ms_char
);
612 ms
[n
].ms_u
.ms_char
= UNCONST("");
616 if ((cp
= hfield1("subject", mp
)) != NULL
) {
619 mime_fromhdr(&in
, &out
, TD_ICONV
);
621 savestr(skipre(out
.s
));
623 makelow(ms
[n
].ms_u
.ms_char
);
625 ms
[n
].ms_u
.ms_char
= UNCONST("");
630 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
635 qsort(ms
, n
, sizeof *ms
, func
);
636 threadroot
= &message
[ms
[0].ms_n
];
637 for (i
= 1; i
< n
; i
++) {
638 message
[ms
[i
-1].ms_n
].m_younger
= &message
[ms
[i
].ms_n
];
639 message
[ms
[i
].ms_n
].m_elder
= &message
[ms
[i
-1].ms_n
];
642 threadroot
= &message
[0];
643 finalize(threadroot
);
646 return vp
&& vp
!= (void *)-1 && !inhook
&&
647 value("header") ? headers(msgvec
) : 0;
651 skipre(char const *cp
)
653 if (lowerconv(cp
[0]) == 'r' && lowerconv(cp
[1]) == 'e' &&
654 cp
[2] == ':' && spacechar(cp
[3])) {
656 while (spacechar(*cp
))
675 colpt(int *msgvec
, int cl
)
679 if (mb
.mb_threaded
!= 1) {
680 puts("Not in threaded mode.");
683 for (ip
= msgvec
; *ip
!= 0; ip
++)
684 colps(&message
[*ip
-1], cl
);
689 colps(struct message
*b
, int cl
)
694 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
|MREAD
)) == MNEW
))
698 colpm(m
, cl
, &cc
, &uc
);
699 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
700 colpm(m
, cl
, &cc
, &uc
);
703 b
->m_collapsed
= -cc
;
704 for (m
= b
->m_parent
; m
; m
= m
->m_parent
)
705 if (m
->m_collapsed
<= -uc
) {
706 m
->m_collapsed
+= uc
;
710 if (b
->m_collapsed
> 0) {
714 for (m
= b
; m
; m
= m
->m_parent
)
715 if (m
->m_collapsed
<= -uc
) {
716 m
->m_collapsed
+= uc
;
723 colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
726 if (m
->m_collapsed
> 0)
728 if ((m
->m_flag
& (MNEW
|MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
730 if (m
->m_collapsed
> 0)
733 if (m
->m_collapsed
> 0) {
740 colpm(m
, cl
, cc
, uc
);
741 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
742 colpm(m
, cl
, cc
, uc
);
747 uncollapse1(struct message
*m
, int always
)
749 if (mb
.mb_threaded
== 1 && (always
|| m
->m_collapsed
> 0))