lastfm plugin: refactoring
[rbot] / data / rbot / plugins / autoop.rb
1 class AutoOP < Plugin
2   BotConfig.register BotConfigBooleanValue.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     @registry[params[:mask]] = params[:channels].dup
42     m.okay
43   end
44
45   def rm(m, params)
46     unless @registry.has_key?(params[:mask])
47       m.reply @bot.lang.get('dunno')
48       return
49     end
50     if (!params[:channels].empty? && @registry[params[:mask]] != nil)
51       params[:channels].each do |c|
52         @registry[params[:mask]] = @registry[params[:mask]].reject {|ele| ele =~ /^#{c}$/i}
53       end
54       if @registry[params[:mask]].empty?
55         @registry.delete(params[:mask])
56       end
57     else
58       @registry.delete(params[:mask])
59     end
60     m.okay
61   end
62
63   def list(m, params)
64     debug @registry.length
65     if(@registry.length > 0)
66       @registry.each { |mask,channels|
67         m.reply "#{mask} in #{channels.empty? ? 'all channels' : channels.join(', ')}"
68       }
69     else
70       m.reply "No entries"
71     end
72   end
73 end
74
75 plugin = AutoOP.new
76
77 plugin.map 'autoop list', :action => 'list'
78 plugin.map 'autoop add :mask [*channels]', :action => 'add'
79 plugin.map 'autoop rm :mask [*channels]', :action => 'rm'