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>
12 #include <lib-mime/mime.h>
13 #include <lib-mime/rfc3676.h>
14 #include <lib-sys/unix.h>
15 #include <lib-ui/curses.h>
16 #include <lib-ui/enter.h>
17 #include <lib-mx/mx.h>
22 #include <lib-crypt/crypt.h>
23 #include "mutt_idna.h"
27 #include <nntp/nntp.h>
32 static void append_signature (FILE * f)
38 fprintf (f, "\n%s", SignOffString);
41 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
42 if (option (OPTSIGDASHES))
44 else if (SignOffString)
46 mutt_copy_stream (tmpfp, f);
49 mutt_wait_filter (thepid);
53 /* compare two e-mail addresses and return 1 if they are equivalent */
54 static int mutt_addrcmp (address_t * a, address_t * b)
56 if (!a->mailbox || !b->mailbox)
58 if (ascii_strcasecmp (a->mailbox, b->mailbox))
63 /* search an e-mail address in a list */
64 static int mutt_addrsrc (address_t * a, address_t * lst)
66 for (; lst; lst = lst->next) {
67 if (mutt_addrcmp (a, lst))
73 /* removes addresses from "b" which are contained in "a" */
74 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
76 address_t *top, *p, *prev = NULL;
80 for (p = a; p; p = p->next) {
81 if (mutt_addrcmp (p, b))
88 address_list_wipe(&b);
94 address_list_wipe(&b);
106 /* remove any address which matches the current user. if `leave_only' is
107 * nonzero, don't remove the user's address if it is the only one in the list
109 static address_t *remove_user (address_t * a, int leave_only)
111 address_t *top = NULL, *last = NULL;
114 if (!mutt_addr_is_user (a)) {
128 if (!leave_only || a || last) {
130 address_list_wipe(&tmp);
139 static address_t *find_mailing_lists (address_t * t, address_t * c)
141 address_t *top = NULL, *ptr = NULL;
143 for (; t || c; t = c, c = NULL) {
144 for (; t; t = t->next) {
145 if (mutt_is_mail_list (t) && !t->group) {
147 ptr->next = address_dup (t);
151 ptr = top = address_dup (t);
158 static int edit_address (address_t ** a, const char *field)
160 char buf[HUGE_STRING];
166 mutt_addrlist_to_local (*a);
167 rfc822_addrcat(buf, sizeof(buf), *a, 0);
168 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
170 address_list_wipe(a);
171 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
172 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
173 mutt_error (_("Error: '%s' is a bad IDN."), err);
179 while (idna_ok != 0);
183 static int edit_envelope (ENVELOPE * en, int flags)
185 char buf[HUGE_STRING];
186 string_list_t *uh = UserHeader;
187 regmatch_t pat_match[1];
190 if (option (OPTNEWSSEND)) {
192 m_strcpy(buf, sizeof(buf), en->newsgroups);
195 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
197 p_delete(&en->newsgroups);
198 en->newsgroups = m_strdup(buf);
201 m_strcpy(buf, sizeof(buf), en->followup_to);
204 if (option (OPTASKFOLLOWUP)
205 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
207 p_delete(&en->followup_to);
208 en->followup_to = m_strdup(buf);
210 if (en->x_comment_to)
211 m_strcpy(buf, sizeof(buf), en->x_comment_to);
214 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
215 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
217 p_delete(&en->x_comment_to);
218 en->x_comment_to = m_strdup(buf);
223 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
225 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
227 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
232 if (option (OPTFASTREPLY))
235 m_strcpy(buf, sizeof(buf), en->subject);
241 for (; uh; uh = uh->next) {
242 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
243 p = vskipspaces(uh->data + 8);
244 m_strcpy(buf, sizeof(buf), p);
249 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
250 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
251 unsigned int pos = pat_match->rm_so;
253 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
254 buf[pos] = '\0'; /* kill match */
255 while (pos-- && buf[pos] == ' ')
256 buf[pos] = '\0'; /* remove trailing spaces */
259 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
263 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
268 ("No subject, abort?"))
270 mutt_message _("No subject, aborting.");
274 m_strreplace(&en->subject, buf);
280 static char *nntp_get_header(const char *s)
282 return m_strdup(skipspaces(s));
286 static void process_user_recips (ENVELOPE * env)
288 string_list_t *uh = UserHeader;
290 for (; uh; uh = uh->next) {
291 const char *p = strchr(uh->data, ':');
295 switch (mime_which_token(uh->data, p++ - uh->data)) {
297 env->to = rfc822_parse_adrlist(env->to, p);
300 env->cc = rfc822_parse_adrlist(env->cc, p);
303 env->bcc = rfc822_parse_adrlist(env->bcc, p);
306 case MIME_NEWSGROUPS:
307 env->newsgroups = nntp_get_header(p);
309 case MIME_FOLLOWUP_TO:
310 env->followup_to = nntp_get_header(p);
312 case MIME_X_COMMENT_TO:
313 env->x_comment_to = nntp_get_header(p);
321 static void process_user_header(ENVELOPE * env)
324 string_list_t **last = string_list_last(&env->userhdrs);
326 for (uh = UserHeader; uh; uh = uh->next) {
327 const char *p = strchr(uh->data, ':');
331 switch (mime_which_token(uh->data, p++ - uh->data)) {
333 /* User has specified a default From: address. Remove default address */
334 address_list_wipe(&env->from);
335 env->from = rfc822_parse_adrlist(env->from, p);
339 address_list_wipe(&env->reply_to);
340 env->reply_to = rfc822_parse_adrlist (env->reply_to, p);
343 case MIME_MESSAGE_ID:
344 m_strreplace(&env->message_id, p);
351 case MIME_NEWSGROUPS:
352 case MIME_FOLLOWUP_TO:
353 case MIME_X_COMMENT_TO:
355 case MIME_SUPERSEDES:
356 case MIME_SUPERCEDES:
361 *last = string_item_new();
362 (*last)->data = m_strdup(uh->data);
363 last = &(*last)->next;
369 void mutt_forward_intro (FILE * fp, HEADER * cur)
373 fputs ("----- Forwarded message from ", fp);
375 rfc822_addrcat(buffer, sizeof(buffer), cur->env->from, 1);
377 fputs (" -----\n\n", fp);
380 void mutt_forward_trailer (FILE * fp)
382 fputs ("\n----- End forwarded message -----\n", fp);
386 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
388 int chflags = CH_DECODE, cmflags = 0;
390 mutt_parse_mime_message (ctx, cur);
391 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
393 if ((cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
394 /* make sure we have the user's passphrase before proceeding... */
395 crypt_valid_passphrase (cur->security);
398 mutt_forward_intro (out, cur);
400 if (option (OPTFORWDECODE)) {
401 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
402 if (option (OPTWEED)) {
403 chflags |= CH_WEED | CH_REORDER;
404 cmflags |= M_CM_WEED;
407 if (option (OPTFORWQUOTE))
408 cmflags |= M_CM_PREFIX;
410 mutt_copy_message (out, ctx, cur, cmflags, chflags);
411 mutt_forward_trailer (out);
415 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
420 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
426 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
430 if (PostIndentString) {
431 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
437 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
439 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
440 int chflags = CH_DECODE;
442 if ((cur->security & ENCRYPT)) {
443 /* make sure we have the user's passphrase before proceeding... */
444 crypt_valid_passphrase (cur->security);
447 mutt_parse_mime_message (ctx, cur);
448 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
450 mutt_make_attribution (ctx, cur, out);
452 if (!option (OPTHEADER))
453 cmflags |= M_CM_NOHEADER;
454 if (option (OPTWEED)) {
455 chflags |= CH_WEED | CH_REORDER;
456 cmflags |= M_CM_WEED;
459 mutt_copy_message (out, ctx, cur, cmflags, chflags);
461 mutt_make_post_indent (ctx, cur, out);
466 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
470 if (flags && env->mail_followup_to && hmfupto == M_YES) {
471 address_list_append(to, address_list_dup(env->mail_followup_to));
475 /* Exit now if we're setting up the default Cc list for list-reply
476 * (only set if Mail-Followup-To is present and honoured).
478 if (flags & SENDLISTREPLY)
481 /* If this message came from a mailing list, ask the user if he really
482 * intended to reply to the author only.
484 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
485 switch (query_quadoption (OPT_LISTREPLY,
486 _("Message came from a mailing list. List-reply to mailing list?")))
489 address_list_append(to, find_mailing_lists (env->to, env->cc));
492 return -1; /* abort */
496 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
497 /* mail is from the user, assume replying to recipients */
498 address_list_append(to, address_list_dup(env->to));
500 else if (env->reply_to) {
501 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
502 (option (OPTIGNORELISTREPLYTO) &&
503 mutt_is_mail_list (env->reply_to) &&
504 (mutt_addrsrc (env->reply_to, env->to) ||
505 mutt_addrsrc (env->reply_to, env->cc)))) {
506 /* If the Reply-To: address is a mailing list, assume that it was
507 * put there by the mailing list, and use the From: address
509 * We also take the from header if our correspondant has a reply-to
510 * header which is identical to the electronic mail address given
511 * in his From header.
514 address_list_append(to, address_list_dup(env->from));
516 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
517 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
518 /* There are quite a few mailing lists which set the Reply-To:
519 * header field to the list address, which makes it quite impossible
520 * to send a message to only the sender of the message. This
521 * provides a way to do that.
523 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
524 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
525 switch (query_quadoption (OPT_REPLYTO, prompt)) {
527 address_list_append(to, address_list_dup(env->reply_to));
531 address_list_append(to, address_list_dup(env->from));
535 return (-1); /* abort */
539 address_list_append(to, address_list_dup(env->reply_to));
542 address_list_append(to, address_list_dup(env->from));
547 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
552 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
553 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
554 in->mail_followup_to->mailbox,
555 in->mail_followup_to->next ? ",..." : "");
557 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
561 if (flags & SENDLISTREPLY) {
562 address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
564 if (in->mail_followup_to && hmfupto == M_YES &&
565 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
566 return (-1); /* abort */
569 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
570 return (-1); /* abort */
572 if ((flags & SENDGROUPREPLY)
573 && (!in->mail_followup_to || hmfupto != M_YES))
575 address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
576 address_list_append(tmp, address_list_dup(in->cc));
582 void mutt_fix_reply_recipients (ENVELOPE * env)
584 mutt_expand_aliases_env (env);
586 if (!option (OPTMETOO)) {
587 /* the order is important here. do the CC: first so that if the
588 * the user is the only recipient, it ends up on the TO: field
590 env->cc = remove_user (env->cc, (env->to == NULL));
591 env->to = remove_user (env->to, (env->cc == NULL));
594 /* the CC field can get cluttered, especially with lists */
595 address_list_uniq(env->to);
596 address_list_uniq(env->cc);
597 env->cc = mutt_remove_xrefs (env->to, env->cc);
599 if (env->cc && !env->to) {
605 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
609 /* set the default subject for the message. */
610 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
611 m_strreplace(&env->subject, buffer);
614 void mutt_make_misc_reply_headers (ENVELOPE * env,
615 CONTEXT * ctx __attribute__ ((unused)),
616 HEADER * cur __attribute__ ((unused)),
619 /* This takes precedence over a subject that might have
620 * been taken from a List-Post header. Is that correct?
622 if (curenv->real_subj) {
623 p_delete(&env->subject);
624 env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
625 sprintf (env->subject, "Re: %s", curenv->real_subj);
627 else if (!env->subject)
628 env->subject = m_strdup("Re: your mail");
631 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
632 env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
636 static string_list_t *mutt_make_references (ENVELOPE * e)
638 string_list_t *t = NULL, *l = NULL;
641 l = string_list_dup(e->references);
643 l = string_list_dup(e->in_reply_to);
646 t = string_item_new();
647 t->data = m_strdup(e->message_id);
655 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
656 string_list_t *** pp, string_list_t *** qq)
658 string_list_t **p = NULL, **q = NULL;
666 p = &env->references;
668 q = &env->in_reply_to;
675 *p = mutt_make_references (curenv);
677 if (curenv->message_id) {
678 *q = string_item_new();
679 (*q)->data = m_strdup(curenv->message_id);
690 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
692 env->references = NULL;
693 env->in_reply_to = NULL;
697 string_list_t **p = NULL, **q = NULL;
700 for (i = 0; i < ctx->vcount; i++) {
701 h = ctx->hdrs[ctx->v2r[i]];
703 mutt_add_to_reference_headers (env, h->env, &p, &q);
707 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
711 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
713 ENVELOPE *curenv = NULL;
718 for (i = 0; i < ctx->vcount; i++)
719 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
720 cur = ctx->hdrs[ctx->v2r[i]];
726 /* This could happen if the user tagged some messages and then did
727 * a limit such that none of the tagged message are visible.
729 mutt_error _("No tagged messages are visible!");
737 if (flags & SENDREPLY) {
739 if ((flags & SENDNEWS)) {
740 /* in case followup set Newsgroups: with Followup-To: if it present */
741 if (!env->newsgroups && curenv &&
742 m_strcasecmp(curenv->followup_to, "poster"))
743 env->newsgroups = m_strdup(curenv->followup_to);
750 for (i = 0; i < ctx->vcount; i++) {
751 h = ctx->hdrs[ctx->v2r[i]];
752 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
756 else if (mutt_fetch_recips (env, curenv, flags) == -1)
759 if ((flags & SENDLISTREPLY) && !env->to) {
760 mutt_error _("No mailing lists found!");
765 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
766 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
768 else if (flags & SENDFORWARD)
769 mutt_make_forward_subject (env, ctx, cur);
774 static int generate_body (FILE * tempfp, /* stream for outgoing message */
775 HEADER * msg, /* header for outgoing message */
776 int flags, /* compose mode */
777 CONTEXT * ctx, /* current mailbox */
779 { /* current message */
784 if (flags & SENDREPLY) {
786 query_quadoption (OPT_INCLUDE,
787 _("Include message in reply?"))) == -1)
791 mutt_message _("Including quoted message...");
794 for (i = 0; i < ctx->vcount; i++) {
795 h = ctx->hdrs[ctx->v2r[i]];
797 if (include_reply (ctx, h, tempfp) == -1) {
798 mutt_error _("Could not include all requested messages!");
802 fputc ('\n', tempfp);
807 include_reply (ctx, cur, tempfp);
811 else if (flags & SENDFORWARD) {
813 query_quadoption (OPT_MIMEFWD,
814 _("Forward as attachment?"))) == M_YES) {
815 BODY *last = msg->content;
817 mutt_message _("Preparing forwarded message...");
819 while (last && last->next)
823 tmp = mutt_make_message_attach (ctx, cur, 0);
830 for (i = 0; i < ctx->vcount; i++) {
831 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
832 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
838 last = msg->content = tmp;
845 include_forward (ctx, cur, tempfp);
847 for (i = 0; i < ctx->vcount; i++)
848 if (ctx->hdrs[ctx->v2r[i]]->tagged)
849 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
854 else if (flags & SENDKEY) {
857 if ((btmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
860 btmp->next = msg->content;
869 void mutt_set_followup_to (ENVELOPE * e)
872 * Only generate the Mail-Followup-To if the user has requested it, and
873 * it hasn't already been set
875 if (!option(OPTFOLLOWUPTO))
879 if (option(OPTNEWSSEND)) {
880 if (!e->followup_to && e->newsgroups && strrchr(e->newsgroups, ','))
881 e->followup_to = m_strdup(e->newsgroups);
886 if (e->mail_followup_to)
889 if (mutt_is_list_cc (0, e->to, e->cc)) {
893 * this message goes to known mailing lists, so create a proper
894 * mail-followup-to header
896 tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
897 address_list_append(tmp, address_list_dup(e->cc));
900 /* remove ourselves from the mail-followup-to header */
901 e->mail_followup_to = remove_user(e->mail_followup_to, 0);
904 * If we are not subscribed to any of the lists in question,
905 * re-add ourselves to the mail-followup-to header. The
906 * mail-followup-to header generated is a no-op with group-reply,
907 * but makes sure list-reply has the desired effect.
909 if (e->mail_followup_to && !mutt_is_list_recipient(0, e->to, e->cc)) {
913 from = address_list_dup(e->reply_to);
915 from = address_list_dup(e->from);
917 from = mutt_default_from();
919 address_list_append(&from, e->mail_followup_to);
920 e->mail_followup_to = from;
923 address_list_uniq(e->mail_followup_to);
927 /* look through the recipients of the message we are replying to, and if
928 we find an address that matches $alternates, we use that as the default
930 static address_t *set_reverse_name (ENVELOPE * env)
932 address_t *tmp = NULL;
934 for (tmp = env->to; tmp; tmp = tmp->next) {
935 if (mutt_addr_is_user(tmp))
938 for (tmp = env->cc; tmp; tmp = tmp->next) {
939 if (mutt_addr_is_user(tmp))
943 if (!mutt_addr_is_user(env->from))
949 tmp = address_dup(tmp);
950 if (!option(OPTREVREAL) || !tmp->personal) {
951 p_delete(&tmp->personal);
952 tmp->personal = m_strdup(Realname);
957 address_t *mutt_default_from (void)
962 * Note: We let $from override $realname here.
963 * Is this the right thing to do?
967 adr = address_dup(From);
968 else if (option (OPTUSEDOMAIN)) {
969 const char *fqdn = mutt_fqdn (1);
971 adr->mailbox = p_new(char, m_strlen(MCore.username) + m_strlen(fqdn) + 2);
972 sprintf(adr->mailbox, "%s@%s", NONULL(MCore.username), NONULL(fqdn));
974 adr = address_new ();
975 adr->mailbox = m_strdup(NONULL(MCore.username));
981 static int send_message (HEADER * msg)
983 char tempfile[_POSIX_PATH_MAX];
987 /* Write out the message in MIME form. */
988 tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
992 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
994 fputc ('\n', tempfp); /* tie off the header. */
996 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1002 if (m_fclose(&tempfp) != 0) {
1003 mutt_perror (_("Can't create temporary file"));
1009 return mix_send_message (msg->chain, tempfile);
1011 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1012 msg->env->bcc, tempfile,
1013 (msg->content->encoding == ENC8BIT));
1017 /* rfc2047 encode the content-descriptions */
1018 static void encode_descriptions (BODY * b, short recurse)
1022 for (t = b; t; t = t->next) {
1023 if (t->description) {
1024 rfc2047_encode_string (&t->description);
1026 if (recurse && t->parts)
1027 encode_descriptions (t->parts, recurse);
1031 /* rfc2047 decode them in case of an error */
1032 static void decode_descriptions (BODY * b)
1036 for (t = b; t; t = t->next) {
1037 if (t->description) {
1038 rfc2047_decode (&t->description);
1041 decode_descriptions (t->parts);
1045 static void fix_end_of_file (const char *data)
1050 if ((fp = safe_fopen (data, "a+")) == NULL)
1052 fseeko (fp, -1, SEEK_END);
1053 if ((c = fgetc (fp)) != '\n')
1058 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1060 HEADER *msg = header_new();
1062 if (mutt_prepare_template (fp, ctx, msg, cur, option(OPTWEED)) < 0)
1065 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1068 int ci_send_message (int flags, /* send mode */
1069 HEADER * msg, /* template to use for new message */
1070 char *tempfile, /* file specified by -i or -H */
1071 CONTEXT * ctx, /* current mailbox */
1073 { /* current message */
1074 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1075 FILE *tempfp = NULL;
1077 int i, killfrom = 0;
1079 int free_clear_content = 0;
1081 BODY *save_content = NULL;
1082 BODY *clear_content = NULL;
1083 char *pgpkeylist = NULL;
1085 /* save current value of "pgp_sign_as" */
1086 char *signas = NULL, *err = NULL;
1087 const char *tag = NULL;
1093 if (flags & SENDNEWS)
1094 set_option (OPTNEWSSEND);
1096 unset_option (OPTNEWSSEND);
1099 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1100 mutt_num_postponed (1)) {
1101 /* If the user is composing a new message, check to see if there
1102 * are any postponed messages first.
1105 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1109 flags |= SENDPOSTPONED;
1113 if (flags & SENDPOSTPONED)
1114 signas = m_strdup(PgpSignAs);
1116 /* Delay expansion of aliases until absolutely necessary--shouldn't
1117 * be necessary unless we are prompting the user or about to execute a
1124 if (flags == SENDPOSTPONED) {
1126 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1130 * If postponed message is a news article, it have
1131 * a "Newsgroups:" header line, then set appropriate flag.
1133 if (msg->env->newsgroups) {
1135 set_option (OPTNEWSSEND);
1139 unset_option (OPTNEWSSEND);
1144 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1145 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1146 mutt_perror (msg->content->filename);
1152 msg->env = envelope_new();
1155 /* Parse and use an eventual list-post header */
1156 if ((flags & SENDLISTREPLY)
1157 && cur && cur->env && cur->env->list_post) {
1158 /* Use any list-post header as a template */
1159 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1160 /* We don't let them set the sender's address. */
1161 address_list_wipe(&msg->env->from);
1164 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1166 pbody->next = msg->content; /* don't kill command-line attachments */
1167 msg->content = pbody;
1169 if (!(ctype = m_strdup(ContentType)))
1170 ctype = m_strdup("text/plain");
1171 mutt_parse_content_type (ctype, msg->content);
1174 msg->content->unlink = 1;
1175 msg->content->use_disp = 0;
1176 msg->content->disposition = DISPINLINE;
1177 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1178 && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1179 parameter_setval(&msg->content->parameter, "format", "flowed");
1180 if (option (OPTDELSP))
1181 parameter_setval(&msg->content->parameter, "delsp", "yes");
1185 char buffer[_POSIX_PATH_MAX];
1186 tempfp = m_tempfile(buffer, sizeof(buffer), NONULL(MCore.tmpdir), NULL);
1187 msg->content->filename = m_strdup(buffer);
1189 tempfp = safe_fopen(tempfile, "a+");
1190 msg->content->filename = m_strdup(tempfile);
1194 mutt_perror (msg->content->filename);
1199 /* this is handled here so that the user can match ~f in send-hook */
1200 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1201 /* we shouldn't have to worry about freeing `msg->env->from' before
1202 * setting it here since this code will only execute when doing some
1203 * sort of reply. the pointer will only be set when using the -H command
1206 * We shouldn't have to worry about alias expansion here since we are
1207 * either replying to a real or postponed message, therefore no aliases
1208 * should exist since the user has not had the opportunity to add
1209 * addresses to the list. We just have to ensure the postponed messages
1210 * have their aliases expanded.
1213 msg->env->from = set_reverse_name (cur->env);
1216 if (!msg->env->from && option (OPTUSEFROM)
1217 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1218 msg->env->from = mutt_default_from ();
1220 if (flags & SENDBATCH) {
1221 mutt_copy_stream (stdin, tempfp);
1222 if (option (OPTHDRS)) {
1223 process_user_recips (msg->env);
1224 process_user_header (msg->env);
1226 mutt_expand_aliases_env (msg->env);
1228 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1229 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1230 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1233 if (option (OPTHDRS))
1234 process_user_recips (msg->env);
1236 /* Expand aliases and remove duplicates/crossrefs */
1237 mutt_fix_reply_recipients (msg->env);
1240 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1241 && !msg->env->newsgroups)
1242 msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
1245 if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1246 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1247 if (edit_envelope (msg->env, flags) == -1)
1251 /* the from address must be set here regardless of whether or not
1252 * $use_from is set so that the `~P' (from you) operator in send-hook
1253 * patterns will work. if $use_from is unset, the from address is killed
1254 * after send-hooks are evaulated */
1256 if (!msg->env->from) {
1257 msg->env->from = mutt_default_from ();
1261 if ((flags & SENDREPLY) && cur) {
1262 /* change setting based upon message we are replying to */
1263 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1266 * set the replied flag for the message we are generating so that the
1267 * user can use ~Q in a send-hook to know when reply-hook's are also
1273 /* change settings based upon recipients */
1275 mutt_message_hook (NULL, msg, M_SENDHOOK);
1278 * Unset the replied flag from the message we are composing since it is
1279 * no longer required. This is done here because the FCC'd copy of
1280 * this message was erroneously get the 'R'eplied flag when stored in
1281 * a maildir-style mailbox.
1286 address_list_wipe(&msg->env->from);
1290 if (option (OPTHDRS))
1291 process_user_header (msg->env);
1294 if (option (OPTSIGONTOP) && (!(flags & SENDKEY)))
1295 append_signature (tempfp);
1297 /* include replies/forwarded messages, unless we are given a template */
1298 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1299 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1302 if (!option (OPTSIGONTOP) && (!(flags & SENDKEY)))
1303 append_signature (tempfp);
1306 * this wants to be done _after_ generate_body, so message-hooks
1310 if (option (OPTCRYPTAUTOSIGN))
1311 msg->security |= SIGN;
1312 if (option (OPTCRYPTAUTOENCRYPT))
1313 msg->security |= ENCRYPT;
1314 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1315 msg->security |= ENCRYPT;
1316 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1317 msg->security |= SIGN;
1318 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1319 && (cur->security & ENCRYPT))
1320 msg->security |= SIGN;
1321 if (msg->security & (ENCRYPT | SIGN)) {
1322 if (option (OPTPGPAUTOINLINE))
1323 msg->security |= INLINE;
1324 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1325 msg->security |= INLINE;
1328 if (msg->security) {
1330 * When reypling / forwarding, use the original message's
1331 * crypto system. According to the documentation,
1332 * smime_is_default should be disregarded here.
1334 * Problem: At least with forwarding, this doesn't really
1335 * make much sense. Should we have an option to completely
1336 * disable individual mechanisms at run-time?
1339 if (option (OPTCRYPTAUTOPGP) && (cur->security & APPLICATION_PGP))
1340 msg->security |= APPLICATION_PGP;
1341 else if (option (OPTCRYPTAUTOSMIME)
1342 && (cur->security & APPLICATION_SMIME))
1343 msg->security |= APPLICATION_SMIME;
1347 * No crypto mechanism selected? Use availability + smime_is_default
1350 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1351 if (option (OPTCRYPTAUTOSMIME) && option (OPTSMIMEISDEFAULT))
1352 msg->security |= APPLICATION_SMIME;
1353 else if (option (OPTCRYPTAUTOPGP))
1354 msg->security |= APPLICATION_PGP;
1355 else if (option (OPTCRYPTAUTOSMIME))
1356 msg->security |= APPLICATION_SMIME;
1360 /* No permissible mechanisms found. Don't sign or encrypt. */
1361 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1366 * This hook is even called for postponed messages, and can, e.g., be
1367 * used for setting the editor, the sendmail path, or the
1370 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1372 /* wait until now to set the real name portion of our return address so
1373 that $realname can be set in a send-hook */
1374 if (msg->env->from && !msg->env->from->personal
1375 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1376 msg->env->from->personal = m_strdup(Realname);
1378 if (!(flags & SENDKEY))
1381 if (!(flags & SENDBATCH)) {
1383 time_t mtime = m_decrease_mtime(msg->content->filename, NULL);
1385 mutt_update_encoding (msg->content);
1388 * Select whether or not the user's editor should be called now. We
1389 * don't want to do this when:
1390 * 1) we are sending a key/cert
1391 * 2) we are forwarding a message and the user doesn't want to edit it.
1392 * This is controled by the quadoption $forward_edit. However, if
1393 * both $edit_headers and $autoedit are set, we want to ignore the
1394 * setting of $forward_edit because the user probably needs to add the
1397 if (!(flags & SENDKEY) &&
1398 ((flags & SENDFORWARD) == 0 ||
1399 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1400 query_quadoption (OPT_FORWEDIT,
1401 _("Edit forwarded message?")) == M_YES)) {
1402 /* If the this isn't a text message, look for a mailcap edit command */
1403 if (rfc1524_mailcap_isneeded(msg->content)) {
1404 if (!mutt_edit_attachment (msg->content))
1406 } else if (option (OPTEDITHDRS)) {
1407 mutt_env_to_local (msg->env);
1408 mutt_edit_headers(msg->content->filename, msg, fcc, sizeof (fcc));
1409 mutt_env_to_idna (msg->env, NULL, NULL);
1412 mutt_edit_file(msg->content->filename);
1414 if (stat (msg->content->filename, &st) == 0) {
1415 if (mtime != st.st_mtime)
1416 fix_end_of_file (msg->content->filename);
1418 mutt_perror (msg->content->filename);
1421 if (option (OPTTEXTFLOWED))
1422 rfc3676_space_stuff (msg);
1424 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1427 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1428 if (stat (msg->content->filename, &st) == 0) {
1429 /* if the file was not modified, bail out now */
1430 if (mtime == st.st_mtime && !msg->content->next &&
1431 query_quadoption (OPT_ABORT,
1432 _("Abort unmodified message?")) == M_YES) {
1433 mutt_message _("Aborted unmodified message.");
1439 mutt_perror (msg->content->filename);
1443 /* specify a default fcc. if we are in batchmode, only save a copy of
1444 * the message if the value of $copy is yes or ask-yes */
1446 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1447 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1448 /* set the default FCC */
1449 if (!msg->env->from) {
1450 msg->env->from = mutt_default_from ();
1451 killfrom = 1; /* no need to check $use_from because if the user specified
1452 a from address it would have already been set by now */
1454 mutt_select_fcc (fcc, sizeof (fcc), msg);
1456 address_list_wipe(&msg->env->from);
1462 mutt_update_encoding (msg->content);
1464 if (!(flags & SENDBATCH)) {
1467 fcc_error = 0; /* reset value since we may have failed before */
1468 mutt_pretty_mailbox (fcc);
1469 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1473 if (flags & SENDNEWS)
1474 mutt_message (_("Article not posted."));
1478 mutt_message _("Mail not sent.");
1482 /* postpone the message until later. */
1483 if (msg->content->next)
1484 msg->content = mutt_make_multipart (msg->content);
1487 * make sure the message is written to the right part of a maildir
1493 encode_descriptions (msg->content, 1);
1494 mutt_prepare_envelope (msg->env, 0);
1495 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1498 || mutt_write_fcc (NONULL (Postponed), msg,
1500 && (flags & SENDREPLY)) ? cur->env->
1501 message_id : NULL, 1, fcc) < 0) {
1502 msg->content = mutt_remove_multipart (msg->content);
1503 decode_descriptions (msg->content);
1504 mutt_unprepare_envelope (msg->env);
1507 mutt_update_num_postponed ();
1508 mutt_message _("Message postponed.");
1515 if (!(flags & SENDNEWS))
1517 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1518 if (!(flags & SENDBATCH)) {
1519 mutt_error _("No recipients are specified!");
1524 puts _("No recipients were specified.");
1530 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1531 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1533 if (!(flags & SENDBATCH))
1539 if (!msg->env->subject && !(flags & SENDBATCH) &&
1541 query_quadoption (OPT_SUBJECT,
1542 _("No subject, abort sending?"))) != M_NO) {
1543 /* if the abort is automatic, print an error message */
1544 if (quadoption (OPT_SUBJECT) == M_YES)
1545 mutt_error _("No subject specified.");
1550 if ((flags & SENDNEWS) && !msg->env->subject) {
1551 mutt_error _("No subject specified.");
1556 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1557 mutt_error _("No newsgroup specified.");
1563 if (msg->content->next)
1564 msg->content = mutt_make_multipart (msg->content);
1566 if (mutt_attach_check (msg) &&
1567 !msg->content->next &&
1568 query_quadoption (OPT_ATTACH,
1569 _("No attachments made but indicator found in text. "
1570 "Cancel sending?")) == M_YES) {
1571 if (quadoption (OPT_ATTACH) == M_YES) {
1572 mutt_message _("No attachments made but indicator found in text. "
1576 mutt_message (_("Mail not sent."));
1581 * Ok, we need to do it this way instead of handling all fcc stuff in
1582 * one place in order to avoid going to main_loop with encoded "env"
1583 * in case of error. Ugh.
1586 encode_descriptions (msg->content, 1);
1589 * Make sure that clear_content and free_clear_content are
1590 * properly initialized -- we may visit this particular place in
1591 * the code multiple times, including after a failed call to
1595 clear_content = NULL;
1596 free_clear_content = 0;
1598 if (msg->security) {
1599 /* save the decrypted attachments */
1600 clear_content = msg->content;
1602 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1603 mutt_protect (msg, pgpkeylist) == -1) {
1604 msg->content = mutt_remove_multipart (msg->content);
1606 p_delete(&pgpkeylist);
1608 decode_descriptions (msg->content);
1611 encode_descriptions (msg->content, 0);
1615 * at this point, msg->content is one of the following three things:
1616 * - multipart/signed. In this case, clear_content is a child.
1617 * - multipart/encrypted. In this case, clear_content exists
1619 * - application/pgp. In this case, clear_content exists independently.
1620 * - something else. In this case, it's the same as clear_content.
1623 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1625 if (clear_content && (msg->content != clear_content)
1626 && (msg->content->parts != clear_content))
1627 free_clear_content = 1;
1629 if (!option (OPTNOCURSES))
1630 mutt_message _("Sending message...");
1632 mutt_prepare_envelope (msg->env, 1);
1634 /* save a copy of the message, if necessary. */
1636 mutt_expand_path (fcc, sizeof (fcc));
1639 /* Don't save a copy when we are in batch-mode, and the FCC
1640 * folder is on an IMAP server: This would involve possibly lots
1641 * of user interaction, which is not available in batch mode.
1643 * Note: A patch to fix the problems with the use of IMAP servers
1644 * from non-curses mode is available from Brendan Cully. However,
1645 * I'd like to think a bit more about this before including it.
1648 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1651 if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1652 BODY *tmpbody = msg->content;
1653 BODY *save_sig = NULL;
1654 BODY *save_parts = NULL;
1656 if (msg->security && option (OPTFCCCLEAR))
1657 msg->content = clear_content;
1659 /* check to see if the user wants copies of all attachments */
1660 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1661 if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1662 m_strcmp(msg->content->subtype, "signed") == 0))
1664 if (clear_content->type == TYPEMULTIPART) {
1665 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1666 /* save initial signature and attachments */
1667 save_sig = msg->content->parts->next;
1668 save_parts = clear_content->parts->next;
1671 /* this means writing only the main part */
1672 msg->content = clear_content->parts;
1674 if (mutt_protect (msg, pgpkeylist) == -1) {
1675 /* we can't do much about it at this point, so
1676 * fallback to saving the whole thing to fcc
1678 msg->content = tmpbody;
1683 save_content = msg->content;
1687 msg->content = msg->content->parts;
1692 /* update received time so that when storing to a mbox-style folder
1693 * the From_ line contains the current time instead of when the
1694 * message was first postponed.
1696 msg->received = time (NULL);
1697 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1699 * Error writing FCC, we should abort sending.
1705 msg->content = tmpbody;
1708 /* cleanup the second signature structures */
1709 if (save_content->parts) {
1710 body_list_wipe(&save_content->parts->next);
1711 save_content->parts = NULL;
1713 body_list_wipe(&save_content);
1715 /* restore old signature and attachments */
1716 msg->content->parts->next = save_sig;
1717 msg->content->parts->parts->next = save_parts;
1719 else if (save_content) {
1720 /* destroy the new encrypted body. */
1721 body_list_wipe(&save_content);
1728 * Don't attempt to send the message if the FCC failed. Just pretend
1729 * the send failed as well so we give the user a chance to fix the
1732 if (fcc_error || (i = send_message (msg)) == -1) {
1733 if (!(flags & SENDBATCH)) {
1734 if ((msg->security & ENCRYPT)
1735 || ((msg->security & SIGN)
1736 && msg->content->type == TYPEAPPLICATION)) {
1737 body_list_wipe(&msg->content); /* destroy PGP data */
1738 msg->content = clear_content; /* restore clear text. */
1740 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1741 body_list_wipe(&msg->content->parts->next); /* destroy sig */
1742 msg->content = mutt_remove_multipart (msg->content);
1745 msg->content = mutt_remove_multipart (msg->content);
1746 decode_descriptions (msg->content);
1747 mutt_unprepare_envelope (msg->env);
1751 puts _("Could not send the message.");
1756 else if (!option (OPTNOCURSES))
1757 mutt_message (i != 0 ? _("Sending in background.") :
1759 (flags & SENDNEWS) ? _("Article posted.") :
1765 if (msg->security & ENCRYPT)
1766 p_delete(&pgpkeylist);
1768 if (free_clear_content)
1769 body_list_wipe(&clear_content);
1771 if (flags & SENDREPLY) {
1773 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1774 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1775 for (i = 0; i < ctx->vcount; i++)
1776 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1777 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1786 if (flags & SENDPOSTPONED) {
1788 p_delete(&PgpSignAs);
1794 header_delete(&msg);
1799 /* vim: set sw=2: */