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