2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/file.h>
20 #include <lib-mime/mime.h>
22 #include <lib-ui/curses.h>
23 #include <lib-ui/enter.h>
30 #include <lib-crypt/crypt.h>
31 #include "mutt_idna.h"
35 #include "lib/debug.h"
46 #include <sys/types.h>
58 static void append_signature (FILE * f)
64 fprintf (f, "\n%s", SignOffString);
67 if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
68 if (option (OPTSIGDASHES))
70 else if (SignOffString)
72 mutt_copy_stream (tmpfp, f);
75 mutt_wait_filter (thepid);
79 /* compare two e-mail addresses and return 1 if they are equivalent */
80 static int mutt_addrcmp (address_t * a, address_t * b)
82 if (!a->mailbox || !b->mailbox)
84 if (ascii_strcasecmp (a->mailbox, b->mailbox))
89 /* search an e-mail address in a list */
90 static int mutt_addrsrc (address_t * a, address_t * lst)
92 for (; lst; lst = lst->next) {
93 if (mutt_addrcmp (a, lst))
99 /* removes addresses from "b" which are contained in "a" */
100 static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
102 address_t *top, *p, *prev = NULL;
106 for (p = a; p; p = p->next) {
107 if (mutt_addrcmp (p, b))
112 prev->next = b->next;
132 /* remove any address which matches the current user. if `leave_only' is
133 * nonzero, don't remove the user's address if it is the only one in the list
135 static address_t *remove_user (address_t * a, int leave_only)
137 address_t *top = NULL, *last = NULL;
140 if (!mutt_addr_is_user (a)) {
154 if (!leave_only || a || last) {
156 address_delete (&tmp);
165 static address_t *find_mailing_lists (address_t * t, address_t * c)
167 address_t *top = NULL, *ptr = NULL;
169 for (; t || c; t = c, c = NULL) {
170 for (; t; t = t->next) {
171 if (mutt_is_mail_list (t) && !t->group) {
173 ptr->next = address_dup (t);
177 ptr = top = address_dup (t);
184 static int edit_address (address_t ** a, const char *field)
186 char buf[HUGE_STRING];
192 mutt_addrlist_to_local (*a);
193 rfc822_write_address (buf, sizeof (buf), *a, 0);
194 if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
197 *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
198 if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
199 mutt_error (_("Error: '%s' is a bad IDN."), err);
205 while (idna_ok != 0);
209 static int edit_envelope (ENVELOPE * en, int flags)
211 char buf[HUGE_STRING];
212 LIST *uh = UserHeader;
213 regmatch_t pat_match[1];
216 if (option (OPTNEWSSEND)) {
218 m_strcpy(buf, sizeof(buf), en->newsgroups);
221 if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
223 p_delete(&en->newsgroups);
224 en->newsgroups = m_strdup(buf);
227 m_strcpy(buf, sizeof(buf), en->followup_to);
230 if (option (OPTASKFOLLOWUP)
231 && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
233 p_delete(&en->followup_to);
234 en->followup_to = m_strdup(buf);
236 if (en->x_comment_to)
237 m_strcpy(buf, sizeof(buf), en->x_comment_to);
240 if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
241 && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
243 p_delete(&en->x_comment_to);
244 en->x_comment_to = m_strdup(buf);
249 if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
251 if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
253 if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
258 if (option (OPTFASTREPLY))
261 m_strcpy(buf, sizeof(buf), en->subject);
267 for (; uh; uh = uh->next) {
268 if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
269 p = vskipspaces(uh->data + 8);
270 m_strcpy(buf, sizeof(buf), p);
275 if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
276 regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
277 unsigned int pos = pat_match->rm_so;
279 if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
280 buf[pos] = '\0'; /* kill match */
281 while (pos-- && buf[pos] == ' ')
282 buf[pos] = '\0'; /* remove trailing spaces */
285 mutt_error (_("Ignoring $strip_was: Subject would be empty."));
289 if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
294 ("No subject, abort?"))
296 mutt_message _("No subject, aborting.");
300 m_strreplace(&en->subject, buf);
306 char *nntp_get_header(const char *s)
308 return m_strdup(skipspaces(s));
312 static void process_user_recips (ENVELOPE * env)
314 LIST *uh = UserHeader;
316 for (; uh; uh = uh->next) {
317 if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
318 env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
319 else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
320 env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
321 else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
322 env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
324 else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
325 env->newsgroups = nntp_get_header (uh->data + 11);
326 else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
327 env->followup_to = nntp_get_header (uh->data + 12);
328 else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
329 env->x_comment_to = nntp_get_header (uh->data + 13);
334 static void process_user_header (ENVELOPE * env)
336 LIST *uh = UserHeader;
337 LIST *last = env->userhdrs;
343 for (; uh; uh = uh->next) {
344 if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
345 /* User has specified a default From: address. Remove default address */
346 address_delete (&env->from);
347 env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
349 else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
350 address_delete (&env->reply_to);
351 env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
353 else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
354 m_strreplace(&env->message_id, uh->data + 11);
355 else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
356 ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
357 ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
359 ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
360 ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
361 ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
363 ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
364 ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
366 last->next = mutt_new_list ();
370 last = env->userhdrs = mutt_new_list ();
371 last->data = m_strdup(uh->data);
376 void mutt_forward_intro (FILE * fp, HEADER * cur)
380 fputs ("----- Forwarded message from ", fp);
382 rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
384 fputs (" -----\n\n", fp);
387 void mutt_forward_trailer (FILE * fp)
389 fputs ("\n----- End forwarded message -----\n", fp);
393 static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
395 int chflags = CH_DECODE, cmflags = 0;
397 mutt_parse_mime_message (ctx, cur);
398 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
400 if ((cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
401 /* make sure we have the user's passphrase before proceeding... */
402 crypt_valid_passphrase (cur->security);
405 mutt_forward_intro (out, cur);
407 if (option (OPTFORWDECODE)) {
408 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
409 if (option (OPTWEED)) {
410 chflags |= CH_WEED | CH_REORDER;
411 cmflags |= M_CM_WEED;
414 if (option (OPTFORWQUOTE))
415 cmflags |= M_CM_PREFIX;
417 mutt_copy_message (out, ctx, cur, cmflags, chflags);
418 mutt_forward_trailer (out);
422 void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
427 mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
433 void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
437 if (PostIndentString) {
438 mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
444 static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
446 int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
447 int chflags = CH_DECODE;
449 if ((cur->security & ENCRYPT)) {
450 /* make sure we have the user's passphrase before proceeding... */
451 crypt_valid_passphrase (cur->security);
454 mutt_parse_mime_message (ctx, cur);
455 mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
457 mutt_make_attribution (ctx, cur, out);
459 if (!option (OPTHEADER))
460 cmflags |= M_CM_NOHEADER;
461 if (option (OPTWEED)) {
462 chflags |= CH_WEED | CH_REORDER;
463 cmflags |= M_CM_WEED;
466 mutt_copy_message (out, ctx, cur, cmflags, chflags);
468 mutt_make_post_indent (ctx, cur, out);
473 static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
477 if (flags && env->mail_followup_to && hmfupto == M_YES) {
478 address_list_append(to, address_list_dup(env->mail_followup_to));
482 /* Exit now if we're setting up the default Cc list for list-reply
483 * (only set if Mail-Followup-To is present and honoured).
485 if (flags & SENDLISTREPLY)
488 /* If this message came from a mailing list, ask the user if he really
489 * intended to reply to the author only.
491 if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
492 switch (query_quadoption (OPT_LISTREPLY,
493 _("Message came from a mailing list. List-reply to mailing list?")))
496 address_list_append(to, find_mailing_lists (env->to, env->cc));
499 return -1; /* abort */
503 if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
504 /* mail is from the user, assume replying to recipients */
505 address_list_append(to, address_list_dup(env->to));
507 else if (env->reply_to) {
508 if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
509 (option (OPTIGNORELISTREPLYTO) &&
510 mutt_is_mail_list (env->reply_to) &&
511 (mutt_addrsrc (env->reply_to, env->to) ||
512 mutt_addrsrc (env->reply_to, env->cc)))) {
513 /* If the Reply-To: address is a mailing list, assume that it was
514 * put there by the mailing list, and use the From: address
516 * We also take the from header if our correspondant has a reply-to
517 * header which is identical to the electronic mail address given
518 * in his From header.
521 address_list_append(to, address_list_dup(env->from));
523 else if (!(mutt_addrcmp (env->from, env->reply_to) &&
524 !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
525 /* There are quite a few mailing lists which set the Reply-To:
526 * header field to the list address, which makes it quite impossible
527 * to send a message to only the sender of the message. This
528 * provides a way to do that.
530 snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
531 env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
532 switch (query_quadoption (OPT_REPLYTO, prompt)) {
534 address_list_append(to, address_list_dup(env->reply_to));
538 address_list_append(to, address_list_dup(env->from));
542 return (-1); /* abort */
546 address_list_append(to, address_list_dup(env->reply_to));
549 address_list_append(to, address_list_dup(env->from));
554 int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
559 if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
560 snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
561 in->mail_followup_to->mailbox,
562 in->mail_followup_to->next ? ",..." : "");
564 if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
568 if (flags & SENDLISTREPLY) {
569 address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
571 if (in->mail_followup_to && hmfupto == M_YES &&
572 default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
573 return (-1); /* abort */
576 if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
577 return (-1); /* abort */
579 if ((flags & SENDGROUPREPLY)
580 && (!in->mail_followup_to || hmfupto != M_YES))
582 address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
583 address_list_append(tmp, address_list_dup(in->cc));
589 LIST *mutt_make_references (ENVELOPE * e)
591 LIST *t = NULL, *l = NULL;
594 l = mutt_copy_list (e->references);
596 l = mutt_copy_list (e->in_reply_to);
599 t = mutt_new_list ();
600 t->data = m_strdup(e->message_id);
608 void mutt_fix_reply_recipients (ENVELOPE * env)
610 mutt_expand_aliases_env (env);
612 if (!option (OPTMETOO)) {
613 /* the order is important here. do the CC: first so that if the
614 * the user is the only recipient, it ends up on the TO: field
616 env->cc = remove_user (env->cc, (env->to == NULL));
617 env->to = remove_user (env->to, (env->cc == NULL));
620 /* the CC field can get cluttered, especially with lists */
621 env->to = mutt_remove_duplicates (env->to);
622 env->cc = mutt_remove_duplicates (env->cc);
623 env->cc = mutt_remove_xrefs (env->to, env->cc);
625 if (env->cc && !env->to) {
631 void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
635 /* set the default subject for the message. */
636 mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
637 m_strreplace(&env->subject, buffer);
640 void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
641 HEADER * cur, ENVELOPE * curenv)
643 /* This takes precedence over a subject that might have
644 * been taken from a List-Post header. Is that correct?
646 if (curenv->real_subj) {
647 p_delete(&env->subject);
648 env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
649 sprintf (env->subject, "Re: %s", curenv->real_subj); /* __SPRINTF_CHECKED__ */
651 else if (!env->subject)
652 env->subject = m_strdup("Re: your mail");
655 if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
656 env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
660 void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
661 LIST *** pp, LIST *** qq)
663 LIST **p = NULL, **q = NULL;
671 p = &env->references;
673 q = &env->in_reply_to;
680 *p = mutt_make_references (curenv);
682 if (curenv->message_id) {
683 *q = mutt_new_list ();
684 (*q)->data = m_strdup(curenv->message_id);
695 mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
697 env->references = NULL;
698 env->in_reply_to = NULL;
702 LIST **p = NULL, **q = NULL;
705 for (i = 0; i < ctx->vcount; i++) {
706 h = ctx->hdrs[ctx->v2r[i]];
708 mutt_add_to_reference_headers (env, h->env, &p, &q);
712 mutt_add_to_reference_headers (env, curenv, NULL, NULL);
716 envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
718 ENVELOPE *curenv = NULL;
723 for (i = 0; i < ctx->vcount; i++)
724 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
725 cur = ctx->hdrs[ctx->v2r[i]];
731 /* This could happen if the user tagged some messages and then did
732 * a limit such that none of the tagged message are visible.
734 mutt_error _("No tagged messages are visible!");
742 if (flags & SENDREPLY) {
744 if ((flags & SENDNEWS)) {
745 /* in case followup set Newsgroups: with Followup-To: if it present */
746 if (!env->newsgroups && curenv &&
747 m_strcasecmp(curenv->followup_to, "poster"))
748 env->newsgroups = m_strdup(curenv->followup_to);
755 for (i = 0; i < ctx->vcount; i++) {
756 h = ctx->hdrs[ctx->v2r[i]];
757 if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
761 else if (mutt_fetch_recips (env, curenv, flags) == -1)
764 if ((flags & SENDLISTREPLY) && !env->to) {
765 mutt_error _("No mailing lists found!");
770 mutt_make_misc_reply_headers (env, ctx, cur, curenv);
771 mutt_make_reference_headers (tag ? NULL : curenv, env, ctx);
773 else if (flags & SENDFORWARD)
774 mutt_make_forward_subject (env, ctx, cur);
779 static int generate_body (FILE * tempfp, /* stream for outgoing message */
780 HEADER * msg, /* header for outgoing message */
781 int flags, /* compose mode */
782 CONTEXT * ctx, /* current mailbox */
784 { /* current message */
789 if (flags & SENDREPLY) {
791 query_quadoption (OPT_INCLUDE,
792 _("Include message in reply?"))) == -1)
796 mutt_message _("Including quoted message...");
799 for (i = 0; i < ctx->vcount; i++) {
800 h = ctx->hdrs[ctx->v2r[i]];
802 if (include_reply (ctx, h, tempfp) == -1) {
803 mutt_error _("Could not include all requested messages!");
807 fputc ('\n', tempfp);
812 include_reply (ctx, cur, tempfp);
816 else if (flags & SENDFORWARD) {
818 query_quadoption (OPT_MIMEFWD,
819 _("Forward as attachment?"))) == M_YES) {
820 BODY *last = msg->content;
822 mutt_message _("Preparing forwarded message...");
824 while (last && last->next)
828 tmp = mutt_make_message_attach (ctx, cur, 0);
835 for (i = 0; i < ctx->vcount; i++) {
836 if (ctx->hdrs[ctx->v2r[i]]->tagged) {
837 tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
843 last = msg->content = tmp;
850 include_forward (ctx, cur, tempfp);
852 for (i = 0; i < ctx->vcount; i++)
853 if (ctx->hdrs[ctx->v2r[i]]->tagged)
854 include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
859 else if (flags & SENDKEY) {
862 if ((tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
865 tmp->next = msg->content;
874 void mutt_set_followup_to (ENVELOPE * e)
879 * Only generate the Mail-Followup-To if the user has requested it, and
880 * it hasn't already been set
883 if (!option (OPTFOLLOWUPTO))
886 if (option (OPTNEWSSEND)) {
887 if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
888 e->followup_to = m_strdup(e->newsgroups);
893 if (!e->mail_followup_to) {
894 if (mutt_is_list_cc (0, e->to, e->cc)) {
897 * this message goes to known mailing lists, so create a proper
898 * mail-followup-to header
901 tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
902 address_list_append(tmp, address_list_dup(e->cc));
905 /* remove ourselves from the mail-followup-to header */
906 e->mail_followup_to = remove_user (e->mail_followup_to, 0);
909 * If we are not subscribed to any of the lists in question,
910 * re-add ourselves to the mail-followup-to header. The
911 * mail-followup-to header generated is a no-op with group-reply,
912 * but makes sure list-reply has the desired effect.
915 if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
917 from = address_list_dup (e->reply_to);
919 from = address_list_dup (e->from);
921 from = mutt_default_from ();
924 address_list_append(&from->next, e->mail_followup_to);
925 e->mail_followup_to = from;
929 e->mail_followup_to = mutt_remove_duplicates(e->mail_followup_to);
935 /* look through the recipients of the message we are replying to, and if
936 we find an address that matches $alternates, we use that as the default
938 static address_t *set_reverse_name (ENVELOPE * env)
942 for (tmp = env->to; tmp; tmp = tmp->next) {
943 if (mutt_addr_is_user (tmp))
947 for (tmp = env->cc; tmp; tmp = tmp->next) {
948 if (mutt_addr_is_user (tmp))
952 if (!tmp && mutt_addr_is_user (env->from))
955 tmp = address_dup (tmp);
956 if (!option (OPTREVREAL))
957 p_delete(&tmp->personal);
959 tmp->personal = m_strdup(Realname);
964 address_t *mutt_default_from (void)
967 const char *fqdn = mutt_fqdn (1);
970 * Note: We let $from override $realname here. Is this the right
975 adr = address_dup (From);
976 else if (option (OPTUSEDOMAIN)) {
977 adr = address_new ();
978 adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
979 sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn)); /* __SPRINTF_CHECKED__ */
982 adr = address_new ();
983 adr->mailbox = m_strdup(NONULL (Username));
989 static int send_message (HEADER * msg)
991 char tempfile[_POSIX_PATH_MAX];
995 /* Write out the message in MIME form. */
996 mutt_mktemp (tempfile);
997 if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
1001 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
1002 msg->chain ? 1 : 0);
1005 mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
1008 fputc ('\n', tempfp); /* tie off the header. */
1010 if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
1016 if (fclose (tempfp) != 0) {
1017 mutt_perror (tempfile);
1024 return mix_send_message (msg->chain, tempfile);
1027 i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
1028 msg->env->bcc, tempfile,
1029 (msg->content->encoding == ENC8BIT));
1033 /* rfc2047 encode the content-descriptions */
1034 static void encode_descriptions (BODY * b, short recurse)
1038 for (t = b; t; t = t->next) {
1039 if (t->description) {
1040 rfc2047_encode_string (&t->description);
1042 if (recurse && t->parts)
1043 encode_descriptions (t->parts, recurse);
1047 /* rfc2047 decode them in case of an error */
1048 static void decode_descriptions (BODY * b)
1052 for (t = b; t; t = t->next) {
1053 if (t->description) {
1054 rfc2047_decode (&t->description);
1057 decode_descriptions (t->parts);
1061 static void fix_end_of_file (const char *data)
1066 if ((fp = safe_fopen (data, "a+")) == NULL)
1068 fseeko (fp, -1, SEEK_END);
1069 if ((c = fgetc (fp)) != '\n')
1074 int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
1076 HEADER *msg = header_new();
1078 if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
1081 return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
1084 int ci_send_message (int flags, /* send mode */
1085 HEADER * msg, /* template to use for new message */
1086 char *tempfile, /* file specified by -i or -H */
1087 CONTEXT * ctx, /* current mailbox */
1089 { /* current message */
1090 char buffer[LONG_STRING];
1091 char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
1092 FILE *tempfp = NULL;
1094 int i, killfrom = 0;
1096 int free_clear_content = 0;
1098 BODY *save_content = NULL;
1099 BODY *clear_content = NULL;
1100 char *pgpkeylist = NULL;
1102 /* save current value of "pgp_sign_as" */
1103 char *signas = NULL, *err = NULL;
1104 const char *tag = NULL;
1110 if (flags & SENDNEWS)
1111 set_option (OPTNEWSSEND);
1113 unset_option (OPTNEWSSEND);
1116 if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
1117 mutt_num_postponed (1)) {
1118 /* If the user is composing a new message, check to see if there
1119 * are any postponed messages first.
1122 query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
1126 flags |= SENDPOSTPONED;
1130 if (flags & SENDPOSTPONED)
1131 signas = m_strdup(PgpSignAs);
1133 /* Delay expansion of aliases until absolutely necessary--shouldn't
1134 * be necessary unless we are prompting the user or about to execute a
1141 if (flags == SENDPOSTPONED) {
1143 mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
1147 * If postponed message is a news article, it have
1148 * a "Newsgroups:" header line, then set appropriate flag.
1150 if (msg->env->newsgroups) {
1152 set_option (OPTNEWSSEND);
1156 unset_option (OPTNEWSSEND);
1161 if (flags & (SENDPOSTPONED | SENDRESEND)) {
1162 if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
1163 mutt_perror (msg->content->filename);
1169 msg->env = envelope_new();
1172 /* Parse and use an eventual list-post header */
1173 if ((flags & SENDLISTREPLY)
1174 && cur && cur->env && cur->env->list_post) {
1175 /* Use any list-post header as a template */
1176 url_parse_mailto (msg->env, NULL, cur->env->list_post);
1177 /* We don't let them set the sender's address. */
1178 address_delete (&msg->env->from);
1181 if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
1182 pbody = mutt_new_body ();
1183 pbody->next = msg->content; /* don't kill command-line attachments */
1184 msg->content = pbody;
1186 if (!(ctype = m_strdup(ContentType)))
1187 ctype = m_strdup("text/plain");
1188 mutt_parse_content_type (ctype, msg->content);
1191 msg->content->unlink = 1;
1192 msg->content->use_disp = 0;
1193 msg->content->disposition = DISPINLINE;
1194 if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
1195 && !ascii_strcasecmp (msg->content->subtype, "plain")) {
1196 mutt_set_parameter ("format", "flowed", &msg->content->parameter);
1197 if (option (OPTDELSP))
1198 mutt_set_parameter ("delsp", "yes", &msg->content->parameter);
1202 mutt_mktemp (buffer);
1203 tempfp = safe_fopen (buffer, "w+");
1204 msg->content->filename = m_strdup(buffer);
1207 tempfp = safe_fopen (tempfile, "a+");
1208 msg->content->filename = m_strdup(tempfile);
1212 debug_print (1, ("can't create tempfile %s (errno=%d)\n",
1213 msg->content->filename, errno));
1214 mutt_perror (msg->content->filename);
1219 /* this is handled here so that the user can match ~f in send-hook */
1220 if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
1221 /* we shouldn't have to worry about freeing `msg->env->from' before
1222 * setting it here since this code will only execute when doing some
1223 * sort of reply. the pointer will only be set when using the -H command
1226 * We shouldn't have to worry about alias expansion here since we are
1227 * either replying to a real or postponed message, therefore no aliases
1228 * should exist since the user has not had the opportunity to add
1229 * addresses to the list. We just have to ensure the postponed messages
1230 * have their aliases expanded.
1233 msg->env->from = set_reverse_name (cur->env);
1236 if (!msg->env->from && option (OPTUSEFROM)
1237 && !(flags & (SENDPOSTPONED | SENDRESEND)))
1238 msg->env->from = mutt_default_from ();
1240 if (flags & SENDBATCH) {
1241 mutt_copy_stream (stdin, tempfp);
1242 if (option (OPTHDRS)) {
1243 process_user_recips (msg->env);
1244 process_user_header (msg->env);
1246 mutt_expand_aliases_env (msg->env);
1248 else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
1249 if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
1250 envelope_defaults (msg->env, ctx, cur, flags) == -1)
1253 if (option (OPTHDRS))
1254 process_user_recips (msg->env);
1256 /* Expand aliases and remove duplicates/crossrefs */
1257 mutt_fix_reply_recipients (msg->env);
1260 if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
1261 && !msg->env->newsgroups)
1262 msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
1265 if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
1266 !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
1267 if (edit_envelope (msg->env, flags) == -1)
1271 /* the from address must be set here regardless of whether or not
1272 * $use_from is set so that the `~P' (from you) operator in send-hook
1273 * patterns will work. if $use_from is unset, the from address is killed
1274 * after send-hooks are evaulated */
1276 if (!msg->env->from) {
1277 msg->env->from = mutt_default_from ();
1281 if ((flags & SENDREPLY) && cur) {
1282 /* change setting based upon message we are replying to */
1283 mutt_message_hook (ctx, cur, M_REPLYHOOK);
1286 * set the replied flag for the message we are generating so that the
1287 * user can use ~Q in a send-hook to know when reply-hook's are also
1293 /* change settings based upon recipients */
1295 mutt_message_hook (NULL, msg, M_SENDHOOK);
1298 * Unset the replied flag from the message we are composing since it is
1299 * no longer required. This is done here because the FCC'd copy of
1300 * this message was erroneously get the 'R'eplied flag when stored in
1301 * a maildir-style mailbox.
1306 address_delete (&msg->env->from);
1310 if (option (OPTHDRS))
1311 process_user_header (msg->env);
1314 if (option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1315 append_signature (tempfp);
1317 /* include replies/forwarded messages, unless we are given a template */
1318 if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
1319 && generate_body (tempfp, msg, flags, ctx, cur) == -1)
1322 if (!option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
1323 append_signature (tempfp);
1326 * this wants to be done _after_ generate_body, so message-hooks
1330 if (option (OPTCRYPTAUTOSIGN))
1331 msg->security |= SIGN;
1332 if (option (OPTCRYPTAUTOENCRYPT))
1333 msg->security |= ENCRYPT;
1334 if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
1335 msg->security |= ENCRYPT;
1336 if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
1337 msg->security |= SIGN;
1338 if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
1339 && (cur->security & ENCRYPT))
1340 msg->security |= SIGN;
1341 if (msg->security & (ENCRYPT | SIGN)) {
1342 if (option (OPTPGPAUTOINLINE))
1343 msg->security |= INLINE;
1344 if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
1345 msg->security |= INLINE;
1348 if (msg->security) {
1350 * When reypling / forwarding, use the original message's
1351 * crypto system. According to the documentation,
1352 * smime_is_default should be disregarded here.
1354 * Problem: At least with forwarding, this doesn't really
1355 * make much sense. Should we have an option to completely
1356 * disable individual mechanisms at run-time?
1359 if (option (OPTCRYPTAUTOPGP) && (cur->security & APPLICATION_PGP))
1360 msg->security |= APPLICATION_PGP;
1361 else if (option (OPTCRYPTAUTOSMIME)
1362 && (cur->security & APPLICATION_SMIME))
1363 msg->security |= APPLICATION_SMIME;
1367 * No crypto mechanism selected? Use availability + smime_is_default
1370 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
1371 if (option (OPTCRYPTAUTOSMIME) && option (OPTSMIMEISDEFAULT))
1372 msg->security |= APPLICATION_SMIME;
1373 else if (option (OPTCRYPTAUTOPGP))
1374 msg->security |= APPLICATION_PGP;
1375 else if (option (OPTCRYPTAUTOSMIME))
1376 msg->security |= APPLICATION_SMIME;
1380 /* No permissible mechanisms found. Don't sign or encrypt. */
1381 if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
1386 * This hook is even called for postponed messages, and can, e.g., be
1387 * used for setting the editor, the sendmail path, or the
1390 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1392 /* wait until now to set the real name portion of our return address so
1393 that $realname can be set in a send-hook */
1394 if (msg->env->from && !msg->env->from->personal
1395 && !(flags & (SENDRESEND | SENDPOSTPONED)))
1396 msg->env->from->personal = m_strdup(Realname);
1398 if (!(flags & SENDKEY))
1399 safe_fclose (&tempfp);
1401 if (!(flags & SENDBATCH)) {
1403 time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
1405 mutt_update_encoding (msg->content);
1408 * Select whether or not the user's editor should be called now. We
1409 * don't want to do this when:
1410 * 1) we are sending a key/cert
1411 * 2) we are forwarding a message and the user doesn't want to edit it.
1412 * This is controled by the quadoption $forward_edit. However, if
1413 * both $edit_headers and $autoedit are set, we want to ignore the
1414 * setting of $forward_edit because the user probably needs to add the
1417 if (!(flags & SENDKEY) &&
1418 ((flags & SENDFORWARD) == 0 ||
1419 (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
1420 query_quadoption (OPT_FORWEDIT,
1421 _("Edit forwarded message?")) == M_YES)) {
1422 /* If the this isn't a text message, look for a mailcap edit command */
1423 if (mutt_needs_mailcap (msg->content)) {
1424 if (!mutt_edit_attachment (msg->content))
1426 } else if (option (OPTEDITHDRS)) {
1427 mutt_env_to_local (msg->env);
1428 mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
1430 mutt_env_to_idna (msg->env, NULL, NULL);
1433 mutt_edit_file (Editor, msg->content->filename);
1435 if (stat (msg->content->filename, &st) == 0) {
1436 if (mtime != st.st_mtime)
1437 fix_end_of_file (msg->content->filename);
1439 mutt_perror (msg->content->filename);
1442 if (option (OPTTEXTFLOWED))
1443 rfc3676_space_stuff (msg);
1445 mutt_message_hook (NULL, msg, M_SEND2HOOK);
1448 if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
1449 if (stat (msg->content->filename, &st) == 0) {
1450 /* if the file was not modified, bail out now */
1451 if (mtime == st.st_mtime && !msg->content->next &&
1452 query_quadoption (OPT_ABORT,
1453 _("Abort unmodified message?")) == M_YES) {
1454 mutt_message _("Aborted unmodified message.");
1460 mutt_perror (msg->content->filename);
1464 /* specify a default fcc. if we are in batchmode, only save a copy of
1465 * the message if the value of $copy is yes or ask-yes */
1467 if (!fcc[0] && !(flags & (SENDPOSTPONED))
1468 && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
1469 /* set the default FCC */
1470 if (!msg->env->from) {
1471 msg->env->from = mutt_default_from ();
1472 killfrom = 1; /* no need to check $use_from because if the user specified
1473 a from address it would have already been set by now */
1475 mutt_select_fcc (fcc, sizeof (fcc), msg);
1477 address_delete (&msg->env->from);
1483 mutt_update_encoding (msg->content);
1485 if (!(flags & SENDBATCH)) {
1488 fcc_error = 0; /* reset value since we may have failed before */
1489 mutt_pretty_mailbox (fcc);
1490 i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
1494 if (flags & SENDNEWS)
1495 mutt_message (_("Article not posted."));
1499 mutt_message _("Mail not sent.");
1503 /* postpone the message until later. */
1504 if (msg->content->next)
1505 msg->content = mutt_make_multipart (msg->content);
1508 * make sure the message is written to the right part of a maildir
1514 encode_descriptions (msg->content, 1);
1515 mutt_prepare_envelope (msg->env, 0);
1516 mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
1519 || mutt_write_fcc (NONULL (Postponed), msg,
1521 && (flags & SENDREPLY)) ? cur->env->
1522 message_id : NULL, 1, fcc) < 0) {
1523 msg->content = mutt_remove_multipart (msg->content);
1524 decode_descriptions (msg->content);
1525 mutt_unprepare_envelope (msg->env);
1528 mutt_update_num_postponed ();
1529 mutt_message _("Message postponed.");
1536 if (!(flags & SENDNEWS))
1538 if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
1539 if (!(flags & SENDBATCH)) {
1540 mutt_error _("No recipients are specified!");
1545 puts _("No recipients were specified.");
1551 if (mutt_env_to_idna (msg->env, &tag, &err)) {
1552 mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
1554 if (!(flags & SENDBATCH))
1560 if (!msg->env->subject && !(flags & SENDBATCH) &&
1562 query_quadoption (OPT_SUBJECT,
1563 _("No subject, abort sending?"))) != M_NO) {
1564 /* if the abort is automatic, print an error message */
1565 if (quadoption (OPT_SUBJECT) == M_YES)
1566 mutt_error _("No subject specified.");
1571 if ((flags & SENDNEWS) && !msg->env->subject) {
1572 mutt_error _("No subject specified.");
1577 if ((flags & SENDNEWS) && !msg->env->newsgroups) {
1578 mutt_error _("No newsgroup specified.");
1584 if (msg->content->next)
1585 msg->content = mutt_make_multipart (msg->content);
1587 if (mutt_attach_check (msg) &&
1588 !msg->content->next &&
1589 query_quadoption (OPT_ATTACH,
1590 _("No attachments made but indicator found in text. "
1591 "Cancel sending?")) == M_YES) {
1592 if (quadoption (OPT_ATTACH) == M_YES) {
1593 mutt_message _("No attachments made but indicator found in text. "
1597 mutt_message (_("Mail not sent."));
1602 * Ok, we need to do it this way instead of handling all fcc stuff in
1603 * one place in order to avoid going to main_loop with encoded "env"
1604 * in case of error. Ugh.
1607 encode_descriptions (msg->content, 1);
1610 * Make sure that clear_content and free_clear_content are
1611 * properly initialized -- we may visit this particular place in
1612 * the code multiple times, including after a failed call to
1616 clear_content = NULL;
1617 free_clear_content = 0;
1619 if (msg->security) {
1620 /* save the decrypted attachments */
1621 clear_content = msg->content;
1623 if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
1624 mutt_protect (msg, pgpkeylist) == -1) {
1625 msg->content = mutt_remove_multipart (msg->content);
1627 p_delete(&pgpkeylist);
1629 decode_descriptions (msg->content);
1632 encode_descriptions (msg->content, 0);
1636 * at this point, msg->content is one of the following three things:
1637 * - multipart/signed. In this case, clear_content is a child.
1638 * - multipart/encrypted. In this case, clear_content exists
1640 * - application/pgp. In this case, clear_content exists independently.
1641 * - something else. In this case, it's the same as clear_content.
1644 /* This is ugly -- lack of "reporting back" from mutt_protect(). */
1646 if (clear_content && (msg->content != clear_content)
1647 && (msg->content->parts != clear_content))
1648 free_clear_content = 1;
1650 if (!option (OPTNOCURSES))
1651 mutt_message _("Sending message...");
1653 mutt_prepare_envelope (msg->env, 1);
1655 /* save a copy of the message, if necessary. */
1657 mutt_expand_path (fcc, sizeof (fcc));
1660 /* Don't save a copy when we are in batch-mode, and the FCC
1661 * folder is on an IMAP server: This would involve possibly lots
1662 * of user interaction, which is not available in batch mode.
1664 * Note: A patch to fix the problems with the use of IMAP servers
1665 * from non-curses mode is available from Brendan Cully. However,
1666 * I'd like to think a bit more about this before including it.
1669 if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
1672 if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
1673 BODY *tmpbody = msg->content;
1674 BODY *save_sig = NULL;
1675 BODY *save_parts = NULL;
1677 if (msg->security && option (OPTFCCCLEAR))
1678 msg->content = clear_content;
1680 /* check to see if the user wants copies of all attachments */
1681 if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
1682 if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
1683 m_strcmp(msg->content->subtype, "signed") == 0))
1685 if (clear_content->type == TYPEMULTIPART) {
1686 if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
1687 /* save initial signature and attachments */
1688 save_sig = msg->content->parts->next;
1689 save_parts = clear_content->parts->next;
1692 /* this means writing only the main part */
1693 msg->content = clear_content->parts;
1695 if (mutt_protect (msg, pgpkeylist) == -1) {
1696 /* we can't do much about it at this point, so
1697 * fallback to saving the whole thing to fcc
1699 msg->content = tmpbody;
1704 save_content = msg->content;
1708 msg->content = msg->content->parts;
1713 /* update received time so that when storing to a mbox-style folder
1714 * the From_ line contains the current time instead of when the
1715 * message was first postponed.
1717 msg->received = time (NULL);
1718 if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
1720 * Error writing FCC, we should abort sending.
1726 msg->content = tmpbody;
1729 /* cleanup the second signature structures */
1730 if (save_content->parts) {
1731 mutt_free_body (&save_content->parts->next);
1732 save_content->parts = NULL;
1734 mutt_free_body (&save_content);
1736 /* restore old signature and attachments */
1737 msg->content->parts->next = save_sig;
1738 msg->content->parts->parts->next = save_parts;
1740 else if (save_content) {
1741 /* destroy the new encrypted body. */
1742 mutt_free_body (&save_content);
1749 * Don't attempt to send the message if the FCC failed. Just pretend
1750 * the send failed as well so we give the user a chance to fix the
1753 if (fcc_error || (i = send_message (msg)) == -1) {
1754 if (!(flags & SENDBATCH)) {
1755 if ((msg->security & ENCRYPT)
1756 || ((msg->security & SIGN)
1757 && msg->content->type == TYPEAPPLICATION)) {
1758 mutt_free_body (&msg->content); /* destroy PGP data */
1759 msg->content = clear_content; /* restore clear text. */
1761 else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
1762 mutt_free_body (&msg->content->parts->next); /* destroy sig */
1763 msg->content = mutt_remove_multipart (msg->content);
1766 msg->content = mutt_remove_multipart (msg->content);
1767 decode_descriptions (msg->content);
1768 mutt_unprepare_envelope (msg->env);
1772 puts _("Could not send the message.");
1777 else if (!option (OPTNOCURSES))
1778 mutt_message (i != 0 ? _("Sending in background.") :
1780 (flags & SENDNEWS) ? _("Article posted.") :
1786 if (msg->security & ENCRYPT)
1787 p_delete(&pgpkeylist);
1789 if (free_clear_content)
1790 mutt_free_body (&clear_content);
1792 if (flags & SENDREPLY) {
1794 mutt_set_flag (ctx, cur, M_REPLIED, 1);
1795 else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
1796 for (i = 0; i < ctx->vcount; i++)
1797 if (ctx->hdrs[ctx->v2r[i]]->tagged)
1798 mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
1807 if (flags & SENDPOSTPONED) {
1809 p_delete(&PgpSignAs);
1814 safe_fclose (&tempfp);
1815 header_delete(&msg);
1820 /* vim: set sw=2: */