less horrible strncpy's
[apps/madmutt.git] / send.c
diff --git a/send.c b/send.c
index a6da714..01fde6c 100644 (file)
--- a/send.c
+++ b/send.c
 # include "config.h"
 #endif
 
+#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 "mutt.h"
 #include "enter.h"
-#include "ascii.h"
 #include "mutt_curses.h"
 #include "rfc2047.h"
+#include "rfc3676.h"
 #include "keymap.h"
 #include "mime.h"
 #include "copy.h"
@@ -25,9 +31,6 @@
 #include "url.h"
 #include "attach.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
 #include "lib/debug.h"
 
 #include <ctype.h>
@@ -56,9 +59,15 @@ static void append_signature (FILE * f)
   FILE *tmpfp;
   pid_t thepid;
 
+  if (SignOffString) {
+    fprintf (f, "\n%s", SignOffString);
+  }
+
   if (Signature && (tmpfp = mutt_open_read (Signature, &thepid))) {
     if (option (OPTSIGDASHES))
       fputs ("\n-- \n", f);
+    else if (SignOffString)
+      fputs ("\n", f);
     mutt_copy_stream (tmpfp, f);
     fclose (tmpfp);
     if (thepid != -1)
@@ -171,7 +180,7 @@ static ADDRESS *find_mailing_lists (ADDRESS * t, ADDRESS * c)
   return top;
 }
 
-static int edit_address (ADDRESS ** a, /* const */ char *field)
+static int edit_address (ADDRESS ** a, const char *field)
 {
   char buf[HUGE_STRING];
   char *err = NULL;
@@ -189,7 +198,7 @@ static int edit_address (ADDRESS ** a, /* const */ char *field)
       mutt_error (_("Error: '%s' is a bad IDN."), err);
       mutt_refresh ();
       mutt_sleep (2);
-      mem_free (&err);
+      p_delete(&err);
     }
   }
   while (idna_ok != 0);
@@ -210,8 +219,8 @@ static int edit_envelope (ENVELOPE * en, int flags)
       buf[0] = 0;
     if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    mem_free (&en->newsgroups);
-    en->newsgroups = str_dup (buf);
+    p_delete(&en->newsgroups);
+    en->newsgroups = m_strdup(buf);
 
     if (en->followup_to)
       strfcpy (buf, en->followup_to, sizeof (buf));
@@ -220,8 +229,8 @@ static int edit_envelope (ENVELOPE * en, int flags)
     if (option (OPTASKFOLLOWUP)
         && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    mem_free (&en->followup_to);
-    en->followup_to = str_dup (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));
@@ -230,8 +239,8 @@ static int edit_envelope (ENVELOPE * en, int flags)
     if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO)
         && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
       return (-1);
-    mem_free (&en->x_comment_to);
-    en->x_comment_to = str_dup (buf);
+    p_delete(&en->x_comment_to);
+    en->x_comment_to = m_strdup(buf);
   }
   else
 #endif
@@ -258,7 +267,7 @@ static int edit_envelope (ENVELOPE * en, int flags)
       if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) {
         p = uh->data + 8;
         SKIPWS (p);
-        strncpy (buf, p, sizeof (buf));
+        m_strcpy(buf, sizeof(buf), p);
       }
     }
   }
@@ -297,7 +306,7 @@ static int edit_envelope (ENVELOPE * en, int flags)
 char *nntp_get_header (const char *s)
 {
   SKIPWS (s);
-  return str_dup (s);
+  return m_strdup(s);
 }
 #endif
 
@@ -360,7 +369,7 @@ static void process_user_header (ENVELOPE * env)
       }
       else
         last = env->userhdrs = mutt_new_list ();
-      last->data = str_dup (uh->data);
+      last->data = m_strdup(uh->data);
     }
   }
 }
@@ -429,6 +438,7 @@ void mutt_make_post_indent (CONTEXT * ctx, HEADER * cur, FILE * out)
   if (PostIndentString) {
     mutt_make_string (buffer, sizeof (buffer), PostIndentString, ctx, cur);
     fputs (buffer, out);
+    fputc ('\n', out);
   }
 }
 
@@ -482,10 +492,9 @@ 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:
+    case M_YES:
       tmp = find_mailing_lists (env->to, env->cc);
       rfc822_append (to, tmp);
       rfc822_free_address (&tmp);
@@ -595,7 +604,7 @@ LIST *mutt_make_references (ENVELOPE * e)
 
   if (e->message_id) {
     t = mutt_new_list ();
-    t->data = str_dup (e->message_id);
+    t->data = m_strdup(e->message_id);
     t->next = l;
     l = t;
   }
@@ -619,6 +628,11 @@ void mutt_fix_reply_recipients (ENVELOPE * env)
   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)
