3 # base class for all rbot plugins
4 # certain methods will be called if they are provided, if you define one of
5 # the following methods, it will be called as appropriate:
7 # listen(UserMessage)::
8 # Called for all messages of any type. To
9 # differentiate them, use message.kind_of? It'll be
10 # either a PrivMessage, NoticeMessage, KickMessage,
11 # QuitMessage, PartMessage, JoinMessage, NickMessage,
14 # privmsg(PrivMessage)::
15 # called for a PRIVMSG if the first word matches one
16 # the plugin register()d for. Use m.plugin to get
17 # that word and m.params for the rest of the message,
21 # Called when a user (or the bot) is kicked from a
22 # channel the bot is in.
25 # Called when a user (or the bot) joins a channel
28 # Called when a user (or the bot) parts a channel
31 # Called when a user (or the bot) quits IRC
34 # Called when a user (or the bot) changes Nick
35 # topic(TopicMessage)::
36 # Called when a user (or the bot) changes a channel
39 # save:: Called when you are required to save your plugin's
40 # state, if you maintain data between sessions
42 # cleanup:: called before your plugin is "unloaded", prior to a
43 # plugin reload or bot quit - close any open
44 # files/connections or flush caches here
46 # initialise your plugin. Always call super if you override this method,
47 # as important variables are set up for you
51 @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
54 # return an identifier for this plugin, defaults to a list of the message
55 # prefixes handled (used for error messages etc)
60 # return a help string for your module. for complex modules, you may wish
61 # to break your help into topics, and return a list of available topics if
62 # +topic+ is nil. +plugin+ is passed containing the matching prefix for
63 # this message - if your plugin handles multiple prefixes, make sure your
64 # return the correct help for the prefix requested
65 def help(plugin, topic)
69 # register the plugin as a handler for messages prefixed +name+
70 # this can be called multiple times for a plugin to handle multiple
73 Plugins.plugins[name] = self
77 # is this plugin listening to all messages?
84 # class to manage multiple plugins and delegate messages to them for
87 # hash of registered message prefixes and associated plugins
89 # associated IrcBot class
92 # bot:: associated IrcBot class
93 # dirlist:: array of directories to scan (in order) for plugins
95 # create a new plugin handler, scanning for plugins in +dirlist+
96 def initialize(bot, dirlist)
102 # access to associated bot
107 # access to list of plugins
112 # load plugins from pre-assigned list of directories
115 dirs << File.dirname(__FILE__) + "/plugins"
118 if(FileTest.directory?(dir))
121 next if(file =~ /^\./)
122 next unless(file =~ /\.rb$/)
123 @tmpfilename = "#{dir}/#{file}"
125 # create a new, anonymous module to "house" the plugin
126 plugin_module = Module.new
129 plugin_string = IO.readlines(@tmpfilename).join("")
130 puts "loading module: #{@tmpfilename}"
131 plugin_module.module_eval(plugin_string)
132 rescue StandardError, NameError, LoadError, SyntaxError => err
133 puts "plugin #{@tmpfilename} load failed: " + err
134 puts err.backtrace.join("\n")
141 # call the save method for each active plugin
143 @@plugins.values.uniq.each {|p|
144 next unless(p.respond_to?("save"))
147 rescue StandardError, NameError, SyntaxError => err
148 puts "plugin #{p.name} save() failed: " + err
149 puts err.backtrace.join("\n")
154 # call the cleanup method for each active plugin
156 @@plugins.values.uniq.each {|p|
157 next unless(p.respond_to?("cleanup"))
160 rescue StandardError, NameError, SyntaxError => err
161 puts "plugin #{p.name} cleanup() failed: " + err
162 puts err.backtrace.join("\n")
167 # drop all plugins and rescan plugins on disk
168 # calls save and cleanup for each plugin before dropping them
176 # return list of help topics (plugin names)
178 if(@@plugins.length > 0)
179 # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
180 return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
182 return " [no plugins active]"
187 @@plugins.values.uniq.length
190 # return help for +topic+ (call associated plugin's help method)
192 if(topic =~ /^(\S+)\s*(.*)$/)
195 if(@@plugins.has_key?(key))
197 return @@plugins[key].help(key, params)
198 rescue StandardError, NameError, SyntaxError => err
199 puts "plugin #{@@plugins[key].name} help() failed: " + err
200 puts err.backtrace.join("\n")
208 # see if each plugin handles +method+, and if so, call it, passing
209 # +message+ as a parameter
210 def delegate(method, message)
211 @@plugins.values.uniq.each {|p|
212 if(p.respond_to? method)
214 p.send method, message
215 rescue StandardError, NameError, SyntaxError => err
216 puts "plugin #{p.name} #{method}() failed: " + err
217 puts err.backtrace.join("\n")
223 # see if we have a plugin that wants to handle this message, if so, pass
224 # it to the plugin and return true, otherwise false
226 return unless(m.plugin)
227 if (@@plugins.has_key?(m.plugin) &&
228 @@plugins[m.plugin].respond_to?("privmsg") &&
229 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
231 @@plugins[m.plugin].privmsg(m)
232 rescue StandardError, NameError, SyntaxError => err
233 puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
234 puts err.backtrace.join("\n")