2 * Copyright (C) 1996-2000,2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
27 #include "mutt_crypt.h"
28 #include "mutt_idna.h"
35 int mutt_is_mail_list (ADDRESS * addr)
37 if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
38 return mutt_match_rx_list (addr->mailbox, MailLists);
42 int mutt_is_subscribed_list (ADDRESS * addr)
44 if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
45 && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
46 return mutt_match_rx_list (addr->mailbox, SubscribedLists);
50 /* Search for a mailing list in the list of addresses pointed to by adr.
51 * If one is found, print pfx and the name of the list into buf, then
52 * return 1. Otherwise, simply return 0.
55 check_for_mailing_list (ADDRESS * adr, char *pfx, char *buf, int buflen)
57 for (; adr; adr = adr->next) {
58 if (mutt_is_subscribed_list (adr)) {
59 if (pfx && buf && buflen)
60 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
67 /* Search for a mailing list in the list of addresses pointed to by adr.
68 * If one is found, print the address of the list into buf, then return 1.
69 * Otherwise, simply return 0.
71 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
73 for (; adr; adr = adr->next) {
74 if (mutt_is_subscribed_list (adr)) {
76 snprintf (buf, buflen, "%s", adr->mailbox);
84 static int first_mailing_list (char *buf, size_t buflen, ADDRESS * a)
86 for (; a; a = a->next) {
87 if (mutt_is_subscribed_list (a)) {
88 mutt_save_path (buf, buflen, a);
95 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
99 me = mutt_addr_is_user (hdr->from);
101 if (do_lists || me) {
102 if (check_for_mailing_list (hdr->to, "To ", buf, len))
104 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
109 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
110 else if (me && hdr->cc)
111 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
113 strfcpy (buf, mutt_get_name (hdr->from), len);
118 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
123 me = mutt_addr_is_user (hdr->from);
125 if (do_lists || me) {
126 if (check_for_mailing_list_addr (hdr->to, buf, len))
128 if (check_for_mailing_list_addr (hdr->cc, buf, len))
133 snprintf (buf, len, "%s", hdr->to->mailbox);
134 else if (me && hdr->cc)
135 snprintf (buf, len, "%s", hdr->cc->mailbox);
137 strfcpy (buf, hdr->from->mailbox, len);
142 static int user_in_addr (ADDRESS * a)
144 for (; a; a = a->next)
145 if (mutt_addr_is_user (a))
151 * 0: user is not in list
152 * 1: user is unique recipient
153 * 2: user is in the TO list
154 * 3: user is in the CC list
155 * 4: user is originator
156 * 5: sent to a subscribed mailinglist
158 int mutt_user_is_recipient (HEADER * h)
160 ENVELOPE *env = h->env;
162 if (!h->recip_valid) {
165 if (mutt_addr_is_user (env->from))
167 else if (user_in_addr (env->to)) {
168 if (env->to->next || env->cc)
169 h->recipient = 2; /* non-unique recipient */
171 h->recipient = 1; /* unique recipient */
173 else if (user_in_addr (env->cc))
175 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
177 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
186 /* %a = address of author
187 * %A = reply-to address (if present; otherwise: address of author
188 * %b = filename of the originating folder
189 * %B = the list to which the letter was sent
190 * %c = size of message in bytes
191 * %C = current message number
192 * %d = date and time of message using $date_format and sender's timezone
193 * %D = date and time of message using $date_format and local timezone
194 * %e = current message number in thread
195 * %E = number of messages in current thread
196 * %f = entire from line
197 * %F = like %n, unless from self
198 * %g = newsgroup name (if compiled with nntp support)
200 * %I = initials of author
201 * %l = number of lines in the message
202 * %L = like %F, except `lists' are displayed first
203 * %m = number of messages in the mailbox
204 * %n = name of author
206 * %O = like %L, except using address instead of name
208 * %S = short message status (e.g., N/O/D/!/r/-)
209 * %t = `to:' field (recipients)
211 * %u = user (login) name of author
212 * %v = first name of author, unless from self
213 * %W = where user is (organization)
214 * %y = `x-label:' field (if present)
215 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
216 * %Z = status flags */
218 struct hdr_format_info {
223 static const char *hdr_format_str (char *dest,
228 const char *ifstring,
229 const char *elsestring,
230 unsigned long data, format_flag flags)
232 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
235 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
237 int optional = (flags & M_FORMAT_OPTIONAL);
238 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
239 int is_index = (flags & M_FORMAT_INDEX);
241 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
242 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
251 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
252 mutt_format_s (dest, destlen, prefix,
253 mutt_addr_for_display (hdr->env->reply_to));
256 /* fall through if 'A' returns nothing */
259 if (hdr->env->from && hdr->env->from->mailbox) {
260 mutt_format_s (dest, destlen, prefix,
261 mutt_addr_for_display (hdr->env->from));
268 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
269 !first_mailing_list (dest, destlen, hdr->env->cc))
272 strfcpy (buf2, dest, sizeof (buf2));
273 mutt_format_s (dest, destlen, prefix, buf2);
276 /* fall through if 'B' returns nothing */
280 if ((p = strrchr (ctx->path, '/')))
281 strfcpy (dest, p + 1, destlen);
283 strfcpy (dest, ctx->path, destlen);
286 strfcpy (dest, "(null)", destlen);
287 strfcpy (buf2, dest, sizeof (buf2));
288 mutt_format_s (dest, destlen, prefix, buf2);
292 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
293 mutt_format_s (dest, destlen, prefix, buf2);
297 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
298 snprintf (dest, destlen, fmt, hdr->msgno + 1);
308 /* preprocess $date_format to handle %Z */
316 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
325 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
326 (op == '{' && *cp != '}') ||
327 (op == '[' && *cp != ']') ||
328 (op == '(' && *cp != ')') ||
329 (op == '<' && *cp != '>'))) {
332 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
334 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
335 hdr->zhours, hdr->zminutes);
340 break; /* not enough space left */
349 break; /* not enough space */
360 if (do_locales && Locale)
361 setlocale (LC_TIME, Locale);
363 if (op == '[' || op == 'D')
364 tm = localtime (&hdr->date_sent);
366 tm = localtime (&hdr->received);
367 else if (op == '<') {
372 /* restore sender's time zone */
375 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
377 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
381 strftime (buf2, sizeof (buf2), dest, tm);
384 setlocale (LC_TIME, "C");
386 mutt_format_s (dest, destlen, prefix, buf2);
387 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
393 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
394 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
399 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
400 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
402 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
408 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
409 mutt_format_s (dest, destlen, prefix, buf2);
414 make_from (hdr->env, buf2, sizeof (buf2), 0);
415 mutt_format_s (dest, destlen, prefix, buf2);
417 else if (mutt_addr_is_user (hdr->env->from))
423 mutt_format_s (dest, destlen, prefix,
424 hdr->env->newsgroups ? hdr->env->newsgroups : "");
429 /* (Hormel) spam score */
431 optional = hdr->env->spam ? 1 : 0;
434 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
436 mutt_format_s (dest, destlen, prefix, "");
441 mutt_format_s (dest, destlen, prefix,
442 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
450 for (i = 0; hdr->env->from && hdr->env->from->personal &&
451 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
452 if (isalpha ((int) hdr->env->from->personal[i])) {
454 buf2[j++] = hdr->env->from->personal[i];
464 mutt_format_s (dest, destlen, prefix, buf2);
469 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
470 snprintf (dest, destlen, fmt, (int) hdr->lines);
472 else if (hdr->lines <= 0)
478 make_from (hdr->env, buf2, sizeof (buf2), 1);
479 mutt_format_s (dest, destlen, prefix, buf2);
481 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
482 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
489 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
490 snprintf (dest, destlen, fmt, ctx->msgcount);
493 strfcpy (dest, "(null)", destlen);
497 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);
513 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
514 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
516 mutt_format_s (dest, destlen, prefix, buf2);
518 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
519 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
525 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
527 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
528 snprintf (dest, destlen, fmt, hdr->num_hidden);
529 else if (is_index && threads)
530 mutt_format_s (dest, destlen, prefix, " ");
535 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
542 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
543 if (flags & M_FORMAT_FORCESUBJ) {
544 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
545 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
546 mutt_format_s_tree (dest, destlen, prefix, buf2);
549 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
552 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
558 else if (hdr->attach_del)
560 else if (hdr->tagged)
562 else if (hdr->flagged)
564 else if (hdr->replied)
566 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
573 /* FOO - this is probably unsafe, but we are not likely to have such
574 a short string passed into this routine */
581 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
582 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
584 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
585 else if (hdr->env->cc)
586 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
588 mutt_format_s (dest, destlen, prefix, buf2);
592 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
593 snprintf (dest, destlen, fmt,
595 && ((i = mutt_user_is_recipient (hdr))) <
596 mutt_strlen (Tochars)) ? Tochars[i] : ' ');
600 if (hdr->env->from && hdr->env->from->mailbox) {
601 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
602 if ((p = strpbrk (buf2, "%@")))
607 mutt_format_s (dest, destlen, prefix, buf2);
611 if (mutt_addr_is_user (hdr->env->from)) {
613 mutt_format_s (buf2, sizeof (buf2), prefix,
614 mutt_get_name (hdr->env->to));
615 else if (hdr->env->cc)
616 mutt_format_s (buf2, sizeof (buf2), prefix,
617 mutt_get_name (hdr->env->cc));
622 mutt_format_s (buf2, sizeof (buf2), prefix,
623 mutt_get_name (hdr->env->from));
624 if ((p = strpbrk (buf2, " %@")))
626 mutt_format_s (dest, destlen, prefix, buf2);
631 mutt_format_s (dest, destlen, prefix,
632 hdr->env->organization ? hdr->env->organization : "");
633 else if (!hdr->env->organization)
641 if (WithCrypto && hdr->security & GOODSIGN)
643 else if (WithCrypto && hdr->security & ENCRYPT)
645 else if (WithCrypto && hdr->security & SIGN)
647 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
650 snprintf (buf2, sizeof (buf2),
651 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
654 && ctx->msgnotreadyet !=
657 replied ? 'r' : ' ') : (hdr->
661 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
662 hdr->tagged ? '*' : (hdr->
663 flagged ? '!' : (Tochars
666 mutt_user_is_recipient
668 mutt_strlen (Tochars)) ?
670 mutt_format_s (dest, destlen, prefix, buf2);
675 optional = hdr->env->x_label ? 1 : 0;
677 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
681 if (hdr->env->x_label) {
682 i = 1; /* reduce reuse recycle */
684 if (flags & M_FORMAT_TREE
685 && (hdr->thread->prev && hdr->thread->prev->message
686 && hdr->thread->prev->message->env->x_label))
687 htmp = hdr->thread->prev->message;
688 else if (flags & M_FORMAT_TREE
689 && (hdr->thread->parent && hdr->thread->parent->message
690 && hdr->thread->parent->message->env->x_label))
691 htmp = hdr->thread->parent->message;
692 if (htmp && mutt_strcasecmp (hdr->env->x_label,
693 htmp->env->x_label) == 0)
703 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
705 mutt_format_s (dest, destlen, prefix, "");
710 snprintf (dest, destlen, "%%%s%c", prefix, op);
715 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
716 (unsigned long) hfi, flags);
717 else if (flags & M_FORMAT_OPTIONAL)
718 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
719 (unsigned long) hfi, flags);
727 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
728 HEADER * hdr, format_flag flags)
730 struct hdr_format_info hfi;
735 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,