136 lines
11 KiB
HTML
136 lines
11 KiB
HTML
|
|
<!-- https://hilite.me/ | bash | borland -->
|
|
|
|
<!-- HTML generated using hilite.me -->
|
|
|
|
<pre style="color:#000000; margin: 0; line-height: 125%" class="yellow-code">
|
|
<span style="color: #008800; font-style: italic">#!/usr/bin/env bash</span>
|
|
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
<span style="color: #008800; font-style: italic"># Wrapper around SCN ( https://scn.blackforestbytes.com/ )</span>
|
|
<span style="color: #008800; font-style: italic"># ========================================================</span>
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
<span style="color: #008800; font-style: italic"># ./scn_send [@channel] title [content] [priority]</span>
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
<span style="color: #008800; font-style: italic"># Call with scn_send "${title}"</span>
|
|
<span style="color: #008800; font-style: italic"># or scn_send "${title}" ${content}"</span>
|
|
<span style="color: #008800; font-style: italic"># or scn_send "${title}" ${content}" "${priority:0|1|2}"</span>
|
|
<span style="color: #008800; font-style: italic"># or scn_send "@${channel} "${title}"</span>
|
|
<span style="color: #008800; font-style: italic"># or scn_send "@${channel} "${title}" ${content}"</span>
|
|
<span style="color: #008800; font-style: italic"># or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"</span>
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
<span style="color: #008800; font-style: italic">#</span>
|
|
|
|
<span style="color: #008800; font-style: italic">################################################################################</span>
|
|
<span style="color: #008800; font-style: italic"># INSERT YOUR DATA HERE #</span>
|
|
<span style="color: #008800; font-style: italic">################################################################################</span>
|
|
user_id=<span style="color: #0000FF">"999"</span>
|
|
user_key=<span style="color: #0000FF">"??"</span>
|
|
<span style="color: #008800; font-style: italic">################################################################################</span>
|
|
|
|
usage() {
|
|
echo <span style="color: #0000FF">"Usage: "</span>
|
|
echo <span style="color: #0000FF">" scn_send [@channel] title [content] [priority]"</span>
|
|
echo <span style="color: #0000FF">""</span>
|
|
}
|
|
|
|
args=( <span style="color: #0000FF">"$@"</span> )
|
|
|
|
title=$1
|
|
content=<span style="color: #0000FF">""</span>
|
|
channel=<span style="color: #0000FF">""</span>
|
|
priority=1
|
|
usr_msg_id=<span style="color: #0000FF">"$(uuidgen)"</span>
|
|
sendtime=<span style="color: #0000FF">"$(date +%s)"</span>
|
|
sender=<span style="color: #0000FF">"$(hostname)"</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #000080; font-weight: bold">${#</span>args[@]<span style="color: #000080; font-weight: bold">}</span> -lt 1 ]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"[ERROR]: no title supplied via parameter"</span> 1>&2
|
|
usage
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [[ <span style="color: #0000FF">"${args[0]}"</span> =~ ^@.* ]]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>channel=<span style="color: #0000FF">"${args[0]}"</span>
|
|
unset <span style="color: #0000FF">"args[0]"</span>
|
|
channel=<span style="color: #0000FF">"${channel:1}"</span>
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #000080; font-weight: bold">${#</span>args[@]<span style="color: #000080; font-weight: bold">}</span> -lt 1 ]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"[ERROR]: no title supplied via parameter"</span> 1>&2
|
|
usage
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
title=<span style="color: #0000FF">"${args[0]}"</span>
|
|
content=<span style="color: #0000FF">""</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #000080; font-weight: bold">${#</span>args[@]<span style="color: #000080; font-weight: bold">}</span> -gt 1 ]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>content=<span style="color: #0000FF">"${args[0]}"</span>
|
|
unset <span style="color: #0000FF">"args[0]"</span>
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #000080; font-weight: bold">${#</span>args[@]<span style="color: #000080; font-weight: bold">}</span> -gt 1 ]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>priority=<span style="color: #0000FF">"${args[0]}"</span>
|
|
unset <span style="color: #0000FF">"args[0]"</span>
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #000080; font-weight: bold">${#</span>args[@]<span style="color: #000080; font-weight: bold">}</span> -gt 1 ]; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Too many arguments to scn_send"</span> 1>&2
|
|
usage
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
|
|
<span style="color: #000080; font-weight: bold">while </span>true ; <span style="color: #000080; font-weight: bold">do</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> </span>curlresp=<span style="color: #000080; font-weight: bold">$(</span>curl --silent <span style="color: #0000FF">\</span>
|
|
--output /dev/null <span style="color: #0000FF">\</span>
|
|
--write-out <span style="color: #0000FF">"%{http_code}"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"user_id=$user_id"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"user_key=$user_key"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"title=$title"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"timestamp=$sendtime"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"content=$content"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"priority=$priority"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"msg_id=$usr_msg_id"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"channel=$channel"</span> <span style="color: #0000FF">\</span>
|
|
--data <span style="color: #0000FF">"sender_name=$sender"</span> <span style="color: #0000FF">\</span>
|
|
<span style="color: #0000FF">"https://scn.blackforestbytes.com/"</span> <span style="color: #000080; font-weight: bold">)</span>
|
|
|
|
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 200 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Successfully send"</span>
|
|
exit 0
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 400 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Bad request - something went wrong"</span> 1>&2
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 401 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Unauthorized - wrong userid/userkey"</span> 1>&2
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 403 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Quota exceeded - wait one hour before re-try"</span> 1>&2
|
|
sleep 3600
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 412 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Precondition Failed - No device linked"</span> 1>&2
|
|
exit 1
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #000080; font-weight: bold"> if</span> [ <span style="color: #0000FF">"$curlresp"</span> == 500 ] ; <span style="color: #000080; font-weight: bold">then</span>
|
|
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">"Internal server error - waiting for better times"</span> 1>&2
|
|
sleep 60
|
|
<span style="color: #000080; font-weight: bold">fi</span>
|
|
|
|
<span style="color: #008800; font-style: italic"># if none of the above matched we probably hav no network ...</span>
|
|
echo <span style="color: #0000FF">"Send failed (response code $curlresp) ... try again in 5s"</span> 1>&2
|
|
sleep 5
|
|
<span style="color: #000080; font-weight: bold">done</span>
|
|
</pre> |