4 # :title: Script plugin for rbot
6 # Author:: Mark Kretschmann <markey@web.de>
7 # Copyright:: (C) 2006 Mark Kretschmann
10 # Create mini plugins on IRC.
12 # Scripts are little Ruby programs that run in the context of the script
13 # plugin. You can create them directly in an IRC channel, and invoke them just
14 # like normal rbot plugins.
17 Command = Struct.new( "Command", :code, :nick, :created, :channel )
20 class ScriptPlugin < Plugin
24 if @registry.has_key?(:commands)
25 @commands = @registry[:commands]
33 @registry[:commands] = @commands
37 def help( plugin, topic="" )
39 "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'."
41 "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>."
47 name = m.message.split.first
49 if m.address? and @commands.has_key?( name )
50 code = @commands[name].code.dup.untaint
52 # Convenience variables, can be accessed by scripts:
53 args = m.message.split
55 user = args.empty? ? m.sourcenick : args.first
58 # TODO allow different safe levels for different botusers
64 m.reply( "Script '#{name}' crapped out :(" )
72 def handle_eval( m, params )
73 code = params[:code].to_s.dup.untaint
75 # TODO allow different safe levels for different botusers
79 m.reply( "Script '#{name}' crapped out :(" )
86 def handle_echo( m, params )
87 code = params[:code].to_s.dup.untaint
89 # TODO allow different safe levels for different botusers
91 m.reply eval( code ).to_s
93 m.reply( "Script '#{name}' crapped out :(" )
100 def handle_add( m, params, force = false )
102 if !force and @commands.has_key?( name )
103 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
107 code = params[:code].to_s
109 created = Time.new.strftime '%Y/%m/%d %H:%m'
112 command = Command.new( code, nick, created, channel )
113 @commands[name] = command
119 def handle_add_force( m, params )
120 handle_add( m, params, true )
124 def handle_del( m, params )
126 unless @commands.has_key?( name )
127 m.reply( "Script does not exist." ); return
130 @commands.delete( name )
135 def handle_list( m, params )
136 if @commands.length == 0
137 m.reply( "No scripts available." ); return
141 cmds = @commands.keys.sort
142 num_pages = cmds.length / cmds_per_page + 1
143 page = params[:page].to_i
145 page = [page, num_pages].min
146 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
148 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
152 def handle_show( m, params )
154 unless @commands.has_key?( name )
155 m.reply( "Script does not exist." ); return
158 cmd = @commands[name]
159 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
165 plugin = ScriptPlugin.new
167 plugin.default_auth( 'edit', false )
168 plugin.default_auth( 'eval', false )
169 plugin.default_auth( 'echo', false )
171 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
172 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
173 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
174 plugin.map 'script eval *code', :action => 'handle_eval'
175 plugin.map 'script echo *code', :action => 'handle_echo'
176 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
177 plugin.map 'script show :name', :action => 'handle_show'