2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
18 #include "mutt_crypt.h"
19 #include "mutt_idna.h"
28 int mutt_is_mail_list (ADDRESS * addr)
30 if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
31 return mutt_match_rx_list (addr->mailbox, MailLists);
35 int mutt_is_subscribed_list (ADDRESS * addr)
37 if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
38 && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
39 return mutt_match_rx_list (addr->mailbox, SubscribedLists);
43 /* Search for a mailing list in the list of addresses pointed to by adr.
44 * If one is found, print pfx and the name of the list into buf, then
45 * return 1. Otherwise, simply return 0.
48 check_for_mailing_list (ADDRESS * adr, char *pfx, char *buf, int buflen)
50 for (; adr; adr = adr->next) {
51 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.
64 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
66 for (; adr; adr = adr->next) {
67 if (mutt_is_subscribed_list (adr)) {
69 snprintf (buf, buflen, "%s", adr->mailbox);
77 static int first_mailing_list (char *buf, size_t buflen, ADDRESS * a)
79 for (; a; a = a->next) {
80 if (mutt_is_subscribed_list (a)) {
81 mutt_save_path (buf, buflen, a);
88 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
92 me = mutt_addr_is_user (hdr->from);
95 if (check_for_mailing_list (hdr->to, "To ", buf, len))
97 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
102 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
103 else if (me && hdr->cc)
104 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
106 strfcpy (buf, mutt_get_name (hdr->from), len);
111 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
116 me = mutt_addr_is_user (hdr->from);
118 if (do_lists || me) {
119 if (check_for_mailing_list_addr (hdr->to, buf, len))
121 if (check_for_mailing_list_addr (hdr->cc, buf, len))
126 snprintf (buf, len, "%s", hdr->to->mailbox);
127 else if (me && hdr->cc)
128 snprintf (buf, len, "%s", hdr->cc->mailbox);
130 strfcpy (buf, hdr->from->mailbox, len);
135 static int user_in_addr (ADDRESS * a)
137 for (; a; a = a->next)
138 if (mutt_addr_is_user (a))
144 * 0: user is not in list
145 * 1: user is unique recipient
146 * 2: user is in the TO list
147 * 3: user is in the CC list
148 * 4: user is originator
149 * 5: sent to a subscribed mailinglist
151 int mutt_user_is_recipient (HEADER * h)
153 ENVELOPE *env = h->env;
155 if (!h->recip_valid) {
158 if (mutt_addr_is_user (env->from))
160 else if (user_in_addr (env->to)) {
161 if (env->to->next || env->cc)
162 h->recipient = 2; /* non-unique recipient */
164 h->recipient = 1; /* unique recipient */
166 else if (user_in_addr (env->cc))
168 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
170 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
179 /* %a = address of author
180 * %A = reply-to address (if present; otherwise: address of author
181 * %b = filename of the originating folder
182 * %B = the list to which the letter was sent
183 * %c = size of message in bytes
184 * %C = current message number
185 * %d = date and time of message using $date_format and sender's timezone
186 * %D = date and time of message using $date_format and local timezone
187 * %e = current message number in thread
188 * %E = number of messages in current thread
189 * %f = entire from line
190 * %F = like %n, unless from self
191 * %g = newsgroup name (if compiled with nntp support)
193 * %I = initials of author
194 * %l = number of lines in the message
195 * %L = like %F, except `lists' are displayed first
196 * %m = number of messages in the mailbox
197 * %n = name of author
199 * %O = like %L, except using address instead of name
201 * %S = short message status (e.g., N/O/D/!/r/-)
202 * %t = `to:' field (recipients)
204 * %u = user (login) name of author
205 * %v = first name of author, unless from self
206 * %W = where user is (organization)
207 * %y = `x-label:' field (if present)
208 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
209 * %Z = status flags */
211 struct hdr_format_info {
216 static const char *hdr_format_str (char *dest,
221 const char *ifstring,
222 const char *elsestring,
223 unsigned long data, format_flag flags)
225 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
228 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
230 int optional = (flags & M_FORMAT_OPTIONAL);
231 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
232 int is_index = (flags & M_FORMAT_INDEX);
234 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
235 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
244 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
245 mutt_format_s (dest, destlen, prefix,
246 mutt_addr_for_display (hdr->env->reply_to));
249 /* fall through if 'A' returns nothing */
252 if (hdr->env->from && hdr->env->from->mailbox) {
253 mutt_format_s (dest, destlen, prefix,
254 mutt_addr_for_display (hdr->env->from));
261 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
262 !first_mailing_list (dest, destlen, hdr->env->cc))
265 strfcpy (buf2, dest, sizeof (buf2));
266 mutt_format_s (dest, destlen, prefix, buf2);
269 /* fall through if 'B' returns nothing */
273 if ((p = strrchr (ctx->path, '/')))
274 strfcpy (dest, p + 1, destlen);
276 strfcpy (dest, ctx->path, destlen);
279 strfcpy (dest, "(null)", destlen);
280 strfcpy (buf2, dest, sizeof (buf2));
281 mutt_format_s (dest, destlen, prefix, buf2);
285 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
286 mutt_format_s (dest, destlen, prefix, buf2);
290 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
291 snprintf (dest, destlen, fmt, hdr->msgno + 1);
301 /* preprocess $date_format to handle %Z */
309 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
318 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
319 (op == '{' && *cp != '}') ||
320 (op == '[' && *cp != ']') ||
321 (op == '(' && *cp != ')') ||
322 (op == '<' && *cp != '>'))) {
325 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
327 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
328 hdr->zhours, hdr->zminutes);
333 break; /* not enough space left */
342 break; /* not enough space */
353 if (do_locales && Locale)
354 setlocale (LC_TIME, Locale);
356 if (op == '[' || op == 'D')
357 tm = localtime (&hdr->date_sent);
359 tm = localtime (&hdr->received);
360 else if (op == '<') {
365 /* restore sender's time zone */
368 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
370 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
374 strftime (buf2, sizeof (buf2), dest, tm);
377 setlocale (LC_TIME, "C");
379 mutt_format_s (dest, destlen, prefix, buf2);
380 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
386 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
387 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
392 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
393 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
395 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
401 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
402 mutt_format_s (dest, destlen, prefix, buf2);
407 make_from (hdr->env, buf2, sizeof (buf2), 0);
408 mutt_format_s (dest, destlen, prefix, buf2);
410 else if (mutt_addr_is_user (hdr->env->from))
416 mutt_format_s (dest, destlen, prefix,
417 hdr->env->newsgroups ? hdr->env->newsgroups : "");
422 /* (Hormel) spam score */
424 optional = hdr->env->spam ? 1 : 0;
427 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
429 mutt_format_s (dest, destlen, prefix, "");
434 mutt_format_s (dest, destlen, prefix,
435 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
443 for (i = 0; hdr->env->from && hdr->env->from->personal &&
444 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
445 if (isalpha ((int) hdr->env->from->personal[i])) {
447 buf2[j++] = hdr->env->from->personal[i];
457 mutt_format_s (dest, destlen, prefix, buf2);
462 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
463 snprintf (dest, destlen, fmt, (int) hdr->lines);
465 else if (hdr->lines <= 0)
471 make_from (hdr->env, buf2, sizeof (buf2), 1);
472 mutt_format_s (dest, destlen, prefix, buf2);
474 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
475 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
482 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
483 snprintf (dest, destlen, fmt, ctx->msgcount);
486 strfcpy (dest, "(null)", destlen);
490 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
495 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
496 snprintf (dest, destlen, fmt, hdr->score);
506 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
507 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
509 mutt_format_s (dest, destlen, prefix, buf2);
511 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
512 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
518 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
520 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
521 snprintf (dest, destlen, fmt, hdr->num_hidden);
522 else if (is_index && threads)
523 mutt_format_s (dest, destlen, prefix, " ");
528 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
535 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
536 if (flags & M_FORMAT_FORCESUBJ) {
537 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
538 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
539 mutt_format_s_tree (dest, destlen, prefix, buf2);
542 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
545 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
551 else if (hdr->attach_del)
553 else if (hdr->tagged)
555 else if (hdr->flagged)
557 else if (hdr->replied)
559 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
566 /* FOO - this is probably unsafe, but we are not likely to have such
567 a short string passed into this routine */
574 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
575 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
577 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
578 else if (hdr->env->cc)
579 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
581 mutt_format_s (dest, destlen, prefix, buf2);
585 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
586 snprintf (dest, destlen, fmt,
588 && ((i = mutt_user_is_recipient (hdr))) <
589 mutt_strlen (Tochars)) ? Tochars[i] : ' ');
593 if (hdr->env->from && hdr->env->from->mailbox) {
594 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
595 if ((p = strpbrk (buf2, "%@")))
600 mutt_format_s (dest, destlen, prefix, buf2);
604 if (mutt_addr_is_user (hdr->env->from)) {
606 mutt_format_s (buf2, sizeof (buf2), prefix,
607 mutt_get_name (hdr->env->to));
608 else if (hdr->env->cc)
609 mutt_format_s (buf2, sizeof (buf2), prefix,
610 mutt_get_name (hdr->env->cc));
615 mutt_format_s (buf2, sizeof (buf2), prefix,
616 mutt_get_name (hdr->env->from));
617 if ((p = strpbrk (buf2, " %@")))
619 mutt_format_s (dest, destlen, prefix, buf2);
624 mutt_format_s (dest, destlen, prefix,
625 hdr->env->organization ? hdr->env->organization : "");
626 else if (!hdr->env->organization)
634 if (WithCrypto && hdr->security & GOODSIGN)
636 else if (WithCrypto && hdr->security & ENCRYPT)
638 else if (WithCrypto && hdr->security & SIGN)
640 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
643 snprintf (buf2, sizeof (buf2),
644 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
647 && ctx->msgnotreadyet !=
650 replied ? 'r' : ' ') : (hdr->
654 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
655 hdr->tagged ? '*' : (hdr->
656 flagged ? '!' : (Tochars
659 mutt_user_is_recipient
661 mutt_strlen (Tochars)) ?
663 mutt_format_s (dest, destlen, prefix, buf2);
668 optional = hdr->env->x_label ? 1 : 0;
670 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
674 if (hdr->env->x_label) {
675 i = 1; /* reduce reuse recycle */
677 if (flags & M_FORMAT_TREE
678 && (hdr->thread->prev && hdr->thread->prev->message
679 && hdr->thread->prev->message->env->x_label))
680 htmp = hdr->thread->prev->message;
681 else if (flags & M_FORMAT_TREE
682 && (hdr->thread->parent && hdr->thread->parent->message
683 && hdr->thread->parent->message->env->x_label))
684 htmp = hdr->thread->parent->message;
685 if (htmp && mutt_strcasecmp (hdr->env->x_label,
686 htmp->env->x_label) == 0)
696 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
698 mutt_format_s (dest, destlen, prefix, "");
703 snprintf (dest, destlen, "%%%s%c", prefix, op);
708 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
709 (unsigned long) hfi, flags);
710 else if (flags & M_FORMAT_OPTIONAL)
711 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
712 (unsigned long) hfi, flags);
720 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
721 HEADER * hdr, format_flag flags)
723 struct hdr_format_info hfi;
728 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,