lastfm plugin: added now playing info to the help
[rbot] / data / rbot / plugins / autoop.rb
1 class AutoOP < Plugin
2   Config.register Config::BooleanValue.new('autoop.on_nick',
3     :default => true,
4     :desc => "Determines if the bot should auto-op when someone changes nick and the new nick matches a listed netmask")
5
6   def help(plugin, topic="")
7     return "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"
8   end
9
10   def join(m)
11     return if m.address?
12     @registry.each { |mask,channels|
13       if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
14         (channels.empty? || channels.include?(m.channel.to_s))
15         @bot.mode(m.channel, "+o", m.source.nick)
16         return
17       end
18     }
19   end
20
21   def nick(m)
22     return if m.address?
23     return unless @bot.config['autoop.on_nick']
24     is_on = m.server.channels.inject(ChannelList.new) { |list, ch|
25       list << ch if ch.users.include?(m.source)
26       list
27     }
28     is_on.each { |channel|
29       ch = channel.to_s
30       @registry.each { |mask,channels|
31         if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
32           (channels.empty? || channels.include?(ch))
33           @bot.mode(ch, "+o", m.source.nick)
34           return
35         end
36       }
37     }
38   end
39
40   def add(m, params)
41     if params[:channels].empty? || !@registry.has_key?(params[:mask])
42       # if the channels parameter is omitted (meaning all channels), or the
43       # hostmask isn't present in the registry, we just (over)write the channels
44       # in the registry
45       @registry[params[:mask]] = params[:channels].dup
46       m.okay
47     else
48       # otherwise, merge the channels with the ones existing in the registry
49       current_channels = @registry[params[:mask]]
50       if current_channels.empty?
51         m.reply "#{params[:mask]} is already being auto-opped on all channels"
52       else
53         # merge the already set channels
54         @registry[params[:mask]] = (params[:channels] | current_channels).uniq
55         m.okay
56       end
57     end
58   end
59
60   def rm(m, params)
61     unless @registry.has_key?(params[:mask])
62       m.reply @bot.lang.get('dunno')
63       return
64     end
65     if (!params[:channels].empty? && @registry[params[:mask]] != nil)
66       params[:channels].each do |c|
67         @registry[params[:mask]] = @registry[params[:mask]].reject {|ele| ele =~ /^#{c}$/i}
68       end
69       if @registry[params[:mask]].empty?
70         @registry.delete(params[:mask])
71       end
72     else
73       @registry.delete(params[:mask])
74     end
75     m.okay
76   end
77
78   def list(m, params)
79     debug @registry.length
80     if(@registry.length > 0)
81       @registry.each { |mask,channels|
82         m.reply "#{mask} in #{channels.empty? ? 'all channels' : channels.join(', ')}"
83       }
84     else
85       m.reply "No entries"
86     end
87   end
88 end
89
90 plugin = AutoOP.new
91
92 plugin.map 'autoop list', :action => 'list'
93 plugin.map 'autoop add :mask [*channels]', :action => 'add'
94 plugin.map 'autoop rm :mask [*channels]', :action => 'rm'