X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fscript.rb;h=8a3e1e847a0876416906a8b86186ba9a91c95e6a;hb=27cb79be57f9377d8c13dab2d7d0776e80dfd115;hp=431b52463d4b04313e03a7a750a6bc9535ab3005;hpb=b565bf81cbe456205b9f9fcf9e3960109c5b7de6;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/script.rb b/data/rbot/plugins/script.rb index 431b5246..8a3e1e84 100644 --- a/data/rbot/plugins/script.rb +++ b/data/rbot/plugins/script.rb @@ -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 +# Copyright:: (C) 2006 Mark Kretschmann +# License:: GPL v2 # -# (c) 2006 Mark Kretschmann -# 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,9 +35,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 @@ -64,40 +59,43 @@ class ScriptPlugin < Plugin begin eval( code ) - rescue => e + rescue Exception => e m.reply( "Script '#{name}' crapped out :(" ) m.reply( e.inspect ) 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 + m.reply( "Script '#{name}' crapped out :(" ) + m.reply( e.inspect ) + 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 + m.reply( "Script '#{name}' crapped out :(" ) + m.reply( e.inspect ) + end + } + m.replied = true end @@ -116,7 +114,7 @@ class ScriptPlugin < Plugin command = Command.new( code, nick, created, channel ) @commands[name] = command - m.reply( "done" ) + m.okay end @@ -132,7 +130,7 @@ class ScriptPlugin < Plugin end @commands.delete( name ) - m.reply( "done" ) + m.okay end @@ -167,7 +165,7 @@ end plugin = ScriptPlugin.new -plugin.register( "script" ) + plugin.default_auth( 'edit', false ) plugin.default_auth( 'eval', false ) plugin.default_auth( 'echo', false )