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