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