File Coverage

lib/Sisimai/Rhost.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 6 100.0
condition 10 12 83.3
subroutine 5 5 100.0
pod 0 2 0.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Sisimai::Rhost;
2 86     86   1262 use v5.26;
  86         268  
3 86     86   366 use strict;
  86         114  
  86         1438  
4 86     86   292 use warnings;
  86         96  
  86         40540  
5              
6             state $RhostClass = {
7             "Aol" => [".mail.aol.com", ".mx.aol.com"],
8             "Apple" => [".mail.icloud.com", ".apple.com", ".me.com", "privaterelay.appleid.com"],
9             "Cloudflare" => [".mx.cloudflare.net"],
10             "Cox" => ["cox.net"],
11             "Facebook" => [".facebook.com"],
12             "FrancePTT" => [".laposte.net", ".orange.fr", ".wanadoo.fr"],
13             "GoDaddy" => ["smtp.secureserver.net", "mailstore1.secureserver.net"],
14             "Google" => ["aspmx.l.google.com", "gmail-smtp-in.l.google.com"],
15             "GSuite" => ["googlemail.com"],
16             "IUA" => [".email.ua"],
17             "KDDI" => [".ezweb.ne.jp", "msmx.au.com"],
18             "MessageLabs" => [".messagelabs.com"],
19             "Microsoft" => [".prod.outlook.com", ".protection.outlook.com", ".onmicrosoft.com", ".exchangelabs.com",],
20             "Mimecast" => [".mimecast.com"],
21             "NTTDOCOMO" => ["mfsmax.docomo.ne.jp"],
22             "Outlook" => [".hotmail.com"],
23             "Spectrum" => ["charter.net"],
24             "Tencent" => [".qq.com"],
25             "YahooInc" => [".yahoodns.net"],
26             "Zoho" => [".zoho.com", ".zoho.eu", ".zoho.jp", ".zoho.com.au", ".zoho.com.cn", ".zoho.in"],
27             };
28              
29             sub name {
30             # Detect the rhost class name
31             # @param [Sisimai::Fact] argvs Decoded email object
32             # @return [String] rhost class name
33 3418     3418 0 104964 my $class = shift;
34 3418   50     6507 my $argvs = shift || return "";
35              
36 3418         4245 my $rhostclass = "";
37 3418   100     8260 my $clienthost = lc $argvs->{"lhost"} || "";
38 3418   100     7462 my $remotehost = lc $argvs->{"rhost"} || "";
39 3418   50     6537 my $domainpart = lc $argvs->{"destination"} || "";
40              
41 3418         6241 FINDRHOST: while( $rhostclass eq "" ) {
42             # Try to match the hostname patterns with the following order:
43             # 1. destination: The domain part of the recipient address
44             # 2. rhost: remote hostname
45             # 3. lhost: local MTA hostname
46 3418         17417 for my $e ( keys %$RhostClass ) {
47             # Try to match the domain part with each value of RhostClass
48 64899 100       64330 next unless grep { index($_, $domainpart) > -1 } $RhostClass->{ $e }->@*;
  120028         146810  
49 337         470 $rhostclass = $e; last FINDRHOST;
  337         577  
50             }
51              
52 3081         10698 for my $e ( keys %$RhostClass ) {
53             # Try to match the remote host with each value of RhostClass
54 55908 100       51046 next unless grep { index($remotehost, $_) > -1 } $RhostClass->{ $e }->@*;
  103412         124705  
55 597         1048 $rhostclass = $e; last FINDRHOST;
  597         971  
56             }
57              
58             # Neither the remote host nor the destination did not matched with any value of RhostClass
59 2484         7591 for my $e ( keys %$RhostClass ) {
60             # Try to match the client host with each value of RhostClass
61 47062 100       42243 next unless grep { index($clienthost, $_) > -1 } $RhostClass->{ $e }->@*;
  87117         100706  
62 249         318 $rhostclass = $e; last FINDRHOST;
  249         398  
63             }
64 2235         4614 last;
65             }
66 3418         11259 return $rhostclass;
67             }
68              
69             sub find {
70             # Detect the bounce reason from certain remote hosts
71             # @param [Sisimai::Fact] argvs Decoded email object
72             # @return [String] The value of bounce reason
73 3329     3329 0 207433 my $class = shift;
74 3329   100     5847 my $argvs = shift || return "";
75 3328   100     7697 my $rhost = __PACKAGE__->name($argvs) || return "";
76              
77 1093         2686 $rhost = __PACKAGE__."::".$rhost; (my $modulepath = $rhost) =~ s|::|/|g;
  1093         5266  
78 1093         73082 require $modulepath.'.pm';
79 1093         7191 return $rhost->find($argvs);
80             }
81              
82             1;
83             __END__