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.
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 plugin_module = Module.new
187 plugin_string = IO.readlines(@tmpfilename).join("")
188 puts "loading module: #{@tmpfilename}"
189 plugin_module.module_eval(plugin_string)
190 rescue StandardError, NameError, LoadError, SyntaxError => err
191 puts "plugin #{@tmpfilename} load failed: " + err
192 puts err.backtrace.join("\n")
199 # call the save method for each active plugin
201 @@plugins.values.uniq.each {|p|
202 next unless(p.respond_to?("save"))
205 rescue StandardError, NameError, SyntaxError => err
206 puts "plugin #{p.name} save() failed: " + err
207 puts err.backtrace.join("\n")
212 # call the cleanup method for each active plugin
214 @@plugins.values.uniq.each {|p|
215 next unless(p.respond_to?("cleanup"))
218 rescue StandardError, NameError, SyntaxError => err
219 puts "plugin #{p.name} cleanup() failed: " + err
220 puts err.backtrace.join("\n")
225 # drop all plugins and rescan plugins on disk
226 # calls save and cleanup for each plugin before dropping them
234 # return list of help topics (plugin names)
236 if(@@plugins.length > 0)
237 # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
238 return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
240 return " [no plugins active]"
245 @@plugins.values.uniq.length
248 # return help for +topic+ (call associated plugin's help method)
250 if(topic =~ /^(\S+)\s*(.*)$/)
253 if(@@plugins.has_key?(key))
255 return @@plugins[key].help(key, params)
256 rescue StandardError, NameError, SyntaxError => err
257 puts "plugin #{@@plugins[key].name} help() failed: " + err
258 puts err.backtrace.join("\n")
266 # see if each plugin handles +method+, and if so, call it, passing
267 # +message+ as a parameter
268 def delegate(method, message)
269 @@plugins.values.uniq.each {|p|
270 if(p.respond_to? method)
272 p.send method, message
273 rescue StandardError, NameError, SyntaxError => err
274 puts "plugin #{p.name} #{method}() failed: " + err
275 puts err.backtrace.join("\n")
281 # see if we have a plugin that wants to handle this message, if so, pass
282 # it to the plugin and return true, otherwise false
284 return unless(m.plugin)
285 if (@@plugins.has_key?(m.plugin) &&
286 @@plugins[m.plugin].respond_to?("privmsg") &&
287 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
289 @@plugins[m.plugin].privmsg(m)
290 rescue StandardError, NameError, SyntaxError => err
291 puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
292 puts err.backtrace.join("\n")