<commit_before>class EmailValidator < ActiveModel::EachValidator

  LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
  LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
  LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
  Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+[^_])|(\w+\.[^_]))*([a-z0-9-]{1,63})\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE, 'n')

  def validates_email_format(email)
    # local part max is 64 chars, domain part max is 255 chars
    # TODO: should this decode escaped entities before counting?
    begin
      local, domain = email.split('@', 2)
    rescue
      return false
    end

    email =~ Regex and not email =~ /\.\./ and domain.length <= 255 and local.length <= 64
  end

  def validates_email_domain(email, options)
    require 'resolv'
    a_fallback = options.is_a?(Hash) ? options[:a_fallback] : nil
    domain = email.match(/\@(.+)/)[1]
    Resolv::DNS.open do |dns|
      @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
      @mx.push(*dns.getresources(domain, Resolv::DNS::Resource::IN::A)) if a_fallback
    end
    @mx.size > 0 ? true : false
  end

  def validate_each(record, attribute, value)
    unless validates_email_format(value)
      record.errors[attribute] << (options[:message] || I18n.t(:invalid, :scope => [:activerecord, :errors, :messages]))
    end
    if options[:mx] && !validates_email_domain(value, options[:mx])
      record.errors[attribute] << (options[:mx_message] || I18n.t(:mx_invalid, :scope => [:activerecord, :errors, :messages]))
    end
  end

end<commit_msg>Intercept only one exception family, instead of everything.<commit_after>class EmailValidator < ActiveModel::EachValidator

  LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
  LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
  LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
  Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+[^_])|(\w+\.[^_]))*([a-z0-9-]{1,63})\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE, 'n')

  def validates_email_format(email)
    # local part max is 64 chars, domain part max is 255 chars
    # TODO: should this decode escaped entities before counting?
    begin
      local, domain = email.split('@', 2)
    rescue NoMethodError
      return false
    end

    email =~ Regex and not email =~ /\.\./ and domain.length <= 255 and local.length <= 64
  end

  def validates_email_domain(email, options)
    require 'resolv'
    a_fallback = options.is_a?(Hash) ? options[:a_fallback] : nil
    domain = email.match(/\@(.+)/)[1]
    Resolv::DNS.open do |dns|
      @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
      @mx.push(*dns.getresources(domain, Resolv::DNS::Resource::IN::A)) if a_fallback
    end
    @mx.size > 0 ? true : false
  end

  def validate_each(record, attribute, value)
    unless validates_email_format(value)
      record.errors[attribute] << (options[:message] || I18n.t(:invalid, :scope => [:activerecord, :errors, :messages]))
    end
    if options[:mx] && !validates_email_domain(value, options[:mx])
      record.errors[attribute] << (options[:mx_message] || I18n.t(:mx_invalid, :scope => [:activerecord, :errors, :messages]))
    end
  end

end
{'fraction_non_alphanumeric': 0.20534698521046643, 'fraction_numerical': 0.01478953356086462, 'mean_word_length': 4.05316091954023, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, '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': '1422128', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1240, 'n_words': 247}