-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushover
executable file
·339 lines (301 loc) · 9.26 KB
/
pushover
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#! /bin/sh
#
# pushover - script to send notification to the service PushOver
#
# https://pushover.net/
#
# Variables
Version="0.0.10"
USAGE="usage: `basename $0` [parameters] text"
ConfigFile="$HOME/.`basename $0`.conf"
# Defaults for additional parameters on priority 'emergency'
EmergencyRetry=60
EmergencyExpire=3600
# Functions
showUsage ()
{
cat <<EOUsageText
$USAGE
Use option -h for getting help.
EOUsageText
}
readConfig ()
{
#
# Read the config
#
# Check if config is readable (and existing)
if [ ! -r $1 ]; then
echo "Error: The config file '$1'"
echo " does not exist or is not readable."
exit 2
fi
# Check access rights
# Get access right in human readable form
#
# Linux and Mac OS/Darwin/BSD have not the same syntax for 'stat'
# Have to determine the OS flavour and use the matching syntax
if [ "$(uname | xargs printf "%s")" = "Linux" ]; then
# We are on Linux
ConfigFileMod=$(stat -c %A $1)
else
# Not Linux. probably Mac OS/Darwin/BSD
ConfigFileMod=$(stat -f %Sp $1)
fi
# Check if file writable for others
# Is mod g=w or o=w? -> error
if [ "$(echo $ConfigFileMod | cut -c 6 | xargs printf "%s")" = "w" -o \
"$(echo $ConfigFileMod | cut -c 9 | xargs printf "%s")" = "w" ]; then
echo "Error: The config file '$1'"
echo " is writable by other users."
echo " The program was halted for security reasons."
echo " Check the file for malicious content and change the access rights:"
echo " chmod 600 $1"
exit 3
fi
# Warn if file is readable for others
# Is mod g=r or o=r
if [ "$(echo $ConfigFileMod | cut -c 5 | xargs printf "%s")" = "r" -o \
"$(echo $ConfigFileMod | cut -c 8 | xargs printf "%s")" = "r" ]; then
echo "Warning: The config file '$1'"
echo " is readable by other users."
echo " You should protect your API key."
echo " Change the access rights:"
echo " chmod 600 $1"
fi
# FixMe: Lazy and unsafe solution using sourcing
. $1
}
showHelp ()
{
cat <<EOHelpText
usage: `basename $0` [-t <title>] [-p <priority>] [-s <sound>] [-u <userid>]
<msg text>
`basename $0` -h
`basename $0` -v
Script for sending notification with Pushover (https://pushover.net)
Version $Version
-t set title for the notification
-p set priority for the notification
Valid priorities are 'lowest', 'low', 'normal', 'high', and 'emergency'.
-s set sound for the notification
Valid sounds are 'pushover' , 'bike', 'bugle', 'cashregister',
'classical', 'cosmic', 'falling', 'gamelan', 'incoming', 'intermission',
'magic', 'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat',
'alien', 'climb', 'persistent', 'echo', 'updown', and 'none'.
-u set key of the user or group which will receive the notification.
-h print this text and exit
-v print version and exit
If the message text is "-" (a single hyphen) the text will be read
from stdin.
EOHelpText
}
urlencode () {
#
# This implematation of urlencoding is taken from
# https://stackoverflow.com/a/3077738/747635
#
[ $# -lt 1 ] && { return; }
encodedurl="$*";
# make sure hexdump exists, if not, just return
[ ! -x "/usr/bin/hexdump" ] && { return; }
echo $(echo $encodedurl | hexdump -v -e '1/1 "%02x\t"' -e '1/1 "%_c\n"' |
LANG=C awk '
$1 == "20" { printf("%s", "%20"); next } # space -> %20 # changed from original
$1 ~ /0[adAD]$/ { printf("%s", "%0A"); next } # newlines -> %0A # changed from original
$2 ~ /^[a-zA-Z0-9.*()\/-]$/ { printf("%s", $2); next } # pass through what we can
{ printf("%%%s", $1) } # take hex value of everything else
')
}
##########
#
# Cause 'urlencode' return no value, if 'hexdump' is not available
# here is a check for 'hexdump':
if [ ! -x "/usr/bin/hexdump" ]; then
echo "Error: The tool 'hexdump' is not available."
exit 1
fi
# EndFunctions
# Parse command line options.
while getopts hvt:u:p:s: OPT; do
case "$OPT" in
h)
showHelp
exit 0
;;
v)
echo "`basename $0` version $Version"
exit 0
;;
t)
# Store MSGTitle from arguments
ArgMSGTitle=$OPTARG
;;
u)
# Store UserKey from arguments
ArgUserKey=$OPTARG
;;
p)
PrioOption=$OPTARG
case "$PrioOption" in #FixMe Move case to function ParsePriority
lowest|stealth|-2)
MSGPriority="-2"
;;
low|silent|-1)
MSGPriority="-1"
;;
normal|0)
MSGPriority="0"
;;
high|1|+1)
MSGPriority="1"
;;
emergency|2|+2)
MSGPriority="2"
;;
*)
# invalid priority -> error
echo "Error: Invalid value \"$PrioOption\" for priority." #FixMe Jump to help
exit 10
;;
esac
;;
s)
MSGSound=$OPTARG
case "$MSGSound" in
pushover|bike|bugle|cashregister|classical|cosmic|falling)
# Value o.k., perhaps my programming skills not...
;;
gamelan|incoming|intermission|magic|mechanical|pianobar)
# Value also .o.k. ...
;;
siren|spacealarm|tugboat|alien|climb|persistent|echo|updown|none)
# Value also o.k. ...
;;
*)
# invalid sound defined -> error
echo "Error: Invalid value \"$MSGSound\" for sound."
exit 10
;;
esac
;;
\?)
# getopts issues an error message
showUsage >&2
exit 10
;;
esac
done
# Remove the options we parsed above.
shift `expr $OPTIND - 1`
# We need at least one non-option argument.
# If not then there is no message text
if [ $# -eq 0 ]; then
showUsage >&2
exit 11
fi
# The additional arguments ($*) are the message text or "-" for reading stdin
# id not "-" use the input
if [ "$*" != "-" ]; then
MSG=$(urlencode $*)
else
# else read stdin
while read LINE; do
MSG=$MSG+$(urlencode ${LINE})
done
fi
#
# Read the config
#
readConfig $ConfigFile
# Overwriting setting from configfile with settings from arguments
# Only use ArgMSGTitle if it's not empty
if [ ! -z "$ArgMSGTitle" ]; then
MSGTitle=$ArgMSGTitle
fi
# Only use ArgUserKey if it's not empty
if [ ! -z "$ArgUserKey" ]; then
UserKey=$ArgUserKey
fi
# Check if all nessary values are set.
# (Values set within the script do not have to be checked.)
#
# Check APIKey
if [ -z "$APIKey" ]; then
echo "Error: API-Key is not provided."
exit 4
fi
# Check User/Group-ID
if [ -z "$UserKey" ]; then
echo "Error: User/Group-ID is not provided."
exit 4
fi
# Genrating string with command-sequence
CurlCall="curl -s -d token=$APIKey&user=$UserKey&message="$MSG""
# Here some more option, when set (e.g. title, priority)
# embeded in conditional-constructs
#
# Is title set, add -t
if [ ! -z "$MSGTitle" ]; then
CurlCall="$CurlCall&title=$(urlencode $MSGTitle)"
fi
# Is priority set
case $MSGPriority in
-2|-1|1)
CurlCall="$CurlCall&priority=$MSGPriority"
;;
2)
CurlCall="$CurlCall&priority=$MSGPriority&retry=$EmergencyRetry&expire=$EmergencyExpire"
;;
esac
# Is sound set
if [ ! -z "$MSGSound" ]; then
CurlCall="$CurlCall&sound=$MSGSound"
fi
# Add API-endpoint to the paraneter
CurlCall="$CurlCall https://api.pushover.net/1/messages.json"
# Call Pushover.net
PushoverResponse=$($CurlCall)
CurlReturnCode=$?
# Checking, if an error accured
#
# Phase 1: curl coneecting Pushover
#
# curl has thrown an error (exit code !=0 )
# codes: https://ec.haxx.se/usingcurl-returns.html
#
if [ ! $CurlReturnCode = "0" ]; then
echo "Error while connecting to Pushover.net (curl exit code $CurlReturnCode)."
exit 20
fi
# Phase 2: Examine resppnse from Pushover.net
#
# If the first attribute is 'status', then call was successful
#
# Exception:
# If the priority is 'emergency' and the the call is successful,
# then the first attribute is 'receipt'
# We can use this information to get the parameter for a call back function
#
# Parse the response
#
# cut the response at the first end second quotation mark to get th first
First=$(echo $PushoverResponse | cut -d'"' -f2)
case $First in
status)
# Success
;;
receipt)
# Also success, cause a receipt is only sended for an successful call
# Parse and store the receipt ID for later use
PushoverReceiptID=$(echo $PushoverResponse | cut -d'"' -f4)
;;
*)
# Everything else is an error (or I don't know it now...)
# Error message is surounded by '[...]'
# (if there is no error message, complete response will be echoed.)
PushoverError=$(echo $PushoverResponse | cut -d[ -f2 | cut -d] -f1)
echo "Error Message from Pushover.net: $PushoverError"
exit 21
;;
esac