Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Regular Expressions #254

Merged
merged 37 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
41e088f
Implement Sisimai::SMTP::Command.test() and tests for the method
azumakuniyuki Feb 17, 2023
d955659
Implement Sisimai::SMTP::Reply.test and tests for the method
azumakuniyuki Feb 17, 2023
e8ba6e0
Implement Sisimai::String.aligned() and tests for the method
azumakuniyuki Feb 18, 2023
7880489
Implement Sisimai::String.ipv4() and tests for the method
azumakuniyuki Feb 18, 2023
2ff5d16
Sisimai::SMTP::Reply.find() borned again
azumakuniyuki Feb 18, 2023
9ee9600
Register "4.4.8" as "networkerror"
azumakuniyuki Feb 18, 2023
0c7b352
Update test to avoid "DEPRECATED: Use assert_nil if expecting nil" me…
azumakuniyuki Feb 18, 2023
0938633
Update the list of bounce reasons
azumakuniyuki Feb 18, 2023
19e7706
Update test for ArgumentError
azumakuniyuki Feb 18, 2023
c322142
Implement Sisimai::SMTP::Status.test() and tests for the method
azumakuniyuki Feb 18, 2023
82b95ec
Fix code for checking the value of "f" returned from Sisimai::RFC1894…
azumakuniyuki Mar 4, 2023
a128154
Fix code for checking the value of "f" returned from Sisimai::RFC1894…
azumakuniyuki Mar 4, 2023
dc69da7
Import updated test results
azumakuniyuki Mar 5, 2023
eeaaf4a
Bug fix for checking the value of "deliverystatus" returned from Sisi…
azumakuniyuki Mar 5, 2023
2eb7427
Reduce regular expressions
azumakuniyuki May 8, 2023
c9e90ab
Reduce regular expressions at each file in lib/sisimai/lhost
azumakuniyuki May 8, 2023
055eba8
Renamed: GoogleApps -> Google
azumakuniyuki May 8, 2023
c7d4972
Renamed: ExchangeOnline -> Microsoft
azumakuniyuki May 8, 2023
2b0b65a
Renamed: TencentQQ -> Tencent
azumakuniyuki May 8, 2023
60d4c33
Reduce regular expressions
azumakuniyuki May 8, 2023
1598c15
Reduce regular expressions
azumakuniyuki May 8, 2023
b5322aa
Reduce regualr expressions, Implement Sisimai::SMTP::Status.prefer() …
azumakuniyuki May 8, 2023
e573390
Reduce regular expressions
azumakuniyuki May 8, 2023
61c9fb2
Update test results for Sisimai::Lhost::*
azumakuniyuki May 8, 2023
8aa93a1
Remove a blank line
azumakuniyuki May 8, 2023
94fa800
Some modules have been renamed, reduce regular expressions
azumakuniyuki May 8, 2023
b2a30cb
Reduce regular expressions
azumakuniyuki May 8, 2023
68825ce
Reduce regular expressions using Sisimai::String.alined()
azumakuniyuki May 8, 2023
125a88e
Some variables have been renamed
azumakuniyuki May 8, 2023
1505db6
Update for changes at Sisimai::Address.undisclosed
azumakuniyuki May 8, 2023
2172e50
Reduce regular expressions
azumakuniyuki May 8, 2023
7c0f353
Update for renamed modules in Sisimai::Rhost::*
azumakuniyuki May 8, 2023
fe5a988
Fix an SMTP reply code in a test string
azumakuniyuki May 8, 2023
4b343a1
Add a test code for Sisimai::String.ipv4
azumakuniyuki May 8, 2023
e2c5aca
Large scale improvement, Reduce regular expressions
azumakuniyuki May 8, 2023
64cc4e7
Update test codes
azumakuniyuki May 8, 2023
915b9a4
Remove renamed email files
azumakuniyuki May 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 63 additions & 47 deletions lib/sisimai/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class Address
}.freeze
Delimiters = { '<' => 1, '>' => 1, '(' => 1, ')' => 1, '"' => 1, ',' => 1 }.freeze

# Return pseudo recipient or sender address
# @param [Symbol] argv0 Address type: true = recipient, false = sender
# @return [String, nil] Pseudo recipient address or sender address or nil when the argv1 is
# neither :r nor :s
def self.undisclosed(argv0 = false)
return sprintf('undisclosed-%[email protected]', argv0 ? 'recipient' : 'sender')
end

# Check that the argument is an email address or not
# @param [String] email Email address string
# @return [True,False] true: is an email address
Expand All @@ -48,34 +56,26 @@ def self.is_emailaddress(email)
end

# Check that the argument is mailer-daemon or not
# @param [String] email Email address
# @param [String] argv0 Email address
# @return [True,False] true: mailer-daemon
# false: Not mailer-daemon
def self.is_mailerdaemon(email)
return false unless email.is_a?(::String)
regex = %r/(?:
(?:mailer-daemon|postmaster)[@]
|[<(](?:mailer-daemon|postmaster)[)>]
|\A(?:mailer-daemon|postmaster)\z
|[ ]?mailer-daemon[ ]
)
/x.freeze
return true if email.downcase =~ regex
def self.is_mailerdaemon(argv0 = nil)
return false unless argv0
return false unless argv0.size > 0
return false unless argv0.is_a?(::String)

