2 require 'rbot/messagemapper'
4 # base class for all rbot plugins
5 # certain methods will be called if they are provided, if you define one of
6 # the following methods, it will be called as appropriate:
8 # map(template, options)::
9 # map is the new, cleaner way to respond to specific message formats
10 # without littering your plugin code with regexps
12 # plugin.map 'karmastats', :action => 'karma_stats'
14 # # while in the plugin...
15 # def karma_stats(m, params)
19 # # the default action is the first component
22 # # attributes can be pulled out of the match string
23 # plugin.map 'karma for :key'
24 # plugin.map 'karma :key'
26 # # while in the plugin...
27 # def karma(m, params)
29 # m.reply 'karma for #{item}'
32 # # you can setup defaults, to make parameters optional
33 # plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
35 # # the default auth check is also against the first component
36 # # but that can be changed
37 # plugin.map 'karmastats', :auth => 'karma'
39 # # maps can be restricted to public or private message:
40 # plugin.map 'karmastats', :private false,
41 # plugin.map 'karmastats', :public false,
44 # To activate your maps, you simply register them
45 # plugin.register_maps
46 # This also sets the privmsg handler to use the map lookups for
47 # handling messages. You can still use listen(), kick() etc methods
49 # listen(UserMessage)::
50 # Called for all messages of any type. To
51 # differentiate them, use message.kind_of? It'll be
52 # either a PrivMessage, NoticeMessage, KickMessage,
53 # QuitMessage, PartMessage, JoinMessage, NickMessage,
56 # privmsg(PrivMessage)::
57 # called for a PRIVMSG if the first word matches one
58 # the plugin register()d for. Use m.plugin to get
59 # that word and m.params for the rest of the message,
63 # Called when a user (or the bot) is kicked from a
64 # channel the bot is in.
67 # Called when a user (or the bot) joins a channel
70 # Called when a user (or the bot) parts a channel
73 # Called when a user (or the bot) quits IRC
76 # Called when a user (or the bot) changes Nick
77 # topic(TopicMessage)::
78 # Called when a user (or the bot) changes a channel
81 # save:: Called when you are required to save your plugin's
82 # state, if you maintain data between sessions
84 # cleanup:: called before your plugin is "unloaded", prior to a
85 # plugin reload or bot quit - close any open
86 # files/connections or flush caches here
88 attr_reader :bot # the associated bot
89 # initialise your plugin. Always call super if you override this method,
90 # as important variables are set up for you
94 @handler = MessageMapper.new(self)
95 @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
101 name = @handler.last.items[0]
103 unless self.respond_to?('privmsg')
110 # return an identifier for this plugin, defaults to a list of the message
111 # prefixes handled (used for error messages etc)
116 # return a help string for your module. for complex modules, you may wish
117 # to break your help into topics, and return a list of available topics if
118 # +topic+ is nil. +plugin+ is passed containing the matching prefix for
119 # this message - if your plugin handles multiple prefixes, make sure your
120 # return the correct help for the prefix requested
121 def help(plugin, topic)
125 # register the plugin as a handler for messages prefixed +name+
126 # this can be called multiple times for a plugin to handle multiple
129 return if Plugins.plugins.has_key?(name)
130 Plugins.plugins[name] = self
134 # default usage method provided as a utility for simple plugins. The
135 # MessageMapper uses 'usage' as its default fallback method.
136 def usage(m, params = {})
137 m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
142 # class to manage multiple plugins and delegate messages to them for
145 # hash of registered message prefixes and associated plugins
147 # associated IrcBot class
150 # bot:: associated IrcBot class
151 # dirlist:: array of directories to scan (in order) for plugins
153 # create a new plugin handler, scanning for plugins in +dirlist+
154 def initialize(bot, dirlist)
160 # access to associated bot
165 # access to list of plugins
170 # load plugins from pre-assigned list of directories
173 dirs << Config::DATADIR + "/plugins"
176 if(FileTest.directory?(dir))
179 next if(file =~ /^\./)
180 next unless(file =~ /\.rb$/)
181 @tmpfilename = "#{dir}/#{file}"
183 # create a new, anonymous module to "house" the plugin
184 # the idea here is to prevent namespace pollution. perhaps there
186 plugin_module = Module.new
189 plugin_string = IO.readlines(@tmpfilename).join("")
190 debug "loading module: #{@tmpfilename}"
191 plugin_module.module_eval(plugin_string)
192 rescue StandardError, NameError, LoadError, SyntaxError => err
193 puts "warning: plugin #{@tmpfilename} load failed: " + err
194 puts err.backtrace.join("\n")
201 # call the save method for each active plugin
206 # call the cleanup method for each active plugin
211 # drop all plugins and rescan plugins on disk
212 # calls save and cleanup for each plugin before dropping them
220 # return list of help topics (plugin names)
222 if(@@plugins.length > 0)
223 # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
224 return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
226 return " [no plugins active]"
231 @@plugins.values.uniq.length
234 # return help for +topic+ (call associated plugin's help method)
236 if(topic =~ /^(\S+)\s*(.*)$/)
239 if(@@plugins.has_key?(key))
241 return @@plugins[key].help(key, params)
242 rescue StandardError, NameError, SyntaxError => err
243 puts "plugin #{@@plugins[key].name} help() failed: " + err
244 puts err.backtrace.join("\n")
252 # see if each plugin handles +method+, and if so, call it, passing
253 # +message+ as a parameter
254 def delegate(method, *args)
255 @@plugins.values.uniq.each {|p|
256 if(p.respond_to? method)
259 rescue StandardError, NameError, SyntaxError => err
260 puts "plugin #{p.name} #{method}() failed: " + err
261 puts err.backtrace.join("\n")
267 # see if we have a plugin that wants to handle this message, if so, pass
268 # it to the plugin and return true, otherwise false
270 return unless(m.plugin)
271 if (@@plugins.has_key?(m.plugin) &&
272 @@plugins[m.plugin].respond_to?("privmsg") &&
273 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
275 @@plugins[m.plugin].privmsg(m)
276 rescue StandardError, NameError, SyntaxError => err
277 puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
278 puts err.backtrace.join("\n")