From aacdc76af2de63ce05af461cd94f71c5c5e98098 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 14 May 2019 17:56:23 +0200 Subject: [PATCH] SendJson bash only, no more python! --- bashbot.sh | 28 ++++++++-------------------- commands.sh | 5 ++++- dev/make-standalone.sh | 4 ++-- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 2b6639d..71462a0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-dev2-12-gdf03727 +#### $$VERSION$$ v0.80-dev2-13-gbce7f1a # # Exit Codes: # - 0 sucess (hopefully) @@ -205,30 +205,18 @@ getBotName() { JsonGetString '"result","username"' <<< "$res" } -# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback -if [ "${BASHBOT_DECODE}" != "" ] && _exists python ; then - JsonDecode() { - printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' - } -else - # pure bash implementaion, done by KayM (@gnadelwartz) - # see https://stackoverflow.com/a/55666449/9381171 - JsonDecode() { - local out="$1" - local remain="" - local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)' - local W1 W2 U +# pure bash implementaion, done by KayM (@gnadelwartz) +# see https://stackoverflow.com/a/55666449/9381171 +JsonDecode() { + local out="$1" remain="" U="" + local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)' while [[ "${out}" =~ $regexp ]] ; do - # match 2 \udxxx hex values, calculate new U, then split and replace - W1=$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 )) - W2=$(( 0xd${BASH_REMATCH[3]} & 0x3ff )) - U=$(( ( W1 | W2 ) + 0x10000 )) + U=$(( ( (0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ) | ( 0xd${BASH_REMATCH[3]} & 0x3ff ) + 0x10000 )) remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}" out="${BASH_REMATCH[1]}" done echo -e "${out}${remain}" - } -fi +} JsonGetString() { sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p' diff --git a/commands.sh b/commands.sh index 79469d2..ac18a16 100644 --- a/commands.sh +++ b/commands.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-dev2-11-gb55c171 +#### $$VERSION$$ v0.80-dev2-13-gbce7f1a # # adjust your language setting here, e.g.when run from other user or cron. @@ -35,6 +35,9 @@ bashbot_help='*Available commands*: Written by Drew (@topkecleon), Daniil Gentili (@danogentili) and KayM(@gnadelwartz). Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ' +# we don't know whom we are ... +copname="" + # load modues on startup and always on on debug if [ "${1}" = "source" ] || [[ "${1}" = *"debug"* ]] ; then diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 2438159..022d221 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -5,7 +5,7 @@ # If you your bot is finished you can use make-standalone.sh to create the # the old all-in-one bashbot: bashbot.sh and commands.sh only! # -#### $$VERSION$$ v0.80-dev2-3-ge492da0 +#### $$VERSION$$ v0.80-dev2-13-gbce7f1a # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -72,7 +72,7 @@ echo " ... create unified bashbot.sh" cat modules/*.sh | sed -e 's/^#\!\/bin\/bash.*//' # last tail of commands.sh - printf '\n##############################\n# bashbot functions starts here ...\n\n' + printf '\n##############################\n# bashbot internal functions starts here ...\n\n' sed -n '/\/commands.sh"/,$ p' bashbot.sh } >>$$bashbot.sh