#!/bin/bash ########################################################################### # # # envbot - an IRC bot in bash # # Copyright (C) 2007-2008 Arvid Norlander # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ########################################################################### #-------------------------------------------------------------------------# ## 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}" readonly BOT_PASS="${config_server_passwd}" # known spam trigger constants readonly SPAM=( 'blog by freenode staff' 'IRC investigative journalists' 'IRC ad service' "Australia's #1 hacker" 'SP9002_@efnet' '/!\ ATTN: ' 'acquisition by Private' '"denial" on the freenode blog' ' ICO scam' 'Oh, and about those donations she speaks of:' 'kaniini has invited you' 'watch the live defcon' 'All told, Handshake' 'Handshake cryptocurrency scam' "Christel and Andrew Lee (PIA's founder)" 'is actively peddling this scam' 'Allah is doing' 'permission of allah' 'Allah takes away the' 'the best amongst you are those who learn and teach quran' 'rampjoulaboy' 'youtube.com' 'LRH OFFICIAL:' ) ## setup/teardown ## 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." } module_spamfilter_UNLOAD() { return 0 } module_spamfilter_REHASH() { return 0 } ## events ## module_spamfilter_on_JOIN() { local whojoined= local channel=$2 local bot_nick=${server_nick_current} parse_hostmask_nick "$1" 'whojoined' DBG_JOIN "${whojoined}" "${channel}" if [[ "${whojoined}" == "${bot_nick}" ]] && \ [[ " ${SPAMFILTER_CHANNELS} " =~ " ${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 else echo "ii is not installed" fi DBG_SET_MODE fi } module_spamfilter_on_raw() # (raw_line) { 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 was_handled DBG_CRITERIA "${sender}" "${target}" "${query}" # 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}" # ignore chat that is known spam or otherwise nonsense if 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 was_handled=0 fi fi # supress or allow further handling of this message return ${was_handled} } ## helpers ## 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 for straw in "${haystack[@]}"; do [[ ${needle} = *"${straw}"* ]] && return 0 ; done ; return 1 } ## 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_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" ; 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}" ; }