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