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"
29 int mutt_is_mail_list (ADDRESS * addr)
31 if (!rx_list_match (UnMailLists, addr->mailbox))
32 return rx_list_match (MailLists, addr->mailbox);
36 int mutt_is_subscribed_list (ADDRESS * addr)
38 if (!rx_list_match (UnMailLists, addr->mailbox)
39 && !rx_list_match (UnSubscribedLists, addr->mailbox))
40 return rx_list_match (SubscribedLists, addr->mailbox);
44 /* Search for a mailing list in the list of addresses pointed to by adr.
45 * If one is found, print pfx and the name of the list into buf, then
46 * return 1. Otherwise, simply return 0.
49 check_for_mailing_list (ADDRESS * adr, char *pfx, char *buf, int buflen)
51 for (; adr; adr = adr->next) {
52 if (mutt_is_subscribed_list (adr)) {
53 if (pfx && buf && buflen)
54 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
61 /* Search for a mailing list in the list of addresses pointed to by adr.
62 * If one is found, print the address of the list into buf, then return 1.
63 * Otherwise, simply return 0.
65 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
67 for (; adr; adr = adr->next) {
68 if (mutt_is_subscribed_list (adr)) {
70 snprintf (buf, buflen, "%s", adr->mailbox);
78 static int first_mailing_list (char *buf, size_t buflen, ADDRESS * a)
80 for (; a; a = a->next) {
81 if (mutt_is_subscribed_list (a)) {
82 mutt_save_path (buf, buflen, a);
89 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
93 me = mutt_addr_is_user (hdr->from);
96 if (check_for_mailing_list (hdr->to, "To ", buf, len))
98 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
103 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
104 else if (me && hdr->cc)
105 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
107 strfcpy (buf, mutt_get_name (hdr->from), len);
112 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
117 me = mutt_addr_is_user (hdr->from);
119 if (do_lists || me) {
120 if (check_for_mailing_list_addr (hdr->to, buf, len))
122 if (check_for_mailing_list_addr (hdr->cc, buf, len))
127 snprintf (buf, len, "%s", hdr->to->mailbox);
128 else if (me && hdr->cc)
129 snprintf (buf, len, "%s", hdr->cc->mailbox);
131 strfcpy (buf, hdr->from->mailbox, len);
136 static int user_in_addr (ADDRESS * a)
138 for (; a; a = a->next)
139 if (mutt_addr_is_user (a))
145 * 0: user is not in list
146 * 1: user is unique recipient
147 * 2: user is in the TO list
148 * 3: user is in the CC list
149 * 4: user is originator
150 * 5: sent to a subscribed mailinglist
152 int mutt_user_is_recipient (HEADER * h)
154 ENVELOPE *env = h->env;
156 if (!h->recip_valid) {
159 if (mutt_addr_is_user (env->from))
161 else if (user_in_addr (env->to)) {
162 if (env->to->next || env->cc)
163 h->recipient = 2; /* non-unique recipient */
165 h->recipient = 1; /* unique recipient */
167 else if (user_in_addr (env->cc))
169 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
171 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
180 /* %a = address of author
181 * %A = reply-to address (if present; otherwise: address of author
182 * %b = filename of the originating folder
183 * %B = the list to which the letter was sent
184 * %c = size of message in bytes
185 * %C = current message number
186 * %d = date and time of message using $date_format and sender's timezone
187 * %D = date and time of message using $date_format and local timezone
188 * %e = current message number in thread
189 * %E = number of messages in current thread
190 * %f = entire from line
191 * %F = like %n, unless from self
192 * %g = newsgroup name (if compiled with nntp support)
194 * %I = initials of author
195 * %l = number of lines in the message
196 * %L = like %F, except `lists' are displayed first
197 * %m = number of messages in the mailbox
198 * %n = name of author
200 * %O = like %L, except using address instead of name
202 * %S = short message status (e.g., N/O/D/!/r/-)
203 * %t = `to:' field (recipients)
205 * %u = user (login) name of author
206 * %v = first name of author, unless from self
207 * %W = where user is (organization)
208 * %y = `x-label:' field (if present)
209 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
210 * %Z = status flags */
212 struct hdr_format_info {
217 static const char *hdr_format_str (char *dest,
222 const char *ifstring,
223 const char *elsestring,
224 unsigned long data, format_flag flags)
226 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
229 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
231 int optional = (flags & M_FORMAT_OPTIONAL);
232 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
233 int is_index = (flags & M_FORMAT_INDEX);
235 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
236 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
245 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
246 mutt_format_s (dest, destlen, prefix,
247 mutt_addr_for_display (hdr->env->reply_to));
250 /* fall through if 'A' returns nothing */
253 if (hdr->env->from && hdr->env->from->mailbox) {
254 mutt_format_s (dest, destlen, prefix,
255 mutt_addr_for_display (hdr->env->from));
262 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
263 !first_mailing_list (dest, destlen, hdr->env->cc))
266 strfcpy (buf2, dest, sizeof (buf2));
267 mutt_format_s (dest, destlen, prefix, buf2);
270 /* fall through if 'B' returns nothing */
274 if ((p = strrchr (ctx->path, '/')))
275 strfcpy (dest, p + 1, destlen);
277 strfcpy (dest, ctx->path, destlen);
280 strfcpy (dest, "(null)", destlen);
281 strfcpy (buf2, dest, sizeof (buf2));
282 mutt_format_s (dest, destlen, prefix, buf2);
286 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
287 mutt_format_s (dest, destlen, prefix, buf2);
291 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
292 snprintf (dest, destlen, fmt, hdr->msgno + 1);
302 /* preprocess $date_format to handle %Z */
310 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
319 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
320 (op == '{' && *cp != '}') ||
321 (op == '[' && *cp != ']') ||
322 (op == '(' && *cp != ')') ||
323 (op == '<' && *cp != '>'))) {
326 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
328 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
329 hdr->zhours, hdr->zminutes);
334 break; /* not enough space left */
343 break; /* not enough space */
354 if (do_locales && Locale)
355 setlocale (LC_TIME, Locale);
357 if (op == '[' || op == 'D')
358 tm = localtime (&hdr->date_sent);
360 tm = localtime (&hdr->received);
361 else if (op == '<') {
366 /* restore sender's time zone */
369 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
371 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
375 strftime (buf2, sizeof (buf2), dest, tm);
378 setlocale (LC_TIME, "C");
380 mutt_format_s (dest, destlen, prefix, buf2);
381 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
387 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
388 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
393 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
394 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
396 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
402 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
403 mutt_format_s (dest, destlen, prefix, buf2);
408 make_from (hdr->env, buf2, sizeof (buf2), 0);
409 mutt_format_s (dest, destlen, prefix, buf2);
411 else if (mutt_addr_is_user (hdr->env->from))
417 mutt_format_s (dest, destlen, prefix,
418 hdr->env->newsgroups ? hdr->env->newsgroups : "");
423 /* (Hormel) spam score */
425 optional = hdr->env->spam ? 1 : 0;
428 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
430 mutt_format_s (dest, destlen, prefix, "");
435 mutt_format_s (dest, destlen, prefix,
436 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
444 for (i = 0; hdr->env->from && hdr->env->from->personal &&
445 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
446 if (isalpha ((int) hdr->env->from->personal[i])) {
448 buf2[j++] = hdr->env->from->personal[i];
458 mutt_format_s (dest, destlen, prefix, buf2);
463 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
464 snprintf (dest, destlen, fmt, (int) hdr->lines);
466 else if (hdr->lines <= 0)
472 make_from (hdr->env, buf2, sizeof (buf2), 1);
473 mutt_format_s (dest, destlen, prefix, buf2);
475 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
476 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
483 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
484 snprintf (dest, destlen, fmt, ctx->msgcount);
487 strfcpy (dest, "(null)", destlen);
491 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
496 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
497 snprintf (dest, destlen, fmt, hdr->score);
507 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
508 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
510 mutt_format_s (dest, destlen, prefix, buf2);
512 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
513 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
519 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
521 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
522 snprintf (dest, destlen, fmt, hdr->num_hidden);
523 else if (is_index && threads)
524 mutt_format_s (dest, destlen, prefix, " ");
529 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
536 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
537 if (flags & M_FORMAT_FORCESUBJ) {
538 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
539 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
540 mutt_format_s_tree (dest, destlen, prefix, buf2);
543 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
546 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
552 else if (hdr->attach_del)
554 else if (hdr->tagged)
556 else if (hdr->flagged)
558 else if (hdr->replied)
560 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
567 /* FOO - this is probably unsafe, but we are not likely to have such
568 a short string passed into this routine */
575 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
576 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
578 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
579 else if (hdr->env->cc)
580 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
582 mutt_format_s (dest, destlen, prefix, buf2);
586 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
587 snprintf (dest, destlen, fmt,
589 && ((i = mutt_user_is_recipient (hdr))) <
590 mutt_strlen (Tochars)) ? Tochars[i] : ' ');
594 if (hdr->env->from && hdr->env->from->mailbox) {
595 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
596 if ((p = strpbrk (buf2, "%@")))
601 mutt_format_s (dest, destlen, prefix, buf2);
605 if (mutt_addr_is_user (hdr->env->from)) {
607 mutt_format_s (buf2, sizeof (buf2), prefix,
608 mutt_get_name (hdr->env->to));
609 else if (hdr->env->cc)
610 mutt_format_s (buf2, sizeof (buf2), prefix,
611 mutt_get_name (hdr->env->cc));
616 mutt_format_s (buf2, sizeof (buf2), prefix,
617 mutt_get_name (hdr->env->from));
618 if ((p = strpbrk (buf2, " %@")))
620 mutt_format_s (dest, destlen, prefix, buf2);
625 mutt_format_s (dest, destlen, prefix,
626 hdr->env->organization ? hdr->env->organization : "");
627 else if (!hdr->env->organization)
635 if (WithCrypto && hdr->security & GOODSIGN)
637 else if (WithCrypto && hdr->security & ENCRYPT)
639 else if (WithCrypto && hdr->security & SIGN)
641 else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
644 snprintf (buf2, sizeof (buf2),
645 "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
648 && ctx->msgnotreadyet !=
651 replied ? 'r' : ' ') : (hdr->
655 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
656 hdr->tagged ? '*' : (hdr->
657 flagged ? '!' : (Tochars
660 mutt_user_is_recipient
662 mutt_strlen (Tochars)) ?
664 mutt_format_s (dest, destlen, prefix, buf2);
669 optional = hdr->env->x_label ? 1 : 0;
671 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
675 if (hdr->env->x_label) {
676 i = 1; /* reduce reuse recycle */
678 if (flags & M_FORMAT_TREE
679 && (hdr->thread->prev && hdr->thread->prev->message
680 && hdr->thread->prev->message->env->x_label))
681 htmp = hdr->thread->prev->message;
682 else if (flags & M_FORMAT_TREE
683 && (hdr->thread->parent && hdr->thread->parent->message
684 && hdr->thread->parent->message->env->x_label))
685 htmp = hdr->thread->parent->message;
686 if (htmp && mutt_strcasecmp (hdr->env->x_label,
687 htmp->env->x_label) == 0)
697 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
699 mutt_format_s (dest, destlen, prefix, "");
704 snprintf (dest, destlen, "%%%s%c", prefix, op);
709 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
710 (unsigned long) hfi, flags);
711 else if (flags & M_FORMAT_OPTIONAL)
712 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
713 (unsigned long) hfi, flags);
721 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
722 HEADER * hdr, format_flag flags)
724 struct hdr_format_info hfi;
729 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,