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