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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
13 #include <lib-ui/curses.h>
14 #include <lib-mx/mx.h>
21 #include <lib-crypt/crypt.h>
22 #include "mutt_idna.h"
24 int mutt_is_mail_list (address_t * addr)
26 if (!rx_list_match (UnMailLists, addr->mailbox))
27 return rx_list_match (MailLists, addr->mailbox);
31 int mutt_is_subscribed_list (address_t * addr)
33 if (!rx_list_match (UnMailLists, addr->mailbox)
34 && !rx_list_match (UnSubscribedLists, addr->mailbox))
35 return rx_list_match (SubscribedLists, addr->mailbox);
39 /* Search for a mailing list in the list of addresses pointed to by adr.
40 * If one is found, print pfx and the name of the list into buf, then
41 * return 1. Otherwise, simply return 0.
44 check_for_mailing_list (address_t * adr, const char *pfx, char *buf, int buflen)
46 for (; adr; adr = adr->next) {
47 if (mutt_is_subscribed_list (adr)) {
48 if (pfx && buf && buflen)
49 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
56 /* Search for a mailing list in the list of addresses pointed to by adr.
57 * If one is found, print the address of the list into buf, then return 1.
58 * Otherwise, simply return 0.
60 static int check_for_mailing_list_addr (address_t * adr, char *buf, int buflen)
62 for (; adr; adr = adr->next) {
63 if (mutt_is_subscribed_list (adr)) {
65 snprintf (buf, buflen, "%s", adr->mailbox);
73 static int first_mailing_list (char *buf, ssize_t buflen, address_t * a)
75 for (; a; a = a->next) {
76 if (mutt_is_subscribed_list (a)) {
77 mutt_save_path (buf, buflen, a);
84 static void make_from (ENVELOPE * hdr, char *buf, ssize_t len, int do_lists)
88 me = mutt_addr_is_user (hdr->from);
91 if (check_for_mailing_list (hdr->to, "To ", buf, len))
93 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
98 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
99 else if (me && hdr->cc)
100 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
102 m_strcpy(buf, len, mutt_get_name(hdr->from));
107 static void make_from_addr (ENVELOPE * hdr, char *buf, ssize_t len,
112 me = mutt_addr_is_user (hdr->from);
114 if (do_lists || me) {
115 if (check_for_mailing_list_addr (hdr->to, buf, len))
117 if (check_for_mailing_list_addr (hdr->cc, buf, len))
122 snprintf (buf, len, "%s", hdr->to->mailbox);
123 else if (me && hdr->cc)
124 snprintf (buf, len, "%s", hdr->cc->mailbox);
126 m_strcpy(buf, len, hdr->from->mailbox);
131 static int user_in_addr (address_t * a)
133 for (; a; a = a->next)
134 if (mutt_addr_is_user (a))
140 * 0: user is not in list
141 * 1: user is unique recipient
142 * 2: user is in the TO list
143 * 3: user is in the CC list
144 * 4: user is originator
145 * 5: sent to a subscribed mailinglist
147 int mutt_user_is_recipient (HEADER * h)
149 ENVELOPE *env = h->env;
151 if (!h->recip_valid) {
154 if (mutt_addr_is_user (env->from))
156 else if (user_in_addr (env->to)) {
157 if (env->to->next || env->cc)
158 h->recipient = 2; /* non-unique recipient */
160 h->recipient = 1; /* unique recipient */
162 else if (user_in_addr (env->cc))
164 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
166 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
175 /* %a = address of author
176 * %A = reply-to address (if present; otherwise: address of author
177 * %b = filename of the originating folder
178 * %B = the list to which the letter was sent
179 * %c = size of message in bytes
180 * %C = current message number
181 * %d = date and time of message using $date_format and sender's timezone
182 * %D = date and time of message using $date_format and local timezone
183 * %e = current message number in thread
184 * %E = number of messages in current thread
185 * %f = entire from line
186 * %F = like %n, unless from self
187 * %g = newsgroup name (if compiled with nntp support)
189 * %I = initials of author
190 * %l = number of lines in the message
191 * %L = like %F, except `lists' are displayed first
192 * %m = number of messages in the mailbox
193 * %n = name of author
195 * %O = like %L, except using address instead of name
197 * %S = short message status (e.g., N/O/D/!/r/-)
198 * %t = `to:' field (recipients)
200 * %u = user (login) name of author
201 * %v = first name of author, unless from self
202 * %W = where user is (organization)
203 * %X = number of MIME attachments
204 * %y = `x-label:' field (if present)
205 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
206 * %Z = status flags */
208 struct hdr_format_info {
213 static const char *hdr_format_str (char *dest,
218 const char *ifstring,
219 const char *elsestring,
220 unsigned long data, format_flag flags)
222 struct hdr_format_info *hfi = (struct hdr_format_info *) data;
225 char fmt[STRING], buf2[STRING], ch, *p;
227 int optional = (flags & M_FORMAT_OPTIONAL);
228 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
229 int is_index = (flags & M_FORMAT_INDEX);
231 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
232 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
241 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
242 mutt_format_s (dest, destlen, prefix,
243 mutt_addr_for_display (hdr->env->reply_to));
246 /* fall through if 'A' returns nothing */
249 if (hdr->env->from && hdr->env->from->mailbox) {
250 mutt_format_s (dest, destlen, prefix,
251 mutt_addr_for_display (hdr->env->from));
258 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
259 !first_mailing_list (dest, destlen, hdr->env->cc))
262 m_strcpy(buf2, sizeof(buf2), dest);
263 mutt_format_s (dest, destlen, prefix, buf2);
266 /* fall through if 'B' returns nothing */
270 if ((p = strrchr (ctx->path, '/')))
271 m_strcpy(dest, destlen, p + 1);
273 m_strcpy(dest, destlen, ctx->path);
276 m_strcpy(dest, destlen, "(null)");
277 m_strcpy(buf2, sizeof(buf2), dest);
278 mutt_format_s (dest, destlen, prefix, buf2);
282 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
283 mutt_format_s (dest, destlen, prefix, buf2);
287 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
288 snprintf (dest, destlen, fmt, hdr->msgno + 1);
298 /* preprocess $date_format to handle %Z */
306 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
315 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
316 (op == '{' && *cp != '}') ||
317 (op == '[' && *cp != ']') ||
318 (op == '(' && *cp != ')') ||
319 (op == '<' && *cp != '>'))) {
322 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
324 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
325 hdr->zhours, hdr->zminutes);
330 break; /* not enough space left */
339 break; /* not enough space */
350 if (do_locales && Locale)
351 setlocale (LC_TIME, Locale);
353 if (op == '[' || op == 'D')
354 tm = localtime (&hdr->date_sent);
356 tm = localtime (&hdr->received);
357 else if (op == '<') {
362 /* restore sender's time zone */
365 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
367 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
371 strftime (buf2, sizeof (buf2), dest, tm);
374 setlocale (LC_TIME, "C");
376 mutt_format_s (dest, destlen, prefix, buf2);
377 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
383 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
384 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
389 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
390 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
392 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
398 rfc822_addrcat(buf2, sizeof (buf2), hdr->env->from, 1);
399 mutt_format_s (dest, destlen, prefix, buf2);
404 make_from (hdr->env, buf2, sizeof (buf2), 0);
405 mutt_format_s (dest, destlen, prefix, buf2);
407 else if (mutt_addr_is_user (hdr->env->from))
413 mutt_format_s (dest, destlen, prefix,
414 hdr->env->newsgroups ? hdr->env->newsgroups : "");
419 /* (Hormel) spam score */
421 optional = hdr->env->spam ? 1 : 0;
424 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
426 mutt_format_s (dest, destlen, prefix, "");
431 mutt_format_s (dest, destlen, prefix,
432 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
440 for (i = 0; hdr->env->from && hdr->env->from->personal &&
441 hdr->env->from->personal[i] && j < STRING - 1; i++) {
442 if (isalpha ((int) hdr->env->from->personal[i])) {
444 buf2[j++] = hdr->env->from->personal[i];
454 mutt_format_s (dest, destlen, prefix, buf2);
459 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
460 snprintf (dest, destlen, fmt, (int) hdr->lines);
462 else if (hdr->lines <= 0)
468 make_from (hdr->env, buf2, sizeof (buf2), 1);
469 mutt_format_s (dest, destlen, prefix, buf2);
471 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
472 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
479 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
480 snprintf (dest, destlen, fmt, ctx->msgcount);
483 m_strcpy(dest, destlen, "(null)");
487 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
492 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
493 snprintf (dest, destlen, fmt, hdr->score);
503 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
504 if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
506 mutt_format_s (dest, destlen, prefix, buf2);
508 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
509 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
515 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
517 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
518 snprintf (dest, destlen, fmt, hdr->num_hidden);
519 else if (is_index && threads)
520 mutt_format_s (dest, destlen, prefix, " ");
525 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
532 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
533 if (flags & M_FORMAT_FORCESUBJ) {
534 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
535 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
536 mutt_format_s_tree (dest, destlen, prefix, buf2);
539 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
542 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
548 else if (hdr->attach_del)
550 else if (hdr->tagged)
552 else if (hdr->flagged)
554 else if (hdr->replied)
556 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
563 /* FOO - this is probably unsafe, but we are not likely to have such
564 a short string passed into this routine */
571 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
572 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
574 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
575 else if (hdr->env->cc)
576 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
578 mutt_format_s (dest, destlen, prefix, buf2);
582 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
583 snprintf (dest, destlen, fmt,
585 && ((i = mutt_user_is_recipient (hdr))) <
586 m_strlen(Tochars)) ? Tochars[i] : ' ');
590 if (hdr->env->from && hdr->env->from->mailbox) {
591 m_strcpy(buf2, sizeof(buf2), mutt_addr_for_display(hdr->env->from));
592 if ((p = strpbrk (buf2, "%@")))
597 mutt_format_s (dest, destlen, prefix, buf2);
601 if (mutt_addr_is_user (hdr->env->from)) {
603 mutt_format_s (buf2, sizeof (buf2), prefix,
604 mutt_get_name (hdr->env->to));
605 else if (hdr->env->cc)
606 mutt_format_s (buf2, sizeof (buf2), prefix,
607 mutt_get_name (hdr->env->cc));
612 mutt_format_s (buf2, sizeof (buf2), prefix,
613 mutt_get_name (hdr->env->from));
614 if ((p = strpbrk (buf2, " %@")))
616 mutt_format_s (dest, destlen, prefix, buf2);
621 mutt_format_s (dest, destlen, prefix,
622 hdr->env->organization ? hdr->env->organization : "");
623 else if (!hdr->env->organization)
631 /* The recursion allows messages without depth to return 0. */
633 optional = count != 0;
635 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
636 snprintf (dest, destlen, fmt, count);
644 if (hdr->security & GOODSIGN)
646 else if (hdr->security & ENCRYPT)
648 else if (hdr->security & SIGN)
650 else if (hdr->security & PGPKEY)
653 snprintf(buf2, sizeof (buf2), "%c%c%c",
657 : ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
658 ? (hdr->replied ? 'r' : ' ')
659 : (hdr->old ? 'O' : 'N')))),
660 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
662 : (hdr->flagged ? '!'
663 : (Tochars && ((i = mutt_user_is_recipient(hdr)) < m_strlen(Tochars))
664 ? Tochars[i] : ' ')));
665 mutt_format_s (dest, destlen, prefix, buf2);
670 optional = hdr->env->x_label ? 1 : 0;
672 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
676 if (hdr->env->x_label) {
677 i = 1; /* reduce reuse recycle */
679 if (flags & M_FORMAT_TREE
680 && (hdr->thread->prev && hdr->thread->prev->message
681 && hdr->thread->prev->message->env->x_label))
682 htmp = hdr->thread->prev->message;
683 else if (flags & M_FORMAT_TREE
684 && (hdr->thread->parent && hdr->thread->parent->message
685 && hdr->thread->parent->message->env->x_label))
686 htmp = hdr->thread->parent->message;
687 if (htmp && m_strcasecmp(hdr->env->x_label,
688 htmp->env->x_label) == 0)
698 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
700 mutt_format_s (dest, destlen, prefix, "");
705 snprintf (dest, destlen, "%%%s%c", prefix, op);
710 mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
711 (unsigned long) hfi, flags);
712 else if (flags & M_FORMAT_OPTIONAL)
713 mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
714 (unsigned long) hfi, flags);
722 _mutt_make_string (char *dest, ssize_t destlen, const char *s, CONTEXT * ctx,
723 HEADER * hdr, format_flag flags)
725 struct hdr_format_info hfi;
730 mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,