mailsync (4129B)
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 export GPG_TTY="$(tty)" 28 29 [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc" 30 [ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config" 31 32 lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun" 33 34 notify () { 35 # just get needs dbus path for cron to notify properly 36 # if using my dotfiles see ~/scripts/preserve_dbus.sh for dbus-session-addr file generation 37 # obviously wont work unless using my dotfiles unless 38 export DBUS_SESSION_BUS_ADDRESS="$(cat /home/jake/.cache/dbus-session-addr)" 39 notify-send -t 5000 --app-name="mutt-wizard" "$1" "$2" 40 } 41 42 # Check account for new mail. Notify if there is new content. 43 syncandnotify() { 44 case "$1" in 45 imap) mbsync -q "$2" ;; 46 pop) mpop -q "$2" ;; 47 esac 48 new=$(find\ 49 "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \ 50 "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \ 51 -type f -newer "$lastrun" 2> /dev/null) 52 newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l) 53 case 1 in 54 $((newcount > 5)) ) 55 echo "$newcount new mail for $2." 56 [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mail(s) in \`$2\` account." 57 ;; 58 $((newcount > 0)) ) 59 echo "$newcount new mail for $2." 60 [ -z "$MAILSYNC_MUTE" ] && 61 for file in $new; do 62 # used to parse the FULL subject. sometimes if its long enough it will wrap down a line. 63 # if its longer than 2 lines then the sender doesn't understand what a subject is supposed to be. 64 sanitize='s/MIME.*$//g; s/Content-Type.*$//g; s/X-MS-Exchange-Transport-Rules-IncidentReport:.*$//g; s/From:.*$//g; s/X-MS.*$//g' 65 subject=$(sed -n "/^Subject:.*$/{ p; n; p; }" "$file" | sed $sanitize | tr -d '\n') 66 67 from="$(sed -n "/^From:/ s|From: *|| p" "$file" | 68 perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')" 69 70 from="${from% *}" ; from="${from%\"}" ; from="${from#\"}" 71 72 notify "📧$from:" "$subject" 73 done 74 ;; 75 *) echo "No new mail for $2." ;; 76 esac 77 } 78 79 allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")" 80 81 # Get accounts to sync. All if no argument. Prefix with `error` if non-existent. 82 IFS=' 83 ' 84 if [ -z "$1" ]; then 85 tosync="$allaccounts" 86 else 87 tosync="$(for arg in "$@"; do for availacc in $allaccounts; do 88 [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break 89 done || echo "error $arg"; done)" 90 fi 91 92 for account in $tosync; do 93 case $account in 94 Channel*) syncandnotify imap "${account##* }" & ;; 95 account*) syncandnotify pop "${account##* }" & ;; 96 error*) echo "ERROR: Account ${account##* } not found." ;; 97 esac 98 done 99 100 wait 101 102 notmuch new --quiet 103 104 #Create a touch file that indicates the time of the last run of mailsync 105 touch "$lastrun"