aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mbsync/.local/bin/maildir-notify
blob: 065339f9c0e341f35a915ca37e69855fcef59323 (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
32
33
34
35
#!/bin/sh

if [ $# = 0 ]; then
	for d in "$XDG_DATA_HOME"/mail/*; do
		set -- "$@" "${d}/INBOX"
	done
fi

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

echo "Watching $*"

inotifywait --monitor --event create --event moved_to "$@" |
	while read -r dir _action file; do
		inbox="$(dirname "$dir")"
		category="$(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 "Category: $category, From: $from, Subject: $subject"
		notify-send -a "Mail" -c "$category" "From: $from" "$subject"
	done