-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntfy
executable file
·100 lines (82 loc) · 1.8 KB
/
ntfy
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh
set -e
die() {
echo "$@" 1>&2
exit 1
}
mail=0
while getopts ":t:m:M" opt; do
case $opt in
t ) title=$OPTARG;;
m ) message=$OPTARG;;
M ) mail=1;;
: ) echo "-$OPTARG requires an argument" >&2; exit 1;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $((OPTIND - 1))
startepoch=$(date +%s)
set +e
"$@"
rc=$?
set -e
endepoch=$(date +%s)
elapsed=$((endepoch - startepoch))
if [ $elapsed -gt 3600 ]; then
duration="$((elapsed/3600))h $(((elapsed%3600)/60))m"
elif [ $elapsed -gt 60 ]; then
duration="$((elapsed/60))m $((elapsed%60))s"
else
duration="${elapsed}s"
fi
if [ "$1" != "" ]; then
if [ "$title" = "" ]; then
if [ $rc -gt 0 ]; then
title="Command failed"
else
title="Command finished"
fi
title="$title on $(hostname -s)"
fi
if [ "$message" = "" ]; then
cmd="$*"
message="Command '$(echo "$cmd" | cut -c1-32)' exited"
if [ $rc -gt 0 ]; then
message="$message with RC $rc"
fi
message="$message in $duration"
fi
fi
if [ "$title" = "" ]; then
title="ntfy called"
fi
if [ "$message" = "" ]; then
message="ntfy called"
fi
pushover() {
curl -sS \
--form-string "token=$(awk -F "=" '/token/ {print $2}' ~/.ntfy)" \
--form-string "user=$(awk -F "=" '/user/ {print $2}' ~/.ntfy)" \
--form-string "title=$1" \
--form-string "message=$2" \
https://api.pushover.net/1/messages.json \
1>/dev/null
}
bell() {
printf '\a'
}
tmuxmsg() {
[ -z "$TMUX" ] || tmux display-message "$1: $2"
}
email() {
command -v mail >/dev/null 2>&1 || die "Can't find 'mail' command"
recipient=$(awk -F "=" '/mail_recipient/ {print $2}' ~/.ntfy)
[ -z "$recipient" ] && die 'Missing recipient in ~/.ntfy'
echo "$2" | mail -s "$1" "$recipient"
}
pushover "$title" "$message"
bell
tmuxmsg "$title" "$message"
if [ $mail -eq 1 ]; then
email "$title" "$message"
fi