@@ -637,16 +651,16 @@ void mutt_make_misc_reply_headers (ENVELOPE * env, CONTEXT * ctx,
    * been taken from a List-Post header.  Is that correct?
    */
   if (curenv->real_subj) {
-    mem_free (&env->subject);
-    env->subject = mem_malloc (str_len (curenv->real_subj) + 5);
+    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 = str_dup ("Re: your mail");
+    env->subject = m_strdup("Re: your mail");
 
 #ifdef USE_NNTP
   if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
-    env->x_comment_to = str_dup (mutt_get_name (curenv->from));
+    env->x_comment_to = m_strdup(mutt_get_name (curenv->from));
 #endif
 }
 
@@ -674,7 +688,7 @@ void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv,
 
   if (curenv->message_id) {
     *q = mutt_new_list ();
-    (*q)->data = str_dup (curenv->message_id);
+    (*q)->data = m_strdup(curenv->message_id);
   }
 
   if (pp)
@@ -738,7 +752,7 @@ envelope_defaults (ENVELOPE * env, CONTEXT * ctx, HEADER * cur, int flags)
       /* in case followup set Newsgroups: with Followup-To: if it present */
       if (!env->newsgroups && curenv &&
           str_casecmp (curenv->followup_to, "poster"))
-        env->newsgroups = str_dup (curenv->followup_to);
+        env->newsgroups = m_strdup(curenv->followup_to);
     }
     else
 #endif
@@ -881,7 +895,7 @@ void mutt_set_followup_to (ENVELOPE * e)
 #ifdef USE_NNTP
   if (option (OPTNEWSSEND)) {
     if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
-      e->followup_to = str_dup (e->newsgroups);
+      e->followup_to = m_strdup(e->newsgroups);
     return;
   }
 #endif
@@ -952,9 +966,9 @@ static ADDRESS *set_reverse_name (ENVELOPE * env)
   if (tmp) {
     tmp = rfc822_cpy_adr_real (tmp);
     if (!option (OPTREVREAL))
-      mem_free (&tmp->personal);
+      p_delete(&tmp->personal);
     if (!tmp->personal)
-      tmp->personal = str_dup (Realname);
+      tmp->personal = m_strdup(Realname);
   }
   return (tmp);
 }
@@ -973,13 +987,12 @@ ADDRESS *mutt_default_from (void)
     adr = rfc822_cpy_adr_real (From);
   else if (option (OPTUSEDOMAIN)) {
     adr = rfc822_new_address ();
-    adr->mailbox =
-      mem_malloc (str_len (Username) + str_len (fqdn) + 2);
+    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 = str_dup (NONULL (Username));
+    adr->mailbox = m_strdup(NONULL (Username));
   }
 
   return (adr);
@@ -1057,6 +1070,19 @@ static void decode_descriptions (BODY * b)
   }
 }
 
