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"
19 #include "mutt_crypt.h"
20 #include "mutt_idna.h"
31 int mutt_is_mail_list (ADDRESS * addr)
33 if (!rx_list_match (UnMailLists, addr->mailbox))
34 return rx_list_match (MailLists, addr->mailbox);
38 int mutt_is_subscribed_list (ADDRESS * addr)
40 if (!rx_list_match (UnMailLists, addr->mailbox)
41 && !rx_list_match (UnSubscribedLists, addr->mailbox))
42 return rx_list_match (SubscribedLists, addr->mailbox);
46 /* Search for a mailing list in the list of addresses pointed to by adr.
47 * If one is found, print pfx and the name of the list into buf, then
48 * return 1. Otherwise, simply return 0.
51 check_for_mailing_list (ADDRESS * adr, char *pfx, char *buf, int buflen)
53 for (; adr; adr = adr->next) {
54 if (mutt_is_subscribed_list (adr)) {
55 if (pfx && buf && buflen)
56 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
63 /* Search for a mailing list in the list of addresses pointed to by adr.
64 * If one is found, print the address of the list into buf, then return 1.
65 * Otherwise, simply return 0.
67 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
69 for (; adr; adr = adr->next) {
70 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) {
83 if (mutt_is_subscribed_list (a)) {
84 mutt_save_path (buf, buflen, a);
91 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
95 me = mutt_addr_is_user (hdr->from);
98 if (check_for_mailing_list (hdr->to, "To ", buf, len))
100 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
105 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
106 else if (me && hdr->cc)
107 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
109 strfcpy (buf, mutt_get_name (hdr->from), len);
114 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
119 me = mutt_addr_is_user (hdr->from);
121 if (do_lists || me) {
122 if (check_for_mailing_list_addr (hdr->to, buf, len))
124 if (check_for_mailing_list_addr (hdr->cc, buf, len))
129 snprintf (buf, len, "%s", hdr->to->mailbox);
130 else if (me && hdr->cc)
131 snprintf (buf, len, "%s", hdr->cc->mailbox);
133 strfcpy (buf, hdr->from->mailbox, len);
138 static int user_in_addr (ADDRESS * a)
140 for (; a; a = a->next)
141 if (mutt_addr_is_user (a))
147 * 0: user is not in list
148 * 1: user is unique recipient
149 * 2: user is in the TO list
150 * 3: user is in the CC list
151 * 4: user is originator
152 * 5: sent to a subscribed mailinglist
154 int mutt_user_is_recipient (HEADER * h)
156 ENVELOPE *env = h->env;
158 if (!h->recip_valid) {
161 if (mutt_addr_is_user (env->from))
163 else if (user_in_addr (env->to)) {
164 if (env->to->next || env->cc)
165 h->recipient = 2; /* non-unique recipient */
167 h->recipient = 1; /* unique recipient */
169 else if (user_in_addr (env->cc))
171 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
173 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
182 /* %a = address of author
183 * %A = reply-to address (if present; otherwise: address of author
184 * %b = filename of the originating folder
185 * %B = the list to which the letter was sent
186 * %c = size of message in bytes
187 * %C = current message number
188 * %d = date and time of message using $date_format and sender's timezone
189 * %D = date and time of message using $date_format and local timezone
190 * %e = current message number in thread
191 * %E = number of messages in current thread
192 * %f = entire from line
193 * %F = like %n, unless from self
194 * %g = newsgroup name (if compiled with nntp support)
196 * %I = initials of author
197 * %l = number of lines in the message
198 * %L = like %F, except `lists' are displayed first
199 * %m = number of messages in the mailbox
200 * %n = name of author
202 * %O = like %L, except using address instead of name
204 * %S = short message status (e.g., N/O/D/!/r/-)
205 * %t = `to:' field (recipients)
207 * %u = user (login) name of author
208 * %v = first name of author, unless from self
209 * %W = where user is (organization)
210 * %X = number of MIME attachments
211 * %y = `x-label:' field (if present)
212 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
213 * %Z = status flags */
215 struct hdr_format_info {
220 static const char *hdr_format_str (char *dest,
225 const char *ifstring,
226 const char *elsestring,
227 unsigned long data, format_flag flags)
229 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
232 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
234 int optional = (flags & M_FORMAT_OPTIONAL);
235 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
236 int is_index = (flags & M_FORMAT_INDEX);
238 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
239 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
248 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
249 mutt_format_s (dest, destlen, prefix,
250 mutt_addr_for_display (hdr->env->reply_to));
253 /* fall through if 'A' returns nothing */
256 if (hdr->env->from && hdr->env->from->mailbox) {
257 mutt_format_s (dest, destlen, prefix,
258 mutt_addr_for_display (hdr->env->from));
265 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
266 !first_mailing_list (dest, destlen, hdr->env->cc))
269 strfcpy (buf2, dest, sizeof (buf2));
270 mutt_format_s (dest, destlen, prefix, buf2);
273 /* fall through if 'B' returns nothing */
277 if ((p = strrchr (ctx->path, '/')))
278 strfcpy (dest, p + 1, destlen);
280 strfcpy (dest, ctx->path, destlen);
283 strfcpy (dest, "(null)", destlen);
284 strfcpy (buf2, dest, sizeof (buf2));
285 mutt_format_s (dest, destlen, prefix, buf2);
289 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
290 mutt_format_s (dest, destlen, prefix, buf2);
294 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
295 snprintf (dest, destlen, fmt, hdr->msgno + 1);
305 /* preprocess $date_format to handle %Z */
313 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
322 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
323 (op == '{' && *cp != '}') ||
324 (op == '[' && *cp != ']') ||
325 (op == '(' && *cp != ')') ||
326 (op == '<' && *cp != '>'))) {
329 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
331 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
332 hdr->zhours, hdr->zminutes);
337 break; /* not enough space left */
346 break; /* not enough space */
357 if (do_locales && Locale)
358 setlocale (LC_TIME, Locale);
360 if (op == '[' || op == 'D')
361 tm = localtime (&hdr->date_sent);
363 tm = localtime (&hdr->received);
364 else if (op == '<') {
369 /* restore sender's time zone */
372 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
374 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
378 strftime (buf2, sizeof (buf2), dest, tm);
381 setlocale (LC_TIME, "C");
383 mutt_format_s (dest, destlen, prefix, buf2);
384 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
390 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
391 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
396 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
397 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
399 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
405 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
406 mutt_format_s (dest, destlen, prefix, buf2);
411 make_from (hdr->env, buf2, sizeof (buf2), 0);
412 mutt_format_s (dest, destlen, prefix, buf2);
414 else if (mutt_addr_is_user (hdr->env->from))
420 mutt_format_s (dest, destlen, prefix,
421 hdr->env->newsgroups ? hdr->env->newsgroups : "");
426 /* (Hormel) spam score */
428 optional = hdr->env->spam ? 1 : 0;
431 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
433 mutt_format_s (dest, destlen, prefix, "");
438 mutt_format_s (dest, destlen, prefix,
439 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
447 for (i = 0; hdr->env->from && hdr->env->from->personal &&
448 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
449 if (isalpha ((int) hdr->env->from->personal[i])) {
451 buf2[j++] = hdr->env->from->personal[i];
461 mutt_format_s (dest, destlen, prefix, buf2);
466 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
467 snprintf (dest, destlen, fmt, (int) hdr->lines);
469 else if (hdr->lines <= 0)
475 make_from (hdr->env, buf2, sizeof (buf2), 1);
476 mutt_format_s (dest, destlen, prefix, buf2);
478 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
479 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
486 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
487 snprintf (dest, destlen, fmt, ctx->msgcount);
490 strfcpy (dest, "(null)", destlen);
494 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
499 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
500 snprintf (dest, destlen, fmt, hdr->score);
510 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
511 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
513 mutt_format_s (dest, destlen, prefix, buf2);
515 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
516 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
522 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
524 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
525 snprintf (dest, destlen, fmt, hdr->num_hidden);
526 else if (is_index && threads)
527 mutt_format_s (dest, destlen, prefix, " ");
532 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
539 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
540 if (flags & M_FORMAT_FORCESUBJ) {
541 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
542 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
543 mutt_format_s_tree (dest, destlen, prefix, buf2);
546 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
549 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
555 else if (hdr->attach_del)
557 else if (hdr->tagged)
559 else if (hdr->flagged)
561 else if (hdr->replied)
563 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
570 /* FOO - this is probably unsafe, but we are not likely to have such
571 a short string passed into this routine */
578 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
579 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
581 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
582 else if (hdr->env->cc)
583 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
585 mutt_format_s (dest, destlen, prefix, buf2);
589 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
590 snprintf (dest, destlen, fmt,
592 && ((i = mutt_user_is_recipient (hdr))) <
593 str_len (Tochars)) ? Tochars[i] : ' ');
597 if (hdr->env->from && hdr->env->from->mailbox) {
598 strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
599 if ((p = strpbrk (buf2, "%@")))
604 mutt_format_s (dest, destlen, prefix, buf2);
608 if (mutt_addr_is_user (hdr->env->from)) {
610 mutt_format_s (buf2, sizeof (buf2), prefix,
611 mutt_get_name (hdr->env->to));
612 else if (hdr->env->cc)
613 mutt_format_s (buf2, sizeof (buf2), prefix,
614 mutt_get_name (hdr->env->cc));
619 mutt_format_s (buf2, sizeof (buf2), prefix,
620 mutt_get_name (hdr->env->from));
621 if ((p = strpbrk (buf2, " %@")))
623 mutt_format_s (dest, destlen, prefix, buf2);
628 mutt_format_s (dest, destlen, prefix,
629 hdr->env->organization ? hdr->env->organization : "");
630 else if (!hdr->env->organization)
638 if (option (OPTCOUNTATTACH)) {
639 if (!hdr->content->parts)
640 mutt_parse_mime_message(ctx, hdr);
641 count = mutt_count_body_parts(hdr, 0);
644 /* The recursion allows messages without depth to return 0. */
646 optional = count != 0;
648 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
649 snprintf (dest, destlen, fmt, count);
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' :
670 && ctx->msgnotreadyet !=
673 replied ? 'r' : ' ') : (hdr->
677 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
678 hdr->tagged ? '*' : (hdr->
679 flagged ? '!' : (Tochars
682 mutt_user_is_recipient
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) {
698 i = 1; /* reduce reuse recycle */
700 if (flags & M_FORMAT_TREE
701 && (hdr->thread->prev && hdr->thread->prev->message
702 && hdr->thread->prev->message->env->x_label))
703 htmp = hdr->thread->prev->message;
704 else if (flags & M_FORMAT_TREE
705 && (hdr->thread->parent && hdr->thread->parent->message
706 && hdr->thread->parent->message->env->x_label))
707 htmp = hdr->thread->parent->message;
708 if (htmp && str_casecmp (hdr->env->x_label,
709 htmp->env->x_label) == 0)
719 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
721 mutt_format_s (dest, destlen, prefix, "");
726 snprintf (dest, destlen, "%%%s%c", prefix, op);
731 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
732 (unsigned long) hfi, flags);
733 else if (flags & M_FORMAT_OPTIONAL)
734 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
735 (unsigned long) hfi, flags);
743 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
744 HEADER * hdr, format_flag flags)
746 struct hdr_format_info hfi;
751 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,