-
Notifications
You must be signed in to change notification settings - Fork 3
/
pushover
311 lines (305 loc) · 7.75 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
#!/bin/bash
VERSION="1.2.3"
DIR_CONFIG="$HOME/.pushover"
USER=""
TOKEN=""
TITLE=""
MESSAGE=""
URL=""
URL_TITLE=""
DEVICE=""
SOUND=""
HTML=""
MONOSPACE=""
TIMESTAMP=""
PRIORITY="0"
RETRY="60"
EXPIRE="3600"
config() {
LABEL_USER_KEY="User Key"
LABEL_APP_TOKEN="API Token"
echo -ne '\033[0;37m\n'
if [ -n "${CONFIG}" ] && [ ${CONFIG} == "rm" ]; then
rm -rf ${DIR_CONFIG}
echo -ne "Pushover CLI configuration deleted.\n\n"
echo -ne '\033[0m'
exit 0
fi
if [ -e ${DIR_CONFIG}/config ]; then
USER_KEY=$(awk -F'"' '/^USER_KEY=/ {print $2}' ${DIR_CONFIG}/config)
APP_TOKEN=$(awk -F'"' '/^APP_TOKEN=/ {print $2}' ${DIR_CONFIG}/config)
if [ -z "${USER_KEY}" ]; then echo -ne "${LABEL_USER_KEY}: "; else echo -ne "${LABEL_USER_KEY} [****************${USER_KEY: -4}]: "; fi
read USER_KEY
if [ -z "${APP_TOKEN}" ]; then echo -ne "${LABEL_APP_TOKEN}: "; else echo -ne "${LABEL_APP_TOKEN} [****************${APP_TOKEN: -4}]: "; fi
read APP_TOKEN
else
echo -ne "${LABEL_USER_KEY}: "
read USER_KEY
echo -ne "${LABEL_APP_TOKEN}: "
read APP_TOKEN
if [ ! -d ${DIR_CONFIG} ]; then mkdir ${DIR_CONFIG}; fi
if [ -e ${DIR_CONFIG}/config ]; then touch ${DIR_CONFIG}/config; fi
fi
cat <<EOF > ${DIR_CONFIG}/config
USER_KEY="${USER_KEY}"
APP_TOKEN="${APP_TOKEN}"
EOF
chmod 600 ${DIR_CONFIG}/config
echo -ne "\nPushover CLI configured successfully.\n\n"
echo -ne '\033[0m'
}
upgrade() {
DIR_TEMP="/tmp/pushover-cli"
DIR_BIN="/usr/local/bin"
BRANCH="${UPGRADE}"
if [ "${BRANCH}" == "dev" ]; then BRANCH="develop"; else BRANCH="master"; fi
echo -ne '\033[0;37m'
echo -ne "\nDownloading latest version..."
curl -s https://raw.githubusercontent.com/aaronfagan/pushover-cli/${BRANCH}/install.sh | bash -s ${BRANCH} upgrade > /dev/null 2>&1
if [ $? == 0 ]; then
echo -ne "done!"
VERSION_OLD=${VERSION}
VERSION_NEW=$(awk -F'"' '/^VERSION=/ {print $2}' ${DIR_TEMP}/pushover)
if [ "${VERSION_OLD}" == "${VERSION_NEW}" ]; then
echo -ne "\nPushover CLI is already the latest version (v${VERSION_OLD}).\n\n"
else
echo -ne "\nPushover CLI upgraded successfully (v${VERSION_OLD} => v${VERSION_NEW}).\n\n"
fi
echo -ne '\033[0m'
rm -rf ${DIR_TEMP}
exit 0
else
echo -ne "failed!"
echo -ne "\nPushover CLI upgrade failed.\n\n"
echo -ne '\033[0m'
rm -rf ${DIR_TEMP}
exit 1
fi
}
version() {
echo -ne "\033[0;37m
Pushover CLI - v${VERSION} - https://github.com/aaronfagan/pushover-cli/
By Aaron Fagan - https://www.aaronfagan.ca/
\033[0m\n"
}
usage() {
echo "
Usage:
pushover [options]
Options:
--user Pushover user key. REQUIRED.
--token Pushover application API token. REQUIRED.
--title Notification title.
--message Notification message. If you are seeing errors, try wrapping your message in 'single quotes.' REQUIRED.
--url A supplementary URL to show with your message.
--url-title A title for your supplementary URL, otherwise just the URL is shown.
--device The target device(s) to send the notification to. Comma separated, no spaces. DEFAULT = All devices.
--sound The notification sound to play upon delivery. More Info: https://pushover.net/api#sounds
--html Enabled HTML message. Set to 1 to enable. More Info: https://pushover.net/api#html
--monospace Enabled Monospace message. Set to 1 to enable. May not be used if --html is used, and vice versa.
--timestamp A Unix timestamp of your message's date and time to display to the user. DEFAULT = The time your message is received by the Pushover API.
--priority Message priority. More Info: https://pushover.net/api#priority. DEFAULT = $PRIORITY.
--retry How many seconds between retries. Only applies when --priority is set to 2 (emergency). DEFAULT = $RETRY.
--expire How many seconds until emergency notification retries will stop. Only applies when --priority is set to 2 (emergency). DEFAULT = $EXPIRE.
--config Configure global Pushover user key and application API token. Set to 'rm' to delete configuration files.
--upgrade Upgrade to the latest Pushover CLI version.'.
--version, -v Currently installed version of Pushover CLI.
--quiet, -q Silence all output.
--help Usage instructions.
Example:
pushover \\
--user YOUR_USER_KEY \\
--token YOUR_APP_TOKEN \\
--title \"Test Notification\" \\
--message \"Here is the notification!\"
"
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--token)
TOKEN="${2:-${TOKEN}}"
shift
shift
;;
--user)
USER="${2:-${USER}}"
shift
shift
;;
--title)
TITLE="${2:-$TITLE}"
shift
shift
;;
--message)
MESSAGE="${2:-${MESSAGE}}"
shift
shift
;;
--url)
URL="${2:-$URL}"
shift
shift
;;
--url-title)
URL_TITLE="${2:-$URL_TITLE}"
shift
shift
;;
--device)
DEVICE="${2:-$DEVICE}"
shift
shift
;;
--sound)
SOUND="${2:-$SOUND}"
shift
shift
;;
--html)
HTML="${2:-$HTML}"
shift
shift
;;
--monospace)
MONOSPACE="${2:-$MONOSPACE}"
shift
shift
;;
--timestamp)
TIMESTAMP="${2:-$TIMESTAMP}"
shift
shift
;;
--priority)
PRIORITY="${2:-$PRIORITY}"
shift
shift
;;
--retry)
RETRY="${2:-$RETRY}"
shift
shift
;;
--expire)
EXPIRE="${2:-$EXPIRE}"
shift
shift
;;
--config)
CONFIG="${2:-$CONFIG}"
shift
shift
config
exit 0
;;
--upgrade)
UPGRADE="${2:-$UPGRADE}"
shift
shift
upgrade
;;
-v | --version)
shift
version
exit 0
;;
-q | --quiet)
shift
QUIET=TRUE
;;
--help)
shift
usage
exit 0
;;
*)
INVALID+=("$1")
shift
echo -ne "\033[0;31m\nERROR:\033[0;37m Invalid arguement: ${INVALID}\033[0m\n"
usage
exit 1
;;
esac
done
if [ -z "${USER}" ] || [ -z "${TOKEN}" ] || [ -z "${MESSAGE}" ]; then
if [ -z "${QUIET}" ]; then
echo -ne '\n'
fi
if [ -z "${USER}" ]; then
if [ -e ${DIR_CONFIG}/config ]; then
USER=$(awk -F'"' '/^USER_KEY=/ {print $2}' ${DIR_CONFIG}/config)
if [ -z "${USER}" ]; then
if [ -z "${QUIET}" ]; then
echo -ne "\033[0;31mERROR:\033[0;37m --user arguement is required.\033[0m\n"
fi
fi
else
if [ -z "${QUIET}" ]; then
echo -ne "\033[0;31mERROR:\033[0;37m --user arguement is required.\033[0m\n"
fi
fi
fi
if [ -z "${TOKEN}" ]; then
if [ -e ${DIR_CONFIG}/config ]; then
TOKEN=$(awk -F'"' '/^APP_TOKEN=/ {print $2}' ${DIR_CONFIG}/config)
if [ -z "${TOKEN}" ]; then
if [ -z "${QUIET}" ]; then
echo -ne "\033[0;31mERROR:\033[0;37m --token arguement is required.\033[0m\n"
fi
fi
else
if [ -z "${QUIET}" ]; then
echo -ne "\033[0;31mERROR:\033[0;37m --token arguement is required.\033[0m\n"
fi
fi
fi
if [ -z "${MESSAGE}" ]; then
if [ -z "${QUIET}" ]; then
echo -ne "\033[0;31mERROR:\033[0;37m --message arguement is required.\033[0m\n"
fi
fi
fi
if [ -z "${USER}" ] || [ -z "${TOKEN}" ] || [ -z "${MESSAGE}" ]; then
if [ -z "${QUIET}" ]; then
usage
fi
exit 1
else
if [ -z "${QUIET}" ]; then
echo -ne '\033[0;37m'
echo -ne "> Sending notification..."
fi
CURL=$(/usr/bin/curl -s \
-F "token=${TOKEN}" \
-F "user=${USER}" \
-F "title=${TITLE}" \
-F "message=$(echo -e ${MESSAGE})" \
-F "url=${URL}" \
-F "url_title=${URL_TITLE}" \
-F "device=${DEVICE}" \
-F "sound=${SOUND}" \
-F "html=${HTML}" \
-F "monospace=${MONOSPACE}" \
-F "timestamp=${TIMESTAMP}" \
-F "priority=${PRIORITY}" \
-F "retry=${RETRY}" \
-F "expire=${EXPIRE}" \
https://api.pushover.net/1/messages.json)
ERROR=$(echo ${CURL} | grep 'errors')
if [ -z "${ERROR}" ]; then
if [ -z "${QUIET}" ]; then
echo -ne "success!\n\n";
echo -ne '\033[0m'
fi
exit 0
else
if [ -z "${QUIET}" ]; then
echo -ne "failed!\n\n";
echo -ne "\033[0;31mERROR:\033[0;37m ${ERROR}.\033[0m\n\n"
echo -ne '\033[0m'
fi
exit 1;
fi
fi