<commit_before># frozen_string_literal: true
require "email_assessor"
require "resolv"
require "mail"

module EmailAssessor
  class Address
    attr_accessor :address

    PROHIBITED_DOMAIN_CHARACTERS_REGEX = %r{[+!_\/\s']}

    def initialize(address)
      @parse_error = false
      @raw_address = address

      begin
        @address = Mail::Address.new(address)
      rescue Mail::Field::ParseError
        @parse_error = true
      end
    end

    def valid?
      return @valid unless @valid.nil?
      return false if @parse_error

      @valid =
        if address.domain && address.address == @raw_address
          domain = address.domain

          !domain.match?(PROHIBITED_DOMAIN_CHARACTERS_REGEX) &&
            domain.include?('.') &&
            !domain.include?('..') &&
            !domain.start_with?('.') &&
            !domain.start_with?('-') &&
            !domain.include?('-.') &&
            !address.local.include?('..') &&
            !address.local.end_with?('.') &&
            !address.local.start_with?('.')
        else
          false
        end
    end

    def disposable?
      valid? && EmailAssessor.domain_is_disposable?(address.domain)
    end

    def blacklisted?
      valid? && EmailAssessor.domain_is_blacklisted?(address.domain)
    end

    def valid_mx?
      valid? && mx_servers.any?
    end

    def mx_server_is_in?(domain_list_file)<commit_msg>Redesign to use regex for prohibited character checking<commit_after># frozen_string_literal: true
require "email_assessor"
require "resolv"
require "mail"

module EmailAssessor
  class Address
    attr_accessor :address

    PROHIBITED_DOMAIN_PREFIXES = [
      '.',
      '-',
    ].freeze

    PROHIBITED_DOMAIN_CONTENT = [
      '+',
      '!',
      '_',
      '/',
      ' ',
      '..',
      '-.',
      "'",
    ].freeze

    PROHIBITED_DOMAIN_SUFFIXES = [
      # none
    ].freeze

    PROHIBITED_LOCAL_PREFIXES = [
      '.',
    ].freeze

    PROHIBITED_LOCAL_CONTENT = [
      '..',
    ].freeze

    PROHIBITED_LOCAL_SUFFIXES = [
      '.',
    ].freeze

    class << self
      def prohibited_domain_regex
        @prohibited_domain_content_regex ||= make_regex(
          prefixes: PROHIBITED_DOMAIN_PREFIXES,
          content: PROHIBITED_DOMAIN_CONTENT,
          suffixes: PROHIBITED_DOMAIN_SUFFIXES
        )
      end

      def prohibited_local_regex
        @prohibited_local_content_regex ||= make_regex(
          prefixes: PROHIBITED_LOCAL_PREFIXES,
          content: PROHIBITED_LOCAL_CONTENT,
          suffixes: PROHIBITED_LOCAL_SUFFIXES
        )
      end

      private

      def make_regex(prefixes: nil, content: nil, suffixes: nil)
        parts = []

        unless prefixes.nil?
          prefixes.each do |prefix|
            parts << "\\A#{Regexp.escape(prefix)}"
          end
        end

        unless content.nil?
          content.each do |prefix|
            parts << Regexp.escape(prefix)
          end
        end

        unless suffixes.nil?
          suffixes.each do |prefix|
            parts << "#{Regexp.escape(prefix)}\\z"
          end
        end

        Regexp.new(parts.join("|"), Regexp::IGNORECASE)
      end
    end

    def initialize(address)
      @parse_error = false
      @raw_address = address

      begin
        @address = Mail::Address.new(address)
      rescue Mail::Field::ParseError
        @parse_error = true
      end
    end

    def valid?
      return @valid unless @valid.nil?
      return false if @parse_error

      @valid =
        if address.domain && address.address == @raw_address
          domain = address.domain

          domain.include?('.') &&
            !domain.match?(self.class.prohibited_domain_regex) &&
            !address.local.match?(self.class.prohibited_local_regex)
        else
          false
        end
    end

    def disposable?
      valid? && EmailAssessor.domain_is_disposable?(address.domain)
    end

    def blacklisted?
      valid? && EmailAssessor.domain_is_blacklisted?(address.domain)
    end

    def valid_mx?
      valid? && mx_servers.any?
    end

    def mx_server_is_in?(domain_list_file)
{'fraction_non_alphanumeric': 0.10801646887866312, 'fraction_numerical': 0.0, 'mean_word_length': 2.047970479704797, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4486167', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1292, 'n_words': 233}