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.
21 #include <sys/types.h>
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 #include <lib-lib/debug.h>
30 #include <lib-mime/mime.h>
32 #include <lib-sys/exit.h>
33 #include <lib-sys/unix.h>
35 #include <lib-ui/curses.h>
36 #include <lib-ui/enter.h>
37 #include <lib-ui/menu.h>
40 #include "recvattach.h"
45 #include <lib-crypt/crypt.h>
46 #include "mutt_idna.h"
48 #include <sys/types.h>
52 #include <imap/imap.h>
58 /* The folder the user last saved to. Used by ci_save_message() */
59 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
61 int mutt_display_message (HEADER * cur)
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;
67 FILE *fpfilterout = NULL;
72 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
73 cur->content->subtype);
75 mutt_parse_mime_message (Context, cur);
76 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
78 mutt_mktemp (tempfile);
79 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
80 mutt_error _("Could not create temporary file!");
85 if (DisplayFilter && *DisplayFilter) {
88 /* mutt_endwin (NULL); */
89 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
90 -1, fileno (fpfilterout), -1);
92 mutt_error (_("Cannot create display filter"));
93 safe_fclose (&fpfilterout);
99 if (!Pager || m_strcmp(Pager, "builtin") == 0)
102 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
104 fputs ("\n\n", fpout);
107 msg = mx_open_message (Context, cur->msgno);
108 if (msg == NULL) res = -1;
111 /* see if crytpo is needed for this message. if so, we should exit curses */
113 if (cur->security & ENCRYPT) {
114 if (cur->security & APPLICATION_SMIME)
115 crypt_smime_getkeys (cur->env);
116 if (!crypt_valid_passphrase (cur->security))
119 cmflags |= M_CM_VERIFY;
121 else if (cur->security & SIGN) {
122 /* find out whether or not the verify signature */
123 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
125 cmflags |= M_CM_VERIFY;
130 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
131 if (cur->security & APPLICATION_PGP) {
133 crypt_pgp_invoke_getkeys (cur->env->from);
135 crypt_invoke_message (APPLICATION_PGP);
138 if (cur->security & APPLICATION_SMIME)
139 crypt_invoke_message (APPLICATION_SMIME);
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 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
150 mx_close_message (&msg);
153 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
154 mutt_error (_("Could not copy message"));
155 if (fpfilterout != NULL) {
156 mutt_wait_filter (filterpid);
157 safe_fclose (&fpfilterout);
160 /* this is maybe just plain wrong but it makes the pager display
161 * what we have; i.e. for the crypto stuff we only get
162 * 'Could not copy message' for invalid passphrases, no PGP output
163 * not nothing; so just display what we have...
166 mutt_unlink (tempfile);
171 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
172 mutt_any_key_to_continue (NULL);
174 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 */
188 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
189 if (cur->security & GOODSIGN) {
190 if (!crypt_smime_verify_sender (cur))
191 mutt_message (_("S/MIME signature successfully verified."));
193 mutt_error (_("S/MIME certificate owner does not match sender."));
195 else if (cur->security & PARTSIGN)
197 ("Warning: Part of this message has not been signed."));
198 else if (cur->security & SIGN || cur->security & BADSIGN)
199 mutt_error (_("S/MIME signature could NOT be verified."));
202 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
203 if (cur->security & GOODSIGN)
204 mutt_message (_("PGP signature successfully verified."));
205 else if (cur->security & PARTSIGN)
207 ("Warning: Part of this message has not been signed."));
208 else if (cur->security & SIGN)
209 mutt_message (_("PGP signature could NOT be verified."));
212 /* Invoke the builtin pager */
216 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
222 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
223 if ((r = mutt_system (buf)) == -1)
224 mutt_error (_("Error running \"%s\"!"), buf);
226 keypad (stdscr, TRUE);
228 mutt_set_flag (Context, cur, M_READ, 1);
229 if (r != -1 && option (OPTPROMPTAFTER)) {
230 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
231 rc = km_dokey (MENU_PAGER);
240 void ci_bounce_message (HEADER * h, int *redraw)
242 char prompt[SHORT_STRING];
243 char buf[HUGE_STRING] = { 0 };
244 address_t *adr = NULL;
249 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
251 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
253 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
255 if (option (OPTNEEDREDRAW)) {
256 unset_option (OPTNEEDREDRAW);
257 *redraw = REDRAW_FULL;
263 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
264 mutt_error _("Error parsing address!");
269 adr = mutt_expand_aliases (adr);
271 if (mutt_addrlist_to_idna (adr, &err) < 0) {
272 mutt_error (_("Bad IDN: '%s'"), err);
274 address_list_wipe(&adr);
279 rfc822_write_address (buf, sizeof (buf), adr, 1);
281 #define extra_space (15 + 7 + 2)
282 snprintf (prompt, sizeof (prompt),
283 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
286 if (mutt_strwidth (prompt) > COLS - extra_space) {
287 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
288 prompt, sizeof(prompt), 0);
289 m_strcat(prompt, sizeof(prompt), "...?");
291 m_strcat(prompt, sizeof(prompt), "?");
294 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
295 address_list_wipe(&adr);
296 CLEARLINE (LINES - 1);
297 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
301 CLEARLINE (LINES - 1);
303 rc = mutt_bounce_message (NULL, h, adr);
304 address_list_wipe(&adr);
305 /* If no error, or background, display message. */
306 if ((rc == 0) || (rc == S_BKG))
307 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
310 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
313 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
314 *chflags |= CH_DECODE | CH_REORDER;
316 if (option (OPTWEED)) {
318 *cmflags |= M_CM_WEED;
323 *cmflags |= M_CM_PRINTING;
327 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
330 int chflags = CH_FROM;
332 pipe_set_flags (decode, print, &cmflags, &chflags);
334 if (decode && h->security & ENCRYPT) {
335 if (!crypt_valid_passphrase (h->security))
341 mutt_parse_mime_message (Context, h);
343 mutt_copy_message (fp, Context, h, cmflags, chflags);
347 /* the following code is shared between printing and piping */
349 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
350 int split, const char *sep)
357 /* mutt_endwin (NULL);
359 is this really needed here ?
360 it makes the screen flicker on pgp and s/mime messages,
361 before asking for a passphrase...
365 mutt_message_hook (Context, h, M_MESSAGEHOOK);
368 mutt_parse_mime_message (Context, h);
369 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
374 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
375 mutt_perror (_("Can't create filter process"));
380 pipe_msg (h, fpout, decode, print);
382 rc = mutt_wait_filter (thepid);
384 else { /* handle tagged messages */
387 for (i = 0; i < Context->vcount; i++)
388 if (Context->hdrs[Context->v2r[i]]->tagged) {
389 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
391 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
392 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
393 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
400 for (i = 0; i < Context->vcount; i++) {
401 if (Context->hdrs[Context->v2r[i]]->tagged) {
402 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
405 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
406 mutt_perror (_("Can't create filter process"));
410 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
411 /* add the message separator */
414 safe_fclose (&fpout);
415 if (mutt_wait_filter (thepid) != 0)
422 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
423 mutt_perror (_("Can't create filter process"));
427 for (i = 0; i < Context->vcount; i++) {
428 if (Context->hdrs[Context->v2r[i]]->tagged) {
429 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
431 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
432 /* add the message separator */
437 safe_fclose (&fpout);
438 if (mutt_wait_filter (thepid) != 0)
443 if (rc || option (OPTWAITKEY))
444 mutt_any_key_to_continue (NULL);
448 void mutt_pipe_message (HEADER * h)
450 char buffer[LONG_STRING];
453 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
457 mutt_expand_path (buffer, sizeof (buffer));
458 _mutt_pipe_message (h, buffer,
459 option (OPTPIPEDECODE),
460 0, option (OPTPIPESPLIT), PipeSep);
463 void mutt_print_message (HEADER * h)
466 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
467 mutt_message (_("No printing command has been defined."));
471 if (query_quadoption (OPT_PRINT,
472 h ? _("Print message?") : _("Print tagged messages?"))
476 if (_mutt_pipe_message (h, PrintCmd,
477 option (OPTPRINTDECODE),
478 1, option (OPTPRINTSPLIT), "\f") == 0)
479 mutt_message (h ? _("Message printed") : _("Messages printed"));
481 mutt_message (h ? _("Message could not be printed") :
482 _("Messages could not be printed"));
486 int mutt_select_sort (int reverse)
488 int method = Sort; /* save the current method in case of abort */
490 switch (mutt_multi_choice (reverse ?
492 ("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?: ")
495 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
497 case -1: /* abort - don't resort */
509 Sort = SORT_RECEIVED;
520 case 6: /* (t)hread */
524 case 7: /* (u)nsort */
532 case 9: /* s(c)ore */
536 case 10: /* s(p)am */
541 Sort |= SORT_REVERSE;
543 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
546 /* invoke a command in a subshell */
547 void mutt_shell_escape (void)
549 char buf[LONG_STRING];
552 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
553 if (!buf[0] && Shell)
554 m_strcpy(buf, sizeof(buf), Shell);
556 CLEARLINE (LINES - 1);
559 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
560 mutt_any_key_to_continue (NULL);
565 /* enter a mutt command */
566 void mutt_enter_command (void)
569 char buffer[LONG_STRING], errbuf[SHORT_STRING];
573 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
577 err.dsize = sizeof (errbuf);
579 r = mutt_parse_rc_line (buffer, &token, &err);
580 p_delete(&token.data);
582 /* since errbuf could potentially contain printf() sequences in it,
583 we must call mutt_error() in this fashion so that vsprintf()
584 doesn't expect more arguments that we passed */
586 mutt_message ("%s", errbuf);
588 mutt_error ("%s", errbuf);
592 void mutt_display_address (ENVELOPE * env)
594 const char *pfx = NULL;
595 char buf[SHORT_STRING];
596 address_t *adr = NULL;
598 adr = mutt_get_address(env, &pfx);
604 * Note: We don't convert IDNA to local representation this time.
605 * That is intentional, so the user has an opportunity to copy &
606 * paste the on-the-wire form of the address to other, IDN-unable
611 rfc822_write_address (buf, sizeof (buf), adr, 0);
612 mutt_message ("%s: %s", pfx, buf);
615 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
616 int *cmflags, int *chflags)
619 *chflags = CH_UPDATE_LEN;
621 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
622 if (mutt_is_multipart_encrypted (hdr->content)) {
623 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
624 *cmflags = M_CM_DECODE_PGP;
626 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
628 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
629 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
630 *cmflags = M_CM_DECODE_SMIME;
635 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
636 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
638 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
639 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
641 if (option (OPTWEED)) {
642 *chflags |= CH_WEED; /* and respect $weed. */
643 *cmflags |= M_CM_WEED;
649 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
651 int cmflags, chflags;
654 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
656 if (decode || decrypt)
657 mutt_parse_mime_message (Context, h);
659 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
663 mutt_set_flag (Context, h, M_DELETE, 1);
664 if (option (OPTDELETEUNTAG))
665 mutt_set_flag (Context, h, M_TAG, 0);
666 mutt_set_flag (Context, h, M_APPENDED, 1);
671 /* returns 0 if the copy/save was successful, or -1 on error/abort */
672 int mutt_save_message (HEADER * h, int delete,
673 int decode, int decrypt, int *redraw) {
674 int i, need_buffy_cleanup;
675 int need_passphrase = 0, app = 0;
676 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
689 snprintf (prompt, sizeof (prompt),
690 decode ? (delete ? _("Decode-save%s to mailbox") :
691 _("Decode-copy%s to mailbox")) :
692 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
693 _("Decrypt-copy%s to mailbox")) :
694 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
695 h ? "" : _(" tagged"));
699 need_passphrase = h->security & ENCRYPT;
701 mutt_message_hook (Context, h, M_MESSAGEHOOK);
702 mutt_default_save (buf, sizeof (buf), h);
705 /* look for the first tagged message */
707 for (i = 0; i < Context->vcount; i++) {
708 if (Context->hdrs[Context->v2r[i]]->tagged) {
709 h = Context->hdrs[Context->v2r[i]];
716 mutt_message_hook (Context, h, M_MESSAGEHOOK);
717 mutt_default_save (buf, sizeof (buf), h);
718 need_passphrase = h->security & ENCRYPT;
724 mutt_pretty_mailbox (buf);
725 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
728 if (*redraw != REDRAW_FULL) {
730 *redraw = REDRAW_INDEX | REDRAW_STATUS;
732 *redraw = REDRAW_STATUS;
738 /* This is an undocumented feature of ELM pointed out to me by Felix von
739 * Leitner <leitner@prz.fu-berlin.de>
741 if (m_strcmp(buf, ".") == 0)
742 m_strcpy(buf, sizeof(buf), LastSaveFolder);
744 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
746 mutt_expand_path (buf, sizeof (buf));
748 /* check to make sure that this file is really the one the user wants */
749 if (mutt_save_confirm (buf, &st) != 0)
752 if (need_passphrase && (decode || decrypt)
753 && !crypt_valid_passphrase (app))
756 mutt_message (_("Copying to %s..."), buf);
758 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
759 switch (imap_copy_messages (Context, h, buf, delete)) {
764 /* non-fatal error: fall through to fetch/append */
767 /* fatal error, abort */
773 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
775 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
776 mx_close_mailbox (&ctx, NULL);
780 for (i = 0; i < Context->vcount; i++) {
781 if (Context->hdrs[Context->v2r[i]]->tagged) {
782 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
784 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
785 decode, decrypt) != 0) {
786 mx_close_mailbox (&ctx, NULL);
793 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
795 mx_close_mailbox (&ctx, NULL);
797 if (need_buffy_cleanup) {
799 tmp = buffy_find_mailbox (buf);
800 if (tmp && tmp->new <= 0)
801 buffy_update_mailbox (tmp);
803 /* fix up the times so buffy won't get confused */
804 if (st.st_mtime > st.st_atime) {
805 ut.actime = st.st_atime;
806 ut.modtime = time (NULL);
821 void mutt_version (void)
823 mutt_message (mutt_make_version (1));
826 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
828 char buf[LONG_STRING];
829 char obuf[LONG_STRING];
833 char charset[STRING];
836 short charset_changed = 0;
837 short type_changed = 0;
839 cp = mutt_get_parameter ("charset", b->parameter);
840 m_strcpy(charset, sizeof(charset), NONULL(cp));
842 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
843 m_strcpy(obuf, sizeof(obuf), buf);
847 for (p = b->parameter; p; p = p->next) {
850 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
851 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
855 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
859 /* clean up previous junk */
860 parameter_list_wipe(&b->parameter);
861 p_delete(&b->subtype);
863 mutt_parse_content_type (buf, b);
866 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
867 type_changed = ascii_strcasecmp (tmp, obuf);
869 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
871 /* if in send mode, check for conversion - current setting is default. */
873 if (!h && b->type == TYPETEXT && charset_changed) {
876 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
877 mutt_get_parameter ("charset", b->parameter));
878 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
879 b->noconv = (r == M_NO);
882 /* inform the user */
884 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
886 mutt_message (_("Content-Type changed to %s."), tmp);
887 if (b->type == TYPETEXT && charset_changed) {
890 mutt_message (_("Character set changed to %s; %s."),
891 mutt_get_parameter ("charset", b->parameter),
892 b->noconv ? _("not converting") : _("converting"));
895 b->force_charset |= charset_changed ? 1 : 0;
897 if (!is_multipart(b) && b->parts)
898 mutt_free_body (&b->parts);
899 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
900 b->hdr->content = NULL;
901 header_delete(&b->hdr);
904 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
905 mutt_parse_part (fp, b);
911 h->security |= crypt_query (b);
916 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
921 h->security |= PGP_TRADITIONAL_CHECKED;
923 mutt_parse_mime_message (Context, h);
924 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
926 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
927 h->security = crypt_query (h->content);
928 *redraw |= REDRAW_FULL;
932 h->security |= PGP_TRADITIONAL_CHECKED;
933 mx_close_message (&msg);
937 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
942 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
943 rv = _mutt_check_traditional_pgp (h, redraw);
945 for (i = 0; i < Context->vcount; i++)
946 if (Context->hdrs[Context->v2r[i]]->tagged &&
947 !(Context->hdrs[Context->v2r[i]]->
948 security & PGP_TRADITIONAL_CHECKED))
950 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)