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