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.
16 define_structure :Command, :code, :nick, :created, :channel
18 class ScriptPlugin < Plugin
22 if @registry.has_key?(:commands)
23 @commands = @registry[:commands]
24 raise unless @commands
32 @registry[:commands] = @commands
36 def help( plugin, topic="" )
38 "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'."
40 "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>."
46 name = m.message.split.first
48 if m.address? and @commands.has_key?( name )
49 code = @commands[name].code.dup.untaint
51 # Convenience variables, can be accessed by scripts:
52 args = m.message.split
54 user = args.empty? ? m.sourcenick : args.first
57 # TODO allow different safe levels for different botusers
63 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 :(" )
87 def handle_echo( m, params )
88 code = params[:code].to_s.dup.untaint
90 # TODO allow different safe levels for different botusers
92 m.reply eval( code ).to_s
94 m.reply( "Script '#{name}' crapped out :(" )
102 def handle_add( m, params, force = false )
104 if !force and @commands.has_key?( name )
105 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
109 code = params[:code].to_s
111 created = Time.new.strftime '%Y/%m/%d %H:%m'
114 command = Command.new( code, nick, created, channel )
115 @commands[name] = command
121 def handle_add_force( m, params )
122 handle_add( m, params, true )
126 def handle_del( m, params )
128 unless @commands.has_key?( name )
129 m.reply( "Script does not exist." ); return
132 @commands.delete( name )
137 def handle_list( m, params )
138 if @commands.length == 0
139 m.reply( "No scripts available." ); return
143 cmds = @commands.keys.sort
144 num_pages = cmds.length / cmds_per_page + 1
145 page = params[:page].to_i
147 page = [page, num_pages].min
148 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
150 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
154 def handle_show( m, params )
156 unless @commands.has_key?( name )
157 m.reply( "Script does not exist." ); return
160 cmd = @commands[name]
161 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
167 plugin = ScriptPlugin.new
169 plugin.default_auth( 'edit', false )
170 plugin.default_auth( 'eval', false )
171 plugin.default_auth( 'echo', false )
173 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
174 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
175 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
176 plugin.map 'script eval *code', :action => 'handle_eval'
177 plugin.map 'script echo *code', :action => 'handle_echo'
178 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
179 plugin.map 'script show :name', :action => 'handle_show'