Sha256: 600ee1cfdef0a8ba2aa7b1c5afb6dd4da556e3584ddf5248b17562a8754584b1

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 KB

Contents

module Marvin
  
  # A Simple Marvin handler based on processing
  # commands, similar in design to MatzBot.
  class CommandHandler < Base
    
    class_inheritable_accessor :exposed_methods, :command_prefix
    
    self.command_prefix  = ""
    self.exposed_methods = []
    
    class << self
      
      def exposes(*args)
        self.exposed_methods ||= []
        self.exposed_methods += args.map { |a| a.to_sym }.flatten
      end
      
    end
    
    on_event :incoming_message do
      logger.debug "Incoming message"
      check_for_commands
    end
    
    def check_for_commands
      data, command = nil, nil
      if self.from_channel?
        logger.debug "Processing command in channel"
        split_message = options.message.split(" ", 3)
        prefix = split_message.shift
        # Return if in channel and it isn't address to the user.
        return unless prefix == "#{self.client.nickname}:"
        command, data = split_message # Set remaining.
      else
        command, data = options.message.split(" ", 2)
      end
      # Double check for sanity
      return if command.blank?
      command_name = extract_command_name(command)
      unless command_name.nil?
        logger.debug "Command Exists - processing"
        # Dispatch the command.
        self.send(command_name, data.to_a) if self.respond_to?(command_name)
      end
    end
    
    def extract_command_name(command)
      prefix_length = self.command_prefix.to_s.length
      has_prefix = command[0...prefix_length] == self.command_prefix.to_s
      logger.debug "Debugging, prefix is #{prefix_length} characters, has prefix? = #{has_prefix}"
      if has_prefix
        # Normalize the method name
        method_name = command[prefix_length..-1].to_s.underscore.to_sym
        logger.debug "Computed method name is #{method_name.inspect}"
        return method_name if self.exposed_methods.to_a.include?(method_name)
      end
    end
    
  end
  
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
Sutto-marvin-0.1.0.20081014 lib/marvin/command_handler.rb
Sutto-marvin-0.1.0.20081016 lib/marvin/command_handler.rb
Sutto-marvin-0.1.20081115 lib/marvin/command_handler.rb
Sutto-marvin-0.1.20081120 lib/marvin/command_handler.rb
Sutto-marvin-0.2.0 lib/marvin/command_handler.rb
jeffrafter-marvin-0.1.20081115 lib/marvin/command_handler.rb
jeffrafter-marvin-0.1.20081120 lib/marvin/command_handler.rb