Skip to content

Commit

Permalink
Implement Sisimai::Rhost.name() method, import commit sisimai/go-sisi…
Browse files Browse the repository at this point in the history
  • Loading branch information
azumakuniyuki committed Jan 11, 2025
1 parent f6e25b8 commit 02475fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
33 changes: 21 additions & 12 deletions lib/sisimai/rhost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ class << self
"YahooInc" => [".yahoodns.net"],
}.freeze

# Detect the bounce reason from certain remote hosts
# Returns the rhost class name
# @param [Hash] argvs Decoded email data
# @return [String] The value of bounce reason
def find(argvs)
return "" if argvs["diagnosticcode"].empty?
# @return [String] rhost class name
def name(argvs)
return "" if argvs.nil?

rhostclass = ""
clienthost = argvs["lhost"].downcase
remotehost = argvs["rhost"].downcase
domainpart = argvs["destination"].downcase
rhostclass = ""
modulename = ""
return "" if (remotehost + domainpart).empty?

catch :FINDRHOST do
# Try to match the hostname patterns with the following order:
Expand All @@ -46,28 +44,39 @@ def find(argvs)
RhostClass.each_key do |e|
# Try to match the domain part of the recipient address with each value of RhostClass
next unless RhostClass[e].any? { |a| a.end_with?(domainpart) }
modulename = 'Sisimai::Rhost::' << e
rhostclass = e
throw :FINDRHOST
end

RhostClass.each_key do |e|
# Try to match the remote host with each value of RhostClass
next unless RhostClass[e].any? { |a| remotehost.end_with?(a) }
modulename = 'Sisimai::Rhost::' << e
rhostclass = e
throw :FINDRHOST
end

# Neither the remote host nor the destination did not matched with any value of RhostClass
RhostClass.each_key do |e|
# Try to match the client host with each value of RhostClass
next unless RhostClass[e].any? { |a| clienthost.end_with?(a) }
modulename = 'Sisimai::Rhost::' << e
rhostclass = e
throw :FINDRHOST
end
end
return "" if modulename.empty?
return rhostclass
end

# Detect the bounce reason from certain remote hosts
# @param [Hash] argvs Decoded email data
# @return [String] The value of bounce reason
def find(argvs)
return "" if argvs.nil?

rhostclass = name(argvs); return "" if rhostclass.empty?
modulepath = "sisimai/rhost/" << rhostclass.downcase; require modulepath
modulename = "Sisimai::Rhost::" << rhostclass

rhostclass = "sisimai/rhost/" << modulename.downcase.split("::")[2]; require rhostclass
#rhostclass = "sisimai/rhost/" << modulename.downcase.split("::")[2]; require rhostclass
reasontext = Module.const_get(modulename).find(argvs)
return "" if reasontext.empty?
return reasontext
Expand Down
2 changes: 1 addition & 1 deletion test/public/mail-maildir-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MailMaildirTest < Minitest::Test
Methods = { class: %w[new], object: %w[path dir file size handle offset read] }
Samples = ['./set-of-emails/maildir/bsd', './set-of-emails/maildir/mac']
Maildir = Sisimai::Mail::Maildir.new(Samples[0])
DirSize = 585
DirSize = 599

def test_methods
Methods[:class].each { |e| assert_respond_to Sisimai::Mail::Maildir, e }
Expand Down
42 changes: 25 additions & 17 deletions test/public/rhost-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,43 @@
require 'sisimai'

class RhostTest < Minitest::Test
Methods = { class: %w[find] }
Methods = { class: %w[name find] }
Classes = %w[
Aol Apple Cox Facebook FrancePTT GoDaddy Google GSuite IUA KDDI MessageLabs Microsoft Mimecast
NTTDOCOMO Outlook Spectrum Tencent YahooInc
]
Objects = []

def test_methods
Methods[:class].each { |e| assert_respond_to Sisimai::Rhost, e }
end

def test_find
def test_name
Dir.glob('./set-of-emails/maildir/bsd/rhost-*.eml').each do |e|
cv = Sisimai.rise(e)
assert_instance_of Array, cv

cv = Sisimai.rise(e); assert_instance_of Array, cv
warn "\nFile = " << e
cv.each do |ee|
assert_instance_of Sisimai::Fact, ee
assert_instance_of String, ee.rhost
assert_instance_of String, ee.reason
refute_empty ee.rhost
refute_empty ee.reason
cx = ee.damn

cr = Sisimai::Rhost.find(cx)
refute_empty cx['destination']
refute_empty cx['reason']
fo = ee.damn
cx = Sisimai::Rhost.name(fo); refute_empty cx
Objects << fo
end

end

ce = assert_raises ArgumentError do
Sisimai::Rhost.find()
Sisimai::Rhost.find(nil, nil, nil)
Sisimai::Rhost.name()
Sisimai::Rhost.name(nil, nil)
end
end

def test_find
Objects.each do |fo|
cr = Sisimai::Rhost.find(fo); assert_instance_of ::String, cr

ce = assert_raises ArgumentError do
Sisimai::Rhost.find()
Sisimai::Rhost.find(nil, nil)
end
end
end
end
Expand Down

0 comments on commit 02475fb

Please sign in to comment.