]> 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 f744ed01a08af2fa9b4f91e04d5e3fe0465f9ad4..2d730fe92c426cb8cd012a3d03ae0db9d0ba35e5 100644 (file)
@@ -1,16 +1,19 @@
-# Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
+#-- vim:sw=2:et
+#++
 #
-# Create mini plugins on IRC.
+# :title: Script plugin for rbot
 #
-# Scripts are little Ruby programs that run in the context of the script plugin. You 
-# can create them directly in an IRC channel, and invoke them just like normal rbot plugins. 
+# Author:: Mark Kretschmann <markey@web.de>
+# Copyright:: (C) 2006 Mark Kretschmann
+# License:: GPL v2
 #
-# (c) 2006 Mark Kretschmann <markey@web.de>
-# Licensed under GPL V2.
-
-
-Command = Struct.new( "Command", :code, :nick, :created, :channel )
+# Create mini plugins on IRC.
+#
+# Scripts are little Ruby programs that run in the context of the script
+# plugin. You can create them directly in an IRC channel, and invoke them just
+# like normal rbot plugins. 
 
+define_structure :Command, :code, :nick, :created, :channel
 
 class ScriptPlugin < Plugin
 
@@ -18,18 +21,10 @@ class ScriptPlugin < Plugin
     super
     if @registry.has_key?(:commands)
       @commands = @registry[:commands]
-    end
-    
-    if @commands.nil?
+      raise unless @commands
+    else
       @commands = Hash.new
     end
-
-    # Migrate old Hash to new:
-    @commands.each_pair do |name, cmd|
-      unless cmd.instance_of?( Command )
-        @commands[name] = Command.new( cmd, 'unknown hacker', 'somedate', '#somechan' )
-      end
-    end
   end
 
 
@@ -40,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 code <source>. '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>."
     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
@@ -59,19 +60,48 @@ class ScriptPlugin < Plugin
       user = args.empty? ? m.sourcenick : args.first  
 
       Thread.start {
+        # TODO allow different safe levels for different botusers
         $SAFE = 3
 
         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 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 Exception => e
+        report_error(m, code, e)
+      end
+    }
+    m.replied = true
+  end
+
+
   def handle_add( m, params, force = false )
     name    = params[:name]
     if !force and @commands.has_key?( name )
@@ -79,7 +109,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
@@ -87,7 +117,7 @@ class ScriptPlugin < Plugin
     command = Command.new( code, nick, created, channel )
     @commands[name] = command
 
-    m.reply( "done" )
+    m.okay
   end
 
 
@@ -103,7 +133,7 @@ class ScriptPlugin < Plugin
     end
 
     @commands.delete( name )
-    m.reply( "done" )
+    m.okay
   end
 
 
@@ -138,11 +168,16 @@ 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.default_auth( 'echo', 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'