]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/script.rb
script plugin: report_error() method
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / script.rb
index 1b69586816c06a53365da5299a8f27ddc796cf07..2d730fe92c426cb8cd012a3d03ae0db9d0ba35e5 100644 (file)
@@ -13,9 +13,7 @@
 # plugin. You can create them directly in an IRC channel, and invoke them just
 # like normal rbot plugins. 
 
-
-Command = Struct.new( "Command", :code, :nick, :created, :channel )
-
+define_structure :Command, :code, :nick, :created, :channel
 
 class ScriptPlugin < Plugin
 
@@ -23,6 +21,7 @@ class ScriptPlugin < Plugin
     super
     if @registry.has_key?(:commands)
       @commands = @registry[:commands]
+      raise unless @commands
     else
       @commands = Hash.new
     end
@@ -42,6 +41,12 @@ class ScriptPlugin < Plugin
     end
   end
 
+  def report_error(m, name, e)
+    # ed = e.backtrace.unshift(e.inspect).join(' ')
+    ed = e.inspect
+    m.reply( "Script '#{name}' crapped out :( #{ed}" )
+  end
+
 
   def listen( m )
     name = m.message.split.first
@@ -60,40 +65,40 @@ class ScriptPlugin < Plugin
 
         begin
           eval( code )
-        rescue => e
-          m.reply( "Script '#{name}' crapped out :(" )
-          m.reply( e.inspect )
+        rescue Exception => e
+          report_error(m, name, e)
         end
       }
+      m.replied = true
     end
   end
 
 
   def handle_eval( m, params )
     code = params[:code].to_s.dup.untaint
-      Thread.start {
-        # TODO allow different safe levels for different botusers
-        begin
-          eval( code )
-        rescue => e
-          m.reply( "Script '#{name}' crapped out :(" )
-          m.reply( e.inspect )
-        end
-      }
+    Thread.start {
+      # TODO allow different safe levels for different botusers
+      begin
+        eval( code )
+      rescue Exception => e
+        report_error(m, code, e)
+      end
+    }
+    m.replied = true
   end
 
 
   def handle_echo( m, params )
     code = params[:code].to_s.dup.untaint
-      Thread.start {
-        # TODO allow different safe levels for different botusers
-        begin
-          m.reply eval( code ).to_s
-        rescue => e
-          m.reply( "Script '#{name}' crapped out :(" )
-          m.reply( e.inspect )
-        end
-      }
+    Thread.start {
+      # TODO allow different safe levels for different botusers
+      begin
+        m.reply eval( code ).to_s
+      rescue Exception => e
+        report_error(m, code, e)
+      end
+    }
+    m.replied = true
   end
 
 
@@ -112,7 +117,7 @@ class ScriptPlugin < Plugin
     command = Command.new( code, nick, created, channel )
     @commands[name] = command
 
-    m.reply( "done" )
+    m.okay
   end
 
 
@@ -128,7 +133,7 @@ class ScriptPlugin < Plugin
     end
 
     @commands.delete( name )
-    m.reply( "done" )
+    m.okay
   end