]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/script.rb
plugin(script): remove deprecated $SAFE
[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
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         begin
72           eval( code )
73         rescue Exception => e
74           report_error(m, name, e)
75         end
76       }
77       m.replied = true
78     end
79   end
80
81   def handle_allow_deny(m, p)
82     name = p[:stuff]
83     if @commands.has_key?( name )
84       @bot.plugins['auth'].auth_allow_deny(m, p.merge(
85         :auth_path => "script::run::#{name}".intern
86       ))
87     else
88       m.reply(_("%{stuff} is not a script I know of") % p)
89     end
90   end
91
92   def handle_allow(m, p)
93     handle_allow_deny(m, p.merge(:allow => true))
94   end
95
96   def handle_deny(m, p)
97     handle_allow_deny(m, p.merge(:allow => false))
98   end
99
100
101
102   def handle_eval( m, params )
103     code = params[:code].to_s.dup
104     Thread.start {
105       begin
106         eval( code )
107       rescue Exception => e
108         report_error(m, code, e)
109       end
110     }
111     m.replied = true
112   end
113
114
115   def handle_echo( m, params )
116     code = params[:code].to_s.dup
117     Thread.start {
118       begin
119         m.reply eval( code ).to_s
120       rescue Exception => e
121         report_error(m, code, e)
122       end
123     }
124     m.replied = true
125   end
126
127
128   def handle_add( m, params, force = false )
129     name    = params[:name]
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." )
132       return
133     end
134
135     code    = params[:code].to_s
136     nick    = m.sourcenick
137     created = Time.new.strftime '%Y/%m/%d %H:%m'
138     channel = m.target.to_s
139
140     command = Command.new( code, nick, created, channel )
141     @commands[name] = command
142
143     m.okay
144   end
145
146
147   def handle_add_force( m, params )
148     handle_add( m, params, true )
149   end
150
151
152   def handle_del( m, params )
153     name = params[:name]
154     unless @commands.has_key?( name )
155       m.reply( "Script does not exist." ); return
156     end
157
158     @commands.delete( name )
159     m.okay
160   end
161
162
163   def handle_list( m, params )
164     if @commands.length == 0
165       m.reply( "No scripts available." ); return
166     end
167
168     cmds_per_page = 30
169     cmds = @commands.keys.sort
170     num_pages = cmds.length / cmds_per_page + 1
171     page = params[:page].to_i
172     page = [page, 1].max
173     page = [page, num_pages].min
174     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
175
176     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
177   end
178
179
180   def handle_show( m, params )
181     name = params[:name]
182     unless @commands.has_key?( name )
183       m.reply( "Script does not exist." ); return
184     end
185
186     cmd = @commands[name]
187     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
188  end
189
190 end
191
192
193 plugin = ScriptPlugin.new
194
195 plugin.default_auth( 'edit', false )
196 plugin.default_auth( 'eval', false )
197 plugin.default_auth( 'echo', false )
198 plugin.default_auth( 'show', false )
199 plugin.default_auth( 'run', true )
200
201 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
202 plugin.map 'script add :name *code',    :action => 'handle_add',       :auth_path => 'edit'
203 plugin.map 'script del :name',          :action => 'handle_del',       :auth_path => 'edit'
204 plugin.map 'script eval *code',         :action => 'handle_eval'
205 plugin.map 'script echo *code',         :action => 'handle_echo'
206 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
207 plugin.map 'script show :name',         :action => 'handle_show'
208
209 plugin.map 'script allow :stuff for :user [*where]',
210   :action => 'handle_allow',
211   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
212   :auth_path => 'edit'
213 plugin.map 'script deny :stuff for :user [*where]',
214   :action => 'handle_deny',
215   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
216   :auth_path => 'edit'
217