fix read timer event timing

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-30 18:02:57 +02:00
parent b240edee41
commit d5f9b1c4fb
5 changed files with 39 additions and 22 deletions

View File

@ -36,14 +36,16 @@ ANTIFL_BAN="5" # 5 minutes
# initialize after installation or update # initialize after installation or update
if [[ "$1" = "init"* ]]; then if [[ "$1" = "init"* ]]; then
ANTIFL_ADMIN="$(< "${BOTADMIN}")" jssh_newDB "addons/$ANTIFL_ME"
fi fi
# register on startbot # register on startbot
if [[ "$1" = "start"* ]]; then if [[ "$1" = "start"* ]]; then
ANTIFL_ADMIN="$(< "${BOTADMIN}")"
#load existing chat settings on start #load existing chat settings on start
jssh_readDB "ANTIFL_CHATS" "${ADDONDIR:-./addons}/$ANTIFL_ME" jssh_readDB "ANTIFL_CHATS" "addons/$ANTIFL_ME"
# register to CMD # register to CMD
BASHBOT_EVENT_CMD["${ANTIFL_ME}"]="${ANTIFL_ME}_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]}","level"]="${ANTIFL_DEFAULT}"
ANTIFL_CHATS["${CHAT[ID]}","ban"]="${ANTIFL_BAN}" ANTIFL_CHATS["${CHAT[ID]}","ban"]="${ANTIFL_BAN}"
[[ "${CMD[1]}" =~ ^[0-9]+$ ]] && ANTIFL_CHATS["${CHAT[ID]}","level"]="${CMD[1]}" [[ "${CMD[1]}" =~ ^[0-9]+$ ]] && ANTIFL_CHATS["${CHAT[ID]}","level"]="${CMD[1]}"
# antiflood_save & antiflood_timer
;; ;;
# command /floodapply starts counter meausares # command /floodapply starts counter meausares
"/floodap"*) "/floodap"*)
@ -65,12 +67,13 @@ if [[ "$1" = "start"* ]]; then
} }
# register to timer # 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 # save settings and reset flood level every BAN Min
antiflood_timer(){ antiflood_timer(){
unset ANTIFL_ACTUALS ANTIFL_ACTUALS=( )
jssh_writeBD "ANTIFL_CHATS" "${ADDONDIR:-./addons}/$ANTIFL_ME" & jssh_writeDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" &
} }
# register to inline and command # register to inline and command

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.90-dev2-16-g74cb204 #### $$VERSION$$ v0.90-dev2-25-gb240ede
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -387,7 +387,7 @@ event_timer() {
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_TIMER[@]}" for event in "${!BASHBOT_EVENT_TIMER[@]}"
do do
timer="${BASHBOT_EVENT_TIMER[${event}]##*,}" timer="${event##*,}"
[[ ! "$timer" =~ ^-*[0-9]+$ ]] && continue [[ ! "$timer" =~ ^-*[0-9]+$ ]] && continue
[[ "$timer" =~ ^-*0+$ ]] && continue [[ "$timer" =~ ^-*0+$ ]] && continue
if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then if [ "$(( EVENT_TIMER % timer ))" = "0" ]; then

View File

@ -455,7 +455,15 @@ Usually message is automatically forwarded in 'commands.sh', but you can forwar
### JSON.sh DB ### 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. 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. 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) #### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md) #### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v0.90-dev2-23-g2a28e7f #### $$VERSION$$ v0.90-dev2-25-gb240ede

View File

@ -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 "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 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 **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) #### [Prev Function Reference](6_reference.md)
#### $$VERSION$$ v0.90-dev2-24-g1c5594a #### $$VERSION$$ v0.90-dev2-25-gb240ede

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # 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 # source from commands.sh to use jsonDB functions
# #
@ -16,9 +16,8 @@
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
# $2 filename, must be relative to BASHBOT_ETC, and not contain '..' # $2 filename, must be relative to BASHBOT_ETC, and not contain '..'
jssh_readDB() { jssh_readDB() {
local DB="${BASHBOT_ETC:-.}/$2.jssh" local DB; DB="$(jssh_checkname "$2")"
[ "$2" = "" ] && return 1 [ "${DB}" = "" ] && return 1
[[ "$2" = *'..'* ]] && return 1
[ ! -f "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 1
Json2Array "$1" <"${DB}" Json2Array "$1" <"${DB}"
} }
@ -27,18 +26,25 @@ jssh_readDB() {
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
jssh_writeDB() { jssh_writeDB() {
local DB="${BASHBOT_ETC:-.}/$2.jssh" local DB; DB="$(jssh_checkname "$2")"
[ "$2" = "" ] && return 1 [ "${DB}" = "" ] && return 1
[[ "$2" = *'..'* ]] && return 1
[ ! -f "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 1
Array2Json "$1" >"${DB}" Array2Json "$1" >"${DB}"
} }
# $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
jssh_newDB() { jssh_newDB() {
local DB="${BASHBOT_ETC:-.}/$1.jssh" local DB; DB="$(jssh_checkname "$1")"
[ "$1" = "" ] && return 1 [ "${DB}" = "" ] && return 1
[[ "$2" = *'..'* ]] && return 1
[ -f "${DB}" ] && return 1 # already exist, do not zero out [ -f "${DB}" ] && return 1 # already exist, do not zero out
printf '\n' >"${DB}" 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}"
}