]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/bans.rb
script echo needs a to_s
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / bans.rb
1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
2 #
3 # Managing kick and bans, automatically removing bans after timeouts, quiet bans, and kickban/quietban based on regexp
4 #
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.
7 #
8 # (c) 2006 Marco Gulino <marco@kmobiletools.org>
9 # Licensed under GPL V2.
10
11
12 class BansPlugin < Plugin
13   def initialize
14     super
15      if @registry.has_key?(:bans)
16        @bansregexps = @registry[:bans]
17      else
18        @bansregexps = Hash.new
19      end
20
21      if @registry.has_key?(:bansmasks)
22              @whitelist = @registry[:bansmasks]
23      else
24              @whitelist = Hash.new
25      end
26     end
27
28
29     def save
30       @registry[:bans] = @bansregexps
31       @registry[:bansmasks] = @whitelist
32     end
33
34
35
36
37   def help(plugin, topic="")
38           case topic
39           when "kickbans"
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."
41           when "whitelist"
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>."
43           when "regexpbans"
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."
45           else
46                   return "Topics: \"kickbans\", \"whitelist\", \"regexpbans\""
47           end
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"
49   end
50
51
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")
62     } end
63
64     @bot.sendq("MODE #{channel} #{smode} #{nick}")
65     #   m.reply "ban cmd nick=#{nick} channel=#{channel} timer=#{timercnt} #{timeru} #{timer}"
66   end
67   def cmd_dokick(m, nick, channel, message)
68       channel=channel != '####currentchannel' ? channel : m.target
69       @bot.sendq("KICK #{channel} #{nick} :#{message}")
70   end
71
72
73   def cmd_kick(m,params)
74     cmd_dokick(m,params[:nick], params[:channel], params[:message])
75   end
76   def cmd_ban(m, params)
77     cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
78   end
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])
82   end
83
84
85   def cmd_quietban(m, params)
86     cmd_setmode(m, params[:nick], params[:channel], "+q", params[:timer], "-q")
87   end
88   def cmd_unban(m, params)
89     cmd_setmode(m, params[:nick], params[:channel], "-b", "0s", "")
90   end
91   def cmd_unquiet(m, params)
92     cmd_setmode(m, params[:nick], params[:channel], "-q", "0s", "")
93   end
94
95   def listen(m)
96     if @bansregexps.length <= 0 then return end
97     @whitelist.each_key do |key|
98             if Irc.netmaskmatch(@whitelist[key], m.source) then
99                     return
100             end
101             next
102     end
103
104     @bansregexps.each_key do |key|
105       match=@bansregexps[key][2]
106       if m.message =~ /^.*#{match}.*$/i then
107         case @bansregexps[key][0]
108         when "quietban"
109                 cmd_setmode(m, m.sourcenick, m.channel, "+q", @bansregexps[key][1], "-q")
110                 return
111         when "kickban"
112                 cmd_setmode(m, m.sourcenick, m.channel, "+b", @bansregexps[key][1], "-b")
113                 cmd_dokick(m, m.sourcenick, m.channel, "Autokick")
114                 return
115         end
116       end
117       next
118     end
119   end
120
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}")
125
126     @bansregexps[regsize]=toadd
127  
128 #    @bansregexps.store(toadd)
129     regsize=@bansregexps.length
130 #    m.reply("New registry size: #{regsize}")
131     m.reply("Done.")
132   end
133   def cmd_listregexp(m, params)
134     if @bansregexps.length == 0
135       m.reply("No regexps stored."); return
136     end
137     @bansregexps.each_key do |key|
138             m.reply("Key: #{key}, type: #{@bansregexps[key][0]}, timeout: #{@bansregexps[key][1]}, pattern: #{@bansregexps[key][2]}")
139             sleep 1
140             next
141     end
142   end
143   def cmd_delregexp(m, params)
144     index=params[:index]
145     @bansregexps.each_key do |key|
146             if ( "#{key}" == "#{index}" ) then
147                     @bansregexps.delete(key)
148                     m.reply("Done.")
149                     return
150             end
151             next
152 end
153 m.reply("Key #{index} not found.")
154   end
155   def cmd_whitelistadd(m, params)
156     regsize=@whitelist.length+1
157     @whitelist[regsize]=params[:netmask]
158     m.reply("Done.")
159   end
160   def cmd_whitelist(m, params)
161       if @whitelist.length == 0
162               m.reply("Whitelist is empty."); return
163       end
164       @whitelist.each_key do |key|
165               m.reply("Key: #{key}, netmask: #{@whitelist[key]}")
166               sleep 1
167               next
168       end
169   end
170   def cmd_whitelistdel(m, params)
171     index=params[:index]
172     @whitelist.each_key do |key|
173             if ( "#{key}" == "#{index}" ) then
174                     @whitelist.delete(key)
175                     m.reply("Done.")
176                     return
177             end
178             next
179     end
180     m.reply("Key #{index} not found.")
181   end
182 end
183 plugin = BansPlugin.new
184 plugin.register("bans")
185
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'}
194
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'}
197
198
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'}
201
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")