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