Compare commits
No commits in common. "7e5c97de7896b26493960101edd7902b44a6ebae" and "e992db6c07247d375db1aaecfb0bb73671076fac" have entirely different histories.
7e5c97de78
...
e992db6c07
1 changed files with 37 additions and 53 deletions
|
@ -14,64 +14,48 @@ if [[ -z "$(notmuch search tag:misdirected)" ]] ; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
# This weird feed-find-output-into-while-loop comes from
|
||||||
# https://github.com/koalaman/shellcheck/wiki/SC2044#correct-code
|
# https://github.com/koalaman/shellcheck/wiki/SC2044#correct-code
|
||||||
while IFS= read -r -d '' thread ; do
|
while IFS= read -r -d '' file ; do
|
||||||
echo "[loop] Preparing the email..."
|
echo "[loop] Preparing the email..."
|
||||||
|
|
||||||
# Make a temporary file
|
# Make a temporary file
|
||||||
temporary_file=$(mktemp /tmp/autoreply-XXXXX)
|
temporary_file=$(mktemp /tmp/autoreply-XXXXX)
|
||||||
echo "[debug] Temporary file: $temporary_file"
|
echo "[debug] Temporary file: $temporary_file" # debug
|
||||||
|
|
||||||
# Parse out the thread-id
|
# Pull sender (i.e. recipient of my email), subject, and date from email
|
||||||
thread_id=$(echo "$thread" | cut -f2 -d':')
|
recipient="$(perl -lane 'print if /^From:/' "$file" | cut -d' ' -f2-)" ### "Frantz Fanon <ffanon@riseup.net>"
|
||||||
echo "[debug] thread_id: $thread_id"
|
subject="Re: $(perl -lane 'print if /^Subject:/' "$file" | cut -d' ' -f2-)" ### "Re: Lorem ipsum baby"
|
||||||
|
date="$(perl -lane 'print if /^Date:/' "$file" | cut -d' ' -f2-)"
|
||||||
# Parse out the reply headers and original message body
|
|
||||||
temporary_reply=$(mktemp /tmp/notmuch-reply-XXXXX)
|
|
||||||
echo "[debug] Temporary reply: $temporary_reply"
|
|
||||||
notmuch reply --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)"
|
|
||||||
orig_body="$(jq -r '.original.body[0].content[] | select(."content-type" == "text/plain") | .content' $temporary_reply)"
|
|
||||||
|
|
||||||
# Copy canned message to temporary file
|
# Copy canned message to temporary file
|
||||||
printf "From: %s\nSubject: %s\nTo: %s\nIn-Reply-To: %s\nReferences: %s\n\n" "$sender" "$subject" "$recipient" "$in_reply_to" "$references" >> "$temporary_file"
|
printf "To: $recipient\nFrom: adam@theadamcooper.com\nCc: amcooper@gmail.com\nDate: $(date +'%a, %d %b %Y %R %z')\nSubject: $subject\n\n" > "$temporary_file"
|
||||||
cat /home/adam/dotfiles/bin/autoreply/misdirected_email_autoreply.txt >> "$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"
|
printf "\n\nOn $date, $recipient wrote:\n" >> "$temporary_file"
|
||||||
|
|
||||||
# Append email body to temporary file
|
# Append email body to temporary file
|
||||||
printf "%s\n" "$orig_body" >> "$temporary_file"
|
discard="0"
|
||||||
|
while IFS= read -r line ; do
|
||||||
|
if [[ $discard = "1" ]]; then
|
||||||
|
echo "> $line" >> "$temporary_file"
|
||||||
|
else
|
||||||
|
if [[ -z "$line" ]]; then
|
||||||
|
discard="1"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < "$file"
|
||||||
|
|
||||||
# Send the email!
|
# Send the email!
|
||||||
printf "[debug] Outgoing mail:\n%s\n" "$(cat $temporary_file)"
|
printf "[debug] Outgoing email:\n$(cat "$temporary_file")\n" # debug
|
||||||
if [[ -z "$dry_run" ]] ; then
|
|
||||||
echo "[loop] Sending reply to $recipient ... "
|
echo "[loop] Sending reply to $recipient ... "
|
||||||
msmtp --read-envelope-from --read-recipients <"$temporary_file"
|
cat "$temporary_file" | msmtp --read-envelope-from --read-recipients
|
||||||
notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply <"$temporary_file"
|
cat "$temporary_file" | notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply
|
||||||
|
|
||||||
# Remove the misdirected tag
|
# Remove the misdirected tag
|
||||||
notmuch tag -misdirected -- thread:"$thread_id"
|
notmuch tag -misdirected -- tag:misdirected
|
||||||
fi
|
|
||||||
|
|
||||||
printf "%s\n\n" "[loop] Done."
|
echo "[loop] Done."
|
||||||
|
|
||||||
done < <(notmuch search --output=threads --format=text0 tag:misdirected)
|
done < <(notmuch search --output=files --format=text0 tag:misdirected)
|
||||||
|
|
||||||
echo "[autoreply.sh] All done!"
|
echo "[autoreply.sh] All done!"
|
||||||
|
|
Loading…
Reference in a new issue