4ab7a87e36fda1ca6ff0829cb543337daf0a3804
[apps/madmutt.git] / recvcmd.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4  *
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.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include <lib-mime/mime.h>
13
14 #include <lib-ui/curses.h>
15 #include <lib-ui/enter.h>
16 #include <lib-ui/menu.h>
17 #include <lib-mx/mx.h>
18
19 #include "mutt.h"
20 #include "alias.h"
21 #include "state.h"
22 #include "handler.h"
23 #include "recvattach.h"
24 #include "attach.h"
25 #include "copy.h"
26 #include "mutt_idna.h"
27
28
29 /* some helper functions to verify that we are exclusively operating
30  * on message/rfc822 attachments
31  */
32
33 static short check_msg (BODY * b, short err)
34 {
35   if (!mutt_is_message_type(b)) {
36     if (err)
37       mutt_error _("You may only bounce message/rfc822 parts.");
38
39     return -1;
40   }
41   return 0;
42 }
43
44 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
45                             BODY * cur, short err)
46 {
47   short i;
48
49   if (cur && check_msg (cur, err) == -1)
50     return -1;
51   else if (!cur) {
52     for (i = 0; i < idxlen; i++) {
53       if (idx[i]->content->tagged) {
54         if (check_msg (idx[i]->content, err) == -1)
55           return -1;
56       }
57     }
58   }
59   return 0;
60 }
61
62
63 /* can we decode all tagged attachments? */
64
65 static short check_can_decode (ATTACHPTR ** idx, short idxlen, BODY * cur)
66 {
67   short i;
68
69   if (cur)
70     return mutt_can_decode (cur);
71
72   for (i = 0; i < idxlen; i++)
73     if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
74       return 0;
75
76   return 1;
77 }
78
79 static short count_tagged (ATTACHPTR ** idx, short idxlen)
80 {
81   short count = 0;
82   short i;
83
84   for (i = 0; i < idxlen; i++)
85     if (idx[i]->content->tagged)
86       count++;
87
88   return count;
89 }
90
91 /* count the number of tagged children below a multipart or message
92  * attachment.
93  */
94
95 static short count_tagged_children (ATTACHPTR ** idx, short idxlen, short i)
96 {
97   short level = idx[i]->level;
98   short count = 0;
99
100   while ((++i < idxlen) && (level < idx[i]->level))
101     if (idx[i]->content->tagged)
102       count++;
103
104   return count;
105 }
106 \f
107
108
109 /**
110  ** The bounce function, from the attachment menu
111  **/
112
113 void mutt_attach_bounce (FILE * fp, HEADER * hdr __attribute__ ((unused)),
114                          ATTACHPTR ** idx, short idxlen, BODY * cur)
115 {
116   short i;
117   char prompt[STRING];
118   char buf[HUGE_STRING];
119   char *err = NULL;
120   address_t *adr = NULL;
121   int ret = 0;
122   int p = 0;
123
124   if (check_all_msg (idx, idxlen, cur, 1) == -1)
125     return;
126
127   /* one or more messages? */
128   p = (cur || count_tagged (idx, idxlen) == 1);
129
130   if (p)
131     m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
132   else
133     m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
134
135   buf[0] = '\0';
136   if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS)
137       || buf[0] == '\0')
138     return;
139
140   if (!(adr = rfc822_parse_adrlist (adr, buf))) {
141     mutt_error _("Error parsing address!");
142
143     return;
144   }
145
146   adr = mutt_expand_aliases (adr);
147
148   if (mutt_addrlist_to_idna (adr, &err) < 0) {
149     mutt_error (_("Bad IDN: '%s'"), err);
150     p_delete(&err);
151     address_list_wipe(&adr);
152     return;
153   }
154
155   buf[0] = 0;
156   rfc822_addrcat(buf, sizeof(buf), adr, 1);
157
158 #define extra_space (15+7+2)
159   /*
160    * See commands.c.
161    */
162   snprintf (prompt, sizeof (prompt) - 4,
163             (p ? _("Bounce message to %s") : _("Bounce messages to %s")),
164             buf);
165
166   if (m_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), "...?");
171   } else {
172     m_strcat(prompt, sizeof(prompt), "?");
173   }
174
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."));
179     return;
180   }
181
182   CLEARLINE (LINES - 1);
183
184   if (cur)
185     ret = mutt_bounce_message (fp, cur->hdr, adr);
186   else {
187     for (i = 0; i < idxlen; i++) {
188       if (idx[i]->content->tagged)
189         if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
190           ret = 1;
191     }
192   }
193
194   if (!ret)
195     mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
196   else
197     mutt_error (p ? _("Error bouncing message!") :
198                 _("Error bouncing messages!"));
199 }
200 \f
201
202
203 /**
204  ** resend-message, from the attachment menu 
205  **/
206
207 void mutt_attach_resend (FILE * fp, HEADER * hdr __attribute__ ((unused)), ATTACHPTR ** idx,
208                          short idxlen, BODY * cur)
209 {
210   short i;
211
212   if (check_all_msg (idx, idxlen, cur, 1) == -1)
213     return;
214
215   if (cur)
216     mutt_resend_message (fp, Context, cur->hdr);
217   else {
218     for (i = 0; i < idxlen; i++)
219       if (idx[i]->content->tagged)
220         mutt_resend_message (fp, Context, idx[i]->content->hdr);
221   }
222 }
223 \f
224
225 /**
226  ** forward-message, from the attachment menu 
227  **/
228
229 /* try to find a common parent message for the tagged attachments. */
230
231 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
232                                    short nattach)
233 {
234   short i;
235   short nchildren;
236
237   for (i = 0; i < idxlen; i++)
238     if (idx[i]->content->tagged)
239       break;
240
241   while (--i >= 0) {
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;
246     }
247   }
248
249   return NULL;
250 }
251
252 /* 
253  * check whether attachment #i is a parent of the attachment
254  * pointed to by cur
255  * 
256  * Note: This and the calling procedure could be optimized quite a 
257  * bit.  For now, it's not worth the effort.
258  */
259
260 static int is_parent (short i, ATTACHPTR ** idx, short idxlen, BODY * cur)
261 {
262   short level = idx[i]->level;
263
264   while ((++i < idxlen) && idx[i]->level > level) {
265     if (idx[i]->content == cur)
266       return 1;
267   }
268
269   return 0;
270 }
271
272 static HEADER *find_parent (ATTACHPTR ** idx, short idxlen, BODY * cur,
273                             short nattach)
274 {
275   short i;
276   HEADER *parent = NULL;
277
278   if (cur) {
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)
284         break;
285     }
286   }
287   else if (nattach)
288     parent = find_common_parent (idx, idxlen, nattach);
289
290   return parent;
291 }
292
293 static void include_header (int quote, FILE * ifp,
294                             HEADER * hdr, FILE * ofp, char *_prefix)
295 {
296   int chflags = CH_DECODE;
297   char prefix[STRING];
298
299   if (option (OPTWEED))
300     chflags |= CH_WEED | CH_REORDER;
301
302   if (quote) {
303     if (_prefix)
304       m_strcpy(prefix, sizeof(prefix), _prefix);
305     else if (!option (OPTTEXTFLOWED))
306       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
307                          Context, hdr, 0);
308     else
309       m_strcpy(prefix, sizeof(prefix), ">");
310
311     chflags |= CH_PREFIX;
312   }
313
314   mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
315 }
316
317 /* create a send-mode duplicate from a receive-mode body */
318 static int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
319 {
320   char tmp[_POSIX_PATH_MAX];
321   BODY *b;
322
323   parameter_t *par, **ppar;
324
325   short use_disp;
326
327   if (src->filename) {
328     use_disp = 1;
329     m_strcpy(tmp, sizeof(tmp), src->filename);
330   } else {
331     use_disp = 0;
332     tmp[0] = '\0';
333   }
334
335   mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
336   if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
337     return -1;
338
339   *tgt = body_new();
340   b = *tgt;
341
342   memcpy (b, src, sizeof (BODY));
343   b->parts = NULL;
344   b->next = NULL;
345
346   b->filename = m_strdup(tmp);
347   b->use_disp = use_disp;
348   b->unlink = 1;
349
350   if (mutt_is_text_part (b))
351     b->noconv = 1;
352
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);
359
360   /* 
361    * we don't seem to need the HEADER structure currently.
362    * XXX - this may change in the future
363    */
364
365   if (b->hdr)
366     b->hdr = NULL;
367
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);
374   }
375
376   mutt_stamp_attachment (b);
377
378   return 0;
379 }
380
381 /* Attach all the body parts which can't be decoded. 
382  * This code is shared by forwarding and replying. */
383
384 static BODY **copy_problematic_attachments (FILE * fp,
385                                             BODY ** last,
386                                             ATTACHPTR ** idx,
387                                             short idxlen, short force)
388 {
389   short i;
390
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);
397     }
398   }
399   return last;
400 }
401
402 /* 
403  * forward one or several MIME bodies 
404  * (non-message types)
405  */
406
407 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
408                                    ATTACHPTR ** idx, short idxlen,
409                                    BODY * cur, short nattach, int flags)
410 {
411   short i;
412   short mime_fwd_all = 0;
413   short mime_fwd_any = 1;
414   HEADER *parent = NULL;
415   HEADER *tmphdr = NULL;
416   BODY **last;
417   char tmpbody[_POSIX_PATH_MAX];
418   FILE *tmpfp = NULL;
419
420   char prefix[STRING];
421
422   int rc = 0;
423
424   STATE st;
425
426   /* 
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.
430    */
431
432
433   parent = find_parent (idx, idxlen, cur, nattach);
434
435   if (parent == NULL)
436     parent = hdr;
437
438
439   tmphdr = header_new();
440   tmphdr->env = envelope_new();
441   mutt_make_forward_subject (tmphdr->env, Context, parent);
442
443   tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(MCore.tmpdir), NULL);
444   if (!tmpfp) {
445     mutt_error(_("Could not create temporary file"));
446     return;
447   }
448
449   mutt_forward_intro (tmpfp, parent);
450
451   /* prepare the prefix here since we'll need it later. */
452
453   if (option (OPTFORWQUOTE)) {
454     if (!option (OPTTEXTFLOWED))
455       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
456                          parent, 0);
457     else
458       m_strcpy(prefix, sizeof(prefix), ">");
459   }
460
461   include_header (option (OPTFORWQUOTE), fp, parent, tmpfp, prefix);
462
463
464   /* 
465    * Now, we have prepared the first part of the message body: The
466    * original message's header. 
467    *
468    * The next part is more interesting: either include the message bodies,
469    * or attach them.
470    */
471
472   if ((!cur || mutt_can_decode (cur)) &&
473       (rc = query_quadoption (OPT_MIMEFWD,
474                               _("Forward as attachments?"))) == M_YES)
475     mime_fwd_all = 1;
476   else if (rc == -1)
477     goto bail;
478
479   /* 
480    * shortcut MIMEFWDREST when there is only one attachment.  Is 
481    * this intuitive?
482    */
483
484   if (!mime_fwd_all && !cur && (nattach > 1)
485       && !check_can_decode (idx, idxlen, cur)) {
486     if ((rc = query_quadoption (OPT_MIMEFWDREST,
487                                 _
488                                 ("Can't decode all tagged attachments.  MIME-forward the others?")))
489         == -1)
490       goto bail;
491     else if (rc == M_NO)
492       mime_fwd_any = 0;
493   }
494
495   /* initialize a state structure */
496
497   p_clear(&st, 1);
498
499   if (option (OPTFORWQUOTE))
500     st.prefix = prefix;
501   st.flags = M_CHARCONV;
502   if (option (OPTWEED))
503     st.flags |= M_WEED;
504   st.fpin = fp;
505   st.fpout = tmpfp;
506
507   /* where do we append new MIME parts? */
508   last = &tmphdr->content;
509
510   if (cur) {
511     /* single body case */
512
513     if (!mime_fwd_all && mutt_can_decode (cur)) {
514       mutt_body_handler (cur, &st);
515       state_putc ('\n', &st);
516     }
517     else {
518       if (mutt_copy_body (fp, last, cur) == -1)
519         goto bail;
520       last = &((*last)->next);
521     }
522   }
523   else {
524     /* multiple body case */
525
526     if (!mime_fwd_all) {
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);
531         }
532       }
533     }
534
535     if (mime_fwd_any &&
536         (last =
537          copy_problematic_attachments (fp, last, idx, idxlen,
538                                        mime_fwd_all)) == NULL)
539       goto bail;
540   }
541
542   mutt_forward_trailer (tmpfp);
543   m_fclose(&tmpfp);
544
545   /* now that we have the template, send it. */
546   ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
547   return;
548
549 bail:
550   m_fclose(&tmpfp);
551   mutt_unlink(tmpbody);
552   header_delete(&tmphdr);
553 }
554
555
556 /* 
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.
560  *
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. 
565  */
566
567 static void attach_forward_msgs (FILE * fp, HEADER * hdr __attribute__ ((unused)),
568                                  ATTACHPTR ** idx, short idxlen, BODY * cur,
569                                  int flags)
570 {
571   HEADER *curhdr = NULL;
572   HEADER *tmphdr;
573   short i;
574   int rc;
575
576   BODY **last;
577   char tmpbody[_POSIX_PATH_MAX];
578   FILE *tmpfp = NULL;
579
580   int cmflags = 0;
581   int chflags = CH_XMIT;
582
583   if (cur)
584     curhdr = cur->hdr;
585   else {
586     for (i = 0; i < idxlen; i++)
587       if (idx[i]->content->tagged) {
588         curhdr = idx[i]->content->hdr;
589         break;
590       }
591   }
592
593   tmphdr = header_new();
594   tmphdr->env = envelope_new();
595   mutt_make_forward_subject (tmphdr->env, Context, curhdr);
596
597
598   tmpbody[0] = '\0';
599
600   if ((rc = query_quadoption (OPT_MIMEFWD,
601                               _("Forward MIME encapsulated?"))) == M_NO) {
602
603     /* no MIME encapsulation */
604
605     tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(MCore.tmpdir), NULL);
606     if (!tmpfp) {
607       mutt_error(_("Could not create temporary file"));
608       header_delete(&tmphdr);
609       return;
610     }
611
612     if (option (OPTFORWQUOTE)) {
613       chflags |= CH_PREFIX;
614       cmflags |= M_CM_PREFIX;
615     }
616
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;
622       }
623     }
624
625
626     if (cur) {
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,
630                           chflags);
631       mutt_forward_trailer (tmpfp);
632     }
633     else {
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,
640                               chflags);
641           mutt_forward_trailer (tmpfp);
642         }
643       }
644     }
645     m_fclose(&tmpfp);
646   }
647   else if (rc == M_YES) {       /* do MIME encapsulation - we don't need to do much here */
648     last = &tmphdr->content;
649     if (cur)
650       mutt_copy_body (fp, last, cur);
651     else {
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);
656         }
657     }
658   }
659   else
660     header_delete(&tmphdr);
661
662   ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, NULL, curhdr);
663
664 }
665
666 void mutt_attach_forward (FILE * fp, HEADER * hdr,
667                           ATTACHPTR ** idx, short idxlen, BODY * cur,
668                           int flags)
669 {
670   short nattach;
671
672
673   if (check_all_msg (idx, idxlen, cur, 0) == 0)
674     attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
675   else {
676     nattach = count_tagged (idx, idxlen);
677     attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
678   }
679 }
680 \f
681
682
683 /**
684  ** the various reply functions, from the attachment menu
685  **/
686
687 /* Create the envelope defaults for a reply.
688  *
689  * This function can be invoked in two ways.
690  * 
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.
693  * 
694  * Or, parent is non-NULL.  In this case, cur is the common parent of all the
695  * tagged attachments.
696  * 
697  * Note that this code is horribly similar to envelope_defaults () from send.c.
698  */
699
700 static int
701 attach_reply_envelope_defaults (ENVELOPE * env, ATTACHPTR ** idx,
702                                 short idxlen, HEADER * parent, int flags)
703 {
704   ENVELOPE *curenv = NULL;
705   HEADER *curhdr = NULL;
706   short i;
707
708   if (!parent) {
709     for (i = 0; i < idxlen; i++) {
710       if (idx[i]->content->tagged) {
711         curhdr = idx[i]->content->hdr;
712         curenv = curhdr->env;
713         break;
714       }
715     }
716   }
717   else {
718     curenv = parent->env;
719     curhdr = parent;
720   }
721
722   if (curenv == NULL || curhdr == NULL) {
723     mutt_error _("Can't find any tagged messages.");
724
725     return -1;
726   }
727
728 #ifdef USE_NNTP
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);
734   }
735   else
736 #endif
737   {
738     if (parent) {
739       if (mutt_fetch_recips (env, curenv, flags) == -1)
740         return -1;
741     }
742     else {
743       for (i = 0; i < idxlen; i++) {
744         if (idx[i]->content->tagged
745             && mutt_fetch_recips (env, idx[i]->content->hdr->env,
746                                   flags) == -1)
747           return -1;
748       }
749     }
750
751     if ((flags & SENDLISTREPLY) && !env->to) {
752       mutt_error _("No mailing lists found!");
753
754       return (-1);
755     }
756
757     mutt_fix_reply_recipients (env);
758   }
759   mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
760
761   if (parent)
762     mutt_add_to_reference_headers (env, curenv, NULL, NULL);
763   else {
764     string_list_t **p = NULL, **q = NULL;
765
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,
769                                        &q);
770     }
771   }
772
773   return 0;
774 }
775
776
777 /*  This is _very_ similar to send.c's include_reply(). */
778
779 static void attach_include_reply (FILE * fp, FILE * tmpfp, HEADER * cur,
780                                   int flags __attribute__ ((unused)))
781 {
782   int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
783   int chflags = CH_DECODE;
784
785   /* mutt_message_hook (cur, M_MESSAGEHOOK); */
786
787   mutt_make_attribution (Context, cur, tmpfp);
788
789   if (!option (OPTHEADER))
790     cmflags |= M_CM_NOHEADER;
791   if (option (OPTWEED)) {
792     chflags |= CH_WEED;
793     cmflags |= M_CM_WEED;
794   }
795
796   _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
797   mutt_make_post_indent (Context, cur, tmpfp);
798 }
799
800 void mutt_attach_reply (FILE * fp, HEADER * hdr,
801                         ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
802 {
803   short mime_reply_any = 0;
804
805   short nattach = 0;
806   HEADER *parent = NULL;
807   HEADER *tmphdr = NULL;
808   short i;
809
810   STATE st;
811   char tmpbody[_POSIX_PATH_MAX];
812   FILE *tmpfp;
813
814   char prefix[STRING];
815   int rc;
816
817 #ifdef USE_NNTP
818   if (flags & SENDNEWS)
819     set_option (OPTNEWSSEND);
820   else
821     unset_option (OPTNEWSSEND);
822 #endif
823
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)
827       parent = hdr;
828   }
829
830   if (nattach > 1 && !check_can_decode (idx, idxlen, cur)) {
831     if ((rc = query_quadoption (OPT_MIMEFWDREST,
832                                 _
833                                 ("Can't decode all tagged attachments.  MIME-encapsulate the others?")))
834         == -1)
835       return;
836     else if (rc == M_YES)
837       mime_reply_any = 1;
838   }
839   else if (nattach == 1)
840     mime_reply_any = 1;
841
842   tmphdr = header_new();
843   tmphdr->env = envelope_new();
844
845   if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen,
846                                       parent ? parent : (cur ? cur->
847                                                          hdr : NULL),
848                                       flags) == -1) {
849     header_delete(&tmphdr);
850     return;
851   }
852
853   tmpfp = m_tempfile(tmpbody, sizeof(tmpbody), NONULL(MCore.tmpdir), NULL);
854   if (!tmpfp) {
855     mutt_error(_("Could not create temporary file"));
856     header_delete(&tmphdr);
857     return;
858   }
859
860   if (!parent) {
861     if (cur)
862       attach_include_reply (fp, tmpfp, cur->hdr, flags);
863     else {
864       for (i = 0; i < idxlen; i++) {
865         if (idx[i]->content->tagged)
866           attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
867       }
868     }
869   }
870   else {
871     mutt_make_attribution (Context, parent, tmpfp);
872
873     p_clear(&st, 1);
874     st.fpin = fp;
875     st.fpout = tmpfp;
876
877     if (!option (OPTTEXTFLOWED))
878       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix),
879                          Context, parent, 0);
880     else
881       m_strcpy(prefix, sizeof(prefix), ">");
882
883     st.prefix = prefix;
884     st.flags = M_CHARCONV;
885
886     if (option (OPTWEED))
887       st.flags |= M_WEED;
888
889     if (option (OPTHEADER))
890       include_header (1, fp, parent, tmpfp, prefix);
891
892     if (cur) {
893       if (mutt_can_decode (cur)) {
894         mutt_body_handler (cur, &st);
895         state_putc ('\n', &st);
896       }
897       else
898         mutt_copy_body (fp, &tmphdr->content, cur);
899     }
900     else {
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);
905         }
906       }
907     }
908
909     mutt_make_post_indent (Context, parent, tmpfp);
910
911     if (mime_reply_any && !cur &&
912         copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen,
913                                       0) == NULL) {
914       header_delete(&tmphdr);
915       m_fclose(&tmpfp);
916       return;
917     }
918   }
919
920   m_fclose(&tmpfp);
921
922   if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
923     mutt_set_flag (Context, hdr, M_REPLIED, 1);
924 }