blib/lib/Lemonldap/NG/Common/EmailTransport.pm

filter: Statementstmt 62.5% 45 / 72 Branchbran 32.0% 16 / 50 Conditioncond 23.5% 4 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 45.7% 75 / 164 CC 32 SCAR 36.5
CC
32
Cov
47%
configTest
87.1
new
40.1
sendTestMail
12.0
CRAP 38.6
« blib/lib/Lemonldap/NG/Common/Cli.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Safelib.pm »
1 package Lemonldap::NG::Common::EmailTransport;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Email::Sender::Transport::SMTP qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Email::Sender::Simple qw(sendmail);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Email::Date::Format qw(email_date);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '2.23.3';
10  
11 # Check that SMTPAuthMech can be honored: it requires Authen::SASL and an
12 # Email::Sender version providing the sasl_authenticator attribute (>= 1.300032)
13 # Returns an error message, or undef when the configuration is usable
14 3 ▶sub checkSasl {
Subroutine checkSasl: called CC=6 SCAR=19.0
Pod undocumented
15 3 my ( $class, $conf, $transportClass ) = @_;
16 3 ▶ return undef unless $conf->{SMTPAuthMech} and $conf->{SMTPAuthUser};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"SMTPAuthMech"} and $conf->{"SMTPAuthUser"} 0 3 3
Condition33%1/3
$conf->{"SMTPAuthMech"} and $conf->{"SMTPAuthUser"}
!ll&&!rl&&r
003
MC/DC0%0/2
$conf->{"SMTPAuthMech"} and $conf->{"SMTPAuthUser"}
$conf->{"SMTPAuthMech"} $conf->{"SMTPAuthUser"}
Truth table33%1/3
A and B
A= $conf->{"SMTPAuthMech"}B= $conf->{"SMTPAuthUser"}
ABresult
0X0
100
111
17 3 ▶ $transportClass ||= 'Email::Sender::Transport::SMTP';
Condition100%2/2
$transportClass ||= "Email::Sender::Transport::SMTP"
l!l
21
MC/DC100%1/1
$transportClass ||= "Email::Sender::Transport::SMTP"
$transportClass
Truth table100%2/2
A
A= $transportClass
Aresult
11
00
18 3 ▶ return "Choosing the SASL mechanism (SMTPAuthMech) is not supported by "
Branch: 1 branch
BranchTrueFalseTotal
unless $transportClass->can("sasl_authenticator") 0 3 3
19 . "$transportClass, Email::Sender 1.300032 or higher is required"
20 unless $transportClass->can('sasl_authenticator');
21 3 eval { require Authen::SASL; };
22 3 ▶ return "Choosing the SASL mechanism (SMTPAuthMech) requires Authen::SASL"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
23 if $@;
24 3 return undef;
25 }
26  
27 # Build the SASL related arguments given to the transport constructor.
28 # When SMTPAuthMech is set, an Authen::SASL object restricted to the wanted
29 # mechanism(s) is used instead of sasl_username/sasl_password: else Net::SMTP
30 # builds itself a SASL client using all the mechanisms advertised by the
31 # server, and picks the "strongest" one, which may be broken server side
32 # (DIGEST-MD5 on some providers for example).
33 # NB: sasl_authenticator and sasl_username are mutually exclusive.
34 6 ▶sub _saslArgs {
Subroutine _saslArgs: called CC=4 SCAR=13.9
35 6 my ( $transportClass, $conf ) = @_;
36 6 ▶ return () unless $conf->{SMTPAuthUser};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"SMTPAuthUser"} 2 4 6
37 4 ▶ if ( $conf->{SMTPAuthMech} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"SMTPAuthMech"}) 2 2 4
38 2 my $error = __PACKAGE__->checkSasl( $conf, $transportClass );
39 2 ▶ die "$error\n" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2 2
40 return (
41 sasl_authenticator => Authen::SASL->new(
42 mechanism => $conf->{SMTPAuthMech},
43 callback => {
44 user => $conf->{SMTPAuthUser},
45 authname => $conf->{SMTPAuthUser},
46 pass => $conf->{SMTPAuthPass},
47 },
48 )
49 2 );
50 }
51 return (
52 sasl_username => $conf->{SMTPAuthUser},
53 sasl_password => $conf->{SMTPAuthPass},
54 2 );
55 }
56  
57 7 ▶sub new {
Subroutine new: called CC=11 SCAR=36.9
Pod undocumented
58 7 my ( $class, $conf ) = @_;
59 7 my $transport;
60 7 my $smtpTls = $conf->{SMTPTLS};
61 return undef
62 7 ▶ unless ( $conf->{SMTPServer} );
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"SMTPServer"} 1 6 7
63 6 ▶ if ( $smtpTls
Branch: 1 branch
BranchTrueFalseTotal
if ($smtpTls and $Email::Sender::Transport::SMTP::VERSION < "1.300027") 0 6 6
Condition33%1/3
$smtpTls and $Email::Sender::Transport::SMTP::VERSION < "1.300027"
!ll&&!rl&&r
600
MC/DC0%0/2
$smtpTls and $Email::Sender::Transport::SMTP::VERSION < "1.300027"
$smtpTls $Email::Sender::Transport::SMTP::VERSION < "1.300027"
Truth table33%1/3
A and B
A= $smtpTlsB= $Email::Sender::Transport::SMTP::VERSION < "1.300027"
ABresult
0X0
100
111
64 and $Email::Sender::Transport::SMTP::VERSION < 1.300027 )
65 {
66 # Try to use Email::Sender::Transport::SMTPS
67 0 eval { require Email::Sender::Transport::SMTPS; };
68  
69 # fall back to Email::Sender::Transport::SMTP if not available
70 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
71 $transport = Email::Sender::Transport::SMTPS->new(
72 host => $conf->{SMTPServer},
73 0 ▶ ( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'SMTPPort'} ? : 0 0 0
74 _saslArgs( 'Email::Sender::Transport::SMTPS', $conf ),
75 ssl => $smtpTls,
76 );
77 0 return $transport;
78 }
79 else {
80 0 ▶ if ( $smtpTls and $smtpTls eq "ssl" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smtpTls and $smtpTls eq 'ssl') { } 0 0 0
Condition0%0/3
$smtpTls and $smtpTls eq "ssl"
!ll&&!rl&&r
000
MC/DC0%0/2
$smtpTls and $smtpTls eq "ssl"
$smtpTls $smtpTls eq "ssl"
Truth table0%0/3
A and B
A= $smtpTlsB= $smtpTls eq "ssl"
ABresult
0X0
100
111
81 0 $smtpTls = 1;
82 }
83 else {
84 0 $smtpTls = 0;
85 }
86 }
87 }
88 $transport = Email::Sender::Transport::SMTP->new(
89 host => $conf->{SMTPServer},
90 ( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ),
91 _saslArgs( 'Email::Sender::Transport::SMTP', $conf ),
92 ( $smtpTls ? ( ssl => $smtpTls ) : () ),
93 (
94 $conf->{SMTPTLSOpts}
95 ? ( ssl_options => $conf->{SMTPTLSOpts} )
96 6 ▶ : ()
Branch: 3 branches
BranchTrueFalseTotal
$conf->{'SMTPPort'} ? : 6 0 6
$smtpTls ? : 0 6 6
$conf->{'SMTPTLSOpts'} ? : 0 6 6
97 ),
98 );
99 6 return $transport;
100 }
101  
102 1 ▶sub configTest {
Subroutine configTest: called CC=12 SCAR=44.7
Pod undocumented
103 1 my ( $class, $conf ) = @_;
104 1 my $res = 1;
105 1 my $message;
106 1 ▶ if ( $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Email::Sender::Transport::SMTP::VERSION < "1.300027") 0 1 1
107 0 ▶ if ( $conf->{SMTPTLS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"SMTPTLS"}) 0 0 0
108 0 $message = "Email::Sender < 1.3.00027 does not validate"
109 . " server identity when using SMTPS, use at your own risks";
110 }
111 0 ▶ if ( $conf->{SMTPTLSOpts} and keys %{ $conf->{SMTPTLSOpts} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"SMTPTLSOpts"} and keys %{$$conf{"SMTPTLSOpts"};}) 0 0 0
Condition0%0/3
$conf->{"SMTPTLSOpts"} and keys %{$$conf{"SMTPTLSOpts"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->{"SMTPTLSOpts"} and keys %{$$conf{"SMTPTLSOpts"};}
$conf->{"SMTPTLSOpts"} keys %{$$conf{"SMTPTLSOpts"};}
Truth table0%0/3
A and B
A= $conf->{"SMTPTLSOpts"}B= keys %{$$conf{"SMTPTLSOpts"};}
ABresult
0X0
100
111
112 0 ▶ $message =
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
113 ( $message ? "$message. " : "" )
114 . "Setting TLS parameters is not supported on "
115 . "Email::Sender < 1.3.00027";
116 }
117 0 eval { require Email::Sender::Transport::SMTPS; };
118 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
119 0 ▶ if ( $conf->{SMTPTLS} and $conf->{SMTPTLS} eq "starttls" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"SMTPTLS"} and $conf->{"SMTPTLS"} eq "starttls") 0 0 0
Condition0%0/3
$conf->{"SMTPTLS"} and $conf->{"SMTPTLS"} eq "starttls"
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->{"SMTPTLS"} and $conf->{"SMTPTLS"} eq "starttls"
$conf->{"SMTPTLS"} $conf->{"SMTPTLS"} eq "starttls"
Truth table0%0/3
A and B
A= $conf->{"SMTPTLS"}B= $conf->{"SMTPTLS"} eq "starttls"
ABresult
0X0
100
111
120 0 ▶ $message =
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
121 ( $message ? "$message. " : "" )
122 . "StartTLS is not supported, "
123 . "install Email::Sender::Transport::SMTPS";
124 }
125 }
126 }
127 1 ▶ if ( my $error = $class->checkSasl($conf) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $class->checkSasl($conf)) 0 1 1
128 0 ▶ $message = ( $message ? "$message. " : "" ) . $error;
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
129 }
130 1 return ( $res, $message );
131 }
132  
133 0 ▶sub sendTestMail {
Subroutine sendTestMail: not called CC=3 SCAR=24.8
Pod undocumented
134 0 my ( $conf, $dest ) = @_;
135 0 my $transport = Lemonldap::NG::Common::EmailTransport->new($conf);
136 my $message = MIME::Entity->build(
137 From => $conf->{mailFrom},
138 0 To => $dest,
139 Subject => 'LemonLDAP::NG test email',
140 Type => 'TEXT',
141 Data => 'This test message was sent from the LemonLDAP::NG Manager',
142 Type => 'text/plain',
143 Date => email_date,
144 );
145  
146 # Send the mail
147 0 eval { sendmail( $message->stringify, { transport => $transport } ); };
148 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
149 0 ▶ my $error = ( $@->isa('Throwable::Error') ? $@->message : $@ );
Branch: 1 branch
BranchTrueFalseTotal
$@->isa('Throwable::Error') ? : 0 0 0
150 0 die $error;
151 }
152 }
153  
154 1 1;
« blib/lib/Lemonldap/NG/Common/Cli.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Safelib.pm »