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