diff options
author | Tom Gilbert <tom@linuxbrit.co.uk> | 2005-07-26 21:50:00 +0000 |
---|---|---|
committer | Tom Gilbert <tom@linuxbrit.co.uk> | 2005-07-26 21:50:00 +0000 |
commit | 5d5d9df1a4825fad5ef045cfc0b21b16e5e2bcc7 (patch) | |
tree | f3814abafc8f1d6c589a29f4ddc89f55e510d493 /rbot/plugins.rb | |
parent | 3ba6917c904f5e664ae78b146f4e394ad805eb96 (diff) |
* Prevent multiple plugin registrations of the same name
* reworking the config system to use yaml for persistence
* reworking the config system key names
* on first startup, the bot will prompt for the essential startup config
* new config module for configuring the bot at runtime
* new config module includes new configurables, for example changing the
bot's language at runtime.
* various other fixes
* New way of mapping plugins to strings, using maps. These may be
familiar to rails users. This is to reduce the amount of regexps plugins
currently need to do to parse arguments. The old method (privmsg) is still
supported, of course. Example plugin now:
def MyPlugin < Plugin
def foo(m, params)
m.reply "bar"
end
def complexfoo(m, params)
m.reply "qux! (#{params[:bar]} #{params[:baz]})"
end
end
plugin = MyPlugin.new
# simple map
plugin.map 'foo'
# this will match "rbot: foo somestring otherstring" and pass the
# parameters as a hash using the names in the map.
plugin.map 'foo :bar :baz', :action => 'complexfoo'
# this means :foo is an optional parameter
plugin.map 'foo :foo', :defaults => {:foo => 'bar'}
# you can also gobble up into an array
plugin.map 'foo *bar' # params[:bar] will be an array of string elements
# and you can validate, here the first param must be a number
plugin.map 'foo :bar', :requirements => {:foo => /^\d+$/}
Diffstat (limited to 'rbot/plugins.rb')
-rw-r--r-- | rbot/plugins.rb | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/rbot/plugins.rb b/rbot/plugins.rb index b99fe562..5db047fb 100644 --- a/rbot/plugins.rb +++ b/rbot/plugins.rb @@ -1,8 +1,50 @@ module Irc + require 'rbot/messagemapper' # base class for all rbot plugins # certain methods will be called if they are provided, if you define one of # the following methods, it will be called as appropriate: + # + # map(template, options):: + # map is the new, cleaner way to respond to specific message formats + # without littering your plugin code with regexps + # examples: + # plugin.map 'karmastats', :action => 'karma_stats' + # + # # while in the plugin... + # def karma_stats(m, params) + # m.reply "..." + # end + # + # # the default action is the first component + # plugin.map 'karma' + # + # # attributes can be pulled out of the match string + # plugin.map 'karma for :key' + # plugin.map 'karma :key' + # + # # while in the plugin... + # def karma(m, params) + # item = params[:key] + # m.reply 'karma for #{item}' + # end + # + # # you can setup defaults, to make parameters optional + # plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'} + # + # # the default auth check is also against the first component + # # but that can be changed + # plugin.map 'karmastats', :auth => 'karma' + # + # # maps can be restricted to public or private message: + # plugin.map 'karmastats', :private false, + # plugin.map 'karmastats', :public false, + # end + # + # To activate your maps, you simply register them + # plugin.register_maps + # This also sets the privmsg handler to use the map lookups for + # handling messages. You can still use listen(), kick() etc methods # # listen(UserMessage):: # Called for all messages of any type. To @@ -43,14 +85,28 @@ module Irc # plugin reload or bot quit - close any open # files/connections or flush caches here class Plugin + attr_reader :bot # the associated bot # initialise your plugin. Always call super if you override this method, # as important variables are set up for you def initialize @bot = Plugins.bot @names = Array.new + @handler = MessageMapper.new(self) @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, "")) end + def map(*args) + @handler.map(*args) + # register this map + name = @handler.last.items[0] + self.register name + unless self.respond_to?('privmsg') + def self.privmsg(m) + @handler.handle(m) + end + end + end + # return an identifier for this plugin, defaults to a list of the message # prefixes handled (used for error messages etc) def name @@ -70,15 +126,17 @@ module Irc # this can be called multiple times for a plugin to handle multiple # message prefixes def register(name) + return if Plugins.plugins.has_key?(name) Plugins.plugins[name] = self @names << name end - # is this plugin listening to all messages? - def listen? - @listen + # default usage method provided as a utility for simple plugins. The + # MessageMapper uses 'usage' as its default fallback method. + def usage(m, params) + m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'" end - + end # class to manage multiple plugins and delegate messages to them for |