Compare commits

...

2 commits

Author SHA1 Message Date
7e5c97de78 Small tweaks 2024-08-03 11:49:52 -04:00
ccc1ff6a21 [bin][autoreply] Nearly working;
messages are not being saved to the Sent folder
2024-08-03 10:39:40 -04:00

View file

@ -10,52 +10,68 @@
set -euo pipefail set -euo pipefail
if [[ -z "$(notmuch search tag:misdirected)" ]] ; then if [[ -z "$(notmuch search tag:misdirected)" ]] ; then
echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..." echo "[autoreply.sh] Currently no emails are tagged 'misdirected'. Exiting..."
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 '' file ; do while IFS= read -r -d '' thread ; 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" # debug echo "[debug] Temporary file: $temporary_file"
# Pull sender (i.e. recipient of my email), subject, and date from email # Parse out the thread-id
recipient="$(perl -lane 'print if /^From:/' "$file" | cut -d' ' -f2-)" ### "Frantz Fanon <ffanon@riseup.net>" thread_id=$(echo "$thread" | cut -f2 -d':')
subject="Re: $(perl -lane 'print if /^Subject:/' "$file" | cut -d' ' -f2-)" ### "Re: Lorem ipsum baby" echo "[debug] thread_id: $thread_id"
date="$(perl -lane 'print if /^Date:/' "$file" | cut -d' ' -f2-)"
# Copy canned message to temporary file # Parse out the reply headers and original message body
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" temporary_reply=$(mktemp /tmp/notmuch-reply-XXXXX)
cat /home/adam/dotfiles/bin/autoreply/misdirected_email_autoreply.txt >> "$temporary_file" echo "[debug] Temporary reply: $temporary_reply"
printf "\n\nOn $date, $recipient wrote:\n" >> "$temporary_file" 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)"
# Append email body to temporary file # Copy canned message to temporary file
discard="0" printf "From: %s\nSubject: %s\nTo: %s\nIn-Reply-To: %s\nReferences: %s\n\n" "$sender" "$subject" "$recipient" "$in_reply_to" "$references" >> "$temporary_file"
while IFS= read -r line ; do cat /home/adam/dotfiles/bin/autoreply/misdirected_email_autoreply.txt >> "$temporary_file"
if [[ $discard = "1" ]]; then printf "\n\nOn %s, %s wrote:\n" "$date" "$orig_sender" >> "$temporary_file"
echo "> $line" >> "$temporary_file"
else
if [[ -z "$line" ]]; then
discard="1"
fi
fi
done < "$file"
# Send the email! # Append email body to temporary file
printf "[debug] Outgoing email:\n$(cat "$temporary_file")\n" # debug printf "%s\n" "$orig_body" >> "$temporary_file"
echo "[loop] Sending reply to $recipient ... "
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 # Send the email!
notmuch tag -misdirected -- tag:misdirected 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"
echo "[loop] Done." # Remove the misdirected tag
notmuch tag -misdirected -- thread:"$thread_id"
fi
done < <(notmuch search --output=files --format=text0 tag:misdirected) printf "%s\n\n" "[loop] Done."
done < <(notmuch search --output=threads --format=text0 tag:misdirected)
echo "[autoreply.sh] All done!" echo "[autoreply.sh] All done!"