82 lines
3.3 KiB
Bash
Executable file
82 lines
3.3 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# autoreply.sh
|
|
#
|
|
# This script sends a canned email response to those who've errone-
|
|
# ously emailed me at <amcooper@gmail.com>. Once I've manually
|
|
# tagged those emails `misdirected`, it iterates over those emails and
|
|
# responds.
|
|
|
|
set -euo pipefail
|
|
|
|
if [[ -z "$(notmuch search tag:misdirected)" ]] ; then
|
|
echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..."
|
|
exit 1
|
|
fi
|
|
|
|
# Handle option(s)
|
|
dry_run=
|
|
while getopts n opt ; do
|
|
printf "[debug] opt: %s\n\n" $opt
|
|
case $opt in
|
|
n) dry_run=1;;
|
|
?) echo "Usage: /path/to/autoreply.sh/ [ -n ]"
|
|
exit 2;;
|
|
esac
|
|
done
|
|
|
|
# This weird feed-find-output-into-while-loop comes from
|
|
# https://github.com/koalaman/shellcheck/wiki/SC2044#correct-code
|
|
while IFS= read -r -d '' thread ; do
|
|
echo "[loop] Preparing the email..."
|
|
|
|
# Make a temporary file
|
|
temporary_file=$(mktemp /tmp/autoreply-XXXXX)
|
|
echo "[debug] Temporary file: $temporary_file"
|
|
|
|
# Parse out the thread-id
|
|
thread_id=$(echo "$thread" | cut -f2 -d':')
|
|
echo "[debug] thread_id: $thread_id"
|
|
|
|
# Parse out the reply headers and original message body
|
|
temporary_reply=$(mktemp /tmp/notmuch-reply-XXXXX)
|
|
echo "[debug] Temporary reply: $temporary_reply"
|
|
notmuch reply --reply-to=sender --format=json thread:$thread_id > $temporary_reply
|
|
subject="$(jq -r '."reply-headers".Subject' $temporary_reply)"
|
|
sender="$(jq -r '."reply-headers".From' $temporary_reply)"
|
|
recipient="$(jq -r '."reply-headers".To' $temporary_reply)"
|
|
in_reply_to="$(jq -r '."reply-headers"."In-reply-to"' $temporary_reply)"
|
|
references="$(jq -r '."reply-headers".References' $temporary_reply)"
|
|
orig_sender="$(jq -r '.original.headers.From' $temporary_reply)"
|
|
date="$(jq -r '.original.headers.Date' $temporary_reply)"
|
|
|
|
# We have to check for multipart messages.
|
|
orig_body="$(jq -r '.original.body[] | select( ."content-type" == "text/plain" ) | .content' $temporary_reply)"
|
|
if [[ -z $orig_body ]] ; then
|
|
orig_body="$(jq -r '.original.body[] | select( ."content-type" == "multipart/alternative" ) | .content[] | select( ."content-type" == "text/plain" ) | .content' $temporary_reply)"
|
|
fi
|
|
|
|
# Copy canned message to temporary file
|
|
printf "From: %s\nSubject: %s\nTo: %s\nCc: amcooper@gmail.com\nDate: $(date +'%a, %d %b %Y %R %z')\nIn-Reply-To: %s\nReferences: %s\n\n" "$sender" "$subject" "$recipient" "$in_reply_to" "$references" >> "$temporary_file"
|
|
cat /home/adam/dotfiles/bin/autoreply/misdirected_email_autoreply.txt >> "$temporary_file"
|
|
printf "\n\nOn %s, %s wrote:\n" "$date" "$orig_sender" >> "$temporary_file"
|
|
|
|
# Append email body to temporary file (with prefixed quote marker `> `)
|
|
echo "$orig_body" | sed 's/^/\> /g' >> "$temporary_file"
|
|
|
|
# Send the email!
|
|
printf "[debug] Outgoing mail:\n%s\n" "$(cat $temporary_file)"
|
|
if [[ -z "$dry_run" ]] ; then
|
|
echo "[loop] Sending reply to $recipient ... "
|
|
msmtp --read-envelope-from --read-recipients <"$temporary_file"
|
|
notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply <"$temporary_file"
|
|
|
|
# Remove the misdirected tag
|
|
notmuch tag -misdirected -- thread:"$thread_id"
|
|
fi
|
|
|
|
printf "%s\n\n" "[loop] Done."
|
|
|
|
done < <(notmuch search --output=threads --format=text0 tag:misdirected)
|
|
|
|
echo "[autoreply.sh] All done!"
|