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>
25 #include "recvattach.h"
30 #include <lib-crypt/crypt.h>
31 #include "mutt_idna.h"
32 #include <sys/types.h>
36 #include <imap/imap.h>
42 /* The folder the user last saved to. Used by ci_save_message() */
43 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
45 int mutt_display_message (HEADER * cur)
47 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
48 int rc = 0, builtin = 0;
49 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
51 FILE *fpfilterout = NULL;
56 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
57 cur->content->subtype);
59 mutt_parse_mime_message (Context, cur);
60 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
62 mutt_mktemp (tempfile);
63 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
64 mutt_error _("Could not create temporary file!");
69 if (DisplayFilter && *DisplayFilter) {
72 /* mutt_endwin (NULL); */
73 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
74 -1, fileno (fpfilterout), -1);
76 mutt_error (_("Cannot create display filter"));
77 safe_fclose (&fpfilterout);
83 if (!Pager || m_strcmp(Pager, "builtin") == 0)
86 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
88 fputs ("\n\n", fpout);
91 msg = mx_open_message (Context, cur->msgno);
92 if (msg == NULL) res = -1;
95 /* see if crytpo is needed for this message. if so, we should exit curses */
97 if (cur->security & ENCRYPT) {
98 if (cur->security & APPLICATION_SMIME)
99 crypt_smime_getkeys (cur->env);
100 if (!crypt_valid_passphrase (cur->security))
103 cmflags |= M_CM_VERIFY;
105 else if (cur->security & SIGN) {
106 /* find out whether or not the verify signature */
107 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
109 cmflags |= M_CM_VERIFY;
114 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
115 if (cur->security & APPLICATION_PGP) {
117 crypt_pgp_invoke_getkeys (cur->env->from);
119 crypt_invoke_message (APPLICATION_PGP);
122 if (cur->security & APPLICATION_SMIME)
123 crypt_invoke_message (APPLICATION_SMIME);
126 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
127 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
128 CH_DECODE | CH_FROM);
129 if (res == 0 && (ferror(fpout) || feof(fpout))) {
133 mx_close_message (&msg);
136 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
137 mutt_error (_("Could not copy message"));
138 if (fpfilterout != NULL) {
139 mutt_wait_filter (filterpid);
140 safe_fclose (&fpfilterout);
143 /* this is maybe just plain wrong but it makes the pager display
144 * what we have; i.e. for the crypto stuff we only get
145 * 'Could not copy message' for invalid passphrases, no PGP output
146 * not nothing; so just display what we have...
149 mutt_unlink (tempfile);
154 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
155 mutt_any_key_to_continue (NULL);
157 safe_fclose (&fpfilterout); /* XXX - check result? */
160 /* update crypto information for this message */
161 cur->security &= ~(GOODSIGN|BADSIGN);
162 cur->security |= crypt_query (cur->content);
164 /* Remove color cache for this message, in case there
165 are color patterns for both ~g and ~V */
171 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
172 if (cur->security & GOODSIGN) {
173 if (!crypt_smime_verify_sender (cur))
174 mutt_message (_("S/MIME signature successfully verified."));
176 mutt_error (_("S/MIME certificate owner does not match sender."));
178 else if (cur->security & PARTSIGN)
180 ("Warning: Part of this message has not been signed."));
181 else if (cur->security & SIGN || cur->security & BADSIGN)
182 mutt_error (_("S/MIME signature could NOT be verified."));
185 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
186 if (cur->security & GOODSIGN)
187 mutt_message (_("PGP signature successfully verified."));
188 else if (cur->security & PARTSIGN)
190 ("Warning: Part of this message has not been signed."));
191 else if (cur->security & SIGN)
192 mutt_message (_("PGP signature could NOT be verified."));
195 /* Invoke the builtin pager */
199 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
205 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
206 if ((r = mutt_system (buf)) == -1)
207 mutt_error (_("Error running \"%s\"!"), buf);
209 keypad (stdscr, TRUE);
211 mutt_set_flag (Context, cur, M_READ, 1);
212 if (r != -1 && option (OPTPROMPTAFTER)) {
213 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
214 rc = km_dokey (MENU_PAGER);
223 void ci_bounce_message (HEADER * h, int *redraw)
225 char prompt[SHORT_STRING];
226 char buf[HUGE_STRING] = { 0 };
227 address_t *adr = NULL;
232 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
234 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
236 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
238 if (option (OPTNEEDREDRAW)) {
239 unset_option (OPTNEEDREDRAW);
240 *redraw = REDRAW_FULL;
246 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
247 mutt_error _("Error parsing address!");
252 adr = mutt_expand_aliases (adr);
254 if (mutt_addrlist_to_idna (adr, &err) < 0) {
255 mutt_error (_("Bad IDN: '%s'"), err);
257 address_list_wipe(&adr);
262 rfc822_write_address (buf, sizeof (buf), adr, 1);
264 #define extra_space (15 + 7 + 2)
265 snprintf (prompt, sizeof (prompt),
266 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
269 if (mutt_strwidth (prompt) > COLS - extra_space) {
270 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
271 prompt, sizeof(prompt), 0);
272 m_strcat(prompt, sizeof(prompt), "...?");
274 m_strcat(prompt, sizeof(prompt), "?");
277 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
278 address_list_wipe(&adr);
279 CLEARLINE (LINES - 1);
280 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
284 CLEARLINE (LINES - 1);
286 rc = mutt_bounce_message (NULL, h, adr);
287 address_list_wipe(&adr);
288 /* If no error, or background, display message. */
289 if ((rc == 0) || (rc == S_BKG))
290 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
293 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
296 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
297 *chflags |= CH_DECODE | CH_REORDER;
299 if (option (OPTWEED)) {
301 *cmflags |= M_CM_WEED;
306 *cmflags |= M_CM_PRINTING;
310 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
313 int chflags = CH_FROM;
315 pipe_set_flags (decode, print, &cmflags, &chflags);
317 if (decode && h->security & ENCRYPT) {
318 if (!crypt_valid_passphrase (h->security))
324 mutt_parse_mime_message (Context, h);
326 mutt_copy_message (fp, Context, h, cmflags, chflags);
330 /* the following code is shared between printing and piping */
332 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
333 int split, const char *sep)
340 /* mutt_endwin (NULL);
342 is this really needed here ?
343 it makes the screen flicker on pgp and s/mime messages,
344 before asking for a passphrase...
348 mutt_message_hook (Context, h, M_MESSAGEHOOK);
351 mutt_parse_mime_message (Context, h);
352 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
357 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
358 mutt_perror (_("Can't create filter process"));
363 pipe_msg (h, fpout, decode, print);
365 rc = mutt_wait_filter (thepid);
367 else { /* handle tagged messages */
370 for (i = 0; i < Context->vcount; i++)
371 if (Context->hdrs[Context->v2r[i]]->tagged) {
372 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
374 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
375 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
376 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
383 for (i = 0; i < Context->vcount; i++) {
384 if (Context->hdrs[Context->v2r[i]]->tagged) {
385 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
388 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
389 mutt_perror (_("Can't create filter process"));
393 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
394 /* add the message separator */
397 safe_fclose (&fpout);
398 if (mutt_wait_filter (thepid) != 0)
405 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
406 mutt_perror (_("Can't create filter process"));
410 for (i = 0; i < Context->vcount; i++) {
411 if (Context->hdrs[Context->v2r[i]]->tagged) {
412 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
414 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
415 /* add the message separator */
420 safe_fclose (&fpout);
421 if (mutt_wait_filter (thepid) != 0)
426 if (rc || option (OPTWAITKEY))
427 mutt_any_key_to_continue (NULL);
431 void mutt_pipe_message (HEADER * h)
433 char buffer[LONG_STRING];
436 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
440 mutt_expand_path (buffer, sizeof (buffer));
441 _mutt_pipe_message (h, buffer,
442 option (OPTPIPEDECODE),
443 0, option (OPTPIPESPLIT), PipeSep);
446 void mutt_print_message (HEADER * h)
449 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
450 mutt_message (_("No printing command has been defined."));
454 if (query_quadoption (OPT_PRINT,
455 h ? _("Print message?") : _("Print tagged messages?"))
459 if (_mutt_pipe_message (h, PrintCmd,
460 option (OPTPRINTDECODE),
461 1, option (OPTPRINTSPLIT), "\f") == 0)
462 mutt_message (h ? _("Message printed") : _("Messages printed"));
464 mutt_message (h ? _("Message could not be printed") :
465 _("Messages could not be printed"));
469 int mutt_select_sort (int reverse)
471 int method = Sort; /* save the current method in case of abort */
473 switch (mutt_multi_choice (reverse ?
475 ("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?: ")
478 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
480 case -1: /* abort - don't resort */
492 Sort = SORT_RECEIVED;
503 case 6: /* (t)hread */
507 case 7: /* (u)nsort */
515 case 9: /* s(c)ore */
519 case 10: /* s(p)am */
524 Sort |= SORT_REVERSE;
526 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
529 /* invoke a command in a subshell */
530 void mutt_shell_escape (void)
532 char buf[LONG_STRING];
535 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
536 if (!buf[0] && Shell)
537 m_strcpy(buf, sizeof(buf), Shell);
539 CLEARLINE (LINES - 1);
542 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
543 mutt_any_key_to_continue (NULL);
548 /* enter a mutt command */
549 void mutt_enter_command (void)
552 char buffer[LONG_STRING], errbuf[SHORT_STRING];
556 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
560 err.dsize = sizeof (errbuf);
562 r = mutt_parse_rc_line (buffer, &token, &err);
563 p_delete(&token.data);
565 /* since errbuf could potentially contain printf() sequences in it,
566 we must call mutt_error() in this fashion so that vsprintf()
567 doesn't expect more arguments that we passed */
569 mutt_message ("%s", errbuf);
571 mutt_error ("%s", errbuf);
575 void mutt_display_address (ENVELOPE * env)
577 const char *pfx = NULL;
578 char buf[SHORT_STRING];
579 address_t *adr = NULL;
581 adr = mutt_get_address(env, &pfx);
587 * Note: We don't convert IDNA to local representation this time.
588 * That is intentional, so the user has an opportunity to copy &
589 * paste the on-the-wire form of the address to other, IDN-unable
594 rfc822_write_address (buf, sizeof (buf), adr, 0);
595 mutt_message ("%s: %s", pfx, buf);
598 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
599 int *cmflags, int *chflags)
602 *chflags = CH_UPDATE_LEN;
604 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
605 if (mutt_is_multipart_encrypted (hdr->content)) {
606 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
607 *cmflags = M_CM_DECODE_PGP;
609 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
611 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
612 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
613 *cmflags = M_CM_DECODE_SMIME;
618 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
619 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
621 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
622 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
624 if (option (OPTWEED)) {
625 *chflags |= CH_WEED; /* and respect $weed. */
626 *cmflags |= M_CM_WEED;
632 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
634 int cmflags, chflags;
637 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
639 if (decode || decrypt)
640 mutt_parse_mime_message (Context, h);
642 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
646 mutt_set_flag (Context, h, M_DELETE, 1);
647 if (option (OPTDELETEUNTAG))
648 mutt_set_flag (Context, h, M_TAG, 0);
649 mutt_set_flag (Context, h, M_APPENDED, 1);
654 /* returns 0 if the copy/save was successful, or -1 on error/abort */
655 int mutt_save_message (HEADER * h, int delete,
656 int decode, int decrypt, int *redraw) {
657 int i, need_buffy_cleanup;
658 int need_passphrase = 0, app = 0;
659 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
672 snprintf (prompt, sizeof (prompt),
673 decode ? (delete ? _("Decode-save%s to mailbox") :
674 _("Decode-copy%s to mailbox")) :
675 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
676 _("Decrypt-copy%s to mailbox")) :
677 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
678 h ? "" : _(" tagged"));
682 need_passphrase = h->security & ENCRYPT;
684 mutt_message_hook (Context, h, M_MESSAGEHOOK);
685 mutt_default_save (buf, sizeof (buf), h);
688 /* look for the first tagged message */
690 for (i = 0; i < Context->vcount; i++) {
691 if (Context->hdrs[Context->v2r[i]]->tagged) {
692 h = Context->hdrs[Context->v2r[i]];
699 mutt_message_hook (Context, h, M_MESSAGEHOOK);
700 mutt_default_save (buf, sizeof (buf), h);
701 need_passphrase = h->security & ENCRYPT;
707 mutt_pretty_mailbox (buf);
708 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
711 if (*redraw != REDRAW_FULL) {
713 *redraw = REDRAW_INDEX | REDRAW_STATUS;
715 *redraw = REDRAW_STATUS;
721 /* This is an undocumented feature of ELM pointed out to me by Felix von
722 * Leitner <leitner@prz.fu-berlin.de>
724 if (m_strcmp(buf, ".") == 0)
725 m_strcpy(buf, sizeof(buf), LastSaveFolder);
727 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
729 mutt_expand_path (buf, sizeof (buf));
731 /* check to make sure that this file is really the one the user wants */
732 if (mutt_save_confirm (buf, &st) != 0)
735 if (need_passphrase && (decode || decrypt)
736 && !crypt_valid_passphrase (app))
739 mutt_message (_("Copying to %s..."), buf);
741 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
742 switch (imap_copy_messages (Context, h, buf, delete)) {
747 /* non-fatal error: fall through to fetch/append */
750 /* fatal error, abort */
756 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
758 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
759 mx_close_mailbox (&ctx, NULL);
763 for (i = 0; i < Context->vcount; i++) {
764 if (Context->hdrs[Context->v2r[i]]->tagged) {
765 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
767 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
768 decode, decrypt) != 0) {
769 mx_close_mailbox (&ctx, NULL);
776 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
778 mx_close_mailbox (&ctx, NULL);
780 if (need_buffy_cleanup) {
782 tmp = buffy_find_mailbox (buf);
783 if (tmp && tmp->new <= 0)
784 buffy_update_mailbox (tmp);
786 /* fix up the times so buffy won't get confused */
787 if (st.st_mtime > st.st_atime) {
788 ut.actime = st.st_atime;
789 ut.modtime = time (NULL);
804 void mutt_version (void)
806 mutt_message (mutt_make_version (1));
809 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
811 char buf[LONG_STRING];
812 char obuf[LONG_STRING];
815 char charset[STRING];
818 short charset_changed = 0;
819 short type_changed = 0;
821 cp = parameter_getval(b->parameter, "charset");
822 m_strcpy(charset, sizeof(charset), NONULL(cp));
824 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
825 m_strcpy(obuf, sizeof(obuf), buf);
830 for (p = b->parameter; p; p = p->next) {
833 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
834 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
838 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
842 /* clean up previous junk */
843 parameter_list_wipe(&b->parameter);
844 p_delete(&b->subtype);
846 mutt_parse_content_type (buf, b);
849 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
850 type_changed = ascii_strcasecmp (tmp, obuf);
852 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
854 /* if in send mode, check for conversion - current setting is default. */
856 if (!h && b->type == TYPETEXT && charset_changed) {
859 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
860 parameter_getval(b->parameter, "charset"));
861 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
862 b->noconv = (r == M_NO);
865 /* inform the user */
867 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
869 mutt_message (_("Content-Type changed to %s."), tmp);
870 if (b->type == TYPETEXT && charset_changed) {
873 mutt_message (_("Character set changed to %s; %s."),
874 parameter_getval(b->parameter, "charset"),
875 b->noconv ? _("not converting") : _("converting"));
878 b->force_charset |= charset_changed ? 1 : 0;
880 if (!is_multipart(b) && b->parts)
881 body_list_wipe(&b->parts);
882 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
883 b->hdr->content = NULL;
884 header_delete(&b->hdr);
887 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
888 mutt_parse_part (fp, b);
894 h->security |= crypt_query (b);
899 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
904 h->security |= PGP_TRADITIONAL_CHECKED;
906 mutt_parse_mime_message (Context, h);
907 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
909 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
910 h->security = crypt_query (h->content);
911 *redraw |= REDRAW_FULL;
915 h->security |= PGP_TRADITIONAL_CHECKED;
916 mx_close_message (&msg);
920 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
925 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
926 rv = _mutt_check_traditional_pgp (h, redraw);
928 for (i = 0; i < Context->vcount; i++)
929 if (Context->hdrs[Context->v2r[i]]->tagged &&
930 !(Context->hdrs[Context->v2r[i]]->
931 security & PGP_TRADITIONAL_CHECKED))
933 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)