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-ui/lib-ui.h>
12 #include <lib-mime/mime.h>
13 #include <lib-mx/mx.h>
21 #include "mutt_idna.h"
23 int mutt_is_mail_list (address_t * addr)
25 if (!rx_list_match (UnMailLists, addr->mailbox))
26 return rx_list_match (MailLists, addr->mailbox);
30 int mutt_is_subscribed_list (address_t * addr)
32 if (!rx_list_match (UnMailLists, addr->mailbox)
33 && !rx_list_match (UnSubscribedLists, addr->mailbox))
34 return rx_list_match (SubscribedLists, addr->mailbox);
38 /* Search for a mailing list in the list of addresses pointed to by adr.
39 * If one is found, print pfx and the name of the list into buf, then
40 * return 1. Otherwise, simply return 0.
43 check_for_mailing_list (address_t * adr, const char *pfx, char *buf, int buflen)
45 for (; adr; adr = adr->next) {
46 if (mutt_is_subscribed_list (adr)) {
47 if (pfx && buf && buflen)
48 snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
55 /* Search for a mailing list in the list of addresses pointed to by adr.
56 * If one is found, print the address of the list into buf, then return 1.
57 * Otherwise, simply return 0.
59 static int check_for_mailing_list_addr (address_t * adr, char *buf, int buflen)
61 for (; adr; adr = adr->next) {
62 if (mutt_is_subscribed_list (adr)) {
64 snprintf (buf, buflen, "%s", adr->mailbox);
72 static int first_mailing_list (char *buf, ssize_t buflen, address_t * a)
74 for (; a; a = a->next) {
75 if (mutt_is_subscribed_list (a)) {
76 mutt_save_path (buf, buflen, a);
83 static void make_from (ENVELOPE * hdr, char *buf, ssize_t len, int do_lists)
87 me = mutt_addr_is_user (hdr->from);
90 if (check_for_mailing_list (hdr->to, "To ", buf, len))
92 if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
97 snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
98 else if (me && hdr->cc)
99 snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
101 m_strcpy(buf, len, mutt_get_name(hdr->from));
106 static void make_from_addr (ENVELOPE * hdr, char *buf, ssize_t len,
111 me = mutt_addr_is_user (hdr->from);
113 if (do_lists || me) {
114 if (check_for_mailing_list_addr (hdr->to, buf, len))
116 if (check_for_mailing_list_addr (hdr->cc, buf, len))
121 snprintf (buf, len, "%s", hdr->to->mailbox);
122 else if (me && hdr->cc)
123 snprintf (buf, len, "%s", hdr->cc->mailbox);
125 m_strcpy(buf, len, hdr->from->mailbox);
130 static int user_in_addr (address_t * a)
132 for (; a; a = a->next)
133 if (mutt_addr_is_user (a))
139 * 0: user is not in list
140 * 1: user is unique recipient
141 * 2: user is in the TO list
142 * 3: user is in the CC list
143 * 4: user is originator
144 * 5: sent to a subscribed mailinglist
146 int mutt_user_is_recipient (HEADER * h)
148 ENVELOPE *env = h->env;
150 if (!h->recip_valid) {
153 if (mutt_addr_is_user (env->from))
155 else if (user_in_addr (env->to)) {
156 if (env->to->next || env->cc)
157 h->recipient = 2; /* non-unique recipient */
159 h->recipient = 1; /* unique recipient */
161 else if (user_in_addr (env->cc))
163 else if (check_for_mailing_list (env->to, NULL, NULL, 0))
165 else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
174 /* %a = address of author
175 * %A = reply-to address (if present; otherwise: address of author
176 * %b = filename of the originating folder
177 * %B = the list to which the letter was sent
178 * %c = size of message in bytes
179 * %C = current message number
180 * %d = date and time of message using $date_format and sender's timezone
181 * %D = date and time of message using $date_format and local timezone
182 * %e = current message number in thread
183 * %E = number of messages in current thread
184 * %f = entire from line
185 * %F = like %n, unless from self
186 * %g = newsgroup name (if compiled with nntp support)
188 * %I = initials of author
189 * %l = number of lines in the message
190 * %L = like %F, except `lists' are displayed first
191 * %m = number of messages in the mailbox
192 * %n = name of author
194 * %O = like %L, except using address instead of name
196 * %S = short message status (e.g., N/O/D/!/r/-)
197 * %t = `to:' field (recipients)
199 * %u = user (login) name of author
200 * %v = first name of author, unless from self
201 * %W = where user is (organization)
202 * %X = number of MIME attachments
203 * %y = `x-label:' field (if present)
204 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
205 * %Z = status flags */
207 struct hdr_format_info {
213 hdr_format_str(char *dest, ssize_t destlen,
214 char op, const char *src, const char *prefix,
215 const char *ifstr, const char *elstr,
216 anytype data, format_flag flags)
218 struct hdr_format_info *hfi = data.ptr;
221 char fmt[STRING], buf2[STRING], ch, *p;
222 int do_locales, i, c;
223 int optional = (flags & M_FORMAT_OPTIONAL);
224 int threads = ((Sort & SORT_MASK) == SORT_THREADS);
225 int is_index = (flags & M_FORMAT_INDEX);
227 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
228 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
237 if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
238 mutt_format_s (dest, destlen, prefix,
239 mutt_addr_for_display (hdr->env->reply_to));
242 /* fall through if 'A' returns nothing */
245 if (hdr->env->from && hdr->env->from->mailbox) {
246 mutt_format_s (dest, destlen, prefix,
247 mutt_addr_for_display (hdr->env->from));
254 if (!first_mailing_list (dest, destlen, hdr->env->to) &&
255 !first_mailing_list (dest, destlen, hdr->env->cc))
258 m_strcpy(buf2, sizeof(buf2), dest);
259 mutt_format_s (dest, destlen, prefix, buf2);
262 /* fall through if 'B' returns nothing */
266 if ((p = strrchr (ctx->path, '/')))
267 m_strcpy(dest, destlen, p + 1);
269 m_strcpy(dest, destlen, ctx->path);
272 m_strcpy(dest, destlen, "(null)");
273 m_strcpy(buf2, sizeof(buf2), dest);
274 mutt_format_s (dest, destlen, prefix, buf2);
278 mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
279 mutt_format_s (dest, destlen, prefix, buf2);
283 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
284 snprintf (dest, destlen, fmt, hdr->msgno + 1);
294 /* preprocess $date_format to handle %Z */
302 cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
311 while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
312 (op == '{' && *cp != '}') ||
313 (op == '[' && *cp != ']') ||
314 (op == '(' && *cp != ')') ||
315 (op == '<' && *cp != '>'))) {
318 if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
320 sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
321 hdr->zhours, hdr->zminutes);
326 break; /* not enough space left */
335 break; /* not enough space */
346 if (do_locales && Locale)
347 setlocale (LC_TIME, Locale);
349 if (op == '[' || op == 'D')
350 tm = localtime (&hdr->date_sent);
352 tm = localtime (&hdr->received);
353 else if (op == '<') {
358 /* restore sender's time zone */
361 T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
363 T += (hdr->zhours * 3600 + hdr->zminutes * 60);
367 strftime (buf2, sizeof (buf2), dest, tm);
370 setlocale (LC_TIME, "C");
372 mutt_format_s (dest, destlen, prefix, buf2);
373 if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
379 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
380 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
385 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
386 snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
388 else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
394 rfc822_addrcat(buf2, sizeof (buf2), hdr->env->from, 1);
395 mutt_format_s (dest, destlen, prefix, buf2);
400 make_from (hdr->env, buf2, sizeof (buf2), 0);
401 mutt_format_s (dest, destlen, prefix, buf2);
403 else if (mutt_addr_is_user (hdr->env->from))
409 mutt_format_s (dest, destlen, prefix,
410 hdr->env->newsgroups ? hdr->env->newsgroups : "");
415 /* (Hormel) spam score */
417 optional = hdr->env->spam ? 1 : 0;
420 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
422 mutt_format_s (dest, destlen, prefix, "");
427 mutt_format_s (dest, destlen, prefix,
428 hdr->env->message_id ? hdr->env->message_id : "<no.id>");
436 for (i = 0; hdr->env->from && hdr->env->from->personal &&
437 hdr->env->from->personal[i] && j < STRING - 1; i++) {
438 if (isalpha ((int) hdr->env->from->personal[i])) {
440 buf2[j++] = hdr->env->from->personal[i];
450 mutt_format_s (dest, destlen, prefix, buf2);
455 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
456 snprintf (dest, destlen, fmt, (int) hdr->lines);
458 else if (hdr->lines <= 0)
464 make_from (hdr->env, buf2, sizeof (buf2), 1);
465 mutt_format_s (dest, destlen, prefix, buf2);
467 else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
468 !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
475 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
476 snprintf (dest, destlen, fmt, ctx->msgcount);
479 m_strcpy(dest, destlen, "(null)");
483 mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
488 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
489 snprintf (dest, destlen, fmt, hdr->score);
499 make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
500 if ((p = strpbrk (buf2, "%@")))
502 mutt_format_s (dest, destlen, prefix, buf2);
504 else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
505 !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
511 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
513 if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
514 snprintf (dest, destlen, fmt, hdr->num_hidden);
515 else if (is_index && threads)
516 mutt_format_s (dest, destlen, prefix, " ");
521 if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
528 if (flags & M_FORMAT_TREE && !hdr->collapsed) {
529 if (flags & M_FORMAT_FORCESUBJ) {
530 mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
531 snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
532 mutt_format_s_tree (dest, destlen, prefix, buf2);
535 mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
538 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
544 else if (hdr->attach_del)
546 else if (hdr->tagged)
548 else if (hdr->flagged)
550 else if (hdr->replied)
552 else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
559 /* FOO - this is probably unsafe, but we are not likely to have such
560 a short string passed into this routine */
567 if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
568 !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
570 snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
571 else if (hdr->env->cc)
572 snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
574 mutt_format_s (dest, destlen, prefix, buf2);
578 i = mutt_user_is_recipient(hdr);
579 assert (i >= 0); /* help compiler to see c is initialized */
581 if (Charset_is_utf8) {
582 const char *s = Tochars;
584 snprintf (fmt, sizeof (fmt), "%%%slc", prefix);
586 c = m_ustrgetc(s, &s);
593 snprintf(dest, destlen, fmt, c);
595 snprintf(fmt, sizeof (fmt), "%%%sc", prefix);
596 snprintf(dest, destlen, fmt, i < m_strlen(Tochars) ? Tochars[i] : ' ');
601 if (hdr->env->from && hdr->env->from->mailbox) {
602 m_strcpy(buf2, sizeof(buf2), mutt_addr_for_display(hdr->env->from));
603 if ((p = strpbrk (buf2, "%@")))
608 mutt_format_s (dest, destlen, prefix, buf2);
612 if (mutt_addr_is_user (hdr->env->from)) {
614 mutt_format_s (buf2, sizeof (buf2), prefix,
615 mutt_get_name (hdr->env->to));
616 else if (hdr->env->cc)
617 mutt_format_s (buf2, sizeof (buf2), prefix,
618 mutt_get_name (hdr->env->cc));
623 mutt_format_s (buf2, sizeof (buf2), prefix,
624 mutt_get_name (hdr->env->from));
625 if ((p = strpbrk (buf2, " %@")))
627 mutt_format_s (dest, destlen, prefix, buf2);
632 mutt_format_s (dest, destlen, prefix,
633 hdr->env->organization ? hdr->env->organization : "");
634 else if (!hdr->env->organization)
642 /* The recursion allows messages without depth to return 0. */
644 optional = count != 0;
646 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
647 snprintf (dest, destlen, fmt, count);
655 if (hdr->security & GOODSIGN)
657 else if (hdr->security & ENCRYPT)
659 else if (hdr->security & SIGN)
661 else if (hdr->security & PGPKEY)
670 i = mutt_user_is_recipient(hdr);
671 assert (i >= 0); /* help compiler to see c is initialized */
673 if (Charset_is_utf8) {
674 const char *s = Tochars;
676 snprintf (fmt, sizeof (fmt), "%%%slc", prefix);
678 c = m_ustrgetc(s, &s);
685 c = i < m_strlen(Tochars) ? Tochars[i] : ' ';
688 snprintf(buf2, sizeof (buf2), Charset_is_utf8 ? "%c%c%lc" : "%c%c%c",
692 : ((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
693 ? (hdr->replied ? 'r' : ' ')
694 : (hdr->old ? 'O' : 'N')))),
695 hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch), c);
696 mutt_format_s (dest, destlen, prefix, buf2);
701 optional = hdr->env->x_label ? 1 : 0;
703 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
707 if (hdr->env->x_label) {
708 i = 1; /* reduce reuse recycle */
710 if (flags & M_FORMAT_TREE
711 && (hdr->thread->prev && hdr->thread->prev->message
712 && hdr->thread->prev->message->env->x_label))
713 htmp = hdr->thread->prev->message;
714 else if (flags & M_FORMAT_TREE
715 && (hdr->thread->parent && hdr->thread->parent->message
716 && hdr->thread->parent->message->env->x_label))
717 htmp = hdr->thread->parent->message;
718 if (htmp && m_strcasecmp(hdr->env->x_label,
719 htmp->env->x_label) == 0)
729 mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
731 mutt_format_s (dest, destlen, prefix, "");
740 if (flags & M_FORMAT_OPTIONAL)
741 m_strformat(dest, destlen, 0, optional ? ifstr: elstr,
742 hdr_format_str, data, flags);
750 _mutt_make_string (char *dest, ssize_t destlen, const char *s, CONTEXT * ctx,
751 HEADER * hdr, format_flag flags)
753 struct hdr_format_info hfi;
758 m_strformat(dest, destlen, getmaxx(main_w), s, hdr_format_str, &hfi, flags);