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"
22 #include <lib-lib/str.h>
24 #include <lib-mime/mime.h>
33 int mutt_is_mail_list (address_t * addr)
35 if (!rx_list_match (UnMailLists, addr->mailbox))
36 return rx_list_match (MailLists, addr->mailbox);
40 int mutt_is_subscribed_list (address_t * addr)
42 if (!rx_list_match (UnMailLists, addr->mailbox)
43 && !rx_list_match (UnSubscribedLists, addr->mailbox))
44 return rx_list_match (SubscribedLists, addr->mailbox);
48 /* Search for a mailing list in the list of addresses pointed to by adr.
49 * If one is found, print pfx and the name of the list into buf, then
50 * return 1. Otherwise, simply return 0.
53 check_for_mailing_list (address_t * adr, const char *pfx, char *buf, int buflen)
55 for (; adr; adr = adr->next) {
56 if (mutt_is_subscribed_list (adr)) {
57 if (pfx && buf && buflen)
58 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
65 /* Search for a mailing list in the list of addresses pointed to by adr.
66 * If one is found, print the address of the list into buf, then return 1.
67 * Otherwise, simply return 0.
69 static int check_for_mailing_list_addr (address_t * adr, char *buf, int buflen)
71 for (; adr; adr = adr->next) {
72 if (mutt_is_subscribed_list (adr)) {
74 snprintf (buf, buflen, "%s", adr->mailbox);
82 static int first_mailing_list (char *buf, size_t buflen, address_t * a)
84 for (; a; a = a->next) {
85 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);
100 if (check_for_mailing_list (hdr->to, "To ", buf, len))
102 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
107 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
108 else if (me && hdr->cc)
109 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
111 m_strcpy(buf, len, mutt_get_name(hdr->from));
116 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
121 me = mutt_addr_is_user (hdr->from);
123 if (do_lists || me) {
124 if (check_for_mailing_list_addr (hdr->to, buf, len))
126 if (check_for_mailing_list_addr (hdr->cc, buf, len))
131 snprintf (buf, len, "%s", hdr->to->mailbox);
132 else if (me && hdr->cc)
133 snprintf (buf, len, "%s", hdr->cc->mailbox);
135 m_strcpy(buf, len, hdr->from->mailbox);
140 static int user_in_addr (address_t * a)
142 for (; a; a = a->next)
143 if (mutt_addr_is_user (a))
149 * 0: user is not in list
150 * 1: user is unique recipient
151 * 2: user is in the TO list
152 * 3: user is in the CC list
153 * 4: user is originator
154 * 5: sent to a subscribed mailinglist
156 int mutt_user_is_recipient (HEADER * h)
158 ENVELOPE *env = h->env;
160 if (!h->recip_valid) {
163 if (mutt_addr_is_user (env->from))
165 else if (user_in_addr (env->to)) {
166 if (env->to->next || env->cc)
167 h->recipient = 2; /* non-unique recipient */
169 h->recipient = 1; /* unique recipient */
171 else if (user_in_addr (env->cc))
173 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
175 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
184 /* %a = address of author
185 * %A = reply-to address (if present; otherwise: address of author
186 * %b = filename of the originating folder
187 * %B = the list to which the letter was sent
188 * %c = size of message in bytes
189 * %C = current message number
190 * %d = date and time of message using $date_format and sender's timezone
191 * %D = date and time of message using $date_format and local timezone
192 * %e = current message number in thread
193 * %E = number of messages in current thread
194 * %f = entire from line
195 * %F = like %n, unless from self
196 * %g = newsgroup name (if compiled with nntp support)
198 * %I = initials of author
199 * %l = number of lines in the message
200 * %L = like %F, except `lists' are displayed first
201 * %m = number of messages in the mailbox
202 * %n = name of author
204 * %O = like %L, except using address instead of name
206 * %S = short message status (e.g., N/O/D/!/r/-)
207 * %t = `to:' field (recipients)
209 * %u = user (login) name of author
210 * %v = first name of author, unless from self
211 * %W = where user is (organization)
212 * %X = number of MIME attachments
213 * %y = `x-label:' field (if present)
214 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
215 * %Z = status flags */
217 struct hdr_format_info {
222 static const char *hdr_format_str (char *dest,
227 const char *ifstring,
228 const char *elsestring,
229 unsigned long data, format_flag flags)
231 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
234 char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
236 int optional = (flags & M_FORMAT_OPTIONAL);
237 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
238 int is_index = (flags & M_FORMAT_INDEX);
240 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
241 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
250 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
251 mutt_format_s (dest, destlen, prefix,
252 mutt_addr_for_display (hdr->env->reply_to));
255 /* fall through if 'A' returns nothing */
258 if (hdr->env->from && hdr->env->from->mailbox) {
259 mutt_format_s (dest, destlen, prefix,
260 mutt_addr_for_display (hdr->env->from));
267 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
268 !first_mailing_list (dest, destlen, hdr->env->cc))
271 m_strcpy(buf2, sizeof(buf2), dest);
272 mutt_format_s (dest, destlen, prefix, buf2);
275 /* fall through if 'B' returns nothing */
279 if ((p = strrchr (ctx->path, '/')))
280 m_strcpy(dest, destlen, p + 1);
282 m_strcpy(dest, destlen, ctx->path);
285 m_strcpy(dest, destlen, "(null)");
286 m_strcpy(buf2, sizeof(buf2), dest);
287 mutt_format_s (dest, destlen, prefix, buf2);
291 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
292 mutt_format_s (dest, destlen, prefix, buf2);
296 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
297 snprintf (dest, destlen, fmt, hdr->msgno + 1);
307 /* preprocess $date_format to handle %Z */
315 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
324 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
325 (op == '{' && *cp != '}') ||
326 (op == '[' && *cp != ']') ||
327 (op == '(' && *cp != ')') ||
328 (op == '<' && *cp != '>'))) {
331 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
333 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
334 hdr->zhours, hdr->zminutes);
339 break; /* not enough space left */
348 break; /* not enough space */
359 if (do_locales && Locale)
360 setlocale (LC_TIME, Locale);
362 if (op == '[' || op == 'D')
363 tm = localtime (&hdr->date_sent);
365 tm = localtime (&hdr->received);
366 else if (op == '<') {
371 /* restore sender's time zone */
374 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
376 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
380 strftime (buf2, sizeof (buf2), dest, tm);
383 setlocale (LC_TIME, "C");
385 mutt_format_s (dest, destlen, prefix, buf2);
386 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
392 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
393 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
398 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
399 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
401 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
407 rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
408 mutt_format_s (dest, destlen, prefix, buf2);
413 make_from (hdr->env, buf2, sizeof (buf2), 0);
414 mutt_format_s (dest, destlen, prefix, buf2);
416 else if (mutt_addr_is_user (hdr->env->from))
422 mutt_format_s (dest, destlen, prefix,
423 hdr->env->newsgroups ? hdr->env->newsgroups : "");
428 /* (Hormel) spam score */
430 optional = hdr->env->spam ? 1 : 0;
433 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
435 mutt_format_s (dest, destlen, prefix, "");
440 mutt_format_s (dest, destlen, prefix,
441 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
449 for (i = 0; hdr->env->from && hdr->env->from->personal &&
450 hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
451 if (isalpha ((int) hdr->env->from->personal[i])) {
453 buf2[j++] = hdr->env->from->personal[i];
463 mutt_format_s (dest, destlen, prefix, buf2);
468 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
469 snprintf (dest, destlen, fmt, (int) hdr->lines);
471 else if (hdr->lines <= 0)
477 make_from (hdr->env, buf2, sizeof (buf2), 1);
478 mutt_format_s (dest, destlen, prefix, buf2);
480 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
481 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
488 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
489 snprintf (dest, destlen, fmt, ctx->msgcount);
492 m_strcpy(dest, destlen, "(null)");
496 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
501 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
502 snprintf (dest, destlen, fmt, hdr->score);
512 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
513 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
515 mutt_format_s (dest, destlen, prefix, buf2);
517 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
518 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
524 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
526 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
527 snprintf (dest, destlen, fmt, hdr->num_hidden);
528 else if (is_index && threads)
529 mutt_format_s (dest, destlen, prefix, " ");
534 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
541 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
542 if (flags & M_FORMAT_FORCESUBJ) {
543 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
544 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
545 mutt_format_s_tree (dest, destlen, prefix, buf2);
548 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
551 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
557 else if (hdr->attach_del)
559 else if (hdr->tagged)
561 else if (hdr->flagged)
563 else if (hdr->replied)
565 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
572 /* FOO - this is probably unsafe, but we are not likely to have such
573 a short string passed into this routine */
580 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
581 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
583 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
584 else if (hdr->env->cc)
585 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
587 mutt_format_s (dest, destlen, prefix, buf2);
591 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
592 snprintf (dest, destlen, fmt,
594 && ((i = mutt_user_is_recipient (hdr))) <
595 m_strlen(Tochars)) ? Tochars[i] : ' ');
599 if (hdr->env->from && hdr->env->from->mailbox) {
600 m_strcpy(buf2, sizeof(buf2), mutt_addr_for_display(hdr->env->from));
601 if ((p = strpbrk (buf2, "%@")))
606 mutt_format_s (dest, destlen, prefix, buf2);
610 if (mutt_addr_is_user (hdr->env->from)) {
612 mutt_format_s (buf2, sizeof (buf2), prefix,
613 mutt_get_name (hdr->env->to));
614 else if (hdr->env->cc)
615 mutt_format_s (buf2, sizeof (buf2), prefix,
616 mutt_get_name (hdr->env->cc));
621 mutt_format_s (buf2, sizeof (buf2), prefix,
622 mutt_get_name (hdr->env->from));
623 if ((p = strpbrk (buf2, " %@")))
625 mutt_format_s (dest, destlen, prefix, buf2);
630 mutt_format_s (dest, destlen, prefix,
631 hdr->env->organization ? hdr->env->organization : "");
632 else if (!hdr->env->organization)
640 if (option (OPTCOUNTATTACH)) {
641 if (!hdr->content->parts)
642 mutt_parse_mime_message(ctx, hdr);
643 count = mutt_count_body_parts(hdr, 0);
646 /* The recursion allows messages without depth to return 0. */
648 optional = count != 0;
650 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
651 snprintf (dest, destlen, fmt, count);
659 if (hdr->security & GOODSIGN)
661 else if (hdr->security & ENCRYPT)
663 else if (hdr->security & SIGN)
665 else if (hdr->security & PGPKEY)
668 snprintf(buf2, sizeof (buf2), "%c%c%c",
672 : ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
673 ? (hdr->replied ? 'r' : ' ')
674 : (hdr->old ? 'O' : 'N')))),
675 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
677 : (hdr->flagged ? '!'
678 : (Tochars && ((i = mutt_user_is_recipient(hdr)) < m_strlen(Tochars))
679 ? Tochars[i] : ' ')));
680 mutt_format_s (dest, destlen, prefix, buf2);
685 optional = hdr->env->x_label ? 1 : 0;
687 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
691 if (hdr->env->x_label) {
692 i = 1; /* reduce reuse recycle */
694 if (flags & M_FORMAT_TREE
695 && (hdr->thread->prev && hdr->thread->prev->message
696 && hdr->thread->prev->message->env->x_label))
697 htmp = hdr->thread->prev->message;
698 else if (flags & M_FORMAT_TREE
699 && (hdr->thread->parent && hdr->thread->parent->message
700 && hdr->thread->parent->message->env->x_label))
701 htmp = hdr->thread->parent->message;
702 if (htmp && m_strcasecmp(hdr->env->x_label,
703 htmp->env->x_label) == 0)
713 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
715 mutt_format_s (dest, destlen, prefix, "");
720 snprintf (dest, destlen, "%%%s%c", prefix, op);
725 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
726 (unsigned long) hfi, flags);
727 else if (flags & M_FORMAT_OPTIONAL)
728 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
729 (unsigned long) hfi, flags);
737 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
738 HEADER * hdr, format_flag flags)
740 struct hdr_format_info hfi;
745 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,