]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/script.rb
script: make $SAFE configureable
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / script.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Script plugin for rbot
5 #
6 # Author:: Mark Kretschmann <markey@web.de>
7 # Copyright:: (C) 2006 Mark Kretschmann
8 # License:: GPL v2
9 #
10 # Create mini plugins on IRC.
11 #
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.
15
16 define_structure :Command, :code, :nick, :created, :channel
17
18 class ScriptPlugin < Plugin
19
20   Config.register Config::IntegerValue.new('script.safe',
21     :default => 3,
22     :desc => 'configure $SAFE level for scripts (3=safe/tainted, 0=unsafe/ruby default)')
23
24   def initialize
25     super
26     if @registry.has_key?(:commands)
27       @commands = @registry[:commands]
28       raise LoadError, "corrupted script database" unless @commands
29     else
30       @commands = Hash.new
31     end
32   end
33
34
35   def save
36     @registry[:commands] = @commands
37   end
38
39
40   def help( plugin, topic="" )
41     case topic
42     when "add"
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'."
44     when "allow"
45       "script allow <script> for <user> [where] => allow <user> to run script <script> [where]"
46     when "allow"
47       "script deny <script> for <user> [where] => prevent <user> from running script <script> [where]"
48     else
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."
50     end
51   end
52
53   def report_error(m, name, e)
54     # ed = e.backtrace.unshift(e.inspect).join(' ')
55     ed = e.inspect
56     m.reply( "Script '#{name}' crapped out :( #{ed}" )
57   end
58
59
60   def message( m )
61     name = m.message.split.first
62
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)
66
67       code = @commands[name].code.dup.untaint
68
69       # Convenience variables, can be accessed by scripts:
70       args = m.message.split
71       args.delete_at( 0 )
72       user = args.empty? ? m.sourcenick : args.first
73
74       Thread.start {
75         $SAFE = @bot.config['script.safe']
76
77         begin
78           eval( code )
79         rescue Exception => e
80           report_error(m, name, e)
81         end
82       }
83       m.replied = true
84     end
85   end
86
87   def handle_allow_deny(m, p)
88     name = p[:stuff]
89     if @commands.has_key?( name )
90       @bot.plugins['auth'].auth_allow_deny(m, p.merge(
91         :auth_path => "script::run::#{name}".intern
92       ))
93     else
94       m.reply(_("%{stuff} is not a script I know of") % p)
95     end
96   end
97
98   def handle_allow(m, p)
99     handle_allow_deny(m, p.merge(:allow => true))
100   end
101
102   def handle_deny(m, p)
103     handle_allow_deny(m, p.merge(:allow => false))
104   end
105
106
107
108   def handle_eval( m, params )
109     code = params[:code].to_s.dup.untaint
110     Thread.start {
111       $SAFE = @bot.config['script.safe']
112
113       begin
114         eval( code )
115       rescue Exception => e
116         report_error(m, code, e)
117       end
118     }
119     m.replied = true
120   end
121
122
123   def handle_echo( m, params )
124     code = params[:code].to_s.dup.untaint
125     Thread.start {
126       $SAFE = @bot.config['script.safe']
127
128       begin
129         m.reply eval( code ).to_s
130       rescue Exception => e
131         report_error(m, code, e)
132       end
133     }
134     m.replied = true
135   end
136
137
138   def handle_add( m, params, force = false )
139     name    = params[:name]
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." )
142       return
143     end
144
145     code    = params[:code].to_s
146     nick    = m.sourcenick
147     created = Time.new.strftime '%Y/%m/%d %H:%m'
148     channel = m.target.to_s
149
150     command = Command.new( code, nick, created, channel )
151     @commands[name] = command
152
153     m.okay
154   end
155
156
157   def handle_add_force( m, params )
158     handle_add( m, params, true )
159   end
160
161
162   def handle_del( m, params )
163     name = params[:name]
164     unless @commands.has_key?( name )
165       m.reply( "Script does not exist." ); return
166     end
167
168     @commands.delete( name )
169     m.okay
170   end
171
172
173   def handle_list( m, params )
174     if @commands.length == 0
175       m.reply( "No scripts available." ); return
176     end
177
178     cmds_per_page = 30
179     cmds = @commands.keys.sort
180     num_pages = cmds.length / cmds_per_page + 1
181     page = params[:page].to_i
182     page = [page, 1].max
183     page = [page, num_pages].min
184     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
185
186     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
187   end
188
189
190   def handle_show( m, params )
191     name = params[:name]
192     unless @commands.has_key?( name )
193       m.reply( "Script does not exist." ); return
194     end
195
196     cmd = @commands[name]
197     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
198  end
199
200 end
201
202
203 plugin = ScriptPlugin.new
204
205 plugin.default_auth( 'edit', false )
206 plugin.default_auth( 'eval', false )
207 plugin.default_auth( 'echo', false )
208 plugin.default_auth( 'run', true )
209
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'
217
218 plugin.map 'script allow :stuff for :user [*where]',
219   :action => 'handle_allow',
220   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
221   :auth_path => 'edit'
222 plugin.map 'script deny :stuff for :user [*where]',
223   :action => 'handle_deny',
224   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
225   :auth_path => 'edit'
226