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
21 if @registry.has_key?(:bansmasks)
22 @whitelist = @registry[:bansmasks]
30 @registry[:bans] = @bansregexps
31 @registry[:bansmasks] = @whitelist
37 def help(plugin, topic="")
40 return "bans ban|quietban nick [channel] [timer]: bans|quietbans a nick from the channel for a specified time. bans kick nick [channel] [message]: kicks <nick> from <channel> with kick <message>. bans kickban nick [channel] [timer] [message]: kick+ban, same parameters as before. timer is always specified as <number><unit>, like 3m, 10s, 1h."
42 return "bans addwhitelist <mask>: adds <mask> to whitelist (NEVER regexp ban this). bans whitelist: shows current whitelist. bans delwhitelist <key>: delete whitelist entry <key>."
44 return "bans addregexp type [timer] regexp: listens for <regexp>, then do actions depending on <type> (\"quietban\" or \"kickban\", currently). If timer if specified, it removes the ban after timer expires. timer is always specified as <number><unit>, like 3m, 10s, 1h. bans delregexp <key>: remove watching for regexp <key>. bans listregexps: show current regexp watching list."
46 return "Topics: \"kickbans\", \"whitelist\", \"regexpbans\""
48 # 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"
52 def cmd_setmode(m, nick, channel, smode, time, umode )
53 channel=channel != '####currentchannel' ? channel : m.target
54 timercnt=/^(\d+)([smh])$/.match(time)[1]
55 timeru=/^(\d+)([smh])$/.match(time)[2]
56 timer = timercnt.to_i if timeru == "s"
57 timer = timercnt.to_i*60 if timeru == "m"
58 timer = timercnt.to_i*3600 if timeru == "h"
59 if timer > 0 then @bot.timer.add_once(timer, m ) {|m|
60 @bot.sendq("MODE #{channel} #{umode} #{nick}")
61 # m.reply("Undo mode")
64 @bot.sendq("MODE #{channel} #{smode} #{nick}")
65 # m.reply "ban cmd nick=#{nick} channel=#{channel} timer=#{timercnt} #{timeru} #{timer}"
67 def cmd_dokick(m, nick, channel, message)
68 channel=channel != '####currentchannel' ? channel : m.target
69 @bot.sendq("KICK #{channel} #{nick} :#{message}")
73 def cmd_kick(m,params)
74 cmd_dokick(m,params[:nick], params[:channel], params[:message])
76 def cmd_ban(m, params)
77 cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
79 def cmd_kickban(m,params)
80 cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
81 cmd_dokick(m,params[:nick], params[:channel], params[:message])
85 def cmd_quietban(m, params)
86 cmd_setmode(m, params[:nick], params[:channel], "+q", params[:timer], "-q")
88 def cmd_unban(m, params)
89 cmd_setmode(m, params[:nick], params[:channel], "-b", "0s", "")
91 def cmd_unquiet(m, params)
92 cmd_setmode(m, params[:nick], params[:channel], "-q", "0s", "")
96 if @bansregexps.length <= 0 then return end
97 @whitelist.each_key do |key|
98 if Irc.netmaskmatch(@whitelist[key], m.source) then
104 @bansregexps.each_key do |key|
105 match=@bansregexps[key][2]
106 if m.message =~ /^.*#{match}.*$/i then
107 case @bansregexps[key][0]
109 cmd_setmode(m, m.sourcenick, m.channel, "+q", @bansregexps[key][1], "-q")
112 cmd_setmode(m, m.sourcenick, m.channel, "+b", @bansregexps[key][1], "-b")
113 cmd_dokick(m, m.sourcenick, m.channel, "Autokick")
121 def cmd_addregexp(m, params)
122 toadd=Array[ params[:type], params[:timeout], "#{params[:regexp]}" ]
123 regsize=@bansregexps.length+1
124 # m.reply("Current registry size: #{regsize}")
126 @bansregexps[regsize]=toadd
128 # @bansregexps.store(toadd)
129 regsize=@bansregexps.length
130 # m.reply("New registry size: #{regsize}")
133 def cmd_listregexp(m, params)
134 if @bansregexps.length == 0
135 m.reply("No regexps stored."); return
137 @bansregexps.each_key do |key|
138 m.reply("Key: #{key}, type: #{@bansregexps[key][0]}, timeout: #{@bansregexps[key][1]}, pattern: #{@bansregexps[key][2]}")
143 def cmd_delregexp(m, params)
145 @bansregexps.each_key do |key|
146 if ( "#{key}" == "#{index}" ) then
147 @bansregexps.delete(key)
153 m.reply("Key #{index} not found.")
155 def cmd_whitelistadd(m, params)
156 regsize=@whitelist.length+1
157 @whitelist[regsize]=params[:netmask]
160 def cmd_whitelist(m, params)
161 if @whitelist.length == 0
162 m.reply("Whitelist is empty."); return
164 @whitelist.each_key do |key|
165 m.reply("Key: #{key}, netmask: #{@whitelist[key]}")
170 def cmd_whitelistdel(m, params)
172 @whitelist.each_key do |key|
173 if ( "#{key}" == "#{index}" ) then
174 @whitelist.delete(key)
180 m.reply("Key #{index} not found.")
183 plugin = BansPlugin.new
184 plugin.register("bans")
186 plugin.map 'bans whitelist', :action => 'cmd_whitelist', :auth => 'bans'
187 plugin.map 'bans delwhitelist :index', :action => 'cmd_whitelistdel', :auth => 'bans', :requirements => { :index => /^\d+$/ }
188 plugin.map 'bans addwhitelist :netmask', :action => 'cmd_whitelistadd', :auth => 'bans'
189 plugin.map 'bans delregexp :index', :action => 'cmd_delregexp', :auth => 'bans', :requirements => { :index => /^\d+$/ }
190 plugin.map 'bans addregexp :type :timeout *regexp', :action => 'cmd_addregexp', :auth => 'bans', :requirements => {:timeout => /^\d+[smh]$/, :type => /(quietban)|(kickban)/ }, :defaults => { :timeout => "0s" }
191 plugin.map 'bans listregexps', :action => 'cmd_listregexp', :auth => 'bans'
192 plugin.map 'bans ban :nick :channel :timer', :action => 'cmd_ban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}
193 plugin.map 'bans quiet :nick :channel :timer', :action => 'cmd_quietban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}
195 plugin.map 'bans kick :nick :channel *message', :action => 'cmd_kick', :auth => 'bans', :requirements => {:channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :message => 'Au revoir.'}
196 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'}
199 plugin.map 'bans unban :nick :channel', :action => 'cmd_unban', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel'}
200 plugin.map 'bans unquiet :nick :channel', :action => 'cmd_unquiet', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel'}
202 #plugin.map 'admin kick :nick :channel *message', :action => 'cmd_kick', :auth => 'admin'
203 #plugin.map 'admin kickban :nick :channel *message', :action => 'cmd_kickban' :auth => 'admin'
204 #plugin.register("quietban")
205 #plugin.register("kickban")