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