mutt-wizard/etc/mailsync.sh

47 lines
1.7 KiB
Bash
Raw Normal View History

2018-03-06 07:38:22 +01:00
#!/bin/sh
# This script will run offlineimap and check
# for new email if there is an internet connection.
#
# If it detects new mail, it uses mpv to play a
# notification sound: notify.opus
#
# I have this run as a cronjob every 5 minutes.
# Check for internet connection. Exit script if none. (timeout in mac is `-t`)
if [ "$(uname)" == "Darwin" ]
then
ping -q -t 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` >/dev/null || exit
else
ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` >/dev/null || exit
fi
2018-03-06 07:38:22 +01:00
# Get current number of new mail, then begin sync.
2018-03-06 09:11:45 +01:00
ori=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)
offlineimap -o "$@"
2018-03-06 07:38:22 +01:00
2018-03-06 09:11:45 +01:00
# Recount new mail.
new=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)
2018-03-06 07:38:22 +01:00
# If new mail has grown, play a notification.
if [ "$new" -gt "$ori" ]; then
mpv --quiet ~/.config/mutt/etc/notify.opus
fi
for account in $(ls ~/.mail)
do
for mailbox in $(ls ~/.mail/$account/)
do
#List unread messages newer than last mailsync and count them
newcount=$(find ~/.mail/$account/$mailbox/new/ -type f -newer ~/.config/mutt/etc/mailsynclastrun 2> /dev/null | wc -l)
#Pop a Mac style notification with the count for that mailbox
if [ "$(uname)" == "Darwin" -a "$newcount" -gt "0" ]
then
osascript -e "display notification \"$newcount in $mailbox\" with title \"Youve got Mail\" subtitle \"Account: $account\""
sleep 2
fi
done
done
#Create a touch file that indicates the time of the last run of mailsync
touch ~/.config/mutt/etc/mailsynclastrun