2022-08-24 08:07:33 +00:00
|
|
|
#!/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
|
2024-07-27 05:00:02 +00:00
|
|
|
# tagged those emails `misdirected`, it iterates over those emails and
|
|
|
|
# responds.
|
2022-08-24 08:07:33 +00:00
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2024-07-27 05:00:02 +00:00
|
|
|
if [[ -z "$(notmuch search tag:misdirected)" ]] ; then
|
|
|
|
echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..."
|
2022-08-24 08:07:33 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# This weird feed-find-output-into-while-loop comes from
|
|
|
|
# https://github.com/koalaman/shellcheck/wiki/SC2044#correct-code
|
|
|
|
while IFS= read -r -d '' file ; do
|
|
|
|
echo "[loop] Preparing the email..."
|
|
|
|
|
|
|
|
# Make a temporary file
|
2024-07-27 05:00:02 +00:00
|
|
|
temporary_file=$(mktemp /tmp/autoreply-XXXXX)
|
|
|
|
echo "[debug] Temporary file: $temporary_file" # debug
|
2022-08-24 08:07:33 +00:00
|
|
|
|
2024-07-27 05:00:02 +00:00
|
|
|
# Pull sender (i.e. recipient of my email), subject, and date from email
|
2022-08-24 08:07:33 +00:00
|
|
|
recipient="$(perl -lane 'print if /^From:/' "$file" | cut -d' ' -f2-)" ### "Frantz Fanon <ffanon@riseup.net>"
|
|
|
|
subject="Re: $(perl -lane 'print if /^Subject:/' "$file" | cut -d' ' -f2-)" ### "Re: Lorem ipsum baby"
|
2024-07-27 05:00:02 +00:00
|
|
|
date="$(perl -lane 'print if /^Date:/' "$file" | cut -d' ' -f2-)"
|
2022-08-24 08:07:33 +00:00
|
|
|
|
|
|
|
# Copy canned message to temporary file
|
2024-07-27 05:00:02 +00:00
|
|
|
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"
|
|
|
|
printf "\n\nOn $date, $recipient wrote:\n" >> "$temporary_file"
|
|
|
|
|
2022-08-24 08:07:33 +00:00
|
|
|
# Append email body to temporary file
|
|
|
|
discard="0"
|
|
|
|
while IFS= read -r line ; do
|
|
|
|
if [[ $discard = "1" ]]; then
|
2024-07-27 05:00:02 +00:00
|
|
|
echo "> $line" >> "$temporary_file"
|
2022-08-24 08:07:33 +00:00
|
|
|
else
|
|
|
|
if [[ -z "$line" ]]; then
|
|
|
|
discard="1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done < "$file"
|
|
|
|
|
|
|
|
# Send the email!
|
2024-07-27 05:00:02 +00:00
|
|
|
printf "[debug] Outgoing email:\n$(cat "$temporary_file")\n" # debug
|
2022-08-24 08:07:33 +00:00
|
|
|
echo "[loop] Sending reply to $recipient ... "
|
2024-07-27 05:00:02 +00:00
|
|
|
cat "$temporary_file" | msmtp --read-envelope-from --read-recipients
|
|
|
|
cat "$temporary_file" | notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply
|
|
|
|
|
|
|
|
# Remove the misdirected tag
|
|
|
|
notmuch tag -misdirected -- tag:misdirected
|
2022-08-24 08:07:33 +00:00
|
|
|
|
|
|
|
echo "[loop] Done."
|
|
|
|
|
2024-07-27 05:00:02 +00:00
|
|
|
done < <(notmuch search --output=files --format=text0 tag:misdirected)
|
2022-08-24 08:07:33 +00:00
|
|
|
|
|
|
|
echo "[autoreply.sh] All done!"
|