From 4b36432d0982dace46270b8c2da8e16ad061741d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 17:48:18 +0200 Subject: [PATCH 01/53] Start 0.96-dev, use MarkdownV2 --- modules/sendMessage.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 654d039..aa0c9d1 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.941-0-ga055b77 +#### $$VERSION$$ v0.96-dev-0-ga72d56a # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" @@ -35,7 +35,7 @@ send_normal_message() { send_markdown_message() { local text; text="$(JsonEscape "${2}")" until [ -z "${text}" ]; do - sendJson "${1}" '"text":"'"${text:0:4096}"'","parse_mode":"markdown"' "${MSG_URL}" + sendJson "${1}" '"text":"'"${text:0:4096}"'","parse_mode":"markdownv2"' "${MSG_URL}" text="${text:4096}" done } From b6f36c6e7afe852a16641474bf6120ed7901c489 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 18:21:56 +0200 Subject: [PATCH 02/53] add jssh_countKeyDB function --- modules/jsonDB.sh | 36 +++++++++++++++++++++++++++++++++++- modules/sendMessage.sh | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 4cdaf64..b29442b 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-1-g4b36432 # # source from commands.sh to use jsonDB functions # @@ -119,6 +119,24 @@ if _exists flock; then } + # add a value to key, used for conters + # $1 key name, can onyl contain -a-zA-Z0-9,._ + # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + # $3 optional count, value added to count3r, add 1 if empty + jssh_countKeyDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + declare -A oldARR + # start atomic delete here, exclusive max wait 10s + { flock -e -w 10 200 + Json2Array "oldARR" <"${DB}" + (( oldARR["$1"]+=COUNT )); + # it's append, but last one counts, its a simple DB ... + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + } 200>"${DB}${BASHBOT_LOCKNAME}" + } + + else ######### # we have no flock, use "old" not atomic functions @@ -142,6 +160,10 @@ else jssh_deleteKeyDB() { jssh_deleteKeyDB_async "$@" } + + jssh_countKeyDB() { + jssh_countKeyDB "$@" + } fi ############## @@ -244,3 +266,15 @@ jssh_deleteKeyDB_async() { jssh_writeDB_async "oldARR" "$2" } +jssh_countKeyDB_async() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + declare -A oldARR + # start atomic delete here, exclusive max wait 10s + Json2Array "oldARR" <"${DB}" + (( oldARR["$1"]+=COUNT )); + # it's append, but last one counts, its a simple DB ... + #printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + Array2Json "oldARR" >"${DB}" +} + diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index aa0c9d1..9f9639c 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.96-dev-0-ga72d56a +#### $$VERSION$$ v0.96-dev-1-g4b36432 # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" From a4636668a7d50247b2a3feb93321930c2e5d4f2a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 19:26:10 +0200 Subject: [PATCH 03/53] optimized version of countKeyDB --- bashbot.sh | 4 +--- modules/jsonDB.sh | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f7b5d04..51b00f9 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.94-7-g3d92bf3 +#### $$VERSION$$ v0.96-dev-2-gb6f36c6 # # Exit Codes: # - 0 sucess (hopefully) @@ -168,8 +168,6 @@ if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then echo -e "${ORANGE}Posilbe problem in the charatcers part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" fi -exit - ################## # here we start with the real stuff URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index b29442b..33b72d3 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev-1-g4b36432 +#### $$VERSION$$ v0.96-dev-2-gb6f36c6 # # source from commands.sh to use jsonDB functions # @@ -123,16 +123,22 @@ if _exists flock; then # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $3 optional count, value added to count3r, add 1 if empty + # side effect: if $3 is not given, we add to end of file to be as fast as possible jssh_countKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR # start atomic delete here, exclusive max wait 10s { flock -e -w 10 200 Json2Array "oldARR" <"${DB}" - (( oldARR["$1"]+=COUNT )); - # it's append, but last one counts, its a simple DB ... - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + if [ "$3" != "" ]; then + (( oldARR["$1"]+="$3" )); + Array2Json "oldARR" >"${DB}" + else + # it's append, but last one counts, its a simple DB ... + (( oldARR["$1"]++ )); + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + fi } 200>"${DB}${BASHBOT_LOCKNAME}" } @@ -269,12 +275,11 @@ jssh_deleteKeyDB_async() { jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + [ "$3" != "" ] && COUNT="$3" declare -A oldARR # start atomic delete here, exclusive max wait 10s Json2Array "oldARR" <"${DB}" (( oldARR["$1"]+=COUNT )); - # it's append, but last one counts, its a simple DB ... - #printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" Array2Json "oldARR" >"${DB}" } From ab863af08b97326613afb7278ed1e4433a3fd8dd Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 19:41:54 +0200 Subject: [PATCH 04/53] add doc for jssh_countKeyDB --- doc/6_reference.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 9fbacf2..dffb8c6 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -685,6 +685,22 @@ Deleted a key=value pair froma jsshDB file, key name is only allowed to contain jssh_deleteKeyDB "delkey"" "${DATADIR:-.}/myvalues" ``` +##### jssh_countKeyDB +Increase a key=value pair from a jsshDB file by 1, key name is only allowed to contain '-a-zA-Z0-9,._' +If value is given key is increased by value. + +Ssde effect: if value is given key is updated "in place" (slower) and file is cleand up, if no value is given fast path is used +and new count is added at the end of file. + +*usage:* jssh_countKeyDB "key" "filename" ["value"] + +*usage:* jssh_countKeyDB "key" "filename" ["value"] + +*example:* +```bash +jssh_countKeyDB "usercount"" "${DATADIR:-.}/myvalues" +``` + https://linuxhint.com/associative_array_bash/ https://linuxconfig.org/how-to-use-arrays-in-bash-script @@ -973,5 +989,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-3-ga463666 From 037b1ea326911111a570ab6de037e679cfebb52a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 19:48:38 +0200 Subject: [PATCH 05/53] fix doc typos --- doc/6_reference.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index dffb8c6..62a6971 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -689,12 +689,12 @@ jssh_deleteKeyDB "delkey"" "${DATADIR:-.}/myvalues" Increase a key=value pair from a jsshDB file by 1, key name is only allowed to contain '-a-zA-Z0-9,._' If value is given key is increased by value. -Ssde effect: if value is given key is updated "in place" (slower) and file is cleand up, if no value is given fast path is used -and new count is added at the end of file. +Side effect: if value is given key is updated "in place" (slower) and file is cleand up, if no value is given fast path is used +and new count is added to the end of file. *usage:* jssh_countKeyDB "key" "filename" ["value"] -*usage:* jssh_countKeyDB "key" "filename" ["value"] +*usage:* jssh_countKeyDB_async "key" "filename" ["value"] *example:* ```bash @@ -989,5 +989,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.96-dev-3-ga463666 +#### $$VERSION$$ v0.96-dev-4-gab863af From 7e83e5dd1c65b02c529aea58ab57481edecd2d81 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 20 May 2020 15:18:23 +0200 Subject: [PATCH 06/53] markdownv2 is not ccompatible, jssh_getKey --- commands.sh | 18 ++++++++---------- modules/jsonDB.sh | 35 ++++++++++++++++++++++++++++++----- modules/sendMessage.sh | 12 +++++++++++- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/commands.sh b/commands.sh index 7bd5be1..25b2c01 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-5-g037b1ea # # adjust your language setting here, e.g.when run from other user or cron. @@ -51,14 +51,12 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ' # load modues on startup and always on on debug -if [ -n "${1}" ]; then - # load all readable modules - for modules in "${MODULEDIR:-.}"/*.sh ; do - if [[ "${1}" == *"debug"* ]] || ! _is_function "$(basename "${modules}")"; then +if [ "${1}" = "startbot" ] || [[ "${1}" = *"debug"* ]] ; then + # load all readable modules + for modules in "${MODULEDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh [ -r "${modules}" ] && source "${modules}" "${1}" - fi - done + done fi # @@ -66,7 +64,7 @@ fi # copy "mycommands.sh.dist" to "mycommnds.sh" and change the values there # defaults to no inline and nonsense home dir export INLINE="0" -export FILE_REGEX="${BASHBOT_ETC}/.*" +export FILE_REGEX='/home/user/allowed/.*' # load mycommands @@ -74,10 +72,10 @@ export FILE_REGEX="${BASHBOT_ETC}/.*" [ -r "${BASHBOT_ETC:-.}/mycommands.sh" ] && source "${BASHBOT_ETC:-.}/mycommands.sh" "${1}" -if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then +if [ "${1}" = "" ] || [[ "${1}" == *"debug"* ]];then # detect inline commands.... # no default commands, all processing is done in myinlines() - if [ "$INLINE" != "0" ] && [ -n "${iQUERY[ID]}" ]; then + if [ "$INLINE" != "0" ] && [ "${iQUERY[ID]}" != "" ]; then # forward iinline query to optional dispatcher _exec_if_function myinlines diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 33b72d3..3799542 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev-2-gb6f36c6 +#### $$VERSION$$ v0.96-dev-5-g037b1ea # # source from commands.sh to use jsonDB functions # @@ -36,7 +36,7 @@ if _exists flock; then local DB; DB="$(jssh_checkDB "$2")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 - # shared lock, many processes can read, maximum wait 1s + # shared lock, many processes can read, max wait 1s { flock -s -w 1 200; Json2Array "$1" <"${DB}"; } 200>"${DB}${BASHBOT_LOCKNAME}" } @@ -95,7 +95,7 @@ if _exists flock; then local DB; DB="$(jssh_checkDB "$3")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 - # start atomic update here, exclusive max wait 2si, it's append, not overwrite + # start atomic update here, exclusive max wait 2, it's append, not overwrite { flock -e -w 2 200 # it's append, but last one counts, its a simple DB ... printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" @@ -118,6 +118,20 @@ if _exists flock; then } 200>"${DB}${BASHBOT_LOCKNAME}" } + # delete key/value from jsshDB + # $1 key name, can onyl contain -a-zA-Z0-9,._ + # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + jssh_getKeyDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + local DB; DB="$(jssh_checkDB "$2")" + declare -A oldARR + # start atomic delete here, exclusive max wait 1s + { flock -e -w 1 200 + Json2Array "oldARR" <"${DB}" + } 200>"${DB}${BASHBOT_LOCKNAME}" + echo "${oldARR["$1"]}" + } + # add a value to key, used for conters # $1 key name, can onyl contain -a-zA-Z0-9,._ @@ -128,8 +142,8 @@ if _exists flock; then [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR - # start atomic delete here, exclusive max wait 10s - { flock -e -w 10 200 + # start atomic delete here, exclusive max wait 5 + { flock -e -w 5 200 Json2Array "oldARR" <"${DB}" if [ "$3" != "" ]; then (( oldARR["$1"]+="$3" )); @@ -167,6 +181,9 @@ else jssh_deleteKeyDB_async "$@" } + jssh_getKeyDB() { + jssh_getKeyDB_async "$@" + } jssh_countKeyDB() { jssh_countKeyDB "$@" } @@ -272,6 +289,14 @@ jssh_deleteKeyDB_async() { jssh_writeDB_async "oldARR" "$2" } +jssh_getKeyDB_async() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + local DB; DB="$(jssh_checkDB "$2")" + declare -A oldARR + Json2Array "oldARR" <"${DB}" + echo "${oldARR["$1"]}" +} + jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB COUNT="1"; DB="$(jssh_checkDB "$2")" diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 9f9639c..fb78f76 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.96-dev-1-g4b36432 +#### $$VERSION$$ v0.96-dev-5-g037b1ea # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" @@ -34,6 +34,16 @@ send_normal_message() { send_markdown_message() { local text; text="$(JsonEscape "${2}")" + until [ -z "${text}" ]; do + sendJson "${1}" '"text":"'"${text:0:4096}"'","parse_mode":"markdown"' "${MSG_URL}" + text="${text:4096}" + done +} + +send_markdownv2_message() { + local text; text="$(JsonEscape "${2}")" + # markdown v2 needs additional double escaping! + text="$(sed -E -e 's|([#{}()!.-])|\\\1|g' <<< "$text")" until [ -z "${text}" ]; do sendJson "${1}" '"text":"'"${text:0:4096}"'","parse_mode":"markdownv2"' "${MSG_URL}" text="${text:4096}" From 01539286d162e6e309e3dfca638f37b59c3ff13a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 20 May 2020 16:35:22 +0200 Subject: [PATCH 07/53] revert old commands.sh --- README.html | 2 +- README.md | 2 +- README.txt | 2 +- addons/antiFlood.sh | 2 +- addons/example.sh | 2 +- bashbot.rc | 2 +- bashbot.sh | 2 +- commands.sh | 18 ++++++++++-------- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- doc/7_develop.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- examples/background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bash2env.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 2 +- modules/background.sh | 2 +- modules/chatMember.sh | 2 +- modules/jsonDB.sh | 2 +- modules/sendMessage.sh | 2 +- mycommands.sh | 2 +- mycommands.sh.clean | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 57 files changed, 66 insertions(+), 64 deletions(-) diff --git a/README.html b/README.html index a0e272d..d3bd543 100644 --- a/README.html +++ b/README.html @@ -228,6 +228,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ V0.94-9-g46af634

+

$$VERSION$$ v0.96-dev-6-g7e83e5d

diff --git a/README.md b/README.md index 682ab9e..dee12a4 100644 --- a/README.md +++ b/README.md @@ -192,4 +192,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ V0.94-9-g46af634 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/README.txt b/README.txt index 38f057c..e1a7652 100644 --- a/README.txt +++ b/README.txt @@ -270,4 +270,4 @@ tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ V0.94-9-g46af634 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index 3ffc72e..dd2c9e1 100644 --- a/addons/antiFlood.sh +++ b/addons/antiFlood.sh @@ -4,7 +4,7 @@ # this addon counts how many files, e.g. stickers, are sent to # a chat and takes actions if threshold is reached # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # used events: # diff --git a/addons/example.sh b/addons/example.sh index e719e92..3bb2a77 100644 --- a/addons/example.sh +++ b/addons/example.sh @@ -4,7 +4,7 @@ # Addons can register to bashbot events at statup # by providing their name and a callback per event # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # If an event occours each registered event function is called. # diff --git a/bashbot.rc b/bashbot.rc index 556e437..1a563c4 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 51b00f9..8edad02 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev-2-gb6f36c6 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # Exit Codes: # - 0 sucess (hopefully) diff --git a/commands.sh b/commands.sh index 25b2c01..4fe0fd7 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,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.96-dev-5-g037b1ea +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # adjust your language setting here, e.g.when run from other user or cron. @@ -51,12 +51,14 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ' # load modues on startup and always on on debug -if [ "${1}" = "startbot" ] || [[ "${1}" = *"debug"* ]] ; then - # load all readable modules - for modules in "${MODULEDIR:-.}"/*.sh ; do +if [ -n "${1}" ]; then + # load all readable modules + for modules in "${MODULEDIR:-.}"/*.sh ; do + if [[ "${1}" == *"debug"* ]] || ! _is_function "$(basename "${modules}")"; then # shellcheck source=./modules/aliases.sh [ -r "${modules}" ] && source "${modules}" "${1}" - done + fi + done fi # @@ -64,7 +66,7 @@ fi # copy "mycommands.sh.dist" to "mycommnds.sh" and change the values there # defaults to no inline and nonsense home dir export INLINE="0" -export FILE_REGEX='/home/user/allowed/.*' +export FILE_REGEX="${BASHBOT_ETC}/.*" # load mycommands @@ -72,10 +74,10 @@ export FILE_REGEX='/home/user/allowed/.*' [ -r "${BASHBOT_ETC:-.}/mycommands.sh" ] && source "${BASHBOT_ETC:-.}/mycommands.sh" "${1}" -if [ "${1}" = "" ] || [[ "${1}" == *"debug"* ]];then +if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then # detect inline commands.... # no default commands, all processing is done in myinlines() - if [ "$INLINE" != "0" ] && [ "${iQUERY[ID]}" != "" ]; then + if [ "$INLINE" != "0" ] && [ -n "${iQUERY[ID]}" ]; then # forward iinline query to optional dispatcher _exec_if_function myinlines diff --git a/dev/all-tests.sh b/dev/all-tests.sh index b4b8eed..73953de 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/git-add.sh b/dev/git-add.sh index 02ffd22..ce53acf 100755 --- a/dev/git-add.sh +++ b/dev/git-add.sh @@ -3,7 +3,7 @@ # # works together with git pre-push.sh and ADD all changed files since last push -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 1bd3686..ada4a1d 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index afb75f6..5af014b 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index 231e0d9..26594e5 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 8d0bc0b..c6df48c 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 2152929..037bea5 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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index daf2243..8d2d324 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,4 +1,4 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d bashbot.rc mycommands.sh.clean diff --git a/dev/version.sh b/dev/version.sh index 54a5284..a3b5608 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/0_install.md b/doc/0_install.md index a16c4b0..4c26dfe 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -101,5 +101,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ V0.94-8-g876361f +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 180c2a1..33675cb 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -65,5 +65,5 @@ group. This step is up to you actually. #### [Prev Installation](0_install.md) #### [Next Getting started](2_usage.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/2_usage.md b/doc/2_usage.md index e2d6747..529dd0d 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -241,5 +241,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ V0.94-2-gced78d3 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/3_advanced.md b/doc/3_advanced.md index aebd317..f792644 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -180,5 +180,5 @@ See also [answer_inline_multi, answer_inline_compose](6_reference.md#answer_inli #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/4_expert.md b/doc/4_expert.md index a0a0c04..b852883 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -348,5 +348,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/5_practice.md b/doc/5_practice.md index dbb4ee5..77f279c 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -152,5 +152,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/6_reference.md b/doc/6_reference.md index 62a6971..a2263de 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -989,5 +989,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.96-dev-4-gab863af +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/doc/7_develop.md b/doc/7_develop.md index 312de72..2de3f93 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -328,5 +328,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/examples/README.md b/examples/README.md index 363162c..f8e058d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -55,6 +55,6 @@ convert existing bots. **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index 7eb1a7e..7ba1062 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -4,7 +4,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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 3f2a157..347af7f 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 391b02a..c0935b3 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index 9e9a3cf..eb552f3 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -4,7 +4,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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bash2env.sh b/examples/bash2env.sh index 15a81cf..c0dfea4 100755 --- a/examples/bash2env.sh +++ b/examples/bash2env.sh @@ -6,7 +6,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.94-7-g3d92bf3 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot-multi.sh b/examples/bashbot-multi.sh index 6a09f59..ebaa9d6 100755 --- a/examples/bashbot-multi.sh +++ b/examples/bashbot-multi.sh @@ -2,7 +2,7 @@ # file. multibot.sh # description: run multiple telegram bots from one installation # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then echo "Usage: $0 botname command" diff --git a/examples/bashbot.cron b/examples/bashbot.cron index d72147c..1302745 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index ea08c46..4701cbc 100755 --- a/examples/calc.sh +++ b/examples/calc.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index 1ce76ca..e50786c 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -4,7 +4,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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index c220667..7d5988c 100755 --- a/examples/question.sh +++ b/examples/question.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl index 8bfeaed..6a98dd8 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,7 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # Format: # user:ressource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 4fe1fe3..70d37a0 100644 --- a/examples/send-system-status/mycommands.sh +++ b/examples/send-system-status/mycommands.sh @@ -5,7 +5,7 @@ # to show how you can customize bashbot by only editing mycommands.sh # NOTE: this is not tested, simply copied from original source and reworked! # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/modules/aliases.sh b/modules/aliases.sh index 6404491..2005499 100644 --- a/modules/aliases.sh +++ b/modules/aliases.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # source from commands.sh to use the aliases diff --git a/modules/answerInline.sh b/modules/answerInline.sh index 7757d97..5d9f02f 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # source from commands.sh to use the inline functions diff --git a/modules/background.sh b/modules/background.sh index 12e9d4d..7c91813 100644 --- a/modules/background.sh +++ b/modules/background.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # source from commands.sh if you want ro use interactive or background jobs diff --git a/modules/chatMember.sh b/modules/chatMember.sh index a308aab..b572ee8 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 3799542..d295f73 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev-5-g037b1ea +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # source from commands.sh to use jsonDB functions # diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index fb78f76..2984c4a 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.96-dev-5-g037b1ea +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/mycommands.sh b/mycommands.sh index 94992a7..c37a30e 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -8,7 +8,7 @@ # #### if you start to develop your own bot, use the clean version of this file: # #### mycommands.clean # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # # uncomment the following lines to overwrite info and help messages diff --git a/mycommands.sh.clean b/mycommands.sh.clean index 66b031f..1354dbf 100644 --- a/mycommands.sh.clean +++ b/mycommands.sh.clean @@ -4,7 +4,7 @@ # files: mycommands.sh.clean # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # ########## diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index cc32650..898d13a 100755 --- a/test/ADD-test-new.sh +++ b/test/ADD-test-new.sh @@ -2,7 +2,7 @@ # # ADD a new test skeleton to test dir, but does not activate test # -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 038e856..2e2a45a 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index deab289..cc0c146 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d ../dev/hooks/pre-commit.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh index 5e9ac32..2df51f4 100644 --- a/test/b-example-test.sh +++ b/test/b-example-test.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # file: b-example-test.sh -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index de076a5..8f11232 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-JSON.sh-test.sh b/test/d-JSON.sh-test.sh index a1c99ec..cae5a1f 100755 --- a/test/d-JSON.sh-test.sh +++ b/test/d-JSON.sh-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_inline-test.sh b/test/d-process_inline-test.sh index 7faf598..496868a 100755 --- a/test/d-process_inline-test.sh +++ b/test/d-process_inline-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 1e9c9c9..773add3 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 74895a3..d842917 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index af6da46..4004883 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/e-env-test.sh b/test/e-env-test.sh index f2041d5..982047b 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ V0.94-0-gbdb50c8 +#### $$VERSION$$ v0.96-dev-6-g7e83e5d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh From e63590b9b4c5966b0a2c2e26e7f413ad24edbac9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 20 May 2020 16:38:56 +0200 Subject: [PATCH 08/53] extended JsonEscape for V2 --- README.html | 2 +- README.md | 2 +- README.txt | 2 +- addons/antiFlood.sh | 2 +- addons/example.sh | 2 +- bashbot.rc | 2 +- bashbot.sh | 5 +++-- commands.sh | 2 +- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- doc/7_develop.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- examples/background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bash2env.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 2 +- modules/background.sh | 2 +- modules/chatMember.sh | 2 +- modules/jsonDB.sh | 2 +- modules/sendMessage.sh | 2 +- mycommands.sh | 2 +- mycommands.sh.clean | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 57 files changed, 59 insertions(+), 58 deletions(-) diff --git a/README.html b/README.html index d3bd543..857543f 100644 --- a/README.html +++ b/README.html @@ -228,6 +228,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ v0.96-dev-6-g7e83e5d

