]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/script.rb
script plugin: reindent
[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>."
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   end
83
84
85   def handle_echo( m, params )
86     code = params[:code].to_s.dup.untaint
87     Thread.start {
88       # TODO allow different safe levels for different botusers
89       begin
90         m.reply eval( code ).to_s
91       rescue Exception => e
92         m.reply( "Script '#{name}' crapped out :(" )
93         m.reply( e.inspect )
94       end
95     }
96   end
97
98
99   def handle_add( m, params, force = false )
100     name    = params[:name]
101     if !force and @commands.has_key?( name )
102       m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
103       return
104     end
105
106     code    = params[:code].to_s
107     nick    = m.sourcenick
108     created = Time.new.strftime '%Y/%m/%d %H:%m'
109     channel = m.target
110
111     command = Command.new( code, nick, created, channel )
112     @commands[name] = command
113
114     m.reply( "done" )
115   end
116
117
118   def handle_add_force( m, params )
119     handle_add( m, params, true )
120   end
121     
122
123   def handle_del( m, params )
124     name = params[:name]
125     unless @commands.has_key?( name )
126       m.reply( "Script does not exist." ); return
127     end
128
129     @commands.delete( name )
130     m.reply( "done" )
131   end
132
133
134   def handle_list( m, params )
135     if @commands.length == 0
136       m.reply( "No scripts available." ); return
137     end
138
139     cmds_per_page = 30
140     cmds = @commands.keys.sort
141     num_pages = cmds.length / cmds_per_page + 1
142     page = params[:page].to_i
143     page = [page, 1].max
144     page = [page, num_pages].min
145     str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ') 
146
147     m.reply "Available scripts (page #{page}/#{num_pages}): #{str}" 
148   end
149
150
151   def handle_show( m, params )
152     name = params[:name]
153     unless @commands.has_key?( name )
154       m.reply( "Script does not exist." ); return
155     end
156
157     cmd = @commands[name]
158     m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
159  end
160
161 end
162
163
164 plugin = ScriptPlugin.new
165
166 plugin.default_auth( 'edit', false )
167 plugin.default_auth( 'eval', false )
168 plugin.default_auth( 'echo', false )
169
170 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
171 plugin.map 'script add :name *code',    :action => 'handle_add',       :auth_path => 'edit'
172 plugin.map 'script del :name',          :action => 'handle_del',       :auth_path => 'edit'
173 plugin.map 'script eval *code',         :action => 'handle_eval'
174 plugin.map 'script echo *code',         :action => 'handle_echo'
175 plugin.map 'script list :page',         :action => 'handle_list',      :defaults => { :page => '1' }
176 plugin.map 'script show :name',         :action => 'handle_show'
177
178