From d5f9b1c4fb2077ab79663071b015d101272c990d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 30 May 2019 18:02:57 +0200 Subject: [PATCH] fix read timer event timing --- addons/antiFlood.sh | 15 +++++++++------ bashbot.sh | 4 ++-- doc/6_reference.md | 12 ++++++++++-- doc/7_develop.md | 4 ++-- modules/jsonDB.sh | 26 ++++++++++++++++---------- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/addons/antiFlood.sh b/addons/antiFlood.sh index 80cfa71..a4c9ee3 100644 --- a/addons/antiFlood.sh +++ b/addons/antiFlood.sh @@ -36,14 +36,16 @@ ANTIFL_BAN="5" # 5 minutes # initialize after installation or update if [[ "$1" = "init"* ]]; then - ANTIFL_ADMIN="$(< "${BOTADMIN}")" + jssh_newDB "addons/$ANTIFL_ME" fi + # register on startbot if [[ "$1" = "start"* ]]; then + ANTIFL_ADMIN="$(< "${BOTADMIN}")" #load existing chat settings on start - jssh_readDB "ANTIFL_CHATS" "${ADDONDIR:-./addons}/$ANTIFL_ME" + jssh_readDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" # register to CMD BASHBOT_EVENT_CMD["${ANTIFL_ME}"]="${ANTIFL_ME}_cmd" @@ -55,7 +57,7 @@ if [[ "$1" = "start"* ]]; then ANTIFL_CHATS["${CHAT[ID]}","level"]="${ANTIFL_DEFAULT}" ANTIFL_CHATS["${CHAT[ID]}","ban"]="${ANTIFL_BAN}" [[ "${CMD[1]}" =~ ^[0-9]+$ ]] && ANTIFL_CHATS["${CHAT[ID]}","level"]="${CMD[1]}" - # antiflood_save & + antiflood_timer ;; # command /floodapply starts counter meausares "/floodap"*) @@ -65,12 +67,13 @@ if [[ "$1" = "start"* ]]; then } # register to timer - BASHBOT_EVENT_TIMER["${ANTIFL_ME}","${ANTIFL_BAN}"]="antiflood_timer" + #BASHBOT_EVENT_TIMER["${ANTIFL_ME}","${ANTIFL_BAN}"]="antiflood_timer" + BASHBOT_EVENT_TIMER["${ANTIFL_ME}","1"]="antiflood_timer" # save settings and reset flood level every BAN Min antiflood_timer(){ - unset ANTIFL_ACTUALS - jssh_writeBD "ANTIFL_CHATS" "${ADDONDIR:-./addons}/$ANTIFL_ME" & + ANTIFL_ACTUALS=( ) + jssh_writeDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" & } # register to inline and command diff --git a/bashbot.sh b/bashbot.sh index 66308b1..a0f1732 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.90-dev2-16-g74cb204 +#### $$VERSION$$ v0.90-dev2-25-gb240ede # # Exit Codes: # - 0 sucess (hopefully) @@ -387,7 +387,7 @@ event_timer() { # shellcheck disable=SC2153 for event in "${!BASHBOT_EVENT_TIMER[@]}" do - timer="${BASHBOT_EVENT_TIMER[${event}]##*,}" + timer="${event##*,}" [[ ! "$timer" =~ ^-*[0-9]+$ ]] && continue [[ "$timer" =~ ^-*0+$ ]] && continue if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then diff --git a/doc/6_reference.md b/doc/6_reference.md index f9895da..0585d1c 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -455,7 +455,15 @@ Usually message is automatically forwarded in 'commands.sh', but you can forwar ### JSON.sh DB Since output of JSON.sh is so handy to use in bash, we provide a simple wrapper to read and write JSON.sh style data from and to files. -All file names must be relaitive to BASHBOT_ETC and must not contain '..'. The suffix '.jssh' is added to file name! +File name is prefixed with BASHBOT_ETC and the suffix '.jssh' is added to file name! File names must not contain '..' + +*Example:* for file name: +```bash +# bashbot is installed in /usr/local/telegram-bot-bash, no BASHBOT_ETC set. +"myfile" -> /usr/local/telegram-bot-bash/myfile.jssh +"addons/myfile" -> /usr/local/telegram-bot-bash/addons/myfile.jssh +"${DATADIR}/myfile usr/local/telegram-bot-bash/data-bot-bash/myfile.jssh +``` You must include ```source modules/jsshDB.sh``` in 'commands.sh' to have the following functions availible. @@ -784,5 +792,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.90-dev2-23-g2a28e7f +#### $$VERSION$$ v0.90-dev2-25-gb240ede diff --git a/doc/7_develop.md b/doc/7_develop.md index 80da0c0..30c1cb5 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -18,7 +18,7 @@ In addition you can change the change the level of verbosity by adding a third a "xdebugterm" same as xdebug but output and errors are sent to terminal ``` -To stop bashhbot in debugging mode use ```./bashbot.sh kill``` as ususal. +To stop bashhbot in debugging mode run ```ps -uf | grep debug``` and use 'kill -9' to kill all processes shwon. ### Modules and Addons **Modules** live in ```modules/*.sh``` and are bashbot functions factored out in seperate files, gouped by functionality. Main reason for creating modules was @@ -275,5 +275,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v0.90-dev2-24-g1c5594a +#### $$VERSION$$ v0.90-dev2-25-gb240ede diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 8cffbca..bbfa76a 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.90-dev2-9-gbbbc8ae +#### $$VERSION$$ v0.90-dev2-25-gb240ede # # source from commands.sh to use jsonDB functions # @@ -16,9 +16,8 @@ # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $2 filename, must be relative to BASHBOT_ETC, and not contain '..' jssh_readDB() { - local DB="${BASHBOT_ETC:-.}/$2.jssh" - [ "$2" = "" ] && return 1 - [[ "$2" = *'..'* ]] && return 1 + local DB; DB="$(jssh_checkname "$2")" + [ "${DB}" = "" ] && return 1 [ ! -f "${DB}" ] && return 1 Json2Array "$1" <"${DB}" } @@ -27,18 +26,25 @@ jssh_readDB() { # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' jssh_writeDB() { - local DB="${BASHBOT_ETC:-.}/$2.jssh" - [ "$2" = "" ] && return 1 - [[ "$2" = *'..'* ]] && return 1 + local DB; DB="$(jssh_checkname "$2")" + [ "${DB}" = "" ] && return 1 [ ! -f "${DB}" ] && return 1 Array2Json "$1" >"${DB}" } # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' jssh_newDB() { - local DB="${BASHBOT_ETC:-.}/$1.jssh" - [ "$1" = "" ] && return 1 - [[ "$2" = *'..'* ]] && return 1 + local DB; DB="$(jssh_checkname "$1")" + [ "${DB}" = "" ] && return 1 [ -f "${DB}" ] && return 1 # already exist, do not zero out printf '\n' >"${DB}" } + +# $1 filename, check if must be relative to BASHBOT_ETC, and not contain '..' +jssh_checkname(){ + [ "$1" = "" ] && return 1 + local DB="${BASHBOT_ETC:-.}/$1.jssh" + [[ "$1" = "${BASHBOT_ETC:-.}"* ]] && DB="$1.jssh" + [[ "$1" = *'..'* ]] && return 1 + printf '%s\n' "${DB}" +}