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