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 LoadError, "corrupted script database" unless @commands
32 @registry[:commands] = @commands
36 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 "script allow <script> for <user> [where] => allow <user> to run script <script> [where]"
43 "script deny <script> for <user> [where] => prevent <user> from running script <script> [where]"
45 "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>. 'script eval <expr>' => evaluate expression <expr>. 'script echo <expr>' => evaluate and display expression <expr>. See also: add, allow, deny."
49 def report_error(m, name, e)
50 # ed = e.backtrace.unshift(e.inspect).join(' ')
52 m.reply( "Script '#{name}' crapped out :( #{ed}" )
57 name = m.message.split.first
59 if m.address? and @commands.has_key?( name )
60 auth_path = "script::run::#{name}".intern
61 return unless @bot.auth.allow?(auth_path, m.source, m.replyto)
63 code = @commands[name].code.dup.untaint
65 # Convenience variables, can be accessed by scripts:
66 args = m.message.split
68 user = args.empty? ? m.sourcenick : args.first
71 # TODO allow different safe levels for different botusers
77 report_error(m, name, e)
84 def handle_allow_deny(m, p)
86 if @commands.has_key?( name )
87 @bot.plugins['auth'].auth_allow_deny(m, p.merge(
88 :auth_path => "script::run::#{name}".intern
91 m.reply(_("%{stuff} is not a script I know of") % p)
95 def handle_allow(m, p)
96 handle_allow_deny(m, p.merge(:allow => true))
100 handle_allow_deny(m, p.merge(:allow => false))
105 def handle_eval( m, params )
106 code = params[:code].to_s.dup.untaint
108 # TODO allow different safe levels for different botusers
111 rescue Exception => e
112 report_error(m, code, e)
119 def handle_echo( m, params )
120 code = params[:code].to_s.dup.untaint
122 # TODO allow different safe levels for different botusers
124 m.reply eval( code ).to_s
125 rescue Exception => e
126 report_error(m, code, e)
133 def handle_add( m, params, force = false )
135 if !force and @commands.has_key?( name )
136 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
140 code = params[:code].to_s
142 created = Time.new.strftime '%Y/%m/%d %H:%m'
143 channel = m.target.to_s
145 command = Command.new( code, nick, created, channel )
146 @commands[name] = command
152 def handle_add_force( m, params )
153 handle_add( m, params, true )
157 def handle_del( m, params )
159 unless @commands.has_key?( name )
160 m.reply( "Script does not exist." ); return
163 @commands.delete( name )
168 def handle_list( m, params )
169 if @commands.length == 0
170 m.reply( "No scripts available." ); return
174 cmds = @commands.keys.sort
175 num_pages = cmds.length / cmds_per_page + 1
176 page = params[:page].to_i
178 page = [page, num_pages].min
179 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
181 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
185 def handle_show( m, params )
187 unless @commands.has_key?( name )
188 m.reply( "Script does not exist." ); return
191 cmd = @commands[name]
192 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
198 plugin = ScriptPlugin.new
200 plugin.default_auth( 'edit', false )
201 plugin.default_auth( 'eval', false )
202 plugin.default_auth( 'echo', false )
203 plugin.default_auth( 'run', true )
205 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
206 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
207 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
208 plugin.map 'script eval *code', :action => 'handle_eval'
209 plugin.map 'script echo *code', :action => 'handle_echo'
210 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
211 plugin.map 'script show :name', :action => 'handle_show'
213 plugin.map 'script allow :stuff for :user [*where]',
214 :action => 'handle_allow',
215 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
217 plugin.map 'script deny :stuff for :user [*where]',
218 :action => 'handle_deny',
219 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},