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