If -H option was used, it was not possible to redefine recipient address with
positional argument. This patch fixes it as well as CC, Bcc, and Subject
headers.

This patch is based on the patch by Petr Písař at
http://dev.mutt.org/trac/attachment/ticket/3580/override_draft_headers_with_arguments.patch

This version of the patch merges the To, Cc, and Bcc commandline
arguments into the template, and fixes a few small problems with the
previous patch.

This patch also fixes the following mutt problems:
- Moves the draftFile reading inside the "if (infile)" block.
  Otherwise for the case (!infile && bodytext) we would try to read from
  a NULL fin inside mutt_read_rfc822_header()

- Move the fin close outside the "if (tempfile)" block


 main.c |  90 +++++++++++++++++++++++++++++++++++------------------------------
 1 files changed, 48 insertions(+), 42 deletions(-)


# HG changeset patch
# User Kevin McCarthy <ke...@8t8.us>
# Date 1383432230 25200
#      Sat Nov 02 15:43:50 2013 -0700
# Node ID 2134ed78942881a9a4c5a3c9f98f9a22ddc5c41c
# Parent  3306cb186f49e83edf15aac91c51f4c6131ef8fe
Override draft headers with arguments. (closes #3580)

If -H option was used, it was not possible to redefine recipient address with
positional argument. This patch fixes it as well as CC, Bcc, and Subject
headers.

This patch is based on the patch by Petr Písař at
http://dev.mutt.org/trac/attachment/ticket/3580/override_draft_headers_with_arguments.patch

This version of the patch merges the To, Cc, and Bcc commandline
arguments into the template, and fixes a few small problems with the
previous patch.

This patch also fixes the following mutt problems:
- Moves the draftFile reading inside the "if (infile)" block.
  Otherwise for the case (!infile && bodytext) we would try to read from
  a NULL fin inside mutt_read_rfc822_header()

- Move the fin close outside the "if (tempfile)" block

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -861,54 +861,50 @@
     char *bodytext = NULL;
     int rv = 0;
     
     if (!option (OPTNOCURSES))
       mutt_flushinp ();
 
     if (!msg)
       msg = mutt_new_header ();
+    if (!msg->env)
+      msg->env = mutt_new_envelope ();
+
+    for (i = optind; i < argc; i++)
+    {
+      if (url_check_scheme (argv[i]) == U_MAILTO)
+      {
+        if (url_parse_mailto (msg->env, &bodytext, argv[i]) < 0)
+        {
+          if (!option (OPTNOCURSES))
+            mutt_endwin (NULL);
+          fputs (_("Failed to parse mailto: link\n"), stderr);
+          exit (1);
+        }
+      }
+      else
+        msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
+    }
+
+    if (!draftFile && option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
+    {
+      if (!option (OPTNOCURSES))
+        mutt_endwin (NULL);
+      fputs (_("No recipients specified.\n"), stderr);
+      exit (1);
+    }
+
+    if (subject)
+      msg->env->subject = safe_strdup (subject);
 
     if (draftFile)
       infile = draftFile;
-    else
-    {
-      if (!msg->env)
-	msg->env = mutt_new_envelope ();
-
-      for (i = optind; i < argc; i++)
-      {
-	if (url_check_scheme (argv[i]) == U_MAILTO)
-	{
-	  if (url_parse_mailto (msg->env, &bodytext, argv[i]) < 0)
-	  {
-	    if (!option (OPTNOCURSES))
-	      mutt_endwin (NULL);
-	    fputs (_("Failed to parse mailto: link\n"), stderr);
-	    exit (1);
-	  }
-	}
-	else
-	  msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
-      }
-
-      if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
-      {
-	if (!option (OPTNOCURSES))
-	  mutt_endwin (NULL);
-	fputs (_("No recipients specified.\n"), stderr);
-	exit (1);
-      }
-
-      if (subject)
-	msg->env->subject = safe_strdup (subject);
-
-      if (includeFile)
-	infile = includeFile;
-    }
+    else if (includeFile)
+      infile = includeFile;
 
     if (infile || bodytext)
     {
       if (infile)
       {
 	if (mutt_strcmp ("-", infile) == 0)
 	  fin = stdin;
 	else 
@@ -920,26 +916,35 @@
 	  if ((fin = fopen (path, "r")) == NULL)
 	  {
 	    if (!option (OPTNOCURSES))
 	      mutt_endwin (NULL);
 	    perror (path);
 	    exit (1);
 	  }
 	}
+
+        if (draftFile)
+        {
+          ENVELOPE *opts_env = msg->env;
+          msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
+
+          rfc822_append (&msg->env->to, opts_env->to, 0);
+          rfc822_append (&msg->env->cc, opts_env->cc, 0);
+          rfc822_append (&msg->env->bcc, opts_env->bcc, 0);
+          if (opts_env->subject)
+            mutt_str_replace (&msg->env->subject, opts_env->subject);
+
+          mutt_free_envelope (&opts_env);
+        }
       }
-      else
-	fin = NULL;
 
       mutt_mktemp (buf, sizeof (buf));
       tempfile = safe_strdup (buf);
 
-      if (draftFile)
-	msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
-
       /* is the following if still needed? */
       
       if (tempfile)
       {
 	FILE *fout;
 
 	if ((fout = safe_fopen (tempfile, "w")) == NULL)
 	{
@@ -950,19 +955,20 @@
 	  FREE (&tempfile);
 	  exit (1);
 	}
 	if (fin)
 	  mutt_copy_stream (fin, fout);
 	else if (bodytext)
 	  fputs (bodytext, fout);
 	safe_fclose (&fout);
-	if (fin && fin != stdin)
-	  safe_fclose (&fin);
       }
+
+      if (fin && fin != stdin)
+        safe_fclose (&fin);
     }
 
     FREE (&bodytext);
     
     if (attach)
     {
       LIST *t = attach;
       BODY *a = NULL;

Reply via email to