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
40 #ifndef HAVE_AMALGAMATION
45 * Open addressing is used for Message-IDs because the maximum number of
46 * messages in the table is known in advance (== msgCount).
49 struct message
*mi_data
;
64 static unsigned mhash(const char *cp
, int mprime
);
65 static struct mitem
*mlook(char *id
, struct mitem
*mt
, struct message
*mdata
,
67 static void adopt(struct message
*parent
, struct message
*child
, int dist
);
68 static struct message
*interlink(struct message
*m
, long cnt
, int nmail
);
69 static void finalize(struct message
*mp
);
71 static int muilt(void const *a
, void const *b
);
73 static int mlonglt(const void *a
, const void *b
);
74 static int mcharlt(const void *a
, const void *b
);
75 static void lookup(struct message
*m
, struct mitem
*mi
, int mprime
);
76 static void makethreads(struct message
*m
, long cnt
, int nmail
);
77 static char const *skipre(char const *cp
);
78 static int colpt(int *msgvec
, int cl
);
79 static void colps(struct message
*b
, int cl
);
80 static void colpm(struct message
*m
, int cl
, int *cc
, int *uc
);
83 * Return the hash value for a message id modulo mprime, or mprime
84 * if the passed string does not look like a message-id.
87 mhash(const char *cp
, int mprime
)
90 unsigned h
= 0, g
, at
= 0;
95 * Pay attention not to hash characters which are
96 * irrelevant for Message-ID semantics.
99 cp
= skip_comment(&cp
[1]) - 1;
102 if (*cp
== '"' || *cp
== '\\')
106 h
= ((h
<< 4) & 0xffffffff) + lowerconv(*cp
& 0377);
107 if ((g
= h
& 0xf0000000) != 0) {
112 return at
? h
% (unsigned int)mprime
: (unsigned int)mprime
;
115 #define NOT_AN_ID ((struct mitem *)-1)
118 * Look up a message id. Returns NOT_AN_ID if the passed string does
119 * not look like a message-id.
121 static struct mitem
*
122 mlook(char *id
, struct mitem
*mt
, struct message
*mdata
, int mprime
)
125 unsigned h
, c
, n
= 0;
127 if (id
== NULL
&& (id
= hfield1("message-id", mdata
)) == NULL
)
129 if (mdata
&& mdata
->m_idhash
)
130 h
= ~mdata
->m_idhash
;
132 h
= mhash(id
, mprime
);
133 if (h
== (unsigned int)mprime
)
137 while (mp
->mi_id
!= NULL
) {
138 if (msgidcmp(mp
->mi_id
, id
) == 0)
140 c
+= n
&1 ? -((n
+1)/2) * ((n
+1)/2) : ((n
+1)/2) * ((n
+1)/2);
142 while (c
>= (unsigned int)mprime
)
146 if (mdata
!= NULL
&& mp
->mi_id
== NULL
) {
149 mdata
->m_idhash
= ~h
;
151 return mp
->mi_id
? mp
: NULL
;
155 * Child is to be adopted by parent. A thread tree is structured
158 * ------ m_child ------ m_child
159 * | |-------------------->| |------------------------> . . .
160 * | |<--------------------| |<----------------------- . . .
161 * ------ m_parent ------ m_parent
163 * | \____ m_younger | |
167 * | m_parent ---- | |
172 * | | |------------------------> . . .
173 * | | |<----------------------- . . .
176 * \----- m_younger | |
185 * | |------------------------> . . .
186 * | |<----------------------- . . .
191 * The base message of a thread does not have a m_parent link. Elements
192 * connected by m_younger/m_elder links are replies to the same message,
193 * which is connected to them by m_parent links. The first reply to a
194 * message gets the m_child link.
197 adopt(struct message
*parent
, struct message
*child
, int dist
)
199 struct message
*mp
, *mq
;
201 for (mp
= parent
; mp
; mp
= mp
->m_parent
)
204 child
->m_level
= dist
; /* temporarily store distance */
205 child
->m_parent
= parent
;
206 if (parent
->m_child
!= NULL
) {
208 for (mp
= parent
->m_child
; mp
; mp
= mp
->m_younger
) {
209 if (mp
->m_date
>= child
->m_date
) {
211 mp
->m_elder
->m_younger
= child
;
212 child
->m_elder
= mp
->m_elder
;
214 child
->m_younger
= mp
;
215 if (mp
== parent
->m_child
)
216 parent
->m_child
= child
;
221 mq
->m_younger
= child
;
224 parent
->m_child
= child
;
228 * Connect all messages on the lowest thread level with m_younger/m_elder
231 static struct message
*
232 interlink(struct message
*m
, long cnt
, int nmail
)
237 struct message
*root
;
238 int autocollapse
= !nmail
&& !(inhook
&2) &&
239 ok_blook(autocollapse
);
241 ms
= smalloc(sizeof *ms
* cnt
);
242 for (n
= 0, i
= 0; i
< cnt
; i
++) {
243 if (m
[i
].m_parent
== NULL
) {
246 ms
[n
].ms_u
.ms_long
= m
[i
].m_date
;
252 qsort(ms
, n
, sizeof *ms
, mlonglt
);
253 root
= &m
[ms
[0].ms_n
];
254 for (i
= 1; i
< n
; i
++) {
255 m
[ms
[i
-1].ms_n
].m_younger
= &m
[ms
[i
].ms_n
];
256 m
[ms
[i
].ms_n
].m_elder
= &m
[ms
[i
-1].ms_n
];
265 finalize(struct message
*mp
)
269 for (n
= 0; mp
; mp
= next_in_thread(mp
)) {
270 mp
->m_threadpos
= ++n
;
271 mp
->m_level
= mp
->m_parent
?
272 mp
->m_level
+ mp
->m_parent
->m_level
: 0;
278 muilt(void const *a
, void const *b
)
280 struct msort
const *xa
= a
, *xb
= b
;
283 i
= (int)(xa
->ms_u
.ms_ui
- xb
->ms_u
.ms_ui
);
285 i
= xa
->ms_n
- xb
->ms_n
;
291 mlonglt(const void *a
, const void *b
)
293 struct msort
const *xa
= a
, *xb
= b
;
296 i
= (int)(xa
->ms_u
.ms_long
- xb
->ms_u
.ms_long
);
298 i
= xa
->ms_n
- xb
->ms_n
;
303 mcharlt(const void *a
, const void *b
)
305 struct msort
const *xa
= a
, *xb
= b
;
308 i
= strcoll(xa
->ms_u
.ms_char
, xb
->ms_u
.ms_char
);
310 i
= xa
->ms_n
- xb
->ms_n
;
315 lookup(struct message
*m
, struct mitem
*mi
, int mprime
)
322 if (m
->m_flag
& MHIDDEN
)
325 if ((cp
= hfield1("in-reply-to", m
)) != NULL
) {
326 if ((np
= extract(cp
, GREF
)) != NULL
)
328 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
329 != NULL
&& ip
!= NOT_AN_ID
) {
330 adopt(ip
->mi_data
, m
, 1);
333 } while ((np
= np
->n_flink
) != NULL
);
335 if ((cp
= hfield1("references", m
)) != NULL
) {
336 if ((np
= extract(cp
, GREF
)) != NULL
) {
337 while (np
->n_flink
!= NULL
)
340 if ((ip
= mlook(np
->n_name
, mi
, NULL
, mprime
))
343 continue; /* skip dist++ */
344 adopt(ip
->mi_data
, m
, dist
);
348 } while ((np
= np
->n_blink
) != NULL
);
354 makethreads(struct message
*m
, long cnt
, int nmail
)
362 mprime
= nextprime(cnt
);
363 mt
= scalloc(mprime
, sizeof *mt
);
364 for (i
= 0; i
< cnt
; i
++) {
365 if ((m
[i
].m_flag
&MHIDDEN
) == 0) {
366 mlook(NULL
, mt
, &m
[i
], mprime
);
367 if (m
[i
].m_date
== 0) {
368 if ((cp
= hfield1("date", &m
[i
])) != NULL
)
369 m
[i
].m_date
= rfctime(cp
);
372 m
[i
].m_child
= m
[i
].m_younger
= m
[i
].m_elder
=
373 m
[i
].m_parent
= NULL
;
375 if (!nmail
&& !(inhook
&2))
376 m
[i
].m_collapsed
= 0;
379 * Most folders contain the eldest messages first. Traversing
380 * them in descending order makes it more likely that younger
381 * brothers are found first, so elder ones can be prepended to
382 * the brother list, which is faster. The worst case is still
383 * in O(n^2) and occurs when all but one messages in a folder
384 * are replies to the one message, and are sorted such that
385 * youngest messages occur first.
387 for (i
= cnt
-1; i
>= 0; i
--)
388 lookup(&m
[i
], mt
, mprime
);
389 threadroot
= interlink(m
, cnt
, nmail
);
390 finalize(threadroot
);
398 if (mb
.mb_threaded
!= 1 || vp
== NULL
|| vp
== (void *)-1) {
400 if (mb
.mb_type
== MB_IMAP
)
401 imap_getheaders(1, msgCount
);
403 makethreads(message
, msgCount
, vp
== (void *)-1);
404 if (mb
.mb_sorted
!= NULL
)
406 mb
.mb_sorted
= sstrdup("thread");
408 if (vp
&& vp
!= (void *)-1 && !inhook
&& ok_blook(header
))
421 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
423 if (vp
&& !inhook
&& ok_blook(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.
507 enum method me_method
;
508 int (*me_func
)(const void *, const void *);
510 { "date", SORT_DATE
, mlonglt
},
511 { "from", SORT_FROM
, mcharlt
},
512 { "to", SORT_TO
, mcharlt
},
513 { "subject", SORT_SUBJECT
, mcharlt
},
514 { "size", SORT_SIZE
, mlonglt
},
516 { "spam", SORT_SPAM
, muilt
},
518 { "status", SORT_STATUS
, mlonglt
},
519 { "thread", SORT_THREAD
, NULL
},
522 char **args
= (char **)vp
, *cp
, *_args
[2];
523 int (*func
)(const void *, const void *);
527 bool_t showname
= ok_blook(showname
);
530 msgvec
[0] = dot
- &message
[0] + 1;
532 if (vp
== NULL
|| vp
== (void *)-1) {
533 _args
[0] = savestr(mb
.mb_sorted
);
536 } else if (args
[0] == NULL
) {
537 printf("Current sorting criterion is: %s\n",
538 mb
.mb_sorted
? mb
.mb_sorted
: "unsorted");
541 for (i
= 0; methnames
[i
].me_name
; i
++)
542 if (*args
[0] && is_prefix(args
[0], methnames
[i
].me_name
))
544 if (methnames
[i
].me_name
== NULL
) {
545 fprintf(stderr
, "Unknown sorting method \"%s\"\n", args
[0]);
548 method
= methnames
[i
].me_method
;
549 func
= methnames
[i
].me_func
;
551 mb
.mb_sorted
= sstrdup(args
[0]);
552 if (method
== SORT_THREAD
)
553 return thread(vp
&& vp
!= (void *)-1 ? msgvec
: vp
);
554 ms
= ac_alloc(sizeof *ms
* msgCount
);
561 if (mb
.mb_type
== MB_IMAP
)
562 imap_getheaders(1, msgCount
);
568 for (n
= 0, i
= 0; i
< msgCount
; i
++) {
570 if ((mp
->m_flag
&MHIDDEN
) == 0) {
573 if (mp
->m_date
== 0 &&
574 (cp
= hfield1("date", mp
)) != 0)
575 mp
->m_date
= rfctime(cp
);
576 ms
[n
].ms_u
.ms_long
= mp
->m_date
;
579 if (mp
->m_flag
& MDELETED
)
580 ms
[n
].ms_u
.ms_long
= 1;
581 else if ((mp
->m_flag
&(MNEW
|MREAD
)) == MNEW
)
582 ms
[n
].ms_u
.ms_long
= 90;
583 else if (mp
->m_flag
& MFLAGGED
)
584 ms
[n
].ms_u
.ms_long
= 85;
585 else if ((mp
->m_flag
&(MNEW
|MBOX
)) == MBOX
)
586 ms
[n
].ms_u
.ms_long
= 70;
587 else if (mp
->m_flag
& MNEW
)
588 ms
[n
].ms_u
.ms_long
= 80;
589 else if (mp
->m_flag
& MREAD
)
590 ms
[n
].ms_u
.ms_long
= 40;
592 ms
[n
].ms_u
.ms_long
= 60;
595 ms
[n
].ms_u
.ms_long
= mp
->m_xsize
;
599 ms
[n
].ms_u
.ms_ui
= mp
->m_spamscore
;
604 if ((cp
= hfield1(method
== SORT_FROM
?
605 "from" : "to", mp
)) != NULL
) {
606 ms
[n
].ms_u
.ms_char
= showname
?
607 realname(cp
) : skin(cp
);
608 makelow(ms
[n
].ms_u
.ms_char
);
610 ms
[n
].ms_u
.ms_char
= UNCONST("");
614 if ((cp
= hfield1("subject", mp
)) != NULL
) {
617 mime_fromhdr(&in
, &out
, TD_ICONV
);
619 savestr(skipre(out
.s
));
621 makelow(ms
[n
].ms_u
.ms_char
);
623 ms
[n
].ms_u
.ms_char
= UNCONST("");
628 mp
->m_child
= mp
->m_younger
= mp
->m_elder
= mp
->m_parent
= NULL
;
633 qsort(ms
, n
, sizeof *ms
, func
);
634 threadroot
= &message
[ms
[0].ms_n
];
635 for (i
= 1; i
< n
; i
++) {
636 message
[ms
[i
-1].ms_n
].m_younger
= &message
[ms
[i
].ms_n
];
637 message
[ms
[i
].ms_n
].m_elder
= &message
[ms
[i
-1].ms_n
];
640 threadroot
= &message
[0];
641 finalize(threadroot
);
644 return ((vp
&& vp
!= (void *)-1 && !inhook
&& ok_blook(header
))
645 ? headers(msgvec
) : 0);
649 skipre(char const *cp
)
651 if (lowerconv(cp
[0]) == 'r' && lowerconv(cp
[1]) == 'e' &&
652 cp
[2] == ':' && spacechar(cp
[3])) {
654 while (spacechar(*cp
))
673 colpt(int *msgvec
, int cl
)
677 if (mb
.mb_threaded
!= 1) {
678 puts("Not in threaded mode.");
681 for (ip
= msgvec
; *ip
!= 0; ip
++)
682 colps(&message
[*ip
-1], cl
);
687 colps(struct message
*b
, int cl
)
692 if (cl
&& (b
->m_collapsed
> 0 || (b
->m_flag
& (MNEW
|MREAD
)) == MNEW
))
696 colpm(m
, cl
, &cc
, &uc
);
697 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
698 colpm(m
, cl
, &cc
, &uc
);
701 b
->m_collapsed
= -cc
;
702 for (m
= b
->m_parent
; m
; m
= m
->m_parent
)
703 if (m
->m_collapsed
<= -uc
) {
704 m
->m_collapsed
+= uc
;
708 if (b
->m_collapsed
> 0) {
712 for (m
= b
; m
; m
= m
->m_parent
)
713 if (m
->m_collapsed
<= -uc
) {
714 m
->m_collapsed
+= uc
;
721 colpm(struct message
*m
, int cl
, int *cc
, int *uc
)
724 if (m
->m_collapsed
> 0)
726 if ((m
->m_flag
& (MNEW
|MREAD
)) != MNEW
|| m
->m_collapsed
< 0)
728 if (m
->m_collapsed
> 0)
731 if (m
->m_collapsed
> 0) {
738 colpm(m
, cl
, cc
, uc
);
739 for (m
= m
->m_younger
; m
; m
= m
->m_younger
)
740 colpm(m
, cl
, cc
, uc
);
745 uncollapse1(struct message
*m
, int always
)
747 if (mb
.mb_threaded
== 1 && (always
|| m
->m_collapsed
> 0))