[PATCH] sh-function: add insert_header function

Jacob Keller jacob.e.keller at intel.com
Fri Mar 14 15:46:38 PDT 2014


Since we use a special trick to insert header information into the mbox,
this could be extraced into a function in aiaiai-sh-functions. This will
help future work which will want to add multiple headers into this mbox
file, using the same file descriptor trick.

Signed-off-by: Jacob Keller <jacob.e.keller at intel.com>
---
 email/aiaiai-email-lda      | 17 ++---------------
 helpers/aiaiai-sh-functions | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/email/aiaiai-email-lda b/email/aiaiai-email-lda
index c0a18875ff20..5db3f15e9918 100755
--- a/email/aiaiai-email-lda
+++ b/email/aiaiai-email-lda
@@ -217,24 +217,11 @@ queue_series()
 		# later.
 		local subj="$(fetch_header "Subject" < "$dir/0")"
 		subj="X-Aiaiai-Cover-Letter-Subject: $subj"
+		insert_header "$mbox" "$subj"
 
 		local id="$(fetch_header "Message-Id" < "$dir/0")"
 		id="X-Aiaiai-Cover-Letter-Message-Id: $id"
-
-		# The below trick allows us to avoid creating a separate
-		# temporary file: open the "$mbox" file, unlink, use the open
-		# file descriptor for reading and redirect the output to the
-		# new version of the "$mbox" file. We could instead use the
-		# "sponge" tool, though.
-		exec 3<$mbox
-		rm $verbose "$mbox" >&2
-		verbose "Adding \"$subj\""
-		formail -s formail -I "$subj" <&3 > "$mbox"
-
-		exec 3<$mbox
-		rm $verbose "$mbox" >&2
-		verbose "Adding \"$id\""
-		formail -s formail -I "$id" <&3 > "$mbox"
+		insert_header "$mbox" "$id"
 	fi
 	queue_mboxfile "$mbox" "$n"
 	rm $verbose -rf -- "$dir" >&2
diff --git a/helpers/aiaiai-sh-functions b/helpers/aiaiai-sh-functions
index 8d155705eb7b..b515cf02eba2 100644
--- a/helpers/aiaiai-sh-functions
+++ b/helpers/aiaiai-sh-functions
@@ -132,6 +132,24 @@ fetch_header_per_patch()
 	formail -s sh -c "formail -z -c -x \"$hdr:\" | head -n1" | aiaiai-decode-rfc-2047
 }
 
+# Insert a header into the given mbox file
+# Usage: insert_header <mbox_file> <header>
+insert_header()
+{
+	local mbox="$1"; shift
+	local header="$1"; shift
+
+	# The below trick allows us to avoid creating a separate temporary
+	# file; open the "$mbox" file, unlink, use the open file descriptor for
+	# reading and redirect the output to the new version of the "$mbox"
+	# file. We could instead use the "sponge" tool, however.
+	exec 3<$mbox
+	rm $verbose "$mbox" >&2
+	verbose "Adding \"$header\""
+	formail -s formail -I "$header" <&3 > "$mbox"
+	exec 3<&-
+}
+
 git_dir()
 {
 	local path="$1"
-- 
1.8.3.1




More information about the aiaiai mailing list