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.
11 #include <lib-lib/lib-lib.h>
14 #include <lib-mime/mime.h>
16 #include <lib-sys/exit.h>
17 #include <lib-sys/unix.h>
19 #include <lib-ui/curses.h>
20 #include <lib-ui/enter.h>
21 #include <lib-ui/menu.h>
22 #include <lib-mx/mx.h>
26 #include "recvattach.h"
30 #include <lib-crypt/crypt.h>
31 #include "mutt_idna.h"
32 #include <sys/types.h>
36 #include <imap/imap.h>
38 /* The folder the user last saved to. Used by ci_save_message() */
39 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
41 int mutt_display_message (HEADER * cur)
43 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
44 int rc = 0, builtin = 0;
45 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
47 FILE *fpfilterout = NULL;
52 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
53 cur->content->subtype);
55 mutt_parse_mime_message (Context, cur);
56 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
58 mutt_mktemp (tempfile);
59 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
60 mutt_error _("Could not create temporary file!");
65 if (DisplayFilter && *DisplayFilter) {
68 /* mutt_endwin (NULL); */
69 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
70 -1, fileno (fpfilterout), -1);
72 mutt_error (_("Cannot create display filter"));
73 safe_fclose (&fpfilterout);
79 if (!Pager || m_strcmp(Pager, "builtin") == 0)
82 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
84 fputs ("\n\n", fpout);
87 msg = mx_open_message (Context, cur->msgno);
88 if (msg == NULL) res = -1;
91 /* see if crytpo is needed for this message. if so, we should exit curses */
93 if (cur->security & ENCRYPT) {
94 if (cur->security & APPLICATION_SMIME)
95 crypt_smime_getkeys (cur->env);
96 if (!crypt_valid_passphrase (cur->security))
99 cmflags |= M_CM_VERIFY;
101 else if (cur->security & SIGN) {
102 /* find out whether or not the verify signature */
103 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
105 cmflags |= M_CM_VERIFY;
110 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
111 if (cur->security & APPLICATION_PGP) {
113 crypt_pgp_invoke_getkeys (cur->env->from);
115 crypt_invoke_message (APPLICATION_PGP);
118 if (cur->security & APPLICATION_SMIME)
119 crypt_invoke_message (APPLICATION_SMIME);
122 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
123 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
124 CH_DECODE | CH_FROM);
125 if (res == 0 && (ferror(fpout) || feof(fpout))) {
129 mx_close_message (&msg);
132 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
133 mutt_error (_("Could not copy message"));
134 if (fpfilterout != NULL) {
135 mutt_wait_filter (filterpid);
136 safe_fclose (&fpfilterout);
139 /* this is maybe just plain wrong but it makes the pager display
140 * what we have; i.e. for the crypto stuff we only get
141 * 'Could not copy message' for invalid passphrases, no PGP output
142 * not nothing; so just display what we have...
145 mutt_unlink (tempfile);
150 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
151 mutt_any_key_to_continue (NULL);
153 safe_fclose (&fpfilterout); /* XXX - check result? */
156 /* update crypto information for this message */
157 cur->security &= ~(GOODSIGN|BADSIGN);
158 cur->security |= crypt_query (cur->content);
160 /* Remove color cache for this message, in case there
161 are color patterns for both ~g and ~V */
167 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
168 if (cur->security & GOODSIGN) {
169 if (!crypt_smime_verify_sender (cur))
170 mutt_message (_("S/MIME signature successfully verified."));
172 mutt_error (_("S/MIME certificate owner does not match sender."));
174 else if (cur->security & PARTSIGN)
176 ("Warning: Part of this message has not been signed."));
177 else if (cur->security & SIGN || cur->security & BADSIGN)
178 mutt_error (_("S/MIME signature could NOT be verified."));
181 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
182 if (cur->security & GOODSIGN)
183 mutt_message (_("PGP signature successfully verified."));
184 else if (cur->security & PARTSIGN)
186 ("Warning: Part of this message has not been signed."));
187 else if (cur->security & SIGN)
188 mutt_message (_("PGP signature could NOT be verified."));
191 /* Invoke the builtin pager */
195 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
201 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
202 if ((r = mutt_system (buf)) == -1)
203 mutt_error (_("Error running \"%s\"!"), buf);
205 keypad (stdscr, TRUE);
207 mutt_set_flag (Context, cur, M_READ, 1);
208 if (r != -1 && option (OPTPROMPTAFTER)) {
209 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
210 rc = km_dokey (MENU_PAGER);
219 void ci_bounce_message (HEADER * h, int *redraw)
221 char prompt[SHORT_STRING];
222 char buf[HUGE_STRING] = { 0 };
223 address_t *adr = NULL;
228 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
230 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
232 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
234 if (option (OPTNEEDREDRAW)) {
235 unset_option (OPTNEEDREDRAW);
236 *redraw = REDRAW_FULL;
242 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
243 mutt_error _("Error parsing address!");
248 adr = mutt_expand_aliases (adr);
250 if (mutt_addrlist_to_idna (adr, &err) < 0) {
251 mutt_error (_("Bad IDN: '%s'"), err);
253 address_list_wipe(&adr);
258 rfc822_write_address (buf, sizeof (buf), adr, 1);
260 #define extra_space (15 + 7 + 2)
261 snprintf (prompt, sizeof (prompt),
262 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
265 if (mutt_strwidth (prompt) > COLS - extra_space) {
266 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
267 prompt, sizeof(prompt), 0);
268 m_strcat(prompt, sizeof(prompt), "...?");
270 m_strcat(prompt, sizeof(prompt), "?");
273 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
274 address_list_wipe(&adr);
275 CLEARLINE (LINES - 1);
276 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
280 CLEARLINE (LINES - 1);
282 rc = mutt_bounce_message (NULL, h, adr);
283 address_list_wipe(&adr);
284 /* If no error, or background, display message. */
285 if ((rc == 0) || (rc == S_BKG))
286 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
289 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
292 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
293 *chflags |= CH_DECODE | CH_REORDER;
295 if (option (OPTWEED)) {
297 *cmflags |= M_CM_WEED;
302 *cmflags |= M_CM_PRINTING;
306 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
309 int chflags = CH_FROM;
311 pipe_set_flags (decode, print, &cmflags, &chflags);
313 if (decode && h->security & ENCRYPT) {
314 if (!crypt_valid_passphrase (h->security))
320 mutt_parse_mime_message (Context, h);
322 mutt_copy_message (fp, Context, h, cmflags, chflags);
326 /* the following code is shared between printing and piping */
328 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
329 int split, const char *sep)
336 /* mutt_endwin (NULL);
338 is this really needed here ?
339 it makes the screen flicker on pgp and s/mime messages,
340 before asking for a passphrase...
344 mutt_message_hook (Context, h, M_MESSAGEHOOK);
347 mutt_parse_mime_message (Context, h);
348 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
353 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
354 mutt_perror (_("Can't create filter process"));
359 pipe_msg (h, fpout, decode, print);
361 rc = mutt_wait_filter (thepid);
363 else { /* handle tagged messages */
366 for (i = 0; i < Context->vcount; i++)
367 if (Context->hdrs[Context->v2r[i]]->tagged) {
368 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
370 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
371 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
372 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
379 for (i = 0; i < Context->vcount; i++) {
380 if (Context->hdrs[Context->v2r[i]]->tagged) {
381 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
384 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
385 mutt_perror (_("Can't create filter process"));
389 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
390 /* add the message separator */
393 safe_fclose (&fpout);
394 if (mutt_wait_filter (thepid) != 0)
401 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
402 mutt_perror (_("Can't create filter process"));
406 for (i = 0; i < Context->vcount; i++) {
407 if (Context->hdrs[Context->v2r[i]]->tagged) {
408 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
410 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
411 /* add the message separator */
416 safe_fclose (&fpout);
417 if (mutt_wait_filter (thepid) != 0)
422 if (rc || option (OPTWAITKEY))
423 mutt_any_key_to_continue (NULL);
427 void mutt_pipe_message (HEADER * h)
429 char buffer[LONG_STRING];
432 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
436 mutt_expand_path (buffer, sizeof (buffer));
437 _mutt_pipe_message (h, buffer,
438 option (OPTPIPEDECODE),
439 0, option (OPTPIPESPLIT), PipeSep);
442 void mutt_print_message (HEADER * h)
445 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
446 mutt_message (_("No printing command has been defined."));
450 if (query_quadoption (OPT_PRINT,
451 h ? _("Print message?") : _("Print tagged messages?"))
455 if (_mutt_pipe_message (h, PrintCmd,
456 option (OPTPRINTDECODE),
457 1, option (OPTPRINTSPLIT), "\f") == 0)
458 mutt_message (h ? _("Message printed") : _("Messages printed"));
460 mutt_message (h ? _("Message could not be printed") :
461 _("Messages could not be printed"));
465 int mutt_select_sort (int reverse)
467 int method = Sort; /* save the current method in case of abort */
469 switch (mutt_multi_choice (reverse ?
471 ("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?: ")
474 ("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 case -1: /* abort - don't resort */
488 Sort = SORT_RECEIVED;
499 case 6: /* (t)hread */
503 case 7: /* (u)nsort */
511 case 9: /* s(c)ore */
515 case 10: /* s(p)am */
520 Sort |= SORT_REVERSE;
522 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
525 /* invoke a command in a subshell */
526 void mutt_shell_escape (void)
528 char buf[LONG_STRING];
531 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
532 if (!buf[0] && Shell)
533 m_strcpy(buf, sizeof(buf), Shell);
535 CLEARLINE (LINES - 1);
538 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
539 mutt_any_key_to_continue (NULL);
544 /* enter a mutt command */
545 void mutt_enter_command (void)
548 char buffer[LONG_STRING], errbuf[SHORT_STRING];
552 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
556 err.dsize = sizeof (errbuf);
558 r = mutt_parse_rc_line (buffer, &token, &err);
559 p_delete(&token.data);
561 /* since errbuf could potentially contain printf() sequences in it,
562 we must call mutt_error() in this fashion so that vsprintf()
563 doesn't expect more arguments that we passed */
565 mutt_message ("%s", errbuf);
567 mutt_error ("%s", errbuf);
571 void mutt_display_address (ENVELOPE * env)
573 const char *pfx = NULL;
574 char buf[SHORT_STRING];
575 address_t *adr = NULL;
577 adr = mutt_get_address(env, &pfx);
583 * Note: We don't convert IDNA to local representation this time.
584 * That is intentional, so the user has an opportunity to copy &
585 * paste the on-the-wire form of the address to other, IDN-unable
590 rfc822_write_address (buf, sizeof (buf), adr, 0);
591 mutt_message ("%s: %s", pfx, buf);
594 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
595 int *cmflags, int *chflags)
598 *chflags = CH_UPDATE_LEN;
600 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
601 if (mutt_is_multipart_encrypted (hdr->content)) {
602 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
603 *cmflags = M_CM_DECODE_PGP;
605 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
607 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
608 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
609 *cmflags = M_CM_DECODE_SMIME;
614 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
615 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
617 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
618 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
620 if (option (OPTWEED)) {
621 *chflags |= CH_WEED; /* and respect $weed. */
622 *cmflags |= M_CM_WEED;
628 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
630 int cmflags, chflags;
633 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
635 if (decode || decrypt)
636 mutt_parse_mime_message (Context, h);
638 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
642 mutt_set_flag (Context, h, M_DELETE, 1);
643 if (option (OPTDELETEUNTAG))
644 mutt_set_flag (Context, h, M_TAG, 0);
645 mutt_set_flag (Context, h, M_APPENDED, 1);
650 /* returns 0 if the copy/save was successful, or -1 on error/abort */
651 int mutt_save_message (HEADER * h, int delete,
652 int decode, int decrypt, int *redraw) {
653 int i, need_buffy_cleanup;
654 int need_passphrase = 0, app = 0;
655 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
663 snprintf (prompt, sizeof (prompt),
664 decode ? (delete ? _("Decode-save%s to mailbox") :
665 _("Decode-copy%s to mailbox")) :
666 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
667 _("Decrypt-copy%s to mailbox")) :
668 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
669 h ? "" : _(" tagged"));
673 need_passphrase = h->security & ENCRYPT;
675 mutt_message_hook (Context, h, M_MESSAGEHOOK);
676 mutt_default_save (buf, sizeof (buf), h);
679 /* look for the first tagged message */
681 for (i = 0; i < Context->vcount; i++) {
682 if (Context->hdrs[Context->v2r[i]]->tagged) {
683 h = Context->hdrs[Context->v2r[i]];
690 mutt_message_hook (Context, h, M_MESSAGEHOOK);
691 mutt_default_save (buf, sizeof (buf), h);
692 need_passphrase = h->security & ENCRYPT;
698 mutt_pretty_mailbox (buf);
699 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
702 if (*redraw != REDRAW_FULL) {
704 *redraw = REDRAW_INDEX | REDRAW_STATUS;
706 *redraw = REDRAW_STATUS;
712 /* This is an undocumented feature of ELM pointed out to me by Felix von
713 * Leitner <leitner@prz.fu-berlin.de>
715 if (m_strcmp(buf, ".") == 0)
716 m_strcpy(buf, sizeof(buf), LastSaveFolder);
718 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
720 mutt_expand_path (buf, sizeof (buf));
722 /* check to make sure that this file is really the one the user wants */
723 if (mutt_save_confirm (buf, &st) != 0)
726 if (need_passphrase && (decode || decrypt)
727 && !crypt_valid_passphrase (app))
730 mutt_message (_("Copying to %s..."), buf);
732 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
733 switch (imap_copy_messages (Context, h, buf, delete)) {
738 /* non-fatal error: fall through to fetch/append */
741 /* fatal error, abort */
747 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
749 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
750 mx_close_mailbox (&ctx, NULL);
754 for (i = 0; i < Context->vcount; i++) {
755 if (Context->hdrs[Context->v2r[i]]->tagged) {
756 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
758 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
759 decode, decrypt) != 0) {
760 mx_close_mailbox (&ctx, NULL);
767 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
769 mx_close_mailbox (&ctx, NULL);
771 if (need_buffy_cleanup) {
772 /* fix up the times so buffy won't get confused */
773 if (st.st_mtime > st.st_atime) {
774 ut.actime = st.st_atime;
775 ut.modtime = time (NULL);
789 void mutt_version (void)
791 mutt_message (mutt_make_version (1));
794 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
796 char buf[LONG_STRING];
797 char obuf[LONG_STRING];
800 char charset[STRING];
803 short charset_changed = 0;
804 short type_changed = 0;
806 cp = parameter_getval(b->parameter, "charset");
807 m_strcpy(charset, sizeof(charset), NONULL(cp));
809 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
810 m_strcpy(obuf, sizeof(obuf), buf);
815 for (p = b->parameter; p; p = p->next) {
818 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
819 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
823 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
827 /* clean up previous junk */
828 parameter_list_wipe(&b->parameter);
829 p_delete(&b->subtype);
831 mutt_parse_content_type (buf, b);
834 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
835 type_changed = ascii_strcasecmp (tmp, obuf);
837 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
839 /* if in send mode, check for conversion - current setting is default. */
841 if (!h && b->type == TYPETEXT && charset_changed) {
844 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
845 parameter_getval(b->parameter, "charset"));
846 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
847 b->noconv = (r == M_NO);
850 /* inform the user */
852 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
854 mutt_message (_("Content-Type changed to %s."), tmp);
855 if (b->type == TYPETEXT && charset_changed) {
858 mutt_message (_("Character set changed to %s; %s."),
859 parameter_getval(b->parameter, "charset"),
860 b->noconv ? _("not converting") : _("converting"));
863 b->force_charset |= charset_changed ? 1 : 0;
865 if (!is_multipart(b) && b->parts)
866 body_list_wipe(&b->parts);
867 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
868 b->hdr->content = NULL;
869 header_delete(&b->hdr);
872 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
873 mutt_parse_part (fp, b);
879 h->security |= crypt_query (b);
884 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
889 h->security |= PGP_TRADITIONAL_CHECKED;
891 mutt_parse_mime_message (Context, h);
892 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
894 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
895 h->security = crypt_query (h->content);
896 *redraw |= REDRAW_FULL;
900 h->security |= PGP_TRADITIONAL_CHECKED;
901 mx_close_message (&msg);
905 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
910 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
911 rv = _mutt_check_traditional_pgp (h, redraw);
913 for (i = 0; i < Context->vcount; i++)
914 if (Context->hdrs[Context->v2r[i]]->tagged &&
915 !(Context->hdrs[Context->v2r[i]]->
916 security & PGP_TRADITIONAL_CHECKED))
918 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)