bin: Significant refactor of the autoreply script

This commit is contained in:
Adam Cooper 2024-07-27 01:00:02 -04:00
parent b49716c6f1
commit e992db6c07

View file

@ -4,15 +4,13 @@
#
# This script sends a canned email response to those who've errone-
# ously emailed me at <amcooper@gmail.com>. Once I've manually
# moved those emails into the `misdirected` folder, it iterates
# over those emails and responds.
# tagged those emails `misdirected`, it iterates over those emails and
# responds.
set -euo pipefail
MISDIRECTED_DIR=/home/adam/Maildir/misdirected
if [[ -z "$(ls -A "$MISDIRECTED_DIR/cur")" ]] && [[ -z "$(ls -A "$MISDIRECTED_DIR/new")" ]]; then
echo "[autoreply.sh] The misdirected folders are currently empty. Exiting..."
if [[ -z "$(notmuch search tag:misdirected)" ]] ; then
echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..."
exit 1
fi
@ -22,20 +20,24 @@ while IFS= read -r -d '' file ; do
echo "[loop] Preparing the email..."
# Make a temporary file
temporary_file=$(mktemp /tmp/autoreplyXXXXX)
temporary_file=$(mktemp /tmp/autoreply-XXXXX)
echo "[debug] Temporary file: $temporary_file" # debug
# Pull sender (i.e. recipient of my email) and subject from email
# Pull sender (i.e. recipient of my email), subject, and date from email
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"
date="$(perl -lane 'print if /^Date:/' "$file" | cut -d' ' -f2-)"
# Copy canned message to temporary file
cp /home/adam/dotfiles/arch/bin/autoreply/misdirected_email_autoreply.txt "$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"
printf "\n\nOn $date, $recipient wrote:\n" >> "$temporary_file"
# Append email body to temporary file
discard="0"
while IFS= read -r line ; do
if [[ $discard = "1" ]]; then
echo "$line" >> "$temporary_file"
echo "> $line" >> "$temporary_file"
else
if [[ -z "$line" ]]; then
discard="1"
@ -44,15 +46,16 @@ while IFS= read -r -d '' file ; do
done < "$file"
# Send the email!
printf "[debug] Outgoing email:\n$(cat "$temporary_file")\n" # debug
echo "[loop] Sending reply to $recipient ... "
neomutt -s "$subject" -c amcooper@gmail.com "$recipient" < "$temporary_file"
cat "$temporary_file" | msmtp --read-envelope-from --read-recipients
cat "$temporary_file" | notmuch insert --folder=Sent -inbox -unread +sent +misdirected-reply
# Move the misdirected email into `INBOX/cur`
echo "[loop] Moving the emails..."
mv "$file" /home/adam/Maildir/INBOX/cur
# Remove the misdirected tag
notmuch tag -misdirected -- tag:misdirected
echo "[loop] Done."
done < <(find "$MISDIRECTED_DIR" -path "$MISDIRECTED_DIR/tmp" -prune -o -type f -print0)
done < <(notmuch search --output=files --format=text0 tag:misdirected)
echo "[autoreply.sh] All done!"