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   5897 use parent 'Sisimai::Lhost';
  19         47  
  19         98  
3 19     19   1161 use feature ':5.10';
  19         45  
  19         1286  
4 19     19   98 use strict;
  19         38  
  19         348  
5 19     19   88 use warnings;
  19         33  
  19         25933  
6              
7 2     2 1 1882 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 1020 my $class = shift;
16 219   100     596 my $mhead = shift // return undef;
17 218   50     589 my $mbody = shift // return undef;
18 218         342 my $match = -1;
19              
20 218         300 while(1) {
21             # Check the value of "From" header
22             # 'subject' => qr/Undeliverable Message/,
23 218 100       337 last unless grep { rindex($_, '.vtext.com (') > -1 } @{ $mhead->{'received'} };
  423         1270  
  218         630  
24 11 100       120 $match = 1 if $mhead->{'from'} eq 'post_master@vtext.com';
25 11 100       84 $match = 0 if $mhead->{'from'} =~ /[<]?sysadmin[@].+[.]vzwpix[.]com[>]?\z/;
26 11         26 last;
27             }
28 218 100       685 return undef if $match < 0;
29              
30 11         38 state $indicators = __PACKAGE__->INDICATORS;
31              
32 11         53 my $rebackbone = qr/__BOUNDARY_STRING_HERE__/m;
33 11         50 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
34 11         42 my $emailsteak = [];
35 11         23 my $readcursor = 0; # (Integer) Points the current cursor position
36 11         23 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
37 11         25 my $senderaddr = ''; # (String) Sender address in the message body
38 11         23 my $subjecttxt = ''; # (String) Subject of the original message
39              
40 11         19 my $startingof = {}; # (Ref->Hash) Delimiter strings
41 11         27 my $markingsof = {}; # (Ref->Hash) Delimiter patterns
42 11         23 my $messagesof = {}; # (Ref->Hash) Error message patterns
43 11         29 my $v = undef;
44              
45 11 100       36 if( $match == 1 ) {
46             # vtext.com
47 5         27 $markingsof = { 'message' => qr/\AError:[ \t]/ };
48 5         20 $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       24 if( my $boundary00 = Sisimai::MIME->boundary($mhead->{'content-type'}, 1) ) {
54             # Convert to regular expression
55 5         106 $rebackbone = qr/^\Q$boundary00\E/m;
56             }
57              
58 5         37 $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
59 5         64 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       141 unless( $readcursor ) {
63             # Beginning of the bounce message or delivery status part
64 10 100       56 $readcursor |= $indicators->{'deliverystatus'} if $e =~ $markingsof->{'message'};
65 10         18 next;
66             }
67 85 50       132 next unless $readcursor & $indicators->{'deliverystatus'};
68 85 100       139 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         67 $v = $dscontents->[-1];
76              
77 65 100       249 if( $e =~ /\A[ \t]+RCPT TO: (.*)\z/ ) {
    100          
    100          
78 5 50       21 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         20 $v->{'recipient'} = $1;
84 5         13 $recipients++;
85 5         9 next;
86              
87             } elsif( $e =~ /\A[ \t]+MAIL FROM:[ \t](.+)\z/ ) {
88             # MAIL FROM: *******@hg.example.com
89 5   33     37 $senderaddr ||= $1;
90              
91             } elsif( $e =~ /\A[ \t]+Subject:[ \t](.+)\z/ ) {
92             # Subject:
93 5   33     44 $subjecttxt ||= $1;
94              
95             } else {
96             # 550 - Requested action not taken: no such user here
97 50 100       113 $v->{'diagnosis'} = $e if $e =~ /\A(\d{3})[ \t][-][ \t](.*)\z/;
98             }
99             }
100             } else {
101             # vzwpix.com
102 6         25 $startingof = { 'message' => ['Message could not be delivered to mobile'] };
103 6         22 $messagesof = { 'userunknown' => ['No valid recipients for this MM'] };
104              
105 6 50       31 if( my $boundary00 = Sisimai::MIME->boundary($mhead->{'content-type'}, 1) ) {
106             # Convert to regular expression
107 6         77 $rebackbone = qr/^\Q$boundary00\E/m;
108             }
109              
110 6         42 $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
111 6         52 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       102 unless( $readcursor ) {
115             # Beginning of the bounce message or delivery status part
116 12 100       56 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
117 12         20 next;
118             }
119 42 50       79 next unless $readcursor & $indicators->{'deliverystatus'};
120 42 100       77 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         48 $v = $dscontents->[-1];
128              
129 36 100       160 if( $e =~ /\ATo:[ \t]+(.*)\z/ ) {
    100          
    100          
130 6 50       26 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         21 $v->{'recipient'} = Sisimai::Address->s3s4($1);
136 6         33 $recipients++;
137 6         13 next;
138              
139             } elsif( $e =~ /\AFrom:[ \t](.+)\z/ ) {
140             # From: kijitora
141 6   33     52 $senderaddr ||= Sisimai::Address->s3s4($1);
142              
143             } elsif( $e =~ /\ASubject:[ \t](.+)\z/ ) {
144             # Subject:
145 6   33     36 $subjecttxt ||= $1;
146              
147             } else {
148             # Message could not be delivered to mobile.
149             # Error: No valid recipients for this MM
150 18 100       58 $v->{'diagnosis'} = $e if $e =~ /\AError:[ \t]+(.+)\z/;
151             }
152             }
153             }
154 11 50       58 return undef unless $recipients;
155              
156             # Set the value of "MAIL FROM:" and "From:"
157 11 50       90 $emailsteak->[1] .= sprintf("From: %s\n", $senderaddr) unless $emailsteak->[1] =~ /^From: /m;
158 11 50       78 $emailsteak->[1] .= sprintf("Subject: %s\n", $subjecttxt) unless $emailsteak->[1] =~ /^Subject: /m;
159              
160 11         42 for my $e ( @$dscontents ) {
161 11         77 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
162              
163 11         61 SESSION: for my $r ( keys %$messagesof ) {
164             # Verify each regular expression of session errors
165 11 50       29 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  11         65  
  11         30  
166 11         28 $e->{'reason'} = $r;
167 11         25 last;
168             }
169             }
170 11         80 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
171             }
172              
173             1;
174             __END__