mailsync (4327B)
1 #!/bin/sh 2 3 # - Syncs mail for all accounts, or a single account given as an argument. 4 # - Displays a notification showing the number of new mails. 5 # - Displays a notification for each new mail with its subject displayed. 6 # - Runs notmuch to index new mail. 7 # - This script can be set up as a cron job for automated mail syncing. 8 9 # There are many arbitrary and ugly features in this script because it is 10 # inherently difficult to pass environmental variables to cronjobs and other 11 # issues. It also should at least be compatible with Linux (and maybe BSD) with 12 # Xorg and MacOS as well. 13 14 # Run only if not already running in other instance 15 pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;} 16 17 # First, we have to get the right variables for the mbsync file, the pass 18 # archive, notmuch and the GPG home. This is done by searching common profile 19 # files for variable assignments. This is ugly, but there are few options that 20 # will work on the maximum number of machines. 21 eval "$(grep -h -- \ 22 "^\s*\(export \)\?\(MBSYNCRC\|MPOPRC\|PASSWORD_STORE_DIR\|PASSWORD_STORE_GPG_OPTS\|NOTMUCH_CONFIG\|GNUPGHOME\|MAILSYNC_MUTE\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \ 23 "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \ 24 "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \ 25 "$HOME/.pam_environment" 2>/dev/null)" 26 27 # For non-interactive shell (e.g. cron job) run only when the GPG key (in $GNUPGHOME or pass --homedir) is unlocked 28 tty -s || (echo "dummy" | gpg --sign --batch --yes -o /dev/null > /dev/null 2>&1) || exit 29 30 export GPG_TTY="$(tty)" 31 32 [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc" 33 [ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config" 34 35 lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun" 36 37 # Settings are different for MacOS (Darwin) systems. 38 case "$(uname)" in 39 Darwin) notify() { osascript -e "display notification \"$2\" with title \"$1\"" ;} ;; 40 *) 41 case "$(readlink -f /sbin/init)" in 42 *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;; 43 *dinit*) export DBUS_SESSION_BUS_ADDRESS=$(grep -E -z "DBUS_SESSION_BUS_ADDRESS" "/proc/$(pgrep -x swaybar)/environ" | sed 's/DBUS_SESSION_BUS_ADDRESS=//') ;; 44 esac 45 46 notify() { 47 notify-send --app-name="mutt-wizard" -- "$1" "$2" 48 } 49 ;; 50 esac 51 52 # Check account for new mail. Notify if there is new content. 53 syncandnotify() { 54 case "$1" in 55 imap) mbsync -q "$2" ;; 56 pop) mpop -q "$2" ;; 57 esac 58 new=$(find\ 59 "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \ 60 "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \ 61 -type f -newer "$lastrun" 2> /dev/null) 62 newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l) 63 case 1 in 64 $((newcount > 5)) ) 65 echo "$newcount new mails for $2." 66 [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mails in \`$2\` account." 67 ;; 68 $((newcount > 0)) ) 69 echo "$newcount new mail(s) for $2." 70 [ -z "$MAILSYNC_MUTE" ] && 71 for file in $new; do 72 # Extract and decode subject and sender from mail. 73 subject=$(awk '/^Subject: / && ++n == 1,/^.*: / && ++i == 2' "$file" | head -n-1 | 74 perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | 75 sed 's/^Subject: //' | tr -d '\n\t') 76 from="$(sed -n "/^From:/ s|From: *|| p" "$file" | 77 perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')" 78 from="${from% *}" ; from="${from%\"}" ; from="${from#\"}" 79 notify "📧$from:" "$subject" 80 done 81 ;; 82 *) echo "No new mail for $2." ;; 83 esac 84 } 85 86 allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")" 87 88 # Get accounts to sync. All if no argument. Prefix with `error` if non-existent. 89 IFS=' 90 ' 91 if [ -z "$1" ]; then 92 tosync="$allaccounts" 93 else 94 tosync="$(for arg in "$@"; do for availacc in $allaccounts; do 95 [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break 96 done || echo "error $arg"; done)" 97 fi 98 99 for account in $tosync; do 100 case $account in 101 Channel*) syncandnotify imap "${account##* }" & ;; 102 account*) syncandnotify pop "${account##* }" & ;; 103 error*) echo "ERROR: Account ${account##* } not found." ;; 104 esac 105 done 106 107 wait 108 109 notmuch new --quiet 110 111 pkill -RTMIN+12 "${STATUSBAR:-i3blocks}" 112 #Create a touch file that indicates the time of the last run of mailsync 113 touch "$lastrun"