script echo needs a to_s
[rbot] / data / rbot / plugins / script.rb
1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
2 #
3 # Create mini plugins on IRC.
4 #
5 # Scripts are little Ruby programs that run in the context of the script plugin. You 
6 # can create them directly in an IRC channel, and invoke them just like normal rbot plugins. 
7 #
8 # (c) 2006 Mark Kretschmann <markey@web.de>
9 # Licensed under GPL V2.
10
11
12 Command = Struct.new( "Command", :code, :nick, :created, :channel )
13
14
15 class ScriptPlugin < Plugin
16
17   def initialize
18     super
19     if @registry.has_key?(:commands)
20       @commands = @registry[:commands]
21     end
22     
23     if @commands.nil?
24       @commands = Hash.new
25     end
26
27     # Migrate old Hash to new:
28     @commands.each_pair do |name, cmd|
29       unless cmd.instance_of?( Command )
30         @commands[name] = Command.new( cmd, 'unknown hacker', 'somedate', '#somechan' )
31       end
32     end
33   end
34
35
36   def save
37     @registry[:commands] = @commands
38   end
39
40
41   def help( plugin, topic="" )
42     if topic == "add"
43       "Scripts are little Ruby programs that run in the context of the script plugin. You can access @bot (class IrcBot), m (class 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     else  
45       "Create mini plugins on IRC. 'script add <name> <code>' => Create script named <name> with the code <source>. '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>."
46     end
47   end
48
49
50   def listen( m )
51     name = m.message.split.first
52
53     if m.address? and @commands.has_key?( name )
54       code = @commands[name].code.dup.untaint
55
56       # Convenience variables, can be accessed by scripts:
57       args = m.message.split
58       args.delete_at( 0 ) 
59       user = args.empty? ? m.sourcenick : args.first  
60
61       Thread.start {
62         # TODO allow different safe levels for different botusers
63         $SAFE = 3
64
65         begin
66           eval( code )
67         rescue => e
68           m.reply( "Script '#{name}' crapped out :(" )
69           m.reply( e.inspect )
70         end
71       }
72     end
73   end
74
75
76   def handle_eval( m, params )
77     code = params[:code].to_s.dup.untaint
78       Thread.start {
79         # TODO allow different safe levels for different botusers
80         begin
81           eval( code )
82         rescue => e
83           m.reply( "Script '#{name}' crapped out :(" )
84           m.reply( e.inspect )
85         end
86       }
87   end
88
89
90   def handle_echo( m, params )
91     code = params[:code].to_s.dup.untaint
92       Thread.start {
93         # TODO allow different safe levels for different botusers
94         begin
95           m.reply eval( code ).to_s
96         rescue => e
97           m.reply( "Script '#{name}' crapped out :(" )
98           m.reply( e.inspect )
99         end
100       }
101   end
102
103
104   def handle_add( m, params, force = false )
105     name    = params[:name]
106     if !force and @commands.has_key?( name )
107       m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
108       return
109     end
110
111     code    = params[:code].to_s
112     nick    = m.sourcenick
113     created = Time.new.strftime '%Y/%m/%d %H:%m'
114     channel = m.target
115
116     command = Command.new( code, nick, created, channel )
117     @commands[name] = command
118
119     m.reply( "done" )
120   end
121
122
123   def handle_add_force( m, params )
124     handle_add( m, params, true )
125   end
126     
127
128   def handle_del( m, params )
129     name = params[:name]
130     unless @commands.has_key?( name )
131       m.reply( "Script does not exist." ); return
132     end
133
134     @commands.delete( name )
135     m.reply( "done" )
136   end
137
138
139   def handle_list( m, params )
140     if @commands.length == 0
141       m.reply( "No scripts available." ); return
142     end
143
144     cmds_per_page = 30
145     cmds = @commands.keys.sort
146     num_pages = cmds.length / cmds_per_page + 1
147     page = params[:page].to_i
148     page = [page, 1].max
149     page = [page, num_pages].min
150     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') 
151
152     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" 
153   end
154
155
156   def handle_show( m, params )
157     name = params[:name]
158     unless @commands.has_key?( name )
159       m.reply( "Script does not exist." ); return
160     end
161
162     cmd = @commands[name]
163     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
164  end
165
166 end
167
168
169 plugin = ScriptPlugin.new
170 plugin.register( "script" )
171 plugin.default_auth( 'edit', false )
172 plugin.default_auth( 'eval', false )
173
174 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
175 plugin.map 'script add :name *code',    :action => 'handle_add',       :auth_path => 'edit'
176 plugin.map 'script del :name',          :action => 'handle_del',       :auth_path => 'edit'
177 plugin.map 'script eval *code',         :action => 'handle_eval'
178 plugin.map 'script echo *code',         :action => 'handle_echo'
179 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
180 plugin.map 'script show :name',         :action => 'handle_show'
181
182