4 # :title: Bans Plugin v3 for rbot 0.9.11 and later
\r
6 # Author:: Marco Gulino <marco@kmobiletools.org>
\r
7 # Author:: kamu <mr.kamu@gmail.com>
\r
8 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
\r
10 # Copyright:: (C) 2006 Marco Gulino
\r
11 # Copyright:: (C) 2007 kamu, Giuseppe Bilotta
\r
15 # Managing kick and bans, automatically removing bans after timeouts, quiet
\r
16 # bans, and kickban/quietban based on regexp
\r
18 # v1 -> v2 (kamu's version, never released)
\r
20 # * autoactions triggered on join
\r
21 # * action on join or badword can be anything: kick, ban, kickban, quiet
\r
24 # * remove the 'bans' prefix from most of the commands
\r
25 # * (un)quiet has been renamed to (un)silence because 'quiet' was used to
\r
26 # tell the bot to keep quiet
\r
27 # * both (un)quiet and (un)silence are accepted as actions
\r
28 # * use the more descriptive 'onjoin' term for autoactions
\r
29 # * convert v1's (0.9.10) :bans and :bansmasks to BadWordActions and
\r
31 # * enhanced list manipulation facilities
\r
32 # * fixed regexp usage in requirements for plugin map
\r
33 # * add proper auth management
\r
35 OnJoinAction = Struct.new("OnJoinAction", :host, :action, :channel, :reason)
\r
36 BadWordAction = Struct.new("BadWordAction", :regexp, :action, :channel, :timer, :reason)
\r
37 WhitelistEntry = Struct.new("WhitelistEntry", :host, :channel)
\r
40 class BansPlugin < Plugin
\r
43 TimerRe = /^\d+[smhd]$/
\r
44 ChannelRe = /^#+[^\s]+$/
\r
45 ChannelAllRe = /^(?:all|#+[^\s]+)$/
\r
46 ActionRe = /(?:ban|kick|kickban|silence|quiet)/
\r
52 def make_badword_rx(txt)
\r
53 return /\b(?:#{txt})\b/i
\r
59 # Convert old BadWordActions, which were simpler and labelled :bans
\r
60 if @registry.has_key? :bans
\r
61 badwords = Array.new
\r
62 bans = @registry[:bans]
\r
63 @registry[:bans].each { |ar|
\r
71 warn "Unknown action in old data #{ar.inspect} -- entry ignored"
\r
75 chan = ar[1].downcase
\r
76 regexp = make_badword_rx(ar[2])
\r
77 badwords << BadWordAction.new(regexp, action, chan, "0s", "")
\r
79 @registry[:badwords] = badwords
\r
81 # Store the ones we couldn't convert
\r
82 @registry[:bans] = bans
\r
84 @registry.delete(:bans)
\r
87 @registry[:badwords] = Array.new unless @registry.has_key? :badwords
\r
90 # Convert old WhitelistEntries, which were simpler and labelled :bansmasks
\r
91 if @registry.has_key? :bans
\r
93 @registry[:bansmasks].each { |mask|
\r
94 badwords << WhitelistEntry.new(mask, "all")
\r
96 @registry[:whitelist] = wl
\r
97 @registry.delete(:bansmasks)
\r
99 @registry[:whitelist] = Array.new unless @registry.has_key? :whitelist
\r
102 @registry[:onjoin] = Array.new unless @registry.has_key? :onjoin
\r
105 def help(plugin, topic="")
\r
108 return "ban <nick/hostmask> [Xs/m/h/d] [#channel]: ban a user from the given channel for the given amount of time. default is forever, on the current channel"
\r
110 return "unban <nick/hostmask> [#channel]: unban a user from the given channel. defaults to the current channel"
\r
112 return "kick <nick> [#channel] [reason ...]: kick a user from the given channel with the given reason. defaults to the current channel, no reason"
\r
114 return "kickban <nick> [Xs/m/h/d] [#channel] [reason ...]: kicks and bans a user from the given channel for the given amount of time, with the given reason. default is forever, on the current channel, with no reason"
\r
116 return "silence <nick/hostmask> [Xs/m/h/d] [#channel]: silence a user on the given channel for the given time. default is forever, on the current channel. not all servers support silencing users"
\r
118 return "unsilence <nick/hostmask> [#channel]: allow the given user to talk on the given channel. defaults to the current channel"
\r
122 return "bans add <onjoin|badword|whitelist>: add an automatic action for people that join or say some bad word, or a whitelist entry. further help available"
\r
124 return "bans add onjoin <hostmask> [action] [#channel] [reason ...]: will add an autoaction for any one who joins with hostmask. default action is silence, default channel is all"
\r
126 return "bans add badword <regexp> [action] [Xs/m/h/d] [#channel|all] [reason ...]: adds a badword regexp, if a user sends a message that matches regexp, the action will be invoked. default action is silence, default channel is all"
\r
127 when "add whitelist"
\r
128 return "bans add whitelist <hostmask> [#channel|all]: add the given hostmask to the whitelist. no autoaction will be triggered by users on the whitelist"
\r
130 return "bans rm <onjoin|badword|whitelist> <hostmask/regexp> [#channel], or bans rm <onjoin|badword|whitelist> index <num>: removes the specified onjoin or badword rule or whitelist entry."
\r
132 return"bans list <onjoin|badword|whitelist>: lists all onjoin or badwords or whitelist entries"
\r
135 return "bans <command>: allows a user of the bot to do a range of bans and unbans. commands are: [un]ban, kick[ban], [un]silence, add, rm and list"
\r
139 return unless m.respond_to?(:public?) and m.public?
\r
140 @registry[:whitelist].each { |white|
\r
141 next unless ['all', m.target.downcase].include?(white.channel)
\r
142 return if m.source.matches?(white.host)
\r
145 @registry[:badwords].each { |badword|
\r
146 next unless ['all', m.target.downcase].include?(badword.channel)
\r
147 next unless badword.regexp.match(m.message)
\r
149 do_cmd(badword.action.to_sym, m.source.nick, m.target, badword.timer, badword.reason)
\r
150 m.reply "bad word detected! #{badword.action} for #{badword.timer} because: #{badword.reason}"
\r
156 @registry[:whitelist].each { |white|
\r
157 next unless ['all', m.target.downcase].include?(white.channel)
\r
158 return if m.source.matches?(white.host)
\r
161 @registry[:onjoin].each { |auto|
\r
162 next unless ['all', m.target.downcase].include?(auto.channel)
\r
163 next unless m.source.matches? auto.host
\r
165 do_cmd(auto.action.to_sym, m.source.nick, m.target, "0s", auto.reason)
\r
170 def ban_user(m, params=nil)
\r
171 nick, channel = params[:nick], check_channel(m, params[:channel])
\r
172 timer = params[:timer]
\r
173 do_cmd(:ban, nick, channel, timer)
\r
176 def unban_user(m, params=nil)
\r
177 nick, channel = params[:nick], check_channel(m, params[:channel])
\r
178 do_cmd(:unban, nick, channel)
\r
181 def kick_user(m, params=nil)
\r
182 nick, channel = params[:nick], check_channel(m, params[:channel])
\r
183 reason = params[:reason].to_s
\r
184 do_cmd(:kick, nick, channel, "0s", reason)
\r
187 def kickban_user(m, params=nil)
\r
188 nick, channel, reason = params[:nick], check_channel(m, params[:channel])
\r
189 timer, reason = params[:timer], params[:reason].to_s
\r
190 do_cmd(:kickban, nick, channel, timer, reason)
\r
193 def silence_user(m, params=nil)
\r
194 nick, channel = params[:nick], check_channel(m, params[:channel])
\r
195 timer = params[:timer]
\r
196 do_cmd(:silence, nick, channel, timer)
\r
199 def unsilence_user(m, params=nil)
\r
200 nick, channel = params[:nick], check_channel(m, params[:channel])
\r
201 do_cmd(:unsilence, nick, channel)
\r
204 def add_onjoin(m, params=nil)
\r
206 host, channel = m.server.new_netmask(params[:host]), params[:channel].downcase
\r
207 action, reason = params[:action], params[:reason].to_s
\r
209 autos = @registry[:onjoin]
\r
210 autos << OnJoinAction.new(host, action, channel, reason.dup)
\r
211 @registry[:onjoin] = autos
\r
220 def list_onjoin(m, params=nil)
\r
221 m.reply "onjoin rules: #{@registry[:onjoin].length}"
\r
222 @registry[:onjoin].each_with_index { |auto, idx|
\r
223 m.reply "\##{idx+1}: #{auto.host} | #{auto.action} | #{auto.channel} | '#{auto.reason}'"
\r
227 def rm_onjoin(m, params=nil)
\r
228 autos = @registry[:onjoin]
\r
229 count = autos.length
\r
232 idx = params[:idx].to_i if params[:idx]
\r
236 m.reply "No such onjoin \##{idx}"
\r
239 autos.delete_at(idx-1)
\r
242 host = m.server.new_netmask(params[:host])
\r
243 channel = params[:channel].downcase
\r
245 autos.each { |rule|
\r
246 next unless ['all', rule.channel].include?(channel)
\r
247 autos.delete rule if rule.host == host
\r
254 @registry[:onjoin] = autos
\r
255 if count > autos.length
\r
258 m.reply "No matching onjoin rule for #{host} found"
\r
262 def add_badword(m, params=nil)
\r
263 regexp, channel = make_badword_rx(params[:regexp]), params[:channel].downcase.dup
\r
264 action, timer, reason = params[:action], params[:timer].dup, params[:reason].to_s
\r
266 badwords = @registry[:badwords]
\r
267 badwords << BadWordAction.new(regexp, action, channel, timer, reason)
\r
268 @registry[:badwords] = badwords
\r
273 def list_badword(m, params=nil)
\r
274 m.reply "badword rules: #{@registry[:badwords].length}"
\r
276 @registry[:badwords].each_with_index { |badword, idx|
\r
277 m.reply "\##{idx+1}: #{badword.regexp.source} | #{badword.action} | #{badword.channel} | #{badword.timer} | #{badword.reason}"
\r
281 def rm_badword(m, params=nil)
\r
282 badwords = @registry[:badwords]
\r
283 count = badwords.length
\r
286 idx = params[:idx].to_i if params[:idx]
\r
290 m.reply "No such badword \##{idx}"
\r
293 badwords.delete_at(idx-1)
\r
295 channel = params[:channel].downcase
\r
297 regexp = make_badword_rx(params[:regexp])
\r
298 debug "Trying to remove #{regexp.inspect} from #{badwords.inspect}"
\r
300 badwords.each { |badword|
\r
301 next unless ['all', badword.channel].include?(channel)
\r
302 debug "Removing #{badword.inspect}" if badword == regexp
\r
303 badwords.delete(badword) if badword == regexp
\r
307 @registry[:badwords] = badwords
\r
308 if count > badwords.length
\r
311 m.reply "No matching badword #{regexp} found"
\r
315 def add_whitelist(m, params=nil)
\r
317 host, channel = m.server.new_netmask(params[:host]), params[:channel].downcase
\r
319 # TODO check if a whitelist entry for this host already exists
\r
320 whitelist = @registry[:whitelist]
\r
321 whitelist << WhitelistEntry.new(host, channel)
\r
322 @registry[:whitelist] = whitelist
\r
331 def list_whitelist(m, params=nil)
\r
332 m.reply "whitelist entries: #{@registry[:whitelist].length}"
\r
333 @registry[:whitelist].each_with_index { |auto, idx|
\r
334 m.reply "\##{idx+1}: #{auto.host} | #{auto.channel}"
\r
338 def rm_whitelist(m, params=nil)
\r
339 wl = @registry[:whitelist]
\r
343 idx = params[:idx].to_i if params[:idx]
\r
347 m.reply "No such whitelist entry \##{idx}"
\r
350 wl.delete_at(idx-1)
\r
353 host = m.server.new_netmask(params[:host])
\r
354 channel = params[:channel].downcase
\r
357 next unless ['all', rule.channel].include?(channel)
\r
358 wl.delete rule if rule.host == host
\r
365 @registry[:whitelist] = wl
\r
366 if count > whitelist.length
\r
369 m.reply "No host matching #{host}"
\r
374 def check_channel(m, strchannel)
\r
376 raise "must specify channel if using privmsg" if m.private? and not strchannel
\r
377 channel = m.server.channel(strchannel) || m.target
\r
378 raise "I am not in that channel" unless channel.has_user?(@bot.nick)
\r
387 def do_cmd(action, nick, channel, timer_in=nil, reason=nil)
\r
394 timer = $1.to_i * 60
\r
396 timer = $1.to_i * 60 * 60
\r
398 timer = $1.to_i * 60 * 60 * 24
\r
400 raise "Wrong time specifications"
\r
405 set_mode(channel, "+b", nick)
\r
406 @bot.timer.add_once(timer) { set_mode(channel, "-b", nick) } if timer > 0
\r
408 set_mode(channel, "-b", nick)
\r
410 do_kick(channel, nick, reason)
\r
412 set_mode(channel, "+b", nick)
\r
413 @bot.timer.add_once(timer) { set_mode(channel, "-b", nick) } if timer > 0
\r
414 do_kick(channel, nick, reason)
\r
415 when :silence, :quiet
\r
416 set_mode(channel, "+q", nick)
\r
417 @bot.timer.add_once(timer) { set_mode(channel, "-q", nick) } if timer > 0
\r
418 when :unsilence, :unquiet
\r
419 set_mode(channel, "-q", nick)
\r
423 def set_mode(channel, mode, nick)
\r
424 host = channel.has_user?(nick) ? "*!*@" + channel.get_user(nick).host : nick
\r
425 @bot.mode(channel, mode, host)
\r
428 def do_kick(channel, nick, reason="")
\r
429 @bot.kick(channel, nick, reason)
\r
433 plugin = BansPlugin.new
\r
435 plugin.default_auth( 'act', false )
\r
436 plugin.default_auth( 'edit', false )
\r
437 plugin.default_auth( 'list', true )
\r
439 plugin.map 'ban :nick :timer :channel', :action => 'ban_user',
\r
440 :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},
\r
441 :defaults => {:timer => nil, :channel => nil},
\r
442 :auth_path => 'act'
\r
443 plugin.map 'unban :nick :channel', :action => 'unban_user',
\r
444 :requirements => {:channel => BansPlugin::ChannelRe},
\r
445 :defaults => {:channel => nil},
\r
446 :auth_path => 'act'
\r
447 plugin.map 'kick :nick :channel *reason', :action => 'kick_user',
\r
448 :requirements => {:channel => BansPlugin::ChannelRe},
\r
449 :defaults => {:channel => nil, :reason => 'requested'},
\r
450 :auth_path => 'act'
\r
451 plugin.map 'kickban :nick :timer :channel *reason', :action => 'kickban_user',
\r
452 :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},
\r
453 :defaults => {:timer => nil, :channel => nil, :reason => 'requested'},
\r
454 :auth_path => 'act'
\r
455 plugin.map 'silence :nick :timer :channel', :action => 'silence_user',
\r
456 :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},
\r
457 :defaults => {:timer => nil, :channel => nil},
\r
458 :auth_path => 'act'
\r
459 plugin.map 'unsilence :nick :channel', :action => 'unsilence_user',
\r
460 :requirements => {:channel => BansPlugin::ChannelRe},
\r
461 :defaults => {:channel => nil},
\r
462 :auth_path => 'act'
\r
464 plugin.map 'bans add onjoin :host :action :channel *reason', :action => 'add_onjoin',
\r
465 :requirements => {:action => BansPlugin::ActionRe, :channel => BansPlugin::ChannelAllRe},
\r
466 :defaults => {:action => 'kickban', :channel => 'all', :reason => 'netmask not welcome'},
\r
467 :auth_path => 'edit::onjoin'
\r
468 plugin.map 'bans rm onjoin index :idx', :action => 'rm_onjoin',
\r
469 :requirements => {:num => BansPlugin::IdxRe},
\r
470 :auth_path => 'edit::onjoin'
\r
471 plugin.map 'bans rm onjoin :host :channel', :action => 'rm_onjoin',
\r
472 :requirements => {:channel => BansPlugin::ChannelAllRe},
\r
473 :defaults => {:channel => 'all'},
\r
474 :auth_path => 'edit::onjoin'
\r
475 plugin.map 'bans list onjoin[s]', :action => 'list_onjoin',
\r
476 :auth_path => 'list::onjoin'
\r
478 plugin.map 'bans add badword :regexp :action :timer :channel *reason', :action => 'add_badword',
\r
479 :requirements => {:action => BansPlugin::ActionRe, :timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelAllRe},
\r
480 :defaults => {:action => 'silence', :timer => "0s", :channel => 'all', :reason => 'bad word'},
\r
481 :auth_path => 'edit::badword'
\r
482 plugin.map 'bans rm badword index :idx', :action => 'rm_badword',
\r
483 :requirements => {:num => BansPlugin::IdxRe},
\r
484 :auth_path => 'edit::badword'
\r
485 plugin.map 'bans rm badword :regexp :channel', :action => 'rm_badword',
\r
486 :requirements => {:channel => BansPlugin::ChannelAllRe},
\r
487 :defaults => {:channel => 'all'},
\r
488 :auth_path => 'edit::badword'
\r
489 plugin.map 'bans list badword[s]', :action => 'list_badword',
\r
490 :auth_path => 'list::badword'
\r
492 plugin.map 'bans add whitelist :host :channel', :action => 'add_whitelist',
\r
493 :requirements => {:channel => BansPlugin::ChannelAllRe},
\r
494 :defaults => {:channel => 'all'},
\r
495 :auth_path => 'edit::whitelist'
\r
496 plugin.map 'bans rm whitelist index :idx', :action => 'rm_whitelist',
\r
497 :requirements => {:num => BansPlugin::IdxRe},
\r
498 :auth_path => 'edit::whitelist'
\r
499 plugin.map 'bans rm whitelist :host :channel', :action => 'rm_whitelist',
\r
500 :requirements => {:channel => BansPlugin::ChannelAllRe},
\r
501 :defaults => {:channel => 'all'},
\r
502 :auth_path => 'edit::whitelist'
\r
503 plugin.map 'bans list whitelist', :action => 'list_whitelist',
\r
504 :auth_path => 'list::whitelist'
\r