blib/lib/Zuzu/Module/Net/SMTP.pm

filter: Statementstmt 64.1% 276 / 430 Branchbran 35.5% 79 / 222 Conditioncond 31.3% 36 / 115 MC/DCmcdc 3.1% 2 / 64 Subroutinesub 69.2% 45 / 65 Podpod 0.0% 0 / 1 totaltotal 48.8% 438 / 897 CC 169 SCAR 36.4
CC
169
Cov
51%
_smtp_auth
216.6
_type_name
110.0
_run_sendmail
110.0
CRAP 38.3
« lib/Zuzu/CLI.pm ↑ Summary blib/lib/Zuzu/Module/HTTP.pm »
1 package Zuzu::Module::Net::SMTP;
2  
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.007002';
6  
7 3 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Socket::SSL qw( SSL_VERIFY_NONE SSL_VERIFY_PEER );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IPC::Run qw( run );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 native_class
17 native_function
18 native_object
19 zuzu_bool
20 zuzu_to_perl
21 3 );
22 3 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Zuzu::Value::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Zuzu::Value::PairList;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 my @SENDMAIL_PATHS = qw(
30 /usr/sbin/sendmail
31 /usr/lib/sendmail
32 /sbin/sendmail
33 /usr/bin/sendmail
34 );
35  
36 0 ▶sub _true {
Subroutine _true: not called CC=1 SCAR=6.9
37 0 return Zuzu::Value::Boolean->new( value => 1 );
38 }
39  
40 0 ▶sub _false {
Subroutine _false: not called CC=1 SCAR=6.9
41 0 return Zuzu::Value::Boolean->new( value => 0 );
42 }
43  
44 0 ▶sub _bool {
Subroutine _bool: not called CC=2 SCAR=17.9
45 0 my ( $value ) = @_;
46  
47 0 ▶ return $value ? _true() : _false();
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
48 }
49  
50 0 ▶sub _array {
Subroutine _array: not called CC=1 SCAR=6.9
51 0 return Zuzu::Value::Array->new( items => [ @_ ] );
52 }
53  
54 0 ▶sub _type_name {
Subroutine _type_name: not called CC=10 SCAR=47.0
55 0 my ( $value ) = @_;
56  
57 0 ▶ return 'Null' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
58 0 ▶ if ( blessed($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 0 0
59 0 ▶ return 'BinaryString' if $value->isa('Zuzu::Value::BinaryString');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Zuzu::Value::BinaryString") 0 0 0
60 0 ▶ return 'PairList' if $value->isa('Zuzu::Value::PairList');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Zuzu::Value::PairList") 0 0 0
61 0 ▶ return 'Dict' if $value->isa('Zuzu::Value::Dict');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Zuzu::Value::Dict") 0 0 0
62 0 ▶ return 'Array' if $value->isa('Zuzu::Value::Array');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Zuzu::Value::Array") 0 0 0
63 0 ▶ return 'Boolean' if $value->isa('Zuzu::Value::Boolean');
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Zuzu::Value::Boolean") 0 0 0
64 0 ▶ return $value->can('class') ? $value->class->name : ref($value);
Branch: 1 branch
BranchTrueFalseTotal
$value->can('class') ? : 0 0 0
65 }
66 0 ▶ return _is_string_scalar($value) ? 'String' : 'Number';
Branch: 1 branch
BranchTrueFalseTotal
_is_string_scalar($value) ? : 0 0 0
67 }
68  
69 4 ▶sub _is_string_scalar {
Subroutine _is_string_scalar: called CC=2 SCAR=7.2
70 4 my ( $value ) = @_;
71  
72 4 ▶ return B::svref_2object( \$value )->FLAGS & B::SVf_POK() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
B::svref_2object(\$value)->FLAGS & 1024 ? : 4 0 4
73 }
74  
75 4 ▶sub _scalar_bytes {
Subroutine _scalar_bytes: called CC=3 SCAR=11.4
76 4 my ( $value ) = @_;
77  
78 4 ▶ my $text = defined $value ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 4 0 4
79 4 ▶ utf8::encode($text) if utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($text) 0 4 4
80 4 return $text;
81 }
82  
83 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
84 0 my ( $category, $message ) = @_;
85  
86 0 die Zuzu::Error->new_runtime(
87 message => "$category: $message",
88 file => '<std/net/smtp>',
89 line => 0,
90 );
91 }
92  
93 0 ▶sub _sendmail_available_path {
Subroutine _sendmail_available_path: not called CC=3 SCAR=24.8
94 0 for my $path ( @SENDMAIL_PATHS ) {
95 0 ▶ return $path if -x $path;
Branch: 1 branch
BranchTrueFalseTotal
if -x $path 0 0 0
96 }
97 0 return undef;
98 }
99  
100 0 ▶sub _capabilities {
Subroutine _capabilities: not called CC=1 SCAR=6.9
101 0 return Zuzu::Value::Dict->new(
102 map => {
103 smtp => _true(),
104 sendmail => _bool( defined _sendmail_available_path() ),
105 tls => _true(),
106 starttls => _true(),
107 auth => _array( qw( plain login xoauth2 ) ),
108 async => _true(),
109 },
110 );
111 }
112  
113 1 ▶sub _hash_from {
Subroutine _hash_from: called CC=3 SCAR=17.3
114 1 my ( $value ) = @_;
115  
116 1 ▶ return {} if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
117 0 my $perl = zuzu_to_perl($value);
118 0 ▶ return {} if ref($perl) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl ne "HASH" 0 0 0
119 0 return $perl;
120 }
121  
122 1 ▶sub _extract_config {
Subroutine _extract_config: called CC=5 SCAR=19.6
123 1 my ( $positional, $named ) = @_;
124  
125 1 ▶ my %config = %{ $named // {} };
Condition50%1/2
$named // {}
l!l
10
MC/DC0%0/1
$named // {}
$named
Truth table50%1/2
A
A= $named
Aresult
11
00
126 1 ▶ if ( @{ $positional // [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$positional // [];}) 0 1 1
Condition50%1/2
$positional // []
l!l
10
MC/DC0%0/1
$positional // []
$positional
Truth table50%1/2
A
A= $positional
Aresult
11
00
127 0 my $first = _hash_from( $positional->[0] );
128 0 for my $key ( keys %{$first} ) {
129 0 $config{$key} = $first->{$key};
130 }
131 }
132  
133 1 return \%config;
134 }
135  
136 2 ▶sub _normalise_config {
Subroutine _normalise_config: called CC=21 SCAR=36.2
137 2 my ( $raw ) = @_;
138  
139 2 ▶ my %config = %{ $raw // {} };
Condition50%1/2
$raw // {}
l!l
20
MC/DC0%0/1
$raw // {}
$raw
Truth table50%1/2
A
A= $raw
Aresult
11
00
140 $config{transport} = defined $config{transport}
141 2 ▶ ? lc "$config{transport}"
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'transport'} ? : 2 0 2
142 : 'smtp';
143 _error( 'mail.unsupported', "transport must be 'smtp' or 'sendmail'" )
144 if $config{transport} ne 'smtp'
145 2 ▶ and $config{transport} ne 'sendmail';
Branch: 1 branch
BranchTrueFalseTotal
if $config{"transport"} ne "smtp" and $config{"transport"} ne "sendmail" 0 2 2
Condition33%1/3
$config{"transport"} ne "smtp" and $config{"transport"} ne "sendmail"
!ll&&!rl&&r
200
MC/DC0%0/2
$config{"transport"} ne "smtp" and $config{"transport"} ne "sendmail"
$config{"transport"} ne "smtp" $config{"transport"} ne "sendmail"
Truth table33%1/3
A and B
A= $config{"transport"} ne "smtp"B= $config{"transport"} ne "sendmail"
ABresult
0X0
100
111
146  
147 2 my $submission = zuzu_bool( $config{submission}, 0 );
148 2 ▶ $config{host} = defined $config{host} ? "$config{host}" : 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'host'} ? : 2 0 2
149 $config{port} = defined $config{port}
150 ? int( $config{port} )
151 2 ▶ : ( $submission ? 587 : 25 );
Branch: 2 branches
BranchTrueFalseTotal
defined $config{'port'} ? : 2 0 2
$submission ? : 0 0 0
152 $config{timeout} = defined $config{timeout}
153 ? 0 + $config{timeout}
154 2 ▶ : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'timeout'} ? : 2 0 2
155 2 ▶ $config{timeout} = 30 if $config{timeout} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $config{"timeout"} <= 0 0 2 2
156 2 ▶ $config{tls} = zuzu_bool( $config{tls}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config{'tls'}, 0) ? : 0 2 2
157 $config{starttls} = exists $config{starttls}
158 2 ▶ ? ( zuzu_bool( $config{starttls}, 0 ) ? 1 : 0 )
Branch: 3 branches
BranchTrueFalseTotal
exists $config{'starttls'} ? : 1 1 2
zuzu_bool($config{'starttls'}, 0) ? : 0 1 1
$submission ? : 0 1 1
159 : ( $submission ? 1 : 0 );
160 2 ▶ $config{tls_verify} = zuzu_bool( $config{tls_verify}, 1 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config{'tls_verify'}, 1) ? : 2 0 2
161 2 ▶ $config{smtputf8} = zuzu_bool( $config{smtputf8}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config{'smtputf8'}, 0) ? : 0 2 2
162 $config{allow_insecure_auth} = zuzu_bool(
163 $config{allow_insecure_auth},
164 2 ▶ 0,
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config{'allow_insecure_auth'}, 0) ? : 0 2 2
165 ) ? 1 : 0;
166 2 ▶ $config{reject_partial} = zuzu_bool( $config{reject_partial}, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($config{'reject_partial'}, 0) ? : 0 2 2
167 $config{sendmail_path} = defined $config{sendmail_path}
168 2 ▶ ? "$config{sendmail_path}"
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'sendmail_path'} ? : 0 2 2
169 : undef;
170 $config{sendmail_args} = _validate_sendmail_args(
171 2 _string_array( $config{sendmail_args}, 'sendmail_args' ),
172 );
173  
174 2 for my $key ( qw( username password auth tls_server_name ) ) {
175 8 ▶ $config{$key} = "$config{$key}" if defined $config{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config{$key} 0 8 8
176 }
177  
178 2 return \%config;
179 }
180  
181 1 ▶sub _merge_config {
Subroutine _merge_config: called CC=3 SCAR=11.2
182 1 my ( $base, $overrides ) = @_;
183  
184 1 ▶ my %merged = %{ $base // {} };
Condition50%1/2
$base // {}
l!l
10
MC/DC0%0/1
$base // {}
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
185 1 my $extra = _hash_from($overrides);
186 1 for my $key ( keys %{$extra} ) {
187 0 $merged{$key} = $extra->{$key};
188 }
189 1 return _normalise_config( \%merged );
190 }
191  
192 2 ▶sub _string_array {
Subroutine _string_array: called CC=7 SCAR=26.4
193 2 my ( $value, $label ) = @_;
194  
195 2 ▶ return [] if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
196 1 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Array') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Array")) 0 1 1
Condition33%1/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
builtin::blessed($value) $value->isa("Zuzu::Value::Array")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
197 0 ▶ return [ map { defined $_ ? "$_" : '' } $value->resolved_items ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
198 }
199 1 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 0 1
200 0 ▶ return [ map { defined $_ ? "$_" : '' } @{$value} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
201 }
202 _error(
203 0 'mail.invalid_address',
204 "$label expects Array, got " . _type_name($value),
205 );
206 }
207  
208 2 ▶sub _validate_sendmail_args {
Subroutine _validate_sendmail_args: called CC=6 SCAR=28.1
209 2 my ( $args ) = @_;
210  
211 2 ▶ for my $arg ( @{ $args // [] } ) {
Condition50%1/2
$args // []
l!l
20
MC/DC0%0/1
$args // []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
212 0 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "--read-recipients" 0 0 0
213 'mail.unsupported',
214 "sendmail_args must not enable header-derived recipients",
215 ) if $arg eq '--read-recipients';
216 0 ▶ next if $arg !~ /\A-[^-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /\A-[^-]/ 0 0 0
217 0 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A-[A-Za-z]*t[A-Za-z]*\z/ 0 0 0
218 'mail.unsupported',
219 "sendmail_args must not enable header-derived recipients",
220 ) if $arg =~ /\A-[A-Za-z]*t[A-Za-z]*\z/;
221 }
222 2 return $args;
223 }
224  
225 1 ▶sub _reject_unsupported_security_options {
Subroutine _reject_unsupported_security_options: called CC=10 SCAR=32.8
226 1 my ( $config ) = @_;
227  
228 1 ▶ my $auth = lc( $config->{auth} // '' );
Condition50%1/2
$config->{"auth"} // ""
l!l
01
MC/DC0%0/1
$config->{"auth"} // ""
$config->{"auth"}
Truth table50%1/2
A
A= $config->{"auth"}
Aresult
11
00
229 1 ▶ if ( $auth ne '' and $auth !~ /\A(?:plain|login|xoauth2)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth ne "" and not $auth =~ /\A(?:plain|login|xoauth2)\z/) 0 1 1
Condition33%1/3
$auth ne "" and not $auth =~ /\A(?:plain|login|xoauth2)\z/
!ll&&!rl&&r
100
MC/DC0%0/2
$auth ne "" and not $auth =~ /\A(?:plain|login|xoauth2)\z/
$auth ne "" not $auth =~ /\A(?:plain|login|xoauth2)\z/
Truth table33%1/3
A and not(B)
A= $auth ne ""B= not $auth =~ /\A(?:plain|login|xoauth2)\z/
ABresult
0X0
100
111
230 0 _error( 'mail.auth', "unsupported SMTP auth mechanism '$auth'" );
231 }
232 1 ▶ if ( ( $config->{username} or $config->{password} or $auth ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"username"} || $config->{"password"} || $auth ne "" and not $config->{"tls"} and not $config->{"starttls"} and not $config->{"allow_insecure_auth"}) 0 1 1
Condition33%5/15
$config->{"username"} || $config->{"password"} || $auth ne "" and not $config->{"tls"} and not $config->{"starttls"} and not $config->{"allow_insecure_auth"}
!ll&&!rl&&r
100
$config->{"username"} || $config->{"password"} || $auth ne "" and not $config->{"tls"} and not $config->{"starttls"}
!ll&&!rl&&r
100
$config->{"username"} || $config->{"password"} || $auth ne "" and not $config->{"tls"}
!ll&&!rl&&r
100
$config->{"username"} or $config->{"password"} or $auth ne ""
l!l&&r!l&&!r
001
$config->{"username"} or $config->{"password"}
l!l&&r!l&&!r
001
MC/DC0%0/6
$config->{"username"} || $config->{"password"} || $auth ne "" and not $config->{"tls"} and not $config->{"starttls"} and not $config->{"allow_insecure_auth"}
$config->{"username"} $config->{"password"} $auth ne "" not $config->{"tls"} not $config->{"starttls"} not $config->{"allow_insecure_auth"}
Truth table7%1/13
A or B or C and not(D) and not(E) and not(F)
A= $config->{"username"}B= $config->{"password"}C= $auth ne ""D= not $config->{"tls"}E= not $config->{"starttls"}F= not $config->{"allow_insecure_auth"}
ABCDEFresult
000XXX0
1XX0XX0
01X0XX0
0010XX0
1XX10X0
01X10X0
00110X0
1XX1100
01X1100
0011100
1XX1111
01X1111
0011111
233 and !$config->{tls}
234 and !$config->{starttls}
235 and !$config->{allow_insecure_auth} ) {
236 0 _error(
237 'mail.auth',
238 'SMTP authentication without TLS requires allow_insecure_auth: true',
239 );
240 }
241 1 return;
242 }
243  
244 1 ▶sub _recipient_list {
Subroutine _recipient_list: called CC=7 SCAR=23.6
245 1 my ( $value ) = @_;
246  
247 1 my @recipients;
248 1 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Array') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa('Zuzu::Value::Array')) { } 1 0 1
elsif (ref $value eq 'ARRAY') { } 0 0 0
Condition33%1/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
builtin::blessed($value) $value->isa("Zuzu::Value::Array")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
249 1 @recipients = $value->resolved_items;
250 }
251 elsif ( ref($value) eq 'ARRAY' ) {
252 0 @recipients = @{$value};
253 }
254 else {
255 0 @recipients = ($value);
256 }
257  
258 1 ▶ @recipients = map { defined $_ ? "$_" : '' } @recipients;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 0 2
259 1 ▶ _error( 'mail.invalid_address', 'at least one envelope recipient is required' )
Branch: 1 branch
BranchTrueFalseTotal
unless @recipients 0 1 1
260 if !@recipients;
261 1 for my $recipient ( @recipients ) {
262 2 _validate_address($recipient);
263 }
264 1 return \@recipients;
265 }
266  
267 3 ▶sub _validate_address {
Subroutine _validate_address: called CC=5 SCAR=17.2
268 3 my ( $address ) = @_;
269  
270 3 ▶ $address = defined $address ? "$address" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $address ? : 3 0 3
271 3 ▶ _error( 'mail.invalid_address', 'envelope address must not be empty' )
Branch: 1 branch
BranchTrueFalseTotal
if $address eq "" 0 3 3
272 if $address eq '';
273 3 ▶ _error( 'mail.invalid_address', 'envelope address contains a control character' )
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /[\x00-\x1F\x7F]/ 0 3 3
274 if $address =~ /[\x00-\x1F\x7F]/;
275 3 ▶ _error( 'mail.invalid_address', 'envelope address must not contain angle brackets' )
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /[<>]/ 0 3 3
276 if $address =~ /[<>]/;
277 3 return $address;
278 }
279  
280 1 ▶sub _validate_envelope_ascii {
Subroutine _validate_envelope_ascii: called CC=4 SCAR=14.3
281 1 my ( $config, @addresses ) = @_;
282  
283 1 ▶ return if $config->{smtputf8};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"smtputf8"} 0 1 1
284 1 for my $address ( @addresses ) {
285 3 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /[^\x00-\x7F]/ 0 3 3
286 'mail.invalid_address',
287 'non-ASCII envelope addresses require smtputf8: true',
288 ) if $address =~ /[^\x00-\x7F]/;
289 }
290 1 return;
291 }
292  
293 4 ▶sub _header_value_bytes {
Subroutine _header_value_bytes: called CC=8 SCAR=28.3
294 4 my ( $value, $name ) = @_;
295  
296 4 my $bytes;
297 4 ▶ if ( not defined $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 4 4
elsif (builtin::blessed($value) and $value->isa('Zuzu::Value::BinaryString')) { } 0 4 4
elsif (not ref $value and _is_string_scalar($value)) { } 4 0 4
298 0 _error(
299 'mail.invalid_headers',
300 "header '$name' expects String or BinaryString, got Null",
301 );
302 }
303 ▶ elsif ( blessed($value) and $value->isa('Zuzu::Value::BinaryString') ) {
Condition33%1/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
400
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
304 0 ▶ $bytes = $value->bytes // '';
Condition0%0/2
$value->bytes // ""
l!l
00
MC/DC0%0/1
$value->bytes // ""
$value->bytes
Truth table0%0/2
A
A= $value->bytes
Aresult
11
00
305 }
306 ▶ elsif ( !ref($value) and _is_string_scalar($value) ) {
Condition33%1/3
not ref $value and _is_string_scalar($value)
!ll&&!rl&&r
004
MC/DC0%0/2
not ref $value and _is_string_scalar($value)
not ref $value _is_string_scalar($value)
Truth table33%1/3
not(A) and B
A= not ref $valueB= _is_string_scalar($value)
ABresult
0X0
100
111
307 4 $bytes = _scalar_bytes($value);
308 }
309 else {
310 0 _error(
311 'mail.invalid_headers',
312 "header '$name' expects String or BinaryString, got "
313 . _type_name($value),
314 );
315 }
316 4 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /[\r\n]/ 0 4 4
317 'mail.invalid_headers',
318 "header '$name' value must not contain CR or LF",
319 ) if $bytes =~ /[\r\n]/;
320 4 return $bytes;
321 }
322  
323 4 ▶sub _validate_header_name {
Subroutine _validate_header_name: called CC=4 SCAR=14.6
324 4 my ( $name ) = @_;
325  
326 4 ▶ $name = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 4 0 4
327 4 ▶ _error( 'mail.invalid_headers', 'header name must not be empty' )
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 4 4
328 if $name eq '';
329 4 ▶ _error( 'mail.invalid_headers', "invalid header name '$name'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[!-9;-~]+\z/ 0 4 4
330 if $name !~ /\A[!-9;-~]+\z/;
331 4 return $name;
332 }
333  
334 1 ▶sub _serialise_message {
Subroutine _serialise_message: called CC=10 SCAR=24.4
335 1 my ( $headers, $body ) = @_;
336  
337 1 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($headers) and $headers->isa("Zuzu::Value::PairList") 0 1 1
Condition33%1/3
builtin::blessed($headers) and $headers->isa("Zuzu::Value::PairList")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($headers) and $headers->isa("Zuzu::Value::PairList")
builtin::blessed($headers) $headers->isa("Zuzu::Value::PairList")
Truth table33%1/3
A and B
A= builtin::blessed($headers)B= $headers->isa("Zuzu::Value::PairList")
ABresult
0X0
100
111
338 'mail.invalid_headers',
339 'headers expects PairList, got ' . _type_name($headers),
340 ) if not blessed($headers) or not $headers->isa('Zuzu::Value::PairList');
341  
342 1 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($body) and $body->isa("Zuzu::Value::BinaryString") 0 1 1
Condition33%1/3
builtin::blessed($body) and $body->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($body) and $body->isa("Zuzu::Value::BinaryString")
builtin::blessed($body) $body->isa("Zuzu::Value::BinaryString")
Truth table33%1/3
A and B
A= builtin::blessed($body)B= $body->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
343 'TypeException',
344 'Mailer.send body expects BinaryString, got ' . _type_name($body),
345 ) if not blessed($body) or not $body->isa('Zuzu::Value::BinaryString');
346  
347 1 my $message = '';
348 1 my $message_id;
349 1 ▶ for ( my $i = 0; $i < @{ $headers->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$headers->list;}) 4 1 5
350 4 my $pair = $headers->list->[$i];
351 4 my $name = _validate_header_name( $pair->[0] );
352 4 ▶ my $value = $headers->can('_value_at')
Branch: 1 branch
BranchTrueFalseTotal
$headers->can('_value_at') ? : 4 0 4
353 ? $headers->_value_at($i)
354 : $pair->[1];
355 4 my $bytes = _header_value_bytes( $value, $name );
356 4 $message .= $name . ': ' . $bytes . "\r\n";
357 4 ▶ $message_id = $bytes
Branch: 1 branch
BranchTrueFalseTotal
if not defined $message_id and lc $name eq "message-id" 1 3 4
Condition66%2/3
not defined $message_id and lc $name eq "message-id"
!ll&&!rl&&r
031
MC/DC50%1/2
not defined $message_id and lc $name eq "message-id"
not defined $message_id lc $name eq "message-id"
Truth table66%2/3
not(A) and B
A= not defined $message_idB= lc $name eq "message-id"
ABresult
0X0
100
111
358 if not defined $message_id
359 and lc($name) eq 'message-id';
360 }
361 1 $message .= "\r\n";
362 1 ▶ $message .= $body->bytes // '';
Condition50%1/2
$body->bytes // ""
l!l
10
MC/DC0%0/1
$body->bytes // ""
$body->bytes
Truth table50%1/2
A
A= $body->bytes
Aresult
11
00
363  
364 1 return ( $message, $message_id );
365 }
366  
367 1 ▶sub _dot_stuff {
Subroutine _dot_stuff: called CC=1 SCAR=0.0
368 1 my ( $bytes ) = @_;
369  
370 1 $bytes =~ s/\A\./../;
371 1 $bytes =~ s/(\r\n)\./$1../g;
372 1 return $bytes;
373 }
374  
375 1 ▶sub _wrap_result {
Subroutine _wrap_result: called CC=3 SCAR=11.4
376 1 my ( $class_obj, $result ) = @_;
377  
378 return native_object(
379 class => $class_obj,
380 slots => {
381 transport => $result->{transport},
382 accepted => Zuzu::Value::Array->new(
383 1 ▶ items => [ @{ $result->{accepted} // [] } ],
Condition50%1/2
$result->{"accepted"} // []
l!l
10
MC/DC0%0/1
$result->{"accepted"} // []
$result->{"accepted"}
Truth table50%1/2
A
A= $result->{"accepted"}
Aresult
11
00
384 ),
385 rejected => Zuzu::Value::Array->new(
386 1 ▶ items => [ @{ $result->{rejected} // [] } ],
Condition50%1/2
$result->{"rejected"} // []
l!l
10
MC/DC0%0/1
$result->{"rejected"} // []
$result->{"rejected"}
Truth table50%1/2
A
A= $result->{"rejected"}
Aresult
11
00
387 ),
388 message_id => $result->{message_id},
389 response => $result->{response},
390 },
391 1 const => {
392 transport => 1,
393 accepted => 1,
394 rejected => 1,
395 message_id => 1,
396 response => 1,
397 },
398 );
399 }
400  
401 0 ▶sub _run_sendmail {
Subroutine _run_sendmail: not called CC=10 SCAR=47.0
402 0 my ( $config, $from, $recipients, $message, $message_id ) = @_;
403  
404 0 ▶ my $path = $config->{sendmail_path} // _sendmail_available_path();
Condition0%0/3
$config->{"sendmail_path"} // _sendmail_available_path()
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"sendmail_path"} // _sendmail_available_path()
$config->{"sendmail_path"} _sendmail_available_path()
Truth table0%0/3
A // B
A= $config->{"sendmail_path"}B= _sendmail_available_path()
ABresult
1X1
011
000
405 0 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path or $path eq "" 0 0 0
Condition0%0/3
not defined $path or $path eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $path or $path eq ""
not defined $path $path eq ""
Truth table0%0/3
not(A) or B
A= not defined $pathB= $path eq ""
ABresult
1X1
011
000
406 'mail.unsupported',
407 'sendmail transport is unavailable; configure sendmail_path',
408 ) if not defined $path or $path eq '';
409  
410 my @cmd = (
411 $path,
412 0 ▶ @{ $config->{sendmail_args} // [] },
Condition0%0/2
$config->{"sendmail_args"} // []
l!l
00
MC/DC0%0/1
$config->{"sendmail_args"} // []
$config->{"sendmail_args"}
Truth table0%0/2
A
A= $config->{"sendmail_args"}
Aresult
11
00
413 '-i',
414 '-f',
415 $from,
416 0 @{$recipients},
417 );
418 0 my ( $stdout, $stderr ) = ( '', '' );
419 0 my $ok = eval {
420 0 run( \@cmd, '<', \$message, '>', \$stdout, '2>', \$stderr );
421 0 1;
422 };
423 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
424 0 ▶ my $err = defined $@ ? "$@" : 'process launch failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $@ ? : 0 0 0
425 0 _error( 'mail.process', "sendmail failed to start: $err" );
426 }
427  
428 0 my $status = $?;
429 0 ▶ if ( $status != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 0 0
430 0 my $exit = $status >> 8;
431 0 ▶ my $diag = defined $stderr ? $stderr : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $stderr ? : 0 0 0
432 0 $diag = substr( $diag, 0, 4096 );
433 0 ▶ _error(
Branch: 1 branch
BranchTrueFalseTotal
$diag ne '' ? : 0 0 0
434 'mail.process',
435 "sendmail exited with status $exit"
436 . ( $diag ne '' ? ": $diag" : '' ),
437 );
438 }
439  
440 return {
441 transport => 'sendmail',
442 0 accepted => [ @{$recipients} ],
443 rejected => [],
444 message_id => $message_id,
445 response => 'sendmail exit 0',
446 };
447 }
448  
449 8 ▶sub _smtp_read_response {
Subroutine _smtp_read_response: called CC=6 SCAR=18.3
450 8 my ( $sock ) = @_;
451  
452 8 my @lines;
453 8 ▶ while ( defined( my $line = <$sock> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 9 0 9
454 9 $line =~ s/\r?\n\z//;
455 9 push @lines, $line;
456 9 ▶ last if $line =~ /^\d{3}\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\d{3}\s/ 8 1 9
457 1 ▶ last if $line !~ /^\d{3}-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\d{3}-/ 0 1 1
458 }
459 8 ▶ _error( 'mail.connection', 'SMTP server closed the connection' )
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 8 8
460 if !@lines;
461  
462 8 my ( $code ) = $lines[0] =~ /^(\d{3})/;
463 8 ▶ _error( 'mail.connection', 'SMTP server returned an invalid response' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 8 8
464 if not defined $code;
465  
466 8 return ( 0 + $code, \@lines );
467 }
468  
469 8 ▶sub _smtp_write {
Subroutine _smtp_write: called CC=4 SCAR=14.0
470 8 my ( $sock, $bytes ) = @_;
471  
472 8 my $offset = 0;
473 8 my $length = length($bytes);
474 8 ▶ while ( $offset < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 8 8 16
475 8 my $written = syswrite $sock, $bytes, $length - $offset, $offset;
476 8 ▶ _error( 'mail.connection', "SMTP write failed: $!" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $written 0 8 8
477 if not defined $written;
478 8 ▶ _error( 'mail.connection', 'SMTP write made no progress' )
Branch: 1 branch
BranchTrueFalseTotal
if $written == 0 0 8 8
479 if $written == 0;
480 8 $offset += $written;
481 }
482 8 return;
483 }
484  
485 6 ▶sub _smtp_command {
Subroutine _smtp_command: called CC=1 SCAR=0.0
486 6 my ( $sock, $command ) = @_;
487  
488 6 _smtp_write( $sock, $command . "\r\n" );
489 6 return _smtp_read_response($sock);
490 }
491  
492 1 ▶sub _smtp_text {
Subroutine _smtp_text: called CC=2 SCAR=7.1
493 1 my ( $lines ) = @_;
494  
495 1 ▶ return join "\n", @{ $lines // [] };
Condition50%1/2
$lines // []
l!l
10
MC/DC0%0/1
$lines // []
$lines
Truth table50%1/2
A
A= $lines
Aresult
11
00
496 }
497  
498 5 ▶sub _smtp_expect {
Subroutine _smtp_expect: called CC=2 SCAR=7.4
499 5 my ( $category, $expected, $code, $lines, $context ) = @_;
500  
501 5 ▶ return if grep { $code == $_ } @{$expected};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$code == $_;} @{$expected;} 5 0 5
502 0 _error( $category, "$context failed: " . _smtp_text($lines) );
503 }
504  
505 1 ▶sub _smtp_extensions {
Subroutine _smtp_extensions: called CC=6 SCAR=18.8
506 1 my ( $lines ) = @_;
507  
508 1 my %extensions;
509 1 ▶ for my $line ( @{ $lines // [] } ) {
Condition50%1/2
$lines // []
l!l
10
MC/DC0%0/1
$lines // []
$lines
Truth table50%1/2
A
A= $lines
Aresult
11
00
510 2 ▶ next if $line !~ /^\d{3}[- ](.+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\d{3}[- ](.+)\z/ 0 2 2
511 2 my $text = $1;
512 2 $text =~ s/^\s+//;
513 2 my ( $name ) = split /\s+/, $text, 2;
514 2 ▶ next if not defined $name or $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 2 2
Condition33%1/3
not defined $name or $name eq ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
515 2 $extensions{ uc $name } = 1;
516 }
517 1 return \%extensions;
518 }
519  
520 0 ▶sub _smtp_auth_mechanisms {
Subroutine _smtp_auth_mechanisms: not called CC=7 SCAR=40.3
521 0 my ( $lines ) = @_;
522  
523 0 my %mechanisms;
524 0 ▶ for my $line ( @{ $lines // [] } ) {
Condition0%0/2
$lines // []
l!l
00
MC/DC0%0/1
$lines // []
$lines
Truth table0%0/2
A
A= $lines
Aresult
11
00
525 0 ▶ next if $line !~ /^\d{3}[- ]AUTH(?:\s+(.+))?\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\d{3}[- ]AUTH(?:\s+(.+))?\z/i 0 0 0
526 0 ▶ my $rest = $1 // '';
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
527 0 for my $mechanism ( split /\s+/, $rest ) {
528 0 ▶ next if $mechanism eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $mechanism eq "" 0 0 0
529 0 $mechanisms{ lc $mechanism } = 1;
530 }
531 }
532 0 return \%mechanisms;
533 }
534  
535 0 ▶sub _tls_options {
Subroutine _tls_options: not called CC=3 SCAR=24.8
536 0 my ( $config ) = @_;
537  
538 0 ▶ my $verify = $config->{tls_verify} ? SSL_VERIFY_PEER : SSL_VERIFY_NONE;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'tls_verify'} ? : 0 0 0
539 0 ▶ my $name = $config->{tls_server_name} // $config->{host};
Condition0%0/3
$config->{"tls_server_name"} // $config->{"host"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"tls_server_name"} // $config->{"host"}
$config->{"tls_server_name"} $config->{"host"}
Truth table0%0/3
A // B
A= $config->{"tls_server_name"}B= $config->{"host"}
ABresult
1X1
011
000
540 return (
541 0 SSL_verify_mode => $verify,
542 SSL_hostname => $name,
543 );
544 }
545  
546 0 ▶sub _smtp_start_tls {
Subroutine _smtp_start_tls: not called CC=2 SCAR=17.9
547 0 my ( $sock, $config ) = @_;
548  
549 0 my @tls_options = _tls_options($config);
550 0 ▶ IO::Socket::SSL->start_SSL( $sock, @tls_options )
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($sock, @tls_options) or _error("mail.tls", 'STARTTLS handshake failed: ' . IO::Socket::SSL::errstr()) 0 0 0
551 or _error(
552 'mail.tls',
553 'STARTTLS handshake failed: ' . IO::Socket::SSL::errstr(),
554 );
555 0 binmode $sock;
556 0 $sock->autoflush(1);
557 0 return $sock;
558 }
559  
560 0 ▶sub _smtp_auth_command {
Subroutine _smtp_auth_command: not called CC=1 SCAR=6.9
561 0 my ( $sock, $line ) = @_;
562  
563 0 return _smtp_command( $sock, $line );
564 }
565  
566 1 ▶sub _smtp_auth {
Subroutine _smtp_auth: called CC=16 SCAR=53.8
567 1 my ( $sock, $config, $lines ) = @_;
568  
569 return if not defined $config->{username}
570 and not defined $config->{password}
571 1 ▶ and not defined $config->{auth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"username"} or defined $config->{"password"} or defined $config->{"auth"} 1 0 1
Condition33%2/6
defined $config->{"username"} or defined $config->{"password"} or defined $config->{"auth"}
l!l&&r!l&&!r
001
defined $config->{"username"} or defined $config->{"password"}
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $config->{"username"} or defined $config->{"password"} or defined $config->{"auth"}
defined $config->{"username"} defined $config->{"password"} defined $config->{"auth"}
Truth table25%1/4
A or B or C
A= defined $config->{"username"}B= defined $config->{"password"}C= defined $config->{"auth"}
ABCresult
1XX1
01X1
0011
0000
572  
573 0 ▶ my $username = $config->{username} // '';
Condition0%0/2
$config->{"username"} // ""
l!l
00
MC/DC0%0/1
$config->{"username"} // ""
$config->{"username"}
Truth table0%0/2
A
A= $config->{"username"}
Aresult
11
00
574 0 ▶ my $password = $config->{password} // '';
Condition0%0/2
$config->{"password"} // ""
l!l
00
MC/DC0%0/1
$config->{"password"} // ""
$config->{"password"}
Truth table0%0/2
A
A= $config->{"password"}
Aresult
11
00
575 0 my $mechanisms = _smtp_auth_mechanisms($lines);
576 0 ▶ my $mechanism = lc( $config->{auth} // '' );
Condition0%0/2
$config->{"auth"} // ""
l!l
00
MC/DC0%0/1
$config->{"auth"} // ""
$config->{"auth"}
Truth table0%0/2
A
A= $config->{"auth"}
Aresult
11
00
577 0 ▶ if ( $mechanism eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mechanism eq "") 0 0 0
578 0 for my $candidate ( qw( plain login xoauth2 ) ) {
579 0 ▶ if ( $mechanisms->{$candidate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mechanisms->{$candidate}) 0 0 0
580 0 $mechanism = $candidate;
581 0 last;
582 }
583 }
584 0 ▶ $mechanism = 'plain' if $mechanism eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $mechanism eq "" 0 0 0
585 }
586 0 ▶ _error( 'mail.auth', "unsupported SMTP auth mechanism '$mechanism'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $mechanism =~ /\A(?:plain|login|xoauth2)\z/ 0 0 0
587 if $mechanism !~ /\A(?:plain|login|xoauth2)\z/;
588 _error( 'mail.auth', "SMTP server did not advertise AUTH $mechanism" )
589 0 ▶ if %{$mechanisms} and !$mechanisms->{$mechanism};
Branch: 1 branch
BranchTrueFalseTotal
if %{$mechanisms;} and not $mechanisms->{$mechanism} 0 0 0
Condition0%0/3
%{$mechanisms;} and not $mechanisms->{$mechanism}
!ll&&!rl&&r
000
MC/DC0%0/2
%{$mechanisms;} and not $mechanisms->{$mechanism}
%{$mechanisms;} not $mechanisms->{$mechanism}
Truth table0%0/3
A and not(B)
A= %{$mechanisms;}B= not $mechanisms->{$mechanism}
ABresult
0X0
100
111
590  
591 0 my ( $code, $auth_lines );
592 0 ▶ if ( $mechanism eq 'plain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mechanism eq "plain") 0 0 0
593 0 my $token = encode_base64( "\0$username\0$password", '' );
594 0 ( $code, $auth_lines ) = _smtp_auth_command(
595 $sock,
596 "AUTH PLAIN $token",
597 );
598 0 _smtp_expect( 'mail.auth', [ 235 ], $code, $auth_lines, 'AUTH PLAIN' );
599 0 return;
600 }
601 0 ▶ if ( $mechanism eq 'login' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mechanism eq "login") 0 0 0
602 0 ( $code, $auth_lines ) = _smtp_auth_command( $sock, 'AUTH LOGIN' );
603 0 _smtp_expect( 'mail.auth', [ 334 ], $code, $auth_lines, 'AUTH LOGIN' );
604 0 ( $code, $auth_lines ) = _smtp_auth_command(
605 $sock,
606 encode_base64( $username, '' ),
607 );
608 0 _smtp_expect( 'mail.auth', [ 334 ], $code, $auth_lines, 'AUTH username' );
609 0 ( $code, $auth_lines ) = _smtp_auth_command(
610 $sock,
611 encode_base64( $password, '' ),
612 );
613 0 _smtp_expect( 'mail.auth', [ 235 ], $code, $auth_lines, 'AUTH password' );
614 0 return;
615 }
616 0 my $xoauth2 = "user=$username\001auth=Bearer $password\001\001";
617 0 ( $code, $auth_lines ) = _smtp_auth_command(
618 $sock,
619 'AUTH XOAUTH2 ' . encode_base64( $xoauth2, '' ),
620 );
621 0 _smtp_expect( 'mail.auth', [ 235 ], $code, $auth_lines, 'AUTH XOAUTH2' );
622 0 return;
623 }
624  
625 1 ▶sub _run_smtp {
Subroutine _run_smtp: called CC=20 SCAR=37.7
626 1 my ( $config, $from, $recipients, $message, $message_id ) = @_;
627  
628 1 my $sock;
629 1 ▶ if ( $config->{tls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'tls'}) { } 0 1 1
630 $sock = IO::Socket::SSL->new(
631 PeerHost => $config->{host},
632 PeerPort => $config->{port},
633 Proto => 'tcp',
634 Timeout => $config->{timeout},
635 0 ▶ _tls_options($config),
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::SSL"->new("PeerHost", $config->{"host"}, "PeerPort", $config->{"port"}, "Proto", "tcp", "Timeout", $config->{"timeout"}, _tls_options($config)) or _error("mail.tls", qq[could not connect to $config->{"host"}:$config->{"port"}: ] . IO::Socket::SSL::errstr()) 0 0 0
636 ) or _error(
637 'mail.tls',
638 "could not connect to $config->{host}:$config->{port}: "
639 . IO::Socket::SSL::errstr(),
640 );
641 }
642 else {
643 $sock = IO::Socket::INET->new(
644 PeerHost => $config->{host},
645 PeerPort => $config->{port},
646 Proto => 'tcp',
647 Timeout => $config->{timeout},
648 1 ▶ ) or _error(
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::INET"->new("PeerHost", $config->{"host"}, "PeerPort", $config->{"port"}, "Proto", "tcp", "Timeout", $config->{"timeout"}) or _error("mail.connection", qq[could not connect to $config->{"host"}:$config->{"port"}: $!]) 1 0 1
649 'mail.connection',
650 "could not connect to $config->{host}:$config->{port}: $!",
651 );
652 }
653 1 binmode $sock;
654 1 $sock->autoflush(1);
655 1 ▶ $sock->timeout( $config->{timeout} ) if $sock->can('timeout');
Branch: 1 branch
BranchTrueFalseTotal
if $sock->can("timeout") 1 0 1
656  
657 1 my ( $code, $lines ) = _smtp_read_response($sock);
658 1 _smtp_expect( 'mail.connection', [ 220 ], $code, $lines, 'SMTP greeting' );
659  
660 1 ▶ my $ehlo_name = hostname() || 'localhost';
Condition50%1/2
hostname() || 'localhost'
l!l
10
MC/DC0%0/1
hostname() || 'localhost'
hostname()
Truth table50%1/2
A
A= hostname()
Aresult
11
00
661 1 ( $code, $lines ) = _smtp_command( $sock, "EHLO $ehlo_name" );
662 1 my $extensions = {};
663 1 ▶ if ( $code >= 500 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 500) { } 0 1 1
664 0 ( $code, $lines ) = _smtp_command( $sock, "HELO $ehlo_name" );
665 0 _smtp_expect( 'mail.connection', [ 250 ], $code, $lines, 'HELO' );
666 }
667 else {
668 1 _smtp_expect( 'mail.connection', [ 250 ], $code, $lines, 'EHLO' );
669 1 $extensions = _smtp_extensions($lines);
670 }
671  
672 1 ▶ if ( $config->{starttls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"starttls"}) 0 1 1
673 _error( 'mail.tls', 'SMTP server did not advertise STARTTLS' )
674 0 ▶ if !$extensions->{STARTTLS};
Branch: 1 branch
BranchTrueFalseTotal
unless $extensions->{"STARTTLS"} 0 0 0
675 0 ( $code, $lines ) = _smtp_command( $sock, 'STARTTLS' );
676 0 _smtp_expect( 'mail.tls', [ 220 ], $code, $lines, 'STARTTLS' );
677 0 $sock = _smtp_start_tls( $sock, $config );
678 0 ( $code, $lines ) = _smtp_command( $sock, "EHLO $ehlo_name" );
679 0 _smtp_expect( 'mail.connection', [ 250 ], $code, $lines, 'EHLO after STARTTLS' );
680 0 $extensions = _smtp_extensions($lines);
681 }
682  
683 1 _smtp_auth( $sock, $config, $lines );
684  
685 1 ▶ if ( $config->{smtputf8} and !$extensions->{SMTPUTF8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"smtputf8"} and not $extensions->{"SMTPUTF8"}) 0 1 1
Condition33%1/3
$config->{"smtputf8"} and not $extensions->{"SMTPUTF8"}
!ll&&!rl&&r
100
MC/DC0%0/2
$config->{"smtputf8"} and not $extensions->{"SMTPUTF8"}
$config->{"smtputf8"} not $extensions->{"SMTPUTF8"}
Truth table33%1/3
A and not(B)
A= $config->{"smtputf8"}B= not $extensions->{"SMTPUTF8"}
ABresult
0X0
100
111
686 0 _error( 'mail.unsupported', 'SMTPUTF8 was requested but not advertised' );
687 }
688  
689 1 my $mail_command = "MAIL FROM:<$from>";
690 1 ▶ $mail_command .= ' SMTPUTF8' if $config->{smtputf8};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"smtputf8"} 0 1 1
691 1 ( $code, $lines ) = _smtp_command( $sock, $mail_command );
692 1 _smtp_expect( 'mail.recipient', [ 250 ], $code, $lines, 'MAIL FROM' );
693  
694 1 my @accepted;
695 my @rejected;
696 1 for my $recipient ( @{$recipients} ) {
697 2 ( $code, $lines ) = _smtp_command( $sock, "RCPT TO:<$recipient>" );
698 2 ▶ if ( $code == 250 or $code == 251 or $code == 252 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 250 or $code == 251 or $code == 252) { } 1 1 2
Condition66%4/6
$code == 250 or $code == 251 or $code == 252
l!l&&r!l&&!r
101
$code == 250 or $code == 251
l!l&&r!l&&!r
101
MC/DC33%1/3
$code == 250 or $code == 251 or $code == 252
$code == 250 $code == 251 $code == 252
Truth table50%2/4
A or B or C
A= $code == 250B= $code == 251C= $code == 252
ABCresult
1XX1
01X1
0011
0000
699 1 push @accepted, $recipient;
700 }
701 else {
702 1 push @rejected, $recipient;
703 }
704 }
705  
706 1 ▶ if ( !@accepted ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@accepted) 0 1 1
707 0 eval { _smtp_command( $sock, 'QUIT' ); 1 };
708 0 _error( 'mail.recipient', 'all recipients were rejected' );
709 }
710 1 ▶ if ( @rejected and $config->{reject_partial} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rejected and $config->{"reject_partial"}) 0 1 1
Condition33%1/3
@rejected and $config->{"reject_partial"}
!ll&&!rl&&r
010
MC/DC0%0/2
@rejected and $config->{"reject_partial"}
@rejected $config->{"reject_partial"}
Truth table33%1/3
A and B
A= @rejectedB= $config->{"reject_partial"}
ABresult
0X0
100
111
711 0 eval { _smtp_command( $sock, 'QUIT' ); 1 };
712 0 _error( 'mail.recipient', 'one or more recipients were rejected' );
713 }
714  
715 1 ( $code, $lines ) = _smtp_command( $sock, 'DATA' );
716 1 _smtp_expect( 'mail.data', [ 354 ], $code, $lines, 'DATA' );
717 1 _smtp_write( $sock, _dot_stuff($message) );
718 1 ▶ _smtp_write( $sock, "\r\n" ) if $message !~ /\r\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\r\n\z/ 0 1 1
719 1 _smtp_write( $sock, ".\r\n" );
720 1 ( $code, $lines ) = _smtp_read_response($sock);
721 1 _smtp_expect( 'mail.data', [ 250 ], $code, $lines, 'message DATA' );
722 1 my $response = _smtp_text($lines);
723  
724 1 eval { _smtp_command( $sock, 'QUIT' ); 1 };
725 1 close $sock;
726  
727 return {
728 1 transport => 'smtp',
729 accepted => \@accepted,
730 rejected => \@rejected,
731 message_id => $message_id,
732 response => $response,
733 };
734 }
735  
736 1 ▶sub _send {
Subroutine _send: called CC=2 SCAR=7.0
737 1 my ( $config, $from_value, $to_value, $headers, $body, $options ) = @_;
738  
739 1 my $send_config = _merge_config( $config, $options );
740 1 _reject_unsupported_security_options($send_config);
741 1 my $from = _validate_address($from_value);
742 1 my $recipients = _recipient_list($to_value);
743 1 _validate_envelope_ascii( $send_config, $from, @{$recipients} );
744 1 my ( $message, $message_id ) = _serialise_message( $headers, $body );
745  
746 1 ▶ if ( $send_config->{transport} eq 'sendmail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_config->{"transport"} eq "sendmail") 0 1 1
747 0 return _run_sendmail(
748 $send_config,
749 $from,
750 $recipients,
751 $message,
752 $message_id,
753 );
754 }
755  
756 1 return _run_smtp(
757 $send_config,
758 $from,
759 $recipients,
760 $message,
761 $message_id,
762 );
763 }
764  
765 0 ▶sub _async_task {
Subroutine _async_task: not called CC=1 SCAR=6.9
766 0 my ( $runtime, $name, $work, $wrap ) = @_;
767  
768 0 my $worker = $runtime->_new_task(
769 name => "$name.worker",
770 start => 1,
771 schedule => 1,
772 thunk => $work,
773 );
774 return $runtime->_new_task(
775 name => $name,
776 schedule => 1,
777 0 ▶ thunk => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
778 0 my $value = $worker->await;
779 0 ▶ return defined $wrap ? $wrap->($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $wrap ? : 0 0 0
780 },
781 0 );
782 }
783  
784 1 ▶sub _warn_blocking_operation {
Subroutine _warn_blocking_operation: called CC=2 SCAR=7.1
785 1 my ( $runtime, $operation ) = @_;
786  
787 1 ▶ $runtime->_warn_blocking_operation($operation)
Branch: 1 branch
BranchTrueFalseTotal
if $runtime->can("_warn_blocking_operation") 1 0 1
788 if $runtime->can('_warn_blocking_operation');
789 1 return;
790 }
791  
792 3 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=0.8
Pod undocumented
793 3 my ( $class, $runtime ) = @_;
794  
795 3 my $result_class = native_class(
796 name => 'MailResult',
797 );
798 my $mailer_class = native_class(
799 name => 'Mailer',
800 static_methods => {
801 capabilities => native_function(
802 name => 'capabilities',
803 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
804 0 return _capabilities();
805 },
806 3 ),
807 },
808 );
809  
810 $result_class->methods->{to_Dict} = native_function(
811 name => 'to_Dict',
812 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
813 0 my ( $self ) = @_;
814 return Zuzu::Value::Dict->new(
815 map => {
816 transport => $self->slots->{transport},
817 accepted => $self->slots->{accepted},
818 rejected => $self->slots->{rejected},
819 message_id => $self->slots->{message_id},
820 response => $self->slots->{response},
821 },
822 0 );
823 },
824 3 );
825  
826 1 ▶ $mailer_class->native_constructor( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
827 1 my ( $rt, $klass, $positional, $named ) = @_;
828 1 my $config = _normalise_config(
829 _extract_config( $positional, $named ),
830 );
831 1 return native_object(
832 class => $klass,
833 slots => {
834 _config => $config,
835 },
836 const => {
837 _config => 1,
838 },
839 );
840 3 } );
841  
842 $mailer_class->methods->{send} = native_function(
843 name => 'send',
844 1 ▶ native => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
845 1 my ( $self, $from, $to, $headers, $body, $options ) = @_;
846 1 _warn_blocking_operation( $runtime, 'std/net/smtp Mailer.send' );
847 my $result = _send(
848 $self->slots->{_config},
849 1 $from,
850 $to,
851 $headers,
852 $body,
853 $options,
854 );
855 1 return _wrap_result( $result_class, $result );
856 },
857 3 );
858  
859 $mailer_class->methods->{send_async} = native_function(
860 name => 'send_async',
861 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
862 0 my ( $self, $from, $to, $headers, $body, $options ) = @_;
863 0 ▶ my $config = { %{ $self->slots->{_config} // {} } };
Condition0%0/2
$self->slots->{"_config"} // {}
l!l
00
MC/DC0%0/1
$self->slots->{"_config"} // {}
$self->slots->{"_config"}
Truth table0%0/2
A
A= $self->slots->{"_config"}
Aresult
11
00
864 return _async_task(
865 $runtime,
866 'smtp.send_async',
867 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
868 0 return _send(
869 $config,
870 $from,
871 $to,
872 $headers,
873 $body,
874 $options,
875 );
876 },
877 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
878 0 my ( $result ) = @_;
879 0 return _wrap_result( $result_class, $result );
880 },
881 0 );
882 },
883 3 );
884  
885 return {
886 3 Mailer => $mailer_class,
887 MailResult => $result_class,
888 };
889 }
890  
891 3 1;
892  
893 =pod
894  
895 =head1 NAME
896  
897 Zuzu::Module::Net::SMTP - std/net/smtp bindings for ZuzuScript.
898  
899 =head1 DESCRIPTION
900  
901 Implements the C<std/net/smtp> runtime-supported module. This backend
902 supports safe sendmail-compatible process delivery and basic plaintext
903 SMTP relay/submission to a configured host and port. TLS, STARTTLS, and
904 authentication are reported as unsupported in this implementation.
905  
906 =head1 COPYRIGHT AND LICENCE
907  
908 B<< Zuzu::Module::Net::SMTP >> is copyright Toby Inkster.
909  
910 It is free software; you may redistribute it and/or modify it under
911 the terms of either the Artistic License 1.0 or the GNU General Public
912 License version 2.
913  
914 =cut
« lib/Zuzu/CLI.pm ↑ Summary blib/lib/Zuzu/Module/HTTP.pm »