<commit_before>class Scamp
  class Matcher
    attr_accessor :conditions, :trigger, :action, :bot, :required_prefix
    
    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 required_prefix
        message_text = handle_prefix(message_text)
        return false if message_text == false
      end
      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 handle_prefix(message_text)
      bot.logger.warn "TEXT: #{message_text}"
      bot.logger.warn "PREFIX: #{required_prefix}".inspect
      
      if required_prefix.is_a? String
        bot.logger.warn "GSUBD: "+ message_text[0...required_prefix.length]
        if required_prefix == message_text[0...required_prefix.length]
          message_text.gsub(required_prefix,'') 
        else
          false
        end
      elsif required_prefix.is_a? Regexp
        if required_prefix.match message_text
          bot.logger.warn "GSUBD"+message_text.gsub(required_prefix,'')
          message_text.gsub(required_prefix,'')
        else
          false
        end
      else
        false
      end
    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}"<commit_msg>Clean up the prefix code a bit<commit_after>class Scamp
  class Matcher
    attr_accessor :conditions, :trigger, :action, :bot, :required_prefix
    
    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 required_prefix 
        message_text = handle_prefix(message_text)
        return false if message_text == false
      end
      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 handle_prefix(message_text)
      if required_prefix.is_a? String
        if required_prefix == message_text[0...required_prefix.length]
          message_text.gsub(required_prefix,'') 
        else
          false
        end
      elsif required_prefix.is_a? Regexp
        if required_prefix.match message_text
          message_text.gsub(required_prefix,'')
        else
          false
        end
      else
        false
      end
    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}"
{'fraction_non_alphanumeric': 0.08558091286307054, 'fraction_numerical': 0.0007780082987551867, 'mean_word_length': 2.05868358445678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7017412', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1144, 'n_words': 282}