summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2018-08-13 01:53:32 -0400
committerbill-auger <mr.j.spam.me@gmail.com>2018-09-28 23:28:54 -0400
commitfed704733b386cb8ba2aa08cdc465f6d571c9a1c (patch)
treeaf3368ae2f7289d3e0229c6affd899368e7e3022
parentb9089ca4103578ff0eec5dea6d2c81efadaf73a6 (diff)
refactor spam filter module
-rw-r--r--modules/m_spamfilter.sh109
1 files changed, 70 insertions, 39 deletions
diff --git a/modules/m_spamfilter.sh b/modules/m_spamfilter.sh
index 4014847..f687cb4 100644
--- a/modules/m_spamfilter.sh
+++ b/modules/m_spamfilter.sh
@@ -18,16 +18,20 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###########################################################################
-#--------------------------------------------------------------------------
-## This module filters known spam using +q and +z modes.
-## It will relay any un-recognized message as: (nick said):.
-## Requires: $BECOME_OP_ON_JOIN non-zero and
-## channel in $OP_CHANNELS in bot_settings.sh
-#--------------------------------------------------------------------------
+#-------------------------------------------------------------------------#
+## This module filters known spam using +q and +z modes. ##
+## It will relay any un-recognized message as: (nick said):. ##
+## Requires: the 'ii' program to be installed ##
+## Requires: (in bot_settings.sh) ##
+## * $BECOME_OP_ON_JOIN non-zero ##
+## * channel in $OP_CHANNELS ##
+## * channel in $config_module_spamfilter_channels ##
+#-------------------------------------------------------------------------#
readonly DEBUG=0
readonly RELAY_NICK='a-user'
+readonly FILTER_CHANNELS="${config_module_spamfilter_channels}"
readonly II_DIR=/home/pbot/irc/${config_server}
readonly II_LOG_REGEX='[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} '
readonly SPAMFILTER_CHANNELS="${config_module_spamfilter_channels}"
@@ -83,7 +87,7 @@ module_spamfilter_on_JOIN()
local bot_nick=${server_nick_current}
parse_hostmask_nick "$1" 'whojoined'
-DBG_JOIN ${whojoined} ${channel}
+DBG_JOIN "${whojoined}" "${channel}"
if [[ "${whojoined}" == "${bot_nick}" ]] && \
[[ " ${SPAMFILTER_CHANNELS} " =~ " ${channel} " ]] && \
@@ -118,45 +122,73 @@ DBG_RAW_LINE "${raw_line}"
local hostmask="${BASH_REMATCH[1]}"
local target="${BASH_REMATCH[2]}"
local query="${BASH_REMATCH[3]}"
+ local sender
parse_hostmask_nick "${hostmask}" 'sender'
- local log_line="<${sender}> ${query}"
- local chat_log="$(sleep 1 ; tail -n 12 ${II_DIR}/${channel}/out)"
+ local was_handled
-DBG_CONDITIONS ${sender} ${target} ${log_line} ${chat_log}
+DBG_CRITERIA "${sender}" "${target}" "${query}"
- # ignore chat that is present in the ii users's chat log
- [[ " ${RELAY_NICK} ${INJECT_NICK} " =~ " ${sender} " ]] && return 0
- [[ " ${config_module_spamfilter_channels} " =~ " ${target} " ]] || return 0
- [[ "${chat_log}" =~ ${II_LOG_REGEX}"${log_line}" ]] && return 0
+ # ignore internal messages and chat from registered users
+ if ! is_filtered_channel "${target}" ||
+ is_internal_user "${sender}" ||
+ is_public_chat "${sender}" "${query}"
+ then was_handled=0
+ else
-DBG_UNREGISTERED ${sender}
+DBG_UNREGISTERED "${sender}"
- # this chat is from an unregistered user - relay it to the channel unless it is known spam
- if is_spam "${query}"
- then
+ # ignore chat that is known spam or otherwise nonsense
+ if is_spam "${query}"
+ then was_handled=1
DBG_SPAM ${sender}
- else
- send_msg "${channel}" "(${sender} said): ${query}"
- #echo "(${sender} said): ${query}" > ${II_DIR}/${channel}/in
+ # relay chat from an unregistered user to the channel
+ else
+ send_msg "${target}" "(${sender} said): ${query}"
+ #echo "(${sender} said): ${query}" > ${II_DIR}/${target}/in
+
+ was_handled=0
+ fi
fi
- # supress further handling of this message
- return 1
+ # supress or allow further handling of this message
+ return ${was_handled}
}
## helpers ##
-is_spam()
+is_internal_user() # (sender)
+{
+ local sender=$1
+
+ [[ " ${RELAY_NICK} ${INJECT_NICK} " =~ " ${sender} " ]]
+}
+
+is_filtered_channel() # (target)
+{
+ local target=$1
+
+ [[ " ${FILTER_CHANNELS} " =~ " ${target} " ]]
+}
+
+is_public_chat() # (sender , query)
+{
+ local sender=$1
+ local query=$2
+ local log_line="<${sender}> ${query}"
+ local chat_log="$(sleep 2 ; tail -n 12 ${II_DIR}/${target}/out 2> /dev/null ;)"
+
+ [[ "${chat_log}" =~ ${II_LOG_REGEX}"${log_line}" ]]
+}
+
+is_spam() # (chat_msg)
{
local needle=$1
local haystack=("${SPAM[@]}")
local straw
- [ -z "$(echo ${needle})" ] && return 0
-
for straw in "${haystack[@]}"; do [[ ${needle} = *"${straw}"* ]] && return 0 ; done ;
return 1
@@ -165,22 +197,21 @@ is_spam()
## DEBUG ##
-DBG_JOIN() { (( $DEBUG )) || return ; local whojoined=$1 ; local channel=$2 ; echo "[SPAMFILTER]: whojoined=${whojoined} channel=${channel}" ; }
-DBG_RELAY_USER() { (( $DEBUG )) && echo "[SPAMFILTER]: launched ii relay user: '${RELAY_NICK}'" ; }
-DBG_SET_MODE() { (( $DEBUG )) && echo "[SPAMFILTER]: set mode +qz" ; }
-DBG_RAW_LINE() { (( $DEBUG )) && echo -e "[SPAMFILTER]: \n\traw_line='$1'" ; }
-DBG_CONDITIONS()
+DBG_JOIN() { (( ${DEBUG} )) || return ; local whojoined=$1 ; local channel=$2 ; echo "[SPAMFILTER]: whojoined=${whojoined} channel=${channel}" ; }
+DBG_RELAY_USER() { (( ${DEBUG} )) && echo "[SPAMFILTER]: launched ii relay user: '${RELAY_NICK}'" ; }
+DBG_SET_MODE() { (( ${DEBUG} )) && echo "[SPAMFILTER]: set mode +qz" ; }
+DBG_RAW_LINE() { (( ${DEBUG} )) && echo -e "[SPAMFILTER]: incoming raw_line=\n\t'$1'" ; }
+DBG_CRITERIA()
{
- (( $DEBUG )) || return
+ (( ${DEBUG} )) || return
local sender=$1
local target=$2
- local log_line=$3
- local chat_log=$4
+ local query=$3
- echo -n "[SPAMFILTER]: sender='${sender}'" ; [[ " ${RELAY_NICK} ${INJECT_NICK} " =~ " ${sender} " ]] && echo -n " => from RELAY_NICK - returning" ; echo
- echo -n "[SPAMFILTER]: target='${target}'" ; [[ " ${config_module_spamfilter_channels} " =~ " ${target} " ]] || echo -n " => wrong channel - returning" ; echo
- echo -n "[SPAMFILTER]: query='${query}'" ; [[ "${chat_log}" =~ ${II_LOG_REGEX}"${log_line}" ]] && echo -n " => from registered user - returning" ; echo
+ echo -n "[SPAMFILTER]: target='${target}'" ; ! is_filtered_channel "${target}" && echo -n " => wrong channel - returning" ; echo
+ echo -n "[SPAMFILTER]: sender='${sender}'" ; is_internal_user "${sender}" && echo -n " => from internal user - returning" ; echo
+ echo -n "[SPAMFILTER]: query='${query}'" ; is_public_chat "${sender}" "${query}" && echo -n " => from registered user - returning" ; echo
}
-DBG_UNREGISTERED() { (( $DEBUG )) || return ; local sender=$1 ; echo "[SPAMFILTER]: unregistered user sender=${sender}" ; }
-DBG_SPAM() { (( $DEBUG )) || return ; local sender=$1 ; echo "[SPAMFILTER]: !!!triggered!!! spambot=${sender}" ; }
+DBG_UNREGISTERED() { (( ${DEBUG} )) || return ; local sender=$1 ; echo "[SPAMFILTER]: unregistered user sender=${sender}" ; }
+DBG_SPAM() { (( ${DEBUG} )) || return ; local sender=$1 ; echo "[SPAMFILTER]: !!!triggered!!! spambot=${sender}" ; }