summaryrefslogtreecommitdiff
path: root/modules/m_spamfilter.sh
blob: f687cb405e20b83a7ade332585af407362ed758c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/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 <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: 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'
)


## 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}" ; }