+static void fix_end_of_file (const char *data)
+{
+  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 = mutt_new_header ();
@@ -1086,8 +1112,8 @@ int ci_send_message (int flags, /* send mode */
   char *pgpkeylist = NULL;
 
   /* save current value of "pgp_sign_as" */
-  char *signas = NULL;
-  char *tag = NULL, *err = NULL;
+  char *signas = NULL, *err = NULL;
+  const char *tag = NULL;
   char *ctype;
 
   int rv = -1;
@@ -1114,7 +1140,7 @@ int ci_send_message (int flags, /* send mode */
 
 
   if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED))
-    signas = str_dup (PgpSignAs);
+    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
@@ -1169,25 +1195,29 @@ int ci_send_message (int flags, /* send mode */
     pbody->next = msg->content; /* don't kill command-line attachments */
     msg->content = pbody;
 
-    ctype = str_dup (ContentType);
+    if (!(ctype = m_strdup(ContentType)))
+      ctype = m_strdup("text/plain");
     mutt_parse_content_type (ctype, msg->content);
-    mem_free (&ctype);
+    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"))
+        && !ascii_strcasecmp (msg->content->subtype, "plain")) {
       mutt_set_parameter ("format", "flowed", &msg->content->parameter);
+      if (option (OPTDELSP))
+        mutt_set_parameter ("delsp", "yes", &msg->content->parameter);
+    }
 
     if (!tempfile) {
       mutt_mktemp (buffer);
       tempfp = safe_fopen (buffer, "w+");
-      msg->content->filename = str_dup (buffer);
+      msg->content->filename = m_strdup(buffer);
     }
     else {
       tempfp = safe_fopen (tempfile, "a+");
-      msg->content->filename = str_dup (tempfile);
+      msg->content->filename = m_strdup(tempfile);
     }
 
     if (!tempfp) {
@@ -1241,7 +1271,7 @@ int ci_send_message (int flags, /* send mode */
 #ifdef USE_NNTP
     if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP
         && !msg->env->newsgroups)
-      msg->env->newsgroups = str_dup (((NNTP_DATA *) ctx->data)->group);
+      msg->env->newsgroups = m_strdup(((NNTP_DATA *) ctx->data)->group);
 #endif
 
     if (!(flags & SENDMAILX) &&
@@ -1296,7 +1326,7 @@ int ci_send_message (int flags, /* send mode */
 
     if (option (OPTSIGONTOP)
         && (!(flags & (SENDMAILX | SENDKEY)) && Editor
-            && str_cmp (Editor, "builtin") != 0))
+            && m_strcmp(Editor, "builtin") != 0))
       append_signature (tempfp);
 
     /* include replies/forwarded messages, unless we are given a template */
@@ -1306,7 +1336,7 @@ int ci_send_message (int flags, /* send mode */
 
     if (!option (OPTSIGONTOP)
         && (!(flags & (SENDMAILX | SENDKEY)) && Editor
-            && str_cmp (Editor, "builtin") != 0))
+            && m_strcmp(Editor, "builtin") != 0))
       append_signature (tempfp);
 
     /* 
@@ -1386,7 +1416,7 @@ int ci_send_message (int flags, /* send mode */
      that $realname can be set in a send-hook */
   if (msg->env->from && !msg->env->from->personal
       && !(flags & (SENDRESEND | SENDPOSTPONED)))
-    msg->env->from->personal = str_dup (Realname);
+    msg->env->from->personal = m_strdup(Realname);
 
   if (!((WithCrypto & APPLICATION_PGP) && (flags & SENDKEY)))
     safe_fclose (&tempfp);
@@ -1417,9 +1447,10 @@ int ci_send_message (int flags, /* send mode */
          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 || str_cmp ("builtin", Editor) == 0)
+      if (mutt_needs_mailcap (msg->content)) {
+        if (!mutt_edit_attachment (msg->content))
+          goto cleanup;
+      } else if (!Editor || m_strcmp("builtin", Editor) == 0)
         mutt_builtin_editor (msg->content->filename, msg, cur);
       else if (option (OPTEDITHDRS)) {
         mutt_env_to_local (msg->env);
@@ -1427,9 +1458,19 @@ int ci_send_message (int flags, /* send mode */
                            sizeof (fcc));
         mutt_env_to_idna (msg->env, NULL, NULL);
       }
-      else
+      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);
     }
 
@@ -1538,7 +1579,7 @@ int ci_send_message (int flags, /* send mode */
 
   if (mutt_env_to_idna (msg->env, &tag, &err)) {
     mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
-    mem_free (&err);
+    p_delete(&err);
     if (!(flags & SENDBATCH))
       goto main_loop;
     else
@@ -1572,6 +1613,20 @@ int ci_send_message (int flags, /* send mode */
   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"
@@ -1599,7 +1654,7 @@ int ci_send_message (int flags, /* send mode */
           mutt_protect (msg, pgpkeylist) == -1) {
         msg->content = mutt_remove_multipart (msg->content);
 
-        mem_free (&pgpkeylist);
+        p_delete(&pgpkeylist);
 
         decode_descriptions (msg->content);
         goto main_loop;
@@ -1647,7 +1702,7 @@ int ci_send_message (int flags, /* send mode */
     fcc[0] = '\0';
 #endif
 
-  if (*fcc && str_cmp ("/dev/null", fcc) != 0) {
+  if (*fcc && m_strcmp("/dev/null", fcc) != 0) {
     BODY *tmpbody = msg->content;
     BODY *save_sig = NULL;
     BODY *save_parts = NULL;
@@ -1658,8 +1713,8 @@ int ci_send_message (int flags, /* send mode */
     /* check to see if the user wants copies of all attachments */
     if (!option (OPTFCCATTACH) && msg->content->type == TYPEMULTIPART) {
       if (WithCrypto
-          && (str_cmp (msg->content->subtype, "encrypted") == 0 ||
-              str_cmp (msg->content->subtype, "signed") == 0)) {
+          && (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 */
@@ -1763,7 +1818,7 @@ int ci_send_message (int flags, /* send mode */
 #endif
 
   if (WithCrypto && (msg->security & ENCRYPT))
-    mem_free (&pgpkeylist);
+    p_delete(&pgpkeylist);
 
   if (WithCrypto && free_clear_content)
     mutt_free_body (&clear_content);
@@ -1785,7 +1840,7 @@ cleanup:
 
   if ((WithCrypto & APPLICATION_PGP) && (flags & SENDPOSTPONED)) {
     if (signas) {
-      mem_free (&PgpSignAs);
+      p_delete(&PgpSignAs);
       PgpSignAs = signas;
     }
   }