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