Skip to content

Instantly share code, notes, and snippets.

@prati0100
Last active November 9, 2019 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prati0100/7f302c75663999a70fc0f676ec787396 to your computer and use it in GitHub Desktop.
Save prati0100/7f302c75663999a70fc0f676ec787396 to your computer and use it in GitHub Desktop.
A script to use with neomutt to save a list of tagged emails.
#!/bin/bash
# This script is used to save a list of emails from neomutt's pipe. Be sure to
# set pipe_split in neomutt.
#
# Pass the directory name as the first argument. The email content is expected
# to be in stdin.
#
# The second argument is optional. It can be used to force the patch version to
# be something else. For example, pass v3 as the second argument. Even if the
# patch subject has v2 in it, it will be saved as v3. I use this when some
# people don't properly version their patches, and I want to avoid manually
# renaming the files.
# Create the target directory if it doesn't already exist.
mkdir -p "$1"
if test $? -ne 0; then
exit 1
fi
message=$(< /dev/stdin)
# Figure out a name for the file. The following logic is a bit flaky. Might
# mess up somewhere.
subject=$(echo "$message" | grep '^Subject:' | head -n 1 | sed 's/Subject: //')
if test -z subject; then
echo "Error! No subject line found"
exit 1
fi
# Remove all blocks wrapped in square brackets. Then remove all initial
# whitespace.
stripped_subject=$(echo "$subject" | sed 's/\[.*\]//g' | sed 's/^[[:space:]]*//')
file_ext=".mbox"
is_patch=$(echo "$subject" | grep '\[.*PATCH.*\]' | wc -l)
if test $is_patch -gt 0; then
patch_num=$(echo "$subject" | grep -o '\[.*PATCH.*\]' | grep -oE '[0-9]+/[0-9]+' | cut -d '/' -f 1)
# If no patch number was found, set it to 1.
if test -z $patch_num; then
patch_num=1
fi
# Make it a 4-digit number, just like the output from git-format-patch.
patch_num=$(printf "%04d" "$patch_num")
patch_version=$(echo "$subject" | grep -o '\[.*PATCH.*\]' | grep -oE 'v[0-9]+')
if test -z $patch_version; then
patch_version="v1"
fi
# If the second argument is supplied, over-ride the patch version.
if test -n "$2"; then
patch_version="$2"
fi
file_ext=".patch"
filename="$patch_version-$patch_num-"
fi
# Convert all non-alpha characters to '-', and remove consecutive dashes.
filename="$filename$(echo "$stripped_subject" | sed 's/[^[[:alpha:]]/-/g' | sed 's/---*/-/g')"
# Strip the filename to 60 characters.
filename=$(echo "$filename" | cut -b -60)
# Add the directory and extension.
filename="$1/$filename$file_ext"
# Make sure the file does not already exist.
if test -e "$filename"; then
echo "Error! $filename already exists"
exit 1
fi
echo "$message" > $filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment