2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
14 #include <lib-ui/curses.h>
21 #include <lib-crypt/crypt.h>
22 #include "mutt_idna.h"
25 int mutt_is_mail_list (address_t * addr)
27 if (!rx_list_match (UnMailLists, addr->mailbox))
28 return rx_list_match (MailLists, addr->mailbox);
32 int mutt_is_subscribed_list (address_t * addr)
34 if (!rx_list_match (UnMailLists, addr->mailbox)
35 && !rx_list_match (UnSubscribedLists, addr->mailbox))
36 return rx_list_match (SubscribedLists, addr->mailbox);
40 /* Search for a mailing list in the list of addresses pointed to by adr.
41 * If one is found, print pfx and the name of the list into buf, then
42 * return 1. Otherwise, simply return 0.
45 check_for_mailing_list (address_t * adr, const char *pfx, char *buf, int buflen)
47 for (; adr; adr = adr->next) {
48 if (mutt_is_subscribed_list (adr)) {
49 if (pfx && buf && buflen)
50 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
57 /* Search for a mailing list in the list of addresses pointed to by adr.
58 * If one is found, print the address of the list into buf, then return 1.
59 * Otherwise, simply return 0.
61 static int check_for_mailing_list_addr (address_t * adr, char *buf, int buflen)
63 for (; adr; adr = adr->next) {
64 if (mutt_is_subscribed_list (adr)) {
66 snprintf (buf, buflen, "%s", adr->mailbox);
74 static int first_mailing_list (char *buf, ssize_t buflen, address_t * a)
76 for (; a; a = a->next) {
77 if (mutt_is_subscribed_list (a)) {
78 mutt_save_path (buf, buflen, a);
85 static void make_from (ENVELOPE * hdr, char *buf, ssize_t len, int do_lists)
89 me = mutt_addr_is_user (hdr->from);
92 if (check_for_mailing_list (hdr->to, "To ", buf, len))
94 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
99 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
100 else if (me && hdr->cc)
101 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
103 m_strcpy(buf, len, mutt_get_name(hdr->from));
108 static void make_from_addr (ENVELOPE * hdr, char *buf, ssize_t len,
113 me = mutt_addr_is_user (hdr->from);
115 if (do_lists || me) {
116 if (check_for_mailing_list_addr (hdr->to, buf, len))
118 if (check_for_mailing_list_addr (hdr->cc, buf, len))
123 snprintf (buf, len, "%s", hdr->to->mailbox);
124 else if (me && hdr->cc)
125 snprintf (buf, len, "%s", hdr->cc->mailbox);
127 m_strcpy(buf, len, hdr->from->mailbox);
132 static int user_in_addr (address_t * a)
134 for (; a; a = a->next)
135 if (mutt_addr_is_user (a))
141 * 0: user is not in list
142 * 1: user is unique recipient
143 * 2: user is in the TO list
144 * 3: user is in the CC list
145 * 4: user is originator
146 * 5: sent to a subscribed mailinglist
148 int mutt_user_is_recipient (HEADER * h)
150 ENVELOPE *env = h->env;
152 if (!h->recip_valid) {
155 if (mutt_addr_is_user (env->from))
157 else if (user_in_addr (env->to)) {
158 if (env->to->next || env->cc)
159 h->recipient = 2; /* non-unique recipient */
161 h->recipient = 1; /* unique recipient */
163 else if (user_in_addr (env->cc))
165 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
167 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
176 /* %a = address of author
177 * %A = reply-to address (if present; otherwise: address of author
178 * %b = filename of the originating folder
179 * %B = the list to which the letter was sent
180 * %c = size of message in bytes
181 * %C = current message number
182 * %d = date and time of message using $date_format and sender's timezone
183 * %D = date and time of message using $date_format and local timezone
184 * %e = current message number in thread
185 * %E = number of messages in current thread
186 * %f = entire from line
187 * %F = like %n, unless from self
188 * %g = newsgroup name (if compiled with nntp support)
190 * %I = initials of author
191 * %l = number of lines in the message
192 * %L = like %F, except `lists' are displayed first
193 * %m = number of messages in the mailbox
194 * %n = name of author
196 * %O = like %L, except using address instead of name
198 * %S = short message status (e.g., N/O/D/!/r/-)
199 * %t = `to:' field (recipients)
201 * %u = user (login) name of author
202 * %v = first name of author, unless from self
203 * %W = where user is (organization)
204 * %X = number of MIME attachments
205 * %y = `x-label:' field (if present)
206 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
207 * %Z = status flags */
209 struct hdr_format_info {
214 static const char *hdr_format_str (char *dest,
219 const char *ifstring,
220 const char *elsestring,
221 unsigned long data, format_flag flags)
223 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
226 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
228 int optional = (flags & M_FORMAT_OPTIONAL);
229 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
230 int is_index = (flags & M_FORMAT_INDEX);
232 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
233 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
242 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
243 mutt_format_s (dest, destlen, prefix,
244 mutt_addr_for_display (hdr->env->reply_to));
247 /* fall through if 'A' returns nothing */
250 if (hdr->env->from && hdr->env->from->mailbox) {
251 mutt_format_s (dest, destlen, prefix,
252 mutt_addr_for_display (hdr->env->from));
259 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
260 !first_mailing_list (dest, destlen, hdr->env->cc))
263 m_strcpy(buf2, sizeof(buf2), dest);
264 mutt_format_s (dest, destlen, prefix, buf2);
267 /* fall through if 'B' returns nothing */
271 if ((p = strrchr (ctx->path, '/')))
272 m_strcpy(dest, destlen, p + 1);
274 m_strcpy(dest, destlen, ctx->path);
277 m_strcpy(dest, destlen, "(null)");
278 m_strcpy(buf2, sizeof(buf2), dest);
279 mutt_format_s (dest, destlen, prefix, buf2);
283 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
284 mutt_format_s (dest, destlen, prefix, buf2);
288 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
289 snprintf (dest, destlen, fmt, hdr->msgno + 1);
299 /* preprocess $date_format to handle %Z */
307 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
316 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
317 (op == '{' && *cp != '}') ||
318 (op == '[' && *cp != ']') ||
319 (op == '(' && *cp != ')') ||
320 (op == '<' && *cp != '>'))) {
323 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
325 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
326 hdr->zhours, hdr->zminutes);
331 break; /* not enough space left */
340 break; /* not enough space */
351 if (do_locales && Locale)
352 setlocale (LC_TIME, Locale);
354 if (op == '[' || op == 'D')
355 tm = localtime (&hdr->date_sent);
357 tm = localtime (&hdr->received);
358 else if (op == '<') {
363 /* restore sender's time zone */
366 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
368 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
372 strftime (buf2, sizeof (buf2), dest, tm);
375 setlocale (LC_TIME, "C");
377 mutt_format_s (dest, destlen, prefix, buf2);
378 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
384 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
385 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
390 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
391 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
393 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
399 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
400 mutt_format_s (dest, destlen, prefix, buf2);
405 make_from (hdr->env, buf2, sizeof (buf2), 0);
406 mutt_format_s (dest, destlen, prefix, buf2);
408 else if (mutt_addr_is_user (hdr->env->from))
414 mutt_format_s (dest, destlen, prefix,
415 hdr->env->newsgroups ? hdr->env->newsgroups : "");
420 /* (Hormel) spam score */
422 optional = hdr->env->spam ? 1 : 0;
425 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
427 mutt_format_s (dest, destlen, prefix, "");
432 mutt_format_s (dest, destlen, prefix,
433 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
441 for (i = 0; hdr->env->from && hdr->env->from->personal &&
442 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
443 if (isalpha ((int) hdr->env->from->personal[i])) {
445 buf2[j++] = hdr->env->from->personal[i];
455 mutt_format_s (dest, destlen, prefix, buf2);
460 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
461 snprintf (dest, destlen, fmt, (int) hdr->lines);
463 else if (hdr->lines <= 0)
469 make_from (hdr->env, buf2, sizeof (buf2), 1);
470 mutt_format_s (dest, destlen, prefix, buf2);
472 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
473 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
480 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
481 snprintf (dest, destlen, fmt, ctx->msgcount);
484 m_strcpy(dest, destlen, "(null)");
488 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
493 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
494 snprintf (dest, destlen, fmt, hdr->score);
504 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
505 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
507 mutt_format_s (dest, destlen, prefix, buf2);
509 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
510 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
516 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
518 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
519 snprintf (dest, destlen, fmt, hdr->num_hidden);
520 else if (is_index && threads)
521 mutt_format_s (dest, destlen, prefix, " ");
526 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
533 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
534 if (flags & M_FORMAT_FORCESUBJ) {
535 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
536 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
537 mutt_format_s_tree (dest, destlen, prefix, buf2);
540 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
543 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
549 else if (hdr->attach_del)
551 else if (hdr->tagged)
553 else if (hdr->flagged)
555 else if (hdr->replied)
557 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
564 /* FOO - this is probably unsafe, but we are not likely to have such
565 a short string passed into this routine */
572 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
573 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
575 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
576 else if (hdr->env->cc)
577 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
579 mutt_format_s (dest, destlen, prefix, buf2);
583 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
584 snprintf (dest, destlen, fmt,
586 && ((i = mutt_user_is_recipient (hdr))) <
587 m_strlen(Tochars)) ? Tochars[i] : ' ');
591 if (hdr->env->from && hdr->env->from->mailbox) {
592 m_strcpy(buf2, sizeof(buf2), mutt_addr_for_display(hdr->env->from));
593 if ((p = strpbrk (buf2, "%@")))
598 mutt_format_s (dest, destlen, prefix, buf2);
602 if (mutt_addr_is_user (hdr->env->from)) {
604 mutt_format_s (buf2, sizeof (buf2), prefix,
605 mutt_get_name (hdr->env->to));
606 else if (hdr->env->cc)
607 mutt_format_s (buf2, sizeof (buf2), prefix,
608 mutt_get_name (hdr->env->cc));
613 mutt_format_s (buf2, sizeof (buf2), prefix,
614 mutt_get_name (hdr->env->from));
615 if ((p = strpbrk (buf2, " %@")))
617 mutt_format_s (dest, destlen, prefix, buf2);
622 mutt_format_s (dest, destlen, prefix,
623 hdr->env->organization ? hdr->env->organization : "");
624 else if (!hdr->env->organization)
632 if (option (OPTCOUNTATTACH)) {
633 if (!hdr->content->parts)
634 mutt_parse_mime_message(ctx, hdr);
635 count = mutt_count_body_parts(hdr, 0);
638 /* The recursion allows messages without depth to return 0. */
640 optional = count != 0;
642 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
643 snprintf (dest, destlen, fmt, count);
651 if (hdr->security & GOODSIGN)
653 else if (hdr->security & ENCRYPT)
655 else if (hdr->security & SIGN)
657 else if (hdr->security & PGPKEY)
660 snprintf(buf2, sizeof (buf2), "%c%c%c",
664 : ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
665 ? (hdr->replied ? 'r' : ' ')
666 : (hdr->old ? 'O' : 'N')))),
667 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
669 : (hdr->flagged ? '!'
670 : (Tochars && ((i = mutt_user_is_recipient(hdr)) < m_strlen(Tochars))
671 ? Tochars[i] : ' ')));
672 mutt_format_s (dest, destlen, prefix, buf2);
677 optional = hdr->env->x_label ? 1 : 0;
679 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
683 if (hdr->env->x_label) {
684 i = 1; /* reduce reuse recycle */
686 if (flags & M_FORMAT_TREE
687 && (hdr->thread->prev && hdr->thread->prev->message
688 && hdr->thread->prev->message->env->x_label))
689 htmp = hdr->thread->prev->message;
690 else if (flags & M_FORMAT_TREE
691 && (hdr->thread->parent && hdr->thread->parent->message
692 && hdr->thread->parent->message->env->x_label))
693 htmp = hdr->thread->parent->message;
694 if (htmp && m_strcasecmp(hdr->env->x_label,
695 htmp->env->x_label) == 0)
705 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
707 mutt_format_s (dest, destlen, prefix, "");
712 snprintf (dest, destlen, "%%%s%c", prefix, op);
717 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
718 (unsigned long) hfi, flags);
719 else if (flags & M_FORMAT_OPTIONAL)
720 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
721 (unsigned long) hfi, flags);
729 _mutt_make_string (char *dest, ssize_t destlen, const char *s, CONTEXT * ctx,
730 HEADER * hdr, format_flag flags)
732 struct hdr_format_info hfi;
737 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,