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)
59 if (mutt_is_subscribed_list (adr))
61 if (pfx && buf && buflen)
62 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
69 /* Search for a mailing list in the list of addresses pointed to by adr.
70 * If one is found, print the address of the list into buf, then return 1.
71 * Otherwise, simply return 0.
74 check_for_mailing_list_addr (ADDRESS *adr, char *buf, int buflen)
76 for (; adr; adr = adr->next)
78 if (mutt_is_subscribed_list (adr))
81 snprintf (buf, buflen, "%s", adr->mailbox);
89 static int first_mailing_list (char *buf, size_t buflen, ADDRESS *a)
91 for (; a; a = a->next)
93 if (mutt_is_subscribed_list (a))
95 mutt_save_path (buf, buflen, a);
102 static void make_from (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
106 me = mutt_addr_is_user (hdr->from);
110 if (check_for_mailing_list (hdr->to, "To ", buf, len))
112 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
117 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
118 else if (me && hdr->cc)
119 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
121 strfcpy (buf, mutt_get_name (hdr->from), len);
126 static void make_from_addr (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
130 me = mutt_addr_is_user (hdr->from);
134 if (check_for_mailing_list_addr (hdr->to, buf, len))
136 if (check_for_mailing_list_addr (hdr->cc, buf, len))
141 snprintf (buf, len, "%s", hdr->to->mailbox);
142 else if (me && hdr->cc)
143 snprintf (buf, len, "%s", hdr->cc->mailbox);
145 strfcpy (buf, hdr->from->mailbox, len);
150 static int user_in_addr (ADDRESS *a)
152 for (; a; a = a->next)
153 if (mutt_addr_is_user (a))
159 * 0: user is not in list
160 * 1: user is unique recipient
161 * 2: user is in the TO list
162 * 3: user is in the CC list
163 * 4: user is originator
164 * 5: sent to a subscribed mailinglist
166 int mutt_user_is_recipient (HEADER *h)
168 ENVELOPE *env = h->env;
174 if (mutt_addr_is_user (env->from))
176 else if (user_in_addr (env->to))
178 if (env->to->next || env->cc)
179 h->recipient = 2; /* non-unique recipient */
181 h->recipient = 1; /* unique recipient */
183 else if (user_in_addr (env->cc))
185 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
187 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
196 /* %a = address of author
197 * %A = reply-to address (if present; otherwise: address of author
198 * %b = filename of the originating folder
199 * %B = the list to which the letter was sent
200 * %c = size of message in bytes
201 * %C = current message number
202 * %d = date and time of message using $date_format and sender's timezone
203 * %D = date and time of message using $date_format and local timezone
204 * %e = current message number in thread
205 * %E = number of messages in current thread
206 * %f = entire from line
207 * %F = like %n, unless from self
208 * %g = newsgroup name (if compiled with nntp support)
210 * %I = initials of author
211 * %l = number of lines in the message
212 * %L = like %F, except `lists' are displayed first
213 * %m = number of messages in the mailbox
214 * %n = name of author
216 * %O = like %L, except using address instead of name
218 * %S = short message status (e.g., N/O/D/!/r/-)
219 * %t = `to:' field (recipients)
221 * %u = user (login) name of author
222 * %v = first name of author, unless from self
223 * %W = where user is (organization)
224 * %y = `x-label:' field (if present)
225 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
226 * %Z = status flags */
228 struct hdr_format_info
235 hdr_format_str (char *dest,
240 const char *ifstring,
241 const char *elsestring,
245 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
248 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
250 int optional = (flags & M_FORMAT_OPTIONAL);
251 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
252 int is_index = (flags & M_FORMAT_INDEX);
253 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
254 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
264 if(hdr->env->reply_to && hdr->env->reply_to->mailbox)
266 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->reply_to));
269 /* fall through if 'A' returns nothing */
272 if(hdr->env->from && hdr->env->from->mailbox)
274 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->from));
281 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
282 !first_mailing_list (dest, destlen, hdr->env->cc))
286 strfcpy (buf2, dest, sizeof(buf2));
287 mutt_format_s (dest, destlen, prefix, buf2);
290 /* fall through if 'B' returns nothing */
295 if ((p = strrchr (ctx->path, '/')))
296 strfcpy (dest, p + 1, destlen);
298 strfcpy (dest, ctx->path, destlen);
301 strfcpy(dest, "(null)", destlen);
302 strfcpy (buf2, dest, sizeof(buf2));
303 mutt_format_s (dest, destlen, prefix, buf2);
307 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
308 mutt_format_s (dest, destlen, prefix, buf2);
312 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
313 snprintf (dest, destlen, fmt, hdr->msgno + 1);
323 /* preprocess $date_format to handle %Z */
331 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
341 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
342 (op == '{' && *cp != '}') ||
343 (op == '[' && *cp != ']') ||
344 (op == '(' && *cp != ')') ||
345 (op == '<' && *cp != '>')))
350 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{'))
354 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
355 hdr->zhours, hdr->zminutes);
360 break; /* not enough space left */
371 break; /* not enough space */
383 if (do_locales && Locale)
384 setlocale (LC_TIME, Locale);
386 if (op == '[' || op == 'D')
387 tm = localtime (&hdr->date_sent);
389 tm = localtime (&hdr->received);
397 /* restore sender's time zone */
400 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
402 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
406 strftime (buf2, sizeof (buf2), dest, tm);
409 setlocale (LC_TIME, "C");
411 mutt_format_s (dest, destlen, prefix, buf2);
412 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
418 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
419 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 1));
425 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
426 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 0));
428 else if (mutt_messages_in_thread(ctx, hdr, 0) <= 1)
434 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
435 mutt_format_s (dest, destlen, prefix, buf2);
441 make_from (hdr->env, buf2, sizeof (buf2), 0);
442 mutt_format_s (dest, destlen, prefix, buf2);
444 else if (mutt_addr_is_user (hdr->env->from))
450 mutt_format_s (dest, destlen, prefix, hdr->env->newsgroups ? hdr->env->newsgroups : "");
455 /* (Hormel) spam score */
457 optional = hdr->env->spam ? 1 : 0;
460 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
462 mutt_format_s (dest, destlen, prefix, "");
467 mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
475 for (i = 0; hdr->env->from && hdr->env->from->personal &&
476 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++)
478 if (isalpha ((int)hdr->env->from->personal[i]))
482 buf2[j++] = hdr->env->from->personal[i];
492 mutt_format_s (dest, destlen, prefix, buf2);
498 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
499 snprintf (dest, destlen, fmt, (int) hdr->lines);
501 else if (hdr->lines <= 0)
508 make_from (hdr->env, buf2, sizeof (buf2), 1);
509 mutt_format_s (dest, destlen, prefix, buf2);
511 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
512 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0))
521 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
522 snprintf (dest, destlen, fmt, ctx->msgcount);
525 strfcpy(dest, "(null)", destlen);
529 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
535 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
536 snprintf (dest, destlen, fmt, hdr->score);
548 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
549 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
551 mutt_format_s (dest, destlen, prefix, buf2);
553 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
554 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0))
561 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
564 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
565 snprintf (dest, destlen, fmt, hdr->num_hidden);
566 else if (is_index && threads)
567 mutt_format_s (dest, destlen, prefix, " ");
573 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
580 if (flags & M_FORMAT_TREE && !hdr->collapsed)
582 if (flags & M_FORMAT_FORCESUBJ)
584 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
585 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
586 mutt_format_s_tree (dest, destlen, prefix, buf2);
589 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
592 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
598 else if (hdr->attach_del)
600 else if (hdr->tagged)
602 else if (hdr->flagged)
604 else if (hdr->replied)
606 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
613 /* FOO - this is probably unsafe, but we are not likely to have such
614 a short string passed into this routine */
621 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
622 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2)))
625 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
626 else if (hdr->env->cc)
627 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
629 mutt_format_s (dest, destlen, prefix, buf2);
633 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
634 snprintf (dest, destlen, fmt,
635 (Tochars && ((i = mutt_user_is_recipient (hdr))) < mutt_strlen (Tochars)) ? Tochars[i] : ' ');
639 if (hdr->env->from && hdr->env->from->mailbox)
641 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
642 if ((p = strpbrk (buf2, "%@")))
647 mutt_format_s (dest, destlen, prefix, buf2);
651 if (mutt_addr_is_user (hdr->env->from))
654 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->to));
655 else if (hdr->env->cc)
656 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->cc));
661 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->from));
662 if ((p = strpbrk (buf2, " %@")))
664 mutt_format_s (dest, destlen, prefix, buf2);
669 mutt_format_s (dest, destlen, prefix, hdr->env->organization ? hdr->env->organization : "");
670 else if (!hdr->env->organization)
678 if (WithCrypto && hdr->security & GOODSIGN)
680 else if (WithCrypto && hdr->security & ENCRYPT)
682 else if (WithCrypto && hdr->security & SIGN)
684 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
687 snprintf (buf2, sizeof (buf2),
688 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
689 ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
690 ? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
691 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
693 (hdr->flagged ? '!' :
694 (Tochars && ((i = mutt_user_is_recipient (hdr)) < mutt_strlen (Tochars)) ? Tochars[i] : ' ')));
695 mutt_format_s (dest, destlen, prefix, buf2);
700 optional = hdr->env->x_label ? 1 : 0;
702 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
706 if (hdr->env->x_label)
708 i = 1; /* reduce reuse recycle */
710 if (flags & M_FORMAT_TREE
711 && (hdr->thread->prev && hdr->thread->prev->message
712 && hdr->thread->prev->message->env->x_label))
713 htmp = hdr->thread->prev->message;
714 else if (flags & M_FORMAT_TREE
715 && (hdr->thread->parent && hdr->thread->parent->message
716 && hdr->thread->parent->message->env->x_label))
717 htmp = hdr->thread->parent->message;
718 if (htmp && mutt_strcasecmp (hdr->env->x_label,
719 htmp->env->x_label) == 0)
729 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
731 mutt_format_s (dest, destlen, prefix, "");
736 snprintf (dest, destlen, "%%%s%c", prefix, op);
741 mutt_FormatString (dest, destlen, ifstring, hdr_format_str, (unsigned long) hfi, flags);
742 else if (flags & M_FORMAT_OPTIONAL)
743 mutt_FormatString (dest, destlen, elsestring, hdr_format_str, (unsigned long) hfi, flags);
751 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT *ctx, HEADER *hdr, format_flag flags)
753 struct hdr_format_info hfi;
758 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi, flags);