source: trunk/modules/flood.tcl @ 1143

Revision 1069, 6.5 KB checked in by james, 2 years ago (diff)

add channel-level flood checking too

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1# bMotion - Flood checking
2#
3
4
5###############################################################################
6# bMotion - an 'AI' TCL script for eggdrops
7# Copyright (C) James Michael Seward 2000-2008
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22###############################################################################
23
24# We're going to track flooding PER NICK globally, not per channel
25# If someone's flooding us in one place, we'll handle it for all channels
26# to stop them being annoying
27
28# HOW IT WORKS
29#
30# Track a score for each nick
31# Reduce the scores by 1 every 30 seconds
32# Matching a plugin is one point
33# Matching the SAME plugin as before is 3
34# Going over 7 will make the bot ignore 50% of what you would trigger
35# Going over 15 cuts you out completely
36
37# Message levels:
38#   log: Flood checks
39#     d: flood ticks
40#     2: flood additions/subtractions
41
42if {![info exists bMotion_flood_info]} {
43  set bMotion_flood_info(_) 0
44  set bMotion_flood_last(_) ""
45  set bMotion_flood_lasttext(_) ""
46  set bMotion_flood_note ""
47  set bMotion_flood_undo 0
48}
49
50proc bMotion_flood_tick { } { 
51  bMotion_putloglev 4 * "bMotion: flood tick"
52        utimer 30 bMotion_flood_tick
53  #tick all values down one, to zero
54  global bMotion_flood_info bMotion_flood_last bMotion_flood_lasttext
55  set stats ""
56  foreach element [array names bMotion_flood_info] {
57    set val $bMotion_flood_info($element)
58    incr val -2
59    if {$val < 0} {
60      catch {
61        unset bMotion_flood_info($element)
62      }
63      catch {
64        unset bMotion_flood_last($element)
65      }
66      catch {
67        unset bMotion_flood_lasttext($element)
68      }
69      bMotion_putloglev 2 * "bMotion: flood tick: $element removed"
70    } else {
71      append stats "$element:\002$val\002 "
72      set bMotion_flood_info($element) $val
73    }
74  }
75  if {$stats != ""} {
76    bMotion_putloglev d * "bMotion: flood tick: $stats"
77  }
78}
79
80proc bMotion_flood_add { nick { callback "" } { text "" } } {
81  global bMotion_flood_info bMotion_flood_last bMotion_flood_lasttext bMotion_flood_last bMotion_flood_undo
82
83  set val 1
84        if {[string index $nick 0] != "#"} {
85                if [validuser $nick] {
86                        set handle $nick
87                } else {
88                        set handle [nick2hand $nick]
89                        if {$handle == "*"} {
90                                set handle $nick
91                        }
92                        bMotion_putloglev d * "Using handle $handle as flood target (was $nick)"
93                }
94        } else {
95                bMotion_putloglev d * "Using channel $nick as flood target"
96                set handle $nick
97        }
98  set lastCallback ""
99  catch {
100    set lastCallback $bMotion_flood_last($handle)
101  }
102  if {$callback != ""} {
103    set bMotion_flood_last($handle) $callback
104    if {$lastCallback == $callback} {
105      #naughty
106      set val 3
107    }
108  }
109
110  set lastText ""
111  catch {
112    set lastText $bMotion_flood_lasttext($handle)
113  }
114  if {$text != ""} {
115    set bMotion_flood_lasttext($handle) $text
116    #putlog "now: $text, last: $lastText"
117    if {$lastText == $text} {
118      #naughty
119      incr val 2
120    }
121  }
122
123  set flood 0
124  catch {
125    set flood $bMotion_flood_info($handle)
126  }
127        set oldflood $flood
128  incr flood $val
129  if {$flood > 40} {
130    set flood 40
131  }
132  bMotion_putloglev 2 * "bMotion: flood $oldflood -- $val --> $flood for $nick"
133        bMotion_putloglev 3 * "flood was added by plugin $callback"
134
135  set bMotion_flood_info($handle) $flood
136  set bMotion_flood_undo $val
137}
138
139proc bMotion_flood_clear { nick } {
140  global bMotion_flood_info bMotion_flood_last
141        bMotion_putloglev d * "Cleared flood for $nick"
142  set bMotion_flood_info($nick) 0
143  set bMotion_flood_last($nick) ""
144}
145
146proc bMotion_flood_remove { nick } {
147  global bMotion_flood_info
148  set val 1
149  if [validuser $nick] {
150    set handle $nick
151  } else {
152    set handle [nick2hand $nick]
153    if {$handle == "*"} {
154      set handle $nick
155    }
156  }
157  set flood 0
158  catch {
159    set flood $bMotion_flood_info($handle)
160  }
161  incr flood -1
162  if {$flood < 0} {
163    return 0
164  }
165  bMotion_putloglev 2 * "bMotion: flood removed 1 from $nick, now $flood"
166  set bMotion_flood_info($handle) $flood
167}
168
169proc bMotion_flood_undo { nick } {
170  global bMotion_flood_undo bMotion_flood_info bMotion_flood_lasttext
171  set val $bMotion_flood_undo
172
173  if {$val <= 1} {
174    return 0
175  }
176
177  if [validuser $nick] {
178    set handle $nick
179  } else {
180    set handle [nick2hand $nick]
181    if {$handle == "*"} {
182      set handle $nick
183    }
184  }
185
186  set flood 0
187  catch {
188    set flood $bMotion_flood_info($handle)
189  }
190        set oldflood $flood
191  incr flood [expr 0 - $val]
192  if {$flood < 0} {
193    set flood 0
194  }
195
196  set bMotion_flood_info($handle) $flood
197  set bMotion_flood_lasttext($handle) ""
198  set bMotion_flood_undo 1
199  bMotion_putloglev 2 * "bMotion: undid flood $oldflood -- $val --> $flood from $nick"
200  return 0
201}
202
203proc bMotion_flood_get { nick } {
204        global bMotion_flood_info
205        if {[string index $nick 0] != "#"} {
206                if [validuser $nick] {
207                        set handle $nick
208                } else {
209                        set handle [nick2hand $nick]
210                        if {$handle == "*"} {
211                                set handle $nick
212                        }
213                }
214        } else {
215                set handle $nick
216        }
217        set flood 0
218        catch {
219    set flood $bMotion_flood_info($handle)
220  }
221  return $flood
222}
223
224proc bMotion_flood_check { nick } {
225  if { [bMotion_setting_get "disableFloodChecks"] != "" } {
226    if { [bMotion_setting_get "disableFloodChecks"] == 1 } {
227      return 0
228    } 
229  } 
230
231  if {[bMotion_setting_get "bitlbee"]} {
232    return 0
233  }
234
235  bMotion_putloglev 3 * "checking flood for $nick"
236  set flood [bMotion_flood_get $nick]
237  set chance 2
238
239        if {[string index $nick 0] == "#"} {
240                set is_chan 1
241        } else {
242                set is_chan 0
243        }
244
245        if {!$is_chan} {
246                if {$flood > 35} {
247                        set chance -1
248                }
249
250                if {$flood > 25} {
251                        set chance -1
252                }
253
254                if {$flood > 15} {
255                        set chance 1
256                }
257                set r [rand 2]
258                if {!($r < $chance)} {
259                        putlog "bMotion: FLOOD check on $nick (http://www.bmotion.net:8000/bmotion/wiki/FAQDisableFlood)"
260                        return 1
261                }
262        } else {
263                if {$flood > 35} {
264                        putlog "bMotion: FLOOD protection for $nick (http://www.bmotion.net:8000/bmotion/wiki/FAQDisableFlood)"
265                        return 1
266                }
267        }
268  return 0
269}
270
271#bind time - "* * * * *" bMotion_flood_tick
272utimer 30 bMotion_flood_tick
Note: See TracBrowser for help on using the repository browser.