#!/usr/bin/env sh # Run only if not already running in other instance pgrep mbsync >/dev/null && { echo "mbsync is already running." exit } notify() { notify-send --app-name="Mail" -- "$1" "$2" } # export GPG_TTY="$(tty)" # export DISPLAY=":0" # export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(id -u)"/bus export MAILDIR="$XDG_DATA_HOME/mail" lastrun="$XDG_CACHE_HOME/.mailsynclastrun" # export PYTHONWARNINGS="ignore::UserWarning" # afew --move-mails --all mbsync -q mit new=$(find "$MAILDIR/mit/INBOX/new/" "$MAILDIR/mit/INBOX/cur/" -type f -newer "$lastrun" 2>/dev/null) newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l) case 1 in $((newcount > 5))) echo "$newcount new mail for mit." notify "New Mail!" "$newcount new mail(s)." ;; $((newcount > 0))) echo "$newcount new mail for mit." for file in $new; do # Extract and decode subject and sender from mail. subject=$(awk '/^Subject: / && ++n == 1,/^.*: / && ++i == 2' "$file" | head -n-1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | tr -d '\n\t') from="$(sed -n "/^From:/ s|From: *|| p" "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')" from="${from% *}" from="${from%\"}" from="${from#\"}" notify "$from:" "$subject" done ;; *) echo "No new mail for mit." ;; esac # notmuch new # afew --tag --new #Create a touch file that indicates the time of the last run of mailsync touch "$lastrun"