2 * Copyright (C) 1996-2002,2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_curses.h"
30 #include "mutt_crypt.h"
31 #include "mutt_idna.h"
44 #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 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)
218 char buf[HUGE_STRING];
219 LIST *uh = UserHeader;
221 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
223 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
225 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
230 if (option (OPTFASTREPLY))
233 strfcpy (buf, en->subject, sizeof (buf));
240 for (; uh; uh = uh->next)
242 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0)
246 strncpy (buf, p, sizeof (buf));
251 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 ||
252 (!buf[0] && query_quadoption (OPT_SUBJECT, _("No subject, abort?")) != M_NO))
254 mutt_message _("No subject, aborting.");
257 mutt_str_replace (&en->subject, buf);
262 static void process_user_recips (ENVELOPE *env)
264 LIST *uh = UserHeader;
266 for (; uh; uh = uh->next)
268 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
269 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
270 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
271 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
272 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
273 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
277 static void process_user_header (ENVELOPE *env)
279 LIST *uh = UserHeader;
280 LIST *last = env->userhdrs;
286 for (; uh; uh = uh->next)
288 if (ascii_strncasecmp ("from:", uh->data, 5) == 0)
290 /* User has specified a default From: address. Remove default address */
291 rfc822_free_address (&env->from);
292 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
294 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0)
296 rfc822_free_address (&env->reply_to);
297 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
299 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
301 char *tmp = mutt_extract_message_id (uh->data + 11, NULL);
302 if (rfc822_valid_msgid (tmp) >= 0)
304 FREE(&env->message_id);
305 env->message_id = tmp;
309 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
310 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
311 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
312 ascii_strncasecmp ("subject:", uh->data, 8) != 0 &&
313 ascii_strncasecmp ("return-path:", uh->data, 12) != 0)
317 last->next = mutt_new_list ();
321 last = env->userhdrs = mutt_new_list ();
322 last->data = safe_strdup (uh->data);
327 LIST *mutt_copy_list (LIST *p)
329 LIST *t, *r=NULL, *l=NULL;
331 for (; p; p = p->next)
333 t = (LIST *) safe_malloc (sizeof (LIST));
334 t->data = safe_strdup (p->data);
347 void mutt_forward_intro (FILE *fp, HEADER *cur)
351 fputs ("----- Forwarded message from ", fp);
353 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
355 fputs (" -----\n\n", fp);
358 void mutt_forward_trailer (FILE *fp)
360 fputs ("\n----- End forwarded message -----\n", fp);
364 static int include_forward (CONTEXT *ctx, HEADER *cur, FILE *out)
366 int chflags = CH_DECODE, cmflags = 0;
368 mutt_parse_mime_message (ctx, cur);
369 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
371 if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE))
373 /* make sure we have the user's passphrase before proceeding... */
374 crypt_valid_passphrase (cur->security);
377 mutt_forward_intro (out, cur);
379 if (option (OPTFORWDECODE))
381 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
382 if (option (OPTWEED))
384 chflags |= CH_WEED | CH_REORDER;
385 cmflags |= M_CM_WEED;
388 if (option (OPTFORWQUOTE))
389 cmflags |= M_CM_PREFIX;
391 /* wrapping headers for forwarding is considered a display
392 * rather than send action */
393 chflags |= CH_DISPLAY;
395 mutt_copy_message (out, ctx, cur, cmflags, chflags);
396 mutt_forward_trailer (out);
400 void mutt_make_attribution (CONTEXT *ctx, HEADER *cur, FILE *out)
405 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
411 void mutt_make_post_indent (CONTEXT *ctx, HEADER *cur, FILE *out)
414 if (PostIndentString)
416 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
422 static int include_reply (CONTEXT *ctx, HEADER *cur, FILE *out)
424 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
425 int chflags = CH_DECODE;
427 if (WithCrypto && (cur->security & ENCRYPT))
429 /* make sure we have the user's passphrase before proceeding... */
430 crypt_valid_passphrase (cur->security);
433 mutt_parse_mime_message (ctx, cur);
434 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
436 mutt_make_attribution (ctx, cur, out);
438 if (!option (OPTHEADER))
439 cmflags |= M_CM_NOHEADER;
440 if (option (OPTWEED))
442 chflags |= CH_WEED | CH_REORDER;
443 cmflags |= M_CM_WEED;
446 mutt_copy_message (out, ctx, cur, cmflags, chflags);
448 mutt_make_post_indent (ctx, cur, out);
453 static int default_to (ADDRESS **to, ENVELOPE *env, int flags, int hmfupto)
457 if (flags && env->mail_followup_to && hmfupto == M_YES)
459 rfc822_append (to, env->mail_followup_to, 1);
463 /* Exit now if we're setting up the default Cc list for list-reply
464 * (only set if Mail-Followup-To is present and honoured).
466 if (flags & SENDLISTREPLY)
469 if (!option(OPTREPLYSELF) && mutt_addr_is_user (env->from))
471 /* mail is from the user, assume replying to recipients */
472 rfc822_append (to, env->to, 1);
474 else if (env->reply_to)
476 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
477 (option (OPTIGNORELISTREPLYTO) &&
478 mutt_is_mail_list (env->reply_to) &&
479 (mutt_addrsrc (env->reply_to, env->to) ||
480 mutt_addrsrc (env->reply_to, env->cc))))
482 /* If the Reply-To: address is a mailing list, assume that it was
483 * put there by the mailing list, and use the From: address
485 * We also take the from header if our correspondant has a reply-to
486 * header which is identical to the electronic mail address given
487 * in his From header.
490 rfc822_append (to, env->from, 0);
492 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
493 !env->reply_to->next) &&
494 quadoption (OPT_REPLYTO) != M_YES)
496 /* There are quite a few mailing lists which set the Reply-To:
497 * header field to the list address, which makes it quite impossible
498 * to send a message to only the sender of the message. This
499 * provides a way to do that.
501 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
502 env->reply_to->mailbox,
503 env->reply_to->next?",...":"");
504 switch (query_quadoption (OPT_REPLYTO, prompt))
507 rfc822_append (to, env->reply_to, 0);
511 rfc822_append (to, env->from, 0);
515 return (-1); /* abort */
519 rfc822_append (to, env->reply_to, 0);
522 rfc822_append (to, env->from, 0);
527 int mutt_fetch_recips (ENVELOPE *out, ENVELOPE *in, int flags)
533 if ((flags & (SENDLISTREPLY|SENDGROUPREPLY)) && in->mail_followup_to)
535 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
536 in->mail_followup_to->mailbox,
537 in->mail_followup_to->next ? ",..." : "");
539 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
543 if (flags & SENDLISTREPLY)
545 tmp = find_mailing_lists (in->to, in->cc);
546 rfc822_append (&out->to, tmp, 0);
547 rfc822_free_address (&tmp);
549 if (in->mail_followup_to && hmfupto == M_YES &&
550 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
551 return (-1); /* abort */
555 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
556 return (-1); /* abort */
558 if ((flags & SENDGROUPREPLY) && (!in->mail_followup_to || hmfupto != M_YES))
560 /* if(!mutt_addr_is_user(in->to)) */
561 rfc822_append (&out->cc, in->to, 1);
562 rfc822_append (&out->cc, in->cc, 1);
568 LIST *mutt_make_references(ENVELOPE *e)
570 LIST *t = NULL, *l = NULL;
573 l = mutt_copy_list (e->references);
575 l = mutt_copy_list (e->in_reply_to);
580 t->data = safe_strdup(e->message_id);
588 void mutt_fix_reply_recipients (ENVELOPE *env)
590 if (! option (OPTMETOO))
592 /* the order is important here. do the CC: first so that if the
593 * the user is the only recipient, it ends up on the TO: field
595 env->cc = remove_user (env->cc, (env->to == NULL));
596 env->to = remove_user (env->to, (env->cc == NULL));
599 /* the CC field can get cluttered, especially with lists */
600 env->to = mutt_remove_duplicates (env->to);
601 env->cc = mutt_remove_duplicates (env->cc);
602 env->cc = mutt_remove_xrefs (env->to, env->cc);
604 if (env->cc && !env->to)
611 void mutt_make_forward_subject (ENVELOPE *env, CONTEXT *ctx, HEADER *cur)
615 /* set the default subject for the message. */
616 mutt_make_string (buffer, sizeof (buffer), NONULL(ForwFmt), ctx, cur);
617 mutt_str_replace (&env->subject, buffer);
620 void mutt_make_misc_reply_headers (ENVELOPE *env, CONTEXT *ctx,
621 HEADER *cur, ENVELOPE *curenv)
623 /* This takes precedence over a subject that might have
624 * been taken from a List-Post header. Is that correct?
626 if (curenv->real_subj)
628 FREE (&env->subject);
629 env->subject = safe_malloc (mutt_strlen (curenv->real_subj) + 5);
630 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
632 else if (!env->subject)
633 env->subject = safe_strdup ("Re: your mail");
636 void mutt_add_to_reference_headers (ENVELOPE *env, ENVELOPE *curenv, LIST ***pp, LIST ***qq)
638 LIST **p = NULL, **q = NULL;
643 if (!p) p = &env->references;
644 if (!q) q = &env->in_reply_to;
646 while (*p) p = &(*p)->next;
647 while (*q) q = &(*q)->next;
649 *p = mutt_make_references (curenv);
651 if (curenv->message_id)
653 *q = mutt_new_list();
654 (*q)->data = safe_strdup (curenv->message_id);
663 mutt_make_reference_headers (ENVELOPE *curenv, ENVELOPE *env, CONTEXT *ctx)
665 env->references = NULL;
666 env->in_reply_to = NULL;
671 LIST **p = NULL, **q = NULL;
674 for(i = 0; i < ctx->vcount; i++)
676 h = ctx->hdrs[ctx->v2r[i]];
678 mutt_add_to_reference_headers (env, h->env, &p, &q);
682 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
684 /* if there's more than entry in In-Reply-To (i.e. message has
685 multiple parents), don't generate a References: header as it's
686 discouraged by RfC2822, sect. 3.6.4 */
687 if (ctx->tagged > 0 && env->in_reply_to && env->in_reply_to->next)
688 mutt_free_list (&env->references);
692 envelope_defaults (ENVELOPE *env, CONTEXT *ctx, HEADER *cur, int flags)
694 ENVELOPE *curenv = NULL;
700 for (i = 0; i < ctx->vcount; i++)
701 if (ctx->hdrs[ctx->v2r[i]]->tagged)
703 cur = ctx->hdrs[ctx->v2r[i]];
710 /* This could happen if the user tagged some messages and then did
711 * a limit such that none of the tagged message are visible.
713 mutt_error _("No tagged messages are visible!");
720 if (flags & SENDREPLY)
726 for (i = 0; i < ctx->vcount; i++)
728 h = ctx->hdrs[ctx->v2r[i]];
729 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
733 else if (mutt_fetch_recips (env, curenv, flags) == -1)
736 if ((flags & SENDLISTREPLY) && !env->to)
738 mutt_error _("No mailing lists found!");
742 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
743 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
745 else if (flags & SENDFORWARD)
746 mutt_make_forward_subject (env, ctx, cur);
752 generate_body (FILE *tempfp, /* stream for outgoing message */
753 HEADER *msg, /* header for outgoing message */
754 int flags, /* compose mode */
755 CONTEXT *ctx, /* current mailbox */
756 HEADER *cur) /* current message */
762 if (flags & SENDREPLY)
764 if ((i = query_quadoption (OPT_INCLUDE, _("Include message in reply?"))) == -1)
769 mutt_message _("Including quoted message...");
772 for (i = 0; i < ctx->vcount; i++)
774 h = ctx->hdrs[ctx->v2r[i]];
777 if (include_reply (ctx, h, tempfp) == -1)
779 mutt_error _("Could not include all requested messages!");
782 fputc ('\n', tempfp);
787 include_reply (ctx, cur, tempfp);
791 else if (flags & SENDFORWARD)
793 if ((i = query_quadoption (OPT_MIMEFWD, _("Forward as attachment?"))) == M_YES)
795 BODY *last = msg->content;
797 mutt_message _("Preparing forwarded message...");
799 while (last && last->next)
804 tmp = mutt_make_message_attach (ctx, cur, 0);
812 for (i = 0; i < ctx->vcount; i++)
814 if (ctx->hdrs[ctx->v2r[i]]->tagged)
816 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
823 last = msg->content = tmp;
831 include_forward (ctx, cur, tempfp);
833 for (i=0; i < ctx->vcount; i++)
834 if (ctx->hdrs[ctx->v2r[i]]->tagged)
835 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
840 /* if (WithCrypto && (flags & SENDKEY)) */
841 else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY))
845 if ((WithCrypto & APPLICATION_PGP)
846 && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
849 tmp->next = msg->content;
858 void mutt_set_followup_to (ENVELOPE *e)
864 * Only generate the Mail-Followup-To if the user has requested it, and
865 * it hasn't already been set
868 if (option (OPTFOLLOWUPTO) && !e->mail_followup_to)
870 if (mutt_is_list_cc (0, e->to, e->cc))
873 * this message goes to known mailing lists, so create a proper
874 * mail-followup-to header
877 t = rfc822_append (&e->mail_followup_to, e->to, 0);
878 rfc822_append (&t, e->cc, 1);
881 /* remove ourselves from the mail-followup-to header */
882 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
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.
891 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc))
894 from = rfc822_cpy_adr (e->reply_to, 0);
896 from = rfc822_cpy_adr (e->from, 0);
898 from = mutt_default_from ();
902 /* Normally, this loop will not even be entered. */
903 for (t = from; t && t->next; t = t->next)
906 t->next = e->mail_followup_to; /* t cannot be NULL at this point. */
907 e->mail_followup_to = from;
911 e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
917 /* look through the recipients of the message we are replying to, and if
918 we find an address that matches $alternates, we use that as the default
920 static ADDRESS *set_reverse_name (ENVELOPE *env)
924 for (tmp = env->to; tmp; tmp = tmp->next)
926 if (mutt_addr_is_user (tmp))
931 for (tmp = env->cc; tmp; tmp = tmp->next)
933 if (mutt_addr_is_user (tmp))
937 if (!tmp && mutt_addr_is_user (env->from))
941 tmp = rfc822_cpy_adr_real (tmp);
942 /* when $reverse_realname is not set, clear the personal name so that it
943 * may be set vi a reply- or send-hook.
945 if (!option (OPTREVREAL))
946 FREE (&tmp->personal);
951 ADDRESS *mutt_default_from (void)
954 const char *fqdn = mutt_fqdn(1);
957 * Note: We let $from override $realname here. Is this the right
962 adr = rfc822_cpy_adr_real (From);
963 else if (option (OPTUSEDOMAIN))
965 adr = rfc822_new_address ();
966 adr->mailbox = safe_malloc (mutt_strlen (Username) + mutt_strlen (fqdn) + 2);
967 sprintf (adr->mailbox, "%s@%s", NONULL(Username), NONULL(fqdn)); /* __SPRINTF_CHECKED__ */
971 adr = rfc822_new_address ();
972 adr->mailbox = safe_strdup (NONULL(Username));
978 static int send_message (HEADER *msg)
980 char tempfile[_POSIX_PATH_MAX];
987 /* Write out the message in MIME form. */
988 mutt_mktemp (tempfile, sizeof (tempfile));
989 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
993 old_write_bcc = option (OPTWRITEBCC);
995 unset_option (OPTWRITEBCC);
998 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, msg->chain ? 1 : 0);
1001 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1005 set_option (OPTWRITEBCC);
1008 fputc ('\n', tempfp); /* tie off the header. */
1010 if ((mutt_write_mime_body (msg->content, tempfp) == -1))
1012 safe_fclose (&tempfp);
1017 if (fclose (tempfp) != 0)
1019 mutt_perror (tempfile);
1026 return mix_send_message (msg->chain, tempfile);
1031 return mutt_smtp_send (msg->env->from, msg->env->to, msg->env->cc,
1032 msg->env->bcc, tempfile,
1033 (msg->content->encoding == ENC8BIT));
1034 #endif /* USE_SMTP */
1036 i = mutt_invoke_sendmail (msg->env->from, msg->env->to, msg->env->cc,
1037 msg->env->bcc, tempfile,
1038 (msg->content->encoding == ENC8BIT));
1042 /* rfc2047 encode the content-descriptions */
1043 static void encode_descriptions (BODY *b, short recurse)
1047 for (t = b; t; t = t->next)
1051 rfc2047_encode_string (&t->description);
1053 if (recurse && t->parts)
1054 encode_descriptions (t->parts, recurse);
1058 /* rfc2047 decode them in case of an error */
1059 static void decode_descriptions (BODY *b)
1063 for (t = b; t; t = t->next)
1067 rfc2047_decode (&t->description);
1070 decode_descriptions (t->parts);
1074 static void fix_end_of_file (const char *data)
1079 if ((fp = safe_fopen (data, "a+")) == NULL)
1081 fseek (fp,-1,SEEK_END);
1082 if ((c = fgetc(fp)) != '\n')
1087 int mutt_resend_message (FILE *fp, CONTEXT *ctx, HEADER *cur)
1089 HEADER *msg = mutt_new_header ();
1091 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1094 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1097 static int is_reply (HEADER *reply, HEADER *orig)
1099 return mutt_find_list (orig->env->references, reply->env->message_id) ||
1100 mutt_find_list (orig->env->in_reply_to, reply->env->message_id);
1103 static int has_recips (ADDRESS *a)
1107 for ( ; a; a = a->next)
1109 if (!a->mailbox || a->group)
1117 ci_send_message (int flags, /* send mode */
1118 HEADER *msg, /* template to use for new message */
1119 char *tempfile, /* file specified by -i or -H */
1120 CONTEXT *ctx, /* current mailbox */
1121 HEADER *cur) /* current message */
1123 char buffer[LONG_STRING];
1124 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1125 FILE *tempfp = NULL;
1127 int i, killfrom = 0;
1129 int free_clear_content = 0;
1131 BODY *save_content = NULL;
1132 BODY *clear_content = NULL;
1133 char *pgpkeylist = NULL;
1134 /* save current value of "pgp_sign_as" */
1135 char *signas = NULL;
1136 char *tag = NULL, *err = NULL;
1141 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1142 mutt_num_postponed (1))
1144 /* If the user is composing a new message, check to see if there
1145 * are any postponed messages first.
1147 if ((i = query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1151 flags |= SENDPOSTPONED;
1155 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1156 signas = safe_strdup(PgpSignAs);
1158 /* Delay expansion of aliases until absolutely necessary--shouldn't
1159 * be necessary unless we are prompting the user or about to execute a
1165 msg = mutt_new_header ();
1167 if (flags == SENDPOSTPONED)
1169 if ((flags = mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1173 if (flags & (SENDPOSTPONED|SENDRESEND))
1175 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL)
1177 mutt_perror (msg->content->filename);
1183 msg->env = mutt_new_envelope ();
1186 /* Parse and use an eventual list-post header */
1187 if ((flags & SENDLISTREPLY)
1188 && cur && cur->env && cur->env->list_post)
1190 /* Use any list-post header as a template */
1191 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1192 /* We don't let them set the sender's address. */
1193 rfc822_free_address (&msg->env->from);
1196 if (! (flags & (SENDKEY | SENDPOSTPONED | SENDRESEND)))
1198 pbody = mutt_new_body ();
1199 pbody->next = msg->content; /* don't kill command-line attachments */
1200 msg->content = pbody;
1202 if (!(ctype = safe_strdup (ContentType)))
1203 ctype = safe_strdup ("text/plain");
1204 mutt_parse_content_type (ctype, msg->content);
1206 msg->content->unlink = 1;
1207 msg->content->use_disp = 0;
1208 msg->content->disposition = DISPINLINE;
1209 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT && !ascii_strcasecmp (msg->content->subtype, "plain"))
1210 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1214 mutt_mktemp (buffer, sizeof (buffer));
1215 tempfp = safe_fopen (buffer, "w+");
1216 msg->content->filename = safe_strdup (buffer);
1220 tempfp = safe_fopen (tempfile, "a+");
1221 msg->content->filename = safe_strdup (tempfile);
1226 dprint(1,(debugfile, "newsend_message: can't create tempfile %s (errno=%d)\n", msg->content->filename, errno));
1227 mutt_perror (msg->content->filename);
1232 /* this is handled here so that the user can match ~f in send-hook */
1233 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED|SENDRESEND)))
1235 /* we shouldn't have to worry about freeing `msg->env->from' before
1236 * setting it here since this code will only execute when doing some
1237 * sort of reply. the pointer will only be set when using the -H command
1240 * We shouldn't have to worry about alias expansion here since we are
1241 * either replying to a real or postponed message, therefore no aliases
1242 * should exist since the user has not had the opportunity to add
1243 * addresses to the list. We just have to ensure the postponed messages
1244 * have their aliases expanded.
1247 msg->env->from = set_reverse_name (cur->env);
1250 if (! (flags & (SENDPOSTPONED|SENDRESEND)))
1252 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1253 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1256 if (option (OPTHDRS))
1257 process_user_recips (msg->env);
1259 /* Expand aliases and remove duplicates/crossrefs */
1260 mutt_expand_aliases_env (msg->env);
1262 if (flags & SENDREPLY)
1263 mutt_fix_reply_recipients (msg->env);
1265 if (! (flags & (SENDMAILX|SENDBATCH)) &&
1266 ! (option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1267 ! ((flags & SENDREPLY) && option (OPTFASTREPLY)))
1269 if (edit_envelope (msg->env) == -1)
1273 /* the from address must be set here regardless of whether or not
1274 * $use_from is set so that the `~P' (from you) operator in send-hook
1275 * patterns will work. if $use_from is unset, the from address is killed
1276 * after send-hooks are evaulated */
1278 if (!msg->env->from)
1280 msg->env->from = mutt_default_from ();
1284 if ((flags & SENDREPLY) && cur)
1286 /* change setting based upon message we are replying to */
1287 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1290 * set the replied flag for the message we are generating so that the
1291 * user can use ~Q in a send-hook to know when reply-hook's are also
1297 /* change settings based upon recipients */
1299 mutt_message_hook (NULL, msg, M_SENDHOOK);
1302 * Unset the replied flag from the message we are composing since it is
1303 * no longer required. This is done here because the FCC'd copy of
1304 * this message was erroneously get the 'R'eplied flag when stored in
1305 * a maildir-style mailbox.
1309 /* $use_from and/or $from might have changed in a send-hook */
1312 rfc822_free_address (&msg->env->from);
1313 if (option (OPTUSEFROM) && !(flags & (SENDPOSTPONED|SENDRESEND)))
1314 msg->env->from = mutt_default_from ();
1318 if (option (OPTHDRS))
1319 process_user_header (msg->env);
1321 if (flags & SENDBATCH)
1322 mutt_copy_stream (stdin, tempfp);
1324 if (option (OPTSIGONTOP) && ! (flags & (SENDMAILX|SENDKEY|SENDBATCH))
1325 && Editor && mutt_strcmp (Editor, "builtin") != 0)
1326 append_signature (tempfp);
1328 /* include replies/forwarded messages, unless we are given a template */
1329 if (!tempfile && (ctx || !(flags & (SENDREPLY|SENDFORWARD)))
1330 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1333 if (!option (OPTSIGONTOP) && ! (flags & (SENDMAILX|SENDKEY|SENDBATCH))
1334 && Editor && mutt_strcmp (Editor, "builtin") != 0)
1335 append_signature (tempfp);
1339 * This hook is even called for postponed messages, and can, e.g., be
1340 * used for setting the editor, the sendmail path, or the
1343 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1345 /* wait until now to set the real name portion of our return address so
1346 that $realname can be set in a send-hook */
1347 if (msg->env->from && !msg->env->from->personal
1348 && !(flags & (SENDRESEND|SENDPOSTPONED)))
1349 msg->env->from->personal = safe_strdup (Realname);
1351 if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
1352 safe_fclose (&tempfp);
1354 if (flags & SENDMAILX)
1356 if (mutt_builtin_editor (msg->content->filename, msg, cur) == -1)
1359 else if (! (flags & SENDBATCH))
1362 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1364 mutt_update_encoding (msg->content);
1367 * Select whether or not the user's editor should be called now. We
1368 * don't want to do this when:
1369 * 1) we are sending a key/cert
1370 * 2) we are forwarding a message and the user doesn't want to edit it.
1371 * This is controlled by the quadoption $forward_edit. However, if
1372 * both $edit_headers and $autoedit are set, we want to ignore the
1373 * setting of $forward_edit because the user probably needs to add the
1376 if (! (flags & SENDKEY) &&
1377 ((flags & SENDFORWARD) == 0 ||
1378 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1379 query_quadoption (OPT_FORWEDIT, _("Edit forwarded message?")) == M_YES))
1381 /* If the this isn't a text message, look for a mailcap edit command */
1382 if (mutt_needs_mailcap (msg->content))
1384 if (!mutt_edit_attachment (msg->content))
1387 else if (!Editor || mutt_strcmp ("builtin", Editor) == 0)
1388 mutt_builtin_editor (msg->content->filename, msg, cur);
1389 else if (option (OPTEDITHDRS))
1391 mutt_env_to_local (msg->env);
1392 mutt_edit_headers (Editor, msg->content->filename, msg, fcc, sizeof (fcc));
1393 mutt_env_to_idna (msg->env, NULL, NULL);
1397 mutt_edit_file (Editor, msg->content->filename);
1398 if (stat (msg->content->filename, &st) == 0)
1400 if (mtime != st.st_mtime)
1401 fix_end_of_file (msg->content->filename);
1404 mutt_perror (msg->content->filename);
1407 if (option (OPTTEXTFLOWED))
1408 rfc3676_space_stuff (msg);
1410 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1413 if (! (flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND)))
1415 if (stat (msg->content->filename, &st) == 0)
1417 /* if the file was not modified, bail out now */
1418 if (mtime == st.st_mtime && !msg->content->next &&
1419 query_quadoption (OPT_ABORT, _("Abort unmodified message?")) == M_YES)
1421 mutt_message _("Aborted unmodified message.");
1426 mutt_perror (msg->content->filename);
1431 * Set the message security unless:
1432 * 1) crypto support is not enabled (WithCrypto==0)
1433 * 2) pgp: header field was present during message editing with $edit_headers (msg->security != 0)
1434 * 3) we are resending a message
1435 * 4) we are recalling a postponed message (don't override the user's saved settings)
1436 * 5) we are in mailx mode
1437 * 6) we are in batch mode
1439 * This is done after allowing the user to edit the message so that security
1440 * settings can be configured with send2-hook and $edit_headers.
1442 if (WithCrypto && (msg->security == 0) && !(flags & (SENDBATCH | SENDMAILX | SENDPOSTPONED | SENDRESEND)))
1444 if (option (OPTCRYPTAUTOSIGN))
1445 msg->security |= SIGN;
1446 if (option (OPTCRYPTAUTOENCRYPT))
1447 msg->security |= ENCRYPT;
1448 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1449 msg->security |= ENCRYPT;
1450 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1451 msg->security |= SIGN;
1452 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur && (cur->security & ENCRYPT))
1453 msg->security |= SIGN;
1454 if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN)))
1456 if (option (OPTPGPAUTOINLINE))
1457 msg->security |= INLINE;
1458 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1459 msg->security |= INLINE;
1465 * When replying / forwarding, use the original message's
1466 * crypto system. According to the documentation,
1467 * smime_is_default should be disregarded here.
1469 * Problem: At least with forwarding, this doesn't really
1470 * make much sense. Should we have an option to completely
1471 * disable individual mechanisms at run-time?
1475 if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP)
1476 && (cur->security & APPLICATION_PGP))
1477 msg->security |= APPLICATION_PGP;
1478 else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1479 && (cur->security & APPLICATION_SMIME))
1480 msg->security |= APPLICATION_SMIME;
1484 * No crypto mechanism selected? Use availability + smime_is_default
1487 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1489 if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1490 && option (OPTSMIMEISDEFAULT))
1491 msg->security |= APPLICATION_SMIME;
1492 else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
1493 msg->security |= APPLICATION_PGP;
1494 else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME))
1495 msg->security |= APPLICATION_SMIME;
1499 /* No permissible mechanisms found. Don't sign or encrypt. */
1500 if (!(msg->security & (APPLICATION_SMIME|APPLICATION_PGP)))
1504 /* specify a default fcc. if we are in batchmode, only save a copy of
1505 * the message if the value of $copy is yes or ask-yes */
1507 if (!fcc[0] && !(flags & (SENDPOSTPONED)) && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1)))
1509 /* set the default FCC */
1510 if (!msg->env->from)
1512 msg->env->from = mutt_default_from ();
1513 killfrom = 1; /* no need to check $use_from because if the user specified
1514 a from address it would have already been set by now */
1516 mutt_select_fcc (fcc, sizeof (fcc), msg);
1519 rfc822_free_address (&msg->env->from);
1525 mutt_update_encoding (msg->content);
1527 if (! (flags & (SENDMAILX | SENDBATCH)))
1531 fcc_error = 0; /* reset value since we may have failed before */
1532 mutt_pretty_mailbox (fcc, sizeof (fcc));
1533 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1537 mutt_message _("Mail not sent.");
1542 /* postpone the message until later. */
1543 if (msg->content->next)
1544 msg->content = mutt_make_multipart (msg->content);
1547 * make sure the message is written to the right part of a maildir
1550 msg->read = 0; msg->old = 0;
1552 encode_descriptions (msg->content, 1);
1553 mutt_prepare_envelope (msg->env, 0);
1554 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1556 if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, 1, fcc) < 0)
1558 msg->content = mutt_remove_multipart (msg->content);
1559 decode_descriptions (msg->content);
1560 mutt_unprepare_envelope (msg->env);
1563 mutt_update_num_postponed ();
1564 mutt_message _("Message postponed.");
1569 if (!has_recips (msg->env->to) && !has_recips (msg->env->cc) &&
1570 !has_recips (msg->env->bcc))
1572 if (! (flags & SENDBATCH))
1574 mutt_error _("No recipients are specified!");
1579 puts _("No recipients were specified.");
1584 if (mutt_env_to_idna (msg->env, &tag, &err))
1586 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1588 if (!(flags & SENDBATCH))
1594 if (!msg->env->subject && ! (flags & SENDBATCH) &&
1595 (i = query_quadoption (OPT_SUBJECT, _("No subject, abort sending?"))) != M_NO)
1597 /* if the abort is automatic, print an error message */
1598 if (quadoption (OPT_SUBJECT) == M_YES)
1599 mutt_error _("No subject specified.");
1603 if (msg->content->next)
1604 msg->content = mutt_make_multipart (msg->content);
1607 * Ok, we need to do it this way instead of handling all fcc stuff in
1608 * one place in order to avoid going to main_loop with encoded "env"
1609 * in case of error. Ugh.
1612 encode_descriptions (msg->content, 1);
1615 * Make sure that clear_content and free_clear_content are
1616 * properly initialized -- we may visit this particular place in
1617 * the code multiple times, including after a failed call to
1621 clear_content = NULL;
1622 free_clear_content = 0;
1628 /* save the decrypted attachments */
1629 clear_content = msg->content;
1631 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1632 mutt_protect (msg, pgpkeylist) == -1)
1634 msg->content = mutt_remove_multipart (msg->content);
1638 decode_descriptions (msg->content);
1641 encode_descriptions (msg->content, 0);
1645 * at this point, msg->content is one of the following three things:
1646 * - multipart/signed. In this case, clear_content is a child.
1647 * - multipart/encrypted. In this case, clear_content exists
1649 * - application/pgp. In this case, clear_content exists independently.
1650 * - something else. In this case, it's the same as clear_content.
1653 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1655 if (clear_content && (msg->content != clear_content)
1656 && (msg->content->parts != clear_content))
1657 free_clear_content = 1;
1660 if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1661 mutt_message _("Sending message...");
1663 mutt_prepare_envelope (msg->env, 1);
1665 /* save a copy of the message, if necessary. */
1667 mutt_expand_path (fcc, sizeof (fcc));
1670 /* Don't save a copy when we are in batch-mode, and the FCC
1671 * folder is on an IMAP server: This would involve possibly lots
1672 * of user interaction, which is not available in batch mode.
1674 * Note: A patch to fix the problems with the use of IMAP servers
1675 * from non-curses mode is available from Brendan Cully. However,
1676 * I'd like to think a bit more about this before including it.
1680 if ((flags & SENDBATCH) && fcc[0] && mx_is_imap (fcc))
1684 if (*fcc && mutt_strcmp ("/dev/null", fcc) != 0)
1686 BODY *tmpbody = msg->content;
1687 BODY *save_sig = NULL;
1688 BODY *save_parts = NULL;
1690 if (WithCrypto && msg->security && option (OPTFCCCLEAR))
1691 msg->content = clear_content;
1693 /* check to see if the user wants copies of all attachments */
1694 if (query_quadoption (OPT_FCCATTACH, _("Save attachments in Fcc?")) != M_YES &&
1695 msg->content->type == TYPEMULTIPART)
1698 && (mutt_strcmp (msg->content->subtype, "encrypted") == 0 ||
1699 mutt_strcmp (msg->content->subtype, "signed") == 0))
1701 if (clear_content->type == TYPEMULTIPART)
1703 if(!(msg->security & ENCRYPT) && (msg->security & SIGN))
1705 /* save initial signature and attachments */
1706 save_sig = msg->content->parts->next;
1707 save_parts = clear_content->parts->next;
1710 /* this means writing only the main part */
1711 msg->content = clear_content->parts;
1713 if (mutt_protect (msg, pgpkeylist) == -1)
1715 /* we can't do much about it at this point, so
1716 * fallback to saving the whole thing to fcc
1718 msg->content = tmpbody;
1723 save_content = msg->content;
1727 msg->content = msg->content->parts;
1733 /* update received time so that when storing to a mbox-style folder
1734 * the From_ line contains the current time instead of when the
1735 * message was first postponed.
1737 msg->received = time (NULL);
1738 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1)
1741 * Error writing FCC, we should abort sending.
1747 msg->content = tmpbody;
1749 if (WithCrypto && save_sig)
1751 /* cleanup the second signature structures */
1752 if (save_content->parts)
1754 mutt_free_body (&save_content->parts->next);
1755 save_content->parts = NULL;
1757 mutt_free_body (&save_content);
1759 /* restore old signature and attachments */
1760 msg->content->parts->next = save_sig;
1761 msg->content->parts->parts->next = save_parts;
1763 else if (WithCrypto && save_content)
1765 /* destroy the new encrypted body. */
1766 mutt_free_body (&save_content);
1773 * Don't attempt to send the message if the FCC failed. Just pretend
1774 * the send failed as well so we give the user a chance to fix the
1777 if (fcc_error || (i = send_message (msg)) < 0)
1779 if (!(flags & SENDBATCH))
1783 else if ((msg->security & ENCRYPT) ||
1784 ((msg->security & SIGN)
1785 && msg->content->type == TYPEAPPLICATION))
1787 mutt_free_body (&msg->content); /* destroy PGP data */
1788 msg->content = clear_content; /* restore clear text. */
1790 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART)
1792 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1793 msg->content = mutt_remove_multipart (msg->content);
1796 msg->content = mutt_remove_multipart (msg->content);
1797 decode_descriptions (msg->content);
1798 mutt_unprepare_envelope (msg->env);
1803 puts _("Could not send the message.");
1807 else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX))
1808 mutt_message (i == 0 ? _("Mail sent.") : _("Sending in background."));
1810 if (WithCrypto && (msg->security & ENCRYPT))
1813 if (WithCrypto && free_clear_content)
1814 mutt_free_body (&clear_content);
1816 /* set 'replied' flag only if the user didn't change/remove
1817 In-Reply-To: and References: headers during edit */
1818 if (flags & SENDREPLY)
1821 mutt_set_flag (ctx, cur, M_REPLIED, is_reply (cur, msg));
1822 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged)
1824 for (i = 0; i < ctx->vcount; i++)
1825 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1826 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED,
1827 is_reply (ctx->hdrs[ctx->v2r[i]], msg));
1836 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1845 safe_fclose (&tempfp);
1846 mutt_free_header (&msg);
1851 /* vim: set sw=2: */