]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/autoop.rb
plugin(alias): use registry for storage see #42
[user/henk/code/ruby/rbot.git] / 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 " +
5              "and the new nick matches a listed netmask")
6
7   Config.register Config::StringValue.new('autoop.seed_format',
8     :default => "*!%{user}@*",
9     :desc => "Hostmask format used when seeding channels. Recognized tokens: " +
10              "nick, user, host")
11
12   def help(plugin, topic="")
13     return "perform autoop based on hostmask - usage:" +
14            "add <hostmask> [channel channel ...], rm <hostmask> [channel], " +
15              "If you don't specify which channels, all channels are assumed, " +
16            "list - list current ops, " +
17            "restore [channel] - op anybody that would " +
18              "have been opped if they had just joined, " +
19            "seed [channel] - Find current ops and make sure they will " +
20              "continue to be autoopped"
21   end
22
23   def join(m)
24     return if m.address?
25     @registry.each { |mask,channels|
26       if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
27         (channels.empty? || channels.include?(m.channel.to_s))
28         @bot.mode(m.channel, "+o", m.source.nick)
29         return
30       end
31     }
32   end
33
34   def nick(m)
35     return if m.address?
36     return unless @bot.config['autoop.on_nick']
37     is_on = m.server.channels.inject(ChannelList.new) { |list, ch|
38       list << ch if ch.users.include?(m.source)
39       list
40     }
41     is_on.each { |channel|
42       ch = channel.to_s
43       @registry.each { |mask,channels|
44         if m.source.matches?(mask.to_irc_netmask(:server => m.server)) &&
45           (channels.empty? || channels.include?(ch))
46           @bot.mode(ch, "+o", m.source.nick)
47           return
48         end
49       }
50     }
51   end
52
53   def add(m, params)
54     if params[:channels].empty? || !@registry.has_key?(params[:mask])
55       # if the channels parameter is omitted (meaning all channels), or the
56       # hostmask isn't present in the registry, we just (over)write the channels
57       # in the registry
58       @registry[params[:mask]] = params[:channels].dup
59       m.okay
60     else
61       # otherwise, merge the channels with the ones existing in the registry
62       current_channels = @registry[params[:mask]]
63       if current_channels.empty?
64         m.reply "#{params[:mask]} is already being auto-opped on all channels"
65       else
66         # merge the already set channels
67         @registry[params[:mask]] = (params[:channels] | current_channels).uniq
68         m.okay
69       end
70     end
71   end
72
73   def seed(m, params)
74     chan = params[:channel]
75     if chan == nil
76       if m.public?
77         chan = m.channel
78       else
79         m.reply _("Either specify a channel to seed, or ask in public")
80       end
81     end
82
83     current_ops = @bot.server.channel(chan).users.select { |u|
84         u.is_op?(chan) and u.nick != @bot.nick
85     }
86
87     netmasks = current_ops.map { |u|
88       @bot.config['autoop.seed_format'] % {
89         :user => u.user,
90         :nick => u.nick,
91         :host => u.host
92       }
93     }.uniq
94
95     to_add = netmasks.select { |mask|
96         @registry.key?(mask) == false or @registry[mask].empty? == false
97     }
98
99     if to_add.empty?
100       m.reply _("Nobody to add")
101       return
102     end
103
104     results = []
105     to_add.each { |mask|
106       if @registry.key? mask
107         if @registry[mask].include? chan
108           next
109         else
110           current_channels = @registry[mask].dup
111           @registry[mask] = ([chan] | current_channels).uniq
112           results << _("Added #{mask} in #{chan}")
113         end
114       else
115         @registry[mask] = [chan]
116         results << _("Created autoop entry for #{mask} and added #{chan}")
117       end
118     }
119     m.reply results.join ". "
120   end
121
122   def rm(m, params)
123     unless @registry.has_key?(params[:mask])
124       m.reply @bot.lang.get('dunno')
125       return
126     end
127     if (!params[:channels].empty? && @registry[params[:mask]] != nil)
128       params[:channels].each do |c|
129         @registry[params[:mask]] = @registry[params[:mask]].reject {|ele| ele =~ /^#{c}$/i}
130       end
131       if @registry[params[:mask]].empty?
132         @registry.delete(params[:mask])
133       end
134     else
135       @registry.delete(params[:mask])
136     end
137     m.okay
138   end
139
140   def list(m, params)
141     debug @registry.length
142     if(@registry.length > 0)
143       @registry.each { |mask,channels|
144         m.reply "#{mask} in #{channels.empty? ? 'all channels' : channels.join(', ')}"
145       }
146     else
147       m.reply "No entries"
148     end
149   end
150
151   def restore(m, params)
152     chan = params[:channel]
153     if chan == nil
154       if m.public?
155         chan = m.channel
156       else
157         m.reply _("Either specify a channel to restore, or ask in public")
158       end
159     end
160
161     current_non_ops = @bot.server.channel(chan).users.select { |u|
162       u.is_op?(chan) == nil and u.nick != @bot.nick
163     }
164
165     @registry.each { |mask,channels|
166       if channels.empty? || channels.include?(chan)
167         current_non_ops.each { |victim|
168           if victim.matches?(mask.to_irc_netmask(:server => m.server))
169             @bot.mode(chan, "+o", victim)
170           end
171         }
172       end
173     }
174   end
175 end
176
177 plugin = AutoOP.new
178
179 plugin.map 'autoop list', :action => 'list'
180 plugin.map 'autoop add :mask [*channels]', :action => 'add'
181 plugin.map 'autoop rm :mask [*channels]', :action => 'rm'
182 plugin.map 'autoop seed [:channel]', :action => 'seed'
183 plugin.map 'autoop restore [:channel]', :action => 'restore'
184
185 plugin.default_auth('*',false)