-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbashover.sh
213 lines (198 loc) · 4.76 KB
/
bashover.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
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
#!/usr/bin/env bash
readonly config="$HOME/.config/bashover/default.conf"
readonly pushover_url="https://api.pushover.net/1/messages.json"
make_config () {
echo token='""' >> "$1"
echo user='""' >> "$1"
echo message='""' >> "$1"
echo attachment='""' >> "$1"
echo attachment_base64='""' >> "$1"
echo attachment_type='""' >> "$1"
echo device='""' >> "$1"
echo html='""' >> "$1"
echo priority='""' >> "$1"
echo sound='""' >> "$1"
echo timestamp='""' >> "$1"
echo title='""' >> "$1"
echo ttl='""' >> "$1"
echo url='""' >> "$1"
echo url_title='""' >> "$1"
echo retry='""' >> "$1"
echo expire='""' >> "$1"
echo callback='""' >> "$1"
}
parse_config () {
source $1
a_token="${a_token:-${token}}"
a_user="${a_user:-${user}}"
a_message="${a_message:-${message}}"
a_attachment="${a_attachment:-${attachment}}"
a_attachment_base64="${a_attachment_base64:-${attachment_base64}}"
a_attachment_type="${a_attachment_type:-${attachment_type}}"
a_device="${a_device:-${device}}"
a_html="${a_html:-${html}}"
a_priority="${a_priority:-${priority}}"
a_sound="${a_sound:-${sound}}"
a_timestamp="${a_timestamp:-${timestamp}}"
a_title="${a_title:-${title}}"
a_ttl="${a_ttl:-${ttl}}"
a_url="${a_url:-${url}}"
a_url_title="${a_url_title:-${url_title}}"
a_retry="${a_retry:-${retry}}"
a_expire="${a_expire:-${expire}}"
a_callback="${a_callback:-${callback}}"
}
parse_arguments() {
while [ : ]; do
case "$1" in
-t | --token)
a_token="$2"
shift
;;
-u | --user)
a_user="$2"
shift
;;
-m | --message)
a_message="$2"
shift
;;
--attachment)
a_attachment="$2"
shift
;;
--attachment-base64)
a_attachment_base64="$2"
shift
;;
--attachment-type)
a_attachment_type="$2"
shift
;;
--device)
a_device="$2"
shift
;;
--html)
a_html="$2"
shift
;;
--priority)
a_priority="$2"
shift
;;
--sound)
a_sound="$2"
shift
;;
--timestamp)
a_timestamp="$2"
shift
;;
--title)
a_title="$2"
shift
;;
--ttl)
a_ttl="$2"
shift
;;
--url)
a_url="$2"
shift
;;
--url-title)
a_url_title="$2"
shift
;;
--retry)
a_retry="$2"
shift
;;
--expire)
a_expire="$2"
shift
;;
--callback)
a_callback="$2"
shift
;;
-i | --ignore-defaults)
a_ignore_defaults=True
;;
-v)
a_verbose=True
;;
-c | --config)
a_config="$2"
shift
;;
--)
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break
esac
shift
done
}
main() {
if [ ! -f "$config" ]; then
echo "No default config found, creating at $config"
mkdir -p $HOME/.config/bashover
make_config $config
fi
parse_arguments "$@"
if [[ -n $a_config ]]; then
if [ ! -f "$a_config" ]; then
echo "Error: Config not found at $a_config, making a template."
make_config $a_config
exit 1;
fi
parse_config $a_config
fi
if [[ ! $a_ignore_defaults && -z $a_config ]]; then
parse_config $config
fi
if [ ${#a_token} == 0 ]; then
echo "Error: You need to supply an app token"
exit 1;
fi
if [ ${#a_user} == 0 ]; then
echo "Error: You need to supply a user key"
exit 1;
fi
if [ ${#a_message} == 0 ]; then
echo "Error: Message cannot be blank"
exit 1;
fi
response=`curl -s \
--form-string "token=$a_token" \
--form-string "user=$a_user" \
--form-string "message=$a_message" \
${a_attachment:+--form "attachment=@$a_attachment"} \
${a_attachment_base64:+--form-string "attachment_base64=$a_attachment_base64"} \
${a_attachment_type:+--form-string "attachment_type=$a_attachment_type"} \
${a_device:+--form-string "device=$a_device"} \
${a_html:+--form-string "html=$a_html"} \
${a_priority:+--form-string "priority=$a_priority"} \
${a_sound:+--form-string "sound=$a_sound"} \
${a_timestamp:+--form-string "timestamp=$a_timestamp"} \
${a_title:+--form-string "title=$a_title"} \
${a_ttl:+--form-string "ttl=$a_ttl"} \
${a_url:+--form-string "url=$a_url"} \
${a_url_title:+--form-string "url_title=$a_url_title"} \
${a_retry:+--form-string "retry=$a_retry"} \
${a_expire:+--form-string "expire=$a_expire"} \
${a_callback:+--form-string "expire=$a_callback"} \
$pushover_url`
if [ $a_verbose ]; then
echo $response
fi
exit 0;
}
main "$@"