]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/script.rb
script plugin: per-script permissions
[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 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     if topic == "add"
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'."
39     else  
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>."
41     end
42   end
43
44   def report_error(m, name, e)
45     # ed = e.backtrace.unshift(e.inspect).join(' ')
46     ed = e.inspect
47     m.reply( "Script '#{name}' crapped out :( #{ed}" )
48   end
49
50
51   def message( m )
52     name = m.message.split.first
53
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)
57
58       code = @commands[name].code.dup.untaint
59
60       # Convenience variables, can be accessed by scripts:
61       args = m.message.split
62       args.delete_at( 0 ) 
63       user = args.empty? ? m.sourcenick : args.first  
64
65       Thread.start {
66         # TODO allow different safe levels for different botusers
67         $SAFE = 3
68
69         begin
70           eval( code )
71         rescue Exception => e
72           report_error(m, name, e)
73         end
74       }
75       m.replied = true
76     end
77   end
78
79
80   def handle_eval( m, params )
81     code = params[:code].to_s.dup.untaint
82     Thread.start {
83       # TODO allow different safe levels for different botusers
84       begin
85         eval( code )
86       rescue Exception => e
87         report_error(m, code, e)
88       end
89     }
90     m.replied = true
91   end
92
93
94   def handle_echo( m, params )
95     code = params[:code].to_s.dup.untaint
96     Thread.start {
97       # TODO allow different safe levels for different botusers
98       begin
99         m.reply eval( code ).to_s
100       rescue Exception => e
101         report_error(m, code, e)
102       end
103     }
104     m.replied = true
105   end
106
107
108   def handle_add( m, params, force = false )
109     name    = params[:name]
110     if !force and @commands.has_key?( name )
111       m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
112       return
113     end
114
115     code    = params[:code].to_s
116     nick    = m.sourcenick
117     created = Time.new.strftime '%Y/%m/%d %H:%m'
118     channel = m.target.to_s
119
120     command = Command.new( code, nick, created, channel )
121     @commands[name] = command
122
123     m.okay
124   end
125
126
127   def handle_add_force( m, params )
128     handle_add( m, params, true )
129   end
130     
131
132   def handle_del( m, params )
133     name = params[:name]
134     unless @commands.has_key?( name )
135       m.reply( "Script does not exist." ); return
136     end
137
138     @commands.delete( name )
139     m.okay
140   end
141
142
143   def handle_list( m, params )
144     if @commands.length == 0
145       m.reply( "No scripts available." ); return
146     end
147
148     cmds_per_page = 30
149     cmds = @commands.keys.sort
150     num_pages = cmds.length / cmds_per_page + 1
151     page = params[:page].to_i
152     page = [page, 1].max
153     page = [page, num_pages].min
154     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') 
155
156     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" 
157   end
158
159
160   def handle_show( m, params )
161     name = params[:name]
162     unless @commands.has_key?( name )
163       m.reply( "Script does not exist." ); return
164     end
165
166     cmd = @commands[name]
167     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
168  end
169
170 end
171
172
173 plugin = ScriptPlugin.new
174
175 plugin.default_auth( 'edit', false )
176 plugin.default_auth( 'eval', false )
177 plugin.default_auth( 'echo', false )
178 plugin.default_auth( 'run', true )
179
180 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
181 plugin.map 'script add :name *code',    :action => 'handle_add',       :auth_path => 'edit'
182 plugin.map 'script del :name',          :action => 'handle_del',       :auth_path => 'edit'
183 plugin.map 'script eval *code',         :action => 'handle_eval'
184 plugin.map 'script echo *code',         :action => 'handle_echo'
185 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
186 plugin.map 'script show :name',         :action => 'handle_show'
187
188