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.
10 #include <lib-lib/lib-lib.h>
13 #include <lib-mime/mime.h>
14 #include <lib-mime/rfc3676.h>
15 #include <lib-sys/unix.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-mx/mx.h>
23 #include <lib-crypt/crypt.h>
24 #include "mutt_idna.h"
28 #include <nntp/nntp.h>
36 static void append_signature (FILE * f)
42 fprintf (f, "\n%s", SignOffString);
45 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
46 if (option (OPTSIGDASHES))
48 else if (SignOffString)
50 mutt_copy_stream (tmpfp, f);
53 mutt_wait_filter (thepid);
57 /* compare two e-mail addresses and return 1 if they are equivalent */
58 static int mutt_addrcmp (address_t * a, address_t * b)
60 if (!a->mailbox || !b->mailbox)
62 if (ascii_strcasecmp (a->mailbox, b->mailbox))
67 /* search an e-mail address in a list */
68 static int mutt_addrsrc (address_t * a, address_t * lst)
70 for (; lst; lst = lst->next) {
71 if (mutt_addrcmp (a, lst))
77 /* removes addresses from "b" which are contained in "a" */
78 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
80 address_t *top, *p, *prev = NULL;
84 for (p = a; p; p = p->next) {
85 if (mutt_addrcmp (p, b))
92 address_list_wipe(&b);
98 address_list_wipe(&b);
110 /* remove any address which matches the current user. if `leave_only' is
111 * nonzero, don't remove the user's address if it is the only one in the list
113 static address_t *remove_user (address_t * a, int leave_only)
115 address_t *top = NULL, *last = NULL;
118 if (!mutt_addr_is_user (a)) {
132 if (!leave_only || a || last) {
134 address_list_wipe(&tmp);
143 static address_t *find_mailing_lists (address_t * t, address_t * c)
145 address_t *top = NULL, *ptr = NULL;
147 for (; t || c; t = c, c = NULL) {
148 for (; t; t = t->next) {
149 if (mutt_is_mail_list (t) && !t->group) {
151 ptr->next = address_dup (t);
155 ptr = top = address_dup (t);
162 static int edit_address (address_t ** a, const char *field)
164 char buf[HUGE_STRING];
170 mutt_addrlist_to_local (*a);
171 rfc822_write_address (buf, sizeof (buf), *a, 0);
172 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
174 address_list_wipe(a);
175 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
176 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
177 mutt_error (_("Error: '%s' is a bad IDN."), err);
183 while (idna_ok != 0);
187 static int edit_envelope (ENVELOPE * en, int flags)
189 char buf[HUGE_STRING];
190 string_list_t *uh = UserHeader;
191 regmatch_t pat_match[1];
194 if (option (OPTNEWSSEND)) {
196 m_strcpy(buf, sizeof(buf), en->newsgroups);
199 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
201 p_delete(&en->newsgroups);
202 en->newsgroups = m_strdup(buf);
205 m_strcpy(buf, sizeof(buf), en->followup_to);
208 if (option (OPTASKFOLLOWUP)
209 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
211 p_delete(&en->followup_to);
212 en->followup_to = m_strdup(buf);
214 if (en->x_comment_to)
215 m_strcpy(buf, sizeof(buf), en->x_comment_to);
218 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
219 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
221 p_delete(&en->x_comment_to);
222 en->x_comment_to = m_strdup(buf);
227 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
229 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
231 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
236 if (option (OPTFASTREPLY))
239 m_strcpy(buf, sizeof(buf), en->subject);
245 for (; uh; uh = uh->next) {
246 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
247 p = vskipspaces(uh->data + 8);
248 m_strcpy(buf, sizeof(buf), p);
253 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
254 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
255 unsigned int pos = pat_match->rm_so;
257 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
258 buf[pos] = '\0'; /* kill match */
259 while (pos-- && buf[pos] == ' ')
260 buf[pos] = '\0'; /* remove trailing spaces */
263 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
267 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
272 ("No subject, abort?"))
274 mutt_message _("No subject, aborting.");
278 m_strreplace(&en->subject, buf);
284 static char *nntp_get_header(const char *s)
286 return m_strdup(skipspaces(s));
290 static void process_user_recips (ENVELOPE * env)
292 string_list_t *uh = UserHeader;
294 for (; uh; uh = uh->next) {
295 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
296 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
297 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
298 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
299 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
300 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
302 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
303 env->newsgroups = nntp_get_header (uh->data + 11);
304 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
305 env->followup_to = nntp_get_header (uh->data + 12);
306 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
307 env->x_comment_to = nntp_get_header (uh->data + 13);
312 static void process_user_header (ENVELOPE * env)
314 string_list_t *uh = UserHeader;
315 string_list_t *last = env->userhdrs;
321 for (; uh; uh = uh->next) {
322 if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
323 /* User has specified a default From: address. Remove default address */
324 address_list_wipe(&env->from);
325 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
327 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
328 address_list_wipe(&env->reply_to);
329 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
331 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
332 m_strreplace(&env->message_id, uh->data + 11);
333 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
334 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
335 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
337 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
338 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
339 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
341 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
342 ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
344 last->next = string_item_new();
348 last = env->userhdrs = string_item_new();
349 last->data = m_strdup(uh->data);
354 void mutt_forward_intro (FILE * fp, HEADER * cur)
358 fputs ("----- Forwarded message from ", fp);
360 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
362 fputs (" -----\n\n", fp);
365 void mutt_forward_trailer (FILE * fp)
367 fputs ("\n----- End forwarded message -----\n", fp);
371 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
373 int chflags = CH_DECODE, cmflags = 0;
375 mutt_parse_mime_message (ctx, cur);
376 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
378 if ((cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
379 /* make sure we have the user's passphrase before proceeding... */
380 crypt_valid_passphrase (cur->security);
383 mutt_forward_intro (out, cur);
385 if (option (OPTFORWDECODE)) {
386 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
387 if (option (OPTWEED)) {
388 chflags |= CH_WEED | CH_REORDER;
389 cmflags |= M_CM_WEED;
392 if (option (OPTFORWQUOTE))
393 cmflags |= M_CM_PREFIX;
395 mutt_copy_message (out, ctx, cur, cmflags, chflags);
396 mutt_forward_trailer (out);
400 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
405 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
411 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
415 if (PostIndentString) {
416 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
422 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
424 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
425 int chflags = CH_DECODE;
427 if ((cur->security & ENCRYPT)) {
428 /* make sure we have the user's passphrase before proceeding... */
429 crypt_valid_passphrase (cur->security);
432 mutt_parse_mime_message (ctx, cur);
433 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
435 mutt_make_attribution (ctx, cur, out);
437 if (!option (OPTHEADER))
438 cmflags |= M_CM_NOHEADER;
439 if (option (OPTWEED)) {
440 chflags |= CH_WEED | CH_REORDER;
441 cmflags |= M_CM_WEED;
444 mutt_copy_message (out, ctx, cur, cmflags, chflags);
446 mutt_make_post_indent (ctx, cur, out);
451 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
455 if (flags && env->mail_followup_to && hmfupto == M_YES) {
456 address_list_append(to, address_list_dup(env->mail_followup_to));
460 /* Exit now if we're setting up the default Cc list for list-reply
461 * (only set if Mail-Followup-To is present and honoured).
463 if (flags & SENDLISTREPLY)
466 /* If this message came from a mailing list, ask the user if he really
467 * intended to reply to the author only.
469 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
470 switch (query_quadoption (OPT_LISTREPLY,
471 _("Message came from a mailing list. List-reply to mailing list?")))
474 address_list_append(to, find_mailing_lists (env->to, env->cc));
477 return -1; /* abort */
481 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
482 /* mail is from the user, assume replying to recipients */
483 address_list_append(to, address_list_dup(env->to));
485 else if (env->reply_to) {
486 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
487 (option (OPTIGNORELISTREPLYTO) &&
488 mutt_is_mail_list (env->reply_to) &&
489 (mutt_addrsrc (env->reply_to, env->to) ||
490 mutt_addrsrc (env->reply_to, env->cc)))) {
491 /* If the Reply-To: address is a mailing list, assume that it was
492 * put there by the mailing list, and use the From: address
494 * We also take the from header if our correspondant has a reply-to
495 * header which is identical to the electronic mail address given
496 * in his From header.
499 address_list_append(to, address_list_dup(env->from));
501 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
502 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
503 /* There are quite a few mailing lists which set the Reply-To:
504 * header field to the list address, which makes it quite impossible
505 * to send a message to only the sender of the message. This
506 * provides a way to do that.
508 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
509 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
510 switch (query_quadoption (OPT_REPLYTO, prompt)) {
512 address_list_append(to, address_list_dup(env->reply_to));
516 address_list_append(to, address_list_dup(env->from));
520 return (-1); /* abort */
524 address_list_append(to, address_list_dup(env->reply_to));
527 address_list_append(to, address_list_dup(env->from));
532 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
537 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
538 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
539 in->mail_followup_to->mailbox,
540 in->mail_followup_to->next ? ",..." : "");
542 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
546 if (flags & SENDLISTREPLY) {
547 address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
549 if (in->mail_followup_to && hmfupto == M_YES &&
550 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
551 return (-1); /* abort */
554 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
555 return (-1); /* abort */
557 if ((flags & SENDGROUPREPLY)
558 && (!in->mail_followup_to || hmfupto != M_YES))
560 address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
561 address_list_append(tmp, address_list_dup(in->cc));
567 void mutt_fix_reply_recipients (ENVELOPE * env)
569 mutt_expand_aliases_env (env);
571 if (!option (OPTMETOO)) {
572 /* the order is important here. do the CC: first so that if the
573 * the user is the only recipient, it ends up on the TO: field
575 env->cc = remove_user (env->cc, (env->to == NULL));
576 env->to = remove_user (env->to, (env->cc == NULL));
579 /* the CC field can get cluttered, especially with lists */
580 address_list_uniq(env->to);
581 address_list_uniq(env->cc);
582 env->cc = mutt_remove_xrefs (env->to, env->cc);
584 if (env->cc && !env->to) {
590 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
594 /* set the default subject for the message. */
595 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
596 m_strreplace(&env->subject, buffer);
599 void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
600 HEADER * cur, ENVELOPE * curenv)
602 /* This takes precedence over a subject that might have
603 * been taken from a List-Post header. Is that correct?
605 if (curenv->real_subj) {
606 p_delete(&env->subject);
607 env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
608 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
610 else if (!env->subject)
611 env->subject = m_strdup("Re: your mail");
614 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
615 env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
619 static string_list_t *mutt_make_references (ENVELOPE * e)
621 string_list_t *t = NULL, *l = NULL;
624 l = string_list_dup(e->references);
626 l = string_list_dup(e->in_reply_to);
629 t = string_item_new();
630 t->data = m_strdup(e->message_id);
638 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
639 string_list_t *** pp, string_list_t *** qq)
641 string_list_t **p = NULL, **q = NULL;
649 p = &env->references;
651 q = &env->in_reply_to;
658 *p = mutt_make_references (curenv);
660 if (curenv->message_id) {
661 *q = string_item_new();
662 (*q)->data = m_strdup(curenv->message_id);
673 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
675 env->references = NULL;
676 env->in_reply_to = NULL;
680 string_list_t **p = NULL, **q = NULL;
683 for (i = 0; i < ctx->vcount; i++) {
684 h = ctx->hdrs[ctx->v2r[i]];
686 mutt_add_to_reference_headers (env, h->env, &p, &q);
690 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
694 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
696 ENVELOPE *curenv = NULL;
701 for (i = 0; i < ctx->vcount; i++)
702 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
703 cur = ctx->hdrs[ctx->v2r[i]];
709 /* This could happen if the user tagged some messages and then did
710 * a limit such that none of the tagged message are visible.
712 mutt_error _("No tagged messages are visible!");
720 if (flags & SENDREPLY) {
722 if ((flags & SENDNEWS)) {
723 /* in case followup set Newsgroups: with Followup-To: if it present */
724 if (!env->newsgroups && curenv &&
725 m_strcasecmp(curenv->followup_to, "poster"))
726 env->newsgroups = m_strdup(curenv->followup_to);
733 for (i = 0; i < ctx->vcount; i++) {
734 h = ctx->hdrs[ctx->v2r[i]];
735 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
739 else if (mutt_fetch_recips (env, curenv, flags) == -1)
742 if ((flags & SENDLISTREPLY) && !env->to) {
743 mutt_error _("No mailing lists found!");
748 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
749 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
751 else if (flags & SENDFORWARD)
752 mutt_make_forward_subject (env, ctx, cur);
757 static int generate_body (FILE * tempfp, /* stream for outgoing message */
758 HEADER * msg, /* header for outgoing message */
759 int flags, /* compose mode */
760 CONTEXT * ctx, /* current mailbox */
762 { /* current message */
767 if (flags & SENDREPLY) {
769 query_quadoption (OPT_INCLUDE,
770 _("Include message in reply?"))) == -1)
774 mutt_message _("Including quoted message...");
777 for (i = 0; i < ctx->vcount; i++) {
778 h = ctx->hdrs[ctx->v2r[i]];
780 if (include_reply (ctx, h, tempfp) == -1) {
781 mutt_error _("Could not include all requested messages!");
785 fputc ('\n', tempfp);
790 include_reply (ctx, cur, tempfp);
794 else if (flags & SENDFORWARD) {
796 query_quadoption (OPT_MIMEFWD,
797 _("Forward as attachment?"))) == M_YES) {
798 BODY *last = msg->content;
800 mutt_message _("Preparing forwarded message...");
802 while (last && last->next)
806 tmp = mutt_make_message_attach (ctx, cur, 0);
813 for (i = 0; i < ctx->vcount; i++) {
814 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
815 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
821 last = msg->content = tmp;
828 include_forward (ctx, cur, tempfp);
830 for (i = 0; i < ctx->vcount; i++)
831 if (ctx->hdrs[ctx->v2r[i]]->tagged)
832 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
837 else if (flags & SENDKEY) {
840 if ((tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
843 tmp->next = msg->content;
852 void mutt_set_followup_to (ENVELOPE * e)
857 * Only generate the Mail-Followup-To if the user has requested it, and
858 * it hasn't already been set
861 if (!option (OPTFOLLOWUPTO))
864 if (option (OPTNEWSSEND)) {
865 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
866 e->followup_to = m_strdup(e->newsgroups);
871 if (!e->mail_followup_to) {
872 if (mutt_is_list_cc (0, e->to, e->cc)) {
875 * this message goes to known mailing lists, so create a proper
876 * mail-followup-to header
879 tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
880 address_list_append(tmp, address_list_dup(e->cc));
883 /* remove ourselves from the mail-followup-to header */
884 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
887 * If we are not subscribed to any of the lists in question,
888 * re-add ourselves to the mail-followup-to header. The
889 * mail-followup-to header generated is a no-op with group-reply,
890 * but makes sure list-reply has the desired effect.
893 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
895 from = address_list_dup (e->reply_to);
897 from = address_list_dup (e->from);
899 from = mutt_default_from ();
902 address_list_append(&from->next, e->mail_followup_to);
903 e->mail_followup_to = from;
907 address_list_uniq(e->mail_followup_to);
913 /* look through the recipients of the message we are replying to, and if
914 we find an address that matches $alternates, we use that as the default
916 static address_t *set_reverse_name (ENVELOPE * env)
920 for (tmp = env->to; tmp; tmp = tmp->next) {
921 if (mutt_addr_is_user (tmp))
925 for (tmp = env->cc; tmp; tmp = tmp->next) {
926 if (mutt_addr_is_user (tmp))
930 if (!tmp && mutt_addr_is_user (env->from))
933 tmp = address_dup (tmp);
934 if (!option (OPTREVREAL))
935 p_delete(&tmp->personal);
937 tmp->personal = m_strdup(Realname);
942 address_t *mutt_default_from (void)
945 const char *fqdn = mutt_fqdn (1);
948 * Note: We let $from override $realname here. Is this the right
953 adr = address_dup (From);
954 else if (option (OPTUSEDOMAIN)) {
955 adr = address_new ();
956 adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
957 sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn)); /* __SPRINTF_CHECKED__ */
960 adr = address_new ();
961 adr->mailbox = m_strdup(NONULL (Username));
967 static int send_message (HEADER * msg)
969 char tempfile[_POSIX_PATH_MAX];
973 /* Write out the message in MIME form. */
974 mutt_mktemp (tempfile);
975 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
979 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
983 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
986 fputc ('\n', tempfp); /* tie off the header. */
988 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
994 if (fclose (tempfp) != 0) {
995 mutt_perror (tempfile);
1002 return mix_send_message (msg->chain, tempfile);
1005 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1006 msg->env->bcc, tempfile,
1007 (msg->content->encoding == ENC8BIT));
1011 /* rfc2047 encode the content-descriptions */
1012 static void encode_descriptions (BODY * b, short recurse)
1016 for (t = b; t; t = t->next) {
1017 if (t->description) {
1018 rfc2047_encode_string (&t->description);
1020 if (recurse && t->parts)
1021 encode_descriptions (t->parts, recurse);
1025 /* rfc2047 decode them in case of an error */
1026 static void decode_descriptions (BODY * b)
1030 for (t = b; t; t = t->next) {
1031 if (t->description) {
1032 rfc2047_decode (&t->description);
1035 decode_descriptions (t->parts);
1039 static void fix_end_of_file (const char *data)
1044 if ((fp = safe_fopen (data, "a+")) == NULL)
1046 fseeko (fp, -1, SEEK_END);
1047 if ((c = fgetc (fp)) != '\n')
1052 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1054 HEADER *msg = header_new();
1056 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1059 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1062 int ci_send_message (int flags, /* send mode */
1063 HEADER * msg, /* template to use for new message */
1064 char *tempfile, /* file specified by -i or -H */
1065 CONTEXT * ctx, /* current mailbox */
1067 { /* current message */
1068 char buffer[LONG_STRING];
1069 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1070 FILE *tempfp = NULL;
1072 int i, killfrom = 0;
1074 int free_clear_content = 0;
1076 BODY *save_content = NULL;
1077 BODY *clear_content = NULL;
1078 char *pgpkeylist = NULL;
1080 /* save current value of "pgp_sign_as" */
1081 char *signas = NULL, *err = NULL;
1082 const char *tag = NULL;
1088 if (flags & SENDNEWS)
1089 set_option (OPTNEWSSEND);
1091 unset_option (OPTNEWSSEND);
1094 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1095 mutt_num_postponed (1)) {
1096 /* If the user is composing a new message, check to see if there
1097 * are any postponed messages first.
1100 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1104 flags |= SENDPOSTPONED;
1108 if (flags & SENDPOSTPONED)
1109 signas = m_strdup(PgpSignAs);
1111 /* Delay expansion of aliases until absolutely necessary--shouldn't
1112 * be necessary unless we are prompting the user or about to execute a
1119 if (flags == SENDPOSTPONED) {
1121 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1125 * If postponed message is a news article, it have
1126 * a "Newsgroups:" header line, then set appropriate flag.
1128 if (msg->env->newsgroups) {
1130 set_option (OPTNEWSSEND);
1134 unset_option (OPTNEWSSEND);
1139 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1140 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1141 mutt_perror (msg->content->filename);
1147 msg->env = envelope_new();
1150 /* Parse and use an eventual list-post header */
1151 if ((flags & SENDLISTREPLY)
1152 && cur && cur->env && cur->env->list_post) {
1153 /* Use any list-post header as a template */
1154 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1155 /* We don't let them set the sender's address. */
1156 address_list_wipe(&msg->env->from);
1159 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1161 pbody->next = msg->content; /* don't kill command-line attachments */
1162 msg->content = pbody;
1164 if (!(ctype = m_strdup(ContentType)))
1165 ctype = m_strdup("text/plain");
1166 mutt_parse_content_type (ctype, msg->content);
1169 msg->content->unlink = 1;
1170 msg->content->use_disp = 0;
1171 msg->content->disposition = DISPINLINE;
1172 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1173 && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1174 parameter_setval(&msg->content->parameter, "format", "flowed");
1175 if (option (OPTDELSP))
1176 parameter_setval(&msg->content->parameter, "delsp", "yes");
1180 mutt_mktemp (buffer);
1181 tempfp = safe_fopen (buffer, "w+");
1182 msg->content->filename = m_strdup(buffer);
1185 tempfp = safe_fopen (tempfile, "a+");
1186 msg->content->filename = m_strdup(tempfile);
1190 mutt_perror (msg->content->filename);
1195 /* this is handled here so that the user can match ~f in send-hook */
1196 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1197 /* we shouldn't have to worry about freeing `msg->env->from' before
1198 * setting it here since this code will only execute when doing some
1199 * sort of reply. the pointer will only be set when using the -H command
1202 * We shouldn't have to worry about alias expansion here since we are
1203 * either replying to a real or postponed message, therefore no aliases
1204 * should exist since the user has not had the opportunity to add
1205 * addresses to the list. We just have to ensure the postponed messages
1206 * have their aliases expanded.
1209 msg->env->from = set_reverse_name (cur->env);
1212 if (!msg->env->from && option (OPTUSEFROM)
1213 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1214 msg->env->from = mutt_default_from ();
1216 if (flags & SENDBATCH) {
1217 mutt_copy_stream (stdin, tempfp);
1218 if (option (OPTHDRS)) {
1219 process_user_recips (msg->env);
1220 process_user_header (msg->env);
1222 mutt_expand_aliases_env (msg->env);
1224 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1225 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1226 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1229 if (option (OPTHDRS))
1230 process_user_recips (msg->env);
1232 /* Expand aliases and remove duplicates/crossrefs */
1233 mutt_fix_reply_recipients (msg->env);
1236 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1237 && !msg->env->newsgroups)
1238 msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
1241 if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1242 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1243 if (edit_envelope (msg->env, flags) == -1)
1247 /* the from address must be set here regardless of whether or not
1248 * $use_from is set so that the `~P' (from you) operator in send-hook
1249 * patterns will work. if $use_from is unset, the from address is killed
1250 * after send-hooks are evaulated */
1252 if (!msg->env->from) {
1253 msg->env->from = mutt_default_from ();
1257 if ((flags & SENDREPLY) && cur) {
1258 /* change setting based upon message we are replying to */
1259 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1262 * set the replied flag for the message we are generating so that the
1263 * user can use ~Q in a send-hook to know when reply-hook's are also
1269 /* change settings based upon recipients */
1271 mutt_message_hook (NULL, msg, M_SENDHOOK);
1274 * Unset the replied flag from the message we are composing since it is
1275 * no longer required. This is done here because the FCC'd copy of
1276 * this message was erroneously get the 'R'eplied flag when stored in
1277 * a maildir-style mailbox.
1282 address_list_wipe(&msg->env->from);
1286 if (option (OPTHDRS))
1287 process_user_header (msg->env);
1290 if (option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1291 append_signature (tempfp);
1293 /* include replies/forwarded messages, unless we are given a template */
1294 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1295 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1298 if (!option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1299 append_signature (tempfp);
1302 * this wants to be done _after_ generate_body, so message-hooks
1306 if (option (OPTCRYPTAUTOSIGN))
1307 msg->security |= SIGN;
1308 if (option (OPTCRYPTAUTOENCRYPT))
1309 msg->security |= ENCRYPT;
1310 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1311 msg->security |= ENCRYPT;
1312 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1313 msg->security |= SIGN;
1314 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1315 && (cur->security & ENCRYPT))
1316 msg->security |= SIGN;
1317 if (msg->security & (ENCRYPT | SIGN)) {
1318 if (option (OPTPGPAUTOINLINE))
1319 msg->security |= INLINE;
1320 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1321 msg->security |= INLINE;
1324 if (msg->security) {
1326 * When reypling / forwarding, use the original message's
1327 * crypto system. According to the documentation,
1328 * smime_is_default should be disregarded here.
1330 * Problem: At least with forwarding, this doesn't really
1331 * make much sense. Should we have an option to completely
1332 * disable individual mechanisms at run-time?
1335 if (option (OPTCRYPTAUTOPGP) && (cur->security & APPLICATION_PGP))
1336 msg->security |= APPLICATION_PGP;
1337 else if (option (OPTCRYPTAUTOSMIME)
1338 && (cur->security & APPLICATION_SMIME))
1339 msg->security |= APPLICATION_SMIME;
1343 * No crypto mechanism selected? Use availability + smime_is_default
1346 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1347 if (option (OPTCRYPTAUTOSMIME) && option (OPTSMIMEISDEFAULT))
1348 msg->security |= APPLICATION_SMIME;
1349 else if (option (OPTCRYPTAUTOPGP))
1350 msg->security |= APPLICATION_PGP;
1351 else if (option (OPTCRYPTAUTOSMIME))
1352 msg->security |= APPLICATION_SMIME;
1356 /* No permissible mechanisms found. Don't sign or encrypt. */
1357 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1362 * This hook is even called for postponed messages, and can, e.g., be
1363 * used for setting the editor, the sendmail path, or the
1366 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1368 /* wait until now to set the real name portion of our return address so
1369 that $realname can be set in a send-hook */
1370 if (msg->env->from && !msg->env->from->personal
1371 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1372 msg->env->from->personal = m_strdup(Realname);
1374 if (!(flags & SENDKEY))
1375 safe_fclose (&tempfp);
1377 if (!(flags & SENDBATCH)) {
1379 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1381 mutt_update_encoding (msg->content);
1384 * Select whether or not the user's editor should be called now. We
1385 * don't want to do this when:
1386 * 1) we are sending a key/cert
1387 * 2) we are forwarding a message and the user doesn't want to edit it.
1388 * This is controled by the quadoption $forward_edit. However, if
1389 * both $edit_headers and $autoedit are set, we want to ignore the
1390 * setting of $forward_edit because the user probably needs to add the
1393 if (!(flags & SENDKEY) &&
1394 ((flags & SENDFORWARD) == 0 ||
1395 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1396 query_quadoption (OPT_FORWEDIT,
1397 _("Edit forwarded message?")) == M_YES)) {
1398 /* If the this isn't a text message, look for a mailcap edit command */
1399 if (rfc1524_mailcap_isneeded(msg->content)) {
1400 if (!mutt_edit_attachment (msg->content))
1402 } else if (option (OPTEDITHDRS)) {
1403 mutt_env_to_local (msg->env);
1404 mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1406 mutt_env_to_idna (msg->env, NULL, NULL);
1409 mutt_edit_file (Editor, msg->content->filename);
1411 if (stat (msg->content->filename, &st) == 0) {
1412 if (mtime != st.st_mtime)
1413 fix_end_of_file (msg->content->filename);
1415 mutt_perror (msg->content->filename);
1418 if (option (OPTTEXTFLOWED))
1419 rfc3676_space_stuff (msg);
1421 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1424 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1425 if (stat (msg->content->filename, &st) == 0) {
1426 /* if the file was not modified, bail out now */
1427 if (mtime == st.st_mtime && !msg->content->next &&
1428 query_quadoption (OPT_ABORT,
1429 _("Abort unmodified message?")) == M_YES) {
1430 mutt_message _("Aborted unmodified message.");
1436 mutt_perror (msg->content->filename);
1440 /* specify a default fcc. if we are in batchmode, only save a copy of
1441 * the message if the value of $copy is yes or ask-yes */
1443 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1444 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1445 /* set the default FCC */
1446 if (!msg->env->from) {
1447 msg->env->from = mutt_default_from ();
1448 killfrom = 1; /* no need to check $use_from because if the user specified
1449 a from address it would have already been set by now */
1451 mutt_select_fcc (fcc, sizeof (fcc), msg);
1453 address_list_wipe(&msg->env->from);
1459 mutt_update_encoding (msg->content);
1461 if (!(flags & SENDBATCH)) {
1464 fcc_error = 0; /* reset value since we may have failed before */
1465 mutt_pretty_mailbox (fcc);
1466 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1470 if (flags & SENDNEWS)
1471 mutt_message (_("Article not posted."));
1475 mutt_message _("Mail not sent.");
1479 /* postpone the message until later. */
1480 if (msg->content->next)
1481 msg->content = mutt_make_multipart (msg->content);
1484 * make sure the message is written to the right part of a maildir
1490 encode_descriptions (msg->content, 1);
1491 mutt_prepare_envelope (msg->env, 0);
1492 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1495 || mutt_write_fcc (NONULL (Postponed), msg,
1497 && (flags & SENDREPLY)) ? cur->env->
1498 message_id : NULL, 1, fcc) < 0) {
1499 msg->content = mutt_remove_multipart (msg->content);
1500 decode_descriptions (msg->content);
1501 mutt_unprepare_envelope (msg->env);
1504 mutt_update_num_postponed ();
1505 mutt_message _("Message postponed.");
1512 if (!(flags & SENDNEWS))
1514 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1515 if (!(flags & SENDBATCH)) {
1516 mutt_error _("No recipients are specified!");
1521 puts _("No recipients were specified.");
1527 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1528 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1530 if (!(flags & SENDBATCH))
1536 if (!msg->env->subject && !(flags & SENDBATCH) &&
1538 query_quadoption (OPT_SUBJECT,
1539 _("No subject, abort sending?"))) != M_NO) {
1540 /* if the abort is automatic, print an error message */
1541 if (quadoption (OPT_SUBJECT) == M_YES)
1542 mutt_error _("No subject specified.");
1547 if ((flags & SENDNEWS) && !msg->env->subject) {
1548 mutt_error _("No subject specified.");
1553 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1554 mutt_error _("No newsgroup specified.");
1560 if (msg->content->next)
1561 msg->content = mutt_make_multipart (msg->content);
1563 if (mutt_attach_check (msg) &&
1564 !msg->content->next &&
1565 query_quadoption (OPT_ATTACH,
1566 _("No attachments made but indicator found in text. "
1567 "Cancel sending?")) == M_YES) {
1568 if (quadoption (OPT_ATTACH) == M_YES) {
1569 mutt_message _("No attachments made but indicator found in text. "
1573 mutt_message (_("Mail not sent."));
1578 * Ok, we need to do it this way instead of handling all fcc stuff in
1579 * one place in order to avoid going to main_loop with encoded "env"
1580 * in case of error. Ugh.
1583 encode_descriptions (msg->content, 1);
1586 * Make sure that clear_content and free_clear_content are
1587 * properly initialized -- we may visit this particular place in
1588 * the code multiple times, including after a failed call to
1592 clear_content = NULL;
1593 free_clear_content = 0;
1595 if (msg->security) {
1596 /* save the decrypted attachments */
1597 clear_content = msg->content;
1599 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1600 mutt_protect (msg, pgpkeylist) == -1) {
1601 msg->content = mutt_remove_multipart (msg->content);
1603 p_delete(&pgpkeylist);
1605 decode_descriptions (msg->content);
1608 encode_descriptions (msg->content, 0);
1612 * at this point, msg->content is one of the following three things:
1613 * - multipart/signed. In this case, clear_content is a child.
1614 * - multipart/encrypted. In this case, clear_content exists
1616 * - application/pgp. In this case, clear_content exists independently.
1617 * - something else. In this case, it's the same as clear_content.
1620 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1622 if (clear_content && (msg->content != clear_content)
1623 && (msg->content->parts != clear_content))
1624 free_clear_content = 1;
1626 if (!option (OPTNOCURSES))
1627 mutt_message _("Sending message...");
1629 mutt_prepare_envelope (msg->env, 1);
1631 /* save a copy of the message, if necessary. */
1633 mutt_expand_path (fcc, sizeof (fcc));
1636 /* Don't save a copy when we are in batch-mode, and the FCC
1637 * folder is on an IMAP server: This would involve possibly lots
1638 * of user interaction, which is not available in batch mode.
1640 * Note: A patch to fix the problems with the use of IMAP servers
1641 * from non-curses mode is available from Brendan Cully. However,
1642 * I'd like to think a bit more about this before including it.
1645 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1648 if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1649 BODY *tmpbody = msg->content;
1650 BODY *save_sig = NULL;
1651 BODY *save_parts = NULL;
1653 if (msg->security && option (OPTFCCCLEAR))
1654 msg->content = clear_content;
1656 /* check to see if the user wants copies of all attachments */
1657 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1658 if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1659 m_strcmp(msg->content->subtype, "signed") == 0))
1661 if (clear_content->type == TYPEMULTIPART) {
1662 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1663 /* save initial signature and attachments */
1664 save_sig = msg->content->parts->next;
1665 save_parts = clear_content->parts->next;
1668 /* this means writing only the main part */
1669 msg->content = clear_content->parts;
1671 if (mutt_protect (msg, pgpkeylist) == -1) {
1672 /* we can't do much about it at this point, so
1673 * fallback to saving the whole thing to fcc
1675 msg->content = tmpbody;
1680 save_content = msg->content;
1684 msg->content = msg->content->parts;
1689 /* update received time so that when storing to a mbox-style folder
1690 * the From_ line contains the current time instead of when the
1691 * message was first postponed.
1693 msg->received = time (NULL);
1694 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1696 * Error writing FCC, we should abort sending.
1702 msg->content = tmpbody;
1705 /* cleanup the second signature structures */
1706 if (save_content->parts) {
1707 body_list_wipe(&save_content->parts->next);
1708 save_content->parts = NULL;
1710 body_list_wipe(&save_content);
1712 /* restore old signature and attachments */
1713 msg->content->parts->next = save_sig;
1714 msg->content->parts->parts->next = save_parts;
1716 else if (save_content) {
1717 /* destroy the new encrypted body. */
1718 body_list_wipe(&save_content);
1725 * Don't attempt to send the message if the FCC failed. Just pretend
1726 * the send failed as well so we give the user a chance to fix the
1729 if (fcc_error || (i = send_message (msg)) == -1) {
1730 if (!(flags & SENDBATCH)) {
1731 if ((msg->security & ENCRYPT)
1732 || ((msg->security & SIGN)
1733 && msg->content->type == TYPEAPPLICATION)) {
1734 body_list_wipe(&msg->content); /* destroy PGP data */
1735 msg->content = clear_content; /* restore clear text. */
1737 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1738 body_list_wipe(&msg->content->parts->next); /* destroy sig */
1739 msg->content = mutt_remove_multipart (msg->content);
1742 msg->content = mutt_remove_multipart (msg->content);
1743 decode_descriptions (msg->content);
1744 mutt_unprepare_envelope (msg->env);
1748 puts _("Could not send the message.");
1753 else if (!option (OPTNOCURSES))
1754 mutt_message (i != 0 ? _("Sending in background.") :
1756 (flags & SENDNEWS) ? _("Article posted.") :
1762 if (msg->security & ENCRYPT)
1763 p_delete(&pgpkeylist);
1765 if (free_clear_content)
1766 body_list_wipe(&clear_content);
1768 if (flags & SENDREPLY) {
1770 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1771 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1772 for (i = 0; i < ctx->vcount; i++)
1773 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1774 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1783 if (flags & SENDPOSTPONED) {
1785 p_delete(&PgpSignAs);
1790 safe_fclose (&tempfp);
1791 header_delete(&msg);
1796 /* vim: set sw=2: */