]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/script.rb
script plugin: echo function. like eval, but implies an m.reply
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / script.rb
index 171dabcb5a813f73ce75fb22a0c4ce54afe61ef5..60c0e97329745a108c30fe4093a2f6ec991e2826 100644 (file)
@@ -18,7 +18,9 @@ class ScriptPlugin < Plugin
     super
     if @registry.has_key?(:commands)
       @commands = @registry[:commands]
-    else
+    end
+    
+    if @commands.nil?
       @commands = Hash.new
     end
 
@@ -57,6 +59,7 @@ class ScriptPlugin < Plugin
       user = args.empty? ? m.sourcenick : args.first  
 
       Thread.start {
+        # TODO allow different safe levels for different botusers
         $SAFE = 3
 
         begin
@@ -70,6 +73,34 @@ class ScriptPlugin < Plugin
   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
+      }
+  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 )
+        rescue => e
+          m.reply( "Script '#{name}' crapped out :(" )
+          m.reply( e.inspect )
+        end
+      }
+  end
+
+
   def handle_add( m, params, force = false )
     name    = params[:name]
     if !force and @commands.has_key?( name )
@@ -77,7 +108,7 @@ class ScriptPlugin < Plugin
       return
     end
 
-    code    = params[:code].join( " " )
+    code    = params[:code].to_s
     nick    = m.sourcenick
     created = Time.new.strftime '%Y/%m/%d %H:%m'
     channel = m.target
@@ -137,10 +168,14 @@ end
 
 plugin = ScriptPlugin.new
 plugin.register( "script" )
-
-plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth => 'scriptedit'
-plugin.map 'script add :name *code',    :action => 'handle_add',       :auth => 'scriptedit'
-plugin.map 'script del :name',          :action => 'handle_del',       :auth => 'scriptedit'
+plugin.default_auth( 'edit', false )
+plugin.default_auth( 'eval', false )
+
+plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
+plugin.map 'script add :name *code',    :action => 'handle_add',       :auth_path => 'edit'
+plugin.map 'script del :name',          :action => 'handle_del',       :auth_path => 'edit'
+plugin.map 'script eval *code',         :action => 'handle_eval'
+plugin.map 'script echo *code',         :action => 'handle_echo'
 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
 plugin.map 'script show :name',         :action => 'handle_show'