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>. 'script eval <expr>' => evaluate expression <expr>. 'script echo <expr>' => evaluate and display expression <expr>."
44 def report_error(m, name, e)
45 # ed = e.backtrace.unshift(e.inspect).join(' ')
47 m.reply( "Script '#{name}' crapped out :( #{ed}" )
52 name = m.message.split.first
54 if m.address? and @commands.has_key?( name )
55 auth_path = "script::run::#{name}".intern
56 return unless @bot.auth.allow?(auth_path, m.source, m.replyto)
58 code = @commands[name].code.dup.untaint
60 # Convenience variables, can be accessed by scripts:
61 args = m.message.split
63 user = args.empty? ? m.sourcenick : args.first
66 # TODO allow different safe levels for different botusers
72 report_error(m, name, e)
79 def handle_allow_deny(m, p)
81 if @commands.has_key?( name )
82 @bot.plugins['auth'].auth_allow_deny(m, p.merge(
83 :auth_path => "script::run::#{name}".intern
86 m.reply(_("%{stuff} is not a script I know of") % p)
90 def handle_allow(m, p)
91 handle_allow_deny(m, p.merge(:allow => true))
95 handle_allow_deny(m, p.merge(:allow => false))
100 def handle_eval( m, params )
101 code = params[:code].to_s.dup.untaint
103 # TODO allow different safe levels for different botusers
106 rescue Exception => e
107 report_error(m, code, e)
114 def handle_echo( m, params )
115 code = params[:code].to_s.dup.untaint
117 # TODO allow different safe levels for different botusers
119 m.reply eval( code ).to_s
120 rescue Exception => e
121 report_error(m, code, e)
128 def handle_add( m, params, force = false )
130 if !force and @commands.has_key?( name )
131 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
135 code = params[:code].to_s
137 created = Time.new.strftime '%Y/%m/%d %H:%m'
138 channel = m.target.to_s
140 command = Command.new( code, nick, created, channel )
141 @commands[name] = command
147 def handle_add_force( m, params )
148 handle_add( m, params, true )
152 def handle_del( m, params )
154 unless @commands.has_key?( name )
155 m.reply( "Script does not exist." ); return
158 @commands.delete( name )
163 def handle_list( m, params )
164 if @commands.length == 0
165 m.reply( "No scripts available." ); return
169 cmds = @commands.keys.sort
170 num_pages = cmds.length / cmds_per_page + 1
171 page = params[:page].to_i
173 page = [page, num_pages].min
174 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
176 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
180 def handle_show( m, params )
182 unless @commands.has_key?( name )
183 m.reply( "Script does not exist." ); return
186 cmd = @commands[name]
187 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
193 plugin = ScriptPlugin.new
195 plugin.default_auth( 'edit', false )
196 plugin.default_auth( 'eval', false )
197 plugin.default_auth( 'echo', false )
198 plugin.default_auth( 'run', true )
200 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
201 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
202 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
203 plugin.map 'script eval *code', :action => 'handle_eval'
204 plugin.map 'script echo *code', :action => 'handle_echo'
205 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
206 plugin.map 'script show :name', :action => 'handle_show'
208 plugin.map 'script allow :stuff for :user [*where]',
209 :action => 'handle_allow',
210 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
212 plugin.map 'script deny :stuff for :user [*where]',
213 :action => 'handle_deny',
214 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},