Skip to content

Instantly share code, notes, and snippets.

@gilbertw1
Last active January 6, 2021 14:32
Show Gist options
  • Save gilbertw1/dec02c758c363c2c0430541290565fc6 to your computer and use it in GitHub Desktop.
Save gilbertw1/dec02c758c363c2c0430541290565fc6 to your computer and use it in GitHub Desktop.
A simple script that re-indexes emails using mu and sends a notification if new emails are detected.
# puts timestamps into an easily comparable format
extract_timestamp() {
local RAW="$(echo "$1" | cut -c1-31)"
echo "$(date --date="$RAW" '+%Y-%m-%d:%H:%M:%S')"
}
# removes html characters that are interpreted poorly by dunst
format_string() {
echo "$1" | sed 's/>/\&gt;/' | sed 's/</\&lt;/' | sed 's/\$/\\$/'
}
# locate the timestamp of the newest message
NEWEST_PREFETCH_MSG="$(mu find -z 'date:today' | head -n 1)"
NEWEST_PREFETCH_MSG_TIMESTAMP="$(extract_timestamp "$NEWEST_PREFETCH_MSG")"
# reindex all emails using mu
mu index --maildir ~/.mail
# grab the newest ten emails that are created today and unread
RECENT_TEN="$(mu find -z 'date:today' 'flag:unread' | head -n 10)"
# maintain the last timestamp to prevent duplicates
LAST_TIMESTAMP=""
# iterate over each message
while read -r msg; do
MSG_TIMESTAMP="$(extract_timestamp "$msg")"
# check if message is newer than previously newest message and not duplicate
if [ "$MSG_TIMESTAMP" \> "$NEWEST_PREFETCH_MSG_TIMESTAMP" ] &&
[ "$MSG_TIMESTAMP" != "$LAST_TIMESTAMP" ]; then
# emit a desktop notification with the email sender and subject
MSG_SENDER="$(echo "$msg" | sed -r 's/^.{32}//' | sed 's/>.*$/>/')"
MSG_SUBJECT="$(echo "$msg" | sed 's/^.*> //')"
# Emit proper notification when run as super user
#sudo -u gilbertw1 DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "$(format_string "$MSG_SENDER")" "$(format_string "$MSG_SUBJECT")"
notify-send "$(format_string "$MSG_SENDER")" "$(format_string "$MSG_SUBJECT")"
fi;
LAST_TIMESTAMP="$MSG_TIMESTAMP"
done <<< "$RECENT_TEN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment