-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushover.sh
executable file
·166 lines (153 loc) · 4.26 KB
/
pushover.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
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
#!/usr/local/bin/bash
# Default config vars
CURL="$(which curl)"
PUSHOVER_URL="https://api.pushover.net/1/messages.json"
TOKEN="" # May be set in pushover.conf or given on command line
USER="" # May be set in pushover.conf or given on command line
CURL_OPTS=""
declare -A device_aliases=()
# Load user config
if [ ! -z "${PUSHOVER_CONFIG}" ]; then
CONFIG_FILE="${PUSHOVER_CONFIG}"
else
CONFIG_FILE="${XDG_CONFIG_HOME-${HOME}/.config}/pushover.conf"
fi
if [ -e "${CONFIG_FILE}" ]; then
. "${CONFIG_FILE}"
fi
# Functions used elsewhere in this script
usage() {
echo "${0} <options> <message>"
echo " -c <callback>"
echo " -d <device>"
echo " -D <timestamp>"
echo " -e <expire>"
echo " -p <priority>"
echo " -r <retry>"
echo " -t <title>"
echo " -T <TOKEN> (required if not in config file)"
echo " -s <sound>"
echo " -u <url>"
echo " -U <USER> (required if not in config file)"
echo " -a <url_title>"
exit 1
}
opt_field() {
field=$1
shift
value="${*}"
if [ ! -z "${value}" ]; then
echo "-F \"${field}=${value}\""
fi
}
validate_token() {
field="${1}"
value="${2}"
opt="${3}"
ret=1
if [ -z "${value}" ]; then
echo "${field} is unset or empty: Did you create ${CONFIG_FILE} or specify ${opt} on the command line?" >&2
elif ! echo "${value}" | egrep -q '[A-Za-z0-9]{30}'; then
echo "Value of ${field}, \"${value}\", does not match expected format. Should be 30 characters of A-Z, a-z and 0-9." >&2;
else
ret=0
fi
return ${ret}
}
expand_aliases() {
BASH_MAJOR="$(echo $BASH_VERSION | cut -d'.' -f1)"
if [ "${BASH_MAJOR}" -lt 4 ]; then
if [ ! -n "${device_aliases}" ]; then
echo "Warning: device_aliases are only support by bash 4+" >&2
fi
echo "${*}"
else
for device in ${*}; do
expanded="${device_aliases["${device}"]}"
if [ -z "${expanded}" ]; then
echo "${device}"
else
echo "${expanded}"
fi
done
fi
}
remove_duplicates() {
echo ${*} | xargs -n1 | sort -u | uniq
}
send_message() {
local device="${1:-}"
curl_cmd="\"${CURL}\" -s -S \
${CURL_OPTS} \
-F \"token=${TOKEN}\" \
-F \"user=${USER}\" \
-F \"message=${message}\" \
$(opt_field device "${device}") \
$(opt_field callback "${callback}") \
$(opt_field timestamp "${timestamp}") \
$(opt_field priority "${priority}") \
$(opt_field retry "${retry}") \
$(opt_field expire "${expire}") \
$(opt_field title "${title}") \
$(opt_field sound "${sound}") \
$(opt_field url "${url}") \
$(opt_field url_title "${url_title}") \
\"${PUSHOVER_URL}\""
# execute and return exit code from curl command
response="$(eval "${curl_cmd}")"
# TODO: Parse response for value of status to give better error to user
r="${?}"
if [ "${r}" -ne 0 ]; then
echo "${0}: Failed to send message" >&2
fi
return "${r}"
}
# Initialize devices
devices="${devices} ${device}"
# Option parsing
optstring="c:d:D:e:p:r:t:T:s:u:U:a:h"
while getopts ${optstring} c; do
case ${c} in
c) callback="${OPTARG}" ;;
d) devices="${devices} ${OPTARG}" ;;
D) timestamp="${OPTARG}" ;;
e) expire="${OPTARG}" ;;
p) priority="${OPTARG}" ;;
r) retry="${OPTARG}" ;;
t) title="${OPTARG}" ;;
T) TOKEN="${OPTARG}" ;;
s) sound="${OPTARG}" ;;
u) url="${OPTARG}" ;;
U) USER="${OPTARG}" ;;
a) url_title="${OPTARG}" ;;
[h\?]) usage ;;
esac
done
shift $((OPTIND-1))
# Is there anything left?
if [ "$#" -lt 1 ]; then
usage
fi
message="$*"
# Check for required config variables
if [ ! -x "${CURL}" ]; then
echo "CURL is unset, empty, or does not point to curl executable. This script requires curl!" >&2
exit 1
fi
validate_token "TOKEN" "${TOKEN}" "-T" || exit $?
validate_token "USER" "${USER}" "-U" || exit $?
devices="$(expand_aliases ${devices})"
devices="$(remove_duplicates ${devices})"
if [ -z "${devices}" ]; then
send_message
r=${?}
else
for device in ${devices}; do
send_message "${device}"
r=${?}
if [ "${r}" -ne 0 ]; then
break;
fi
done
fi
exit "${r}"