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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/file.h>
18 #include <lib-lib/mapping.h>
20 #include <lib-mime/mime.h>
22 #include <lib-ui/curses.h>
23 #include <lib-ui/enter.h>
24 #include <lib-ui/menu.h>
30 #include "recvattach.h"
34 #include "mutt_idna.h"
37 /* some helper functions to verify that we are exclusively operating
38 * on message/rfc822 attachments
41 static short check_msg (BODY * b, short err)
43 if (!mutt_is_message_type (b->type, b->subtype)) {
45 mutt_error _("You may only bounce message/rfc822 parts.");
52 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
53 BODY * cur, short err)
57 if (cur && check_msg (cur, err) == -1)
60 for (i = 0; i < idxlen; i++) {
61 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, BODY * cur)
78 return mutt_can_decode (cur);
80 for (i = 0; i < idxlen; i++)
81 if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
87 static short count_tagged (ATTACHPTR ** idx, short idxlen)
92 for (i = 0; i < idxlen; i++)
93 if (idx[i]->content->tagged)
99 /* count the number of tagged children below a multipart or message
103 static short count_tagged_children (ATTACHPTR ** idx, short idxlen, short i)
105 short level = idx[i]->level;
108 while ((++i < idxlen) && (level < idx[i]->level))
109 if (idx[i]->content->tagged)
118 ** The bounce function, from the attachment menu
121 void mutt_attach_bounce (FILE * fp, HEADER * hdr __attribute__ ((unused)),
122 ATTACHPTR ** idx, short idxlen, BODY * cur)
126 char buf[HUGE_STRING];
128 address_t *adr = NULL;
132 if (check_all_msg (idx, idxlen, cur, 1) == -1)
135 /* one or more messages? */
136 p = (cur || count_tagged (idx, idxlen) == 1);
139 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
141 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
144 if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
148 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
149 mutt_error _("Error parsing address!");
154 adr = mutt_expand_aliases (adr);
156 if (mutt_addrlist_to_idna (adr, &err) < 0) {
157 mutt_error (_("Bad IDN: '%s'"), err);
159 address_list_wipe(&adr);
164 rfc822_write_address (buf, sizeof (buf), adr, 1);
166 #define extra_space (15+7+2)
170 snprintf (prompt, sizeof (prompt) - 4,
171 (p ? _("Bounce message to %s") : _("Bounce messages to %s")),
174 if (mutt_strwidth (prompt) > COLS - extra_space) {
175 mutt_format_string (prompt, sizeof (prompt) - 4,
176 0, COLS - extra_space, 0, 0,
177 prompt, sizeof (prompt), 0);
178 m_strcat(prompt, sizeof(prompt), "...?");
180 m_strcat(prompt, sizeof(prompt), "?");
183 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
184 address_list_wipe(&adr);
185 CLEARLINE (LINES - 1);
186 mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
190 CLEARLINE (LINES - 1);
193 ret = mutt_bounce_message (fp, cur->hdr, adr);
195 for (i = 0; i < idxlen; i++) {
196 if (idx[i]->content->tagged)
197 if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
203 mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
205 mutt_error (p ? _("Error bouncing message!") :
206 _("Error bouncing messages!"));
212 ** resend-message, from the attachment menu
215 void mutt_attach_resend (FILE * fp, HEADER * hdr __attribute__ ((unused)), ATTACHPTR ** idx,
216 short idxlen, BODY * cur)
220 if (check_all_msg (idx, idxlen, cur, 1) == -1)
224 mutt_resend_message (fp, Context, cur->hdr);
226 for (i = 0; i < idxlen; i++)
227 if (idx[i]->content->tagged)
228 mutt_resend_message (fp, Context, idx[i]->content->hdr);
234 ** forward-message, from the attachment menu
237 /* try to find a common parent message for the tagged attachments. */
239 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
245 for (i = 0; i < idxlen; i++)
246 if (idx[i]->content->tagged)
250 if (mutt_is_message_type
251 (idx[i]->content->type, idx[i]->content->subtype)) {
252 nchildren = count_tagged_children (idx, idxlen, i);
253 if (nchildren == nattach)
254 return idx[i]->content->hdr;
262 * check whether attachment #i is a parent of the attachment
265 * Note: This and the calling procedure could be optimized quite a
266 * bit. For now, it's not worth the effort.
269 static int is_parent (short i, ATTACHPTR ** idx, short idxlen, BODY * cur)
271 short level = idx[i]->level;
273 while ((++i < idxlen) && idx[i]->level > level) {
274 if (idx[i]->content == cur)
281 static HEADER *find_parent (ATTACHPTR ** idx, short idxlen, BODY * cur,
285 HEADER *parent = NULL;
288 for (i = 0; i < idxlen; i++) {
289 if (mutt_is_message_type
290 (idx[i]->content->type, idx[i]->content->subtype)
291 && is_parent (i, idx, idxlen, cur))
292 parent = idx[i]->content->hdr;
293 if (idx[i]->content == cur)
298 parent = find_common_parent (idx, idxlen, nattach);
303 static void include_header (int quote, FILE * ifp,
304 HEADER * hdr, FILE * ofp, char *_prefix)
306 int chflags = CH_DECODE;
307 char prefix[SHORT_STRING];
309 if (option (OPTWEED))
310 chflags |= CH_WEED | CH_REORDER;
314 m_strcpy(prefix, sizeof(prefix), _prefix);
315 else if (!option (OPTTEXTFLOWED))
316 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
319 m_strcpy(prefix, sizeof(prefix), ">");
321 chflags |= CH_PREFIX;
324 mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
327 /* Attach all the body parts which can't be decoded.
328 * This code is shared by forwarding and replying. */
330 static BODY **copy_problematic_attachments (FILE * fp,
333 short idxlen, short force)
337 for (i = 0; i < idxlen; i++) {
338 if (idx[i]->content->tagged &&
339 (force || !mutt_can_decode (idx[i]->content))) {
340 if (mutt_copy_body (fp, last, idx[i]->content) == -1)
341 return NULL; /* XXXXX - may lead to crashes */
342 last = &((*last)->next);
349 * forward one or several MIME bodies
350 * (non-message types)
353 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
354 ATTACHPTR ** idx, short idxlen,
355 BODY * cur, short nattach, int flags)
358 short mime_fwd_all = 0;
359 short mime_fwd_any = 1;
360 HEADER *parent = NULL;
361 HEADER *tmphdr = NULL;
363 char tmpbody[_POSIX_PATH_MAX];
373 * First, find the parent message.
374 * Note: This could be made an option by just
375 * putting the following lines into an if block.
379 parent = find_parent (idx, idxlen, cur, nattach);
385 tmphdr = header_new();
386 tmphdr->env = envelope_new();
387 mutt_make_forward_subject (tmphdr->env, Context, parent);
389 mutt_mktemp (tmpbody);
390 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
391 mutt_error (_("Can't open temporary file %s."), tmpbody);
395 mutt_forward_intro (tmpfp, parent);
397 /* prepare the prefix here since we'll need it later. */
399 if (option (OPTFORWQUOTE)) {
400 if (!option (OPTTEXTFLOWED))
401 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
404 m_strcpy(prefix, sizeof(prefix), ">");
407 include_header (option (OPTFORWQUOTE), fp, parent, tmpfp, prefix);
411 * Now, we have prepared the first part of the message body: The
412 * original message's header.
414 * The next part is more interesting: either include the message bodies,
418 if ((!cur || mutt_can_decode (cur)) &&
419 (rc = query_quadoption (OPT_MIMEFWD,
420 _("Forward as attachments?"))) == M_YES)
426 * shortcut MIMEFWDREST when there is only one attachment. Is
430 if (!mime_fwd_all && !cur && (nattach > 1)
431 && !check_can_decode (idx, idxlen, cur)) {
432 if ((rc = query_quadoption (OPT_MIMEFWDREST,
434 ("Can't decode all tagged attachments. MIME-forward the others?")))
441 /* initialize a state structure */
445 if (option (OPTFORWQUOTE))
447 st.flags = M_CHARCONV;
448 if (option (OPTWEED))
453 /* where do we append new MIME parts? */
454 last = &tmphdr->content;
457 /* single body case */
459 if (!mime_fwd_all && mutt_can_decode (cur)) {
460 mutt_body_handler (cur, &st);
461 state_putc ('\n', &st);
464 if (mutt_copy_body (fp, last, cur) == -1)
466 last = &((*last)->next);
470 /* multiple body case */
473 for (i = 0; i < idxlen; i++) {
474 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
475 mutt_body_handler (idx[i]->content, &st);
476 state_putc ('\n', &st);
483 copy_problematic_attachments (fp, last, idx, idxlen,
484 mime_fwd_all)) == NULL)
488 mutt_forward_trailer (tmpfp);
493 /* now that we have the template, send it. */
494 ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
501 mutt_unlink (tmpbody);
504 header_delete(&tmphdr);
509 * Forward one or several message-type attachments. This
510 * is different from the previous function
511 * since we want to mimic the index menu's behaviour.
513 * Code reuse from ci_send_message is not possible here -
514 * ci_send_message relies on a context structure to find messages,
515 * while, on the attachment menu, messages are referenced through
516 * the attachment index.
519 static void attach_forward_msgs (FILE * fp, HEADER * hdr __attribute__ ((unused)),
520 ATTACHPTR ** idx, short idxlen, BODY * cur,
523 HEADER *curhdr = NULL;
529 char tmpbody[_POSIX_PATH_MAX];
533 int chflags = CH_XMIT;
538 for (i = 0; i < idxlen; i++)
539 if (idx[i]->content->tagged) {
540 curhdr = idx[i]->content->hdr;
545 tmphdr = header_new();
546 tmphdr->env = envelope_new();
547 mutt_make_forward_subject (tmphdr->env, Context, curhdr);
552 if ((rc = query_quadoption (OPT_MIMEFWD,
553 _("Forward MIME encapsulated?"))) == M_NO) {
555 /* no MIME encapsulation */
557 mutt_mktemp (tmpbody);
558 if (!(tmpfp = safe_fopen (tmpbody, "w"))) {
559 mutt_error (_("Can't create %s."), tmpbody);
560 header_delete(&tmphdr);
564 if (option (OPTFORWQUOTE)) {
565 chflags |= CH_PREFIX;
566 cmflags |= M_CM_PREFIX;
569 if (option (OPTFORWDECODE)) {
570 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
571 if (option (OPTWEED)) {
572 chflags |= CH_WEED | CH_REORDER;
573 cmflags |= M_CM_WEED;
579 /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */
580 mutt_forward_intro (tmpfp, cur->hdr);
581 _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags,
583 mutt_forward_trailer (tmpfp);
586 for (i = 0; i < idxlen; i++) {
587 if (idx[i]->content->tagged) {
588 /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */
589 mutt_forward_intro (tmpfp, idx[i]->content->hdr);
590 _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
591 idx[i]->content->hdr->content, cmflags,
593 mutt_forward_trailer (tmpfp);
599 else if (rc == M_YES) { /* do MIME encapsulation - we don't need to do much here */
600 last = &tmphdr->content;
602 mutt_copy_body (fp, last, cur);
604 for (i = 0; i < idxlen; i++)
605 if (idx[i]->content->tagged) {
606 mutt_copy_body (fp, last, idx[i]->content);
607 last = &((*last)->next);
612 header_delete(&tmphdr);
614 ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, NULL, curhdr);
618 void mutt_attach_forward (FILE * fp, HEADER * hdr,
619 ATTACHPTR ** idx, short idxlen, BODY * cur,
625 if (check_all_msg (idx, idxlen, cur, 0) == 0)
626 attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
628 nattach = count_tagged (idx, idxlen);
629 attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
636 ** the various reply functions, from the attachment menu
639 /* Create the envelope defaults for a reply.
641 * This function can be invoked in two ways.
643 * Either, parent is NULL. In this case, all tagged bodies are of a message type,
644 * and the header information is fetched from them.
646 * Or, parent is non-NULL. In this case, cur is the common parent of all the
647 * tagged attachments.
649 * Note that this code is horribly similar to envelope_defaults () from send.c.
653 attach_reply_envelope_defaults (ENVELOPE * env, ATTACHPTR ** idx,
654 short idxlen, HEADER * parent, int flags)
656 ENVELOPE *curenv = NULL;
657 HEADER *curhdr = NULL;
661 for (i = 0; i < idxlen; i++) {
662 if (idx[i]->content->tagged) {
663 curhdr = idx[i]->content->hdr;
664 curenv = curhdr->env;
670 curenv = parent->env;
674 if (curenv == NULL || curhdr == NULL) {
675 mutt_error _("Can't find any tagged messages.");
681 if ((flags & SENDNEWS)) {
682 /* in case followup set Newsgroups: with Followup-To: if it present */
683 if (!env->newsgroups && curenv &&
684 m_strcasecmp(curenv->followup_to, "poster"))
685 env->newsgroups = m_strdup(curenv->followup_to);
691 if (mutt_fetch_recips (env, curenv, flags) == -1)
695 for (i = 0; i < idxlen; i++) {
696 if (idx[i]->content->tagged
697 && mutt_fetch_recips (env, idx[i]->content->hdr->env,
703 if ((flags & SENDLISTREPLY) && !env->to) {
704 mutt_error _("No mailing lists found!");
709 mutt_fix_reply_recipients (env);
711 mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
714 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
716 string_list_t **p = NULL, **q = NULL;
718 for (i = 0; i < idxlen; i++) {
719 if (idx[i]->content->tagged)
720 mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p,
729 /* This is _very_ similar to send.c's include_reply(). */
731 static void attach_include_reply (FILE * fp, FILE * tmpfp, HEADER * cur,
732 int flags __attribute__ ((unused)))
734 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
735 int chflags = CH_DECODE;
737 /* mutt_message_hook (cur, M_MESSAGEHOOK); */
739 mutt_make_attribution (Context, cur, tmpfp);
741 if (!option (OPTHEADER))
742 cmflags |= M_CM_NOHEADER;
743 if (option (OPTWEED)) {
745 cmflags |= M_CM_WEED;
748 _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
749 mutt_make_post_indent (Context, cur, tmpfp);
752 void mutt_attach_reply (FILE * fp, HEADER * hdr,
753 ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
755 short mime_reply_any = 0;
758 HEADER *parent = NULL;
759 HEADER *tmphdr = NULL;
763 char tmpbody[_POSIX_PATH_MAX];
766 char prefix[SHORT_STRING];
770 if (flags & SENDNEWS)
771 set_option (OPTNEWSSEND);
773 unset_option (OPTNEWSSEND);
776 if (check_all_msg (idx, idxlen, cur, 0) == -1) {
777 nattach = count_tagged (idx, idxlen);
778 if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
782 if (nattach > 1 && !check_can_decode (idx, idxlen, cur)) {
783 if ((rc = query_quadoption (OPT_MIMEFWDREST,
785 ("Can't decode all tagged attachments. MIME-encapsulate the others?")))
788 else if (rc == M_YES)
791 else if (nattach == 1)
794 tmphdr = header_new();
795 tmphdr->env = envelope_new();
797 if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
798 parent ? parent : (cur ? cur->
801 header_delete(&tmphdr);
805 mutt_mktemp (tmpbody);
806 if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL) {
807 mutt_error (_("Can't create %s."), tmpbody);
808 header_delete(&tmphdr);
814 attach_include_reply (fp, tmpfp, cur->hdr, flags);
816 for (i = 0; i < idxlen; i++) {
817 if (idx[i]->content->tagged)
818 attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
823 mutt_make_attribution (Context, parent, tmpfp);
829 if (!option (OPTTEXTFLOWED))
830 _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
833 m_strcpy(prefix, sizeof(prefix), ">");
836 st.flags = M_CHARCONV;
838 if (option (OPTWEED))
841 if (option (OPTHEADER))
842 include_header (1, fp, parent, tmpfp, prefix);
845 if (mutt_can_decode (cur)) {
846 mutt_body_handler (cur, &st);
847 state_putc ('\n', &st);
850 mutt_copy_body (fp, &tmphdr->content, cur);
853 for (i = 0; i < idxlen; i++) {
854 if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content)) {
855 mutt_body_handler (idx[i]->content, &st);
856 state_putc ('\n', &st);
861 mutt_make_post_indent (Context, parent, tmpfp);
863 if (mime_reply_any && !cur &&
864 copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
866 header_delete(&tmphdr);
874 if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
875 mutt_set_flag (Context, hdr, M_REPLIED, 1);