diff options
-rw-r--r-- | modules/m_spamfilter.sh | 52 |
1 files changed, 20 insertions, 32 deletions
diff --git a/modules/m_spamfilter.sh b/modules/m_spamfilter.sh index 1da93ea..115621f 100644 --- a/modules/m_spamfilter.sh +++ b/modules/m_spamfilter.sh @@ -29,7 +29,6 @@ #-------------------------------------------------------------------------# -readonly DEBUG=0 readonly RELAY_NICK='a-user' readonly FILTER_CHANNELS="${config_module_spamfilter_channels}" readonly II_DIR=/home/pbot/irc/${config_server} @@ -75,10 +74,10 @@ readonly SPAM=( module_spamfilter_INIT() { modinit_API='2' - modinit_HOOKS='on_JOIN on_raw' + modinit_HOOKS='on_JOIN on_PRIVMSG' helpentry_module_spamfilter_description="Provides support for filtering known spam using +z mode." - which ii &>/dev/null || echo "[SPAMFILTER]: ERROR: module failed to load - cannot find the \`ii\` program" + which ii &>/dev/null || log_error "[SPAMFILTER]: ERROR: module failed to load - cannot find the \`ii\` program" which ii &>/dev/null } @@ -102,14 +101,14 @@ module_spamfilter_on_JOIN() local bot_nick=${server_nick_current} parse_hostmask_nick "$1" 'whojoined' -DBG_JOIN "${whojoined}" "${channel}" +DBG_SPAMFILTER_JOIN if [[ "${whojoined}" == "${bot_nick}" ]] && \ [[ " ${FILTER_CHANNELS} " =~ " ${channel} " ]] && \ [[ " ${OP_CHANNELS} " =~ " ${channel} " ]] && (( ${BECOME_OP_ON_JOIN} )) then send_modes "${channel}" '+qz $~a' -DBG_SET_MODE +DBG_SPAMFILTER_SET_MODE # launch a second bot (not op) so we can compare the chat logs [[ -f ${II_DIR}/in ]] && printf "/quit\n" > ${II_DIR}/in ; sleep 2 ; @@ -120,26 +119,19 @@ DBG_SET_MODE fi printf "/j %s\n" "${channel}" > ${II_DIR}/in -DBG_RELAY_USER +DBG_SPAMFILTER_RELAY_USER fi } -module_spamfilter_on_raw() # (raw_line) +module_spamfilter_on_PRIVMSG() # (hostmask , target , query) { - local raw_line=$1 - -DBG_RAW_LINE "${raw_line}" - - [[ "${raw_line}" =~ ^:([^ ]*)\ +PRIVMSG\ +([^:]+)\ +:(.*) ]] || return 0 - - local hostmask="${BASH_REMATCH[1]}" - local target="${BASH_REMATCH[2]}" - local query="${BASH_REMATCH[3]}" local sender - parse_hostmask_nick "${hostmask}" 'sender' + local hostmask=$1 ; parse_hostmask_nick "${hostmask}" 'sender' ; + local target=$2 + local query=$3 local was_handled -DBG_CRITERIA "${sender}" "${target}" "${query}" +DBG_SPAMFILTER_CRITERIA # ignore internal messages and chat from registered users if ! is_filtered_channel "${target}" || \ @@ -151,7 +143,7 @@ DBG_CRITERIA "${sender}" "${target}" "${query}" elif is_nonsense "${query}" || is_spam "${query}" then was_handled=1 -DBG_SPAM ${sender} +DBG_SPAMFILTER # relay chat from an unregistered user to the channel else local message="(${sender} said): ${query}" @@ -161,7 +153,7 @@ DBG_SPAM ${sender} else send_msg "${target}" "${message}" fi -DBG_RELAY_CHAT "${sender}" +DBG_SPAMFILTER_RELAY_CHAT was_handled=0 fi @@ -210,7 +202,7 @@ is_nonsense() # (chat_msg) is_spam() # (chat_msg) { local needle=$1 - local haystack=("${SPAM[@]}") + local haystack=( "${SPAM[@]}" ) local straw for straw in "${haystack[@]}"; do [[ ${needle} = *"${straw}"* ]] && return 0 ; done ; @@ -221,23 +213,19 @@ is_spam() # (chat_msg) ## DEBUG ## -DBG_JOIN() { (( ${DEBUG} )) && echo "[SPAMFILTER]: whojoined=$1 channel=$2" ; } -DBG_SET_MODE() { (( ${DEBUG} )) && echo "[SPAMFILTER]: set mode +qz" ; } -DBG_RELAY_USER() { (( ${DEBUG} )) && echo "[SPAMFILTER]: launched ii relay user: '${RELAY_NICK}'" ; } -DBG_RAW_LINE() { (( ${DEBUG} )) && echo "[SPAMFILTER]: incoming raw_line=$1" ; } -DBG_CRITERIA() +# readonly DEBUG=0 +DBG_SPAMFILTER_JOIN() { (( ${DEBUG} )) && echo "[SPAMFILTER]: whojoined=${whojoined} channel=${channel}" ; } +DBG_SPAMFILTER_SET_MODE() { (( ${DEBUG} )) && echo "[SPAMFILTER]: set mode +qz" ; } +DBG_SPAMFILTER_RELAY_USER() { (( ${DEBUG} )) && echo "[SPAMFILTER]: launched ii relay user: '${RELAY_NICK}'" ; } +DBG_SPAMFILTER_CRITERIA() { (( ${DEBUG} )) || return - local sender=$1 - local target=$2 - local query=$3 - 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" ; is_nonsense "${query}" && echo -n " => is nonsense - returning" ; is_spam "${query}" && echo -n " => is known spam - returning" ; echo ; } -DBG_SPAM() { (( ${DEBUG} )) && echo "[SPAMFILTER]: !!!triggered!!! sender=$1" ; } -DBG_RELAY_CHAT() { (( ${DEBUG} )) && echo "[SPAMFILTER]: relaying chat from unregistered user sender=$1" ; } +DBG_SPAMFILTER() { (( ${DEBUG} )) && echo "[SPAMFILTER]: !!!triggered!!! sender=${sender}" ; } +DBG_SPAMFILTER_RELAY_CHAT() { (( ${DEBUG} )) && echo "[SPAMFILTER]: relaying chat from unregistered user sender=${sender}" ; } |