File Coverage

lib/Sisimai/Lhost/Verizon.pm
Criterion Covered Total %
statement 90 94 95.7
branch 48 58 82.7
condition 7 16 43.7
subroutine 6 6 100.0
pod 2 2 100.0
total 153 176 86.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Verizon;
2 19     19   5168 use parent 'Sisimai::Lhost';
  19         43  
  19         112  
3 19     19   1159 use feature ':5.10';
  19         40  
  19         1305  
4 19     19   100 use strict;
  19         41  
  19         372  
5 19     19   93 use warnings;
  19         35  
  19         25197  
6              
7 2     2 1 951 sub description { 'Verizon Wireless: https://www.verizonwireless.com' }
8             sub make {
9             # Detect an error from Verizon
10             # @param [Hash] mhead Message headers of a bounce email
11             # @param [String] mbody Message body of a bounce email
12             # @return [Hash] Bounce data list and message/rfc822 part
13             # @return [Undef] failed to parse or the arguments are missing
14             # @since v4.0.0
15 219     219 1 685 my $class = shift;
16 219   100     688 my $mhead = shift // return undef;
17 218   50     596 my $mbody = shift // return undef;
18 218         445 my $match = -1;
19              
20 218         410 while(1) {
21             # Check the value of "From" header
22             # 'subject' => qr/Undeliverable Message/,
23 218 100       297 last unless grep { rindex($_, '.vtext.com (') > -1 } @{ $mhead->{'received'} };
  423         1285  
  218         602  
24 11 100       51 $match = 1 if $mhead->{'from'} eq 'post_master@vtext.com';
25 11 100       81 $match = 0 if $mhead->{'from'} =~ /[<]?sysadmin[@].+[.]vzwpix[.]com[>]?\z/;
26 11         23 last;
27             }
28 218 100       773 return undef if $match < 0;
29              
30 11         42 state $indicators = __PACKAGE__->INDICATORS;
31              
32 11         45 my $rebackbone = qr/__BOUNDARY_STRING_HERE__/m;
33 11         47 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
34 11         27 my $emailsteak = [];
35 11         25 my $readcursor = 0; # (Integer) Points the current cursor position
36 11         23 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
37 11         15 my $senderaddr = ''; # (String) Sender address in the message body
38 11         21 my $subjecttxt = ''; # (String) Subject of the original message
39              
40 11         21 my $startingof = {}; # (Ref->Hash) Delimiter strings
41 11         20 my $markingsof = {}; # (Ref->Hash) Delimiter patterns
42 11         20 my $messagesof = {}; # (Ref->Hash) Error message patterns
43 11         19 my $v = undef;
44              
45 11 100       38 if( $match == 1 ) {
46             # vtext.com
47 5         24 $markingsof = { 'message' => qr/\AError:[ \t]/ };
48 5         19 $messagesof = {
49             # The attempted recipient address does not exist.
50             'userunknown' => ['550 - Requested action not taken: no such user here'],
51             };
52              
53 5 50       28 if( my $boundary00 = Sisimai::MIME->boundary($mhead->{'content-type'}, 1) ) {
54             # Convert to regular expression
55 5         71 $rebackbone = qr/^\Q$boundary00\E/m;
56             }
57              
58 5         34 $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
59 5         45 for my $e ( split("\n", $emailsteak->[0]) ) {
60             # Read error messages and delivery status lines from the head of the email
61             # to the previous line of the beginning of the original message.
62 95 100       136 unless( $readcursor ) {
63             # Beginning of the bounce message or delivery status part
64 10 100       52 $readcursor |= $indicators->{'deliverystatus'} if $e =~ $markingsof->{'message'};
65 10         17 next;
66             }
67 85 50       121 next unless $readcursor & $indicators->{'deliverystatus'};
68 85 100       115 next unless length $e;
69              
70             # Message details:
71             # Subject: Test message
72             # Sent date: Wed Jun 12 02:21:53 GMT 2013
73             # MAIL FROM: *******@hg.example.com
74             # RCPT TO: *****@vtext.com
75 65         64 $v = $dscontents->[-1];
76              
77 65 100       198 if( $e =~ /\A[ \t]+RCPT TO: (.*)\z/ ) {
    100          
    100          
78 5 50       18 if( $v->{'recipient'} ) {
79             # There are multiple recipient addresses in the message body.
80 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
81 0         0 $v = $dscontents->[-1];
82             }
83 5         14 $v->{'recipient'} = $1;
84 5         13 $recipients++;
85 5         8 next;
86              
87             } elsif( $e =~ /\A[ \t]+MAIL FROM:[ \t](.+)\z/ ) {
88             # MAIL FROM: *******@hg.example.com
89 5   33     28 $senderaddr ||= $1;
90              
91             } elsif( $e =~ /\A[ \t]+Subject:[ \t](.+)\z/ ) {
92             # Subject:
93 5   33     27 $subjecttxt ||= $1;
94              
95             } else {
96             # 550 - Requested action not taken: no such user here
97 50 100       100 $v->{'diagnosis'} = $e if $e =~ /\A(\d{3})[ \t][-][ \t](.*)\z/;
98             }
99             }
100             } else {
101             # vzwpix.com
102 6         22 $startingof = { 'message' => ['Message could not be delivered to mobile'] };
103 6         18 $messagesof = { 'userunknown' => ['No valid recipients for this MM'] };
104              
105 6 50       23 if( my $boundary00 = Sisimai::MIME->boundary($mhead->{'content-type'}, 1) ) {
106             # Convert to regular expression
107 6         64 $rebackbone = qr/^\Q$boundary00\E/m;
108             }
109              
110 6         41 $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
111 6         41 for my $e ( split("\n", $emailsteak->[0]) ) {
112             # Read error messages and delivery status lines from the head of the email
113             # to the previous line of the beginning of the original message.
114 54 100       94 unless( $readcursor ) {
115             # Beginning of the bounce message or delivery status part
116 12 100       49 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
117 12         17 next;
118             }
119 42 50       68 next unless $readcursor & $indicators->{'deliverystatus'};
120 42 100       72 next unless length $e;
121              
122             # Original Message:
123             # From: kijitora
124             # To: 0000000000@vzwpix.com
125             # Subject: test for bounce
126             # Date: Wed, 20 Jun 2013 10:29:52 +0000
127 36         35 $v = $dscontents->[-1];
128              
129 36 100       133 if( $e =~ /\ATo:[ \t]+(.*)\z/ ) {
    100          
    100          
130 6 50       19 if( $v->{'recipient'} ) {
131             # There are multiple recipient addresses in the message body.
132 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
133 0         0 $v = $dscontents->[-1];
134             }
135 6         22 $v->{'recipient'} = Sisimai::Address->s3s4($1);
136 6         15 $recipients++;
137 6         15 next;
138              
139             } elsif( $e =~ /\AFrom:[ \t](.+)\z/ ) {
140             # From: kijitora
141 6   33     45 $senderaddr ||= Sisimai::Address->s3s4($1);
142              
143             } elsif( $e =~ /\ASubject:[ \t](.+)\z/ ) {
144             # Subject:
145 6   33     40 $subjecttxt ||= $1;
146              
147             } else {
148             # Message could not be delivered to mobile.
149             # Error: No valid recipients for this MM
150 18 100       63 $v->{'diagnosis'} = $e if $e =~ /\AError:[ \t]+(.+)\z/;
151             }
152             }
153             }
154 11 50       78 return undef unless $recipients;
155              
156             # Set the value of "MAIL FROM:" and "From:"
157 11 50       82 $emailsteak->[1] .= sprintf("From: %s\n", $senderaddr) unless $emailsteak->[1] =~ /^From: /m;
158 11 50       68 $emailsteak->[1] .= sprintf("Subject: %s\n", $subjecttxt) unless $emailsteak->[1] =~ /^Subject: /m;
159              
160 11         29 for my $e ( @$dscontents ) {
161 11         73 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
162              
163 11         47 SESSION: for my $r ( keys %$messagesof ) {
164             # Verify each regular expression of session errors
165 11 50       22 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  11         66  
  11         32  
166 11         25 $e->{'reason'} = $r;
167 11         23 last;
168             }
169             }
170 11         82 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
171             }
172              
173             1;
174             __END__