diff options
author | bill-auger <mr.j.spam.me@gmail.com> | 2018-09-04 13:17:55 +0000 |
---|---|---|
committer | bill-auger <mr.j.spam.me@gmail.com> | 2018-10-02 19:02:23 -0400 |
commit | f9d287109f20e173d6fae78be0fd71a33f428415 (patch) | |
tree | b9a0972d68b6592813d10e6f8d952a7e640cdc63 | |
parent | c439f4898dc8ff3142f215ae084ced5315ca4aad (diff) |
refactor spam filter module
-rw-r--r-- | modules/m_spamfilter.sh | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/modules/m_spamfilter.sh b/modules/m_spamfilter.sh index 2471b5e..7ef20a4 100644 --- a/modules/m_spamfilter.sh +++ b/modules/m_spamfilter.sh @@ -34,6 +34,7 @@ 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 IS_RELAY_BOT_REGISTERED=0 readonly BOT_PASS="${config_server_passwd}" # generic spam-like regexes @@ -76,6 +77,9 @@ module_spamfilter_INIT() modinit_API='2' modinit_HOOKS='on_JOIN on_raw' 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 } module_spamfilter_UNLOAD() @@ -105,20 +109,18 @@ DBG_JOIN "${whojoined}" "${channel}" [[ " ${OP_CHANNELS} " =~ " ${channel} " ]] && (( ${BECOME_OP_ON_JOIN} )) then send_modes "${channel}" '+qz $~a' - # launch a second bot so we can compare the chat logs - if which ii - then [[ -f ${II_DIR}/in ]] && printf "/quit\n" > ${II_DIR}/in ; sleep 2 ; - pkill ii ; sleep 2 ; - ii -s ${config_server} -n ${RELAY_NICK} -f "Spam Filter" & sleep 5 ; -# printf "/j NickServ IDENTIFY %s\n" ${BOT_PASS} > ${II_DIR}/in ; sleep 2 ; - printf "/j %s\n" "${channel}" > ${II_DIR}/in - -DBG_RELAY_USER +DBG_SET_MODE - else echo "ii is not installed" + # 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 ; + pkill ii ; sleep 2 ; + ii -s ${config_server} -n ${RELAY_NICK} -f "Spam Filter" & sleep 5 ; + if (( ${IS_RELAY_BOT_REGISTERED} )) + then printf "/j NickServ IDENTIFY %s\n" ${BOT_PASS} > ${II_DIR}/in ; sleep 2 ; fi + printf "/j %s\n" "${channel}" > ${II_DIR}/in -DBG_SET_MODE +DBG_RELAY_USER fi } @@ -140,27 +142,28 @@ DBG_RAW_LINE "${raw_line}" DBG_CRITERIA "${sender}" "${target}" "${query}" # ignore internal messages and chat from registered users - if ! is_filtered_channel "${target}" || - is_internal_user "${sender}" || + if ! is_filtered_channel "${target}" || \ + is_internal_user "${sender}" || \ is_public_chat "${sender}" "${query}" then was_handled=0 - else - -DBG_UNREGISTERED "${sender}" - # ignore chat that is known spam or otherwise nonsense - if is_nonsense "${query}" || is_spam "${query}" - then was_handled=1 + # ignore chat that is known spam or otherwise nonsense + elif is_nonsense "${query}" || is_spam "${query}" + then was_handled=1 DBG_SPAM ${sender} - # relay chat from an unregistered user to the channel - else - send_msg "${target}" "(${sender} said): ${query}" - #echo "(${sender} said): ${query}" > ${II_DIR}/${target}/in + # relay chat from an unregistered user to the channel + else local message="(${sender} said): ${query}" - was_handled=0 + if (( ${IS_RELAY_BOT_REGISTERED} )) + then echo "${message}" > ${II_DIR}/${target}/in + else send_msg "${target}" "${message}" fi + +DBG_RELAY_CHAT "${sender}" + + was_handled=0 fi # supress or allow further handling of this message @@ -218,10 +221,10 @@ is_spam() # (chat_msg) ## 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]: incoming raw_line=\n\t'$1'" ; } +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() { (( ${DEBUG} )) || return @@ -236,5 +239,5 @@ DBG_CRITERIA() is_nonsense "${query}" && echo -n " => is nonsense - returning" ; is_spam "${query}" && echo -n " => is known spam - 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_SPAM() { (( ${DEBUG} )) && echo "[SPAMFILTER]: !!!triggered!!! sender=$1" ; } +DBG_RELAY_CHAT() { (( ${DEBUG} )) && echo "[SPAMFILTER]: relaying chat from unregistered user sender=$1" ; } |