remove most of the debug code: often makes the code unreadable, for little
[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 "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       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   const 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_list_wipe(&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_list_wipe(&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_list_wipe(&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   if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
758     switch (imap_copy_messages (Context, h, buf, delete)) {
759       /* success */
760     case 0:
761       mutt_clear_error ();
762       return 0;
763       /* non-fatal error: fall through to fetch/append */
764     case 1:
765       break;
766       /* fatal error, abort */
767     case -1:
768       return -1;
769     }
770   }
771
772   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
773     if (h) {
774       if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
775         mx_close_mailbox (&ctx, NULL);
776         return (-1);
777       }
778     } else {
779       for (i = 0; i < Context->vcount; i++) {
780         if (Context->hdrs[Context->v2r[i]]->tagged) {
781           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
782                              M_MESSAGEHOOK);
783           if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
784                                   decode, decrypt) != 0) {
785             mx_close_mailbox (&ctx, NULL);
786             return (-1);
787           }
788         }
789       }
790     }
791
792     need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
793
794     mx_close_mailbox (&ctx, NULL);
795
796     if (need_buffy_cleanup) {
797 #ifdef BUFFY_SIZE
798       tmp = buffy_find_mailbox (buf);
799       if (tmp && tmp->new <= 0)
800         buffy_update_mailbox (tmp);
801 #else
802       /* fix up the times so buffy won't get confused */
803       if (st.st_mtime > st.st_atime) {
804         ut.actime = st.st_atime;
805         ut.modtime = time (NULL);
806         utime (buf, &ut);
807       }
808       else
809         utime (buf, NULL);
810 #endif
811     }
812
813     mutt_clear_error ();
814     return (0);
815   }
816
817   return -1;
818 }
819
820 void mutt_version (void)
821 {
822   mutt_message (mutt_make_version (1));
823 }
824
825 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
826 {
827   char buf[LONG_STRING];
828   char obuf[LONG_STRING];
829   char tmp[STRING];
830   PARAMETER *p;
831
832   char charset[STRING];
833   char *cp;
834
835   short charset_changed = 0;
836   short type_changed = 0;
837
838   cp = mutt_get_parameter ("charset", b->parameter);
839   m_strcpy(charset, sizeof(charset), NONULL(cp));
840
841   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
842   m_strcpy(obuf, sizeof(obuf), buf);
843   if (b->parameter) {
844     ssize_t l;
845
846     for (p = b->parameter; p; p = p->next) {
847       l = m_strlen(buf);
848
849       rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
850       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
851     }
852   }
853
854   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
855       buf[0] == 0)
856     return;
857
858   /* clean up previous junk */
859   parameter_list_wipe(&b->parameter);
860   p_delete(&b->subtype);
861
862   mutt_parse_content_type (buf, b);
863
864
865   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
866   type_changed = ascii_strcasecmp (tmp, obuf);
867   charset_changed =
868     ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
869
870   /* if in send mode, check for conversion - current setting is default. */
871
872   if (!h && b->type == TYPETEXT && charset_changed) {
873     int r;
874
875     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
876               mutt_get_parameter ("charset", b->parameter));
877     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
878       b->noconv = (r == M_NO);
879   }
880
881   /* inform the user */
882
883   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
884   if (type_changed)
885     mutt_message (_("Content-Type changed to %s."), tmp);
886   if (b->type == TYPETEXT && charset_changed) {
887     if (type_changed)
888       mutt_sleep (1);
889     mutt_message (_("Character set changed to %s; %s."),
890                   mutt_get_parameter ("charset", b->parameter),
891                   b->noconv ? _("not converting") : _("converting"));
892   }
893
894   b->force_charset |= charset_changed ? 1 : 0;
895
896   if (!is_multipart(b) && b->parts)
897     mutt_free_body (&b->parts);
898   if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
899     b->hdr->content = NULL;
900     header_delete(&b->hdr);
901   }
902
903   if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
904     mutt_parse_part (fp, b);
905
906   if (h) {
907     if (h->content == b)
908       h->security = 0;
909
910     h->security |= crypt_query (b);
911   }
912 }
913
914
915 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
916 {
917   MESSAGE *msg;
918   int rv = 0;
919
920   h->security |= PGP_TRADITIONAL_CHECKED;
921
922   mutt_parse_mime_message (Context, h);
923   if ((msg = mx_open_message (Context, h->msgno)) == NULL)
924     return 0;
925   if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
926     h->security = crypt_query (h->content);
927     *redraw |= REDRAW_FULL;
928     rv = 1;
929   }
930
931   h->security |= PGP_TRADITIONAL_CHECKED;
932   mx_close_message (&msg);
933   return rv;
934 }
935
936 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
937 {
938   int i;
939   int rv = 0;
940
941   if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
942     rv = _mutt_check_traditional_pgp (h, redraw);
943   else {
944     for (i = 0; i < Context->vcount; i++)
945       if (Context->hdrs[Context->v2r[i]]->tagged &&
946           !(Context->hdrs[Context->v2r[i]]->
947             security & PGP_TRADITIONAL_CHECKED))
948         rv =
949           _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)
950           || rv;
951   }
952   return rv;
953 }