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
20 Config.register Config::IntegerValue.new('script.safe',
22 :desc => 'configure $SAFE level for scripts (3=safe/tainted, 0=unsafe/ruby default)')
26 if @registry.has_key?(:commands)
27 @commands = @registry[:commands]
28 raise LoadError, "corrupted script database" unless @commands
36 @registry[:commands] = @commands
40 def help( plugin, topic="" )
43 "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'."
45 "script allow <script> for <user> [where] => allow <user> to run script <script> [where]"
47 "script deny <script> for <user> [where] => prevent <user> from running script <script> [where]"
49 "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."
53 def report_error(m, name, e)
54 # ed = e.backtrace.unshift(e.inspect).join(' ')
56 m.reply( "Script '#{name}' crapped out :( #{ed}" )
61 name = m.message.split.first
63 if m.address? and @commands.has_key?( name )
64 auth_path = "script::run::#{name}".intern
65 return unless @bot.auth.allow?(auth_path, m.source, m.replyto)
67 code = @commands[name].code.dup.untaint
69 # Convenience variables, can be accessed by scripts:
70 args = m.message.split
72 user = args.empty? ? m.sourcenick : args.first
75 $SAFE = @bot.config['script.safe']
80 report_error(m, name, e)
87 def handle_allow_deny(m, p)
89 if @commands.has_key?( name )
90 @bot.plugins['auth'].auth_allow_deny(m, p.merge(
91 :auth_path => "script::run::#{name}".intern
94 m.reply(_("%{stuff} is not a script I know of") % p)
98 def handle_allow(m, p)
99 handle_allow_deny(m, p.merge(:allow => true))
102 def handle_deny(m, p)
103 handle_allow_deny(m, p.merge(:allow => false))
108 def handle_eval( m, params )
109 code = params[:code].to_s.dup.untaint
111 $SAFE = @bot.config['script.safe']
115 rescue Exception => e
116 report_error(m, code, e)
123 def handle_echo( m, params )
124 code = params[:code].to_s.dup.untaint
126 $SAFE = @bot.config['script.safe']
129 m.reply eval( code ).to_s
130 rescue Exception => e
131 report_error(m, code, e)
138 def handle_add( m, params, force = false )
140 if !force and @commands.has_key?( name )
141 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
145 code = params[:code].to_s
147 created = Time.new.strftime '%Y/%m/%d %H:%m'
148 channel = m.target.to_s
150 command = Command.new( code, nick, created, channel )
151 @commands[name] = command
157 def handle_add_force( m, params )
158 handle_add( m, params, true )
162 def handle_del( m, params )
164 unless @commands.has_key?( name )
165 m.reply( "Script does not exist." ); return
168 @commands.delete( name )
173 def handle_list( m, params )
174 if @commands.length == 0
175 m.reply( "No scripts available." ); return
179 cmds = @commands.keys.sort
180 num_pages = cmds.length / cmds_per_page + 1
181 page = params[:page].to_i
183 page = [page, num_pages].min
184 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
186 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
190 def handle_show( m, params )
192 unless @commands.has_key?( name )
193 m.reply( "Script does not exist." ); return
196 cmd = @commands[name]
197 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
203 plugin = ScriptPlugin.new
205 plugin.default_auth( 'edit', false )
206 plugin.default_auth( 'eval', false )
207 plugin.default_auth( 'echo', false )
208 plugin.default_auth( 'run', true )
210 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
211 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
212 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
213 plugin.map 'script eval *code', :action => 'handle_eval'
214 plugin.map 'script echo *code', :action => 'handle_echo'
215 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
216 plugin.map 'script show :name', :action => 'handle_show'
218 plugin.map 'script allow :stuff for :user [*where]',
219 :action => 'handle_allow',
220 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
222 plugin.map 'script deny :stuff for :user [*where]',
223 :action => 'handle_deny',
224 :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},