summaryrefslogtreecommitdiff
path: root/.config/eww/scripts/email-listener
blob: 41693f3a00fe46db7ab6017dd7ee5ae6cb284057 (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 'icon=\ntext=No Email'
		;;
	1)
		printf 'icon=\ntext=1 Email'
		;;
	*)
		printf 'icon=\ntext=%d Emails' $sum
		;;
	esac
	[ $new -gt 0 ] && printf ' (%d Unread)' $new
	echo
	echo $new >"$CACHE"
}

{
	while :
	do
		sleep 1
		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 | jo

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