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