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-lua/lib-lua.h>
20 #include <lib-ui/curses.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
27 #include "recvattach.h"
32 #include "mutt_idna.h"
33 #include <sys/types.h>
37 #include <imap/imap.h>
39 /* The folder the user last saved to. Used by ci_save_message() */
40 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
42 int mutt_display_message (HEADER * cur)
44 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
45 int rc = 0, builtin = 0;
46 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
48 FILE *fpfilterout = NULL;
53 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
54 cur->content->subtype);
56 mutt_parse_mime_message (Context, cur);
57 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
59 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
61 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 m_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 cmflags |= M_CM_VERIFY;
96 else if (cur->security & SIGN) {
97 /* find out whether or not the verify signature */
98 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
100 cmflags |= M_CM_VERIFY;
105 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
106 crypt_invoke_message(cur->security);
109 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
110 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
111 CH_DECODE | CH_FROM);
112 if (res == 0 && (ferror(fpout) || feof(fpout))) {
116 mx_close_message (&msg);
119 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
120 mutt_error (_("Could not copy message"));
121 if (fpfilterout != NULL) {
122 mutt_wait_filter (filterpid);
123 m_fclose(&fpfilterout);
127 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
128 mutt_any_key_to_continue (NULL);
130 m_fclose(&fpfilterout); /* XXX - check result? */
133 /* update crypto information for this message */
134 cur->security &= ~(GOODSIGN|BADSIGN);
135 cur->security |= crypt_query (cur->content);
137 /* Remove color cache for this message, in case there
138 are color patterns for both ~g and ~V */
144 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
145 if (cur->security & GOODSIGN) {
146 if (!crypt_smime_verify_sender (cur))
147 mutt_message (_("S/MIME signature successfully verified."));
149 mutt_error (_("S/MIME certificate owner does not match sender."));
151 else if (cur->security & PARTSIGN)
153 ("Warning: Part of this message has not been signed."));
154 else if (cur->security & SIGN || cur->security & BADSIGN)
155 mutt_error (_("S/MIME signature could NOT be verified."));
158 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
159 if (cur->security & GOODSIGN)
160 mutt_message (_("PGP signature successfully verified."));
161 else if (cur->security & PARTSIGN)
163 ("Warning: Part of this message has not been signed."));
164 else if (cur->security & SIGN)
165 mutt_message (_("PGP signature could NOT be verified."));
168 /* Invoke the builtin pager */
172 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
178 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
179 if ((r = mutt_system (buf)) == -1)
180 mutt_error (_("Error running \"%s\"!"), buf);
182 keypad (stdscr, TRUE);
184 mutt_set_flag (Context, cur, M_READ, 1);
185 if (r != -1 && option (OPTPROMPTAFTER)) {
186 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
187 rc = km_dokey (MENU_PAGER);
196 void ci_bounce_message (HEADER * h, int *redraw)
199 char buf[HUGE_STRING] = { 0 };
200 address_t *adr = NULL;
205 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
207 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
209 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
211 if (option (OPTNEEDREDRAW)) {
212 unset_option (OPTNEEDREDRAW);
213 *redraw = REDRAW_FULL;
219 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
220 mutt_error _("Error parsing address!");
225 adr = mutt_expand_aliases (adr);
227 if (mutt_addrlist_to_idna (adr, &err) < 0) {
228 mutt_error (_("Bad IDN: '%s'"), err);
230 address_list_wipe(&adr);
235 rfc822_addrcat(buf, sizeof (buf), adr, 1);
237 #define extra_space (15 + 7 + 2)
238 snprintf (prompt, sizeof (prompt),
239 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
242 if (m_strwidth(prompt) > COLS - extra_space) {
243 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
244 prompt, sizeof(prompt), 0);
245 m_strcat(prompt, sizeof(prompt), "...?");
247 m_strcat(prompt, sizeof(prompt), "?");
250 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
251 address_list_wipe(&adr);
252 CLEARLINE (LINES - 1);
253 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
257 CLEARLINE (LINES - 1);
259 rc = mutt_bounce_message (NULL, h, adr);
260 address_list_wipe(&adr);
261 /* If no error, or background, display message. */
262 if ((rc == 0) || (rc == S_BKG))
263 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
266 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
269 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
270 *chflags |= CH_DECODE | CH_REORDER;
272 if (option (OPTWEED)) {
274 *cmflags |= M_CM_WEED;
279 *cmflags |= M_CM_PRINTING;
283 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
286 int chflags = CH_FROM;
288 pipe_set_flags (decode, print, &cmflags, &chflags);
290 if (decode && h->security & ENCRYPT) {
295 mutt_parse_mime_message (Context, h);
297 mutt_copy_message (fp, Context, h, cmflags, chflags);
301 /* the following code is shared between printing and piping */
303 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
304 int split, const char *sep)
311 /* mutt_endwin (NULL);
313 is this really needed here ?
314 it makes the screen flicker on pgp and s/mime messages,
315 before asking for a passphrase...
319 mutt_message_hook (Context, h, M_MESSAGEHOOK);
322 mutt_parse_mime_message (Context, h);
326 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
327 mutt_perror (_("Can't create filter process"));
332 pipe_msg (h, fpout, decode, print);
334 rc = mutt_wait_filter (thepid);
336 else { /* handle tagged messages */
339 for (i = 0; i < Context->vcount; i++)
340 if (Context->hdrs[Context->v2r[i]]->tagged) {
341 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
343 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
348 for (i = 0; i < Context->vcount; i++) {
349 if (Context->hdrs[Context->v2r[i]]->tagged) {
350 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
353 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
354 mutt_perror (_("Can't create filter process"));
358 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
359 /* add the message separator */
363 if (mutt_wait_filter (thepid) != 0)
370 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
371 mutt_perror (_("Can't create filter process"));
375 for (i = 0; i < Context->vcount; i++) {
376 if (Context->hdrs[Context->v2r[i]]->tagged) {
377 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
379 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
380 /* add the message separator */
386 if (mutt_wait_filter (thepid) != 0)
391 if (rc || option (OPTWAITKEY))
392 mutt_any_key_to_continue (NULL);
396 void mutt_pipe_message (HEADER * h)
398 char buffer[LONG_STRING];
401 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
405 mutt_expand_path (buffer, sizeof (buffer));
406 _mutt_pipe_message (h, buffer,
407 option (OPTPIPEDECODE),
408 0, option (OPTPIPESPLIT), PipeSep);
411 void mutt_print_message (HEADER * h)
414 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
415 mutt_message (_("No printing command has been defined."));
419 if (query_quadoption (OPT_PRINT,
420 h ? _("Print message?") : _("Print tagged messages?"))
424 if (_mutt_pipe_message (h, PrintCmd,
425 option (OPTPRINTDECODE),
426 1, option (OPTPRINTSPLIT), "\f") == 0)
427 mutt_message (h ? _("Message printed") : _("Messages printed"));
429 mutt_message (h ? _("Message could not be printed") :
430 _("Messages could not be printed"));
434 int mutt_select_sort (int reverse)
436 int method = Sort; /* save the current method in case of abort */
438 switch (mutt_multi_choice (reverse ?
440 ("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?: ")
443 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
445 case -1: /* abort - don't resort */
457 Sort = SORT_RECEIVED;
468 case 6: /* (t)hread */
472 case 7: /* (u)nsort */
480 case 9: /* s(c)ore */
484 case 10: /* s(p)am */
489 Sort |= SORT_REVERSE;
491 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
494 /* invoke a command in a subshell */
495 void mutt_shell_escape (void)
497 char buf[LONG_STRING];
500 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
502 m_strcpy(buf, sizeof(buf), MCore.shell);
504 CLEARLINE (LINES - 1);
507 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
508 mutt_any_key_to_continue (NULL);
513 /* enter a mutt command */
514 void mutt_enter_command (void)
517 char buffer[LONG_STRING], errbuf[STRING];
521 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
525 err.dsize = sizeof (errbuf);
527 r = mutt_parse_rc_line (buffer, &token, &err);
528 p_delete(&token.data);
530 /* since errbuf could potentially contain printf() sequences in it,
531 we must call mutt_error() in this fashion so that vsprintf()
532 doesn't expect more arguments that we passed */
534 mutt_message ("%s", errbuf);
536 mutt_error ("%s", errbuf);
540 void mutt_display_address (ENVELOPE * env)
542 const char *pfx = NULL;
544 address_t *adr = NULL;
546 adr = mutt_get_address(env, &pfx);
552 * Note: We don't convert IDNA to local representation this time.
553 * That is intentional, so the user has an opportunity to copy &
554 * paste the on-the-wire form of the address to other, IDN-unable
559 rfc822_addrcat(buf, sizeof (buf), adr, 0);
560 mutt_message ("%s: %s", pfx, buf);
563 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
564 int *cmflags, int *chflags)
567 *chflags = CH_UPDATE_LEN;
569 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
570 if (mutt_is_multipart_encrypted (hdr->content)) {
571 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
572 *cmflags = M_CM_DECODE_PGP;
574 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
576 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
577 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
578 *cmflags = M_CM_DECODE_SMIME;
583 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
584 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
586 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
587 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
589 if (option (OPTWEED)) {
590 *chflags |= CH_WEED; /* and respect $weed. */
591 *cmflags |= M_CM_WEED;
597 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
599 int cmflags, chflags;
602 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
604 if (decode || decrypt)
605 mutt_parse_mime_message (Context, h);
607 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
611 mutt_set_flag (Context, h, M_DELETE, 1);
612 if (option (OPTDELETEUNTAG))
613 mutt_set_flag (Context, h, M_TAG, 0);
614 mutt_set_flag (Context, h, M_APPENDED, 1);
619 /* returns 0 if the copy/save was successful, or -1 on error/abort */
620 int mutt_save_message (HEADER * h, int delete,
621 int decode, int decrypt, int *redraw) {
622 int i, need_buffy_cleanup;
623 int need_passphrase = 0, app = 0;
624 char prompt[STRING], buf[_POSIX_PATH_MAX];
632 snprintf (prompt, sizeof (prompt),
633 decode ? (delete ? _("Decode-save%s to mailbox") :
634 _("Decode-copy%s to mailbox")) :
635 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
636 _("Decrypt-copy%s to mailbox")) :
637 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
638 h ? "" : _(" tagged"));
642 need_passphrase = h->security & ENCRYPT;
644 mutt_message_hook (Context, h, M_MESSAGEHOOK);
645 mutt_default_save (buf, sizeof (buf), h);
648 /* look for the first tagged message */
650 for (i = 0; i < Context->vcount; i++) {
651 if (Context->hdrs[Context->v2r[i]]->tagged) {
652 h = Context->hdrs[Context->v2r[i]];
659 mutt_message_hook (Context, h, M_MESSAGEHOOK);
660 mutt_default_save (buf, sizeof (buf), h);
661 need_passphrase = h->security & ENCRYPT;
667 mutt_pretty_mailbox (buf);
668 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
671 if (*redraw != REDRAW_FULL) {
673 *redraw = REDRAW_INDEX | REDRAW_STATUS;
675 *redraw = REDRAW_STATUS;
681 /* This is an undocumented feature of ELM pointed out to me by Felix von
682 * Leitner <leitner@prz.fu-berlin.de>
684 if (m_strcmp(buf, ".") == 0)
685 m_strcpy(buf, sizeof(buf), LastSaveFolder);
687 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
689 mutt_expand_path (buf, sizeof (buf));
691 /* check to make sure that this file is really the one the user wants */
692 if (mutt_save_confirm (buf, &st) != 0)
695 mutt_message (_("Copying to %s..."), buf);
697 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
698 switch (imap_copy_messages (Context, h, buf, delete)) {
703 /* non-fatal error: fall through to fetch/append */
706 /* fatal error, abort */
712 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
714 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
715 mx_close_mailbox (&ctx, NULL);
719 for (i = 0; i < Context->vcount; i++) {
720 if (Context->hdrs[Context->v2r[i]]->tagged) {
721 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
723 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
724 decode, decrypt) != 0) {
725 mx_close_mailbox (&ctx, NULL);
732 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
734 mx_close_mailbox (&ctx, NULL);
736 if (need_buffy_cleanup) {
737 /* fix up the times so buffy won't get confused */
738 if (st.st_mtime > st.st_atime) {
739 ut.actime = st.st_atime;
740 ut.modtime = time (NULL);
754 void mutt_version (void)
756 mutt_message (mutt_make_version());
759 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
761 char buf[LONG_STRING];
762 char obuf[LONG_STRING];
765 char charset[STRING];
768 short charset_changed = 0;
769 short type_changed = 0;
771 cp = parameter_getval(b->parameter, "charset");
772 m_strcpy(charset, sizeof(charset), NONULL(cp));
774 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
775 m_strcpy(obuf, sizeof(obuf), buf);
780 for (p = b->parameter; p; p = p->next) {
783 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
784 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
788 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
792 /* clean up previous junk */
793 parameter_list_wipe(&b->parameter);
794 p_delete(&b->subtype);
796 mutt_parse_content_type (buf, b);
799 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
800 type_changed = ascii_strcasecmp (tmp, obuf);
802 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
804 /* if in send mode, check for conversion - current setting is default. */
806 if (!h && b->type == TYPETEXT && charset_changed) {
809 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
810 parameter_getval(b->parameter, "charset"));
811 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
812 b->noconv = (r == M_NO);
815 /* inform the user */
817 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
819 mutt_message (_("Content-Type changed to %s."), tmp);
820 if (b->type == TYPETEXT && charset_changed) {
823 mutt_message (_("Character set changed to %s; %s."),
824 parameter_getval(b->parameter, "charset"),
825 b->noconv ? _("not converting") : _("converting"));
828 b->force_charset |= charset_changed ? 1 : 0;
830 if (!is_multipart(b) && b->parts)
831 body_list_wipe(&b->parts);
832 if (!mutt_is_message_type(b) && b->hdr) {
833 b->hdr->content = NULL;
834 header_delete(&b->hdr);
837 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
838 mutt_parse_part (fp, b);
844 h->security |= crypt_query (b);
849 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
854 h->security |= PGP_TRADITIONAL_CHECKED;
856 mutt_parse_mime_message (Context, h);
857 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
859 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
860 h->security = crypt_query (h->content);
861 *redraw |= REDRAW_FULL;
865 h->security |= PGP_TRADITIONAL_CHECKED;
866 mx_close_message (&msg);
870 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
875 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
876 rv = _mutt_check_traditional_pgp (h, redraw);
878 for (i = 0; i < Context->vcount; i++)
879 if (Context->hdrs[Context->v2r[i]]->tagged &&
880 !(Context->hdrs[Context->v2r[i]]->
881 security & PGP_TRADITIONAL_CHECKED))
883 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)