e0dfe2e079652e4a55d3e7d25bb88a03433c7b34
[apps/madmutt.git] / commands.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 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 #include <utime.h>
13
14 #include <lib-mime/mime.h>
15
16 #include <lib-sys/exit.h>
17 #include <lib-sys/unix.h>
18
19 #include <lib-ui/curses.h>
20 #include <lib-ui/enter.h>
21 #include <lib-ui/menu.h>
22 #include <lib-mx/mx.h>
23
24 #include "mutt.h"
25 #include "alias.h"
26 #include "recvattach.h"
27 #include "sort.h"
28 #include "copy.h"
29 #include "pager.h"
30 #include <lib-crypt/crypt.h>
31 #include "mutt_idna.h"
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #include <imap/imap.h>
37
38 /* The folder the user last saved to.  Used by ci_save_message() */
39 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
40
41 int mutt_display_message (HEADER * cur)
42 {
43   char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
44   int rc = 0, builtin = 0;
45   int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
46   FILE *fpout = NULL;
47   FILE *fpfilterout = NULL;
48   MESSAGE *msg = NULL;
49   pid_t filterpid = -1;
50   int res = 0;
51
52   snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
53             cur->content->subtype);
54
55   mutt_parse_mime_message (Context, cur);
56   mutt_message_hook (Context, cur, M_MESSAGEHOOK);
57
58   fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
59   if (!fpout) {
60     mutt_error _("Could not create temporary file!");
61     return 0;
62   }
63
64   if (DisplayFilter && *DisplayFilter) {
65     fpfilterout = fpout;
66     fpout = NULL;
67     /* mutt_endwin (NULL); */
68     filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
69                                        -1, fileno (fpfilterout), -1);
70     if (filterpid < 0) {
71       mutt_error (_("Cannot create display filter"));
72       m_fclose(&fpfilterout);
73       unlink (tempfile);
74       return 0;
75     }
76   }
77
78   if (!Pager || m_strcmp(Pager, "builtin") == 0)
79     builtin = 1;
80   else {
81     mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
82     fputs (buf, fpout);
83     fputs ("\n\n", fpout);
84   }
85
86   msg = mx_open_message (Context, cur->msgno);
87   if (msg == NULL) res = -1;
88
89   if (res != -1) {
90     /* see if crytpo is needed for this message.  if so, we should exit curses */
91     if (cur->security) {
92       if (cur->security & ENCRYPT) {
93         if (cur->security & APPLICATION_SMIME)
94           crypt_smime_getkeys (cur->env);
95         if (!crypt_valid_passphrase (cur->security))
96           return 0;
97
98         cmflags |= M_CM_VERIFY;
99       }
100       else if (cur->security & SIGN) {
101         /* find out whether or not the verify signature */
102         if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
103             M_YES) {
104           cmflags |= M_CM_VERIFY;
105         }
106       }
107     }
108
109     if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
110       if (cur->security & APPLICATION_PGP) {
111         if (cur->env->from)
112           crypt_pgp_invoke_getkeys (cur->env->from);
113
114         crypt_invoke_message (APPLICATION_PGP);
115       }
116
117       if (cur->security & APPLICATION_SMIME)
118         crypt_invoke_message (APPLICATION_SMIME);
119     }
120
121     res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
122                              (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
123                              CH_DECODE | CH_FROM);
124     if (res == 0 && (ferror(fpout) || feof(fpout))) {
125       res = -1;
126     }
127
128     mx_close_message (&msg);
129   }
130
131   if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
132     mutt_error (_("Could not copy message"));
133     if (fpfilterout != NULL) {
134       mutt_wait_filter (filterpid);
135       m_fclose(&fpfilterout);
136     }
137 #if 0
138     /* this is maybe just plain wrong but it makes the pager display
139      * what we have; i.e. for the crypto stuff we only get
140      * 'Could not copy message' for invalid passphrases, no PGP output
141      * not nothing; so just display what we have...
142      * - pdmef
143      */
144     mutt_unlink (tempfile);
145     return 0;
146 #endif
147   }
148
149   if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
150     mutt_any_key_to_continue (NULL);
151
152   m_fclose(&fpfilterout);   /* XXX - check result? */
153
154
155   /* update crypto information for this message */
156   cur->security &= ~(GOODSIGN|BADSIGN);
157   cur->security |= crypt_query (cur->content);
158
159   /* Remove color cache for this message, in case there
160      are color patterns for both ~g and ~V */
161   cur->pair = 0;
162
163   if (builtin) {
164     pager_t info;
165
166     if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
167       if (cur->security & GOODSIGN) {
168         if (!crypt_smime_verify_sender (cur))
169           mutt_message (_("S/MIME signature successfully verified."));
170         else
171           mutt_error (_("S/MIME certificate owner does not match sender."));
172       }
173       else if (cur->security & PARTSIGN)
174         mutt_message (_
175                       ("Warning: Part of this message has not been signed."));
176       else if (cur->security & SIGN || cur->security & BADSIGN)
177         mutt_error (_("S/MIME signature could NOT be verified."));
178     }
179
180     if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
181       if (cur->security & GOODSIGN)
182         mutt_message (_("PGP signature successfully verified."));
183       else if (cur->security & PARTSIGN)
184         mutt_message (_
185                       ("Warning: Part of this message has not been signed."));
186       else if (cur->security & SIGN)
187         mutt_message (_("PGP signature could NOT be verified."));
188     }
189
190     /* Invoke the builtin pager */
191     p_clear(&info, 1);
192     info.hdr = cur;
193     info.ctx = Context;
194     rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
195   }
196   else {
197     int r;
198
199     mutt_endwin (NULL);
200     snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
201     if ((r = mutt_system (buf)) == -1)
202       mutt_error (_("Error running \"%s\"!"), buf);
203     unlink (tempfile);
204     keypad (stdscr, TRUE);
205     if (r != -1)
206       mutt_set_flag (Context, cur, M_READ, 1);
207     if (r != -1 && option (OPTPROMPTAFTER)) {
208       mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
209       rc = km_dokey (MENU_PAGER);
210     }
211     else
212       rc = 0;
213   }
214
215   return rc;
216 }
217
218 void ci_bounce_message (HEADER * h, int *redraw)
219 {
220   char prompt[SHORT_STRING];
221   char buf[HUGE_STRING] = { 0 };
222   address_t *adr = NULL;
223   char *err = NULL;
224   int rc;
225
226   if (h)
227     m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
228   else
229     m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
230
231   rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
232
233   if (option (OPTNEEDREDRAW)) {
234     unset_option (OPTNEEDREDRAW);
235     *redraw = REDRAW_FULL;
236   }
237
238   if (rc || !buf[0])
239     return;
240
241   if (!(adr = rfc822_parse_adrlist (adr, buf))) {
242     mutt_error _("Error parsing address!");
243
244     return;
245   }
246
247   adr = mutt_expand_aliases (adr);
248
249   if (mutt_addrlist_to_idna (adr, &err) < 0) {
250     mutt_error (_("Bad IDN: '%s'"), err);
251     p_delete(&err);
252     address_list_wipe(&adr);
253     return;
254   }
255
256   buf[0] = 0;
257   rfc822_write_address (buf, sizeof (buf), adr, 1);
258
259 #define extra_space (15 + 7 + 2)
260   snprintf (prompt, sizeof (prompt),
261             (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
262             buf);
263
264   if (mutt_strwidth (prompt) > COLS - extra_space) {
265     mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
266                        prompt, sizeof(prompt), 0);
267     m_strcat(prompt, sizeof(prompt), "...?");
268   } else {
269     m_strcat(prompt, sizeof(prompt), "?");
270   }
271
272   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
273     address_list_wipe(&adr);
274     CLEARLINE (LINES - 1);
275     mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
276     return;
277   }
278
279   CLEARLINE (LINES - 1);
280
281   rc = mutt_bounce_message (NULL, h, adr);
282   address_list_wipe(&adr);
283   /* If no error, or background, display message. */
284   if ((rc == 0) || (rc == S_BKG))
285     mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
286 }
287
288 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
289 {
290   if (decode) {
291     *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
292     *chflags |= CH_DECODE | CH_REORDER;
293
294     if (option (OPTWEED)) {
295       *chflags |= CH_WEED;
296       *cmflags |= M_CM_WEED;
297     }
298   }
299
300   if (print)
301     *cmflags |= M_CM_PRINTING;
302
303 }
304
305 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
306 {
307   int cmflags = 0;
308   int chflags = CH_FROM;
309
310   pipe_set_flags (decode, print, &cmflags, &chflags);
311
312   if (decode && h->security & ENCRYPT) {
313     if (!crypt_valid_passphrase (h->security))
314       return;
315     endwin ();
316   }
317
318   if (decode)
319     mutt_parse_mime_message (Context, h);
320
321   mutt_copy_message (fp, Context, h, cmflags, chflags);
322 }
323
324
325 /* the following code is shared between printing and piping */
326
327 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
328                               int split, const char *sep)
329 {
330
331   int i, rc = 0;
332   pid_t thepid;
333   FILE *fpout;
334
335 /*   mutt_endwin (NULL); 
336
337      is this really needed here ? 
338      it makes the screen flicker on pgp and s/mime messages,
339      before asking for a passphrase...
340                                      Oliver Ehli */
341   if (h) {
342
343     mutt_message_hook (Context, h, M_MESSAGEHOOK);
344
345     if (decode) {
346       mutt_parse_mime_message (Context, h);
347       if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
348         return 1;
349     }
350     mutt_endwin (NULL);
351
352     if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
353       mutt_perror (_("Can't create filter process"));
354
355       return 1;
356     }
357
358     pipe_msg (h, fpout, decode, print);
359     fclose (fpout);
360     rc = mutt_wait_filter (thepid);
361   }
362   else {                        /* handle tagged messages */
363
364     if (decode) {
365       for (i = 0; i < Context->vcount; i++)
366         if (Context->hdrs[Context->v2r[i]]->tagged) {
367           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
368                              M_MESSAGEHOOK);
369           mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
370           if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
371               !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
372                                        security))
373             return 1;
374         }
375     }
376
377     if (split) {
378       for (i = 0; i < Context->vcount; i++) {
379         if (Context->hdrs[Context->v2r[i]]->tagged) {
380           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
381                              M_MESSAGEHOOK);
382           mutt_endwin (NULL);
383           if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
384             mutt_perror (_("Can't create filter process"));
385
386             return 1;
387           }
388           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
389           /* add the message separator */
390           if (sep)
391             fputs (sep, fpout);
392           m_fclose(&fpout);
393           if (mutt_wait_filter (thepid) != 0)
394             rc = 1;
395         }
396       }
397     }
398     else {
399       mutt_endwin (NULL);
400       if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
401         mutt_perror (_("Can't create filter process"));
402
403         return 1;
404       }
405       for (i = 0; i < Context->vcount; i++) {
406         if (Context->hdrs[Context->v2r[i]]->tagged) {
407           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
408                              M_MESSAGEHOOK);
409           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
410           /* add the message separator */
411           if (sep)
412             fputs (sep, fpout);
413         }
414       }
415       m_fclose(&fpout);
416       if (mutt_wait_filter (thepid) != 0)
417         rc = 1;
418     }
419   }
420
421   if (rc || option (OPTWAITKEY))
422     mutt_any_key_to_continue (NULL);
423   return rc;
424 }
425
426 void mutt_pipe_message (HEADER * h)
427 {
428   char buffer[LONG_STRING];
429
430   buffer[0] = 0;
431   if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
432       != 0 || !buffer[0])
433     return;
434
435   mutt_expand_path (buffer, sizeof (buffer));
436   _mutt_pipe_message (h, buffer,
437                       option (OPTPIPEDECODE),
438                       0, option (OPTPIPESPLIT), PipeSep);
439 }
440
441 void mutt_print_message (HEADER * h)
442 {
443
444   if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
445     mutt_message (_("No printing command has been defined."));
446     return;
447   }
448
449   if (query_quadoption (OPT_PRINT,
450                         h ? _("Print message?") : _("Print tagged messages?"))
451       != M_YES)
452     return;
453
454   if (_mutt_pipe_message (h, PrintCmd,
455                           option (OPTPRINTDECODE),
456                           1, option (OPTPRINTSPLIT), "\f") == 0)
457     mutt_message (h ? _("Message printed") : _("Messages printed"));
458   else
459     mutt_message (h ? _("Message could not be printed") :
460                   _("Messages could not be printed"));
461 }
462
463
464 int mutt_select_sort (int reverse)
465 {
466   int method = Sort;            /* save the current method in case of abort */
467
468   switch (mutt_multi_choice (reverse ?
469                              _
470                              ("Rev-Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: ")
471                              :
472                              _
473                              ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
474                              _("dfrsotuzcp"))) {
475   case -1:                     /* abort - don't resort */
476     return -1;
477
478   case 1:                      /* (d)ate */
479     Sort = SORT_DATE;
480     break;
481
482   case 2:                      /* (f)rm */
483     Sort = SORT_FROM;
484     break;
485
486   case 3:                      /* (r)ecv */
487     Sort = SORT_RECEIVED;
488     break;
489
490   case 4:                      /* (s)ubj */
491     Sort = SORT_SUBJECT;
492     break;
493
494   case 5:                      /* t(o) */
495     Sort = SORT_TO;
496     break;
497
498   case 6:                      /* (t)hread */
499     Sort = SORT_THREADS;
500     break;
501
502   case 7:                      /* (u)nsort */
503     Sort = SORT_ORDER;
504     break;
505
506   case 8:                      /* si(z)e */
507     Sort = SORT_SIZE;
508     break;
509
510   case 9:                      /* s(c)ore */
511     Sort = SORT_SCORE;
512     break;
513
514   case 10:                     /* s(p)am */
515     Sort = SORT_SPAM;
516     break;
517   }
518   if (reverse)
519     Sort |= SORT_REVERSE;
520
521   return (Sort != method ? 0 : -1);     /* no need to resort if it's the same */
522 }
523
524 /* invoke a command in a subshell */
525 void mutt_shell_escape (void)
526 {
527   char buf[LONG_STRING];
528
529   buf[0] = 0;
530   if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
531     if (!buf[0] && Shell)
532       m_strcpy(buf, sizeof(buf), Shell);
533     if (buf[0]) {
534       CLEARLINE (LINES - 1);
535       mutt_endwin (NULL);
536       fflush (stdout);
537       if (mutt_system (buf) != 0 || option (OPTWAITKEY))
538         mutt_any_key_to_continue (NULL);
539     }
540   }
541 }
542
543 /* enter a mutt command */
544 void mutt_enter_command (void)
545 {
546   BUFFER err, token;
547   char buffer[LONG_STRING], errbuf[SHORT_STRING];
548   int r;
549
550   buffer[0] = 0;
551   if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
552       || !buffer[0])
553     return;
554   err.data = errbuf;
555   err.dsize = sizeof (errbuf);
556   p_clear(&token, 1);
557   r = mutt_parse_rc_line (buffer, &token, &err);
558   p_delete(&token.data);
559   if (errbuf[0]) {
560     /* since errbuf could potentially contain printf() sequences in it,
561        we must call mutt_error() in this fashion so that vsprintf()
562        doesn't expect more arguments that we passed */
563     if (r == 0)
564       mutt_message ("%s", errbuf);
565     else
566       mutt_error ("%s", errbuf);
567   }
568 }
569
570 void mutt_display_address (ENVELOPE * env)
571 {
572   const char *pfx = NULL;
573   char buf[SHORT_STRING];
574   address_t *adr = NULL;
575
576   adr = mutt_get_address(env, &pfx);
577
578   if (!adr)
579     return;
580
581   /* 
582    * Note: We don't convert IDNA to local representation this time.
583    * That is intentional, so the user has an opportunity to copy &
584    * paste the on-the-wire form of the address to other, IDN-unable
585    * software. 
586    */
587
588   buf[0] = 0;
589   rfc822_write_address (buf, sizeof (buf), adr, 0);
590   mutt_message ("%s: %s", pfx, buf);
591 }
592
593 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
594                             int *cmflags, int *chflags)
595 {
596   *cmflags = 0;
597   *chflags = CH_UPDATE_LEN;
598
599   if (!decode && decrypt && (hdr->security & ENCRYPT)) {
600     if (mutt_is_multipart_encrypted (hdr->content)) {
601       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
602       *cmflags = M_CM_DECODE_PGP;
603     }
604     else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
605       decode = 1;
606     else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
607       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
608       *cmflags = M_CM_DECODE_SMIME;
609     }
610   }
611
612   if (decode) {
613     *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
614     *cmflags = M_CM_DECODE | M_CM_CHARCONV;
615
616     if (!decrypt) {             /* If decode doesn't kick in for decrypt, */
617       *chflags |= CH_DECODE;    /* then decode RFC 2047 headers, */
618
619       if (option (OPTWEED)) {
620         *chflags |= CH_WEED;    /* and respect $weed. */
621         *cmflags |= M_CM_WEED;
622       }
623     }
624   }
625 }
626
627 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
628                          int decrypt) {
629   int cmflags, chflags;
630   int rc;
631
632   set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
633
634   if (decode || decrypt)
635     mutt_parse_mime_message (Context, h);
636
637   if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
638     return rc;
639
640   if (delete) {
641     mutt_set_flag (Context, h, M_DELETE, 1);
642     if (option (OPTDELETEUNTAG))
643       mutt_set_flag (Context, h, M_TAG, 0);
644     mutt_set_flag (Context, h, M_APPENDED, 1);
645   }
646   return (0);
647 }
648
649 /* returns 0 if the copy/save was successful, or -1 on error/abort */
650 int mutt_save_message (HEADER * h, int delete,
651                        int decode, int decrypt, int *redraw) {
652   int i, need_buffy_cleanup;
653   int need_passphrase = 0, app = 0;
654   char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
655   CONTEXT ctx;
656   struct stat st;
657   struct utimbuf ut;
658
659   *redraw = 0;
660
661
662   snprintf (prompt, sizeof (prompt),
663             decode ? (delete ? _("Decode-save%s to mailbox") :
664                       _("Decode-copy%s to mailbox")) :
665             (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
666                         _("Decrypt-copy%s to mailbox")) :
667              (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
668             h ? "" : _(" tagged"));
669
670
671   if (h) {
672     need_passphrase = h->security & ENCRYPT;
673     app = h->security;
674     mutt_message_hook (Context, h, M_MESSAGEHOOK);
675     mutt_default_save (buf, sizeof (buf), h);
676   }
677   else {
678     /* look for the first tagged message */
679
680     for (i = 0; i < Context->vcount; i++) {
681       if (Context->hdrs[Context->v2r[i]]->tagged) {
682         h = Context->hdrs[Context->v2r[i]];
683         break;
684       }
685     }
686
687
688     if (h) {
689       mutt_message_hook (Context, h, M_MESSAGEHOOK);
690       mutt_default_save (buf, sizeof (buf), h);
691       need_passphrase = h->security & ENCRYPT;
692       app = h->security;
693       h = NULL;
694     }
695   }
696
697   mutt_pretty_mailbox (buf);
698   if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
699     return (-1);
700
701   if (*redraw != REDRAW_FULL) {
702     if (!h)
703       *redraw = REDRAW_INDEX | REDRAW_STATUS;
704     else
705       *redraw = REDRAW_STATUS;
706   }
707
708   if (!buf[0])
709     return (-1);
710
711   /* This is an undocumented feature of ELM pointed out to me by Felix von
712    * Leitner <leitner@prz.fu-berlin.de>
713    */
714   if (m_strcmp(buf, ".") == 0)
715     m_strcpy(buf, sizeof(buf), LastSaveFolder);
716   else
717     m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
718
719   mutt_expand_path (buf, sizeof (buf));
720
721   /* check to make sure that this file is really the one the user wants */
722   if (mutt_save_confirm (buf, &st) != 0)
723     return -1;
724
725   if (need_passphrase && (decode || decrypt)
726       && !crypt_valid_passphrase (app))
727     return -1;
728
729   mutt_message (_("Copying to %s..."), buf);
730
731   if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
732     switch (imap_copy_messages (Context, h, buf, delete)) {
733       /* success */
734     case 0:
735       mutt_clear_error ();
736       return 0;
737       /* non-fatal error: fall through to fetch/append */
738     case 1:
739       break;
740       /* fatal error, abort */
741     case -1:
742       return -1;
743     }
744   }
745
746   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
747     if (h) {
748       if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
749         mx_close_mailbox (&ctx, NULL);
750         return (-1);
751       }
752     } else {
753       for (i = 0; i < Context->vcount; i++) {
754         if (Context->hdrs[Context->v2r[i]]->tagged) {
755           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
756                              M_MESSAGEHOOK);
757           if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
758                                   decode, decrypt) != 0) {
759             mx_close_mailbox (&ctx, NULL);
760             return (-1);
761           }
762         }
763       }
764     }
765
766     need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
767
768     mx_close_mailbox (&ctx, NULL);
769
770     if (need_buffy_cleanup) {
771       /* fix up the times so buffy won't get confused */
772       if (st.st_mtime > st.st_atime) {
773         ut.actime = st.st_atime;
774         ut.modtime = time (NULL);
775         utime (buf, &ut);
776       } else {
777         utime (buf, NULL);
778       }
779     }
780
781     mutt_clear_error ();
782     return (0);
783   }
784
785   return -1;
786 }
787
788 void mutt_version (void)
789 {
790   mutt_message (mutt_make_version (1));
791 }
792
793 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
794 {
795   char buf[LONG_STRING];
796   char obuf[LONG_STRING];
797   char tmp[STRING];
798
799   char charset[STRING];
800   char *cp;
801
802   short charset_changed = 0;
803   short type_changed = 0;
804
805   cp = parameter_getval(b->parameter, "charset");
806   m_strcpy(charset, sizeof(charset), NONULL(cp));
807
808   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
809   m_strcpy(obuf, sizeof(obuf), buf);
810   if (b->parameter) {
811     parameter_t *p;
812     ssize_t l;
813
814     for (p = b->parameter; p; p = p->next) {
815       l = m_strlen(buf);
816
817       rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
818       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
819     }
820   }
821
822   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
823       buf[0] == 0)
824     return;
825
826   /* clean up previous junk */
827   parameter_list_wipe(&b->parameter);
828   p_delete(&b->subtype);
829
830   mutt_parse_content_type (buf, b);
831
832
833   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
834   type_changed = ascii_strcasecmp (tmp, obuf);
835   charset_changed =
836     ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
837
838   /* if in send mode, check for conversion - current setting is default. */
839
840   if (!h && b->type == TYPETEXT && charset_changed) {
841     int r;
842
843     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
844               parameter_getval(b->parameter, "charset"));
845     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
846       b->noconv = (r == M_NO);
847   }
848
849   /* inform the user */
850
851   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
852   if (type_changed)
853     mutt_message (_("Content-Type changed to %s."), tmp);
854   if (b->type == TYPETEXT && charset_changed) {
855     if (type_changed)
856       mutt_sleep (1);
857     mutt_message (_("Character set changed to %s; %s."),
858                   parameter_getval(b->parameter, "charset"),
859                   b->noconv ? _("not converting") : _("converting"));
860   }
861
862   b->force_charset |= charset_changed ? 1 : 0;
863
864   if (!is_multipart(b) && b->parts)
865     body_list_wipe(&b->parts);
866   if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
867     b->hdr->content = NULL;
868     header_delete(&b->hdr);
869   }
870
871   if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
872     mutt_parse_part (fp, b);
873
874   if (h) {
875     if (h->content == b)
876       h->security = 0;
877
878     h->security |= crypt_query (b);
879   }
880 }
881
882
883 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
884 {
885   MESSAGE *msg;
886   int rv = 0;
887
888   h->security |= PGP_TRADITIONAL_CHECKED;
889
890   mutt_parse_mime_message (Context, h);
891   if ((msg = mx_open_message (Context, h->msgno)) == NULL)
892     return 0;
893   if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
894     h->security = crypt_query (h->content);
895     *redraw |= REDRAW_FULL;
896     rv = 1;
897   }
898
899   h->security |= PGP_TRADITIONAL_CHECKED;
900   mx_close_message (&msg);
901   return rv;
902 }
903
904 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
905 {
906   int i;
907   int rv = 0;
908
909   if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
910     rv = _mutt_check_traditional_pgp (h, redraw);
911   else {
912     for (i = 0; i < Context->vcount; i++)
913       if (Context->hdrs[Context->v2r[i]]->tagged &&
914           !(Context->hdrs[Context->v2r[i]]->
915             security & PGP_TRADITIONAL_CHECKED))
916         rv =
917           _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)
918           || rv;
919   }
920   return rv;
921 }