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