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