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 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
60 mutt_error _("Could not create temporary file!");
64 if (DisplayFilter && *DisplayFilter) {
67 /* mutt_endwin (NULL); */
68 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
69 -1, fileno (fpfilterout), -1);
71 mutt_error (_("Cannot create display filter"));
72 safe_fclose (&fpfilterout);
78 if (!Pager || m_strcmp(Pager, "builtin") == 0)
81 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
83 fputs ("\n\n", fpout);
86 msg = mx_open_message (Context, cur->msgno);
87 if (msg == NULL) res = -1;
90 /* see if crytpo is needed for this message. if so, we should exit curses */
92 if (cur->security & ENCRYPT) {
93 if (cur->security & APPLICATION_SMIME)
94 crypt_smime_getkeys (cur->env);
95 if (!crypt_valid_passphrase (cur->security))
98 cmflags |= M_CM_VERIFY;
100 else if (cur->security & SIGN) {
101 /* find out whether or not the verify signature */
102 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
104 cmflags |= M_CM_VERIFY;
109 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
110 if (cur->security & APPLICATION_PGP) {
112 crypt_pgp_invoke_getkeys (cur->env->from);
114 crypt_invoke_message (APPLICATION_PGP);
117 if (cur->security & APPLICATION_SMIME)
118 crypt_invoke_message (APPLICATION_SMIME);
121 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
122 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
123 CH_DECODE | CH_FROM);
124 if (res == 0 && (ferror(fpout) || feof(fpout))) {
128 mx_close_message (&msg);
131 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
132 mutt_error (_("Could not copy message"));
133 if (fpfilterout != NULL) {
134 mutt_wait_filter (filterpid);
135 safe_fclose (&fpfilterout);
138 /* this is maybe just plain wrong but it makes the pager display
139 * what we have; i.e. for the crypto stuff we only get
140 * 'Could not copy message' for invalid passphrases, no PGP output
141 * not nothing; so just display what we have...
144 mutt_unlink (tempfile);
149 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
150 mutt_any_key_to_continue (NULL);
152 safe_fclose (&fpfilterout); /* XXX - check result? */
155 /* update crypto information for this message */
156 cur->security &= ~(GOODSIGN|BADSIGN);
157 cur->security |= crypt_query (cur->content);
159 /* Remove color cache for this message, in case there
160 are color patterns for both ~g and ~V */
166 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
167 if (cur->security & GOODSIGN) {
168 if (!crypt_smime_verify_sender (cur))
169 mutt_message (_("S/MIME signature successfully verified."));
171 mutt_error (_("S/MIME certificate owner does not match sender."));
173 else if (cur->security & PARTSIGN)
175 ("Warning: Part of this message has not been signed."));
176 else if (cur->security & SIGN || cur->security & BADSIGN)
177 mutt_error (_("S/MIME signature could NOT be verified."));
180 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
181 if (cur->security & GOODSIGN)
182 mutt_message (_("PGP signature successfully verified."));
183 else if (cur->security & PARTSIGN)
185 ("Warning: Part of this message has not been signed."));
186 else if (cur->security & SIGN)
187 mutt_message (_("PGP signature could NOT be verified."));
190 /* Invoke the builtin pager */
194 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
200 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
201 if ((r = mutt_system (buf)) == -1)
202 mutt_error (_("Error running \"%s\"!"), buf);
204 keypad (stdscr, TRUE);
206 mutt_set_flag (Context, cur, M_READ, 1);
207 if (r != -1 && option (OPTPROMPTAFTER)) {
208 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
209 rc = km_dokey (MENU_PAGER);
218 void ci_bounce_message (HEADER * h, int *redraw)
220 char prompt[SHORT_STRING];
221 char buf[HUGE_STRING] = { 0 };
222 address_t *adr = NULL;
227 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
229 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
231 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
233 if (option (OPTNEEDREDRAW)) {
234 unset_option (OPTNEEDREDRAW);
235 *redraw = REDRAW_FULL;
241 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
242 mutt_error _("Error parsing address!");
247 adr = mutt_expand_aliases (adr);
249 if (mutt_addrlist_to_idna (adr, &err) < 0) {
250 mutt_error (_("Bad IDN: '%s'"), err);
252 address_list_wipe(&adr);
257 rfc822_write_address (buf, sizeof (buf), adr, 1);
259 #define extra_space (15 + 7 + 2)
260 snprintf (prompt, sizeof (prompt),
261 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
264 if (mutt_strwidth (prompt) > COLS - extra_space) {
265 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
266 prompt, sizeof(prompt), 0);
267 m_strcat(prompt, sizeof(prompt), "...?");
269 m_strcat(prompt, sizeof(prompt), "?");
272 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
273 address_list_wipe(&adr);
274 CLEARLINE (LINES - 1);
275 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
279 CLEARLINE (LINES - 1);
281 rc = mutt_bounce_message (NULL, h, adr);
282 address_list_wipe(&adr);
283 /* If no error, or background, display message. */
284 if ((rc == 0) || (rc == S_BKG))
285 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
288 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
291 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
292 *chflags |= CH_DECODE | CH_REORDER;
294 if (option (OPTWEED)) {
296 *cmflags |= M_CM_WEED;
301 *cmflags |= M_CM_PRINTING;
305 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
308 int chflags = CH_FROM;
310 pipe_set_flags (decode, print, &cmflags, &chflags);
312 if (decode && h->security & ENCRYPT) {
313 if (!crypt_valid_passphrase (h->security))
319 mutt_parse_mime_message (Context, h);
321 mutt_copy_message (fp, Context, h, cmflags, chflags);
325 /* the following code is shared between printing and piping */
327 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
328 int split, const char *sep)
335 /* mutt_endwin (NULL);
337 is this really needed here ?
338 it makes the screen flicker on pgp and s/mime messages,
339 before asking for a passphrase...
343 mutt_message_hook (Context, h, M_MESSAGEHOOK);
346 mutt_parse_mime_message (Context, h);
347 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
352 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
353 mutt_perror (_("Can't create filter process"));
358 pipe_msg (h, fpout, decode, print);
360 rc = mutt_wait_filter (thepid);
362 else { /* handle tagged messages */
365 for (i = 0; i < Context->vcount; i++)
366 if (Context->hdrs[Context->v2r[i]]->tagged) {
367 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
369 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
370 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
371 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
378 for (i = 0; i < Context->vcount; i++) {
379 if (Context->hdrs[Context->v2r[i]]->tagged) {
380 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
383 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
384 mutt_perror (_("Can't create filter process"));
388 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
389 /* add the message separator */
392 safe_fclose (&fpout);
393 if (mutt_wait_filter (thepid) != 0)
400 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
401 mutt_perror (_("Can't create filter process"));
405 for (i = 0; i < Context->vcount; i++) {
406 if (Context->hdrs[Context->v2r[i]]->tagged) {
407 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
409 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
410 /* add the message separator */
415 safe_fclose (&fpout);
416 if (mutt_wait_filter (thepid) != 0)
421 if (rc || option (OPTWAITKEY))
422 mutt_any_key_to_continue (NULL);
426 void mutt_pipe_message (HEADER * h)
428 char buffer[LONG_STRING];
431 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
435 mutt_expand_path (buffer, sizeof (buffer));
436 _mutt_pipe_message (h, buffer,
437 option (OPTPIPEDECODE),
438 0, option (OPTPIPESPLIT), PipeSep);
441 void mutt_print_message (HEADER * h)
444 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
445 mutt_message (_("No printing command has been defined."));
449 if (query_quadoption (OPT_PRINT,
450 h ? _("Print message?") : _("Print tagged messages?"))
454 if (_mutt_pipe_message (h, PrintCmd,
455 option (OPTPRINTDECODE),
456 1, option (OPTPRINTSPLIT), "\f") == 0)
457 mutt_message (h ? _("Message printed") : _("Messages printed"));
459 mutt_message (h ? _("Message could not be printed") :
460 _("Messages could not be printed"));
464 int mutt_select_sort (int reverse)
466 int method = Sort; /* save the current method in case of abort */
468 switch (mutt_multi_choice (reverse ?
470 ("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?: ")
473 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
475 case -1: /* abort - don't resort */
487 Sort = SORT_RECEIVED;
498 case 6: /* (t)hread */
502 case 7: /* (u)nsort */
510 case 9: /* s(c)ore */
514 case 10: /* s(p)am */
519 Sort |= SORT_REVERSE;
521 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
524 /* invoke a command in a subshell */
525 void mutt_shell_escape (void)
527 char buf[LONG_STRING];
530 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
531 if (!buf[0] && Shell)
532 m_strcpy(buf, sizeof(buf), Shell);
534 CLEARLINE (LINES - 1);
537 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
538 mutt_any_key_to_continue (NULL);
543 /* enter a mutt command */
544 void mutt_enter_command (void)
547 char buffer[LONG_STRING], errbuf[SHORT_STRING];
551 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
555 err.dsize = sizeof (errbuf);
557 r = mutt_parse_rc_line (buffer, &token, &err);
558 p_delete(&token.data);
560 /* since errbuf could potentially contain printf() sequences in it,
561 we must call mutt_error() in this fashion so that vsprintf()
562 doesn't expect more arguments that we passed */
564 mutt_message ("%s", errbuf);
566 mutt_error ("%s", errbuf);
570 void mutt_display_address (ENVELOPE * env)
572 const char *pfx = NULL;
573 char buf[SHORT_STRING];
574 address_t *adr = NULL;
576 adr = mutt_get_address(env, &pfx);
582 * Note: We don't convert IDNA to local representation this time.
583 * That is intentional, so the user has an opportunity to copy &
584 * paste the on-the-wire form of the address to other, IDN-unable
589 rfc822_write_address (buf, sizeof (buf), adr, 0);
590 mutt_message ("%s: %s", pfx, buf);
593 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
594 int *cmflags, int *chflags)
597 *chflags = CH_UPDATE_LEN;
599 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
600 if (mutt_is_multipart_encrypted (hdr->content)) {
601 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
602 *cmflags = M_CM_DECODE_PGP;
604 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
606 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
607 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
608 *cmflags = M_CM_DECODE_SMIME;
613 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
614 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
616 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
617 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
619 if (option (OPTWEED)) {
620 *chflags |= CH_WEED; /* and respect $weed. */
621 *cmflags |= M_CM_WEED;
627 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
629 int cmflags, chflags;
632 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
634 if (decode || decrypt)
635 mutt_parse_mime_message (Context, h);
637 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
641 mutt_set_flag (Context, h, M_DELETE, 1);
642 if (option (OPTDELETEUNTAG))
643 mutt_set_flag (Context, h, M_TAG, 0);
644 mutt_set_flag (Context, h, M_APPENDED, 1);
649 /* returns 0 if the copy/save was successful, or -1 on error/abort */
650 int mutt_save_message (HEADER * h, int delete,
651 int decode, int decrypt, int *redraw) {
652 int i, need_buffy_cleanup;
653 int need_passphrase = 0, app = 0;
654 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
662 snprintf (prompt, sizeof (prompt),
663 decode ? (delete ? _("Decode-save%s to mailbox") :
664 _("Decode-copy%s to mailbox")) :
665 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
666 _("Decrypt-copy%s to mailbox")) :
667 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
668 h ? "" : _(" tagged"));
672 need_passphrase = h->security & ENCRYPT;
674 mutt_message_hook (Context, h, M_MESSAGEHOOK);
675 mutt_default_save (buf, sizeof (buf), h);
678 /* look for the first tagged message */
680 for (i = 0; i < Context->vcount; i++) {
681 if (Context->hdrs[Context->v2r[i]]->tagged) {
682 h = Context->hdrs[Context->v2r[i]];
689 mutt_message_hook (Context, h, M_MESSAGEHOOK);
690 mutt_default_save (buf, sizeof (buf), h);
691 need_passphrase = h->security & ENCRYPT;
697 mutt_pretty_mailbox (buf);
698 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
701 if (*redraw != REDRAW_FULL) {
703 *redraw = REDRAW_INDEX | REDRAW_STATUS;
705 *redraw = REDRAW_STATUS;
711 /* This is an undocumented feature of ELM pointed out to me by Felix von
712 * Leitner <leitner@prz.fu-berlin.de>
714 if (m_strcmp(buf, ".") == 0)
715 m_strcpy(buf, sizeof(buf), LastSaveFolder);
717 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
719 mutt_expand_path (buf, sizeof (buf));
721 /* check to make sure that this file is really the one the user wants */
722 if (mutt_save_confirm (buf, &st) != 0)
725 if (need_passphrase && (decode || decrypt)
726 && !crypt_valid_passphrase (app))
729 mutt_message (_("Copying to %s..."), buf);
731 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
732 switch (imap_copy_messages (Context, h, buf, delete)) {
737 /* non-fatal error: fall through to fetch/append */
740 /* fatal error, abort */
746 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
748 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
749 mx_close_mailbox (&ctx, NULL);
753 for (i = 0; i < Context->vcount; i++) {
754 if (Context->hdrs[Context->v2r[i]]->tagged) {
755 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
757 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
758 decode, decrypt) != 0) {
759 mx_close_mailbox (&ctx, NULL);
766 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
768 mx_close_mailbox (&ctx, NULL);
770 if (need_buffy_cleanup) {
771 /* fix up the times so buffy won't get confused */
772 if (st.st_mtime > st.st_atime) {
773 ut.actime = st.st_atime;
774 ut.modtime = time (NULL);
788 void mutt_version (void)
790 mutt_message (mutt_make_version (1));
793 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
795 char buf[LONG_STRING];
796 char obuf[LONG_STRING];
799 char charset[STRING];
802 short charset_changed = 0;
803 short type_changed = 0;
805 cp = parameter_getval(b->parameter, "charset");
806 m_strcpy(charset, sizeof(charset), NONULL(cp));
808 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
809 m_strcpy(obuf, sizeof(obuf), buf);
814 for (p = b->parameter; p; p = p->next) {
817 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
818 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
822 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
826 /* clean up previous junk */
827 parameter_list_wipe(&b->parameter);
828 p_delete(&b->subtype);
830 mutt_parse_content_type (buf, b);
833 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
834 type_changed = ascii_strcasecmp (tmp, obuf);
836 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
838 /* if in send mode, check for conversion - current setting is default. */
840 if (!h && b->type == TYPETEXT && charset_changed) {
843 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
844 parameter_getval(b->parameter, "charset"));
845 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
846 b->noconv = (r == M_NO);
849 /* inform the user */
851 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
853 mutt_message (_("Content-Type changed to %s."), tmp);
854 if (b->type == TYPETEXT && charset_changed) {
857 mutt_message (_("Character set changed to %s; %s."),
858 parameter_getval(b->parameter, "charset"),
859 b->noconv ? _("not converting") : _("converting"));
862 b->force_charset |= charset_changed ? 1 : 0;
864 if (!is_multipart(b) && b->parts)
865 body_list_wipe(&b->parts);
866 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
867 b->hdr->content = NULL;
868 header_delete(&b->hdr);
871 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
872 mutt_parse_part (fp, b);
878 h->security |= crypt_query (b);
883 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
888 h->security |= PGP_TRADITIONAL_CHECKED;
890 mutt_parse_mime_message (Context, h);
891 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
893 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
894 h->security = crypt_query (h->content);
895 *redraw |= REDRAW_FULL;
899 h->security |= PGP_TRADITIONAL_CHECKED;
900 mx_close_message (&msg);
904 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
909 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
910 rv = _mutt_check_traditional_pgp (h, redraw);
912 for (i = 0; i < Context->vcount; i++)
913 if (Context->hdrs[Context->v2r[i]]->tagged &&
914 !(Context->hdrs[Context->v2r[i]]->
915 security & PGP_TRADITIONAL_CHECKED))
917 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)