160 lines
24 KiB
HTML
160 lines
24 KiB
HTML
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env bash</span>
|
|
|
|
<span class="c1">#</span>
|
|
<span class="c1"># Wrapper around SCN ( https://simplecloudnotifier.de/ )</span>
|
|
<span class="c1"># ======================================================</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1"># ./scn_send [@channel] title [content] [priority]</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1"># Call with scn_send "${title}"</span>
|
|
<span class="c1"># or scn_send "${title}" ${content}"</span>
|
|
<span class="c1"># or scn_send "${title}" ${content}" "${priority:0|1|2}"</span>
|
|
<span class="c1"># or scn_send "@${channel} "${title}"</span>
|
|
<span class="c1"># or scn_send "@${channel} "${title}" ${content}"</span>
|
|
<span class="c1"># or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1">#</span>
|
|
|
|
<span class="c1">################################################################################</span>
|
|
<span class="c1"># INSERT YOUR DATA HERE #</span>
|
|
<span class="c1">################################################################################</span>
|
|
<span class="nv">user_id</span><span class="o">=</span><span class="s2">"999"</span><span class="w"> </span><span class="c1"># your user_id</span>
|
|
<span class="nv">user_key</span><span class="o">=</span><span class="s2">"??"</span><span class="w"> </span><span class="c1"># use userkey with SEND permissions on the used channel</span>
|
|
<span class="c1">################################################################################</span>
|
|
|
|
usage<span class="o">()</span><span class="w"> </span><span class="o">{</span>
|
|
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"Usage: "</span>
|
|
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">" scn_send [@channel] title [content] [priority]"</span>
|
|
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">""</span>
|
|
<span class="o">}</span>
|
|
|
|
<span class="k">function</span><span class="w"> </span>cfgcol<span class="w"> </span><span class="o">{</span><span class="w"> </span><span class="o">[</span><span class="w"> </span>-t<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="o">[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">"</span><span class="k">$(</span>tput<span class="w"> </span>colors<span class="k">)</span><span class="s2">"</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="k">$(</span>tput<span class="w"> </span>colors<span class="k">)</span><span class="s2">"</span><span class="w"> </span>-ge<span class="w"> </span><span class="m">8</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="o">}</span>
|
|
|
|
<span class="k">function</span><span class="w"> </span>rederr<span class="o">()</span><span class="w"> </span><span class="o">{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span>cfgcol<span class="p">;</span><span class="w"> </span><span class="k">then</span><span class="w"> </span>><span class="p">&</span><span class="m">2</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span>-e<span class="w"> </span><span class="s2">"\x1B[31m</span><span class="nv">$1</span><span class="s2">\x1B[0m"</span><span class="p">;</span><span class="w"> </span><span class="k">else</span><span class="w"> </span>><span class="p">&</span><span class="m">2</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="p">;</span><span class="w"> </span><span class="k">fi</span><span class="p">;</span><span class="w"> </span><span class="o">}</span>
|
|
<span class="k">function</span><span class="w"> </span>green<span class="o">()</span><span class="w"> </span><span class="o">{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span>cfgcol<span class="p">;</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span>-e<span class="w"> </span><span class="s2">"\x1B[32m</span><span class="nv">$1</span><span class="s2">\x1B[0m"</span><span class="p">;</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span><span class="p">;</span><span class="w"> </span><span class="k">fi</span><span class="p">;</span><span class="w"> </span><span class="o">}</span>
|
|
|
|
<span class="nv">args</span><span class="o">=(</span><span class="w"> </span><span class="s2">"</span><span class="nv">$@</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
|
|
<span class="nv">title</span><span class="o">=</span><span class="s2">""</span>
|
|
<span class="nv">content</span><span class="o">=</span><span class="s2">""</span>
|
|
<span class="nv">channel</span><span class="o">=</span><span class="s2">""</span>
|
|
<span class="nv">priority</span><span class="o">=</span><span class="m">1</span>
|
|
<span class="nv">usr_msg_id</span><span class="o">=</span><span class="s2">"</span><span class="k">$(</span>head<span class="w"> </span>/dev/urandom<span class="w"> </span><span class="p">|</span><span class="w"> </span>tr<span class="w"> </span>-dc<span class="w"> </span>A-Za-z0-9<span class="w"> </span><span class="p">|</span><span class="w"> </span>head<span class="w"> </span>-c<span class="w"> </span><span class="m">32</span><span class="k">)</span><span class="s2">"</span>
|
|
<span class="nv">sendtime</span><span class="o">=</span><span class="s2">"</span><span class="k">$(</span>date<span class="w"> </span>+%s<span class="k">)</span><span class="s2">"</span>
|
|
<span class="nv">sender</span><span class="o">=</span><span class="s2">"</span><span class="k">$(</span>hostname<span class="k">)</span><span class="s2">"</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="si">${#</span><span class="nv">args</span><span class="p">[@]</span><span class="si">}</span><span class="w"> </span>-lt<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"[ERROR]: no title supplied via parameter"</span>
|
|
<span class="w"> </span>usage
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[[</span><span class="w"> </span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">"</span><span class="w"> </span><span class="o">=</span>~<span class="w"> </span>^@.*<span class="w"> </span><span class="o">]]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">channel</span><span class="o">=</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">"</span>
|
|
<span class="w"> </span><span class="nv">args</span><span class="o">=(</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[@]:</span><span class="nv">1</span><span class="si">}</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="w"> </span><span class="nv">channel</span><span class="o">=</span><span class="s2">"</span><span class="si">${</span><span class="nv">channel</span><span class="p">:</span><span class="nv">1</span><span class="si">}</span><span class="s2">"</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="si">${#</span><span class="nv">args</span><span class="p">[@]</span><span class="si">}</span><span class="w"> </span>-lt<span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"[ERROR]: no title supplied via parameter"</span>
|
|
<span class="w"> </span>usage
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="nv">title</span><span class="o">=</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">"</span>
|
|
<span class="nv">args</span><span class="o">=(</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[@]:</span><span class="nv">1</span><span class="si">}</span><span class="s2">"</span><span class="o">)</span>
|
|
|
|
<span class="nv">content</span><span class="o">=</span><span class="s2">""</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="si">${#</span><span class="nv">args</span><span class="p">[@]</span><span class="si">}</span><span class="w"> </span>-gt<span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">content</span><span class="o">=</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">"</span>
|
|
<span class="w"> </span><span class="nv">args</span><span class="o">=(</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[@]:</span><span class="nv">1</span><span class="si">}</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="si">${#</span><span class="nv">args</span><span class="p">[@]</span><span class="si">}</span><span class="w"> </span>-gt<span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">priority</span><span class="o">=</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">"</span>
|
|
<span class="w"> </span><span class="nv">args</span><span class="o">=(</span><span class="s2">"</span><span class="si">${</span><span class="nv">args</span><span class="p">[@]:</span><span class="nv">1</span><span class="si">}</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="si">${#</span><span class="nv">args</span><span class="p">[@]</span><span class="si">}</span><span class="w"> </span>-gt<span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Too many arguments to scn_send"</span>
|
|
<span class="w"> </span>usage
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="nv">curlparams</span><span class="o">=()</span>
|
|
|
|
<span class="nv">curlparams</span><span class="o">+=(</span><span class="w"> </span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"user_id=</span><span class="si">${</span><span class="nv">SCN_UID</span><span class="si">}</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
<span class="nv">curlparams</span><span class="o">+=(</span><span class="w"> </span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"key=</span><span class="si">${</span><span class="nv">SCN_KEY</span><span class="si">}</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
<span class="nv">curlparams</span><span class="o">+=(</span><span class="w"> </span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"title=</span><span class="nv">$title</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
<span class="nv">curlparams</span><span class="o">+=(</span><span class="w"> </span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"timestamp=</span><span class="nv">$sendtime</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
<span class="nv">curlparams</span><span class="o">+=(</span><span class="w"> </span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"msg_id=</span><span class="nv">$usr_msg_id</span><span class="s2">"</span><span class="w"> </span><span class="o">)</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">"</span><span class="nv">$content</span><span class="s2">"</span><span class="w"> </span><span class="o">]]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">curlparams</span><span class="o">+=(</span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"content=</span><span class="nv">$content</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">"</span><span class="nv">$priority</span><span class="s2">"</span><span class="w"> </span><span class="o">]]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">curlparams</span><span class="o">+=(</span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"priority=</span><span class="nv">$priority</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">"</span><span class="nv">$channel</span><span class="s2">"</span><span class="w"> </span><span class="o">]]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">curlparams</span><span class="o">+=(</span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"channel=</span><span class="nv">$channel</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">if</span><span class="w"> </span><span class="o">[[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">"</span><span class="nv">$sender</span><span class="s2">"</span><span class="w"> </span><span class="o">]]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span><span class="nv">curlparams</span><span class="o">+=(</span><span class="s2">"--data"</span><span class="w"> </span><span class="s2">"sender_name=</span><span class="nv">$sender</span><span class="s2">"</span><span class="o">)</span>
|
|
<span class="k">fi</span>
|
|
|
|
<span class="k">while</span><span class="w"> </span><span class="nb">true</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">do</span>
|
|
|
|
<span class="w"> </span><span class="nv">outf</span><span class="o">=</span><span class="s2">"</span><span class="k">$(</span>mktemp<span class="k">)</span><span class="s2">"</span>
|
|
|
|
<span class="w"> </span><span class="nv">curlresp</span><span class="o">=</span><span class="k">$(</span>curl<span class="w"> </span>--silent<span class="w"> </span><span class="se">\</span>
|
|
<span class="w"> </span>--output<span class="w"> </span><span class="s2">"</span><span class="si">${</span><span class="nv">outf</span><span class="si">}</span><span class="s2">"</span><span class="w"> </span><span class="se">\</span>
|
|
<span class="w"> </span>--write-out<span class="w"> </span><span class="s2">"%{http_code}"</span><span class="w"> </span><span class="se">\</span>
|
|
<span class="w"> </span><span class="s2">"</span><span class="si">${</span><span class="nv">curlparams</span><span class="p">[@]</span><span class="si">}</span><span class="s2">"</span><span class="w"> </span><span class="se">\</span>
|
|
<span class="w"> </span><span class="s2">"https://simplecloudnotifier.de/"</span><span class="w"> </span><span class="k">)</span>
|
|
|
|
<span class="w"> </span><span class="nv">curlout</span><span class="o">=</span><span class="s2">"</span><span class="k">$(</span>cat<span class="w"> </span><span class="s2">"</span><span class="nv">$outf</span><span class="s2">"</span><span class="k">)</span><span class="s2">"</span>
|
|
<span class="w"> </span>rm<span class="w"> </span><span class="s2">"</span><span class="nv">$outf</span><span class="s2">"</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">200</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>green<span class="w"> </span><span class="s2">"Successfully send"</span>
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">0</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">400</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Bad request - something went wrong"</span>
|
|
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlout</span><span class="s2">"</span>
|
|
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">""</span>
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">401</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Unauthorized - wrong userid/userkey"</span>
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">403</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Quota exceeded - wait 5 min before re-try"</span>
|
|
<span class="w"> </span>sleep<span class="w"> </span><span class="m">300</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">412</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Precondition Failed - No device linked"</span>
|
|
<span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">"</span><span class="nv">$curlresp</span><span class="s2">"</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">500</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Internal server error - waiting for better times"</span>
|
|
<span class="w"> </span>sleep<span class="w"> </span><span class="m">60</span>
|
|
<span class="w"> </span><span class="k">fi</span>
|
|
|
|
<span class="w"> </span><span class="c1"># if none of the above matched we probably have no network ...</span>
|
|
<span class="w"> </span>rederr<span class="w"> </span><span class="s2">"Send failed (response code </span><span class="nv">$curlresp</span><span class="s2">) ... try again in 5s"</span>
|
|
<span class="w"> </span>sleep<span class="w"> </span><span class="m">5</span>
|
|
<span class="k">done</span>
|
|
</pre></div>
|