new feature block users, use jsshDB for count

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-05-29 20:11:51 +02:00
parent 2a3dcaa471
commit 578056b57d
2 changed files with 78 additions and 35 deletions

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.96-dev-8-ge63590b #### $$VERSION$$ 0.96-dev2-4-g2a3dcaa
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -93,7 +93,7 @@ TOKENFILE="${BASHBOT_ETC:-.}/token"
BOTADMIN="${BASHBOT_ETC:-.}/botadmin" BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
BOTACL="${BASHBOT_ETC:-.}/botacl" BOTACL="${BASHBOT_ETC:-.}/botacl"
DATADIR="${BASHBOT_VAR:-.}/data-bot-bash" DATADIR="${BASHBOT_VAR:-.}/data-bot-bash"
# !!!!! DEPRECATED !!!!! BLOCKEDFILE="${BASHBOT_VAR:-.}/blocked"
COUNTFILE="${BASHBOT_VAR:-.}/count" COUNTFILE="${BASHBOT_VAR:-.}/count"
# we assume everthing is already set up correctly if we have TOKEN # we assume everthing is already set up correctly if we have TOKEN
@ -143,15 +143,22 @@ if [ -z "${BOTTOKEN}" ]; then
ls -ld "${DATADIR}" ls -ld "${DATADIR}"
exit 2 exit 2
fi fi
# setup count file !!!!! DEPRECATED !!!!! # setup count file
if [ ! -f "${COUNTFILE}" ]; then if [ ! -f "${COUNTFILE}.jssh" ]; then
printf '\n' >"${COUNTFILE}" printf '["counted_user_id"]\t"num_messages_seen"\n' >"${COUNTFILE}.jssh"
elif [ ! -w "${COUNTFILE}" ]; 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}" ls -l "${COUNTFILE}.jssh"
exit 2 exit 2
fi fi
# setup blocked file
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh"
fi
fi fi
# cleanup (remove double entries) countfile on startup
[ "${SOURCE}" != "yes" ] && _exec_if_function 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
echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}"
@ -390,8 +397,16 @@ process_updates() {
process_client() { process_client() {
local num="$1" debug="$2" local num="$1" debug="$2"
CMD=( ); iQUERY=( ) CMD=( ); iQUERY=( )
iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}"
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log"
iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}"
CHAT[ID]="${UPD["result",${num},"message","chat","id"]}"
USER[ID]="${UPD["result",${num},"message","from","id"]}"
# check for uers / groups to ignore
if [ -n "${USER[ID]}" ]; then
[[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return
[ -r "${BLOCKEDFILE}" ] && _exec_if_function jssh_readDB "BASHBOT_BLOCKED" "${BLOCKEDFILE}"
fi
if [ -z "${iQUERY[ID]}" ]; then if [ -z "${iQUERY[ID]}" ]; then
process_message "${num}" "${debug}" process_message "${num}" "${debug}"
else else
@ -411,13 +426,11 @@ process_client() {
fi fi
# last count users # last count users
# !!!!! DEPRECATED !!!!! _exec_if_function jssh_countKeyDB "${CHAT[ID]}" "${COUNTFILE}"
tmpcount="COUNT${CHAT[ID]}"
grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${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
declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT BASHBOT_EVENT_TIMER declare -Ax BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT BASHBOT_EVENT_TIMER BASHBOT_BLOCKED
start_timer(){ start_timer(){
# send alarm every ~60 s # send alarm every ~60 s
@ -551,8 +564,8 @@ process_message() {
MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')"
MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}" MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}"
# Chat # Chat ID is now parsed when update isrecieved
CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" #CHAT[ID]="${UPD["result",${num},"message","chat","id"]}"
CHAT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","last_name"]}")" CHAT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","last_name"]}")"
CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")" CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")"
CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")" CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")"
@ -561,8 +574,8 @@ process_message() {
CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}" CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}"
CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="${CHAT[ALL_ADMIN]}" # backward compatibility CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="${CHAT[ALL_ADMIN]}" # backward compatibility
# User # user ID is now parsed when update isrecieved
USER[ID]="${UPD["result",${num},"message","from","id"]}" #USER[ID]="${UPD["result",${num},"message","from","id"]}"
USER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","first_name"]}")" USER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","first_name"]}")"
USER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","last_name"]}")" USER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","last_name"]}")"
USER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","from","username"]}")" USER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","from","username"]}")"
@ -687,9 +700,9 @@ start_bot() {
trap "kill -9 $!; exit" EXIT INT HUP TERM QUIT trap "kill -9 $!; exit" EXIT INT HUP TERM QUIT
fi fi
while true; do while true; do
UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)" # ignore timeout error message on waiting for updates
UPDATE="$(getJson "$UPD_URL$OFFSET" 2>/dev/null | "${JSONSHFILE}" -s -b -n | iconv -f utf-8 -t utf-8 -c)"
UPDATE="${UPDATE//$/\\$}" UPDATE="${UPDATE//$/\\$}"
# Offset # Offset
OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
((OFFSET++)) ((OFFSET++))
@ -736,11 +749,11 @@ bot_init() {
chown -R "$TOUSER" . ./* chown -R "$TOUSER" . ./*
chmod 711 . chmod 711 .
chmod -R a-w ./* chmod -R a-w ./*
chmod -R u+w "${COUNTFILE}" "${DATADIR}" "${BOTADMIN}" ./*.log 2>/dev/null chmod -R u+w "${COUNTFILE}"* "${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}"* "${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
fi fi
} }
@ -806,16 +819,46 @@ if [ "${SOURCE}" != "yes" ]; then
BOTPID="$(proclist "${SESSION}")" BOTPID="$(proclist "${SESSION}")"
case "$1" in case "$1" in
"count") # !!!!! DEPRECATED !!!!! "stats"|'count')
echo "A total of $(wc -l <"${COUNTFILE}") users used me." 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
exit exit
;; ;;
"broadcast") # !!!!! DEPRECATED !!!!! 'broadcast')
NUMCOUNT="$(wc -l <"${COUNTFILE}")" declare -A SENDALL
echo "Sending the broadcast $* to $NUMCOUNT users."
[ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5"
shift shift
while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" 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
exit
;; ;;
"status") "status")
if [ -n "${BOTPID}" ]; then if [ -n "${BOTPID}" ]; then
@ -861,7 +904,7 @@ if [ "${SOURCE}" != "yes" ]; then
;; ;;
*) *)
echo -e "${RED}${REALME}: BAD REQUEST${NC}" echo -e "${RED}${REALME}: BAD REQUEST${NC}"
echo -e "${RED}Available arguments: ${GREY}start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}" echo -e "${RED}Available arguments: ${GREY}start, stop, kill, status, status, broadcast, help, suspendback, resumeback, killback${NC}"
exit 4 exit 4
;; ;;
esac esac

View File

@ -494,15 +494,15 @@ slow functions:
A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA. A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BASHBOT_DATA.
- `filename.jssh` is a text file containing the key/value data in json.sh format´s. - `filename.jssh` is a text file containing the key/value data in json.sh format.
- `filename.jssh.flock` is used for read/write locking with flock - `filename.jssh.flock` is used for read/write locking with flock
Path names containing `..` or not in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error.
Since version 0.94 jsshDB functions support file locking with flock. Write/Update operations are serialised with flock to wait until Since version 0.94 jsshDB functions support file locking with flock. write/update operations are serialised with flock to wait until
previous operations are finished, see "man flock" for more information. To avoid deadlocks flock is used with a timeout of 10s for write and 5s for read operations. previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations.
In case flock is mot availibe or you don't want locking, jsshDB functions will be used without file locking by using *_async variants. In case flock is not availibe or you don't want locking, jsshDB functions will be used without file locking.
*Example:* for allowed file names: *Example:* for allowed file names:
```bash ```bash
@ -1019,5 +1019,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$$ 0.96-dev2-3-g975f99a #### $$VERSION$$ 0.96-dev2-4-g2a3dcaa