and less size_t's
[apps/madmutt.git] / send.c
diff --git a/send.c b/send.c
index c687492..6821f01 100644 (file)
--- a/send.c
+++ b/send.c
@@ -1,37 +1,16 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
- * 
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- * 
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
- */ 
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
+ */
 
 #if HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include "mutt.h"
-#include "mutt_curses.h"
-#include "rfc2047.h"
-#include "keymap.h"
-#include "mime.h"
-#include "mailbox.h"
-#include "copy.h"
-#include "mx.h"
-#include "mutt_crypt.h"
-#include "mutt_idna.h"
-#include "url.h"
-
 #include <ctype.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <utime.h>
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/macros.h>
+#include <lib-lib/file.h>
+#include <lib-lib/debug.h>
+#include <lib-lib/url.h>
+
+#include <lib-mime/mime.h>
+
+#include <lib-ui/curses.h>
+#include <lib-ui/enter.h>
+
+#include "mutt.h"
+#include "rfc3676.h"
+#include "keymap.h"
+#include "copy.h"
+#include "mx.h"
+#include <lib-crypt/crypt.h>
+#include "mutt_idna.h"
+#include "attach.h"
+
 #ifdef USE_NNTP
 #include "nntp.h"
 #endif
 #endif
 
 
-static void append_signature (FILE *f)
+static void append_signature (FILE * f)
 {
   FILE *tmpfp;
   pid_t thepid;
 
   if (SignOffString) {
-    fprintf(f,"\n%s",SignOffString);
+    fprintf (f, "\n%s", SignOffString);
   }
 
-  if (Signature && (tmpfp = mutt_open_read (Signature, &thepid)))
-  {
+  if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
     if (option (OPTSIGDASHES))
       fputs ("\n-- \n", f);
     else if (SignOffString)
-      fputs("\n",f);
+      fputs ("\n", f);
     mutt_copy_stream (tmpfp, f);
     fclose (tmpfp);
     if (thepid != -1)
@@ -76,7 +76,7 @@ static void append_signature (FILE *f)
 }
 
 /* compare two e-mail addresses and return 1 if they are equivalent */
-static int mutt_addrcmp (ADDRESS *a, ADDRESS *b)
+static int mutt_addrcmp (address_t * a, address_t * b)
 {
   if (!a->mailbox || !b->mailbox)
     return 0;
@@ -86,10 +86,9 @@ static int mutt_addrcmp (ADDRESS *a, ADDRESS *b)
 }
 
 /* search an e-mail address in a list */
-static int mutt_addrsrc (ADDRESS *a, ADDRESS *lst)
+static int mutt_addrsrc (address_t * a, address_t * lst)
 {
-  for (; lst; lst = lst->next)
-  {
+  for (; lst; lst = lst->next) {
     if (mutt_addrcmp (a, lst))
       return (1);
   }
@@ -97,37 +96,31 @@ static int mutt_addrsrc (ADDRESS *a, ADDRESS *lst)
 }
 
 /* removes addresses from "b" which are contained in "a" */
-static ADDRESS *mutt_remove_xrefs (ADDRESS *a, ADDRESS *b)
+static address_t *mutt_remove_xrefs (address_t * a, address_t * b)
 {
-  ADDRESS *top, *p, *prev = NULL;
+  address_t *top, *p, *prev = NULL;
 
   top = b;
-  while (b)
-  {
-    for (p = a; p; p = p->next)
-    {
+  while (b) {
+    for (p = a; p; p = p->next) {
       if (mutt_addrcmp (p, b))
-       break;
+        break;
     }
-    if (p)
-    {
-      if (prev)
-      {
-       prev->next = b->next;
-       b->next = NULL;
-       rfc822_free_address (&b);
-       b = prev;
+    if (p) {
+      if (prev) {
+        prev->next = b->next;
+        b->next = NULL;
+        address_list_wipe(&b);
+        b = prev;
       }
-      else
-      {
-       top = top->next;
-       b->next = NULL;
-       rfc822_free_address (&b);
-       b = top;
+      else {
+        top = top->next;
+        b->next = NULL;
+        address_list_wipe(&b);
+        b = top;
       }
     }
-    else
-    {
+    else {
       prev = b;
       b = b->next;
     }
@@ -138,16 +131,13 @@ static ADDRESS *mutt_remove_xrefs (ADDRESS *a, ADDRESS *b)
 /* remove any address which matches the current user.  if `leave_only' is
  * nonzero, don't remove the user's address if it is the only one in the list
  */
-static ADDRESS *remove_user (ADDRESS *a, int leave_only)
+static address_t *remove_user (address_t * a, int leave_only)
 {
-  ADDRESS *top = NULL, *last = NULL;
+  address_t *top = NULL, *last = NULL;
 
-  while (a)
-  {
-    if (!mutt_addr_is_user (a))
-    {
-      if (top)
-      {
+  while (a) {
+    if (!mutt_addr_is_user (a)) {
+      if (top) {
         last->next = a;
         last = last->next;
       }
@@ -156,108 +146,101 @@ static ADDRESS *remove_user (ADDRESS *a, int leave_only)
       a = a->next;
       last->next = NULL;
     }
-    else
-    {
-      ADDRESS *tmp = a;
-      
+    else {
+      address_t *tmp = a;
+
       a = a->next;
-      if (!leave_only || a || last)
-      {
-       tmp->next = NULL;
-       rfc822_free_address (&tmp);
+      if (!leave_only || a || last) {
+        tmp->next = NULL;
+        address_list_wipe(&tmp);
       }
       else
-       last = top = tmp;
+        last = top = tmp;
     }
   }
   return top;
 }
 
-static ADDRESS *find_mailing_lists (ADDRESS *t, ADDRESS *c)
+static address_t *find_mailing_lists (address_t * t, address_t * c)
 {
-  ADDRESS *top = NULL, *ptr = NULL;
-
-  for (; t || c; t = c, c = NULL)
-  {
-    for (; t; t = t->next)
-    {
-      if (mutt_is_mail_list (t) && !t->group)
-      {
-       if (top)
-       {
-         ptr->next = rfc822_cpy_adr_real (t);
-         ptr = ptr->next;
-       }
-       else
-         ptr = top = rfc822_cpy_adr_real (t);
+  address_t *top = NULL, *ptr = NULL;
+
+  for (; t || c; t = c, c = NULL) {
+    for (; t; t = t->next) {
+      if (mutt_is_mail_list (t) && !t->group) {
+        if (top) {
+          ptr->next = address_dup (t);
+          ptr = ptr->next;
+        }
+        else
+          ptr = top = address_dup (t);
       }
     }
   }
   return top;
 }
 
-static int edit_address (ADDRESS **a, /* const */ char *field)
+static int edit_address (address_t ** a, const char *field)
 {
   char buf[HUGE_STRING];
-  char *err = NULL;
+  const char *err = NULL;
   int idna_ok = 0;
-  
-  do
-  {
+
+  do {
     buf[0] = 0;
     mutt_addrlist_to_local (*a);
     rfc822_write_address (buf, sizeof (buf), *a, 0);
     if (mutt_get_field (field, buf, sizeof (buf), M_ALIAS) != 0)
       return (-1);
-    rfc822_free_address (a);
+    address_list_wipe(a);
     *a = mutt_expand_aliases (mutt_parse_adrlist (NULL, buf));
-    if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0)
-    {
+    if ((idna_ok = mutt_addrlist_to_idna (*a, &err)) != 0) {
       mutt_error (_("Error: '%s' is a bad IDN."), err);
       mutt_refresh ();
       mutt_sleep (2);
-      FREE (&err);
+      p_delete(&err);
     }
-  } 
+  }
   while (idna_ok != 0);
   return 0;
 }
 
-static int edit_envelope (ENVELOPE *en, int flags)
+static int edit_envelope (ENVELOPE * en, int flags)
 {
   char buf[HUGE_STRING];
-  LIST *uh = UserHeader;
+  string_list_t *uh = UserHeader;
   regmatch_t pat_match[1];
 
 #ifdef USE_NNTP
-  if (option (OPTNEWSSEND))
-  {
+  if (option (OPTNEWSSEND)) {
     if (en->newsgroups)
-      strfcpy (buf, en->newsgroups, sizeof (buf));
+      m_strcpy(buf, sizeof(buf), en->newsgroups);
     else
       buf[0] = 0;
     if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    FREE (&en->newsgroups);
-    en->newsgroups = safe_strdup (buf);
+    p_delete(&en->newsgroups);
+    en->newsgroups = m_strdup(buf);
 
     if (en->followup_to)
-      strfcpy (buf, en->followup_to, sizeof (buf));
+      m_strcpy(buf, sizeof(buf), en->followup_to);
     else
       buf[0] = 0;
-    if (option (OPTASKFOLLOWUP) && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
+    if (option (OPTASKFOLLOWUP)
+        && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    FREE (&en->followup_to);
-    en->followup_to = safe_strdup (buf);
+    p_delete(&en->followup_to);
+    en->followup_to = m_strdup(buf);
 
     if (en->x_comment_to)
-      strfcpy (buf, en->x_comment_to, sizeof (buf));
+      m_strcpy(buf, sizeof(buf), en->x_comment_to);
     else
       buf[0] = 0;
-    if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO) && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
+    if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
+        && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    FREE (&en->x_comment_to);
-    en->x_comment_to = safe_strdup (buf);
+    p_delete(&en->x_comment_to);
+    en->x_comment_to = m_strdup(buf);
   }
   else
 #endif
@@ -270,25 +253,20 @@ static int edit_envelope (ENVELOPE *en, int flags)
       return (-1);
   }
 
-  if (en->subject)
-  {
+  if (en->subject) {
     if (option (OPTFASTREPLY))
       return (0);
     else
-      strfcpy (buf, en->subject, sizeof (buf));
+      m_strcpy(buf, sizeof(buf), en->subject);
   }
-  else
-  {
+  else {
     char *p;
 
     buf[0] = 0;
-    for (; uh; uh = uh->next)
-    {
-      if (ascii_strncasecmp ("subject:", uh->data, 8) == 0)
-      {
-       p = uh->data + 8;
-       SKIPWS (p);
-       strncpy (buf, p, sizeof (buf));
+    for (; uh; uh = uh->next) {
+      if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
+        p = vskipspaces(uh->data + 8);
+        m_strcpy(buf, sizeof(buf), p);
       }
     }
   }
@@ -296,40 +274,45 @@ static int edit_envelope (ENVELOPE *en, int flags)
   if ((flags & (SENDREPLY)) && option (OPTSTRIPWAS) && StripWasRegexp.rx &&
       regexec (StripWasRegexp.rx, buf, 1, pat_match, 0) == 0) {
     unsigned int pos = pat_match->rm_so;
+
     if (ascii_strncasecmp (buf, "re: ", pos) != 0) {
-      buf[pos] = '\0';                  /* kill match */
+      buf[pos] = '\0';          /* kill match */
       while (pos-- && buf[pos] == ' ')
-        buf[pos] = '\0';                /* remove trailing spaces */
-    } else {
+        buf[pos] = '\0';        /* remove trailing spaces */
+    }
+    else {
       mutt_error (_("Ignoring $strip_was: Subject would be empty."));
       sleep (2);
     }
   }
-  if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 ||
-      (!buf[0] && query_quadoption (OPT_SUBJECT, _("No subject, abort?")) != M_NO))
-  {
+  if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) != 0 || (!buf[0]
+                                                                  &&
+                                                                  query_quadoption
+                                                                  (OPT_SUBJECT,
+                                                                   _
+                                                                   ("No subject, abort?"))
+                                                                  != M_NO)) {
     mutt_message _("No subject, aborting.");
+
     return (-1);
   }
-  mutt_str_replace (&en->subject, buf);
+  m_strreplace(&en->subject, buf);
 
   return 0;
 }
 
 #ifdef USE_NNTP
-char *nntp_get_header (const char *s)
+char *nntp_get_header(const char *s)
 {
-  SKIPWS (s);
-  return safe_strdup (s);
+    return m_strdup(skipspaces(s));
 }
 #endif
 
-static void process_user_recips (ENVELOPE *env)
+static void process_user_recips (ENVELOPE * env)
 {
-  LIST *uh = UserHeader;
+  string_list_t *uh = UserHeader;
 
-  for (; uh; uh = uh->next)
-  {
+  for (; uh; uh = uh->next) {
     if (ascii_strncasecmp ("to:", uh->data, 3) == 0)
       env->to = rfc822_parse_adrlist (env->to, uh->data + 3);
     else if (ascii_strncasecmp ("cc:", uh->data, 3) == 0)
@@ -347,77 +330,52 @@ static void process_user_recips (ENVELOPE *env)
   }
 }
 
-static void process_user_header (ENVELOPE *env)
+static void process_user_header (ENVELOPE * env)
 {
-  LIST *uh = UserHeader;
-  LIST *last = env->userhdrs;
+  string_list_t *uh = UserHeader;
+  string_list_t *last = env->userhdrs;
 
   if (last)
     while (last->next)
       last = last->next;
 
-  for (; uh; uh = uh->next)
-  {
-    if (ascii_strncasecmp ("from:", uh->data, 5) == 0)
-    {
+  for (; uh; uh = uh->next) {
+    if (ascii_strncasecmp ("from:", uh->data, 5) == 0) {
       /* User has specified a default From: address.  Remove default address */
-      rfc822_free_address (&env->from);
+      address_list_wipe(&env->from);
       env->from = rfc822_parse_adrlist (env->from, uh->data + 5);
     }
-    else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0)
-    {
-      rfc822_free_address (&env->reply_to);
+    else if (ascii_strncasecmp ("reply-to:", uh->data, 9) == 0) {
+      address_list_wipe(&env->reply_to);
       env->reply_to = rfc822_parse_adrlist (env->reply_to, uh->data + 9);
     }
     else if (ascii_strncasecmp ("message-id:", uh->data, 11) == 0)
-      mutt_str_replace (&env->message_id, uh->data + 11);
+      m_strreplace(&env->message_id, uh->data + 11);
     else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
-            ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
-            ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
+             ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
+             ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
 #ifdef USE_NNTP
-            ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
-            ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
-            ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
+             ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
+             ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
+             ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
 #endif
-            ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
-            ascii_strncasecmp ("subject:", uh->data, 8) != 0)
-    {
-      if (last)
-      {
-       last->next = mutt_new_list ();
-       last = last->next;
+             ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
+             ascii_strncasecmp ("subject:", uh->data, 8) != 0) {
+      if (last) {
+        last->next = string_item_new();
+        last = last->next;
       }
       else
-       last = env->userhdrs = mutt_new_list ();
-      last->data = safe_strdup (uh->data);
+        last = env->userhdrs = string_item_new();
+      last->data = m_strdup(uh->data);
     }
   }
 }
 
-LIST *mutt_copy_list (LIST *p)
-{
-  LIST *t, *r=NULL, *l=NULL;
-
-  for (; p; p = p->next)
-  {
-    t = (LIST *) safe_malloc (sizeof (LIST));
-    t->data = safe_strdup (p->data);
-    t->next = NULL;
-    if (l)
-    {
-      r->next = t;
-      r = r->next;
-    }
-    else
-      l = r = t;
-  }
-  return (l);
-}
-
-void mutt_forward_intro (FILE *fp, HEADER *cur)
+void mutt_forward_intro (FILE * fp, HEADER * cur)
 {
   char buffer[STRING];
-  
+
   fputs ("----- Forwarded message from ", fp);
   buffer[0] = 0;
   rfc822_write_address (buffer, sizeof (buffer), cur->env->from, 1);
@@ -425,32 +383,29 @@ void mutt_forward_intro (FILE *fp, HEADER *cur)
   fputs (" -----\n\n", fp);
 }
 
-void mutt_forward_trailer (FILE *fp)
+void mutt_forward_trailer (FILE * fp)
 {
   fputs ("\n----- End forwarded message -----\n", fp);
 }
 
 
-static int include_forward (CONTEXT *ctx, HEADER *cur, FILE *out)
+static int include_forward (CONTEXT * ctx, HEADER * cur, FILE * out)
 {
   int chflags = CH_DECODE, cmflags = 0;
-  
+
   mutt_parse_mime_message (ctx, cur);
   mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
 
-  if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE))
-  {
+  if ((cur->security & ENCRYPT) && option (OPTFORWDECODE)) {
     /* make sure we have the user's passphrase before proceeding... */
     crypt_valid_passphrase (cur->security);
   }
 
   mutt_forward_intro (out, cur);
 
-  if (option (OPTFORWDECODE))
-  {
+  if (option (OPTFORWDECODE)) {
     cmflags |= M_CM_DECODE | M_CM_CHARCONV;
-    if (option (OPTWEED))
-    {
+    if (option (OPTWEED)) {
       chflags |= CH_WEED | CH_REORDER;
       cmflags |= M_CM_WEED;
     }
@@ -463,48 +418,46 @@ static int include_forward (CONTEXT *ctx, HEADER *cur, FILE *out)
   return 0;
 }
 
-void mutt_make_attribution (CONTEXT *ctx, HEADER *cur, FILE *out)
+void mutt_make_attribution (CONTEXT * ctx, HEADER * cur, FILE * out)
 {
   char buffer[STRING];
-  if (Attribution)
-  {
+
+  if (Attribution) {
     mutt_make_string (buffer, sizeof (buffer), Attribution, ctx, cur);
     fputs (buffer, out);
     fputc ('\n', out);
   }
 }
 
-void mutt_make_post_indent (CONTEXT *ctx, HEADER *cur, FILE *out)
+void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
 {
   char buffer[STRING];
-  if (PostIndentString)
-  {
+
+  if (PostIndentString) {
     mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
     fputs (buffer, out);
     fputc ('\n', out);
   }
 }
 
-static int include_reply (CONTEXT *ctx, HEADER *cur, FILE *out)
+static int include_reply (CONTEXT * ctx, HEADER * cur, FILE * out)
 {
   int cmflags = M_CM_PREFIX | M_CM_DECODE | M_CM_CHARCONV | M_CM_REPLYING;
   int chflags = CH_DECODE;
 
-  if (WithCrypto && (cur->security & ENCRYPT))
-  {
+  if ((cur->security & ENCRYPT)) {
     /* make sure we have the user's passphrase before proceeding... */
     crypt_valid_passphrase (cur->security);
   }
 
   mutt_parse_mime_message (ctx, cur);
   mutt_message_hook (ctx, cur, M_MESSAGEHOOK);
-  
+
   mutt_make_attribution (ctx, cur, out);
-  
+
   if (!option (OPTHEADER))
     cmflags |= M_CM_NOHEADER;
-  if (option (OPTWEED))
-  {
+  if (option (OPTWEED)) {
     chflags |= CH_WEED | CH_REORDER;
     cmflags |= M_CM_WEED;
   }
@@ -512,18 +465,16 @@ static int include_reply (CONTEXT *ctx, HEADER *cur, FILE *out)
   mutt_copy_message (out, ctx, cur, cmflags, chflags);
 
   mutt_make_post_indent (ctx, cur, out);
-  
+
   return 0;
 }
 
-static int default_to (ADDRESS **to, ENVELOPE *env, int flags, int hmfupto)
+static int default_to (address_t ** to, ENVELOPE * env, int flags, int hmfupto)
 {
   char prompt[STRING];
-  ADDRESS *tmp;
 
-  if (flags && env->mail_followup_to && hmfupto == M_YES) 
-  {
-    rfc822_append (to, env->mail_followup_to);
+  if (flags && env->mail_followup_to && hmfupto == M_YES) {
+    address_list_append(to, address_list_dup(env->mail_followup_to));
     return 0;
   }
 
@@ -538,31 +489,26 @@ static int default_to (ADDRESS **to, ENVELOPE *env, int flags, int hmfupto)
    */
   if (!(flags & SENDGROUPREPLY) && mutt_is_list_cc (0, env->to, env->cc)) {
     switch (query_quadoption (OPT_LISTREPLY,
-            _("Message came from a mailing list. Reply to author only?")))
+                              _("Message came from a mailing list. List-reply to mailing list?")))
     {
-    case M_NO:
-      tmp = find_mailing_lists (env->to, env->cc);
-      rfc822_append (to, tmp);
-      rfc822_free_address (&tmp);
+    case M_YES:
+      address_list_append(to, find_mailing_lists (env->to, env->cc));
       return 0;
     case -1:
-      return -1; /* abort */
+      return -1;                /* abort */
     }
   }
 
-  if (!option(OPTREPLYSELF) && mutt_addr_is_user (env->from))
-  {
+  if (!option (OPTREPLYSELF) && mutt_addr_is_user (env->from)) {
     /* mail is from the user, assume replying to recipients */
-    rfc822_append (to, env->to);
-  }
-  else if (env->reply_to)
-  {
-    if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) || 
-       (option (OPTIGNORELISTREPLYTO) &&
-       mutt_is_mail_list (env->reply_to) &&
-       (mutt_addrsrc (env->reply_to, env->to) ||
-       mutt_addrsrc (env->reply_to, env->cc))))
-    {
+    address_list_append(to, address_list_dup(env->to));
+  }
+  else if (env->reply_to) {
+    if ((mutt_addrcmp (env->from, env->reply_to) && !env->reply_to->next) ||
+        (option (OPTIGNORELISTREPLYTO) &&
+         mutt_is_mail_list (env->reply_to) &&
+         (mutt_addrsrc (env->reply_to, env->to) ||
+          mutt_addrsrc (env->reply_to, env->cc)))) {
       /* If the Reply-To: address is a mailing list, assume that it was
        * put there by the mailing list, and use the From: address
        * 
@@ -571,196 +517,194 @@ static int default_to (ADDRESS **to, ENVELOPE *env, int flags, int hmfupto)
        * in his From header.
        * 
        */
-      rfc822_append (to, env->from);
+      address_list_append(to, address_list_dup(env->from));
     }
-    else if (!(mutt_addrcmp (env->from, env->reply_to) && 
-              !env->reply_to->next) &&
-            quadoption (OPT_REPLYTO) != M_YES)
-    {
+    else if (!(mutt_addrcmp (env->from, env->reply_to) &&
+               !env->reply_to->next) && quadoption (OPT_REPLYTO) != M_YES) {
       /* There are quite a few mailing lists which set the Reply-To:
        * header field to the list address, which makes it quite impossible
        * to send a message to only the sender of the message.  This
        * provides a way to do that.
        */
       snprintf (prompt, sizeof (prompt), _("Reply to %s%s?"),
-               env->reply_to->mailbox, 
-               env->reply_to->next?",...":"");
-      switch (query_quadoption (OPT_REPLYTO, prompt))
-      {
+                env->reply_to->mailbox, env->reply_to->next ? ",..." : "");
+      switch (query_quadoption (OPT_REPLYTO, prompt)) {
       case M_YES:
-       rfc822_append (to, env->reply_to);
-       break;
+        address_list_append(to, address_list_dup(env->reply_to));
+        break;
 
       case M_NO:
-       rfc822_append (to, env->from);
-       break;
+        address_list_append(to, address_list_dup(env->from));
+        break;
 
       default:
-       return (-1); /* abort */
+        return (-1);            /* abort */
       }
     }
     else
-      rfc822_append (to, env->reply_to);
+      address_list_append(to, address_list_dup(env->reply_to));
   }
   else
-    rfc822_append (to, env->from);
+    address_list_append(to, address_list_dup(env->from));
 
   return (0);
 }
 
-int mutt_fetch_recips (ENVELOPE *out, ENVELOPE *in, int flags)
+int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags)
 {
   char prompt[STRING];
-  ADDRESS *tmp;
   int hmfupto = -1;
 
-  if ((flags & (SENDLISTREPLY|SENDGROUPREPLY)) && in->mail_followup_to)
-  {
+  if ((flags & (SENDLISTREPLY | SENDGROUPREPLY)) && in->mail_followup_to) {
     snprintf (prompt, sizeof (prompt), _("Follow-up to %s%s?"),
-             in->mail_followup_to->mailbox,
-             in->mail_followup_to->next ? ",..." : "");
+              in->mail_followup_to->mailbox,
+              in->mail_followup_to->next ? ",..." : "");
 
     if ((hmfupto = query_quadoption (OPT_MFUPTO, prompt)) == -1)
       return -1;
   }
 
-  if (flags & SENDLISTREPLY)
-  {
-    tmp = find_mailing_lists (in->to, in->cc);
-    rfc822_append (&out->to, tmp);
-    rfc822_free_address (&tmp);
+  if (flags & SENDLISTREPLY) {
+    address_list_append(&out->to, find_mailing_lists(in->to, in->cc));
 
     if (in->mail_followup_to && hmfupto == M_YES &&
         default_to (&out->cc, in, flags & SENDLISTREPLY, hmfupto) == -1)
-      return (-1); /* abort */
+      return (-1);              /* abort */
   }
-  else
-  {
+  else {
     if (default_to (&out->to, in, flags & SENDGROUPREPLY, hmfupto) == -1)
-      return (-1); /* abort */
+      return (-1);              /* abort */
 
-    if ((flags & SENDGROUPREPLY) && (!in->mail_followup_to || hmfupto != M_YES))
+    if ((flags & SENDGROUPREPLY)
+        && (!in->mail_followup_to || hmfupto != M_YES))
     {
-      /* if(!mutt_addr_is_user(in->to)) */
-      rfc822_append (&out->cc, in->to);
-      rfc822_append (&out->cc, in->cc);
+      address_t **tmp = address_list_append(&out->cc, address_list_dup(in->to));
+      address_list_append(tmp, address_list_dup(in->cc));
     }
   }
   return 0;
 }
 
-LIST *mutt_make_references(ENVELOPE *e)
+string_list_t *mutt_make_references (ENVELOPE * e)
 {
-  LIST *t = NULL, *l = NULL;
+  string_list_t *t = NULL, *l = NULL;
 
   if (e->references)
-    l = mutt_copy_list (e->references);
+    l = string_list_dup(e->references);
   else
-    l = mutt_copy_list (e->in_reply_to);
-  
-  if (e->message_id)
-  {
-    t = mutt_new_list();
-    t->data = safe_strdup(e->message_id);
+    l = string_list_dup(e->in_reply_to);
+
+  if (e->message_id) {
+    t = string_item_new();
+    t->data = m_strdup(e->message_id);
     t->next = l;
     l = t;
   }
-  
+
   return l;
 }
 
-void mutt_fix_reply_recipients (ENVELOPE *env)
+void mutt_fix_reply_recipients (ENVELOPE * env)
 {
   mutt_expand_aliases_env (env);
 
-  if (! option (OPTMETOO))
-  {
+  if (!option (OPTMETOO)) {
     /* the order is important here.  do the CC: first so that if the
      * the user is the only recipient, it ends up on the TO: field
      */
     env->cc = remove_user (env->cc, (env->to == NULL));
     env->to = remove_user (env->to, (env->cc == NULL));
   }
-  
+
   /* the CC field can get cluttered, especially with lists */
   env->to = mutt_remove_duplicates (env->to);
   env->cc = mutt_remove_duplicates (env->cc);
   env->cc = mutt_remove_xrefs (env->to, env->cc);
+
+  if (env->cc && !env->to) {
+    env->to = env->cc;
+    env->cc = NULL;
+  }
 }
 
-void mutt_make_forward_subject (ENVELOPE *env, CONTEXT *ctx, HEADER *cur)
+void mutt_make_forward_subject (ENVELOPE * env, CONTEXT * ctx, HEADER * cur)
 {
   char buffer[STRING];
 
   /* set the default subject for the message. */
-  mutt_make_string (buffer, sizeof (buffer), NONULL(ForwFmt), ctx, cur);
-  mutt_str_replace (&env->subject, buffer);
+  mutt_make_string (buffer, sizeof (buffer), NONULL (ForwFmt), ctx, cur);
+  m_strreplace(&env->subject, buffer);
 }
 
-void mutt_make_misc_reply_headers (ENVELOPE *env, CONTEXT *ctx,
-                                   HEADER *cur, ENVELOPE *curenv)
+void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
+                                   HEADER * cur, ENVELOPE * curenv)
 {
   /* This takes precedence over a subject that might have
    * been taken from a List-Post header.  Is that correct?
    */
-  if (curenv->real_subj)
-  {
-    FREE (&env->subject);
-    env->subject = safe_malloc (mutt_strlen (curenv->real_subj) + 5);
-    sprintf (env->subject, "Re: %s", curenv->real_subj);       /* __SPRINTF_CHECKED__ */
+  if (curenv->real_subj) {
+    p_delete(&env->subject);
+    env->subject = p_new(char, m_strlen(curenv->real_subj) + 5);
+    sprintf (env->subject, "Re: %s", curenv->real_subj);        /* __SPRINTF_CHECKED__ */
   }
   else if (!env->subject)
-    env->subject = safe_strdup ("Re: your mail");
-  
+    env->subject = m_strdup("Re: your mail");
+
 #ifdef USE_NNTP
   if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
-    env->x_comment_to = safe_strdup (mutt_get_name (curenv->from));
+    env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
 #endif
 }
 
-void mutt_add_to_reference_headers (ENVELOPE *env, ENVELOPE *curenv, LIST ***pp, LIST ***qq)
+void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
+                                    string_list_t *** pp, string_list_t *** qq)
 {
-  LIST **p = NULL, **q = NULL;
-
-  if (pp) p = *pp;
-  if (qq) q = *qq;
-  
-  if (!p) p = &env->references;
-  if (!q) q = &env->in_reply_to;
-  
-  while (*p) p = &(*p)->next;
-  while (*q) q = &(*q)->next;
-  
+  string_list_t **p = NULL, **q = NULL;
+
+  if (pp)
+    p = *pp;
+  if (qq)
+    q = *qq;
+
+  if (!p)
+    p = &env->references;
+  if (!q)
+    q = &env->in_reply_to;
+
+  while (*p)
+    p = &(*p)->next;
+  while (*q)
+    q = &(*q)->next;
+
   *p = mutt_make_references (curenv);
-  
-  if (curenv->message_id)
-  {
-    *q = mutt_new_list();
-    (*q)->data = safe_strdup (curenv->message_id);
+
+  if (curenv->message_id) {
+    *q = string_item_new();
+    (*q)->data = m_strdup(curenv->message_id);
   }
-  
-  if (pp) *pp = p;
-  if (qq) *qq = q;
-  
+
+  if (pp)
+    *pp = p;
+  if (qq)
+    *qq = q;
+
 }
 
-static void 
-mutt_make_reference_headers (ENVELOPE *curenv, ENVELOPE *env, CONTEXT *ctx)
+static void
+mutt_make_reference_headers (ENVELOPE * curenv, ENVELOPE * env, CONTEXT * ctx)
 {
   env->references = NULL;
   env->in_reply_to = NULL;
-  
-  if (!curenv)
-  {
+
+  if (!curenv) {
     HEADER *h;
-    LIST **p = NULL, **q = NULL;
+    string_list_t **p = NULL, **q = NULL;
     int i;
-    
-    for(i = 0; i < ctx->vcount; i++)
-    {
+
+    for (i = 0; i < ctx->vcount; i++) {
       h = ctx->hdrs[ctx->v2r[i]];
       if (h->tagged)
-       mutt_add_to_reference_headers (env, h->env, &p, &q);
+        mutt_add_to_reference_headers (env, h->env, &p, &q);
     }
   }
   else
@@ -768,63 +712,57 @@ mutt_make_reference_headers (ENVELOPE *curenv, ENVELOPE *env, CONTEXT *ctx)
 }
 
 static int
-envelope_defaults (ENVELOPE *env, CONTEXT *ctx, HEADER *cur, int flags)
+envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
 {
   ENVELOPE *curenv = NULL;
   int i = 0, tag = 0;
 
-  if (!cur)
-  {
+  if (!cur) {
     tag = 1;
     for (i = 0; i < ctx->vcount; i++)
-      if (ctx->hdrs[ctx->v2r[i]]->tagged)
-      {
-       cur = ctx->hdrs[ctx->v2r[i]];
-       curenv = cur->env;
-       break;
+      if (ctx->hdrs[ctx->v2r[i]]->tagged) {
+        cur = ctx->hdrs[ctx->v2r[i]];
+        curenv = cur->env;
+        break;
       }
 
-    if (!cur)
-    {
+    if (!cur) {
       /* This could happen if the user tagged some messages and then did
        * a limit such that none of the tagged message are visible.
        */
       mutt_error _("No tagged messages are visible!");
+
       return (-1);
     }
   }
   else
     curenv = cur->env;
 
-  if (flags & SENDREPLY)
-  {
+  if (flags & SENDREPLY) {
 #ifdef USE_NNTP
-    if ((flags & SENDNEWS))
-    {
+    if ((flags & SENDNEWS)) {
       /* in case followup set Newsgroups: with Followup-To: if it present */
       if (!env->newsgroups && curenv &&
-         mutt_strcasecmp (curenv->followup_to, "poster"))
-       env->newsgroups = safe_strdup (curenv->followup_to);
+          m_strcasecmp(curenv->followup_to, "poster"))
+        env->newsgroups = m_strdup(curenv->followup_to);
     }
     else
 #endif
-    if (tag)
-    {
+    if (tag) {
       HEADER *h;
 
-      for (i = 0; i < ctx->vcount; i++)
-      {
-       h = ctx->hdrs[ctx->v2r[i]];
-       if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
-         return -1;
+      for (i = 0; i < ctx->vcount; i++) {
+        h = ctx->hdrs[ctx->v2r[i]];
+        if (h->tagged && mutt_fetch_recips (env, h->env, flags) == -1)
+          return -1;
       }
     }
     else if (mutt_fetch_recips (env, curenv, flags) == -1)
       return -1;
 
-    if ((flags & SENDLISTREPLY) && !env->to)
-    {
+    if ((flags & SENDLISTREPLY) && !env->to) {
       mutt_error _("No mailing lists found!");
+
       return (-1);
     }
 
@@ -837,102 +775,90 @@ envelope_defaults (ENVELOPE *env, CONTEXT *ctx, HEADER *cur, int flags)
   return (0);
 }
 
-static int
-generate_body (FILE *tempfp,   /* stream for outgoing message */
-              HEADER *msg,     /* header for outgoing message */
-              int flags,       /* compose mode */
-              CONTEXT *ctx,    /* current mailbox */
-              HEADER *cur)     /* current message */
-{
+static int generate_body (FILE * tempfp,        /* stream for outgoing message */
+                          HEADER * msg, /* header for outgoing message */
+                          int flags,    /* compose mode */
+                          CONTEXT * ctx,        /* current mailbox */
+                          HEADER * cur)
+{                               /* current message */
   int i;
   HEADER *h;
   BODY *tmp;
 
-  if (flags & SENDREPLY)
-  {
-    if ((i = query_quadoption (OPT_INCLUDE, _("Include message in reply?"))) == -1)
+  if (flags & SENDREPLY) {
+    if ((i =
+         query_quadoption (OPT_INCLUDE,
+                           _("Include message in reply?"))) == -1)
       return (-1);
 
-    if (i == M_YES)
-    {
+    if (i == M_YES) {
       mutt_message _("Including quoted message...");
-      if (!cur)
-      {
-       for (i = 0; i < ctx->vcount; i++)
-       {
-         h = ctx->hdrs[ctx->v2r[i]];
-         if (h->tagged)
-         {
-           if (include_reply (ctx, h, tempfp) == -1)
-           {
-             mutt_error _("Could not include all requested messages!");
-             return (-1);
-           }
-           fputc ('\n', tempfp);
-         }
-       }
+
+      if (!cur) {
+        for (i = 0; i < ctx->vcount; i++) {
+          h = ctx->hdrs[ctx->v2r[i]];
+          if (h->tagged) {
+            if (include_reply (ctx, h, tempfp) == -1) {
+              mutt_error _("Could not include all requested messages!");
+
+              return (-1);
+            }
+            fputc ('\n', tempfp);
+          }
+        }
       }
       else
-       include_reply (ctx, cur, tempfp);
+        include_reply (ctx, cur, tempfp);
 
     }
   }
-  else if (flags & SENDFORWARD)
-  {
-    if ((i = query_quadoption (OPT_MIMEFWD, _("Forward as attachment?"))) == M_YES)
-    {
+  else if (flags & SENDFORWARD) {
+    if ((i =
+         query_quadoption (OPT_MIMEFWD,
+                           _("Forward as attachment?"))) == M_YES) {
       BODY *last = msg->content;
 
       mutt_message _("Preparing forwarded message...");
-      
+
       while (last && last->next)
-       last = last->next;
+        last = last->next;
 
-      if (cur)
-      {
-       tmp = mutt_make_message_attach (ctx, cur, 0);
-       if (last)
-         last->next = tmp;
-       else
-         msg->content = tmp;
+      if (cur) {
+        tmp = mutt_make_message_attach (ctx, cur, 0);
+        if (last)
+          last->next = tmp;
+        else
+          msg->content = tmp;
       }
-      else
-      {
-       for (i = 0; i < ctx->vcount; i++)
-       {
-         if (ctx->hdrs[ctx->v2r[i]]->tagged)
-         {
-           tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
-           if (last)
-           {
-             last->next = tmp;
-             last = tmp;
-           }
-           else
-             last = msg->content = tmp;
-         }
-       }
+      else {
+        for (i = 0; i < ctx->vcount; i++) {
+          if (ctx->hdrs[ctx->v2r[i]]->tagged) {
+            tmp = mutt_make_message_attach (ctx, ctx->hdrs[ctx->v2r[i]], 0);
+            if (last) {
+              last->next = tmp;
+              last = tmp;
+            }
+            else
+              last = msg->content = tmp;
+          }
+        }
       }
     }
-    else if (i != -1)
-    {
+    else if (i != -1) {
       if (cur)
-       include_forward (ctx, cur, tempfp);
+        include_forward (ctx, cur, tempfp);
       else
-       for (i=0; i < ctx->vcount; i++)
-         if (ctx->hdrs[ctx->v2r[i]]->tagged)
-           include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
+        for (i = 0; i < ctx->vcount; i++)
+          if (ctx->hdrs[ctx->v2r[i]]->tagged)
+            include_forward (ctx, ctx->hdrs[ctx->v2r[i]], tempfp);
     }
     else if (i == -1)
       return -1;
   }
-  /* if (WithCrypto && (flags & SENDKEY)) */
-  else if ((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)) 
-  {
+  else if (flags & SENDKEY) {
     BODY *tmp;
 
-    if ((WithCrypto & APPLICATION_PGP)
-        && (tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
+    if ((tmp = crypt_pgp_make_key_attachment (NULL)) == NULL)
       return -1;
 
     tmp->next = msg->content;
@@ -944,10 +870,9 @@ generate_body (FILE *tempfp,       /* stream for outgoing message */
   return (0);
 }
 
-void mutt_set_followup_to (ENVELOPE *e)
+void mutt_set_followup_to (ENVELOPE * e)
 {
-  ADDRESS *t = NULL;
-  ADDRESS *from;
+  address_t *from;
 
   /* 
    * Only generate the Mail-Followup-To if the user has requested it, and
@@ -957,25 +882,23 @@ void mutt_set_followup_to (ENVELOPE *e)
   if (!option (OPTFOLLOWUPTO))
     return;
 #ifdef USE_NNTP
-  if (option (OPTNEWSSEND))
-  {
+  if (option (OPTNEWSSEND)) {
     if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
-      e->followup_to = safe_strdup (e->newsgroups);
+      e->followup_to = m_strdup(e->newsgroups);
     return;
   }
 #endif
 
-  if (!e->mail_followup_to)
-  {
-    if (mutt_is_list_cc (0, e->to, e->cc))
-    {
+  if (!e->mail_followup_to) {
+    if (mutt_is_list_cc (0, e->to, e->cc)) {
+      address_t **tmp;
       /* 
        * this message goes to known mailing lists, so create a proper
        * mail-followup-to header
        */
 
-      t = rfc822_append (&e->mail_followup_to, e->to);
-      rfc822_append (&t, e->cc);
+      tmp = address_list_append(&e->mail_followup_to, address_list_dup(e->to));
+      address_list_append(tmp, address_list_dup(e->cc));
     }
 
     /* remove ourselves from the mail-followup-to header */
@@ -988,28 +911,22 @@ void mutt_set_followup_to (ENVELOPE *e)
      * but makes sure list-reply has the desired effect.
      */
 
-    if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc))
-    {
+    if (e->mail_followup_to && !mutt_is_list_recipient (0, e->to, e->cc)) {
       if (e->reply_to)
-       from = rfc822_cpy_adr (e->reply_to);
+        from = address_list_dup (e->reply_to);
       else if (e->from)
-       from = rfc822_cpy_adr (e->from);
+        from = address_list_dup (e->from);
       else
-       from = mutt_default_from ();
-      
-      if (from)
-      {
-       /* Normally, this loop will not even be entered. */
-       for (t = from; t && t->next; t = t->next)
-         ;
-       
-       t->next = e->mail_followup_to;  /* t cannot be NULL at this point. */
-       e->mail_followup_to = from;
+        from = mutt_default_from ();
+
+      if (from) {
+        address_list_append(&from->next, e->mail_followup_to);
+        e->mail_followup_to = from;
       }
     }
-    
-    e->mail_followup_to = mutt_remove_duplicates (e->mail_followup_to);
-    
+
+    e->mail_followup_to = mutt_remove_duplicates(e->mail_followup_to);
+
   }
 }
 
@@ -1017,40 +934,36 @@ void mutt_set_followup_to (ENVELOPE *e)
 /* look through the recipients of the message we are replying to, and if
    we find an address that matches $alternates, we use that as the default
    from field */
-static ADDRESS *set_reverse_name (ENVELOPE *env)
+static address_t *set_reverse_name (ENVELOPE * env)
 {
-  ADDRESS *tmp;
+  address_t *tmp;
 
-  for (tmp = env->to; tmp; tmp = tmp->next)
-  {
+  for (tmp = env->to; tmp; tmp = tmp->next) {
     if (mutt_addr_is_user (tmp))
       break;
   }
-  if (!tmp)
-  {
-    for (tmp = env->cc; tmp; tmp = tmp->next)
-    {
+  if (!tmp) {
+    for (tmp = env->cc; tmp; tmp = tmp->next) {
       if (mutt_addr_is_user (tmp))
-       break;
+        break;
     }
   }
   if (!tmp && mutt_addr_is_user (env->from))
     tmp = env->from;
-  if (tmp)
-  {
-    tmp = rfc822_cpy_adr_real (tmp);
+  if (tmp) {
+    tmp = address_dup (tmp);
     if (!option (OPTREVREAL))
-      FREE (&tmp->personal);
+      p_delete(&tmp->personal);
     if (!tmp->personal)
-      tmp->personal = safe_strdup (Realname);
+      tmp->personal = m_strdup(Realname);
   }
   return (tmp);
 }
 
-ADDRESS *mutt_default_from (void)
+address_t *mutt_default_from (void)
 {
-  ADDRESS *adr;
-  const char *fqdn = mutt_fqdn(1);
+  address_t *adr;
+  const char *fqdn = mutt_fqdn (1);
 
   /* 
    * Note: We let $from override $realname here.  Is this the right
@@ -1058,51 +971,48 @@ ADDRESS *mutt_default_from (void)
    */
 
   if (From)
-    adr = rfc822_cpy_adr_real (From);
-  else if (option (OPTUSEDOMAIN))
-  {
-    adr = rfc822_new_address ();
-    adr->mailbox = safe_malloc (mutt_strlen (Username) + mutt_strlen (fqdn) + 2);
-    sprintf (adr->mailbox, "%s@%s", NONULL(Username), NONULL(fqdn));   /* __SPRINTF_CHECKED__ */
+    adr = address_dup (From);
+  else if (option (OPTUSEDOMAIN)) {
+    adr = address_new ();
+    adr->mailbox = p_new(char, m_strlen(Username) + m_strlen(fqdn) + 2);
+    sprintf (adr->mailbox, "%s@%s", NONULL (Username), NONULL (fqdn));  /* __SPRINTF_CHECKED__ */
   }
-  else
-  {
-    adr = rfc822_new_address ();
-    adr->mailbox = safe_strdup (NONULL(Username));
+  else {
+    adr = address_new ();
+    adr->mailbox = m_strdup(NONULL (Username));
   }
-  
+
   return (adr);
 }
 
-static int send_message (HEADER *msg)
-{  
+static int send_message (HEADER * msg)
+{
   char tempfile[_POSIX_PATH_MAX];
   FILE *tempfp;
   int i;
-  
+
   /* Write out the message in MIME form. */
   mutt_mktemp (tempfile);
   if ((tempfp = safe_fopen (tempfile, "w")) == NULL)
     return (-1);
 
 #ifdef MIXMASTER
-  mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, msg->chain ? 1 : 0);
+  mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0,
+                            msg->chain ? 1 : 0);
 #endif
 #ifndef MIXMASTER
   mutt_write_rfc822_header (tempfp, msg->env, msg->content, 0, 0);
 #endif
-  
-  fputc ('\n', tempfp); /* tie off the header. */
 
-  if ((mutt_write_mime_body (msg->content, tempfp) == -1))
-  {
-    fclose(tempfp);
+  fputc ('\n', tempfp);         /* tie off the header. */
+
+  if ((mutt_write_mime_body (msg->content, tempfp) == -1)) {
+    fclose (tempfp);
     unlink (tempfile);
     return (-1);
   }
-  
-  if (fclose (tempfp) != 0)
-  {
+
+  if (fclose (tempfp) != 0) {
     mutt_perror (tempfile);
     unlink (tempfile);
     return (-1);
@@ -1113,20 +1023,19 @@ static int send_message (HEADER *msg)
     return mix_send_message (msg->chain, tempfile);
 #endif
 
-  i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc, 
-                           msg->env->bcc, tempfile, (msg->content->encoding == ENC8BIT));
+  i = mutt_invoke_mta (msg->env->from, msg->env->to, msg->env->cc,
+                       msg->env->bcc, tempfile,
+                       (msg->content->encoding == ENC8BIT));
   return (i);
 }
 
 /* rfc2047 encode the content-descriptions */
-static void encode_descriptions (BODY *b, short recurse)
+static void encode_descriptions (BODY * b, short recurse)
 {
   BODY *t;
 
-  for (t = b; t; t = t->next)
-  {
-    if (t->description)
-    {
+  for (t = b; t; t = t->next) {
+    if (t->description) {
       rfc2047_encode_string (&t->description);
     }
     if (recurse && t->parts)
@@ -1135,14 +1044,12 @@ static void encode_descriptions (BODY *b, short recurse)
 }
 
 /* rfc2047 decode them in case of an error */
-static void decode_descriptions (BODY *b)
+static void decode_descriptions (BODY * b)
 {
   BODY *t;
-  
-  for (t = b; t; t = t->next)
-  {
-    if (t->description)
-    {
+
+  for (t = b; t; t = t->next) {
+    if (t->description) {
       rfc2047_decode (&t->description);
     }
     if (t->parts)
@@ -1150,25 +1057,37 @@ static void decode_descriptions (BODY *b)
   }
 }
 
-int mutt_resend_message (FILE *fp, CONTEXT *ctx, HEADER *cur)
+static void fix_end_of_file (const char *data)
 {
-  HEADER *msg = mutt_new_header ();
-  
+  FILE *fp;
+  int c;
+
+  if ((fp = safe_fopen (data, "a+")) == NULL)
+    return;
+  fseeko (fp, -1, SEEK_END);
+  if ((c = fgetc (fp)) != '\n')
+    fputc ('\n', fp);
+  safe_fclose (&fp);
+}
+
+int mutt_resend_message (FILE * fp, CONTEXT * ctx, HEADER * cur)
+{
+  HEADER *msg = header_new();
+
   if (mutt_prepare_template (fp, ctx, msg, cur, 1) < 0)
     return -1;
-  
+
   return ci_send_message (SENDRESEND, msg, NULL, ctx, cur);
 }
 
-int
-ci_send_message (int flags,            /* send mode */
-                HEADER *msg,           /* template to use for new message */
-                char *tempfile,        /* file specified by -i or -H */
-                CONTEXT *ctx,          /* current mailbox */
-                HEADER *cur)           /* current message */
-{
+int ci_send_message (int flags, /* send mode */
+                     HEADER * msg,      /* template to use for new message */
+                     char *tempfile,    /* file specified by -i or -H */
+                     CONTEXT * ctx,     /* current mailbox */
+                     HEADER * cur)
+{                               /* current message */
   char buffer[LONG_STRING];
-  char fcc[_POSIX_PATH_MAX] = ""; /* where to copy this message */
+  char fcc[_POSIX_PATH_MAX] = "";       /* where to copy this message */
   FILE *tempfp = NULL;
   BODY *pbody;
   int i, killfrom = 0;
@@ -1178,12 +1097,14 @@ ci_send_message (int flags,             /* send mode */
   BODY *save_content = NULL;
   BODY *clear_content = NULL;
   char *pgpkeylist = NULL;
+
   /* save current value of "pgp_sign_as" */
   char *signas = NULL;
-  char *tag = NULL, *err = NULL;
+  const char *tag = NULL, *err = NULL;
+  char *ctype;
 
   int rv = -1;
-  
+
 #ifdef USE_NNTP
   if (flags & SENDNEWS)
     set_option (OPTNEWSSEND);
@@ -1192,112 +1113,110 @@ ci_send_message (int flags,           /* send mode */
 #endif
 
   if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
-      mutt_num_postponed (1))
-  {
+      mutt_num_postponed (1)) {
     /* If the user is composing a new message, check to see if there
      * are any postponed messages first.
      */
-    if ((i = query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
+    if ((i =
+         query_quadoption (OPT_RECALL, _("Recall postponed message?"))) == -1)
       return rv;
 
-    if(i == M_YES)
+    if (i == M_YES)
       flags |= SENDPOSTPONED;
   }
-  
-  
-  if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
-    signas = safe_strdup(PgpSignAs);
+
+
+  if (flags & SENDPOSTPONED)
+    signas = m_strdup(PgpSignAs);
 
   /* Delay expansion of aliases until absolutely necessary--shouldn't
    * be necessary unless we are prompting the user or about to execute a
    * send-hook.
    */
 
-  if (!msg)
-  {
-    msg = mutt_new_header ();
+  if (!msg) {
+    msg = header_new();
 
-    if (flags == SENDPOSTPONED)
-    {
-      if ((flags = mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
-       goto cleanup;
+    if (flags == SENDPOSTPONED) {
+      if ((flags =
+           mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
+        goto cleanup;
 #ifdef USE_NNTP
       /*
        * If postponed message is a news article, it have
        * a "Newsgroups:" header line, then set appropriate flag.
        */
-      if (msg->env->newsgroups)
-      {
-       flags |= SENDNEWS;
-       set_option (OPTNEWSSEND);
+      if (msg->env->newsgroups) {
+        flags |= SENDNEWS;
+        set_option (OPTNEWSSEND);
       }
-      else
-      {
-       flags &= ~SENDNEWS;
-       unset_option (OPTNEWSSEND);
+      else {
+        flags &= ~SENDNEWS;
+        unset_option (OPTNEWSSEND);
       }
 #endif
     }
 
-    if (flags & (SENDPOSTPONED|SENDRESEND))
-    {
-      if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL)
-      {
-       mutt_perror (msg->content->filename);
-       goto cleanup;
+    if (flags & (SENDPOSTPONED | SENDRESEND)) {
+      if ((tempfp = safe_fopen (msg->content->filename, "a+")) == NULL) {
+        mutt_perror (msg->content->filename);
+        goto cleanup;
       }
     }
 
     if (!msg->env)
-      msg->env = mutt_new_envelope ();
+      msg->env = envelope_new();
   }
 
   /* Parse and use an eventual list-post header */
-  if ((flags & SENDLISTREPLY) 
-      && cur && cur->env && cur->env->list_post) 
-  {
+  if ((flags & SENDLISTREPLY)
+      && cur && cur->env && cur->env->list_post) {
     /* Use any list-post header as a template */
     url_parse_mailto (msg->env, NULL, cur->env->list_post);
     /* We don't let them set the sender's address. */
-    rfc822_free_address (&msg->env->from);
+    address_list_wipe(&msg->env->from);
   }
-  
-  if (! (flags & (SENDKEY | SENDPOSTPONED | SENDRESEND)))
-  {
+
+  if (!(flags & (SENDKEY | SENDPOSTPONED | SENDRESEND))) {
     pbody = mutt_new_body ();
     pbody->next = msg->content; /* don't kill command-line attachments */
     msg->content = pbody;
-    
-    mutt_parse_content_type (ContentType, msg->content);
+
+    if (!(ctype = m_strdup(ContentType)))
+      ctype = m_strdup("text/plain");
+    mutt_parse_content_type (ctype, msg->content);
+    p_delete(&ctype);
+
     msg->content->unlink = 1;
     msg->content->use_disp = 0;
     msg->content->disposition = DISPINLINE;
-    if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT && !ascii_strcasecmp (msg->content->subtype, "plain"))
+    if (option (OPTTEXTFLOWED) && msg->content->type == TYPETEXT
+        && !ascii_strcasecmp (msg->content->subtype, "plain")) {
       mutt_set_parameter ("format", "flowed", &msg->content->parameter);
-    
-    if (!tempfile)
-    {
+      if (option (OPTDELSP))
+        mutt_set_parameter ("delsp", "yes", &msg->content->parameter);
+    }
+
+    if (!tempfile) {
       mutt_mktemp (buffer);
       tempfp = safe_fopen (buffer, "w+");
-      msg->content->filename = safe_strdup (buffer);
+      msg->content->filename = m_strdup(buffer);
     }
-    else
-    {
+    else {
       tempfp = safe_fopen (tempfile, "a+");
-      msg->content->filename = safe_strdup (tempfile);
+      msg->content->filename = m_strdup(tempfile);
     }
 
-    if (!tempfp)
-    {
-      dprint(1,(debugfile, "newsend_message: can't create tempfile %s (errno=%d)\n", msg->content->filename, errno));
+    if (!tempfp) {
+      debug_print (1, ("can't create tempfile %s (errno=%d)\n", 
+                  msg->content->filename, errno));
       mutt_perror (msg->content->filename);
       goto cleanup;
     }
   }
 
   /* this is handled here so that the user can match ~f in send-hook */
-  if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED|SENDRESEND)))
-  {
+  if (cur && option (OPTREVNAME) && !(flags & (SENDPOSTPONED | SENDRESEND))) {
     /* we shouldn't have to worry about freeing `msg->env->from' before
      * setting it here since this code will only execute when doing some
      * sort of reply.  the pointer will only be set when using the -H command
@@ -1313,23 +1232,21 @@ ci_send_message (int flags,             /* send mode */
     msg->env->from = set_reverse_name (cur->env);
   }
 
-  if (!msg->env->from && option (OPTUSEFROM) && !(flags & (SENDPOSTPONED|SENDRESEND)))
+  if (!msg->env->from && option (OPTUSEFROM)
+      && !(flags & (SENDPOSTPONED | SENDRESEND)))
     msg->env->from = mutt_default_from ();
 
-  if (flags & SENDBATCH) 
-  {
+  if (flags & SENDBATCH) {
     mutt_copy_stream (stdin, tempfp);
-    if (option (OPTHDRS))
-    {
+    if (option (OPTHDRS)) {
       process_user_recips (msg->env);
       process_user_header (msg->env);
     }
     mutt_expand_aliases_env (msg->env);
   }
-  else if (! (flags & (SENDPOSTPONED|SENDRESEND)))
-  {
+  else if (!(flags & (SENDPOSTPONED | SENDRESEND))) {
     if ((flags & (SENDREPLY | SENDFORWARD)) && ctx &&
-       envelope_defaults (msg->env, ctx, cur, flags) == -1)
+        envelope_defaults (msg->env, ctx, cur, flags) == -1)
       goto cleanup;
 
     if (option (OPTHDRS))
@@ -1339,16 +1256,15 @@ ci_send_message (int flags,             /* send mode */
     mutt_fix_reply_recipients (msg->env);
 
 #ifdef USE_NNTP
-    if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP && !msg->env->newsgroups)
-      msg->env->newsgroups = safe_strdup (((NNTP_DATA *)ctx->data)->group);
+    if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
+        && !msg->env->newsgroups)
+      msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
 #endif
 
-    if (! (flags & SENDMAILX) &&
-       ! (option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
-       ! ((flags & SENDREPLY) && option (OPTFASTREPLY)))
-    {
+    if (!(option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
+        !((flags & SENDREPLY) && option (OPTFASTREPLY))) {
       if (edit_envelope (msg->env, flags) == -1)
-       goto cleanup;
+        goto cleanup;
     }
 
     /* the from address must be set here regardless of whether or not
@@ -1356,14 +1272,12 @@ ci_send_message (int flags,             /* send mode */
      * patterns will work.  if $use_from is unset, the from address is killed
      * after send-hooks are evaulated */
 
-    if (!msg->env->from)
-    {
+    if (!msg->env->from) {
       msg->env->from = mutt_default_from ();
       killfrom = 1;
     }
 
-    if ((flags & SENDREPLY) && cur)
-    {
+    if ((flags & SENDREPLY) && cur) {
       /* change setting based upon message we are replying to */
       mutt_message_hook (ctx, cur, M_REPLYHOOK);
 
@@ -1376,7 +1290,7 @@ ci_send_message (int flags,               /* send mode */
     }
 
     /* change settings based upon recipients */
-    
+
     mutt_message_hook (NULL, msg, M_SENDHOOK);
 
     /*
@@ -1387,9 +1301,8 @@ ci_send_message (int flags,               /* send mode */
      */
     msg->replied = 0;
 
-    if (killfrom)
-    {
-      rfc822_free_address (&msg->env->from);
+    if (killfrom) {
+      address_list_wipe(&msg->env->from);
       killfrom = 0;
     }
 
@@ -1397,15 +1310,15 @@ ci_send_message (int flags,             /* send mode */
       process_user_header (msg->env);
 
 
-    if (option (OPTSIGONTOP) && (! (flags & (SENDMAILX | SENDKEY)) && Editor && mutt_strcmp (Editor, "builtin") != 0))
+    if (option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
       append_signature (tempfp);
 
     /* include replies/forwarded messages, unless we are given a template */
-    if (!tempfile && (ctx || !(flags & (SENDREPLY|SENDFORWARD)))
-       && generate_body (tempfp, msg, flags, ctx, cur) == -1)
+    if (!tempfile && (ctx || !(flags & (SENDREPLY | SENDFORWARD)))
+        && generate_body (tempfp, msg, flags, ctx, cur) == -1)
       goto cleanup;
 
-    if (!option (OPTSIGONTOP) && (! (flags & (SENDMAILX | SENDKEY)) && Editor && mutt_strcmp (Editor, "builtin") != 0))
+    if (!option (OPTSIGONTOP) && (!(flags & SENDKEY) && Editor))
       append_signature (tempfp);
 
     /* 
@@ -1413,29 +1326,25 @@ ci_send_message (int flags,             /* send mode */
      * can take effect.
      */
 
-    if (WithCrypto && !(flags & SENDMAILX))
-    {
-      if (option (OPTCRYPTAUTOSIGN))
-       msg->security |= SIGN;
-      if (option (OPTCRYPTAUTOENCRYPT))
-       msg->security |= ENCRYPT;
-      if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
-       msg->security |= ENCRYPT;
-      if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
-       msg->security |= SIGN;
-      if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur && (cur->security & ENCRYPT))
-       msg->security |= SIGN;
-      if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN)))
-      {
-       if (option (OPTPGPAUTOINLINE))
-         msg->security |= INLINE;
-       if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
-         msg->security |= INLINE;
-      }
+    if (option (OPTCRYPTAUTOSIGN))
+      msg->security |= SIGN;
+    if (option (OPTCRYPTAUTOENCRYPT))
+      msg->security |= ENCRYPT;
+    if (option (OPTCRYPTREPLYENCRYPT) && cur && (cur->security & ENCRYPT))
+      msg->security |= ENCRYPT;
+    if (option (OPTCRYPTREPLYSIGN) && cur && (cur->security & SIGN))
+      msg->security |= SIGN;
+    if (option (OPTCRYPTREPLYSIGNENCRYPTED) && cur
+        && (cur->security & ENCRYPT))
+      msg->security |= SIGN;
+    if (msg->security & (ENCRYPT | SIGN)) {
+      if (option (OPTPGPAUTOINLINE))
+        msg->security |= INLINE;
+      if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
+        msg->security |= INLINE;
     }
 
-    if (WithCrypto && msg->security)
-    {
+    if (msg->security) {
       /* 
        * When reypling / forwarding, use the original message's
        * crypto system.  According to the documentation,
@@ -1445,63 +1354,53 @@ ci_send_message (int flags,             /* send mode */
        * make much sense. Should we have an option to completely
        * disable individual mechanisms at run-time?
        */
-      if (cur)
-      {
-       if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP) 
-           && (cur->security & APPLICATION_PGP))
-         msg->security |= APPLICATION_PGP;
-       else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME)
-                && (cur->security & APPLICATION_SMIME))
-         msg->security |= APPLICATION_SMIME;
+      if (cur) {
+        if (option (OPTCRYPTAUTOPGP) && (cur->security & APPLICATION_PGP))
+          msg->security |= APPLICATION_PGP;
+        else if (option (OPTCRYPTAUTOSMIME)
+                 && (cur->security & APPLICATION_SMIME))
+          msg->security |= APPLICATION_SMIME;
       }
-      
+
       /*
        * No crypto mechanism selected? Use availability + smime_is_default
        * for the decision. 
        */
-      if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
-      {
-       if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME) 
-           && option (OPTSMIMEISDEFAULT))
-         msg->security |= APPLICATION_SMIME;
-       else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
-         msg->security |= APPLICATION_PGP;
-       else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME))
-         msg->security |= APPLICATION_SMIME;
+      if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP))) {
+        if (option (OPTCRYPTAUTOSMIME) && option (OPTSMIMEISDEFAULT))
+          msg->security |= APPLICATION_SMIME;
+        else if (option (OPTCRYPTAUTOPGP))
+          msg->security |= APPLICATION_PGP;
+        else if (option (OPTCRYPTAUTOSMIME))
+          msg->security |= APPLICATION_SMIME;
       }
     }
-    
+
     /* No permissible mechanisms found.  Don't sign or encrypt. */
-    if (!(msg->security & (APPLICATION_SMIME|APPLICATION_PGP)))
+    if (!(msg->security & (APPLICATION_SMIME | APPLICATION_PGP)))
       msg->security = 0;
   }
-  
+
   /* 
    * This hook is even called for postponed messages, and can, e.g., be
    * used for setting the editor, the sendmail path, or the
    * envelope sender.
    */
   mutt_message_hook (NULL, msg, M_SEND2HOOK);
-  
+
   /* wait until now to set the real name portion of our return address so
      that $realname can be set in a send-hook */
   if (msg->env->from && !msg->env->from->personal
-      && !(flags & (SENDRESEND|SENDPOSTPONED)))
-    msg->env->from->personal = safe_strdup (Realname);
+      && !(flags & (SENDRESEND | SENDPOSTPONED)))
+    msg->env->from->personal = m_strdup(Realname);
 
-  if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
+  if (!(flags & SENDKEY))
     safe_fclose (&tempfp);
 
-  if (flags & SENDMAILX)
-  {
-    if (mutt_builtin_editor (msg->content->filename, msg, cur) == -1)
-      goto cleanup;
-  }
-  else if (! (flags & SENDBATCH))
-  {
+  if (!(flags & SENDBATCH)) {
     struct stat st;
     time_t mtime = mutt_decrease_mtime (msg->content->filename, NULL);
-    
+
     mutt_update_encoding (msg->content);
 
     /*
@@ -1514,111 +1413,120 @@ ci_send_message (int flags,           /* send mode */
      *    setting of $forward_edit because the user probably needs to add the
      *    recipients.
      */
-    if (! (flags & SENDKEY) &&
-       ((flags & SENDFORWARD) == 0 ||
-        (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
-        query_quadoption (OPT_FORWEDIT, _("Edit forwarded message?")) == M_YES))
-    {
+    if (!(flags & SENDKEY) &&
+        ((flags & SENDFORWARD) == 0 ||
+         (option (OPTEDITHDRS) && option (OPTAUTOEDIT)) ||
+         query_quadoption (OPT_FORWEDIT,
+                           _("Edit forwarded message?")) == M_YES)) {
       /* If the this isn't a text message, look for a mailcap edit command */
-      if (mutt_needs_mailcap (msg->content))
-       mutt_edit_attachment (msg->content);
-      else if (!Editor || mutt_strcmp ("builtin", Editor) == 0)
-       mutt_builtin_editor (msg->content->filename, msg, cur);
-      else if (option (OPTEDITHDRS))
-      {
-       mutt_env_to_local (msg->env);
-       mutt_edit_headers (Editor, msg->content->filename, msg, fcc, sizeof (fcc));
-       mutt_env_to_idna (msg->env, NULL, NULL);
+      if (mutt_needs_mailcap (msg->content)) {
+        if (!mutt_edit_attachment (msg->content))
+          goto cleanup;
+      } else if (option (OPTEDITHDRS)) {
+        mutt_env_to_local (msg->env);
+        mutt_edit_headers (Editor, msg->content->filename, msg, fcc,
+                           sizeof (fcc));
+        mutt_env_to_idna (msg->env, NULL, NULL);
       }
-      else
-       mutt_edit_file (Editor, msg->content->filename);
-      
+      else {
+        mutt_edit_file (Editor, msg->content->filename);
+
+        if (stat (msg->content->filename, &st) == 0) {
+          if (mtime != st.st_mtime)
+            fix_end_of_file (msg->content->filename);
+        } else
+          mutt_perror (msg->content->filename);
+      }
+
+      if (option (OPTTEXTFLOWED))
+        rfc3676_space_stuff (msg);
+
       mutt_message_hook (NULL, msg, M_SEND2HOOK);
     }
 
-    if (! (flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND)))
-    {
-      if (stat (msg->content->filename, &st) == 0)
-      {
-       /* if the file was not modified, bail out now */
-       if (mtime == st.st_mtime && !msg->content->next &&
-           query_quadoption (OPT_ABORT, _("Abort unmodified message?")) == M_YES)
-       {
-         mutt_message _("Aborted unmodified message.");
-         goto cleanup;
-       }
+    if (!(flags & (SENDPOSTPONED | SENDFORWARD | SENDKEY | SENDRESEND))) {
+      if (stat (msg->content->filename, &st) == 0) {
+        /* if the file was not modified, bail out now */
+        if (mtime == st.st_mtime && !msg->content->next &&
+            query_quadoption (OPT_ABORT,
+                              _("Abort unmodified message?")) == M_YES) {
+          mutt_message _("Aborted unmodified message.");
+
+          goto cleanup;
+        }
       }
       else
-       mutt_perror (msg->content->filename);
+        mutt_perror (msg->content->filename);
     }
   }
 
   /* specify a default fcc.  if we are in batchmode, only save a copy of
    * the message if the value of $copy is yes or ask-yes */
 
-  if (!fcc[0] && !(flags & (SENDPOSTPONED)) && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1)))
-  {
+  if (!fcc[0] && !(flags & (SENDPOSTPONED))
+      && (!(flags & SENDBATCH) || (quadoption (OPT_COPY) & 0x1))) {
     /* set the default FCC */
-    if (!msg->env->from)
-    {
+    if (!msg->env->from) {
       msg->env->from = mutt_default_from ();
-      killfrom = 1; /* no need to check $use_from because if the user specified
-                      a from address it would have already been set by now */
+      killfrom = 1;             /* no need to check $use_from because if the user specified
+                                   a from address it would have already been set by now */
     }
     mutt_select_fcc (fcc, sizeof (fcc), msg);
-    if (killfrom)
-    {
-      rfc822_free_address (&msg->env->from);
+    if (killfrom) {
+      address_list_wipe(&msg->env->from);
       killfrom = 0;
     }
   }
 
-  
+
   mutt_update_encoding (msg->content);
 
-  if (! (flags & (SENDMAILX | SENDBATCH)))
-  {
-main_loop:
+  if (!(flags & SENDBATCH)) {
+  main_loop:
 
-    fcc_error = 0; /* reset value since we may have failed before */
+    fcc_error = 0;              /* reset value since we may have failed before */
     mutt_pretty_mailbox (fcc);
     i = mutt_compose_menu (msg, fcc, sizeof (fcc), cur);
-    if (i == -1)
-    {
+    if (i == -1) {
       /* abort */
 #ifdef USE_NNTP
       if (flags & SENDNEWS)
-       mutt_message _("Article not posted.");
+        mutt_message (_("Article not posted."));
+
       else
 #endif
-      mutt_message _("Mail not sent.");
+        mutt_message _("Mail not sent.");
       goto cleanup;
     }
-    else if (i == 1)
-    {
+    else if (i == 1) {
       /* postpone the message until later. */
       if (msg->content->next)
-       msg->content = mutt_make_multipart (msg->content);
+        msg->content = mutt_make_multipart (msg->content);
 
       /*
        * make sure the message is written to the right part of a maildir 
        * postponed folder.
        */
-      msg->read = 0; msg->old = 0;
+      msg->read = 0;
+      msg->old = 0;
 
       encode_descriptions (msg->content, 1);
       mutt_prepare_envelope (msg->env, 0);
-      mutt_env_to_idna (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
+      mutt_env_to_idna (msg->env, NULL, NULL);  /* Handle bad IDNAs the next time. */
 
-      if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, 1, fcc) < 0)
-      {
-       msg->content = mutt_remove_multipart (msg->content);
-       decode_descriptions (msg->content);
-       mutt_unprepare_envelope (msg->env);
-       goto main_loop;
+      if (!Postponed
+          || mutt_write_fcc (NONULL (Postponed), msg,
+                             (cur
+                              && (flags & SENDREPLY)) ? cur->env->
+                             message_id : NULL, 1, fcc) < 0) {
+        msg->content = mutt_remove_multipart (msg->content);
+        decode_descriptions (msg->content);
+        mutt_unprepare_envelope (msg->env);
+        goto main_loop;
       }
       mutt_update_num_postponed ();
       mutt_message _("Message postponed.");
+
       goto cleanup;
     }
   }
@@ -1626,48 +1534,48 @@ main_loop:
 #ifdef USE_NNTP
   if (!(flags & SENDNEWS))
 #endif
-  if (!msg->env->to && !msg->env->cc && !msg->env->bcc)
-  {
-    if (! (flags & SENDBATCH))
-    {
-      mutt_error _("No recipients are specified!");
-      goto main_loop;
-    }
-    else
-    {
-      puts _("No recipients were specified.");
-      goto cleanup;
+    if (!msg->env->to && !msg->env->cc && !msg->env->bcc) {
+      if (!(flags & SENDBATCH)) {
+        mutt_error _("No recipients are specified!");
+
+        goto main_loop;
+      }
+      else {
+        puts _("No recipients were specified.");
+
+        goto cleanup;
+      }
     }
-  }
 
-  if (mutt_env_to_idna (msg->env, &tag, &err))
-  {
+  if (mutt_env_to_idna (msg->env, &tag, &err)) {
     mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
-    FREE (&err);
+    p_delete(&err);
     if (!(flags & SENDBATCH))
       goto main_loop;
-    else 
+    else
       goto cleanup;
   }
-  
-  if (!msg->env->subject && ! (flags & SENDBATCH) &&
-      (i = query_quadoption (OPT_SUBJECT, _("No subject, abort sending?"))) != M_NO)
-  {
+
+  if (!msg->env->subject && !(flags & SENDBATCH) &&
+      (i =
+       query_quadoption (OPT_SUBJECT,
+                         _("No subject, abort sending?"))) != M_NO) {
     /* if the abort is automatic, print an error message */
     if (quadoption (OPT_SUBJECT) == M_YES)
       mutt_error _("No subject specified.");
+
     goto main_loop;
   }
 #ifdef USE_NNTP
-  if ((flags & SENDNEWS) && !msg->env->subject)
-  {
+  if ((flags & SENDNEWS) && !msg->env->subject) {
     mutt_error _("No subject specified.");
+
     goto main_loop;
   }
 
-  if ((flags & SENDNEWS) && !msg->env->newsgroups)
-  {
+  if ((flags & SENDNEWS) && !msg->env->newsgroups) {
     mutt_error _("No newsgroup specified.");
+
     goto main_loop;
   }
 #endif
@@ -1675,6 +1583,20 @@ main_loop:
   if (msg->content->next)
     msg->content = mutt_make_multipart (msg->content);
 
+  if (mutt_attach_check (msg) &&
+      !msg->content->next &&
+      query_quadoption (OPT_ATTACH,
+                        _("No attachments made but indicator found in text. "
+                          "Cancel sending?")) == M_YES) {
+    if (quadoption (OPT_ATTACH) == M_YES) {
+      mutt_message _("No attachments made but indicator found in text. "
+                     "Abort sending.");
+      sleep (2);
+    }
+    mutt_message (_("Mail not sent."));
+    goto main_loop;
+  }
+
   /* 
    * Ok, we need to do it this way instead of handling all fcc stuff in
    * one place in order to avoid going to main_loop with encoded "env"
@@ -1682,54 +1604,49 @@ main_loop:
    */
 
   encode_descriptions (msg->content, 1);
-  
+
   /*
    * Make sure that clear_content and free_clear_content are
    * properly initialized -- we may visit this particular place in
    * the code multiple times, including after a failed call to
    * mutt_protect().
    */
-  
+
   clear_content = NULL;
   free_clear_content = 0;
-  
-  if (WithCrypto)
-  {
-    if (msg->security)  
-    {
-      /* save the decrypted attachments */
-      clear_content = msg->content;
-  
-      if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
-          mutt_protect (msg, pgpkeylist) == -1)
-      {
-        msg->content = mutt_remove_multipart (msg->content);
-        
-       FREE (&pgpkeylist);
-        
-        decode_descriptions (msg->content);
-        goto main_loop;
-      }
-      encode_descriptions (msg->content, 0);
+
+  if (msg->security) {
+    /* save the decrypted attachments */
+    clear_content = msg->content;
+
+    if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
+        mutt_protect (msg, pgpkeylist) == -1) {
+      msg->content = mutt_remove_multipart (msg->content);
+
+      p_delete(&pgpkeylist);
+
+      decode_descriptions (msg->content);
+      goto main_loop;
     }
-  
-    /* 
-     * at this point, msg->content is one of the following three things:
-     * - multipart/signed.  In this case, clear_content is a child.
-     * - multipart/encrypted.  In this case, clear_content exists
-     *   independently
-     * - application/pgp.  In this case, clear_content exists independently.
-     * - something else.  In this case, it's the same as clear_content.
-     */
-  
-    /* This is ugly -- lack of "reporting back" from mutt_protect(). */
-    
-    if (clear_content && (msg->content != clear_content)
-        && (msg->content->parts != clear_content))
-      free_clear_content = 1;
+    encode_descriptions (msg->content, 0);
   }
 
-  if (!option (OPTNOCURSES) && !(flags & SENDMAILX))
+  /* 
+   * at this point, msg->content is one of the following three things:
+   * - multipart/signed.  In this case, clear_content is a child.
+   * - multipart/encrypted.  In this case, clear_content exists
+   *   independently
+   * - application/pgp.  In this case, clear_content exists independently.
+   * - something else.  In this case, it's the same as clear_content.
+   */
+
+  /* This is ugly -- lack of "reporting back" from mutt_protect(). */
+
+  if (clear_content && (msg->content != clear_content)
+      && (msg->content->parts != clear_content))
+    free_clear_content = 1;
+
+  if (!option (OPTNOCURSES))
     mutt_message _("Sending message...");
 
   mutt_prepare_envelope (msg->env, 1);
@@ -1738,7 +1655,7 @@ main_loop:
 
   mutt_expand_path (fcc, sizeof (fcc));
 
-  
+
   /* Don't save a copy when we are in batch-mode, and the FCC
    * folder is on an IMAP server: This would involve possibly lots
    * of user interaction, which is not available in batch mode. 
@@ -1748,82 +1665,70 @@ main_loop:
    * I'd like to think a bit more about this before including it.
    */
 
-#ifdef USE_IMAP
-  if ((flags & SENDBATCH) && fcc[0] && mx_is_imap (fcc))
+  if ((flags & SENDBATCH) && fcc[0] && mx_get_magic (fcc) == M_IMAP)
     fcc[0] = '\0';
-#endif
 
-  if (*fcc && mutt_strcmp ("/dev/null", fcc) != 0)
-  {
+  if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
     BODY *tmpbody = msg->content;
     BODY *save_sig = NULL;
     BODY *save_parts = NULL;
 
-    if (WithCrypto && msg->security && option (OPTFCCCLEAR))
+    if (msg->security && option (OPTFCCCLEAR))
       msg->content = clear_content;
 
     /* check to see if the user wants copies of all attachments */
-    if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART)
-    {
-      if (WithCrypto
-          && (mutt_strcmp (msg->content->subtype, "encrypted") == 0 ||
-              mutt_strcmp (msg->content->subtype, "signed") == 0))
+    if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
+      if ((m_strcmp(msg->content->subtype, "encrypted") == 0 ||
+              m_strcmp(msg->content->subtype, "signed") == 0))
       {
-       if (clear_content->type == TYPEMULTIPART)
-       {
-         if(!(msg->security & ENCRYPT) && (msg->security & SIGN))
-         {
-           /* save initial signature and attachments */
-           save_sig = msg->content->parts->next;
-           save_parts = clear_content->parts->next;
-         }
-
-         /* this means writing only the main part */
-         msg->content = clear_content->parts;
-
-         if (mutt_protect (msg, pgpkeylist) == -1)
-         {
-           /* we can't do much about it at this point, so
-            * fallback to saving the whole thing to fcc
-            */
-           msg->content = tmpbody;
-           save_sig = NULL;
-           goto full_fcc;
-         }
-
-         save_content = msg->content;
-       }
+        if (clear_content->type == TYPEMULTIPART) {
+          if (!(msg->security & ENCRYPT) && (msg->security & SIGN)) {
+            /* save initial signature and attachments */
+            save_sig = msg->content->parts->next;
+            save_parts = clear_content->parts->next;
+          }
+
+          /* this means writing only the main part */
+          msg->content = clear_content->parts;
+
+          if (mutt_protect (msg, pgpkeylist) == -1) {
+            /* we can't do much about it at this point, so
+             * fallback to saving the whole thing to fcc
+             */
+            msg->content = tmpbody;
+            save_sig = NULL;
+            goto full_fcc;
+          }
+
+          save_content = msg->content;
+        }
       }
       else
-       msg->content = msg->content->parts;
+        msg->content = msg->content->parts;
     }
 
-full_fcc:
-    if (msg->content)
-    {
+  full_fcc:
+    if (msg->content) {
       /* update received time so that when storing to a mbox-style folder
        * the From_ line contains the current time instead of when the
        * message was first postponed.
        */
       msg->received = time (NULL);
-      if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1)
-      {
-       /*
-        * Error writing FCC, we should abort sending.
-        */
-       fcc_error = 1;
+      if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1) {
+        /*
+         * Error writing FCC, we should abort sending.
+         */
+        fcc_error = 1;
       }
     }
 
     msg->content = tmpbody;
 
-    if (WithCrypto && save_sig)
-    {
+    if (save_sig) {
       /* cleanup the second signature structures */
-      if (save_content->parts)
-      {
-       mutt_free_body (&save_content->parts->next);
-       save_content->parts = NULL;
+      if (save_content->parts) {
+        mutt_free_body (&save_content->parts->next);
+        save_content->parts = NULL;
       }
       mutt_free_body (&save_content);
 
@@ -1831,8 +1736,7 @@ full_fcc:
       msg->content->parts->next = save_sig;
       msg->content->parts->parts->next = save_parts;
     }
-    else if (WithCrypto && save_content)
-    {
+    else if (save_content) {
       /* destroy the new encrypted body. */
       mutt_free_body (&save_content);
     }
@@ -1845,23 +1749,17 @@ full_fcc:
    * the send failed as well so we give the user a chance to fix the
    * error.
    */
-  if (fcc_error || (i = send_message (msg)) == -1)
-  {
-    if (!(flags & SENDBATCH))
-    {
-      if (!WithCrypto)
-        ;
-      else if ((msg->security & ENCRYPT) || 
-               ((msg->security & SIGN)
-                && msg->content->type == TYPEAPPLICATION))
-      {
-       mutt_free_body (&msg->content); /* destroy PGP data */
-       msg->content = clear_content;   /* restore clear text. */
+  if (fcc_error || (i = send_message (msg)) == -1) {
+    if (!(flags & SENDBATCH)) {
+      if ((msg->security & ENCRYPT)
+      ||  ((msg->security & SIGN)
+      &&  msg->content->type == TYPEAPPLICATION)) {
+        mutt_free_body (&msg->content); /* destroy PGP data */
+        msg->content = clear_content;   /* restore clear text. */
       }
-      else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART)
-      {
-       mutt_free_body (&msg->content->parts->next);         /* destroy sig */
-       msg->content = mutt_remove_multipart (msg->content); 
+      else if ((msg->security & SIGN) && msg->content->type == TYPEMULTIPART) {
+        mutt_free_body (&msg->content->parts->next);    /* destroy sig */
+        msg->content = mutt_remove_multipart (msg->content);
       }
 
       msg->content = mutt_remove_multipart (msg->content);
@@ -1869,55 +1767,52 @@ full_fcc:
       mutt_unprepare_envelope (msg->env);
       goto main_loop;
     }
-    else
-    {
+    else {
       puts _("Could not send the message.");
+
       goto cleanup;
     }
   }
-  else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX))
+  else if (!option (OPTNOCURSES))
     mutt_message (i != 0 ? _("Sending in background.") :
 #ifdef USE_NNTP
-                 (flags & SENDNEWS) ? _("Article posted.") : _("Mail sent."));
+                  (flags & SENDNEWS) ? _("Article posted.") :
+                  _("Mail sent.")
 #else
-                 _("Mail sent."));
+                  _("Mail sent.")
 #endif
+    );
+  if (msg->security & ENCRYPT)
+    p_delete(&pgpkeylist);
 
-  if (WithCrypto && (msg->security & ENCRYPT))
-    FREE (&pgpkeylist);
-  
-  if (WithCrypto && free_clear_content)
+  if (free_clear_content)
     mutt_free_body (&clear_content);
 
-  if (flags & SENDREPLY)
-  {
+  if (flags & SENDREPLY) {
     if (cur && ctx)
       mutt_set_flag (ctx, cur, M_REPLIED, 1);
-    else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged)
-    {
+    else if (!(flags & SENDPOSTPONED) && ctx && ctx->tagged) {
       for (i = 0; i < ctx->vcount; i++)
-       if (ctx->hdrs[ctx->v2r[i]]->tagged)
-         mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
+        if (ctx->hdrs[ctx->v2r[i]]->tagged)
+          mutt_set_flag (ctx, ctx->hdrs[ctx->v2r[i]], M_REPLIED, 1);
     }
   }
 
 
   rv = 0;
-  
+
 cleanup:
 
-  if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
-  {
-    if(signas)
-    {
-      FREE (&PgpSignAs);
+  if (flags & SENDPOSTPONED) {
+    if (signas) {
+      p_delete(&PgpSignAs);
       PgpSignAs = signas;
     }
   }
-   
+
   safe_fclose (&tempfp);
-  mutt_free_header (&msg);
-  
+  header_delete(&msg);
+
   return rv;
 }