<commit_before>#Copyright (c) 2008-12 <NAME> of 9thBit LLC
#Released under the MIT license

module SanitizeEmail
  class Bleach

    class MissingTo < StandardError; end
    class UnknownOverride < StandardError; end

    # Can override global configs at the instance level.
    attr_accessor :engage, # Turn sanitization on or off just for this instance
      :sanitized_to, :sanitized_cc, :sanitized_bcc, # Replace non-white-listed addresses with these sanitized addresses.
      :good_list, # White-listed addresses will not be molested as to, cc, or bcc
      :bad_list, # Black-listed addresses will be removed from to, cc and bcc when sanitization is engaged
      :injected  # Track whether or not the subject has been injected with usernames

    def initialize(args = {})
      # Not using extract_options! because non-rails compatibility is a goal
      @sanitized_to = args[:sanitized_to]    || SanitizeEmail[:sanitized_to]
      @sanitized_cc = args[:sanitized_cc]    || SanitizeEmail[:sanitized_cc]
      @sanitized_bcc = args[:sanitized_bcc]  || SanitizeEmail[:sanitized_bcc]
      @good_list = args[:good_list]          || SanitizeEmail[:good_list]     || []
      @bad_list = args[:bad_list]            || SanitizeEmail[:bad_list]      || []
      @engage = args[:engage]                || SanitizeEmail[:engage]
      @injected = false
    end

    # If all recipient addresses are white-listed the field is left alone.
    def delivering_email(message)
      if self.sanitize_engaged?
        # Add headers by string concat. Setting hash values on message.headers does nothing, strangely. http://goo.gl/v46GY
        headers = {
            'X-Sanitize-Email-To' => message.to,
            'X-Sanitize-Email-Cc' => message.cc,
            'X-Sanitize-Email-Bcc' => message.bcc
        }.each { |k,v|
          # For each type of address line
          v.each { |a|
            # For each address
            message.header = message.header.to_s + "\n#{k}: #{a}"
          } if v
        }
        message.subject = self.subject_override(message.subject, message.to) if SanitizeEmail.use_actual_email_prepended_to_subject
        message.to = self.to_override(message.to)
        message.cc = self.cc_override(message.cc)
        message.bcc = self.bcc_override(message.bcc)
      end
    end

    def activate?
      SanitizeEmail.activation_proc.call if SanitizeEmail.activation_proc.respond_to?(:call)
    end

    # This method will be called by the Hook to determine if an override should occur
    # There are three ways SanitizeEmail can be turned on; in order of precedence they are:
    #
    # 1. SanitizeEmail.force_sanitize = true # by default it is nil
    # 2. Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default it is nil
    # 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } be default it is false
    #
    # Note: Number 1 is the method used by the SanitizeEmail.sanitary block
    # Note: Number 2 would not be used unless you setup your own register_interceptor)
    # If installed but not configured, sanitize email DOES NOTHING.  Until configured the defaults leave it turned off.
    def sanitize_engaged?

      # Has it been forced via the force_sanitize mattr?
      forced = SanitizeEmail.force_sanitize
      return forced unless forced.nil?

      # Is this particular instance of Bleach engaged
      engaged = self.engage
      return engaged unless engaged.nil?

      # Should we sanitize due to the activation_proc?
      return self.activate?

    end

    def subject_override(real_subject, actual_addresses)
      if !actual_addresses.respond_to?(:join)
        real_subject
      else
        "(#{actual_addresses.join(',').gsub(/@/,' at ').gsub(/[<>]/,'~')}) #{real_subject}"
      end
    end

    def to_override(actual_addresses)
      to = override_email(:to, actual_addresses)
      raise MissingTo, 'after overriding :to there are no addresses to send in To: header.' if to.empty?
      to.join(',')
    end

    def cc_override(actual_addresses)
      override_email(:cc, actual_addresses).join(',')
    end

    def bcc_override(actual_addresses)
      override_email(:bcc, actual_addresses).join(',')
    end

    #######<commit_msg>Adding message to engage proc, so we can sanitize on a message by message basis.<commit_after>#Copyright (c) 2008-12 <NAME> of 9thBit LLC
#Released under the MIT license

