3 require 'rbot/messagemapper'
5 # base class for all rbot plugins
6 # certain methods will be called if they are provided, if you define one of
7 # the following methods, it will be called as appropriate:
9 # map(template, options)::
10 # map is the new, cleaner way to respond to specific message formats
11 # without littering your plugin code with regexps. examples:
13 # plugin.map 'karmastats', :action => 'karma_stats'
15 # # while in the plugin...
16 # def karma_stats(m, params)
20 # # the default action is the first component
23 # # attributes can be pulled out of the match string
24 # plugin.map 'karma for :key'
25 # plugin.map 'karma :key'
27 # # while in the plugin...
28 # def karma(m, params)
30 # m.reply 'karma for #{item}'
33 # # you can setup defaults, to make parameters optional
34 # plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
36 # # the default auth check is also against the first component
37 # # but that can be changed
38 # plugin.map 'karmastats', :auth => 'karma'
40 # # maps can be restricted to public or private message:
41 # plugin.map 'karmastats', :private false,
42 # plugin.map 'karmastats', :public false,
45 # To activate your maps, you simply register them
46 # plugin.register_maps
47 # This also sets the privmsg handler to use the map lookups for
48 # handling messages. You can still use listen(), kick() etc methods
50 # listen(UserMessage)::
51 # Called for all messages of any type. To
52 # differentiate them, use message.kind_of? It'll be
53 # either a PrivMessage, NoticeMessage, KickMessage,
54 # QuitMessage, PartMessage, JoinMessage, NickMessage,
57 # privmsg(PrivMessage)::
58 # called for a PRIVMSG if the first word matches one
59 # the plugin register()d for. Use m.plugin to get
60 # that word and m.params for the rest of the message,
64 # Called when a user (or the bot) is kicked from a
65 # channel the bot is in.
68 # Called when a user (or the bot) joins a channel
71 # Called when a user (or the bot) parts a channel
74 # Called when a user (or the bot) quits IRC
77 # Called when a user (or the bot) changes Nick
78 # topic(TopicMessage)::
79 # Called when a user (or the bot) changes a channel
82 # connect():: Called when a server is joined successfully, but
83 # before autojoin channels are joined (no params)
85 # save:: Called when you are required to save your plugin's
86 # state, if you maintain data between sessions
88 # cleanup:: called before your plugin is "unloaded", prior to a
89 # plugin reload or bot quit - close any open
90 # files/connections or flush caches here
92 attr_reader :bot # the associated bot
93 # initialise your plugin. Always call super if you override this method,
94 # as important variables are set up for you
98 @handler = MessageMapper.new(self)
99 @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
105 name = @handler.last.items[0]
107 unless self.respond_to?('privmsg')
114 # return an identifier for this plugin, defaults to a list of the message
115 # prefixes handled (used for error messages etc)
120 # return a help string for your module. for complex modules, you may wish
121 # to break your help into topics, and return a list of available topics if
122 # +topic+ is nil. +plugin+ is passed containing the matching prefix for
123 # this message - if your plugin handles multiple prefixes, make sure your
124 # return the correct help for the prefix requested
125 def help(plugin, topic)
129 # register the plugin as a handler for messages prefixed +name+
130 # this can be called multiple times for a plugin to handle multiple
133 return if Plugins.plugins.has_key?(name)
134 Plugins.plugins[name] = self
138 # default usage method provided as a utility for simple plugins. The
139 # MessageMapper uses 'usage' as its default fallback method.
140 def usage(m, params = {})
141 m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
146 # class to manage multiple plugins and delegate messages to them for
149 # hash of registered message prefixes and associated plugins
151 # associated IrcBot class
154 # bot:: associated IrcBot class
155 # dirlist:: array of directories to scan (in order) for plugins
157 # create a new plugin handler, scanning for plugins in +dirlist+
158 def initialize(bot, dirlist)
164 # access to associated bot
169 # access to list of plugins
174 # load plugins from pre-assigned list of directories
176 processed = Array.new
178 dirs << Config::datadir + "/plugins"
180 dirs.reverse.each {|dir|
181 if(FileTest.directory?(dir))
184 next if(file =~ /^\./)
185 next if(processed.include?(file))
186 if(file =~ /^(.+\.rb)\.disabled$/)
190 next unless(file =~ /\.rb$/)
191 tmpfilename = "#{dir}/#{file}"
193 # create a new, anonymous module to "house" the plugin
194 # the idea here is to prevent namespace pollution. perhaps there
196 plugin_module = Module.new
199 plugin_string = IO.readlines(tmpfilename).join("")
200 debug "loading plugin #{tmpfilename}"
201 plugin_module.module_eval(plugin_string)
203 rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
204 puts "warning: plugin #{tmpfilename} load failed: " + err
205 puts err.backtrace.join("\n")
212 # call the save method for each active plugin
217 # call the cleanup method for each active plugin
222 # drop all plugins and rescan plugins on disk
223 # calls save and cleanup for each plugin before dropping them
231 # return list of help topics (plugin names)
233 if(@@plugins.length > 0)
234 # return " [plugins: " + @@plugins.keys.sort.join(", ") + "]"
235 return " [#{length} plugins: " + @@plugins.values.uniq.collect{|p| p.name}.sort.join(", ") + "]"
237 return " [no plugins active]"
242 @@plugins.values.uniq.length
245 # return help for +topic+ (call associated plugin's help method)
247 if(topic =~ /^(\S+)\s*(.*)$/)
250 if(@@plugins.has_key?(key))
252 return @@plugins[key].help(key, params)
253 rescue TimeoutError, StandardError, NameError, SyntaxError => err
254 puts "plugin #{@@plugins[key].name} help() failed: " + err
255 puts err.backtrace.join("\n")
263 # see if each plugin handles +method+, and if so, call it, passing
264 # +message+ as a parameter
265 def delegate(method, *args)
266 @@plugins.values.uniq.each {|p|
267 if(p.respond_to? method)
270 rescue TimeoutError, StandardError, NameError, SyntaxError => err
271 puts "plugin #{p.name} #{method}() failed: " + err
272 puts err.backtrace.join("\n")
278 # see if we have a plugin that wants to handle this message, if so, pass
279 # it to the plugin and return true, otherwise false
281 return unless(m.plugin)
282 if (@@plugins.has_key?(m.plugin) &&
283 @@plugins[m.plugin].respond_to?("privmsg") &&
284 @@bot.auth.allow?(m.plugin, m.source, m.replyto))
286 @@plugins[m.plugin].privmsg(m)
287 rescue TimeoutError, StandardError, NameError, SyntaxError => err
288 puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
289 puts err.backtrace.join("\n")