2019-05-27 14:59:39 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# file: addons/antiFlood.sh.dist
|
|
|
|
#
|
|
|
|
# this addon counts how many files, e.g. stickers, are sent to
|
|
|
|
# a chat and takes actions if threshold is reached
|
|
|
|
#
|
2020-06-23 14:35:50 +00:00
|
|
|
#### $$VERSION$$ v0.98-dev-70-g694ee61
|
2019-05-27 14:59:39 +00:00
|
|
|
|
|
|
|
# used events:
|
|
|
|
#
|
|
|
|
# BASHBOT_EVENT_TEXT message containing message text received
|
2020-06-11 07:23:03 +00:00
|
|
|
# BASHBOT_EVENT_CMD a command is received
|
2019-05-27 14:59:39 +00:00
|
|
|
# BASHBOT_EVENT_FILE file received
|
|
|
|
#
|
|
|
|
# all global variables and functions can be used in registered functions.
|
|
|
|
#
|
|
|
|
# parameters when loaded
|
|
|
|
# $1 event: init, startbot ...
|
|
|
|
# $2 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
|
|
|
|
#
|
2020-06-23 14:35:50 +00:00
|
|
|
# parameters on events
|
2019-05-27 14:59:39 +00:00
|
|
|
# $1 event: inline, message, ..., file
|
|
|
|
# $2 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
|
|
|
|
#
|
|
|
|
# shellcheck disable=SC2140
|
|
|
|
|
|
|
|
# export used events
|
2019-05-30 13:08:07 +00:00
|
|
|
export BASHBOT_EVENT_TEXT BASHBOT_EVENT_CMD BASHBOT_EVENT_FILE BASHBOT_EVENT_TIMER
|
2019-05-27 14:59:39 +00:00
|
|
|
|
|
|
|
# any global variable defined by addons MUST be prefixed by addon name
|
2019-05-30 16:07:47 +00:00
|
|
|
ANTIFL_ME="antiFlood"
|
2019-05-27 14:59:39 +00:00
|
|
|
|
2019-05-30 13:08:07 +00:00
|
|
|
declare -Ax ANTIFL_CHATS ANTIFL_ACTUALS
|
2019-05-27 14:59:39 +00:00
|
|
|
|
2019-05-30 13:08:07 +00:00
|
|
|
ANTIFL_DEFAULT="5" # 5 files per minute
|
|
|
|
ANTIFL_BAN="5" # 5 minutes
|
2019-05-27 14:59:39 +00:00
|
|
|
|
|
|
|
# initialize after installation or update
|
|
|
|
if [[ "$1" = "init"* ]]; then
|
2019-05-30 16:02:57 +00:00
|
|
|
jssh_newDB "addons/$ANTIFL_ME"
|
2019-05-27 14:59:39 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2019-05-30 16:02:57 +00:00
|
|
|
|
2019-05-27 14:59:39 +00:00
|
|
|
# register on startbot
|
|
|
|
if [[ "$1" = "start"* ]]; then
|
2019-05-30 16:02:57 +00:00
|
|
|
ANTIFL_ADMIN="$(< "${BOTADMIN}")"
|
2019-05-27 14:59:39 +00:00
|
|
|
#load existing chat settings on start
|
2019-05-30 16:02:57 +00:00
|
|
|
jssh_readDB "ANTIFL_CHATS" "addons/$ANTIFL_ME"
|
2019-05-27 14:59:39 +00:00
|
|
|
|
|
|
|
# register to CMD
|
2019-05-30 13:08:07 +00:00
|
|
|
BASHBOT_EVENT_CMD["${ANTIFL_ME}"]="${ANTIFL_ME}_cmd"
|
2019-05-27 14:59:39 +00:00
|
|
|
|
2019-05-30 17:52:14 +00:00
|
|
|
antiFlood_cmd(){
|
2019-06-05 11:05:47 +00:00
|
|
|
# shellcheck disable=SC2153
|
|
|
|
local chat="${CHAT[ID]}"
|
|
|
|
|
2019-05-27 14:59:39 +00:00
|
|
|
case "${CMD[0]}" in
|
2019-05-30 17:52:14 +00:00
|
|
|
# command /afstart starts detection, $1 floodlevel
|
|
|
|
"/afstart")
|
2019-06-05 11:05:47 +00:00
|
|
|
# allow bot admin to activate for other chats
|
|
|
|
[[ "${CMD[3]}" =~ ^[-0-9]+$ ]] && user_is_botadmin "${USER[ID]}" && chat="$3"
|
|
|
|
[[ "${CMD[1]}" =~ ^[0-9]+$ ]] && ANTIFL_CHATS["${chat}","level"]="${CMD[1]}" \
|
|
|
|
|| ANTIFL_CHATS["${chat}","level"]="${ANTIFL_DEFAULT}"
|
|
|
|
[[ "${CMD[2]}" =~ ^[0-9]+$ ]] && ANTIFL_CHATS["${chat}","ban"]="${CMD[2]}" \
|
|
|
|
|| ANTIFL_CHATS["${chat}","ban"]="${ANTIFL_BAN}"
|
2019-05-30 17:52:14 +00:00
|
|
|
antiFlood_timer
|
2019-06-05 11:05:47 +00:00
|
|
|
send_normal_message "${USER[ID]}" "Antiflood set for chat ${chat}" &
|
2019-05-27 14:59:39 +00:00
|
|
|
;;
|
2019-05-30 17:52:14 +00:00
|
|
|
# command /afactive starts counter meausares
|
2019-06-03 14:46:32 +00:00
|
|
|
"/afdo" | "/afactive")
|
2019-06-05 11:05:47 +00:00
|
|
|
[[ "${CMD[1]}" =~ ^[-0-9]+$ ]] && user_is_botadmin "${USER[ID]}" && chat="$3"
|
|
|
|
ANTIFL_CHATS["${chat}","active"]="yes"
|
2019-06-03 14:46:32 +00:00
|
|
|
jssh_writeDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" &
|
2019-06-05 11:05:47 +00:00
|
|
|
send_normal_message "${USER[ID]}" "Antiflood activated for chat ${chat}" &
|
2019-05-30 13:08:07 +00:00
|
|
|
;;
|
2019-05-30 17:52:14 +00:00
|
|
|
# command /afactive starts counter meausares
|
|
|
|
"/afstop")
|
2019-06-05 11:05:47 +00:00
|
|
|
[[ "${CMD[1]}" =~ ^[-0-9]+$ ]] && user_is_botadmin "${USER[ID]}" && chat="$3"
|
|
|
|
ANTIFL_CHATS["${chat}","active"]="no"
|
2019-06-03 14:46:32 +00:00
|
|
|
jssh_writeDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" &
|
2019-06-05 11:05:47 +00:00
|
|
|
send_normal_message "${USER[ID]}" "Antiflood stopped for chat ${chat}" &
|
2019-05-30 17:52:14 +00:00
|
|
|
;;
|
2019-05-27 14:59:39 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:08:07 +00:00
|
|
|
# register to timer
|
2019-05-30 17:52:14 +00:00
|
|
|
BASHBOT_EVENT_TIMER["${ANTIFL_ME}","${ANTIFL_BAN}"]="antiFlood_timer"
|
2019-05-30 13:08:07 +00:00
|
|
|
|
|
|
|
# save settings and reset flood level every BAN Min
|
2019-05-30 17:52:14 +00:00
|
|
|
antiFlood_timer(){
|
2019-05-30 16:02:57 +00:00
|
|
|
ANTIFL_ACTUALS=( )
|
|
|
|
jssh_writeDB "ANTIFL_CHATS" "addons/$ANTIFL_ME" &
|
2019-05-30 13:08:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 14:59:39 +00:00
|
|
|
# register to inline and command
|
2019-05-30 13:08:07 +00:00
|
|
|
BASHBOT_EVENT_TEXT["${ANTIFL_ME}"]="${ANTIFL_ME}_multievent"
|
|
|
|
BASHBOT_EVENT_FILE["${ANTIFL_ME}"]="${ANTIFL_ME}_multievent"
|
2019-05-27 14:59:39 +00:00
|
|
|
|
2019-05-30 17:52:14 +00:00
|
|
|
antiFlood_multievent(){
|
2019-05-27 14:59:39 +00:00
|
|
|
# not started
|
2020-05-14 18:49:13 +00:00
|
|
|
[ -z "${ANTIFL_CHATS["${CHAT[ID]}","level"]}" ] && return
|
2019-05-30 18:59:17 +00:00
|
|
|
# count user flood text
|
2019-05-30 13:08:07 +00:00
|
|
|
if [ "$1" = "text" ]; then
|
|
|
|
if [ "${#MESSAGE[0]}" -gt "${ANTIFL_CHATS["${CHAT[ID]}","level"]}" ]; then
|
|
|
|
(( ANTIFL_ACTUALS["${CHAT[ID]}","${USER[ID]}"]-- ))
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
(( ANTIFL_ACTUALS["${CHAT[ID]}","${USER[ID]}","file"]-- ))
|
2019-05-30 18:59:17 +00:00
|
|
|
else
|
2019-05-30 13:08:07 +00:00
|
|
|
# shellcheck disable=SC2154
|
|
|
|
(( ANTIFL_ACTUALS["${CHAT[ID]}","${USER[ID]}"]++ ))
|
2019-05-30 18:59:17 +00:00
|
|
|
fi
|
2019-05-30 13:08:07 +00:00
|
|
|
fi
|
2019-05-30 18:59:17 +00:00
|
|
|
# count user chat flood
|
2019-05-30 18:09:41 +00:00
|
|
|
if [ "$1" = "file" ]; then
|
2019-05-30 18:59:17 +00:00
|
|
|
# shellcheck disable=SC2154
|
2019-05-30 18:09:41 +00:00
|
|
|
(( ANTIFL_ACTUALS["${CHAT[ID]}","${USER[ID]}","file"]++ ))
|
2019-05-30 18:59:17 +00:00
|
|
|
# shellcheck disable=SC2154
|
|
|
|
(( ANTIFL_ACTUALS["${CHAT[ID]}","file"]++ ))
|
2019-05-30 18:09:41 +00:00
|
|
|
antiFlood_action & # do actions in subshell
|
|
|
|
fi
|
2019-05-30 13:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# check and handle actions
|
2019-05-30 17:52:14 +00:00
|
|
|
antiFlood_action() {
|
2019-05-30 13:08:07 +00:00
|
|
|
# check flood level of user
|
2019-05-30 17:52:14 +00:00
|
|
|
if [ "$(( ANTIFL_ACTUALS["${CHAT[ID]}","${USER[ID]}","file"] +1))" -gt "${ANTIFL_CHATS["${CHAT[ID]}","level"]}" ]; then
|
2019-05-30 13:08:07 +00:00
|
|
|
if [ "${ANTIFL_CHATS["${CHAT[ID]}","active"]}" = "yes" ]; then
|
|
|
|
# remove message
|
|
|
|
delete_message "${CHAT[ID]}" "${MESSAGE[ID]}"
|
|
|
|
else
|
|
|
|
# inform admin
|
|
|
|
send_markdown_message "${ANTIFL_ADMIN}" "User ${USER[USERNAME]} reached flood level in chat ${CHAT[USERNAME]}!"
|
|
|
|
fi
|
|
|
|
fi
|
2019-05-30 18:59:17 +00:00
|
|
|
# check flood level of chat
|
|
|
|
if [ "$(( ANTIFL_ACTUALS["${CHAT[ID]}","file"] +1))" -gt "$(( ANTIFL_CHATS["${CHAT[ID]}","level"] * ANTIFL_BAN ))" ]; then
|
|
|
|
if [ "${ANTIFL_CHATS["${CHAT[ID]}","active"]}" = "yes" ]; then
|
|
|
|
# remove message
|
|
|
|
delete_message "${CHAT[ID]}" "${MESSAGE[ID]}"
|
|
|
|
else
|
|
|
|
# inform admin
|
|
|
|
send_markdown_message "${ANTIFL_ADMIN}" "Chat ${CHAT[USERNAME]} reached max flood level!"
|
|
|
|
fi
|
|
|
|
fi
|
2019-05-27 14:59:39 +00:00
|
|
|
}
|
|
|
|
fi
|