email = argv0.downcase
postmaster = [
'mailer-daemon@', '<mailer-daemon>', '(mailer-daemon)', ' mailer-daemon ',
'postmaster@', '<postmaster>', '(postmaster)'
].freeze

return true if postmaster.any? { |a| email.include?(a) }
return true if email == 'mailer-daemon'
return true if email == 'postmaster'
return false
end

# Return pseudo recipient or sender address
# @param [Symbol] argv1 Address type: :r or :s
# @return [String, nil] Pseudo recipient address or sender address or nil when the argv1 is
# neither :r nor :s
def self.undisclosed(argv1)
return nil unless argv1
return nil unless %w[r s].index(argv1)

local = argv1 == 'r' ? 'recipient' : 'sender'
return sprintf('undisclosed-%[email protected]', local)
end

def self.find(argv1 = nil, addrs = false)
# Email address parser with a name and a comment
# @param [String] argv1 String including email address
Expand Down Expand Up @@ -252,10 +252,12 @@ def self.find(argv1 = nil, addrs = false)

unless v[:address].empty?
# Remove the comment from the address
if cv = v[:address].match(/(.*)([(].+[)])(.*)/)
if Sisimai::String.aligned(v[:address], ['(', ')'])
# (nyaan)[email protected], nekochan(nyaan)[email protected] or nekochan(nyaan)@example.org
v[:address] = cv[1] << cv[3]
v[:comment] = cv[2]
p1 = v[:address].index('(')
p2 = v[:address].index(')')
v[:address] = v[:address][0, p1] + v[:address][p2 + 1, v[:address].size]
v[:comment] = v[:address][p1, p2 - p1 - 1]
end
readbuffer << v
end
Expand All @@ -264,7 +266,7 @@ def self.find(argv1 = nil, addrs = false)
while e = readbuffer.shift do
# The element must not include any character except from 0x20 to 0x7e.
next if e[:address] =~ /[^\x20-\x7e]/
unless e[:address] =~ /\A.+[@].+\z/
if e[:address].include?('@') == false
# Allow if the argument is MAILER-DAEMON
next unless Sisimai::Address.is_mailerdaemon(e[:address])
end
Expand Down Expand Up @@ -343,58 +345,72 @@ def self.expand_alias(email)
attr_accessor :name, :comment

# Constructor of Sisimai::Address
# @param [Hash] argv1 Email address, name, and other elements
# @param [Hash] argvs Email address, name, and other elements
# @return [Sisimai::Address] Object or nil when the email address was not valid.
# @example new({address: '[email protected]', name: 'Neko', comment: '(nyaan)')} # => Sisimai::Address object
def initialize(argv1)
return nil unless argv1.is_a? Hash
return nil unless argv1[:address]
return nil if argv1[:address].empty?
def initialize(argvs)
return nil unless argvs.is_a? Hash
return nil unless argvs[:address]
return nil if argvs[:address].empty?

heads = ['<']
tails = ['>', ',', '.', ';']
if cv = argv1[:address].match(/\A([^\s]+)[@]([^@]+)\z/) ||
argv1[:address].match(/\A(["].+?["])[@]([^@]+)\z/)
point = argvs[:address].rindex('@')
if cv = argvs[:address].match(/\A([^\s]+)[@]([^@]+)\z/) ||
argvs[:address].match(/\A(["].+?["])[@]([^@]+)\z/)
# Get the local part and the domain part from the email address
lpart = cv[1]; heads.each { |e| lpart.gsub!(/\A#{e}/, '') if lpart.start_with?(e) }
dpart = cv[2]; tails.each { |e| dpart.gsub!(/#{e}\z/, '') if dpart.end_with?(e) }
email = Sisimai::Address.expand_verp(argv1[:address])
lpart = argvs[:address][0, point]
dpart = argvs[:address][point + 1, argvs[:address].size]
email = Sisimai::Address.expand_verp(argvs[:address])
aname = nil

unless email
# Is not VERP address, try to expand the address as an alias
email = Sisimai::Address.expand_alias(argv1[:address]) || ''
email = Sisimai::Address.expand_alias(argvs[:address]) || ''
aname = true unless email.empty?
end

if email =~ /\A.+[@].+?\z/
if email.include?('@')
# The address is a VERP or an alias
if aname
# The address is an alias: [email protected]
@alias = argv1[:address]
@alias = argvs[:address]
else
# The address is a VERP: [email protected]
@verp = argv1[:address]
@verp = argvs[:address]
end
end

heads.each do |e|
while lpart[0, 1] == e
lpart[0, 1] = ''
end
end

tails.each do |e|
while dpart[-1, 1] == e
dpart[-1, 1] = ''
end
end

@user = lpart
@host = dpart
@address = lpart + '@' + dpart
else
# The argument does not include "@"
return nil unless Sisimai::Address.is_mailerdaemon(argv1[:address])
return nil if argv1[:address].include?(' ')
return nil unless Sisimai::Address.is_mailerdaemon(argvs[:address])
return nil if argvs[:address].include?(' ')

# The argument does not include " "
@user = argv1[:address]
@user = argvs[:address]
@host ||= ''
@address = argv1[:address]
@address = argvs[:address]
end

@alias ||= ''
@verp ||= ''
@name = argv1[:name] || ''
@comment = argv1[:comment] || ''
@name = argvs[:name] || ''
@comment = argvs[:comment] || ''
end

# Check whether the object has valid content or not
Expand Down
Loading