build tools on configure
[apps/madmutt.git] / postpone.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
13 #include <lib-mime/mime.h>
14
15 #include <lib-ui/enter.h>
16 #include <lib-ui/menu.h>
17 #include <lib-ui/lib-ui.h>
18 #include <lib-mx/mx.h>
19
20 #include <lib-sys/unix.h>
21
22 #include "mutt.h"
23 #include "handler.h"
24 #include "sort.h"
25 #include "thread.h"
26 #include "crypt.h"
27
28 #include <imap/imap.h>
29
30 static short PostCount = 0;
31 static CONTEXT *PostContext = NULL;
32 static short UpdateNumPostponed = 0;
33
34 /* Return the number of postponed messages.
35  * if force is 0, use a cached value if it is costly to get a fresh
36  * count (IMAP) - else check.
37  */
38 int mutt_num_postponed (int force)
39 {
40   struct stat st;
41   CONTEXT ctx;
42
43   static time_t LastModify = 0;
44   static char *OldPostponed = NULL;
45
46   if (UpdateNumPostponed) {
47     UpdateNumPostponed = 0;
48     force = 1;
49   }
50
51   if (Postponed != OldPostponed) {
52     OldPostponed = Postponed;
53     LastModify = 0;
54     force = 1;
55   }
56
57   if (!Postponed)
58     return 0;
59
60   /* LastModify is useless for IMAP */
61   if (imap_is_magic (Postponed, NULL) == M_IMAP) {
62     if (force) {
63       short newpc;
64
65       newpc = imap_mailbox_check (Postponed, 0);
66       if (newpc >= 0) {
67         PostCount = newpc;
68       }
69     }
70     return PostCount;
71   }
72
73   if (stat (Postponed, &st) == -1) {
74     PostCount = 0;
75     LastModify = 0;
76     return (0);
77   }
78
79   if (S_ISDIR (st.st_mode)) {
80     /* if we have a maildir mailbox, we need to stat the "new" dir */
81
82     char buf[_POSIX_PATH_MAX];
83
84     snprintf (buf, sizeof (buf), "%s/new", Postponed);
85     if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
86       PostCount = 0;
87       LastModify = 0;
88       return 0;
89     }
90   }
91
92   if (LastModify < st.st_mtime) {
93     LastModify = st.st_mtime;
94
95     if (access (Postponed, R_OK | F_OK) != 0)
96       return (PostCount = 0);
97     if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
98       PostCount = 0;
99     else
100       PostCount = ctx.msgcount;
101     mx_fastclose_mailbox (&ctx);
102   }
103
104   return (PostCount);
105 }
106
107 void mutt_update_num_postponed (void)
108 {
109   UpdateNumPostponed = 1;
110 }
111
112 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
113 {
114   CONTEXT *ctx = (CONTEXT *)menu->data;
115   _mutt_make_string(s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry], 0);
116 }
117
118 static HEADER *select_msg (void)
119 {
120   MUTTMENU *menu;
121   int i, done = 0, r = -1;
122   short orig_sort;
123
124   menu = mutt_new_menu ();
125   menu->make_entry = post_entry;
126   menu->menu = MENU_POST;
127   menu->max = PostContext->msgcount;
128   menu->title = _("Postponed Messages");
129   menu->data = PostContext;
130
131   /* The postponed mailbox is setup to have sorting disabled, but the global
132    * Sort variable may indicate something different.   Sorting has to be
133    * disabled while the postpone menu is being displayed. */
134   orig_sort = Sort;
135   Sort = SORT_ORDER;
136
137   while (!done) {
138     switch (i = mutt_menuLoop (menu)) {
139     case OP_DELETE:
140     case OP_UNDELETE:
141       mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
142                      (i == OP_DELETE) ? 1 : 0);
143       PostCount = PostContext->msgcount - PostContext->deleted;
144       if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
145         menu->oldcurrent = menu->current;
146         menu->current++;
147         if (menu->current >= menu->top + menu->pagelen) {
148           menu->top = menu->current;
149           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
150         }
151         else
152           menu->redraw |= REDRAW_MOTION_RESYNCH;
153       }
154       else
155         menu->redraw = REDRAW_CURRENT;
156       break;
157
158     case OP_GENERIC_SELECT_ENTRY:
159       r = menu->current;
160       done = 1;
161       break;
162
163     case OP_EXIT:
164       done = 1;
165       break;
166     }
167   }
168
169   Sort = orig_sort;
170   mutt_menuDestroy (&menu);
171   return (r > -1 ? PostContext->hdrs[r] : NULL);
172 }
173
174 /* args:
175  *      ctx     Context info, used when recalling a message to which
176  *              we reply.
177  *      hdr     envelope/attachment info for recalled message
178  *      cur     if message was a reply, `cur' is set to the message which
179  *              `hdr' is in reply to
180  *      fcc     fcc for the recalled message
181  *      fcclen  max length of fcc
182  *
183  * return vals:
184  *      -1              error/no messages
185  *      0               normal exit
186  *      SENDREPLY       recalled message is a reply
187  */
188 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
189                         ssize_t fcclen)
190 {
191   HEADER *h;
192   int code = SENDPOSTPONED;
193   string_list_t *tmp;
194   string_list_t *last = NULL;
195   string_list_t *next;
196   char *p;
197   int opt_delete;
198
199   if (!Postponed)
200     return (-1);
201
202   if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
203     PostCount = 0;
204     mutt_error _("No postponed messages.");
205
206     return (-1);
207   }
208
209   if (!PostContext->msgcount) {
210     PostCount = 0;
211     mx_close_mailbox (PostContext, NULL);
212     p_delete(&PostContext);
213     mutt_error _("No postponed messages.");
214
215     return (-1);
216   }
217
218   if (PostContext->msgcount == 1) {
219     /* only one message, so just use that one. */
220     h = PostContext->hdrs[0];
221   }
222   else if ((h = select_msg ()) == NULL) {
223     mx_close_mailbox (PostContext, NULL);
224     p_delete(&PostContext);
225     return (-1);
226   }
227
228   if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
229     mx_fastclose_mailbox (PostContext);
230     p_delete(&PostContext);
231     return (-1);
232   }
233
234   /* finished with this message, so delete it. */
235   mutt_set_flag (PostContext, h, M_DELETE, 1);
236
237   /* and consider it saved, so that it won't be moved to the trash folder */
238   mutt_set_flag (PostContext, h, M_APPENDED, 1);
239
240   /* update the count for the status display */
241   PostCount = PostContext->msgcount - PostContext->deleted;
242
243   /* avoid the "purge deleted messages" prompt */
244   opt_delete = quadoption (OPT_DELETE);
245   set_quadoption (OPT_DELETE, M_YES);
246   mx_close_mailbox (PostContext, NULL);
247   set_quadoption (OPT_DELETE, opt_delete);
248
249   p_delete(&PostContext);
250
251   for (tmp = hdr->env->userhdrs; tmp;) {
252     if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
253       if (ctx) {
254         /* if a mailbox is currently open, look to see if the orignal message
255            the user attempted to reply to is in this mailbox */
256         p = vskipspaces(tmp->data + 18);
257         if (!ctx->id_hash)
258           ctx->id_hash = mutt_make_id_hash (ctx);
259         *cur = hash_find (ctx->id_hash, p);
260       }
261
262       /* Remove the X-Mutt-References: header field. */
263       next = tmp->next;
264       if (last)
265         last->next = tmp->next;
266       else
267         hdr->env->userhdrs = tmp->next;
268       tmp->next = NULL;
269       string_list_wipe(&tmp);
270       tmp = next;
271       if (*cur)
272         code |= SENDREPLY;
273     }
274     else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
275       p = vskipspaces(tmp->data + 11);
276       m_strcpy(fcc, fcclen, p);
277       mutt_pretty_mailbox (fcc);
278
279       /* remove the X-Mutt-Fcc: header field */
280       next = tmp->next;
281       if (last)
282         last->next = tmp->next;
283       else
284         hdr->env->userhdrs = tmp->next;
285       tmp->next = NULL;
286       string_list_wipe(&tmp);
287       tmp = next;
288     }
289     else if (m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0) {
290       hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
291       hdr->security |= APPLICATION_PGP;
292
293       /* remove the pgp field */
294       next = tmp->next;
295       if (last)
296         last->next = tmp->next;
297       else
298         hdr->env->userhdrs = tmp->next;
299       tmp->next = NULL;
300       string_list_wipe(&tmp);
301       tmp = next;
302     }
303     else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
304       hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
305       hdr->security |= APPLICATION_SMIME;
306
307       /* remove the smime field */
308       next = tmp->next;
309       if (last)
310         last->next = tmp->next;
311       else
312         hdr->env->userhdrs = tmp->next;
313       tmp->next = NULL;
314       string_list_wipe(&tmp);
315       tmp = next;
316     }
317     else {
318       last = tmp;
319       tmp = tmp->next;
320     }
321   }
322   return (code);
323 }
324
325 int mutt_parse_crypt_hdr (char *p, int set_signas)
326 {
327   int pgp = 0;
328   char pgp_sign_as[LONG_STRING] = "\0", *q;
329   char smime_cryptalg[LONG_STRING] = "\0";
330
331   for (p = vskipspaces(p); *p; p++) {
332     switch (*p) {
333     case 'e':
334     case 'E':
335       pgp |= ENCRYPT;
336       break;
337
338     case 's':
339     case 'S':
340       pgp |= SIGN;
341       q = pgp_sign_as;
342
343       if (*(p + 1) == '<') {
344         for (p += 2;
345              *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
346              *q++ = *p++);
347
348         if (*p != '>') {
349           mutt_error _("Illegal PGP header");
350
351           return 0;
352         }
353       }
354
355       *q = '\0';
356       break;
357
358       /* This used to be the micalg parameter.
359        * 
360        * It's no longer needed, so we just skip the parameter in order
361        * to be able to recall old messages.
362        */
363     case 'm':
364     case 'M':
365       if (*(p + 1) == '<') {
366         for (p += 2; *p && *p != '>'; p++);
367         if (*p != '>') {
368           mutt_error _("Illegal PGP header");
369
370           return 0;
371         }
372       }
373
374       break;
375
376
377     case 'c':
378     case 'C':
379       q = smime_cryptalg;
380
381       if (*(p + 1) == '<') {
382         for (p += 2;
383              *p && *p != '>'
384              && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
385              *q++ = *p++);
386
387         if (*p != '>') {
388           mutt_error _("Illegal S/MIME header");
389
390           return 0;
391         }
392       }
393
394       *q = '\0';
395       break;
396
397     case 'i':
398     case 'I':
399       pgp |= INLINE;
400       break;
401
402     default:
403       mutt_error _("Illegal PGP header");
404       return 0;
405     }
406
407   }
408
409   /* the cryptalg field must not be empty */
410   if (*smime_cryptalg)
411     m_strreplace(&SmimeCryptAlg, smime_cryptalg);
412
413   if (set_signas || *pgp_sign_as)
414     m_strreplace(&PgpSignAs, pgp_sign_as);
415
416   return pgp;
417 }
418
419
420
421 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
422                            HEADER * hdr, short weed)
423 {
424   MESSAGE *msg = NULL;
425   char file[_POSIX_PATH_MAX];
426   BODY *b;
427   FILE *bfp;
428
429   int rv = -1;
430   STATE s;
431
432   p_clear(&s, 1);
433
434   if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
435     return (-1);
436
437   if (!fp)
438     fp = msg->fp;
439
440   bfp = fp;
441
442   /* parse the message header and MIME structure */
443
444   fseeko (fp, hdr->offset, 0);
445   newhdr->offset = hdr->offset;
446   newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
447   newhdr->content->length = hdr->content->length;
448   mutt_parse_part (fp, newhdr->content);
449
450   p_delete(&newhdr->env->message_id);
451   p_delete(&newhdr->env->mail_followup_to);        /* really? */
452
453   /* decrypt pgp/mime encoded messages */
454
455   if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
456       && mutt_is_multipart_encrypted (newhdr->content))
457   {
458     int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
459     newhdr->security |= ENCRYPT | ccap;
460     mutt_message _("Decrypting message...");
461
462     if (((ccap & APPLICATION_PGP)
463          && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
464         || ((ccap & APPLICATION_SMIME)
465             && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
466         || b == NULL)
467     {
468       mx_close_message (&msg);
469       envelope_delete(&newhdr->env);
470       body_list_wipe(&newhdr->content);
471       mutt_error _("Decryption failed.");
472
473       return -1;
474     }
475
476     body_list_wipe(&newhdr->content);
477     newhdr->content = b;
478
479     mutt_clear_error ();
480   }
481
482   /* 
483    * remove a potential multipart/signed layer - useful when
484    * resending messages 
485    */
486
487   if (mutt_is_multipart_signed (newhdr->content)) {
488     newhdr->security |= SIGN;
489     if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
490                          "application/pgp-signature") == 0)
491       newhdr->security |= APPLICATION_PGP;
492     else
493       newhdr->security |= APPLICATION_SMIME;
494
495     /* destroy the signature */
496     body_list_wipe(&newhdr->content->parts->next);
497     newhdr->content = mutt_remove_multipart (newhdr->content);
498   }
499
500
501   /* 
502    * We don't need no primary multipart.
503    * Note: We _do_ preserve messages!
504    * 
505    * XXX - we don't handle multipart/alternative in any 
506    * smart way when sending messages.  However, one may
507    * consider this a feature.
508    * 
509    */
510
511   if (newhdr->content->type == TYPEMULTIPART)
512     newhdr->content = mutt_remove_multipart (newhdr->content);
513
514   s.fpin = bfp;
515
516   /* create temporary files for all attachments */
517   for (b = newhdr->content; b; b = b->next) {
518
519     /* what follows is roughly a receive-mode variant of
520      * mutt_get_tmp_attachment () from muttlib.c
521      */
522
523     file[0] = '\0';
524     if (b->filename) {
525       m_strcpy(file, sizeof(file), b->filename);
526       b->d_filename = m_strdup(b->filename);
527     }
528     else {
529       /* avoid Content-Disposition: header with temporary filename */
530       b->use_disp = 0;
531     }
532
533     /* set up state flags */
534
535     s.flags = 0;
536
537     if (b->type == TYPETEXT) {
538       b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter,
539                                                             "x-mutt-noconv"));
540       if (b->noconv)
541         s.flags |= M_CHARCONV;
542
543       parameter_delval(&b->parameter, "x-mutt-noconv");
544     }
545
546     s.fpout = m_tempfile(file, sizeof(file), NONULL(mod_core.tmpdir), file);
547     if (!s.fpout)
548       goto bail;
549
550     if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
551
552       mutt_body_handler (b, &s);
553
554       newhdr->security |= mutt_is_application_pgp (newhdr->content);
555
556       b->type = TYPETEXT;
557       m_strreplace(&b->subtype, "plain");
558       parameter_delval(&b->parameter, "x-action");
559     }
560     else
561       mutt_decode_attachment (b, &s);
562
563     if (m_fclose(&s.fpout) != 0)
564       goto bail;
565
566     m_strreplace(&b->filename, file);
567     b->unlink = 1;
568
569     mutt_stamp_attachment (b);
570
571     body_list_wipe(&b->parts);
572     if (b->hdr)
573       b->hdr->content = NULL;   /* avoid dangling pointer */
574   }
575
576   /* Fix encryption flags. */
577
578   /* No inline if multipart. */
579   if ((newhdr->security & INLINE) && newhdr->content->next)
580     newhdr->security &= ~INLINE;
581
582   /* Theoretically, both could be set. Take the one the user wants to set by default. */
583   if ((newhdr->security & APPLICATION_PGP)
584       && (newhdr->security & APPLICATION_SMIME)) {
585     if (mod_crypt.smime_is_default)
586       newhdr->security &= ~APPLICATION_PGP;
587     else
588       newhdr->security &= ~APPLICATION_SMIME;
589   }
590
591   rv = 0;
592
593 bail:
594
595   /* that's it. */
596   if (bfp != fp)
597     m_fclose(&bfp);
598   if (msg)
599     mx_close_message (&msg);
600
601   if (rv == -1) {
602     envelope_delete(&newhdr->env);
603     body_list_wipe(&newhdr->content);
604   }
605
606   return rv;
607 }