]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/plugins.rb
fix httputil.rb when using proxy (Ticket #80)
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins.rb
index 1a66b7d3250ef4fe120a7daa4b1f888818368b74..54b814e0eb400c89e5c58c09c0f017a3141ce93d 100644 (file)
@@ -1,4 +1,5 @@
 module Irc
+module Plugins
   require 'rbot/messagemapper'
 
   # base class for all rbot plugins
@@ -7,8 +8,8 @@ module Irc
   #
   # map(template, options)::
   #    map is the new, cleaner way to respond to specific message formats
-  #    without littering your plugin code with regexps
-  #    examples:
+  #    without littering your plugin code with regexps. examples:
+  #
   #      plugin.map 'karmastats', :action => 'karma_stats'
   #
   #      # while in the plugin...
@@ -77,6 +78,9 @@ module Irc
   # topic(TopicMessage)::
   #                        Called when a user (or the bot) changes a channel
   #                        topic
+  #
+  # connect()::            Called when a server is joined successfully, but
+  #                        before autojoin channels are joined (no params)
   # 
   # save::                 Called when you are required to save your plugin's
   #                        state, if you maintain data between sessions
@@ -95,6 +99,10 @@ module Irc
       @registry = BotRegistryAccessor.new(@bot, self.class.to_s.gsub(/^.*::/, ""))
     end
 
+    def flush_registry
+      @registry.flush
+    end
+
     def map(*args)
       @handler.map(*args)
       # register this map
@@ -133,7 +141,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
 
@@ -169,26 +177,36 @@ module Irc
 
     # load plugins from pre-assigned list of directories
     def scan
+      processed = Array.new
       dirs = Array.new
-      dirs << Config::DATADIR + "/plugins"
+      dirs << Config::datadir + "/plugins"
       dirs += @dirs
-      dirs.each {|dir|
+      dirs.reverse.each {|dir|
         if(FileTest.directory?(dir))
           d = Dir.new(dir)
-          d.each {|file|
+          d.sort.each {|file|
             next if(file =~ /^\./)
+            next if(processed.include?(file))
+            if(file =~ /^(.+\.rb)\.disabled$/)
+              processed << $1
+              next
+            end
             next unless(file =~ /\.rb$/)
-            @tmpfilename = "#{dir}/#{file}"
+            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}"
+              plugin_string = IO.readlines(tmpfilename).join("")
+              debug "loading plugin #{tmpfilename}"
               plugin_module.module_eval(plugin_string)
-            rescue StandardError, NameError, LoadError, SyntaxError => err
-              puts "plugin #{@tmpfilename} load failed: " + err
+              processed << file
+            rescue Exception => err
+              # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
+              puts "warning: plugin #{tmpfilename} load failed: " + err
               puts err.backtrace.join("\n")
             end
           }
@@ -198,28 +216,13 @@ 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 'flush_registry'
+      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
@@ -253,7 +256,8 @@ module Irc
         if(@@plugins.has_key?(key))
           begin
             return @@plugins[key].help(key, params)
-          rescue StandardError, NameError, SyntaxError => err
+          rescue Exception => err
+          #rescue TimeoutError, StandardError, NameError, SyntaxError => err
             puts "plugin #{@@plugins[key].name} help() failed: " + err
             puts err.backtrace.join("\n")
           end
@@ -265,12 +269,13 @@ 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
-          rescue StandardError, NameError, SyntaxError => err
+            p.send method, *args
+          rescue Exception => err
+            #rescue TimeoutError, StandardError, NameError, SyntaxError => err
             puts "plugin #{p.name} #{method}() failed: " + err
             puts err.backtrace.join("\n")
           end
@@ -287,7 +292,8 @@ module Irc
           @@bot.auth.allow?(m.plugin, m.source, m.replyto))
         begin
           @@plugins[m.plugin].privmsg(m)
-        rescue StandardError, NameError, SyntaxError => err
+        rescue Exception => err
+          #rescue TimeoutError, StandardError, NameError, SyntaxError => err
           puts "plugin #{@@plugins[m.plugin].name} privmsg() failed: " + err
           puts err.backtrace.join("\n")
         end
@@ -298,3 +304,4 @@ module Irc
   end
 
 end
+end