+

$$VERSION$$ v0.96-dev-7-g0153928

diff --git a/README.md b/README.md index dee12a4..fd1c59e 100644 --- a/README.md +++ b/README.md @@ -192,4 +192,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/README.txt b/README.txt index e1a7652..ec7f8c1 100644 --- a/README.txt +++ b/README.txt @@ -270,4 +270,4 @@ tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index dd2c9e1..e4a8a3f 100644 --- a/addons/antiFlood.sh +++ b/addons/antiFlood.sh @@ -4,7 +4,7 @@ # this addon counts how many files, e.g. stickers, are sent to # a chat and takes actions if threshold is reached # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # used events: # diff --git a/addons/example.sh b/addons/example.sh index 3bb2a77..bb08160 100644 --- a/addons/example.sh +++ b/addons/example.sh @@ -4,7 +4,7 @@ # Addons can register to bashbot events at statup # by providing their name and a callback per event # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # If an event occours each registered event function is called. # diff --git a/bashbot.rc b/bashbot.rc index 1a563c4..565ad94 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 8edad02..b0bfd6f 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # Exit Codes: # - 0 sucess (hopefully) @@ -168,6 +168,7 @@ if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then echo -e "${ORANGE}Posilbe problem in the charatcers part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" fi + ################## # here we start with the real stuff URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" @@ -317,7 +318,7 @@ fi # $1 string # output escaped string JsonEscape() { - sed 's/\([-"`´,§$%&ß/(){}#@?*]\)/\\\1/g' <<< "$1" + sed 's/\([-"`´,§$%&/(){}#@!?*.]\)/\\\1/g' <<< "$1" } # convert common telegram entities to JSON diff --git a/commands.sh b/commands.sh index 4fe0fd7..9b2e61c 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # adjust your language setting here, e.g.when run from other user or cron. diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 73953de..84488b0 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/git-add.sh b/dev/git-add.sh index ce53acf..b3a0a59 100755 --- a/dev/git-add.sh +++ b/dev/git-add.sh @@ -3,7 +3,7 @@ # # works together with git pre-push.sh and ADD all changed files since last push -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index ada4a1d..3b23e11 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index 5af014b..c3b3489 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index 26594e5..6c62a23 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index c6df48c..5f2743a 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 037bea5..c5584f1 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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index 8d2d324..167d5c2 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,4 +1,4 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 bashbot.rc mycommands.sh.clean diff --git a/dev/version.sh b/dev/version.sh index a3b5608..892b220 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/0_install.md b/doc/0_install.md index 4c26dfe..d391955 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -101,5 +101,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 33675cb..26e6a30 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -65,5 +65,5 @@ group. This step is up to you actually. #### [Prev Installation](0_install.md) #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/2_usage.md b/doc/2_usage.md index 529dd0d..124078f 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -241,5 +241,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index f792644..5de1535 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -180,5 +180,5 @@ See also [answer_inline_multi, answer_inline_compose](6_reference.md#answer_inli #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/4_expert.md b/doc/4_expert.md index b852883..f40e45d 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -348,5 +348,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/5_practice.md b/doc/5_practice.md index 77f279c..468f886 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -152,5 +152,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/6_reference.md b/doc/6_reference.md index a2263de..15bc332 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -989,5 +989,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/doc/7_develop.md b/doc/7_develop.md index 2de3f93..1be455b 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -328,5 +328,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/examples/README.md b/examples/README.md index f8e058d..d9b2870 100644 --- a/examples/README.md +++ b/examples/README.md @@ -55,6 +55,6 @@ convert existing bots. **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index 7ba1062..a5dec8e 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -4,7 +4,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 347af7f..5964805 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index c0935b3..34bfb50 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index eb552f3..9da4cfa 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -4,7 +4,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bash2env.sh b/examples/bash2env.sh index c0dfea4..d770aed 100755 --- a/examples/bash2env.sh +++ b/examples/bash2env.sh @@ -6,7 +6,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot-multi.sh b/examples/bashbot-multi.sh index ebaa9d6..43f3a2a 100755 --- a/examples/bashbot-multi.sh +++ b/examples/bashbot-multi.sh @@ -2,7 +2,7 @@ # file. multibot.sh # description: run multiple telegram bots from one installation # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then echo "Usage: $0 botname command" diff --git a/examples/bashbot.cron b/examples/bashbot.cron index 1302745..6a28210 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 4701cbc..d2bd29b 100755 --- a/examples/calc.sh +++ b/examples/calc.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index e50786c..1fc2d54 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -4,7 +4,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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index 7d5988c..8e850ad 100755 --- a/examples/question.sh +++ b/examples/question.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl index 6a98dd8..392b5d5 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,7 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # Format: # user:ressource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 70d37a0..6616f8c 100644 --- a/examples/send-system-status/mycommands.sh +++ b/examples/send-system-status/mycommands.sh @@ -5,7 +5,7 @@ # to show how you can customize bashbot by only editing mycommands.sh # NOTE: this is not tested, simply copied from original source and reworked! # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/modules/aliases.sh b/modules/aliases.sh index 2005499..6c87266 100644 --- a/modules/aliases.sh +++ b/modules/aliases.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # source from commands.sh to use the aliases diff --git a/modules/answerInline.sh b/modules/answerInline.sh index 5d9f02f..976dc08 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # source from commands.sh to use the inline functions diff --git a/modules/background.sh b/modules/background.sh index 7c91813..90ca9e1 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # source from commands.sh if you want ro use interactive or background jobs diff --git a/modules/chatMember.sh b/modules/chatMember.sh index b572ee8..12e3cd3 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index d295f73..c098fbb 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # source from commands.sh to use jsonDB functions # diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 2984c4a..389c918 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/mycommands.sh b/mycommands.sh index c37a30e..c8e6b67 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -8,7 +8,7 @@ # #### if you start to develop your own bot, use the clean version of this file: # #### mycommands.clean # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # # uncomment the following lines to overwrite info and help messages diff --git a/mycommands.sh.clean b/mycommands.sh.clean index 1354dbf..7c8c1c4 100644 --- a/mycommands.sh.clean +++ b/mycommands.sh.clean @@ -4,7 +4,7 @@ # files: mycommands.sh.clean # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # ########## diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index 898d13a..0e6b0dd 100755 --- a/test/ADD-test-new.sh +++ b/test/ADD-test-new.sh @@ -2,7 +2,7 @@ # # ADD a new test skeleton to test dir, but does not activate test # -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 2e2a45a..e62023d 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index cc0c146..4742d8d 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 ../dev/hooks/pre-commit.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh index 2df51f4..ed39d3f 100644 --- a/test/b-example-test.sh +++ b/test/b-example-test.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # file: b-example-test.sh -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 8f11232..a0f79bc 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-JSON.sh-test.sh b/test/d-JSON.sh-test.sh index cae5a1f..638b788 100755 --- a/test/d-JSON.sh-test.sh +++ b/test/d-JSON.sh-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_inline-test.sh b/test/d-process_inline-test.sh index 496868a..71ebc24 100755 --- a/test/d-process_inline-test.sh +++ b/test/d-process_inline-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 773add3..2c58c4c 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index d842917..9d9d481 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index 4004883..2558a07 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/e-env-test.sh b/test/e-env-test.sh index 982047b..0eec21a 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-6-g7e83e5d +#### $$VERSION$$ v0.96-dev-7-g0153928 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh From 8f29907442ccf35553f42a4553e4cb8996e26cb3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 22 May 2020 08:29:49 +0200 Subject: [PATCH 09/53] fix shell expansion --- README.html | 4 ++-- README.md | 4 ++-- README.txt | 6 +++--- bashbot.sh | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.html b/README.html index 857543f..ac3842f 100644 --- a/README.html +++ b/README.html @@ -224,10 +224,10 @@ It features background tasks and interactive chats, and can serve as an interfac wget -t 1 -T 10 https://api.telegram.org/bot #Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out. -

This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a tor proxy on your server you may uncomment the BASHBOT_CURL_ARGS line in 'mycommands.sh'

+

This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a socks or tor proxy on your server look for the BASHBOT_CURL_ARGS lines in 'mycommands.sh' as example.

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ v0.96-dev-7-g0153928

+

$$VERSION$$ v0.96-dev-8-ge63590b

diff --git a/README.md b/README.md index fd1c59e..e7912c0 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ curl -m 10 https://api.telegram.org/bot wget -t 1 -T 10 https://api.telegram.org/bot #Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out. ``` -This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in 'mycommands.sh' +This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a socks or tor proxy on your server look for the ```BASHBOT_CURL_ARGS``` lines in 'mycommands.sh' as example. @Gnadelwartz @@ -192,4 +192,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev-8-ge63590b diff --git a/README.txt b/README.txt index ec7f8c1..07f8a71 100644 --- a/README.txt +++ b/README.txt @@ -259,8 +259,8 @@ failed: Connection timed out. This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a -tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in -'mycommands.sh' +socks or tor proxy on your server look for the ```BASHBOT_CURL_ARGS``` lines +in 'mycommands.sh' as example. @Gnadelwartz @@ -270,4 +270,4 @@ tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev-8-ge63590b diff --git a/bashbot.sh b/bashbot.sh index b0bfd6f..9a34b88 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev-8-ge63590b # # Exit Codes: # - 0 sucess (hopefully) @@ -688,6 +688,7 @@ start_bot() { fi while true; do UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)" + UPDATE="${UPDATE//$/\\$}" # Offset OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" From 53bab25459c87743d34ede865fff06928d460f01 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 22 May 2020 08:33:29 +0200 Subject: [PATCH 10/53] fix shell expansion --- bashbot.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bashbot.sh b/bashbot.sh index 3c470e0..670b1bc 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.94-7-g3d92bf3 +#### $$VERSION$$ V0.94-8-g3d92bf3 # # Exit Codes: # - 0 sucess (hopefully) @@ -688,6 +688,7 @@ start_bot() { fi while true; do UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)" + UPDATE="${UPDATE//$/\\$}" # Offset OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" From feddcabf9c905177b527435b4be9267c5afd22fe Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 22 May 2020 21:42:10 +0200 Subject: [PATCH 11/53] update doc because of RCE bug --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 682ab9e..f47847b 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,9 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version! +One of the most powerful features of unix shells like bash is variable and command substitution, this can lead to RCE and information disclosing bugs if you do not escape '$' porperly, see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) + A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected. @@ -140,6 +143,8 @@ To set access rights for your bashbot installation to a reasonable default run ` ### Is this Bot insecure? Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ... +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version! + ### Why Bash and not the much better xyz? Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side: From 67b9039d51c17f8d9b752af75187f56580127856 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 27 May 2020 09:06:22 +0200 Subject: [PATCH 12/53] fix not working jssh_updateDB --- README.html | 4 +++- README.md | 2 +- README.txt | 12 +++++++++++- modules/jsonDB.sh | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.html b/README.html index ac3842f..15590ae 100644 --- a/README.html +++ b/README.html @@ -178,6 +178,7 @@ It features background tasks and interactive chats, and can serve as an interfac

Running a Telegram Bot means it is connected to the public and you never know whats send to your Bot.

Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see Implications of wrong quoting

Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands.

+

Note: Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version! One of the most powerful features of unix shells like bash is variable and command substitution, this can lead to RCE and information disclosing bugs if you do not escape '$' porperly, see Issue #125

A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

Do not use #!/usr/bin/env bash

We stay with /bin/bash shebang, because it's more save from security perspective.

@@ -193,6 +194,7 @@ It features background tasks and interactive chats, and can serve as an interfac

FAQ

Is this Bot insecure?

Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ...

+

Note: Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version!

