<commit_before># frozen_string_literal: true

require 'resolv'

class EmailMxValidator < ActiveModel::Validator
  def validate(user)
    return if Rails.env.test? || Rails.env.development?
    user.errors.add(:email, I18n.t('users.invalid_email')) if invalid_mx?(user.email)
  end

  private

  def invalid_mx?(value)
    _, domain = value.split('@', 2)

    return true if domain.nil?

    records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }
    records.empty? || on_blacklist?(records)
  end

  def on_blacklist?(values)
    EmailDomainBlock.where(domain: values).any?
  end
end<commit_msg>In e-mail validator, fallback from MX to A record (#7955)<commit_after># frozen_string_literal: true

require 'resolv'

class EmailMxValidator < ActiveModel::Validator
  def validate(user)
    return if Rails.env.test? || Rails.env.development?
    user.errors.add(:email, I18n.t('users.invalid_email')) if invalid_mx?(user.email)
  end

  private

  def invalid_mx?(value)
    _, domain = value.split('@', 2)

    return true if domain.nil?

    records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }
    records = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::A).to_a.map { |e| e.exchange.to_s } if records.empty?

    records.empty? || on_blacklist?(records)
  end

  def on_blacklist?(values)
    EmailDomainBlock.where(domain: values).any?
  end
end
{'fraction_non_alphanumeric': 0.14266396213657878, 'fraction_numerical': 0.0067613252197430695, 'mean_word_length': 4.461254612546125, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1670301', 'n_tokens_mistral': 535, 'n_tokens_neox': 521, 'n_words': 101}