email (1527B)
1 #!/bin/sh 2 NO_CLIENT=0 3 SYNC_MAIL=0 4 SYNC_MAIL_FREQ=0 5 SYNC_MAIL_FREQ_SECS=1800 6 7 NOTIFY_MSG=" New Mail 8 You have %N% new mail%S% in '%A%'." 9 10 MBSYNCRC="${MBSYNCRC:-${XDG_CONFIG_HOME:-$HOME/.config}/mbsync/mbsyncrc}" 11 MBSYNC_ACCS="${XDG_DATA_HOME:-$HOME/.local/share}/mail" 12 sync_mail() { 13 mbsync -c "$MBSYNCRC" -a 14 for ACC in "$MBSYNC_ACCS"/*; do 15 LAST_RUN="$ACC/.sync_mail.lastrun" 16 NEW_MAILS=$(find \ 17 "$ACC/INBOX/new" \ 18 "$ACC/Inbox/new" \ 19 "$ACC/inbox/new" \ 20 -type f -newer "$LAST_RUN" 2>/dev/null | wc -l | sed 's/ //g') 21 if [ "$NEW_MAILS" -ge 1 ]; then 22 [ "$NEW_MAILS" -gt 1 ] && SUF="s" || SUF="" 23 ACC_SHOW=${ACC##*/} 24 MSG=$(echo "$NOTIFY_MSG" | 25 sed "s|%N%|$NEW_MAILS|;s|%S%|$SUF|;s|%A%|$ACC_SHOW|") 26 notify "$MSG" 27 fi 28 touch "$LAST_RUN" 29 done 30 } 31 32 while getopts ':nosS:' flag; do 33 case "$flag" in 34 n) NO_CLIENT=1 ;; 35 o) SYNC_MAIL=1 ;; 36 s) SYNC_MAIL_FREQ=1 ;; 37 S) 38 echo "${OPTARG}" | grep "^-\?[0-9]+$" && \ 39 SYNC_MAIL_FREQ_SECS=${OPTARG} 40 ;; 41 *) 42 printf 'Usage: %s [-n] [-s]\n\n' "$0" 43 printf ' -n: Do not open client\n' 44 printf ' -o: Sync email before opening client\n' 45 printf ' -s: Sync email every X seconds (default: %s)\n' \ 46 "$SYNC_MAIL_FREQ_SECS" 47 printf ' -S: Frequency of syncing emails (-s option)\n' 48 exit 2 49 ;; 50 esac 51 done 52 53 if [ $SYNC_MAIL_FREQ -eq 1 ]; then 54 while true; do 55 sync_mail 56 sleep "$SYNC_MAIL_FREQ_SECS" 57 done & 58 elif [ $SYNC_MAIL -eq 1 ]; then 59 sync_mail 60 fi 61 62 [ $NO_CLIENT -eq 1 ] || mutt 63 64 exit