module SanitizeEmail
  class Bleach

    class MissingTo < StandardError; end
    class UnknownOverride < StandardError; end

    # Can override global configs at the instance level.
    attr_accessor :engage, # Turn sanitization on or off just for this instance
      :sanitized_to, :sanitized_cc, :sanitized_bcc, # Replace non-white-listed addresses with these sanitized addresses.
      :good_list, # White-listed addresses will not be molested as to, cc, or bcc
      :bad_list, # Black-listed addresses will be removed from to, cc and bcc when sanitization is engaged
      :injected  # Track whether or not the subject has been injected with usernames

    def initialize(args = {})
      # Not using extract_options! because non-rails compatibility is a goal
      @sanitized_to = args[:sanitized_to]    || SanitizeEmail[:sanitized_to]
      @sanitized_cc = args[:sanitized_cc]    || SanitizeEmail[:sanitized_cc]
      @sanitized_bcc = args[:sanitized_bcc]  || SanitizeEmail[:sanitized_bcc]
      @good_list = args[:good_list]          || SanitizeEmail[:good_list]     || []
      @bad_list = args[:bad_list]            || SanitizeEmail[:bad_list]      || []
      @engage = args[:engage]                || SanitizeEmail[:engage]
      @injected = false
    end

    # If all recipient addresses are white-listed the field is left alone.
    def delivering_email(message)
      if self.sanitize_engaged?(message)
        # Add headers by string concat. Setting hash values on message.headers does nothing, strangely. http://goo.gl/v46GY
        headers = {
            'X-Sanitize-Email-To' => message.to,
            'X-Sanitize-Email-Cc' => message.cc,
            'X-Sanitize-Email-Bcc' => message.bcc
        }.each { |k,v|
          # For each type of address line
          v.each { |a|
            # For each address
            message.header = message.header.to_s + "\n#{k}: #{a}"
          } if v
        }
        message.subject = self.subject_override(message.subject, message.to) if SanitizeEmail.use_actual_email_prepended_to_subject
        message.to = self.to_override(message.to)
        message.cc = self.cc_override(message.cc)
        message.bcc = self.bcc_override(message.bcc)
      end
    end

    def activate?(message)
      SanitizeEmail.activation_proc.call(message) if SanitizeEmail.activation_proc.respond_to?(:call)
    end

    # This method will be called by the Hook to determine if an override should occur
    # There are three ways SanitizeEmail can be turned on; in order of precedence they are:
    #
    # 1. SanitizeEmail.force_sanitize = true # by default it is nil
    # 2. Mail.register_interceptor(SanitizeEmail::Bleach.new(:engage => true)) # by default it is nil
    # 3. SanitizeEmail::Config.configure {|config| config[:activation_proc] = Proc.new { true } } be default it is false
    #
    # Note: Number 1 is the method used by the SanitizeEmail.sanitary block
    # Note: Number 2 would not be used unless you setup your own register_interceptor)
    # If installed but not configured, sanitize email DOES NOTHING.  Until configured the defaults leave it turned off.
    def sanitize_engaged?(message)

      # Has it been forced via the force_sanitize mattr?
      forced = SanitizeEmail.force_sanitize
      return forced unless forced.nil?

      # Is this particular instance of Bleach engaged
      engaged = self.engage
      return engaged unless engaged.nil?

      # Should we sanitize due to the activation_proc?
      return self.activate?(message)

    end

    def subject_override(real_subject, actual_addresses)
      if !actual_addresses.respond_to?(:join)
        real_subject
      else
        "(#{actual_addresses.join(',').gsub(/@/,' at ').gsub(/[<>]/,'~')}) #{real_subject}"
      end
    end

    def to_override(actual_addresses)
      to = override_email(:to, actual_addresses)
      raise MissingTo, 'after overriding :to there are no addresses to send in To: header.' if to.empty?
      to.join(',')
    end

    def cc_override(actual_addresses)
      override_email(:cc, actual_addresses).join(',')
    end

    def bcc_override(actual_addresses)
      override_email(:bcc, actual_addresses).join(',')
    end

    #######
{'fraction_non_alphanumeric': 0.09286209286209286, 'fraction_numerical': 0.003234003234003234, 'mean_word_length': 2.943078324225865, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2857689', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2573, 'n_words': 776}