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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/file.h>
20 #include <lib-mime/mime.h>
24 #include "mutt_curses.h"
29 #include "mutt_crypt.h"
30 #include "mutt_idna.h"
34 #include "lib/debug.h"
45 #include <sys/types.h>
57 static void append_signature (FILE * f)
63 fprintf (f, "\n%s", SignOffString);
66 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
67 if (option (OPTSIGDASHES))
69 else if (SignOffString)
71 mutt_copy_stream (tmpfp, f);
74 mutt_wait_filter (thepid);
78 /* compare two e-mail addresses and return 1 if they are equivalent */
79 static int mutt_addrcmp (address_t * a, address_t * b)
81 if (!a->mailbox || !b->mailbox)
83 if (ascii_strcasecmp (a->mailbox, b->mailbox))
88 /* search an e-mail address in a list */
89 static int mutt_addrsrc (address_t * a, address_t * lst)
91 for (; lst; lst = lst->next) {
92 if (mutt_addrcmp (a, lst))
98 /* removes addresses from "b" which are contained in "a" */
99 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
101 address_t *top, *p, *prev = NULL;
105 for (p = a; p; p = p->next) {
106 if (mutt_addrcmp (p, b))
111 prev->next = b->next;
131 /* remove any address which matches the current user. if `leave_only' is
132 * nonzero, don't remove the user's address if it is the only one in the list
134 static address_t *remove_user (address_t * a, int leave_only)
136 address_t *top = NULL, *last = NULL;
139 if (!mutt_addr_is_user (a)) {
153 if (!leave_only || a || last) {
155 address_delete (&tmp);
164 static address_t *find_mailing_lists (address_t * t, address_t * c)
166 address_t *top = NULL, *ptr = NULL;
168 for (; t || c; t = c, c = NULL) {
169 for (; t; t = t->next) {
170 if (mutt_is_mail_list (t) && !t->group) {
172 ptr->next = address_dup (t);
176 ptr = top = address_dup (t);
183 static int edit_address (address_t ** a, const char *field)
185 char buf[HUGE_STRING];
191 mutt_addrlist_to_local (*a);
192 rfc822_write_address (buf, sizeof (buf), *a, 0);
193 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
196 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
197 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
198 mutt_error (_("Error: '%s' is a bad IDN."), err);
204 while (idna_ok != 0);
208 static int edit_envelope (ENVELOPE * en, int flags)
210 char buf[HUGE_STRING];
211 LIST *uh = UserHeader;
212 regmatch_t pat_match[1];
215 if (option (OPTNEWSSEND)) {
217 m_strcpy(buf, sizeof(buf), en->newsgroups);
220 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
222 p_delete(&en->newsgroups);
223 en->newsgroups = m_strdup(buf);
226 m_strcpy(buf, sizeof(buf), en->followup_to);
229 if (option (OPTASKFOLLOWUP)
230 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
232 p_delete(&en->followup_to);
233 en->followup_to = m_strdup(buf);
235 if (en->x_comment_to)
236 m_strcpy(buf, sizeof(buf), en->x_comment_to);
239 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
240 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
242 p_delete(&en->x_comment_to);
243 en->x_comment_to = m_strdup(buf);
248 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
250 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
252 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
257 if (option (OPTFASTREPLY))
260 m_strcpy(buf, sizeof(buf), en->subject);
266 for (; uh; uh = uh->next) {
267 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
268 p = vskipspaces(uh->data + 8);
269 m_strcpy(buf, sizeof(buf), p);
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 m_strreplace(&en->subject, buf);
305 char *nntp_get_header(const char *s)
307 return m_strdup(skipspaces(s));
311 static void process_user_recips (ENVELOPE * env)
313 LIST *uh = UserHeader;
315 for (; uh; uh = uh->next) {
316 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
317 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
318 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
319 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
320 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
321 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
323 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
324 env->newsgroups = nntp_get_header (uh->data + 11);
325 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
326 env->followup_to = nntp_get_header (uh->data + 12);
327 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
328 env->x_comment_to = nntp_get_header (uh->data + 13);
333 static void process_user_header (ENVELOPE * env)
335 LIST *uh = UserHeader;
336 LIST *last = env->userhdrs;
342 for (; uh; uh = uh->next) {
343 if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
344 /* User has specified a default From: address. Remove default address */
345 address_delete (&env->from);
346 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
348 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
349 address_delete (&env->reply_to);
350 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
352 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
353 m_strreplace(&env->message_id, uh->data + 11);
354 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
355 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
356 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
358 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
359 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
360 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
362 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
363 ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
365 last->next = mutt_new_list ();
369 last = env->userhdrs = mutt_new_list ();
370 last->data = m_strdup(uh->data);
375 void mutt_forward_intro (FILE * fp, HEADER * cur)
379 fputs ("----- Forwarded message from ", fp);
381 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
383 fputs (" -----\n\n", fp);
386 void mutt_forward_trailer (FILE * fp)
388 fputs ("\n----- End forwarded message -----\n", fp);
392 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
394 int chflags = CH_DECODE, cmflags = 0;
396 mutt_parse_mime_message (ctx, cur);
397 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
399 if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
400 /* make sure we have the user's passphrase before proceeding... */
401 crypt_valid_passphrase (cur->security);
404 mutt_forward_intro (out, cur);
406 if (option (OPTFORWDECODE)) {
407 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
408 if (option (OPTWEED)) {
409 chflags |= CH_WEED | CH_REORDER;
410 cmflags |= M_CM_WEED;
413 if (option (OPTFORWQUOTE))
414 cmflags |= M_CM_PREFIX;
416 mutt_copy_message (out, ctx, cur, cmflags, chflags);
417 mutt_forward_trailer (out);
421 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
426 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
432 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
436 if (PostIndentString) {
437 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
443 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
445 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
446 int chflags = CH_DECODE;
448 if (WithCrypto && (cur->security & ENCRYPT)) {
449 /* make sure we have the user's passphrase before proceeding... */
450 crypt_valid_passphrase (cur->security);
453 mutt_parse_mime_message (ctx, cur);
454 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
456 mutt_make_attribution (ctx, cur, out);
458 if (!option (OPTHEADER))
459 cmflags |= M_CM_NOHEADER;
460 if (option (OPTWEED)) {
461 chflags |= CH_WEED | CH_REORDER;
462 cmflags |= M_CM_WEED;
465 mutt_copy_message (out, ctx, cur, cmflags, chflags);
467 mutt_make_post_indent (ctx, cur, out);
472 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
476 if (flags && env->mail_followup_to && hmfupto == M_YES) {
477 address_list_append(to, address_list_dup(env->mail_followup_to));
481 /* Exit now if we're setting up the default Cc list for list-reply
482 * (only set if Mail-Followup-To is present and honoured).
484 if (flags & SENDLISTREPLY)
487 /* If this message came from a mailing list, ask the user if he really
488 * intended to reply to the author only.
490 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
491 switch (query_quadoption (OPT_LISTREPLY,
492 _("Message came from a mailing list. List-reply to mailing list?")))
495 address_list_append(to, find_mailing_lists (env->to, env->cc));
498 return -1; /* abort */
502 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
503 /* mail is from the user, assume replying to recipients */
504 address_list_append(to, address_list_dup(env->to));
506 else if (env->reply_to) {
507 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
508 (option (OPTIGNORELISTREPLYTO) &&
509 mutt_is_mail_list (env->reply_to) &&
510 (mutt_addrsrc (env->reply_to, env->to) ||
511 mutt_addrsrc (env->reply_to, env->cc)))) {
512 /* If the Reply-To: address is a mailing list, assume that it was
513 * put there by the mailing list, and use the From: address
515 * We also take the from header if our correspondant has a reply-to
516 * header which is identical to the electronic mail address given
517 * in his From header.
520 address_list_append(to, address_list_dup(env->from));
522 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
523 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
524 /* There are quite a few mailing lists which set the Reply-To:
525 * header field to the list address, which makes it quite impossible
526 * to send a message to only the sender of the message. This
527 * provides a way to do that.
529 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
530 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
531 switch (query_quadoption (OPT_REPLYTO, prompt)) {
533 address_list_append(to, address_list_dup(env->reply_to));
537 address_list_append(to, address_list_dup(env->from));
541 return (-1); /* abort */
545 address_list_append(to, address_list_dup(env->reply_to));
548 address_list_append(to, address_list_dup(env->from));
553 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
558 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
559 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
560 in->mail_followup_to->mailbox,
561 in->mail_followup_to->next ? ",..." : "");
563 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
567 if (flags & SENDLISTREPLY) {
568 address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
570 if (in->mail_followup_to && hmfupto == M_YES &&
571 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
572 return (-1); /* abort */
575 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
576 return (-1); /* abort */
578 if ((flags & SENDGROUPREPLY)
579 && (!in->mail_followup_to || hmfupto != M_YES))
581 address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
582 address_list_append(tmp, address_list_dup(in->cc));
588 LIST *mutt_make_references (ENVELOPE * e)
590 LIST *t = NULL, *l = NULL;
593 l = mutt_copy_list (e->references);
595 l = mutt_copy_list (e->in_reply_to);
598 t = mutt_new_list ();
599 t->data = m_strdup(e->message_id);
607 void mutt_fix_reply_recipients (ENVELOPE * env)
609 mutt_expand_aliases_env (env);
611 if (!option (OPTMETOO)) {
612 /* the order is important here. do the CC: first so that if the
613 * the user is the only recipient, it ends up on the TO: field
615 env->cc = remove_user (env->cc, (env->to == NULL));
616 env->to = remove_user (env->to, (env->cc == NULL));
619 /* the CC field can get cluttered, especially with lists */
620 env->to = mutt_remove_duplicates (env->to);
621 env->cc = mutt_remove_duplicates (env->cc);
622 env->cc = mutt_remove_xrefs (env->to, env->cc);
624 if (env->cc && !env->to) {
630 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
634 /* set the default subject for the message. */
635 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
636 m_strreplace(&env->subject, buffer);
639 void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
640 HEADER * cur, ENVELOPE * curenv)
642 /* This takes precedence over a subject that might have
643 * been taken from a List-Post header. Is that correct?
645 if (curenv->real_subj) {
646 p_delete(&env->subject);
647 env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
648 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
650 else if (!env->subject)
651 env->subject = m_strdup("Re: your mail");
654 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
655 env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
659 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
660 LIST *** pp, LIST *** qq)
662 LIST **p = NULL, **q = NULL;
670 p = &env->references;
672 q = &env->in_reply_to;
679 *p = mutt_make_references (curenv);
681 if (curenv->message_id) {
682 *q = mutt_new_list ();
683 (*q)->data = m_strdup(curenv->message_id);
694 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
696 env->references = NULL;
697 env->in_reply_to = NULL;
701 LIST **p = NULL, **q = NULL;
704 for (i = 0; i < ctx->vcount; i++) {
705 h = ctx->hdrs[ctx->v2r[i]];
707 mutt_add_to_reference_headers (env, h->env, &p, &q);
711 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
715 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
717 ENVELOPE *curenv = NULL;
722 for (i = 0; i < ctx->vcount; i++)
723 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
724 cur = ctx->hdrs[ctx->v2r[i]];
730 /* This could happen if the user tagged some messages and then did
731 * a limit such that none of the tagged message are visible.
733 mutt_error _("No tagged messages are visible!");
741 if (flags & SENDREPLY) {
743 if ((flags & SENDNEWS)) {
744 /* in case followup set Newsgroups: with Followup-To: if it present */
745 if (!env->newsgroups && curenv &&
746 m_strcasecmp(curenv->followup_to, "poster"))
747 env->newsgroups = m_strdup(curenv->followup_to);
754 for (i = 0; i < ctx->vcount; i++) {
755 h = ctx->hdrs[ctx->v2r[i]];
756 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
760 else if (mutt_fetch_recips (env, curenv, flags) == -1)
763 if ((flags & SENDLISTREPLY) && !env->to) {
764 mutt_error _("No mailing lists found!");
769 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
770 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
772 else if (flags & SENDFORWARD)
773 mutt_make_forward_subject (env, ctx, cur);
778 static int generate_body (FILE * tempfp, /* stream for outgoing message */
779 HEADER * msg, /* header for outgoing message */
780 int flags, /* compose mode */
781 CONTEXT * ctx, /* current mailbox */
783 { /* current message */
788 if (flags & SENDREPLY) {
790 query_quadoption (OPT_INCLUDE,
791 _("Include message in reply?"))) == -1)
795 mutt_message _("Including quoted message...");
798 for (i = 0; i < ctx->vcount; i++) {
799 h = ctx->hdrs[ctx->v2r[i]];
801 if (include_reply (ctx, h, tempfp) == -1) {
802 mutt_error _("Could not include all requested messages!");
806 fputc ('\n', tempfp);
811 include_reply (ctx, cur, tempfp);
815 else if (flags & SENDFORWARD) {
817 query_quadoption (OPT_MIMEFWD,
818 _("Forward as attachment?"))) == M_YES) {
819 BODY *last = msg->content;
821 mutt_message _("Preparing forwarded message...");
823 while (last && last->next)
827 tmp = mutt_make_message_attach (ctx, cur, 0);
834 for (i = 0; i < ctx->vcount; i++) {
835 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
836 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
842 last = msg->content = tmp;
849 include_forward (ctx, cur, tempfp);
851 for (i = 0; i < ctx->vcount; i++)
852 if (ctx->hdrs[ctx->v2r[i]]->tagged)
853 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
858 /* if (WithCrypto && (flags & SENDKEY)) */
859 else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)) {
862 if ((WithCrypto & APPLICATION_PGP)
863 && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
866 tmp->next = msg->content;
875 void mutt_set_followup_to (ENVELOPE * e)
880 * Only generate the Mail-Followup-To if the user has requested it, and
881 * it hasn't already been set
884 if (!option (OPTFOLLOWUPTO))
887 if (option (OPTNEWSSEND)) {
888 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
889 e->followup_to = m_strdup(e->newsgroups);
894 if (!e->mail_followup_to) {
895 if (mutt_is_list_cc (0, e->to, e->cc)) {
898 * this message goes to known mailing lists, so create a proper
899 * mail-followup-to header
902 tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
903 address_list_append(tmp, address_list_dup(e->cc));
906 /* remove ourselves from the mail-followup-to header */
907 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
910 * If we are not subscribed to any of the lists in question,
911 * re-add ourselves to the mail-followup-to header. The
912 * mail-followup-to header generated is a no-op with group-reply,
913 * but makes sure list-reply has the desired effect.
916 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
918 from = address_list_dup (e->reply_to);
920 from = address_list_dup (e->from);
922 from = mutt_default_from ();
925 address_list_append(&from->next, e->mail_followup_to);
926 e->mail_followup_to = from;
930 e->mail_followup_to = mutt_remove_duplicates(e->mail_followup_to);
936 /* look through the recipients of the message we are replying to, and if
937 we find an address that matches $alternates, we use that as the default
939 static address_t *set_reverse_name (ENVELOPE * env)
943 for (tmp = env->to; tmp; tmp = tmp->next) {
944 if (mutt_addr_is_user (tmp))
948 for (tmp = env->cc; tmp; tmp = tmp->next) {
949 if (mutt_addr_is_user (tmp))
953 if (!tmp && mutt_addr_is_user (env->from))
956 tmp = address_dup (tmp);
957 if (!option (OPTREVREAL))
958 p_delete(&tmp->personal);
960 tmp->personal = m_strdup(Realname);
965 address_t *mutt_default_from (void)
968 const char *fqdn = mutt_fqdn (1);
971 * Note: We let $from override $realname here. Is this the right
976 adr = address_dup (From);
977 else if (option (OPTUSEDOMAIN)) {
978 adr = address_new ();
979 adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
980 sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn)); /* __SPRINTF_CHECKED__ */
983 adr = address_new ();
984 adr->mailbox = m_strdup(NONULL (Username));
990 static int send_message (HEADER * msg)
992 char tempfile[_POSIX_PATH_MAX];
996 /* Write out the message in MIME form. */
997 mutt_mktemp (tempfile);
998 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
1002 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
1003 msg->chain ? 1 : 0);
1006 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1009 fputc ('\n', tempfp); /* tie off the header. */
1011 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1017 if (fclose (tempfp) != 0) {
1018 mutt_perror (tempfile);
1025 return mix_send_message (msg->chain, tempfile);
1028 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1029 msg->env->bcc, tempfile,
1030 (msg->content->encoding == ENC8BIT));
1034 /* rfc2047 encode the content-descriptions */
1035 static void encode_descriptions (BODY * b, short recurse)
1039 for (t = b; t; t = t->next) {
1040 if (t->description) {
1041 rfc2047_encode_string (&t->description);
1043 if (recurse && t->parts)
1044 encode_descriptions (t->parts, recurse);
1048 /* rfc2047 decode them in case of an error */
1049 static void decode_descriptions (BODY * b)
1053 for (t = b; t; t = t->next) {
1054 if (t->description) {
1055 rfc2047_decode (&t->description);
1058 decode_descriptions (t->parts);
1062 static void fix_end_of_file (const char *data)
1067 if ((fp = safe_fopen (data, "a+")) == NULL)
1069 fseeko (fp, -1, SEEK_END);
1070 if ((c = fgetc (fp)) != '\n')
1075 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1077 HEADER *msg = mutt_new_header ();
1079 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1082 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1085 int ci_send_message (int flags, /* send mode */
1086 HEADER * msg, /* template to use for new message */
1087 char *tempfile, /* file specified by -i or -H */
1088 CONTEXT * ctx, /* current mailbox */
1090 { /* current message */
1091 char buffer[LONG_STRING];
1092 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1093 FILE *tempfp = NULL;
1095 int i, killfrom = 0;
1097 int free_clear_content = 0;
1099 BODY *save_content = NULL;
1100 BODY *clear_content = NULL;
1101 char *pgpkeylist = NULL;
1103 /* save current value of "pgp_sign_as" */
1104 char *signas = NULL, *err = NULL;
1105 const char *tag = NULL;
1111 if (flags & SENDNEWS)
1112 set_option (OPTNEWSSEND);
1114 unset_option (OPTNEWSSEND);
1117 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1118 mutt_num_postponed (1)) {
1119 /* If the user is composing a new message, check to see if there
1120 * are any postponed messages first.
1123 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1127 flags |= SENDPOSTPONED;
1131 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
1132 signas = m_strdup(PgpSignAs);
1134 /* Delay expansion of aliases until absolutely necessary--shouldn't
1135 * be necessary unless we are prompting the user or about to execute a
1140 msg = mutt_new_header ();
1142 if (flags == SENDPOSTPONED) {
1144 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1148 * If postponed message is a news article, it have
1149 * a "Newsgroups:" header line, then set appropriate flag.
1151 if (msg->env->newsgroups) {
1153 set_option (OPTNEWSSEND);
1157 unset_option (OPTNEWSSEND);
1162 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1163 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1164 mutt_perror (msg->content->filename);
1170 msg->env = mutt_new_envelope ();
1173 /* Parse and use an eventual list-post header */
1174 if ((flags & SENDLISTREPLY)
1175 && cur && cur->env && cur->env->list_post) {
1176 /* Use any list-post header as a template */
1177 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1178 /* We don't let them set the sender's address. */
1179 address_delete (&msg->env->from);
1182 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1183 pbody = mutt_new_body ();
1184 pbody->next = msg->content; /* don't kill command-line attachments */
1185 msg->content = pbody;
1187 if (!(ctype = m_strdup(ContentType)))
1188 ctype = m_strdup("text/plain");
1189 mutt_parse_content_type (ctype, msg->content);
1192 msg->content->unlink = 1;
1193 msg->content->use_disp = 0;
1194 msg->content->disposition = DISPINLINE;
1195 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1196 && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1197 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1198 if (option (OPTDELSP))
1199 mutt_set_parameter ("delsp", "yes", &msg->content->parameter);
1203 mutt_mktemp (buffer);
1204 tempfp = safe_fopen (buffer, "w+");
1205 msg->content->filename = m_strdup(buffer);
1208 tempfp = safe_fopen (tempfile, "a+");
1209 msg->content->filename = m_strdup(tempfile);
1213 debug_print (1, ("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 = m_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 address_delete (&msg->env->from);
1312 if (option (OPTHDRS))
1313 process_user_header (msg->env);
1316 if (option (OPTSIGONTOP)
1317 && (!(flags & (SENDMAILX | SENDKEY)) && Editor
1318 && m_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 && m_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 = m_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 if (!mutt_edit_attachment (msg->content))
1442 } else if (!Editor || m_strcmp("builtin", Editor) == 0)
1443 mutt_builtin_editor (msg->content->filename, msg, cur);
1444 else if (option (OPTEDITHDRS)) {
1445 mutt_env_to_local (msg->env);
1446 mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1448 mutt_env_to_idna (msg->env, NULL, NULL);
1451 mutt_edit_file (Editor, msg->content->filename);
1453 if (stat (msg->content->filename, &st) == 0) {
1454 if (mtime != st.st_mtime)
1455 fix_end_of_file (msg->content->filename);
1457 mutt_perror (msg->content->filename);
1460 if (option (OPTTEXTFLOWED))
1461 rfc3676_space_stuff (msg);
1463 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1466 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1467 if (stat (msg->content->filename, &st) == 0) {
1468 /* if the file was not modified, bail out now */
1469 if (mtime == st.st_mtime && !msg->content->next &&
1470 query_quadoption (OPT_ABORT,
1471 _("Abort unmodified message?")) == M_YES) {
1472 mutt_message _("Aborted unmodified message.");
1478 mutt_perror (msg->content->filename);
1482 /* specify a default fcc. if we are in batchmode, only save a copy of
1483 * the message if the value of $copy is yes or ask-yes */
1485 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1486 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1487 /* set the default FCC */
1488 if (!msg->env->from) {
1489 msg->env->from = mutt_default_from ();
1490 killfrom = 1; /* no need to check $use_from because if the user specified
1491 a from address it would have already been set by now */
1493 mutt_select_fcc (fcc, sizeof (fcc), msg);
1495 address_delete (&msg->env->from);
1501 mutt_update_encoding (msg->content);
1503 if (!(flags & (SENDMAILX | SENDBATCH))) {
1506 fcc_error = 0; /* reset value since we may have failed before */
1507 mutt_pretty_mailbox (fcc);
1508 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1512 if (flags & SENDNEWS)
1513 mutt_message (_("Article not posted."));
1517 mutt_message _("Mail not sent.");
1521 /* postpone the message until later. */
1522 if (msg->content->next)
1523 msg->content = mutt_make_multipart (msg->content);
1526 * make sure the message is written to the right part of a maildir
1532 encode_descriptions (msg->content, 1);
1533 mutt_prepare_envelope (msg->env, 0);
1534 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1537 || mutt_write_fcc (NONULL (Postponed), msg,
1539 && (flags & SENDREPLY)) ? cur->env->
1540 message_id : NULL, 1, fcc) < 0) {
1541 msg->content = mutt_remove_multipart (msg->content);
1542 decode_descriptions (msg->content);
1543 mutt_unprepare_envelope (msg->env);
1546 mutt_update_num_postponed ();
1547 mutt_message _("Message postponed.");
1554 if (!(flags & SENDNEWS))
1556 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1557 if (!(flags & SENDBATCH)) {
1558 mutt_error _("No recipients are specified!");
1563 puts _("No recipients were specified.");
1569 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1570 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1572 if (!(flags & SENDBATCH))
1578 if (!msg->env->subject && !(flags & SENDBATCH) &&
1580 query_quadoption (OPT_SUBJECT,
1581 _("No subject, abort sending?"))) != M_NO) {
1582 /* if the abort is automatic, print an error message */
1583 if (quadoption (OPT_SUBJECT) == M_YES)
1584 mutt_error _("No subject specified.");
1589 if ((flags & SENDNEWS) && !msg->env->subject) {
1590 mutt_error _("No subject specified.");
1595 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1596 mutt_error _("No newsgroup specified.");
1602 if (msg->content->next)
1603 msg->content = mutt_make_multipart (msg->content);
1605 if (mutt_attach_check (msg) &&
1606 !msg->content->next &&
1607 query_quadoption (OPT_ATTACH,
1608 _("No attachments made but indicator found in text. "
1609 "Cancel sending?")) == M_YES) {
1610 if (quadoption (OPT_ATTACH) == M_YES) {
1611 mutt_message _("No attachments made but indicator found in text. "
1615 mutt_message (_("Mail not sent."));
1620 * Ok, we need to do it this way instead of handling all fcc stuff in
1621 * one place in order to avoid going to main_loop with encoded "env"
1622 * in case of error. Ugh.
1625 encode_descriptions (msg->content, 1);
1628 * Make sure that clear_content and free_clear_content are
1629 * properly initialized -- we may visit this particular place in
1630 * the code multiple times, including after a failed call to
1634 clear_content = NULL;
1635 free_clear_content = 0;
1638 if (msg->security) {
1639 /* save the decrypted attachments */
1640 clear_content = msg->content;
1642 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1643 mutt_protect (msg, pgpkeylist) == -1) {
1644 msg->content = mutt_remove_multipart (msg->content);
1646 p_delete(&pgpkeylist);
1648 decode_descriptions (msg->content);
1651 encode_descriptions (msg->content, 0);
1655 * at this point, msg->content is one of the following three things:
1656 * - multipart/signed. In this case, clear_content is a child.
1657 * - multipart/encrypted. In this case, clear_content exists
1659 * - application/pgp. In this case, clear_content exists independently.
1660 * - something else. In this case, it's the same as clear_content.
1663 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1665 if (clear_content && (msg->content != clear_content)
1666 && (msg->content->parts != clear_content))
1667 free_clear_content = 1;
1670 if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1671 mutt_message _("Sending message...");
1673 mutt_prepare_envelope (msg->env, 1);
1675 /* save a copy of the message, if necessary. */
1677 mutt_expand_path (fcc, sizeof (fcc));
1680 /* Don't save a copy when we are in batch-mode, and the FCC
1681 * folder is on an IMAP server: This would involve possibly lots
1682 * of user interaction, which is not available in batch mode.
1684 * Note: A patch to fix the problems with the use of IMAP servers
1685 * from non-curses mode is available from Brendan Cully. However,
1686 * I'd like to think a bit more about this before including it.
1690 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1694 if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1695 BODY *tmpbody = msg->content;
1696 BODY *save_sig = NULL;
1697 BODY *save_parts = NULL;
1699 if (WithCrypto && msg->security && option (OPTFCCCLEAR))
1700 msg->content = clear_content;
1702 /* check to see if the user wants copies of all attachments */
1703 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1705 && (m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1706 m_strcmp(msg->content->subtype, "signed") == 0)) {
1707 if (clear_content->type == TYPEMULTIPART) {
1708 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1709 /* save initial signature and attachments */
1710 save_sig = msg->content->parts->next;
1711 save_parts = clear_content->parts->next;
1714 /* this means writing only the main part */
1715 msg->content = clear_content->parts;
1717 if (mutt_protect (msg, pgpkeylist) == -1) {
1718 /* we can't do much about it at this point, so
1719 * fallback to saving the whole thing to fcc
1721 msg->content = tmpbody;
1726 save_content = msg->content;
1730 msg->content = msg->content->parts;
1735 /* update received time so that when storing to a mbox-style folder
1736 * the From_ line contains the current time instead of when the
1737 * message was first postponed.
1739 msg->received = time (NULL);
1740 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1742 * Error writing FCC, we should abort sending.
1748 msg->content = tmpbody;
1750 if (WithCrypto && save_sig) {
1751 /* cleanup the second signature structures */
1752 if (save_content->parts) {
1753 mutt_free_body (&save_content->parts->next);
1754 save_content->parts = NULL;
1756 mutt_free_body (&save_content);
1758 /* restore old signature and attachments */
1759 msg->content->parts->next = save_sig;
1760 msg->content->parts->parts->next = save_parts;
1762 else if (WithCrypto && save_content) {
1763 /* destroy the new encrypted body. */
1764 mutt_free_body (&save_content);
1771 * Don't attempt to send the message if the FCC failed. Just pretend
1772 * the send failed as well so we give the user a chance to fix the
1775 if (fcc_error || (i = send_message (msg)) == -1) {
1776 if (!(flags & SENDBATCH)) {
1778 else if ((msg->security & ENCRYPT) || ((msg->security & SIGN)
1779 && msg->content->type ==
1781 mutt_free_body (&msg->content); /* destroy PGP data */
1782 msg->content = clear_content; /* restore clear text. */
1784 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1785 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1786 msg->content = mutt_remove_multipart (msg->content);
1789 msg->content = mutt_remove_multipart (msg->content);
1790 decode_descriptions (msg->content);
1791 mutt_unprepare_envelope (msg->env);
1795 puts _("Could not send the message.");
1800 else if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
1801 mutt_message (i != 0 ? _("Sending in background.") :
1803 (flags & SENDNEWS) ? _("Article posted.") :
1809 if (WithCrypto && (msg->security & ENCRYPT))
1810 p_delete(&pgpkeylist);
1812 if (WithCrypto && free_clear_content)
1813 mutt_free_body (&clear_content);
1815 if (flags & SENDREPLY) {
1817 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1818 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1819 for (i = 0; i < ctx->vcount; i++)
1820 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1821 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1830 if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED)) {
1832 p_delete(&PgpSignAs);
1837 safe_fclose (&tempfp);
1838 mutt_free_header (&msg);
1843 /* vim: set sw=2: */