2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-08-18 18:18:09 +08:00
discourse/lib/validators/email_address_validator.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
758 B
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
class EmailAddressValidator
EMAIL_REGEX =
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
ENCODED_WORD_REGEX = /\=\?[^?]+\?[BbQq]\?[^?]+\?\=/
class << self
def valid_value?(email)
email.match?(email_regex) && !email.match?(encoded_word_regex) &&
decode(email)&.match?(email_regex)
end
def email_regex
EMAIL_REGEX
end
def encoded_word_regex
ENCODED_WORD_REGEX
end
private
def decode(email)
Mail::Address.new(email).decoded
rescue Mail::Field::ParseError, Mail::Field::IncompleteParseError
nil
end
end
end