Nico Golde:
[apps/madmutt.git] / recvcmd.c
1 /*
2  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
3  * 
4  *     This program is free software; you can redistribute it
5  *     and/or modify it under the terms of the GNU General Public
6  *     License as published by the Free Software Foundation; either
7  *     version 2 of the License, or (at your option) any later
8  *     version.
9  * 
10  *     This program is distributed in the hope that it will be
11  *     useful, but WITHOUT ANY WARRANTY; without even the implied
12  *     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  *     PURPOSE.  See the GNU General Public License for more
14  *     details.
15  * 
16  *     You should have received a copy of the GNU General Public
17  *     License along with this program; if not, write to the Free
18  *     Software Foundation, Inc., 59 Temple Place - Suite 330,
19  *     Boston, MA  02111, USA. 
20  */
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "mutt.h"
27 #include "mutt_curses.h"
28 #include "mutt_menu.h"
29 #include "rfc1524.h"
30 #include "mime.h"
31 #include "mailbox.h"
32 #include "attach.h"
33 #include "mapping.h"
34 #include "mx.h"
35 #include "copy.h"
36 #include "mutt_idna.h"
37
38 /* some helper functions to verify that we are exclusively operating
39  * on message/rfc822 attachments
40  */
41
42 static short check_msg (BODY * b, short err)
43 {
44   if (!mutt_is_message_type (b->type, b->subtype))
45   {
46     if (err)
47       mutt_error _("You may only bounce message/rfc822 parts.");
48     return -1;
49   }
50   return 0;
51 }
52
53 static short check_all_msg (ATTACHPTR ** idx, short idxlen,
54                             BODY * cur, short err)
55 {
56   short i;
57
58   if (cur && check_msg (cur, err) == -1)
59     return -1;
60   else if (!cur)
61   {
62     for (i = 0; i < idxlen; i++)
63     {
64       if (idx[i]->content->tagged)
65       {
66         if (check_msg (idx[i]->content, err) == -1)
67           return -1;
68       }
69     }
70   }
71   return 0;
72 }
73
74
75 /* can we decode all tagged attachments? */
76
77 static short check_can_decode (ATTACHPTR ** idx, short idxlen, 
78                               BODY * cur)
79 {
80   short i;
81
82   if (cur)
83     return mutt_can_decode (cur);
84
85   for (i = 0; i < idxlen; i++)
86     if (idx[i]->content->tagged && !mutt_can_decode (idx[i]->content))
87       return 0;
88
89   return 1;
90 }
91
92 static short count_tagged (ATTACHPTR **idx, short idxlen)
93 {
94   short count = 0;
95   short i;
96   
97   for (i = 0; i < idxlen; i++)
98     if (idx[i]->content->tagged)
99       count++;
100   
101   return count;
102 }
103
104 /* count the number of tagged children below a multipart or message
105  * attachment.
106  */
107
108 static short count_tagged_children (ATTACHPTR ** idx, 
109                                     short idxlen, short i)
110 {
111   short level = idx[i]->level;
112   short count = 0;
113
114   while ((++i < idxlen) && (level < idx[i]->level))
115     if (idx[i]->content->tagged)
116       count++;
117
118   return count;
119 }
120
121
122 \f
123 /**
124  **
125  ** The bounce function, from the attachment menu
126  **
127  **/
128
129 void mutt_attach_bounce (FILE * fp, HEADER * hdr, 
130            ATTACHPTR ** idx, short idxlen, BODY * cur)
131 {
132   short i;
133   char prompt[STRING];
134   char buf[HUGE_STRING];
135   char *err = NULL;
136   ADDRESS *adr = NULL;
137   int ret = 0;
138   int p   = 0;
139
140   if (check_all_msg (idx, idxlen, cur, 1) == -1)
141     return;
142
143   /* one or more messages? */
144   p = (cur || count_tagged (idx, idxlen) == 1);
145
146   if (p)
147     strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
148   else
149     strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
150
151   buf[0] = '\0';
152   if (mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS) 
153       || buf[0] == '\0')
154     return;
155
156   if (!(adr = rfc822_parse_adrlist (adr, buf)))
157   {
158     mutt_error _("Error parsing address!");
159     return;
160   }
161
162   adr = mutt_expand_aliases (adr);
163   
164   if (mutt_addrlist_to_idna (adr, &err) < 0)
165   {
166     mutt_error (_("Bad IDN: '%s'"), err);
167     FREE (&err);
168     rfc822_free_address (&adr);
169     return;
170   }
171   
172   buf[0] = 0;
173   rfc822_write_address (buf, sizeof (buf), adr, 1);
174
175 #define extra_space (15+7+2)
176   /*
177    * See commands.c.
178    */
179   snprintf (prompt, sizeof (prompt) - 4, 
180    (p ? _("Bounce message to %s") : _("Bounce messages to %s")), buf);
181   
182   if (mutt_strwidth (prompt) > COLS - extra_space)
183   {
184     mutt_format_string (prompt, sizeof (prompt) - 4,
185                         0, COLS-extra_space, 0, 0,
186                         prompt, sizeof (prompt), 0);
187     safe_strcat (prompt, sizeof (prompt), "...?");
188   }
189   else
190     safe_strcat (prompt, sizeof (prompt), "?");
191
192   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES)
193   {
194     rfc822_free_address (&adr);
195     CLEARLINE (LINES - 1);
196     mutt_message (p ? _("Message not bounced.") : _("Messages not bounced."));
197     return;
198   }
199   
200   CLEARLINE (LINES - 1);
201   
202   if (cur)
203     ret = mutt_bounce_message (fp, cur->hdr, adr);
204   else
205   {
206     for (i = 0; i < idxlen; i++)
207     {
208       if (idx[i]->content->tagged)
209         if (mutt_bounce_message (fp, idx[i]->content->hdr, adr))
210           ret = 1;
211     }
212   }
213
214   if (!ret)
215     mutt_message (p ? _("Message bounced.") : _("Messages bounced."));
216   else
217     mutt_error (p ? _("Error bouncing message!") : _("Error bouncing messages!"));
218 }
219
220
221 \f
222 /**
223  **
224  ** resend-message, from the attachment menu 
225  **
226  **
227  **/
228
229 void mutt_attach_resend (FILE * fp, HEADER * hdr, ATTACHPTR ** idx, 
230                          short idxlen, BODY * cur)
231 {
232   short i;
233
234   if (check_all_msg (idx, idxlen, cur, 1) == -1)
235     return;
236
237   if (cur)
238     mutt_resend_message (fp, Context, cur->hdr);
239   else
240   {
241     for (i = 0; i < idxlen; i++)
242       if (idx[i]->content->tagged)
243         mutt_resend_message (fp, Context, idx[i]->content->hdr);
244   }
245 }
246
247 \f
248 /**
249  **
250  ** forward-message, from the attachment menu 
251  **
252  **/
253   
254 /* try to find a common parent message for the tagged attachments. */
255
256 static HEADER *find_common_parent (ATTACHPTR ** idx, short idxlen,
257                                    short nattach)
258 {
259   short i;
260   short nchildren;
261
262   for (i = 0; i < idxlen; i++)
263     if (idx[i]->content->tagged)
264       break;
265   
266   while (--i >= 0)
267   {
268     if (mutt_is_message_type (idx[i]->content->type, idx[i]->content->subtype))
269     {
270       nchildren = count_tagged_children (idx, idxlen, i);
271       if (nchildren == nattach)
272         return idx[i]->content->hdr;
273     }
274   }
275
276   return NULL;
277 }
278
279 /* 
280  * check whether attachment #i is a parent of the attachment
281  * pointed to by cur
282  * 
283  * Note: This and the calling procedure could be optimized quite a 
284  * bit.  For now, it's not worth the effort.
285  */
286
287 static int is_parent (short i, ATTACHPTR **idx, short idxlen, BODY *cur)
288 {
289   short level = idx[i]->level;
290
291   while ((++i < idxlen) && idx[i]->level > level)
292   {
293     if (idx[i]->content == cur)
294       return 1;
295   }
296
297   return 0;
298 }
299
300 static HEADER *find_parent (ATTACHPTR **idx, short idxlen, BODY *cur, short nattach)
301 {
302   short i;
303   HEADER *parent = NULL;
304   
305   if (cur)
306   {
307     for (i = 0; i < idxlen; i++)
308     {
309       if (mutt_is_message_type (idx[i]->content->type, idx[i]->content->subtype) 
310           && is_parent (i, idx, idxlen, cur))
311         parent = idx[i]->content->hdr;
312       if (idx[i]->content == cur)
313         break;
314     }
315   }
316   else if (nattach)
317     parent = find_common_parent (idx, idxlen, nattach);
318   
319   return parent;
320 }
321
322 static void include_header (int quote, FILE * ifp,
323                             HEADER * hdr, FILE * ofp,
324                             char *_prefix)
325 {
326   int chflags = CH_DECODE;
327   char prefix[SHORT_STRING];
328   
329   if (option (OPTWEED))
330     chflags |= CH_WEED | CH_REORDER;
331
332   if (quote)
333   {
334     if (_prefix)
335       strfcpy (prefix, _prefix, sizeof (prefix));
336     else if (!option (OPTTEXTFLOWED))
337       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), 
338                          Context, hdr, 0);
339     else
340       strfcpy (prefix, ">", sizeof (prefix));
341
342     chflags |= CH_PREFIX;
343   }
344   
345   mutt_copy_header (ifp, hdr, ofp, chflags, quote ? prefix : NULL);
346 }
347
348 /* Attach all the body parts which can't be decoded. 
349  * This code is shared by forwarding and replying. */
350
351 static BODY ** copy_problematic_attachments (FILE *fp,
352                                              BODY **last, 
353                                              ATTACHPTR **idx, 
354                                              short idxlen,
355                                              short force)
356 {
357   short i;
358   
359   for (i = 0; i < idxlen; i++)
360   {
361     if (idx[i]->content->tagged && 
362         (force || !mutt_can_decode (idx[i]->content)))
363     {
364       if (mutt_copy_body (fp, last, idx[i]->content) == -1)
365         return NULL;            /* XXXXX - may lead to crashes */
366       last = &((*last)->next);
367     }
368   }
369   return last;
370 }
371
372 /* 
373  * forward one or several MIME bodies 
374  * (non-message types)
375  */
376
377 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
378                                    ATTACHPTR ** idx, short idxlen,
379                                    BODY * cur,
380                                    short nattach, int flags)
381 {
382   short i;
383   short mime_fwd_all = 0;
384   short mime_fwd_any = 1;
385   HEADER *parent = NULL;
386   HEADER *tmphdr = NULL;
387   BODY **last;
388   char tmpbody[_POSIX_PATH_MAX];
389   FILE *tmpfp = NULL;
390
391   char prefix[STRING];
392
393   int rc = 0;
394
395   STATE st;
396
397   /* 
398    * First, find the parent message.
399    * Note: This could be made an option by just
400    * putting the following lines into an if block.
401    */
402
403
404   parent = find_parent (idx, idxlen, cur, nattach);
405   
406   if (parent == NULL)
407     parent = hdr;
408
409
410   tmphdr = mutt_new_header ();
411   tmphdr->env = mutt_new_envelope ();
412   mutt_make_forward_subject (tmphdr->env, Context, parent);
413
414   mutt_mktemp (tmpbody);
415   if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL)
416   {
417     mutt_error (_("Can't open temporary file %s."), tmpbody);
418     return;
419   }
420
421   mutt_forward_intro (tmpfp, parent);
422
423   /* prepare the prefix here since we'll need it later. */
424
425   if (option (OPTFORWQUOTE))
426   {
427     if (!option (OPTTEXTFLOWED))
428       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), Context,
429                          parent, 0);
430     else
431       strfcpy (prefix, ">", sizeof (prefix));
432   }
433     
434   include_header (option (OPTFORWQUOTE), fp, parent,
435                   tmpfp, prefix);
436
437
438   /* 
439    * Now, we have prepared the first part of the message body: The
440    * original message's header. 
441    *
442    * The next part is more interesting: either include the message bodies,
443    * or attach them.
444    */
445
446   if ((!cur || mutt_can_decode (cur)) &&
447       (rc = query_quadoption (OPT_MIMEFWD, 
448                               _("Forward as attachments?"))) == M_YES)
449     mime_fwd_all = 1;
450   else if (rc == -1)
451     goto bail;
452
453   /* 
454    * shortcut MIMEFWDREST when there is only one attachment.  Is 
455    * this intuitive?
456    */
457
458   if (!mime_fwd_all && !cur && (nattach > 1) 
459       && !check_can_decode (idx, idxlen, cur))
460   {
461     if ((rc = query_quadoption (OPT_MIMEFWDREST,
462 _("Can't decode all tagged attachments.  MIME-forward the others?"))) == -1)
463       goto bail;
464     else if (rc == M_NO)
465       mime_fwd_any = 0;
466   }
467
468   /* initialize a state structure */
469   
470   memset (&st, 0, sizeof (st));
471   
472   if (option (OPTFORWQUOTE))
473     st.prefix = prefix;
474   st.flags = M_CHARCONV;
475   if (option (OPTWEED))
476     st.flags |= M_WEED;
477   st.fpin = fp;
478   st.fpout = tmpfp;
479
480   /* where do we append new MIME parts? */
481   last = &tmphdr->content;
482
483   if (cur)
484   {
485     /* single body case */
486
487     if (!mime_fwd_all && mutt_can_decode (cur))
488     {
489       mutt_body_handler (cur, &st);
490       state_putc ('\n', &st);
491     }
492     else
493     {
494       if (mutt_copy_body (fp, last, cur) == -1)
495         goto bail;
496       last = &((*last)->next);
497     }
498   }
499   else
500   {
501     /* multiple body case */
502
503     if (!mime_fwd_all)
504     {
505       for (i = 0; i < idxlen; i++)
506       {
507         if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content))
508         {
509           mutt_body_handler (idx[i]->content, &st);
510           state_putc ('\n', &st);
511         }
512       }
513     }
514
515     if (mime_fwd_any && 
516         (last = copy_problematic_attachments (fp, last, idx, idxlen, mime_fwd_all)) == NULL)
517       goto bail;
518   }
519   
520   mutt_forward_trailer (tmpfp);
521   
522   fclose (tmpfp);
523   tmpfp = NULL;
524
525   /* now that we have the template, send it. */
526   ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
527   return;
528   
529   bail:
530   
531   if (tmpfp)
532   {
533     fclose (tmpfp);
534     mutt_unlink (tmpbody);
535   }
536
537   mutt_free_header (&tmphdr);
538 }
539
540
541 /* 
542  * Forward one or several message-type attachments. This 
543  * is different from the previous function
544  * since we want to mimic the index menu's behaviour.
545  *
546  * Code reuse from ci_send_message is not possible here -
547  * ci_send_message relies on a context structure to find messages,
548  * while, on the attachment menu, messages are referenced through
549  * the attachment index. 
550  */
551
552 static void attach_forward_msgs (FILE * fp, HEADER * hdr, 
553                ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
554 {
555   HEADER *curhdr = NULL;
556   HEADER *tmphdr;
557   short i;
558   int rc;
559
560   BODY **last;
561   char tmpbody[_POSIX_PATH_MAX];
562   FILE *tmpfp = NULL;
563
564   int cmflags = 0;
565   int chflags = CH_XMIT;
566   
567   if (cur)
568     curhdr = cur->hdr;
569   else
570   {
571     for (i = 0; i < idxlen; i++)
572       if (idx[i]->content->tagged)
573       {
574         curhdr = idx[i]->content->hdr;
575         break;
576       }
577   }
578
579   tmphdr = mutt_new_header ();
580   tmphdr->env = mutt_new_envelope ();
581   mutt_make_forward_subject (tmphdr->env, Context, curhdr);
582
583
584   tmpbody[0] = '\0';
585
586   if ((rc = query_quadoption (OPT_MIMEFWD, 
587                  _("Forward MIME encapsulated?"))) == M_NO)
588   {
589     
590     /* no MIME encapsulation */
591     
592     mutt_mktemp (tmpbody);
593     if (!(tmpfp = safe_fopen (tmpbody, "w")))
594     {
595       mutt_error (_("Can't create %s."), tmpbody);
596       mutt_free_header (&tmphdr);
597       return;
598     }
599
600     if (option (OPTFORWQUOTE))
601     {
602       chflags |= CH_PREFIX;
603       cmflags |= M_CM_PREFIX;
604     }
605
606     if (option (OPTFORWDECODE))
607     {
608       cmflags |= M_CM_DECODE | M_CM_CHARCONV;
609       if (option (OPTWEED))
610       {
611         chflags |= CH_WEED | CH_REORDER;
612         cmflags |= M_CM_WEED;
613       }
614     }
615     
616     
617     if (cur)
618     {
619       /* mutt_message_hook (cur->hdr, M_MESSAGEHOOK); */ 
620       mutt_forward_intro (tmpfp, cur->hdr);
621       _mutt_copy_message (tmpfp, fp, cur->hdr, cur->hdr->content, cmflags, chflags);
622       mutt_forward_trailer (tmpfp);
623     }
624     else
625     {
626       for (i = 0; i < idxlen; i++)
627       {
628         if (idx[i]->content->tagged)
629         {
630           /* mutt_message_hook (idx[i]->content->hdr, M_MESSAGEHOOK); */ 
631           mutt_forward_intro (tmpfp, idx[i]->content->hdr);
632           _mutt_copy_message (tmpfp, fp, idx[i]->content->hdr,
633                               idx[i]->content->hdr->content, cmflags, chflags);
634           mutt_forward_trailer (tmpfp);
635         }
636       }
637     }
638     fclose (tmpfp);
639   }
640   else if (rc == M_YES) /* do MIME encapsulation - we don't need to do much here */
641   {
642     last = &tmphdr->content;
643     if (cur)
644       mutt_copy_body (fp, last, cur);
645     else
646     {
647       for (i = 0; i < idxlen; i++)
648         if (idx[i]->content->tagged)
649         {
650           mutt_copy_body (fp, last, idx[i]->content);
651           last = &((*last)->next);
652         }
653     }
654   }
655   else
656     mutt_free_header (&tmphdr);
657
658   ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, 
659                    NULL, curhdr);
660
661 }
662
663 void mutt_attach_forward (FILE * fp, HEADER * hdr, 
664                           ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
665 {
666   short nattach;
667   
668
669   if (check_all_msg (idx, idxlen, cur, 0) == 0)
670     attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
671   else
672   {
673     nattach = count_tagged (idx, idxlen);
674     attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
675   }
676 }
677
678
679 \f
680 /**
681  ** 
682  ** the various reply functions, from the attachment menu
683  **
684  **
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, short idxlen,
702                                 HEADER *parent, int flags)
703 {
704   ENVELOPE *curenv = NULL;
705   HEADER *curhdr = NULL;
706   short i;
707   
708   if (!parent)
709   {
710     for (i = 0; i < idxlen; i++)
711     {
712       if (idx[i]->content->tagged)
713       {
714         curhdr = idx[i]->content->hdr;
715         curenv = curhdr->env;
716         break;
717       }
718     }
719   }
720   else
721   {
722     curenv = parent->env;
723     curhdr = parent;
724   }
725
726   if (curenv == NULL  ||  curhdr == NULL)
727   {
728     mutt_error _("Can't find any tagged messages.");
729     return -1;
730   }
731
732 #ifdef USE_NNTP
733   if ((flags & SENDNEWS))
734   {
735     /* in case followup set Newsgroups: with Followup-To: if it present */
736     if (!env->newsgroups && curenv &&
737         mutt_strcasecmp (curenv->followup_to, "poster"))
738       env->newsgroups = safe_strdup (curenv->followup_to);
739   }
740   else
741 #endif
742   {
743     if (parent)
744     {
745       if (mutt_fetch_recips (env, curenv, flags) == -1)
746         return -1;
747     }
748     else
749     {
750       for (i = 0; i < idxlen; i++)
751       {
752         if (idx[i]->content->tagged
753             && mutt_fetch_recips (env, idx[i]->content->hdr->env, flags) == -1)
754           return -1;
755       }
756     }
757
758     if ((flags & SENDLISTREPLY) && !env->to)
759     {
760       mutt_error _("No mailing lists found!");
761       return (-1);
762     }
763
764     mutt_fix_reply_recipients (env);
765   }
766   mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
767
768   if (parent)
769     mutt_add_to_reference_headers (env, curenv, NULL, NULL);
770   else
771   {
772     LIST **p = NULL, **q = NULL;
773     
774     for (i = 0; i < idxlen; i++)
775     {
776       if (idx[i]->content->tagged)
777         mutt_add_to_reference_headers (env, idx[i]->content->hdr->env, &p, &q);
778     }
779   }
780   
781   return 0;
782 }
783
784
785 /*  This is _very_ similar to send.c's include_reply(). */
786
787 static void attach_include_reply (FILE *fp, FILE *tmpfp, HEADER *cur, int flags)
788 {
789   int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV;
790   int chflags = CH_DECODE;
791
792   /* mutt_message_hook (cur, M_MESSAGEHOOK); */ 
793   
794   mutt_make_attribution (Context, cur, tmpfp);
795   
796   if (!option (OPTHEADER))
797     cmflags |= M_CM_NOHEADER;
798   if (option (OPTWEED))
799   {
800     chflags |= CH_WEED;
801     cmflags |= M_CM_WEED;
802   }
803
804   _mutt_copy_message (tmpfp, fp, cur, cur->content, cmflags, chflags);
805   mutt_make_post_indent (Context, cur, tmpfp);
806 }
807   
808 void mutt_attach_reply (FILE * fp, HEADER * hdr, 
809                         ATTACHPTR ** idx, short idxlen, BODY * cur, 
810                         int flags)
811 {
812   short mime_reply_any = 0;
813   
814   short nattach = 0;
815   HEADER *parent = NULL;
816   HEADER *tmphdr = NULL;
817   short i;
818
819   STATE st;
820   char tmpbody[_POSIX_PATH_MAX];
821   FILE *tmpfp;
822   
823   char prefix[SHORT_STRING];
824   int rc;
825   
826 #ifdef USE_NNTP
827   if (flags & SENDNEWS)
828     set_option (OPTNEWSSEND);
829   else
830     unset_option (OPTNEWSSEND);
831 #endif
832
833   if (check_all_msg (idx, idxlen, cur, 0) == -1)
834   {
835     nattach = count_tagged (idx, idxlen);
836     if ((parent = find_parent (idx, idxlen, cur, nattach)) == NULL)
837       parent = hdr;
838   }
839
840   if (nattach > 1 && !check_can_decode (idx, idxlen, cur))
841   {
842     if ((rc = query_quadoption (OPT_MIMEFWDREST,
843       _("Can't decode all tagged attachments.  MIME-encapsulate the others?"))) == -1)
844       return;
845     else if (rc == M_YES)
846       mime_reply_any = 1;
847   }
848   else if (nattach == 1)
849     mime_reply_any = 1;
850
851   tmphdr = mutt_new_header ();
852   tmphdr->env = mutt_new_envelope ();
853
854   if (attach_reply_envelope_defaults (tmphdr->env, idx, idxlen, 
855                                       parent ? parent : (cur ? cur->hdr : NULL), flags) == -1)
856   {
857     mutt_free_header (&tmphdr);
858     return;
859   }
860   
861   mutt_mktemp (tmpbody);
862   if ((tmpfp = safe_fopen (tmpbody, "w")) == NULL)
863   {
864     mutt_error (_("Can't create %s."), tmpbody);
865     mutt_free_header (&tmphdr);
866     return;
867   }
868
869   if (!parent)
870   {
871     if (cur)
872       attach_include_reply (fp, tmpfp, cur->hdr, flags);
873     else
874     {
875       for (i = 0; i < idxlen; i++)
876       {
877         if (idx[i]->content->tagged)
878           attach_include_reply (fp, tmpfp, idx[i]->content->hdr, flags);
879       }
880     }
881   }
882   else
883   {
884     mutt_make_attribution (Context, parent, tmpfp);
885     
886     memset (&st, 0, sizeof (STATE));
887     st.fpin = fp;
888     st.fpout = tmpfp;
889
890     if (!option (OPTTEXTFLOWED))
891       _mutt_make_string (prefix, sizeof (prefix), NONULL (Prefix), 
892                          Context, parent, 0);
893     else
894       strfcpy (prefix, ">", sizeof (prefix));
895
896     st.prefix = prefix;
897     st.flags  = M_CHARCONV;
898     
899     if (option (OPTWEED)) 
900       st.flags |= M_WEED;
901
902     if (option (OPTHEADER))
903       include_header (1, fp, parent, tmpfp, prefix);
904
905     if (cur)
906     {
907       if (mutt_can_decode (cur))
908       {
909         mutt_body_handler (cur, &st);
910         state_putc ('\n', &st);
911       }
912       else
913         mutt_copy_body (fp, &tmphdr->content, cur);
914     }
915     else
916     {
917       for (i = 0; i < idxlen; i++)
918       {
919         if (idx[i]->content->tagged && mutt_can_decode (idx[i]->content))
920         {
921           mutt_body_handler (idx[i]->content, &st);
922           state_putc ('\n', &st);
923         }
924       }
925     }
926
927     mutt_make_post_indent (Context, parent, tmpfp);
928
929     if (mime_reply_any && !cur && 
930         copy_problematic_attachments (fp, &tmphdr->content, idx, idxlen, 0) == NULL)
931     {
932       mutt_free_header (&tmphdr);
933       fclose (tmpfp);
934       return;
935     }
936   }
937
938   fclose (tmpfp);
939   
940   if (ci_send_message (flags, tmphdr, tmpbody, NULL, parent) == 0)
941     mutt_set_flag (Context, hdr, M_REPLIED, 1);
942 }
943