-
Notifications
You must be signed in to change notification settings - Fork 40
/
notify-action.sh
executable file
·68 lines (59 loc) · 1.94 KB
/
notify-action.sh
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
#!/usr/bin/env bash
GDBUS_MONITOR_PID=/tmp/notify-action-dbus-monitor.$$.pid
GDBUS_MONITOR=(gdbus monitor --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications)
NOTIFICATION_ID="$1"
if [[ -z "$NOTIFICATION_ID" ]]; then
echo "no notification id passed: $@"
exit 1;
fi
shift
ACTION_COMMANDS=("$@")
if [[ -z "$ACTION_COMMANDS" ]]; then
echo "no action commands passed: $@"
exit 1;
fi
cleanup() {
rm -f "$GDBUS_MONITOR_PID"
}
create_pid_file(){
rm -f "$GDBUS_MONITOR_PID"
umask 077
touch "$GDBUS_MONITOR_PID"
}
invoke_action() {
invoked_action_id="$1"
local action="" cmd=""
for index in "${!ACTION_COMMANDS[@]}"; do
if [[ $((index % 2)) == 0 ]]; then
action="${ACTION_COMMANDS[$index]}"
else
cmd="${ACTION_COMMANDS[$index]}"
if [[ "$action" == "$invoked_action_id" ]]; then
bash -c "${cmd}" &
fi
fi
done
}
monitor() {
create_pid_file
( "${GDBUS_MONITOR[@]}" & echo $! >&3 ) 3>"$GDBUS_MONITOR_PID" | while read -r line
do
local closed_notification_id="$(sed '/^\/org\/freedesktop\/Notifications: org.freedesktop.Notifications.NotificationClosed (uint32 \([0-9]\+\), uint32 [0-9]\+)$/!d;s//\1/' <<< "$line")"
if [[ -n "$closed_notification_id" ]]; then
if [[ "$closed_notification_id" == "$NOTIFICATION_ID" ]]; then
invoke_action close
break
fi
else
local action_invoked="$(sed '/\/org\/freedesktop\/Notifications: org.freedesktop.Notifications.ActionInvoked (uint32 \([0-9]\+\), '\''\(.*\)'\'')$/!d;s//\1:\2/' <<< "$line")"
IFS=: read invoked_id action_id <<< "$action_invoked"
if [[ "$invoked_id" == "$NOTIFICATION_ID" ]]; then
invoke_action "$action_id"
break
fi
fi
done
kill $(<"$GDBUS_MONITOR_PID")
cleanup
}
monitor