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