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.
24 #include "mutt_curses.h"
31 #include "mutt_crypt.h"
32 #include "mutt_idna.h"
44 #include <sys/types.h>
56 static void append_signature (FILE * f)
62 fprintf (f, "\n%s", SignOffString);
65 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
66 if (option (OPTSIGDASHES))
68 else if (SignOffString)
70 mutt_copy_stream (tmpfp, f);
73 mutt_wait_filter (thepid);
77 /* compare two e-mail addresses and return 1 if they are equivalent */
78 static int mutt_addrcmp (ADDRESS * a, ADDRESS * b)
80 if (!a->mailbox || !b->mailbox)
82 if (ascii_strcasecmp (a->mailbox, b->mailbox))
87 /* search an e-mail address in a list */
88 static int mutt_addrsrc (ADDRESS * a, ADDRESS * lst)
90 for (; lst; lst = lst->next) {
91 if (mutt_addrcmp (a, lst))
97 /* removes addresses from "b" which are contained in "a" */
98 static ADDRESS *mutt_remove_xrefs (ADDRESS * a, ADDRESS * b)
100 ADDRESS *top, *p, *prev = NULL;
104 for (p = a; p; p = p->next) {
105 if (mutt_addrcmp (p, b))
110 prev->next = b->next;
112 rfc822_free_address (&b);
118 rfc822_free_address (&b);
130 /* remove any address which matches the current user. if `leave_only' is
131 * nonzero, don't remove the user's address if it is the only one in the list
133 static ADDRESS *remove_user (ADDRESS * a, int leave_only)
135 ADDRESS *top = NULL, *last = NULL;
138 if (!mutt_addr_is_user (a)) {
152 if (!leave_only || a || last) {
154 rfc822_free_address (&tmp);
163 static ADDRESS *find_mailing_lists (ADDRESS * t, ADDRESS * c)
165 ADDRESS *top = NULL, *ptr = NULL;
167 for (; t || c; t = c, c = NULL) {
168 for (; t; t = t->next) {
169 if (mutt_is_mail_list (t) && !t->group) {
171 ptr->next = rfc822_cpy_adr_real (t);
175 ptr = top = rfc822_cpy_adr_real (t);
182 static int edit_address (ADDRESS ** a, /* const */ char *field)
184 char buf[HUGE_STRING];
190 mutt_addrlist_to_local (*a);
191 rfc822_write_address (buf, sizeof (buf), *a, 0);
192 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
194 rfc822_free_address (a);
195 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
196 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
197 mutt_error (_("Error: '%s' is a bad IDN."), err);
203 while (idna_ok != 0);
207 static int edit_envelope (ENVELOPE * en, int flags)
209 char buf[HUGE_STRING];
210 LIST *uh = UserHeader;
211 regmatch_t pat_match[1];
214 if (option (OPTNEWSSEND)) {
216 strfcpy (buf, en->newsgroups, sizeof (buf));
219 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
221 FREE (&en->newsgroups);
222 en->newsgroups = safe_strdup (buf);
225 strfcpy (buf, en->followup_to, sizeof (buf));
228 if (option (OPTASKFOLLOWUP)
229 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
231 FREE (&en->followup_to);
232 en->followup_to = safe_strdup (buf);
234 if (en->x_comment_to)
235 strfcpy (buf, en->x_comment_to, sizeof (buf));
238 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
239 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
241 FREE (&en->x_comment_to);
242 en->x_comment_to = safe_strdup (buf);
247 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
249 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
251 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
256 if (option (OPTFASTREPLY))
259 strfcpy (buf, en->subject, sizeof (buf));
265 for (; uh; uh = uh->next) {
266 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
269 strncpy (buf, p, sizeof (buf));
274 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
275 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
276 unsigned int pos = pat_match->rm_so;
278 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
279 buf[pos] = '\0'; /* kill match */
280 while (pos-- && buf[pos] == ' ')
281 buf[pos] = '\0'; /* remove trailing spaces */
284 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
288 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
293 ("No subject, abort?"))
295 mutt_message _("No subject, aborting.");
299 mutt_str_replace (&en->subject, buf);
305 char *nntp_get_header (const char *s)
308 return safe_strdup (s);
312 static void process_user_recips (ENVELOPE * env)
314 LIST *uh = UserHeader;
316 for (; uh; uh = uh->next) {
317 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
318 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
319 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
320 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
321 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
322 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
324 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
325 env->newsgroups = nntp_get_header (uh->data + 11);
326 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
327 env->followup_to = nntp_get_header (uh->data + 12);
328 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
329 env->x_comment_to = nntp_get_header (uh->data + 13);
334 static void process_user_header (ENVELOPE * env)
336 LIST *uh = UserHeader;
337 LIST *last = env->userhdrs;
343 for (; uh; uh = uh->next) {
344 if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
345 /* User has specified a default From: address. Remove default address */
346 rfc822_free_address (&env->from);
347 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
349 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
350 rfc822_free_address (&env->reply_to);
351 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
353 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
354 mutt_str_replace (&env->message_id, uh->data + 11);
355 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
356 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
357 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
359 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
360 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
361 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
363 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
364 ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
366 last->next = mutt_new_list ();
370 last = env->userhdrs = mutt_new_list ();
371 last->data = safe_strdup (uh->data);
376 LIST *mutt_copy_list (LIST * p)
378 LIST *t, *r = NULL, *l = NULL;
380 for (; p; p = p->next) {
381 t = (LIST *) safe_malloc (sizeof (LIST));
382 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)) {
419 /* make sure we have the user's passphrase before proceeding... */
420 crypt_valid_passphrase (cur->security);
423 mutt_forward_intro (out, cur);
425 if (option (OPTFORWDECODE)) {
426 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
427 if (option (OPTWEED)) {
428 chflags |= CH_WEED | CH_REORDER;
429 cmflags |= M_CM_WEED;
432 if (option (OPTFORWQUOTE))
433 cmflags |= M_CM_PREFIX;
435 mutt_copy_message (out, ctx, cur, cmflags, chflags);
436 mutt_forward_trailer (out);
440 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
445 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
451 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
455 if (PostIndentString) {
456 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
462 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
464 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
465 int chflags = CH_DECODE;
467 if (WithCrypto && (cur->security & ENCRYPT)) {
468 /* make sure we have the user's passphrase before proceeding... */
469 crypt_valid_passphrase (cur->security);
472 mutt_parse_mime_message (ctx, cur);
473 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
475 mutt_make_attribution (ctx, cur, out);
477 if (!option (OPTHEADER))
478 cmflags |= M_CM_NOHEADER;
479 if (option (OPTWEED)) {
480 chflags |= CH_WEED | CH_REORDER;
481 cmflags |= M_CM_WEED;
484 mutt_copy_message (out, ctx, cur, cmflags, chflags);
486 mutt_make_post_indent (ctx, cur, out);
491 static int default_to (ADDRESS ** to, ENVELOPE * env, int flags, int hmfupto)
496 if (flags && env->mail_followup_to && hmfupto == M_YES) {
497 rfc822_append (to, env->mail_followup_to);
501 /* Exit now if we're setting up the default Cc list for list-reply
502 * (only set if Mail-Followup-To is present and honoured).
504 if (flags & SENDLISTREPLY)
507 /* If this message came from a mailing list, ask the user if he really
508 * intended to reply to the author only.
510 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
511 switch (query_quadoption (OPT_LISTREPLY,
513 ("Message came from a mailing list. Reply to author only?")))
516 tmp = find_mailing_lists (env->to, env->cc);
517 rfc822_append (to, tmp);
518 rfc822_free_address (&tmp);
521 return -1; /* abort */
525 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
526 /* mail is from the user, assume replying to recipients */
527 rfc822_append (to, env->to);
529 else if (env->reply_to) {
530 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
531 (option (OPTIGNORELISTREPLYTO) &&
532 mutt_is_mail_list (env->reply_to) &&
533 (mutt_addrsrc (env->reply_to, env->to) ||
534 mutt_addrsrc (env->reply_to, env->cc)))) {
535 /* If the Reply-To: address is a mailing list, assume that it was
536 * put there by the mailing list, and use the From: address
538 * We also take the from header if our correspondant has a reply-to
539 * header which is identical to the electronic mail address given
540 * in his From header.
543 rfc822_append (to, env->from);
545 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
546 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
547 /* There are quite a few mailing lists which set the Reply-To:
548 * header field to the list address, which makes it quite impossible
549 * to send a message to only the sender of the message. This
550 * provides a way to do that.
552 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
553 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
554 switch (query_quadoption (OPT_REPLYTO, prompt)) {
556 rfc822_append (to, env->reply_to);
560 rfc822_append (to, env->from);
564 return (-1); /* abort */
568 rfc822_append (to, env->reply_to);
571 rfc822_append (to, env->from);
576 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
582 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
583 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
584 in->mail_followup_to->mailbox,
585 in->mail_followup_to->next ? ",..." : "");
587 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
591 if (flags & SENDLISTREPLY) {
592 tmp = find_mailing_lists (in->to, in->cc);
593 rfc822_append (&out->to, tmp);
594 rfc822_free_address (&tmp);
596 if (in->mail_followup_to && hmfupto == M_YES &&
597 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
598 return (-1); /* abort */
601 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
602 return (-1); /* abort */
604 if ((flags & SENDGROUPREPLY)
605 && (!in->mail_followup_to || hmfupto != M_YES)) {
606 /* if(!mutt_addr_is_user(in->to)) */
607 rfc822_append (&out->cc, in->to);
608 rfc822_append (&out->cc, in->cc);
614 LIST *mutt_make_references (ENVELOPE * e)
616 LIST *t = NULL, *l = NULL;
619 l = mutt_copy_list (e->references);
621 l = mutt_copy_list (e->in_reply_to);
624 t = mutt_new_list ();
625 t->data = safe_strdup (e->message_id);
633 void mutt_fix_reply_recipients (ENVELOPE * env)
635 mutt_expand_aliases_env (env);
637 if (!option (OPTMETOO)) {
638 /* the order is important here. do the CC: first so that if the
639 * the user is the only recipient, it ends up on the TO: field
641 env->cc = remove_user (env->cc, (env->to == NULL));
642 env->to = remove_user (env->to, (env->cc == NULL));
645 /* the CC field can get cluttered, especially with lists */
646 env->to = mutt_remove_duplicates (env->to);
647 env->cc = mutt_remove_duplicates (env->cc);
648 env->cc = mutt_remove_xrefs (env->to, env->cc);
651 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
655 /* set the default subject for the message. */
656 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
657 mutt_str_replace (&env->subject, buffer);
660 void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
661 HEADER * cur, ENVELOPE * curenv)
663 /* This takes precedence over a subject that might have
664 * been taken from a List-Post header. Is that correct?
666 if (curenv->real_subj) {
667 FREE (&env->subject);
668 env->subject = safe_malloc (mutt_strlen (curenv->real_subj) + 5);
669 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
671 else if (!env->subject)
672 env->subject = safe_strdup ("Re: your mail");
675 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
676 env->x_comment_to = safe_strdup (mutt_get_name (curenv->from));
680 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
681 LIST *** pp, LIST *** qq)
683 LIST **p = NULL, **q = NULL;
691 p = &env->references;
693 q = &env->in_reply_to;
700 *p = mutt_make_references (curenv);
702 if (curenv->message_id) {
703 *q = mutt_new_list ();
704 (*q)->data = safe_strdup (curenv->message_id);
715 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
717 env->references = NULL;
718 env->in_reply_to = NULL;
722 LIST **p = NULL, **q = NULL;
725 for (i = 0; i < ctx->vcount; i++) {
726 h = ctx->hdrs[ctx->v2r[i]];
728 mutt_add_to_reference_headers (env, h->env, &p, &q);
732 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
736 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
738 ENVELOPE *curenv = NULL;
743 for (i = 0; i < ctx->vcount; i++)
744 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
745 cur = ctx->hdrs[ctx->v2r[i]];
751 /* This could happen if the user tagged some messages and then did
752 * a limit such that none of the tagged message are visible.
754 mutt_error _("No tagged messages are visible!");
762 if (flags & SENDREPLY) {
764 if ((flags & SENDNEWS)) {
765 /* in case followup set Newsgroups: with Followup-To: if it present */
766 if (!env->newsgroups && curenv &&
767 mutt_strcasecmp (curenv->followup_to, "poster"))
768 env->newsgroups = safe_strdup (curenv->followup_to);
775 for (i = 0; i < ctx->vcount; i++) {
776 h = ctx->hdrs[ctx->v2r[i]];
777 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
781 else if (mutt_fetch_recips (env, curenv, flags) == -1)
784 if ((flags & SENDLISTREPLY) && !env->to) {
785 mutt_error _("No mailing lists found!");
790 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
791 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
793 else if (flags & SENDFORWARD)
794 mutt_make_forward_subject (env, ctx, cur);
799 static int generate_body (FILE * tempfp, /* stream for outgoing message */
800 HEADER * msg, /* header for outgoing message */
801 int flags, /* compose mode */
802 CONTEXT * ctx, /* current mailbox */
804 { /* current message */
809 if (flags & SENDREPLY) {
811 query_quadoption (OPT_INCLUDE,
812 _("Include message in reply?"))) == -1)
816 mutt_message _("Including quoted message...");
819 for (i = 0; i < ctx->vcount; i++) {
820 h = ctx->hdrs[ctx->v2r[i]];
822 if (include_reply (ctx, h, tempfp) == -1) {
823 mutt_error _("Could not include all requested messages!");
827 fputc ('\n', tempfp);
832 include_reply (ctx, cur, tempfp);
836 else if (flags & SENDFORWARD) {
838 query_quadoption (OPT_MIMEFWD,
839 _("Forward as attachment?"))) == M_YES) {
840 BODY *last = msg->content;
842 mutt_message _("Preparing forwarded message...");
844 while (last && last->next)
848 tmp = mutt_make_message_attach (ctx, cur, 0);
855 for (i = 0; i < ctx->vcount; i++) {
856 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
857 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
863 last = msg->content = tmp;
870 include_forward (ctx, cur, tempfp);
872 for (i = 0; i < ctx->vcount; i++)
873 if (ctx->hdrs[ctx->v2r[i]]->tagged)
874 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
879 /* if (WithCrypto && (flags & SENDKEY)) */
880 else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)) {
883 if ((WithCrypto & APPLICATION_PGP)
884 && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
887 tmp->next = msg->content;
896 void mutt_set_followup_to (ENVELOPE * e)
902 * Only generate the Mail-Followup-To if the user has requested it, and
903 * it hasn't already been set
906 if (!option (OPTFOLLOWUPTO))
909 if (option (OPTNEWSSEND)) {
910 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
911 e->followup_to = safe_strdup (e->newsgroups);
916 if (!e->mail_followup_to) {
917 if (mutt_is_list_cc (0, e->to, e->cc)) {
919 * this message goes to known mailing lists, so create a proper
920 * mail-followup-to header
923 t = rfc822_append (&e->mail_followup_to, e->to);
924 rfc822_append (&t, e->cc);
927 /* remove ourselves from the mail-followup-to header */
928 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
931 * If we are not subscribed to any of the lists in question,
932 * re-add ourselves to the mail-followup-to header. The
933 * mail-followup-to header generated is a no-op with group-reply,
934 * but makes sure list-reply has the desired effect.
937 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
939 from = rfc822_cpy_adr (e->reply_to);
941 from = rfc822_cpy_adr (e->from);
943 from = mutt_default_from ();
946 /* Normally, this loop will not even be entered. */
947 for (t = from; t && t->next; t = t->next);
949 t->next = e->mail_followup_to; /* t cannot be NULL at this point. */
950 e->mail_followup_to = from;
954 e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
960 /* look through the recipients of the message we are replying to, and if
961 we find an address that matches $alternates, we use that as the default
963 static ADDRESS *set_reverse_name (ENVELOPE * env)
967 for (tmp = env->to; tmp; tmp = tmp->next) {
968 if (mutt_addr_is_user (tmp))
972 for (tmp = env->cc; tmp; tmp = tmp->next) {
973 if (mutt_addr_is_user (tmp))
977 if (!tmp && mutt_addr_is_user (env->from))
980 tmp = rfc822_cpy_adr_real (tmp);
981 if (!option (OPTREVREAL))
982 FREE (&tmp->personal);
984 tmp->personal = safe_strdup (Realname);
989 ADDRESS *mutt_default_from (void)
992 const char *fqdn = mutt_fqdn (1);
995 * Note: We let $from override $realname here. Is this the right
1000 adr = rfc822_cpy_adr_real (From);
1001 else if (option (OPTUSEDOMAIN)) {
1002 adr = rfc822_new_address ();
1004 safe_malloc (mutt_strlen (Username) + mutt_strlen (fqdn) + 2);
1005 sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn)); /* __SPRINTF_CHECKED__ */
1008 adr = rfc822_new_address ();
1009 adr->mailbox = safe_strdup (NONULL (Username));
1015 static int send_message (HEADER * msg)
1017 char tempfile[_POSIX_PATH_MAX];
1021 /* Write out the message in MIME form. */
1022 mutt_mktemp (tempfile);
1023 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
1027 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
1028 msg->chain ? 1 : 0);
1031 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1034 fputc ('\n', tempfp); /* tie off the header. */
1036 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1042 if (fclose (tempfp) != 0) {
1043 mutt_perror (tempfile);
1050 return mix_send_message (msg->chain, tempfile);
1053 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1054 msg->env->bcc, tempfile,
1055 (msg->content->encoding == ENC8BIT));
1059 /* rfc2047 encode the content-descriptions */
1060 static void encode_descriptions (BODY * b, short recurse)
1064 for (t = b; t; t = t->next) {
1065 if (t->description) {
1066 rfc2047_encode_string (&t->description);
1068 if (recurse && t->parts)
1069 encode_descriptions (t->parts, recurse);
1073 /* rfc2047 decode them in case of an error */
1074 static void decode_descriptions (BODY * b)
1078 for (t = b; t; t = t->next) {
1079 if (t->description) {
1080 rfc2047_decode (&t->description);
1083 decode_descriptions (t->parts);
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 int ci_send_message (int flags, /* send mode */
1098 HEADER * msg, /* template to use for new message */
1099 char *tempfile, /* file specified by -i or -H */
1100 CONTEXT * ctx, /* current mailbox */
1102 { /* current message */
1103 char buffer[LONG_STRING];
1104 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1105 FILE *tempfp = NULL;
1107 int i, killfrom = 0;
1109 int free_clear_content = 0;
1111 BODY *save_content = NULL;
1112 BODY *clear_content = NULL;
1113 char *pgpkeylist = NULL;
1115 /* save current value of "pgp_sign_as" */
1116 char *signas = NULL;
1117 char *tag = NULL, *err = NULL;
1122 if (flags & SENDNEWS)
1123 set_option (OPTNEWSSEND);
1125 unset_option (OPTNEWSSEND);
1128 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1129 mutt_num_postponed (1)) {
1130 /* If the user is composing a new message, check to see if there
1131 * are any postponed messages first.
1134 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1138 flags |= SENDPOSTPONED;
1142 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1143 signas = safe_strdup (PgpSignAs);
1145 /* Delay expansion of aliases until absolutely necessary--shouldn't
1146 * be necessary unless we are prompting the user or about to execute a
1151 msg = mutt_new_header ();
1153 if (flags == SENDPOSTPONED) {
1155 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1159 * If postponed message is a news article, it have
1160 * a "Newsgroups:" header line, then set appropriate flag.
1162 if (msg->env->newsgroups) {
1164 set_option (OPTNEWSSEND);
1168 unset_option (OPTNEWSSEND);
1173 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1174 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1175 mutt_perror (msg->content->filename);
1181 msg->env = mutt_new_envelope ();
1184 /* Parse and use an eventual list-post header */
1185 if ((flags & SENDLISTREPLY)
1186 && cur && cur->env && cur->env->list_post) {
1187 /* Use any list-post header as a template */
1188 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1189 /* We don't let them set the sender's address. */
1190 rfc822_free_address (&msg->env->from);
1193 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1194 pbody = mutt_new_body ();
1195 pbody->next = msg->content; /* don't kill command-line attachments */
1196 msg->content = pbody;
1198 mutt_parse_content_type (ContentType, msg->content);
1199 msg->content->unlink = 1;
1200 msg->content->use_disp = 0;
1201 msg->content->disposition = DISPINLINE;
1202 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1203 && !ascii_strcasecmp (msg->content->subtype, "plain"))
1204 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1207 mutt_mktemp (buffer);
1208 tempfp = safe_fopen (buffer, "w+");
1209 msg->content->filename = safe_strdup (buffer);
1212 tempfp = safe_fopen (tempfile, "a+");
1213 msg->content->filename = safe_strdup (tempfile);
1219 "newsend_message: can't create tempfile %s (errno=%d)\n",
1220 msg->content->filename, errno));
1221 mutt_perror (msg->content->filename);
1226 /* this is handled here so that the user can match ~f in send-hook */
1227 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1228 /* we shouldn't have to worry about freeing `msg->env->from' before
1229 * setting it here since this code will only execute when doing some
1230 * sort of reply. the pointer will only be set when using the -H command
1233 * We shouldn't have to worry about alias expansion here since we are
1234 * either replying to a real or postponed message, therefore no aliases
1235 * should exist since the user has not had the opportunity to add
1236 * addresses to the list. We just have to ensure the postponed messages
1237 * have their aliases expanded.
1240 msg->env->from = set_reverse_name (cur->env);
1243 if (!msg->env->from && option (OPTUSEFROM)
1244 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1245 msg->env->from = mutt_default_from ();
1247 if (flags & SENDBATCH) {
1248 mutt_copy_stream (stdin, tempfp);
1249 if (option (OPTHDRS)) {
1250 process_user_recips (msg->env);
1251 process_user_header (msg->env);
1253 mutt_expand_aliases_env (msg->env);
1255 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1256 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1257 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1260 if (option (OPTHDRS))
1261 process_user_recips (msg->env);
1263 /* Expand aliases and remove duplicates/crossrefs */
1264 mutt_fix_reply_recipients (msg->env);
1267 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1268 && !msg->env->newsgroups)
1269 msg->env->newsgroups = safe_strdup (((NNTP_DATA *) ctx->data)->group);
1272 if (!(flags & SENDMAILX) &&
1273 !(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1274 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1275 if (edit_envelope (msg->env, flags) == -1)
1279 /* the from address must be set here regardless of whether or not
1280 * $use_from is set so that the `~P' (from you) operator in send-hook
1281 * patterns will work. if $use_from is unset, the from address is killed
1282 * after send-hooks are evaulated */
1284 if (!msg->env->from) {
1285 msg->env->from = mutt_default_from ();
1289 if ((flags & SENDREPLY) && cur) {
1290 /* change setting based upon message we are replying to */
1291 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1294 * set the replied flag for the message we are generating so that the
1295 * user can use ~Q in a send-hook to know when reply-hook's are also
1301 /* change settings based upon recipients */
1303 mutt_message_hook (NULL, msg, M_SENDHOOK);
1306 * Unset the replied flag from the message we are composing since it is
1307 * no longer required. This is done here because the FCC'd copy of
1308 * this message was erroneously get the 'R'eplied flag when stored in
1309 * a maildir-style mailbox.
1314 rfc822_free_address (&msg->env->from);
1318 if (option (OPTHDRS))
1319 process_user_header (msg->env);
1322 if (option (OPTSIGONTOP)
1323 && (!(flags & (SENDMAILX | SENDKEY)) && Editor
1324 && mutt_strcmp (Editor, "builtin") != 0))
1325 append_signature (tempfp);
1327 /* include replies/forwarded messages, unless we are given a template */
1328 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1329 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1332 if (!option (OPTSIGONTOP)
1333 && (!(flags & (SENDMAILX | SENDKEY)) && Editor
1334 && mutt_strcmp (Editor, "builtin") != 0))
1335 append_signature (tempfp);
1338 * this wants to be done _after_ generate_body, so message-hooks
1342 if (WithCrypto && !(flags & SENDMAILX)) {
1343 if (option (OPTCRYPTAUTOSIGN))
1344 msg->security |= SIGN;
1345 if (option (OPTCRYPTAUTOENCRYPT))
1346 msg->security |= ENCRYPT;
1347 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1348 msg->security |= ENCRYPT;
1349 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1350 msg->security |= SIGN;
1351 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1352 && (cur->security & ENCRYPT))
1353 msg->security |= SIGN;
1354 if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN))) {
1355 if (option (OPTPGPAUTOINLINE))
1356 msg->security |= INLINE;
1357 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1358 msg->security |= INLINE;
1362 if (WithCrypto && msg->security) {
1364 * When reypling / forwarding, use the original message's
1365 * crypto system. According to the documentation,
1366 * smime_is_default should be disregarded here.
1368 * Problem: At least with forwarding, this doesn't really
1369 * make much sense. Should we have an option to completely
1370 * disable individual mechanisms at run-time?
1373 if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP)
1374 && (cur->security & APPLICATION_PGP))
1375 msg->security |= APPLICATION_PGP;
1376 else if ((WithCrypto & APPLICATION_SMIME)
1377 && option (OPTCRYPTAUTOSMIME)
1378 && (cur->security & APPLICATION_SMIME))
1379 msg->security |= APPLICATION_SMIME;
1383 * No crypto mechanism selected? Use availability + smime_is_default
1386 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1387 if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1388 && option (OPTSMIMEISDEFAULT))
1389 msg->security |= APPLICATION_SMIME;
1390 else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
1391 msg->security |= APPLICATION_PGP;
1392 else if ((WithCrypto & APPLICATION_SMIME)
1393 && option (OPTCRYPTAUTOSMIME))
1394 msg->security |= APPLICATION_SMIME;
1398 /* No permissible mechanisms found. Don't sign or encrypt. */
1399 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1404 * This hook is even called for postponed messages, and can, e.g., be
1405 * used for setting the editor, the sendmail path, or the
1408 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1410 /* wait until now to set the real name portion of our return address so
1411 that $realname can be set in a send-hook */
1412 if (msg->env->from && !msg->env->from->personal
1413 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1414 msg->env->from->personal = safe_strdup (Realname);
1416 if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
1417 safe_fclose (&tempfp);
1419 if (flags & SENDMAILX) {
1420 if (mutt_builtin_editor (msg->content->filename, msg, cur) == -1)
1423 else if (!(flags & SENDBATCH)) {
1425 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1427 mutt_update_encoding (msg->content);
1430 * Select whether or not the user's editor should be called now. We
1431 * don't want to do this when:
1432 * 1) we are sending a key/cert
1433 * 2) we are forwarding a message and the user doesn't want to edit it.
1434 * This is controled by the quadoption $forward_edit. However, if
1435 * both $edit_headers and $autoedit are set, we want to ignore the
1436 * setting of $forward_edit because the user probably needs to add the
1439 if (!(flags & SENDKEY) &&
1440 ((flags & SENDFORWARD) == 0 ||
1441 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1442 query_quadoption (OPT_FORWEDIT,
1443 _("Edit forwarded message?")) == M_YES)) {
1444 /* If the this isn't a text message, look for a mailcap edit command */
1445 if (mutt_needs_mailcap (msg->content))
1446 mutt_edit_attachment (msg->content);
1447 else if (!Editor || mutt_strcmp ("builtin", Editor) == 0)
1448 mutt_builtin_editor (msg->content->filename, msg, cur);
1449 else if (option (OPTEDITHDRS)) {
1450 mutt_env_to_local (msg->env);
1451 mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1453 mutt_env_to_idna (msg->env, NULL, NULL);
1456 mutt_edit_file (Editor, msg->content->filename);
1458 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1461 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1462 if (stat (msg->content->filename, &st) == 0) {
1463 /* if the file was not modified, bail out now */
1464 if (mtime == st.st_mtime && !msg->content->next &&
1465 query_quadoption (OPT_ABORT,
1466 _("Abort unmodified message?")) == M_YES) {
1467 mutt_message _("Aborted unmodified message.");
1473 mutt_perror (msg->content->filename);
1477 /* specify a default fcc. if we are in batchmode, only save a copy of
1478 * the message if the value of $copy is yes or ask-yes */
1480 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1481 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1482 /* set the default FCC */
1483 if (!msg->env->from) {
1484 msg->env->from = mutt_default_from ();
1485 killfrom = 1; /* no need to check $use_from because if the user specified
1486 a from address it would have already been set by now */
1488 mutt_select_fcc (fcc, sizeof (fcc), msg);
1490 rfc822_free_address (&msg->env->from);
1496 mutt_update_encoding (msg->content);
1498 if (!(flags & (SENDMAILX | SENDBATCH))) {
1501 fcc_error = 0; /* reset value since we may have failed before */
1502 mutt_pretty_mailbox (fcc);
1503 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1507 if (flags & SENDNEWS)
1508 mutt_message (_("Article not posted."));
1512 mutt_message _("Mail not sent.");
1516 /* postpone the message until later. */
1517 if (msg->content->next)
1518 msg->content = mutt_make_multipart (msg->content);
1521 * make sure the message is written to the right part of a maildir
1527 encode_descriptions (msg->content, 1);
1528 mutt_prepare_envelope (msg->env, 0);
1529 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1532 || mutt_write_fcc (NONULL (Postponed), msg,
1534 && (flags & SENDREPLY)) ? cur->env->
1535 message_id : NULL, 1, fcc) < 0) {
1536 msg->content = mutt_remove_multipart (msg->content);
1537 decode_descriptions (msg->content);
1538 mutt_unprepare_envelope (msg->env);
1541 mutt_update_num_postponed ();
1542 mutt_message _("Message postponed.");
1549 if (!(flags & SENDNEWS))
1551 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1552 if (!(flags & SENDBATCH)) {
1553 mutt_error _("No recipients are specified!");
1558 puts _("No recipients were specified.");
1564 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1565 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1567 if (!(flags & SENDBATCH))
1573 if (!msg->env->subject && !(flags & SENDBATCH) &&
1575 query_quadoption (OPT_SUBJECT,
1576 _("No subject, abort sending?"))) != M_NO) {
1577 /* if the abort is automatic, print an error message */
1578 if (quadoption (OPT_SUBJECT) == M_YES)
1579 mutt_error _("No subject specified.");
1584 if ((flags & SENDNEWS) && !msg->env->subject) {
1585 mutt_error _("No subject specified.");
1590 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1591 mutt_error _("No newsgroup specified.");
1597 if (msg->content->next)
1598 msg->content = mutt_make_multipart (msg->content);
1601 * Ok, we need to do it this way instead of handling all fcc stuff in
1602 * one place in order to avoid going to main_loop with encoded "env"
1603 * in case of error. Ugh.
1606 encode_descriptions (msg->content, 1);
1609 * Make sure that clear_content and free_clear_content are
1610 * properly initialized -- we may visit this particular place in
1611 * the code multiple times, including after a failed call to
1615 clear_content = NULL;
1616 free_clear_content = 0;
1619 if (msg->security) {
1620 /* save the decrypted attachments */
1621 clear_content = msg->content;
1623 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1624 mutt_protect (msg, pgpkeylist) == -1) {
1625 msg->content = mutt_remove_multipart (msg->content);
1629 decode_descriptions (msg->content);
1632 encode_descriptions (msg->content, 0);
1636 * at this point, msg->content is one of the following three things:
1637 * - multipart/signed. In this case, clear_content is a child.
1638 * - multipart/encrypted. In this case, clear_content exists
1640 * - application/pgp. In this case, clear_content exists independently.
1641 * - something else. In this case, it's the same as clear_content.
1644 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1646 if (clear_content && (msg->content != clear_content)
1647 && (msg->content->parts != clear_content))
1648 free_clear_content = 1;
1651 if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1652 mutt_message _("Sending message...");
1654 mutt_prepare_envelope (msg->env, 1);
1656 /* save a copy of the message, if necessary. */
1658 mutt_expand_path (fcc, sizeof (fcc));
1661 /* Don't save a copy when we are in batch-mode, and the FCC
1662 * folder is on an IMAP server: This would involve possibly lots
1663 * of user interaction, which is not available in batch mode.
1665 * Note: A patch to fix the problems with the use of IMAP servers
1666 * from non-curses mode is available from Brendan Cully. However,
1667 * I'd like to think a bit more about this before including it.
1671 if ((flags & SENDBATCH) && fcc[0] && mx_is_imap (fcc))
1675 if (*fcc && mutt_strcmp ("/dev/null", fcc) != 0) {
1676 BODY *tmpbody = msg->content;
1677 BODY *save_sig = NULL;
1678 BODY *save_parts = NULL;
1680 if (WithCrypto && msg->security && option (OPTFCCCLEAR))
1681 msg->content = clear_content;
1683 /* check to see if the user wants copies of all attachments */
1684 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1686 && (mutt_strcmp (msg->content->subtype, "encrypted") == 0 ||
1687 mutt_strcmp (msg->content->subtype, "signed") == 0)) {
1688 if (clear_content->type == TYPEMULTIPART) {
1689 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1690 /* save initial signature and attachments */
1691 save_sig = msg->content->parts->next;
1692 save_parts = clear_content->parts->next;
1695 /* this means writing only the main part */
1696 msg->content = clear_content->parts;
1698 if (mutt_protect (msg, pgpkeylist) == -1) {
1699 /* we can't do much about it at this point, so
1700 * fallback to saving the whole thing to fcc
1702 msg->content = tmpbody;
1707 save_content = msg->content;
1711 msg->content = msg->content->parts;
1716 /* update received time so that when storing to a mbox-style folder
1717 * the From_ line contains the current time instead of when the
1718 * message was first postponed.
1720 msg->received = time (NULL);
1721 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1723 * Error writing FCC, we should abort sending.
1729 msg->content = tmpbody;
1731 if (WithCrypto && save_sig) {
1732 /* cleanup the second signature structures */
1733 if (save_content->parts) {
1734 mutt_free_body (&save_content->parts->next);
1735 save_content->parts = NULL;
1737 mutt_free_body (&save_content);
1739 /* restore old signature and attachments */
1740 msg->content->parts->next = save_sig;
1741 msg->content->parts->parts->next = save_parts;
1743 else if (WithCrypto && save_content) {
1744 /* destroy the new encrypted body. */
1745 mutt_free_body (&save_content);
1752 * Don't attempt to send the message if the FCC failed. Just pretend
1753 * the send failed as well so we give the user a chance to fix the
1756 if (fcc_error || (i = send_message (msg)) == -1) {
1757 if (!(flags & SENDBATCH)) {
1759 else if ((msg->security & ENCRYPT) || ((msg->security & SIGN)
1760 && msg->content->type ==
1762 mutt_free_body (&msg->content); /* destroy PGP data */
1763 msg->content = clear_content; /* restore clear text. */
1765 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1766 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1767 msg->content = mutt_remove_multipart (msg->content);
1770 msg->content = mutt_remove_multipart (msg->content);
1771 decode_descriptions (msg->content);
1772 mutt_unprepare_envelope (msg->env);
1776 puts _("Could not send the message.");
1781 else if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1782 mutt_message (i != 0 ? _("Sending in background.") :
1784 (flags & SENDNEWS) ? _("Article posted.") :
1790 if (WithCrypto && (msg->security & ENCRYPT))
1793 if (WithCrypto && free_clear_content)
1794 mutt_free_body (&clear_content);
1796 if (flags & SENDREPLY) {
1798 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1799 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1800 for (i = 0; i < ctx->vcount; i++)
1801 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1802 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1811 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED)) {
1818 safe_fclose (&tempfp);
1819 mutt_free_header (&msg);
1824 /* vim: set sw=2: */