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 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(MCore.tmpdir), 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 msg = mx_open_message (Context, cur->msgno);
79 if (msg == NULL) res = -1;
82 /* see if crytpo is needed for this message. if so, we should exit curses */
84 if (cur->security & ENCRYPT) {
85 cmflags |= M_CM_VERIFY;
87 else if (cur->security & SIGN) {
88 /* find out whether or not the verify signature */
89 if (query_quadoption2(mod_crypt.verify_sig, _("Verify PGP signature?")) ==
91 cmflags |= M_CM_VERIFY;
96 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
97 crypt_invoke_message(cur->security);
100 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
101 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
102 CH_DECODE | CH_FROM);
103 if (res == 0 && (ferror(fpout) || feof(fpout))) {
107 mx_close_message (&msg);
110 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
111 mutt_error (_("Could not copy message"));
112 if (fpfilterout != NULL) {
113 mutt_wait_filter (filterpid);
114 m_fclose(&fpfilterout);
118 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
119 mutt_any_key_to_continue (NULL);
121 m_fclose(&fpfilterout); /* XXX - check result? */
124 /* update crypto information for this message */
125 cur->security &= ~(GOODSIGN|BADSIGN);
126 cur->security |= crypt_query (cur->content);
128 /* Remove color cache for this message, in case there
129 are color patterns for both ~g and ~V */
132 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
133 if (cur->security & GOODSIGN) {
134 if (!crypt_smime_verify_sender (cur))
135 mutt_message (_("S/MIME signature successfully verified."));
137 mutt_error (_("S/MIME certificate owner does not match sender."));
139 else if (cur->security & PARTSIGN)
141 ("Warning: Part of this message has not been signed."));
142 else if (cur->security & SIGN || cur->security & BADSIGN)
143 mutt_error (_("S/MIME signature could NOT be verified."));
146 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
147 if (cur->security & GOODSIGN)
148 mutt_message (_("PGP signature successfully verified."));
149 else if (cur->security & PARTSIGN)
151 ("Warning: Part of this message has not been signed."));
152 else if (cur->security & SIGN)
153 mutt_message (_("PGP signature could NOT be verified."));
162 return mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
166 void ci_bounce_message (HEADER * h, int *redraw)
169 char buf[HUGE_STRING] = { 0 };
170 address_t *adr = NULL;
175 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
177 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
179 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
181 if (option (OPTNEEDREDRAW)) {
182 unset_option (OPTNEEDREDRAW);
183 *redraw = REDRAW_FULL;
189 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
190 mutt_error _("Error parsing address!");
195 adr = mutt_expand_aliases (adr);
197 if (mutt_addrlist_to_idna (adr, &err) < 0) {
198 mutt_error (_("Bad IDN: '%s'"), err);
200 address_list_wipe(&adr);
205 rfc822_addrcat(buf, sizeof (buf), adr, 1);
207 snprintf (prompt, sizeof (prompt),
208 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
211 #define extra_space (15 + 7 + 2)
212 if (m_strwidth(prompt) > COLS - extra_space) {
213 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
214 prompt, sizeof(prompt), 0);
215 m_strcat(prompt, sizeof(prompt), "...?");
217 m_strcat(prompt, sizeof(prompt), "?");
221 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
222 address_list_wipe(&adr);
223 CLEARLINE (LINES - 1);
224 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
228 CLEARLINE (LINES - 1);
230 rc = mutt_bounce_message (NULL, h, adr);
231 address_list_wipe(&adr);
232 /* If no error, or background, display message. */
233 if ((rc == 0) || (rc == S_BKG))
234 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
237 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
240 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
241 *chflags |= CH_DECODE | CH_REORDER;
243 if (option (OPTWEED)) {
245 *cmflags |= M_CM_WEED;
250 *cmflags |= M_CM_PRINTING;
254 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
257 int chflags = CH_FROM;
259 pipe_set_flags (decode, print, &cmflags, &chflags);
261 if (decode && h->security & ENCRYPT) {
266 mutt_parse_mime_message (Context, h);
268 mutt_copy_message (fp, Context, h, cmflags, chflags);
272 /* the following code is shared between printing and piping */
274 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
275 int split, const char *sep)
282 /* mutt_endwin (NULL);
284 is this really needed here ?
285 it makes the screen flicker on pgp and s/mime messages,
286 before asking for a passphrase...
290 mutt_message_hook (Context, h, M_MESSAGEHOOK);
293 mutt_parse_mime_message (Context, h);
297 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
298 mutt_perror (_("Can't create filter process"));
303 pipe_msg (h, fpout, decode, print);
305 rc = mutt_wait_filter (thepid);
307 else { /* handle tagged messages */
310 for (i = 0; i < Context->vcount; i++)
311 if (Context->hdrs[Context->v2r[i]]->tagged) {
312 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
314 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
319 for (i = 0; i < Context->vcount; i++) {
320 if (Context->hdrs[Context->v2r[i]]->tagged) {
321 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
324 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
325 mutt_perror (_("Can't create filter process"));
329 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
330 /* add the message separator */
334 if (mutt_wait_filter (thepid) != 0)
341 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
342 mutt_perror (_("Can't create filter process"));
346 for (i = 0; i < Context->vcount; i++) {
347 if (Context->hdrs[Context->v2r[i]]->tagged) {
348 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
350 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
351 /* add the message separator */
357 if (mutt_wait_filter (thepid) != 0)
362 if (rc || option (OPTWAITKEY))
363 mutt_any_key_to_continue (NULL);
367 void mutt_pipe_message (HEADER * h)
369 char buffer[LONG_STRING];
372 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
376 mutt_expand_path (buffer, sizeof (buffer));
377 _mutt_pipe_message (h, buffer,
378 option (OPTPIPEDECODE),
379 0, option (OPTPIPESPLIT), PipeSep);
382 void mutt_print_message (HEADER * h)
385 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
386 mutt_message (_("No printing command has been defined."));
390 if (query_quadoption (OPT_PRINT,
391 h ? _("Print message?") : _("Print tagged messages?"))
395 if (_mutt_pipe_message (h, PrintCmd,
396 option (OPTPRINTDECODE),
397 1, option (OPTPRINTSPLIT), "\f") == 0)
398 mutt_message (h ? _("Message printed") : _("Messages printed"));
400 mutt_message (h ? _("Message could not be printed") :
401 _("Messages could not be printed"));
405 int mutt_select_sort (int reverse)
407 int method = Sort; /* save the current method in case of abort */
409 switch (mutt_multi_choice (reverse ?
411 ("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?: ")
414 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
416 case -1: /* abort - don't resort */
428 Sort = SORT_RECEIVED;
439 case 6: /* (t)hread */
443 case 7: /* (u)nsort */
451 case 9: /* s(c)ore */
455 case 10: /* s(p)am */
460 Sort |= SORT_REVERSE;
462 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
465 /* invoke a command in a subshell */
466 void mutt_shell_escape (void)
468 char buf[LONG_STRING];
471 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
473 m_strcpy(buf, sizeof(buf), MCore.shell);
475 CLEARLINE (LINES - 1);
478 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
479 mutt_any_key_to_continue (NULL);
484 /* enter a mutt command */
485 void mutt_enter_command (void)
488 char buffer[LONG_STRING], errbuf[STRING];
492 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
496 err.dsize = sizeof (errbuf);
498 r = mutt_parse_rc_line (buffer, &token, &err);
499 p_delete(&token.data);
501 /* since errbuf could potentially contain printf() sequences in it,
502 we must call mutt_error() in this fashion so that vsprintf()
503 doesn't expect more arguments that we passed */
505 mutt_message ("%s", errbuf);
507 mutt_error ("%s", errbuf);
511 void mutt_display_address (ENVELOPE * env)
513 const char *pfx = NULL;
515 address_t *adr = NULL;
517 adr = mutt_get_address(env, &pfx);
523 * Note: We don't convert IDNA to local representation this time.
524 * That is intentional, so the user has an opportunity to copy &
525 * paste the on-the-wire form of the address to other, IDN-unable
530 rfc822_addrcat(buf, sizeof (buf), adr, 0);
531 mutt_message ("%s: %s", pfx, buf);
534 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
535 int *cmflags, int *chflags)
538 *chflags = CH_UPDATE_LEN;
540 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
541 if (mutt_is_multipart_encrypted (hdr->content)) {
542 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
543 *cmflags = M_CM_DECODE_PGP;
545 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
547 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
548 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
549 *cmflags = M_CM_DECODE_SMIME;
554 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
555 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
557 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
558 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
560 if (option (OPTWEED)) {
561 *chflags |= CH_WEED; /* and respect $weed. */
562 *cmflags |= M_CM_WEED;
568 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
570 int cmflags, chflags;
573 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
575 if (decode || decrypt)
576 mutt_parse_mime_message (Context, h);
578 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
582 mutt_set_flag (Context, h, M_DELETE, 1);
583 if (option (OPTDELETEUNTAG))
584 mutt_set_flag (Context, h, M_TAG, 0);
585 mutt_set_flag (Context, h, M_APPENDED, 1);
590 /* returns 0 if the copy/save was successful, or -1 on error/abort */
591 int mutt_save_message (HEADER * h, int delete,
592 int decode, int decrypt, int *redraw) {
593 int i, need_buffy_cleanup;
594 int need_passphrase = 0, app = 0;
595 char prompt[STRING], buf[_POSIX_PATH_MAX];
603 snprintf (prompt, sizeof (prompt),
604 decode ? (delete ? _("Decode-save%s to mailbox") :
605 _("Decode-copy%s to mailbox")) :
606 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
607 _("Decrypt-copy%s to mailbox")) :
608 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
609 h ? "" : _(" tagged"));
613 need_passphrase = h->security & ENCRYPT;
615 mutt_message_hook (Context, h, M_MESSAGEHOOK);
616 mutt_default_save (buf, sizeof (buf), h);
619 /* look for the first tagged message */
621 for (i = 0; i < Context->vcount; i++) {
622 if (Context->hdrs[Context->v2r[i]]->tagged) {
623 h = Context->hdrs[Context->v2r[i]];
630 mutt_message_hook (Context, h, M_MESSAGEHOOK);
631 mutt_default_save (buf, sizeof (buf), h);
632 need_passphrase = h->security & ENCRYPT;
638 mutt_pretty_mailbox (buf);
639 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
642 if (*redraw != REDRAW_FULL) {
644 *redraw = REDRAW_INDEX | REDRAW_STATUS;
646 *redraw = REDRAW_STATUS;
652 /* This is an undocumented feature of ELM pointed out to me by Felix von
653 * Leitner <leitner@prz.fu-berlin.de>
655 if (m_strcmp(buf, ".") == 0)
656 m_strcpy(buf, sizeof(buf), LastSaveFolder);
658 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
660 mutt_expand_path (buf, sizeof (buf));
662 /* check to make sure that this file is really the one the user wants */
663 if (mutt_save_confirm (buf, &st) != 0)
666 mutt_message (_("Copying to %s..."), buf);
668 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
669 switch (imap_copy_messages (Context, h, buf, delete)) {
674 /* non-fatal error: fall through to fetch/append */
677 /* fatal error, abort */
683 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
685 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
686 mx_close_mailbox (&ctx, NULL);
690 for (i = 0; i < Context->vcount; i++) {
691 if (Context->hdrs[Context->v2r[i]]->tagged) {
692 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
694 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
695 decode, decrypt) != 0) {
696 mx_close_mailbox (&ctx, NULL);
703 need_buffy_cleanup = (ctx.magic == M_MBOX);
705 mx_close_mailbox (&ctx, NULL);
707 if (need_buffy_cleanup) {
708 /* fix up the times so buffy won't get confused */
709 if (st.st_mtime > st.st_atime) {
710 ut.actime = st.st_atime;
711 ut.modtime = time (NULL);
725 void mutt_version (void)
727 mutt_message (mutt_make_version());
730 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
732 char buf[LONG_STRING];
733 char obuf[LONG_STRING];
736 char charset[STRING];
739 short charset_changed = 0;
740 short type_changed = 0;
742 cp = parameter_getval(b->parameter, "charset");
743 m_strcpy(charset, sizeof(charset), NONULL(cp));
745 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
746 m_strcpy(obuf, sizeof(obuf), buf);
751 for (p = b->parameter; p; p = p->next) {
754 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
755 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
759 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
763 /* clean up previous junk */
764 parameter_list_wipe(&b->parameter);
765 p_delete(&b->subtype);
767 mutt_parse_content_type (buf, b);
770 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
771 type_changed = ascii_strcasecmp (tmp, obuf);
773 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
775 /* if in send mode, check for conversion - current setting is default. */
777 if (!h && b->type == TYPETEXT && charset_changed) {
780 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
781 parameter_getval(b->parameter, "charset"));
782 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
783 b->noconv = (r == M_NO);
786 /* inform the user */
788 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
790 mutt_message (_("Content-Type changed to %s."), tmp);
791 if (b->type == TYPETEXT && charset_changed) {
794 mutt_message (_("Character set changed to %s; %s."),
795 parameter_getval(b->parameter, "charset"),
796 b->noconv ? _("not converting") : _("converting"));
799 b->force_charset |= charset_changed ? 1 : 0;
801 if (!is_multipart(b) && b->parts)
802 body_list_wipe(&b->parts);
803 if (!mutt_is_message_type(b) && b->hdr) {
804 b->hdr->content = NULL;
805 header_delete(&b->hdr);
808 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
809 mutt_parse_part (fp, b);
815 h->security |= crypt_query (b);
820 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
825 h->security |= PGP_TRADITIONAL_CHECKED;
827 mutt_parse_mime_message (Context, h);
828 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
830 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
831 h->security = crypt_query (h->content);
832 *redraw |= REDRAW_FULL;
836 h->security |= PGP_TRADITIONAL_CHECKED;
837 mx_close_message (&msg);
841 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
846 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
847 rv = _mutt_check_traditional_pgp (h, redraw);
849 for (i = 0; i < Context->vcount; i++)
850 if (Context->hdrs[Context->v2r[i]]->tagged &&
851 !(Context->hdrs[Context->v2r[i]]->
852 security & PGP_TRADITIONAL_CHECKED))
854 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)