summaryrefslogtreecommitdiff
path: root/.config/eww/scripts/email-listener
blob: a5444c8e97cd78368187239a67a3d6123748cdfb (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh

export NOTIFY_SHORT=email
export NOTIFY_LONG=email

readonly CACHE="${XDG_CACHE_HOME:=$HOME/.cache}/email-listener"
touch "$CACHE"

count()
{
	local cur=`find "$MAILDIR"/*/Inbox/cur -type f | wc -l`
	local new=`find "$MAILDIR"/*/Inbox/new -type f | wc -l`
	local sum=$((cur + new))

	case $sum in
	0)
		printf 'No Mail'
		;;
	1)
		printf '1 Mail'
		;;
	*)
		printf '%d Mails' $sum
		;;
	esac
	[ $new -gt 0 ] && printf ' (%d Unread)' $new
	echo

	echo $new >"$CACHE"
}

{
	while sleep 1
	do
		read new_mails <"$CACHE"
		if [ $new_mails -gt ${prev:=0} ]
		then
			local title
			local desc

			case $((new_mails - prev)) in
			1)
				title='New Email'
				desc='1 new email has been received'
				;;
			*)
				title='New Emails'
				desc="$new_mails new emails have been received"
				;;
			esac

			notify "$title" "$desc"
			prev=$new_mails
		fi
	done
} &

count

inotifywait -qm "$MAILDIR"/*/Inbox/new "$MAILDIR"/*/Inbox/cur \
| while read _ event _
do
	case "$event" in
	CREATE|DELETE|MOVED_*)
		count
		;;
	esac
done