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.
20 #include "mutt_curses.h"
23 #include "mutt_crypt.h"
24 #include "mutt_idna.h"
31 int mutt_is_mail_list (ADDRESS *addr)
33 return mutt_match_rx_list (addr->mailbox, MailLists);
36 int mutt_is_subscribed_list (ADDRESS *addr)
38 return mutt_match_rx_list (addr->mailbox, SubscribedLists);
41 /* Search for a mailing list in the list of addresses pointed to by adr.
42 * If one is found, print pfx and the name of the list into buf, then
43 * return 1. Otherwise, simply return 0.
46 check_for_mailing_list (ADDRESS *adr, char *pfx, char *buf, int buflen)
48 for (; adr; adr = adr->next)
50 if (mutt_is_subscribed_list (adr))
52 if (pfx && buf && buflen)
53 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
60 /* Search for a mailing list in the list of addresses pointed to by adr.
61 * If one is found, print the address of the list into buf, then return 1.
62 * Otherwise, simply return 0.
65 check_for_mailing_list_addr (ADDRESS *adr, char *buf, int buflen)
67 for (; adr; adr = adr->next)
69 if (mutt_is_subscribed_list (adr))
72 snprintf (buf, buflen, "%s", adr->mailbox);
80 static int first_mailing_list (char *buf, size_t buflen, ADDRESS *a)
82 for (; a; a = a->next)
84 if (mutt_is_subscribed_list (a))
86 mutt_save_path (buf, buflen, a);
93 static void make_from (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
97 me = mutt_addr_is_user (hdr->from);
101 if (check_for_mailing_list (hdr->to, "To ", buf, len))
103 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
108 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
109 else if (me && hdr->cc)
110 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
112 strfcpy (buf, mutt_get_name (hdr->from), len);
117 static void make_from_addr (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
121 me = mutt_addr_is_user (hdr->from);
125 if (check_for_mailing_list_addr (hdr->to, buf, len))
127 if (check_for_mailing_list_addr (hdr->cc, buf, len))
132 snprintf (buf, len, "%s", hdr->to->mailbox);
133 else if (me && hdr->cc)
134 snprintf (buf, len, "%s", hdr->cc->mailbox);
136 strfcpy (buf, hdr->from->mailbox, len);
141 static int user_in_addr (ADDRESS *a)
143 for (; a; a = a->next)
144 if (mutt_addr_is_user (a))
150 * 0: user is not in list
151 * 1: user is unique recipient
152 * 2: user is in the TO list
153 * 3: user is in the CC list
154 * 4: user is originator
155 * 5: sent to a subscribed mailinglist
157 int mutt_user_is_recipient (HEADER *h)
159 ENVELOPE *env = h->env;
165 if (mutt_addr_is_user (env->from))
167 else if (user_in_addr (env->to))
169 if (env->to->next || env->cc)
170 h->recipient = 2; /* non-unique recipient */
172 h->recipient = 1; /* unique recipient */
174 else if (user_in_addr (env->cc))
176 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
178 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
187 /* %a = address of author
188 * %A = reply-to address (if present; otherwise: address of author
189 * %b = filename of the originating folder
190 * %B = the list to which the letter was sent
191 * %c = size of message in bytes
192 * %C = current message number
193 * %d = date and time of message using $date_format and sender's timezone
194 * %D = date and time of message using $date_format and local timezone
195 * %e = current message number in thread
196 * %E = number of messages in current thread
197 * %f = entire from line
198 * %F = like %n, unless from self
199 * %g = newsgroup name (if compiled with nntp support)
201 * %I = initials of author
202 * %l = number of lines in the message
203 * %L = like %F, except `lists' are displayed first
204 * %m = number of messages in the mailbox
205 * %n = name of author
207 * %O = like %L, except using address instead of name
209 * %S = short message status (e.g., N/O/D/!/r/-)
210 * %t = `to:' field (recipients)
212 * %u = user (login) name of author
213 * %v = first name of author, unless from self
214 * %W = where user is (organization)
215 * %y = `x-label:' field (if present)
216 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
217 * %Z = status flags */
219 struct hdr_format_info
226 hdr_format_str (char *dest,
231 const char *ifstring,
232 const char *elsestring,
236 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
239 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
241 int optional = (flags & M_FORMAT_OPTIONAL);
242 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
243 int is_index = (flags & M_FORMAT_INDEX);
244 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
245 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
255 if(hdr->env->reply_to && hdr->env->reply_to->mailbox)
257 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->reply_to));
260 /* fall through if 'A' returns nothing */
263 if(hdr->env->from && hdr->env->from->mailbox)
265 mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->from));
272 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
273 !first_mailing_list (dest, destlen, hdr->env->cc))
277 strfcpy (buf2, dest, sizeof(buf2));
278 mutt_format_s (dest, destlen, prefix, buf2);
281 /* fall through if 'B' returns nothing */
286 if ((p = strrchr (ctx->path, '/')))
287 strfcpy (dest, p + 1, destlen);
289 strfcpy (dest, ctx->path, destlen);
292 strfcpy(dest, "(null)", destlen);
293 strfcpy (buf2, dest, sizeof(buf2));
294 mutt_format_s (dest, destlen, prefix, buf2);
298 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
299 mutt_format_s (dest, destlen, prefix, buf2);
303 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
304 snprintf (dest, destlen, fmt, hdr->msgno + 1);
314 /* preprocess $date_format to handle %Z */
322 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
332 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
333 (op == '{' && *cp != '}') ||
334 (op == '[' && *cp != ']') ||
335 (op == '(' && *cp != ')') ||
336 (op == '<' && *cp != '>')))
341 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{'))
345 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
346 hdr->zhours, hdr->zminutes);
351 break; /* not enough space left */
362 break; /* not enough space */
374 if (do_locales && Locale)
375 setlocale (LC_TIME, Locale);
377 if (op == '[' || op == 'D')
378 tm = localtime (&hdr->date_sent);
380 tm = localtime (&hdr->received);
388 /* restore sender's time zone */
391 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
393 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
397 strftime (buf2, sizeof (buf2), dest, tm);
400 setlocale (LC_TIME, "C");
402 mutt_format_s (dest, destlen, prefix, buf2);
403 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
409 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
410 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 1));
416 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
417 snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 0));
419 else if (mutt_messages_in_thread(ctx, hdr, 0) <= 1)
425 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
426 mutt_format_s (dest, destlen, prefix, buf2);
432 make_from (hdr->env, buf2, sizeof (buf2), 0);
433 mutt_format_s (dest, destlen, prefix, buf2);
435 else if (mutt_addr_is_user (hdr->env->from))
441 mutt_format_s (dest, destlen, prefix, hdr->env->newsgroups ? hdr->env->newsgroups : "");
446 /* (Hormel) spam score */
448 optional = hdr->env->spam ? 1 : 0;
451 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
453 mutt_format_s (dest, destlen, prefix, "");
458 mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
466 for (i = 0; hdr->env->from && hdr->env->from->personal &&
467 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++)
469 if (isalpha ((int)hdr->env->from->personal[i]))
473 buf2[j++] = hdr->env->from->personal[i];
483 mutt_format_s (dest, destlen, prefix, buf2);
489 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
490 snprintf (dest, destlen, fmt, (int) hdr->lines);
492 else if (hdr->lines <= 0)
499 make_from (hdr->env, buf2, sizeof (buf2), 1);
500 mutt_format_s (dest, destlen, prefix, buf2);
502 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
503 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0))
512 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
513 snprintf (dest, destlen, fmt, ctx->msgcount);
516 strfcpy(dest, "(null)", destlen);
520 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
526 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
527 snprintf (dest, destlen, fmt, hdr->score);
539 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
540 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
542 mutt_format_s (dest, destlen, prefix, buf2);
544 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
545 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0))
552 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
555 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
556 snprintf (dest, destlen, fmt, hdr->num_hidden);
557 else if (is_index && threads)
558 mutt_format_s (dest, destlen, prefix, " ");
564 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
571 if (flags & M_FORMAT_TREE && !hdr->collapsed)
573 if (flags & M_FORMAT_FORCESUBJ)
575 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
576 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
577 mutt_format_s_tree (dest, destlen, prefix, buf2);
580 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
583 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
589 else if (hdr->attach_del)
591 else if (hdr->tagged)
593 else if (hdr->flagged)
595 else if (hdr->replied)
597 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
604 /* FOO - this is probably unsafe, but we are not likely to have such
605 a short string passed into this routine */
612 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
613 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2)))
616 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
617 else if (hdr->env->cc)
618 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
620 mutt_format_s (dest, destlen, prefix, buf2);
624 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
625 snprintf (dest, destlen, fmt,
626 (Tochars && ((i = mutt_user_is_recipient (hdr))) < mutt_strlen (Tochars)) ? Tochars[i] : ' ');
630 if (hdr->env->from && hdr->env->from->mailbox)
632 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
633 if ((p = strpbrk (buf2, "%@")))
638 mutt_format_s (dest, destlen, prefix, buf2);
642 if (mutt_addr_is_user (hdr->env->from))
645 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->to));
646 else if (hdr->env->cc)
647 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->cc));
652 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->from));
653 if ((p = strpbrk (buf2, " %@")))
655 mutt_format_s (dest, destlen, prefix, buf2);
660 mutt_format_s (dest, destlen, prefix, hdr->env->organization ? hdr->env->organization : "");
661 else if (!hdr->env->organization)
669 if (WithCrypto && hdr->security & GOODSIGN)
671 else if (WithCrypto && hdr->security & ENCRYPT)
673 else if (WithCrypto && hdr->security & SIGN)
675 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
678 snprintf (buf2, sizeof (buf2),
679 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
680 ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
681 ? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
682 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
684 (hdr->flagged ? '!' :
685 (Tochars && ((i = mutt_user_is_recipient (hdr)) < mutt_strlen (Tochars)) ? Tochars[i] : ' ')));
686 mutt_format_s (dest, destlen, prefix, buf2);
691 optional = hdr->env->x_label ? 1 : 0;
693 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
697 if (hdr->env->x_label)
699 i = 1; /* reduce reuse recycle */
701 if (flags & M_FORMAT_TREE
702 && (hdr->thread->prev && hdr->thread->prev->message
703 && hdr->thread->prev->message->env->x_label))
704 htmp = hdr->thread->prev->message;
705 else if (flags & M_FORMAT_TREE
706 && (hdr->thread->parent && hdr->thread->parent->message
707 && hdr->thread->parent->message->env->x_label))
708 htmp = hdr->thread->parent->message;
709 if (htmp && mutt_strcasecmp (hdr->env->x_label,
710 htmp->env->x_label) == 0)
720 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
722 mutt_format_s (dest, destlen, prefix, "");
727 snprintf (dest, destlen, "%%%s%c", prefix, op);
732 mutt_FormatString (dest, destlen, ifstring, hdr_format_str, (unsigned long) hfi, flags);
733 else if (flags & M_FORMAT_OPTIONAL)
734 mutt_FormatString (dest, destlen, elsestring, hdr_format_str, (unsigned long) hfi, flags);
742 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT *ctx, HEADER *hdr, format_flag flags)
744 struct hdr_format_info hfi;
749 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi, flags);