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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-mime/mime.h>
14 #include <lib-ui/lib-ui.h>
15 #include <lib-ui/menu.h>
16 #include <lib-mx/mx.h>
22 #include "recvattach.h"
25 #include "mutt_idna.h"
28 /* some helper functions to verify that we are exclusively operating
29 * on message/rfc822 attachments
31 static int check_all_msg(ATTACHPTR **idx, int idxlen, BODY *cur, bool err)
34 if (!mutt_is_message_type(cur))
37 for (int i = 0; i < idxlen; i++) {
38 BODY *b = idx[i]->content;
39 if (b->tagged && !mutt_is_message_type(b))
47 mutt_error(_("You may only bounce message/rfc822 parts."));
52 /* can we decode all tagged attachments? */
53 static int check_can_decode(ATTACHPTR ** idx, int idxlen, BODY * cur)
56 return mutt_can_decode(cur);
58 for (int i = 0; i < idxlen; i++) {
59 if (idx[i]->content->tagged && !mutt_can_decode(idx[i]->content))
66 static int count_tagged(ATTACHPTR **idx, int idxlen)
70 for (int i = 0; i < idxlen; i++) {
71 if (idx[i]->content->tagged)
78 /* count the number of tagged children below a multipart or message
81 static short count_tagged_children (ATTACHPTR ** idx, short idxlen, short i)
83 short level = idx[i]->level;
86 while ((++i < idxlen) && (level < idx[i]->level))
87 if (idx[i]->content->tagged)
95 ** The bounce function, from the attachment menu
98 void mutt_attach_bounce (FILE * fp, HEADER * hdr __attribute__ ((unused)),
99 ATTACHPTR ** idx, short idxlen, BODY * cur)
103 char buf[HUGE_STRING];
105 address_t *adr = NULL;
109 if (check_all_msg (idx, idxlen, cur, 1) == -1)
112 /* one or more messages? */
113 p = (cur || count_tagged (idx, idxlen) == 1);
116 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
118 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
121 if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
125 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
126 mutt_error _("Error parsing address!");
131 adr = mutt_expand_aliases (adr);
133 if (mutt_addrlist_to_idna (adr, &err) < 0) {
134 mutt_error (_("Bad IDN: '%s'"), err);
136 address_list_wipe(&adr);
141 rfc822_addrcat(buf, sizeof(buf), adr, 1);
146 snprintf (prompt, sizeof (prompt) - 4,
147 (p ? _("Bounce message to %s") : _("Bounce messages to %s")),
150 #define extra_space (15+7+2)
151 if (m_strwidth(prompt) > getmaxx(main_w) - extra_space) {
152 mutt_format_string (prompt, sizeof (prompt) - 4, 0,
153 getmaxx(main_w) - extra_space, 0, 0,
154 prompt, sizeof (prompt), 0);
155 m_strcat(prompt, sizeof(prompt), "...?");
157 m_strcat(prompt, sizeof(prompt), "?");
161 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
162 address_list_wipe(&adr);
163 mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
168 ret = mutt_bounce_message (fp, cur->hdr, adr);
170 for (i = 0; i < idxlen; i++) {
171 if (idx[i]->content->tagged)
172 if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
178 mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
180 mutt_error (p ? _("Error bouncing message!") :
181 _("Error bouncing messages!"));
186 ** resend-message, from the attachment menu
189 void mutt_attach_resend (FILE * fp, HEADER * hdr __attribute__ ((unused)), ATTACHPTR ** idx,
190 short idxlen, BODY * cur)
194 if (check_all_msg (idx, idxlen, cur, 1) == -1)
198 mutt_resend_message (fp, Context, cur->hdr);
200 for (i = 0; i < idxlen; i++)
201 if (idx[i]->content->tagged)
202 mutt_resend_message (fp, Context, idx[i]->content->hdr);
207 ** forward-message, from the attachment menu
210 /* try to find a common parent message for the tagged attachments. */
212 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
218 for (i = 0; i < idxlen; i++)
219 if (idx[i]->content->tagged)
223 if (mutt_is_message_type(idx[i]->content)) {
224 nchildren = count_tagged_children (idx, idxlen, i);
225 if (nchildren == nattach)
226 return idx[i]->content->hdr;
234 * check whether attachment #i is a parent of the attachment
237 * Note: This and the calling procedure could be optimized quite a
238 * bit. For now, it's not worth the effort.
241 static int is_parent (short i, ATTACHPTR ** idx, short idxlen, BODY * cur)
243 short level = idx[i]->level;
245 while ((++i < idxlen) && idx[i]->level > level) {
246 if (idx[i]->content == cur)
253 static HEADER *find_parent (ATTACHPTR ** idx, short idxlen, BODY * cur,
257 HEADER *parent = NULL;
260 for (i = 0; i < idxlen; i++) {
261 if (mutt_is_message_type(idx[i]->content)
262 && is_parent (i, idx, idxlen, cur))
263 parent = idx[i]->content->hdr;
264 if (idx[i]->content == cur)
269 parent = find_common_parent (idx, idxlen, nattach);
274 static void include_header (int quote, FILE * ifp,
275 HEADER * hdr, FILE * ofp, char *_prefix)
277 int chflags = CH_DECODE;
280 if (option (OPTWEED))
281 chflags |= CH_WEED | CH_REORDER;
285 m_strcpy(prefix, sizeof(prefix), _prefix);
286 else if (!option (OPTTEXTFLOWED))
287 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
290 m_strcpy(prefix, sizeof(prefix), ">");
292 chflags |= CH_PREFIX;
295 mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
298 /* create a send-mode duplicate from a receive-mode body */
299 static int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
301 char tmp[_POSIX_PATH_MAX];
304 parameter_t *par, **ppar;
310 m_strcpy(tmp, sizeof(tmp), src->filename);
316 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
317 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
323 memcpy (b, src, sizeof (BODY));
327 b->filename = m_strdup(tmp);
328 b->use_disp = use_disp;
331 if (mutt_is_text_part (b))
334 b->xtype = m_strdup(b->xtype);
335 b->subtype = m_strdup(b->subtype);
336 b->form_name = m_strdup(b->form_name);
337 b->filename = m_strdup(b->filename);
338 b->d_filename = m_strdup(b->d_filename);
339 b->description = m_strdup(b->description);
342 * we don't seem to need the HEADER structure currently.
343 * XXX - this may change in the future
349 /* copy parameters */
350 for (par = b->parameter, ppar = &b->parameter; par;
351 ppar = &(*ppar)->next, par = par->next) {
352 *ppar = parameter_new();
353 (*ppar)->attribute = m_strdup(par->attribute);
354 (*ppar)->value = m_strdup(par->value);
357 mutt_stamp_attachment (b);
362 /* Attach all the body parts which can't be decoded.
363 * This code is shared by forwarding and replying. */
365 static BODY **copy_problematic_attachments (FILE * fp,
368 short idxlen, short force)
372 for (i = 0; i < idxlen; i++) {
373 if (idx[i]->content->tagged &&
374 (force || !mutt_can_decode (idx[i]->content))) {
375 if (mutt_copy_body (fp, last, idx[i]->content) == -1)
376 return NULL; /* XXXXX - may lead to crashes */
377 last = &((*last)->next);
384 * forward one or several MIME bodies
385 * (non-message types)
388 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
389 ATTACHPTR ** idx, short idxlen,
390 BODY * cur, short nattach, int flags)
393 short mime_fwd_all = 0;
394 short mime_fwd_any = 1;
395 HEADER *parent = NULL;
396 HEADER *tmphdr = NULL;
398 char tmpbody[_POSIX_PATH_MAX];
408 * First, find the parent message.
409 * Note: This could be made an option by just
410 * putting the following lines into an if block.
414 parent = find_parent (idx, idxlen, cur, nattach);
420 tmphdr = header_new();
421 tmphdr->env = envelope_new();
422 mutt_make_forward_subject (tmphdr->env, Context, parent);
424 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(mod_core.tmpdir), NULL);
426 mutt_error(_("Could not create temporary file"));
430 mutt_forward_intro (tmpfp, parent);
432 /* prepare the prefix here since we'll need it later. */
434 if (option (OPTFORWQUOTE)) {
435 if (!option (OPTTEXTFLOWED))
436 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
439 m_strcpy(prefix, sizeof(prefix), ">");
442 include_header (option (OPTFORWQUOTE), fp, parent, tmpfp, prefix);
446 * Now, we have prepared the first part of the message body: The
447 * original message's header.
449 * The next part is more interesting: either include the message bodies,
453 if ((!cur || mutt_can_decode (cur)) &&
454 (rc = query_quadoption (OPT_MIMEFWD,
455 _("Forward as attachments?"))) == M_YES)
461 * shortcut MIMEFWDREST when there is only one attachment. Is
465 if (!mime_fwd_all && !cur && (nattach > 1)
466 && !check_can_decode (idx, idxlen, cur)) {
467 if ((rc = query_quadoption (OPT_MIMEFWDREST,
469 ("Can't decode all tagged attachments. MIME-forward the others?")))
476 /* initialize a state structure */
480 if (option (OPTFORWQUOTE))
482 st.flags = M_CHARCONV;
483 if (option (OPTWEED))
488 /* where do we append new MIME parts? */
489 last = &tmphdr->content;
492 /* single body case */
494 if (!mime_fwd_all && mutt_can_decode (cur)) {
495 mutt_body_handler (cur, &st);
496 state_putc ('\n', &st);
499 if (mutt_copy_body (fp, last, cur) == -1)
501 last = &((*last)->next);
505 /* multiple body case */
508 for (i = 0; i < idxlen; i++) {
509 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
510 mutt_body_handler (idx[i]->content, &st);
511 state_putc ('\n', &st);
518 copy_problematic_attachments (fp, last, idx, idxlen,
519 mime_fwd_all)) == NULL)
523 mutt_forward_trailer (tmpfp);
526 /* now that we have the template, send it. */
527 ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
532 mutt_unlink(tmpbody);
533 header_delete(&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 __attribute__ ((unused)),
549 ATTACHPTR ** idx, short idxlen, BODY * cur,
552 HEADER *curhdr = NULL;
558 char tmpbody[_POSIX_PATH_MAX];
562 int chflags = CH_XMIT;
567 for (i = 0; i < idxlen; i++)
568 if (idx[i]->content->tagged) {
569 curhdr = idx[i]->content->hdr;
574 tmphdr = header_new();
575 tmphdr->env = envelope_new();
576 mutt_make_forward_subject (tmphdr->env, Context, curhdr);
581 if ((rc = query_quadoption (OPT_MIMEFWD,
582 _("Forward MIME encapsulated?"))) == M_NO) {
584 /* no MIME encapsulation */
586 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(mod_core.tmpdir), NULL);
588 mutt_error(_("Could not create temporary file"));
589 header_delete(&tmphdr);
593 if (option (OPTFORWQUOTE)) {
594 chflags |= CH_PREFIX;
595 cmflags |= M_CM_PREFIX;
598 if (option (OPTFORWDECODE)) {
599 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
600 if (option (OPTWEED)) {
601 chflags |= CH_WEED | CH_REORDER;
602 cmflags |= M_CM_WEED;
608 /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */
609 mutt_forward_intro (tmpfp, cur->hdr);
610 _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags,
612 mutt_forward_trailer (tmpfp);
615 for (i = 0; i < idxlen; i++) {
616 if (idx[i]->content->tagged) {
617 /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */
618 mutt_forward_intro (tmpfp, idx[i]->content->hdr);
619 _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
620 idx[i]->content->hdr->content, cmflags,
622 mutt_forward_trailer (tmpfp);
628 else if (rc == M_YES) { /* do MIME encapsulation - we don't need to do much here */
629 last = &tmphdr->content;
631 mutt_copy_body (fp, last, cur);
633 for (i = 0; i < idxlen; i++)
634 if (idx[i]->content->tagged) {
635 mutt_copy_body (fp, last, idx[i]->content);
636 last = &((*last)->next);
641 header_delete(&tmphdr);
643 ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, NULL, curhdr);
647 void mutt_attach_forward (FILE * fp, HEADER * hdr,
648 ATTACHPTR ** idx, short idxlen, BODY * cur,
654 if (check_all_msg (idx, idxlen, cur, 0) == 0) {
655 attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
657 nattach = count_tagged (idx, idxlen);
658 attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
664 ** the various reply functions, from the attachment menu
667 /* Create the envelope defaults for a reply.
669 * This function can be invoked in two ways.
671 * Either, parent is NULL. In this case, all tagged bodies are of a message type,
672 * and the header information is fetched from them.
674 * Or, parent is non-NULL. In this case, cur is the common parent of all the
675 * tagged attachments.
677 * Note that this code is horribly similar to envelope_defaults () from send.c.
681 attach_reply_envelope_defaults (ENVELOPE * env, ATTACHPTR ** idx,
682 short idxlen, HEADER * parent, int flags)
684 ENVELOPE *curenv = NULL;
685 HEADER *curhdr = NULL;
689 for (i = 0; i < idxlen; i++) {
690 if (idx[i]->content->tagged) {
691 curhdr = idx[i]->content->hdr;
692 curenv = curhdr->env;
698 curenv = parent->env;
702 if (curenv == NULL || curhdr == NULL) {
703 mutt_error _("Can't find any tagged messages.");
709 if (mutt_fetch_recips (env, curenv, flags) == -1)
712 for (i = 0; i < idxlen; i++) {
713 if (idx[i]->content->tagged
714 && mutt_fetch_recips (env, idx[i]->content->hdr->env,
720 if ((flags & SENDLISTREPLY) && !env->to) {
721 mutt_error _("No mailing lists found!");
726 mutt_fix_reply_recipients (env);
727 mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
730 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
732 string_list_t **p = NULL, **q = NULL;
734 for (i = 0; i < idxlen; i++) {
735 if (idx[i]->content->tagged)
736 mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p,
745 /* This is _very_ similar to send.c's include_reply(). */
747 static void attach_include_reply (FILE * fp, FILE * tmpfp, HEADER * cur,
748 int flags __attribute__ ((unused)))
750 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
751 int chflags = CH_DECODE;
753 /* mutt_message_hook (cur, M_MESSAGEHOOK); */
755 mutt_make_attribution (Context, cur, tmpfp);
757 if (!option (OPTHEADER))
758 cmflags |= M_CM_NOHEADER;
759 if (option (OPTWEED)) {
761 cmflags |= M_CM_WEED;
764 _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
767 void mutt_attach_reply (FILE * fp, HEADER * hdr,
768 ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
770 short mime_reply_any = 0;
773 HEADER *parent = NULL;
774 HEADER *tmphdr = NULL;
778 char tmpbody[_POSIX_PATH_MAX];
784 if (check_all_msg (idx, idxlen, cur, 0) == -1) {
785 nattach = count_tagged (idx, idxlen);
786 if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
790 if (nattach > 1 && !check_can_decode (idx, idxlen, cur)) {
791 if ((rc = query_quadoption (OPT_MIMEFWDREST,
793 ("Can't decode all tagged attachments. MIME-encapsulate the others?")))
796 else if (rc == M_YES)
799 else if (nattach == 1)
802 tmphdr = header_new();
803 tmphdr->env = envelope_new();
805 if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
806 parent ? parent : (cur ? cur->
809 header_delete(&tmphdr);
813 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(mod_core.tmpdir), NULL);
815 mutt_error(_("Could not create temporary file"));
816 header_delete(&tmphdr);
822 attach_include_reply (fp, tmpfp, cur->hdr, flags);
824 for (i = 0; i < idxlen; i++) {
825 if (idx[i]->content->tagged)
826 attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
831 mutt_make_attribution (Context, parent, tmpfp);
837 if (!option (OPTTEXTFLOWED))
838 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
841 m_strcpy(prefix, sizeof(prefix), ">");
844 st.flags = M_CHARCONV;
846 if (option (OPTWEED))
849 if (option (OPTHEADER))
850 include_header (1, fp, parent, tmpfp, prefix);
853 if (mutt_can_decode (cur)) {
854 mutt_body_handler (cur, &st);
855 state_putc ('\n', &st);
858 mutt_copy_body (fp, &tmphdr->content, cur);
861 for (i = 0; i < idxlen; i++) {
862 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
863 mutt_body_handler (idx[i]->content, &st);
864 state_putc ('\n', &st);
869 if (mime_reply_any && !cur &&
870 copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
872 header_delete(&tmphdr);
880 if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
881 mutt_set_flag (Context, hdr, M_REPLIED, 1);