]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/script.rb
script plugin: store channels as strings
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / script.rb
index c091b5864a430baa4fc291016ff888a6cdf1b208..eae1493956192ab65e7f7cf25e12df23f5102143 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
@@ -36,12 +35,18 @@ class ScriptPlugin < Plugin
 
   def help( plugin, topic="" )
     if topic == "add"
-      "Scripts are little Ruby programs that run in the context of the script plugin. You can access @bot (class IrcBot), m (class PrivMessage), user (class String, either the first argument, or if missing the sourcenick), and args (class Array, an array of arguments). Example: 'script add greet m.reply( 'Hello ' + user )'. Invoke the script just like a plugin: '<botnick>: greet'."
+      "Scripts are little Ruby programs that run in the context of the script plugin. You can access @bot (class Irc::Bot), m (class Irc::PrivMessage), user (class String, either the first argument, or if missing the sourcenick), and args (class Array, an array of arguments). Example: 'script add greet m.reply( 'Hello ' + user )'. Invoke the script just like a plugin: '<botnick>: greet'."
     else  
-      "Create mini plugins on IRC. 'script add <name> <code>' => Create script named <name> with the Ruby program <code>. 'script list' => Show a list of all known scripts. 'script show <name>' => Show the source code for <name>. 'script del <name>' => Delete the script <name>."
+      "Create mini plugins on IRC. 'script add <name> <code>' => Create script named <name> with the Ruby program <code>. 'script list' => Show a list of all known scripts. 'script show <name>' => Show the source code for <name>. 'script del <name>' => Delete the script <name>. 'script eval <expr>' => evaluate expression <expr>. 'script echo <expr>' => evaluate and display expression <expr>."
     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
 
 
@@ -107,12 +112,12 @@ class ScriptPlugin < Plugin
     code    = params[:code].to_s
     nick    = m.sourcenick
     created = Time.new.strftime '%Y/%m/%d %H:%m'
-    channel = m.target
+    channel = m.target.to_s
 
     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