summaryrefslogtreecommitdiff
path: root/.config/eww/scripts/email-listener
blob: b5c1cf38ba09de9b2a9fce86d5c04ac240e4619f (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh

export NOTIFY_SHORT=email
export NOTIFY_LONG=email

: ${MAILDIR:=$HOME/mail}

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

T()
{
	en="$1"
	sv="$2"
	shift 2
	case "${LC_ALL:-$LANG}" in
	en_*) printf "$en" "$@" ;;
	sv_*) printf "$sv" "$@"
	esac
}

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)
		T 'icon=\ntext=No Emails' 'icon=\ntext=Inga meddelanden'
		;;
	1)
		T 'icon=\ntext=1 Email' 'icon=\ntext=1 meddelande'
		;;
	*)
		T 'icon=\ntext=%d Emails' 'icon=\ntext=%d meddelanden' $sum
		;;
	esac
	[ $new -gt 0 ] && T ' (%d Unread)' ' (%d ölasta)' $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="$(T 'New Email' 'Nytt meddelande')"
				desc="$(T '1 new email has been received'
						  '1 nytt meddelande har mottagits')"
				;;
			*)
				title="$(T 'New Emails' 'Nya meddelanden')"
				desc="$(T "$new_mails new emails have been received"
						  "$new_mails nya meddelanden har mottagits")"
				;;
			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