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>
23 #include "mutt_idna.h"
27 #include <nntp/nntp.h>
32 int url_parse_mailto(ENVELOPE *e, char **body, const char *src)
38 char scratch[HUGE_STRING];
42 string_list_t **last = &e->userhdrs;
44 if (!(t = strchr (src, ':')))
47 if ((tmp = m_strdup(t + 1)) == NULL)
50 if ((headers = strchr (tmp, '?')))
54 e->to = rfc822_parse_adrlist(e->to, tmp);
56 tag = headers ? strtok (headers, "&") : NULL;
58 for (; tag; tag = strtok(NULL, "&")) {
59 if ((value = strchr (tag, '=')))
61 if (!value || !*value)
67 if (mime_which_token(tag, -1) == MIME_BODY) {
69 m_strreplace(body, value);
71 #define SAFEPFX (option(OPTSTRICTMAILTO) ? "" : "X-Mailto-")
72 taglen = m_strlen(tag) + strlen(SAFEPFX);
73 /* mutt_parse_rfc822_line makes some assumptions */
74 snprintf(scratch, sizeof(scratch), "%s%s: %s", SAFEPFX, tag, value);
76 scratch[taglen] = '\0';
77 value = vskipspaces(&scratch[taglen + 1]);
78 last = mutt_parse_rfc822_line (e, NULL, scratch, value, 0, 0, last);
79 /* if $strict_mailto is set, force editing headers to let
80 * users have a look at what we got */
81 if (!option (OPTSTRICTMAILTO)) {
82 set_option (OPTXMAILTO);
83 set_option (OPTEDITHDRS);
91 static void append_signature (FILE * f)
97 fprintf (f, "\n%s", SignOffString);
100 if ((tmpfp = mutt_open_read(NONULL(MAlias.signature), &thepid))) {
101 if (option (OPTSIGDASHES))
102 fputs ("\n-- \n", f);
103 else if (SignOffString)
105 mutt_copy_stream (tmpfp, f);
108 mutt_wait_filter (thepid);
112 /* compare two e-mail addresses and return 1 if they are equivalent */
113 static int mutt_addrcmp (address_t * a, address_t * b)
115 if (!a->mailbox || !b->mailbox)
117 if (ascii_strcasecmp (a->mailbox, b->mailbox))
122 /* search an e-mail address in a list */
123 static int mutt_addrsrc (address_t * a, address_t * lst)
125 for (; lst; lst = lst->next) {
126 if (mutt_addrcmp (a, lst))
132 /* removes addresses from "b" which are contained in "a" */
133 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
135 address_t *top, *p, *prev = NULL;
139 for (p = a; p; p = p->next) {
140 if (mutt_addrcmp (p, b))
145 prev->next = b->next;
147 address_list_wipe(&b);
153 address_list_wipe(&b);
165 /* remove any address which matches the current user. if `leave_only' is
166 * nonzero, don't remove the user's address if it is the only one in the list
168 static address_t *remove_user (address_t * a, int leave_only)
170 address_t *top = NULL, *last = NULL;
173 if (!mutt_addr_is_user (a)) {
187 if (!leave_only || a || last) {
189 address_list_wipe(&tmp);
198 static address_t *find_mailing_lists (address_t * t, address_t * c)
200 address_t *top = NULL, *ptr = NULL;
202 for (; t || c; t = c, c = NULL) {
203 for (; t; t = t->next) {
204 if (mutt_is_mail_list (t) && !t->group) {
206 ptr->next = address_dup (t);
210 ptr = top = address_dup (t);
217 static int edit_address (address_t ** a, const char *field)
219 char buf[HUGE_STRING];
225 mutt_addrlist_to_local (*a);
226 rfc822_addrcat(buf, sizeof(buf), *a, 0);
227 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
229 address_list_wipe(a);
230 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
231 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
232 mutt_error (_("Error: '%s' is a bad IDN."), err);
238 while (idna_ok != 0);
242 static int edit_envelope (ENVELOPE * en, int flags)
244 char buf[HUGE_STRING];
245 string_list_t *uh = UserHeader;
246 regmatch_t pat_match[1];
249 if (option (OPTNEWSSEND)) {
251 m_strcpy(buf, sizeof(buf), en->newsgroups);
254 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
256 p_delete(&en->newsgroups);
257 en->newsgroups = m_strdup(buf);
260 m_strcpy(buf, sizeof(buf), en->followup_to);
263 if (option (OPTASKFOLLOWUP)
264 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
266 p_delete(&en->followup_to);
267 en->followup_to = m_strdup(buf);
269 if (en->x_comment_to)
270 m_strcpy(buf, sizeof(buf), en->x_comment_to);
273 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
274 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
276 p_delete(&en->x_comment_to);
277 en->x_comment_to = m_strdup(buf);
282 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
284 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
286 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
291 if (option (OPTFASTREPLY))
294 m_strcpy(buf, sizeof(buf), en->subject);
300 for (; uh; uh = uh->next) {
301 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
302 p = vskipspaces(uh->data + 8);
303 m_strcpy(buf, sizeof(buf), p);
308 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
309 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
310 unsigned int pos = pat_match->rm_so;
312 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
313 buf[pos] = '\0'; /* kill match */
314 while (pos-- && buf[pos] == ' ')
315 buf[pos] = '\0'; /* remove trailing spaces */
318 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
322 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
327 ("No subject, abort?"))
329 mutt_message _("No subject, aborting.");
333 m_strreplace(&en->subject, buf);
339 static char *nntp_get_header(const char *s)
341 return m_strdup(skipspaces(s));
345 static void process_user_recips (ENVELOPE * env)
347 string_list_t *uh = UserHeader;
349 for (; uh; uh = uh->next) {
350 const char *p = strchr(uh->data, ':');
354 switch (mime_which_token(uh->data, p++ - uh->data)) {
356 env->to = rfc822_parse_adrlist(env->to, p);
359 env->cc = rfc822_parse_adrlist(env->cc, p);
362 env->bcc = rfc822_parse_adrlist(env->bcc, p);
365 case MIME_NEWSGROUPS:
366 env->newsgroups = nntp_get_header(p);
368 case MIME_FOLLOWUP_TO:
369 env->followup_to = nntp_get_header(p);
371 case MIME_X_COMMENT_TO:
372 env->x_comment_to = nntp_get_header(p);
380 static void process_user_header(ENVELOPE * env)
383 string_list_t **last = string_list_last(&env->userhdrs);
385 for (uh = UserHeader; uh; uh = uh->next) {
386 const char *p = strchr(uh->data, ':');
390 switch (mime_which_token(uh->data, p++ - uh->data)) {
392 /* User has specified a default From: address. Remove default address */
393 address_list_wipe(&env->from);
394 env->from = rfc822_parse_adrlist(env->from, p);
398 address_list_wipe(&env->reply_to);
399 env->reply_to = rfc822_parse_adrlist (env->reply_to, p);
402 case MIME_MESSAGE_ID:
403 m_strreplace(&env->message_id, p);
410 case MIME_NEWSGROUPS:
411 case MIME_FOLLOWUP_TO:
412 case MIME_X_COMMENT_TO:
414 case MIME_SUPERSEDES:
415 case MIME_SUPERCEDES:
420 *last = string_item_new();
421 (*last)->data = m_strdup(uh->data);
422 last = &(*last)->next;
428 void mutt_forward_intro (FILE * fp, HEADER * cur)
432 fputs ("----- Forwarded message from ", fp);
434 rfc822_addrcat(buffer, sizeof(buffer), cur->env->from, 1);
436 fputs (" -----\n\n", fp);
439 void mutt_forward_trailer (FILE * fp)
441 fputs ("\n----- End forwarded message -----\n", fp);
445 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
447 int chflags = CH_DECODE, cmflags = 0;
449 mutt_parse_mime_message (ctx, cur);
450 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
452 mutt_forward_intro (out, cur);
454 if (option (OPTFORWDECODE)) {
455 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
456 if (option (OPTWEED)) {
457 chflags |= CH_WEED | CH_REORDER;
458 cmflags |= M_CM_WEED;
461 if (option (OPTFORWQUOTE))
462 cmflags |= M_CM_PREFIX;
464 mutt_copy_message (out, ctx, cur, cmflags, chflags);
465 mutt_forward_trailer (out);
469 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
474 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
480 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
482 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
483 int chflags = CH_DECODE;
485 mutt_parse_mime_message (ctx, cur);
486 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
487 mutt_make_attribution (ctx, cur, out);
489 if (!option (OPTHEADER))
490 cmflags |= M_CM_NOHEADER;
491 if (option (OPTWEED)) {
492 chflags |= CH_WEED | CH_REORDER;
493 cmflags |= M_CM_WEED;
496 mutt_copy_message (out, ctx, cur, cmflags, chflags);
500 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
504 if (flags && env->mail_followup_to && hmfupto == M_YES) {
505 address_list_append(to, address_list_dup(env->mail_followup_to));
509 /* Exit now if we're setting up the default Cc list for list-reply
510 * (only set if Mail-Followup-To is present and honoured).
512 if (flags & SENDLISTREPLY)
515 /* If this message came from a mailing list, ask the user if he really
516 * intended to reply to the author only.
518 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
519 switch (query_quadoption (OPT_LISTREPLY,
520 _("Message came from a mailing list. List-reply to mailing list?")))
523 address_list_append(to, find_mailing_lists (env->to, env->cc));
526 return -1; /* abort */
530 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
531 /* mail is from the user, assume replying to recipients */
532 address_list_append(to, address_list_dup(env->to));
534 else if (env->reply_to) {
535 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
536 (option (OPTIGNORELISTREPLYTO) &&
537 mutt_is_mail_list (env->reply_to) &&
538 (mutt_addrsrc (env->reply_to, env->to) ||
539 mutt_addrsrc (env->reply_to, env->cc)))) {
540 /* If the Reply-To: address is a mailing list, assume that it was
541 * put there by the mailing list, and use the From: address
543 * We also take the from header if our correspondant has a reply-to
544 * header which is identical to the electronic mail address given
545 * in his From header.
548 address_list_append(to, address_list_dup(env->from));
550 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
551 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
552 /* There are quite a few mailing lists which set the Reply-To:
553 * header field to the list address, which makes it quite impossible
554 * to send a message to only the sender of the message. This
555 * provides a way to do that.
557 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
558 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
559 switch (query_quadoption (OPT_REPLYTO, prompt)) {
561 address_list_append(to, address_list_dup(env->reply_to));
565 address_list_append(to, address_list_dup(env->from));
569 return (-1); /* abort */
573 address_list_append(to, address_list_dup(env->reply_to));
576 address_list_append(to, address_list_dup(env->from));
581 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
586 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
587 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
588 in->mail_followup_to->mailbox,
589 in->mail_followup_to->next ? ",..." : "");
591 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
595 if (flags & SENDLISTREPLY) {
596 address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
598 if (in->mail_followup_to && hmfupto == M_YES &&
599 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
600 return (-1); /* abort */
603 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
604 return (-1); /* abort */
606 if ((flags & SENDGROUPREPLY)
607 && (!in->mail_followup_to || hmfupto != M_YES))
609 address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
610 address_list_append(tmp, address_list_dup(in->cc));
616 void mutt_fix_reply_recipients (ENVELOPE * env)
618 mutt_expand_aliases_env (env);
620 if (!option (OPTMETOO)) {
621 /* the order is important here. do the CC: first so that if the
622 * the user is the only recipient, it ends up on the TO: field
624 env->cc = remove_user (env->cc, (env->to == NULL));
625 env->to = remove_user (env->to, (env->cc == NULL));
628 /* the CC field can get cluttered, especially with lists */
629 address_list_uniq(env->to);
630 address_list_uniq(env->cc);
631 env->cc = mutt_remove_xrefs (env->to, env->cc);
633 if (env->cc && !env->to) {
639 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
643 /* set the default subject for the message. */
644 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
645 m_strreplace(&env->subject, buffer);
648 void mutt_make_misc_reply_headers (ENVELOPE * env,
649 CONTEXT * ctx __attribute__ ((unused)),
650 HEADER * cur __attribute__ ((unused)),
653 /* This takes precedence over a subject that might have
654 * been taken from a List-Post header. Is that correct?
656 if (curenv->real_subj) {
657 p_delete(&env->subject);
658 env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
659 sprintf (env->subject, "Re: %s", curenv->real_subj);
661 else if (!env->subject)
662 env->subject = m_strdup("Re: your mail");
665 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
666 env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
670 static string_list_t *mutt_make_references (ENVELOPE * e)
672 string_list_t *t = NULL, *l = NULL;
675 l = string_list_dup(e->references);
677 l = string_list_dup(e->in_reply_to);
680 t = string_item_new();
681 t->data = m_strdup(e->message_id);
689 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
690 string_list_t *** pp, string_list_t *** qq)
692 string_list_t **p = NULL, **q = NULL;
700 p = &env->references;
702 q = &env->in_reply_to;
709 *p = mutt_make_references (curenv);
711 if (curenv->message_id) {
712 *q = string_item_new();
713 (*q)->data = m_strdup(curenv->message_id);
724 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
726 env->references = NULL;
727 env->in_reply_to = NULL;
731 string_list_t **p = NULL, **q = NULL;
734 for (i = 0; i < ctx->vcount; i++) {
735 h = ctx->hdrs[ctx->v2r[i]];
737 mutt_add_to_reference_headers (env, h->env, &p, &q);
741 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
745 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
747 ENVELOPE *curenv = NULL;
752 for (i = 0; i < ctx->vcount; i++)
753 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
754 cur = ctx->hdrs[ctx->v2r[i]];
760 /* This could happen if the user tagged some messages and then did
761 * a limit such that none of the tagged message are visible.
763 mutt_error _("No tagged messages are visible!");
771 if (flags & SENDREPLY) {
773 if ((flags & SENDNEWS)) {
774 /* in case followup set Newsgroups: with Followup-To: if it present */
775 if (!env->newsgroups && curenv &&
776 m_strcasecmp(curenv->followup_to, "poster"))
777 env->newsgroups = m_strdup(curenv->followup_to);
784 for (i = 0; i < ctx->vcount; i++) {
785 h = ctx->hdrs[ctx->v2r[i]];
786 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
790 else if (mutt_fetch_recips (env, curenv, flags) == -1)
793 if ((flags & SENDLISTREPLY) && !env->to) {
794 mutt_error _("No mailing lists found!");
799 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
800 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
802 else if (flags & SENDFORWARD)
803 mutt_make_forward_subject (env, ctx, cur);
808 static int generate_body (FILE * tempfp, /* stream for outgoing message */
809 HEADER * msg, /* header for outgoing message */
810 int flags, /* compose mode */
811 CONTEXT * ctx, /* current mailbox */
813 { /* current message */
818 if (flags & SENDREPLY) {
820 query_quadoption (OPT_INCLUDE,
821 _("Include message in reply?"))) == -1)
825 mutt_message _("Including quoted message...");
828 for (i = 0; i < ctx->vcount; i++) {
829 h = ctx->hdrs[ctx->v2r[i]];
831 if (include_reply (ctx, h, tempfp) == -1) {
832 mutt_error _("Could not include all requested messages!");
836 fputc ('\n', tempfp);
841 include_reply (ctx, cur, tempfp);
845 else if (flags & SENDFORWARD) {
847 query_quadoption (OPT_MIMEFWD,
848 _("Forward as attachment?"))) == M_YES) {
849 BODY *last = msg->content;
851 mutt_message _("Preparing forwarded message...");
853 while (last && last->next)
857 tmp = mutt_make_message_attach (ctx, cur, 0);
864 for (i = 0; i < ctx->vcount; i++) {
865 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
866 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
872 last = msg->content = tmp;
879 include_forward (ctx, cur, tempfp);
881 for (i = 0; i < ctx->vcount; i++)
882 if (ctx->hdrs[ctx->v2r[i]]->tagged)
883 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
894 void mutt_set_followup_to (ENVELOPE * e)
897 * Only generate the Mail-Followup-To if the user has requested it, and
898 * it hasn't already been set
900 if (!option(OPTFOLLOWUPTO))
904 if (option(OPTNEWSSEND)) {
905 if (!e->followup_to && e->newsgroups && strrchr(e->newsgroups, ','))
906 e->followup_to = m_strdup(e->newsgroups);
911 if (e->mail_followup_to)
914 if (mutt_is_list_cc (0, e->to, e->cc)) {
918 * this message goes to known mailing lists, so create a proper
919 * mail-followup-to header
921 tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
922 address_list_append(tmp, address_list_dup(e->cc));
925 /* remove ourselves from the mail-followup-to header */
926 e->mail_followup_to = remove_user(e->mail_followup_to, 0);
929 * If we are not subscribed to any of the lists in question,
930 * re-add ourselves to the mail-followup-to header. The
931 * mail-followup-to header generated is a no-op with group-reply,
932 * but makes sure list-reply has the desired effect.
934 if (e->mail_followup_to && !mutt_is_list_recipient(0, e->to, e->cc)) {
938 from = address_list_dup(e->reply_to);
940 from = address_list_dup(e->from);
942 from = mutt_default_from();
944 address_list_append(&from, e->mail_followup_to);
945 e->mail_followup_to = from;
948 address_list_uniq(e->mail_followup_to);
952 /* look through the recipients of the message we are replying to, and if
953 we find an address that matches $alternates, we use that as the default
955 static address_t *set_reverse_name (ENVELOPE * env)
957 address_t *tmp = NULL;
959 for (tmp = env->to; tmp; tmp = tmp->next) {
960 if (mutt_addr_is_user(tmp))
963 for (tmp = env->cc; tmp; tmp = tmp->next) {
964 if (mutt_addr_is_user(tmp))
968 if (!mutt_addr_is_user(env->from))
974 tmp = address_dup(tmp);
975 if (!option(OPTREVREAL) || !tmp->personal) {
976 p_delete(&tmp->personal);
977 tmp->personal = m_strdup(Realname);
982 address_t *mutt_default_from (void)
987 * Note: We let $from override $realname here.
988 * Is this the right thing to do?
992 adr = address_dup(MAlias.from);
993 else if (MCore.use_domain) {
994 const char *fqdn = mutt_fqdn (1);
996 adr->mailbox = p_new(char, m_strlen(MCore.username) + m_strlen(fqdn) + 2);
997 sprintf(adr->mailbox, "%s@%s", NONULL(MCore.username), NONULL(fqdn));
999 adr = address_new ();
1000 adr->mailbox = m_strdup(NONULL(MCore.username));
1006 static int send_message (HEADER * msg)
1008 char tempfile[_POSIX_PATH_MAX];
1012 /* Write out the message in MIME form. */
1013 tempfp = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
1017 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
1018 msg->chain ? 1 : 0);
1019 fputc ('\n', tempfp); /* tie off the header. */
1021 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1027 if (m_fclose(&tempfp) != 0) {
1028 mutt_perror (_("Can't create temporary file"));
1034 return mix_send_message (msg->chain, tempfile);
1036 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1037 msg->env->bcc, tempfile,
1038 (msg->content->encoding == ENC8BIT));
1042 /* rfc2047 encode the content-descriptions */
1043 static void encode_descriptions (BODY * b, short recurse)
1047 for (t = b; t; t = t->next) {
1048 if (t->description) {
1049 rfc2047_encode_string (&t->description);
1051 if (recurse && t->parts)
1052 encode_descriptions (t->parts, recurse);
1056 /* rfc2047 decode them in case of an error */
1057 static void decode_descriptions (BODY * b)
1061 for (t = b; t; t = t->next) {
1062 if (t->description) {
1063 rfc2047_decode (&t->description);
1066 decode_descriptions (t->parts);
1070 static void fix_end_of_file (const char *data)
1075 if ((fp = safe_fopen (data, "a+")) == NULL)
1077 fseeko (fp, -1, SEEK_END);
1078 if ((c = fgetc (fp)) != '\n')
1083 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1085 HEADER *msg = header_new();
1087 if (mutt_prepare_template (fp, ctx, msg, cur, option(OPTWEED)) < 0)
1090 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1093 int ci_send_message (int flags, /* send mode */
1094 HEADER * msg, /* template to use for new message */
1095 char *tempfile, /* file specified by -i or -H */
1096 CONTEXT * ctx, /* current mailbox */
1098 { /* current message */
1099 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1100 FILE *tempfp = NULL;
1102 int i, killfrom = 0;
1104 int free_clear_content = 0;
1106 BODY *save_content = NULL;
1107 BODY *clear_content = NULL;
1108 char *pgpkeylist = NULL;
1110 /* save current value of "pgp_sign_as" */
1111 char *signas = NULL, *err = NULL;
1112 const char *tag = NULL;
1118 if (flags & SENDNEWS)
1119 set_option (OPTNEWSSEND);
1121 unset_option (OPTNEWSSEND);
1124 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1125 mutt_num_postponed (1)) {
1126 /* If the user is composing a new message, check to see if there
1127 * are any postponed messages first.
1130 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1134 flags |= SENDPOSTPONED;
1138 if (flags & SENDPOSTPONED)
1139 signas = m_strdup(PgpSignAs);
1141 /* Delay expansion of aliases until absolutely necessary--shouldn't
1142 * be necessary unless we are prompting the user or about to execute a
1149 if (flags == SENDPOSTPONED) {
1151 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1155 * If postponed message is a news article, it have
1156 * a "Newsgroups:" header line, then set appropriate flag.
1158 if (msg->env->newsgroups) {
1160 set_option (OPTNEWSSEND);
1164 unset_option (OPTNEWSSEND);
1169 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1170 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1171 mutt_perror (msg->content->filename);
1177 msg->env = envelope_new();
1180 /* Parse and use an eventual list-post header */
1181 if ((flags & SENDLISTREPLY)
1182 && cur && cur->env && cur->env->list_post) {
1183 /* Use any list-post header as a template */
1184 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1185 /* We don't let them set the sender's address. */
1186 address_list_wipe(&msg->env->from);
1189 if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1191 pbody->next = msg->content; /* don't kill command-line attachments */
1192 msg->content = pbody;
1194 if (!(ctype = m_strdup(ContentType)))
1195 ctype = m_strdup("text/plain");
1196 mutt_parse_content_type (ctype, msg->content);
1199 msg->content->unlink = 1;
1200 msg->content->use_disp = 0;
1201 msg->content->disposition = DISPINLINE;
1202 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1203 && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1204 parameter_setval(&msg->content->parameter, "format", "flowed");
1205 if (option (OPTDELSP))
1206 parameter_setval(&msg->content->parameter, "delsp", "yes");
1210 char buffer[_POSIX_PATH_MAX];
1211 tempfp = m_tempfile(buffer, sizeof(buffer), NONULL(MCore.tmpdir), NULL);
1212 msg->content->filename = m_strdup(buffer);
1214 tempfp = safe_fopen(tempfile, "a+");
1215 msg->content->filename = m_strdup(tempfile);
1219 mutt_perror (msg->content->filename);
1224 /* this is handled here so that the user can match ~f in send-hook */
1225 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1226 /* we shouldn't have to worry about freeing `msg->env->from' before
1227 * setting it here since this code will only execute when doing some
1228 * sort of reply. the pointer will only be set when using the -H command
1231 * We shouldn't have to worry about alias expansion here since we are
1232 * either replying to a real or postponed message, therefore no aliases
1233 * should exist since the user has not had the opportunity to add
1234 * addresses to the list. We just have to ensure the postponed messages
1235 * have their aliases expanded.
1238 msg->env->from = set_reverse_name (cur->env);
1241 if (!msg->env->from && option (OPTUSEFROM)
1242 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1243 msg->env->from = mutt_default_from ();
1245 if (flags & SENDBATCH) {
1246 mutt_copy_stream (stdin, tempfp);
1247 if (option (OPTHDRS)) {
1248 process_user_recips (msg->env);
1249 process_user_header (msg->env);
1251 mutt_expand_aliases_env (msg->env);
1253 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1254 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1255 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1258 if (option (OPTHDRS))
1259 process_user_recips (msg->env);
1261 /* Expand aliases and remove duplicates/crossrefs */
1262 mutt_fix_reply_recipients (msg->env);
1265 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1266 && !msg->env->newsgroups)
1267 msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
1270 if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1271 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1272 if (edit_envelope (msg->env, flags) == -1)
1276 /* the from address must be set here regardless of whether or not
1277 * $use_from is set so that the `~P' (from you) operator in send-hook
1278 * patterns will work. if $use_from is unset, the from address is killed
1279 * after send-hooks are evaulated */
1281 if (!msg->env->from) {
1282 msg->env->from = mutt_default_from ();
1286 if ((flags & SENDREPLY) && cur) {
1287 /* change setting based upon message we are replying to */
1288 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1291 * set the replied flag for the message we are generating so that the
1292 * user can use ~Q in a send-hook to know when reply-hook's are also
1298 /* change settings based upon recipients */
1300 mutt_message_hook (NULL, msg, M_SENDHOOK);
1303 * Unset the replied flag from the message we are composing since it is
1304 * no longer required. This is done here because the FCC'd copy of
1305 * this message was erroneously get the 'R'eplied flag when stored in
1306 * a maildir-style mailbox.
1311 address_list_wipe(&msg->env->from);
1315 if (option (OPTHDRS))
1316 process_user_header (msg->env);
1318 /* include replies/forwarded messages, unless we are given a template */
1319 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1320 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1323 append_signature (tempfp);
1326 * this wants to be done _after_ generate_body, so message-hooks
1330 if (mod_crypt.autosign)
1331 msg->security |= SIGN;
1332 if (mod_crypt.autoencrypt)
1333 msg->security |= ENCRYPT;
1334 if (mod_crypt.replyencrypt && cur && (cur->security & ENCRYPT))
1335 msg->security |= ENCRYPT;
1336 if (mod_crypt.replysign && cur && (cur->security & SIGN))
1337 msg->security |= SIGN;
1338 if (mod_crypt.replysignencrypted && cur && (cur->security & ENCRYPT))
1339 msg->security |= SIGN;
1341 if (msg->security) {
1343 * When reypling / forwarding, use the original message's
1344 * crypto system. According to the documentation,
1345 * smime_is_default should be disregarded here.
1347 * Problem: At least with forwarding, this doesn't really
1348 * make much sense. Should we have an option to completely
1349 * disable individual mechanisms at run-time?
1352 if (mod_crypt.autopgp && (cur->security & APPLICATION_PGP))
1353 msg->security |= APPLICATION_PGP;
1354 else if (mod_crypt.autosmime && (cur->security & APPLICATION_SMIME))
1355 msg->security |= APPLICATION_SMIME;
1359 * No crypto mechanism selected? Use availability + smime_is_default
1362 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1363 if (mod_crypt.autosmime && mod_crypt.smime_is_default)
1364 msg->security |= APPLICATION_SMIME;
1365 else if (mod_crypt.autopgp)
1366 msg->security |= APPLICATION_PGP;
1367 else if (mod_crypt.autosmime)
1368 msg->security |= APPLICATION_SMIME;
1372 /* No permissible mechanisms found. Don't sign or encrypt. */
1373 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1378 * This hook is even called for postponed messages, and can, e.g., be
1379 * used for setting the editor, the sendmail path, or the
1382 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1384 /* wait until now to set the real name portion of our return address so
1385 that $realname can be set in a send-hook */
1386 if (msg->env->from && !msg->env->from->personal
1387 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1388 msg->env->from->personal = m_strdup(Realname);
1392 if (!(flags & SENDBATCH)) {
1394 time_t mtime = m_decrease_mtime(msg->content->filename, NULL);
1396 mutt_update_encoding (msg->content);
1399 * Select whether or not the user's editor should be called now. We
1400 * don't want to do this when:
1401 * 1) we are sending a key/cert
1402 * 2) we are forwarding a message and the user doesn't want to edit it.
1403 * This is controled by the quadoption $forward_edit. However, if
1404 * both $edit_headers and $autoedit are set, we want to ignore the
1405 * setting of $forward_edit because the user probably needs to add the
1408 if (((flags & SENDFORWARD) == 0 ||
1409 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1410 query_quadoption (OPT_FORWEDIT,
1411 _("Edit forwarded message?")) == M_YES))
1413 /* If the this isn't a text message, look for a mailcap edit command */
1414 if (rfc1524_mailcap_isneeded(msg->content)) {
1415 if (!mutt_edit_attachment (msg->content))
1417 } else if (option (OPTEDITHDRS)) {
1418 mutt_env_to_local (msg->env);
1419 mutt_edit_headers(msg->content->filename, msg, fcc, sizeof (fcc));
1420 mutt_env_to_idna (msg->env, NULL, NULL);
1423 mutt_edit_file(msg->content->filename);
1425 if (stat (msg->content->filename, &st) == 0) {
1426 if (mtime != st.st_mtime)
1427 fix_end_of_file (msg->content->filename);
1429 mutt_perror (msg->content->filename);
1432 if (option (OPTTEXTFLOWED))
1433 rfc3676_space_stuff (msg);
1435 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1438 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDRESEND))) {
1439 if (stat (msg->content->filename, &st) == 0) {
1440 /* if the file was not modified, bail out now */
1441 if (mtime == st.st_mtime && !msg->content->next &&
1442 query_quadoption (OPT_ABORT,
1443 _("Abort unmodified message?")) == M_YES) {
1444 mutt_message _("Aborted unmodified message.");
1450 mutt_perror (msg->content->filename);
1454 /* specify a default fcc. if we are in batchmode, only save a copy of
1455 * the message if the value of $copy is yes or ask-yes */
1457 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1458 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1459 /* set the default FCC */
1460 if (!msg->env->from) {
1461 msg->env->from = mutt_default_from ();
1462 killfrom = 1; /* no need to check $use_from because if the user specified
1463 a from address it would have already been set by now */
1465 mutt_select_fcc (fcc, sizeof (fcc), msg);
1467 address_list_wipe(&msg->env->from);
1473 mutt_update_encoding (msg->content);
1475 if (!(flags & SENDBATCH)) {
1478 fcc_error = 0; /* reset value since we may have failed before */
1479 mutt_pretty_mailbox (fcc);
1480 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1484 if (flags & SENDNEWS)
1485 mutt_message (_("Article not posted."));
1489 mutt_message _("Mail not sent.");
1493 /* postpone the message until later. */
1494 if (msg->content->next)
1495 msg->content = mutt_make_multipart (msg->content);
1498 * make sure the message is written to the right part of a maildir
1504 encode_descriptions (msg->content, 1);
1505 mutt_prepare_envelope (msg->env, 0);
1506 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1509 || mutt_write_fcc (NONULL (Postponed), msg,
1511 && (flags & SENDREPLY)) ? cur->env->
1512 message_id : NULL, 1, fcc) < 0) {
1513 msg->content = mutt_remove_multipart (msg->content);
1514 decode_descriptions (msg->content);
1515 mutt_unprepare_envelope (msg->env);
1518 mutt_update_num_postponed ();
1519 mutt_message _("Message postponed.");
1526 if (!(flags & SENDNEWS))
1528 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1529 if (!(flags & SENDBATCH)) {
1530 mutt_error _("No recipients are specified!");
1535 puts _("No recipients were specified.");
1541 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1542 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1544 if (!(flags & SENDBATCH))
1550 if (!msg->env->subject && !(flags & SENDBATCH) &&
1552 query_quadoption (OPT_SUBJECT,
1553 _("No subject, abort sending?"))) != M_NO) {
1554 /* if the abort is automatic, print an error message */
1555 if (quadoption (OPT_SUBJECT) == M_YES)
1556 mutt_error _("No subject specified.");
1561 if ((flags & SENDNEWS) && !msg->env->subject) {
1562 mutt_error _("No subject specified.");
1567 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1568 mutt_error _("No newsgroup specified.");
1574 if (msg->content->next)
1575 msg->content = mutt_make_multipart (msg->content);
1577 if (mutt_attach_check (msg) &&
1578 !msg->content->next &&
1579 query_quadoption (OPT_ATTACH,
1580 _("No attachments made but indicator found in text. "
1581 "Cancel sending?")) == M_YES) {
1582 if (quadoption (OPT_ATTACH) == M_YES) {
1583 mutt_message _("No attachments made but indicator found in text. "
1587 mutt_message (_("Mail not sent."));
1592 * Ok, we need to do it this way instead of handling all fcc stuff in
1593 * one place in order to avoid going to main_loop with encoded "env"
1594 * in case of error. Ugh.
1597 encode_descriptions (msg->content, 1);
1600 * Make sure that clear_content and free_clear_content are
1601 * properly initialized -- we may visit this particular place in
1602 * the code multiple times, including after a failed call to
1606 clear_content = NULL;
1607 free_clear_content = 0;
1609 if (msg->security) {
1610 /* save the decrypted attachments */
1611 clear_content = msg->content;
1613 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1614 mutt_protect (msg, pgpkeylist) == -1) {
1615 msg->content = mutt_remove_multipart (msg->content);
1617 p_delete(&pgpkeylist);
1619 decode_descriptions (msg->content);
1622 encode_descriptions (msg->content, 0);
1626 * at this point, msg->content is one of the following three things:
1627 * - multipart/signed. In this case, clear_content is a child.
1628 * - multipart/encrypted. In this case, clear_content exists
1630 * - application/pgp. In this case, clear_content exists independently.
1631 * - something else. In this case, it's the same as clear_content.
1634 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1636 if (clear_content && (msg->content != clear_content)
1637 && (msg->content->parts != clear_content))
1638 free_clear_content = 1;
1640 if (!option (OPTNOCURSES))
1641 mutt_message _("Sending message...");
1643 mutt_prepare_envelope (msg->env, 1);
1645 /* save a copy of the message, if necessary. */
1647 mutt_expand_path (fcc, sizeof (fcc));
1650 /* Don't save a copy when we are in batch-mode, and the FCC
1651 * folder is on an IMAP server: This would involve possibly lots
1652 * of user interaction, which is not available in batch mode.
1654 * Note: A patch to fix the problems with the use of IMAP servers
1655 * from non-curses mode is available from Brendan Cully. However,
1656 * I'd like to think a bit more about this before including it.
1659 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1662 if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1663 BODY *tmpbody = msg->content;
1664 BODY *save_sig = NULL;
1665 BODY *save_parts = NULL;
1667 if (msg->security && option (OPTFCCCLEAR))
1668 msg->content = clear_content;
1670 /* check to see if the user wants copies of all attachments */
1671 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1672 if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1673 m_strcmp(msg->content->subtype, "signed") == 0))
1675 if (clear_content->type == TYPEMULTIPART) {
1676 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1677 /* save initial signature and attachments */
1678 save_sig = msg->content->parts->next;
1679 save_parts = clear_content->parts->next;
1682 /* this means writing only the main part */
1683 msg->content = clear_content->parts;
1685 if (mutt_protect (msg, pgpkeylist) == -1) {
1686 /* we can't do much about it at this point, so
1687 * fallback to saving the whole thing to fcc
1689 msg->content = tmpbody;
1694 save_content = msg->content;
1698 msg->content = msg->content->parts;
1703 /* update received time so that when storing to a mbox-style folder
1704 * the From_ line contains the current time instead of when the
1705 * message was first postponed.
1707 msg->received = time (NULL);
1708 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1710 * Error writing FCC, we should abort sending.
1716 msg->content = tmpbody;
1719 /* cleanup the second signature structures */
1720 if (save_content->parts) {
1721 body_list_wipe(&save_content->parts->next);
1722 save_content->parts = NULL;
1724 body_list_wipe(&save_content);
1726 /* restore old signature and attachments */
1727 msg->content->parts->next = save_sig;
1728 msg->content->parts->parts->next = save_parts;
1730 else if (save_content) {
1731 /* destroy the new encrypted body. */
1732 body_list_wipe(&save_content);
1739 * Don't attempt to send the message if the FCC failed. Just pretend
1740 * the send failed as well so we give the user a chance to fix the
1743 if (fcc_error || (i = send_message (msg)) == -1) {
1744 if (!(flags & SENDBATCH)) {
1745 if ((msg->security & ENCRYPT)
1746 || ((msg->security & SIGN)
1747 && msg->content->type == TYPEAPPLICATION)) {
1748 body_list_wipe(&msg->content); /* destroy PGP data */
1749 msg->content = clear_content; /* restore clear text. */
1751 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1752 body_list_wipe(&msg->content->parts->next); /* destroy sig */
1753 msg->content = mutt_remove_multipart (msg->content);
1756 msg->content = mutt_remove_multipart (msg->content);
1757 decode_descriptions (msg->content);
1758 mutt_unprepare_envelope (msg->env);
1762 puts _("Could not send the message.");
1767 else if (!option (OPTNOCURSES))
1768 mutt_message (i != 0 ? _("Sending in background.") :
1770 (flags & SENDNEWS) ? _("Article posted.") :
1776 if (msg->security & ENCRYPT)
1777 p_delete(&pgpkeylist);
1779 if (free_clear_content)
1780 body_list_wipe(&clear_content);
1782 if (flags & SENDREPLY) {
1784 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1785 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1786 for (i = 0; i < ctx->vcount; i++)
1787 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1788 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1797 if (flags & SENDPOSTPONED) {
1799 p_delete(&PgpSignAs);
1805 header_delete(&msg);
1810 /* vim: set sw=2: */