Sisimai/Lhost/AmazonSES.pm

filter: Statementstmt 90.0% 108 / 120 Branchbran 69.0% 29 / 42 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 83.2% 154 / 185 CC 32 SCAR 34.7
CC
32
Cov
84%
inquire
38.2
BEGIN
1.0
BEGIN
1.0
CRAP 32.3
« Sisimai/Lhost/Mimecast.pm ↑ Summary Sisimai/RFC3834.pm »
1 package Sisimai::Lhost::AmazonSES;
2 43 ▶use parent 'Sisimai::Lhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 43 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use Sisimai::Eb;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ---------------------------------------------------------------------------------------------
9 # "notificationType": "Bounce"
10 # https://docs.aws.amazon.com/ses/latest/dg/notification-contents.html#bounce-object
11 #
12 # Bounce types
13 # The bounce object contains a bounce type of Undetermined, Permanent, or Transient. The
14 # Permanent and Transient bounce types can also contain one of several bounce subtypes.
15 #
16 # When you receive a bounce notification with a bounce type of Transient, you might be
17 # able to send email to that recipient in the future if the issue that caused the message
18 # to bounce is resolved.
19 #
20 # When you receive a bounce notification with a bounce type of Permanent, it's unlikely
21 # that you'll be able to send email to that recipient in the future. For this reason, you
22 # should immediately remove the recipient whose address produced the bounce from your
23 # mailing lists.
24 #
25 # "bounceType"/"bounceSubType" "Desription"
26 # Undetermined/Undetermined -- The bounce message didn't contain enough information for
27 # Amazon SES to determine the reason for the bounce.
28 #
29 # Permanent/General ---------- When you receive this type of bounce notification, you should
30 # immediately remove the recipient's email address from your
31 # mailing list.
32 # Permanent/NoEmail ---------- It was not possible to retrieve the recipient email address
33 # from the bounce message.
34 # Permanent/Suppressed ------- The recipient's email address is on the Amazon SES suppression
35 # list because it has a recent history of producing hard bounces.
36 # Permanent/OnAccountSuppressionList
37 # Amazon SES has suppressed sending to this address because it
38 # is on the account-level suppression list.
39 #
40 # Transient/General ---------- You might be able to send a message to the same recipient
41 # in the future if the issue that caused the message to bounce
42 # is resolved.
43 # Transient/MailboxFull ------ the recipient's inbox was full.
44 # Transient/MessageTooLarge -- message you sent was too large
45 # Transient/ContentRejected -- message you sent contains content that the provider doesn't allow
46 # Transient/AttachmentRejected the message contained an unacceptable attachment
47 43 state $ReasonPair = {
48 "Suppressed" => $Sisimai::Eb::ReSTOP,
49 "OnAccountSuppressionList" => $Sisimai::Eb::ReSTOP,
50 "General" => $Sisimai::Eb::Re___1,
51 "MailboxFull" => $Sisimai::Eb::ReFULL,
52 "MessageTooLarge" => $Sisimai::Eb::ReSIZE,
53 "ContentRejected" => $Sisimai::Eb::ReBODY,
54 "AttachmentRejected" => $Sisimai::Eb::ReSAFE,
55 };
56  
57 # https://aws.amazon.com/ses/
58 1 ▶sub description { 'Amazon SES(Sending): https://aws.amazon.com/ses/' };
Subroutine description: called CC=1 SCAR=0.0
Pod documented
59 971 ▶sub inquire {
Subroutine inquire: called CC=32 SCAR=36.4
Pod documented
60 # Detect an error from Amazon SES
61 # @param [Hash] mhead Message headers of a bounce email (JSON)
62 # @param [String] mbody Message body of a bounce email
63 # @return [Hash] Bounce data list and message/rfc822 part
64 # @return [undef] failed to decode or the arguments are missing
65 # @see https://docs.aws.amazon.com/ses/latest/dg/notification-contents.html
66 # @since v4.0.2
67 971 my $class = shift;
68 971 ▶ my $mhead = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
9701
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
69 970 ▶ my $mbody = shift // return undef; return undef unless index($$mbody, "{") > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($$mbody, "{") != -1 922 47 969
Condition100%2/2
shift() // (return undef)
l!l
9691
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
70 47 ▶ return undef unless exists $mhead->{'x-amz-sns-message-id'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mhead->{"x-amz-sns-message-id"} 22 25 47
71 25 ▶ return undef unless $mhead->{'x-amz-sns-message-id'};
Branch: 1 branch
BranchTrueFalseTotal
unless $mhead->{"x-amz-sns-message-id"} 0 25 25
72  
73 25 my $proceedsto = 0;
74 25 my $sespayload = $$mbody;
75 25 while(1) {
76 # Remote the following string begins with "--"
77 # --
78 # If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
79 # https://sns.us-west-2.amazonaws.com/unsubscribe.html?SubscriptionArn=arn:aws:sns:us-west-2:1...
80 25 my $p1 = index($$mbody, "\n\n--\n");
81 25 ▶ $sespayload = substr($$mbody, 0, $p1) if $p1 > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 > 0 20 5 25
82 25 $sespayload =~ s/!\n //g;
83 25 my $p2 = index($sespayload, '"Message"');
84  
85 25 ▶ if( $p2 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p2 > 0) 5 20 25
86 # The JSON included in the email is a format like the following:
87 # {
88 # "Type" : "Notification",
89 # "MessageId" : "02f86d9b-eecf-573d-b47d-3d1850750c30",
90 # "TopicArn" : "arn:aws:sns:us-west-2:123456789012:SES-EJ-B",
91 # "Message" : "{\"notificationType\"...
92 5 $sespayload =~ s/\\//g;
93 5 my $p3 = index($sespayload, "{", $p2 + 9);
94 5 my $p4 = index($sespayload, "\n", $p2 + 9);
95 5 $sespayload = substr($sespayload, $p3, $p4 - $p3);
96 5 $sespayload =~ s/,$//g;
97 5 $sespayload =~ s/"$//g;
98 }
99 25 ▶ last if index($sespayload, "notificationType") < 0 || index($sespayload, "{") != 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($sespayload, "notificationType") == -1 or index($sespayload, "{") != 0 0 25 25
Condition33%1/3
index($sespayload, "notificationType") == -1 or index($sespayload, "{") != 0
l!l&&r!l&&!r
0025
MC/DC0%0/2
index($sespayload, "notificationType") == -1 or index($sespayload, "{") != 0
index($sespayload, "notificationType") == -1 index($sespayload, "{") != 0
Truth table33%1/3
A or B
A= index($sespayload, "notificationType") == -1B= index($sespayload, "{") != 0
ABresult
1X1
011
000
100 25 ▶ last if substr($sespayload, -1, 1) ne "}";
Branch: 1 branch
BranchTrueFalseTotal
if substr($sespayload, -1, 1) ne "}" 0 25 25
101 25 $proceedsto = 1; last;
102 }
103 25 ▶ return undef unless $proceedsto;
Branch: 1 branch
BranchTrueFalseTotal
unless $proceedsto 0 25 25
104  
105 # Load as JSON string and decode
106 25 require JSON;
107 25 my $jsonobject = undef; eval { $jsonobject = JSON->new->decode($sespayload) };
108 25 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 25 25
109 # Something wrong in decoding JSON
110 0 warn sprintf(" ***warning: Failed to decode JSON: %s", $@);
111 0 return undef;
112 }
113 25 ▶ return undef unless exists $jsonobject->{'notificationType'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $jsonobject->{"notificationType"} 0 25 25
114  
115 25 require Sisimai::RFC1123;
116 25 require Sisimai::SMTP::Reply;
117 25 require Sisimai::SMTP::Status;
118 25 require Sisimai::SMTP::Command;
119  
120 25 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
121 25 my $recipients = 0;
122 25 ▶ my $whatnotify = substr($jsonobject->{"notificationType"}, 0, 1) || "";
Condition50%1/2
substr($jsonobject->{'notificationType'}, 0, 1) || ''
l!l
250
MC/DC0%0/1
substr($jsonobject->{'notificationType'}, 0, 1) || ''
substr($jsonobject->{'notificationType'}, 0, 1)
Truth table50%1/2
A
A= substr($jsonobject->{'notificationType'}, 0, 1)
Aresult
11
00
123 25 my $feedbackid = '';
124 25 my $v = $dscontents->[-1];
125  
126 25 ▶ if( $whatnotify eq "B" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($whatnotify eq 'B') { } 10 15 25
elsif ($whatnotify eq 'C') { } 5 10 15
elsif ($whatnotify eq 'D') { } 10 0 10
127 # "notificationType":"Bounce"
128 10 my $p = $jsonobject->{"bounce"};
129 10 ▶ my $r = $p->{"bounceType"} eq "Permanent" ? "5" : "4";
Branch: 1 branch
BranchTrueFalseTotal
$p->{'bounceType'} eq 'Permanent' ? : 10 0 10
130  
131 10 for my $e ( $p->{"bouncedRecipients"}->@* ) {
132 # {"emailAddress":"neko@example.jp", "action":"failed", "status":"5.1.1", "diagnosticCode": "..."}
133 10 ▶ if( $v->{"recipient"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 0 10 10
134 # There are multiple recipient addresses in the message body.
135 0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
136 0 $v = $dscontents->[-1];
137 }
138 10 $v->{"recipient"} = $e->{"emailAddress"};
139 10 $v->{"diagnosis"} = $e->{"diagnosticCode"};
140 10 $v->{"command"} = Sisimai::SMTP::Command->find($v->{"diagnosis"});
141 10 $v->{"action"} = $e->{"action"};
142 10 $v->{"status"} = Sisimai::SMTP::Status->find($v->{"diagnosis"}, $r);
143 10 $v->{"replycode"} = Sisimai::SMTP::Reply->find($v->{"diagnosis"}, $v->{"status"});
144 10 $v->{"date"} = $p->{"timestamp"};
145 10 $v->{"lhost"} = Sisimai::RFC1123->find($p->{"reportingMTA"});
146 10 $feedbackid = $p->{"feedbackId"};
147 10 $recipients++;
148  
149 10 for my $f ( keys %$ReasonPair ) {
150 # Try to find the bounce reason by "bounceSubType"
151 70 ▶ next unless $ReasonPair->{ $f } eq $p->{"bounceSubType"};
Branch: 1 branch
BranchTrueFalseTotal
unless $ReasonPair->{$f} eq $p->{"bounceSubType"} 70 0 70
152 0 $v->{"reason"} = $f; last;
153 }
154 }
155 } elsif( $whatnotify eq "C" ) {
156 # "notificationType":"Complaint"
157 5 my $p = $jsonobject->{"complaint"}; for my $e ( $p->{"complainedRecipients"}->@* ) {
158 # {"emailAddress":"neko@example.jp"}
159 5 ▶ if( $v->{"recipient"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 0 5 5
160 # There are multiple recipient addresses in the message body.
161 0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
162 0 $v = $dscontents->[-1];
163 }
164 5 $v->{"recipient"} = $e->{"emailAddress"};
165 5 $v->{"reason"} = $Sisimai::Eb::ReFEED;
166 5 $v->{"feedbacktype"} = $p->{"complaintFeedbackType"};
167 5 $v->{"date"} = $p->{"timestamp"};
168 5 $v->{"diagnosis"} = sprintf(qq|{"feedbackid":"%s", "useragent":"%s"}|, $p->{"feedbackId"}, $p->{"userAgent"});
169 5 $feedbackid = $p->{"feedbackId"};
170 5 $recipients++;
171 }
172 } elsif( $whatnotify eq "D" ) {
173 # "notificationType":"Delivery"
174 10 my $p = $jsonobject->{"delivery"}; for my $e ( $p->{"recipients"}->@* ) {
175 # {"recipients":["neko@example.jp"]}
176 10 ▶ if( $v->{"recipient"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"recipient"}) 0 10 10
177 # There are multiple recipient addresses in the message body.
178 0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
179 0 $v = $dscontents->[-1];
180 }
181 10 $v->{"recipient"} = $e;
182 10 $v->{"reason"} = $Sisimai::Eb::ReSENT;
183 10 $v->{"action"} = $Sisimai::Eb::AeSENT;
184 10 $v->{"date"} = $p->{"timestamp"};
185 10 $v->{"lhost"} = $p->{"reportingMTA"};
186 10 $v->{"diagnosis"} = $p->{"smtpResponse"};
187 10 $v->{"status"} = Sisimai::SMTP::Status->find($v->{"diagnosis"}, "2");
188 10 $v->{"replycode"} = Sisimai::SMTP::Reply->find($v->{"diagnosis"}, "2");
189 10 $recipients++;
190 }
191 } else {
192 # Unknown "notificationType" value
193 0 warn sprintf(" ***warning: There is no notificationType field or unknown type of notificationType field");
194 0 return undef;
195 }
196 25 ▶ return undef unless $recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless $recipients 0 25 25
197  
198 # Time::Piece->strptime() cannot parse "2016-11-25T01:49:01.000Z" format
199 25 for my $e ( @$dscontents ) { s/T/ /, s/[.]\d{3}Z$// for $e->{'date'} }
200  
201 # Generate pseudo email headers as the original message
202 25 my $cv = "";
203 25 my $ch = ["date", "subject"];
204 25 my $or = $jsonobject->{'mail'};
205  
206 25 ▶ $cv = sprintf("Feedback-ID: %s\n", $feedbackid) if $feedbackid ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $feedbackid ne "" 15 10 25
207 25 map { $cv .= sprintf("%s: %s\n", $_->{"name"}, $_->{"value"}) } $or->{"headers"}->@*;
208 25 ▶ map { $cv .= sprintf("%s: %s\n", ucfirst($_), $or->{"commonHeaders"}->{ $_ }) if exists $or->{"commonHeaders"}->{ $_ } } @$ch;
Branch: 1 branch
BranchTrueFalseTotal
if exists $or->{"commonHeaders"}{$_} 25 25 50
209  
210 25 return {"ds" => $dscontents, "rfc822" => $cv};
211 }
212  
213 43 1;
214 __END__
« Sisimai/Lhost/Mimecast.pm ↑ Summary Sisimai/RFC3834.pm »