2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
21 #include "mutt_crypt.h"
22 #include "mutt_idna.h"
38 #include <sys/types.h>
50 static void append_signature (FILE * f)
56 fprintf (f, "\n%s", SignOffString);
59 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
60 if (option (OPTSIGDASHES))
62 else if (SignOffString)
64 mutt_copy_stream (tmpfp, f);
67 mutt_wait_filter (thepid);
71 /* compare two e-mail addresses and return 1 if they are equivalent */
72 static int mutt_addrcmp (ADDRESS * a, ADDRESS * b)
74 if (!a->mailbox || !b->mailbox)
76 if (ascii_strcasecmp (a->mailbox, b->mailbox))
81 /* search an e-mail address in a list */
82 static int mutt_addrsrc (ADDRESS * a, ADDRESS * lst)
84 for (; lst; lst = lst->next) {
85 if (mutt_addrcmp (a, lst))
91 /* removes addresses from "b" which are contained in "a" */
92 static ADDRESS *mutt_remove_xrefs (ADDRESS * a, ADDRESS * b)
94 ADDRESS *top, *p, *prev = NULL;
98 for (p = a; p; p = p->next) {
99 if (mutt_addrcmp (p, b))
104 prev->next = b->next;
106 rfc822_free_address (&b);
112 rfc822_free_address (&b);
124 /* remove any address which matches the current user. if `leave_only' is
125 * nonzero, don't remove the user's address if it is the only one in the list
127 static ADDRESS *remove_user (ADDRESS * a, int leave_only)
129 ADDRESS *top = NULL, *last = NULL;
132 if (!mutt_addr_is_user (a)) {
146 if (!leave_only || a || last) {
148 rfc822_free_address (&tmp);
157 static ADDRESS *find_mailing_lists (ADDRESS * t, ADDRESS * c)
159 ADDRESS *top = NULL, *ptr = NULL;
161 for (; t || c; t = c, c = NULL) {
162 for (; t; t = t->next) {
163 if (mutt_is_mail_list (t) && !t->group) {
165 ptr->next = rfc822_cpy_adr_real (t);
169 ptr = top = rfc822_cpy_adr_real (t);
176 static int edit_address (ADDRESS ** a, /* const */ char *field)
178 char buf[HUGE_STRING];
184 mutt_addrlist_to_local (*a);
185 rfc822_write_address (buf, sizeof (buf), *a, 0);
186 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
188 rfc822_free_address (a);
189 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
190 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
191 mutt_error (_("Error: '%s' is a bad IDN."), err);
197 while (idna_ok != 0);
201 static int edit_envelope (ENVELOPE * en, int flags)
203 char buf[HUGE_STRING];
204 LIST *uh = UserHeader;
205 regmatch_t pat_match[1];
208 if (option (OPTNEWSSEND)) {
210 strfcpy (buf, en->newsgroups, sizeof (buf));
213 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
215 FREE (&en->newsgroups);
216 en->newsgroups = safe_strdup (buf);
219 strfcpy (buf, en->followup_to, sizeof (buf));
222 if (option (OPTASKFOLLOWUP)
223 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
225 FREE (&en->followup_to);
226 en->followup_to = safe_strdup (buf);
228 if (en->x_comment_to)
229 strfcpy (buf, en->x_comment_to, sizeof (buf));
232 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
233 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
235 FREE (&en->x_comment_to);
236 en->x_comment_to = safe_strdup (buf);
241 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
243 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
245 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
250 if (option (OPTFASTREPLY))
253 strfcpy (buf, en->subject, sizeof (buf));
259 for (; uh; uh = uh->next) {
260 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
263 strncpy (buf, p, sizeof (buf));
268 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
269 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
270 unsigned int pos = pat_match->rm_so;
272 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
273 buf[pos] = '\0'; /* kill match */
274 while (pos-- && buf[pos] == ' ')
275 buf[pos] = '\0'; /* remove trailing spaces */
278 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
282 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
287 ("No subject, abort?"))
289 mutt_message _("No subject, aborting.");
293 str_replace (&en->subject, buf);
299 char *nntp_get_header (const char *s)
302 return safe_strdup (s);
306 static void process_user_recips (ENVELOPE * env)
308 LIST *uh = UserHeader;
310 for (; uh; uh = uh->next) {
311 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
312 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
313 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
314 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
315 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
316 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
318 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
319 env->newsgroups = nntp_get_header (uh->data + 11);
320 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
321 env->followup_to = nntp_get_header (uh->data + 12);
322 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
323 env->x_comment_to = nntp_get_header (uh->data + 13);
328 static void process_user_header (ENVELOPE * env)
330 LIST *uh = UserHeader;
331 LIST *last = env->userhdrs;
337 for (; uh; uh = uh->next) {
338 if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
339 /* User has specified a default From: address. Remove default address */
340 rfc822_free_address (&env->from);
341 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
343 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
344 rfc822_free_address (&env->reply_to);
345 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
347 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
348 str_replace (&env->message_id, uh->data + 11);
349 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
350 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
351 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
353 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
354 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
355 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
357 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
358 ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
360 last->next = mutt_new_list ();
364 last = env->userhdrs = mutt_new_list ();
365 last->data = safe_strdup (uh->data);
370 LIST *mutt_copy_list (LIST * p)
372 LIST *t, *r = NULL, *l = NULL;
374 for (; p; p = p->next) {
375 t = (LIST *) safe_malloc (sizeof (LIST));
376 t->data = safe_strdup (p->data);
388 void mutt_forward_intro (FILE * fp, HEADER * cur)
392 fputs ("----- Forwarded message from ", fp);
394 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
396 fputs (" -----\n\n", fp);
399 void mutt_forward_trailer (FILE * fp)
401 fputs ("\n----- End forwarded message -----\n", fp);
405 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
407 int chflags = CH_DECODE, cmflags = 0;
409 mutt_parse_mime_message (ctx, cur);
410 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
412 if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
413 /* make sure we have the user's passphrase before proceeding... */
414 crypt_valid_passphrase (cur->security);
417 mutt_forward_intro (out, cur);
419 if (option (OPTFORWDECODE)) {
420 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
421 if (option (OPTWEED)) {
422 chflags |= CH_WEED | CH_REORDER;
423 cmflags |= M_CM_WEED;
426 if (option (OPTFORWQUOTE))
427 cmflags |= M_CM_PREFIX;
429 mutt_copy_message (out, ctx, cur, cmflags, chflags);
430 mutt_forward_trailer (out);
434 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
439 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
445 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
449 if (PostIndentString) {
450 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
456 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
458 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
459 int chflags = CH_DECODE;
461 if (WithCrypto && (cur->security & ENCRYPT)) {
462 /* make sure we have the user's passphrase before proceeding... */
463 crypt_valid_passphrase (cur->security);
466 mutt_parse_mime_message (ctx, cur);
467 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
469 mutt_make_attribution (ctx, cur, out);
471 if (!option (OPTHEADER))
472 cmflags |= M_CM_NOHEADER;
473 if (option (OPTWEED)) {
474 chflags |= CH_WEED | CH_REORDER;
475 cmflags |= M_CM_WEED;
478 mutt_copy_message (out, ctx, cur, cmflags, chflags);
480 mutt_make_post_indent (ctx, cur, out);
485 static int default_to (ADDRESS ** to, ENVELOPE * env, int flags, int hmfupto)
490 if (flags && env->mail_followup_to && hmfupto == M_YES) {
491 rfc822_append (to, env->mail_followup_to);
495 /* Exit now if we're setting up the default Cc list for list-reply
496 * (only set if Mail-Followup-To is present and honoured).
498 if (flags & SENDLISTREPLY)
501 /* If this message came from a mailing list, ask the user if he really
502 * intended to reply to the author only.
504 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
505 switch (query_quadoption (OPT_LISTREPLY,
507 ("Message came from a mailing list. Reply to author only?")))
510 tmp = find_mailing_lists (env->to, env->cc);
511 rfc822_append (to, tmp);
512 rfc822_free_address (&tmp);
515 return -1; /* abort */
519 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
520 /* mail is from the user, assume replying to recipients */
521 rfc822_append (to, env->to);
523 else if (env->reply_to) {
524 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
525 (option (OPTIGNORELISTREPLYTO) &&
526 mutt_is_mail_list (env->reply_to) &&
527 (mutt_addrsrc (env->reply_to, env->to) ||
528 mutt_addrsrc (env->reply_to, env->cc)))) {
529 /* If the Reply-To: address is a mailing list, assume that it was
530 * put there by the mailing list, and use the From: address
532 * We also take the from header if our correspondant has a reply-to
533 * header which is identical to the electronic mail address given
534 * in his From header.
537 rfc822_append (to, env->from);
539 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
540 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
541 /* There are quite a few mailing lists which set the Reply-To:
542 * header field to the list address, which makes it quite impossible
543 * to send a message to only the sender of the message. This
544 * provides a way to do that.
546 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
547 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
548 switch (query_quadoption (OPT_REPLYTO, prompt)) {
550 rfc822_append (to, env->reply_to);
554 rfc822_append (to, env->from);
558 return (-1); /* abort */
562 rfc822_append (to, env->reply_to);
565 rfc822_append (to, env->from);
570 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
576 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
577 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
578 in->mail_followup_to->mailbox,
579 in->mail_followup_to->next ? ",..." : "");
581 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
585 if (flags & SENDLISTREPLY) {
586 tmp = find_mailing_lists (in->to, in->cc);
587 rfc822_append (&out->to, tmp);
588 rfc822_free_address (&tmp);
590 if (in->mail_followup_to && hmfupto == M_YES &&
591 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
592 return (-1); /* abort */
595 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
596 return (-1); /* abort */
598 if ((flags & SENDGROUPREPLY)
599 && (!in->mail_followup_to || hmfupto != M_YES)) {
600 /* if(!mutt_addr_is_user(in->to)) */
601 rfc822_append (&out->cc, in->to);
602 rfc822_append (&out->cc, in->cc);
608 LIST *mutt_make_references (ENVELOPE * e)
610 LIST *t = NULL, *l = NULL;
613 l = mutt_copy_list (e->references);
615 l = mutt_copy_list (e->in_reply_to);
618 t = mutt_new_list ();
619 t->data = safe_strdup (e->message_id);
627 void mutt_fix_reply_recipients (ENVELOPE * env)
629 mutt_expand_aliases_env (env);
631 if (!option (OPTMETOO)) {
632 /* the order is important here. do the CC: first so that if the
633 * the user is the only recipient, it ends up on the TO: field
635 env->cc = remove_user (env->cc, (env->to == NULL));
636 env->to = remove_user (env->to, (env->cc == NULL));
639 /* the CC field can get cluttered, especially with lists */
640 env->to = mutt_remove_duplicates (env->to);
641 env->cc = mutt_remove_duplicates (env->cc);
642 env->cc = mutt_remove_xrefs (env->to, env->cc);
645 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
649 /* set the default subject for the message. */
650 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
651 str_replace (&env->subject, buffer);
654 void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
655 HEADER * cur, ENVELOPE * curenv)
657 /* This takes precedence over a subject that might have
658 * been taken from a List-Post header. Is that correct?
660 if (curenv->real_subj) {
661 FREE (&env->subject);
662 env->subject = safe_malloc (safe_strlen (curenv->real_subj) + 5);
663 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
665 else if (!env->subject)
666 env->subject = safe_strdup ("Re: your mail");
669 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
670 env->x_comment_to = safe_strdup (mutt_get_name (curenv->from));
674 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
675 LIST *** pp, LIST *** qq)
677 LIST **p = NULL, **q = NULL;
685 p = &env->references;
687 q = &env->in_reply_to;
694 *p = mutt_make_references (curenv);
696 if (curenv->message_id) {
697 *q = mutt_new_list ();
698 (*q)->data = safe_strdup (curenv->message_id);
709 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
711 env->references = NULL;
712 env->in_reply_to = NULL;
716 LIST **p = NULL, **q = NULL;
719 for (i = 0; i < ctx->vcount; i++) {
720 h = ctx->hdrs[ctx->v2r[i]];
722 mutt_add_to_reference_headers (env, h->env, &p, &q);
726 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
730 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
732 ENVELOPE *curenv = NULL;
737 for (i = 0; i < ctx->vcount; i++)
738 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
739 cur = ctx->hdrs[ctx->v2r[i]];
745 /* This could happen if the user tagged some messages and then did
746 * a limit such that none of the tagged message are visible.
748 mutt_error _("No tagged messages are visible!");
756 if (flags & SENDREPLY) {
758 if ((flags & SENDNEWS)) {
759 /* in case followup set Newsgroups: with Followup-To: if it present */
760 if (!env->newsgroups && curenv &&
761 safe_strcasecmp (curenv->followup_to, "poster"))
762 env->newsgroups = safe_strdup (curenv->followup_to);
769 for (i = 0; i < ctx->vcount; i++) {
770 h = ctx->hdrs[ctx->v2r[i]];
771 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
775 else if (mutt_fetch_recips (env, curenv, flags) == -1)
778 if ((flags & SENDLISTREPLY) && !env->to) {
779 mutt_error _("No mailing lists found!");
784 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
785 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
787 else if (flags & SENDFORWARD)
788 mutt_make_forward_subject (env, ctx, cur);
793 static int generate_body (FILE * tempfp, /* stream for outgoing message */
794 HEADER * msg, /* header for outgoing message */
795 int flags, /* compose mode */
796 CONTEXT * ctx, /* current mailbox */
798 { /* current message */
803 if (flags & SENDREPLY) {
805 query_quadoption (OPT_INCLUDE,
806 _("Include message in reply?"))) == -1)
810 mutt_message _("Including quoted message...");
813 for (i = 0; i < ctx->vcount; i++) {
814 h = ctx->hdrs[ctx->v2r[i]];
816 if (include_reply (ctx, h, tempfp) == -1) {
817 mutt_error _("Could not include all requested messages!");
821 fputc ('\n', tempfp);
826 include_reply (ctx, cur, tempfp);
830 else if (flags & SENDFORWARD) {
832 query_quadoption (OPT_MIMEFWD,
833 _("Forward as attachment?"))) == M_YES) {
834 BODY *last = msg->content;
836 mutt_message _("Preparing forwarded message...");
838 while (last && last->next)
842 tmp = mutt_make_message_attach (ctx, cur, 0);
849 for (i = 0; i < ctx->vcount; i++) {
850 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
851 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
857 last = msg->content = tmp;
864 include_forward (ctx, cur, tempfp);
866 for (i = 0; i < ctx->vcount; i++)
867 if (ctx->hdrs[ctx->v2r[i]]->tagged)
868 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
873 /* if (WithCrypto && (flags & SENDKEY)) */
874 else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)) {
877 if ((WithCrypto & APPLICATION_PGP)
878 && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
881 tmp->next = msg->content;
890 void mutt_set_followup_to (ENVELOPE * e)
896 * Only generate the Mail-Followup-To if the user has requested it, and
897 * it hasn't already been set
900 if (!option (OPTFOLLOWUPTO))
903 if (option (OPTNEWSSEND)) {
904 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
905 e->followup_to = safe_strdup (e->newsgroups);
910 if (!e->mail_followup_to) {
911 if (mutt_is_list_cc (0, e->to, e->cc)) {
913 * this message goes to known mailing lists, so create a proper
914 * mail-followup-to header
917 t = rfc822_append (&e->mail_followup_to, e->to);
918 rfc822_append (&t, e->cc);
921 /* remove ourselves from the mail-followup-to header */
922 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
925 * If we are not subscribed to any of the lists in question,
926 * re-add ourselves to the mail-followup-to header. The
927 * mail-followup-to header generated is a no-op with group-reply,
928 * but makes sure list-reply has the desired effect.
931 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
933 from = rfc822_cpy_adr (e->reply_to);
935 from = rfc822_cpy_adr (e->from);
937 from = mutt_default_from ();
940 /* Normally, this loop will not even be entered. */
941 for (t = from; t && t->next; t = t->next);
943 t->next = e->mail_followup_to; /* t cannot be NULL at this point. */
944 e->mail_followup_to = from;
948 e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
954 /* look through the recipients of the message we are replying to, and if
955 we find an address that matches $alternates, we use that as the default
957 static ADDRESS *set_reverse_name (ENVELOPE * env)
961 for (tmp = env->to; tmp; tmp = tmp->next) {
962 if (mutt_addr_is_user (tmp))
966 for (tmp = env->cc; tmp; tmp = tmp->next) {
967 if (mutt_addr_is_user (tmp))
971 if (!tmp && mutt_addr_is_user (env->from))
974 tmp = rfc822_cpy_adr_real (tmp);
975 if (!option (OPTREVREAL))
976 FREE (&tmp->personal);
978 tmp->personal = safe_strdup (Realname);
983 ADDRESS *mutt_default_from (void)
986 const char *fqdn = mutt_fqdn (1);
989 * Note: We let $from override $realname here. Is this the right
994 adr = rfc822_cpy_adr_real (From);
995 else if (option (OPTUSEDOMAIN)) {
996 adr = rfc822_new_address ();
998 safe_malloc (safe_strlen (Username) + safe_strlen (fqdn) + 2);
999 sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn)); /* __SPRINTF_CHECKED__ */
1002 adr = rfc822_new_address ();
1003 adr->mailbox = safe_strdup (NONULL (Username));
1009 static int send_message (HEADER * msg)
1011 char tempfile[_POSIX_PATH_MAX];
1015 /* Write out the message in MIME form. */
1016 mutt_mktemp (tempfile);
1017 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
1021 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
1022 msg->chain ? 1 : 0);
1025 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1028 fputc ('\n', tempfp); /* tie off the header. */
1030 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1036 if (fclose (tempfp) != 0) {
1037 mutt_perror (tempfile);
1044 return mix_send_message (msg->chain, tempfile);
1047 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1048 msg->env->bcc, tempfile,
1049 (msg->content->encoding == ENC8BIT));
1053 /* rfc2047 encode the content-descriptions */
1054 static void encode_descriptions (BODY * b, short recurse)
1058 for (t = b; t; t = t->next) {
1059 if (t->description) {
1060 rfc2047_encode_string (&t->description);
1062 if (recurse && t->parts)
1063 encode_descriptions (t->parts, recurse);
1067 /* rfc2047 decode them in case of an error */
1068 static void decode_descriptions (BODY * b)
1072 for (t = b; t; t = t->next) {
1073 if (t->description) {
1074 rfc2047_decode (&t->description);
1077 decode_descriptions (t->parts);
1081 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1083 HEADER *msg = mutt_new_header ();
1085 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1088 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1091 int ci_send_message (int flags, /* send mode */
1092 HEADER * msg, /* template to use for new message */
1093 char *tempfile, /* file specified by -i or -H */
1094 CONTEXT * ctx, /* current mailbox */
1096 { /* current message */
1097 char buffer[LONG_STRING];
1098 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1099 FILE *tempfp = NULL;
1101 int i, killfrom = 0;
1103 int free_clear_content = 0;
1105 BODY *save_content = NULL;
1106 BODY *clear_content = NULL;
1107 char *pgpkeylist = NULL;
1109 /* save current value of "pgp_sign_as" */
1110 char *signas = NULL;
1111 char *tag = NULL, *err = NULL;
1116 if (flags & SENDNEWS)
1117 set_option (OPTNEWSSEND);
1119 unset_option (OPTNEWSSEND);
1122 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1123 mutt_num_postponed (1)) {
1124 /* If the user is composing a new message, check to see if there
1125 * are any postponed messages first.
1128 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1132 flags |= SENDPOSTPONED;
1136 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1137 signas = safe_strdup (PgpSignAs);
1139 /* Delay expansion of aliases until absolutely necessary--shouldn't
1140 * be necessary unless we are prompting the user or about to execute a
1145 msg = mutt_new_header ();
1147 if (flags == SENDPOSTPONED) {
1149 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1153 * If postponed message is a news article, it have
1154 * a "Newsgroups:" header line, then set appropriate flag.
1156 if (msg->env->newsgroups) {
1158 set_option (OPTNEWSSEND);
1162 unset_option (OPTNEWSSEND);
1167 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1168 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1169 mutt_perror (msg->content->filename);
1175 msg->env = mutt_new_envelope ();
1178 /* Parse and use an eventual list-post header */
1179 if ((flags & SENDLISTREPLY)
1180 && cur && cur->env && cur->env->list_post) {
1181 /* Use any list-post header as a template */
1182 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1183 /* We don't let them set the sender's address. */
1184 rfc822_free_address (&msg->env->from);
1187 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1188 pbody = mutt_new_body ();
1189 pbody->next = msg->content; /* don't kill command-line attachments */
1190 msg->content = pbody;
1192 mutt_parse_content_type (ContentType, msg->content);
1193 msg->content->unlink = 1;
1194 msg->content->use_disp = 0;
1195 msg->content->disposition = DISPINLINE;
1196 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1197 && !ascii_strcasecmp (msg->content->subtype, "plain"))
1198 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1201 mutt_mktemp (buffer);
1202 tempfp = safe_fopen (buffer, "w+");
1203 msg->content->filename = safe_strdup (buffer);
1206 tempfp = safe_fopen (tempfile, "a+");
1207 msg->content->filename = safe_strdup (tempfile);
1213 "newsend_message: can't create tempfile %s (errno=%d)\n",
1214 msg->content->filename, errno));
1215 mutt_perror (msg->content->filename);
1220 /* this is handled here so that the user can match ~f in send-hook */
1221 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1222 /* we shouldn't have to worry about freeing `msg->env->from' before
1223 * setting it here since this code will only execute when doing some
1224 * sort of reply. the pointer will only be set when using the -H command
1227 * We shouldn't have to worry about alias expansion here since we are
1228 * either replying to a real or postponed message, therefore no aliases
1229 * should exist since the user has not had the opportunity to add
1230 * addresses to the list. We just have to ensure the postponed messages
1231 * have their aliases expanded.
1234 msg->env->from = set_reverse_name (cur->env);
1237 if (!msg->env->from && option (OPTUSEFROM)
1238 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1239 msg->env->from = mutt_default_from ();
1241 if (flags & SENDBATCH) {
1242 mutt_copy_stream (stdin, tempfp);
1243 if (option (OPTHDRS)) {
1244 process_user_recips (msg->env);
1245 process_user_header (msg->env);
1247 mutt_expand_aliases_env (msg->env);
1249 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1250 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1251 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1254 if (option (OPTHDRS))
1255 process_user_recips (msg->env);
1257 /* Expand aliases and remove duplicates/crossrefs */
1258 mutt_fix_reply_recipients (msg->env);
1261 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1262 && !msg->env->newsgroups)
1263 msg->env->newsgroups = safe_strdup (((NNTP_DATA *) ctx->data)->group);
1266 if (!(flags & SENDMAILX) &&
1267 !(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1268 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1269 if (edit_envelope (msg->env, flags) == -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) {
1279 msg->env->from = mutt_default_from ();
1283 if ((flags & SENDREPLY) && cur) {
1284 /* change setting based upon message we are replying to */
1285 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1288 * set the replied flag for the message we are generating so that the
1289 * user can use ~Q in a send-hook to know when reply-hook's are also
1295 /* change settings based upon recipients */
1297 mutt_message_hook (NULL, msg, M_SENDHOOK);
1300 * Unset the replied flag from the message we are composing since it is
1301 * no longer required. This is done here because the FCC'd copy of
1302 * this message was erroneously get the 'R'eplied flag when stored in
1303 * a maildir-style mailbox.
1308 rfc822_free_address (&msg->env->from);
1312 if (option (OPTHDRS))
1313 process_user_header (msg->env);
1316 if (option (OPTSIGONTOP)
1317 && (!(flags & (SENDMAILX | SENDKEY)) && Editor
1318 && safe_strcmp (Editor, "builtin") != 0))
1319 append_signature (tempfp);
1321 /* include replies/forwarded messages, unless we are given a template */
1322 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1323 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1326 if (!option (OPTSIGONTOP)
1327 && (!(flags & (SENDMAILX | SENDKEY)) && Editor
1328 && safe_strcmp (Editor, "builtin") != 0))
1329 append_signature (tempfp);
1332 * this wants to be done _after_ generate_body, so message-hooks
1336 if (WithCrypto && !(flags & SENDMAILX)) {
1337 if (option (OPTCRYPTAUTOSIGN))
1338 msg->security |= SIGN;
1339 if (option (OPTCRYPTAUTOENCRYPT))
1340 msg->security |= ENCRYPT;
1341 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1342 msg->security |= ENCRYPT;
1343 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1344 msg->security |= SIGN;
1345 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1346 && (cur->security & ENCRYPT))
1347 msg->security |= SIGN;
1348 if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN))) {
1349 if (option (OPTPGPAUTOINLINE))
1350 msg->security |= INLINE;
1351 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1352 msg->security |= INLINE;
1356 if (WithCrypto && msg->security) {
1358 * When reypling / forwarding, use the original message's
1359 * crypto system. According to the documentation,
1360 * smime_is_default should be disregarded here.
1362 * Problem: At least with forwarding, this doesn't really
1363 * make much sense. Should we have an option to completely
1364 * disable individual mechanisms at run-time?
1367 if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP)
1368 && (cur->security & APPLICATION_PGP))
1369 msg->security |= APPLICATION_PGP;
1370 else if ((WithCrypto & APPLICATION_SMIME)
1371 && option (OPTCRYPTAUTOSMIME)
1372 && (cur->security & APPLICATION_SMIME))
1373 msg->security |= APPLICATION_SMIME;
1377 * No crypto mechanism selected? Use availability + smime_is_default
1380 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1381 if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
1382 && option (OPTSMIMEISDEFAULT))
1383 msg->security |= APPLICATION_SMIME;
1384 else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
1385 msg->security |= APPLICATION_PGP;
1386 else if ((WithCrypto & APPLICATION_SMIME)
1387 && option (OPTCRYPTAUTOSMIME))
1388 msg->security |= APPLICATION_SMIME;
1392 /* No permissible mechanisms found. Don't sign or encrypt. */
1393 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1398 * This hook is even called for postponed messages, and can, e.g., be
1399 * used for setting the editor, the sendmail path, or the
1402 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1404 /* wait until now to set the real name portion of our return address so
1405 that $realname can be set in a send-hook */
1406 if (msg->env->from && !msg->env->from->personal
1407 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1408 msg->env->from->personal = safe_strdup (Realname);
1410 if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
1411 safe_fclose (&tempfp);
1413 if (flags & SENDMAILX) {
1414 if (mutt_builtin_editor (msg->content->filename, msg, cur) == -1)
1417 else if (!(flags & SENDBATCH)) {
1419 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1421 mutt_update_encoding (msg->content);
1424 * Select whether or not the user's editor should be called now. We
1425 * don't want to do this when:
1426 * 1) we are sending a key/cert
1427 * 2) we are forwarding a message and the user doesn't want to edit it.
1428 * This is controled by the quadoption $forward_edit. However, if
1429 * both $edit_headers and $autoedit are set, we want to ignore the
1430 * setting of $forward_edit because the user probably needs to add the
1433 if (!(flags & SENDKEY) &&
1434 ((flags & SENDFORWARD) == 0 ||
1435 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1436 query_quadoption (OPT_FORWEDIT,
1437 _("Edit forwarded message?")) == M_YES)) {
1438 /* If the this isn't a text message, look for a mailcap edit command */
1439 if (mutt_needs_mailcap (msg->content))
1440 mutt_edit_attachment (msg->content);
1441 else if (!Editor || safe_strcmp ("builtin", Editor) == 0)
1442 mutt_builtin_editor (msg->content->filename, msg, cur);
1443 else if (option (OPTEDITHDRS)) {
1444 mutt_env_to_local (msg->env);
1445 mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1447 mutt_env_to_idna (msg->env, NULL, NULL);
1450 mutt_edit_file (Editor, msg->content->filename);
1452 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1455 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1456 if (stat (msg->content->filename, &st) == 0) {
1457 /* if the file was not modified, bail out now */
1458 if (mtime == st.st_mtime && !msg->content->next &&
1459 query_quadoption (OPT_ABORT,
1460 _("Abort unmodified message?")) == M_YES) {
1461 mutt_message _("Aborted unmodified message.");
1467 mutt_perror (msg->content->filename);
1471 /* specify a default fcc. if we are in batchmode, only save a copy of
1472 * the message if the value of $copy is yes or ask-yes */
1474 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1475 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1476 /* set the default FCC */
1477 if (!msg->env->from) {
1478 msg->env->from = mutt_default_from ();
1479 killfrom = 1; /* no need to check $use_from because if the user specified
1480 a from address it would have already been set by now */
1482 mutt_select_fcc (fcc, sizeof (fcc), msg);
1484 rfc822_free_address (&msg->env->from);
1490 mutt_update_encoding (msg->content);
1492 if (!(flags & (SENDMAILX | SENDBATCH))) {
1495 fcc_error = 0; /* reset value since we may have failed before */
1496 mutt_pretty_mailbox (fcc);
1497 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1501 if (flags & SENDNEWS)
1502 mutt_message (_("Article not posted."));
1506 mutt_message _("Mail not sent.");
1510 /* postpone the message until later. */
1511 if (msg->content->next)
1512 msg->content = mutt_make_multipart (msg->content);
1515 * make sure the message is written to the right part of a maildir
1521 encode_descriptions (msg->content, 1);
1522 mutt_prepare_envelope (msg->env, 0);
1523 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1526 || mutt_write_fcc (NONULL (Postponed), msg,
1528 && (flags & SENDREPLY)) ? cur->env->
1529 message_id : NULL, 1, fcc) < 0) {
1530 msg->content = mutt_remove_multipart (msg->content);
1531 decode_descriptions (msg->content);
1532 mutt_unprepare_envelope (msg->env);
1535 mutt_update_num_postponed ();
1536 mutt_message _("Message postponed.");
1543 if (!(flags & SENDNEWS))
1545 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1546 if (!(flags & SENDBATCH)) {
1547 mutt_error _("No recipients are specified!");
1552 puts _("No recipients were specified.");
1558 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1559 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1561 if (!(flags & SENDBATCH))
1567 if (!msg->env->subject && !(flags & SENDBATCH) &&
1569 query_quadoption (OPT_SUBJECT,
1570 _("No subject, abort sending?"))) != M_NO) {
1571 /* if the abort is automatic, print an error message */
1572 if (quadoption (OPT_SUBJECT) == M_YES)
1573 mutt_error _("No subject specified.");
1578 if ((flags & SENDNEWS) && !msg->env->subject) {
1579 mutt_error _("No subject specified.");
1584 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1585 mutt_error _("No newsgroup specified.");
1591 if (msg->content->next)
1592 msg->content = mutt_make_multipart (msg->content);
1595 * Ok, we need to do it this way instead of handling all fcc stuff in
1596 * one place in order to avoid going to main_loop with encoded "env"
1597 * in case of error. Ugh.
1600 encode_descriptions (msg->content, 1);
1603 * Make sure that clear_content and free_clear_content are
1604 * properly initialized -- we may visit this particular place in
1605 * the code multiple times, including after a failed call to
1609 clear_content = NULL;
1610 free_clear_content = 0;
1613 if (msg->security) {
1614 /* save the decrypted attachments */
1615 clear_content = msg->content;
1617 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1618 mutt_protect (msg, pgpkeylist) == -1) {
1619 msg->content = mutt_remove_multipart (msg->content);
1623 decode_descriptions (msg->content);
1626 encode_descriptions (msg->content, 0);
1630 * at this point, msg->content is one of the following three things:
1631 * - multipart/signed. In this case, clear_content is a child.
1632 * - multipart/encrypted. In this case, clear_content exists
1634 * - application/pgp. In this case, clear_content exists independently.
1635 * - something else. In this case, it's the same as clear_content.
1638 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1640 if (clear_content && (msg->content != clear_content)
1641 && (msg->content->parts != clear_content))
1642 free_clear_content = 1;
1645 if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1646 mutt_message _("Sending message...");
1648 mutt_prepare_envelope (msg->env, 1);
1650 /* save a copy of the message, if necessary. */
1652 mutt_expand_path (fcc, sizeof (fcc));
1655 /* Don't save a copy when we are in batch-mode, and the FCC
1656 * folder is on an IMAP server: This would involve possibly lots
1657 * of user interaction, which is not available in batch mode.
1659 * Note: A patch to fix the problems with the use of IMAP servers
1660 * from non-curses mode is available from Brendan Cully. However,
1661 * I'd like to think a bit more about this before including it.
1665 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1669 if (*fcc && safe_strcmp ("/dev/null", fcc) != 0) {
1670 BODY *tmpbody = msg->content;
1671 BODY *save_sig = NULL;
1672 BODY *save_parts = NULL;
1674 if (WithCrypto && msg->security && option (OPTFCCCLEAR))
1675 msg->content = clear_content;
1677 /* check to see if the user wants copies of all attachments */
1678 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1680 && (safe_strcmp (msg->content->subtype, "encrypted") == 0 ||
1681 safe_strcmp (msg->content->subtype, "signed") == 0)) {
1682 if (clear_content->type == TYPEMULTIPART) {
1683 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1684 /* save initial signature and attachments */
1685 save_sig = msg->content->parts->next;
1686 save_parts = clear_content->parts->next;
1689 /* this means writing only the main part */
1690 msg->content = clear_content->parts;
1692 if (mutt_protect (msg, pgpkeylist) == -1) {
1693 /* we can't do much about it at this point, so
1694 * fallback to saving the whole thing to fcc
1696 msg->content = tmpbody;
1701 save_content = msg->content;
1705 msg->content = msg->content->parts;
1710 /* update received time so that when storing to a mbox-style folder
1711 * the From_ line contains the current time instead of when the
1712 * message was first postponed.
1714 msg->received = time (NULL);
1715 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1717 * Error writing FCC, we should abort sending.
1723 msg->content = tmpbody;
1725 if (WithCrypto && save_sig) {
1726 /* cleanup the second signature structures */
1727 if (save_content->parts) {
1728 mutt_free_body (&save_content->parts->next);
1729 save_content->parts = NULL;
1731 mutt_free_body (&save_content);
1733 /* restore old signature and attachments */
1734 msg->content->parts->next = save_sig;
1735 msg->content->parts->parts->next = save_parts;
1737 else if (WithCrypto && save_content) {
1738 /* destroy the new encrypted body. */
1739 mutt_free_body (&save_content);
1746 * Don't attempt to send the message if the FCC failed. Just pretend
1747 * the send failed as well so we give the user a chance to fix the
1750 if (fcc_error || (i = send_message (msg)) == -1) {
1751 if (!(flags & SENDBATCH)) {
1753 else if ((msg->security & ENCRYPT) || ((msg->security & SIGN)
1754 && msg->content->type ==
1756 mutt_free_body (&msg->content); /* destroy PGP data */
1757 msg->content = clear_content; /* restore clear text. */
1759 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1760 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1761 msg->content = mutt_remove_multipart (msg->content);
1764 msg->content = mutt_remove_multipart (msg->content);
1765 decode_descriptions (msg->content);
1766 mutt_unprepare_envelope (msg->env);
1770 puts _("Could not send the message.");
1775 else if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1776 mutt_message (i != 0 ? _("Sending in background.") :
1778 (flags & SENDNEWS) ? _("Article posted.") :
1784 if (WithCrypto && (msg->security & ENCRYPT))
1787 if (WithCrypto && free_clear_content)
1788 mutt_free_body (&clear_content);
1790 if (flags & SENDREPLY) {
1792 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1793 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1794 for (i = 0; i < ctx->vcount; i++)
1795 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1796 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1805 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED)) {
1812 safe_fclose (&tempfp);
1813 mutt_free_header (&msg);
1818 /* vim: set sw=2: */