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_quadoption (OPT_VERIFYSIG, _("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 #define extra_space (15 + 7 + 2)
208 snprintf (prompt, sizeof (prompt),
209 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
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), "?");
220 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
221 address_list_wipe(&adr);
222 CLEARLINE (LINES - 1);
223 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
227 CLEARLINE (LINES - 1);
229 rc = mutt_bounce_message (NULL, h, adr);
230 address_list_wipe(&adr);
231 /* If no error, or background, display message. */
232 if ((rc == 0) || (rc == S_BKG))
233 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
236 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
239 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
240 *chflags |= CH_DECODE | CH_REORDER;
242 if (option (OPTWEED)) {
244 *cmflags |= M_CM_WEED;
249 *cmflags |= M_CM_PRINTING;
253 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
256 int chflags = CH_FROM;
258 pipe_set_flags (decode, print, &cmflags, &chflags);
260 if (decode && h->security & ENCRYPT) {
265 mutt_parse_mime_message (Context, h);
267 mutt_copy_message (fp, Context, h, cmflags, chflags);
271 /* the following code is shared between printing and piping */
273 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
274 int split, const char *sep)
281 /* mutt_endwin (NULL);
283 is this really needed here ?
284 it makes the screen flicker on pgp and s/mime messages,
285 before asking for a passphrase...
289 mutt_message_hook (Context, h, M_MESSAGEHOOK);
292 mutt_parse_mime_message (Context, h);
296 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
297 mutt_perror (_("Can't create filter process"));
302 pipe_msg (h, fpout, decode, print);
304 rc = mutt_wait_filter (thepid);
306 else { /* handle tagged messages */
309 for (i = 0; i < Context->vcount; i++)
310 if (Context->hdrs[Context->v2r[i]]->tagged) {
311 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
313 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
318 for (i = 0; i < Context->vcount; i++) {
319 if (Context->hdrs[Context->v2r[i]]->tagged) {
320 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
323 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
324 mutt_perror (_("Can't create filter process"));
328 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
329 /* add the message separator */
333 if (mutt_wait_filter (thepid) != 0)
340 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
341 mutt_perror (_("Can't create filter process"));
345 for (i = 0; i < Context->vcount; i++) {
346 if (Context->hdrs[Context->v2r[i]]->tagged) {
347 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
349 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
350 /* add the message separator */
356 if (mutt_wait_filter (thepid) != 0)
361 if (rc || option (OPTWAITKEY))
362 mutt_any_key_to_continue (NULL);
366 void mutt_pipe_message (HEADER * h)
368 char buffer[LONG_STRING];
371 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
375 mutt_expand_path (buffer, sizeof (buffer));
376 _mutt_pipe_message (h, buffer,
377 option (OPTPIPEDECODE),
378 0, option (OPTPIPESPLIT), PipeSep);
381 void mutt_print_message (HEADER * h)
384 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
385 mutt_message (_("No printing command has been defined."));
389 if (query_quadoption (OPT_PRINT,
390 h ? _("Print message?") : _("Print tagged messages?"))
394 if (_mutt_pipe_message (h, PrintCmd,
395 option (OPTPRINTDECODE),
396 1, option (OPTPRINTSPLIT), "\f") == 0)
397 mutt_message (h ? _("Message printed") : _("Messages printed"));
399 mutt_message (h ? _("Message could not be printed") :
400 _("Messages could not be printed"));
404 int mutt_select_sort (int reverse)
406 int method = Sort; /* save the current method in case of abort */
408 switch (mutt_multi_choice (reverse ?
410 ("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?: ")
413 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
415 case -1: /* abort - don't resort */
427 Sort = SORT_RECEIVED;
438 case 6: /* (t)hread */
442 case 7: /* (u)nsort */
450 case 9: /* s(c)ore */
454 case 10: /* s(p)am */
459 Sort |= SORT_REVERSE;
461 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
464 /* invoke a command in a subshell */
465 void mutt_shell_escape (void)
467 char buf[LONG_STRING];
470 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
472 m_strcpy(buf, sizeof(buf), MCore.shell);
474 CLEARLINE (LINES - 1);
477 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
478 mutt_any_key_to_continue (NULL);
483 /* enter a mutt command */
484 void mutt_enter_command (void)
487 char buffer[LONG_STRING], errbuf[STRING];
491 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
495 err.dsize = sizeof (errbuf);
497 r = mutt_parse_rc_line (buffer, &token, &err);
498 p_delete(&token.data);
500 /* since errbuf could potentially contain printf() sequences in it,
501 we must call mutt_error() in this fashion so that vsprintf()
502 doesn't expect more arguments that we passed */
504 mutt_message ("%s", errbuf);
506 mutt_error ("%s", errbuf);
510 void mutt_display_address (ENVELOPE * env)
512 const char *pfx = NULL;
514 address_t *adr = NULL;
516 adr = mutt_get_address(env, &pfx);
522 * Note: We don't convert IDNA to local representation this time.
523 * That is intentional, so the user has an opportunity to copy &
524 * paste the on-the-wire form of the address to other, IDN-unable
529 rfc822_addrcat(buf, sizeof (buf), adr, 0);
530 mutt_message ("%s: %s", pfx, buf);
533 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
534 int *cmflags, int *chflags)
537 *chflags = CH_UPDATE_LEN;
539 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
540 if (mutt_is_multipart_encrypted (hdr->content)) {
541 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
542 *cmflags = M_CM_DECODE_PGP;
544 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
546 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
547 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
548 *cmflags = M_CM_DECODE_SMIME;
553 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
554 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
556 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
557 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
559 if (option (OPTWEED)) {
560 *chflags |= CH_WEED; /* and respect $weed. */
561 *cmflags |= M_CM_WEED;
567 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
569 int cmflags, chflags;
572 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
574 if (decode || decrypt)
575 mutt_parse_mime_message (Context, h);
577 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
581 mutt_set_flag (Context, h, M_DELETE, 1);
582 if (option (OPTDELETEUNTAG))
583 mutt_set_flag (Context, h, M_TAG, 0);
584 mutt_set_flag (Context, h, M_APPENDED, 1);
589 /* returns 0 if the copy/save was successful, or -1 on error/abort */
590 int mutt_save_message (HEADER * h, int delete,
591 int decode, int decrypt, int *redraw) {
592 int i, need_buffy_cleanup;
593 int need_passphrase = 0, app = 0;
594 char prompt[STRING], buf[_POSIX_PATH_MAX];
602 snprintf (prompt, sizeof (prompt),
603 decode ? (delete ? _("Decode-save%s to mailbox") :
604 _("Decode-copy%s to mailbox")) :
605 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
606 _("Decrypt-copy%s to mailbox")) :
607 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
608 h ? "" : _(" tagged"));
612 need_passphrase = h->security & ENCRYPT;
614 mutt_message_hook (Context, h, M_MESSAGEHOOK);
615 mutt_default_save (buf, sizeof (buf), h);
618 /* look for the first tagged message */
620 for (i = 0; i < Context->vcount; i++) {
621 if (Context->hdrs[Context->v2r[i]]->tagged) {
622 h = Context->hdrs[Context->v2r[i]];
629 mutt_message_hook (Context, h, M_MESSAGEHOOK);
630 mutt_default_save (buf, sizeof (buf), h);
631 need_passphrase = h->security & ENCRYPT;
637 mutt_pretty_mailbox (buf);
638 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
641 if (*redraw != REDRAW_FULL) {
643 *redraw = REDRAW_INDEX | REDRAW_STATUS;
645 *redraw = REDRAW_STATUS;
651 /* This is an undocumented feature of ELM pointed out to me by Felix von
652 * Leitner <leitner@prz.fu-berlin.de>
654 if (m_strcmp(buf, ".") == 0)
655 m_strcpy(buf, sizeof(buf), LastSaveFolder);
657 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
659 mutt_expand_path (buf, sizeof (buf));
661 /* check to make sure that this file is really the one the user wants */
662 if (mutt_save_confirm (buf, &st) != 0)
665 mutt_message (_("Copying to %s..."), buf);
667 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
668 switch (imap_copy_messages (Context, h, buf, delete)) {
673 /* non-fatal error: fall through to fetch/append */
676 /* fatal error, abort */
682 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
684 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
685 mx_close_mailbox (&ctx, NULL);
689 for (i = 0; i < Context->vcount; i++) {
690 if (Context->hdrs[Context->v2r[i]]->tagged) {
691 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
693 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
694 decode, decrypt) != 0) {
695 mx_close_mailbox (&ctx, NULL);
702 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
704 mx_close_mailbox (&ctx, NULL);
706 if (need_buffy_cleanup) {
707 /* fix up the times so buffy won't get confused */
708 if (st.st_mtime > st.st_atime) {
709 ut.actime = st.st_atime;
710 ut.modtime = time (NULL);
724 void mutt_version (void)
726 mutt_message (mutt_make_version());
729 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
731 char buf[LONG_STRING];
732 char obuf[LONG_STRING];
735 char charset[STRING];
738 short charset_changed = 0;
739 short type_changed = 0;
741 cp = parameter_getval(b->parameter, "charset");
742 m_strcpy(charset, sizeof(charset), NONULL(cp));
744 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
745 m_strcpy(obuf, sizeof(obuf), buf);
750 for (p = b->parameter; p; p = p->next) {
753 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
754 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
758 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
762 /* clean up previous junk */
763 parameter_list_wipe(&b->parameter);
764 p_delete(&b->subtype);
766 mutt_parse_content_type (buf, b);
769 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
770 type_changed = ascii_strcasecmp (tmp, obuf);
772 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
774 /* if in send mode, check for conversion - current setting is default. */
776 if (!h && b->type == TYPETEXT && charset_changed) {
779 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
780 parameter_getval(b->parameter, "charset"));
781 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
782 b->noconv = (r == M_NO);
785 /* inform the user */
787 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
789 mutt_message (_("Content-Type changed to %s."), tmp);
790 if (b->type == TYPETEXT && charset_changed) {
793 mutt_message (_("Character set changed to %s; %s."),
794 parameter_getval(b->parameter, "charset"),
795 b->noconv ? _("not converting") : _("converting"));
798 b->force_charset |= charset_changed ? 1 : 0;
800 if (!is_multipart(b) && b->parts)
801 body_list_wipe(&b->parts);
802 if (!mutt_is_message_type(b) && b->hdr) {
803 b->hdr->content = NULL;
804 header_delete(&b->hdr);
807 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
808 mutt_parse_part (fp, b);
814 h->security |= crypt_query (b);
819 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
824 h->security |= PGP_TRADITIONAL_CHECKED;
826 mutt_parse_mime_message (Context, h);
827 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
829 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
830 h->security = crypt_query (h->content);
831 *redraw |= REDRAW_FULL;
835 h->security |= PGP_TRADITIONAL_CHECKED;
836 mx_close_message (&msg);
840 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
845 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
846 rv = _mutt_check_traditional_pgp (h, redraw);
848 for (i = 0; i < Context->vcount; i++)
849 if (Context->hdrs[Context->v2r[i]]->tagged &&
850 !(Context->hdrs[Context->v2r[i]]->
851 security & PGP_TRADITIONAL_CHECKED))
853 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)