Nico Golde:
[apps/madmutt.git] / hdrline.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mutt_curses.h"
16 #include "sort.h"
17 #include "charset.h"
18 #include "mutt_crypt.h"
19 #include "mutt_idna.h"
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <locale.h>
25
26 int mutt_is_mail_list (ADDRESS * addr)
27 {
28   if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
29     return mutt_match_rx_list (addr->mailbox, MailLists);
30   return 0;
31 }
32
33 int mutt_is_subscribed_list (ADDRESS * addr)
34 {
35   if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
36       && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
37     return mutt_match_rx_list (addr->mailbox, SubscribedLists);
38   return 0;
39 }
40
41 /* Search for a mailing list in the list of addresses pointed to by adr.
42  * If one is found, print pfx and the name of the list into buf, then
43  * return 1.  Otherwise, simply return 0.
44  */
45 static int
46 check_for_mailing_list (ADDRESS * adr, char *pfx, char *buf, int buflen)
47 {
48   for (; adr; adr = adr->next) {
49     if (mutt_is_subscribed_list (adr)) {
50       if (pfx && buf && buflen)
51         snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
52       return 1;
53     }
54   }
55   return 0;
56 }
57
58 /* Search for a mailing list in the list of addresses pointed to by adr.
59  * If one is found, print the address of the list into buf, then return 1.
60  * Otherwise, simply return 0.
61  */
62 static int check_for_mailing_list_addr (ADDRESS * adr, char *buf, int buflen)
63 {
64   for (; adr; adr = adr->next) {
65     if (mutt_is_subscribed_list (adr)) {
66       if (buf && buflen)
67         snprintf (buf, buflen, "%s", adr->mailbox);
68       return 1;
69     }
70   }
71   return 0;
72 }
73
74
75 static int first_mailing_list (char *buf, size_t buflen, ADDRESS * a)
76 {
77   for (; a; a = a->next) {
78     if (mutt_is_subscribed_list (a)) {
79       mutt_save_path (buf, buflen, a);
80       return 1;
81     }
82   }
83   return 0;
84 }
85
86 static void make_from (ENVELOPE * hdr, char *buf, size_t len, int do_lists)
87 {
88   int me;
89
90   me = mutt_addr_is_user (hdr->from);
91
92   if (do_lists || me) {
93     if (check_for_mailing_list (hdr->to, "To ", buf, len))
94       return;
95     if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
96       return;
97   }
98
99   if (me && hdr->to)
100     snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
101   else if (me && hdr->cc)
102     snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
103   else if (hdr->from)
104     strfcpy (buf, mutt_get_name (hdr->from), len);
105   else
106     *buf = 0;
107 }
108
109 static void make_from_addr (ENVELOPE * hdr, char *buf, size_t len,
110                             int do_lists)
111 {
112   int me;
113
114   me = mutt_addr_is_user (hdr->from);
115
116   if (do_lists || me) {
117     if (check_for_mailing_list_addr (hdr->to, buf, len))
118       return;
119     if (check_for_mailing_list_addr (hdr->cc, buf, len))
120       return;
121   }
122
123   if (me && hdr->to)
124     snprintf (buf, len, "%s", hdr->to->mailbox);
125   else if (me && hdr->cc)
126     snprintf (buf, len, "%s", hdr->cc->mailbox);
127   else if (hdr->from)
128     strfcpy (buf, hdr->from->mailbox, len);
129   else
130     *buf = 0;
131 }
132
133 static int user_in_addr (ADDRESS * a)
134 {
135   for (; a; a = a->next)
136     if (mutt_addr_is_user (a))
137       return 1;
138   return 0;
139 }
140
141 /* Return values:
142  * 0: user is not in list
143  * 1: user is unique recipient
144  * 2: user is in the TO list
145  * 3: user is in the CC list
146  * 4: user is originator
147  * 5: sent to a subscribed mailinglist
148  */
149 int mutt_user_is_recipient (HEADER * h)
150 {
151   ENVELOPE *env = h->env;
152
153   if (!h->recip_valid) {
154     h->recip_valid = 1;
155
156     if (mutt_addr_is_user (env->from))
157       h->recipient = 4;
158     else if (user_in_addr (env->to)) {
159       if (env->to->next || env->cc)
160         h->recipient = 2;       /* non-unique recipient */
161       else
162         h->recipient = 1;       /* unique recipient */
163     }
164     else if (user_in_addr (env->cc))
165       h->recipient = 3;
166     else if (check_for_mailing_list (env->to, NULL, NULL, 0))
167       h->recipient = 5;
168     else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
169       h->recipient = 5;
170     else
171       h->recipient = 0;
172   }
173
174   return h->recipient;
175 }
176
177 /* %a = address of author
178  * %A = reply-to address (if present; otherwise: address of author
179  * %b = filename of the originating folder
180  * %B = the list to which the letter was sent
181  * %c = size of message in bytes
182  * %C = current message number
183  * %d = date and time of message using $date_format and sender's timezone
184  * %D = date and time of message using $date_format and local timezone
185  * %e = current message number in thread
186  * %E = number of messages in current thread
187  * %f = entire from line
188  * %F = like %n, unless from self
189  * %g = newsgroup name (if compiled with nntp support)
190  * %i = message-id
191  * %I = initials of author
192  * %l = number of lines in the message
193  * %L = like %F, except `lists' are displayed first
194  * %m = number of messages in the mailbox
195  * %n = name of author
196  * %N = score
197  * %O = like %L, except using address instead of name
198  * %s = subject
199  * %S = short message status (e.g., N/O/D/!/r/-)
200  * %t = `to:' field (recipients)
201  * %T = $to_chars
202  * %u = user (login) name of author
203  * %v = first name of author, unless from self
204  * %W = where user is (organization)
205  * %y = `x-label:' field (if present)
206  * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
207  * %Z = status flags    */
208
209 struct hdr_format_info {
210   CONTEXT *ctx;
211   HEADER *hdr;
212 };
213
214 static const char *hdr_format_str (char *dest,
215                                    size_t destlen,
216                                    char op,
217                                    const char *src,
218                                    const char *prefix,
219                                    const char *ifstring,
220                                    const char *elsestring,
221                                    unsigned long data, format_flag flags)
222 {
223   struct hdr_format_info *hfi = (struct hdr_format_info *) data;
224   HEADER *hdr, *htmp;
225   CONTEXT *ctx;
226   char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
227   int do_locales, i;
228   int optional = (flags & M_FORMAT_OPTIONAL);
229   int threads = ((Sort & SORT_MASK) == SORT_THREADS);
230   int is_index = (flags & M_FORMAT_INDEX);
231
232 #define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
233 #define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
234   size_t len;
235
236   hdr = hfi->hdr;
237   ctx = hfi->ctx;
238
239   dest[0] = 0;
240   switch (op) {
241   case 'A':
242     if (hdr->env->reply_to && hdr->env->reply_to->mailbox) {
243       mutt_format_s (dest, destlen, prefix,
244                      mutt_addr_for_display (hdr->env->reply_to));
245       break;
246     }
247     /* fall through if 'A' returns nothing */
248
249   case 'a':
250     if (hdr->env->from && hdr->env->from->mailbox) {
251       mutt_format_s (dest, destlen, prefix,
252                      mutt_addr_for_display (hdr->env->from));
253     }
254     else
255       dest[0] = '\0';
256     break;
257
258   case 'B':
259     if (!first_mailing_list (dest, destlen, hdr->env->to) &&
260         !first_mailing_list (dest, destlen, hdr->env->cc))
261       dest[0] = 0;
262     if (dest[0]) {
263       strfcpy (buf2, dest, sizeof (buf2));
264       mutt_format_s (dest, destlen, prefix, buf2);
265       break;
266     }
267     /* fall through if 'B' returns nothing */
268
269   case 'b':
270     if (ctx) {
271       if ((p = strrchr (ctx->path, '/')))
272         strfcpy (dest, p + 1, destlen);
273       else
274         strfcpy (dest, ctx->path, destlen);
275     }
276     else
277       strfcpy (dest, "(null)", destlen);
278     strfcpy (buf2, dest, sizeof (buf2));
279     mutt_format_s (dest, destlen, prefix, buf2);
280     break;
281
282   case 'c':
283     mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
284     mutt_format_s (dest, destlen, prefix, buf2);
285     break;
286
287   case 'C':
288     snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
289     snprintf (dest, destlen, fmt, hdr->msgno + 1);
290     break;
291
292   case 'd':
293   case 'D':
294   case '{':
295   case '[':
296   case '(':
297   case '<':
298
299     /* preprocess $date_format to handle %Z */
300     {
301       const char *cp;
302       struct tm *tm;
303       time_t T;
304
305       p = dest;
306
307       cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
308       if (*cp == '!') {
309         do_locales = 0;
310         cp++;
311       }
312       else
313         do_locales = 1;
314
315       len = destlen - 1;
316       while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
317                          (op == '{' && *cp != '}') ||
318                          (op == '[' && *cp != ']') ||
319                          (op == '(' && *cp != ')') ||
320                          (op == '<' && *cp != '>'))) {
321         if (*cp == '%') {
322           cp++;
323           if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{')) {
324             if (len >= 5) {
325               sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
326                        hdr->zhours, hdr->zminutes);
327               p += 5;
328               len -= 5;
329             }
330             else
331               break;            /* not enough space left */
332           }
333           else {
334             if (len >= 2) {
335               *p++ = '%';
336               *p++ = *cp;
337               len -= 2;
338             }
339             else
340               break;            /* not enough space */
341           }
342           cp++;
343         }
344         else {
345           *p++ = *cp++;
346           len--;
347         }
348       }
349       *p = 0;
350
351       if (do_locales && Locale)
352         setlocale (LC_TIME, Locale);
353
354       if (op == '[' || op == 'D')
355         tm = localtime (&hdr->date_sent);
356       else if (op == '(')
357         tm = localtime (&hdr->received);
358       else if (op == '<') {
359         T = time (NULL);
360         tm = localtime (&T);
361       }
362       else {
363         /* restore sender's time zone */
364         T = hdr->date_sent;
365         if (hdr->zoccident)
366           T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
367         else
368           T += (hdr->zhours * 3600 + hdr->zminutes * 60);
369         tm = gmtime (&T);
370       }
371
372       strftime (buf2, sizeof (buf2), dest, tm);
373
374       if (do_locales)
375         setlocale (LC_TIME, "C");
376
377       mutt_format_s (dest, destlen, prefix, buf2);
378       if (len > 0 && op != 'd' && op != 'D')    /* Skip ending op */
379         src = cp + 1;
380     }
381     break;
382
383   case 'e':
384     snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
385     snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 1));
386     break;
387
388   case 'E':
389     if (!optional) {
390       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
391       snprintf (dest, destlen, fmt, mutt_messages_in_thread (ctx, hdr, 0));
392     }
393     else if (mutt_messages_in_thread (ctx, hdr, 0) <= 1)
394       optional = 0;
395     break;
396
397   case 'f':
398     buf2[0] = 0;
399     rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
400     mutt_format_s (dest, destlen, prefix, buf2);
401     break;
402
403   case 'F':
404     if (!optional) {
405       make_from (hdr->env, buf2, sizeof (buf2), 0);
406       mutt_format_s (dest, destlen, prefix, buf2);
407     }
408     else if (mutt_addr_is_user (hdr->env->from))
409       optional = 0;
410     break;
411
412 #ifdef USE_NNTP
413   case 'g':
414     mutt_format_s (dest, destlen, prefix,
415                    hdr->env->newsgroups ? hdr->env->newsgroups : "");
416     break;
417 #endif
418
419   case 'H':
420     /* (Hormel) spam score */
421     if (optional)
422       optional = hdr->env->spam ? 1 : 0;
423
424     if (hdr->env->spam)
425       mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
426     else
427       mutt_format_s (dest, destlen, prefix, "");
428
429     break;
430
431   case 'i':
432     mutt_format_s (dest, destlen, prefix,
433                    hdr->env->message_id ? hdr->env->message_id : "<no.id>");
434     break;
435
436   case 'I':
437     {
438       int iflag = FALSE;
439       int j = 0;
440
441       for (i = 0; hdr->env->from && hdr->env->from->personal &&
442            hdr->env->from->personal[i] && j < SHORT_STRING - 1; i++) {
443         if (isalpha ((int) hdr->env->from->personal[i])) {
444           if (!iflag) {
445             buf2[j++] = hdr->env->from->personal[i];
446             iflag = TRUE;
447           }
448         }
449         else
450           iflag = FALSE;
451       }
452
453       buf2[j] = '\0';
454     }
455     mutt_format_s (dest, destlen, prefix, buf2);
456     break;
457
458   case 'l':
459     if (!optional) {
460       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
461       snprintf (dest, destlen, fmt, (int) hdr->lines);
462     }
463     else if (hdr->lines <= 0)
464       optional = 0;
465     break;
466
467   case 'L':
468     if (!optional) {
469       make_from (hdr->env, buf2, sizeof (buf2), 1);
470       mutt_format_s (dest, destlen, prefix, buf2);
471     }
472     else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
473              !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0)) {
474       optional = 0;
475     }
476     break;
477
478   case 'm':
479     if (ctx) {
480       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
481       snprintf (dest, destlen, fmt, ctx->msgcount);
482     }
483     else
484       strfcpy (dest, "(null)", destlen);
485     break;
486
487   case 'n':
488     mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
489     break;
490
491   case 'N':
492     if (!optional) {
493       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
494       snprintf (dest, destlen, fmt, hdr->score);
495     }
496     else {
497       if (hdr->score == 0)
498         optional = 0;
499     }
500     break;
501
502   case 'O':
503     if (!optional) {
504       make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
505       if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
506         *p = 0;
507       mutt_format_s (dest, destlen, prefix, buf2);
508     }
509     else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
510              !check_for_mailing_list_addr (hdr->env->cc, NULL, 0)) {
511       optional = 0;
512     }
513     break;
514
515   case 'M':
516     snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
517     if (!optional) {
518       if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
519         snprintf (dest, destlen, fmt, hdr->num_hidden);
520       else if (is_index && threads)
521         mutt_format_s (dest, destlen, prefix, " ");
522       else
523         *dest = '\0';
524     }
525     else {
526       if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
527         optional = 0;
528     }
529     break;
530
531   case 's':
532
533     if (flags & M_FORMAT_TREE && !hdr->collapsed) {
534       if (flags & M_FORMAT_FORCESUBJ) {
535         mutt_format_s (dest, destlen, "", NONULL (hdr->env->subject));
536         snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
537         mutt_format_s_tree (dest, destlen, prefix, buf2);
538       }
539       else
540         mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
541     }
542     else
543       mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->subject));
544     break;
545
546   case 'S':
547     if (hdr->deleted)
548       ch = 'D';
549     else if (hdr->attach_del)
550       ch = 'd';
551     else if (hdr->tagged)
552       ch = '*';
553     else if (hdr->flagged)
554       ch = '!';
555     else if (hdr->replied)
556       ch = 'r';
557     else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
558       ch = '-';
559     else if (hdr->old)
560       ch = 'O';
561     else
562       ch = 'N';
563
564     /* FOO - this is probably unsafe, but we are not likely to have such
565        a short string passed into this routine */
566     *dest = ch;
567     *(dest + 1) = 0;
568     break;
569
570   case 't':
571     buf2[0] = 0;
572     if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
573         !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2))) {
574       if (hdr->env->to)
575         snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
576       else if (hdr->env->cc)
577         snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
578     }
579     mutt_format_s (dest, destlen, prefix, buf2);
580     break;
581
582   case 'T':
583     snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
584     snprintf (dest, destlen, fmt,
585               (Tochars
586                && ((i = mutt_user_is_recipient (hdr))) <
587                mutt_strlen (Tochars)) ? Tochars[i] : ' ');
588     break;
589
590   case 'u':
591     if (hdr->env->from && hdr->env->from->mailbox) {
592       strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
593       if ((p = strpbrk (buf2, "%@")))
594         *p = 0;
595     }
596     else
597       buf2[0] = 0;
598     mutt_format_s (dest, destlen, prefix, buf2);
599     break;
600
601   case 'v':
602     if (mutt_addr_is_user (hdr->env->from)) {
603       if (hdr->env->to)
604         mutt_format_s (buf2, sizeof (buf2), prefix,
605                        mutt_get_name (hdr->env->to));
606       else if (hdr->env->cc)
607         mutt_format_s (buf2, sizeof (buf2), prefix,
608                        mutt_get_name (hdr->env->cc));
609       else
610         *buf2 = 0;
611     }
612     else
613       mutt_format_s (buf2, sizeof (buf2), prefix,
614                      mutt_get_name (hdr->env->from));
615     if ((p = strpbrk (buf2, " %@")))
616       *p = 0;
617     mutt_format_s (dest, destlen, prefix, buf2);
618     break;
619
620   case 'W':
621     if (!optional)
622       mutt_format_s (dest, destlen, prefix,
623                      hdr->env->organization ? hdr->env->organization : "");
624     else if (!hdr->env->organization)
625       optional = 0;
626     break;
627
628   case 'Z':
629
630     ch = ' ';
631
632     if (WithCrypto && hdr->security & GOODSIGN)
633       ch = 'S';
634     else if (WithCrypto && hdr->security & ENCRYPT)
635       ch = 'P';
636     else if (WithCrypto && hdr->security & SIGN)
637       ch = 's';
638     else if ((WithCrypto & APPLICATION_PGP) && hdr->security & PGPKEY)
639       ch = 'K';
640
641     snprintf (buf2, sizeof (buf2),
642               "%c%c%c", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
643                                              ((hdr->read
644                                                && (ctx
645                                                    && ctx->msgnotreadyet !=
646                                                    hdr->msgno))
647                                               ? (hdr->
648                                                  replied ? 'r' : ' ') : (hdr->
649                                                                          old ?
650                                                                          'O' :
651                                                                          'N')))),
652               hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
653               hdr->tagged ? '*' : (hdr->
654                                    flagged ? '!' : (Tochars
655                                                     &&
656                                                     ((i =
657                                                       mutt_user_is_recipient
658                                                       (hdr)) <
659                                                      mutt_strlen (Tochars)) ?
660                                                     Tochars[i] : ' ')));
661     mutt_format_s (dest, destlen, prefix, buf2);
662     break;
663
664   case 'y':
665     if (optional)
666       optional = hdr->env->x_label ? 1 : 0;
667
668     mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
669     break;
670
671   case 'Y':
672     if (hdr->env->x_label) {
673       i = 1;                    /* reduce reuse recycle */
674       htmp = NULL;
675       if (flags & M_FORMAT_TREE
676           && (hdr->thread->prev && hdr->thread->prev->message
677               && hdr->thread->prev->message->env->x_label))
678         htmp = hdr->thread->prev->message;
679       else if (flags & M_FORMAT_TREE
680                && (hdr->thread->parent && hdr->thread->parent->message
681                    && hdr->thread->parent->message->env->x_label))
682         htmp = hdr->thread->parent->message;
683       if (htmp && mutt_strcasecmp (hdr->env->x_label,
684                                    htmp->env->x_label) == 0)
685         i = 0;
686     }
687     else
688       i = 0;
689
690     if (optional)
691       optional = i;
692
693     if (i)
694       mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
695     else
696       mutt_format_s (dest, destlen, prefix, "");
697
698     break;
699
700   default:
701     snprintf (dest, destlen, "%%%s%c", prefix, op);
702     break;
703   }
704
705   if (optional)
706     mutt_FormatString (dest, destlen, ifstring, hdr_format_str,
707                        (unsigned long) hfi, flags);
708   else if (flags & M_FORMAT_OPTIONAL)
709     mutt_FormatString (dest, destlen, elsestring, hdr_format_str,
710                        (unsigned long) hfi, flags);
711
712   return (src);
713 #undef THREAD_NEW
714 #undef THREAD_OLD
715 }
716
717 void
718 _mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT * ctx,
719                    HEADER * hdr, format_flag flags)
720 {
721   struct hdr_format_info hfi;
722
723   hfi.hdr = hdr;
724   hfi.ctx = ctx;
725
726   mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi,
727                      flags);
728 }