Directly use pygmentize in Makefile (for scn script in Website)

This commit is contained in:
Mike Schwörer 2023-05-27 17:42:06 +02:00
parent 28c2721036
commit fd72b512f8
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
16 changed files with 484 additions and 299 deletions

View File

@ -5,9 +5,9 @@ PORT=9090
NAMESPACE=$(shell git rev-parse --abbrev-ref HEAD)
HASH=$(shell git rev-parse HEAD)
.PHONY: test swagger
.PHONY: test swagger pygmentize
build: swagger fmt
build: swagger pygmentize fmt
mkdir -p _build
rm -f ./_build/scn_backend
go generate ./...
@ -18,8 +18,8 @@ run: build
_build/scn_backend
gow:
# go install github.com/mitranim/gow@latest
gow run blackforestbytes.com/portfoliomanager2/cmd/server
which gow || go install github.com/mitranim/gow@latest
gow -e "go,mod,html,css,json,yaml,js" run -tags "timetzdata sqlite_fts5 sqlite_foreign_keys" blackforestbytes.com/simplecloudnotifier/cmd/scnserver
docker: build
[ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO
@ -40,6 +40,13 @@ swagger:
which swag || go install github.com/swaggo/swag/cmd/swag@v1.8.12
swag init -generalInfo api/router.go --propertyStrategy snakecase --output ./swagger/ --outputTypes "json,yaml"
pygmentize: website/scn_send.html
website/scn_send.html: website/scn_send.sh.txt
_pygments/pygmentizew -l bash -f html "$(shell pwd)/website/scn_send.sh.txt" > "$(shell pwd)/website/scn_send.html"
_pygments/pygmentizew -S monokai -f html > "$(shell pwd)/website/css/pygmnetize-dark.css"
_pygments/pygmentizew -S borland -f html > "$(shell pwd)/website/css/pygmnetize-light.css"
run-docker-local: docker
mkdir -p .run-data
docker run --rm \

View File

@ -39,8 +39,6 @@
- jobs to clear requests-db and logs-db after to only keep X entries...
-> logs and request-logging into their own sqlite files (sqlite-files are prepped)
- /send endpoint should be compatible with the [ webhook ] notifier of uptime-kuma
(or add another /kuma endpoint)
-> https://webhook.site/
@ -49,13 +47,10 @@
- ios purchase verification
- [X] re-add ack labels as compat table for v1 api user
- return channel as "[..] asdf" in compat methods (mark clients as compat and send compat FB to them...)
(then we can replace the old server without switching phone clients)
(still needs switching of the send-script)
- do not use uuidgen in bash script (potetnially not installed) - use `head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 `
- move to KeyToken model
* [X] User can have multiple keys with different permissions
* [X] compat simply uses default-keys

3
scnserver/_pygments/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.pyc
*.swp
env

16
scnserver/_pygments/pygmentizew Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -o nounset # disallow usage of unset vars ( set -u )
set -o errexit # Exit immediately if a pipeline returns non-zero. ( set -e )
set -o errtrace # Allow the above trap be inherited by all functions in the script. ( set -E )
set -o pipefail # Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
IFS=$'\n\t' # Set $IFS to only newline and tab.
cd "$(dirname "$0")" || exit 1
1>&2 virtualenv env
1>&2 source env/bin/activate
1>&2 pip install Pygments
pygmentize "$@"

View File

@ -21,16 +21,16 @@
<p>Get your user-id and user-key from the android or iOS app.<br/>And send notifications to your phone by performing a POST request against <code>{{config|baseURL}}/</code> from anywhere</p>
<pre>
curl \
--data "user_id=${userid}" \
--data "key=${key}" \
--data "title=${message_title}" \
--data "content=${message_body}" \
--data "priority=${0|1|2}" \
--data "msg_id=$(uuidgen)" \
--data "timestamp=$(date +%s)" \
--data "channel={channel_name}" \
--data "sender_name=$(hostname)" \
curl \
--data "user_id=${userid}" \
--data "key=${key}" \
--data "title=${message_title}" \
--data "content=${message_body}" \
--data "priority=${0|1|2}" \
--data "msg_id=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)" \
--data "timestamp=$(date +%s)" \
--data "channel={channel_name}" \
--data "sender_name=$(hostname)" \
{{config|baseURL}}/</pre>
<p>Most parameters are optional, you can send a message with only a title (default priority and channel will be used)</p>
<pre>

View File

@ -171,7 +171,7 @@
Sometimes your script can run in an environment with an unstable connection and you want to implement an automatic re-try mechanism to send a message again if the last try failed due to bad connectivity.
</p>
<p>
To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple <code>uuidgen</code>).
To ensure that a message is only send once you can generate a unique id for your message (I would recommend a simple <code>uuidgen</code> or <code>head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32</code>).
If you send a message with a UUID that was already used in the near past the API still returns OK, but no new message is sent.
</p>
<p>
@ -210,7 +210,7 @@
Depending on your use case it can be useful to create a bash script that handles things like resending messages if you have connection problems or waiting if there is no quota left.<br/>
Here is an example how such a scrippt could look like, you can put it into <code>/usr/local/sbin</code> and call it with <code>scn_send "title" "content"</code> (or with more parameters, see the script itself)
</p>
{{template|scn_send.[theme].html}}
<div class="yellow-code">{{template|scn_send.html}}</div>
</div>
</div>

