1package Sisimai::Rhost;
2use feature ':5.10';
3use strict;
4use warnings;
5
6use constant RhostClass => {
7    qr/[.](?:prod|protection)[.]outlook[.]com\z/      => 'ExchangeOnline',
8    qr/\b(?>laposte[.]net|(?:orange|wanadoo)[.]fr)\z/ => 'FrancePTT',
9    qr/\A(?:smtp|mailstore1)[.]secureserver[.]net\z/  => 'GoDaddy',
10    qr/(?:aspmx|gmail-smtp-in)[.]l[.]google[.]com\z/  => 'GoogleApps',
11    qr/[.]email[.]ua\z/                               => 'IUA',
12    qr/[.](?:ezweb[.]ne[.]jp|au[.]com)\z/             => 'KDDI',
13    qr/charter[.]net/                                 => 'Spectrum',
14    qr/cox[.]net/                                     => 'Cox',
15    qr/mx[0-9]+[.]qq[.]com\z/                         => 'TencentQQ',
16};
17
18sub match {
19    # The value of "rhost" is listed in RhostClass or not
20    # @param    [String] argv1  Remote host name
21    # @return   [Integer]       0: did not match
22    #                           1: match
23    my $class = shift;
24    my $rhost = shift // return undef;
25    my $host0 = lc($rhost) || return 0;
26    my $match = 0;
27
28    for my $e ( keys %{ RhostClass() } ) {
29        # Try to match with each key of RhostClass
30        next unless $host0 =~ $e;
31        $match = 1;
32        last;
33    }
34    return $match;
35}
36
37sub get {
38    # Detect the bounce reason from certain remote hosts
39    # @param    [Sisimai::Data] argvs   Parsed email object
40    # @param    [String]        proxy   The alternative of the "rhost"
41    # @return   [String]                The value of bounce reason
42    my $class = shift;
43    my $argvs = shift || return undef;
44    my $proxy = shift || undef;
45
46    my $remotehost = $proxy || lc $argvs->rhost;
47    my $rhostclass = '';
48
49    for my $e ( keys %{ RhostClass() } ) {
50        # Try to match with each key of RhostClass
51        next unless $remotehost =~ $e;
52        $rhostclass = __PACKAGE__.'::'.RhostClass->{ $e };
53        last;
54    }
55    return undef unless $rhostclass;
56
57    (my $modulepath = $rhostclass) =~ s|::|/|g;
58    require $modulepath.'.pm';
59    return $rhostclass->get($argvs);
60}
61
621;
63__END__
64
65=encoding utf-8
66
67=head1 NAME
68
69Sisimai::Rhost - Detect the bounce reason returned from certain remote hosts.
70
71=head1 SYNOPSIS
72
73    use Sisimai::Rhost;
74
75=head1 DESCRIPTION
76
77Sisimai::Rhost detects the bounce reason from the content of Sisimai::Data
78object as an argument of get() method when the value of C<rhost> of the object
79is listed in the results of Sisimai::Rhost->list() method.
80This class is called only Sisimai::Data class.
81
82=head1 CLASS METHODS
83
84=head2 C<B<match(I<remote host>)>>
85
86Returns 1 if the remote host is listed in the results of Sisimai::Rhost->list()
87method.
88
89=head2 C<B<get(I<Sisimai::Data Object>)>>
90
91C<get()> detects the bounce reason.
92
93=head1 AUTHOR
94
95azumakuniyuki
96
97=head1 COPYRIGHT
98
99Copyright (C) 2014-2020 azumakuniyuki, All rights reserved.
100
101=head1 LICENSE
102
103This software is distributed under The BSD 2-Clause License.
104
105=cut
106