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 m_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 ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
132 mutt_error (_("Could not copy message"));
133 if (fpfilterout != NULL) {
134 mutt_wait_filter (filterpid);
135 m_fclose(&fpfilterout);
139 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
140 mutt_any_key_to_continue (NULL);
142 m_fclose(&fpfilterout); /* XXX - check result? */
145 /* update crypto information for this message */
146 cur->security &= ~(GOODSIGN|BADSIGN);
147 cur->security |= crypt_query (cur->content);
149 /* Remove color cache for this message, in case there
150 are color patterns for both ~g and ~V */
156 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
157 if (cur->security & GOODSIGN) {
158 if (!crypt_smime_verify_sender (cur))
159 mutt_message (_("S/MIME signature successfully verified."));
161 mutt_error (_("S/MIME certificate owner does not match sender."));
163 else if (cur->security & PARTSIGN)
165 ("Warning: Part of this message has not been signed."));
166 else if (cur->security & SIGN || cur->security & BADSIGN)
167 mutt_error (_("S/MIME signature could NOT be verified."));
170 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
171 if (cur->security & GOODSIGN)
172 mutt_message (_("PGP signature successfully verified."));
173 else if (cur->security & PARTSIGN)
175 ("Warning: Part of this message has not been signed."));
176 else if (cur->security & SIGN)
177 mutt_message (_("PGP signature could NOT be verified."));
180 /* Invoke the builtin pager */
184 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
190 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
191 if ((r = mutt_system (buf)) == -1)
192 mutt_error (_("Error running \"%s\"!"), buf);
194 keypad (stdscr, TRUE);
196 mutt_set_flag (Context, cur, M_READ, 1);
197 if (r != -1 && option (OPTPROMPTAFTER)) {
198 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
199 rc = km_dokey (MENU_PAGER);
208 void ci_bounce_message (HEADER * h, int *redraw)
211 char buf[HUGE_STRING] = { 0 };
212 address_t *adr = NULL;
217 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
219 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
221 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
223 if (option (OPTNEEDREDRAW)) {
224 unset_option (OPTNEEDREDRAW);
225 *redraw = REDRAW_FULL;
231 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
232 mutt_error _("Error parsing address!");
237 adr = mutt_expand_aliases (adr);
239 if (mutt_addrlist_to_idna (adr, &err) < 0) {
240 mutt_error (_("Bad IDN: '%s'"), err);
242 address_list_wipe(&adr);
247 rfc822_addrcat(buf, sizeof (buf), adr, 1);
249 #define extra_space (15 + 7 + 2)
250 snprintf (prompt, sizeof (prompt),
251 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
254 if (m_strwidth(prompt) > COLS - extra_space) {
255 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
256 prompt, sizeof(prompt), 0);
257 m_strcat(prompt, sizeof(prompt), "...?");
259 m_strcat(prompt, sizeof(prompt), "?");
262 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
263 address_list_wipe(&adr);
264 CLEARLINE (LINES - 1);
265 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
269 CLEARLINE (LINES - 1);
271 rc = mutt_bounce_message (NULL, h, adr);
272 address_list_wipe(&adr);
273 /* If no error, or background, display message. */
274 if ((rc == 0) || (rc == S_BKG))
275 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
278 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
281 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
282 *chflags |= CH_DECODE | CH_REORDER;
284 if (option (OPTWEED)) {
286 *cmflags |= M_CM_WEED;
291 *cmflags |= M_CM_PRINTING;
295 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
298 int chflags = CH_FROM;
300 pipe_set_flags (decode, print, &cmflags, &chflags);
302 if (decode && h->security & ENCRYPT) {
303 if (!crypt_valid_passphrase (h->security))
309 mutt_parse_mime_message (Context, h);
311 mutt_copy_message (fp, Context, h, cmflags, chflags);
315 /* the following code is shared between printing and piping */
317 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
318 int split, const char *sep)
325 /* mutt_endwin (NULL);
327 is this really needed here ?
328 it makes the screen flicker on pgp and s/mime messages,
329 before asking for a passphrase...
333 mutt_message_hook (Context, h, M_MESSAGEHOOK);
336 mutt_parse_mime_message (Context, h);
337 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
342 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
343 mutt_perror (_("Can't create filter process"));
348 pipe_msg (h, fpout, decode, print);
350 rc = mutt_wait_filter (thepid);
352 else { /* handle tagged messages */
355 for (i = 0; i < Context->vcount; i++)
356 if (Context->hdrs[Context->v2r[i]]->tagged) {
357 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
359 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
360 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
361 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
368 for (i = 0; i < Context->vcount; i++) {
369 if (Context->hdrs[Context->v2r[i]]->tagged) {
370 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
373 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
374 mutt_perror (_("Can't create filter process"));
378 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
379 /* add the message separator */
383 if (mutt_wait_filter (thepid) != 0)
390 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
391 mutt_perror (_("Can't create filter process"));
395 for (i = 0; i < Context->vcount; i++) {
396 if (Context->hdrs[Context->v2r[i]]->tagged) {
397 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
399 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
400 /* add the message separator */
406 if (mutt_wait_filter (thepid) != 0)
411 if (rc || option (OPTWAITKEY))
412 mutt_any_key_to_continue (NULL);
416 void mutt_pipe_message (HEADER * h)
418 char buffer[LONG_STRING];
421 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
425 mutt_expand_path (buffer, sizeof (buffer));
426 _mutt_pipe_message (h, buffer,
427 option (OPTPIPEDECODE),
428 0, option (OPTPIPESPLIT), PipeSep);
431 void mutt_print_message (HEADER * h)
434 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
435 mutt_message (_("No printing command has been defined."));
439 if (query_quadoption (OPT_PRINT,
440 h ? _("Print message?") : _("Print tagged messages?"))
444 if (_mutt_pipe_message (h, PrintCmd,
445 option (OPTPRINTDECODE),
446 1, option (OPTPRINTSPLIT), "\f") == 0)
447 mutt_message (h ? _("Message printed") : _("Messages printed"));
449 mutt_message (h ? _("Message could not be printed") :
450 _("Messages could not be printed"));
454 int mutt_select_sort (int reverse)
456 int method = Sort; /* save the current method in case of abort */
458 switch (mutt_multi_choice (reverse ?
460 ("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?: ")
463 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
465 case -1: /* abort - don't resort */
477 Sort = SORT_RECEIVED;
488 case 6: /* (t)hread */
492 case 7: /* (u)nsort */
500 case 9: /* s(c)ore */
504 case 10: /* s(p)am */
509 Sort |= SORT_REVERSE;
511 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
514 /* invoke a command in a subshell */
515 void mutt_shell_escape (void)
517 char buf[LONG_STRING];
520 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
521 if (!buf[0] && Shell)
522 m_strcpy(buf, sizeof(buf), Shell);
524 CLEARLINE (LINES - 1);
527 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
528 mutt_any_key_to_continue (NULL);
533 /* enter a mutt command */
534 void mutt_enter_command (void)
537 char buffer[LONG_STRING], errbuf[STRING];
541 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
545 err.dsize = sizeof (errbuf);
547 r = mutt_parse_rc_line (buffer, &token, &err);
548 p_delete(&token.data);
550 /* since errbuf could potentially contain printf() sequences in it,
551 we must call mutt_error() in this fashion so that vsprintf()
552 doesn't expect more arguments that we passed */
554 mutt_message ("%s", errbuf);
556 mutt_error ("%s", errbuf);
560 void mutt_display_address (ENVELOPE * env)
562 const char *pfx = NULL;
564 address_t *adr = NULL;
566 adr = mutt_get_address(env, &pfx);
572 * Note: We don't convert IDNA to local representation this time.
573 * That is intentional, so the user has an opportunity to copy &
574 * paste the on-the-wire form of the address to other, IDN-unable
579 rfc822_addrcat(buf, sizeof (buf), adr, 0);
580 mutt_message ("%s: %s", pfx, buf);
583 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
584 int *cmflags, int *chflags)
587 *chflags = CH_UPDATE_LEN;
589 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
590 if (mutt_is_multipart_encrypted (hdr->content)) {
591 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
592 *cmflags = M_CM_DECODE_PGP;
594 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
596 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
597 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
598 *cmflags = M_CM_DECODE_SMIME;
603 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
604 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
606 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
607 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
609 if (option (OPTWEED)) {
610 *chflags |= CH_WEED; /* and respect $weed. */
611 *cmflags |= M_CM_WEED;
617 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
619 int cmflags, chflags;
622 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
624 if (decode || decrypt)
625 mutt_parse_mime_message (Context, h);
627 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
631 mutt_set_flag (Context, h, M_DELETE, 1);
632 if (option (OPTDELETEUNTAG))
633 mutt_set_flag (Context, h, M_TAG, 0);
634 mutt_set_flag (Context, h, M_APPENDED, 1);
639 /* returns 0 if the copy/save was successful, or -1 on error/abort */
640 int mutt_save_message (HEADER * h, int delete,
641 int decode, int decrypt, int *redraw) {
642 int i, need_buffy_cleanup;
643 int need_passphrase = 0, app = 0;
644 char prompt[STRING], buf[_POSIX_PATH_MAX];
652 snprintf (prompt, sizeof (prompt),
653 decode ? (delete ? _("Decode-save%s to mailbox") :
654 _("Decode-copy%s to mailbox")) :
655 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
656 _("Decrypt-copy%s to mailbox")) :
657 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
658 h ? "" : _(" tagged"));
662 need_passphrase = h->security & ENCRYPT;
664 mutt_message_hook (Context, h, M_MESSAGEHOOK);
665 mutt_default_save (buf, sizeof (buf), h);
668 /* look for the first tagged message */
670 for (i = 0; i < Context->vcount; i++) {
671 if (Context->hdrs[Context->v2r[i]]->tagged) {
672 h = Context->hdrs[Context->v2r[i]];
679 mutt_message_hook (Context, h, M_MESSAGEHOOK);
680 mutt_default_save (buf, sizeof (buf), h);
681 need_passphrase = h->security & ENCRYPT;
687 mutt_pretty_mailbox (buf);
688 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
691 if (*redraw != REDRAW_FULL) {
693 *redraw = REDRAW_INDEX | REDRAW_STATUS;
695 *redraw = REDRAW_STATUS;
701 /* This is an undocumented feature of ELM pointed out to me by Felix von
702 * Leitner <leitner@prz.fu-berlin.de>
704 if (m_strcmp(buf, ".") == 0)
705 m_strcpy(buf, sizeof(buf), LastSaveFolder);
707 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
709 mutt_expand_path (buf, sizeof (buf));
711 /* check to make sure that this file is really the one the user wants */
712 if (mutt_save_confirm (buf, &st) != 0)
715 if (need_passphrase && (decode || decrypt)
716 && !crypt_valid_passphrase (app))
719 mutt_message (_("Copying to %s..."), buf);
721 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
722 switch (imap_copy_messages (Context, h, buf, delete)) {
727 /* non-fatal error: fall through to fetch/append */
730 /* fatal error, abort */
736 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
738 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
739 mx_close_mailbox (&ctx, NULL);
743 for (i = 0; i < Context->vcount; i++) {
744 if (Context->hdrs[Context->v2r[i]]->tagged) {
745 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
747 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
748 decode, decrypt) != 0) {
749 mx_close_mailbox (&ctx, NULL);
756 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
758 mx_close_mailbox (&ctx, NULL);
760 if (need_buffy_cleanup) {
761 /* fix up the times so buffy won't get confused */
762 if (st.st_mtime > st.st_atime) {
763 ut.actime = st.st_atime;
764 ut.modtime = time (NULL);
778 void mutt_version (void)
780 mutt_message (mutt_make_version (1));
783 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
785 char buf[LONG_STRING];
786 char obuf[LONG_STRING];
789 char charset[STRING];
792 short charset_changed = 0;
793 short type_changed = 0;
795 cp = parameter_getval(b->parameter, "charset");
796 m_strcpy(charset, sizeof(charset), NONULL(cp));
798 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
799 m_strcpy(obuf, sizeof(obuf), buf);
804 for (p = b->parameter; p; p = p->next) {
807 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
808 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
812 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
816 /* clean up previous junk */
817 parameter_list_wipe(&b->parameter);
818 p_delete(&b->subtype);
820 mutt_parse_content_type (buf, b);
823 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
824 type_changed = ascii_strcasecmp (tmp, obuf);
826 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
828 /* if in send mode, check for conversion - current setting is default. */
830 if (!h && b->type == TYPETEXT && charset_changed) {
833 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
834 parameter_getval(b->parameter, "charset"));
835 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
836 b->noconv = (r == M_NO);
839 /* inform the user */
841 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
843 mutt_message (_("Content-Type changed to %s."), tmp);
844 if (b->type == TYPETEXT && charset_changed) {
847 mutt_message (_("Character set changed to %s; %s."),
848 parameter_getval(b->parameter, "charset"),
849 b->noconv ? _("not converting") : _("converting"));
852 b->force_charset |= charset_changed ? 1 : 0;
854 if (!is_multipart(b) && b->parts)
855 body_list_wipe(&b->parts);
856 if (!mutt_is_message_type(b) && b->hdr) {
857 b->hdr->content = NULL;
858 header_delete(&b->hdr);
861 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
862 mutt_parse_part (fp, b);
868 h->security |= crypt_query (b);
873 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
878 h->security |= PGP_TRADITIONAL_CHECKED;
880 mutt_parse_mime_message (Context, h);
881 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
883 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
884 h->security = crypt_query (h->content);
885 *redraw |= REDRAW_FULL;
889 h->security |= PGP_TRADITIONAL_CHECKED;
890 mx_close_message (&msg);
894 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
899 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
900 rv = _mutt_check_traditional_pgp (h, redraw);
902 for (i = 0; i < Context->vcount; i++)
903 if (Context->hdrs[Context->v2r[i]]->tagged &&
904 !(Context->hdrs[Context->v2r[i]]->
905 security & PGP_TRADITIONAL_CHECKED))
907 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)