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 mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
454 for (i = 0; hdr->env->from && hdr->env->from->personal &&
455 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++)
457 if (isalpha ((int)hdr->env->from->personal[i]))
461 buf2[j++] = hdr->env->from->personal[i];
471 mutt_format_s (dest, destlen, prefix, buf2);
477 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
478 snprintf (dest, destlen, fmt, (int) hdr->lines);
480 else if (hdr->lines <= 0)
487 make_from (hdr->env, buf2, sizeof (buf2), 1);
488 mutt_format_s (dest, destlen, prefix, buf2);
490 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
491 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0))
500 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
501 snprintf (dest, destlen, fmt, ctx->msgcount);
504 strfcpy(dest, "(null)", destlen);
508 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
514 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
515 snprintf (dest, destlen, fmt, hdr->score);
527 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
528 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
530 mutt_format_s (dest, destlen, prefix, buf2);
532 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
533 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0))
540 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
543 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
544 snprintf (dest, destlen, fmt, hdr->num_hidden);
545 else if (is_index && threads)
546 mutt_format_s (dest, destlen, prefix, " ");
552 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
559 if (flags & M_FORMAT_TREE && !hdr->collapsed)
561 if (flags & M_FORMAT_FORCESUBJ)
563 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
564 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
565 mutt_format_s_tree (dest, destlen, prefix, buf2);
568 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
571 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
577 else if (hdr->attach_del)
579 else if (hdr->tagged)
581 else if (hdr->flagged)
583 else if (hdr->replied)
585 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
592 /* FOO - this is probably unsafe, but we are not likely to have such
593 a short string passed into this routine */
600 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
601 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2)))
604 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
605 else if (hdr->env->cc)
606 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
608 mutt_format_s (dest, destlen, prefix, buf2);
612 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
613 snprintf (dest, destlen, fmt,
614 (Tochars && ((i = mutt_user_is_recipient (hdr))) < mutt_strlen (Tochars)) ? Tochars[i] : ' ');
618 if (hdr->env->from && hdr->env->from->mailbox)
620 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
621 if ((p = strpbrk (buf2, "%@")))
626 mutt_format_s (dest, destlen, prefix, buf2);
630 if (mutt_addr_is_user (hdr->env->from))
633 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->to));
634 else if (hdr->env->cc)
635 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->cc));
640 mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->from));
641 if ((p = strpbrk (buf2, " %@")))
643 mutt_format_s (dest, destlen, prefix, buf2);
648 mutt_format_s (dest, destlen, prefix, hdr->env->organization ? hdr->env->organization : "");
649 else if (!hdr->env->organization)
657 if (WithCrypto && hdr->security & GOODSIGN)
659 else if (WithCrypto && hdr->security & ENCRYPT)
661 else if (WithCrypto && hdr->security & SIGN)
663 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
666 snprintf (buf2, sizeof (buf2),
667 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
668 ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
669 ? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
670 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
672 (hdr->flagged ? '!' :
673 (Tochars && ((i = mutt_user_is_recipient (hdr)) < mutt_strlen (Tochars)) ? Tochars[i] : ' ')));
674 mutt_format_s (dest, destlen, prefix, buf2);
679 optional = hdr->env->x_label ? 1 : 0;
681 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
685 if (hdr->env->x_label)
687 i = 1; /* reduce reuse recycle */
689 if (flags & M_FORMAT_TREE
690 && (hdr->thread->prev && hdr->thread->prev->message
691 && hdr->thread->prev->message->env->x_label))
692 htmp = hdr->thread->prev->message;
693 else if (flags & M_FORMAT_TREE
694 && (hdr->thread->parent && hdr->thread->parent->message
695 && hdr->thread->parent->message->env->x_label))
696 htmp = hdr->thread->parent->message;
697 if (htmp && mutt_strcasecmp (hdr->env->x_label,
698 htmp->env->x_label) == 0)
708 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
710 mutt_format_s (dest, destlen, prefix, "");
715 snprintf (dest, destlen, "%%%s%c", prefix, op);
720 mutt_FormatString (dest, destlen, ifstring, hdr_format_str, (unsigned long) hfi, flags);
721 else if (flags & M_FORMAT_OPTIONAL)
722 mutt_FormatString (dest, destlen, elsestring, hdr_format_str, (unsigned long) hfi, flags);
730 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT *ctx, HEADER *hdr, format_flag flags)
732 struct hdr_format_info hfi;
737 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi, flags);