Why Bash and not the much better xyz?

Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side:

    @@ -228,6 +230,6 @@ It features background tasks and interactive chats, and can serve as an interfac

    @Gnadelwartz

    That's it!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v0.96-dev-8-ge63590b

    +

    $$VERSION$$ 0.96-dev2-0-gcbad540

    diff --git a/README.md b/README.md index 2f9fd2c..6df4f22 100644 --- a/README.md +++ b/README.md @@ -197,4 +197,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-8-ge63590b +#### $$VERSION$$ 0.96-dev2-0-gcbad540 diff --git a/README.txt b/README.txt index 07f8a71..02c6066 100644 --- a/README.txt +++ b/README.txt @@ -147,6 +147,13 @@ globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code +execution bug, pls update if you use an older version! +One of the most powerful features of unix shells like bash is variable and +command substitution, this can lead to RCE and information disclosing bugs if +you do not escape '$' porperly, see [Issue +#125](https://github.com/topkecleon/telegram-bot-bash/issues/125) + A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used @@ -200,6 +207,9 @@ Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ... +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code +execution bug, pls update if you use an older version! + ### Why Bash and not the much better xyz? Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side: @@ -270,4 +280,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-dev-8-ge63590b +#### $$VERSION$$ 0.96-dev2-0-gcbad540 diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index c098fbb..a856261 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-0-gcbad540 # # source from commands.sh to use jsonDB functions # @@ -63,7 +63,7 @@ if _exists flock; then declare -n ARRAY="$1" [ -z "${ARRAY[*]}" ] && return 1 - declare -A oldARR newARR + declare -A oldARR # start atomic update here, exclusive max wait 10s { flock -e -w 10 200 @@ -73,14 +73,14 @@ if _exists flock; then Array2Json "$1" >"${DB}" else # merge arrays - local o1 o2 n1 n2 - o1="$(declare -p oldARR)"; o2="${o1#*\(}" - n1="$(declare -p ARRAY)"; n2="${n1#*\(}" - unset IFS; set -f - #shellcheck disable=SC2034,SC2190,SC2206 - newARR=( ${o2:0:${#o2}-1} ${n2:0:${#n2}-1} ) - set +f - Array2Json "newARR" >"${DB}" + local key +set -x + for key in "${!ARRAY[@]}" + do + oldARR["${key}"]="${ARRAY["${key}"]}" + done + Array2Json "oldARR" >"${DB}" +set +x fi } 200>"${DB}${BASHBOT_LOCKNAME}" } From a7997f21b5b7a1b934231ff31f3e50a60511f097 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 27 May 2020 10:57:25 +0200 Subject: [PATCH 13/53] update jsshdb doc --- doc/6_reference.md | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 15bc332..421c53f 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -466,9 +466,41 @@ Since output generated by JSON.sh is so handy to use in bash, we use the format fucntions to read and write JSON.sh style data from and to files. The file extions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA.. -Note: File names containg '..' and absolute file names pointing outside BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions. +#### fast and slow database operations -Note2: Since version 0.94 jsshDB functions support file locking with flock. Write / Update operations use flock to wait until +jsshDB files are simple text files and you can add new Key/value pairs at the end of the file, this overwritesn +already existing key/value pairs with new values. + +Funtions simply add to the end without deleting an existing key/value pair are fast, but over time the +file grows with every write. I call this funtions "fast functions". + +"slow funtions" in contrast read the whole file, modify the key/value pairs and write the whole file back. +this needs more time and IO, but does clean up the file. All duplicate added key/value pairs are deleted +and only the last valid value is written to the file. + +fast functions: + +``` + jssh_insertDB , jssh_addKeyDB , jssh_countKeyDB +``` + +slow functions: + +``` + jssh_writeDB, jssh_updateDB , jssh_deleteKeyDBB +``` + + +#### File nameing and locking + +A jssh fileDB consists of maximum two files witch must reside inside BASHBOT_ETC or BASHBOT_DATA. + +- `filename.jssh` is a text file containing the data in the format as json.sh outputs. +- optional file `filename.jssh.flock` used for read/write locking with flock + +Path names names containing '..' and absolute path names pointing outside BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions. + +Since version 0.94 jsshDB functions support file locking with flock. Write / Update operations use flock to wait until previous operations are finished. see "man flock" for more information. Bashbot uses a maximum timeout of 10 seconds for flock. If you don't want atomic write / update operations use the *_async variant of jsshDB functions. If flock is not availible @@ -989,5 +1021,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-1-g67b9039 From 975f99a4b0556e388884533854cd75fb8e1a9c11 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 27 May 2020 22:28:09 +0200 Subject: [PATCH 14/53] fix jsshDB doc --- doc/6_reference.md | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 421c53f..892c691 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -462,21 +462,20 @@ Usually message is automatically forwarded in 'commands.sh', but you can forwar ---- ### jsshDB -Since output generated by JSON.sh is so handy to use in bash, we use the format for a simple keys/value storage and providing -fucntions to read and write JSON.sh style data from and to files. +Since output generated by JSON.sh is so handy to use in bash, we use the format for a simple keys/value file store. The file extions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA.. -#### fast and slow database operations +#### fast and slow operations -jsshDB files are simple text files and you can add new Key/value pairs at the end of the file, this overwritesn -already existing key/value pairs with new values. +jsshDB files are simple text files and if you append a new Key/value pairs to the end of the file it overwrites +an existing key/value pair. We use this behaivor for "fast" file operations. -Funtions simply add to the end without deleting an existing key/value pair are fast, but over time the -file grows with every write. I call this funtions "fast functions". +"slow funtions" simply add a new key/value pair without deleting an existing key/value pair, this is fast but over (long) +time the file grows infinitly. -"slow funtions" in contrast read the whole file, modify the key/value pairs and write the whole file back. -this needs more time and IO, but does clean up the file. All duplicate added key/value pairs are deleted -and only the last valid value is written to the file. +"slow funtions" in contrast modify the key/value pairs and write the whole file back, +this is slower but does clean up the file. All duplicate key/value pairs are deleted +and only the last one is written back to the file. fast functions: @@ -1021,5 +1020,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ 0.96-dev2-1-g67b9039 +#### $$VERSION$$ 0.96-dev2-2-ga7997f2 From 2a3dcaa471a492e12d0c79958b820bd29fb761db Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 27 May 2020 22:53:47 +0200 Subject: [PATCH 15/53] betterjsshDB doc --- doc/6_reference.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 892c691..3a4e567 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -470,11 +470,11 @@ The file extions is '.jssh' and for security reasons location of jssh files is r jsshDB files are simple text files and if you append a new Key/value pairs to the end of the file it overwrites an existing key/value pair. We use this behaivor for "fast" file operations. -"slow funtions" simply add a new key/value pair without deleting an existing key/value pair, this is fast but over (long) +"fast funtions" add a new key/value pair to the end of the file without deleting an existing one, this is fast but over (long) time the file grows infinitly. -"slow funtions" in contrast modify the key/value pairs and write the whole file back, -this is slower but does clean up the file. All duplicate key/value pairs are deleted +"slow funtions" in contrast modify the key/value pairs in place and write the whole file back, +this is slower but clean up the file. All previously added key/value pairs are replaced and only the last one is written back to the file. fast functions: @@ -490,22 +490,21 @@ slow functions: ``` -#### File nameing and locking +#### File naming and locking -A jssh fileDB consists of maximum two files witch must reside inside BASHBOT_ETC or BASHBOT_DATA. +A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA. -- `filename.jssh` is a text file containing the data in the format as json.sh outputs. -- optional file `filename.jssh.flock` used for read/write locking with flock +- `filename.jssh` is a text file containing the key/value data in json.sh format´s. +- `filename.jssh.flock` is used for read/write locking with flock -Path names names containing '..' and absolute path names pointing outside BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions. +Path names containing `..` or not in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. -Since version 0.94 jsshDB functions support file locking with flock. Write / Update operations use flock to wait until -previous operations are finished. see "man flock" for more information. Bashbot uses a maximum timeout of 10 seconds for flock. +Since version 0.94 jsshDB functions support file locking with flock. Write/Update operations are serialised with flock to wait until +previous operations are finished, see "man flock" for more information. To avoid deadlocks flock is used with a timeout of 10s for write and 5s for read operations. -If you don't want atomic write / update operations use the *_async variant of jsshDB functions. If flock is not availible -the *_async variant is automatically used. +In case flock is mot availibe or you don't want locking, jsshDB functions will be used without file locking by using *_async variants. -*Example:* for file name: +*Example:* for allowed file names: ```bash # bashbot is installed in /usr/local/telegram-bot-bash, BASHBOT_ETC is not set. "myfile" -> /usr/local/telegram-bot-bash/myfile.jssh @@ -1020,5 +1019,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ 0.96-dev2-2-ga7997f2 +#### $$VERSION$$ 0.96-dev2-3-g975f99a From 578056b57d167bc9d1849c036ace67e783e6b1e9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 29 May 2020 20:11:51 +0200 Subject: [PATCH 16/53] new feature block users, use jsshDB for count --- bashbot.sh | 101 ++++++++++++++++++++++++++++++++------------- doc/6_reference.md | 12 +++--- 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 9a34b88..df42a2c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev-8-ge63590b +#### $$VERSION$$ 0.96-dev2-4-g2a3dcaa # # Exit Codes: # - 0 sucess (hopefully) @@ -93,7 +93,7 @@ TOKENFILE="${BASHBOT_ETC:-.}/token" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" BOTACL="${BASHBOT_ETC:-.}/botacl" DATADIR="${BASHBOT_VAR:-.}/data-bot-bash" -# !!!!! DEPRECATED !!!!! +BLOCKEDFILE="${BASHBOT_VAR:-.}/blocked" COUNTFILE="${BASHBOT_VAR:-.}/count" # we assume everthing is already set up correctly if we have TOKEN @@ -143,15 +143,22 @@ if [ -z "${BOTTOKEN}" ]; then ls -ld "${DATADIR}" exit 2 fi - # setup count file !!!!! DEPRECATED !!!!! - if [ ! -f "${COUNTFILE}" ]; then - printf '\n' >"${COUNTFILE}" - elif [ ! -w "${COUNTFILE}" ]; then + # setup count file + if [ ! -f "${COUNTFILE}.jssh" ]; then + printf '["counted_user_id"]\t"num_messages_seen"\n' >"${COUNTFILE}.jssh" + elif [ ! -w "${COUNTFILE}.jssh" ]; then echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" - ls -l "${COUNTFILE}" + ls -l "${COUNTFILE}.jssh" exit 2 fi + # setup blocked file + if [ ! -f "${BLOCKEDFILE}.jssh" ]; then + printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh" + fi fi +# cleanup (remove double entries) countfile on startup +[ "${SOURCE}" != "yes" ] && _exec_if_function jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" + # do we have BSD sed if ! sed '1ia' /dev/null; then echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" @@ -390,8 +397,16 @@ process_updates() { process_client() { local num="$1" debug="$2" CMD=( ); iQUERY=( ) - iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" + iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" + CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" + USER[ID]="${UPD["result",${num},"message","from","id"]}" + + # check for uers / groups to ignore + if [ -n "${USER[ID]}" ]; then + [[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return + [ -r "${BLOCKEDFILE}" ] && _exec_if_function jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}" + fi if [ -z "${iQUERY[ID]}" ]; then process_message "${num}" "${debug}" else @@ -411,13 +426,11 @@ process_client() { fi # last count users - # !!!!! DEPRECATED !!!!! - tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}" + _exec_if_function jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}" } declare -Ax BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASHBOT_EVENT_SEND -declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT BASHBOT_EVENT_TIMER +declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT BASHBOT_EVENT_TIMER BASHBOT_BLOCKED start_timer(){ # send alarm every ~60 s @@ -551,8 +564,8 @@ process_message() { MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}" - # Chat - CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" + # Chat ID is now parsed when update isrecieved + #CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" CHAT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","last_name"]}")" CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")" CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")" @@ -561,8 +574,8 @@ process_message() { CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}" CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="${CHAT[ALL_ADMIN]}" # backward compatibility - # User - USER[ID]="${UPD["result",${num},"message","from","id"]}" + # user ID is now parsed when update isrecieved + #USER[ID]="${UPD["result",${num},"message","from","id"]}" USER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","first_name"]}")" USER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","last_name"]}")" USER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","from","username"]}")" @@ -687,9 +700,9 @@ start_bot() { trap "kill -9 $!; exit" EXIT INT HUP TERM QUIT fi while true; do - UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)" + # ignore timeout error message on waiting for updates + UPDATE="$(getJson "$UPD_URL$OFFSET" 2>/dev/null | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)" UPDATE="${UPDATE//$/\\$}" - # Offset OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" ((OFFSET++)) @@ -736,11 +749,11 @@ bot_init() { chown -R "$TOUSER" . ./* chmod 711 . chmod -R a-w ./* - chmod -R u+w "${COUNTFILE}" "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null - chmod -R o-r,o-w "${COUNTFILE}" "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R u+w "${COUNTFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh' -exec chmod u+w \{\} + - ls -la + #ls -la fi } @@ -806,16 +819,46 @@ if [ "${SOURCE}" != "yes" ]; then BOTPID="$(proclist "${SESSION}")" case "$1" in - "count") # !!!!! DEPRECATED !!!!! - echo "A total of $(wc -l <"${COUNTFILE}") users used me." + "stats"|'count') + declare -A STATS + if _is_function jssh_readDB ; then + jssh_readDB "STATS" "${COUNTFILE}" + for MSG in ${!STATS[*]} + do + [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue + (( USERS++ )) + done + for MSG in ${STATS[*]} + do + (( MESSAGES+=MSG )) + done + echo "A total of ${MESSAGES} messages from ${USERS} users are processed." + else + echo "Module jsshDB is not availible." + fi exit ;; - "broadcast") # !!!!! DEPRECATED !!!!! - NUMCOUNT="$(wc -l <"${COUNTFILE}")" - echo "Sending the broadcast $* to $NUMCOUNT users." - [ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5" + 'broadcast') + declare -A SENDALL shift - while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" + if _is_function jssh_readDB ; then + jssh_readDB "SENDALL" "${COUNTFILE}" + echo -e "Sending broadcast message to all users \c" + for MSG in ${!SENDALL[*]} + do + [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue + (( USERS++ )) + if [ -n "$*" ]; then + send_markdown_message "${MSG}" "$*" + echo -e ".\c" + sleep 0.1 + fi + done + echo -e "\nMessage \"$*\" sent to ${USERS} users." + else + echo "Module jsshDB is not availible." + fi + exit ;; "status") if [ -n "${BOTPID}" ]; then @@ -861,7 +904,7 @@ if [ "${SOURCE}" != "yes" ]; then ;; *) echo -e "${RED}${REALME}: BAD REQUEST${NC}" - echo -e "${RED}Available arguments: ${GREY}start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}" + echo -e "${RED}Available arguments: ${GREY}start, stop, kill, status, status, broadcast, help, suspendback, resumeback, killback${NC}" exit 4 ;; esac diff --git a/doc/6_reference.md b/doc/6_reference.md index 3a4e567..3f93e91 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -494,15 +494,15 @@ slow functions: A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA. -- `filename.jssh` is a text file containing the key/value data in json.sh format´s. +- `filename.jssh` is a text file containing the key/value data in json.sh format. - `filename.jssh.flock` is used for read/write locking with flock -Path names containing `..` or not in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. +Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. -Since version 0.94 jsshDB functions support file locking with flock. Write/Update operations are serialised with flock to wait until -previous operations are finished, see "man flock" for more information. To avoid deadlocks flock is used with a timeout of 10s for write and 5s for read operations. +Since version 0.94 jsshDB functions support file locking with flock. write/update operations are serialised with flock to wait until +previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations. -In case flock is mot availibe or you don't want locking, jsshDB functions will be used without file locking by using *_async variants. +In case flock is not availibe or you don't want locking, jsshDB functions will be used without file locking. *Example:* for allowed file names: ```bash @@ -1019,5 +1019,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ 0.96-dev2-3-g975f99a +#### $$VERSION$$ 0.96-dev2-4-g2a3dcaa From da98b09d6b9013f8de51249e8564c25cc5887b00 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 29 May 2020 20:31:53 +0200 Subject: [PATCH 17/53] autoconvert old count file --- commands.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.sh b/commands.sh index 9b2e61c..e043525 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,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.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-5-g578056b # # adjust your language setting here, e.g.when run from other user or cron. From 6d1e7cc0656886b0d02ac01e49e89f3c6a778fa4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 31 May 2020 11:04:38 +0200 Subject: [PATCH 18/53] jsonDB updates: insertKey, use builtin alias, fix updateDB_async, fix shellcheck hints --- dev/make-distribution.sh | 6 ++-- dev/make-standalone.sh | 4 +-- modules/jsonDB.sh | 78 ++++++++++++++++++---------------------- modules/sendMessage.sh | 5 +-- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 5f2743a..4bc4c55 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-6-gda98b09 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -21,9 +21,9 @@ DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean d # run tests first! -for test in "dev/all-tests.sh" +for test in dev/all-test*.sh do - [ ! -x ""${test} ] && continue + [ ! -x "${test}" ] && continue if ! "${test}" ; then echo "Test ${test} failed, can't create dist!" exit 1 diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index c5584f1..916c1fe 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.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-6-gda98b09 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -22,7 +22,7 @@ DISTFILES="bashbot.sh commands.sh mycommands.sh modules LICENSE README.txt tok # run tests first! -for test in "dev/all-tests.sh" +for test in dev/all-test*.sh do [ ! -x "${test}" ] && continue if ! "${test}" ; then diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index a856261..7abed5a 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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$$ 0.96-dev2-0-gcbad540 +#### $$VERSION$$ 0.96-dev2-6-gda98b09 # # source from commands.sh to use jsonDB functions # @@ -74,13 +74,11 @@ if _exists flock; then else # merge arrays local key -set -x for key in "${!ARRAY[@]}" do oldARR["${key}"]="${ARRAY["${key}"]}" done Array2Json "oldARR" >"${DB}" -set +x fi } 200>"${DB}${BASHBOT_LOCKNAME}" } @@ -89,7 +87,9 @@ set +x # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 key value # $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' - jssh_insertDB() { + alias jssh_insertDB=jssh_insertKeyDB # backward compatibility + # renamed to be more consistent + jssh_insertKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local key="$1" value="$2" local DB; DB="$(jssh_checkDB "$3")" @@ -126,7 +126,7 @@ set +x local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR # start atomic delete here, exclusive max wait 1s - { flock -e -w 1 200 + { flock -s -w 1 200 Json2Array "oldARR" <"${DB}" } 200>"${DB}${BASHBOT_LOCKNAME}" echo "${oldARR["$1"]}" @@ -136,7 +136,7 @@ set +x # add a value to key, used for conters # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' - # $3 optional count, value added to count3r, add 1 if empty + # $3 optional count, value added to counter, add 1 if empty # side effect: if $3 is not given, we add to end of file to be as fast as possible jssh_countKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 @@ -160,35 +160,28 @@ set +x else ######### # we have no flock, use "old" not atomic functions - jssh_readDB() { - jssh_readDB_async "$@" - } - - jssh_writeDB() { - jssh_writeDB_async "$@" - } - - jssh_updateDB() { - jssh_updateDB_async "$@" - } - - jssh_insertDB() { - jssh_insertDB_async "$@" - - } - - jssh_deleteKeyDB() { - jssh_deleteKeyDB_async "$@" - } - - jssh_getKeyDB() { - jssh_getKeyDB_async "$@" - } - jssh_countKeyDB() { - jssh_countKeyDB "$@" - } + alias jssh_readDB=ssh_readDB_async + alias jssh_writeDB=jssh_writeDB_async + alias jssh_updateDB=jssh_updateDB_async + alias jssh_insertDB=jssh_insertDB_async + alias ssh_deleteKeyDB=jssh_deleteKeyDB_async + alias jssh_getKeyDB=jssh_getKeyDB_async + alias jssh_countKeyDB=jssh_countKeyDB_async fi +# updatie key/value in place to jsshDB +# $1 key name, can onyl contain -a-zA-Z0-9,._ +# $2 key value +# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +#no own locking, so async is the same as updatekeyDB +alias jssh_updateKeyDB_async=jssh_updateKeyDB +jssh_updateKeyDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + declare -A oldARR + oldARR["$1"]="$2" + jssh_updateDB "oldARR" "${3}" || return 3 +} + ############## # no need for atomic @@ -251,25 +244,24 @@ jssh_writeDB_async() { jssh_updateDB_async() { declare -n ARRAY="$1" [ -z "${ARRAY[*]}" ] && return 1 - declare -A oldARR newARR + declare -A oldARR jssh_readDB_async "oldARR" "$2" || return "$?" if [ -z "${oldARR[*]}" ]; then # no old content jssh_writeDB_async "$1" "$2" else # merge arrays - local o1 o2 n1 n2 - o1="$(declare -p oldARR)"; o2="${o1#*\(}" - n1="$(declare -p ARRAY)"; n2="${n1#*\(}" - unset IFS; set -f - #shellcheck disable=SC2034,SC2190,SC2206 - newARR=( ${o2:0:${#o2}-1} ${n2:0:${#n2}-1} ) - set +f - jssh_writeDB_async "newARR" "$2" + local key + for key in "${!ARRAY[@]}" + do + oldARR["${key}"]="${ARRAY["${key}"]}" + done + Array2Json "oldARR" >"${DB}" fi } -jssh_insertDB_async() { +alias jssh_insertDB_async=jssh_insertKeyDB_async +jssh_insertKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local key="$1" value="$2" local DB; DB="$(jssh_checkDB "$3")" diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 389c918..b2963ef 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-6-gda98b09 # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" @@ -189,7 +189,8 @@ send_message() { [ -z "$2" ] && return local text keyboard btext burl no_keyboard file lat long title address sent text="$(sed <<< "${2}" 's/ mykeyboardend.*//;s/ *my[kfltab][a-z]\{2,13\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')" - text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" + #text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" + text="${text//?([[:blank:]]|^)mynewlinestartshere?([[:blank:]]|$)/$'\n\r'}" [ "$3" != "safe" ] && { no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" From 46748ee4b8568be3644735883d55287fd4deb9f7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 31 May 2020 11:55:08 +0200 Subject: [PATCH 19/53] revert shellcheck tip for send_message, sed is faster --- modules/sendMessage.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index b2963ef..61f8dac 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.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$$ 0.96-dev2-6-gda98b09 +#### $$VERSION$$ 0.96-dev2-7-g6d1e7cc # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" @@ -189,8 +189,8 @@ send_message() { [ -z "$2" ] && return local text keyboard btext burl no_keyboard file lat long title address sent text="$(sed <<< "${2}" 's/ mykeyboardend.*//;s/ *my[kfltab][a-z]\{2,13\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')" - #text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" - text="${text//?([[:blank:]]|^)mynewlinestartshere?([[:blank:]]|$)/$'\n\r'}" + # shellcheck disable=SC2001 + text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" [ "$3" != "safe" ] && { no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" From ed14e850a49d83313224d1e2c8b4fa55793a13ef Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 09:54:35 +0200 Subject: [PATCH 20/53] use jsshDB internally, e.g. block / count users, it's working well now --- bashbot.sh | 67 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index df42a2c..ddaf438 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-4-g2a3dcaa +#### $$VERSION$$ 0.96-dev2-8-g46748ee # # Exit Codes: # - 0 sucess (hopefully) @@ -20,6 +20,7 @@ # - 3 user / command / file not found # - 4 unkown command # - 5 cannot connect to telegram bot +# - 6 mandatory module not found # shellcheck disable=SC2140 # are we runnig in a terminal? @@ -48,6 +49,22 @@ _is_function() { [ "$(LC_ALL=C type -t "$1")" = "function" ] } +# read JSON.sh style data and asssign to an ARRAY +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Json2Array() { + # shellcheck source=./commands.sh + [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) +} +# output ARRAY as JSON.sh style data +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Array2Json() { + local key + declare -n ARRAY="$1" + for key in "${!ARRAY[@]}" + do + printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" + done +} # get location and name of bashbot.sh SCRIPT="$0" @@ -87,6 +104,14 @@ if [ ! -w "." ]; then ls -ld . fi +#jsonDB is now mandatory +if [ ! -r "${MODULEDIR:-./modules}"/jsonDB.sh ]; then + echo -e "${RED}ERROR: Mandatory module ${MODULEDIR:-./modules}/jsonDB.sh is missing or not readable!" + exit 6 +fi +# shellcheck source=./modules/jsonDB.sh +source "${MODULEDIR:-./modules}"/jsonDB.sh + ##################### # Setup and check environment if BOTTOKEN is NOT set TOKENFILE="${BASHBOT_ETC:-.}/token" @@ -145,7 +170,12 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup count file if [ ! -f "${COUNTFILE}.jssh" ]; then - printf '["counted_user_id"]\t"num_messages_seen"\n' >"${COUNTFILE}.jssh" + jssh_newDB "${COUNTFILE}" + jssh_insertDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}" + # conveqrt old file on creation + if [ -r "${COUNTFILE}" ];then + sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" + fi elif [ ! -w "${COUNTFILE}.jssh" ]; then echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" ls -l "${COUNTFILE}.jssh" @@ -153,11 +183,12 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup blocked file if [ ! -f "${BLOCKEDFILE}.jssh" ]; then - printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh" + jssh_newDB "${BLOCKEDFILE}" + jssh_insertDB 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}" fi fi # cleanup (remove double entries) countfile on startup -[ "${SOURCE}" != "yes" ] && _exec_if_function jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" +[ "${SOURCE}" != "yes" ] && jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" # do we have BSD sed if ! sed '1ia' /dev/null; then @@ -208,7 +239,7 @@ fi ############### # load modules -for modules in "${MODULEDIR:-.}"/*.sh ; do +for modules in "${MODULEDIR:-./modules}"/*.sh ; do # shellcheck source=./modules/aliases.sh if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi done @@ -367,22 +398,6 @@ JsonGetLine() { JsonGetValue() { sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p' } -# read JSON.sh style data and asssign to an ARRAY -# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling -Json2Array() { - # shellcheck source=./commands.sh - [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) -} -# output ARRAY as JSON.sh style data -# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling -Array2Json() { - local key - declare -n ARRAY="$1" - for key in "${!ARRAY[@]}" - do - printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" - done -} ################ # processing of updates starts here @@ -405,7 +420,7 @@ process_client() { # check for uers / groups to ignore if [ -n "${USER[ID]}" ]; then [[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return - [ -r "${BLOCKEDFILE}" ] && _exec_if_function jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}" + jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}" fi if [ -z "${iQUERY[ID]}" ]; then process_message "${num}" "${debug}" @@ -426,7 +441,7 @@ process_client() { fi # last count users - _exec_if_function jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}" + jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}" } declare -Ax BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASHBOT_EVENT_SEND @@ -748,9 +763,9 @@ bot_init() { [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" chown -R "$TOUSER" . ./* chmod 711 . - chmod -R a-w ./* - chmod -R u+w "${COUNTFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null - chmod -R o-r,o-w "${COUNTFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R o-w ./* + chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh' -exec chmod u+w \{\} + #ls -la From ed373a8a03639e8f49518c1a689863796aa9ad08 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 10:35:51 +0200 Subject: [PATCH 21/53] MODULEDIR has full path always, . is only for corner cases --- commands.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.sh b/commands.sh index e043525..ffda32b 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-5-g578056b +#### $$VERSION$$ 0.96-dev2-9-ged14e85 # # adjust your language setting here, e.g.when run from other user or cron. From e366633ebd64bc1d7b8b8c0a0ad29c6fcd530b09 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 11:47:07 +0200 Subject: [PATCH 22/53] adapt tests to new output and processing --- test/ALL-tests.inc.sh | 7 +-- test/c-init-test.sh | 4 +- test/c-init-test/blocked.jssh | 1 + test/c-init-test/count | 1 - test/c-init-test/count.jssh | 1 + test/c-init-test/count.test | 80 ---------------------------------- test/d-process_message-test.sh | 5 ++- 7 files changed, 12 insertions(+), 87 deletions(-) create mode 100644 test/c-init-test/blocked.jssh delete mode 100644 test/c-init-test/count create mode 100644 test/c-init-test/count.jssh delete mode 100644 test/c-init-test/count.test diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index e62023d..e3ae191 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-10-ged373a8 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME @@ -11,10 +11,11 @@ export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME TESTNAME="${REFDIR//-/ }" # common filenames -export TOKENFILE ACLFILE COUNTFILE ADMINFILE DATADIR JSONSHFILE +export TOKENFILE ACLFILE COUNTFILE BLOCKEDFILE ADMINFILE DATADIR JSONSHFILE TOKENFILE="token" ACLFILE="botacl" - COUNTFILE="count" + COUNTFILE="count.jssh" + BLOCKEDFILE="blocked.jssh" ADMINFILE="botadmin" DATADIR="data-bot-bash" JSONSHFILE="JSON.sh/JSON.sh" diff --git a/test/c-init-test.sh b/test/c-init-test.sh index a0f79bc..5f96083 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-10-ged373a8 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -46,5 +46,5 @@ cd "${DIRME}" || exit 1 echo "${SUCCESS}" echo "Test bashbot.sh count" -cp "${REFDIR}/count.test" "${TESTDIR}/count" +#cp "${REFDIR}/count.test" "${TESTDIR}/counti.jssh" #"${TESTDIR}/bashbot.sh" count diff --git a/test/c-init-test/blocked.jssh b/test/c-init-test/blocked.jssh new file mode 100644 index 0000000..2de89ec --- /dev/null +++ b/test/c-init-test/blocked.jssh @@ -0,0 +1 @@ +["blocked_user_or_chat_id"] "name and reason" diff --git a/test/c-init-test/count b/test/c-init-test/count deleted file mode 100644 index 8b13789..0000000 --- a/test/c-init-test/count +++ /dev/null @@ -1 +0,0 @@ - diff --git a/test/c-init-test/count.jssh b/test/c-init-test/count.jssh new file mode 100644 index 0000000..9dadf52 --- /dev/null +++ b/test/c-init-test/count.jssh @@ -0,0 +1 @@ +["counted_user_chat_id"] "num_messages_seen" diff --git a/test/c-init-test/count.test b/test/c-init-test/count.test deleted file mode 100644 index bc1c186..0000000 --- a/test/c-init-test/count.test +++ /dev/null @@ -1,80 +0,0 @@ -COUNT-3474588 -COUNT2224533 -COUNT-10011894466 -COUNT7053247 -COUNT6391479 -COUNT6341244 -COUNT4919830 -COUNT2640175 -COUNT7349731 -COUNT7244826 -COUNT2741728 -COUNT6376258 -COUNT-10012414459 -COUNT5869285 -COUNT-10011297801 -COUNT190323 -COUNT7784183 -COUNT-2766119 -COUNT-3859393 -COUNT-10013599717 -COUNT-10012886611 -COUNT2442778 -COUNT5759451 -COUNT5254245 -COUNT3781067 -COUNT7916264 -COUNT7235018 -COUNT3349831 -COUNT2933766 -COUNT5924516 -COUNT6374645 -COUNT6407149 -COUNT2383316 -COUNT5447722 -COUNT231261 -COUNT7853337 -COUNT7768269 -COUNT7546987 -COUNT7882954 -COUNT6081264 -COUNT4473899 -COUNT6000622 -COUNT7489339 -COUNT7925467 -COUNT146383 -COUNT1850458 -COUNT5232050 -COUNT7827320 -COUNT3230155 -COUNT5110579 -COUNT2968981 -COUNT6709735 -COUNT3042403 -COUNT6973351 -COUNT3739552 -COUNT6073516 -COUNT7773218 -COUNT7570810 -COUNT7165830 -COUNT7836756 -COUNT2603688 -COUNT2792256 -COUNT1199924 -COUNT7044680 -COUNT5011616 -COUNT5944645 -COUNT6040303 -COUNT7787713 -COUNT7670967 -COUNT4902763 -COUNT8168450 -COUNT5233276 -COUNT-10012311537 -COUNT7648681 -COUNT4170584 -COUNT3045040 -COUNT5919227 -COUNT3364980 -COUNT-3450384 -COUNT7287381 diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 2c58c4c..ade74d2 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ 0.96-dev2-10-ged373a8 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -30,6 +30,9 @@ echo "Check process_message ..." set -x { process_message "0"; set +x; } >>"${LOGFILE}" 2>&1; +USER[ID]="123456789" +CHAT[ID]="123456789" + # output processed input print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" "VENUE" >"${OUTPUTFILE}" diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1 From 31b32034835ce28f55d66682f0f1cc7e0287927d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 11:50:42 +0200 Subject: [PATCH 23/53] fix problems found by tests --- bashbot.sh | 16 ++++++++-------- modules/jsonDB.sh | 16 +++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ddaf438..fd0f524 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-8-g46748ee +#### $$VERSION$$ 0.96-dev2-11-ge366633 # # Exit Codes: # - 0 sucess (hopefully) @@ -105,12 +105,12 @@ if [ ! -w "." ]; then fi #jsonDB is now mandatory -if [ ! -r "${MODULEDIR:-./modules}"/jsonDB.sh ]; then - echo -e "${RED}ERROR: Mandatory module ${MODULEDIR:-./modules}/jsonDB.sh is missing or not readable!" +if [ ! -r "${MODULEDIR:-.}"/jsonDB.sh ]; then + echo -e "${RED}ERROR: Mandatory module ${MODULEDIR:-.}/jsonDB.sh is missing or not readable!" exit 6 fi # shellcheck source=./modules/jsonDB.sh -source "${MODULEDIR:-./modules}"/jsonDB.sh +source "${MODULEDIR:-.}"/jsonDB.sh ##################### # Setup and check environment if BOTTOKEN is NOT set @@ -171,7 +171,7 @@ if [ -z "${BOTTOKEN}" ]; then # setup count file if [ ! -f "${COUNTFILE}.jssh" ]; then jssh_newDB "${COUNTFILE}" - jssh_insertDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}" + jssh_insertKeyDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}" # conveqrt old file on creation if [ -r "${COUNTFILE}" ];then sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" @@ -184,7 +184,7 @@ if [ -z "${BOTTOKEN}" ]; then # setup blocked file if [ ! -f "${BLOCKEDFILE}.jssh" ]; then jssh_newDB "${BLOCKEDFILE}" - jssh_insertDB 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}" + jssh_insertKeyDB 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}" fi fi # cleanup (remove double entries) countfile on startup @@ -238,8 +238,8 @@ if [ "${SOURCE}" != "yes" ]; then fi ############### -# load modules -for modules in "${MODULEDIR:-./modules}"/*.sh ; do +# load +for modules in "${MODULEDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi done diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 7abed5a..ae8c6e6 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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$$ 0.96-dev2-6-gda98b09 +#### $$VERSION$$ 0.96-dev2-11-ge366633 # # source from commands.sh to use jsonDB functions # @@ -208,18 +208,20 @@ jssh_newDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 [ -f "${DB}" ] && return 2 # already exist, do not zero out - printf '\n' >"${DB}" + printf '' >"${DB}" } -# $1 filename, check filename, it must be relative to BASHBOT_ETC, and not contain '..' +# $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..' # returns real path to DB file if everything is ok jssh_checkDB(){ + local DB [ -z "$1" ] && return 1 - local DB="${BASHBOT_ETC:-.}/$1.jssh" - if [[ "$1" = "${BASHBOT_ETC:-.}"* ]] || [[ "$1" = "${BASHBOT_DATA:-.}"* ]]; then - DB="$1.jssh" - fi [[ "$1" = *'..'* ]] && return 2 + if [[ "$1" == "${BASHBOT_VAR:-.}"* ]] || [[ "$1" == "${BASHBOT_DATA:-.}"* ]]; then + DB="$1.jssh" + else + DB="${BASHBOT_VAR:-.}/$1.jssh" + fi printf '%s\n' "${DB}" } From dddd1ce2de4ce02da04324f9c2fa981253e56f14 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 12:00:37 +0200 Subject: [PATCH 24/53] update docs --- doc/6_reference.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 3f93e91..d8a6147 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -480,7 +480,7 @@ and only the last one is written back to the file. fast functions: ``` - jssh_insertDB , jssh_addKeyDB , jssh_countKeyDB + jssh_insertKeyDB , jssh_addKeyDB , jssh_countKeyDB ``` slow functions: @@ -519,9 +519,11 @@ Creats new empty jsshDB file if not exist. *usage:* jssh_newDB "filename" ##### jssh_checkDB -Check if DB name respects the rules mentioned above and returns the real/final path to DB file. +Check if DB name respects the rules mentioned above and print to STDOUT the real/final path to DB file. Used internally by all jssh DB functions, but can also used to get the real filename for a jssh DB. +An error is returned and nothing is printed if the given filename is not valid + *usage:* jssh_checkDB "filename" ```bash @@ -689,18 +691,20 @@ jssh_printDB READVALUES ["whynot","subindex1"] "whynot A" ``` -##### jssh_insertDB +##### jssh_insertKeyDB Insert, update, append a key=value pair to a jsshDB file, key name is only allowed to contain '-a-zA-Z0-9,._' -*usage:* jssh_insertDB "key" "value" "filename" +*usage:* jssh_insertKeyDB "key" "value" "filename" -*usage:* jssh_insertDB_asnyc "key" "value" "filename" +*usage:* jssh_insertKeyDB_asnyc "key" "value" "filename" -Note: insertDB uses also excusiv write locking, but with a maximum timeout of 2s. insertDB is a "fast" operation, simply adding the value to the end of the file. +*deprecated:* jssh_insertDB *was renamed in verion 0.96 to* jssh_insertKeyDB + +Note: inserKeytDB uses also excusive write locking, but with a maximum timeout of 2s. insertKeyDB is a "fast" operation, simply adding the value to the end of the file. *example:* ```bash -jssh_insertDB "newkey" "an other value" "${DATADIR:-.}/myvalues" +jssh_insertKeyDB "newkey" "an other value" "${DATADIR:-.}/myvalues" ``` ##### jssh_deleteKeyDB @@ -1019,5 +1023,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ 0.96-dev2-4-g2a3dcaa +#### $$VERSION$$ 0.96-dev2-12-g31b3203 From 2a66ee928f0671e63c6b2c299d694b7c3b09ffcb Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 13:44:30 +0200 Subject: [PATCH 25/53] 0.96-dev3, update tests and docs for new features --- README.html | 3 +- README.md | 3 +- README.txt | 3 +- doc/2_usage.md | 19 +++++---- test/ALL-tests.inc.sh | 5 ++- test/c-init-test.sh | 9 ++-- test/c-init-test/count.test | 27 ++++++++++++ test/c-init-test/token | 2 +- test/d-process_message-test.sh | 4 +- .../d-process_message-test.input | 13 ++++++ .../d-process_message-test.result | 6 +++ .../d-send_message-test.result | 42 +++++++++---------- test/e-env-test.sh | 3 +- 13 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 test/c-init-test/count.test diff --git a/README.html b/README.html index 15590ae..da7e872 100644 --- a/README.html +++ b/README.html @@ -139,6 +139,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
  • Sending Messages, Files, Keyboards
  • User Access Control
  • Inline Queries
  • +
  • jsshDB Bashbot key-value storage
  • Background and Interactive Jobs
  • Deveoper Notes @@ -230,6 +231,6 @@ It features background tasks and interactive chats, and can serve as an interfac

    @Gnadelwartz

    That's it!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ 0.96-dev2-0-gcbad540

    +

    $$VERSION$$ v0.96-dev3-0-gdddd1ce

    diff --git a/README.md b/README.md index 6df4f22..51c0bfe 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do * Sending Messages, Files, Keyboards * User Access Control * Inline Queries + * jsshDB Bashbot key-value storage * Background and Interactive Jobs * [Deveoper Notes](doc/7_develop.md) * Debug bashbot @@ -197,4 +198,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ 0.96-dev2-0-gcbad540 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce diff --git a/README.txt b/README.txt index 02c6066..288cbd7 100644 --- a/README.txt +++ b/README.txt @@ -65,6 +65,7 @@ availible on www.github.com * Sending Messages, Files, Keyboards * User Access Control * Inline Queries + * jsshDB Bashbot key-value storage * Background and Interactive Jobs * [Deveoper Notes](doc/7_develop.md) * Debug bashbot @@ -280,4 +281,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ 0.96-dev2-0-gcbad540 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce diff --git a/doc/2_usage.md b/doc/2_usage.md index 124078f..629dadb 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -17,22 +17,27 @@ Have FUN! ### Files ``` . -├── mycommands.sh # THIS is your bot, place logic and commands here! +├── mycommands.sh # THIS is your bot, place logic and commands here! │ ├── mycommands.sh.clean # copy to "mycommands.sh" if you start devloping your bot -├── mycommands.sh.dist # example bot, also used for testing bashbot internally +├── mycommands.sh.dist # example bot, also used for testing bashbot internally │ -├── bashbot.sh # main bashbot script - DO NOT EDIT! +├── count.jssh # count bashbot usage in jssh key-value store +├── blocked.jssh # list of blocked USER[ID] in jssh key-value store +│ +├── bashbot.sh # main bashbot script - DO NOT EDIT! ├── commands.sh # command dispatcher - DO NOT EDIT! -├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh +├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh +│ +├── scripts # place your bashbot interactive and background scripts hereh │ ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off │   ├── answerInline.sh -│   ├── jsshDB.sh # read and store JSON.sh stlye JSON +│   ├── jsshDB.sh # read and store JSON.sh stlye JSON, mandatory │   ├── background.sh # interactive and background functions │   ├── chatMember.sh -│   └── sendMessage.sh # main send message functions, do not disable +│   └── sendMessage.sh # main send message functions, mandatory │ ├── addons # optional addons, disbaled by default │   ├── example.sh # to enable addons change their XXX_ENABLE to true @@ -241,5 +246,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index e3ae191..9d78bd0 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.96-dev2-10-ged373a8 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME @@ -32,8 +32,9 @@ export INPUTFILE REFFILE OUTPUTFILE OUTPUTFILE="${TESTDIR}/${REFDIR}.out" # do not query telegram when testing -export BASHBOT_URL +export BASHBOT_URL TESTTOKEN BASHBOT_URL="https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?" +TESTTOKEN="123456789:BASHBOTTESTSCRIPTbashbottestscript_" # print arrays in reproducible order print_array() { diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 5f96083..6cc9c1b 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,12 +1,11 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.96-dev2-10-ged373a8 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh source "./ALL-tests.inc.sh" -TESTTOKEN="bashbottestscript" -TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${ADMINFILE}" +TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${BLOCKEDFILE} ${ADMINFILE}" set -e @@ -46,5 +45,5 @@ cd "${DIRME}" || exit 1 echo "${SUCCESS}" echo "Test bashbot.sh count" -#cp "${REFDIR}/count.test" "${TESTDIR}/counti.jssh" -#"${TESTDIR}/bashbot.sh" count +cp "${REFDIR}/count.test" "${TESTDIR}/count.jssh" +"${TESTDIR}/bashbot.sh" count diff --git a/test/c-init-test/count.test b/test/c-init-test/count.test new file mode 100644 index 0000000..313248d --- /dev/null +++ b/test/c-init-test/count.test @@ -0,0 +1,27 @@ +["counted_user_id"] "num_messages_seen" +["712677"] "2" +["-1001189446"] "29" +["-1001288661"] "6" +["-1001433755"] "4" +["-408138"] "2" +["1246831"] "7" +["-1001186489"] "4" +["-1001259400"] "8" +["791626"] "18" +["-1001293952"] "4" +["-1001435141"] "7" +["733039"] "2" +["-1001319011"] "6" +["-1001220313"] "15" +["26122"] "3" +["988411"] "8" +["908527"] "6" +["-1001450413"] "58" +["748933"] "2" +["-1001425571"] "5" +["788295"] "3" +["586928"] "45" +["-1001359971"] "7" +["1069707"] "19" +["-1001189446"] "30" +["-1001189446"] "31" diff --git a/test/c-init-test/token b/test/c-init-test/token index 60557b1..006890a 100644 --- a/test/c-init-test/token +++ b/test/c-init-test/token @@ -1 +1 @@ -bashbottestscript +123456789:BASHBOTTESTSCRIPTbashbottestscript_ diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index ade74d2..fad677e 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.96-dev2-10-ged373a8 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -34,7 +34,7 @@ USER[ID]="123456789" CHAT[ID]="123456789" # output processed input -print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" "VENUE" >"${OUTPUTFILE}" +print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" "VENUE" "SERVICE">"${OUTPUTFILE}" diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1 echo "${SUCCESS}" diff --git a/test/d-process_message-test/d-process_message-test.input b/test/d-process_message-test/d-process_message-test.input index e166651..798449c 100644 --- a/test/d-process_message-test/d-process_message-test.input +++ b/test/d-process_message-test/d-process_message-test.input @@ -89,3 +89,16 @@ ["result",0,"message","voice","duration"] 2 ["result",0,"message","voice","mime_type"] "audio/ogg" ["result",0,"message","voice","file_size"] 4262 +["result",0,"message","new_chat_participant","id"] 123456789 +["result",0,"message","new_chat_participant","is_bot"] false +["result",0,"message","new_chat_participant","first_name"] "Kay" +["result",0,"message","new_chat_participant","last_name"] "M" +["result",0,"message","new_chat_member","id"] 123456789 +["result",0,"message","new_chat_member","is_bot"] false +["result",0,"message","new_chat_member","first_name"] "Kay" +["result",0,"message","new_chat_member","last_name"] "M" +["result",0,"message","new_chat_members",0,"id"] 123456789 +["result",0,"message","new_chat_members",0,"is_bot"] false +["result",0,"message","new_chat_members",0,"first_name"] "Kay" +["result",0,"message","new_chat_members",0,"last_name"] "M" +["result",0,"message","left_chat_member","id"] 123456789 diff --git a/test/d-process_message-test/d-process_message-test.result b/test/d-process_message-test/d-process_message-test.result index d242a2f..7fbedaf 100644 --- a/test/d-process_message-test/d-process_message-test.result +++ b/test/d-process_message-test/d-process_message-test.result @@ -42,3 +42,9 @@ VENUE: FOURSQUARE 4c4321afce54e21eee980d1a VENUE: LATITUDE 49.631824 VENUE: LONGITUDE 8.377072 VENUE: TITLE Kolb's Biergarten +SERVICE: 0 yes +SERVICE: LEFTMEMBER 123456789 +SERVICE: NEWMEMBER 123456789 +SERVICE: NEWPHOTO +SERVICE: NEWTILE +SERVICE: PINNED diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index 05ddf73..da76762 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -1,64 +1,64 @@ chat:123456 JSON:"text":"\# test for text only output" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"This is a normal text" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"This is a normal text with a line break" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":" This is a HTML<\/b> text","parse_mode":"html" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":" This is a HTML<\/b> text with a line break","parse_mode":"html" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":" This is a \*MARKDOWN\* text","parse_mode":"markdown" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":" This is a \*MARKDOWN\* text with a line break","parse_mode":"markdown" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"\# test for keyboard\, file\, venue output" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"Text plus keyboard will appear in chat", "reply_markup": {"keyboard": [ [ "Yep, sure" , "No, highly unlikely" ] ] , "one_time_keyboard":true} -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"latitude": la10, "longitude": lo20 -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendLocation +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendLocation chat:123456 JSON:"latitude": la10, "longitude": lo20, "address": "Diagon Alley N. 37", "title": "my home" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendVenue +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendVenue chat:123456 JSON:"text":"\# test for new inline button" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"Text plus keyboard will appear in chat", "reply_markup": {"inline_keyboard": [ [ {"text":"Button Text", "url":"https://www..."}] ]} -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"STABILO 88\/240 Fineliner point 88 -[https:\/\/images\-na.ssl\-images\-amazon.com\/images\/I\/41oypA3kmHL.l_SX300.jpg] +[https:\/\/images\-na\.ssl\-images\-amazon\.com\/images\/I\/41oypA3kmHL\.l_SX300\.jpg] second part of text -plus newline.", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen ...", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +plus newline\.", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen \.\.\.", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"text":"\# test for sendfile" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendMessage chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendPhoto +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendPhoto chat:123456 JSON:"action": "upload_photo" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendChatAction +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendChatAction chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendDocument +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendDocument chat:123456 JSON:"action": "upload_document" -URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendChatAction +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?123456789:BASHBOTTESTSCRIPTbashbottestscript_/sendChatAction diff --git a/test/e-env-test.sh b/test/e-env-test.sh index 0eec21a..8aeeb73 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-0-gdddd1ce # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -33,7 +33,6 @@ cp ${TESTDIR}/*commands.sh "${BASHBOT_ETC}" || exit 1 set -f cp -r "${TESTDIR}/bashbot.sh" "${TESTDIR}/modules" "${BASHBOT_BIN}" || exit 1 -TESTTOKEN="bashbottestscript" TESTFILES="${TOKENFILE} ${ACLFILE} ${ADMINFILE}" From 8e7583d3b79c620cd993128e411b42602a20b3bd Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 1 Jun 2020 15:04:37 +0200 Subject: [PATCH 26/53] add test and doc for stats, make test send_message more stable --- dev/all-tests.sh | 2 +- doc/2_usage.md | 15 +++++++++++---- test/c-init-test.sh | 9 +++++---- test/c-init-test/stats.out | 1 + test/d-send_message-test.sh | 9 +++++++-- test/e-env-test/token | 2 +- 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 test/c-init-test/stats.out diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 84488b0..cd1fcd3 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-1-g2a66ee9 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/doc/2_usage.md b/doc/2_usage.md index 629dadb..c74e196 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -74,14 +74,21 @@ Start or Stop your Bot use the following commands: ./bashbot.sh kill ``` -### User count +### User stats -deprecated, will be removed! +To count the total number of users and messages run the following command: + +``` +./bashbot.sh stats ``` ### Sending broadcasts to all users + +To send a broadcast to all of users that ever used the bot run the following command: + +``` +./bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!" ``` -deprecated, will be removed! ---- @@ -246,5 +253,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev3-0-gdddd1ce +#### $$VERSION$$ v0.96-dev3-1-g2a66ee9 diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 6cc9c1b..8a43419 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev3-0-gdddd1ce +#### $$VERSION$$ v0.96-dev3-1-g2a66ee9 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -32,7 +32,7 @@ trap exit 1 EXIT cd "${TESTDIR}" || exit echo "Test if $JSONSHFILE exists ..." -[ ! -x "$JSONSHFILE" ] && { echo "${NOSUCCESS} Fail diff ${file}!"; exit 1; } +[ ! -x "$JSONSHFILE" ] && { echo "${NOSUCCESS} json.sh not found"; exit 1; } echo "Test Sourcing of bashbot.sh ..." # shellcheck source=./bashbot.sh @@ -44,6 +44,7 @@ trap '' EXIT cd "${DIRME}" || exit 1 echo "${SUCCESS}" -echo "Test bashbot.sh count" +echo "Test bashbot.sh stat" cp "${REFDIR}/count.test" "${TESTDIR}/count.jssh" -"${TESTDIR}/bashbot.sh" count +"${TESTDIR}/bashbot.sh" stats >"${TESTDIR}/stats.out" +diff -q "${TESTDIR}/stats.out" "${REFDIR}/stats.out" >>"${LOGFILE}" || { echo "${NOSUCCESS} Fail diff stats output!"; FAIL="1"; } diff --git a/test/c-init-test/stats.out b/test/c-init-test/stats.out new file mode 100644 index 0000000..3f1015a --- /dev/null +++ b/test/c-init-test/stats.out @@ -0,0 +1 @@ +A total of 272 messages from 24 users are processed. diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 9d9d481..45a083d 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-1-g2a66ee9 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -54,7 +54,12 @@ done < "${INPUTFILE}" 2>>"${LOGFILE}" echo " done." -{ diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1; } | cat -v +sort -d -o "${OUTPUTFILE}.sort" "${OUTPUTFILE}" +sort -d -o "${REFFILE}.sort" "${REFFILE}" + +{ diff -c "${REFFILE}.sort" "${OUTPUTFILE}.sort" || exit 1; } | cat -v +rm -f "${REFFILE}.sort" + echo " ... all \"send_message\" functions seems to work as expected." echo "${SUCCESS}" diff --git a/test/e-env-test/token b/test/e-env-test/token index 60557b1..006890a 100644 --- a/test/e-env-test/token +++ b/test/e-env-test/token @@ -1 +1 @@ -bashbottestscript +123456789:BASHBOTTESTSCRIPTbashbottestscript_ From c729cf428b63f2e2f211dadfd8f2bfeef9e26077 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 4 Jun 2020 16:10:58 +0200 Subject: [PATCH 27/53] fix wrong json.sh location if included from other location --- bashbot.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index fd0f524..c91eb88 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-11-ge366633 +#### $$VERSION$$ V0.94-6-gdcf6534 # # Exit Codes: # - 0 sucess (hopefully) @@ -84,14 +84,16 @@ fi if [ -n "$BASHBOT_HOME" ]; then SCRIPTDIR="$BASHBOT_HOME" - [ -z "${BASHBOT_ETC}" ] && BASHBOT_ETC="$BASHBOT_HOME" - [ -z "${BASHBOT_VAR}" ] && BASHBOT_VAR="$BASHBOT_HOME" + else + BASHBOT_HOME="${SCRIPTDIR}" fi +[ -z "${BASHBOT_ETC}" ] && BASHBOT_ETC="$BASHBOT_HOME" +[ -z "${BASHBOT_VAR}" ] && BASHBOT_VAR="$BASHBOT_HOME" ADDONDIR="${BASHBOT_ETC:-./addons}" - RUNUSER="${USER}" # USER is overwritten by bashbot array +# OK everthing setup, lest start if [ "${SOURCE}" != "yes" ] && [ -z "$BASHBOT_HOME" ] && ! cd "${RUNDIR}" ; then echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}" exit 1 @@ -777,7 +779,7 @@ if ! _is_function send_message ; then exit 1 fi -JSONSHFILE="${BASHBOT_JSONSH:-${RUNDIR}/JSON.sh/JSON.sh}" +JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" [[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3 if [ ! -f "${JSONSHFILE}" ]; then From bc74141db7b96d33b07e7dcebaf43f6c8d9d85d9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Jun 2020 09:01:20 +0200 Subject: [PATCH 28/53] _async for every jssh function, use async in bashbot.sh --- bashbot.sh | 74 +++++++++++++++++++++-------------------------- doc/2_usage.md | 5 ++-- modules/jsonDB.sh | 62 +++++++++++++++++++++------------------ 3 files changed, 70 insertions(+), 71 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index c91eb88..aff057c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.94-6-gdcf6534 +#### $$VERSION$$ v0.96-dev3-3-gc729cf4 # # Exit Codes: # - 0 sucess (hopefully) @@ -172,9 +172,9 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup count file if [ ! -f "${COUNTFILE}.jssh" ]; then - jssh_newDB "${COUNTFILE}" - jssh_insertKeyDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}" - # conveqrt old file on creation + jssh_newDB_async "${COUNTFILE}" + jssh_insertKeyDB_async 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}" + # convert old file on creation if [ -r "${COUNTFILE}" ];then sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" fi @@ -185,12 +185,12 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup blocked file if [ ! -f "${BLOCKEDFILE}.jssh" ]; then - jssh_newDB "${BLOCKEDFILE}" - jssh_insertKeyDB 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}" + jssh_newDB_async "${BLOCKEDFILE}" + jssh_insertKeyDB_async 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}" fi fi # cleanup (remove double entries) countfile on startup -[ "${SOURCE}" != "yes" ] && jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" +[ "${SOURCE}" != "yes" ] && jssh_deleteKeyDB_async "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" # do we have BSD sed if ! sed '1ia' /dev/null; then @@ -422,7 +422,7 @@ process_client() { # check for uers / groups to ignore if [ -n "${USER[ID]}" ]; then [[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return - jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}" + jssh_readDB_async "BASHBOT_BLOCKED" "${BLOCKEDFILE}" fi if [ -z "${iQUERY[ID]}" ]; then process_message "${num}" "${debug}" @@ -443,7 +443,7 @@ process_client() { fi # last count users - jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}" + jssh_countKeyDB_async "${CHAT[ID]}" "${COUNTFILE}" } declare -Ax BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASHBOT_EVENT_SEND @@ -838,43 +838,35 @@ if [ "${SOURCE}" != "yes" ]; then case "$1" in "stats"|'count') declare -A STATS - if _is_function jssh_readDB ; then - jssh_readDB "STATS" "${COUNTFILE}" - for MSG in ${!STATS[*]} - do - [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue - (( USERS++ )) - done - for MSG in ${STATS[*]} - do - (( MESSAGES+=MSG )) - done - echo "A total of ${MESSAGES} messages from ${USERS} users are processed." - else - echo "Module jsshDB is not availible." - fi + jssh_readDB_async "STATS" "${COUNTFILE}" + for MSG in ${!STATS[*]} + do + [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue + (( USERS++ )) + done + for MSG in ${STATS[*]} + do + (( MESSAGES+=MSG )) + done + echo "A total of ${MESSAGES} messages from ${USERS} users are processed." exit ;; 'broadcast') declare -A SENDALL shift - if _is_function jssh_readDB ; then - jssh_readDB "SENDALL" "${COUNTFILE}" - echo -e "Sending broadcast message to all users \c" - for MSG in ${!SENDALL[*]} - do - [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue - (( USERS++ )) - if [ -n "$*" ]; then - send_markdown_message "${MSG}" "$*" - echo -e ".\c" - sleep 0.1 - fi - done - echo -e "\nMessage \"$*\" sent to ${USERS} users." - else - echo "Module jsshDB is not availible." - fi + jssh_readDB_async "SENDALL" "${COUNTFILE}" + echo -e "Sending broadcast message to all users \c" + for MSG in ${!SENDALL[*]} + do + [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue + (( USERS++ )) + if [ -n "$*" ]; then + send_markdown_message "${MSG}" "$*" + echo -e ".\c" + sleep 0.1 + fi + done + echo -e "\nMessage \"$*\" sent to ${USERS} users." exit ;; "status") diff --git a/doc/2_usage.md b/doc/2_usage.md index c74e196..4889cc7 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -29,7 +29,8 @@ Have FUN! ├── commands.sh # command dispatcher - DO NOT EDIT! ├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh │ -├── scripts # place your bashbot interactive and background scripts hereh +├── scripts # place your bashbot interactive and background scripts here +├── logs # here you'll find ERROR, DEBUG and MESSAGE.log │ ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off @@ -253,5 +254,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev3-1-g2a66ee9 +#### $$VERSION$$ v0.96-dev3-3-gc729cf4 diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index ae8c6e6..7d17974 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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$$ 0.96-dev2-11-ge366633 +#### $$VERSION$$ v0.96-dev3-3-gc729cf4 # # source from commands.sh to use jsonDB functions # @@ -118,9 +118,10 @@ if _exists flock; then } 200>"${DB}${BASHBOT_LOCKNAME}" } - # delete key/value from jsshDB + # get key/value from jsshDB # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + alias jssh_getDB=jssh_getKeyDB jssh_getKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" @@ -156,6 +157,18 @@ if _exists flock; then } 200>"${DB}${BASHBOT_LOCKNAME}" } + # updatie key/value in place to jsshDB + # $1 key name, can onyl contain -a-zA-Z0-9,._ + # $2 key value + # $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + #no own locking, so async is the same as updatekeyDB + jssh_updateKeyDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + declare -A oldARR + oldARR["$1"]="$2" + jssh_updateDB "oldARR" "${3}" || return 3 + } + else ######### @@ -165,45 +178,24 @@ else alias jssh_updateDB=jssh_updateDB_async alias jssh_insertDB=jssh_insertDB_async alias ssh_deleteKeyDB=jssh_deleteKeyDB_async + alias jssh_getDB=jssh_getKeyDB_async alias jssh_getKeyDB=jssh_getKeyDB_async alias jssh_countKeyDB=jssh_countKeyDB_async + alias jssh_updateKeyDB=jssh_updateKeyDB_async fi -# updatie key/value in place to jsshDB -# $1 key name, can onyl contain -a-zA-Z0-9,._ -# $2 key value -# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' -#no own locking, so async is the same as updatekeyDB -alias jssh_updateKeyDB_async=jssh_updateKeyDB -jssh_updateKeyDB() { - [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - declare -A oldARR - oldARR["$1"]="$2" - jssh_updateDB "oldARR" "${3}" || return 3 -} - ############## # no need for atomic # print ARRAY content to stdout instead of file # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront +alias jssh_printDB_async=jssh_printDB jssh_printDB() { Array2Json "$1" } -# get key/value from jsshDB -# $1 key name, can onyl contain -a-zA-Z0-9,._ -# $2 key value -# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' -# returns value -jssh_getDB() { - [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - declare -A getARR - jssh_readDB "getARR" "$3" || return "$?" - printf '%s\n' "${getARR[${key}]}" -} - # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +alias jssh_newDB_async=jssh_newDB jssh_newDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 @@ -213,6 +205,7 @@ jssh_newDB() { # $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..' # returns real path to DB file if everything is ok +alias jssh_checkDB_async=jssh_checkDB jssh_checkDB(){ local DB [ -z "$1" ] && return 1 @@ -227,7 +220,7 @@ jssh_checkDB(){ ###################### -# "old" implementations as non atomic functions +# implementations as non atomic functions # can be used explictitly or as fallback if flock is not availible jssh_readDB_async() { local DB; DB="$(jssh_checkDB "$2")" @@ -302,3 +295,16 @@ jssh_countKeyDB_async() { Array2Json "oldARR" >"${DB}" } +# updatie key/value in place to jsshDB +# $1 key name, can onyl contain -a-zA-Z0-9,._ +# $2 key value +# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' +#no own locking, so async is the same as updatekeyDB +jssh_updateKeyDB() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + declare -A oldARR + oldARR["$1"]="$2" + jssh_updateDB_async "oldARR" "${3}" || return 3 +} + + From 407e1476a37079321d890a1a72c80e8f65e7a336 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Jun 2020 22:13:18 +0200 Subject: [PATCH 29/53] add jssh_clearBD --- bashbot.sh | 2 +- doc/2_usage.md | 2 +- modules/jsonDB.sh | 21 ++++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index aff057c..15229dc 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-3-gc729cf4 +#### $$VERSION$$ v0.96-dev3-4-gbc74141 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/doc/2_usage.md b/doc/2_usage.md index 4889cc7..e772e88 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -254,5 +254,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev3-3-gc729cf4 +#### $$VERSION$$ v0.96-dev3-4-gbc74141 diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 7d17974..c117783 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev3-3-gc729cf4 +#### $$VERSION$$ v0.96-dev3-4-gbc74141 # # source from commands.sh to use jsonDB functions # @@ -157,7 +157,7 @@ if _exists flock; then } 200>"${DB}${BASHBOT_LOCKNAME}" } - # updatie key/value in place to jsshDB + # update key/value in place to jsshDB # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 key value # $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' @@ -169,6 +169,14 @@ if _exists flock; then jssh_updateDB "oldARR" "${3}" || return 3 } + # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + jssh_clearDB() { + local DB; DB="$(jssh_checkDB "$1")" + [ -z "${DB}" ] && return 1 + { flock -e -w 10 200 + printf '' >"${DB}" + } 200>"${DB}${BASHBOT_LOCKNAME}" + } else ######### @@ -182,6 +190,7 @@ else alias jssh_getKeyDB=jssh_getKeyDB_async alias jssh_countKeyDB=jssh_countKeyDB_async alias jssh_updateKeyDB=jssh_updateKeyDB_async + alias jssh_clearDB=jssh_clearDB_async fi ############## @@ -300,11 +309,17 @@ jssh_countKeyDB_async() { # $2 key value # $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' #no own locking, so async is the same as updatekeyDB -jssh_updateKeyDB() { +jssh_updateKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 declare -A oldARR oldARR["$1"]="$2" jssh_updateDB_async "oldARR" "${3}" || return 3 } +jssh_clearDB_async() { + local DB; DB="$(jssh_checkDB "$1")" + [ -z "${DB}" ] && return 1 + printf '' >"${DB}" +} + From d0e9bd874b54ea022dc393b7da9f6c2c9e69b160 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Jun 2020 22:24:33 +0200 Subject: [PATCH 30/53] update doc --- doc/6_reference.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index d8a6147..bb4338a 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -486,7 +486,7 @@ fast functions: slow functions: ``` - jssh_writeDB, jssh_updateDB , jssh_deleteKeyDBB + jssh_writeDB, jssh_updateDB , jssh_deleteKeyDB, jssh_clearDB ``` @@ -502,7 +502,8 @@ Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are ref Since version 0.94 jsshDB functions support file locking with flock. write/update operations are serialised with flock to wait until previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations. -In case flock is not availibe or you don't want locking, jsshDB functions will be used without file locking. +Every jssh_*DB function exist as jssj_*DB_async also. +In case flock is not availibe or you don't want locking, jssh_*DB_async functions without file locking will be used. *Example:* for allowed file names: ```bash @@ -518,6 +519,15 @@ Creats new empty jsshDB file if not exist. *usage:* jssh_newDB "filename" +*usage:* jssh_newDB_async "filename" + +##### jssh_clearDB +Delete all contents of jsshDB file. + +*usage:* jssh_clearDB "filename" + +*usage:* jssh_clearDB_async "filename" + ##### jssh_checkDB Check if DB name respects the rules mentioned above and print to STDOUT the real/final path to DB file. Used internally by all jssh DB functions, but can also used to get the real filename for a jssh DB. @@ -526,6 +536,8 @@ An error is returned and nothing is printed if the given filename is not valid *usage:* jssh_checkDB "filename" +*usage:* jssh_checkDB_async "filename" + ```bash if file=$(jssh_checkDB somename); then echo "Final filename is ${file}" @@ -1023,5 +1035,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ 0.96-dev2-12-g31b3203 +#### $$VERSION$$ v0.96-dev3-5-g407e147 From 0ad70fda87ced765bb2249b002b3f4b99fbfbb98 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 6 Jun 2020 09:21:56 +0200 Subject: [PATCH 31/53] fix addon dir if BASHBOT_HOME is set --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 15229dc..60a87cc 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-4-gbc74141 +#### $$VERSION$$ v0.96-dev3-6-gd0e9bd8 # # Exit Codes: # - 0 sucess (hopefully) @@ -90,7 +90,7 @@ fi [ -z "${BASHBOT_ETC}" ] && BASHBOT_ETC="$BASHBOT_HOME" [ -z "${BASHBOT_VAR}" ] && BASHBOT_VAR="$BASHBOT_HOME" -ADDONDIR="${BASHBOT_ETC:-./addons}" +ADDONDIR="${BASHBOT_ETC:-.}/addons" RUNUSER="${USER}" # USER is overwritten by bashbot array # OK everthing setup, lest start From d42429efad7d8ffa0a31af8fa0391f3fd07f82b9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 6 Jun 2020 10:16:27 +0200 Subject: [PATCH 32/53] log Errors in telegram response --- bashbot.sh | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 60a87cc..3b2b44c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-6-gd0e9bd8 +#### $$VERSION$$ v0.96-dev3-7-g0ad70fd # # Exit Codes: # - 0 sucess (hopefully) @@ -313,8 +313,9 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then # shellcheck disable=SC2086 res="$(curl -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" - BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" + BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & } #$1 Chat, $2 what , $3 file, $4 URL, $5 caption @@ -327,7 +328,8 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then # shellcheck disable=SC2086 res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } else @@ -343,12 +345,13 @@ else # shellcheck disable=SC2086 res="$(wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - --post-data='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' \ --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" - BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" + BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & } sendUpload() { - sendJson "$1" '"text":"Sorry, wget does not support file upload"' "${MSG_URL}" + printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"ERROR.log" BOTSENT[OK]="false" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } @@ -370,7 +373,7 @@ title2Json(){ [ -n "$3" ] && desc=',"description":"'$(JsonEscape "$3")'"' [ -n "$4" ] && markup=',"parse_mode":"'$(JsonEscape "$4")'"' [ -n "$5" ] && keyboard=',"reply_markup":"'$(JsonEscape "$5")'"' - echo "${title}${caption}${desc}${markup}${keyboard}" + printf "%s\n" "${title}${caption}${desc}${markup}${keyboard}" } # get bot name @@ -695,9 +698,9 @@ start_bot() { local mysleep="100" # ms local addsleep="100" local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))" - [[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log" + [[ "${DEBUG}" == *"debug" ]] && exec &>>"DEBUG.log" [ -n "${DEBUG}" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" - [[ "${DEBUG}" = "xdebug"* ]] && set -x + [[ "${DEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles find "${DATADIR}" -type p -delete find "${DATADIR}" -size 0 -name "*.log" -delete From 313ff03e17e2592246b4730f0c20117f76e917cf Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 6 Jun 2020 14:48:01 +0200 Subject: [PATCH 33/53] place DEBUG anf ERROR log in ./logs if exist --- bashbot.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 3b2b44c..89e824c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-7-g0ad70fd +#### $$VERSION$$ v0.96-dev3-8-gd42429e # # Exit Codes: # - 0 sucess (hopefully) @@ -123,6 +123,11 @@ DATADIR="${BASHBOT_VAR:-.}/data-bot-bash" BLOCKEDFILE="${BASHBOT_VAR:-.}/blocked" COUNTFILE="${BASHBOT_VAR:-.}/count" +LOGDIR="${RUNDIR:-.}/logs" +if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then + LOGDIR="${RUNDIR:-.}" +fi + # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then # BOTTOKEN empty read from file @@ -315,7 +320,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & } #$1 Chat, $2 what , $3 file, $4 URL, $5 caption @@ -329,7 +334,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } else @@ -347,11 +352,11 @@ else --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"ERROR.log" + [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & } sendUpload() { - printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"ERROR.log" + printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" BOTSENT[OK]="false" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } @@ -417,7 +422,7 @@ process_updates() { process_client() { local num="$1" debug="$2" CMD=( ); iQUERY=( ) - [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" + [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"${LOGDIR}/MESSAGE.log" iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" USER[ID]="${UPD["result",${num},"message","from","id"]}" @@ -698,7 +703,7 @@ start_bot() { local mysleep="100" # ms local addsleep="100" local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))" - [[ "${DEBUG}" == *"debug" ]] && exec &>>"DEBUG.log" + [[ "${DEBUG}" == *"debug" ]] && exec &>>"${LOGDIR}/DEBUG.log" [ -n "${DEBUG}" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" [[ "${DEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles @@ -769,7 +774,7 @@ bot_init() { chown -R "$TOUSER" . ./* chmod 711 . chmod -R o-w ./* - chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null + chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" "${LOGDIR}/"*.log 2>/dev/null chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh' -exec chmod u+w \{\} + From 4407ccf8adef6c2187b3fb73f3813beacd25becf Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 6 Jun 2020 23:01:11 +0200 Subject: [PATCH 34/53] no more named pipe for background --- modules/background.sh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index 90ca9e1..d66133f 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-9-g313ff03 # source from commands.sh if you want ro use interactive or background jobs @@ -39,23 +39,30 @@ killproc() { # $1 chatid # $2 program # $3 jobname +# $4 $5 parameters start_back() { - local fifo; fifo="${DATADIR:-.}/$(procname "$1")" - printf '%s\n' "$1:$3:$2" >"${fifo}$3-back.cmd" - start_proc "$1" "$2" "back-$3-" + #local fifo; fifo="${DATADIR:-.}/$(procname "$1")" + printf '%s\n' "$1:$3:$2" >"${DATADIR:-.}/$(procname "$1")$3-back.cmd" + restart_back "$@" +} +restart_back() { + local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-")" + kill_proc "$1" "back-$3-" + nohup bash -c "{ $2 \"$4\" \"$5\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & } # $1 chatid # $2 program # $3 prefix +# $4 $5 parameters start_proc() { [ -z "$2" ] && return [ -x "${2%% *}" ] || return 1 local fifo; fifo="${DATADIR:-.}/$(procname "$1" "$3")" kill_proc "$1" "$3" mkfifo "${fifo}" - nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" + nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & } @@ -120,12 +127,12 @@ job_control() { CHAT="${content%%:*}" job="${content#*:}" proc="${job#*:}" - job="back-${job%:*}-" + job="${job%:*}" fifo="$(procname "${CHAT}" "${job}")" case "$1" in "resumeb"*|"backgr"*) echo "Restart Job: ${proc} ${fifo}" - start_proc "${CHAT}" "${proc}" "${job}" + restart_back "${CHAT}" "${proc}" "${job}" ;; "suspendb"*) echo "Suspend Job: ${proc} ${fifo}" From 515e07de5d61c04619856f05ca48b1a69aa22ad1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 09:10:02 +0200 Subject: [PATCH 35/53] fix suspend/kill background jobs --- modules/background.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index d66133f..2bcf092 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev3-9-g313ff03 +#### $$VERSION$$ v0.96-dev3-10-g4407ccf # source from commands.sh if you want ro use interactive or background jobs @@ -46,9 +46,9 @@ start_back() { restart_back "$@" } restart_back() { - local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-")" + local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-$3-")" kill_proc "$1" "back-$3-" - nohup bash -c "{ $2 \"$4\" \"$5\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & + nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & } From 3f85134d2fb9b4c3583e9efae8c521d3821cbfa6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 09:31:03 +0200 Subject: [PATCH 36/53] simplyfy start_proc, may fix interactive also --- modules/background.sh | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index 2bcf092..060306d 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev3-10-g4407ccf +#### $$VERSION$$ v0.96-dev3-11-g515e07d # source from commands.sh if you want ro use interactive or background jobs @@ -54,16 +54,14 @@ restart_back() { # $1 chatid # $2 program -# $3 prefix -# $4 $5 parameters +# $3 $4 parameters start_proc() { [ -z "$2" ] && return [ -x "${2%% *}" ] || return 1 - local fifo; fifo="${DATADIR:-.}/$(procname "$1" "$3")" - kill_proc "$1" "$3" + local fifo; fifo="${DATADIR:-.}/$(procname "$1")" + kill_proc "$1" mkfifo "${fifo}" - nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" - rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & + nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"$3\" \"$4\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & } @@ -102,7 +100,7 @@ kill_proc() { [ -p "${fifo}" ] && rm -f "${fifo}"; } -# $1 chat +# $1 chatid # $2 message send_interactive() { local fifo; fifo="${DATADIR:-.}/$(procname "$1")" From 601fe0ea51ee3c612acbbcc1bc69f1aabbbcfb81 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 13:30:59 +0200 Subject: [PATCH 37/53] interactive working again, more logging --- bashbot.rc | 19 +++----- bashbot.sh | 11 +++-- commands.sh | 6 ++- examples/background-scripts/run_diskusage.sh | 10 ++++- .../background-scripts/run_filecontent.sh | 9 +++- examples/background-scripts/run_filename.sh | 9 +++- examples/background-scripts/run_notify.sh | 10 ++++- examples/calc.sh | 18 +++++--- examples/notify.sh | 10 ++++- examples/question.sh | 20 ++++++--- modules/background.sh | 5 ++- scripts/interactive.sh.clean | 44 +++++++++++++++++++ 12 files changed, 131 insertions(+), 40 deletions(-) create mode 100755 scripts/interactive.sh.clean diff --git a/bashbot.rc b/bashbot.rc index 565ad94..e2d1569 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # shellcheck disable=SC2009 # shellcheck disable=SC2181 @@ -31,29 +31,22 @@ runcmd="echo Dry run:" # not actived until you edit lines below # runcmd="runuser $runas -s /bin/bash -c " # runasuser with *runuser* # edit the values of the following lines to fit your config: -start="/usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script +start="cd /usr/local/telegram-bot-bash; /usr/local/telegram-bot-bash/bashbot.sh" # location of your bashbot.sh script name='' # your bot name as given to botfather, e.g. mysomething_bot # END Configuration ####################### -lockfile="$(dirname $start)/lockfile" [ "$name" = "" ] && name="$runas" case "$1" in 'start') $runcmd "$start start" # >/dev/null 2>&1 /dev/null 2>&1 - fi ;; 'stop') $runcmd "$start kill" RETVAL=$? - if [ "$RETVAL" = "0" ]; then - rm -f "$lockfile" - fi ;; 'status') ps -f -u "$runas" | grep "$name" | grep -qF "bashbot.sh startbot" @@ -76,10 +69,10 @@ case "$1" in 'suspendback'|'resumeback'|'killback') $runcmd "$start $1" RETVAL=$? - # kill inotifywait from runuser if long running bg scripts use it - KILLINOTIFY="" - if [ "$1" != "resumeback" ] && [ -n "${KILLINOTIFY}" ]; then - kill -9 "$(ps -u "$runas" | grep inotifywait | sed 's/ .*//')" >/dev/null 2>&1 + # kill inotifywait from runuser + if [ "$1" != "resumeback" ]; then + # shellcheck disable=SC2046 + kill -9 $(ps -u "$runas" | grep inotifywait | sed 's/ .*//') >/dev/null 2>&1 fi ;; *) diff --git a/bashbot.sh b/bashbot.sh index 89e824c..7701d8a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-8-gd42429e +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # # Exit Codes: # - 0 sucess (hopefully) @@ -320,8 +320,9 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } #$1 Chat, $2 what , $3 file, $4 URL, $5 caption sendUpload() { @@ -334,8 +335,9 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } else # simple curl or wget call outputs result to stdout @@ -352,8 +354,9 @@ else --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${BOTSENT[OK]}" != "true" ] && printf "%s: %s\n" "$(date)" "${res}" >>"${LOGDIR}/ERROR.log" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & + [ "${BOTSENT[OK]}" != "true" ] &&\ + printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" } sendUpload() { printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" diff --git a/commands.sh b/commands.sh index ffda32b..5628a99 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.96-dev2-9-ged14e85 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # # adjust your language setting here, e.g.when run from other user or cron. @@ -126,7 +126,9 @@ if [ -z "${1}" ] || [[ "${1}" == *"debug"* ]];then unban_chat_member "${CHAT[ID]}" "${USER[ID]}" ;; - *) # forward messages to optional dispatcher + '/'*) # discard all unkown commands + : ;; + *) # forward message to interactive chats _exec_if_function send_interactive "${CHAT[ID]}" "${MESSAGE}" ;; esac diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index a5dec8e..1d897d0 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: run_diskcusage.sh # example for an background job display a system value - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 5964805..80c7260 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,13 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -33,3 +39,4 @@ loop_callback() { } watch_dir_loop "$WATCHDIR" + diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 34bfb50..e6f1434 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,13 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -31,3 +37,4 @@ loop_callback() { } watch_dir_loop "$WATCHDIR" + diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index 9da4cfa..20433d9 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: notify.sh # example for an background job, run with startback notify.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/calc.sh b/examples/calc.sh index d2bd29b..40f4ea4 100755 --- a/examples/calc.sh +++ b/examples/calc.sh @@ -1,11 +1,18 @@ #!/bin/bash # file: calc.sh # example for an interactive chat, run with startproc calc.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 -#### $$VERSION$$ v0.96-dev-7-g0153928 +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + +INPUT="${3:-/dev/stdin}" # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -18,11 +25,11 @@ unset IFS echo 'Starting Calculator ...' echo 'Enter first number.' -read -r A +read -r A <"${INPUT}" echo 'Enter second number.' -read -r B +read -r B <"${INPUT}" echo 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Multiplication" , "Division" , "Cancel" ]' -read -r opt +read -r opt <"${INPUT}" echo -n 'Result: ' case $opt in 'add'* | 'Add'* ) res="$(( A + B ))" ;; @@ -34,3 +41,4 @@ case $opt in esac echo "$res" echo "Bye .." + diff --git a/examples/notify.sh b/examples/notify.sh index 1fc2d54..6254ec9 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -1,10 +1,16 @@ #!/bin/bash # file: notify.sh # example for an background job, run with startback notify.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe/log + # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index 8e850ad..757d4c2 100755 --- a/examples/question.sh +++ b/examples/question.sh @@ -1,11 +1,19 @@ #!/bin/bash # file: question.sh # example for an interactive chat, run with startproc question.sh - +# # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe + +INPUT="${3:-/dev/stdin}" -#### $$VERSION$$ v0.96-dev-7-g0153928 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -18,16 +26,16 @@ unset IFS echo "Why hello there. Would you like some tea (y/n)?" -read -r answer +read -r answer <"${INPUT}" [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." until [ "$SUCCESS" = "y" ] ;do echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' - read -r answer + read -r answer <"${INPUT}" case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; - '') echo "empty answer!" && exit;; + '') echo "empty answer!" && cleanup "$3";; *) SUCCESS=n;; esac done -exit + diff --git a/modules/background.sh b/modules/background.sh index 060306d..e624bca 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev3-11-g515e07d +#### $$VERSION$$ v0.96-dev3-12-g3f85134 # source from commands.sh if you want ro use interactive or background jobs @@ -61,7 +61,8 @@ start_proc() { local fifo; fifo="${DATADIR:-.}/$(procname "$1")" kill_proc "$1" mkfifo "${fifo}" - nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"$3\" \"$4\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & + nohup bash -c "{ $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" + rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & } diff --git a/scripts/interactive.sh.clean b/scripts/interactive.sh.clean new file mode 100755 index 0000000..e6e72c0 --- /dev/null +++ b/scripts/interactive.sh.clean @@ -0,0 +1,44 @@ +#!/bin/bash +# file: interactive.sh +# template for an interactive chat +# test it with: start_proc "${CHAT[ID]}" "./scripts/interactive.sh.clean" +# +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.96-dev3-12-g3f85134 + +###### +# parameters +# $1 $2 args as given to starct_proc chat srcipt arg1 arg2 +# $3 path to named pipe + + + +# adjust your language setting here +# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment +export 'LC_ALL=C.UTF-8' +export 'LANG=C.UTF-8' +export 'LANGUAGE=C.UTF-8' + +unset IFS +# set -f # if you are paranoid use set -f to disable globbing + +####################### +# place your commands here +# +# IMPORTTANT: to read user input from $INPUT +INPUT="${3:-/dev/stdin}" +# +# read -r variable <"${INPUT}" + +# example wait form user input and echo it +echo "Enter a message:" + +read -r test <"${INPUT}" +echo -e "Your Message: ${test}\nbye!" + + +# your commands ends here +###################### + From 5fc4d01a5102f20759255a42c6d7c3de89e1ae0e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 14:04:06 +0200 Subject: [PATCH 38/53] update interactive doc --- doc/3_advanced.md | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 5de1535..1f2bbea 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -21,7 +21,9 @@ user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are * user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *CHATADMIN*." ``` + In addition you can check individual capabilities of users as you must define in the file ```./botacl```: + ```bash # file: botacl # a user not listed here, will return false from 'user_is_allowed' @@ -45,6 +47,7 @@ In addition you can check individual capabilities of users as you must define in *:*:98979695 ``` You must use the function ```user_is_allowed``` to check if a user has the capability to do something. Example: Check if user has capability to start bot. + ```bash case "$MESSAGE" in ################################################ @@ -61,13 +64,37 @@ You must use the function ```user_is_allowed``` to check if a user has the capab **See also [Bashbot User Access Control functions](6_reference.md#User-Access-Control)** ### Interactive Chats -To create interactive chats, write *(or edit the 'exmaples/question.sh' script)* a bash *(or C or python)* script, make it executable -and then use the 'startproc' function to start the script. -The output of the script will be sent to the user and user input will be sent to the script. -To stop the script use the function 'killprog' +Interactive chats are scripts, reading user input and echo data to the user. -The output of the script will be processed by 'send_messages' to enable you to not only send text, but also keyboards, files, locations and more. -Each newline in the output will start an new message to the user, to have line breaks in your message you can use 'mynewlinestartshere'. +To create a new interactive chat script copy 'scripts/interactive.sh.clean' to e.g. 'scripts/mynewinteractive.sh', make it executeable +and then use 'start_proc' function from your bot, it's possible to pass two arguments. You find more examples for interactive scripts in 'examples' + +*usage*: start_proc chat_id script arg1 arg2 + +**Note:** From Version 0.96 on scripts must read user input from '$3' instead of stdin! + +```bash +#!/bin/bash + +###### +# parameters +# $1 $2 args as given to start_proc chat srcipt arg1 arg2 +# $3 path to named pipe + +####################### +# place your commands here +# +INPUT="${3:-/dev/stdin}" # read from stdin if run in terminal + +echo "Enter a message:" +read -r test <"${INPUT}" +echo -e "Your Message: ${test}\nbye!" +``` + +#### message formatting and keyboards + +The output of the script will be processed by 'send_messages', so you can not only send text, but also keyboards, files, locations and more. +Each newline in the output will start an new message to the user, to insert line breaks in your message you must insert ' mynewlinestartshere ' instead of a newline.. To open up a keyboard in an interactive script, print out the keyboard layout in the following way: ```bash @@ -107,7 +134,6 @@ out="Text that will appear mylatstartshere 45 mylongstartshere 45" [[ "$out" != *'in chat'* ]] && out="$out mytextstartshere in chat." echo "$out" ``` -Note: Interactive Chats run independent from main bot and continue running until your script exits or you /cancel if from your Bot. ### Background Jobs @@ -180,5 +206,5 @@ See also [answer_inline_multi, answer_inline_compose](6_reference.md#answer_inli #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-13-g601fe0e From 3fcb8547e8fc50fd0e8365f92e9871d56df712d7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 14:32:18 +0200 Subject: [PATCH 39/53] update doc --- doc/2_usage.md | 52 +++++++++++++++++++++++--------------------- examples/question.sh | 4 ++-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/doc/2_usage.md b/doc/2_usage.md index e772e88..f3df000 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -17,42 +17,44 @@ Have FUN! ### Files ``` . -├── mycommands.sh # THIS is your bot, place logic and commands here! +├── mycommands.sh # THIS is your bot, place logic and commands here! │ -├── mycommands.sh.clean # copy to "mycommands.sh" if you start devloping your bot -├── mycommands.sh.dist # example bot, also used for testing bashbot internally +├── mycommands.sh.clean # copy to "mycommands.sh" if you start devloping your bot +├── mycommands.sh.dist # example bot, also used for testing bashbot internally │ -├── count.jssh # count bashbot usage in jssh key-value store -├── blocked.jssh # list of blocked USER[ID] in jssh key-value store +├── count.jssh # count bashbot usage in jssh key-value store +├── blocked.jssh # list of blocked USER[ID] in jssh key-value store │ -├── bashbot.sh # main bashbot script - DO NOT EDIT! -├── commands.sh # command dispatcher - DO NOT EDIT! -├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh +├── bashbot.sh # main bashbot script - DO NOT EDIT! +├── commands.sh # command dispatcher - DO NOT EDIT! +├── JSON.sh # bashbots JSON parser, see https://github.com/dominictarr/JSON.sh │ -├── scripts # place your bashbot interactive and background scripts here -├── logs # here you'll find ERROR, DEBUG and MESSAGE.log +├── scripts # place your bashbot interactive and background scripts here +│   └── interactive.sh.clean # interactive script template for new scripts │ -├── modules # optional functions, sourced by commands.sh -│   ├── aliases.sh # to disable modules rename them xxx.sh.off +├── logs # here you'll find ERROR, DEBUG and MESSAGE.log +│ +├── modules # optional functions, sourced by commands.sh +│   ├── aliases.sh # to disable modules rename them xxx.sh.off │   ├── answerInline.sh -│   ├── jsshDB.sh # read and store JSON.sh stlye JSON, mandatory -│   ├── background.sh # interactive and background functions +│   ├── jsshDB.sh # read and store JSON.sh stlye JSON, mandatory +│   ├── background.sh # interactive and background functions │   ├── chatMember.sh -│   └── sendMessage.sh # main send message functions, mandatory +│   └── sendMessage.sh # main send message functions, mandatory │ -├── addons # optional addons, disbaled by default -│   ├── example.sh # to enable addons change their XXX_ENABLE to true -│   ├── antiFlood.sh # simple addon taking actions based on # files and text sent to chat +├── addons # optional addons, disbaled by default +│   ├── example.sh # to enable addons change their XXX_ENABLE to true +│   ├── antiFlood.sh # simple addon taking actions based on # files and text sent to chat │   └── xxxxxage.sh │ -├── bashbot.rc # start/stop script if you run basbot as service +├── bashbot.rc # start/stop script if you run basbot as service │ -├── examples # example scripts and configs for bashbot -│   ├── README.md # description of files and examples -│   ├── bash2env.shh # script to convert /bin/bash shebang to /usr/bin/env, see [Security Considerations](../README.md#Security-Considerations) -│   └── bashbot.cron # example crontab +├── examples # example scripts and configs for bashbot +│   ├── README.md # description of files and examples +│   ├── bash2env.sh # script to convert shebang to /usr/bin/env, see [Security Considerations](../README.md#Security-Considerations) +│   └── bashbot.cron # example crontab │ -├── doc # Documentation and License +├── doc # Documentation and License ├── html ├── LICENSE ├── README.html @@ -254,5 +256,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-dev3-4-gbc74141 +#### $$VERSION$$ v0.96-dev3-14-g5fc4d01a diff --git a/examples/question.sh b/examples/question.sh index 757d4c2..a860292 100755 --- a/examples/question.sh +++ b/examples/question.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.96-dev3-12-g3f85134 +#### $$VERSION$$ v0.96-dev3-14-g5fc4d01a ###### # parameters @@ -34,7 +34,7 @@ until [ "$SUCCESS" = "y" ] ;do case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; - '') echo "empty answer!" && cleanup "$3";; + '') echo "empty answer!" && exit ;; *) SUCCESS=n;; esac done From d11598e0829f713a2f84cd4473b24c2395735e34 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 19:06:02 +0200 Subject: [PATCH 40/53] make curl location configurable --- bashbot.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 7701d8a..d42e672 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-12-g3f85134 +#### $$VERSION$$ v0.96-dev3-15-g3fcb854 # # Exit Codes: # - 0 sucess (hopefully) @@ -306,17 +306,19 @@ TIMEOUT="${BASHBOT_TIMEOUT}" [[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20" if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then + [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ # shellcheck disable=SC2086 - curl -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" + "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } # usage: sendJson "chat" "JSON" "URL" sendJson(){ local chat=""; [ -n "${1}" ] && chat='"chat_id":'"${1}"',' # shellcheck disable=SC2086 - res="$(curl -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ + res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}"\ + -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" @@ -329,10 +331,12 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ "$#" -lt 4 ] && return if [ -n "$5" ]; then # shellcheck disable=SC2086 - res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )" + res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ + -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )" else # shellcheck disable=SC2086 - res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" + res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ + -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & From 720dc597138be3538fb58813042137a5cbf507a0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 19:47:46 +0200 Subject: [PATCH 41/53] update environment doc --- doc/.4_expert.md.swp | Bin 0 -> 16384 bytes doc/4_expert.md | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 doc/.4_expert.md.swp diff --git a/doc/.4_expert.md.swp b/doc/.4_expert.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..67ca0d9b87936d76e780befb8bff4ea4f7cf8e2f GIT binary patch literal 16384 zcmeI3U5q5xRmThBU^|K9*p~QEr0BY5qn;gRx@Ttnv0i13W@l$V$nMzMnXv({Gp_Es z-CdgMs-|w$^t1pW2ojP_gn}PpgOGVhKqv|#i4x@nh!O-*L>`bJK}djPAtWafM(_|n z5QpD6Rn^|ITf0nq8hTFk@iG zz>I+z12YC@49pmqF)(A`{}Kb);12JXQRs(DrEZkpKXl~#`{m~&<@v26*WWC!x61RM zJ#zgW<*{FWKTr-(jZ>bUea#q{F)(9b#=wk$83Qv0W(>?2m@zP8V8+0Vff)lo00w-X zuRlP)L)nP){J+}&|I59e_jll1;0s_5)WFN{^}I9SFYocZzW|>Cm%u!zgYUk_^L`C{ z6#NAEaqvFy#=AZ5Rq!?N=b#TPxC)x!K5!3sCwT2$p7&SaGvE)wDp&!ZzuWUZ2R;Gr z0(XMf@1!3PfY*P-^ZpeK!JF^&yzhbk1b+kGxC7b1Z-M850||)1kAZu^cPUc*6Zl8) zBKRoyRq%7*{oq>^NxlhQ1{cAbaP~d$8u)AQ4e&eQMeqwiINb*)e4cymInU)mVj4|( z?}7Ip8fn|O%uh3qYjV!JlqP)>Cqt9OCU@41m7tMMScfxL-`dOI7Xz%y!`Fh7FBKRW(m$5>0(tKB_kq?Jful1@2pNrLMc*G|JhhP*Zi zGsCcE-;Z+Zq^%rvk|;`sjBTPY<}?C!Xs9%_RlReVt4Z^?-4D*!_=ddHoZ<&cEjiO{ zg|W(GmfEI<=6XdN4S6p`4&~rbA1=T3?QkdT9qGg6+>QiEzi;D=8ML<&pVw?eu=BMz zX(hp^X1H;zd2A0dRkPAk2vA8;*9?8SMtwRkrEQcmRP?1hOwG92vTdFDlZ|9fEJ$r{ zqvF03M%I*P3oGTo_;D~1^7a!_+8gq4^HIBJ%ELIEnEjT`@`28@jrYSeiIu@llIpp) zQoax?&Y11Mx-hkYawYaW#fw3Z3x&a%&}rMa8`HYv635E@C>oj8Xfo!ommnvm9!0ba zvxeEU`@Esl;~ggFI8k?IkT@5%Agt^Nv!n|jGpAXq!Yp0AxG2u$8#i*9?Hep@KWt+j zDVA@8Tzc<_=eJS4VYYin$iE~WMJBa{K?Tx=8}uEi&@vggLVlnFxQ2uPa|4@Tn90C8 zz|@e0rGHTI)LGssh`?M&DoL{?^}@a17I zZ1;#tBZ5|DhDo}oLQ1SK?Iew1pzS;B78V-b^~7bIlnyd$Ts{~iX{OBsbPUA1arb^6 zWnpVXi+0aX{Wc;v`hd?^X2+UAn(T+dM%Xcf5t<+1v z`Ul<3n{k~Jp0|zHZuFBZBtAK5WB%=xmwrY5ykuVe-A}&$rQej_&-3TwAJfY|6suSa z<-2ow?q+`R?3s)F-B?~RlY^DB9IQV2$U`^tOB>6V4qaJZn;fh^bWu9-w#zr}#8e9H znO-Lr)D-IqF=-R{ncp}>4KdbbDAmPkT^WAQnf1wx2r+A0>sPOuh55%Nky)?{KY&ReR$qI3j4J?`kU5F-}Lj79m zx5D-zOIg6t;8C>;V-qB~xTc!Z&i19Hl}BVo)pRk8Djw*pBPaH6`ZzW#EK5x4$8JDp zaTbjhMOr@%)a}bPNzBvP49gQ@#cOeBu~B=_wgc4zyBzjdPcZ=r8)8`M=4ygMxlmE^ ztS&rfDw#yuvu3Ms609jPLFfh%jwVw2`+gW{)X}w9IG(b4T^^vixN4Ss^NXgRq++oR z^UP&*N)q#QxEDS<-^;RrJJ)OuIcjwEN+U_TdfZegPLutS0Qh_G+LJ&~Wgq3T?bNV{ zcH;3ERA%5m6Kx$KnA{==@@-Z`p1b$3jk(85HS&^AOh~-bt9K zUt8O}yt8(B<9uM_-JPw5idLVx%%$CvFRt&dUAuOE>Kc7Z;JeiryB9ZZHN**5ivAgX za;@s0OyQ~~JjzlLncx_o#jNeDczv~$#ACz6J~bQa;7Y!$}JGhAtBjmj_FRJw-oiT4J!4vFAAsKnFM>JnMfUuE1bzwZ zfNih@q!w@o@WA)k|9=O(01Wsxd;eE~)B|1s&x3970r2&^s84{eg0Fzz1`h%Q{)9Tg z7r<|T9(Wv_2ls%x!Cl}_sULh1d<=x(r@;N-KJYrVgztcFgO|a_K?XhyeiFPJybJt2 z^@MMNzXjg_e++&dbirkC3H&IyAKV9Cga0psKLalVosV)ZW?%mg8IT>iId$r(jT>86 zpV&Ng%Iq&U9(zRogUr&SOWoy_htE8=dRF|LX3uO&o#28cSUxFHfc@mN^AGP1%HQhf zMZ+gkyKmNleV=4JkkIh#{Fz<9Jl1v|(U%qZZ5DINk`*_Ux!Hxatt%Iv*xr3=?S`o} zn*9+)gVoyc>l@qa)7R(BW|G-+x@ZK%eGP_^OgOdxC}s~rmpw>FbCN<+#M0G=(uZb? z7ubTVis+ih{Qz`Wxs48b{lBln%2)@PuQ}s%k6mtp(6LaRG>CPU{jMa+Y%KOHn+)0Z zvKKb>WB;qF&VC^+YA;h0Th+m&t|Nsvtv()~Dp{Ylu9#Yr)vxIWNfK4nyc0Old4F3i z)dHF}Yaj0IZ8BI*bU_j;?*W?$_9TZaLrQL>ha ztN#u4OB*@L!lF9JQ5vS}BV!IU&puwZg_nhe!Y>b{o8vU7NM&XF??~2du&8-#kv~^e zygvJT@SJTwo)_YXRfLwf5@yXFE7h3T9P@+8xam<=0%Z1CAQP%)_rpO zvi3ViS41fPAnfN9^Q5DdiGpeZsd-AJP?PJJ z!i4ps5e1wMwUy}`CuNFq>ZQaZ^MFo)rI^7cin;a!NphuAiESkF?1r`MB3oh|#i!g) z@vgXOaV$sD>FCy@3YmW3Q+T1WQ1(^SR4A&H3zV`}7|(I$pT4}YZK%jYJ!O0n?_t;K z;&Wt`E82~$lg9{EyAx)zXje|8v}@;Sbi!0>yJ{xK?T*hS{MIW)2}P|H^(%h7of^(r4f!v6ZsjcdDWH!g3T zuPrUPq`l`JUh1*&)CX7^(d0-LsbX@piRmKjklGnlvT+*^0W@>ilHzZtDEqTul=7u%mh53p`s7IV;AN3sFA?&~%Pc*W zklRmucw>`LGYP&Tqde6bKk_Ue64j+nC}F>Nq?Uy|S`fRFCm2(`Rn7n*A;nediwJ^) z9;+T$3g~c7F zQN|bHq{>YLUGOIHNoBcJe8R9yt*X}cM-3*jfiE_ZyNX0wBa(I}H~WxI(&OHL0Tqdj AqW}N^ literal 0 HcmV?d00001 diff --git a/doc/4_expert.md b/doc/4_expert.md index f40e45d..ac9d486 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -277,6 +277,26 @@ Uses given URL instead of offical telegram API URL, useful if you have your own ``` ##### BASHBOT_TOKEN +If BASHBOT_TOKEN is set, bashbot assumes you know what you are doing and skips environment validation and +uses the value of BASHBOT_TOKEN as bot token. + +I recommend to run 'bashbot.sh init' at least one time without BASHBOT_TOKEN set to validate and setup +the environment. Afterwards you can delete the token file and provide the bot token in BASHBOT_TOKEN. + +##### BASHBOT_CURL_ARGS +The value of BASHBOT_CURL_ARGS is passed to every curl execution. +```bash + # use socks gateway on localhost + export BASHBOT_CURL_ARGS="--socks5-hostname localhost" +``` + +##### BASHBOT_CURL +If BASHBOT_CURL is not set your systems default curl is used. If you want to use an alternative curl executable +set BASHBOT_CURL to point to it. +```bash + # use curl from /usr/local/bin + export BASHBOT_CURL="/usr/local/bin/mycurl" +``` ##### BASHBOT_WGET Bashbot uses ```curl``` to communicate with telegram server. if ```curl``` is not availible ```wget``` is used. @@ -290,6 +310,20 @@ If 'BASHBOT_WGET' is set to any value (not undefined or not empty) wget is used ``` +##### BASHBOT_TIMEOUT +Bashbot uses a default timeout of 20 seconds for curl and wget. If you want a different timeout, set +BASHBOT_TIMEOUT to a numeric value between 1 and 999. Any non numeric or negative value is ignored. +```bash + # set timeout to 100 seconds + export BASHBOT_TIMEOUT="100" + + # 100s is not a numbers + export BASHBOT_TIMEOUT="100s" # wrong, default timeout is used + + # -100 is not between 1 and 999s + export BASHBOT_TIMEOUT="-100" # wrong, default timeout is used +``` + ##### BASHBOT_SLEEP Instead of polling permanently or with a fixed delay, bashbot offers a simple adaptive polling. If messages are recieved bashbot polls with no dealy. If no messages are availible bashbot add 100ms delay @@ -348,5 +382,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-16-gd11598e From 3e4008844f1a115bf34e97d96ba8714ed2a6a355 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 7 Jun 2020 19:49:02 +0200 Subject: [PATCH 42/53] rm vi swap file --- doc/.4_expert.md.swp | Bin 16384 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/.4_expert.md.swp diff --git a/doc/.4_expert.md.swp b/doc/.4_expert.md.swp deleted file mode 100644 index 67ca0d9b87936d76e780befb8bff4ea4f7cf8e2f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3U5q5xRmThBU^|K9*p~QEr0BY5qn;gRx@Ttnv0i13W@l$V$nMzMnXv({Gp_Es z-CdgMs-|w$^t1pW2ojP_gn}PpgOGVhKqv|#i4x@nh!O-*L>`bJK}djPAtWafM(_|n z5QpD6Rn^|ITf0nq8hTFk@iG zz>I+z12YC@49pmqF)(A`{}Kb);12JXQRs(DrEZkpKXl~#`{m~&<@v26*WWC!x61RM zJ#zgW<*{FWKTr-(jZ>bUea#q{F)(9b#=wk$83Qv0W(>?2m@zP8V8+0Vff)lo00w-X zuRlP)L)nP){J+}&|I59e_jll1;0s_5)WFN{^}I9SFYocZzW|>Cm%u!zgYUk_^L`C{ z6#NAEaqvFy#=AZ5Rq!?N=b#TPxC)x!K5!3sCwT2$p7&SaGvE)wDp&!ZzuWUZ2R;Gr z0(XMf@1!3PfY*P-^ZpeK!JF^&yzhbk1b+kGxC7b1Z-M850||)1kAZu^cPUc*6Zl8) zBKRoyRq%7*{oq>^NxlhQ1{cAbaP~d$8u)AQ4e&eQMeqwiINb*)e4cymInU)mVj4|( z?}7Ip8fn|O%uh3qYjV!JlqP)>Cqt9OCU@41m7tMMScfxL-`dOI7Xz%y!`Fh7FBKRW(m$5>0(tKB_kq?Jful1@2pNrLMc*G|JhhP*Zi zGsCcE-;Z+Zq^%rvk|;`sjBTPY<}?C!Xs9%_RlReVt4Z^?-4D*!_=ddHoZ<&cEjiO{ zg|W(GmfEI<=6XdN4S6p`4&~rbA1=T3?QkdT9qGg6+>QiEzi;D=8ML<&pVw?eu=BMz zX(hp^X1H;zd2A0dRkPAk2vA8;*9?8SMtwRkrEQcmRP?1hOwG92vTdFDlZ|9fEJ$r{ zqvF03M%I*P3oGTo_;D~1^7a!_+8gq4^HIBJ%ELIEnEjT`@`28@jrYSeiIu@llIpp) zQoax?&Y11Mx-hkYawYaW#fw3Z3x&a%&}rMa8`HYv635E@C>oj8Xfo!ommnvm9!0ba zvxeEU`@Esl;~ggFI8k?IkT@5%Agt^Nv!n|jGpAXq!Yp0AxG2u$8#i*9?Hep@KWt+j zDVA@8Tzc<_=eJS4VYYin$iE~WMJBa{K?Tx=8}uEi&@vggLVlnFxQ2uPa|4@Tn90C8 zz|@e0rGHTI)LGssh`?M&DoL{?^}@a17I zZ1;#tBZ5|DhDo}oLQ1SK?Iew1pzS;B78V-b^~7bIlnyd$Ts{~iX{OBsbPUA1arb^6 zWnpVXi+0aX{Wc;v`hd?^X2+UAn(T+dM%Xcf5t<+1v z`Ul<3n{k~Jp0|zHZuFBZBtAK5WB%=xmwrY5ykuVe-A}&$rQej_&-3TwAJfY|6suSa z<-2ow?q+`R?3s)F-B?~RlY^DB9IQV2$U`^tOB>6V4qaJZn;fh^bWu9-w#zr}#8e9H znO-Lr)D-IqF=-R{ncp}>4KdbbDAmPkT^WAQnf1wx2r+A0>sPOuh55%Nky)?{KY&ReR$qI3j4J?`kU5F-}Lj79m zx5D-zOIg6t;8C>;V-qB~xTc!Z&i19Hl}BVo)pRk8Djw*pBPaH6`ZzW#EK5x4$8JDp zaTbjhMOr@%)a}bPNzBvP49gQ@#cOeBu~B=_wgc4zyBzjdPcZ=r8)8`M=4ygMxlmE^ ztS&rfDw#yuvu3Ms609jPLFfh%jwVw2`+gW{)X}w9IG(b4T^^vixN4Ss^NXgRq++oR z^UP&*N)q#QxEDS<-^;RrJJ)OuIcjwEN+U_TdfZegPLutS0Qh_G+LJ&~Wgq3T?bNV{ zcH;3ERA%5m6Kx$KnA{==@@-Z`p1b$3jk(85HS&^AOh~-bt9K zUt8O}yt8(B<9uM_-JPw5idLVx%%$CvFRt&dUAuOE>Kc7Z;JeiryB9ZZHN**5ivAgX za;@s0OyQ~~JjzlLncx_o#jNeDczv~$#ACz6J~bQa;7Y!$}JGhAtBjmj_FRJw-oiT4J!4vFAAsKnFM>JnMfUuE1bzwZ zfNih@q!w@o@WA)k|9=O(01Wsxd;eE~)B|1s&x3970r2&^s84{eg0Fzz1`h%Q{)9Tg z7r<|T9(Wv_2ls%x!Cl}_sULh1d<=x(r@;N-KJYrVgztcFgO|a_K?XhyeiFPJybJt2 z^@MMNzXjg_e++&dbirkC3H&IyAKV9Cga0psKLalVosV)ZW?%mg8IT>iId$r(jT>86 zpV&Ng%Iq&U9(zRogUr&SOWoy_htE8=dRF|LX3uO&o#28cSUxFHfc@mN^AGP1%HQhf zMZ+gkyKmNleV=4JkkIh#{Fz<9Jl1v|(U%qZZ5DINk`*_Ux!Hxatt%Iv*xr3=?S`o} zn*9+)gVoyc>l@qa)7R(BW|G-+x@ZK%eGP_^OgOdxC}s~rmpw>FbCN<+#M0G=(uZb? z7ubTVis+ih{Qz`Wxs48b{lBln%2)@PuQ}s%k6mtp(6LaRG>CPU{jMa+Y%KOHn+)0Z zvKKb>WB;qF&VC^+YA;h0Th+m&t|Nsvtv()~Dp{Ylu9#Yr)vxIWNfK4nyc0Old4F3i z)dHF}Yaj0IZ8BI*bU_j;?*W?$_9TZaLrQL>ha ztN#u4OB*@L!lF9JQ5vS}BV!IU&puwZg_nhe!Y>b{o8vU7NM&XF??~2du&8-#kv~^e zygvJT@SJTwo)_YXRfLwf5@yXFE7h3T9P@+8xam<=0%Z1CAQP%)_rpO zvi3ViS41fPAnfN9^Q5DdiGpeZsd-AJP?PJJ z!i4ps5e1wMwUy}`CuNFq>ZQaZ^MFo)rI^7cin;a!NphuAiESkF?1r`MB3oh|#i!g) z@vgXOaV$sD>FCy@3YmW3Q+T1WQ1(^SR4A&H3zV`}7|(I$pT4}YZK%jYJ!O0n?_t;K z;&Wt`E82~$lg9{EyAx)zXje|8v}@;Sbi!0>yJ{xK?T*hS{MIW)2}P|H^(%h7of^(r4f!v6ZsjcdDWH!g3T zuPrUPq`l`JUh1*&)CX7^(d0-LsbX@piRmKjklGnlvT+*^0W@>ilHzZtDEqTul=7u%mh53p`s7IV;AN3sFA?&~%Pc*W zklRmucw>`LGYP&Tqde6bKk_Ue64j+nC}F>Nq?Uy|S`fRFCm2(`Rn7n*A;nediwJ^) z9;+T$3g~c7F zQN|bHq{>YLUGOIHNoBcJe8R9yt*X}cM-3*jfiE_ZyNX0wBa(I}H~WxI(&OHL0Tqdj AqW}N^ From 8792e8353c54bb12eedb6a6c96211e3cf6716d8c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 10:55:14 +0200 Subject: [PATCH 43/53] update gitignore --- .gitignore | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1912318..d8956d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,13 @@ *~ /.github/ -/count -/token +/count* +/token* +/bocked* *.save *.log +*.swp +*.swo +/log/* /JSON.sh/ /data-bot-bash/ /DIST/ From bd22e2aa1795faee3e457fbf5f9dc1e30d588402 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 12:04:48 +0200 Subject: [PATCH 44/53] fix make-standalone, add minimized standalone version --- bashbot.sh | 2 +- dev/make-standalone.sh | 21 +++++++++------------ doc/4_expert.md | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d42e672..65a17dc 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-15-g3fcb854 +#### $$VERSION$$ v0.96-dev3-19-g8792e83 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 916c1fe..7ad64a0 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$$ 0.96-dev2-6-gda98b09 +#### $$VERSION$$ v0.96-dev3-19-g8792e83 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -20,16 +20,8 @@ fi DISTDIR="./STANDALONE/${DISTNAME}" DISTFILES="bashbot.sh commands.sh mycommands.sh modules LICENSE README.txt token count botacl botadmin" -# run tests first! - -for test in dev/all-test*.sh -do - [ ! -x "${test}" ] && continue - if ! "${test}" ; then - echo "Test ${test} failed, can't create standalone!" - exit 1 - fi -done +# run pre_commit on files +dev/hooks/pre-commit.sh # create dir for distribution and copy files mkdir -p "${DISTDIR}" 2>/dev/null @@ -41,7 +33,7 @@ cd "${DISTDIR}" || exit 1 # here the magic starts # create all in one bashbot.sh file -echo "OK, noe lets do the magic ..." +echo "OK, now lets do the magic ..." echo " ... create unified commands.sh" { @@ -82,6 +74,11 @@ chmod +x bashbot.sh rm -rf modules +echo "Create minimized Version of bashbot.sh and commands.sh" +sed -E -e '/(shellcheck)|(#!\/bin\/bash)/! s/^[[:space:]]*#.*//' -e 's/^[[:space:]]*//' -e '/^$/d' bashbot.sh > bashbot.sh.min +sed -E -e '/(shellcheck)|(#!\/bin\/bash)/! s/^[[:space:]]*#.*//' -e 's/^[[:space:]]*//' -e '/^$/d' commands.sh > commands.sh.min +chmod +x bashbot.sh.min + echo "Done!" cd .. || exit 1 diff --git a/doc/4_expert.md b/doc/4_expert.md index ac9d486..db7e8a7 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -382,5 +382,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.96-dev3-16-gd11598e +#### $$VERSION$$ v0.96-dev3-17-g720dc59 From 6f86036809d99e19f061d600d32185de2ba79357 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 12:58:36 +0200 Subject: [PATCH 45/53] more sendJson error handling, rearrange source modules --- bashbot.sh | 81 ++++++++++++++++++++++++++--------------- dev/hooks/pre-commit.sh | 4 +- dev/make-standalone.sh | 6 +-- modules/jsonDB.sh | 10 ++--- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 65a17dc..173bc77 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-19-g8792e83 +#### $$VERSION$$ v0.96-dev3-20-gbd22e2a # # Exit Codes: # - 0 sucess (hopefully) @@ -21,7 +21,7 @@ # - 4 unkown command # - 5 cannot connect to telegram bot # - 6 mandatory module not found -# shellcheck disable=SC2140 +# shellcheck disable=SC2140,SC2031,SC2120,SC1091 # are we runnig in a terminal? if [ -t 1 ] && [ -n "$TERM" ]; then @@ -106,15 +106,28 @@ if [ ! -w "." ]; then ls -ld . fi -#jsonDB is now mandatory -if [ ! -r "${MODULEDIR:-.}"/jsonDB.sh ]; then - echo -e "${RED}ERROR: Mandatory module ${MODULEDIR:-.}/jsonDB.sh is missing or not readable!" - exit 6 -fi +############### +# load modules +for modules in "${MODULEDIR:-.}"/*.sh ; do + # shellcheck source=./modules/aliases.sh + if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi +done + # shellcheck source=./modules/jsonDB.sh source "${MODULEDIR:-.}"/jsonDB.sh + + ##################### +# BASHBOT INTERNAL functions +# + +#jsonDB is now mandatory +if ! _is_function jssh_newDB ; then + echo -e "${RED}ERROR: Mandatory module jsonDB is missing or not readable!" + exit 6 +fi + # Setup and check environment if BOTTOKEN is NOT set TOKENFILE="${BASHBOT_ETC:-.}/token" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" @@ -231,7 +244,7 @@ declare -Ax UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO export res CAPTION -#################EW# +################## # read commamds file if we are not sourced COMMANDS="${BASHBOT_ETC:-.}/commands.sh" if [ "${SOURCE}" != "yes" ]; then @@ -244,15 +257,9 @@ if [ "${SOURCE}" != "yes" ]; then source "${COMMANDS}" "source" fi -############### -# load -for modules in "${MODULEDIR:-.}"/*.sh ; do - # shellcheck source=./modules/aliases.sh - if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi -done ################# -# BASHBOT INTERNAL functions +# BASHBOT COMMON functions # $1 URL, $2 filename in DATADIR # outputs final filename download() { @@ -320,11 +327,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}"\ -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & - [ "${BOTSENT[OK]}" != "true" ] &&\ - printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" + sendJsonResult "${res}" "sendJson (curl)" "$@" } #$1 Chat, $2 what , $3 file, $4 URL, $5 caption sendUpload() { @@ -338,10 +341,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" fi - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & - [ "${BOTSENT[OK]}" != "true" ] &&\ - printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" + sendJsonResult "${res}" "sendUpload (curl)" "$@" } else # simple curl or wget call outputs result to stdout @@ -356,11 +356,7 @@ else # shellcheck disable=SC2086 res="$(wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - --post-data='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' \ --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${res}")" - BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${res}")" - [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" & - [ "${BOTSENT[OK]}" != "true" ] &&\ - printf "%s: CHAT[ID]=%s ACTION=%s\nRESULT=%s\n" "$(date)" "${1}" "${2}" "${res}" >>"${LOGDIR}/ERROR.log" + sendJsonResult "${res}" "sendJson (wget)" "$@" } sendUpload() { printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" @@ -368,11 +364,36 @@ else [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } fi +# internal function for sendJson sendUplaod etc +# checks and processes resukt +# $1 result +# $2 function +# $3 .. $n original arguments +# first argument ($3) is Chat_id +sendJsonResult(){ + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${3}")" + if [ "${BOTSENT[OK]}" = "true" ]; then + BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${3}")" + [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "${@:2}" + return + else + if [ "${res}" != "" ]; then + BOTSENT[ERROR]="$(JsonGetValue '"error_code"' <<< "${3}")" + BOTSENT[DESCRIPTION]="$(JsonGetValue '"description"' <<< "${3}")" + else + BOTSENT[ERROR]="999" + BOTSENT[DESCRIPTION]="Timeout or broken/no connection" + fi + printf "%s: CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n"\ + "$(date)" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" + fi +} + # escape / remove text charaters for json strings, eg. " -> \" # $1 string # output escaped string -JsonEscape() { +JsonEscape(){ sed 's/\([-"`´,§$%&/(){}#@!?*.]\)/\\\1/g' <<< "$1" } diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 3b23e11..95d024a 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.96-dev-7-g0153928 +#### $$VERSION$$ v0.96-dev3-20-gbd22e2a ############ # NOTE: you MUST run install-hooks.sh again when updating this file! @@ -30,7 +30,7 @@ fi # run shellcheck before commit set +f -FILES="$(find ./* -name '*.sh' | grep -v 'DIST\/' )" +FILES="$(find ./* -name '*.sh' | grep -v 'DIST\/' | grep -v 'STANDALONE\/')" set -f FILES="${FILES} $(sed '/^#/d' <"dev/shellcheck.files")" if [ "$FILES" != "" ]; then diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 7ad64a0..ef25f60 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.96-dev3-19-g8792e83 +#### $$VERSION$$ v0.96-dev3-20-gbd22e2a # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -18,7 +18,7 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE/${DISTNAME}" -DISTFILES="bashbot.sh commands.sh mycommands.sh modules LICENSE README.txt token count botacl botadmin" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh modules LICENSE README.txt token count botacl botadmin" # run pre_commit on files dev/hooks/pre-commit.sh @@ -65,7 +65,7 @@ echo " ... create unified bashbot.sh" # last tail of commands.sh printf '\n##############################\n# bashbot internal functions starts here ...\n\n' - sed -n '/BASHBOT INTERNAL functions/,$ p' bashbot.sh | head -n -4 + sed -n '/BASHBOT INTERNAL functions/,$ p' bashbot.sh } >>$$bashbot.sh diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index c117783..a77189e 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev3-4-gbc74141 +#### $$VERSION$$ v0.96-dev3-20-gbd22e2a # # source from commands.sh to use jsonDB functions # @@ -198,13 +198,13 @@ fi # print ARRAY content to stdout instead of file # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront -alias jssh_printDB_async=jssh_printDB +jssh_printDB_async() { jssh_printDB "$@"; } jssh_printDB() { Array2Json "$1" } # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' -alias jssh_newDB_async=jssh_newDB +jssh_newDB_async() { jssh_newDB "$@"; } jssh_newDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 @@ -214,7 +214,7 @@ jssh_newDB() { # $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..' # returns real path to DB file if everything is ok -alias jssh_checkDB_async=jssh_checkDB +jssh_checkDB_sync() { jssh_checkDB "$@"; } jssh_checkDB(){ local DB [ -z "$1" ] && return 1 @@ -264,7 +264,7 @@ jssh_updateDB_async() { fi } -alias jssh_insertDB_async=jssh_insertKeyDB_async +jssh_insertDB_async() { jssh_insertKeyDB "$@"; } jssh_insertKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local key="$1" value="$2" From 19c45c4771e47ea790e37ceebfcc709d7a98eda0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 15:48:07 +0200 Subject: [PATCH 46/53] fix JSONS.sh download, obfuscate joke hack --- bashbot.sh | 4 ++-- dev/make-standalone.sh | 4 ++-- dev/obfuscate.sh | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100755 dev/obfuscate.sh diff --git a/bashbot.sh b/bashbot.sh index 173bc77..dfb85c1 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-20-gbd22e2a +#### $$VERSION$$ v0.96-dev3-21-g6f86036 # # Exit Codes: # - 0 sucess (hopefully) @@ -820,7 +820,7 @@ JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" if [ ! -f "${JSONSHFILE}" ]; then echo "Seems to be first run, Downloading ${JSONSHFILE}..." - [[ "${JSONSHFILE}" = "${RUNDIR}/JSON.sh/JSON.sh" ]] && mkdir "JSON.sh" 2>/dev/null && chmod +w "JSON.sh" + mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh" getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}" chmod +x "${JSONSHFILE}" fi diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index ef25f60..4080d15 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.96-dev3-20-gbd22e2a +#### $$VERSION$$ v0.96-dev3-21-g6f86036 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -18,7 +18,7 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE/${DISTNAME}" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh modules LICENSE README.txt token count botacl botadmin" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules LICENSE README.txt token count botacl botadmin" # run pre_commit on files dev/hooks/pre-commit.sh diff --git a/dev/obfuscate.sh b/dev/obfuscate.sh new file mode 100755 index 0000000..9f2da99 --- /dev/null +++ b/dev/obfuscate.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# shellcheck disable=SC2028,2016 +# joke hack to obfuscate bashbot.min.sh + +infile="bashbot.sh" +outfile="./bashbot.obf.sh" + +[ ! -f "${infile}" ] && echo "Hey, this is a joke hack to obfuscate ${infile}, copy me to STANDANLONE first" && exit + +{ +echo '#!/bin/bash' +echo 'b="./bashbot";h="$PWD";cd "$(mktemp -d)"||exit' +echo 'printf '"'%s\n'"' '"'$(gzip -9 "$b";export BASHBOT_HOME="$h";chmod +x "$b";"$b" "$@";b="$(pwd)";cd ..;#rm -rf "$b"' +} >"${outfile}" + +chmod +x "${outfile}" +ls -l "${outfile}" +echo "Try to run ${outfile} init ;-)" From 02e776c426df7b7a407dd5440221abbb089a2634 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 15:50:07 +0200 Subject: [PATCH 47/53] rm tempdir after run --- dev/obfuscate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/obfuscate.sh b/dev/obfuscate.sh index 9f2da99..c4ca168 100755 --- a/dev/obfuscate.sh +++ b/dev/obfuscate.sh @@ -10,7 +10,7 @@ outfile="./bashbot.obf.sh" { echo '#!/bin/bash' echo 'b="./bashbot";h="$PWD";cd "$(mktemp -d)"||exit' -echo 'printf '"'%s\n'"' '"'$(gzip -9 "$b";export BASHBOT_HOME="$h";chmod +x "$b";"$b" "$@";b="$(pwd)";cd ..;#rm -rf "$b"' +echo 'printf '"'%s\n'"' '"'$(gzip -9 "$b";export BASHBOT_HOME="$h";chmod +x "$b";"$b" "$@";b="$(pwd)";cd ..;rm -rf "$b"' } >"${outfile}" chmod +x "${outfile}" From b189191b85f57755cf1bcf992a753658958f6526 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 16:04:45 +0200 Subject: [PATCH 48/53] fix init with BASHBOT_HOME --- bashbot.sh | 3 ++- dev/make-standalone.sh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index dfb85c1..67daaf0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-21-g6f86036 +#### $$VERSION$$ v0.96-dev3-23-g02e776c # # Exit Codes: # - 0 sucess (hopefully) @@ -773,6 +773,7 @@ start_bot() { # initialize bot environment, user and permissions bot_init() { + [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 local DEBUG="$1" # upgrade from old version local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 4080d15..353f21b 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.96-dev3-21-g6f86036 +#### $$VERSION$$ v0.96-dev3-22-g19c45c4 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script From 9b138eecbdf3d7291f7189b3191f8958520cc890 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 17:36:42 +0200 Subject: [PATCH 49/53] fix/extend sendJson error handling --- bashbot.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 67daaf0..06ca514 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-23-g02e776c +#### $$VERSION$$ v0.96-dev3-24-gb189191 # # Exit Codes: # - 0 sucess (hopefully) @@ -379,7 +379,8 @@ sendJsonResult(){ else if [ "${res}" != "" ]; then BOTSENT[ERROR]="$(JsonGetValue '"error_code"' <<< "${3}")" - BOTSENT[DESCRIPTION]="$(JsonGetValue '"description"' <<< "${3}")" + BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${3}")" + BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${3}")" else BOTSENT[ERROR]="999" BOTSENT[DESCRIPTION]="Timeout or broken/no connection" From de745719dc7d80dc835650b958d6bec0052102cd Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 20:51:15 +0200 Subject: [PATCH 50/53] error handling: retry or not to retry? --- bashbot.sh | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 06ca514..ab8d470 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-24-gb189191 +#### $$VERSION$$ v0.96-dev3-25-g9b138ee # # Exit Codes: # - 0 sucess (hopefully) @@ -364,29 +364,51 @@ else [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } fi -# internal function for sendJson sendUplaod etc # checks and processes resukt # $1 result # $2 function # $3 .. $n original arguments # first argument ($3) is Chat_id sendJsonResult(){ - BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${3}")" + BOTSENT=( ) + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" if [ "${BOTSENT[OK]}" = "true" ]; then - BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${3}")" + BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${1}")" [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "${@:2}" return + # hot path everthing OK! else + # oops something went wrong! if [ "${res}" != "" ]; then - BOTSENT[ERROR]="$(JsonGetValue '"error_code"' <<< "${3}")" - BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${3}")" - BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${3}")" + BOTSENT[ERROR]="$(JsonGeOtValue '"error_code"' <<< "${1}")" + BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")" + BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")" else BOTSENT[ERROR]="999" BOTSENT[DESCRIPTION]="Timeout or broken/no connection" fi - printf "%s: CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n"\ - "$(date)" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" + # log error + printf "%s: RESULT=%s CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n" "$(date)"\ + "${BOTSENT[OK]}" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" + # retry once if we are throttled + [ -n "${BOTSEND_RETRY}" ] && return + if [ -n "${BOTSENT[RETRY]}" ]; then + BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))" + echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." >>"${LOGDIR}/ERROR.log" + fi + # timeout or failed connection + if [ "${BOTSENT[ERROR]}" == "999" ];then + # check if default curl and args are OK + if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then + echo "BASHBOT is blocked!" >>"${LOGDIR}/ERROR.log" + return + fi + # retry without custom curl and args + if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then + BOTSEND_RETRY="2" + echo "Timeout or broken connection, retry ${2} with default curl parameters ..." >>"${LOGDIR}/ERROR.log" + fi + fi fi } From 78c066e1e578d12fd55c62b24e17ded505d7cc62 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 8 Jun 2020 21:47:36 +0200 Subject: [PATCH 51/53] finalize retry --- bashbot.sh | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ab8d470..02e2fe1 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-25-g9b138ee +#### $$VERSION$$ v0.96-dev3-26-gde74571 # # Exit Codes: # - 0 sucess (hopefully) @@ -230,6 +230,7 @@ fi ################## # here we start with the real stuff URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" +BOTSEND_RETRY="no" # do not retry by default ME_URL=$URL'/getMe' @@ -364,11 +365,31 @@ else [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } fi -# checks and processes resukt + +# retry sendJson +# $1 function to retry +# $2 seconds to sleep before +# $2 ... $n arguments +sendJsonRetry(){ + local retry="${1}"; shift + sleep "${1}"; shift + case "${retry}" in + 'sendJson'*) + sendJson "$@" + ;; + 'sendUpload'*) + sendUpload "$@" + ;; + *) + echo "SendJsonRetry: unknown, cannot rerty ${retry}" >>"${LOGDIR}/ERROR.log" + ;; + esac +} + +# process sendJson result # $1 result # $2 function -# $3 .. $n original arguments -# first argument ($3) is Chat_id +# $3 .. $n original arguments, $3 is Chat_id sendJsonResult(){ BOTSENT=( ) BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" @@ -390,23 +411,32 @@ sendJsonResult(){ # log error printf "%s: RESULT=%s CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n" "$(date)"\ "${BOTSENT[OK]}" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" - # retry once if we are throttled + # warm path, do not retry on error [ -n "${BOTSEND_RETRY}" ] && return + + # OK, we can retry sendJson, let's see wehat's failed + # throttled, telegram say we send to much messages if [ -n "${BOTSENT[RETRY]}" ]; then BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))" echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." >>"${LOGDIR}/ERROR.log" + sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" + unset BOTSEND_RETRY fi - # timeout or failed connection + # timeout, failed connection or blocked if [ "${BOTSENT[ERROR]}" == "999" ];then # check if default curl and args are OK if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then echo "BASHBOT is blocked!" >>"${LOGDIR}/ERROR.log" return + # can we recover from block? currently not. fi - # retry without custom curl and args + # if we are not blocked, default curl and args are working if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then BOTSEND_RETRY="2" echo "Timeout or broken connection, retry ${2} with default curl parameters ..." >>"${LOGDIR}/ERROR.log" + unset BASHBOT_CURL BASHBOT_CURL_ARGS + sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" + unset BOTSEND_RETRY fi fi fi From eb4924122ea97c1b5db83f8b9b101e4fde16c9d2 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Jun 2020 08:47:13 +0200 Subject: [PATCH 52/53] bashbotBlockRecover: notify bot about block --- bashbot.sh | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 02e2fe1..5394146 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-26-gde74571 +#### $$VERSION$$ v0.96-dev3-27-g78c066e # # Exit Codes: # - 0 sucess (hopefully) @@ -140,6 +140,7 @@ LOGDIR="${RUNDIR:-.}/logs" if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then LOGDIR="${RUNDIR:-.}" fi +ERRORLOG="${LOGDIR}/ERROR.log" # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then @@ -360,19 +361,17 @@ else sendJsonResult "${res}" "sendJson (wget)" "$@" } sendUpload() { - printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" + printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${ERRORLOG}" BOTSENT[OK]="false" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } fi # retry sendJson -# $1 function to retry -# $2 seconds to sleep before -# $2 ... $n arguments +# $1 function $2 sleep $3 ... $n arguments sendJsonRetry(){ local retry="${1}"; shift - sleep "${1}"; shift + [[ "${1}" =~ ^[0-9.]+$ ]] && sleep "${1}"; shift case "${retry}" in 'sendJson'*) sendJson "$@" @@ -381,15 +380,14 @@ sendJsonRetry(){ sendUpload "$@" ;; *) - echo "SendJsonRetry: unknown, cannot rerty ${retry}" >>"${LOGDIR}/ERROR.log" + printf '%s: SendJsonRetry: unknown, cannot retry %s' "$(date)" "${retry}" >>"${ERRORLOG}" ;; esac } # process sendJson result -# $1 result -# $2 function -# $3 .. $n original arguments, $3 is Chat_id +# stdout is written to ERROR.log +# $1 result $2 function $3 .. $n original arguments, $3 is Chat_id sendJsonResult(){ BOTSENT=( ) BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" @@ -409,38 +407,46 @@ sendJsonResult(){ BOTSENT[DESCRIPTION]="Timeout or broken/no connection" fi # log error - printf "%s: RESULT=%s CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n" "$(date)"\ - "${BOTSENT[OK]}" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" + printf "%s: RESULT=%s ACTION=%s CHAT[ID]=%s ERROR=%s DESC=%s\n" "$(date)"\ + "${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" # warm path, do not retry on error [ -n "${BOTSEND_RETRY}" ] && return - # OK, we can retry sendJson, let's see wehat's failed + # OK, we can retry sendJson, let's see what's failed # throttled, telegram say we send to much messages if [ -n "${BOTSENT[RETRY]}" ]; then BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))" - echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." >>"${LOGDIR}/ERROR.log" + echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" unset BOTSEND_RETRY + return fi # timeout, failed connection or blocked if [ "${BOTSENT[ERROR]}" == "999" ];then # check if default curl and args are OK if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then - echo "BASHBOT is blocked!" >>"${LOGDIR}/ERROR.log" + echo "BASHBOT IP Adress is blocked!" + # user provided function to recover or notify block + if _exec_if_function bashbotBlockRecover; then + BOTSEND_RETRY="2" + echo "Function bashbotBlockRecover returned true, retry ${2}." + sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" + unset BOTSEND_RETRY + fi return - # can we recover from block? currently not. fi - # if we are not blocked, default curl and args are working + # if we are not blocked, so default curl and args is working if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then BOTSEND_RETRY="2" - echo "Timeout or broken connection, retry ${2} with default curl parameters ..." >>"${LOGDIR}/ERROR.log" + printf 'Possible Problem with "%s %s", retry %s with default curl config ...'\ + "${BASHBOT_CURL}" "${BASHBOT_CURL_ARGS}" "${2}" unset BASHBOT_CURL BASHBOT_CURL_ARGS sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" unset BOTSEND_RETRY fi fi fi -} +} >>"${ERRORLOG}" # escape / remove text charaters for json strings, eg. " -> \" From b01e3e91b6c9a51eeaef8223a20182ed35e25ec6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 9 Jun 2020 09:15:36 +0200 Subject: [PATCH 53/53] prepare for release, start cleanup and optimizing --- bashbot.sh | 19 ++++++++++--------- modules/background.sh | 8 ++++---- modules/jsonDB.sh | 12 ++++++------ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5394146..ed7c527 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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.96-dev3-27-g78c066e +#### $$VERSION$$ v0.96-pre-0-geb49241 # # Exit Codes: # - 0 sucess (hopefully) @@ -361,7 +361,7 @@ else sendJsonResult "${res}" "sendJson (wget)" "$@" } sendUpload() { - printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${ERRORLOG}" + printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload\n" >>"${ERRORLOG}" BOTSENT[OK]="false" [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & } @@ -380,7 +380,7 @@ sendJsonRetry(){ sendUpload "$@" ;; *) - printf '%s: SendJsonRetry: unknown, cannot retry %s' "$(date)" "${retry}" >>"${ERRORLOG}" + printf "%s: SendJsonRetry: unknown, cannot retry %s\n" "$(date)" "${retry}" >>"${ERRORLOG}" ;; esac } @@ -416,7 +416,7 @@ sendJsonResult(){ # throttled, telegram say we send to much messages if [ -n "${BOTSENT[RETRY]}" ]; then BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))" - echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." + printf "Retry %s in %s seconds ...\n" "${2}" "${BOTSEND_RETRY}" sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" unset BOTSEND_RETRY return @@ -425,20 +425,20 @@ sendJsonResult(){ if [ "${BOTSENT[ERROR]}" == "999" ];then # check if default curl and args are OK if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then - echo "BASHBOT IP Adress is blocked!" + printf "BASHBOT IP Adress is blocked!\n" # user provided function to recover or notify block if _exec_if_function bashbotBlockRecover; then BOTSEND_RETRY="2" - echo "Function bashbotBlockRecover returned true, retry ${2}." + printf "Function bashbotBlockRecover returned true, retry %s.\n" "${2}" sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" unset BOTSEND_RETRY fi return fi - # if we are not blocked, so default curl and args is working + # we are not blocked, default curl and args are working if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then BOTSEND_RETRY="2" - printf 'Possible Problem with "%s %s", retry %s with default curl config ...'\ + printf "Possible Problem with \"%s %s\", retry %s with default curl config ...\n"\ "${BASHBOT_CURL}" "${BASHBOT_CURL_ARGS}" "${2}" unset BASHBOT_CURL BASHBOT_CURL_ARGS sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" @@ -465,7 +465,7 @@ title2Json(){ [ -n "$3" ] && desc=',"description":"'$(JsonEscape "$3")'"' [ -n "$4" ] && markup=',"parse_mode":"'$(JsonEscape "$4")'"' [ -n "$5" ] && keyboard=',"reply_markup":"'$(JsonEscape "$5")'"' - printf "%s\n" "${title}${caption}${desc}${markup}${keyboard}" + printf '%s\n' "${title}${caption}${desc}${markup}${keyboard}" } # get bot name @@ -483,6 +483,7 @@ JsonDecode() { remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}" out="${BASH_REMATCH[1]}" done + # this echo must stay for correct decoding! echo -e "${out}${remain}" } diff --git a/modules/background.sh b/modules/background.sh index e624bca..4bc93d3 100644 --- a/modules/background.sh +++ b/modules/background.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.96-dev3-12-g3f85134 +#### $$VERSION$$ v0.96-pre-0-geb49241 # source from commands.sh if you want ro use interactive or background jobs @@ -130,16 +130,16 @@ job_control() { fifo="$(procname "${CHAT}" "${job}")" case "$1" in "resumeb"*|"backgr"*) - echo "Restart Job: ${proc} ${fifo}" + printf "Restart Job: %s %s\n" "${proc}" " ${fifo}" restart_back "${CHAT}" "${proc}" "${job}" ;; "suspendb"*) - echo "Suspend Job: ${proc} ${fifo}" + printf "Suspend Job: %s %s\n" "${proc}" " ${fifo}" kill_proc "${CHAT}" "${job}" killall="y" ;; "killb"*) - echo "Kill Job: ${proc} ${fifo}" + printf "Kill Job: %s %s\n" "${proc}" " ${fifo}" kill_proc "${CHAT}" "${job}" rm -f "${FILE}" # remove job killall="y" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index a77189e..8da70b4 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.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.96-dev3-20-gbd22e2a +#### $$VERSION$$ v0.96-pre-0-geb49241 # # source from commands.sh to use jsonDB functions # @@ -130,7 +130,7 @@ if _exists flock; then { flock -s -w 1 200 Json2Array "oldARR" <"${DB}" } 200>"${DB}${BASHBOT_LOCKNAME}" - echo "${oldARR["$1"]}" + printf '%f' "${oldARR["$1"]}" } @@ -208,8 +208,8 @@ jssh_newDB_async() { jssh_newDB "$@"; } jssh_newDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 - [ -f "${DB}" ] && return 2 # already exist, do not zero out - printf '' >"${DB}" + [ -f "${DB}" ] && return 2 # already exist + touch "${DB}" } # $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..' @@ -224,7 +224,7 @@ jssh_checkDB(){ else DB="${BASHBOT_VAR:-.}/$1.jssh" fi - printf '%s\n' "${DB}" + printf '%s' "${DB}" } @@ -290,7 +290,7 @@ jssh_getKeyDB_async() { local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR Json2Array "oldARR" <"${DB}" - echo "${oldARR["$1"]}" + printf '%s' "${oldARR["$1"]}" } jssh_countKeyDB_async() {