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