aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mbsync/.local/bin/maildir-notify
blob: 198046efef85861ab923c96944388ce951bc1eb6 (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
#!/bin/sh

if [ $# = 0 ]; then
	set -- "$MAIL"
fi

for i; do
	set -- "$@" "${i}/new"
	shift
done

inotifywait --monitor --event create --event moved_to "$@" |
	while read -r dir _action file; do
		inbox="$(dirname "$dir")"
		catagory="$(dirname "$inbox" | xargs basename)"
		from="$(grep -m1 -oP '(?<=^From: ).*' "${dir}${file}")"
		subject="$(grep -m1 -oP '(?<=^Subject: ).*' "${dir}${file}")"

		if [ ! -e "$inbox/new/$file" ]; then
			if [ -e "$inbox/cur/$file" ]; then
				from="$(grep -m1 -oP '(?<=^From: ).*' "$inbox/cur/$file")"
				subject="$(grep -m1 -oP '(?<=^Subject: ).*' "$inbox/cur/$file")"
			else
				echo "Failed to read message. File: $file"
				continue
			fi
		fi

		echo "Catagory: $catagory, From: $from, Subject: $subject"
		notify-send -a "Mail" -c "$catagory" "From: $from" "$subject"
	done