1 # Plugin for the Ruby IRC bot (http://linuxbrit.co.uk/rbot/)
3 # Create mini plugins on IRC.
5 # Commands are little Ruby programs that run in the context of the command 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 CommandPlugin < Plugin
19 if @registry.has_key?(:commands)
20 @commands = @registry[:commands]
25 # Migrate old Hash to new:
26 @commands.each_pair do |name, cmd|
27 unless cmd.instance_of?( Command )
28 @commands[name] = Command.new( cmd, 'unknown hacker', 'somedate', '#somechan' )
35 @registry[:commands] = @commands
39 def help( plugin, topic="" )
41 "Commands are little Ruby programs that run in the context of the command 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: 'command add greet m.reply( 'Hello ' + user )'. Invoke the command just like a plugin: '<botnick>: greet'."
43 "Create mini plugins on IRC. 'command add <name> <code>' => Create command named <name> with the code <source>. 'command list' => Show a list of all known commands. 'command show <name>' => Show the source code for <name>. 'command del <name>' => Delete command <name>."
49 name = m.message.split.first
51 if m.address? and @commands.has_key?( name )
52 code = @commands[name].code.dup.untaint
54 # Convenience variables, can be accessed by commands:
55 args = m.message.split
57 user = args.empty? ? m.sourcenick : args.first
65 m.reply( "Command '#{name}' crapped out :(" )
73 def handle_add( m, params, force = false )
75 if !force and @commands.has_key?( name )
76 m.reply( "#{m.sourcenick}: #{name} already exists. Use 'add -f' if you really want to overwrite it." )
80 code = params[:code].join( " " )
82 created = Time.new.strftime '%Y/%m/%d %H:%m'
85 command = Command.new( code, nick, created, channel )
86 @commands[name] = command
92 def handle_add_force( m, params )
93 handle_add( m, params, true )
97 def handle_del( m, params )
99 unless @commands.has_key?( name )
100 m.reply( "Command does not exist." ); return
103 @commands.delete( name )
108 def handle_list( m, params )
109 if @commands.length == 0
110 m.reply( "No commands available." ); return
114 cmds = @commands.keys.sort
115 num_pages = cmds.length / cmds_per_page
116 page = params[:page].to_i
118 page = [page, num_pages].min
119 str = cmds[(page-1)*cmds_per_page, cmds_per_page].join(', ')
121 m.reply "Available commands (page #{page}/#{num_pages}): #{str}"
125 def handle_show( m, params )
127 unless @commands.has_key?( name )
128 m.reply( "Command does not exist." ); return
131 cmd = @commands[name]
132 m.reply( "#{cmd.code} [#{cmd.nick}, #{cmd.created} in #{cmd.channel}]" )
138 plugin = CommandPlugin.new
139 plugin.register( "command" )
141 plugin.map 'command add -f :name *code', :action => 'handle_add_force', :auth => 'commandedit'
142 plugin.map 'command add :name *code', :action => 'handle_add', :auth => 'commandedit'
143 plugin.map 'command del :name', :action => 'handle_del', :auth => 'commandedit'
144 plugin.map 'command list :page', :action => 'handle_list', :defaults => { :page => '1' }
145 plugin.map 'command show :name', :action => 'handle_show'