1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
3 # Create mini plugins on IRC.
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.
8 # (c) 2006 Mark Kretschmann <markey@web.de>
9 # Licensed under GPL V2.
12 Command = Struct.new( "Command", :code, :nick, :created, :channel )
15 class ScriptPlugin < Plugin
19 if @registry.has_key?(:commands)
20 @commands = @registry[:commands]
27 # Migrate old Hash to new:
28 @commands.each_pair do |name, cmd|
29 unless cmd.instance_of?( Command )
30 @commands[name] = Command.new( cmd, 'unknown hacker', 'somedate', '#somechan' )
37 @registry[:commands] = @commands
41 def help( plugin, topic="" )
43 "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'."
45 "Create mini plugins on IRC. 'script add <name> <code>' => Create script named <name> with the code <source>. '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>."
51 name = m.message.split.first
53 if m.address? and @commands.has_key?( name )
54 code = @commands[name].code.dup.untaint
56 # Convenience variables, can be accessed by scripts:
57 args = m.message.split
59 user = args.empty? ? m.sourcenick : args.first
62 # TODO allow different safe levels for different botusers
68 m.reply( "Script '#{name}' crapped out :(" )
76 def handle_eval( m, params )
77 code = params[:code].to_s.dup.untaint
79 # TODO allow different safe levels for different botusers
83 m.reply( "Script '#{name}' crapped out :(" )
90 def handle_add( m, params, force = false )
92 if !force and @commands.has_key?( name )
93 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
97 code = params[:code].to_s
99 created = Time.new.strftime '%Y/%m/%d %H:%m'
102 command = Command.new( code, nick, created, channel )
103 @commands[name] = command
109 def handle_add_force( m, params )
110 handle_add( m, params, true )
114 def handle_del( m, params )
116 unless @commands.has_key?( name )
117 m.reply( "Script does not exist." ); return
120 @commands.delete( name )
125 def handle_list( m, params )
126 if @commands.length == 0
127 m.reply( "No scripts available." ); return
131 cmds = @commands.keys.sort
132 num_pages = cmds.length / cmds_per_page + 1
133 page = params[:page].to_i
135 page = [page, num_pages].min
136 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
138 m.reply "Available scripts (page #{page}/#{num_pages}): #{str}"
142 def handle_show( m, params )
144 unless @commands.has_key?( name )
145 m.reply( "Script does not exist." ); return
148 cmd = @commands[name]
149 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
155 plugin = ScriptPlugin.new
156 plugin.register( "script" )
157 plugin.default_auth( 'edit', false )
158 plugin.default_auth( 'eval', false )
160 plugin.map 'script add -f :name *code', :action => 'handle_add_force', :auth_path => 'edit'
161 plugin.map 'script add :name *code', :action => 'handle_add', :auth_path => 'edit'
162 plugin.map 'script del :name', :action => 'handle_del', :auth_path => 'edit'
163 plugin.map 'script eval *code', :action => 'handle_eval'
164 plugin.map 'script list :page', :action => 'handle_list', :defaults => { :page => '1' }
165 plugin.map 'script show :name', :action => 'handle_show'