Skip to content

Commit

Permalink
Fixes #35 for non-standard tag formats
Browse files Browse the repository at this point in the history
Tags can be more random and not confirm to the mailbox rules.

Seperates validation of local into mailbox and tag.
  • Loading branch information
afair committed Nov 22, 2019
1 parent 8b2f5e7 commit fe055a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
3 changes: 0 additions & 3 deletions lib/email_address/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ def valid?(options={})
else
return false unless self.local.valid?
return false unless self.host.valid?
end
if @config[:address_validation] == :smtp

end
true
end
Expand Down
19 changes: 16 additions & 3 deletions lib/email_address/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ class Local

REDACTED_REGEX = /\A \{ [0-9a-f]{40} \} \z/x # {sha1}

CONVENTIONAL_TAG_REGEX = # AZaz09_!'+-/=
%r/^([\w\!\'\+\-\/\=]+)$/i.freeze
RELAXED_TAG_REGEX = # AZaz09_!#$%&'*+-/=?^`{|}~
%r/^([\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+)$/i.freeze

def initialize(local, config={}, host=nil)
self.config = config.empty? ? EmailAddress::Config.all_settings : config
self.local = local
Expand Down Expand Up @@ -328,7 +333,12 @@ def valid_encoding?(enc=@config[:local_encoding]||:ascii)
# True if the part matches the conventional format
def conventional?
self.syntax = :invalid
self.local =~ CONVENTIONAL_MAILBOX_REGEX or return false
if self.tag
return false unless self.mailbox =~ CONVENTIONAL_MAILBOX_REGEX &&
self.tag =~ CONVENTIONAL_TAG_REGEX
else
return false unless self.local =~ CONVENTIONAL_MAILBOX_REGEX
end
self.valid_size? or return false
self.valid_encoding? or return false
self.syntax = :conventional
Expand All @@ -340,7 +350,10 @@ def relaxed?
self.syntax = :invalid
self.valid_size? or return false
self.valid_encoding? or return false
if self.local =~ RELAXED_MAILBOX_REGEX
if self.tag
return false unless self.mailbox =~ RELAXED_MAILBOX_REGEX &&
self.tag =~ RELAXED_TAG_REGEX
elsif self.local =~ RELAXED_MAILBOX_REGEX
self.syntax = :relaxed
true
else
Expand Down Expand Up @@ -384,7 +397,7 @@ def error_message
end

def error
self.valid? ? nil : @error
self.valid? ? nil : ( @error || :local_invalid)
end

end
Expand Down
1 change: 1 addition & 0 deletions lib/email_address/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ en:
ipv6_address_invalid: "This is not a valid IPv6 address"
local_size_long: "Mailbox name too long"
local_size_short: "Mailbox name too short"
local_invalid: "Recipient is not valid"
not_allowed: "Address is not allowed"
server_not_available: "The remote email server is not available"
4 changes: 4 additions & 0 deletions test/email_address/test_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ def test_relaxed_normal
def test_nil_address
assert_nil EmailAddress.new(nil).normal, "Expected a nil input to make nil output"
end

def test_nonstandard_tag
assert EmailAddress.valid?("[email protected]")
end
end

0 comments on commit fe055a9

Please sign in to comment.