2 * Copyright notice from original mutt:
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
16 #include "mutt_menu.h"
24 #include "mutt_idna.h"
26 /* some helper functions to verify that we are exclusively operating
27 * on message/rfc822 attachments
30 static short check_msg (BODY * b, short err)
32 if (!mutt_is_message_type (b->type, b->subtype)) {
34 mutt_error _("You may only bounce message/rfc822 parts.");
41 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
42 BODY * cur, short err)
46 if (cur && check_msg (cur, err) == -1)
49 for (i = 0; i < idxlen; i++) {
50 if (idx[i]->content->tagged) {
51 if (check_msg (idx[i]->content, err) == -1)
60 /* can we decode all tagged attachments? */
62 static short check_can_decode (ATTACHPTR ** idx, short idxlen, BODY * cur)
67 return mutt_can_decode (cur);
69 for (i = 0; i < idxlen; i++)
70 if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
76 static short count_tagged (ATTACHPTR ** idx, short idxlen)
81 for (i = 0; i < idxlen; i++)
82 if (idx[i]->content->tagged)
88 /* count the number of tagged children below a multipart or message
92 static short count_tagged_children (ATTACHPTR ** idx, short idxlen, short i)
94 short level = idx[i]->level;
97 while ((++i < idxlen) && (level < idx[i]->level))
98 if (idx[i]->content->tagged)
108 ** The bounce function, from the attachment menu
112 void mutt_attach_bounce (FILE * fp, HEADER * hdr,
113 ATTACHPTR ** idx, short idxlen, BODY * cur)
117 char buf[HUGE_STRING];
123 if (check_all_msg (idx, idxlen, cur, 1) == -1)
126 /* one or more messages? */
127 p = (cur || count_tagged (idx, idxlen) == 1);
130 strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
132 strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
135 if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
139 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
140 mutt_error _("Error parsing address!");
145 adr = mutt_expand_aliases (adr);
147 if (mutt_addrlist_to_idna (adr, &err) < 0) {
148 mutt_error (_("Bad IDN: '%s'"), err);
150 rfc822_free_address (&adr);
155 rfc822_write_address (buf, sizeof (buf), adr, 1);
157 #define extra_space (15+7+2)
161 snprintf (prompt, sizeof (prompt) - 4,
162 (p ? _("Bounce message to %s") : _("Bounce messages to %s")),
165 if (mutt_strwidth (prompt) > COLS - extra_space) {
166 mutt_format_string (prompt, sizeof (prompt) - 4,
167 0, COLS - extra_space, 0, 0,
168 prompt, sizeof (prompt), 0);
169 safe_strcat (prompt, sizeof (prompt), "...?");
172 safe_strcat (prompt, sizeof (prompt), "?");
174 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
175 rfc822_free_address (&adr);
176 CLEARLINE (LINES - 1);
177 mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
181 CLEARLINE (LINES - 1);
184 ret = mutt_bounce_message (fp, cur->hdr, adr);
186 for (i = 0; i < idxlen; i++) {
187 if (idx[i]->content->tagged)
188 if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
194 mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
196 mutt_error (p ? _("Error bouncing message!") :
197 _("Error bouncing messages!"));
204 ** resend-message, from the attachment menu
209 void mutt_attach_resend (FILE * fp, HEADER * hdr, ATTACHPTR ** idx,
210 short idxlen, BODY * cur)
214 if (check_all_msg (idx, idxlen, cur, 1) == -1)
218 mutt_resend_message (fp, Context, cur->hdr);
220 for (i = 0; i < idxlen; i++)
221 if (idx[i]->content->tagged)
222 mutt_resend_message (fp, Context, idx[i]->content->hdr);
229 ** forward-message, from the attachment menu
233 /* try to find a common parent message for the tagged attachments. */
235 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
241 for (i = 0; i < idxlen; i++)
242 if (idx[i]->content->tagged)
246 if (mutt_is_message_type
247 (idx[i]->content->type, idx[i]->content->subtype)) {
248 nchildren = count_tagged_children (idx, idxlen, i);
249 if (nchildren == nattach)
250 return idx[i]->content->hdr;
258 * check whether attachment #i is a parent of the attachment
261 * Note: This and the calling procedure could be optimized quite a
262 * bit. For now, it's not worth the effort.
265 static int is_parent (short i, ATTACHPTR ** idx, short idxlen, BODY * cur)
267 short level = idx[i]->level;
269 while ((++i < idxlen) && idx[i]->level > level) {
270 if (idx[i]->content == cur)
277 static HEADER *find_parent (ATTACHPTR ** idx, short idxlen, BODY * cur,
281 HEADER *parent = NULL;
284 for (i = 0; i < idxlen; i++) {
285 if (mutt_is_message_type
286 (idx[i]->content->type, idx[i]->content->subtype)
287 && is_parent (i, idx, idxlen, cur))
288 parent = idx[i]->content->hdr;
289 if (idx[i]->content == cur)
294 parent = find_common_parent (idx, idxlen, nattach);
299 static void include_header (int quote, FILE * ifp,
300 HEADER * hdr, FILE * ofp, char *_prefix)
302 int chflags = CH_DECODE;
303 char prefix[SHORT_STRING];
305 if (option (OPTWEED))
306 chflags |= CH_WEED | CH_REORDER;
310 strfcpy (prefix, _prefix, sizeof (prefix));
311 else if (!option (OPTTEXTFLOWED))
312 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
315 strfcpy (prefix, ">", sizeof (prefix));
317 chflags |= CH_PREFIX;
320 mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
323 /* Attach all the body parts which can't be decoded.
324 * This code is shared by forwarding and replying. */
326 static BODY **copy_problematic_attachments (FILE * fp,
329 short idxlen, short force)
333 for (i = 0; i < idxlen; i++) {
334 if (idx[i]->content->tagged &&
335 (force || !mutt_can_decode (idx[i]->content))) {
336 if (mutt_copy_body (fp, last, idx[i]->content) == -1)
337 return NULL; /* XXXXX - may lead to crashes */
338 last = &((*last)->next);
345 * forward one or several MIME bodies
346 * (non-message types)
349 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
350 ATTACHPTR ** idx, short idxlen,
351 BODY * cur, short nattach, int flags)
354 short mime_fwd_all = 0;
355 short mime_fwd_any = 1;
356 HEADER *parent = NULL;
357 HEADER *tmphdr = NULL;
359 char tmpbody[_POSIX_PATH_MAX];
369 * First, find the parent message.
370 * Note: This could be made an option by just
371 * putting the following lines into an if block.
375 parent = find_parent (idx, idxlen, cur, nattach);
381 tmphdr = mutt_new_header ();
382 tmphdr->env = mutt_new_envelope ();
383 mutt_make_forward_subject (tmphdr->env, Context, parent);
385 mutt_mktemp (tmpbody);
386 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
387 mutt_error (_("Can't open temporary file %s."), tmpbody);
391 mutt_forward_intro (tmpfp, parent);
393 /* prepare the prefix here since we'll need it later. */
395 if (option (OPTFORWQUOTE)) {
396 if (!option (OPTTEXTFLOWED))
397 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
400 strfcpy (prefix, ">", sizeof (prefix));
403 include_header (option (OPTFORWQUOTE), fp, parent, tmpfp, prefix);
407 * Now, we have prepared the first part of the message body: The
408 * original message's header.
410 * The next part is more interesting: either include the message bodies,
414 if ((!cur || mutt_can_decode (cur)) &&
415 (rc = query_quadoption (OPT_MIMEFWD,
416 _("Forward as attachments?"))) == M_YES)
422 * shortcut MIMEFWDREST when there is only one attachment. Is
426 if (!mime_fwd_all && !cur && (nattach > 1)
427 && !check_can_decode (idx, idxlen, cur)) {
428 if ((rc = query_quadoption (OPT_MIMEFWDREST,
430 ("Can't decode all tagged attachments. MIME-forward the others?")))
437 /* initialize a state structure */
439 memset (&st, 0, sizeof (st));
441 if (option (OPTFORWQUOTE))
443 st.flags = M_CHARCONV;
444 if (option (OPTWEED))
449 /* where do we append new MIME parts? */
450 last = &tmphdr->content;
453 /* single body case */
455 if (!mime_fwd_all && mutt_can_decode (cur)) {
456 mutt_body_handler (cur, &st);
457 state_putc ('\n', &st);
460 if (mutt_copy_body (fp, last, cur) == -1)
462 last = &((*last)->next);
466 /* multiple body case */
469 for (i = 0; i < idxlen; i++) {
470 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
471 mutt_body_handler (idx[i]->content, &st);
472 state_putc ('\n', &st);
479 copy_problematic_attachments (fp, last, idx, idxlen,
480 mime_fwd_all)) == NULL)
484 mutt_forward_trailer (tmpfp);
489 /* now that we have the template, send it. */
490 ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
497 mutt_unlink (tmpbody);
500 mutt_free_header (&tmphdr);
505 * Forward one or several message-type attachments. This
506 * is different from the previous function
507 * since we want to mimic the index menu's behaviour.
509 * Code reuse from ci_send_message is not possible here -
510 * ci_send_message relies on a context structure to find messages,
511 * while, on the attachment menu, messages are referenced through
512 * the attachment index.
515 static void attach_forward_msgs (FILE * fp, HEADER * hdr,
516 ATTACHPTR ** idx, short idxlen, BODY * cur,
519 HEADER *curhdr = NULL;
525 char tmpbody[_POSIX_PATH_MAX];
529 int chflags = CH_XMIT;
534 for (i = 0; i < idxlen; i++)
535 if (idx[i]->content->tagged) {
536 curhdr = idx[i]->content->hdr;
541 tmphdr = mutt_new_header ();
542 tmphdr->env = mutt_new_envelope ();
543 mutt_make_forward_subject (tmphdr->env, Context, curhdr);
548 if ((rc = query_quadoption (OPT_MIMEFWD,
549 _("Forward MIME encapsulated?"))) == M_NO) {
551 /* no MIME encapsulation */
553 mutt_mktemp (tmpbody);
554 if (!(tmpfp = safe_fopen (tmpbody, "w"))) {
555 mutt_error (_("Can't create %s."), tmpbody);
556 mutt_free_header (&tmphdr);
560 if (option (OPTFORWQUOTE)) {
561 chflags |= CH_PREFIX;
562 cmflags |= M_CM_PREFIX;
565 if (option (OPTFORWDECODE)) {
566 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
567 if (option (OPTWEED)) {
568 chflags |= CH_WEED | CH_REORDER;
569 cmflags |= M_CM_WEED;
575 /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */
576 mutt_forward_intro (tmpfp, cur->hdr);
577 _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags,
579 mutt_forward_trailer (tmpfp);
582 for (i = 0; i < idxlen; i++) {
583 if (idx[i]->content->tagged) {
584 /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */
585 mutt_forward_intro (tmpfp, idx[i]->content->hdr);
586 _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
587 idx[i]->content->hdr->content, cmflags,
589 mutt_forward_trailer (tmpfp);
595 else if (rc == M_YES) { /* do MIME encapsulation - we don't need to do much here */
596 last = &tmphdr->content;
598 mutt_copy_body (fp, last, cur);
600 for (i = 0; i < idxlen; i++)
601 if (idx[i]->content->tagged) {
602 mutt_copy_body (fp, last, idx[i]->content);
603 last = &((*last)->next);
608 mutt_free_header (&tmphdr);
610 ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, NULL, curhdr);
614 void mutt_attach_forward (FILE * fp, HEADER * hdr,
615 ATTACHPTR ** idx, short idxlen, BODY * cur,
621 if (check_all_msg (idx, idxlen, cur, 0) == 0)
622 attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
624 nattach = count_tagged (idx, idxlen);
625 attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
633 ** the various reply functions, from the attachment menu
638 /* Create the envelope defaults for a reply.
640 * This function can be invoked in two ways.
642 * Either, parent is NULL. In this case, all tagged bodies are of a message type,
643 * and the header information is fetched from them.
645 * Or, parent is non-NULL. In this case, cur is the common parent of all the
646 * tagged attachments.
648 * Note that this code is horribly similar to envelope_defaults () from send.c.
652 attach_reply_envelope_defaults (ENVELOPE * env, ATTACHPTR ** idx,
653 short idxlen, HEADER * parent, int flags)
655 ENVELOPE *curenv = NULL;
656 HEADER *curhdr = NULL;
660 for (i = 0; i < idxlen; i++) {
661 if (idx[i]->content->tagged) {
662 curhdr = idx[i]->content->hdr;
663 curenv = curhdr->env;
669 curenv = parent->env;
673 if (curenv == NULL || curhdr == NULL) {
674 mutt_error _("Can't find any tagged messages.");
680 if ((flags & SENDNEWS)) {
681 /* in case followup set Newsgroups: with Followup-To: if it present */
682 if (!env->newsgroups && curenv &&
683 mutt_strcasecmp (curenv->followup_to, "poster"))
684 env->newsgroups = safe_strdup (curenv->followup_to);
690 if (mutt_fetch_recips (env, curenv, flags) == -1)
694 for (i = 0; i < idxlen; i++) {
695 if (idx[i]->content->tagged
696 && mutt_fetch_recips (env, idx[i]->content->hdr->env,
702 if ((flags & SENDLISTREPLY) && !env->to) {
703 mutt_error _("No mailing lists found!");
708 mutt_fix_reply_recipients (env);
710 mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
713 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
715 LIST **p = NULL, **q = NULL;
717 for (i = 0; i < idxlen; i++) {
718 if (idx[i]->content->tagged)
719 mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p,
728 /* This is _very_ similar to send.c's include_reply(). */
730 static void attach_include_reply (FILE * fp, FILE * tmpfp, HEADER * cur,
733 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
734 int chflags = CH_DECODE;
736 /* mutt_message_hook (cur, M_MESSAGEHOOK); */
738 mutt_make_attribution (Context, cur, tmpfp);
740 if (!option (OPTHEADER))
741 cmflags |= M_CM_NOHEADER;
742 if (option (OPTWEED)) {
744 cmflags |= M_CM_WEED;
747 _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
748 mutt_make_post_indent (Context, cur, tmpfp);
751 void mutt_attach_reply (FILE * fp, HEADER * hdr,
752 ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
754 short mime_reply_any = 0;
757 HEADER *parent = NULL;
758 HEADER *tmphdr = NULL;
762 char tmpbody[_POSIX_PATH_MAX];
765 char prefix[SHORT_STRING];
769 if (flags & SENDNEWS)
770 set_option (OPTNEWSSEND);
772 unset_option (OPTNEWSSEND);
775 if (check_all_msg (idx, idxlen, cur, 0) == -1) {
776 nattach = count_tagged (idx, idxlen);
777 if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
781 if (nattach > 1 && !check_can_decode (idx, idxlen, cur)) {
782 if ((rc = query_quadoption (OPT_MIMEFWDREST,
784 ("Can't decode all tagged attachments. MIME-encapsulate the others?")))
787 else if (rc == M_YES)
790 else if (nattach == 1)
793 tmphdr = mutt_new_header ();
794 tmphdr->env = mutt_new_envelope ();
796 if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
797 parent ? parent : (cur ? cur->
800 mutt_free_header (&tmphdr);
804 mutt_mktemp (tmpbody);
805 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
806 mutt_error (_("Can't create %s."), tmpbody);
807 mutt_free_header (&tmphdr);
813 attach_include_reply (fp, tmpfp, cur->hdr, flags);
815 for (i = 0; i < idxlen; i++) {
816 if (idx[i]->content->tagged)
817 attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
822 mutt_make_attribution (Context, parent, tmpfp);
824 memset (&st, 0, sizeof (STATE));
828 if (!option (OPTTEXTFLOWED))
829 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
832 strfcpy (prefix, ">", sizeof (prefix));
835 st.flags = M_CHARCONV;
837 if (option (OPTWEED))
840 if (option (OPTHEADER))
841 include_header (1, fp, parent, tmpfp, prefix);
844 if (mutt_can_decode (cur)) {
845 mutt_body_handler (cur, &st);
846 state_putc ('\n', &st);
849 mutt_copy_body (fp, &tmphdr->content, cur);
852 for (i = 0; i < idxlen; i++) {
853 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
854 mutt_body_handler (idx[i]->content, &st);
855 state_putc ('\n', &st);
860 mutt_make_post_indent (Context, parent, tmpfp);
862 if (mime_reply_any && !cur &&
863 copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
865 mutt_free_header (&tmphdr);
873 if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
874 mutt_set_flag (Context, hdr, M_REPLIED, 1);