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"
26 int mutt_is_mail_list (ADDRESS * addr)
28 if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
29 return mutt_match_rx_list (addr->mailbox, MailLists);
33 int mutt_is_subscribed_list (ADDRESS * addr)
35 if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
36 && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
37 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) {
49 if (mutt_is_subscribed_list (adr)) {
50 if (pfx && buf && buflen)
51 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
58 /* Search for a mailing list in the list of addresses pointed to by adr.
59 * If one is found, print the address of the list into buf, then return 1.
60 * Otherwise, simply return 0.
62 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
64 for (; adr; adr = adr->next) {
65 if (mutt_is_subscribed_list (adr)) {
67 snprintf (buf, buflen, "%s", adr->mailbox);
75 static int first_mailing_list (char *buf, size_t buflen, ADDRESS * a)
77 for (; a; a = a->next) {
78 if (mutt_is_subscribed_list (a)) {
79 mutt_save_path (buf, buflen, a);
86 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
90 me = mutt_addr_is_user (hdr->from);
93 if (check_for_mailing_list (hdr->to, "To ", buf, len))
95 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
100 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
101 else if (me && hdr->cc)
102 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
104 strfcpy (buf, mutt_get_name (hdr->from), len);
109 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
114 me = mutt_addr_is_user (hdr->from);
116 if (do_lists || me) {
117 if (check_for_mailing_list_addr (hdr->to, buf, len))
119 if (check_for_mailing_list_addr (hdr->cc, buf, len))
124 snprintf (buf, len, "%s", hdr->to->mailbox);
125 else if (me && hdr->cc)
126 snprintf (buf, len, "%s", hdr->cc->mailbox);
128 strfcpy (buf, hdr->from->mailbox, len);
133 static int user_in_addr (ADDRESS * a)
135 for (; a; a = a->next)
136 if (mutt_addr_is_user (a))
142 * 0: user is not in list
143 * 1: user is unique recipient
144 * 2: user is in the TO list
145 * 3: user is in the CC list
146 * 4: user is originator
147 * 5: sent to a subscribed mailinglist
149 int mutt_user_is_recipient (HEADER * h)
151 ENVELOPE *env = h->env;
153 if (!h->recip_valid) {
156 if (mutt_addr_is_user (env->from))
158 else if (user_in_addr (env->to)) {
159 if (env->to->next || env->cc)
160 h->recipient = 2; /* non-unique recipient */
162 h->recipient = 1; /* unique recipient */
164 else if (user_in_addr (env->cc))
166 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
168 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
177 /* %a = address of author
178 * %A = reply-to address (if present; otherwise: address of author
179 * %b = filename of the originating folder
180 * %B = the list to which the letter was sent
181 * %c = size of message in bytes
182 * %C = current message number
183 * %d = date and time of message using $date_format and sender's timezone
184 * %D = date and time of message using $date_format and local timezone
185 * %e = current message number in thread
186 * %E = number of messages in current thread
187 * %f = entire from line
188 * %F = like %n, unless from self
189 * %g = newsgroup name (if compiled with nntp support)
191 * %I = initials of author
192 * %l = number of lines in the message
193 * %L = like %F, except `lists' are displayed first
194 * %m = number of messages in the mailbox
195 * %n = name of author
197 * %O = like %L, except using address instead of name
199 * %S = short message status (e.g., N/O/D/!/r/-)
200 * %t = `to:' field (recipients)
202 * %u = user (login) name of author
203 * %v = first name of author, unless from self
204 * %W = where user is (organization)
205 * %y = `x-label:' field (if present)
206 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
207 * %Z = status flags */
209 struct hdr_format_info {
214 static const char *hdr_format_str (char *dest,
219 const char *ifstring,
220 const char *elsestring,
221 unsigned long data, format_flag flags)
223 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
226 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
228 int optional = (flags & M_FORMAT_OPTIONAL);
229 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
230 int is_index = (flags & M_FORMAT_INDEX);
232 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
233 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
242 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
243 mutt_format_s (dest, destlen, prefix,
244 mutt_addr_for_display (hdr->env->reply_to));
247 /* fall through if 'A' returns nothing */
250 if (hdr->env->from && hdr->env->from->mailbox) {
251 mutt_format_s (dest, destlen, prefix,
252 mutt_addr_for_display (hdr->env->from));
259 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
260 !first_mailing_list (dest, destlen, hdr->env->cc))
263 strfcpy (buf2, dest, sizeof (buf2));
264 mutt_format_s (dest, destlen, prefix, buf2);
267 /* fall through if 'B' returns nothing */
271 if ((p = strrchr (ctx->path, '/')))
272 strfcpy (dest, p + 1, destlen);
274 strfcpy (dest, ctx->path, destlen);
277 strfcpy (dest, "(null)", destlen);
278 strfcpy (buf2, dest, sizeof (buf2));
279 mutt_format_s (dest, destlen, prefix, buf2);
283 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
284 mutt_format_s (dest, destlen, prefix, buf2);
288 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
289 snprintf (dest, destlen, fmt, hdr->msgno + 1);
299 /* preprocess $date_format to handle %Z */
307 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
316 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
317 (op == '{' && *cp != '}') ||
318 (op == '[' && *cp != ']') ||
319 (op == '(' && *cp != ')') ||
320 (op == '<' && *cp != '>'))) {
323 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
325 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
326 hdr->zhours, hdr->zminutes);
331 break; /* not enough space left */
340 break; /* not enough space */
351 if (do_locales && Locale)
352 setlocale (LC_TIME, Locale);
354 if (op == '[' || op == 'D')
355 tm = localtime (&hdr->date_sent);
357 tm = localtime (&hdr->received);
358 else if (op == '<') {
363 /* restore sender's time zone */
366 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
368 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
372 strftime (buf2, sizeof (buf2), dest, tm);
375 setlocale (LC_TIME, "C");
377 mutt_format_s (dest, destlen, prefix, buf2);
378 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
384 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
385 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
390 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
391 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
393 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
399 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
400 mutt_format_s (dest, destlen, prefix, buf2);
405 make_from (hdr->env, buf2, sizeof (buf2), 0);
406 mutt_format_s (dest, destlen, prefix, buf2);
408 else if (mutt_addr_is_user (hdr->env->from))
414 mutt_format_s (dest, destlen, prefix,
415 hdr->env->newsgroups ? hdr->env->newsgroups : "");
420 /* (Hormel) spam score */
422 optional = hdr->env->spam ? 1 : 0;
425 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
427 mutt_format_s (dest, destlen, prefix, "");
432 mutt_format_s (dest, destlen, prefix,
433 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
441 for (i = 0; hdr->env->from && hdr->env->from->personal &&
442 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
443 if (isalpha ((int) hdr->env->from->personal[i])) {
445 buf2[j++] = hdr->env->from->personal[i];
455 mutt_format_s (dest, destlen, prefix, buf2);
460 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
461 snprintf (dest, destlen, fmt, (int) hdr->lines);
463 else if (hdr->lines <= 0)
469 make_from (hdr->env, buf2, sizeof (buf2), 1);
470 mutt_format_s (dest, destlen, prefix, buf2);
472 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
473 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
480 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
481 snprintf (dest, destlen, fmt, ctx->msgcount);
484 strfcpy (dest, "(null)", destlen);
488 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
493 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
494 snprintf (dest, destlen, fmt, hdr->score);
504 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
505 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
507 mutt_format_s (dest, destlen, prefix, buf2);
509 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
510 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
516 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
518 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
519 snprintf (dest, destlen, fmt, hdr->num_hidden);
520 else if (is_index && threads)
521 mutt_format_s (dest, destlen, prefix, " ");
526 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
533 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
534 if (flags & M_FORMAT_FORCESUBJ) {
535 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
536 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
537 mutt_format_s_tree (dest, destlen, prefix, buf2);
540 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
543 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
549 else if (hdr->attach_del)
551 else if (hdr->tagged)
553 else if (hdr->flagged)
555 else if (hdr->replied)
557 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
564 /* FOO - this is probably unsafe, but we are not likely to have such
565 a short string passed into this routine */
572 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
573 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
575 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
576 else if (hdr->env->cc)
577 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
579 mutt_format_s (dest, destlen, prefix, buf2);
583 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
584 snprintf (dest, destlen, fmt,
586 && ((i = mutt_user_is_recipient (hdr))) <
587 mutt_strlen (Tochars)) ? Tochars[i] : ' ');
591 if (hdr->env->from && hdr->env->from->mailbox) {
592 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
593 if ((p = strpbrk (buf2, "%@")))
598 mutt_format_s (dest, destlen, prefix, buf2);
602 if (mutt_addr_is_user (hdr->env->from)) {
604 mutt_format_s (buf2, sizeof (buf2), prefix,
605 mutt_get_name (hdr->env->to));
606 else if (hdr->env->cc)
607 mutt_format_s (buf2, sizeof (buf2), prefix,
608 mutt_get_name (hdr->env->cc));
613 mutt_format_s (buf2, sizeof (buf2), prefix,
614 mutt_get_name (hdr->env->from));
615 if ((p = strpbrk (buf2, " %@")))
617 mutt_format_s (dest, destlen, prefix, buf2);
622 mutt_format_s (dest, destlen, prefix,
623 hdr->env->organization ? hdr->env->organization : "");
624 else if (!hdr->env->organization)
632 if (WithCrypto && hdr->security & GOODSIGN)
634 else if (WithCrypto && hdr->security & ENCRYPT)
636 else if (WithCrypto && hdr->security & SIGN)
638 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
641 snprintf (buf2, sizeof (buf2),
642 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
645 && ctx->msgnotreadyet !=
648 replied ? 'r' : ' ') : (hdr->
652 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
653 hdr->tagged ? '*' : (hdr->
654 flagged ? '!' : (Tochars
657 mutt_user_is_recipient
659 mutt_strlen (Tochars)) ?
661 mutt_format_s (dest, destlen, prefix, buf2);
666 optional = hdr->env->x_label ? 1 : 0;
668 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
672 if (hdr->env->x_label) {
673 i = 1; /* reduce reuse recycle */
675 if (flags & M_FORMAT_TREE
676 && (hdr->thread->prev && hdr->thread->prev->message
677 && hdr->thread->prev->message->env->x_label))
678 htmp = hdr->thread->prev->message;
679 else if (flags & M_FORMAT_TREE
680 && (hdr->thread->parent && hdr->thread->parent->message
681 && hdr->thread->parent->message->env->x_label))
682 htmp = hdr->thread->parent->message;
683 if (htmp && mutt_strcasecmp (hdr->env->x_label,
684 htmp->env->x_label) == 0)
694 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
696 mutt_format_s (dest, destlen, prefix, "");
701 snprintf (dest, destlen, "%%%s%c", prefix, op);
706 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
707 (unsigned long) hfi, flags);
708 else if (flags & M_FORMAT_OPTIONAL)
709 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
710 (unsigned long) hfi, flags);
718 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
719 HEADER * hdr, format_flag flags)
721 struct hdr_format_info hfi;
726 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,