summaryrefslogtreecommitdiffstatshomepage
path: root/mbsync/.local/bin/maildir-notify
blob: 61fc43406e771313c59d364294023979e781ddbb (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
#!/bin/sh

for maildir; do
	echo "Monitoring $maildir"
	set -- "$@" "${maildir}/new"
	shift
done

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

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

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