View File

@ -0,0 +1,83 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.hll { background-color: #49483e }
.c { color: #75715e } /* Comment */
.err { color: #960050; background-color: #1e0010 } /* Error */
.esc { color: #f8f8f2 } /* Escape */
.g { color: #f8f8f2 } /* Generic */
.k { color: #66d9ef } /* Keyword */
.l { color: #ae81ff } /* Literal */
.n { color: #f8f8f2 } /* Name */
.o { color: #f92672 } /* Operator */
.x { color: #f8f8f2 } /* Other */
.p { color: #f8f8f2 } /* Punctuation */
.ch { color: #75715e } /* Comment.Hashbang */
.cm { color: #75715e } /* Comment.Multiline */
.cp { color: #75715e } /* Comment.Preproc */
.cpf { color: #75715e } /* Comment.PreprocFile */
.c1 { color: #75715e } /* Comment.Single */
.cs { color: #75715e } /* Comment.Special */
.gd { color: #f92672 } /* Generic.Deleted */
.ge { color: #f8f8f2; font-style: italic } /* Generic.Emph */
.gr { color: #f8f8f2 } /* Generic.Error */
.gh { color: #f8f8f2 } /* Generic.Heading */
.gi { color: #a6e22e } /* Generic.Inserted */
.go { color: #66d9ef } /* Generic.Output */
.gp { color: #f92672; font-weight: bold } /* Generic.Prompt */
.gs { color: #f8f8f2; font-weight: bold } /* Generic.Strong */
.gu { color: #75715e } /* Generic.Subheading */
.gt { color: #f8f8f2 } /* Generic.Traceback */
.kc { color: #66d9ef } /* Keyword.Constant */
.kd { color: #66d9ef } /* Keyword.Declaration */
.kn { color: #f92672 } /* Keyword.Namespace */
.kp { color: #66d9ef } /* Keyword.Pseudo */
.kr { color: #66d9ef } /* Keyword.Reserved */
.kt { color: #66d9ef } /* Keyword.Type */
.ld { color: #e6db74 } /* Literal.Date */
.m { color: #ae81ff } /* Literal.Number */
.s { color: #e6db74 } /* Literal.String */
.na { color: #a6e22e } /* Name.Attribute */
.nb { color: #f8f8f2 } /* Name.Builtin */
.nc { color: #a6e22e } /* Name.Class */
.no { color: #66d9ef } /* Name.Constant */
.nd { color: #a6e22e } /* Name.Decorator */
.ni { color: #f8f8f2 } /* Name.Entity */
.ne { color: #a6e22e } /* Name.Exception */
.nf { color: #a6e22e } /* Name.Function */
.nl { color: #f8f8f2 } /* Name.Label */
.nn { color: #f8f8f2 } /* Name.Namespace */
.nx { color: #a6e22e } /* Name.Other */
.py { color: #f8f8f2 } /* Name.Property */
.nt { color: #f92672 } /* Name.Tag */
.nv { color: #f8f8f2 } /* Name.Variable */
.ow { color: #f92672 } /* Operator.Word */
.pm { color: #f8f8f2 } /* Punctuation.Marker */
.w { color: #f8f8f2 } /* Text.Whitespace */
.mb { color: #ae81ff } /* Literal.Number.Bin */
.mf { color: #ae81ff } /* Literal.Number.Float */
.mh { color: #ae81ff } /* Literal.Number.Hex */
.mi { color: #ae81ff } /* Literal.Number.Integer */
.mo { color: #ae81ff } /* Literal.Number.Oct */
.sa { color: #e6db74 } /* Literal.String.Affix */
.sb { color: #e6db74 } /* Literal.String.Backtick */
.sc { color: #e6db74 } /* Literal.String.Char */
.dl { color: #e6db74 } /* Literal.String.Delimiter */
.sd { color: #e6db74 } /* Literal.String.Doc */
.s2 { color: #e6db74 } /* Literal.String.Double */
.se { color: #ae81ff } /* Literal.String.Escape */
.sh { color: #e6db74 } /* Literal.String.Heredoc */
.si { color: #e6db74 } /* Literal.String.Interpol */
.sx { color: #e6db74 } /* Literal.String.Other */
.sr { color: #e6db74 } /* Literal.String.Regex */
.s1 { color: #e6db74 } /* Literal.String.Single */
.ss { color: #e6db74 } /* Literal.String.Symbol */
.bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.fm { color: #a6e22e } /* Name.Function.Magic */
.vc { color: #f8f8f2 } /* Name.Variable.Class */
.vg { color: #f8f8f2 } /* Name.Variable.Global */
.vi { color: #f8f8f2 } /* Name.Variable.Instance */
.vm { color: #f8f8f2 } /* Name.Variable.Magic */
.il { color: #ae81ff } /* Literal.Number.Integer.Long */

View File

@ -0,0 +1,56 @@
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.hll { background-color: #ffffcc }
.c { color: #008800; font-style: italic } /* Comment */
.err { color: #a61717; background-color: #e3d2d2 } /* Error */
.k { color: #000080; font-weight: bold } /* Keyword */
.ch { color: #008800; font-style: italic } /* Comment.Hashbang */
.cm { color: #008800; font-style: italic } /* Comment.Multiline */
.cp { color: #008080 } /* Comment.Preproc */
.cpf { color: #008800; font-style: italic } /* Comment.PreprocFile */
.c1 { color: #008800; font-style: italic } /* Comment.Single */
.cs { color: #008800; font-weight: bold } /* Comment.Special */
.gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #aa0000 } /* Generic.Error */
.gh { color: #999999 } /* Generic.Heading */
.gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.go { color: #888888 } /* Generic.Output */
.gp { color: #555555 } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #aaaaaa } /* Generic.Subheading */
.gt { color: #aa0000 } /* Generic.Traceback */
.kc { color: #000080; font-weight: bold } /* Keyword.Constant */
.kd { color: #000080; font-weight: bold } /* Keyword.Declaration */
.kn { color: #000080; font-weight: bold } /* Keyword.Namespace */
.kp { color: #000080; font-weight: bold } /* Keyword.Pseudo */
.kr { color: #000080; font-weight: bold } /* Keyword.Reserved */
.kt { color: #000080; font-weight: bold } /* Keyword.Type */
.m { color: #0000FF } /* Literal.Number */
.s { color: #0000FF } /* Literal.String */
.na { color: #FF0000 } /* Name.Attribute */
.nt { color: #000080; font-weight: bold } /* Name.Tag */
.ow { font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mb { color: #0000FF } /* Literal.Number.Bin */
.mf { color: #0000FF } /* Literal.Number.Float */
.mh { color: #0000FF } /* Literal.Number.Hex */
.mi { color: #0000FF } /* Literal.Number.Integer */
.mo { color: #0000FF } /* Literal.Number.Oct */
.sa { color: #0000FF } /* Literal.String.Affix */
.sb { color: #0000FF } /* Literal.String.Backtick */
.sc { color: #800080 } /* Literal.String.Char */
.dl { color: #0000FF } /* Literal.String.Delimiter */
.sd { color: #0000FF } /* Literal.String.Doc */
.s2 { color: #0000FF } /* Literal.String.Double */
.se { color: #0000FF } /* Literal.String.Escape */
.sh { color: #0000FF } /* Literal.String.Heredoc */
.si { color: #0000FF } /* Literal.String.Interpol */
.sx { color: #0000FF } /* Literal.String.Other */
.sr { color: #0000FF } /* Literal.String.Regex */
.s1 { color: #0000FF } /* Literal.String.Single */
.ss { color: #0000FF } /* Literal.String.Symbol */
.il { color: #0000FF } /* Literal.Number.Integer.Long */

View File

@ -20,8 +20,9 @@
border-color: #888 !important;
}
pre.yellow-code,
pre.yellow-code span {
.yellow-code,
.yellow-code pre,
.yellow-code span {
background: none;
}
@ -31,4 +32,9 @@ pre.yellow-code span {
#theme-switch:hover {
fill: #88F;
}
code {
background: var(--secondary-back-color);
border: .0625rem solid #444;
}

View File

@ -41,7 +41,7 @@ body
border-left: .25rem solid #E53935;
}
.yellow-code
.yellow-code pre
{
border-left: .25rem solid #FFCB05;
}
@ -270,6 +270,7 @@ a.card:hover
table.scode_table {
max-height: none;
overflow: hidden;
}
table.scode_table td:nth-child(2) {
@ -300,7 +301,7 @@ pre, pre span
}
#theme-switch {
position: absolute;
position: fixed;
top: 0.75rem;
right: 0.75rem;
cursor: pointer;
@ -311,3 +312,8 @@ pre, pre span
filter: blur(1px);
fill: #008;
}
code {
background: #F9F9F9;
border: .0625rem solid var(--secondary-border-color);
}

View File

@ -8,6 +8,8 @@
<link rel="stylesheet" href="/css/mini-dark.min.css">
<link rel="stylesheet" href="/css/style-dark.css">
<link rel="stylesheet" href="/css/pygmnetize-dark.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/favicon.png"/>

View File

@ -6,6 +6,8 @@
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/pygmnetize-light.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/favicon.png"/>
<link rel="icon" type="image/png" href="/favicon.ico"/>

View File

@ -1,138 +0,0 @@
<!-- 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 &quot;${title}&quot;</span>
<span style="color: #75715e"># or scn_send &quot;${title}&quot; ${content}&quot;</span>
<span style="color: #75715e"># or scn_send &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</span>
<span style="color: #75715e"># or scn_send &quot;@${channel} &quot;${title}&quot;</span>
<span style="color: #75715e"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot;</span>
<span style="color: #75715e"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</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">&quot;999&quot;</span>
<span style="color: #f8f8f2">user_key</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;??&quot;</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">&quot;Usage: &quot;</span>
<span style="color: #f8f8f2">echo</span> <span style="color: #e6db74">&quot; scn_send [@channel] title [content] [priority]&quot;</span>
<span style="color: #f8f8f2">echo</span> <span style="color: #e6db74">&quot;&quot;</span>
<span style="color: #f92672">}</span>
<span style="color: #f8f8f2">args</span><span style="color: #f92672">=(</span> <span style="color: #e6db74">&quot;$@&quot;</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">&quot;&quot;</span>
<span style="color: #f8f8f2">channel</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;&quot;</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">&quot;$(uuidgen)&quot;</span>
<span style="color: #f8f8f2">sendtime</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;$(date +%s)&quot;</span>
<span style="color: #f8f8f2">sender</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;$(hostname)&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</span> 1&gt;&amp;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">&quot;${args[0]}&quot;</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">&quot;${args[0]}&quot;</span>
<span style="color: #f8f8f2">unset</span> <span style="color: #e6db74">&quot;args[0]&quot;</span>
<span style="color: #f8f8f2">channel</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;${channel:1}&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</span> 1&gt;&amp;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">&quot;${args[0]}&quot;</span>
<span style="color: #f8f8f2">content</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;&quot;</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">&quot;${args[0]}&quot;</span>
<span style="color: #f8f8f2">unset</span> <span style="color: #e6db74">&quot;args[0]&quot;</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">&quot;${args[0]}&quot;</span>
<span style="color: #f8f8f2">unset</span> <span style="color: #e6db74">&quot;args[0]&quot;</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">&quot;Too many arguments to scn_send&quot;</span> 1&gt;&amp;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">&quot;%{http_code}&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;user_id=$user_id&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;key=$key&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;title=$title&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;timestamp=$sendtime&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;content=$content&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;priority=$priority&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;msg_id=$usr_msg_id&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;channel=$channel&quot;</span> <span style="color: #ae81ff">\</span>
--data <span style="color: #e6db74">&quot;sender_name=$sender&quot;</span> <span style="color: #ae81ff">\</span>
<span style="color: #e6db74">&quot;https://scn.blackforestbytes.com/&quot;</span> <span style="color: #66d9ef">)</span>
<span style="color: #66d9ef">if</span> <span style="color: #f92672">[</span> <span style="color: #e6db74">&quot;$curlresp&quot;</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">&quot;Successfully send&quot;</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">&quot;$curlresp&quot;</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">&quot;Bad request - something went wrong&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</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">&quot;Unauthorized - wrong userid/userkey&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</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">&quot;Quota exceeded - wait one hour before re-try&quot;</span> 1&gt;&amp;2
sleep 3600
<span style="color: #66d9ef">fi</span>
<span style="color: #66d9ef"> if</span> <span style="color: #f92672">[</span> <span style="color: #e6db74">&quot;$curlresp&quot;</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">&quot;Precondition Failed - No device linked&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</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">&quot;Internal server error - waiting for better times&quot;</span> 1&gt;&amp;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">&quot;Send failed (response code $curlresp) ... try again in 5s&quot;</span> 1&gt;&amp;2
sleep 5
<span style="color: #66d9ef">done</span>
</pre>

View File

@ -0,0 +1,142 @@
<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://scn.blackforestbytes.com/ )</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 &quot;${title}&quot;</span>
<span class="c1"># or scn_send &quot;${title}&quot; ${content}&quot;</span>
<span class="c1"># or scn_send &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</span>
<span class="c1"># or scn_send &quot;@${channel} &quot;${title}&quot;</span>
<span class="c1"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot;</span>
<span class="c1"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</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">&quot;999&quot;</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">&quot;??&quot;</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">&quot;Usage: &quot;</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot; scn_send [@channel] title [content] [priority]&quot;</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;&quot;</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">&amp;&amp;</span><span class="w"> </span><span class="o">[</span><span class="w"> </span>-n<span class="w"> </span><span class="s2">&quot;</span><span class="k">$(</span>tput<span class="w"> </span>colors<span class="k">)</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">]</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="s2">&quot;</span><span class="k">$(</span>tput<span class="w"> </span>colors<span class="k">)</span><span class="s2">&quot;</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>&gt;<span class="p">&amp;</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">&quot;\x1B[31m</span><span class="nv">$1</span><span class="s2">\x1B[0m&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">else</span><span class="w"> </span>&gt;<span class="p">&amp;</span><span class="m">2</span><span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</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">&quot;\x1B[32m</span><span class="nv">$1</span><span class="s2">\x1B[0m&quot;</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">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</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">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">)</span>
<span class="nv">title</span><span class="o">=</span><span class="nv">$1</span>
<span class="nv">content</span><span class="o">=</span><span class="s2">&quot;&quot;</span>
<span class="nv">channel</span><span class="o">=</span><span class="s2">&quot;&quot;</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">&quot;</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">&quot;</span>
<span class="nv">sendtime</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$(</span>date<span class="w"> </span>+%s<span class="k">)</span><span class="s2">&quot;</span>
<span class="nv">sender</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$(</span>hostname<span class="k">)</span><span class="s2">&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</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">&quot;</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">&quot;</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">&quot;</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="nb">unset</span><span class="w"> </span><span class="s2">&quot;args[0]&quot;</span>
<span class="w"> </span><span class="nv">channel</span><span class="o">=</span><span class="s2">&quot;</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">&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</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">&quot;</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="nv">content</span><span class="o">=</span><span class="s2">&quot;&quot;</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">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><span class="nv">content</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="nb">unset</span><span class="w"> </span><span class="s2">&quot;args[0]&quot;</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">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><span class="nv">priority</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">args</span><span class="p">[0]</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="nb">unset</span><span class="w"> </span><span class="s2">&quot;args[0]&quot;</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">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">&quot;Too many arguments to scn_send&quot;</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">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">&quot;</span><span class="k">$(</span>mktemp<span class="k">)</span><span class="s2">&quot;</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">&quot;</span><span class="si">${</span><span class="nv">outf</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--write-out<span class="w"> </span><span class="s2">&quot;%{http_code}&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;user_id=</span><span class="nv">$user_id</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;key=</span><span class="nv">$user_key</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;title=</span><span class="nv">$title</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;timestamp=</span><span class="nv">$sendtime</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;content=</span><span class="nv">$content</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;priority=</span><span class="nv">$priority</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;msg_id=</span><span class="nv">$usr_msg_id</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;channel=</span><span class="nv">$channel</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>--data<span class="w"> </span><span class="s2">&quot;sender_name=</span><span class="nv">$sender</span><span class="s2">&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span><span class="s2">&quot;https://scn.blackforestbytes.com/&quot;</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">&quot;</span><span class="k">$(</span>cat<span class="w"> </span><span class="s2">&quot;</span><span class="nv">$outf</span><span class="s2">&quot;</span><span class="k">)</span><span class="s2">&quot;</span>
<span class="w"> </span>rm<span class="w"> </span><span class="s2">&quot;</span><span class="nv">$outf</span><span class="s2">&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Successfully send&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Bad request - something went wrong&quot;</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;</span><span class="nv">$curlout</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Unauthorized - wrong userid/userkey&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Quota exceeded - wait 5 min before re-try&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Precondition Failed - No device linked&quot;</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">&quot;</span><span class="nv">$curlresp</span><span class="s2">&quot;</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">&quot;Internal server error - waiting for better times&quot;</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">&quot;Send failed (response code </span><span class="nv">$curlresp</span><span class="s2">) ... try again in 5s&quot;</span>
<span class="w"> </span>sleep<span class="w"> </span><span class="m">5</span>
<span class="k">done</span>
</pre></div>

View File

@ -1,136 +0,0 @@
<!-- 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 &quot;${title}&quot;</span>
<span style="color: #008800; font-style: italic"># or scn_send &quot;${title}&quot; ${content}&quot;</span>
<span style="color: #008800; font-style: italic"># or scn_send &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</span>
<span style="color: #008800; font-style: italic"># or scn_send &quot;@${channel} &quot;${title}&quot;</span>
<span style="color: #008800; font-style: italic"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot;</span>
<span style="color: #008800; font-style: italic"># or scn_send &quot;@${channel} &quot;${title}&quot; ${content}&quot; &quot;${priority:0|1|2}&quot;</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">&quot;999&quot;</span>
key=<span style="color: #0000FF">&quot;??&quot;</span>
<span style="color: #008800; font-style: italic">################################################################################</span>
usage() {
echo <span style="color: #0000FF">&quot;Usage: &quot;</span>
echo <span style="color: #0000FF">&quot; scn_send [@channel] title [content] [priority]&quot;</span>
echo <span style="color: #0000FF">&quot;&quot;</span>
}
args=( <span style="color: #0000FF">&quot;$@&quot;</span> )
title=$1
content=<span style="color: #0000FF">&quot;&quot;</span>
channel=<span style="color: #0000FF">&quot;&quot;</span>
priority=1
usr_msg_id=<span style="color: #0000FF">&quot;$(uuidgen)&quot;</span>
sendtime=<span style="color: #0000FF">&quot;$(date +%s)&quot;</span>
sender=<span style="color: #0000FF">&quot;$(hostname)&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</span> 1&gt;&amp;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">&quot;${args[0]}&quot;</span> =~ ^@.* ]]; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>channel=<span style="color: #0000FF">&quot;${args[0]}&quot;</span>
unset <span style="color: #0000FF">&quot;args[0]&quot;</span>
channel=<span style="color: #0000FF">&quot;${channel:1}&quot;</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">&quot;[ERROR]: no title supplied via parameter&quot;</span> 1&gt;&amp;2
usage
exit 1
<span style="color: #000080; font-weight: bold">fi</span>
title=<span style="color: #0000FF">&quot;${args[0]}&quot;</span>
content=<span style="color: #0000FF">&quot;&quot;</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">&quot;${args[0]}&quot;</span>
unset <span style="color: #0000FF">&quot;args[0]&quot;</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">&quot;${args[0]}&quot;</span>
unset <span style="color: #0000FF">&quot;args[0]&quot;</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">&quot;Too many arguments to scn_send&quot;</span> 1&gt;&amp;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">&quot;%{http_code}&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;user_id=$user_id&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;key=$key&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;title=$title&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;timestamp=$sendtime&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;content=$content&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;priority=$priority&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;msg_id=$usr_msg_id&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;channel=$channel&quot;</span> <span style="color: #0000FF">\</span>
--data <span style="color: #0000FF">&quot;sender_name=$sender&quot;</span> <span style="color: #0000FF">\</span>
<span style="color: #0000FF">&quot;https://scn.blackforestbytes.com/&quot;</span> <span style="color: #000080; font-weight: bold">)</span>
<span style="color: #000080; font-weight: bold">if</span> [ <span style="color: #0000FF">&quot;$curlresp&quot;</span> == 200 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Successfully send&quot;</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">&quot;$curlresp&quot;</span> == 400 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Bad request - something went wrong&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</span> == 401 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Unauthorized - wrong userid/userkey&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</span> == 403 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Quota exceeded - wait one hour before re-try&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</span> == 412 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Precondition Failed - No device linked&quot;</span> 1&gt;&amp;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">&quot;$curlresp&quot;</span> == 500 ] ; <span style="color: #000080; font-weight: bold">then</span>
<span style="color: #000080; font-weight: bold"> </span>echo <span style="color: #0000FF">&quot;Internal server error - waiting for better times&quot;</span> 1&gt;&amp;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">&quot;Send failed (response code $curlresp) ... try again in 5s&quot;</span> 1&gt;&amp;2
sleep 5
<span style="color: #000080; font-weight: bold">done</span>
</pre>

View File

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