mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2025-02-14 00:00:28 +00:00
_async for every jssh function, use async in bashbot.sh
This commit is contained in:
parent
c729cf428b
commit
bc74141db7
74
bashbot.sh
74
bashbot.sh
@ -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.94-6-gdcf6534
|
#### $$VERSION$$ v0.96-dev3-3-gc729cf4
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
@ -172,9 +172,9 @@ if [ -z "${BOTTOKEN}" ]; then
|
|||||||
fi
|
fi
|
||||||
# setup count file
|
# setup count file
|
||||||
if [ ! -f "${COUNTFILE}.jssh" ]; then
|
if [ ! -f "${COUNTFILE}.jssh" ]; then
|
||||||
jssh_newDB "${COUNTFILE}"
|
jssh_newDB_async "${COUNTFILE}"
|
||||||
jssh_insertKeyDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}"
|
jssh_insertKeyDB_async 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}"
|
||||||
# conveqrt old file on creation
|
# convert old file on creation
|
||||||
if [ -r "${COUNTFILE}" ];then
|
if [ -r "${COUNTFILE}" ];then
|
||||||
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
|
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
|
||||||
fi
|
fi
|
||||||
@ -185,12 +185,12 @@ if [ -z "${BOTTOKEN}" ]; then
|
|||||||
fi
|
fi
|
||||||
# setup blocked file
|
# setup blocked file
|
||||||
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
|
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
|
||||||
jssh_newDB "${BLOCKEDFILE}"
|
jssh_newDB_async "${BLOCKEDFILE}"
|
||||||
jssh_insertKeyDB 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}"
|
jssh_insertKeyDB_async 'blocked_user_or_chat_id' "name and reason" "${BLOCKEDFILE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
# cleanup (remove double entries) countfile on startup
|
# 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
|
# do we have BSD sed
|
||||||
if ! sed '1ia' </dev/null 2>/dev/null; then
|
if ! sed '1ia' </dev/null 2>/dev/null; then
|
||||||
@ -422,7 +422,7 @@ process_client() {
|
|||||||
# check for uers / groups to ignore
|
# check for uers / groups to ignore
|
||||||
if [ -n "${USER[ID]}" ]; then
|
if [ -n "${USER[ID]}" ]; then
|
||||||
[[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return
|
[[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return
|
||||||
jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
jssh_readDB_async "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
||||||
fi
|
fi
|
||||||
if [ -z "${iQUERY[ID]}" ]; then
|
if [ -z "${iQUERY[ID]}" ]; then
|
||||||
process_message "${num}" "${debug}"
|
process_message "${num}" "${debug}"
|
||||||
@ -443,7 +443,7 @@ process_client() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# last count users
|
# 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
|
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
|
case "$1" in
|
||||||
"stats"|'count')
|
"stats"|'count')
|
||||||
declare -A STATS
|
declare -A STATS
|
||||||
if _is_function jssh_readDB ; then
|
jssh_readDB_async "STATS" "${COUNTFILE}"
|
||||||
jssh_readDB "STATS" "${COUNTFILE}"
|
for MSG in ${!STATS[*]}
|
||||||
for MSG in ${!STATS[*]}
|
do
|
||||||
do
|
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
|
||||||
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
|
(( USERS++ ))
|
||||||
(( USERS++ ))
|
done
|
||||||
done
|
for MSG in ${STATS[*]}
|
||||||
for MSG in ${STATS[*]}
|
do
|
||||||
do
|
(( MESSAGES+=MSG ))
|
||||||
(( MESSAGES+=MSG ))
|
done
|
||||||
done
|
echo "A total of ${MESSAGES} messages from ${USERS} users are processed."
|
||||||
echo "A total of ${MESSAGES} messages from ${USERS} users are processed."
|
|
||||||
else
|
|
||||||
echo "Module jsshDB is not availible."
|
|
||||||
fi
|
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
'broadcast')
|
'broadcast')
|
||||||
declare -A SENDALL
|
declare -A SENDALL
|
||||||
shift
|
shift
|
||||||
if _is_function jssh_readDB ; then
|
jssh_readDB_async "SENDALL" "${COUNTFILE}"
|
||||||
jssh_readDB "SENDALL" "${COUNTFILE}"
|
echo -e "Sending broadcast message to all users \c"
|
||||||
echo -e "Sending broadcast message to all users \c"
|
for MSG in ${!SENDALL[*]}
|
||||||
for MSG in ${!SENDALL[*]}
|
do
|
||||||
do
|
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
|
||||||
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
|
(( USERS++ ))
|
||||||
(( USERS++ ))
|
if [ -n "$*" ]; then
|
||||||
if [ -n "$*" ]; then
|
send_markdown_message "${MSG}" "$*"
|
||||||
send_markdown_message "${MSG}" "$*"
|
echo -e ".\c"
|
||||||
echo -e ".\c"
|
sleep 0.1
|
||||||
sleep 0.1
|
fi
|
||||||
fi
|
done
|
||||||
done
|
echo -e "\nMessage \"$*\" sent to ${USERS} users."
|
||||||
echo -e "\nMessage \"$*\" sent to ${USERS} users."
|
|
||||||
else
|
|
||||||
echo "Module jsshDB is not availible."
|
|
||||||
fi
|
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
"status")
|
"status")
|
||||||
|
@ -29,7 +29,8 @@ Have FUN!
|
|||||||
├── commands.sh # command dispatcher - 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
|
├── 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
|
├── modules # optional functions, sourced by commands.sh
|
||||||
│ ├── aliases.sh # to disable modules rename them xxx.sh.off
|
│ ├── 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)
|
#### [Prev Create Bot](1_firstbot.md)
|
||||||
#### [Next Advanced Usage](3_advanced.md)
|
#### [Next Advanced Usage](3_advanced.md)
|
||||||
|
|
||||||
#### $$VERSION$$ v0.96-dev3-1-g2a66ee9
|
#### $$VERSION$$ v0.96-dev3-3-gc729cf4
|
||||||
|
|
||||||
|
@ -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$$ 0.96-dev2-11-ge366633
|
#### $$VERSION$$ v0.96-dev3-3-gc729cf4
|
||||||
#
|
#
|
||||||
# source from commands.sh to use jsonDB functions
|
# source from commands.sh to use jsonDB functions
|
||||||
#
|
#
|
||||||
@ -118,9 +118,10 @@ if _exists flock; then
|
|||||||
} 200>"${DB}${BASHBOT_LOCKNAME}"
|
} 200>"${DB}${BASHBOT_LOCKNAME}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# delete key/value from jsshDB
|
# get key/value from jsshDB
|
||||||
# $1 key name, can onyl contain -a-zA-Z0-9,._
|
# $1 key name, can onyl contain -a-zA-Z0-9,._
|
||||||
# $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 '..'
|
||||||
|
alias jssh_getDB=jssh_getKeyDB
|
||||||
jssh_getKeyDB() {
|
jssh_getKeyDB() {
|
||||||
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
|
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
|
||||||
local DB; DB="$(jssh_checkDB "$2")"
|
local DB; DB="$(jssh_checkDB "$2")"
|
||||||
@ -156,6 +157,18 @@ if _exists flock; then
|
|||||||
} 200>"${DB}${BASHBOT_LOCKNAME}"
|
} 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
|
else
|
||||||
#########
|
#########
|
||||||
@ -165,45 +178,24 @@ else
|
|||||||
alias jssh_updateDB=jssh_updateDB_async
|
alias jssh_updateDB=jssh_updateDB_async
|
||||||
alias jssh_insertDB=jssh_insertDB_async
|
alias jssh_insertDB=jssh_insertDB_async
|
||||||
alias ssh_deleteKeyDB=jssh_deleteKeyDB_async
|
alias ssh_deleteKeyDB=jssh_deleteKeyDB_async
|
||||||
|
alias jssh_getDB=jssh_getKeyDB_async
|
||||||
alias jssh_getKeyDB=jssh_getKeyDB_async
|
alias jssh_getKeyDB=jssh_getKeyDB_async
|
||||||
alias jssh_countKeyDB=jssh_countKeyDB_async
|
alias jssh_countKeyDB=jssh_countKeyDB_async
|
||||||
|
alias jssh_updateKeyDB=jssh_updateKeyDB_async
|
||||||
fi
|
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
|
# no need for atomic
|
||||||
|
|
||||||
# print ARRAY content to stdout instead of file
|
# print ARRAY content to stdout instead of file
|
||||||
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
|
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
|
||||||
|
alias jssh_printDB_async=jssh_printDB
|
||||||
jssh_printDB() {
|
jssh_printDB() {
|
||||||
Array2Json "$1"
|
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 '..'
|
# $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
|
||||||
|
alias jssh_newDB_async=jssh_newDB
|
||||||
jssh_newDB() {
|
jssh_newDB() {
|
||||||
local DB; DB="$(jssh_checkDB "$1")"
|
local DB; DB="$(jssh_checkDB "$1")"
|
||||||
[ -z "${DB}" ] && return 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 '..'
|
# $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..'
|
||||||
# returns real path to DB file if everything is ok
|
# returns real path to DB file if everything is ok
|
||||||
|
alias jssh_checkDB_async=jssh_checkDB
|
||||||
jssh_checkDB(){
|
jssh_checkDB(){
|
||||||
local DB
|
local DB
|
||||||
[ -z "$1" ] && return 1
|
[ -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
|
# can be used explictitly or as fallback if flock is not availible
|
||||||
jssh_readDB_async() {
|
jssh_readDB_async() {
|
||||||
local DB; DB="$(jssh_checkDB "$2")"
|
local DB; DB="$(jssh_checkDB "$2")"
|
||||||
@ -302,3 +295,16 @@ jssh_countKeyDB_async() {
|
|||||||
Array2Json "oldARR" >"${DB}"
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user