]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/bans.rb
bans plugin from RockMan :D
[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     end
21
22
23     def save
24 #      @bot.action("#rck.train", @bansregexps.inspect)
25       @registry[:bans] = @bansregexps
26     end
27
28
29
30
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"
33   end
34
35
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")
46     } end
47
48     @bot.sendq("MODE #{channel} #{smode} #{nick}")
49     #   m.reply "ban cmd nick=#{nick} channel=#{channel} timer=#{timercnt} #{timeru} #{timer}"
50   end
51   def cmd_dokick(m, nick, channel, message)
52       channel=channel != '####currentchannel' ? channel : m.target
53       @bot.sendq("KICK #{channel} #{nick} :#{message}")
54   end
55
56
57   def cmd_kick(m,params)
58     cmd_dokick(m,params[:nick], params[:channel], params[:message])
59   end
60   def cmd_ban(m, params)
61     cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
62   end
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])
66   end
67
68
69   def cmd_quietban(m, params)
70     cmd_setmode(m, params[:nick], params[:channel], "+q", params[:timer], "-q")
71   end
72   def cmd_unban(m, params)
73     cmd_setmode(m, params[:nick], params[:channel], "-b", "0s", "")
74   end
75   def cmd_unquiet(m, params)
76     cmd_setmode(m, params[:nick], params[:channel], "-q", "0s", "")
77   end
78
79   def listen(m)
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]
85         when "quietban"
86                 cmd_setmode(m, m.sourcenick, m.channel, "+q", @bansregexps[key][1], "-q")
87                 return
88         when "kickban"
89                 cmd_setmode(m, m.sourcenick, m.channel, "+b", @bansregexps[key][1], "-b")
90                 cmd_dokick(m, m.sourcenick, m.channel, "Autokick")
91                 return
92         end
93       end
94       next
95     end
96   end
97
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}")
102
103     @bansregexps[regsize]=toadd
104  
105 #    @bansregexps.store(toadd)
106     regsize=@bansregexps.length
107 #    m.reply("New registry size: #{regsize}")
108     m.reply("Done.")
109   end
110   def cmd_listregexp(m, params)
111     if @bansregexps.length == 0
112       m.reply("No regexps stored."); return
113     end
114     @bansregexps.each_key do |key|
115             m.reply("Key: #{key}, type: #{@bansregexps[key][0]}, timeout: #{@bansregexps[key][1]}, pattern: #{@bansregexps[key][2]}")
116             sleep 1
117             next
118     end
119   end
120   def cmd_delregexp(m, params)
121     index=params[:index]
122     @bansregexps.each_key do |key|
123             if ( "#{key}" == "#{index}" ) then
124                     @bansregexps.delete(key)
125                     m.reply("Done.")
126                     return
127             end
128             next
129 end
130 m.reply("Key #{index} not found")
131 #    unless @bansregexps.has_key?(index)
132 #      m.reply("Regexp \"#{index}\" doesn't exist"); return
133 #    end
134 #    m.reply("Done.")
135   end
136 end
137 plugin = BansPlugin.new
138 plugin.register("bans")
139
140
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'}
146
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'}
149
150
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'}
153
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")