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 is the new, cleaner way to respond to specific message formats
14 # without littering your plugin code with regexps. examples:
16 # plugin.map 'karmastats', :action => 'karma_stats'
18 # # while in the plugin...
19 # def karma_stats(m, params)
23 # # the default action is the first component
26 # # attributes can be pulled out of the match string
27 # plugin.map 'karma for :key'
28 # plugin.map 'karma :key'
30 # # while in the plugin...
31 # def karma(m, params)
33 # m.reply 'karma for #{item}'
36 # # you can setup defaults, to make parameters optional
37 # plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
39 # # the default auth check is also against the first component
40 # # but that can be changed
41 # plugin.map 'karmastats', :auth => 'karma'
43 # # maps can be restricted to public or private message:
44 # plugin.map 'karmastats', :private false,
45 # plugin.map 'karmastats', :public false,
48 # To activate your maps, you simply register them
49 # plugin.register_maps
50 # This also sets the privmsg handler to use the map lookups for
51 # handling messages. You can still use listen(), kick() etc methods
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}"
118 name = @handler.last.items[0]
120 unless self.respond_to?('privmsg')
127 # return an identifier for this plugin, defaults to a list of the message
128 # prefixes handled (used for error messages etc)
133 # return a help string for your module. for complex modules, you may wish
134 # to break your help into topics, and return a list of available topics if
135 # +topic+ is nil. +plugin+ is passed containing the matching prefix for
136 # this message - if your plugin handles multiple prefixes, make sure your
137 # return the correct help for the prefix requested
138 def help(plugin, topic)
142 # register the plugin as a handler for messages prefixed +name+
143 # this can be called multiple times for a plugin to handle multiple
146 return if Plugins.plugins.has_key?(name)
147 Plugins.plugins[name] = self
151 # default usage method provided as a utility for simple plugins. The
152 # MessageMapper uses 'usage' as its default fallback method.
153 def usage(m, params = {})
154 m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
159 # class to manage multiple plugins and delegate messages to them for
162 # hash of registered message prefixes and associated plugins
164 # associated IrcBot class
167 # bot:: associated IrcBot class
168 # dirlist:: array of directories to scan (in order) for plugins
170 # create a new plugin handler, scanning for plugins in +dirlist+
171 def initialize(bot, dirlist)
174 @blacklist = Array.new
175 @@bot.config['plugins.blacklist'].each { |p|
176 @blacklist << p+".rb"
181 # access to associated bot
186 # access to list of plugins
191 # load plugins from pre-assigned list of directories
193 processed = @blacklist
195 dirs << Config::datadir + "/plugins"
197 dirs.reverse.each {|dir|
198 if(FileTest.directory?(dir))
201 next if(file =~ /^\./)
202 next if(processed.include?(file))
203 if(file =~ /^(.+\.rb)\.disabled$/)
207 next unless(file =~ /\.rb$/)
208 tmpfilename = "#{dir}/#{file}"
210 # create a new, anonymous module to "house" the plugin
211 # the idea here is to prevent namespace pollution. perhaps there
213 plugin_module = Module.new
216 plugin_string = IO.readlines(tmpfilename).join("")
217 debug "loading plugin #{tmpfilename}"
218 plugin_module.module_eval(plugin_string)
220 rescue Exception => err
221 # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
222 warning "plugin #{tmpfilename} load failed: " + err.inspect
223 warning err.backtrace.join("\n")
230 # call the save method for each active plugin
232 delegate 'flush_registry'
236 # call the cleanup method for each active plugin
241 # drop all plugins and rescan plugins on disk
242 # calls save and cleanup for each plugin before dropping them
250 # return list of help topics (plugin names)
252 if(@@plugins.length > 0)
253 # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
254 return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
256 return " [no plugins active]"
261 @@plugins.values.uniq.length
264 # return help for +topic+ (call associated plugin's help method)
266 if(topic =~ /^(\S+)\s*(.*)$/)
269 if(@@plugins.has_key?(key))
271 return @@plugins[key].help(key, params)
272 rescue Exception => err
273 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
274 error "plugin #{@@plugins[key].name} help() failed: #{err.class}: #{err}"
275 error err.backtrace.join("\n")
283 # see if each plugin handles +method+, and if so, call it, passing
284 # +message+ as a parameter
285 def delegate(method, *args)
286 @@plugins.values.uniq.each {|p|
287 if(p.respond_to? method)
290 rescue Exception => err
291 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
292 error "plugin #{p.name} #{method}() failed: #{err.class}: #{err}"
293 error err.backtrace.join("\n")
299 # see if we have a plugin that wants to handle this message, if so, pass
300 # it to the plugin and return true, otherwise false
302 return unless(m.plugin)
303 if (@@plugins.has_key?(m.plugin) &&
304 @@plugins[m.plugin].respond_to?("privmsg") &&
305 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
307 @@plugins[m.plugin].privmsg(m)
308 rescue Exception => err
309 #rescue TimeoutError, StandardError, NameError, SyntaxError => err
310 error "plugin #{@@plugins[m.plugin].name} privmsg() failed: #{err.class}: #{err}"
311 error err.backtrace.join("\n")