]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/plugins.rb
Sun Jul 31 02:20:08 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins.rb
index d239c5e6bfcd3626ac911e7f4ce6b3c164e7d4dc..ba01aa7d5c83a2c6e37f6c59e4a2b85c1f6c80e5 100644 (file)
@@ -1,4 +1,5 @@
 module Irc
+module Plugins
   require 'rbot/messagemapper'
 
   # base class for all rbot plugins
@@ -133,7 +134,7 @@ module Irc
 
     # default usage method provided as a utility for simple plugins. The
     # MessageMapper uses 'usage' as its default fallback method.
-    def usage(m, params)
+    def usage(m, params = {})
       m.reply "incorrect usage, ask for help using '#{@bot.nick}: help #{m.plugin}'"
     end
 
@@ -170,25 +171,27 @@ module Irc
     # load plugins from pre-assigned list of directories
     def scan
       dirs = Array.new
-      dirs << @bot.datadir + "/plugins"
+      dirs << Config::DATADIR + "/plugins"
       dirs += @dirs
       dirs.each {|dir|
         if(FileTest.directory?(dir))
           d = Dir.new(dir)
-          d.each {|file|
+          d.sort.each {|file|
             next if(file =~ /^\./)
             next unless(file =~ /\.rb$/)
             @tmpfilename = "#{dir}/#{file}"
 
             # create a new, anonymous module to "house" the plugin
+            # the idea here is to prevent namespace pollution. perhaps there
+            # is another way?
             plugin_module = Module.new
             
             begin
               plugin_string = IO.readlines(@tmpfilename).join("")
-              puts "loading module: #{@tmpfilename}"
+              debug "loading module: #{@tmpfilename}"
               plugin_module.module_eval(plugin_string)
             rescue StandardError, NameError, LoadError, SyntaxError => err
-              puts "plugin #{@tmpfilename} load failed: " + err
+              puts "warning: plugin #{@tmpfilename} load failed: " + err
               puts err.backtrace.join("\n")
             end
           }
@@ -198,28 +201,12 @@ module Irc
 
     # call the save method for each active plugin
     def save
-      @@plugins.values.uniq.each {|p|
-        next unless(p.respond_to?("save"))
-        begin
-          p.save
-        rescue StandardError, NameError, SyntaxError => err
-          puts "plugin #{p.name} save() failed: " + err
-          puts err.backtrace.join("\n")
-        end
-      }
+      delegate 'save'
     end
 
     # call the cleanup method for each active plugin
     def cleanup
-      @@plugins.values.uniq.each {|p|
-        next unless(p.respond_to?("cleanup"))
-        begin
-          p.cleanup
-        rescue StandardError, NameError, SyntaxError => err
-          puts "plugin #{p.name} cleanup() failed: " + err
-          puts err.backtrace.join("\n")
-        end
-      }
+      delegate 'cleanup'
     end
 
     # drop all plugins and rescan plugins on disk
@@ -265,11 +252,11 @@ module Irc
     
     # see if each plugin handles +method+, and if so, call it, passing
     # +message+ as a parameter
-    def delegate(method, message)
+    def delegate(method, *args)
       @@plugins.values.uniq.each {|p|
         if(p.respond_to? method)
           begin
-            p.send method, message
+            p.send method, *args
           rescue StandardError, NameError, SyntaxError => err
             puts "plugin #{p.name} #{method}() failed: " + err
             puts err.backtrace.join("\n")
@@ -298,3 +285,4 @@ module Irc
   end
 
 end
+end