diff --git a/lib/sisimai/rhost.rb b/lib/sisimai/rhost.rb index 1130a198..829438c6 100644 --- a/lib/sisimai/rhost.rb +++ b/lib/sisimai/rhost.rb @@ -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: @@ -46,14 +44,14 @@ 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 @@ -61,13 +59,24 @@ def find(argvs) 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 diff --git a/test/public/mail-maildir-test.rb b/test/public/mail-maildir-test.rb index e52ec513..e6a3d7d2 100644 --- a/test/public/mail-maildir-test.rb +++ b/test/public/mail-maildir-test.rb @@ -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 } diff --git a/test/public/rhost-test.rb b/test/public/rhost-test.rb index e43dabc5..a83bf150 100644 --- a/test/public/rhost-test.rb +++ b/test/public/rhost-test.rb @@ -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