|
- #!/bin/sh
-
- # - Syncs mail for all accounts, or a single account given as an argument.
- # - Displays a notification showing the number of new mails.
- # - Displays a notification for each new mail with its subject displayed.
- # - Runs notmuch to index new mail.
- # - This script can be set up as a cron job for automated mail syncing.
-
- # There are many arbitrary and ugly features in this script because it is
- # inherently difficult to pass environmental variables to cronjobs and other
- # issues. It also should at least be compatible with Linux (and maybe BSD) with
- # Xorg and MacOS as well.
-
- # Run only if not already running in other instance
- pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
-
- # First, we have to get the right variables for the mbsync file, the pass
- # archive, notmuch and the GPG home. This is done by searching common profile
- # files for variable assignments. This is ugly, but there are few options that
- # will work on the maximum number of machines.
- eval "$(grep -h -- \
- "^\s*\(export \)\?\(MBSYNCRC\|MPOPRC\|PASSWORD_STORE_DIR\|PASSWORD_STORE_GPG_OPTS\|NOTMUCH_CONFIG\|GNUPGHOME\|MAILSYNC_MUTE\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
- "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \
- "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \
- "$HOME/.pam_environment" 2>/dev/null)"
-
- export GPG_TTY="$(tty)"
-
- [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
- [ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config"
-
- lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
-
- # Settings are different for MacOS (Darwin) systems.
- case "$(uname)" in
- Darwin)
- notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
- ;;
- *)
- case "$(readlink -f /sbin/init)" in
- *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
- esac
- # remember if a display server is running since `ps` doesn't always contain a display
- pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))"
- displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
- [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done)
-
- notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
- export DISPLAY="$x"
- notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
- done ;}
- ;;
- esac
-
- # Check account for new mail. Notify if there is new content.
- syncandnotify() {
- case "$1" in
- imap) mbsync -q "$2" ;;
- pop) mpop -q "$2" ;;
- esac
- new=$(find\
- "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
- "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
- -type f -newer "$lastrun" 2> /dev/null)
- newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
- case "$newcount" in
- 0) echo "No new mail for $2." ;;
- *) echo "$newcount new mail for $2."
- [ -z "$MAILSYNC_MUTE" ] && notify "$2" "$newcount" ;;
- esac
- }
-
- allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")"
-
- # Get accounts to sync. All if no argument. Prefix with `error` if non-existent.
- IFS='
- '
- if [ -z "$1" ]; then
- tosync="$allaccounts"
- else
- tosync="$(for arg in "$@"; do for availacc in $allaccounts; do
- [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break
- done || echo "error $arg"; done)"
- fi
-
- for account in $tosync; do
- case $account in
- Channel*) syncandnotify imap "${account##* }" & ;;
- account*) syncandnotify pop "${account##* }" & ;;
- error*) echo "ERROR: Account ${account##* } not found." ;;
- esac
- done
-
-
- wait
-
- notmuch new --quiet
-
- #Create a touch file that indicates the time of the last run of mailsync
- touch "$lastrun"
|