Sha256: 799393699dc6f5406e3e3e4060923e39209e457a45e14a4992891b459c869ee8

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

class Scamp
  class Matcher
    attr_accessor :conditions, :trigger, :action, :bot
    
    def initialize(bot, params = {})
      params ||= {}
      params[:conditions] ||= {}
      params.each { |k,v| send("#{k}=", v) }
      @bot = bot
    end
    
    def attempt(msg)
      return false unless conditions_satisfied_by(msg)
      match = triggered_by(msg[:body])
      if match
        if match.is_a? MatchData
          run(msg, match)
        else
          run(msg)
        end
        return true
      end
      false
    end
    
    private
    
    def triggered_by(message_text)
      if trigger.is_a? String
        return true if trigger == message_text
      elsif trigger.is_a? Regexp
        return trigger.match message_text
      else
        bot.logger.warn "Don't know what to do with #{trigger.inspect} at #{__FILE__}:#{__LINE__}"
      end
      false
    end
    
    def run(msg, match = nil)
      action_run = Action.new(bot, action, msg)
      action_run.matches = match if match
      action_run.run
    end
    
    def conditions_satisfied_by(msg)
      bot.logger.debug "Checking message against #{conditions.inspect}"
      
      # item will be :nick or :channel
      # cond is the int or string value.
      conditions.each do |item, cond|
        bot.logger.debug "Checking #{item} against #{cond}"
        bot.logger.debug "msg is #{msg.inspect}"
        if cond.is_a? Integer
          # bot.logger.debug "item is #{msg[{:channel => :room_id, :user => :user_id}[item]]}"
          return false unless msg[{:channel => :room_id, :user => :user_id}[item]] == cond
        elsif cond.is_a? String
          case item
          when :channel
            return false unless bot.channel_name_for(msg[:room_id]) == cond
          when :user
            return false unless bot.username_for(msg[:user_id]) == cond
          end
          bot.logger.error "Don't know how to deal with a match item of #{item}, cond #{cond}"
        end
      end
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scamp-0.0.4 lib/scamp/matcher.rb