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}"