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(const void *a
, const void *b
);
111 static int _mcharlt(const void *a
, const void *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 char const * _skipre(char const *cp
);
117 static int _colpt(int *msgvec
, int cl
);
118 static void _colps(struct message
*b
, int cl
);
119 static void _colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
122 _mhash(char const *cp
, ui32_t mprime
)
124 ui32_t h
= 0, g
, at
= 0;
128 /* Pay attention not to hash characters which are irrelevant for
129 * Message-ID semantics */
131 cp
= skip_comment(&cp
[1]) - 1;
134 if (*cp
== '"' || *cp
== '\\')
138 /* TODO torek hash */
139 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
);
140 if ((g
= h
& 0xf0000000) != 0) {
146 return (at
? h
% mprime
: mprime
);
149 static struct mitem
*
150 _mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, ui32_t mprime
)
152 struct mitem
*mp
= NULL
;
156 if (id
== NULL
&& (id
= hfield1("message-id", mdata
)) == NULL
)
159 if (mdata
&& mdata
->m_idhash
)
160 h
= ~mdata
->m_idhash
;
162 h
= _mhash(id
, mprime
);
170 while (mp
->mi_id
!= NULL
) {
171 if (!msgidcmp(mp
->mi_id
, id
))
173 c
+= (n
& 1) ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
180 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
181 mp
->mi_id
= sstrdup(id
);
183 mdata
->m_idhash
= ~h
;
185 if (mp
->mi_id
== NULL
)
193 _adopt(struct message
*parent
, struct message
*child
, int dist
)
195 struct message
*mp
, *mq
;
198 for (mp
= parent
; mp
; mp
= mp
->m_parent
)
202 child
->m_level
= dist
; /* temporarily store distance */
203 child
->m_parent
= parent
;
205 if (parent
->m_child
!= NULL
) {
207 for (mp
= parent
->m_child
; mp
!= NULL
; mp
= mp
->m_younger
) {
208 if (mp
->m_date
>= child
->m_date
) {
209 if (mp
->m_elder
!= NULL
)
210 mp
->m_elder
->m_younger
= child
;
211 child
->m_elder
= mp
->m_elder
;
213 child
->m_younger
= mp
;
214 if (mp
== parent
->m_child
)
215 parent
->m_child
= child
;
220 mq
->m_younger
= child
;
223 parent
->m_child
= child
;
228 static struct message
*
229 _interlink(struct message
*m
, ui32_t cnt
, int nmail
)
231 struct message
*root
;
237 autocollapse
= (!nmail
&& !(inhook
& 2) && ok_blook(autocollapse
));
238 ms
= smalloc(sizeof *ms
* cnt
);
240 for (n
= 0, i
= 0; UICMP(32, i
, <, cnt
); ++i
) {
241 if (m
[i
].m_parent
== NULL
) {
244 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
251 qsort(ms
, n
, sizeof *ms
, &_mlonglt
);
252 root
= &m
[ms
[0].ms_n
];
253 for (i
= 1; UICMP(32, i
, <, n
); ++i
) {
254 m
[ms
[i
-1].ms_n
].m_younger
= &m
[ms
[i
].ms_n
];
255 m
[ms
[i
].ms_n
].m_elder
= &m
[ms
[i
-1].ms_n
];
266 _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
? mp
->m_level
+ mp
->m_parent
->m_level
: 0;
280 _mui32lt(void const *a
, void const *b
)
282 struct msort
const *xa
= a
, *xb
= b
;
286 i
= (int)(xa
->ms_u
.ms_ui
- xb
->ms_u
.ms_ui
);
288 i
= xa
->ms_n
- xb
->ms_n
;
295 _mlonglt(const void *a
, const void *b
)
297 struct msort
const *xa
= a
, *xb
= b
;
301 i
= (int)(xa
->ms_u
.ms_long
- xb
->ms_u
.ms_long
);
303 i
= xa
->ms_n
- xb
->ms_n
;
309 _mcharlt(const void *a
, const void *b
)
311 struct msort
const *xa
= a
, *xb
= b
;
315 i
= strcoll(xa
->ms_u
.ms_char
, xb
->ms_u
.ms_char
);
317 i
= xa
->ms_n
- xb
->ms_n
;
323 _lookup(struct message
*m
, struct mitem
*mi
, ui32_t mprime
)
331 if (m
->m_flag
& MHIDDEN
)
335 if ((cp
= hfield1("in-reply-to", m
)) != NULL
) {
336 if ((np
= extract(cp
, GREF
)) != NULL
)
338 if ((ip
= _mlook(np
->n_name
, mi
, NULL
, mprime
)) != NULL
&&
340 _adopt(ip
->mi_data
, m
, 1);
343 } while ((np
= np
->n_flink
) != NULL
);
346 if ((cp
= hfield1("references", m
)) != NULL
) {
347 if ((np
= extract(cp
, GREF
)) != NULL
) {
348 while (np
->n_flink
!= NULL
)
351 if ((ip
= _mlook(np
->n_name
, mi
, NULL
, mprime
)) != NULL
) {
353 continue; /* skip dist++ */
354 _adopt(ip
->mi_data
, m
, dist
);
358 } while ((np
= np
->n_blink
) != NULL
);
366 _makethreads(struct message
*m
, ui32_t cnt
, int nmail
)
376 mprime
= nextprime(cnt
);
377 mt
= scalloc(mprime
, sizeof *mt
);
381 for (i
= 0; i
< cnt
; ++i
) {
382 if (!(m
[i
].m_flag
& MHIDDEN
)) {
383 _mlook(NULL
, mt
, &m
[i
], mprime
);
384 if (m
[i
].m_date
== 0) {
385 if ((cp
= hfield1("date", &m
[i
])) != NULL
)
386 m
[i
].m_date
= rfctime(cp
);
389 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
= m
[i
].m_parent
= NULL
;
391 if (!nmail
&& !(inhook
& 2))
392 m
[i
].m_collapsed
= 0;
396 /* Most folders contain the eldest messages first. Traversing them in
397 * descending order makes it more likely that younger brothers are found
398 * first, so elder ones can be prepended to the brother list, which is
399 * faster. The worst case is still in O(n^2) and occurs when all but one
400 * messages in a folder are replies to the one message, and are sorted such
401 * that youngest messages occur first */
402 for (i
= cnt
; i
> 0; --i
) {
403 _lookup(&m
[i
- 1], mt
, mprime
);
409 threadroot
= _interlink(m
, cnt
, nmail
);
410 _finalize(threadroot
);
412 for (i
= 0; i
< mprime
; ++i
)
413 if (mt
[i
].mi_id
!= NULL
)
423 _skipre(char const *cp
)
426 if (lowerconv(cp
[0]) == 'r' && lowerconv(cp
[1]) == 'e' && cp
[2] == ':' &&
429 while (spacechar(*cp
))
437 _colpt(int *msgvec
, int cl
)
442 if (mb
.mb_threaded
!= 1) {
443 puts("Not in threaded mode.");
446 for (ip
= msgvec
; *ip
!= 0; ++ip
)
447 _colps(&message
[*ip
- 1], cl
);
455 _colps(struct message
*b
, int cl
)
461 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
| MREAD
)) == MNEW
))
464 if (b
->m_child
!= NULL
) {
466 _colpm(m
, cl
, &cc
, &uc
);
467 for (m
= m
->m_younger
; m
!= NULL
; m
= m
->m_younger
)
468 _colpm(m
, cl
, &cc
, &uc
);
472 b
->m_collapsed
= -cc
;
473 for (m
= b
->m_parent
; m
!= NULL
; m
= m
->m_parent
)
474 if (m
->m_collapsed
<= -uc
) {
475 m
->m_collapsed
+= uc
;
479 if (b
->m_collapsed
> 0) {
483 for (m
= b
; m
!= NULL
; m
= m
->m_parent
)
484 if (m
->m_collapsed
<= -uc
) {
485 m
->m_collapsed
+= uc
;
494 _colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
498 if (m
->m_collapsed
> 0)
500 if ((m
->m_flag
& (MNEW
| MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
502 if (m
->m_collapsed
> 0)
505 if (m
->m_collapsed
> 0) {
511 if (m
->m_child
!= NULL
) {
513 _colpm(m
, cl
, cc
, uc
);
514 for (m
= m
->m_younger
; m
!= NULL
; m
= m
->m_younger
)
515 _colpm(m
, cl
, cc
, uc
);
526 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void*)-1) {
528 if (mb
.mb_type
== MB_IMAP
)
529 imap_getheaders(1, msgCount
);
531 _makethreads(message
, msgCount
, (vp
== (void*)-1));
532 if (mb
.mb_sorted
!= NULL
)
534 mb
.mb_sorted
= sstrdup("thread");
537 if (vp
!= NULL
&& vp
!= (void*)-1 && !inhook
&& ok_blook(header
))
556 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
559 if (vp
&& !inhook
&& ok_blook(header
))
568 next_in_thread(struct message
*mp
)
573 if ((rv
= mp
->m_child
) != NULL
)
575 if ((rv
= mp
->m_younger
) != NULL
)
578 while ((rv
= mp
->m_parent
) != NULL
) {
580 if ((rv
= rv
->m_younger
) != NULL
)
589 prev_in_thread(struct message
*mp
)
594 if ((rv
= mp
->m_elder
) != NULL
) {
595 for (mp
= rv
; (rv
= mp
->m_child
) != NULL
;) {
597 while ((rv
= mp
->m_younger
) != NULL
)
610 this_in_thread(struct message
*mp
, long n
)
615 if (n
== -1) { /* find end of thread */
617 if ((rv
= mp
->m_younger
) != NULL
) {
621 rv
= next_in_thread(mp
);
622 if (rv
== NULL
|| rv
->m_threadpos
< mp
->m_threadpos
) {
632 while (mp
!= NULL
&& mp
->m_threadpos
< n
) {
633 if ((rv
= mp
->m_younger
) != NULL
&& rv
->m_threadpos
<= n
) {
637 mp
= next_in_thread(mp
);
639 rv
= (mp
!= NULL
&& mp
->m_threadpos
== n
) ? mp
: NULL
;
648 enum method
{SORT_SUBJECT
, SORT_DATE
, SORT_STATUS
, SORT_SIZE
, SORT_FROM
,
649 SORT_TO
, SORT_SPAM
, SORT_THREAD
} method
;
652 enum method me_method
;
653 int (*me_func
)(void const *, void const *);
654 } const methnames
[] = {
655 {"date", SORT_DATE
, &_mlonglt
},
656 {"from", SORT_FROM
, &_mcharlt
},
657 {"to", SORT_TO
, &_mcharlt
},
658 {"subject", SORT_SUBJECT
, &_mcharlt
},
659 {"size", SORT_SIZE
, &_mlonglt
},
661 {"spam", SORT_SPAM
, &_mui32lt
},
663 {"status", SORT_STATUS
, &_mlonglt
},
664 {"thread", SORT_THREAD
, NULL
}
668 char **args
= (char**)vp
, *cp
, *_args
[2];
669 int (*func
)(void const *, void const *);
676 showname
= ok_blook(showname
);
677 msgvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
680 if (vp
== NULL
|| vp
== (void*)-1) {
681 _args
[0] = savestr(mb
.mb_sorted
);
684 } else if (args
[0] == NULL
) {
685 printf("Current sorting criterion is: %s\n",
686 (mb
.mb_sorted
? mb
.mb_sorted
: "unsorted"));
691 for (i
= 0; UICMP(z
, i
, <, NELEM(methnames
)); ++i
)
692 if (*args
[0] && is_prefix(args
[0], methnames
[i
].me_name
))
694 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
699 method
= methnames
[i
].me_method
;
700 func
= methnames
[i
].me_func
;
702 mb
.mb_sorted
= sstrdup(args
[0]);
704 if (method
== SORT_THREAD
) {
705 i
= c_thread((vp
!= NULL
&& vp
!= (void*)-1) ? msgvec
: vp
);
709 ms
= ac_alloc(sizeof *ms
* msgCount
);
716 if (mb
.mb_type
== MB_IMAP
)
717 imap_getheaders(1, msgCount
);
725 for (n
= 0, i
= 0; i
< msgCount
; ++i
) {
727 if (!(mp
->m_flag
& MHIDDEN
)) {
730 if (mp
->m_date
== 0 && (cp
= hfield1("date", mp
)) != NULL
)
731 mp
->m_date
= rfctime(cp
);
732 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
735 if (mp
->m_flag
& MDELETED
)
736 ms
[n
].ms_u
.ms_long
= 1;
737 else if ((mp
->m_flag
& (MNEW
| MREAD
)) == MNEW
)
738 ms
[n
].ms_u
.ms_long
= 90;
739 else if (mp
->m_flag
& MFLAGGED
)
740 ms
[n
].ms_u
.ms_long
= 85;
741 else if ((mp
->m_flag
& (MNEW
| MBOX
)) == MBOX
)
742 ms
[n
].ms_u
.ms_long
= 70;
743 else if (mp
->m_flag
& MNEW
)
744 ms
[n
].ms_u
.ms_long
= 80;
745 else if (mp
->m_flag
& MREAD
)
746 ms
[n
].ms_u
.ms_long
= 40;
748 ms
[n
].ms_u
.ms_long
= 60;
751 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
755 ms
[n
].ms_u
.ms_ui
= mp
->m_spamscore
;
760 if ((cp
= hfield1((method
== SORT_FROM
? "from" : "to"), mp
)) !=
762 ms
[n
].ms_u
.ms_char
= sstrdup(showname
? realname(cp
) : skin(cp
));
763 makelow(ms
[n
].ms_u
.ms_char
);
765 ms
[n
].ms_u
.ms_char
= sstrdup("");
769 if ((cp
= hfield1("subject", mp
)) != NULL
) {
772 mime_fromhdr(&in
, &out
, TD_ICONV
);
773 ms
[n
].ms_u
.ms_char
= sstrdup(_skipre(out
.s
));
775 makelow(ms
[n
].ms_u
.ms_char
);
777 ms
[n
].ms_u
.ms_char
= sstrdup("");
782 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
790 qsort(ms
, n
, sizeof *ms
, func
);
791 threadroot
= &message
[ms
[0].ms_n
];
792 for (i
= 1; i
< n
; ++i
) {
793 message
[ms
[i
-1].ms_n
].m_younger
= &message
[ms
[i
].ms_n
];
794 message
[ms
[i
].ms_n
].m_elder
= &message
[ms
[i
-1].ms_n
];
797 threadroot
= &message
[0];
798 _finalize(threadroot
);
805 for (i
= 0; i
< n
; ++i
)
806 free(ms
[i
].ms_u
.ms_char
);
812 i
= ((vp
!= NULL
&& vp
!= (void*)-1 && !inhook
&& ok_blook(header
))
813 ? c_headers(msgvec
) : 0);
831 c_uncollapse(void *v
)
842 uncollapse1(struct message
*mp
, int always
)
845 if (mb
.mb_threaded
== 1 && (always
|| mp
->m_collapsed
> 0))
850 /* vim:set fenc=utf-8:s-it-mode */