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/lib-ui.h>
21 #include <lib-ui/menu.h>
22 #include <lib-mx/mx.h>
26 #include "recvattach.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 cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
46 FILE *fpfilterout = NULL;
51 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
52 cur->content->subtype);
54 mutt_parse_mime_message (Context, cur);
55 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
57 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
59 mutt_error _("Could not create temporary file!");
63 if (DisplayFilter && *DisplayFilter) {
66 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
67 -1, fileno (fpfilterout), -1);
69 mutt_error (_("Cannot create display filter"));
70 m_fclose(&fpfilterout);
76 msg = mx_open_message (Context, cur->msgno);
77 if (msg == NULL) res = -1;
80 /* see if crytpo is needed for this message. if so, we should exit curses */
82 if (cur->security & ENCRYPT) {
83 cmflags |= M_CM_VERIFY;
85 else if (cur->security & SIGN) {
86 /* find out whether or not the verify signature */
87 if (query_quadoption2(mod_crypt.verify_sig, _("Verify PGP signature?")) ==
89 cmflags |= M_CM_VERIFY;
94 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
95 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
97 if (res == 0 && (ferror(fpout) || feof(fpout))) {
101 mx_close_message (&msg);
104 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
105 mutt_error (_("Could not copy message"));
106 if (fpfilterout != NULL) {
107 mutt_wait_filter (filterpid);
108 m_fclose(&fpfilterout);
112 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
113 mutt_any_key_to_continue (NULL);
115 m_fclose(&fpfilterout); /* XXX - check result? */
118 /* update crypto information for this message */
119 cur->security &= ~(GOODSIGN|BADSIGN);
120 cur->security |= crypt_query (cur->content);
122 /* Remove color cache for this message, in case there
123 are color patterns for both ~g and ~V */
126 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
127 if (cur->security & GOODSIGN) {
128 if (!crypt_smime_verify_sender (cur))
129 mutt_message (_("S/MIME signature successfully verified."));
131 mutt_error (_("S/MIME certificate owner does not match sender."));
133 else if (cur->security & PARTSIGN)
135 ("Warning: Part of this message has not been signed."));
136 else if (cur->security & SIGN || cur->security & BADSIGN)
137 mutt_error (_("S/MIME signature could NOT be verified."));
140 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
141 if (cur->security & GOODSIGN)
142 mutt_message (_("PGP signature successfully verified."));
143 else if (cur->security & PARTSIGN)
145 ("Warning: Part of this message has not been signed."));
146 else if (cur->security & SIGN)
147 mutt_message (_("PGP signature could NOT be verified."));
156 return mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
160 void ci_bounce_message (HEADER * h, int *redraw)
163 char buf[HUGE_STRING] = { 0 };
164 address_t *adr = NULL;
169 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
171 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
173 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
175 if (option (OPTNEEDREDRAW)) {
176 unset_option (OPTNEEDREDRAW);
177 *redraw = REDRAW_FULL;
183 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
184 mutt_error _("Error parsing address!");
189 adr = mutt_expand_aliases (adr);
191 if (mutt_addrlist_to_idna (adr, &err) < 0) {
192 mutt_error (_("Bad IDN: '%s'"), err);
194 address_list_wipe(&adr);
199 rfc822_addrcat(buf, sizeof (buf), adr, 1);
201 snprintf (prompt, sizeof (prompt),
202 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
205 #define extra_space (15 + 7 + 2)
206 if (m_strwidth(prompt) > getmaxx(main_w) - extra_space) {
207 mutt_format_string(prompt, sizeof(prompt), 0,
208 getmaxx(main_w) - extra_space, 0, 0, prompt,
210 m_strcat(prompt, sizeof(prompt), "...?");
212 m_strcat(prompt, sizeof(prompt), "?");
216 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
217 address_list_wipe(&adr);
218 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
222 rc = mutt_bounce_message (NULL, h, adr);
223 address_list_wipe(&adr);
224 /* If no error, or background, display message. */
225 if ((rc == 0) || (rc == S_BKG))
226 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
229 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
232 int chflags = CH_FROM;
235 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
236 chflags |= CH_DECODE | CH_REORDER;
238 if (option(OPTWEED)) {
240 cmflags |= M_CM_WEED;
245 cmflags |= M_CM_PRINTING;
247 if (decode && h->security & ENCRYPT) {
252 mutt_parse_mime_message (Context, h);
254 mutt_copy_message (fp, Context, h, cmflags, chflags);
257 /* the following code is shared between printing and piping */
258 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
259 int split, const char *sep)
267 mutt_message_hook (Context, h, M_MESSAGEHOOK);
270 mutt_parse_mime_message (Context, h);
274 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
275 mutt_perror (_("Can't create filter process"));
280 pipe_msg (h, fpout, decode, print);
282 rc = mutt_wait_filter (thepid);
283 } else { /* handle tagged messages */
285 for (i = 0; i < Context->vcount; i++)
286 if (Context->hdrs[Context->v2r[i]]->tagged) {
287 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
289 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
294 for (i = 0; i < Context->vcount; i++) {
295 if (Context->hdrs[Context->v2r[i]]->tagged) {
296 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
299 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
300 mutt_perror (_("Can't create filter process"));
304 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
305 /* add the message separator */
309 if (mutt_wait_filter (thepid) != 0)
315 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
316 mutt_perror (_("Can't create filter process"));
320 for (i = 0; i < Context->vcount; i++) {
321 if (Context->hdrs[Context->v2r[i]]->tagged) {
322 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
324 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
325 /* add the message separator */
331 if (mutt_wait_filter (thepid) != 0)
336 if (rc || option (OPTWAITKEY))
337 mutt_any_key_to_continue (NULL);
341 void mutt_pipe_message (HEADER * h)
343 char buffer[LONG_STRING];
346 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
350 mutt_expand_path (buffer, sizeof (buffer));
351 _mutt_pipe_message (h, buffer,
352 option (OPTPIPEDECODE),
353 0, option (OPTPIPESPLIT), PipeSep);
356 void mutt_print_message (HEADER * h)
359 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
360 mutt_message (_("No printing command has been defined."));
364 if (query_quadoption (OPT_PRINT,
365 h ? _("Print message?") : _("Print tagged messages?"))
369 if (_mutt_pipe_message (h, PrintCmd,
370 option (OPTPRINTDECODE),
371 1, option (OPTPRINTSPLIT), "\f") == 0)
372 mutt_message (h ? _("Message printed") : _("Messages printed"));
374 mutt_message (h ? _("Message could not be printed") :
375 _("Messages could not be printed"));
379 int mutt_select_sort (int reverse)
381 int method = Sort; /* save the current method in case of abort */
383 switch (mutt_multi_choice (reverse ?
385 ("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?: ")
388 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
390 case -1: /* abort - don't resort */
402 Sort = SORT_RECEIVED;
413 case 6: /* (t)hread */
417 case 7: /* (u)nsort */
425 case 9: /* s(c)ore */
429 case 10: /* s(p)am */
434 Sort |= SORT_REVERSE;
436 return Sort != method ? 0 : -1; /* no need to resort if it's the same */
439 /* invoke a command in a subshell */
440 void mutt_shell_escape (void)
442 char buf[LONG_STRING];
445 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
447 m_strcpy(buf, sizeof(buf), mod_core.shell);
451 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
452 mutt_any_key_to_continue (NULL);
457 /* enter a mutt command */
458 void mutt_enter_command (void)
461 char buffer[LONG_STRING], errbuf[STRING];
465 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
469 err.dsize = sizeof (errbuf);
471 r = mutt_parse_rc_line (buffer, &token, &err);
472 p_delete(&token.data);
474 /* since errbuf could potentially contain printf() sequences in it,
475 we must call mutt_error() in this fashion so that vsprintf()
476 doesn't expect more arguments that we passed */
478 mutt_message ("%s", errbuf);
480 mutt_error ("%s", errbuf);
484 void mutt_display_address (ENVELOPE * env)
486 const char *pfx = NULL;
488 address_t *adr = NULL;
490 adr = mutt_get_address(env, &pfx);
496 * Note: We don't convert IDNA to local representation this time.
497 * That is intentional, so the user has an opportunity to copy &
498 * paste the on-the-wire form of the address to other, IDN-unable
503 rfc822_addrcat(buf, sizeof (buf), adr, 0);
504 mutt_message ("%s: %s", pfx, buf);
507 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
508 int *cmflags, int *chflags)
511 *chflags = CH_UPDATE_LEN;
513 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
514 if (mutt_is_multipart_encrypted (hdr->content)) {
515 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
516 *cmflags = M_CM_DECODE_PGP;
518 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
520 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
521 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
522 *cmflags = M_CM_DECODE_SMIME;
527 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
528 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
530 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
531 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
533 if (option (OPTWEED)) {
534 *chflags |= CH_WEED; /* and respect $weed. */
535 *cmflags |= M_CM_WEED;
541 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
543 int cmflags, chflags;
546 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
548 if (decode || decrypt)
549 mutt_parse_mime_message (Context, h);
551 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
555 mutt_set_flag (Context, h, M_DELETE, 1);
556 if (option (OPTDELETEUNTAG))
557 mutt_set_flag (Context, h, M_TAG, 0);
558 mutt_set_flag (Context, h, M_APPENDED, 1);
563 /* returns 0 if the copy/save was successful, or -1 on error/abort */
564 int mutt_save_message (HEADER * h, int delete,
565 int decode, int decrypt, int *redraw) {
566 int i, need_buffy_cleanup;
567 int need_passphrase = 0, app = 0;
568 char prompt[STRING], buf[_POSIX_PATH_MAX];
576 snprintf (prompt, sizeof (prompt),
577 decode ? (delete ? _("Decode-save%s to mailbox") :
578 _("Decode-copy%s to mailbox")) :
579 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
580 _("Decrypt-copy%s to mailbox")) :
581 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
582 h ? "" : _(" tagged"));
586 need_passphrase = h->security & ENCRYPT;
588 mutt_message_hook (Context, h, M_MESSAGEHOOK);
589 mutt_default_save (buf, sizeof (buf), h);
592 /* look for the first tagged message */
594 for (i = 0; i < Context->vcount; i++) {
595 if (Context->hdrs[Context->v2r[i]]->tagged) {
596 h = Context->hdrs[Context->v2r[i]];
603 mutt_message_hook (Context, h, M_MESSAGEHOOK);
604 mutt_default_save (buf, sizeof (buf), h);
605 need_passphrase = h->security & ENCRYPT;
611 mutt_pretty_mailbox (buf);
612 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
615 if (*redraw != REDRAW_FULL) {
617 *redraw = REDRAW_INDEX | REDRAW_STATUS;
619 *redraw = REDRAW_STATUS;
625 /* This is an undocumented feature of ELM pointed out to me by Felix von
626 * Leitner <leitner@prz.fu-berlin.de>
628 if (m_strcmp(buf, ".") == 0)
629 m_strcpy(buf, sizeof(buf), LastSaveFolder);
631 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
633 mutt_expand_path (buf, sizeof (buf));
635 /* check to make sure that this file is really the one the user wants */
636 if (mutt_save_confirm (buf, &st) != 0)
639 mutt_message (_("Copying to %s..."), buf);
641 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
642 switch (imap_copy_messages (Context, h, buf, delete)) {
647 /* non-fatal error: fall through to fetch/append */
650 /* fatal error, abort */
656 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
658 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
659 mx_close_mailbox (&ctx, NULL);
663 for (i = 0; i < Context->vcount; i++) {
664 if (Context->hdrs[Context->v2r[i]]->tagged) {
665 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
667 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
668 decode, decrypt) != 0) {
669 mx_close_mailbox (&ctx, NULL);
676 need_buffy_cleanup = (ctx.magic == M_MBOX);
678 mx_close_mailbox (&ctx, NULL);
680 if (need_buffy_cleanup) {
681 /* fix up the times so buffy won't get confused */
682 if (st.st_mtime > st.st_atime) {
683 ut.actime = st.st_atime;
684 ut.modtime = time (NULL);
698 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
700 char buf[LONG_STRING];
701 char obuf[LONG_STRING];
704 char charset[STRING];
707 short charset_changed = 0;
708 short type_changed = 0;
710 cp = parameter_getval(b->parameter, "charset");
711 m_strcpy(charset, sizeof(charset), NONULL(cp));
713 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
714 m_strcpy(obuf, sizeof(obuf), buf);
719 for (p = b->parameter; p; p = p->next) {
722 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
723 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
727 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
731 /* clean up previous junk */
732 parameter_list_wipe(&b->parameter);
733 p_delete(&b->subtype);
735 mutt_parse_content_type (buf, b);
738 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
739 type_changed = ascii_strcasecmp (tmp, obuf);
741 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
743 /* if in send mode, check for conversion - current setting is default. */
745 if (!h && b->type == TYPETEXT && charset_changed) {
748 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
749 parameter_getval(b->parameter, "charset"));
750 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
751 b->noconv = (r == M_NO);
754 /* inform the user */
756 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
758 mutt_message (_("Content-Type changed to %s."), tmp);
759 if (b->type == TYPETEXT && charset_changed) {
762 mutt_message (_("Character set changed to %s; %s."),
763 parameter_getval(b->parameter, "charset"),
764 b->noconv ? _("not converting") : _("converting"));
767 b->force_charset |= charset_changed ? 1 : 0;
769 if (!is_multipart(b) && b->parts)
770 body_list_wipe(&b->parts);
771 if (!mutt_is_message_type(b) && b->hdr) {
772 b->hdr->content = NULL;
773 header_delete(&b->hdr);
776 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
777 mutt_parse_part (fp, b);
783 h->security |= crypt_query (b);