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