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