2 BotConfig.register BotConfigArrayValue.new('plugins.blacklist',
3 :default => [], :wizard => false, :requires_restart => true,
4 :desc => "Plugins that should not be loaded")
6 require 'rbot/messagemapper'
8 # base class for all rbot plugins
9 # certain methods will be called if they are provided, if you define one of
10 # the following methods, it will be called as appropriate:
12 # map(template, options)::
13 # map!(template, options)::
14 # map is the new, cleaner way to respond to specific message formats
15 # without littering your plugin code with regexps. The difference
16 # between map and map! is that map! will not register the new command
17 # as an alternative name for the plugin.
21 # plugin.map 'karmastats', :action => 'karma_stats'
23 # # while in the plugin...
24 # def karma_stats(m, params)
28 # # the default action is the first component
31 # # attributes can be pulled out of the match string
32 # plugin.map 'karma for :key'
33 # plugin.map 'karma :key'
35 # # while in the plugin...
36 # def karma(m, params)
38 # m.reply 'karma for #{item}'
41 # # you can setup defaults, to make parameters optional
42 # plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
44 # # the default auth check is also against the first component
45 # # but that can be changed
46 # plugin.map 'karmastats', :auth => 'karma'
48 # # maps can be restricted to public or private message:
49 # plugin.map 'karmastats', :private false,
50 # plugin.map 'karmastats', :public false,
53 # listen(UserMessage)::
54 # Called for all messages of any type. To
55 # differentiate them, use message.kind_of? It'll be
56 # either a PrivMessage, NoticeMessage, KickMessage,
57 # QuitMessage, PartMessage, JoinMessage, NickMessage,
60 # privmsg(PrivMessage)::
61 # called for a PRIVMSG if the first word matches one
62 # the plugin register()d for. Use m.plugin to get
63 # that word and m.params for the rest of the message,
67 # Called when a user (or the bot) is kicked from a
68 # channel the bot is in.
71 # Called when a user (or the bot) joins a channel
74 # Called when a user (or the bot) parts a channel
77 # Called when a user (or the bot) quits IRC
80 # Called when a user (or the bot) changes Nick
81 # topic(TopicMessage)::
82 # Called when a user (or the bot) changes a channel
85 # connect():: Called when a server is joined successfully, but
86 # before autojoin channels are joined (no params)
88 # save:: Called when you are required to save your plugin's
89 # state, if you maintain data between sessions
91 # cleanup:: called before your plugin is "unloaded", prior to a
92 # plugin reload or bot quit - close any open
93 # files/connections or flush caches here
95 attr_reader :bot # the associated bot
96 # initialise your plugin. Always call super if you override this method,
97 # as important variables are set up for you
101 @handler = MessageMapper.new(self)
102 @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
106 # debug "Flushing #{@registry}"
111 # debug "Closing #{@registry}"
122 name = @handler.last.items[0]
124 unless self.respond_to?('privmsg')
134 name = @handler.last.items[0]
135 self.register name, {:hidden => true}
136 unless self.respond_to?('privmsg')
143 # return an identifier for this plugin, defaults to a list of the message
144 # prefixes handled (used for error messages etc)
149 # return a help string for your module. for complex modules, you may wish
150 # to break your help into topics, and return a list of available topics if
151 # +topic+ is nil. +plugin+ is passed containing the matching prefix for
152 # this message - if your plugin handles multiple prefixes, make sure you
153 # return the correct help for the prefix requested
154 def help(plugin, topic)
158 # register the plugin as a handler for messages prefixed +name+
159 # this can be called multiple times for a plugin to handle multiple
161 def register(name,opts={})
162 raise ArgumentError, "Second argument must be a hash!" unless opts.kind_of?(Hash)
163 return if Plugins.plugins.has_key?(name)
164 Plugins.plugins[name] = self
165 @names << name unless opts.fetch(:hidden, false)
168 # default usage method provided as a utility for simple plugins. The
169 # MessageMapper uses 'usage' as its default fallback method.
170 def usage(m, params = {})
171 m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
176 # class to manage multiple plugins and delegate messages to them for
179 # hash of registered message prefixes and associated plugins
181 # associated IrcBot class
184 # bot:: associated IrcBot class
185 # dirlist:: array of directories to scan (in order) for plugins
187 # create a new plugin handler, scanning for plugins in +dirlist+
188 def initialize(bot, dirlist)
194 # access to associated bot
199 # access to list of plugins
204 # load plugins from pre-assigned list of directories
206 @blacklist = Array.new
207 @@bot.config['plugins.blacklist'].each { |p|
208 @blacklist << p+".rb"
213 @blacklist.each { |p|
214 processed[p.intern] = :blacklisted
217 dirs << Config::datadir + "/plugins"
219 dirs.reverse.each {|dir|
220 if(FileTest.directory?(dir))
223 next if(file =~ /^\./)
224 if processed.has_key?(file.intern)
225 @ignored << {:name => file, :dir => dir, :reason => processed[file.intern]}
228 if(file =~ /^(.+\.rb)\.disabled$/)
229 # GB: Do we want to do this? This means that a disabled plugin in a directory
230 # will disable in all subsequent directories. This was probably meant
231 # to be used before plugins.blacklist was implemented, so I think
232 # we don't need this anymore
233 processed[$1.intern] = :disabled
234 @ignored << {:name => $1, :dir => dir, :reason => processed[$1.intern]}
237 next unless(file =~ /\.rb$/)
238 tmpfilename = "#{dir}/#{file}"
240 # create a new, anonymous module to "house" the plugin
241 # the idea here is to prevent namespace pollution. perhaps there
243 plugin_module = Module.new
246 plugin_string = IO.readlines(tmpfilename).join("")
247 debug "loading plugin #{tmpfilename}"
248 plugin_module.module_eval(plugin_string)
249 processed[file.intern] = :loaded
250 rescue Exception => err
251 # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
252 warning "plugin #{tmpfilename} load failed\n" + err.inspect
253 debug err.backtrace.join("\n")
254 bt = err.backtrace.select { |line|
255 line.match(/^(\(eval\)|#{tmpfilename}):\d+/)
258 el.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
259 "#{tmpfilename}#{$1}#{$3}"
262 msg = err.to_str.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
263 "#{tmpfilename}#{$1}#{$3}"
265 newerr = err.class.new(msg)
266 newerr.set_backtrace(bt)
267 # debug "Simplified error: " << newerr.inspect
268 # debug newerr.backtrace.join("\n")
269 @failed << { :name => file, :dir => dir, :reason => newerr }
270 # debug "Failures: #{@failed.inspect}"
277 # call the save method for each active plugin
279 delegate 'flush_registry'
283 # call the cleanup method for each active plugin
288 # drop all plugins and rescan plugins on disk
289 # calls save and cleanup for each plugin before dropping them
297 # return list of help topics (plugin names)
299 # Active plugins first
300 if(@@plugins.length > 0)
301 list = " [#{length} plugin#{'s' if length > 1}: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ")
303 list = " [no plugins active"
305 # Ignored plugins next
306 list << "; #{Underline}#{@ignored.length} plugin#{'s' if @ignored.length > 1} ignored#{Underline}: use #{Bold}help ignored plugins#{Bold} to see why" unless @ignored.empty?
307 # Failed plugins next
308 list << "; #{Reverse}#{@failed.length} plugin#{'s' if @failed.length > 1} failed to load#{Reverse}: use #{Bold}help failed plugins#{Bold} to see why" unless @failed.empty?
314 @@plugins.values.uniq.length
317 # return help for +topic+ (call associated plugin's help method)
320 when /fail(?:ed)?\s*plugins?.*(trace(?:back)?s?)?/
321 # debug "Failures: #{@failed.inspect}"
322 return "no plugins failed to load" if @failed.empty?
323 return (@failed.inject(Array.new) { |list, p|
324 list << "#{Bold}#{p[:name]}#{Bold} in #{p[:dir]} failed"
325 list << "with error #{p[:reason].class}: #{p[:reason]}"
326 list << "at #{p[:reason].backtrace.join(', ')}" if $1 and not p[:reason].backtrace.empty?
329 when /ignored?\s*plugins?/
330 return "no plugins were ignored" if @ignored.empty?
331 return (@ignored.inject(Array.new) { |list, p|
334 list << "#{p[:name]} in #{p[:dir]} (overruled by previous)"
336 list << "#{p[:name]} in #{p[:dir]} (#{p[:reason].to_s})"
340 when /^(\S+)\s*(.*)$/
343 if(@@plugins.has_key?(key))
345 return @@plugins[key].help(key, params)
346 rescue Exception => err
347 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
348 error "plugin #{@@plugins[key].name} help() failed: #{err.class}: #{err}"
349 error err.backtrace.join("\n")
357 # see if each plugin handles +method+, and if so, call it, passing
358 # +message+ as a parameter
359 def delegate(method, *args)
360 @@plugins.values.uniq.each {|p|
361 if(p.respond_to? method)
364 rescue Exception => err
365 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
366 error "plugin #{p.name} #{method}() failed: #{err.class}: #{err}"
367 error err.backtrace.join("\n")
373 # see if we have a plugin that wants to handle this message, if so, pass
374 # it to the plugin and return true, otherwise false
376 return unless(m.plugin)
377 if (@@plugins.has_key?(m.plugin) &&
378 @@plugins[m.plugin].respond_to?("privmsg") &&
379 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
381 @@plugins[m.plugin].privmsg(m)
382 rescue Exception => err
383 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
384 error "plugin #{@@plugins[m.plugin].name} privmsg() failed: #{err.class}: #{err}"
385 error err.backtrace.join("\n")