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 - 2014 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
40 #ifndef HAVE_AMALGAMATION
44 /* Open addressing is used for Message-IDs because the maximum number of
45 * messages in the table is known in advance (== msgCount) */
47 struct message
*mi_data
;
50 #define NOT_AN_ID ((struct mitem*)-1)
63 /* Return the hash value for a message id modulo mprime, or mprime if the
64 * passed string does not look like a message-id */
65 static ui32_t
_mhash(char const *cp
, ui32_t mprime
);
67 /* Look up a message id. Returns NOT_AN_ID if the passed string does not look
68 * like a message-id */
69 static struct mitem
* _mlook(char *id
, struct mitem
*mt
,
70 struct message
*mdata
, ui32_t mprime
);
72 /* Child is to be adopted by parent. A thread tree is structured as follows:
74 * ------ m_child ------ m_child
75 * | |-------------------->| |------------------------> . . .
76 * | |<--------------------| |<----------------------- . . .
77 * ------ m_parent ------ m_parent
79 * | \____ m_younger | |
88 * | |------------------------> . . .
89 * | |<----------------------- . . .
94 * The base message of a thread does not have a m_parent link. Elements
95 * connected by m_younger/m_elder links are replies to the same message, which
96 * is connected to them by m_parent links. The first reply to a message gets
98 static void _adopt(struct message
*parent
, struct message
*child
,
101 /* Connect all msgs on the lowest thread level with m_younger/m_elder links */
102 static struct message
* _interlink(struct message
*m
, ui32_t cnt
, int nmail
);
104 static void _finalize(struct message
*mp
);
106 /* Several sort comparison PTFs */
108 static int _mui32lt(void const *a
, void const *b
);
110 static int _mlonglt(void const *a
, void const *b
);
111 static int _mcharlt(void const *a
, void const *b
);
113 static void _lookup(struct message
*m
, struct mitem
*mi
,
115 static void _makethreads(struct message
*m
, ui32_t cnt
, int nmail
);
116 static int _colpt(int *msgvec
, int cl
);
117 static void _colps(struct message
*b
, int cl
);
118 static void _colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
121 _mhash(char const *cp
, ui32_t mprime
)
123 ui32_t h
= 0, g
, at
= 0;
126 for (--cp
; *++cp
!= '\0';) {
127 /* Pay attention not to hash characters which are irrelevant for
128 * Message-ID semantics */
130 cp
= skip_comment(cp
+ 1) - 1;
133 if (*cp
== '"' || *cp
== '\\')
137 /* TODO torek hash */
138 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
);
139 if ((g
= h
& 0xf0000000) != 0) {
145 return (at
? h
% mprime
: mprime
);
148 static struct mitem
*
149 _mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, ui32_t mprime
)
151 struct mitem
*mp
= NULL
;
155 if (id
== NULL
&& (id
= hfield1("message-id", mdata
)) == NULL
)
158 if (mdata
!= NULL
&& mdata
->m_idhash
)
159 h
= ~mdata
->m_idhash
;
161 h
= _mhash(id
, mprime
);
169 while (mp
->mi_id
!= NULL
) {
170 if (!msgidcmp(mp
->mi_id
, id
))
172 c
+= (n
& 1) ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
179 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
180 mp
->mi_id
= sstrdup(id
);
182 mdata
->m_idhash
= ~h
;
184 if (mp
->mi_id
== NULL
)
192 _adopt(struct message
*parent
, struct message
*child
, int dist
)
194 struct message
*mp
, *mq
;
197 for (mp
= parent
; mp
!= NULL
; mp
= mp
->m_parent
)
201 child
->m_level
= dist
; /* temporarily store distance */
202 child
->m_parent
= parent
;
204 if (parent
->m_child
!= NULL
) {
206 for (mp
= parent
->m_child
; mp
!= NULL
; mp
= mp
->m_younger
) {
207 if (mp
->m_date
>= child
->m_date
) {
208 if (mp
->m_elder
!= NULL
)
209 mp
->m_elder
->m_younger
= child
;
210 child
->m_elder
= mp
->m_elder
;
212 child
->m_younger
= mp
;
213 if (mp
== parent
->m_child
)
214 parent
->m_child
= child
;
219 mq
->m_younger
= child
;
222 parent
->m_child
= child
;
227 static struct message
*
228 _interlink(struct message
*m
, ui32_t cnt
, int nmail
)
230 struct message
*root
;
236 autocollapse
= (!nmail
&& !(inhook
& 2) && ok_blook(autocollapse
));
237 ms
= smalloc(sizeof *ms
* cnt
);
239 for (n
= 0, i
= 0; UICMP(32, i
, <, cnt
); ++i
) {
240 if (m
[i
].m_parent
== NULL
) {
243 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
250 qsort(ms
, n
, sizeof *ms
, &_mlonglt
);
251 root
= m
+ ms
[0].ms_n
;
252 for (i
= 1; UICMP(32, i
, <, n
); ++i
) {
253 m
[ms
[i
-1].ms_n
].m_younger
= m
+ ms
[i
].ms_n
;
254 m
[ms
[i
].ms_n
].m_elder
= m
+ ms
[i
- 1].ms_n
;
265 _finalize(struct message
*mp
)
270 for (n
= 0; mp
; mp
= next_in_thread(mp
)) {
271 mp
->m_threadpos
= ++n
;
272 mp
->m_level
= mp
->m_parent
? mp
->m_level
+ mp
->m_parent
->m_level
: 0;
279 _mui32lt(void const *a
, void const *b
)
281 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
;
294 _mlonglt(void const *a
, void const *b
)
296 struct msort
const *xa
= a
, *xb
= b
;
300 i
= (int)(xa
->ms_u
.ms_long
- xb
->ms_u
.ms_long
);
302 i
= xa
->ms_n
- xb
->ms_n
;
308 _mcharlt(void const *a
, void const *b
)
310 struct msort
const *xa
= a
, *xb
= b
;
314 i
= strcoll(xa
->ms_u
.ms_char
, xb
->ms_u
.ms_char
);
316 i
= xa
->ms_n
- xb
->ms_n
;
322 _lookup(struct message
*m
, struct mitem
*mi
, ui32_t mprime
)
330 if (m
->m_flag
& MHIDDEN
)
334 if ((cp
= hfield1("in-reply-to", m
)) != NULL
) {
335 if ((np
= extract(cp
, GREF
)) != NULL
)
337 if ((ip
= _mlook(np
->n_name
, mi
, NULL
, mprime
)) != NULL
&&
339 _adopt(ip
->mi_data
, m
, 1);
342 } while ((np
= np
->n_flink
) != NULL
);
345 if ((cp
= hfield1("references", m
)) != NULL
) {
346 if ((np
= extract(cp
, GREF
)) != NULL
) {
347 while (np
->n_flink
!= NULL
)
350 if ((ip
= _mlook(np
->n_name
, mi
, NULL
, mprime
)) != NULL
) {
352 continue; /* skip dist++ */
353 _adopt(ip
->mi_data
, m
, dist
);
357 } while ((np
= np
->n_blink
) != NULL
);
365 _makethreads(struct message
*m
, ui32_t cnt
, int nmail
)
375 mprime
= nextprime(cnt
);
376 mt
= scalloc(mprime
, sizeof *mt
);
380 for (i
= 0; i
< cnt
; ++i
) {
381 if (!(m
[i
].m_flag
& MHIDDEN
)) {
382 _mlook(NULL
, mt
, m
+ i
, mprime
);
383 if (m
[i
].m_date
== 0) {
384 if ((cp
= hfield1("date", m
+ i
)) != NULL
)
385 m
[i
].m_date
= rfctime(cp
);
388 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
= m
[i
].m_parent
= NULL
;
390 if (!nmail
&& !(inhook
& 2))
391 m
[i
].m_collapsed
= 0;
395 /* Most folders contain the eldest messages first. Traversing them in
396 * descending order makes it more likely that younger brothers are found
397 * first, so elder ones can be prepended to the brother list, which is
398 * faster. The worst case is still in O(n^2) and occurs when all but one
399 * messages in a folder are replies to the one message, and are sorted such
400 * that youngest messages occur first */
401 for (i
= cnt
; i
> 0; --i
) {
402 _lookup(m
+ i
- 1, mt
, mprime
);
408 threadroot
= _interlink(m
, cnt
, nmail
);
409 _finalize(threadroot
);
411 for (i
= 0; i
< mprime
; ++i
)
412 if (mt
[i
].mi_id
!= NULL
)
422 _colpt(int *msgvec
, int cl
)
427 if (mb
.mb_threaded
!= 1) {
428 puts("Not in threaded mode.");
431 for (ip
= msgvec
; *ip
!= 0; ++ip
)
432 _colps(message
+ *ip
- 1, cl
);
440 _colps(struct message
*b
, int cl
)
446 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
| MREAD
)) == MNEW
))
449 if (b
->m_child
!= NULL
) {
451 _colpm(m
, cl
, &cc
, &uc
);
452 for (m
= m
->m_younger
; m
!= NULL
; m
= m
->m_younger
)
453 _colpm(m
, cl
, &cc
, &uc
);
457 b
->m_collapsed
= -cc
;
458 for (m
= b
->m_parent
; m
!= NULL
; m
= m
->m_parent
)
459 if (m
->m_collapsed
<= -uc
) {
460 m
->m_collapsed
+= uc
;
464 if (b
->m_collapsed
> 0) {
468 for (m
= b
; m
!= NULL
; m
= m
->m_parent
)
469 if (m
->m_collapsed
<= -uc
) {
470 m
->m_collapsed
+= uc
;
479 _colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
483 if (m
->m_collapsed
> 0)
485 if ((m
->m_flag
& (MNEW
| MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
487 if (m
->m_collapsed
> 0)
490 if (m
->m_collapsed
> 0) {
496 if (m
->m_child
!= NULL
) {
498 _colpm(m
, cl
, cc
, uc
);
499 for (m
= m
->m_younger
; m
!= NULL
; m
= m
->m_younger
)
500 _colpm(m
, cl
, cc
, uc
);
511 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void*)-1) {
513 if (mb
.mb_type
== MB_IMAP
)
514 imap_getheaders(1, msgCount
);
516 _makethreads(message
, msgCount
, (vp
== (void*)-1));
517 if (mb
.mb_sorted
!= NULL
)
519 mb
.mb_sorted
= sstrdup("thread");
522 if (vp
!= NULL
&& vp
!= (void*)-1 && !inhook
&& ok_blook(header
))
541 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
544 if (vp
&& !inhook
&& ok_blook(header
))
553 next_in_thread(struct message
*mp
)
558 if ((rv
= mp
->m_child
) != NULL
)
560 if ((rv
= mp
->m_younger
) != NULL
)
563 while ((rv
= mp
->m_parent
) != NULL
) {
565 if ((rv
= rv
->m_younger
) != NULL
)
574 prev_in_thread(struct message
*mp
)
579 if ((rv
= mp
->m_elder
) != NULL
) {
580 for (mp
= rv
; (rv
= mp
->m_child
) != NULL
;) {
582 while ((rv
= mp
->m_younger
) != NULL
)
595 this_in_thread(struct message
*mp
, long n
)
600 if (n
== -1) { /* find end of thread */
602 if ((rv
= mp
->m_younger
) != NULL
) {
606 rv
= next_in_thread(mp
);
607 if (rv
== NULL
|| rv
->m_threadpos
< mp
->m_threadpos
) {
617 while (mp
!= NULL
&& mp
->m_threadpos
< n
) {
618 if ((rv
= mp
->m_younger
) != NULL
&& rv
->m_threadpos
<= n
) {
622 mp
= next_in_thread(mp
);
624 rv
= (mp
!= NULL
&& mp
->m_threadpos
== n
) ? mp
: NULL
;
633 enum method
{SORT_SUBJECT
, SORT_DATE
, SORT_STATUS
, SORT_SIZE
, SORT_FROM
,
634 SORT_TO
, SORT_SPAM
, SORT_THREAD
} method
;
637 enum method me_method
;
638 int (*me_func
)(void const *, void const *);
639 } const methnames
[] = {
640 {"date", SORT_DATE
, &_mlonglt
},
641 {"from", SORT_FROM
, &_mcharlt
},
642 {"to", SORT_TO
, &_mcharlt
},
643 {"subject", SORT_SUBJECT
, &_mcharlt
},
644 {"size", SORT_SIZE
, &_mlonglt
},
646 {"spam", SORT_SPAM
, &_mui32lt
},
648 {"status", SORT_STATUS
, &_mlonglt
},
649 {"thread", SORT_THREAD
, NULL
}
653 char **args
= (char**)vp
, *cp
, *_args
[2];
654 int (*func
)(void const *, void const *);
661 showname
= ok_blook(showname
);
662 msgvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
665 if (vp
== NULL
|| vp
== (void*)-1) {
666 _args
[0] = savestr(mb
.mb_sorted
);
669 } else if (args
[0] == NULL
) {
670 printf("Current sorting criterion is: %s\n",
671 (mb
.mb_sorted
? mb
.mb_sorted
: "unsorted"));
676 for (i
= 0; UICMP(z
, i
, <, NELEM(methnames
)); ++i
)
677 if (*args
[0] != '\0' && is_prefix(args
[0], methnames
[i
].me_name
))
679 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
684 method
= methnames
[i
].me_method
;
685 func
= methnames
[i
].me_func
;
687 mb
.mb_sorted
= sstrdup(args
[0]);
689 if (method
== SORT_THREAD
) {
690 i
= c_thread((vp
!= NULL
&& vp
!= (void*)-1) ? msgvec
: vp
);
694 ms
= ac_alloc(sizeof *ms
* msgCount
);
701 if (mb
.mb_type
== MB_IMAP
)
702 imap_getheaders(1, msgCount
);
710 for (n
= 0, i
= 0; i
< msgCount
; ++i
) {
712 if (!(mp
->m_flag
& MHIDDEN
)) {
715 if (mp
->m_date
== 0 && (cp
= hfield1("date", mp
)) != NULL
)
716 mp
->m_date
= rfctime(cp
);
717 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
720 if (mp
->m_flag
& MDELETED
)
721 ms
[n
].ms_u
.ms_long
= 1;
722 else if ((mp
->m_flag
& (MNEW
| MREAD
)) == MNEW
)
723 ms
[n
].ms_u
.ms_long
= 90;
724 else if (mp
->m_flag
& MFLAGGED
)
725 ms
[n
].ms_u
.ms_long
= 85;
726 else if ((mp
->m_flag
& (MNEW
| MBOX
)) == MBOX
)
727 ms
[n
].ms_u
.ms_long
= 70;
728 else if (mp
->m_flag
& MNEW
)
729 ms
[n
].ms_u
.ms_long
= 80;
730 else if (mp
->m_flag
& MREAD
)
731 ms
[n
].ms_u
.ms_long
= 40;
733 ms
[n
].ms_u
.ms_long
= 60;
736 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
740 ms
[n
].ms_u
.ms_ui
= mp
->m_spamscore
;
745 if ((cp
= hfield1((method
== SORT_FROM
? "from" : "to"), mp
)) !=
747 ms
[n
].ms_u
.ms_char
= sstrdup(showname
? realname(cp
) : skin(cp
));
748 makelow(ms
[n
].ms_u
.ms_char
);
750 ms
[n
].ms_u
.ms_char
= sstrdup("");
754 if ((cp
= hfield1("subject", mp
)) != NULL
) {
757 mime_fromhdr(&in
, &out
, TD_ICONV
);
758 ms
[n
].ms_u
.ms_char
= sstrdup(subject_re_trim(out
.s
));
760 makelow(ms
[n
].ms_u
.ms_char
);
762 ms
[n
].ms_u
.ms_char
= sstrdup("");
767 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
775 qsort(ms
, n
, sizeof *ms
, func
);
776 threadroot
= message
+ ms
[0].ms_n
;
777 for (i
= 1; i
< n
; ++i
) {
778 message
[ms
[i
- 1].ms_n
].m_younger
= message
+ ms
[i
].ms_n
;
779 message
[ms
[i
].ms_n
].m_elder
= message
+ ms
[i
- 1].ms_n
;
782 threadroot
= message
;
783 _finalize(threadroot
);
790 for (i
= 0; i
< n
; ++i
)
791 free(ms
[i
].ms_u
.ms_char
);
797 i
= ((vp
!= NULL
&& vp
!= (void*)-1 && !inhook
&& ok_blook(header
))
798 ? c_headers(msgvec
) : 0);
816 c_uncollapse(void *v
)
827 uncollapse1(struct message
*mp
, int always
)
830 if (mb
.mb_threaded
== 1 && (always
|| mp
->m_collapsed
> 0))