summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/autoop.rb
blob: a47375ca69d00e6ae5ccf096d07a4b1f961fdfa0 (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
class AutoOP < Plugin
    def help(plugin, topic="")
        "perform autoop based on hostmask - usage: add <hostmask> [channel channel ...], rm <hostmask> [channel], list - list current ops. If you don't specify which channels, all channels are assumed"
    end
    
    def join(m)
      return if m.address?
      @registry.each { |mask,channels|
        if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
            (channels.empty? || channels.include?(m.channel.to_s))
          @bot.mode(m.channel, "+o", m.source.nick)
          return
        end
      }
    end

    def nick(m)
      return if m.address?
      is_on = m.server.channels.inject(ChannelList.new) { |list, ch|
        list << ch if ch.users.include?(m.source)
        list
      }
      is_on.each { |channel|
        ch = channel.to_s
        @registry.each { |mask,channels|
          if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
            (channels.empty? || channels.include?(ch))
            @bot.mode(ch, "+o", m.source.nick)
            return
          end
        }
      }
    end

    def add(m, params)
      @registry[params[:mask]] = params[:channels].dup
      m.okay
    end

    def rm(m, params)
      unless @registry.has_key?(params[:mask])
        m.reply @bot.lang.get('dunno')
        return
      end
      if (!params[:channels].empty? && @registry[params[:mask]] != nil)
        params[:channels].each do |c|
          @registry[params[:mask]] = @registry[params[:mask]].reject {|ele| ele =~ /^#{c}$/i}
        end
        if @registry[params[:mask]].empty?
          @registry.delete(params[:mask])
        end
      else
        @registry.delete(params[:mask])
      end
      m.okay
    end

    def list(m, params)
      debug @registry.length
      if(@registry.length > 0)
        @registry.each { |mask,channels|
          m.reply "#{mask} in #{channels.empty? ? 'all channels' : channels.join(', ')}"
        }
      else
        m.reply "No entries"
      end
    end
end

plugin = AutoOP.new

plugin.map 'autoop list', :action => 'list'
plugin.map 'autoop add :mask *channels', :action => 'add'
plugin.map 'autoop add :mask', :action => 'add'
plugin.map 'autoop rm :mask *channels', :action => 'rm'
plugin.map 'autoop rm :mask', :action => 'rm'