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