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