lib/Mail/DMARC/Report/Send/SMTP.pm

filter: Statementstmt 93.8% 92 / 98 Branchbran 43.7% 14 / 32 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 10 totaltotal 69.6% 131 / 188 CC 25 SCAR 14.5
CC
25
Cov
76%
assemble_too_big_message_object
12.0
get_domain_mx
6.7
human_summary
5.5
CRAP 4.3
« lib/Mail/DMARC/Report/Aggregate/Record/Auth_Results/SPF.pm ↑ Summary lib/Mail/DMARC/Result.pm »
1 package Mail::DMARC::Report::Send::SMTP;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '2.20261110';
9  
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #use Mail::Sender; # something to consider
14 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶sub get_domain_mx( $self, $domain ) {
Subroutine get_domain_mx: called CC=6 SCAR=19.1
Pod undocumented
20 3 print "getting MX for $domain\n";
21 3 my $query;
22 3 try {
23 3 $query = $self->get_resolver->send( $domain, 'MX' );
24 }
25 catch ($error) {
26 0 print "\terror: $error";
27 }
28 3 ▶ return [] if !$query;
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 3 3
29  
30 3 my @mx;
31 3 for my $rr ( $query->answer ) {
32 4 ▶ next if $rr->type ne 'MX';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "MX" 0 4 4
33 4 push @mx, { pref => $rr->preference, addr => $rr->exchange };
34 4 ▶ print $rr->exchange if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
35 }
36 3 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 3 3
37 0 print "found " . scalar @mx . "MX exchanges\n";
38 }
39 3 return \@mx;
40 }
41  
42 2 ▶sub get_smtp_hosts( $self, $email = undef ) {
Subroutine get_smtp_hosts: called CC=4 SCAR=14.0
Pod undocumented
43 2 ▶ $email or croak "missing email!";
Branch: 1 branch
BranchTrueFalseTotal
$email or croak("missing email!") 2 0 2
44  
45 2 my ($domain) = ( split /@/, $email )[-1];
46 3 my @mx = map { $_->{addr} }
47 2 sort { $a->{pref} <=> $b->{pref} } @{ $self->get_domain_mx($domain) };
48  
49 2 push @mx, $domain;
50 2 ▶ print "\tfound " . scalar @mx . " MX for $email\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
51 2 return @mx;
52 }
53  
54 12 ▶sub get_subject( $self, $agg_ref ) {
Subroutine get_subject: called CC=3 SCAR=12.3
Pod undocumented
55  
56 # RFC 9990 3.1.4
57 12 ▶ my $rid = $$agg_ref->metadata->report_id || $self->time;
Condition33%1/3
$$agg_ref->metadata->report_id || $self->time
l!l&&r!l&&!r
1200
MC/DC0%0/2
$$agg_ref->metadata->report_id || $self->time
$$agg_ref->metadata->report_id $self->time
Truth table33%1/3
A || B
A= $$agg_ref->metadata->report_idB= $self->time
ABresult
1X1
011
000
58 12 my $us = $self->config->{organization}{domain};
59 12 ▶ if ( $us eq 'example.com' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($us eq "example.com") 0 12 12
60 0 die "Please update mail-dmarc.ini";
61 }
62 12 my $pol_dom = $$agg_ref->policy_published->domain;
63 12 return "Report Domain: $pol_dom Submitter: $us Report-ID: $rid";
64 }
65  
66 12 ▶sub human_summary( $self, $agg_ref ) {
Subroutine human_summary: called CC=5 SCAR=17.1
Pod undocumented
67  
68 12 my $records = scalar @{ $$agg_ref->{record} };
69 12 my $OrgName = $self->config->{organization}{org_name};
70 my $pass = grep {
71 'pass' eq $_->{row}{policy_evaluated}{dkim}
72 || 'pass' eq $_->{row}{policy_evaluated}{spf}
73 12 ▶ } @{ $$agg_ref->{record} };
Condition33%1/3
'pass' eq $_->{'row'}{'policy_evaluated'}{'dkim'} || 'pass' eq $_->{'row'}{'policy_evaluated'}{'spf'}
l!l&&r!l&&!r
0016
MC/DC0%0/2
'pass' eq $_->{'row'}{'policy_evaluated'}{'dkim'} || 'pass' eq $_->{'row'}{'policy_evaluated'}{'spf'}
'pass' eq $_->{'row'}{'policy_evaluated'}{'dkim'} 'pass' eq $_->{'row'}{'policy_evaluated'}{'spf'}
Truth table33%1/3
A || B
A= 'pass' eq $_->{'row'}{'policy_evaluated'}{'dkim'}B= 'pass' eq $_->{'row'}{'policy_evaluated'}{'spf'}
ABresult
1X1
011
000
74 my $fail = grep {
75 'pass' ne $_->{row}{policy_evaluated}{dkim}
76 && 'pass' ne $_->{row}{policy_evaluated}{spf}
77 12 ▶ } @{ $$agg_ref->{record} };
Condition33%1/3
'pass' ne $_->{'row'}{'policy_evaluated'}{'dkim'} && 'pass' ne $_->{'row'}{'policy_evaluated'}{'spf'}
!ll&&!rl&&r
0016
MC/DC0%0/2
'pass' ne $_->{'row'}{'policy_evaluated'}{'dkim'} && 'pass' ne $_->{'row'}{'policy_evaluated'}{'spf'}
'pass' ne $_->{'row'}{'policy_evaluated'}{'dkim'} 'pass' ne $_->{'row'}{'policy_evaluated'}{'spf'}
Truth table33%1/3
A && B
A= 'pass' ne $_->{'row'}{'policy_evaluated'}{'dkim'}B= 'pass' ne $_->{'row'}{'policy_evaluated'}{'spf'}
ABresult
0X0
100
111
78 12 ▶ my $ver = $Mail::DMARC::Base::VERSION || ''; # undef in author environ
Condition50%1/2
$Mail::DMARC::Base::VERSION || ''
l!l
120
MC/DC0%0/1
$Mail::DMARC::Base::VERSION || ''
$Mail::DMARC::Base::VERSION
Truth table50%1/2
A
A= $Mail::DMARC::Base::VERSION
Aresult
11
00
79 12 ▶ my $from = $$agg_ref->{policy_published}{domain} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $from = $$agg_ref->{"policy_published"}{"domain"} or croak() 12 0 12
80  
81 return <<"EO_REPORT"
82  
83 This is a DMARC aggregate report for $from
84  
85 $records records.
86 $pass passed.
87 $fail failed.
88  
89 Submitted by $OrgName
90 Generated with Mail::DMARC $ver
91  
92 EO_REPORT
93 12 ;
94 }
95  
96 12 ▶sub get_filename( $self, $agg_ref ) {
Subroutine get_filename: called CC=2 SCAR=9.2
Pod undocumented
97  
98 # 2013 DMARC Draft, 12.2.1 Email
99 #
100 # filename = receiver "!" policy-domain "!" begin-timestamp "!"
101 # end-timestamp [ "!" unique-id ] "." extension
102 # filename="mail.receiver.example!example.com!1013662812!1013749130.gz"
103 return join( '!',
104 $self->config->{organization}{domain},
105 12 ▶ $$agg_ref->policy_published->domain,
Condition33%1/3
$$agg_ref->metadata->report_id || $self->time
l!l&&r!l&&!r
1200
MC/DC0%0/2
$$agg_ref->metadata->report_id || $self->time
$$agg_ref->metadata->report_id $self->time
Truth table33%1/3
A || B
A= $$agg_ref->metadata->report_idB= $self->time
ABresult
1X1
011
000
106 $$agg_ref->metadata->begin,
107 $$agg_ref->metadata->end,
108 $$agg_ref->metadata->report_id || $self->time,
109 ) . '.xml';
110 }
111  
112 0 ▶sub assemble_too_big_message_object( $self, $to, $body ) {
Subroutine assemble_too_big_message_object: not called CC=3 SCAR=24.8
Pod undocumented
113  
114 0 ▶ my @parts = Email::MIME->create(
Branch: 1 branch
BranchTrueFalseTotal
my(@parts) = "Email::MIME"->create("attributes", {"content_type", "text/plain", "disposition", "inline", "charset", "US-ASCII"}, "body", $body) or croak("unable to add body!") 0 0 0
115 attributes => {
116 content_type => "text/plain",
117 disposition => "inline",
118 charset => "US-ASCII",
119 },
120 body => $body,
121 ) or croak "unable to add body!";
122  
123 my $email = Email::MIME->create(
124 header_str => [
125 From => $self->config->{organization}{email},
126 0 ▶ To => $to,
Branch: 1 branch
BranchTrueFalseTotal
my $email = "Email::MIME"->create("header_str", ["From", $self->config->{"organization"}{"email"}, "To", $to, "Date", $self->get_timestamp_rfc2822, "Subject", "DMARC too big report"], "parts", [@parts]) or croak("unable to assemble message\n") 0 0 0
127 Date => $self->get_timestamp_rfc2822,
128 Subject => 'DMARC too big report',
129 ],
130 parts => [@parts],
131 ) or croak "unable to assemble message\n";
132  
133 0 return $email;
134 }
135  
136 11 ▶sub assemble_message_object( $self, $agg_ref, $to, $shrunk ) {
Subroutine assemble_message_object: called CC=5 SCAR=16.8
Pod undocumented
137  
138 11 my $filename = $self->get_filename($agg_ref);
139  
140 # WARNING: changes made here MAY affect Send::compress. Check it!
141 # my $cf = ( time > 1372662000 ) ? 'gzip' : 'zip'; # gz after 7/1/13
142 11 my $cf = 'gzip';
143 11 ▶ $filename .= $cf eq 'gzip' ? '.gz' : '.zip';
Branch: 1 branch
BranchTrueFalseTotal
$cf eq 'gzip' ? : 11 0 11
144  
145 11 ▶ my @parts = Email::MIME->create(
Branch: 1 branch
BranchTrueFalseTotal
my(@parts) = "Email::MIME"->create("attributes", {"content_type", "text/plain", "disposition", "inline", "charset", "US-ASCII"}, "body", $self->human_summary($agg_ref)) or croak("unable to add body!") 11 0 11
146 attributes => {
147 content_type => "text/plain",
148 disposition => "inline",
149 charset => "US-ASCII",
150 },
151 body => $self->human_summary($agg_ref),
152 ) or croak "unable to add body!";
153  
154 11 ▶ push @parts,
Branch: 1 branch
BranchTrueFalseTotal
push @parts, "Email::MIME"->create("attributes", {"filename", $filename, "content_type", "application/$cf", "encoding", "base64", "name", $filename}, "body", $shrunk) or croak("unable to add report!") 11 0 11
155 Email::MIME->create(
156 attributes => {
157 filename => $filename,
158 content_type => "application/$cf",
159 encoding => "base64",
160 name => $filename,
161 },
162 body => $shrunk,
163 ) or croak "unable to add report!";
164  
165 my $email = Email::MIME->create(
166 header_str => [
167 From => $self->config->{organization}{email},
168 11 ▶ To => $to,
Branch: 1 branch
BranchTrueFalseTotal
my $email = "Email::MIME"->create("header_str", ["From", $self->config->{"organization"}{"email"}, "To", $to, "Date", $self->get_timestamp_rfc2822, "Subject", $self->get_subject($agg_ref)], "parts", [@parts]) or croak("unable to assemble message\n") 11 0 11
169 Date => $self->get_timestamp_rfc2822,
170 Subject => $self->get_subject($agg_ref),
171 ],
172 parts => [@parts],
173 ) or croak "unable to assemble message\n";
174 11 $email->header_str_set( 'Message-ID' => $self->get_message_id );
175  
176 11 return $email;
177 }
178  
179 13 ▶sub get_timestamp_rfc2822( $self, @args ) {
Subroutine get_timestamp_rfc2822: called CC=2 SCAR=7.0
Pod undocumented
180 13 ▶ my @ts = @args ? @args : localtime $self->time;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 13 13
181 13 my $locale = setlocale(LC_CTYPE);
182 13 setlocale( LC_ALL, 'C' );
183 13 my $timestamp = POSIX::strftime( '%a, %d %b %Y %H:%M:%S %z', @ts );
184 13 setlocale( LC_ALL, $locale );
185 13 return $timestamp;
186 }
187  
188 42 ▶sub get_helo_hostname($self) {
Subroutine get_helo_hostname: called CC=3 SCAR=12.5
Pod undocumented
189 42 my $host = $self->config->{smtp}{hostname};
190 42 ▶ return $host if $host && $host ne 'mail.example.com';
Branch: 1 branch
BranchTrueFalseTotal
if $host and $host ne "mail.example.com" 0 42 42
Condition33%1/3
$host and $host ne "mail.example.com"
!ll&&!rl&&r
0420
MC/DC0%0/2
$host and $host ne "mail.example.com"
$host $host ne "mail.example.com"
Truth table33%1/3
A and B
A= $hostB= $host ne "mail.example.com"
ABresult
0X0
100
111
191 42 return Sys::Hostname::hostname;
192 }
193  
194 11 ▶sub get_message_id($self) {
Subroutine get_message_id: called CC=1 SCAR=0.0
Pod undocumented
195 11 my $host = $self->get_helo_hostname;
196  
197 11 my ( $ss, $mm, $hh, $mday, $mon, $year, $wday, $yday, $isdst )
198 = localtime(time);
199  
200 # Generate a "random" Message-ID
201 11 return sprintf(
202 "<%04d%02d%02d%02d%02d.%s\@%s>",
203 $year + 1900,
204 $mon + 1, $mday, $hh, $mm, rand(), $host
205 );
206 }
207  
208 4 1;
209  
210 __END__
« lib/Mail/DMARC/Report/Aggregate/Record/Auth_Results/SPF.pm ↑ Summary lib/Mail/DMARC/Result.pm »