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>
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.
17 #include "recvattach.h"
19 #include "mutt_curses.h"
20 #include "mutt_menu.h"
26 #include "mutt_crypt.h"
27 #include "mutt_idna.h"
29 #include <sys/types.h>
44 #include "lib/debug.h"
52 #include <sys/types.h>
55 extern char *ReleaseDate;
57 /* The folder the user last saved to. Used by ci_save_message() */
58 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
60 int mutt_display_message (HEADER * cur)
62 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
63 int rc = 0, builtin = 0;
64 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
66 FILE *fpfilterout = NULL;
71 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
72 cur->content->subtype);
74 mutt_parse_mime_message (Context, cur);
75 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
77 mutt_mktemp (tempfile);
78 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
79 mutt_error _("Could not create temporary file!");
84 if (DisplayFilter && *DisplayFilter) {
87 /* mutt_endwin (NULL); */
88 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
89 -1, fileno (fpfilterout), -1);
91 mutt_error (_("Cannot create display filter"));
92 safe_fclose (&fpfilterout);
98 if (!Pager || str_cmp (Pager, "builtin") == 0)
101 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
103 fputs ("\n\n", fpout);
106 msg = mx_open_message (Context, cur->msgno);
107 if (msg == NULL) res = -1;
110 /* see if crytpo is needed for this message. if so, we should exit curses */
111 if (WithCrypto && cur->security) {
112 if (cur->security & ENCRYPT) {
113 if (cur->security & APPLICATION_SMIME)
114 crypt_smime_getkeys (cur->env);
115 if (!crypt_valid_passphrase (cur->security))
118 cmflags |= M_CM_VERIFY;
120 else if (cur->security & SIGN) {
121 /* find out whether or not the verify signature */
122 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
124 cmflags |= M_CM_VERIFY;
129 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
130 if (cur->security & APPLICATION_PGP) {
132 crypt_pgp_invoke_getkeys (cur->env->from);
134 crypt_invoke_message (APPLICATION_PGP);
137 if (cur->security & APPLICATION_SMIME)
138 crypt_invoke_message (APPLICATION_SMIME);
141 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
142 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
143 CH_DECODE | CH_FROM);
144 if (res == 0 && (ferror(fpout) || feof(fpout))) {
145 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
149 mx_close_message (&msg);
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);
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...
165 mutt_unlink (tempfile);
170 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
171 mutt_any_key_to_continue (NULL);
173 safe_fclose (&fpfilterout); /* XXX - check result? */
177 /* update crypto information for this message */
178 cur->security &= ~(GOODSIGN|BADSIGN);
179 cur->security |= crypt_query (cur->content);
181 /* Remove color cache for this message, in case there
182 are color patterns for both ~g and ~V */
190 && (cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
191 if (cur->security & GOODSIGN) {
192 if (!crypt_smime_verify_sender (cur))
193 mutt_message (_("S/MIME signature successfully verified."));
195 mutt_error (_("S/MIME certificate owner does not match sender."));
197 else if (cur->security & PARTSIGN)
199 ("Warning: Part of this message has not been signed."));
200 else if (cur->security & SIGN || cur->security & BADSIGN)
201 mutt_error (_("S/MIME signature could NOT be verified."));
205 && (cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
206 if (cur->security & GOODSIGN)
207 mutt_message (_("PGP signature successfully verified."));
208 else if (cur->security & PARTSIGN)
210 ("Warning: Part of this message has not been signed."));
211 else if (cur->security & SIGN)
212 mutt_message (_("PGP signature could NOT be verified."));
215 /* Invoke the builtin pager */
216 memset (&info, 0, sizeof (pager_t));
219 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
225 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
226 if ((r = mutt_system (buf)) == -1)
227 mutt_error (_("Error running \"%s\"!"), buf);
229 keypad (stdscr, TRUE);
231 mutt_set_flag (Context, cur, M_READ, 1);
232 if (r != -1 && option (OPTPROMPTAFTER)) {
233 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
234 rc = km_dokey (MENU_PAGER);
243 void ci_bounce_message (HEADER * h, int *redraw)
245 char prompt[SHORT_STRING];
246 char buf[HUGE_STRING] = { 0 };
252 strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
254 strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
256 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
258 if (option (OPTNEEDREDRAW)) {
259 unset_option (OPTNEEDREDRAW);
260 *redraw = REDRAW_FULL;
266 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
267 mutt_error _("Error parsing address!");
272 adr = mutt_expand_aliases (adr);
274 if (mutt_addrlist_to_idna (adr, &err) < 0) {
275 mutt_error (_("Bad IDN: '%s'"), err);
277 rfc822_free_address (&adr);
282 rfc822_write_address (buf, sizeof (buf), adr, 1);
284 #define extra_space (15 + 7 + 2)
285 snprintf (prompt, sizeof (prompt),
286 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
289 if (mutt_strwidth (prompt) > COLS - extra_space) {
290 mutt_format_string (prompt, sizeof (prompt),
291 0, COLS - extra_space, 0, 0,
292 prompt, sizeof (prompt), 0);
293 str_cat (prompt, sizeof (prompt), "...?");
296 str_cat (prompt, sizeof (prompt), "?");
298 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
299 rfc822_free_address (&adr);
300 CLEARLINE (LINES - 1);
301 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
305 CLEARLINE (LINES - 1);
307 rc = mutt_bounce_message (NULL, h, adr);
308 rfc822_free_address (&adr);
309 /* If no error, or background, display message. */
310 if ((rc == 0) || (rc == S_BKG))
311 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
314 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
317 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
318 *chflags |= CH_DECODE | CH_REORDER;
320 if (option (OPTWEED)) {
322 *cmflags |= M_CM_WEED;
327 *cmflags |= M_CM_PRINTING;
331 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
334 int chflags = CH_FROM;
336 pipe_set_flags (decode, print, &cmflags, &chflags);
338 if (WithCrypto && decode && h->security & ENCRYPT) {
339 if (!crypt_valid_passphrase (h->security))
345 mutt_parse_mime_message (Context, h);
347 mutt_copy_message (fp, Context, h, cmflags, chflags);
351 /* the following code is shared between printing and piping */
353 static int _mutt_pipe_message (HEADER * h, char *cmd,
354 int decode, int print, int split, char *sep)
361 /* mutt_endwin (NULL);
363 is this really needed here ?
364 it makes the screen flicker on pgp and s/mime messages,
365 before asking for a passphrase...
369 mutt_message_hook (Context, h, M_MESSAGEHOOK);
371 if (WithCrypto && decode) {
372 mutt_parse_mime_message (Context, h);
373 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
378 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
379 mutt_perror (_("Can't create filter process"));
384 pipe_msg (h, fpout, decode, print);
386 rc = mutt_wait_filter (thepid);
388 else { /* handle tagged messages */
390 if (WithCrypto && decode) {
391 for (i = 0; i < Context->vcount; i++)
392 if (Context->hdrs[Context->v2r[i]]->tagged) {
393 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
395 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
396 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
397 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
404 for (i = 0; i < Context->vcount; i++) {
405 if (Context->hdrs[Context->v2r[i]]->tagged) {
406 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
409 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
410 mutt_perror (_("Can't create filter process"));
414 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
415 /* add the message separator */
418 safe_fclose (&fpout);
419 if (mutt_wait_filter (thepid) != 0)
426 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
427 mutt_perror (_("Can't create filter process"));
431 for (i = 0; i < Context->vcount; i++) {
432 if (Context->hdrs[Context->v2r[i]]->tagged) {
433 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
435 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
436 /* add the message separator */
441 safe_fclose (&fpout);
442 if (mutt_wait_filter (thepid) != 0)
447 if (rc || option (OPTWAITKEY))
448 mutt_any_key_to_continue (NULL);
452 void mutt_pipe_message (HEADER * h)
454 char buffer[LONG_STRING];
457 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
461 mutt_expand_path (buffer, sizeof (buffer));
462 _mutt_pipe_message (h, buffer,
463 option (OPTPIPEDECODE),
464 0, option (OPTPIPESPLIT), PipeSep);
467 void mutt_print_message (HEADER * h)
470 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
471 mutt_message (_("No printing command has been defined."));
475 if (query_quadoption (OPT_PRINT,
476 h ? _("Print message?") : _("Print tagged messages?"))
480 if (_mutt_pipe_message (h, PrintCmd,
481 option (OPTPRINTDECODE),
482 1, option (OPTPRINTSPLIT), "\f") == 0)
483 mutt_message (h ? _("Message printed") : _("Messages printed"));
485 mutt_message (h ? _("Message could not be printed") :
486 _("Messages could not be printed"));
490 int mutt_select_sort (int reverse)
492 int method = Sort; /* save the current method in case of abort */
494 switch (mutt_multi_choice (reverse ?
496 ("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?: ")
499 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
501 case -1: /* abort - don't resort */
513 Sort = SORT_RECEIVED;
524 case 6: /* (t)hread */
528 case 7: /* (u)nsort */
536 case 9: /* s(c)ore */
540 case 10: /* s(p)am */
545 Sort |= SORT_REVERSE;
547 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
550 /* invoke a command in a subshell */
551 void mutt_shell_escape (void)
553 char buf[LONG_STRING];
556 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
557 if (!buf[0] && Shell)
558 strfcpy (buf, Shell, sizeof (buf));
560 CLEARLINE (LINES - 1);
563 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
564 mutt_any_key_to_continue (NULL);
569 /* enter a mutt command */
570 void mutt_enter_command (void)
573 char buffer[LONG_STRING], errbuf[SHORT_STRING];
577 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
581 err.dsize = sizeof (errbuf);
582 memset (&token, 0, sizeof (token));
583 r = mutt_parse_rc_line (buffer, &token, &err);
584 mem_free (&token.data);
586 /* since errbuf could potentially contain printf() sequences in it,
587 we must call mutt_error() in this fashion so that vsprintf()
588 doesn't expect more arguments that we passed */
590 mutt_message ("%s", errbuf);
592 mutt_error ("%s", errbuf);
596 void mutt_display_address (ENVELOPE * env)
599 char buf[SHORT_STRING];
602 adr = mutt_get_address (env, &pfx);
608 * Note: We don't convert IDNA to local representation this time.
609 * That is intentional, so the user has an opportunity to copy &
610 * paste the on-the-wire form of the address to other, IDN-unable
615 rfc822_write_address (buf, sizeof (buf), adr, 0);
616 mutt_message ("%s: %s", pfx, buf);
619 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
620 int *cmflags, int *chflags)
623 *chflags = CH_UPDATE_LEN;
625 if (WithCrypto && !decode && decrypt && (hdr->security & ENCRYPT)) {
626 if ((WithCrypto & APPLICATION_PGP)
627 && mutt_is_multipart_encrypted (hdr->content)) {
628 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
629 *cmflags = M_CM_DECODE_PGP;
631 else if ((WithCrypto & APPLICATION_PGP)
632 && mutt_is_application_pgp (hdr->content) & ENCRYPT)
634 else if ((WithCrypto & APPLICATION_SMIME)
635 && mutt_is_application_smime (hdr->content) & ENCRYPT) {
636 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
637 *cmflags = M_CM_DECODE_SMIME;
642 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
643 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
645 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
646 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
648 if (option (OPTWEED)) {
649 *chflags |= CH_WEED; /* and respect $weed. */
650 *cmflags |= M_CM_WEED;
656 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
658 int cmflags, chflags;
661 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
663 if (decode || decrypt)
664 mutt_parse_mime_message (Context, h);
666 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
670 mutt_set_flag (Context, h, M_DELETE, 1);
671 if (option (OPTDELETEUNTAG))
672 mutt_set_flag (Context, h, M_TAG, 0);
673 mutt_set_flag (Context, h, M_APPENDED, 1);
678 /* returns 0 if the copy/save was successful, or -1 on error/abort */
679 int mutt_save_message (HEADER * h, int delete,
680 int decode, int decrypt, int *redraw) {
681 int i, need_buffy_cleanup;
682 int need_passphrase = 0, app = 0;
683 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
696 snprintf (prompt, sizeof (prompt),
697 decode ? (delete ? _("Decode-save%s to mailbox") :
698 _("Decode-copy%s to mailbox")) :
699 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
700 _("Decrypt-copy%s to mailbox")) :
701 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
702 h ? "" : _(" tagged"));
707 need_passphrase = h->security & ENCRYPT;
710 mutt_message_hook (Context, h, M_MESSAGEHOOK);
711 mutt_default_save (buf, sizeof (buf), h);
714 /* look for the first tagged message */
716 for (i = 0; i < Context->vcount; i++) {
717 if (Context->hdrs[Context->v2r[i]]->tagged) {
718 h = Context->hdrs[Context->v2r[i]];
725 mutt_message_hook (Context, h, M_MESSAGEHOOK);
726 mutt_default_save (buf, sizeof (buf), h);
728 need_passphrase = h->security & ENCRYPT;
735 mutt_pretty_mailbox (buf);
736 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
739 if (*redraw != REDRAW_FULL) {
741 *redraw = REDRAW_INDEX | REDRAW_STATUS;
743 *redraw = REDRAW_STATUS;
749 /* This is an undocumented feature of ELM pointed out to me by Felix von
750 * Leitner <leitner@prz.fu-berlin.de>
752 if (str_cmp (buf, ".") == 0)
753 strfcpy (buf, LastSaveFolder, sizeof (buf));
755 strfcpy (LastSaveFolder, buf, sizeof (LastSaveFolder));
757 mutt_expand_path (buf, sizeof (buf));
759 /* check to make sure that this file is really the one the user wants */
760 if (mutt_save_confirm (buf, &st) != 0)
763 if (WithCrypto && need_passphrase && (decode || decrypt)
764 && !crypt_valid_passphrase (app))
767 mutt_message (_("Copying to %s..."), buf);
770 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
771 switch (imap_copy_messages (Context, h, buf, delete)) {
776 /* non-fatal error: fall through to fetch/append */
779 /* fatal error, abort */
786 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
788 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
789 mx_close_mailbox (&ctx, NULL);
793 for (i = 0; i < Context->vcount; i++) {
794 if (Context->hdrs[Context->v2r[i]]->tagged) {
795 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
797 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
798 decode, decrypt) != 0) {
799 mx_close_mailbox (&ctx, NULL);
806 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
808 mx_close_mailbox (&ctx, NULL);
810 if (need_buffy_cleanup) {
812 tmp = buffy_find_mailbox (buf);
813 if (tmp && tmp->new <= 0)
814 buffy_update_mailbox (tmp);
816 /* fix up the times so buffy won't get confused */
817 if (st.st_mtime > st.st_atime) {
818 ut.actime = st.st_atime;
819 ut.modtime = time (NULL);
834 void mutt_version (void)
836 mutt_message (mutt_make_version (1));
839 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
841 char buf[LONG_STRING];
842 char obuf[LONG_STRING];
846 char charset[STRING];
849 short charset_changed = 0;
850 short type_changed = 0;
852 cp = mutt_get_parameter ("charset", b->parameter);
853 strfcpy (charset, NONULL (cp), sizeof (charset));
855 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
856 strfcpy (obuf, buf, sizeof (obuf));
860 for (p = b->parameter; p; p = p->next) {
863 rfc822_cat (tmp, sizeof (tmp), p->value, MimeSpecials);
864 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
868 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
872 /* clean up previous junk */
873 mutt_free_parameter (&b->parameter);
874 mem_free (&b->subtype);
876 mutt_parse_content_type (buf, b);
879 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
880 type_changed = ascii_strcasecmp (tmp, obuf);
882 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
884 /* if in send mode, check for conversion - current setting is default. */
886 if (!h && b->type == TYPETEXT && charset_changed) {
889 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
890 mutt_get_parameter ("charset", b->parameter));
891 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
892 b->noconv = (r == M_NO);
895 /* inform the user */
897 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
899 mutt_message (_("Content-Type changed to %s."), tmp);
900 if (b->type == TYPETEXT && charset_changed) {
903 mutt_message (_("Character set changed to %s; %s."),
904 mutt_get_parameter ("charset", b->parameter),
905 b->noconv ? _("not converting") : _("converting"));
908 b->force_charset |= charset_changed ? 1 : 0;
910 if (!is_multipart (b) && b->parts)
911 mutt_free_body (&b->parts);
912 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
913 b->hdr->content = NULL;
914 mutt_free_header (&b->hdr);
917 if (fp && (is_multipart (b) || mutt_is_message_type (b->type, b->subtype)))
918 mutt_parse_part (fp, b);
920 if (WithCrypto && h) {
924 h->security |= crypt_query (b);
929 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
934 h->security |= PGP_TRADITIONAL_CHECKED;
936 mutt_parse_mime_message (Context, h);
937 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
939 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
940 h->security = crypt_query (h->content);
941 *redraw |= REDRAW_FULL;
945 h->security |= PGP_TRADITIONAL_CHECKED;
946 mx_close_message (&msg);
950 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
955 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
956 rv = _mutt_check_traditional_pgp (h, redraw);
958 for (i = 0; i < Context->vcount; i++)
959 if (Context->hdrs[Context->v2r[i]]->tagged &&
960 !(Context->hdrs[Context->v2r[i]]->
961 security & PGP_TRADITIONAL_CHECKED))
963 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)