summaryrefslogtreecommitdiff
path: root/etc/mailsync.sh
blob: 205a1c864a02adfa5bf865e5fe1f8b87696ccc74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/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

# Get current number of new mail, then begin sync.
ori=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)
offlineimap -o "$@"

# Recount new mail.
new=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)

# 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