From 05917e75de2cbd14071ef335f4c3129a33f30cf6 Mon Sep 17 00:00:00 2001 From: Mark Kretschmann Date: Tue, 1 Aug 2006 11:32:06 +0000 Subject: rename file --- data/rbot/plugins/command.rb | 147 ------------------------------------------- data/rbot/plugins/script.rb | 147 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 147 deletions(-) delete mode 100644 data/rbot/plugins/command.rb create mode 100644 data/rbot/plugins/script.rb (limited to 'data/rbot/plugins') diff --git a/data/rbot/plugins/command.rb b/data/rbot/plugins/command.rb deleted file mode 100644 index 8234442c..00000000 --- a/data/rbot/plugins/command.rb +++ /dev/null @@ -1,147 +0,0 @@ -# Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/) -# -# 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. -# -# (c) 2006 Mark Kretschmann -# Licensed under GPL V2. - - -Command = Struct.new( "Command", :code, :nick, :created, :channel ) - - -class ScriptPlugin < Plugin - - def initialize - super - if @registry.has_key?(:commands) - @commands = @registry[: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 - - - def save - @registry[:commands] = @commands - end - - - 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'." - 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 ." - end - end - - - def listen( m ) - name = m.message.split.first - - if m.address? and @commands.has_key?( name ) - code = @commands[name].code.dup.untaint - - # Convenience variables, can be accessed by scripts: - args = m.message.split - args.delete_at( 0 ) - user = args.empty? ? m.sourcenick : args.first - - Thread.start { - $SAFE = 3 - - begin - eval( code ) - rescue => e - m.reply( "Script '#{name}' crapped out :(" ) - m.reply( e.inspect ) - end - } - end - end - - - def handle_add( m, params, force = false ) - name = params[:name] - if !force and @commands.has_key?( name ) - m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." ) - return - end - - code = params[:code].join( " " ) - nick = m.sourcenick - created = Time.new.strftime '%Y/%m/%d %H:%m' - channel = m.target - - command = Command.new( code, nick, created, channel ) - @commands[name] = command - - m.reply( "done" ) - end - - - def handle_add_force( m, params ) - handle_add( m, params, true ) - end - - - def handle_del( m, params ) - name = params[:name] - unless @commands.has_key?( name ) - m.reply( "Script does not exist." ); return - end - - @commands.delete( name ) - m.reply( "done" ) - end - - - def handle_list( m, params ) - if @commands.length == 0 - m.reply( "No scripts available." ); return - end - - cmds_per_page = 30 - cmds = @commands.keys.sort - num_pages = cmds.length / cmds_per_page - page = params[:page].to_i - page = [page, 1].max - page = [page, num_pages].min - str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') - - m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" - end - - - def handle_show( m, params ) - name = params[:name] - unless @commands.has_key?( name ) - m.reply( "Script does not exist." ); return - end - - cmd = @commands[name] - m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" ) - end - -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.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' } -plugin.map 'script show :name', :action => 'handle_show' - - diff --git a/data/rbot/plugins/script.rb b/data/rbot/plugins/script.rb new file mode 100644 index 00000000..8234442c --- /dev/null +++ b/data/rbot/plugins/script.rb @@ -0,0 +1,147 @@ +# Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/) +# +# 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. +# +# (c) 2006 Mark Kretschmann +# Licensed under GPL V2. + + +Command = Struct.new( "Command", :code, :nick, :created, :channel ) + + +class ScriptPlugin < Plugin + + def initialize + super + if @registry.has_key?(:commands) + @commands = @registry[: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 + + + def save + @registry[:commands] = @commands + end + + + 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'." + 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 ." + end + end + + + def listen( m ) + name = m.message.split.first + + if m.address? and @commands.has_key?( name ) + code = @commands[name].code.dup.untaint + + # Convenience variables, can be accessed by scripts: + args = m.message.split + args.delete_at( 0 ) + user = args.empty? ? m.sourcenick : args.first + + Thread.start { + $SAFE = 3 + + begin + eval( code ) + rescue => e + m.reply( "Script '#{name}' crapped out :(" ) + m.reply( e.inspect ) + end + } + end + end + + + def handle_add( m, params, force = false ) + name = params[:name] + if !force and @commands.has_key?( name ) + m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." ) + return + end + + code = params[:code].join( " " ) + nick = m.sourcenick + created = Time.new.strftime '%Y/%m/%d %H:%m' + channel = m.target + + command = Command.new( code, nick, created, channel ) + @commands[name] = command + + m.reply( "done" ) + end + + + def handle_add_force( m, params ) + handle_add( m, params, true ) + end + + + def handle_del( m, params ) + name = params[:name] + unless @commands.has_key?( name ) + m.reply( "Script does not exist." ); return + end + + @commands.delete( name ) + m.reply( "done" ) + end + + + def handle_list( m, params ) + if @commands.length == 0 + m.reply( "No scripts available." ); return + end + + cmds_per_page = 30 + cmds = @commands.keys.sort + num_pages = cmds.length / cmds_per_page + page = params[:page].to_i + page = [page, 1].max + page = [page, num_pages].min + str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') + + m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" + end + + + def handle_show( m, params ) + name = params[:name] + unless @commands.has_key?( name ) + m.reply( "Script does not exist." ); return + end + + cmd = @commands[name] + m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" ) + end + +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.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' } +plugin.map 'script show :name', :action => 'handle_show' + + -- cgit v1.2.3