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 :(" )
71 def handle_eval( m, params )
72 code = params[:code].to_s.dup.untaint
74 # TODO allow different safe levels for different botusers
78 m.reply( "Script '#{name}' crapped out :(" )
85 def handle_echo( m, params )
86 code = params[:code].to_s.dup.untaint
88 # TODO allow different safe levels for different botusers
90 m.reply eval( code ).to_s
92 m.reply( "Script '#{name}' crapped out :(" )
99 def handle_add( m, params, force = false )
101 if !force and @commands.has_key?( name )
102 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
106 code = params[:code].to_s
108 created = Time.new.strftime '%Y/%m/%d %H:%m'
111 command = Command.new( code, nick, created, channel )
112 @commands[name] = command
118 def handle_add_force( m, params )
119 handle_add( m, params, true )
123 def handle_del( m, params )
125 unless @commands.has_key?( name )
126 m.reply( "Script does not exist." ); return
129 @commands.delete( name )
134 def handle_list( m, params )
135 if @commands.length == 0
136 m.reply( "No scripts available." ); return
140 cmds = @commands.keys.sort
141 num_pages = cmds.length / cmds_per_page + 1
142 page = params[:page].to_i
144 page = [page, num_pages].min
145 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
147 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
151 def handle_show( m, params )
153 unless @commands.has_key?( name )
154 m.reply( "Script does not exist." ); return
157 cmd = @commands[name]
158 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
164 plugin = ScriptPlugin.new
166 plugin.default_auth( 'edit', false )
167 plugin.default_auth( 'eval', false )
168 plugin.default_auth( 'echo', false )
170 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
171 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
172 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
173 plugin.map 'script eval *code', :action => 'handle_eval'
174 plugin.map 'script echo *code', :action => 'handle_echo'
175 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
176 plugin.map 'script show :name', :action => 'handle_show'