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