summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/bans.rb
blob: 7f4b63eb15a29c6be2128e445263b6d52a9c5a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
#
# Managing kick and bans, automatically removing bans after timeouts, quiet bans, and kickban/quietban based on regexp
#
# Commands are little Ruby programs that run in the context of the command plugin. You
# can create them directly in an IRC channel, and invoke them just like normal rbot plugins.
#
# (c) 2006 Marco Gulino <marco@kmobiletools.org>
# Licensed under GPL V2.


class BansPlugin < Plugin
  def initialize
    super
     if @registry.has_key?(:bans)
       @bansregexps = @registry[:bans]
     else
       @bansregexps = Hash.new
     end
    end


    def save
#      @bot.action("#rck.train", @bansregexps.inspect)
      @registry[:bans] = @bansregexps
    end




  def help(plugin, topic="")
    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"
  end


  def cmd_setmode(m, nick, channel, smode, time, umode )
    channel=channel != '####currentchannel' ? channel : m.target
    timercnt=/^(\d+)([smh])$/.match(time)[1]
    timeru=/^(\d+)([smh])$/.match(time)[2]
    timer = timercnt.to_i if timeru == "s"
    timer = timercnt.to_i*60 if timeru == "m"
    timer = timercnt.to_i*3600 if timeru == "h"
    if timer > 0 then @bot.timer.add_once(timer, m ) {|m|
      @bot.sendq("MODE #{channel} #{umode} #{nick}")
      #		m.reply("Undo mode")
    } end

    @bot.sendq("MODE #{channel} #{smode} #{nick}")
    #	m.reply "ban cmd nick=#{nick} channel=#{channel} timer=#{timercnt} #{timeru} #{timer}"
  end
  def cmd_dokick(m, nick, channel, message)
      channel=channel != '####currentchannel' ? channel : m.target
      @bot.sendq("KICK #{channel} #{nick} :#{message}")
  end


  def cmd_kick(m,params)
    cmd_dokick(m,params[:nick], params[:channel], params[:message])
  end
  def cmd_ban(m, params)
    cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
  end
  def cmd_kickban(m,params)
      cmd_setmode(m, params[:nick], params[:channel], "+b", params[:timer], "-b")
      cmd_dokick(m,params[:nick], params[:channel], params[:message])
  end


  def cmd_quietban(m, params)
    cmd_setmode(m, params[:nick], params[:channel], "+q", params[:timer], "-q")
  end
  def cmd_unban(m, params)
    cmd_setmode(m, params[:nick], params[:channel], "-b", "0s", "")
  end
  def cmd_unquiet(m, params)
    cmd_setmode(m, params[:nick], params[:channel], "-q", "0s", "")
  end

  def listen(m)
    if @bansregexps.length <= 0 then return end
    @bansregexps.each_key do |key|
      match=@bansregexps[key][2]
      if m.message =~ /^.*#{match}.*$/i then
        case @bansregexps[key][0]
	when "quietban"
		cmd_setmode(m, m.sourcenick, m.channel, "+q", @bansregexps[key][1], "-q")
		return
	when "kickban"
                cmd_setmode(m, m.sourcenick, m.channel, "+b", @bansregexps[key][1], "-b")
		cmd_dokick(m, m.sourcenick, m.channel, "Autokick")
		return
	end
      end
      next
    end
  end

  def cmd_addregexp(m, params)
    toadd=Array[ params[:type], params[:timeout], "#{params[:regexp]}" ]
    regsize=@bansregexps.length+1
#    m.reply("Current registry size: #{regsize}")

    @bansregexps[regsize]=toadd
 
#    @bansregexps.store(toadd)
    regsize=@bansregexps.length
#    m.reply("New registry size: #{regsize}")
    m.reply("Done.")
  end
  def cmd_listregexp(m, params)
    if @bansregexps.length == 0
      m.reply("No regexps stored."); return
    end
    @bansregexps.each_key do |key|
	    m.reply("Key: #{key}, type: #{@bansregexps[key][0]}, timeout: #{@bansregexps[key][1]}, pattern: #{@bansregexps[key][2]}")
	    sleep 1
	    next
    end
  end
  def cmd_delregexp(m, params)
    index=params[:index]
    @bansregexps.each_key do |key|
	    if ( "#{key}" == "#{index}" ) then
		    @bansregexps.delete(key)
		    m.reply("Done.")
		    return
	    end
	    next
end
m.reply("Key #{index} not found")
#    unless @bansregexps.has_key?(index)
#      m.reply("Regexp \"#{index}\" doesn't exist"); return
#    end
#    m.reply("Done.")
  end
end
plugin = BansPlugin.new
plugin.register("bans")


plugin.map 'bans delregexp :index', :action => 'cmd_delregexp', :auth => 'bans', :requirements => { :index => /^\d+$/ }
plugin.map 'bans addregexp :type :timeout *regexp', :action => 'cmd_addregexp', :auth => 'bans', :requirements => {:timeout => /^\d+[smh]$/, :type => /(quietban)|(kickban)/ }, :defaults => { :timeout => "0s" }
plugin.map 'bans listregexps', :action => 'cmd_listregexp', :auth => 'bans'
plugin.map 'bans ban :nick :channel :timer', :action => 'cmd_ban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}
plugin.map 'bans quiet :nick :channel :timer', :action => 'cmd_quietban', :auth => 'bans', :requirements => {:timer => /^\d+[smh]$/, :channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :timer => '0s'}

plugin.map 'bans kick :nick :channel *message', :action => 'cmd_kick', :auth => 'bans', :requirements => {:channel => /^#+[^\s]+$/}, :defaults => {:channel => '####currentchannel', :message => 'Au revoir.'}
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'}


plugin.map 'bans unban :nick :channel', :action => 'cmd_unban', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel =>	'####currentchannel'}
plugin.map 'bans unquiet :nick :channel', :action => 'cmd_unquiet', :auth => 'bans', :requirements => { :channel => /^#+[^\s]+$/}, :defaults => {:channel =>	'####currentchannel'}

#plugin.map 'admin kick :nick :channel *message', :action => 'cmd_kick', :auth => 'admin'
#plugin.map 'admin kickban :nick :channel *message', :action => 'cmd_kickban' :auth => 'admin'
#plugin.register("quietban")
#plugin.register("kickban")