2 * Copyright (C) 1999-2004 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include "mutt_curses.h"
28 #include "mutt_menu.h"
32 #include "mutt_idna.h"
34 /* some helper functions to verify that we are exclusively operating
35 * on message/rfc822 attachments
38 static short check_msg (BODY * b, short err)
40 if (!mutt_is_message_type (b->type, b->subtype))
43 mutt_error _("You may only bounce message/rfc822 parts.");
49 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
50 BODY * cur, short err)
54 if (cur && check_msg (cur, err) == -1)
58 for (i = 0; i < idxlen; i++)
60 if (idx[i]->content->tagged)
62 if (check_msg (idx[i]->content, err) == -1)
71 /* can we decode all tagged attachments? */
73 static short check_can_decode (ATTACHPTR ** idx, short idxlen,
79 return mutt_can_decode (cur);
81 for (i = 0; i < idxlen; i++)
82 if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
88 static short count_tagged (ATTACHPTR **idx, short idxlen)
93 for (i = 0; i < idxlen; i++)
94 if (idx[i]->content->tagged)
100 /* count the number of tagged children below a multipart or message
104 static short count_tagged_children (ATTACHPTR ** idx,
105 short idxlen, short i)
107 short level = idx[i]->level;
110 while ((++i < idxlen) && (level < idx[i]->level))
111 if (idx[i]->content->tagged)
121 ** The bounce function, from the attachment menu
125 void mutt_attach_bounce (FILE * fp, HEADER * hdr,
126 ATTACHPTR ** idx, short idxlen, BODY * cur)
130 char buf[HUGE_STRING];
136 if (check_all_msg (idx, idxlen, cur, 1) == -1)
139 /* one or more messages? */
140 p = (cur || count_tagged (idx, idxlen) == 1);
143 strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
145 strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
148 if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
152 if (!(adr = rfc822_parse_adrlist (adr, buf)))
154 mutt_error _("Error parsing address!");
158 adr = mutt_expand_aliases (adr);
160 if (mutt_addrlist_to_idna (adr, &err) < 0)
162 mutt_error (_("Bad IDN: '%s'"), err);
164 rfc822_free_address (&adr);
169 rfc822_write_address (buf, sizeof (buf), adr, 1);
171 #define extra_space (15+7+2)
175 snprintf (prompt, sizeof (prompt) - 4,
176 (p ? _("Bounce message to %s") : _("Bounce messages to %s")), buf);
178 if (mutt_strwidth (prompt) > COLS - extra_space)
180 mutt_format_string (prompt, sizeof (prompt) - 4,
181 0, COLS-extra_space, FMT_LEFT, 0,
182 prompt, sizeof (prompt), 0);
183 safe_strcat (prompt, sizeof (prompt), "...?");
186 safe_strcat (prompt, sizeof (prompt), "?");
188 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES)
190 rfc822_free_address (&adr);
191 CLEARLINE (LINES - 1);
192 mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
196 CLEARLINE (LINES - 1);
199 ret = mutt_bounce_message (fp, cur->hdr, adr);
202 for (i = 0; i < idxlen; i++)
204 if (idx[i]->content->tagged)
205 if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
211 mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
213 mutt_error (p ? _("Error bouncing message!") : _("Error bouncing messages!"));
220 ** resend-message, from the attachment menu
225 void mutt_attach_resend (FILE * fp, HEADER * hdr, ATTACHPTR ** idx,
226 short idxlen, BODY * cur)
230 if (check_all_msg (idx, idxlen, cur, 1) == -1)
234 mutt_resend_message (fp, Context, cur->hdr);
237 for (i = 0; i < idxlen; i++)
238 if (idx[i]->content->tagged)
239 mutt_resend_message (fp, Context, idx[i]->content->hdr);
246 ** forward-message, from the attachment menu
250 /* try to find a common parent message for the tagged attachments. */
252 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
258 for (i = 0; i < idxlen; i++)
259 if (idx[i]->content->tagged)
264 if (mutt_is_message_type (idx[i]->content->type, idx[i]->content->subtype))
266 nchildren = count_tagged_children (idx, idxlen, i);
267 if (nchildren == nattach)
268 return idx[i]->content->hdr;
276 * check whether attachment #i is a parent of the attachment
279 * Note: This and the calling procedure could be optimized quite a
280 * bit. For now, it's not worth the effort.
283 static int is_parent (short i, ATTACHPTR **idx, short idxlen, BODY *cur)
285 short level = idx[i]->level;
287 while ((++i < idxlen) && idx[i]->level > level)
289 if (idx[i]->content == cur)
296 static HEADER *find_parent (ATTACHPTR **idx, short idxlen, BODY *cur, short nattach)
299 HEADER *parent = NULL;
303 for (i = 0; i < idxlen; i++)
305 if (mutt_is_message_type (idx[i]->content->type, idx[i]->content->subtype)
306 && is_parent (i, idx, idxlen, cur))
307 parent = idx[i]->content->hdr;
308 if (idx[i]->content == cur)
313 parent = find_common_parent (idx, idxlen, nattach);
318 static void include_header (int quote, FILE * ifp,
319 HEADER * hdr, FILE * ofp,
322 int chflags = CH_DECODE;
323 char prefix[SHORT_STRING];
325 if (option (OPTWEED))
326 chflags |= CH_WEED | CH_REORDER;
331 strfcpy (prefix, _prefix, sizeof (prefix));
332 else if (!option (OPTTEXTFLOWED))
333 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
336 strfcpy (prefix, ">", sizeof (prefix));
338 chflags |= CH_PREFIX;
341 mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
344 /* Attach all the body parts which can't be decoded.
345 * This code is shared by forwarding and replying. */
347 static BODY ** copy_problematic_attachments (FILE *fp,
355 for (i = 0; i < idxlen; i++)
357 if (idx[i]->content->tagged &&
358 (force || !mutt_can_decode (idx[i]->content)))
360 if (mutt_copy_body (fp, last, idx[i]->content) == -1)
361 return NULL; /* XXXXX - may lead to crashes */
362 last = &((*last)->next);
369 * forward one or several MIME bodies
370 * (non-message types)
373 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
374 ATTACHPTR ** idx, short idxlen,
379 short mime_fwd_all = 0;
380 short mime_fwd_any = 1;
381 HEADER *parent = NULL;
382 HEADER *tmphdr = NULL;
384 char tmpbody[_POSIX_PATH_MAX];
394 * First, find the parent message.
395 * Note: This could be made an option by just
396 * putting the following lines into an if block.
400 parent = find_parent (idx, idxlen, cur, nattach);
406 tmphdr = mutt_new_header ();
407 tmphdr->env = mutt_new_envelope ();
408 mutt_make_forward_subject (tmphdr->env, Context, parent);
410 mutt_mktemp (tmpbody);
411 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL)
413 mutt_error (_("Can't open temporary file %s."), tmpbody);
417 mutt_forward_intro (tmpfp, parent);
419 /* prepare the prefix here since we'll need it later. */
421 if (option (OPTFORWQUOTE))
423 if (!option (OPTTEXTFLOWED))
424 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
427 strfcpy (prefix, ">", sizeof (prefix));
430 include_header (option (OPTFORWQUOTE), fp, parent,
435 * Now, we have prepared the first part of the message body: The
436 * original message's header.
438 * The next part is more interesting: either include the message bodies,
442 if ((!cur || mutt_can_decode (cur)) &&
443 (rc = query_quadoption (OPT_MIMEFWD,
444 _("Forward as attachments?"))) == M_YES)
450 * shortcut MIMEFWDREST when there is only one attachment. Is
454 if (!mime_fwd_all && !cur && (nattach > 1)
455 && !check_can_decode (idx, idxlen, cur))
457 if ((rc = query_quadoption (OPT_MIMEFWDREST,
458 _("Can't decode all tagged attachments. MIME-forward the others?"))) == -1)
464 /* initialize a state structure */
466 memset (&st, 0, sizeof (st));
468 if (option (OPTFORWQUOTE))
470 st.flags = M_CHARCONV;
471 if (option (OPTWEED))
476 /* where do we append new MIME parts? */
477 last = &tmphdr->content;
481 /* single body case */
483 if (!mime_fwd_all && mutt_can_decode (cur))
485 mutt_body_handler (cur, &st);
486 state_putc ('\n', &st);
490 if (mutt_copy_body (fp, last, cur) == -1)
492 last = &((*last)->next);
497 /* multiple body case */
501 for (i = 0; i < idxlen; i++)
503 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content))
505 mutt_body_handler (idx[i]->content, &st);
506 state_putc ('\n', &st);
512 (last = copy_problematic_attachments (fp, last, idx, idxlen, mime_fwd_all)) == NULL)
516 mutt_forward_trailer (tmpfp);
521 /* now that we have the template, send it. */
522 ci_send_message (0, tmphdr, tmpbody, NULL, parent);
530 mutt_unlink (tmpbody);
533 mutt_free_header (&tmphdr);
538 * Forward one or several message-type attachments. This
539 * is different from the previous function
540 * since we want to mimic the index menu's behaviour.
542 * Code reuse from ci_send_message is not possible here -
543 * ci_send_message relies on a context structure to find messages,
544 * while, on the attachment menu, messages are referenced through
545 * the attachment index.
548 static void attach_forward_msgs (FILE * fp, HEADER * hdr,
549 ATTACHPTR ** idx, short idxlen, BODY * cur)
551 HEADER *curhdr = NULL;
557 char tmpbody[_POSIX_PATH_MAX];
561 int chflags = CH_XMIT;
567 for (i = 0; i < idxlen; i++)
568 if (idx[i]->content->tagged)
570 curhdr = idx[i]->content->hdr;
575 tmphdr = mutt_new_header ();
576 tmphdr->env = mutt_new_envelope ();
577 mutt_make_forward_subject (tmphdr->env, Context, curhdr);
582 if ((rc = query_quadoption (OPT_MIMEFWD,
583 _("Forward MIME encapsulated?"))) == M_NO)
586 /* no MIME encapsulation */
588 mutt_mktemp (tmpbody);
589 if (!(tmpfp = safe_fopen (tmpbody, "w")))
591 mutt_error (_("Can't create %s."), tmpbody);
592 mutt_free_header (&tmphdr);
596 if (option (OPTFORWQUOTE))
598 chflags |= CH_PREFIX;
599 cmflags |= M_CM_PREFIX;
602 if (option (OPTFORWDECODE))
604 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
605 if (option (OPTWEED))
607 chflags |= CH_WEED | CH_REORDER;
608 cmflags |= M_CM_WEED;
615 /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */
616 mutt_forward_intro (tmpfp, cur->hdr);
617 _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags, chflags);
618 mutt_forward_trailer (tmpfp);
622 for (i = 0; i < idxlen; i++)
624 if (idx[i]->content->tagged)
626 /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */
627 mutt_forward_intro (tmpfp, idx[i]->content->hdr);
628 _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
629 idx[i]->content->hdr->content, cmflags, chflags);
630 mutt_forward_trailer (tmpfp);
636 else if (rc == M_YES) /* do MIME encapsulation - we don't need to do much here */
638 last = &tmphdr->content;
640 mutt_copy_body (fp, last, cur);
643 for (i = 0; i < idxlen; i++)
644 if (idx[i]->content->tagged)
646 mutt_copy_body (fp, last, idx[i]->content);
647 last = &((*last)->next);
652 mutt_free_header (&tmphdr);
654 ci_send_message (0, tmphdr, *tmpbody ? tmpbody : NULL,
659 void mutt_attach_forward (FILE * fp, HEADER * hdr,
660 ATTACHPTR ** idx, short idxlen, BODY * cur)
665 if (check_all_msg (idx, idxlen, cur, 0) == 0)
666 attach_forward_msgs (fp, hdr, idx, idxlen, cur);
669 nattach = count_tagged (idx, idxlen);
670 attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach);
678 ** the various reply functions, from the attachment menu
683 /* Create the envelope defaults for a reply.
685 * This function can be invoked in two ways.
687 * Either, parent is NULL. In this case, all tagged bodies are of a message type,
688 * and the header information is fetched from them.
690 * Or, parent is non-NULL. In this case, cur is the common parent of all the
691 * tagged attachments.
693 * Note that this code is horribly similar to envelope_defaults () from send.c.
697 attach_reply_envelope_defaults (ENVELOPE *env, ATTACHPTR **idx, short idxlen,
698 HEADER *parent, int flags)
700 ENVELOPE *curenv = NULL;
701 HEADER *curhdr = NULL;
706 for (i = 0; i < idxlen; i++)
708 if (idx[i]->content->tagged)
710 curhdr = idx[i]->content->hdr;
711 curenv = curhdr->env;
718 curenv = parent->env;
722 if (curenv == NULL || curhdr == NULL)
724 mutt_error _("Can't find any tagged messages.");
730 if (mutt_fetch_recips (env, curenv, flags) == -1)
735 for (i = 0; i < idxlen; i++)
737 if (idx[i]->content->tagged
738 && mutt_fetch_recips (env, idx[i]->content->hdr->env, flags) == -1)
743 if ((flags & SENDLISTREPLY) && !env->to)
745 mutt_error _("No mailing lists found!");
749 mutt_fix_reply_recipients (env);
750 mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
753 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
756 LIST **p = NULL, **q = NULL;
758 for (i = 0; i < idxlen; i++)
760 if (idx[i]->content->tagged)
761 mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p, &q);
769 /* This is _very_ similar to send.c's include_reply(). */
771 static void attach_include_reply (FILE *fp, FILE *tmpfp, HEADER *cur, int flags)
773 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
774 int chflags = CH_DECODE;
776 /* mutt_message_hook (cur, M_MESSAGEHOOK); */
778 mutt_make_attribution (Context, cur, tmpfp);
780 if (!option (OPTHEADER))
781 cmflags |= M_CM_NOHEADER;
782 if (option (OPTWEED))
785 cmflags |= M_CM_WEED;
788 _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
789 mutt_make_post_indent (Context, cur, tmpfp);
792 void mutt_attach_reply (FILE * fp, HEADER * hdr,
793 ATTACHPTR ** idx, short idxlen, BODY * cur,
796 short mime_reply_any = 0;
799 HEADER *parent = NULL;
800 HEADER *tmphdr = NULL;
804 char tmpbody[_POSIX_PATH_MAX];
807 char prefix[SHORT_STRING];
810 if (check_all_msg (idx, idxlen, cur, 0) == -1)
812 nattach = count_tagged (idx, idxlen);
813 if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
817 if (nattach > 1 && !check_can_decode (idx, idxlen, cur))
819 if ((rc = query_quadoption (OPT_MIMEFWDREST,
820 _("Can't decode all tagged attachments. MIME-encapsulate the others?"))) == -1)
822 else if (rc == M_YES)
825 else if (nattach == 1)
828 tmphdr = mutt_new_header ();
829 tmphdr->env = mutt_new_envelope ();
831 if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
832 parent ? parent : (cur ? cur->hdr : NULL), flags) == -1)
834 mutt_free_header (&tmphdr);
838 mutt_mktemp (tmpbody);
839 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL)
841 mutt_error (_("Can't create %s."), tmpbody);
842 mutt_free_header (&tmphdr);
849 attach_include_reply (fp, tmpfp, cur->hdr, flags);
852 for (i = 0; i < idxlen; i++)
854 if (idx[i]->content->tagged)
855 attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
861 mutt_make_attribution (Context, parent, tmpfp);
863 memset (&st, 0, sizeof (STATE));
867 if (!option (OPTTEXTFLOWED))
868 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
871 strfcpy (prefix, ">", sizeof (prefix));
874 st.flags = M_CHARCONV;
876 if (option (OPTWEED))
879 if (option (OPTHEADER))
880 include_header (1, fp, parent, tmpfp, prefix);
884 if (mutt_can_decode (cur))
886 mutt_body_handler (cur, &st);
887 state_putc ('\n', &st);
890 mutt_copy_body (fp, &tmphdr->content, cur);
894 for (i = 0; i < idxlen; i++)
896 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content))
898 mutt_body_handler (idx[i]->content, &st);
899 state_putc ('\n', &st);
904 mutt_make_post_indent (Context, parent, tmpfp);
906 if (mime_reply_any && !cur &&
907 copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen, 0) == NULL)
909 mutt_free_header (&tmphdr);
917 if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
918 mutt_set_flag (Context, hdr, M_REPLIED, 1);