]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/plugins.rb
core: sets plugin_path to loaded plugins
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins.rb
index 3ec52fd99b6463e564ff27e60507d57283aecdb1..76c76ae199467d7537e90cd213b3807b89bbb45f 100644 (file)
@@ -4,6 +4,7 @@
 # :title: rbot plugin management
 
 require 'singleton'
+require_relative './core/utils/where_is.rb'
 
 module Irc
 class Bot
@@ -37,36 +38,36 @@ module Plugins
 
      Examples:
 
-       plugin.map 'karmastats', :action => 'karma_stats'
+       plugin.map 'pointstats', :action => 'point_stats'
 
        # while in the plugin...
-       def karma_stats(m, params)
+       def point_stats(m, params)
          m.reply "..."
        end
 
        # the default action is the first component
-       plugin.map 'karma'
+       plugin.map 'points'
 
        # attributes can be pulled out of the match string
-       plugin.map 'karma for :key'
-       plugin.map 'karma :key'
+       plugin.map 'points for :key'
+       plugin.map 'points :key'
 
        # while in the plugin...
-       def karma(m, params)
+       def points(m, params)
          item = params[:key]
-         m.reply 'karma for #{item}'
+         m.reply 'points for #{item}'
        end
 
        # you can setup defaults, to make parameters optional
-       plugin.map 'karma :key', :defaults => {:key => 'defaultvalue'}
+       plugin.map 'points :key', :defaults => {:key => 'defaultvalue'}
 
        # the default auth check is also against the first component
        # but that can be changed
-       plugin.map 'karmastats', :auth => 'karma'
+       plugin.map 'pointstats', :auth => 'points'
 
        # maps can be restricted to public or private message:
-       plugin.map 'karmastats', :private => false
-       plugin.map 'karmastats', :public => false
+       plugin.map 'pointstats', :private => false
+       plugin.map 'pointstats', :public => false
 
      See MessageMapper#map for more information on the template format and the
      allowed options.
@@ -160,6 +161,9 @@ module Plugins
     # the message map handler
     attr_reader :handler
 
+    # the directory in which the plugin is located
+    attr_reader :plugin_path
+
     # Initialise your bot module. Always call super if you override this method,
     # as important variables are set up for you:
     #
@@ -190,6 +194,7 @@ module Plugins
       @registry = @bot.registry_factory.create(@bot.path, self.class.to_s.gsub(/^.*::/, ''))
 
       @manager.add_botmodule(self)
+      @plugin_path = @manager.next_plugin_path
       if self.respond_to?('set_language')
         self.set_language(@bot.lang.language)
       end
@@ -214,6 +219,7 @@ module Plugins
       @registry.flush
     end
 
+
     # Method called to cleanup before the plugin is unloaded. If you overload
     # this method to handle additional cleanup tasks, remember to call super()
     # so that the default cleanup actions are taken care of as well.
@@ -409,6 +415,10 @@ module Plugins
     attr_reader :botmodules
     attr_reader :maps
 
+    attr_reader :core_module_dirs
+    attr_reader :plugin_dirs
+    attr_reader :next_plugin_path
+
     # This is the list of patterns commonly delegated to plugins.
     # A fast delegation lookup is enabled for them.
     DEFAULT_DELEGATE_PATTERNS = %r{^(?:
@@ -578,6 +588,10 @@ module Plugins
       ([str, err.inspect] + err.backtrace).join("\n")
     end
 
+    def get_plugin(name)
+      plugins.find { |plugin| plugin.name == name }
+    end
+
     # This method is the one that actually loads a module from the
     # file _fname_
     #
@@ -601,18 +615,13 @@ module Plugins
       begin
         plugin_string = IO.read(fname)
         debug "loading #{desc}#{fname}"
+
+        # set path of the plugin that will be loaded next (see BotModule#initialize)
+        @next_plugin_path = File.dirname fname
+
         plugin_module.module_eval(plugin_string, fname)
 
-        # this sets a BOTMODULE_FNAME constant in all BotModule
-        # classes defined in the module. This allows us to know
-        # the filename the plugin was declared in from outside
-        # the plugin itself (from within, a __FILE__ would work.)
-        plugin_module.constants.each do |const|
-          cls = plugin_module.const_get(const)
-          if cls.is_a? Class and cls < BotModule
-            cls.const_set("BOTMODULE_FNAME", fname)
-          end
-        end
+        @next_plugin_path = nil
 
         return :loaded
       rescue Exception => err
@@ -660,7 +669,6 @@ module Plugins
         return newerr
       end
     end
-    private :load_botmodule_file
 
     # add one or more directories to the list of directories to
     # load core modules from
@@ -767,7 +775,7 @@ module Plugins
     def save(botmodule=nil)
       if botmodule
         botmodule.flush_registry
-        botmodule.save          
+        botmodule.save if botmodule.respond_to? 'save'
       else
         delegate 'flush_registry'
         delegate 'save'
@@ -800,7 +808,7 @@ module Plugins
       if botmodule
         @failed.clear
         @ignored.clear
-        filename = botmodule.class::BOTMODULE_FNAME
+        filename = where_is(botmodule.class)
         err = load_botmodule_file(filename, "plugin")
         if err.is_a? Exception
           @failed << { :name => botmodule.to_s,
@@ -1008,7 +1016,7 @@ module Plugins
       sort_modules unless @sorted_modules
 
       opts = {}
-      opts.merge(args.pop) if args.last.class == Hash
+      opts.merge!(args.pop) if args.last.class == Hash
 
       m = args.first
       if BasicUserMessage === m