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/curses.h>
15 #include <lib-ui/enter.h>
16 #include <lib-ui/menu.h>
17 #include <lib-mx/mx.h>
23 #include "recvattach.h"
26 #include "mutt_idna.h"
29 /* some helper functions to verify that we are exclusively operating
30 * on message/rfc822 attachments
33 static short check_msg (BODY * b, short err)
35 if (!mutt_is_message_type(b)) {
37 mutt_error _("You may only bounce message/rfc822 parts.");
44 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
45 BODY * cur, short err)
49 if (cur && check_msg (cur, err) == -1)
52 for (i = 0; i < idxlen; i++) {
53 if (idx[i]->content->tagged) {
54 if (check_msg (idx[i]->content, err) == -1)
63 /* can we decode all tagged attachments? */
65 static short check_can_decode (ATTACHPTR ** idx, short idxlen, BODY * cur)
70 return mutt_can_decode (cur);
72 for (i = 0; i < idxlen; i++)
73 if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
79 static short count_tagged (ATTACHPTR ** idx, short idxlen)
84 for (i = 0; i < idxlen; i++)
85 if (idx[i]->content->tagged)
91 /* count the number of tagged children below a multipart or message
95 static short count_tagged_children (ATTACHPTR ** idx, short idxlen, short i)
97 short level = idx[i]->level;
100 while ((++i < idxlen) && (level < idx[i]->level))
101 if (idx[i]->content->tagged)
110 ** The bounce function, from the attachment menu
113 void mutt_attach_bounce (FILE * fp, HEADER * hdr __attribute__ ((unused)),
114 ATTACHPTR ** idx, short idxlen, BODY * cur)
118 char buf[HUGE_STRING];
120 address_t *adr = NULL;
124 if (check_all_msg (idx, idxlen, cur, 1) == -1)
127 /* one or more messages? */
128 p = (cur || count_tagged (idx, idxlen) == 1);
131 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
133 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
136 if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
140 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
141 mutt_error _("Error parsing address!");
146 adr = mutt_expand_aliases (adr);
148 if (mutt_addrlist_to_idna (adr, &err) < 0) {
149 mutt_error (_("Bad IDN: '%s'"), err);
151 address_list_wipe(&adr);
156 rfc822_addrcat(buf, sizeof(buf), adr, 1);
158 #define extra_space (15+7+2)
162 snprintf (prompt, sizeof (prompt) - 4,
163 (p ? _("Bounce message to %s") : _("Bounce messages to %s")),
166 if (mutt_strwidth (prompt) > COLS - extra_space) {
167 mutt_format_string (prompt, sizeof (prompt) - 4,
168 0, COLS - extra_space, 0, 0,
169 prompt, sizeof (prompt), 0);
170 m_strcat(prompt, sizeof(prompt), "...?");
172 m_strcat(prompt, sizeof(prompt), "?");
175 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
176 address_list_wipe(&adr);
177 CLEARLINE (LINES - 1);
178 mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
182 CLEARLINE (LINES - 1);
185 ret = mutt_bounce_message (fp, cur->hdr, adr);
187 for (i = 0; i < idxlen; i++) {
188 if (idx[i]->content->tagged)
189 if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
195 mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
197 mutt_error (p ? _("Error bouncing message!") :
198 _("Error bouncing messages!"));
204 ** resend-message, from the attachment menu
207 void mutt_attach_resend (FILE * fp, HEADER * hdr __attribute__ ((unused)), ATTACHPTR ** idx,
208 short idxlen, BODY * cur)
212 if (check_all_msg (idx, idxlen, cur, 1) == -1)
216 mutt_resend_message (fp, Context, cur->hdr);
218 for (i = 0; i < idxlen; i++)
219 if (idx[i]->content->tagged)
220 mutt_resend_message (fp, Context, idx[i]->content->hdr);
226 ** forward-message, from the attachment menu
229 /* try to find a common parent message for the tagged attachments. */
231 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
237 for (i = 0; i < idxlen; i++)
238 if (idx[i]->content->tagged)
242 if (mutt_is_message_type(idx[i]->content)) {
243 nchildren = count_tagged_children (idx, idxlen, i);
244 if (nchildren == nattach)
245 return idx[i]->content->hdr;
253 * check whether attachment #i is a parent of the attachment
256 * Note: This and the calling procedure could be optimized quite a
257 * bit. For now, it's not worth the effort.
260 static int is_parent (short i, ATTACHPTR ** idx, short idxlen, BODY * cur)
262 short level = idx[i]->level;
264 while ((++i < idxlen) && idx[i]->level > level) {
265 if (idx[i]->content == cur)
272 static HEADER *find_parent (ATTACHPTR ** idx, short idxlen, BODY * cur,
276 HEADER *parent = NULL;
279 for (i = 0; i < idxlen; i++) {
280 if (mutt_is_message_type(idx[i]->content)
281 && is_parent (i, idx, idxlen, cur))
282 parent = idx[i]->content->hdr;
283 if (idx[i]->content == cur)
288 parent = find_common_parent (idx, idxlen, nattach);
293 static void include_header (int quote, FILE * ifp,
294 HEADER * hdr, FILE * ofp, char *_prefix)
296 int chflags = CH_DECODE;
299 if (option (OPTWEED))
300 chflags |= CH_WEED | CH_REORDER;
304 m_strcpy(prefix, sizeof(prefix), _prefix);
305 else if (!option (OPTTEXTFLOWED))
306 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
309 m_strcpy(prefix, sizeof(prefix), ">");
311 chflags |= CH_PREFIX;
314 mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
317 /* create a send-mode duplicate from a receive-mode body */
318 static int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
320 char tmp[_POSIX_PATH_MAX];
323 parameter_t *par, **ppar;
329 m_strcpy(tmp, sizeof(tmp), src->filename);
335 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
336 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
342 memcpy (b, src, sizeof (BODY));
346 b->filename = m_strdup(tmp);
347 b->use_disp = use_disp;
350 if (mutt_is_text_part (b))
353 b->xtype = m_strdup(b->xtype);
354 b->subtype = m_strdup(b->subtype);
355 b->form_name = m_strdup(b->form_name);
356 b->filename = m_strdup(b->filename);
357 b->d_filename = m_strdup(b->d_filename);
358 b->description = m_strdup(b->description);
361 * we don't seem to need the HEADER structure currently.
362 * XXX - this may change in the future
368 /* copy parameters */
369 for (par = b->parameter, ppar = &b->parameter; par;
370 ppar = &(*ppar)->next, par = par->next) {
371 *ppar = parameter_new();
372 (*ppar)->attribute = m_strdup(par->attribute);
373 (*ppar)->value = m_strdup(par->value);
376 mutt_stamp_attachment (b);
381 /* Attach all the body parts which can't be decoded.
382 * This code is shared by forwarding and replying. */
384 static BODY **copy_problematic_attachments (FILE * fp,
387 short idxlen, short force)
391 for (i = 0; i < idxlen; i++) {
392 if (idx[i]->content->tagged &&
393 (force || !mutt_can_decode (idx[i]->content))) {
394 if (mutt_copy_body (fp, last, idx[i]->content) == -1)
395 return NULL; /* XXXXX - may lead to crashes */
396 last = &((*last)->next);
403 * forward one or several MIME bodies
404 * (non-message types)
407 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
408 ATTACHPTR ** idx, short idxlen,
409 BODY * cur, short nattach, int flags)
412 short mime_fwd_all = 0;
413 short mime_fwd_any = 1;
414 HEADER *parent = NULL;
415 HEADER *tmphdr = NULL;
417 char tmpbody[_POSIX_PATH_MAX];
427 * First, find the parent message.
428 * Note: This could be made an option by just
429 * putting the following lines into an if block.
433 parent = find_parent (idx, idxlen, cur, nattach);
439 tmphdr = header_new();
440 tmphdr->env = envelope_new();
441 mutt_make_forward_subject (tmphdr->env, Context, parent);
443 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
445 mutt_error(_("Could not create temporary file"));
449 mutt_forward_intro (tmpfp, parent);
451 /* prepare the prefix here since we'll need it later. */
453 if (option (OPTFORWQUOTE)) {
454 if (!option (OPTTEXTFLOWED))
455 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
458 m_strcpy(prefix, sizeof(prefix), ">");
461 include_header (option (OPTFORWQUOTE), fp, parent, tmpfp, prefix);
465 * Now, we have prepared the first part of the message body: The
466 * original message's header.
468 * The next part is more interesting: either include the message bodies,
472 if ((!cur || mutt_can_decode (cur)) &&
473 (rc = query_quadoption (OPT_MIMEFWD,
474 _("Forward as attachments?"))) == M_YES)
480 * shortcut MIMEFWDREST when there is only one attachment. Is
484 if (!mime_fwd_all && !cur && (nattach > 1)
485 && !check_can_decode (idx, idxlen, cur)) {
486 if ((rc = query_quadoption (OPT_MIMEFWDREST,
488 ("Can't decode all tagged attachments. MIME-forward the others?")))
495 /* initialize a state structure */
499 if (option (OPTFORWQUOTE))
501 st.flags = M_CHARCONV;
502 if (option (OPTWEED))
507 /* where do we append new MIME parts? */
508 last = &tmphdr->content;
511 /* single body case */
513 if (!mime_fwd_all && mutt_can_decode (cur)) {
514 mutt_body_handler (cur, &st);
515 state_putc ('\n', &st);
518 if (mutt_copy_body (fp, last, cur) == -1)
520 last = &((*last)->next);
524 /* multiple body case */
527 for (i = 0; i < idxlen; i++) {
528 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
529 mutt_body_handler (idx[i]->content, &st);
530 state_putc ('\n', &st);
537 copy_problematic_attachments (fp, last, idx, idxlen,
538 mime_fwd_all)) == NULL)
542 mutt_forward_trailer (tmpfp);
545 /* now that we have the template, send it. */
546 ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
551 mutt_unlink(tmpbody);
552 header_delete(&tmphdr);
557 * Forward one or several message-type attachments. This
558 * is different from the previous function
559 * since we want to mimic the index menu's behaviour.
561 * Code reuse from ci_send_message is not possible here -
562 * ci_send_message relies on a context structure to find messages,
563 * while, on the attachment menu, messages are referenced through
564 * the attachment index.
567 static void attach_forward_msgs (FILE * fp, HEADER * hdr __attribute__ ((unused)),
568 ATTACHPTR ** idx, short idxlen, BODY * cur,
571 HEADER *curhdr = NULL;
577 char tmpbody[_POSIX_PATH_MAX];
581 int chflags = CH_XMIT;
586 for (i = 0; i < idxlen; i++)
587 if (idx[i]->content->tagged) {
588 curhdr = idx[i]->content->hdr;
593 tmphdr = header_new();
594 tmphdr->env = envelope_new();
595 mutt_make_forward_subject (tmphdr->env, Context, curhdr);
600 if ((rc = query_quadoption (OPT_MIMEFWD,
601 _("Forward MIME encapsulated?"))) == M_NO) {
603 /* no MIME encapsulation */
605 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
607 mutt_error(_("Could not create temporary file"));
608 header_delete(&tmphdr);
612 if (option (OPTFORWQUOTE)) {
613 chflags |= CH_PREFIX;
614 cmflags |= M_CM_PREFIX;
617 if (option (OPTFORWDECODE)) {
618 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
619 if (option (OPTWEED)) {
620 chflags |= CH_WEED | CH_REORDER;
621 cmflags |= M_CM_WEED;
627 /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */
628 mutt_forward_intro (tmpfp, cur->hdr);
629 _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags,
631 mutt_forward_trailer (tmpfp);
634 for (i = 0; i < idxlen; i++) {
635 if (idx[i]->content->tagged) {
636 /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */
637 mutt_forward_intro (tmpfp, idx[i]->content->hdr);
638 _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
639 idx[i]->content->hdr->content, cmflags,
641 mutt_forward_trailer (tmpfp);
647 else if (rc == M_YES) { /* do MIME encapsulation - we don't need to do much here */
648 last = &tmphdr->content;
650 mutt_copy_body (fp, last, cur);
652 for (i = 0; i < idxlen; i++)
653 if (idx[i]->content->tagged) {
654 mutt_copy_body (fp, last, idx[i]->content);
655 last = &((*last)->next);
660 header_delete(&tmphdr);
662 ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, NULL, curhdr);
666 void mutt_attach_forward (FILE * fp, HEADER * hdr,
667 ATTACHPTR ** idx, short idxlen, BODY * cur,
673 if (check_all_msg (idx, idxlen, cur, 0) == 0)
674 attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
676 nattach = count_tagged (idx, idxlen);
677 attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
684 ** the various reply functions, from the attachment menu
687 /* Create the envelope defaults for a reply.
689 * This function can be invoked in two ways.
691 * Either, parent is NULL. In this case, all tagged bodies are of a message type,
692 * and the header information is fetched from them.
694 * Or, parent is non-NULL. In this case, cur is the common parent of all the
695 * tagged attachments.
697 * Note that this code is horribly similar to envelope_defaults () from send.c.
701 attach_reply_envelope_defaults (ENVELOPE * env, ATTACHPTR ** idx,
702 short idxlen, HEADER * parent, int flags)
704 ENVELOPE *curenv = NULL;
705 HEADER *curhdr = NULL;
709 for (i = 0; i < idxlen; i++) {
710 if (idx[i]->content->tagged) {
711 curhdr = idx[i]->content->hdr;
712 curenv = curhdr->env;
718 curenv = parent->env;
722 if (curenv == NULL || curhdr == NULL) {
723 mutt_error _("Can't find any tagged messages.");
729 if ((flags & SENDNEWS)) {
730 /* in case followup set Newsgroups: with Followup-To: if it present */
731 if (!env->newsgroups && curenv &&
732 m_strcasecmp(curenv->followup_to, "poster"))
733 env->newsgroups = m_strdup(curenv->followup_to);
739 if (mutt_fetch_recips (env, curenv, flags) == -1)
743 for (i = 0; i < idxlen; i++) {
744 if (idx[i]->content->tagged
745 && mutt_fetch_recips (env, idx[i]->content->hdr->env,
751 if ((flags & SENDLISTREPLY) && !env->to) {
752 mutt_error _("No mailing lists found!");
757 mutt_fix_reply_recipients (env);
759 mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
762 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
764 string_list_t **p = NULL, **q = NULL;
766 for (i = 0; i < idxlen; i++) {
767 if (idx[i]->content->tagged)
768 mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p,
777 /* This is _very_ similar to send.c's include_reply(). */
779 static void attach_include_reply (FILE * fp, FILE * tmpfp, HEADER * cur,
780 int flags __attribute__ ((unused)))
782 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
783 int chflags = CH_DECODE;
785 /* mutt_message_hook (cur, M_MESSAGEHOOK); */
787 mutt_make_attribution (Context, cur, tmpfp);
789 if (!option (OPTHEADER))
790 cmflags |= M_CM_NOHEADER;
791 if (option (OPTWEED)) {
793 cmflags |= M_CM_WEED;
796 _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
797 mutt_make_post_indent (Context, cur, tmpfp);
800 void mutt_attach_reply (FILE * fp, HEADER * hdr,
801 ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
803 short mime_reply_any = 0;
806 HEADER *parent = NULL;
807 HEADER *tmphdr = NULL;
811 char tmpbody[_POSIX_PATH_MAX];
818 if (flags & SENDNEWS)
819 set_option (OPTNEWSSEND);
821 unset_option (OPTNEWSSEND);
824 if (check_all_msg (idx, idxlen, cur, 0) == -1) {
825 nattach = count_tagged (idx, idxlen);
826 if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
830 if (nattach > 1 && !check_can_decode (idx, idxlen, cur)) {
831 if ((rc = query_quadoption (OPT_MIMEFWDREST,
833 ("Can't decode all tagged attachments. MIME-encapsulate the others?")))
836 else if (rc == M_YES)
839 else if (nattach == 1)
842 tmphdr = header_new();
843 tmphdr->env = envelope_new();
845 if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
846 parent ? parent : (cur ? cur->
849 header_delete(&tmphdr);
853 tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(Tempdir), NULL);
855 mutt_error(_("Could not create temporary file"));
856 header_delete(&tmphdr);
862 attach_include_reply (fp, tmpfp, cur->hdr, flags);
864 for (i = 0; i < idxlen; i++) {
865 if (idx[i]->content->tagged)
866 attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
871 mutt_make_attribution (Context, parent, tmpfp);
877 if (!option (OPTTEXTFLOWED))
878 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
881 m_strcpy(prefix, sizeof(prefix), ">");
884 st.flags = M_CHARCONV;
886 if (option (OPTWEED))
889 if (option (OPTHEADER))
890 include_header (1, fp, parent, tmpfp, prefix);
893 if (mutt_can_decode (cur)) {
894 mutt_body_handler (cur, &st);
895 state_putc ('\n', &st);
898 mutt_copy_body (fp, &tmphdr->content, cur);
901 for (i = 0; i < idxlen; i++) {
902 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
903 mutt_body_handler (idx[i]->content, &st);
904 state_putc ('\n', &st);
909 mutt_make_post_indent (Context, parent, tmpfp);
911 if (mime_reply_any && !cur &&
912 copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
914 header_delete(&tmphdr);
922 if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
923 mutt_set_flag (Context, hdr, M_REPLIED, 1);