2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_curses.h"
27 #include "mutt_crypt.h"
28 #include "mutt_idna.h"
40 #include <sys/types.h>
52 static void append_signature (FILE *f)
57 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid)))
59 if (option (OPTSIGDASHES))
61 mutt_copy_stream (tmpfp, f);
64 mutt_wait_filter (thepid);
68 /* compare two e-mail addresses and return 1 if they are equivalent */
69 static int mutt_addrcmp (ADDRESS *a, ADDRESS *b)
71 if (!a->mailbox || !b->mailbox)
73 if (ascii_strcasecmp (a->mailbox, b->mailbox))
78 /* search an e-mail address in a list */
79 static int mutt_addrsrc (ADDRESS *a, ADDRESS *lst)
81 for (; lst; lst = lst->next)
83 if (mutt_addrcmp (a, lst))
89 /* removes addresses from "b" which are contained in "a" */
90 static ADDRESS *mutt_remove_xrefs (ADDRESS *a, ADDRESS *b)
92 ADDRESS *top, *p, *prev = NULL;
97 for (p = a; p; p = p->next)
99 if (mutt_addrcmp (p, b))
106 prev->next = b->next;
108 rfc822_free_address (&b);
115 rfc822_free_address (&b);
128 /* remove any address which matches the current user. if `leave_only' is
129 * nonzero, don't remove the user's address if it is the only one in the list
131 static ADDRESS *remove_user (ADDRESS *a, int leave_only)
133 ADDRESS *top = NULL, *last = NULL;
137 if (!mutt_addr_is_user (a))
154 if (!leave_only || a || last)
157 rfc822_free_address (&tmp);
166 static ADDRESS *find_mailing_lists (ADDRESS *t, ADDRESS *c)
168 ADDRESS *top = NULL, *ptr = NULL;
170 for (; t || c; t = c, c = NULL)
172 for (; t; t = t->next)
174 if (mutt_is_mail_list (t) && !t->group)
178 ptr->next = rfc822_cpy_adr_real (t);
182 ptr = top = rfc822_cpy_adr_real (t);
189 static int edit_address (ADDRESS **a, /* const */ char *field)
191 char buf[HUGE_STRING];
198 mutt_addrlist_to_local (*a);
199 rfc822_write_address (buf, sizeof (buf), *a, 0);
200 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
202 rfc822_free_address (a);
203 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
204 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0)
206 mutt_error (_("Error: '%s' is a bad IDN."), err);
212 while (idna_ok != 0);
216 static int edit_envelope (ENVELOPE *en, int flags)
218 char buf[HUGE_STRING];
219 LIST *uh = UserHeader;
222 if (option (OPTNEWSSEND))
225 strfcpy (buf, en->newsgroups, sizeof (buf));
228 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
230 FREE (&en->newsgroups);
231 en->newsgroups = safe_strdup (buf);
234 strfcpy (buf, en->followup_to, sizeof (buf));
237 if (option (OPTASKFOLLOWUP) && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
239 FREE (&en->followup_to);
240 en->followup_to = safe_strdup (buf);
242 if (en->x_comment_to)
243 strfcpy (buf, en->x_comment_to, sizeof (buf));
246 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO) && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
248 FREE (&en->x_comment_to);
249 en->x_comment_to = safe_strdup (buf);
254 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
256 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
258 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
264 if (option (OPTFASTREPLY))
267 strfcpy (buf, en->subject, sizeof (buf));
274 for (; uh; uh = uh->next)
276 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0)
280 strncpy (buf, p, sizeof (buf));
285 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 ||
286 (!buf[0] && query_quadoption (OPT_SUBJECT, _("No subject, abort?")) != M_NO))
288 mutt_message _("No subject, aborting.");
291 mutt_str_replace (&en->subject, buf);
297 char *nntp_get_header (const char *s)
300 return safe_strdup (s);
304 static void process_user_recips (ENVELOPE *env)
306 LIST *uh = UserHeader;
308 for (; uh; uh = uh->next)
310 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
311 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
312 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
313 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
314 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
315 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
317 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
318 env->newsgroups = nntp_get_header (uh->data + 11);
319 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
320 env->followup_to = nntp_get_header (uh->data + 12);
321 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
322 env->x_comment_to = nntp_get_header (uh->data + 13);
327 static void process_user_header (ENVELOPE *env)
329 LIST *uh = UserHeader;
330 LIST *last = env->userhdrs;
336 for (; uh; uh = uh->next)
338 if (ascii_strncasecmp ("from:", uh->data, 5) == 0)
340 /* User has specified a default From: address. Remove default address */
341 rfc822_free_address (&env->from);
342 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
344 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0)
346 rfc822_free_address (&env->reply_to);
347 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
349 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
350 mutt_str_replace (&env->message_id, uh->data + 11);
351 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
352 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
353 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
355 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
356 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
357 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
359 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
360 ascii_strncasecmp ("subject:", uh->data, 8) != 0)
364 last->next = mutt_new_list ();
368 last = env->userhdrs = mutt_new_list ();
369 last->data = safe_strdup (uh->data);
374 LIST *mutt_copy_list (LIST *p)
376 LIST *t, *r=NULL, *l=NULL;
378 for (; p; p = p->next)
380 t = (LIST *) safe_malloc (sizeof (LIST));
381 t->data = safe_strdup (p->data);
394 void mutt_forward_intro (FILE *fp, HEADER *cur)
398 fputs ("----- Forwarded message from ", fp);
400 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
402 fputs (" -----\n\n", fp);
405 void mutt_forward_trailer (FILE *fp)
407 fputs ("\n----- End forwarded message -----\n", fp);
411 static int include_forward (CONTEXT *ctx, HEADER *cur, FILE *out)
413 int chflags = CH_DECODE, cmflags = 0;
415 mutt_parse_mime_message (ctx, cur);
416 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
418 if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE))
420 /* make sure we have the user's passphrase before proceeding... */
421 crypt_valid_passphrase (cur->security);
424 mutt_forward_intro (out, cur);
426 if (option (OPTFORWDECODE))
428 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
429 if (option (OPTWEED))
431 chflags |= CH_WEED | CH_REORDER;
432 cmflags |= M_CM_WEED;
435 if (option (OPTFORWQUOTE))
436 cmflags |= M_CM_PREFIX;
438 mutt_copy_message (out, ctx, cur, cmflags, chflags);
439 mutt_forward_trailer (out);
443 void mutt_make_attribution (CONTEXT *ctx, HEADER *cur, FILE *out)
448 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
454 void mutt_make_post_indent (CONTEXT *ctx, HEADER *cur, FILE *out)
457 if (PostIndentString)
459 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
465 static int include_reply (CONTEXT *ctx, HEADER *cur, FILE *out)
467 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
468 int chflags = CH_DECODE;
470 if (WithCrypto && (cur->security & ENCRYPT))
472 /* make sure we have the user's passphrase before proceeding... */
473 crypt_valid_passphrase (cur->security);
476 mutt_parse_mime_message (ctx, cur);
477 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
479 mutt_make_attribution (ctx, cur, out);
481 if (!option (OPTHEADER))
482 cmflags |= M_CM_NOHEADER;
483 if (option (OPTWEED))
485 chflags |= CH_WEED | CH_REORDER;
486 cmflags |= M_CM_WEED;
489 mutt_copy_message (out, ctx, cur, cmflags, chflags);
491 mutt_make_post_indent (ctx, cur, out);
496 static int default_to (ADDRESS **to, ENVELOPE *env, int flags, int hmfupto)
501 if (flags && env->mail_followup_to && hmfupto == M_YES)
503 rfc822_append (to, env->mail_followup_to);
507 /* Exit now if we're setting up the default Cc list for list-reply
508 * (only set if Mail-Followup-To is present and honoured).
510 if (flags & SENDLISTREPLY)
513 /* If this message came from a mailing list, ask the user if he really
514 * intended to reply to the author only.
516 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
517 switch (query_quadoption (OPT_LISTREPLY,
518 _("Message came from a mailing list. Reply to author only?")))
521 tmp = find_mailing_lists (env->to, env->cc);
522 rfc822_append (to, tmp);
523 rfc822_free_address (&tmp);
526 return -1; /* abort */
530 if (!option(OPTREPLYSELF) && mutt_addr_is_user (env->from))
532 /* mail is from the user, assume replying to recipients */
533 rfc822_append (to, env->to);
535 else if (env->reply_to)
537 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
538 (option (OPTIGNORELISTREPLYTO) &&
539 mutt_is_mail_list (env->reply_to) &&
540 (mutt_addrsrc (env->reply_to, env->to) ||
541 mutt_addrsrc (env->reply_to, env->cc))))
543 /* If the Reply-To: address is a mailing list, assume that it was
544 * put there by the mailing list, and use the From: address
546 * We also take the from header if our correspondant has a reply-to
547 * header which is identical to the electronic mail address given
548 * in his From header.
551 rfc822_append (to, env->from);
553 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
554 !env->reply_to->next) &&
555 quadoption (OPT_REPLYTO) != M_YES)
557 /* There are quite a few mailing lists which set the Reply-To:
558 * header field to the list address, which makes it quite impossible
559 * to send a message to only the sender of the message. This
560 * provides a way to do that.
562 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
563 env->reply_to->mailbox,
564 env->reply_to->next?",...":"");
565 switch (query_quadoption (OPT_REPLYTO, prompt))
568 rfc822_append (to, env->reply_to);
572 rfc822_append (to, env->from);
576 return (-1); /* abort */
580 rfc822_append (to, env->reply_to);
583 rfc822_append (to, env->from);
588 int mutt_fetch_recips (ENVELOPE *out, ENVELOPE *in, int flags)
594 if ((flags & (SENDLISTREPLY|SENDGROUPREPLY)) && in->mail_followup_to)
596 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
597 in->mail_followup_to->mailbox,
598 in->mail_followup_to->next ? ",..." : "");
600 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
604 if (flags & SENDLISTREPLY)
606 tmp = find_mailing_lists (in->to, in->cc);
607 rfc822_append (&out->to, tmp);
608 rfc822_free_address (&tmp);
610 if (in->mail_followup_to && hmfupto == M_YES &&
611 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
612 return (-1); /* abort */
616 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
617 return (-1); /* abort */
619 if ((flags & SENDGROUPREPLY) && (!in->mail_followup_to || hmfupto != M_YES))
621 /* if(!mutt_addr_is_user(in->to)) */
622 rfc822_append (&out->cc, in->to);
623 rfc822_append (&out->cc, in->cc);
629 LIST *mutt_make_references(ENVELOPE *e)
631 LIST *t = NULL, *l = NULL;
634 l = mutt_copy_list (e->references);
636 l = mutt_copy_list (e->in_reply_to);
641 t->data = safe_strdup(e->message_id);
649 void mutt_fix_reply_recipients (ENVELOPE *env)
651 mutt_expand_aliases_env (env);
653 if (! option (OPTMETOO))
655 /* the order is important here. do the CC: first so that if the
656 * the user is the only recipient, it ends up on the TO: field
658 env->cc = remove_user (env->cc, (env->to == NULL));
659 env->to = remove_user (env->to, (env->cc == NULL));
662 /* the CC field can get cluttered, especially with lists */
663 env->to = mutt_remove_duplicates (env->to);
664 env->cc = mutt_remove_duplicates (env->cc);
665 env->cc = mutt_remove_xrefs (env->to, env->cc);
668 void mutt_make_forward_subject (ENVELOPE *env, CONTEXT *ctx, HEADER *cur)
672 /* set the default subject for the message. */
673 mutt_make_string (buffer, sizeof (buffer), NONULL(ForwFmt), ctx, cur);
674 mutt_str_replace (&env->subject, buffer);
677 void mutt_make_misc_reply_headers (ENVELOPE *env, CONTEXT *ctx,
678 HEADER *cur, ENVELOPE *curenv)
680 /* This takes precedence over a subject that might have
681 * been taken from a List-Post header. Is that correct?
683 if (curenv->real_subj)
685 FREE (&env->subject);
686 env->subject = safe_malloc (mutt_strlen (curenv->real_subj) + 5);
687 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
689 else if (!env->subject)
690 env->subject = safe_strdup ("Re: your mail");
693 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
694 env->x_comment_to = safe_strdup (mutt_get_name (curenv->from));
698 void mutt_add_to_reference_headers (ENVELOPE *env, ENVELOPE *curenv, LIST ***pp, LIST ***qq)
700 LIST **p = NULL, **q = NULL;
705 if (!p) p = &env->references;
706 if (!q) q = &env->in_reply_to;
708 while (*p) p = &(*p)->next;
709 while (*q) q = &(*q)->next;
711 *p = mutt_make_references (curenv);
713 if (curenv->message_id)
715 *q = mutt_new_list();
716 (*q)->data = safe_strdup (curenv->message_id);
725 mutt_make_reference_headers (ENVELOPE *curenv, ENVELOPE *env, CONTEXT *ctx)
727 env->references = NULL;
728 env->in_reply_to = NULL;
733 LIST **p = NULL, **q = NULL;
736 for(i = 0; i < ctx->vcount; i++)
738 h = ctx->hdrs[ctx->v2r[i]];
740 mutt_add_to_reference_headers (env, h->env, &p, &q);
744 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
748 envelope_defaults (ENVELOPE *env, CONTEXT *ctx, HEADER *cur, int flags)
750 ENVELOPE *curenv = NULL;
756 for (i = 0; i < ctx->vcount; i++)
757 if (ctx->hdrs[ctx->v2r[i]]->tagged)
759 cur = ctx->hdrs[ctx->v2r[i]];
766 /* This could happen if the user tagged some messages and then did
767 * a limit such that none of the tagged message are visible.
769 mutt_error _("No tagged messages are visible!");
776 if (flags & SENDREPLY)
779 if ((flags & SENDNEWS))
781 /* in case followup set Newsgroups: with Followup-To: if it present */
782 if (!env->newsgroups && curenv &&
783 mutt_strcasecmp (curenv->followup_to, "poster"))
784 env->newsgroups = safe_strdup (curenv->followup_to);
792 for (i = 0; i < ctx->vcount; i++)
794 h = ctx->hdrs[ctx->v2r[i]];
795 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
799 else if (mutt_fetch_recips (env, curenv, flags) == -1)
802 if ((flags & SENDLISTREPLY) && !env->to)
804 mutt_error _("No mailing lists found!");
808 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
809 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
811 else if (flags & SENDFORWARD)
812 mutt_make_forward_subject (env, ctx, cur);
818 generate_body (FILE *tempfp, /* stream for outgoing message */
819 HEADER *msg, /* header for outgoing message */
820 int flags, /* compose mode */
821 CONTEXT *ctx, /* current mailbox */
822 HEADER *cur) /* current message */
828 if (flags & SENDREPLY)
830 if ((i = query_quadoption (OPT_INCLUDE, _("Include message in reply?"))) == -1)
835 mutt_message _("Including quoted message...");
838 for (i = 0; i < ctx->vcount; i++)
840 h = ctx->hdrs[ctx->v2r[i]];
843 if (include_reply (ctx, h, tempfp) == -1)
845 mutt_error _("Could not include all requested messages!");
848 fputc ('\n', tempfp);
853 include_reply (ctx, cur, tempfp);
857 else if (flags & SENDFORWARD)
859 if ((i = query_quadoption (OPT_MIMEFWD, _("Forward as attachment?"))) == M_YES)
861 BODY *last = msg->content;
863 mutt_message _("Preparing forwarded message...");
865 while (last && last->next)
870 tmp = mutt_make_message_attach (ctx, cur, 0);
878 for (i = 0; i < ctx->vcount; i++)
880 if (ctx->hdrs[ctx->v2r[i]]->tagged)
882 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
889 last = msg->content = tmp;
897 include_forward (ctx, cur, tempfp);
899 for (i=0; i < ctx->vcount; i++)
900 if (ctx->hdrs[ctx->v2r[i]]->tagged)
901 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
906 /* if (WithCrypto && (flags & SENDKEY)) */
907 else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY))
911 if ((WithCrypto & APPLICATION_PGP)
912 && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
915 tmp->next = msg->content;
924 void mutt_set_followup_to (ENVELOPE *e)
930 * Only generate the Mail-Followup-To if the user has requested it, and
931 * it hasn't already been set
934 if (!option (OPTFOLLOWUPTO))
937 if (option (OPTNEWSSEND))
939 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
940 e->followup_to = safe_strdup (e->newsgroups);
945 if (!e->mail_followup_to)
947 if (mutt_is_list_cc (0, e->to, e->cc))
950 * this message goes to known mailing lists, so create a proper
951 * mail-followup-to header
954 t = rfc822_append (&e->mail_followup_to, e->to);
955 rfc822_append (&t, e->cc);
958 /* remove ourselves from the mail-followup-to header */
959 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
962 * If we are not subscribed to any of the lists in question,
963 * re-add ourselves to the mail-followup-to header. The
964 * mail-followup-to header generated is a no-op with group-reply,
965 * but makes sure list-reply has the desired effect.
968 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc))
971 from = rfc822_cpy_adr (e->reply_to);
973 from = rfc822_cpy_adr (e->from);
975 from = mutt_default_from ();
979 /* Normally, this loop will not even be entered. */
980 for (t = from; t && t->next; t = t->next)
983 t->next = e->mail_followup_to; /* t cannot be NULL at this point. */
984 e->mail_followup_to = from;
988 e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
994 /* look through the recipients of the message we are replying to, and if
995 we find an address that matches $alternates, we use that as the default
997 static ADDRESS *set_reverse_name (ENVELOPE *env)
1001 for (tmp = env->to; tmp; tmp = tmp->next)
1003 if (mutt_addr_is_user (tmp))
1008 for (tmp = env->cc; tmp; tmp = tmp->next)
1010 if (mutt_addr_is_user (tmp))
1014 if (!tmp && mutt_addr_is_user (env->from))
1018 tmp = rfc822_cpy_adr_real (tmp);
1019 if (!option (OPTREVREAL))
1020 FREE (&tmp->personal);
1022 tmp->personal = safe_strdup (Realname);
1027 ADDRESS *mutt_default_from (void)
1030 const char *fqdn = mutt_fqdn(1);
1033 * Note: We let $from override $realname here. Is this the right
1038 adr = rfc822_cpy_adr_real (From);
1039 else if (option (OPTUSEDOMAIN))
1041 adr = rfc822_new_address ();
1042 adr->mailbox = safe_malloc (mutt_strlen (Username) + mutt_strlen (fqdn) + 2);
1043 sprintf (adr->mailbox, "%s@%s", NONULL(Username), NONULL(fqdn)); /* __SPRINTF_CHECKED__ */
1047 adr = rfc822_new_address ();
1048 adr->mailbox = safe_strdup (NONULL(Username));
1054 static int send_message (HEADER *msg)
1056 char tempfile[_POSIX_PATH_MAX];
1060 /* Write out the message in MIME form. */
1061 mutt_mktemp (tempfile);
1062 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
1066 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, msg->chain ? 1 : 0);
1069 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1072 fputc ('\n', tempfp); /* tie off the header. */
1074 if ((mutt_write_mime_body (msg->content, tempfp) == -1))
1081 if (fclose (tempfp) != 0)
1083 mutt_perror (tempfile);
1090 return mix_send_message (msg->chain, tempfile);
1093 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1094 msg->env->bcc, tempfile, (msg->content->encoding == ENC8BIT));
1098 /* rfc2047 encode the content-descriptions */
1099 static void encode_descriptions (BODY *b, short recurse)
1103 for (t = b; t; t = t->next)
1107 rfc2047_encode_string (&t->description);
1109 if (recurse && t->parts)
1110 encode_descriptions (t->parts, recurse);
1114 /* rfc2047 decode them in case of an error */
1115 static void decode_descriptions (BODY *b)
1119 for (t = b; t; t = t->next)
1123 rfc2047_decode (&t->description);
1126 decode_descriptions (t->parts);
1130 int mutt_resend_message (FILE *fp, CONTEXT *ctx, HEADER *cur)
1132 HEADER *msg = mutt_new_header ();
1134 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1137 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1141 ci_send_message (int flags, /* send mode */
1142 HEADER *msg, /* template to use for new message */
1143 char *tempfile, /* file specified by -i or -H */
1144 CONTEXT *ctx, /* current mailbox */
1145 HEADER *cur) /* current message */
1147 char buffer[LONG_STRING];
1148 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1149 FILE *tempfp = NULL;
1151 int i, killfrom = 0;
1153 int free_clear_content = 0;
1155 BODY *save_content = NULL;
1156 BODY *clear_content = NULL;
1157 char *pgpkeylist = NULL;
1158 /* save current value of "pgp_sign_as" */
1159 char *signas = NULL;
1160 char *tag = NULL, *err = NULL;
1165 if (flags & SENDNEWS)
1166 set_option (OPTNEWSSEND);
1168 unset_option (OPTNEWSSEND);
1171 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1172 mutt_num_postponed (1))
1174 /* If the user is composing a new message, check to see if there
1175 * are any postponed messages first.
1177 if ((i = query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1181 flags |= SENDPOSTPONED;
1185 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1186 signas = safe_strdup(PgpSignAs);
1188 /* Delay expansion of aliases until absolutely necessary--shouldn't
1189 * be necessary unless we are prompting the user or about to execute a
1195 msg = mutt_new_header ();
1197 if (flags == SENDPOSTPONED)
1199 if ((flags = mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1203 * If postponed message is a news article, it have
1204 * a "Newsgroups:" header line, then set appropriate flag.
1206 if (msg->env->newsgroups)
1209 set_option (OPTNEWSSEND);
1214 unset_option (OPTNEWSSEND);
1219 if (flags & (SENDPOSTPONED|SENDRESEND))
1221 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL)
1223 mutt_perror (msg->content->filename);
1229 msg->env = mutt_new_envelope ();
1232 /* Parse and use an eventual list-post header */
1233 if ((flags & SENDLISTREPLY)
1234 && cur && cur->env && cur->env->list_post)
1236 /* Use any list-post header as a template */
1237 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1238 /* We don't let them set the sender's address. */
1239 rfc822_free_address (&msg->env->from);
1242 if (! (flags & (SENDKEY | SENDPOSTPONED | SENDRESEND)))
1244 pbody = mutt_new_body ();
1245 pbody->next = msg->content; /* don't kill command-line attachments */
1246 msg->content = pbody;
1248 mutt_parse_content_type (ContentType, msg->content);
1249 msg->content->unlink = 1;
1250 msg->content->use_disp = 0;
1251 msg->content->disposition = DISPINLINE;
1252 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT && !ascii_strcasecmp (msg->content->subtype, "plain"))
1253 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1257 mutt_mktemp (buffer);
1258 tempfp = safe_fopen (buffer, "w+");
1259 msg->content->filename = safe_strdup (buffer);
1263 tempfp = safe_fopen (tempfile, "a+");
1264 msg->content->filename = safe_strdup (tempfile);
1269 dprint(1,(debugfile, "newsend_message: can't create tempfile %s (errno=%d)\n", msg->content->filename, errno));
1270 mutt_perror (msg->content->filename);
1275 /* this is handled here so that the user can match ~f in send-hook */
1276 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED|SENDRESEND)))
1278 /* we shouldn't have to worry about freeing `msg->env->from' before
1279 * setting it here since this code will only execute when doing some
1280 * sort of reply. the pointer will only be set when using the -H command
1283 * We shouldn't have to worry about alias expansion here since we are
1284 * either replying to a real or postponed message, therefore no aliases
1285 * should exist since the user has not had the opportunity to add
1286 * addresses to the list. We just have to ensure the postponed messages
1287 * have their aliases expanded.
1290 msg->env->from = set_reverse_name (cur->env);
1293 if (!msg->env->from && option (OPTUSEFROM) && !(flags & (SENDPOSTPONED|SENDRESEND)))
1294 msg->env->from = mutt_default_from ();
1296 if (flags & SENDBATCH)
1298 mutt_copy_stream (stdin, tempfp);
1299 if (option (OPTHDRS))
1301 process_user_recips (msg->env);
1302 process_user_header (msg->env);
1304 mutt_expand_aliases_env (msg->env);
1306 else if (! (flags & (SENDPOSTPONED|SENDRESEND)))
1308 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1309 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1312 if (option (OPTHDRS))
1313 process_user_recips (msg->env);
1315 /* Expand aliases and remove duplicates/crossrefs */
1316 mutt_fix_reply_recipients (msg->env);
1319 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP && !msg->env->newsgroups)
1320 msg->env->newsgroups = safe_strdup (((NNTP_DATA *)ctx->data)->group);
1323 if (! (flags & SENDMAILX) &&
1324 ! (option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1325 ! ((flags & SENDREPLY) && option (OPTFASTREPLY)))
1327 if (edit_envelope (msg->env, flags) == -1)
1331 /* the from address must be set here regardless of whether or not
1332 * $use_from is set so that the `~P' (from you) operator in send-hook
1333 * patterns will work. if $use_from is unset, the from address is killed
1334 * after send-hooks are evaulated */
1336 if (!msg->env->from)
1338 msg->env->from = mutt_default_from ();
1342 if ((flags & SENDREPLY) && cur)
1344 /* change setting based upon message we are replying to */
1345 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1348 * set the replied flag for the message we are generating so that the
1349 * user can use ~Q in a send-hook to know when reply-hook's are also
1355 /* change settings based upon recipients */
1357 mutt_message_hook (NULL, msg, M_SENDHOOK);
1360 * Unset the replied flag from the message we are composing since it is
1361 * no longer required. This is done here because the FCC'd copy of
1362 * this message was erroneously get the 'R'eplied flag when stored in
1363 * a maildir-style mailbox.
1369 rfc822_free_address (&msg->env->from);
1373 if (option (OPTHDRS))
1374 process_user_header (msg->env);
1377 if (option (OPTSIGONTOP) && (! (flags & (SENDMAILX | SENDKEY)) && Editor && mutt_strcmp (Editor, "builtin") != 0))
1378 append_signature (tempfp);
1380 /* include replies/forwarded messages, unless we are given a template */
1381 if (!tempfile && (ctx || !(flags & (SENDREPLY|SENDFORWARD)))
1382 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1385 if (!option (OPTSIGONTOP) && (! (flags & (SENDMAILX | SENDKEY)) && Editor && mutt_strcmp (Editor, "builtin") != 0))
1386 append_signature (tempfp);
1389 * this wants to be done _after_ generate_body, so message-hooks
1393 if (WithCrypto && !(flags & SENDMAILX))
1395 if (option (OPTCRYPTAUTOSIGN))
1396 msg->security |= SIGN;
1397 if (option (OPTCRYPTAUTOENCRYPT))
1398 msg->security |= ENCRYPT;
1399 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1400 msg->security |= ENCRYPT;
1401 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1402 msg->security |= SIGN;
1403 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur && (cur->security & ENCRYPT))
1404 msg->security |= SIGN;
1405 if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN)))
1407 if (option (OPTPGPAUTOINLINE))
1408 msg->security |= INLINE;
1409 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1410 msg->security |= INLINE;
1414 if (WithCrypto && msg->security)
1417 * When reypling / forwarding, use the original message's
1418 * crypto system. According to the documentation,
1419 * smime_is_default should be disregarded here.
1421 * Problem: At least with forwarding, this doesn't really
1422 * make much sense. Should we have an option to completely
1423 * disable individual mechanisms at run-time?
1427 if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP)
1428 && (cur->security & APPLICATION_PGP))
1429 msg->security |= APPLICATION_PGP;
1430 else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1431 && (cur->security & APPLICATION_SMIME))
1432 msg->security |= APPLICATION_SMIME;
1436 * No crypto mechanism selected? Use availability + smime_is_default
1439 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1441 if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1442 && option (OPTSMIMEISDEFAULT))
1443 msg->security |= APPLICATION_SMIME;
1444 else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
1445 msg->security |= APPLICATION_PGP;
1446 else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME))
1447 msg->security |= APPLICATION_SMIME;
1451 /* No permissible mechanisms found. Don't sign or encrypt. */
1452 if (!(msg->security & (APPLICATION_SMIME|APPLICATION_PGP)))
1457 * This hook is even called for postponed messages, and can, e.g., be
1458 * used for setting the editor, the sendmail path, or the
1461 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1463 /* wait until now to set the real name portion of our return address so
1464 that $realname can be set in a send-hook */
1465 if (msg->env->from && !msg->env->from->personal
1466 && !(flags & (SENDRESEND|SENDPOSTPONED)))
1467 msg->env->from->personal = safe_strdup (Realname);
1469 if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
1470 safe_fclose (&tempfp);
1472 if (flags & SENDMAILX)
1474 if (mutt_builtin_editor (msg->content->filename, msg, cur) == -1)
1477 else if (! (flags & SENDBATCH))
1480 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1482 mutt_update_encoding (msg->content);
1485 * Select whether or not the user's editor should be called now. We
1486 * don't want to do this when:
1487 * 1) we are sending a key/cert
1488 * 2) we are forwarding a message and the user doesn't want to edit it.
1489 * This is controled by the quadoption $forward_edit. However, if
1490 * both $edit_headers and $autoedit are set, we want to ignore the
1491 * setting of $forward_edit because the user probably needs to add the
1494 if (! (flags & SENDKEY) &&
1495 ((flags & SENDFORWARD) == 0 ||
1496 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1497 query_quadoption (OPT_FORWEDIT, _("Edit forwarded message?")) == M_YES))
1499 /* If the this isn't a text message, look for a mailcap edit command */
1500 if (mutt_needs_mailcap (msg->content))
1501 mutt_edit_attachment (msg->content);
1502 else if (!Editor || mutt_strcmp ("builtin", Editor) == 0)
1503 mutt_builtin_editor (msg->content->filename, msg, cur);
1504 else if (option (OPTEDITHDRS))
1506 mutt_env_to_local (msg->env);
1507 mutt_edit_headers (Editor, msg->content->filename, msg, fcc, sizeof (fcc));
1508 mutt_env_to_idna (msg->env, NULL, NULL);
1511 mutt_edit_file (Editor, msg->content->filename);
1513 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1516 if (! (flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND)))
1518 if (stat (msg->content->filename, &st) == 0)
1520 /* if the file was not modified, bail out now */
1521 if (mtime == st.st_mtime && !msg->content->next &&
1522 query_quadoption (OPT_ABORT, _("Abort unmodified message?")) == M_YES)
1524 mutt_message _("Aborted unmodified message.");
1529 mutt_perror (msg->content->filename);
1533 /* specify a default fcc. if we are in batchmode, only save a copy of
1534 * the message if the value of $copy is yes or ask-yes */
1536 if (!fcc[0] && !(flags & (SENDPOSTPONED)) && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1)))
1538 /* set the default FCC */
1539 if (!msg->env->from)
1541 msg->env->from = mutt_default_from ();
1542 killfrom = 1; /* no need to check $use_from because if the user specified
1543 a from address it would have already been set by now */
1545 mutt_select_fcc (fcc, sizeof (fcc), msg);
1548 rfc822_free_address (&msg->env->from);
1554 mutt_update_encoding (msg->content);
1556 if (! (flags & (SENDMAILX | SENDBATCH)))
1560 fcc_error = 0; /* reset value since we may have failed before */
1561 mutt_pretty_mailbox (fcc);
1562 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1567 if (flags & SENDNEWS)
1568 mutt_message _("Article not posted.");
1571 mutt_message _("Mail not sent.");
1576 /* postpone the message until later. */
1577 if (msg->content->next)
1578 msg->content = mutt_make_multipart (msg->content);
1581 * make sure the message is written to the right part of a maildir
1584 msg->read = 0; msg->old = 0;
1586 encode_descriptions (msg->content, 1);
1587 mutt_prepare_envelope (msg->env, 0);
1588 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1590 if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, 1, fcc) < 0)
1592 msg->content = mutt_remove_multipart (msg->content);
1593 decode_descriptions (msg->content);
1594 mutt_unprepare_envelope (msg->env);
1597 mutt_update_num_postponed ();
1598 mutt_message _("Message postponed.");
1604 if (!(flags & SENDNEWS))
1606 if (!msg->env->to && !msg->env->cc && !msg->env->bcc)
1608 if (! (flags & SENDBATCH))
1610 mutt_error _("No recipients are specified!");
1615 puts _("No recipients were specified.");
1620 if (mutt_env_to_idna (msg->env, &tag, &err))
1622 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1624 if (!(flags & SENDBATCH))
1630 if (!msg->env->subject && ! (flags & SENDBATCH) &&
1631 (i = query_quadoption (OPT_SUBJECT, _("No subject, abort sending?"))) != M_NO)
1633 /* if the abort is automatic, print an error message */
1634 if (quadoption (OPT_SUBJECT) == M_YES)
1635 mutt_error _("No subject specified.");
1639 if ((flags & SENDNEWS) && !msg->env->subject)
1641 mutt_error _("No subject specified.");
1645 if ((flags & SENDNEWS) && !msg->env->newsgroups)
1647 mutt_error _("No newsgroup specified.");
1652 if (msg->content->next)
1653 msg->content = mutt_make_multipart (msg->content);
1656 * Ok, we need to do it this way instead of handling all fcc stuff in
1657 * one place in order to avoid going to main_loop with encoded "env"
1658 * in case of error. Ugh.
1661 encode_descriptions (msg->content, 1);
1664 * Make sure that clear_content and free_clear_content are
1665 * properly initialized -- we may visit this particular place in
1666 * the code multiple times, including after a failed call to
1670 clear_content = NULL;
1671 free_clear_content = 0;
1677 /* save the decrypted attachments */
1678 clear_content = msg->content;
1680 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1681 mutt_protect (msg, pgpkeylist) == -1)
1683 msg->content = mutt_remove_multipart (msg->content);
1687 decode_descriptions (msg->content);
1690 encode_descriptions (msg->content, 0);
1694 * at this point, msg->content is one of the following three things:
1695 * - multipart/signed. In this case, clear_content is a child.
1696 * - multipart/encrypted. In this case, clear_content exists
1698 * - application/pgp. In this case, clear_content exists independently.
1699 * - something else. In this case, it's the same as clear_content.
1702 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1704 if (clear_content && (msg->content != clear_content)
1705 && (msg->content->parts != clear_content))
1706 free_clear_content = 1;
1709 if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1710 mutt_message _("Sending message...");
1712 mutt_prepare_envelope (msg->env, 1);
1714 /* save a copy of the message, if necessary. */
1716 mutt_expand_path (fcc, sizeof (fcc));
1719 /* Don't save a copy when we are in batch-mode, and the FCC
1720 * folder is on an IMAP server: This would involve possibly lots
1721 * of user interaction, which is not available in batch mode.
1723 * Note: A patch to fix the problems with the use of IMAP servers
1724 * from non-curses mode is available from Brendan Cully. However,
1725 * I'd like to think a bit more about this before including it.
1729 if ((flags & SENDBATCH) && fcc[0] && mx_is_imap (fcc))
1733 if (*fcc && mutt_strcmp ("/dev/null", fcc) != 0)
1735 BODY *tmpbody = msg->content;
1736 BODY *save_sig = NULL;
1737 BODY *save_parts = NULL;
1739 if (WithCrypto && msg->security && option (OPTFCCCLEAR))
1740 msg->content = clear_content;
1742 /* check to see if the user wants copies of all attachments */
1743 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART)
1746 && (mutt_strcmp (msg->content->subtype, "encrypted") == 0 ||
1747 mutt_strcmp (msg->content->subtype, "signed") == 0))
1749 if (clear_content->type == TYPEMULTIPART)
1751 if(!(msg->security & ENCRYPT) && (msg->security & SIGN))
1753 /* save initial signature and attachments */
1754 save_sig = msg->content->parts->next;
1755 save_parts = clear_content->parts->next;
1758 /* this means writing only the main part */
1759 msg->content = clear_content->parts;
1761 if (mutt_protect (msg, pgpkeylist) == -1)
1763 /* we can't do much about it at this point, so
1764 * fallback to saving the whole thing to fcc
1766 msg->content = tmpbody;
1771 save_content = msg->content;
1775 msg->content = msg->content->parts;
1781 /* update received time so that when storing to a mbox-style folder
1782 * the From_ line contains the current time instead of when the
1783 * message was first postponed.
1785 msg->received = time (NULL);
1786 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1)
1789 * Error writing FCC, we should abort sending.
1795 msg->content = tmpbody;
1797 if (WithCrypto && save_sig)
1799 /* cleanup the second signature structures */
1800 if (save_content->parts)
1802 mutt_free_body (&save_content->parts->next);
1803 save_content->parts = NULL;
1805 mutt_free_body (&save_content);
1807 /* restore old signature and attachments */
1808 msg->content->parts->next = save_sig;
1809 msg->content->parts->parts->next = save_parts;
1811 else if (WithCrypto && save_content)
1813 /* destroy the new encrypted body. */
1814 mutt_free_body (&save_content);
1821 * Don't attempt to send the message if the FCC failed. Just pretend
1822 * the send failed as well so we give the user a chance to fix the
1825 if (fcc_error || (i = send_message (msg)) == -1)
1827 if (!(flags & SENDBATCH))
1831 else if ((msg->security & ENCRYPT) ||
1832 ((msg->security & SIGN)
1833 && msg->content->type == TYPEAPPLICATION))
1835 mutt_free_body (&msg->content); /* destroy PGP data */
1836 msg->content = clear_content; /* restore clear text. */
1838 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART)
1840 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1841 msg->content = mutt_remove_multipart (msg->content);
1844 msg->content = mutt_remove_multipart (msg->content);
1845 decode_descriptions (msg->content);
1846 mutt_unprepare_envelope (msg->env);
1851 puts _("Could not send the message.");
1855 else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX))
1856 mutt_message (i != 0 ? _("Sending in background.") :
1858 (flags & SENDNEWS) ? _("Article posted.") : _("Mail sent."));
1863 if (WithCrypto && (msg->security & ENCRYPT))
1866 if (WithCrypto && free_clear_content)
1867 mutt_free_body (&clear_content);
1869 if (flags & SENDREPLY)
1872 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1873 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged)
1875 for (i = 0; i < ctx->vcount; i++)
1876 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1877 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1886 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1895 safe_fclose (&tempfp);
1896 mutt_free_header (&msg);
1901 /* vim: set sw=2: */