1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
3 # Managing kick and bans, automatically removing bans after timeouts, quiet bans, and kickban/quietban based on regexp
5 # Commands are little Ruby programs that run in the context of the command plugin. You
6 # can create them directly in an IRC channel, and invoke them just like normal rbot plugins.
8 # (c) 2006 Marco Gulino <marco@kmobiletools.org>
9 # Licensed under GPL V2.
12 class BansPlugin < Plugin
15 if @registry.has_key?(:bans)
16 @bansregexps = @registry[:bans]
18 @bansregexps = Hash.new
24 # @bot.action("#rck.train", @bansregexps.inspect)
25 @registry[:bans] = @bansregexps
31 def help(plugin, topic="")
32 return "Channel administration plugin. bans ban nick [channel] [timer]: bans a nick from the channel for a specified time; bans unban nick [channel]: removes the ban on <nick>; bans quiet nick [channel] [timer] and bans unquiet nick [channel]: same as ban and unban, but uses quiet ban instead. Timer is specified as 6s, 10m, 2h. If channel is not specified will use current channel.\nbans listregexps|addregexp type timeout regexp|delregexp: regexp banning management. Type can be quietban or kickban"
36 def cmd_setmode(m, nick, channel, smode, time, umode )
37 channel=channel != '####currentchannel' ? channel : m.target
38 timercnt=/^(\d+)([smh])$/.match(time)[1]
39 timeru=/^(\d+)([smh])$/.match(time)[2]
40 timer = timercnt.to_i if timeru == "s"
41 timer = timercnt.to_i*60 if timeru == "m"
42 timer = timercnt.to_i*3600 if timeru == "h"
43 if timer > 0 then @bot.timer.add_once(timer, m ) {|m|
44 @bot.sendq("MODE #{channel} #{umode} #{nick}")
45 # m.reply("Undo mode")
48 @bot.sendq("MODE #{channel} #{smode} #{nick}")
49 # m.reply "ban cmd nick=#{nick} channel=#{channel} timer=#{timercnt} #{timeru} #{timer}"
51 def cmd_dokick(m, nick, channel, message)
52 channel=channel != '####currentchannel' ? channel : m.target
53 @bot.sendq("KICK #{channel} #{nick} :#{message}")
57 def cmd_kick(m,params)
58 cmd_dokick(m,params[:nick], params[:channel], params[:message])
60 def cmd_ban(m, params)
61 cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
63 def cmd_kickban(m,params)
64 cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
65 cmd_dokick(m,params[:nick], params[:channel], params[:message])
69 def cmd_quietban(m, params)
70 cmd_setmode(m, params[:nick], params[:channel], "+q", params[:timer], "-q")
72 def cmd_unban(m, params)
73 cmd_setmode(m, params[:nick], params[:channel], "-b", "0s", "")
75 def cmd_unquiet(m, params)
76 cmd_setmode(m, params[:nick], params[:channel], "-q", "0s", "")
80 if @bansregexps.length <= 0 then return end
81 @bansregexps.each_key do |key|
82 match=@bansregexps[key][2]
83 if m.message =~ /^.*#{match}.*$/i then
84 case @bansregexps[key][0]
86 cmd_setmode(m, m.sourcenick, m.channel, "+q", @bansregexps[key][1], "-q")
89 cmd_setmode(m, m.sourcenick, m.channel, "+b", @bansregexps[key][1], "-b")
90 cmd_dokick(m, m.sourcenick, m.channel, "Autokick")
98 def cmd_addregexp(m, params)
99 toadd=Array[ params[:type], params[:timeout], "#{params[:regexp]}" ]
100 regsize=@bansregexps.length+1
101 # m.reply("Current registry size: #{regsize}")
103 @bansregexps[regsize]=toadd
105 # @bansregexps.store(toadd)
106 regsize=@bansregexps.length
107 # m.reply("New registry size: #{regsize}")
110 def cmd_listregexp(m, params)
111 if @bansregexps.length == 0
112 m.reply("No regexps stored."); return
114 @bansregexps.each_key do |key|
115 m.reply("Key: #{key}, type: #{@bansregexps[key][0]}, timeout: #{@bansregexps[key][1]}, pattern: #{@bansregexps[key][2]}")
120 def cmd_delregexp(m, params)
122 @bansregexps.each_key do |key|
123 if ( "#{key}" == "#{index}" ) then
124 @bansregexps.delete(key)
130 m.reply("Key #{index} not found")
131 # unless @bansregexps.has_key?(index)
132 # m.reply("Regexp \"#{index}\" doesn't exist"); return
137 plugin = BansPlugin.new
138 plugin.register("bans")
141 plugin.map 'bans delregexp :index', :action => 'cmd_delregexp', :auth => 'bans', :requirements => { :index => /^\d+$/ }
142 plugin.map 'bans addregexp :type :timeout *regexp', :action => 'cmd_addregexp', :auth => 'bans', :requirements => {:timeout => /^\d+[smh]$/, :type => /(quietban)|(kickban)/ }, :defaults => { :timeout => "0s" }
143 plugin.map 'bans listregexps', :action => 'cmd_listregexp', :auth => 'bans'
144 plugin.map 'bans ban :nick :channel :timer', :action => 'cmd_ban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}
145 plugin.map 'bans quiet :nick :channel :timer', :action => 'cmd_quietban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}
147 plugin.map 'bans kick :nick :channel *message', :action => 'cmd_kick', :auth => 'bans', :requirements => {:channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :message => 'Au revoir.'}
148 plugin.map 'bans kickban :nick :channel :timer *message', :action => 'cmd_kickban', :auth => 'bans', :requirements => {:channel => /^#+[^\s]+$/, :timer => /^\d+[smh]$/ }, :defaults => {:channel => '####currentchannel', :message => 'Au revoir.', :timer => '0s'}
151 plugin.map 'bans unban :nick :channel', :action => 'cmd_unban', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel'}
152 plugin.map 'bans unquiet :nick :channel', :action => 'cmd_unquiet', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel'}
154 #plugin.map 'admin kick :nick :channel *message', :action => 'cmd_kick', :auth => 'admin'
155 #plugin.map 'admin kickban :nick :channel *message', :action => 'cmd_kickban' :auth => 'admin'
156 #plugin.register("quietban")
157 #plugin.register("kickban")