X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fscript.rb;h=1b69586816c06a53365da5299a8f27ddc796cf07;hb=7b7f1309e8c3dbc3bb4408d56489ae5fba77d57a;hp=f559addd6941ed79a0d7310e751958200883163d;hpb=e90a6ac1e600db1a6b19c56532e4a1f268455a8b;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/script.rb b/data/rbot/plugins/script.rb index f559addd..1b695868 100644 --- a/data/rbot/plugins/script.rb +++ b/data/rbot/plugins/script.rb @@ -1,12 +1,17 @@ -# 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 +# +# Author:: Mark Kretschmann +# Copyright:: (C) 2006 Mark Kretschmann +# License:: GPL v2 # -# 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. +# Create mini plugins on IRC. # -# (c) 2006 Mark Kretschmann -# Licensed under GPL V2. +# 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. Command = Struct.new( "Command", :code, :nick, :created, :channel ) @@ -18,18 +23,9 @@ class ScriptPlugin < Plugin super if @registry.has_key?(:commands) @commands = @registry[:commands] - end - - if @commands.nil? + 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,9 +36,9 @@ 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: ': 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: ': greet'." else - "Create mini plugins on IRC. 'script add ' => Create script named with the code . 'script list' => Show a list of all known scripts. 'script show ' => Show the source code for . 'script del ' => Delete the script ." + "Create mini plugins on IRC. 'script add ' => Create script named with the Ruby program . 'script list' => Show a list of all known scripts. 'script show ' => Show the source code for . 'script del ' => Delete the script ." end end @@ -59,6 +55,7 @@ class ScriptPlugin < Plugin user = args.empty? ? m.sourcenick : args.first Thread.start { + # TODO allow different safe levels for different botusers $SAFE = 3 begin @@ -72,6 +69,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 ).to_s + 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 ) @@ -79,7 +104,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 @@ -138,12 +163,16 @@ end plugin = ScriptPlugin.new -plugin.register( "script" ) + 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'