Add scn_send.sh to repo

This commit is contained in:
Mike Schwörer 2023-10-14 21:37:00 +02:00
parent b712ad3488
commit 0a380f861e
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 79 additions and 24 deletions

View File

@ -14,13 +14,10 @@
# or scn_send "@${channel} "${title}" ${content}"
# or scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"
#
# content can be of format "--scnsend-read-body-from-file={path}" to read body from file
# (this circumvents max commandline length)
#
################################################################################
# INSERT YOUR DATA HERE #
################################################################################
user_id="999" # your user_id
user_key="??" # use userkey with SEND permissions on the used channel
################################################################################
usage() {
@ -34,16 +31,40 @@ function cfgcol { [ -t 1 ] && [ -n "$(tput colors)" ] && [ "$(tput colors)" -ge
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; }
################################################################################
#
# Get env 'SCN_UID' and 'SCN_KEY' from conf file
#
# shellcheck source=/dev/null
. "/etc/scn.conf"
SCN_UID=${SCN_UID:-}
SCN_KEY=${SCN_KEY:-}
[ -z "${SCN_UID}" ] && { rederr "Missing config value 'SCN_UID' in /etc/scn.conf"; exit 1; }
[ -z "${SCN_KEY}" ] && { rederr "Missing config value 'SCN_KEY' in /etc/scn.conf"; exit 1; }
################################################################################
args=( "$@" )
title=""
content=""
channel=""
priority=1
priority=""
usr_msg_id="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)"
sendtime="$(date +%s)"
sender="$(hostname)"
if command -v srvname &> /dev/null; then
sender="$( srvname )"
fi
if [[ "${args[0]}" = "--" ]]; then
# only positional args form here on (currently not handled)
args=("${args[@]:1}")
fi
if [ ${#args[@]} -lt 1 ]; then
rederr "[ERROR]: no title supplied via parameter"
usage
@ -83,6 +104,11 @@ if [ ${#args[@]} -gt 0 ]; then
exit 1
fi
if [[ "$content" == --scnsend-read-body-from-file=* ]]; then
path="$( awk '{ print substr($0, 31) }' <<< "$content" )"
content="$( cat "$path" )"
fi
curlparams=()
curlparams+=( "--data-urlencode" "user_id=${SCN_UID}" )

View File

@ -45,10 +45,10 @@ swagger:
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"
website/scn_send.html: ../scn_send.sh
_pygments/pygmentizew -l bash -f html "$(shell pwd)/../scn_send.sh" > "$(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

View File

@ -10,9 +10,11 @@
- ios purchase verification
- use goext.ginWrapper
- (!) use goext.ginWrapper
- use goext.exerr
- (!) use goext.exerr
- use bfcodegen (enums+id)
#### UNSURE

View File

@ -1,18 +1,19 @@
package handler
import (
"database/sql"
"errors"
"net/http"
"strings"
"time"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"net/http"
"strings"
"time"
)
// ListMessages swaggerdoc
@ -174,7 +175,7 @@ func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
// @Failure 404 {object} ginresp.apiError "message not found"
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/messages/{mid} [PATCH]
// @Router /api/v2/messages/{mid} [GET]
func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
type uri struct {
MessageID models.MessageID `uri:"mid" binding:"entityid"`

View File

@ -14,13 +14,10 @@
<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"># content can be of format &quot;--scnsend-read-body-from-file={path}&quot; to read body from file</span>
<span class="c1"># (this circumvents max commandline length)</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>
@ -34,16 +31,40 @@ usage<span class="o">()</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="c1">################################################################################</span>
<span class="c1">#</span>
<span class="c1"># Get env &#39;SCN_UID&#39; and &#39;SCN_KEY&#39; from conf file</span>
<span class="c1"># </span>
<span class="c1"># shellcheck source=/dev/null</span>
.<span class="w"> </span><span class="s2">&quot;/etc/scn.conf&quot;</span>
<span class="nv">SCN_UID</span><span class="o">=</span><span class="si">${</span><span class="nv">SCN_UID</span><span class="k">:-</span><span class="si">}</span>
<span class="nv">SCN_KEY</span><span class="o">=</span><span class="si">${</span><span class="nv">SCN_KEY</span><span class="k">:-</span><span class="si">}</span>
<span class="o">[</span><span class="w"> </span>-z<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">SCN_UID</span><span class="si">}</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>rederr<span class="w"> </span><span class="s2">&quot;Missing config value &#39;SCN_UID&#39; in /etc/scn.conf&quot;</span><span class="p">;</span><span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span><span class="p">;</span><span class="w"> </span><span class="o">}</span>
<span class="o">[</span><span class="w"> </span>-z<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">SCN_KEY</span><span class="si">}</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>rederr<span class="w"> </span><span class="s2">&quot;Missing config value &#39;SCN_KEY&#39; in /etc/scn.conf&quot;</span><span class="p">;</span><span class="w"> </span><span class="nb">exit</span><span class="w"> </span><span class="m">1</span><span class="p">;</span><span class="w"> </span><span class="o">}</span>
<span class="c1">################################################################################</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="s2">&quot;&quot;</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">priority</span><span class="o">=</span><span class="s2">&quot;&quot;</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="nb">command</span><span class="w"> </span>-v<span class="w"> </span>srvname<span class="w"> </span><span class="p">&amp;</span>&gt;<span class="w"> </span>/dev/null<span class="p">;</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="nv">sender</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$(</span><span class="w"> </span>srvname<span class="w"> </span><span class="k">)</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="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="s2">&quot;--&quot;</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="c1"># only positional args form here on (currently not handled)</span>
<span class="w"> </span><span class="nv">args</span><span class="o">=(</span><span class="s2">&quot;</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">&quot;</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>-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
@ -83,6 +104,11 @@ usage<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<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="nv">$content</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">==</span><span class="w"> </span>--scnsend-read-body-from-file<span class="o">=</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">path</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$(</span><span class="w"> </span>awk<span class="w"> </span><span class="s1">&#39;{ print substr($0, 31) }&#39;</span><span class="w"> </span><span class="o">&lt;&lt;&lt;</span><span class="w"> </span><span class="s2">&quot;</span><span class="nv">$content</span><span class="s2">&quot;</span><span class="w"> </span><span class="k">)</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="nv">content</span><span class="o">=</span><span class="s2">&quot;</span><span class="k">$(</span><span class="w"> </span>cat<span class="w"> </span><span class="s2">&quot;</span><span class="nv">$path</span><span class="s2">&quot;</span><span class="w"> </span><span class="k">)</span><span class="s2">&quot;</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">&quot;--data-urlencode&quot;</span><span class="w"> </span><span class="s2">&quot;user_id=</span><span class="si">${</span><span class="nv">SCN_UID</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">)</span>