2 * Copyright (C) 1996-2000,2002,2007 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_curses.h"
27 #include "mutt_crypt.h"
28 #include "mutt_idna.h"
39 int mutt_is_mail_list (ADDRESS *addr)
41 if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
42 return mutt_match_rx_list (addr->mailbox, MailLists);
46 int mutt_is_subscribed_list (ADDRESS *addr)
48 if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
49 && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
50 return mutt_match_rx_list (addr->mailbox, SubscribedLists);
54 /* Search for a mailing list in the list of addresses pointed to by adr.
55 * If one is found, print pfx and the name of the list into buf, then
56 * return 1. Otherwise, simply return 0.
59 check_for_mailing_list (ADDRESS *adr, char *pfx, char *buf, int buflen)
61 for (; adr; adr = adr->next)
63 if (mutt_is_subscribed_list (adr))
65 if (pfx && buf && buflen)
66 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
73 /* Search for a mailing list in the list of addresses pointed to by adr.
74 * If one is found, print the address of the list into buf, then return 1.
75 * Otherwise, simply return 0.
78 check_for_mailing_list_addr (ADDRESS *adr, char *buf, int buflen)
80 for (; adr; adr = adr->next)
82 if (mutt_is_subscribed_list (adr))
85 snprintf (buf, buflen, "%s", adr->mailbox);
93 static int first_mailing_list (char *buf, size_t buflen, ADDRESS *a)
95 for (; a; a = a->next)
97 if (mutt_is_subscribed_list (a))
99 mutt_save_path (buf, buflen, a);
106 static void make_from (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
110 me = mutt_addr_is_user (hdr->from);
114 if (check_for_mailing_list (hdr->to, "To ", buf, len))
116 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
121 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
122 else if (me && hdr->cc)
123 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
125 strfcpy (buf, mutt_get_name (hdr->from), len);
130 static void make_from_addr (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
134 me = mutt_addr_is_user (hdr->from);
138 if (check_for_mailing_list_addr (hdr->to, buf, len))
140 if (check_for_mailing_list_addr (hdr->cc, buf, len))
145 snprintf (buf, len, "%s", hdr->to->mailbox);
146 else if (me && hdr->cc)
147 snprintf (buf, len, "%s", hdr->cc->mailbox);
149 strfcpy (buf, hdr->from->mailbox, len);
154 static int user_in_addr (ADDRESS *a)
156 for (; a; a = a->next)
157 if (mutt_addr_is_user (a))
163 * 0: user is not in list
164 * 1: user is unique recipient
165 * 2: user is in the TO list
166 * 3: user is in the CC list
167 * 4: user is originator
168 * 5: sent to a subscribed mailinglist
170 int mutt_user_is_recipient (HEADER *h)
172 ENVELOPE *env = h->env;
178 if (mutt_addr_is_user (env->from))
180 else if (user_in_addr (env->to))
182 if (env->to->next || env->cc)
183 h->recipient = 2; /* non-unique recipient */
185 h->recipient = 1; /* unique recipient */
187 else if (user_in_addr (env->cc))
189 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
191 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
200 /* %a = address of author
201 * %A = reply-to address (if present; otherwise: address of author
202 * %b = filename of the originating folder
203 * %B = the list to which the letter was sent
204 * %c = size of message in bytes
205 * %C = current message number
206 * %d = date and time of message using $date_format and sender's timezone
207 * %D = date and time of message using $date_format and local timezone
208 * %e = current message number in thread
209 * %E = number of messages in current thread
210 * %f = entire from line
211 * %F = like %n, unless from self
213 * %l = number of lines in the message
214 * %L = like %F, except `lists' are displayed first
215 * %m = number of messages in the mailbox
216 * %n = name of author
218 * %O = like %L, except using address instead of name
219 * %P = progress indicator for builtin pager
221 * %S = short message status (e.g., N/O/D/!/r/-)
222 * %t = `to:' field (recipients)
224 * %u = user (login) name of author
225 * %v = first name of author, unless from self
226 * %X = number of MIME attachments
227 * %y = `x-label:' field (if present)
228 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
229 * %Z = status flags */
232 hdr_format_str (char *dest,
238 const char *ifstring,
239 const char *elsestring,
243 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
246 char fmt[SHORT_STRING], buf2[LONG_STRING], ch, *p;
248 int optional = (flags & M_FORMAT_OPTIONAL);
249 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
250 int is_index = (flags & M_FORMAT_INDEX);
251 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
252 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
262 if(hdr->env->reply_to && hdr->env->reply_to->mailbox)
264 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->reply_to));
267 /* fall through if 'A' returns nothing */
270 if(hdr->env->from && hdr->env->from->mailbox)
272 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->from));
279 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
280 !first_mailing_list (dest, destlen, hdr->env->cc))
284 strfcpy (buf2, dest, sizeof(buf2));
285 mutt_format_s (dest, destlen, prefix, buf2);
288 /* fall through if 'B' returns nothing */
293 if ((p = strrchr (ctx->path, '/')))
294 strfcpy (dest, p + 1, destlen);
296 strfcpy (dest, ctx->path, destlen);
299 strfcpy(dest, "(null)", destlen);
300 strfcpy (buf2, dest, sizeof(buf2));
301 mutt_format_s (dest, destlen, prefix, buf2);
305 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
306 mutt_format_s (dest, destlen, prefix, buf2);
310 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
311 snprintf (dest, destlen, fmt, hdr->msgno + 1);
321 /* preprocess $date_format to handle %Z */
329 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
339 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
340 (op == '{' && *cp != '}') ||
341 (op == '[' && *cp != ']') ||
342 (op == '(' && *cp != ')') ||
343 (op == '<' && *cp != '>')))
348 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{'))
352 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
353 hdr->zhours, hdr->zminutes);
358 break; /* not enough space left */
369 break; /* not enough space */
381 if (do_locales && Locale)
382 setlocale (LC_TIME, Locale);
384 if (op == '[' || op == 'D')
385 tm = localtime (&hdr->date_sent);
387 tm = localtime (&hdr->received);
395 /* restore sender's time zone */
398 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
400 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
404 strftime (buf2, sizeof (buf2), dest, tm);
407 setlocale (LC_TIME, "C");
409 mutt_format_s (dest, destlen, prefix, buf2);
410 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
416 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
417 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 1));
423 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
424 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 0));
426 else if (mutt_messages_in_thread(ctx, hdr, 0) <= 1)
432 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
433 mutt_format_s (dest, destlen, prefix, buf2);
439 make_from (hdr->env, buf2, sizeof (buf2), 0);
440 mutt_format_s (dest, destlen, prefix, buf2);
442 else if (mutt_addr_is_user (hdr->env->from))
447 /* (Hormel) spam score */
449 optional = hdr->env->spam ? 1 : 0;
452 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
454 mutt_format_s (dest, destlen, prefix, "");
459 mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
465 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
466 snprintf (dest, destlen, fmt, (int) hdr->lines);
468 else if (hdr->lines <= 0)
475 make_from (hdr->env, buf2, sizeof (buf2), 1);
476 mutt_format_s (dest, destlen, prefix, buf2);
478 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
479 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0))
488 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
489 snprintf (dest, destlen, fmt, ctx->msgcount);
492 strfcpy(dest, "(null)", destlen);
496 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
502 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
503 snprintf (dest, destlen, fmt, hdr->score);
515 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
516 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
518 mutt_format_s (dest, destlen, prefix, buf2);
520 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
521 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0))
528 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
531 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
532 snprintf (dest, destlen, fmt, hdr->num_hidden);
533 else if (is_index && threads)
534 mutt_format_s (dest, destlen, prefix, " ");
540 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
546 strfcpy(dest, NONULL(hfi->pager_progress), destlen);
551 if (flags & M_FORMAT_TREE && !hdr->collapsed)
553 if (flags & M_FORMAT_FORCESUBJ)
555 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
556 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
557 mutt_format_s_tree (dest, destlen, prefix, buf2);
560 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
563 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
569 else if (hdr->attach_del)
571 else if (hdr->tagged)
573 else if (hdr->flagged)
575 else if (hdr->replied)
577 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
584 /* FOO - this is probably unsafe, but we are not likely to have such
585 a short string passed into this routine */
592 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
593 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2)))
596 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
597 else if (hdr->env->cc)
598 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
600 mutt_format_s (dest, destlen, prefix, buf2);
604 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
605 snprintf (dest, destlen, fmt,
606 (Tochars && ((i = mutt_user_is_recipient (hdr))) < mutt_strlen (Tochars)) ? Tochars[i] : ' ');
610 if (hdr->env->from && hdr->env->from->mailbox)
612 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
613 if ((p = strpbrk (buf2, "%@")))
618 mutt_format_s (dest, destlen, prefix, buf2);
622 if (mutt_addr_is_user (hdr->env->from))
625 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->to));
626 else if (hdr->env->cc)
627 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->cc));
632 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->from));
633 if ((p = strpbrk (buf2, " %@")))
635 mutt_format_s (dest, destlen, prefix, buf2);
642 if (WithCrypto && hdr->security & GOODSIGN)
644 else if (WithCrypto && hdr->security & ENCRYPT)
646 else if (WithCrypto && hdr->security & SIGN)
648 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
651 snprintf (buf2, sizeof (buf2),
652 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
653 ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
654 ? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
655 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
657 (hdr->flagged ? '!' :
658 (Tochars && ((i = mutt_user_is_recipient (hdr)) < mutt_strlen (Tochars)) ? Tochars[i] : ' ')));
659 mutt_format_s (dest, destlen, prefix, buf2);
664 int count = mutt_count_body_parts (ctx, hdr);
666 /* The recursion allows messages without depth to return 0. */
668 optional = count != 0;
670 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
671 snprintf (dest, destlen, fmt, count);
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)
685 i = 1; /* reduce reuse recycle */
687 if (flags & M_FORMAT_TREE
688 && (hdr->thread->prev && hdr->thread->prev->message
689 && hdr->thread->prev->message->env->x_label))
690 htmp = hdr->thread->prev->message;
691 else if (flags & M_FORMAT_TREE
692 && (hdr->thread->parent && hdr->thread->parent->message
693 && hdr->thread->parent->message->env->x_label))
694 htmp = hdr->thread->parent->message;
695 if (htmp && mutt_strcasecmp (hdr->env->x_label,
696 htmp->env->x_label) == 0)
706 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
708 mutt_format_s (dest, destlen, prefix, "");
713 snprintf (dest, destlen, "%%%s%c", prefix, op);
718 mutt_FormatString (dest, destlen, col, ifstring, hdr_format_str, (unsigned long) hfi, flags);
719 else if (flags & M_FORMAT_OPTIONAL)
720 mutt_FormatString (dest, destlen, col, elsestring, hdr_format_str, (unsigned long) hfi, flags);
728 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT *ctx, HEADER *hdr, format_flag flags)
730 struct hdr_format_info hfi;
734 hfi.pager_progress = 0;
736 mutt_FormatString (dest, destlen, 0, s, hdr_format_str, (unsigned long) &hfi, flags);
740 mutt_make_string_info (char *dst, size_t dstlen, const char *s, struct hdr_format_info *hfi, format_flag flags)
742 mutt_FormatString (dst, dstlen, 0, s, hdr_format_str, (unsigned long) hfi, flags);