string documentation.
[apps/madmutt.git] / send.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.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 #include <lib-mime/rfc3676.h>
14 #include <lib-sys/unix.h>
15 #include <lib-ui/curses.h>
16 #include <lib-ui/enter.h>
17 #include <lib-mx/mx.h>
18
19 #include "alias.h"
20 #include "keymap.h"
21 #include "copy.h"
22 #include <lib-crypt/crypt.h>
23 #include "mutt_idna.h"
24 #include "attach.h"
25
26 #ifdef USE_NNTP
27 #include <nntp/nntp.h>
28 #endif
29
30 #include "remailer.h"
31
32 static void append_signature (FILE * f)
33 {
34   FILE *tmpfp;
35   pid_t thepid;
36
37   if (SignOffString) {
38     fprintf (f, "\n%s", SignOffString);
39   }
40
41   if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
42     if (option (OPTSIGDASHES))
43       fputs ("\n-- \n", f);
44     else if (SignOffString)
45       fputs ("\n", f);
46     mutt_copy_stream (tmpfp, f);
47     m_fclose(&tmpfp);
48     if (thepid != -1)
49       mutt_wait_filter (thepid);
50   }
51 }
52
53 /* compare two e-mail addresses and return 1 if they are equivalent */
54 static int mutt_addrcmp (address_t * a, address_t * b)
55 {
56   if (!a->mailbox || !b->mailbox)
57     return 0;
58   if (ascii_strcasecmp (a->mailbox, b->mailbox))
59     return 0;
60   return 1;
61 }
62
63 /* search an e-mail address in a list */
64 static int mutt_addrsrc (address_t * a, address_t * lst)
65 {
66   for (; lst; lst = lst->next) {
67     if (mutt_addrcmp (a, lst))
68       return (1);
69   }
70   return (0);
71 }
72
73 /* removes addresses from "b" which are contained in "a" */
74 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
75 {
76   address_t *top, *p, *prev = NULL;
77
78   top = b;
79   while (b) {
80     for (p = a; p; p = p->next) {
81       if (mutt_addrcmp (p, b))
82         break;
83     }
84     if (p) {
85       if (prev) {
86         prev->next = b->next;
87         b->next = NULL;
88         address_list_wipe(&b);
89         b = prev;
90       }
91       else {
92         top = top->next;
93         b->next = NULL;
94         address_list_wipe(&b);
95         b = top;
96       }
97     }
98     else {
99       prev = b;
100       b = b->next;
101     }
102   }
103   return top;
104 }
105
106 /* remove any address which matches the current user.  if `leave_only' is
107  * nonzero, don't remove the user's address if it is the only one in the list
108  */
109 static address_t *remove_user (address_t * a, int leave_only)
110 {
111   address_t *top = NULL, *last = NULL;
112
113   while (a) {
114     if (!mutt_addr_is_user (a)) {
115       if (top) {
116         last->next = a;
117         last = last->next;
118       }
119       else
120         last = top = a;
121       a = a->next;
122       last->next = NULL;
123     }
124     else {
125       address_t *tmp = a;
126
127       a = a->next;
128       if (!leave_only || a || last) {
129         tmp->next = NULL;
130         address_list_wipe(&tmp);
131       }
132       else
133         last = top = tmp;
134     }
135   }
136   return top;
137 }
138
139 static address_t *find_mailing_lists (address_t * t, address_t * c)
140 {
141   address_t *top = NULL, *ptr = NULL;
142
143   for (; t || c; t = c, c = NULL) {
144     for (; t; t = t->next) {
145       if (mutt_is_mail_list (t) && !t->group) {
146         if (top) {
147           ptr->next = address_dup (t);
148           ptr = ptr->next;
149         }
150         else
151           ptr = top = address_dup (t);
152       }
153     }
154   }
155   return top;
156 }
157
158 static int edit_address (address_t ** a, const char *field)
159 {
160   char buf[HUGE_STRING];
161   char *err = NULL;
162   int idna_ok = 0;
163
164   do {
165     buf[0] = 0;
166     mutt_addrlist_to_local (*a);
167     rfc822_addrcat(buf, sizeof(buf), *a, 0);
168     if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
169       return (-1);
170     address_list_wipe(a);
171     *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
172     if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
173       mutt_error (_("Error: '%s' is a bad IDN."), err);
174       mutt_refresh ();
175       mutt_sleep (2);
176       p_delete(&err);
177     }
178   }
179   while (idna_ok != 0);
180   return 0;
181 }
182
183 static int edit_envelope (ENVELOPE * en, int flags)
184 {
185   char buf[HUGE_STRING];
186   string_list_t *uh = UserHeader;
187   regmatch_t pat_match[1];
188
189 #ifdef USE_NNTP
190   if (option (OPTNEWSSEND)) {
191     if (en->newsgroups)
192       m_strcpy(buf, sizeof(buf), en->newsgroups);
193     else
194       buf[0] = 0;
195     if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
196       return (-1);
197     p_delete(&en->newsgroups);
198     en->newsgroups = m_strdup(buf);
199
200     if (en->followup_to)
201       m_strcpy(buf, sizeof(buf), en->followup_to);
202     else
203       buf[0] = 0;
204     if (option (OPTASKFOLLOWUP)
205         && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
206       return (-1);
207     p_delete(&en->followup_to);
208     en->followup_to = m_strdup(buf);
209
210     if (en->x_comment_to)
211       m_strcpy(buf, sizeof(buf), en->x_comment_to);
212     else
213       buf[0] = 0;
214     if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
215         && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
216       return (-1);
217     p_delete(&en->x_comment_to);
218     en->x_comment_to = m_strdup(buf);
219   }
220   else
221 #endif
222   {
223     if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
224       return (-1);
225     if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
226       return (-1);
227     if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
228       return (-1);
229   }
230
231   if (en->subject) {
232     if (option (OPTFASTREPLY))
233       return (0);
234     else
235       m_strcpy(buf, sizeof(buf), en->subject);
236   }
237   else {
238     char *p;
239
240     buf[0] = 0;
241     for (; uh; uh = uh->next) {
242       if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
243         p = vskipspaces(uh->data + 8);
244         m_strcpy(buf, sizeof(buf), p);
245       }
246     }
247   }
248
249   if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
250       regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
251     unsigned int pos = pat_match->rm_so;
252
253     if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
254       buf[pos] = '\0';          /* kill match */
255       while (pos-- && buf[pos] == ' ')
256         buf[pos] = '\0';        /* remove trailing spaces */
257     }
258     else {
259       mutt_error (_("Ignoring $strip_was: Subject would be empty."));
260       sleep (2);
261     }
262   }
263   if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
264                                                                   &&
265                                                                   query_quadoption
266                                                                   (OPT_SUBJECT,
267                                                                    _
268                                                                    ("No subject, abort?"))
269                                                                   != M_NO)) {
270     mutt_message _("No subject, aborting.");
271
272     return (-1);
273   }
274   m_strreplace(&en->subject, buf);
275
276   return 0;
277 }
278
279 #ifdef USE_NNTP
280 static char *nntp_get_header(const char *s)
281 {
282     return m_strdup(skipspaces(s));
283 }
284 #endif
285
286 static void process_user_recips (ENVELOPE * env)
287 {
288   string_list_t *uh = UserHeader;
289
290   for (; uh; uh = uh->next) {
291     if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
292       env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
293     else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
294       env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
295     else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
296       env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
297 #ifdef USE_NNTP
298     else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
299       env->newsgroups = nntp_get_header (uh->data + 11);
300     else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
301       env->followup_to = nntp_get_header (uh->data + 12);
302     else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
303       env->x_comment_to = nntp_get_header (uh->data + 13);
304 #endif
305   }
306 }
307
308 static void process_user_header (ENVELOPE * env)
309 {
310   string_list_t *uh = UserHeader;
311   string_list_t *last = env->userhdrs;
312
313   if (last)
314     while (last->next)
315       last = last->next;
316
317   for (; uh; uh = uh->next) {
318     if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
319       /* User has specified a default From: address.  Remove default address */
320       address_list_wipe(&env->from);
321       env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
322     }
323     else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
324       address_list_wipe(&env->reply_to);
325       env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
326     }
327     else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
328       m_strreplace(&env->message_id, uh->data + 11);
329     else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
330              ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
331              ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
332 #ifdef USE_NNTP
333              ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
334              ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
335              ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
336 #endif
337              ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
338              ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
339       if (last) {
340         last->next = string_item_new();
341         last = last->next;
342       }
343       else
344         last = env->userhdrs = string_item_new();
345       last->data = m_strdup(uh->data);
346     }
347   }
348 }
349
350 void mutt_forward_intro (FILE * fp, HEADER * cur)
351 {
352   char buffer[STRING];
353
354   fputs ("----- Forwarded message from ", fp);
355   buffer[0] = 0;
356   rfc822_addrcat(buffer, sizeof(buffer), cur->env->from, 1);
357   fputs (buffer, fp);
358   fputs (" -----\n\n", fp);
359 }
360
361 void mutt_forward_trailer (FILE * fp)
362 {
363   fputs ("\n----- End forwarded message -----\n", fp);
364 }
365
366
367 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
368 {
369   int chflags = CH_DECODE, cmflags = 0;
370
371   mutt_parse_mime_message (ctx, cur);
372   mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
373
374   if ((cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
375     /* make sure we have the user's passphrase before proceeding... */
376     crypt_valid_passphrase (cur->security);
377   }
378
379   mutt_forward_intro (out, cur);
380
381   if (option (OPTFORWDECODE)) {
382     cmflags |= M_CM_DECODE | M_CM_CHARCONV;
383     if (option (OPTWEED)) {
384       chflags |= CH_WEED | CH_REORDER;
385       cmflags |= M_CM_WEED;
386     }
387   }
388   if (option (OPTFORWQUOTE))
389     cmflags |= M_CM_PREFIX;
390
391   mutt_copy_message (out, ctx, cur, cmflags, chflags);
392   mutt_forward_trailer (out);
393   return 0;
394 }
395
396 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
397 {
398   char buffer[STRING];
399
400   if (Attribution) {
401     mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
402     fputs (buffer, out);
403     fputc ('\n', out);
404   }
405 }
406
407 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
408 {
409   char buffer[STRING];
410
411   if (PostIndentString) {
412     mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
413     fputs (buffer, out);
414     fputc ('\n', out);
415   }
416 }
417
418 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
419 {
420   int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
421   int chflags = CH_DECODE;
422
423   if ((cur->security & ENCRYPT)) {
424     /* make sure we have the user's passphrase before proceeding... */
425     crypt_valid_passphrase (cur->security);
426   }
427
428   mutt_parse_mime_message (ctx, cur);
429   mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
430
431   mutt_make_attribution (ctx, cur, out);
432
433   if (!option (OPTHEADER))
434     cmflags |= M_CM_NOHEADER;
435   if (option (OPTWEED)) {
436     chflags |= CH_WEED | CH_REORDER;
437     cmflags |= M_CM_WEED;
438   }
439
440   mutt_copy_message (out, ctx, cur, cmflags, chflags);
441
442   mutt_make_post_indent (ctx, cur, out);
443
444   return 0;
445 }
446
447 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
448 {
449   char prompt[STRING];
450
451   if (flags && env->mail_followup_to && hmfupto == M_YES) {
452     address_list_append(to, address_list_dup(env->mail_followup_to));
453     return 0;
454   }
455
456   /* Exit now if we're setting up the default Cc list for list-reply
457    * (only set if Mail-Followup-To is present and honoured).
458    */
459   if (flags & SENDLISTREPLY)
460     return 0;
461
462   /* If this message came from a mailing list, ask the user if he really
463    * intended to reply to the author only.
464    */
465   if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
466     switch (query_quadoption (OPT_LISTREPLY,
467                               _("Message came from a mailing list. List-reply to mailing list?")))
468     {
469     case M_YES:
470       address_list_append(to, find_mailing_lists (env->to, env->cc));
471       return 0;
472     case -1:
473       return -1;                /* abort */
474     }
475   }
476
477   if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
478     /* mail is from the user, assume replying to recipients */
479     address_list_append(to, address_list_dup(env->to));
480   }
481   else if (env->reply_to) {
482     if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
483         (option (OPTIGNORELISTREPLYTO) &&
484          mutt_is_mail_list (env->reply_to) &&
485          (mutt_addrsrc (env->reply_to, env->to) ||
486           mutt_addrsrc (env->reply_to, env->cc)))) {
487       /* If the Reply-To: address is a mailing list, assume that it was
488        * put there by the mailing list, and use the From: address
489        * 
490        * We also take the from header if our correspondant has a reply-to
491        * header which is identical to the electronic mail address given
492        * in his From header.
493        * 
494        */
495       address_list_append(to, address_list_dup(env->from));
496     }
497     else if (!(mutt_addrcmp (env->from, env->reply_to) &&
498                !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
499       /* There are quite a few mailing lists which set the Reply-To:
500        * header field to the list address, which makes it quite impossible
501        * to send a message to only the sender of the message.  This
502        * provides a way to do that.
503        */
504       snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
505                 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
506       switch (query_quadoption (OPT_REPLYTO, prompt)) {
507       case M_YES:
508         address_list_append(to, address_list_dup(env->reply_to));
509         break;
510
511       case M_NO:
512         address_list_append(to, address_list_dup(env->from));
513         break;
514
515       default:
516         return (-1);            /* abort */
517       }
518     }
519     else
520       address_list_append(to, address_list_dup(env->reply_to));
521   }
522   else
523     address_list_append(to, address_list_dup(env->from));
524
525   return (0);
526 }
527
528 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
529 {
530   char prompt[STRING];
531   int hmfupto = -1;
532
533   if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
534     snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
535               in->mail_followup_to->mailbox,
536               in->mail_followup_to->next ? ",..." : "");
537
538     if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
539       return -1;
540   }
541
542   if (flags & SENDLISTREPLY) {
543     address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
544
545     if (in->mail_followup_to && hmfupto == M_YES &&
546         default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
547       return (-1);              /* abort */
548   }
549   else {
550     if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
551       return (-1);              /* abort */
552
553     if ((flags & SENDGROUPREPLY)
554         && (!in->mail_followup_to || hmfupto != M_YES))
555     {
556       address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
557       address_list_append(tmp, address_list_dup(in->cc));
558     }
559   }
560   return 0;
561 }
562
563 void mutt_fix_reply_recipients (ENVELOPE * env)
564 {
565   mutt_expand_aliases_env (env);
566
567   if (!option (OPTMETOO)) {
568     /* the order is important here.  do the CC: first so that if the
569      * the user is the only recipient, it ends up on the TO: field
570      */
571     env->cc = remove_user (env->cc, (env->to == NULL));
572     env->to = remove_user (env->to, (env->cc == NULL));
573   }
574
575   /* the CC field can get cluttered, especially with lists */
576   address_list_uniq(env->to);
577   address_list_uniq(env->cc);
578   env->cc = mutt_remove_xrefs (env->to, env->cc);
579
580   if (env->cc && !env->to) {
581     env->to = env->cc;
582     env->cc = NULL;
583   }
584 }
585
586 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
587 {
588   char buffer[STRING];
589
590   /* set the default subject for the message. */
591   mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
592   m_strreplace(&env->subject, buffer);
593 }
594
595 void mutt_make_misc_reply_headers (ENVELOPE * env,
596                                    CONTEXT * ctx __attribute__ ((unused)),
597                                    HEADER * cur __attribute__ ((unused)),
598                                    ENVELOPE * curenv)
599 {
600   /* This takes precedence over a subject that might have
601    * been taken from a List-Post header.  Is that correct?
602    */
603   if (curenv->real_subj) {
604     p_delete(&env->subject);
605     env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
606     sprintf (env->subject, "Re: %s", curenv->real_subj);
607   }
608   else if (!env->subject)
609     env->subject = m_strdup("Re: your mail");
610
611 #ifdef USE_NNTP
612   if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
613     env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
614 #endif
615 }
616
617 static string_list_t *mutt_make_references (ENVELOPE * e)
618 {
619   string_list_t *t = NULL, *l = NULL;
620
621   if (e->references)
622     l = string_list_dup(e->references);
623   else
624     l = string_list_dup(e->in_reply_to);
625
626   if (e->message_id) {
627     t = string_item_new();
628     t->data = m_strdup(e->message_id);
629     t->next = l;
630     l = t;
631   }
632
633   return l;
634 }
635
636 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
637                                     string_list_t *** pp, string_list_t *** qq)
638 {
639   string_list_t **p = NULL, **q = NULL;
640
641   if (pp)
642     p = *pp;
643   if (qq)
644     q = *qq;
645
646   if (!p)
647     p = &env->references;
648   if (!q)
649     q = &env->in_reply_to;
650
651   while (*p)
652     p = &(*p)->next;
653   while (*q)
654     q = &(*q)->next;
655
656   *p = mutt_make_references (curenv);
657
658   if (curenv->message_id) {
659     *q = string_item_new();
660     (*q)->data = m_strdup(curenv->message_id);
661   }
662
663   if (pp)
664     *pp = p;
665   if (qq)
666     *qq = q;
667
668 }
669
670 static void
671 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
672 {
673   env->references = NULL;
674   env->in_reply_to = NULL;
675
676   if (!curenv) {
677     HEADER *h;
678     string_list_t **p = NULL, **q = NULL;
679     int i;
680
681     for (i = 0; i < ctx->vcount; i++) {
682       h = ctx->hdrs[ctx->v2r[i]];
683       if (h->tagged)
684         mutt_add_to_reference_headers (env, h->env, &p, &q);
685     }
686   }
687   else
688     mutt_add_to_reference_headers (env, curenv, NULL, NULL);
689 }
690
691 static int
692 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
693 {
694   ENVELOPE *curenv = NULL;
695   int i = 0, tag = 0;
696
697   if (!cur) {
698     tag = 1;
699     for (i = 0; i < ctx->vcount; i++)
700       if (ctx->hdrs[ctx->v2r[i]]->tagged) {
701         cur = ctx->hdrs[ctx->v2r[i]];
702         curenv = cur->env;
703         break;
704       }
705
706     if (!cur) {
707       /* This could happen if the user tagged some messages and then did
708        * a limit such that none of the tagged message are visible.
709        */
710       mutt_error _("No tagged messages are visible!");
711
712       return (-1);
713     }
714   }
715   else
716     curenv = cur->env;
717
718   if (flags & SENDREPLY) {
719 #ifdef USE_NNTP
720     if ((flags & SENDNEWS)) {
721       /* in case followup set Newsgroups: with Followup-To: if it present */
722       if (!env->newsgroups && curenv &&
723           m_strcasecmp(curenv->followup_to, "poster"))
724         env->newsgroups = m_strdup(curenv->followup_to);
725     }
726     else
727 #endif
728     if (tag) {
729       HEADER *h;
730
731       for (i = 0; i < ctx->vcount; i++) {
732         h = ctx->hdrs[ctx->v2r[i]];
733         if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
734           return -1;
735       }
736     }
737     else if (mutt_fetch_recips (env, curenv, flags) == -1)
738       return -1;
739
740     if ((flags & SENDLISTREPLY) && !env->to) {
741       mutt_error _("No mailing lists found!");
742
743       return (-1);
744     }
745
746     mutt_make_misc_reply_headers (env, ctx, cur, curenv);
747     mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
748   }
749   else if (flags & SENDFORWARD)
750     mutt_make_forward_subject (env, ctx, cur);
751
752   return (0);
753 }
754
755 static int generate_body (FILE * tempfp,        /* stream for outgoing message */
756                           HEADER * msg, /* header for outgoing message */
757                           int flags,    /* compose mode */
758                           CONTEXT * ctx,        /* current mailbox */
759                           HEADER * cur)
760 {                               /* current message */
761   int i;
762   HEADER *h;
763   BODY *tmp;
764
765   if (flags & SENDREPLY) {
766     if ((i =
767          query_quadoption (OPT_INCLUDE,
768                            _("Include message in reply?"))) == -1)
769       return (-1);
770
771     if (i == M_YES) {
772       mutt_message _("Including quoted message...");
773
774       if (!cur) {
775         for (i = 0; i < ctx->vcount; i++) {
776           h = ctx->hdrs[ctx->v2r[i]];
777           if (h->tagged) {
778             if (include_reply (ctx, h, tempfp) == -1) {
779               mutt_error _("Could not include all requested messages!");
780
781               return (-1);
782             }
783             fputc ('\n', tempfp);
784           }
785         }
786       }
787       else
788         include_reply (ctx, cur, tempfp);
789
790     }
791   }
792   else if (flags & SENDFORWARD) {
793     if ((i =
794          query_quadoption (OPT_MIMEFWD,
795                            _("Forward as attachment?"))) == M_YES) {
796       BODY *last = msg->content;
797
798       mutt_message _("Preparing forwarded message...");
799
800       while (last && last->next)
801         last = last->next;
802
803       if (cur) {
804         tmp = mutt_make_message_attach (ctx, cur, 0);
805         if (last)
806           last->next = tmp;
807         else
808           msg->content = tmp;
809       }
810       else {
811         for (i = 0; i < ctx->vcount; i++) {
812           if (ctx->hdrs[ctx->v2r[i]]->tagged) {
813             tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
814             if (last) {
815               last->next = tmp;
816               last = tmp;
817             }
818             else
819               last = msg->content = tmp;
820           }
821         }
822       }
823     }
824     else if (i != -1) {
825       if (cur)
826         include_forward (ctx, cur, tempfp);
827       else
828         for (i = 0; i < ctx->vcount; i++)
829           if (ctx->hdrs[ctx->v2r[i]]->tagged)
830             include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
831     }
832     else if (i == -1)
833       return -1;
834   }
835   else if (flags & SENDKEY) {
836     BODY *btmp;
837
838     if ((btmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
839       return -1;
840
841     btmp->next = msg->content;
842     msg->content = btmp;
843   }
844
845   mutt_clear_error ();
846
847   return (0);
848 }
849
850 void mutt_set_followup_to (ENVELOPE * e)
851 {
852     /*
853      * Only generate the Mail-Followup-To if the user has requested it, and
854      * it hasn't already been set
855      */
856     if (!option(OPTFOLLOWUPTO))
857         return;
858
859 #ifdef USE_NNTP
860     if (option(OPTNEWSSEND)) {
861         if (!e->followup_to && e->newsgroups && strrchr(e->newsgroups, ','))
862             e->followup_to = m_strdup(e->newsgroups);
863         return;
864     }
865 #endif
866
867     if (e->mail_followup_to)
868         return;
869
870     if (mutt_is_list_cc (0, e->to, e->cc)) {
871         address_t **tmp;
872
873         /*
874          * this message goes to known mailing lists, so create a proper
875          * mail-followup-to header
876          */
877         tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
878         address_list_append(tmp, address_list_dup(e->cc));
879     }
880
881     /* remove ourselves from the mail-followup-to header */
882     e->mail_followup_to = remove_user(e->mail_followup_to, 0);
883
884     /*
885      * If we are not subscribed to any of the lists in question,
886      * re-add ourselves to the mail-followup-to header.  The
887      * mail-followup-to header generated is a no-op with group-reply,
888      * but makes sure list-reply has the desired effect.
889      */
890     if (e->mail_followup_to && !mutt_is_list_recipient(0, e->to, e->cc)) {
891         address_t *from;
892
893         if (e->reply_to)
894             from = address_list_dup(e->reply_to);
895         else if (e->from)
896             from = address_list_dup(e->from);
897         else
898             from = mutt_default_from();
899
900         address_list_append(&from, e->mail_followup_to);
901         e->mail_followup_to = from;
902     }
903
904     address_list_uniq(e->mail_followup_to);
905 }
906
907
908 /* look through the recipients of the message we are replying to, and if
909    we find an address that matches $alternates, we use that as the default
910    from field */
911 static address_t *set_reverse_name (ENVELOPE * env)
912 {
913     address_t *tmp = NULL;
914
915     for (tmp = env->to; tmp; tmp = tmp->next) {
916         if (mutt_addr_is_user(tmp))
917             goto found;
918     }
919     for (tmp = env->cc; tmp; tmp = tmp->next) {
920         if (mutt_addr_is_user(tmp))
921             goto found;
922     }
923
924     if (!mutt_addr_is_user(env->from))
925         return NULL;
926
927     tmp = env->from;
928
929   found:
930     tmp = address_dup(tmp);
931     if (!option(OPTREVREAL) || !tmp->personal) {
932         p_delete(&tmp->personal);
933         tmp->personal = m_strdup(Realname);
934     }
935     return tmp;
936 }
937
938 address_t *mutt_default_from (void)
939 {
940   address_t *adr;
941
942   /*
943    * Note: We let $from override $realname here.
944    * Is this the right thing to do?
945    */
946
947   if (From)
948     adr = address_dup(From);
949   else if (option (OPTUSEDOMAIN)) {
950     const char *fqdn = mutt_fqdn (1);
951     adr = address_new();
952     adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
953     sprintf(adr->mailbox, "%s@%s", NONULL(Username), NONULL(fqdn));
954   } else {
955     adr = address_new ();
956     adr->mailbox = m_strdup(NONULL(Username));
957   }
958
959   return (adr);
960 }
961
962 static int send_message (HEADER * msg)
963 {
964   char tempfile[_POSIX_PATH_MAX];
965   FILE *tempfp;
966   int i;
967
968   /* Write out the message in MIME form. */
969   tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
970   if (!tempfp)
971     return -1;
972
973   mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
974                             msg->chain ? 1 : 0);
975   fputc ('\n', tempfp);         /* tie off the header. */
976
977   if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
978     m_fclose(&tempfp);
979     unlink (tempfile);
980     return (-1);
981   }
982
983   if (m_fclose(&tempfp) != 0) {
984     mutt_perror (_("Can't create temporary file"));
985     unlink (tempfile);
986     return (-1);
987   }
988
989   if (msg->chain)
990     return mix_send_message (msg->chain, tempfile);
991
992   i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
993                        msg->env->bcc, tempfile,
994                        (msg->content->encoding == ENC8BIT));
995   return (i);
996 }
997
998 /* rfc2047 encode the content-descriptions */
999 static void encode_descriptions (BODY * b, short recurse)
1000 {
1001   BODY *t;
1002
1003   for (t = b; t; t = t->next) {
1004     if (t->description) {
1005       rfc2047_encode_string (&t->description);
1006     }
1007     if (recurse && t->parts)
1008       encode_descriptions (t->parts, recurse);
1009   }
1010 }
1011
1012 /* rfc2047 decode them in case of an error */
1013 static void decode_descriptions (BODY * b)
1014 {
1015   BODY *t;
1016
1017   for (t = b; t; t = t->next) {
1018     if (t->description) {
1019       rfc2047_decode (&t->description);
1020     }
1021     if (t->parts)
1022       decode_descriptions (t->parts);
1023   }
1024 }
1025
1026 static void fix_end_of_file (const char *data)
1027 {
1028   FILE *fp;
1029   int c;
1030
1031   if ((fp = safe_fopen (data, "a+")) == NULL)
1032     return;
1033   fseeko (fp, -1, SEEK_END);
1034   if ((c = fgetc (fp)) != '\n')
1035     fputc ('\n', fp);
1036   m_fclose(&fp);
1037 }
1038
1039 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1040 {
1041   HEADER *msg = header_new();
1042
1043   if (mutt_prepare_template (fp, ctx, msg, cur, option(OPTWEED)) < 0)
1044     return -1;
1045
1046   return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1047 }
1048
1049 int ci_send_message (int flags, /* send mode */
1050                      HEADER * msg,      /* template to use for new message */
1051                      char *tempfile,    /* file specified by -i or -H */
1052                      CONTEXT * ctx,     /* current mailbox */
1053                      HEADER * cur)
1054 {                               /* current message */
1055   char fcc[_POSIX_PATH_MAX] = "";       /* where to copy this message */
1056   FILE *tempfp = NULL;
1057   BODY *pbody;
1058   int i, killfrom = 0;
1059   int fcc_error = 0;
1060   int free_clear_content = 0;
1061
1062   BODY *save_content = NULL;
1063   BODY *clear_content = NULL;
1064   char *pgpkeylist = NULL;
1065
1066   /* save current value of "pgp_sign_as" */
1067   char *signas = NULL, *err = NULL;
1068   const char *tag = NULL;
1069   char *ctype;
1070
1071   int rv = -1;
1072
1073 #ifdef USE_NNTP
1074   if (flags & SENDNEWS)
1075     set_option (OPTNEWSSEND);
1076   else
1077     unset_option (OPTNEWSSEND);
1078 #endif
1079
1080   if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1081       mutt_num_postponed (1)) {
1082     /* If the user is composing a new message, check to see if there
1083      * are any postponed messages first.
1084      */
1085     if ((i =
1086          query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1087       return rv;
1088
1089     if (i == M_YES)
1090       flags |= SENDPOSTPONED;
1091   }
1092
1093
1094   if (flags & SENDPOSTPONED)
1095     signas = m_strdup(PgpSignAs);
1096
1097   /* Delay expansion of aliases until absolutely necessary--shouldn't
1098    * be necessary unless we are prompting the user or about to execute a
1099    * send-hook.
1100    */
1101
1102   if (!msg) {
1103     msg = header_new();
1104
1105     if (flags == SENDPOSTPONED) {
1106       if ((flags =
1107            mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1108         goto cleanup;
1109 #ifdef USE_NNTP
1110       /*
1111        * If postponed message is a news article, it have
1112        * a "Newsgroups:" header line, then set appropriate flag.
1113        */
1114       if (msg->env->newsgroups) {
1115         flags |= SENDNEWS;
1116         set_option (OPTNEWSSEND);
1117       }
1118       else {
1119         flags &= ~SENDNEWS;
1120         unset_option (OPTNEWSSEND);
1121       }
1122 #endif
1123     }
1124
1125     if (flags & (SENDPOSTPONED | SENDRESEND)) {
1126       if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1127         mutt_perror (msg->content->filename);
1128         goto cleanup;
1129       }
1130     }
1131
1132     if (!msg->env)
1133       msg->env = envelope_new();
1134   }
1135
1136   /* Parse and use an eventual list-post header */
1137   if ((flags & SENDLISTREPLY)
1138       && cur && cur->env && cur->env->list_post) {
1139     /* Use any list-post header as a template */
1140     url_parse_mailto (msg->env, NULL, cur->env->list_post);
1141     /* We don't let them set the sender's address. */
1142     address_list_wipe(&msg->env->from);
1143   }
1144
1145   if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1146     pbody = body_new();
1147     pbody->next = msg->content; /* don't kill command-line attachments */
1148     msg->content = pbody;
1149
1150     if (!(ctype = m_strdup(ContentType)))
1151       ctype = m_strdup("text/plain");
1152     mutt_parse_content_type (ctype, msg->content);
1153     p_delete(&ctype);
1154
1155     msg->content->unlink = 1;
1156     msg->content->use_disp = 0;
1157     msg->content->disposition = DISPINLINE;
1158     if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1159         && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1160       parameter_setval(&msg->content->parameter, "format", "flowed");
1161       if (option (OPTDELSP))
1162         parameter_setval(&msg->content->parameter, "delsp", "yes");
1163     }
1164
1165     if (!tempfile) {
1166       char buffer[_POSIX_PATH_MAX];
1167       tempfp = m_tempfile(buffer, sizeof(buffer), NONULL(Tempdir), NULL);
1168       msg->content->filename = m_strdup(buffer);
1169     } else {
1170       tempfp = safe_fopen(tempfile, "a+");
1171       msg->content->filename = m_strdup(tempfile);
1172     }
1173
1174     if (!tempfp) {
1175       mutt_perror (msg->content->filename);
1176       goto cleanup;
1177     }
1178   }
1179
1180   /* this is handled here so that the user can match ~f in send-hook */
1181   if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1182     /* we shouldn't have to worry about freeing `msg->env->from' before
1183      * setting it here since this code will only execute when doing some
1184      * sort of reply.  the pointer will only be set when using the -H command
1185      * line option.
1186      *
1187      * We shouldn't have to worry about alias expansion here since we are
1188      * either replying to a real or postponed message, therefore no aliases
1189      * should exist since the user has not had the opportunity to add
1190      * addresses to the list.  We just have to ensure the postponed messages
1191      * have their aliases expanded.
1192      */
1193
1194     msg->env->from = set_reverse_name (cur->env);
1195   }
1196
1197   if (!msg->env->from && option (OPTUSEFROM)
1198       && !(flags & (SENDPOSTPONED | SENDRESEND)))
1199     msg->env->from = mutt_default_from ();
1200
1201   if (flags & SENDBATCH) {
1202     mutt_copy_stream (stdin, tempfp);
1203     if (option (OPTHDRS)) {
1204       process_user_recips (msg->env);
1205       process_user_header (msg->env);
1206     }
1207     mutt_expand_aliases_env (msg->env);
1208   }
1209   else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1210     if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1211         envelope_defaults (msg->env, ctx, cur, flags) == -1)
1212       goto cleanup;
1213
1214     if (option (OPTHDRS))
1215       process_user_recips (msg->env);
1216
1217     /* Expand aliases and remove duplicates/crossrefs */
1218     mutt_fix_reply_recipients (msg->env);
1219
1220 #ifdef USE_NNTP
1221     if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1222         && !msg->env->newsgroups)
1223       msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
1224 #endif
1225
1226     if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1227         !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1228       if (edit_envelope (msg->env, flags) == -1)
1229         goto cleanup;
1230     }
1231
1232     /* the from address must be set here regardless of whether or not
1233      * $use_from is set so that the `~P' (from you) operator in send-hook
1234      * patterns will work.  if $use_from is unset, the from address is killed
1235      * after send-hooks are evaulated */
1236
1237     if (!msg->env->from) {
1238       msg->env->from = mutt_default_from ();
1239       killfrom = 1;
1240     }
1241
1242     if ((flags & SENDREPLY) && cur) {
1243       /* change setting based upon message we are replying to */
1244       mutt_message_hook (ctx, cur, M_REPLYHOOK);
1245
1246       /*
1247        * set the replied flag for the message we are generating so that the
1248        * user can use ~Q in a send-hook to know when reply-hook's are also
1249        * being used.
1250        */
1251       msg->replied = 1;
1252     }
1253
1254     /* change settings based upon recipients */
1255
1256     mutt_message_hook (NULL, msg, M_SENDHOOK);
1257
1258     /*
1259      * Unset the replied flag from the message we are composing since it is
1260      * no longer required.  This is done here because the FCC'd copy of
1261      * this message was erroneously get the 'R'eplied flag when stored in
1262      * a maildir-style mailbox.
1263      */
1264     msg->replied = 0;
1265
1266     if (killfrom) {
1267       address_list_wipe(&msg->env->from);
1268       killfrom = 0;
1269     }
1270
1271     if (option (OPTHDRS))
1272       process_user_header (msg->env);
1273
1274
1275     if (option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1276       append_signature (tempfp);
1277
1278     /* include replies/forwarded messages, unless we are given a template */
1279     if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1280         && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1281       goto cleanup;
1282
1283     if (!option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1284       append_signature (tempfp);
1285
1286     /* 
1287      * this wants to be done _after_ generate_body, so message-hooks
1288      * can take effect.
1289      */
1290
1291     if (option (OPTCRYPTAUTOSIGN))
1292       msg->security |= SIGN;
1293     if (option (OPTCRYPTAUTOENCRYPT))
1294       msg->security |= ENCRYPT;
1295     if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1296       msg->security |= ENCRYPT;
1297     if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1298       msg->security |= SIGN;
1299     if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1300         && (cur->security & ENCRYPT))
1301       msg->security |= SIGN;
1302     if (msg->security & (ENCRYPT | SIGN)) {
1303       if (option (OPTPGPAUTOINLINE))
1304         msg->security |= INLINE;
1305       if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1306         msg->security |= INLINE;
1307     }
1308
1309     if (msg->security) {
1310       /* 
1311        * When reypling / forwarding, use the original message's
1312        * crypto system.  According to the documentation,
1313        * smime_is_default should be disregarded here.
1314        * 
1315        * Problem: At least with forwarding, this doesn't really
1316        * make much sense. Should we have an option to completely
1317        * disable individual mechanisms at run-time?
1318        */
1319       if (cur) {
1320         if (option (OPTCRYPTAUTOPGP) && (cur->security & APPLICATION_PGP))
1321           msg->security |= APPLICATION_PGP;
1322         else if (option (OPTCRYPTAUTOSMIME)
1323                  && (cur->security & APPLICATION_SMIME))
1324           msg->security |= APPLICATION_SMIME;
1325       }
1326
1327       /*
1328        * No crypto mechanism selected? Use availability + smime_is_default
1329        * for the decision. 
1330        */
1331       if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1332         if (option (OPTCRYPTAUTOSMIME) && option (OPTSMIMEISDEFAULT))
1333           msg->security |= APPLICATION_SMIME;
1334         else if (option (OPTCRYPTAUTOPGP))
1335           msg->security |= APPLICATION_PGP;
1336         else if (option (OPTCRYPTAUTOSMIME))
1337           msg->security |= APPLICATION_SMIME;
1338       }
1339     }
1340
1341     /* No permissible mechanisms found.  Don't sign or encrypt. */
1342     if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1343       msg->security = 0;
1344   }
1345
1346   /* 
1347    * This hook is even called for postponed messages, and can, e.g., be
1348    * used for setting the editor, the sendmail path, or the
1349    * envelope sender.
1350    */
1351   mutt_message_hook (NULL, msg, M_SEND2HOOK);
1352
1353   /* wait until now to set the real name portion of our return address so
1354      that $realname can be set in a send-hook */
1355   if (msg->env->from && !msg->env->from->personal
1356       && !(flags & (SENDRESEND | SENDPOSTPONED)))
1357     msg->env->from->personal = m_strdup(Realname);
1358
1359   if (!(flags & SENDKEY))
1360     m_fclose(&tempfp);
1361
1362   if (!(flags & SENDBATCH)) {
1363     struct stat st;
1364     time_t mtime = m_decrease_mtime(msg->content->filename, NULL);
1365
1366     mutt_update_encoding (msg->content);
1367
1368     /*
1369      * Select whether or not the user's editor should be called now.  We
1370      * don't want to do this when:
1371      * 1) we are sending a key/cert
1372      * 2) we are forwarding a message and the user doesn't want to edit it.
1373      *    This is controled by the quadoption $forward_edit.  However, if
1374      *    both $edit_headers and $autoedit are set, we want to ignore the
1375      *    setting of $forward_edit because the user probably needs to add the
1376      *    recipients.
1377      */
1378     if (!(flags & SENDKEY) &&
1379         ((flags & SENDFORWARD) == 0 ||
1380          (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1381          query_quadoption (OPT_FORWEDIT,
1382                            _("Edit forwarded message?")) == M_YES)) {
1383       /* If the this isn't a text message, look for a mailcap edit command */
1384       if (rfc1524_mailcap_isneeded(msg->content)) {
1385         if (!mutt_edit_attachment (msg->content))
1386           goto cleanup;
1387       } else if (option (OPTEDITHDRS)) {
1388         mutt_env_to_local (msg->env);
1389         mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1390                            sizeof (fcc));
1391         mutt_env_to_idna (msg->env, NULL, NULL);
1392       }
1393       else {
1394         mutt_edit_file (Editor, msg->content->filename);
1395
1396         if (stat (msg->content->filename, &st) == 0) {
1397           if (mtime != st.st_mtime)
1398             fix_end_of_file (msg->content->filename);
1399         } else
1400           mutt_perror (msg->content->filename);
1401       }
1402
1403       if (option (OPTTEXTFLOWED))
1404         rfc3676_space_stuff (msg);
1405
1406       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1407     }
1408
1409     if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1410       if (stat (msg->content->filename, &st) == 0) {
1411         /* if the file was not modified, bail out now */
1412         if (mtime == st.st_mtime && !msg->content->next &&
1413             query_quadoption (OPT_ABORT,
1414                               _("Abort unmodified message?")) == M_YES) {
1415           mutt_message _("Aborted unmodified message.");
1416
1417           goto cleanup;
1418         }
1419       }
1420       else
1421         mutt_perror (msg->content->filename);
1422     }
1423   }
1424
1425   /* specify a default fcc.  if we are in batchmode, only save a copy of
1426    * the message if the value of $copy is yes or ask-yes */
1427
1428   if (!fcc[0] && !(flags & (SENDPOSTPONED))
1429       && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1430     /* set the default FCC */
1431     if (!msg->env->from) {
1432       msg->env->from = mutt_default_from ();
1433       killfrom = 1;             /* no need to check $use_from because if the user specified
1434                                    a from address it would have already been set by now */
1435     }
1436     mutt_select_fcc (fcc, sizeof (fcc), msg);
1437     if (killfrom) {
1438       address_list_wipe(&msg->env->from);
1439       killfrom = 0;
1440     }
1441   }
1442
1443
1444   mutt_update_encoding (msg->content);
1445
1446   if (!(flags & SENDBATCH)) {
1447   main_loop:
1448
1449     fcc_error = 0;              /* reset value since we may have failed before */
1450     mutt_pretty_mailbox (fcc);
1451     i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1452     if (i == -1) {
1453       /* abort */
1454 #ifdef USE_NNTP
1455       if (flags & SENDNEWS)
1456         mutt_message (_("Article not posted."));
1457
1458       else
1459 #endif
1460         mutt_message _("Mail not sent.");
1461       goto cleanup;
1462     }
1463     else if (i == 1) {
1464       /* postpone the message until later. */
1465       if (msg->content->next)
1466         msg->content = mutt_make_multipart (msg->content);
1467
1468       /*
1469        * make sure the message is written to the right part of a maildir 
1470        * postponed folder.
1471        */
1472       msg->read = 0;
1473       msg->old = 0;
1474
1475       encode_descriptions (msg->content, 1);
1476       mutt_prepare_envelope (msg->env, 0);
1477       mutt_env_to_idna (msg->env, NULL, NULL);  /* Handle bad IDNAs the next time. */
1478
1479       if (!Postponed
1480           || mutt_write_fcc (NONULL (Postponed), msg,
1481                              (cur
1482                               && (flags & SENDREPLY)) ? cur->env->
1483                              message_id : NULL, 1, fcc) < 0) {
1484         msg->content = mutt_remove_multipart (msg->content);
1485         decode_descriptions (msg->content);
1486         mutt_unprepare_envelope (msg->env);
1487         goto main_loop;
1488       }
1489       mutt_update_num_postponed ();
1490       mutt_message _("Message postponed.");
1491
1492       goto cleanup;
1493     }
1494   }
1495
1496 #ifdef USE_NNTP
1497   if (!(flags & SENDNEWS))
1498 #endif
1499     if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1500       if (!(flags & SENDBATCH)) {
1501         mutt_error _("No recipients are specified!");
1502
1503         goto main_loop;
1504       }
1505       else {
1506         puts _("No recipients were specified.");
1507
1508         goto cleanup;
1509       }
1510     }
1511
1512   if (mutt_env_to_idna (msg->env, &tag, &err)) {
1513     mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1514     p_delete(&err);
1515     if (!(flags & SENDBATCH))
1516       goto main_loop;
1517     else
1518       goto cleanup;
1519   }
1520
1521   if (!msg->env->subject && !(flags & SENDBATCH) &&
1522       (i =
1523        query_quadoption (OPT_SUBJECT,
1524                          _("No subject, abort sending?"))) != M_NO) {
1525     /* if the abort is automatic, print an error message */
1526     if (quadoption (OPT_SUBJECT) == M_YES)
1527       mutt_error _("No subject specified.");
1528
1529     goto main_loop;
1530   }
1531 #ifdef USE_NNTP
1532   if ((flags & SENDNEWS) && !msg->env->subject) {
1533     mutt_error _("No subject specified.");
1534
1535     goto main_loop;
1536   }
1537
1538   if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1539     mutt_error _("No newsgroup specified.");
1540
1541     goto main_loop;
1542   }
1543 #endif
1544
1545   if (msg->content->next)
1546     msg->content = mutt_make_multipart (msg->content);
1547
1548   if (mutt_attach_check (msg) &&
1549       !msg->content->next &&
1550       query_quadoption (OPT_ATTACH,
1551                         _("No attachments made but indicator found in text. "
1552                           "Cancel sending?")) == M_YES) {
1553     if (quadoption (OPT_ATTACH) == M_YES) {
1554       mutt_message _("No attachments made but indicator found in text. "
1555                      "Abort sending.");
1556       sleep (2);
1557     }
1558     mutt_message (_("Mail not sent."));
1559     goto main_loop;
1560   }
1561
1562   /* 
1563    * Ok, we need to do it this way instead of handling all fcc stuff in
1564    * one place in order to avoid going to main_loop with encoded "env"
1565    * in case of error.  Ugh.
1566    */
1567
1568   encode_descriptions (msg->content, 1);
1569
1570   /*
1571    * Make sure that clear_content and free_clear_content are
1572    * properly initialized -- we may visit this particular place in
1573    * the code multiple times, including after a failed call to
1574    * mutt_protect().
1575    */
1576
1577   clear_content = NULL;
1578   free_clear_content = 0;
1579
1580   if (msg->security) {
1581     /* save the decrypted attachments */
1582     clear_content = msg->content;
1583
1584     if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1585         mutt_protect (msg, pgpkeylist) == -1) {
1586       msg->content = mutt_remove_multipart (msg->content);
1587
1588       p_delete(&pgpkeylist);
1589
1590       decode_descriptions (msg->content);
1591       goto main_loop;
1592     }
1593     encode_descriptions (msg->content, 0);
1594   }
1595
1596   /* 
1597    * at this point, msg->content is one of the following three things:
1598    * - multipart/signed.  In this case, clear_content is a child.
1599    * - multipart/encrypted.  In this case, clear_content exists
1600    *   independently
1601    * - application/pgp.  In this case, clear_content exists independently.
1602    * - something else.  In this case, it's the same as clear_content.
1603    */
1604
1605   /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1606
1607   if (clear_content && (msg->content != clear_content)
1608       && (msg->content->parts != clear_content))
1609     free_clear_content = 1;
1610
1611   if (!option (OPTNOCURSES))
1612     mutt_message _("Sending message...");
1613
1614   mutt_prepare_envelope (msg->env, 1);
1615
1616   /* save a copy of the message, if necessary. */
1617
1618   mutt_expand_path (fcc, sizeof (fcc));
1619
1620
1621   /* Don't save a copy when we are in batch-mode, and the FCC
1622    * folder is on an IMAP server: This would involve possibly lots
1623    * of user interaction, which is not available in batch mode. 
1624    * 
1625    * Note: A patch to fix the problems with the use of IMAP servers
1626    * from non-curses mode is available from Brendan Cully.  However, 
1627    * I'd like to think a bit more about this before including it.
1628    */
1629
1630   if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1631     fcc[0] = '\0';
1632
1633   if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1634     BODY *tmpbody = msg->content;
1635     BODY *save_sig = NULL;
1636     BODY *save_parts = NULL;
1637
1638     if (msg->security && option (OPTFCCCLEAR))
1639       msg->content = clear_content;
1640
1641     /* check to see if the user wants copies of all attachments */
1642     if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1643       if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1644               m_strcmp(msg->content->subtype, "signed") == 0))
1645       {
1646         if (clear_content->type == TYPEMULTIPART) {
1647           if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1648             /* save initial signature and attachments */
1649             save_sig = msg->content->parts->next;
1650             save_parts = clear_content->parts->next;
1651           }
1652
1653           /* this means writing only the main part */
1654           msg->content = clear_content->parts;
1655
1656           if (mutt_protect (msg, pgpkeylist) == -1) {
1657             /* we can't do much about it at this point, so
1658              * fallback to saving the whole thing to fcc
1659              */
1660             msg->content = tmpbody;
1661             save_sig = NULL;
1662             goto full_fcc;
1663           }
1664
1665           save_content = msg->content;
1666         }
1667       }
1668       else
1669         msg->content = msg->content->parts;
1670     }
1671
1672   full_fcc:
1673     if (msg->content) {
1674       /* update received time so that when storing to a mbox-style folder
1675        * the From_ line contains the current time instead of when the
1676        * message was first postponed.
1677        */
1678       msg->received = time (NULL);
1679       if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1680         /*
1681          * Error writing FCC, we should abort sending.
1682          */
1683         fcc_error = 1;
1684       }
1685     }
1686
1687     msg->content = tmpbody;
1688
1689     if (save_sig) {
1690       /* cleanup the second signature structures */
1691       if (save_content->parts) {
1692         body_list_wipe(&save_content->parts->next);
1693         save_content->parts = NULL;
1694       }
1695       body_list_wipe(&save_content);
1696
1697       /* restore old signature and attachments */
1698       msg->content->parts->next = save_sig;
1699       msg->content->parts->parts->next = save_parts;
1700     }
1701     else if (save_content) {
1702       /* destroy the new encrypted body. */
1703       body_list_wipe(&save_content);
1704     }
1705
1706   }
1707
1708
1709   /*
1710    * Don't attempt to send the message if the FCC failed.  Just pretend
1711    * the send failed as well so we give the user a chance to fix the
1712    * error.
1713    */
1714   if (fcc_error || (i = send_message (msg)) == -1) {
1715     if (!(flags & SENDBATCH)) {
1716       if ((msg->security & ENCRYPT)
1717       ||  ((msg->security & SIGN)
1718       &&  msg->content->type == TYPEAPPLICATION)) {
1719         body_list_wipe(&msg->content); /* destroy PGP data */
1720         msg->content = clear_content;   /* restore clear text. */
1721       }
1722       else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1723         body_list_wipe(&msg->content->parts->next);    /* destroy sig */
1724         msg->content = mutt_remove_multipart (msg->content);
1725       }
1726
1727       msg->content = mutt_remove_multipart (msg->content);
1728       decode_descriptions (msg->content);
1729       mutt_unprepare_envelope (msg->env);
1730       goto main_loop;
1731     }
1732     else {
1733       puts _("Could not send the message.");
1734
1735       goto cleanup;
1736     }
1737   }
1738   else if (!option (OPTNOCURSES))
1739     mutt_message (i != 0 ? _("Sending in background.") :
1740 #ifdef USE_NNTP
1741                   (flags & SENDNEWS) ? _("Article posted.") :
1742                   _("Mail sent.")
1743 #else
1744                   _("Mail sent.")
1745 #endif
1746     );
1747   if (msg->security & ENCRYPT)
1748     p_delete(&pgpkeylist);
1749
1750   if (free_clear_content)
1751     body_list_wipe(&clear_content);
1752
1753   if (flags & SENDREPLY) {
1754     if (cur && ctx)
1755       mutt_set_flag (ctx, cur, M_REPLIED, 1);
1756     else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1757       for (i = 0; i < ctx->vcount; i++)
1758         if (ctx->hdrs[ctx->v2r[i]]->tagged)
1759           mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1760     }
1761   }
1762
1763
1764   rv = 0;
1765
1766 cleanup:
1767
1768   if (flags & SENDPOSTPONED) {
1769     if (signas) {
1770       p_delete(&PgpSignAs);
1771       PgpSignAs = signas;
1772     }
1773   }
1774
1775   m_fclose(&tempfp);
1776   header_delete(&msg);
1777
1778   return rv;
1779 }
1780
1781 /* vim: set sw=2: */