]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/plugins.rb
plugins: register maps with full information accessible via @bot.plugins.maps
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins.rb
index 96f61efc24a54d00dc068d8b205b6a59a05604b4..6f308609f55d1adb60eac2060504399d07af71ee 100644 (file)
@@ -6,7 +6,8 @@
 require 'singleton'
 
 module Irc
-    BotConfig.register BotConfigArrayValue.new('plugins.blacklist',
+class Bot
+    Config.register Config::ArrayValue.new('plugins.blacklist',
       :default => [], :wizard => false, :requires_rescan => true,
       :desc => "Plugins that should not be loaded")
 module Plugins
@@ -17,7 +18,7 @@ module Plugins
   functionality. Rather than subclassing BotModule, however, one should
   subclass either CoreBotModule (reserved for system modules) or Plugin
   (for user plugins).
-  
+
   A BotModule interacts with Irc events by defining one or more of the following
   methods, which get called as appropriate when the corresponding Irc event
   happens.
@@ -27,7 +28,7 @@ module Plugins
      map is the new, cleaner way to respond to specific message formats without
      littering your plugin code with regexps, and should be used instead of
      #register() and #privmsg() (see below) when possible.
-     
+
      The difference between map and map! is that map! will not register the new
      command as an alternative name for the plugin.
 
@@ -134,7 +135,7 @@ module Plugins
     #   the rbot instance
     # @registry::
     #   the botmodule's registry, which can be used to store permanent data
-    #   (see BotRegistryAccessor for additional documentation)
+    #   (see Registry::Accessor for additional documentation)
     #
     # Other instance variables which are defined and should not be overwritten
     # byt the user, but aren't usually accessed directly, are:
@@ -153,7 +154,7 @@ module Plugins
       @botmodule_triggers = Array.new
 
       @handler = MessageMapper.new(self)
-      @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
+      @registry = Registry::Accessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
 
       @manager.add_botmodule(self)
       if self.respond_to?('set_language')
@@ -203,15 +204,7 @@ module Plugins
     # responds to appropriately-formed messages on Irc.
     #
     def map(*args)
-      @handler.map(self, *args)
-      # register this map
-      name = @handler.last.items[0]
-      self.register name, :auth => nil
-      unless self.respond_to?('privmsg')
-        def self.privmsg(m) #:nodoc:
-          handle(m)
-        end
-      end
+      do_map(false, *args)
     end
 
     # call-seq: map!(template, options)
@@ -220,10 +213,17 @@ module Plugins
     # as an alternative name for the plugin.
     #
     def map!(*args)
+      do_map(true, *args)
+    end
+
+    # Auxiliary method called by #map and #map!
+    def do_map(silent, *args)
       @handler.map(self, *args)
       # register this map
-      name = @handler.last.items[0]
-      self.register name, :auth => nil, :hidden => true
+      map = @handler.last
+      name = map.items[0]
+      self.register name, :auth => nil, :hidden => silent
+      @manager.register_map(self, map)
       unless self.respond_to?('privmsg')
         def self.privmsg(m) #:nodoc:
           handle(m)
@@ -337,6 +337,7 @@ module Plugins
     include Singleton
     attr_reader :bot
     attr_reader :botmodules
+    attr_reader :maps
 
     # This is the list of patterns commonly delegated to plugins.
     # A fast delegation lookup is enabled for them.
@@ -356,6 +357,7 @@ module Plugins
 
       @names_hash = Hash.new
       @commandmappers = Hash.new
+      @maps = Hash.new
       @delegate_list = Hash.new { |h, k|
         h[k] = Array.new
       }
@@ -368,12 +370,26 @@ module Plugins
       bot_associate(nil)
     end
 
+    def inspect
+      ret = self.to_s[0..-2]
+      ret << ' corebotmodules='
+      ret << @botmodules[:CoreBotModule].map { |m|
+        m.name
+      }.inspect
+      ret << ' plugins='
+      ret << @botmodules[:Plugin].map { |m|
+        m.name
+      }.inspect
+      ret << ">"
+    end
+
     # Reset lists of botmodules
     def reset_botmodule_lists
       @botmodules[:CoreBotModule].clear
       @botmodules[:Plugin].clear
       @names_hash.clear
       @commandmappers.clear
+      @maps.clear
       @failures_shown = false
     end
 
@@ -400,6 +416,19 @@ module Plugins
       @commandmappers[cmd.to_sym] = {:botmodule => botmodule, :auth => auth_path}
     end
 
+    # Registers botmodule _botmodule_ with map _map_. This adds the map to the #maps hash
+    # which has three keys:
+    #
+    # botmodule:: the associated botmodule
+    # auth:: an array of auth keys checked by the map; the first is the full_auth_path of the map
+    # map:: the actual MessageTemplate object
+    #
+    #
+    def register_map(botmodule, map)
+      raise TypeError, "First argument #{botmodule.inspect} is not of class BotModule" unless botmodule.kind_of?(BotModule)
+      @maps[map.template] = { :botmodule => botmodule, :auth => [map.options[:full_auth_path]], :map => map }
+    end
+
     def add_botmodule(botmodule)
       raise TypeError, "Argument #{botmodule.inspect} is not of class BotModule" unless botmodule.kind_of?(BotModule)
       kl = botmodule.botmodule_class
@@ -683,9 +712,9 @@ module Plugins
         key = $1
         params = $2
 
-       # Let's see if we can match a plugin by the given name
+        # Let's see if we can match a plugin by the given name
         (core_modules + plugins).each { |p|
-         next unless p.name == key
+          next unless p.name == key
           begin
             return p.help(key, params)
           rescue Exception => err
@@ -694,7 +723,7 @@ module Plugins
           end
         }
 
-       # Nope, let's see if it's a command, and ask for help at the corresponding botmodule
+        # Nope, let's see if it's a command, and ask for help at the corresponding botmodule
         k = key.to_sym
         if commands.has_key?(k)
           p = commands[k][:botmodule]
@@ -792,3 +821,4 @@ module Plugins
 
 end
 end
+end