mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-26 17:26:27 +00:00
use jsshDB internally, e.g. block / count users, it's working well now
This commit is contained in:
parent
46748ee4b8
commit
ed14e850a4
67
bashbot.sh
67
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$$ 0.96-dev2-4-g2a3dcaa
|
#### $$VERSION$$ 0.96-dev2-8-g46748ee
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
@ -20,6 +20,7 @@
|
|||||||
# - 3 user / command / file not found
|
# - 3 user / command / file not found
|
||||||
# - 4 unkown command
|
# - 4 unkown command
|
||||||
# - 5 cannot connect to telegram bot
|
# - 5 cannot connect to telegram bot
|
||||||
|
# - 6 mandatory module not found
|
||||||
# shellcheck disable=SC2140
|
# shellcheck disable=SC2140
|
||||||
|
|
||||||
# are we runnig in a terminal?
|
# are we runnig in a terminal?
|
||||||
@ -48,6 +49,22 @@ _is_function()
|
|||||||
{
|
{
|
||||||
[ "$(LC_ALL=C type -t "$1")" = "function" ]
|
[ "$(LC_ALL=C type -t "$1")" = "function" ]
|
||||||
}
|
}
|
||||||
|
# read JSON.sh style data and asssign to an ARRAY
|
||||||
|
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
||||||
|
Json2Array() {
|
||||||
|
# shellcheck source=./commands.sh
|
||||||
|
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
|
||||||
|
}
|
||||||
|
# output ARRAY as JSON.sh style data
|
||||||
|
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
||||||
|
Array2Json() {
|
||||||
|
local key
|
||||||
|
declare -n ARRAY="$1"
|
||||||
|
for key in "${!ARRAY[@]}"
|
||||||
|
do
|
||||||
|
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
# get location and name of bashbot.sh
|
# get location and name of bashbot.sh
|
||||||
SCRIPT="$0"
|
SCRIPT="$0"
|
||||||
@ -87,6 +104,14 @@ if [ ! -w "." ]; then
|
|||||||
ls -ld .
|
ls -ld .
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
#jsonDB is now mandatory
|
||||||
|
if [ ! -r "${MODULEDIR:-./modules}"/jsonDB.sh ]; then
|
||||||
|
echo -e "${RED}ERROR: Mandatory module ${MODULEDIR:-./modules}/jsonDB.sh is missing or not readable!"
|
||||||
|
exit 6
|
||||||
|
fi
|
||||||
|
# shellcheck source=./modules/jsonDB.sh
|
||||||
|
source "${MODULEDIR:-./modules}"/jsonDB.sh
|
||||||
|
|
||||||
#####################
|
#####################
|
||||||
# Setup and check environment if BOTTOKEN is NOT set
|
# Setup and check environment if BOTTOKEN is NOT set
|
||||||
TOKENFILE="${BASHBOT_ETC:-.}/token"
|
TOKENFILE="${BASHBOT_ETC:-.}/token"
|
||||||
@ -145,7 +170,12 @@ if [ -z "${BOTTOKEN}" ]; then
|
|||||||
fi
|
fi
|
||||||
# setup count file
|
# setup count file
|
||||||
if [ ! -f "${COUNTFILE}.jssh" ]; then
|
if [ ! -f "${COUNTFILE}.jssh" ]; then
|
||||||
printf '["counted_user_id"]\t"num_messages_seen"\n' >"${COUNTFILE}.jssh"
|
jssh_newDB "${COUNTFILE}"
|
||||||
|
jssh_insertDB 'counted_user_chat_id' "num_messages_seen" "${COUNTFILE}"
|
||||||
|
# conveqrt old file on creation
|
||||||
|
if [ -r "${COUNTFILE}" ];then
|
||||||
|
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
|
||||||
|
fi
|
||||||
elif [ ! -w "${COUNTFILE}.jssh" ]; then
|
elif [ ! -w "${COUNTFILE}.jssh" ]; then
|
||||||
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}"
|
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}"
|
||||||
ls -l "${COUNTFILE}.jssh"
|
ls -l "${COUNTFILE}.jssh"
|
||||||
@ -153,11 +183,12 @@ if [ -z "${BOTTOKEN}" ]; then
|
|||||||
fi
|
fi
|
||||||
# setup blocked file
|
# setup blocked file
|
||||||
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
|
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
|
||||||
printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh"
|
jssh_newDB "${BLOCKEDFILE}"
|
||||||
|
jssh_insertDB '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" ] && _exec_if_function jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}"
|
[ "${SOURCE}" != "yes" ] && jssh_deleteKeyDB "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
|
||||||
@ -208,7 +239,7 @@ fi
|
|||||||
|
|
||||||
###############
|
###############
|
||||||
# load modules
|
# load modules
|
||||||
for modules in "${MODULEDIR:-.}"/*.sh ; do
|
for modules in "${MODULEDIR:-./modules}"/*.sh ; do
|
||||||
# shellcheck source=./modules/aliases.sh
|
# shellcheck source=./modules/aliases.sh
|
||||||
if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi
|
if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi
|
||||||
done
|
done
|
||||||
@ -367,22 +398,6 @@ JsonGetLine() {
|
|||||||
JsonGetValue() {
|
JsonGetValue() {
|
||||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
||||||
}
|
}
|
||||||
# read JSON.sh style data and asssign to an ARRAY
|
|
||||||
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
|
||||||
Json2Array() {
|
|
||||||
# shellcheck source=./commands.sh
|
|
||||||
[ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
|
|
||||||
}
|
|
||||||
# output ARRAY as JSON.sh style data
|
|
||||||
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
|
||||||
Array2Json() {
|
|
||||||
local key
|
|
||||||
declare -n ARRAY="$1"
|
|
||||||
for key in "${!ARRAY[@]}"
|
|
||||||
do
|
|
||||||
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
################
|
################
|
||||||
# processing of updates starts here
|
# processing of updates starts here
|
||||||
@ -405,7 +420,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
|
||||||
[ -r "${BLOCKEDFILE}" ] && _exec_if_function jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
|
||||||
fi
|
fi
|
||||||
if [ -z "${iQUERY[ID]}" ]; then
|
if [ -z "${iQUERY[ID]}" ]; then
|
||||||
process_message "${num}" "${debug}"
|
process_message "${num}" "${debug}"
|
||||||
@ -426,7 +441,7 @@ process_client() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# last count users
|
# last count users
|
||||||
_exec_if_function jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}"
|
jssh_countKeyDB "${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
|
||||||
@ -748,9 +763,9 @@ bot_init() {
|
|||||||
[ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc"
|
[ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc"
|
||||||
chown -R "$TOUSER" . ./*
|
chown -R "$TOUSER" . ./*
|
||||||
chmod 711 .
|
chmod 711 .
|
||||||
chmod -R a-w ./*
|
chmod -R o-w ./*
|
||||||
chmod -R u+w "${COUNTFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null
|
chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null
|
||||||
chmod -R o-r,o-w "${COUNTFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
|
chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
|
||||||
# jsshDB must writeable by owner
|
# jsshDB must writeable by owner
|
||||||
find . -name '*.jssh' -exec chmod u+w \{\} +
|
find . -name '*.jssh' -exec chmod u+w \{\} +
|
||||||
#ls -la
|
#ls -la
|
||||||
|
Loading…
Reference in New Issue
Block a user