SMTP/Client.pm

filter: Statementstmt 70.9% 127 / 179 Branchbran 45.8% 22 / 48 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 65.9% 31 / 47 Podpod 100% 21 / 21 totaltotal 67.6% 205 / 303 CC 29 SCAR 15.9
CC
29
Cov
66%
send_mail
20.1
__ANON__
20.0
handle_line
6.3
CRAP 4.9
↑ Summary SMTP.pm »
1 package Protocol::SMTP::Client;
2 8 $Protocol::SMTP::Client::VERSION = '0.002';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Future::Utils qw(try_repeat fmap_void);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Authen::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Protocol::SMTP::Client - abstract client support for mail sending
17  
18 =head1 VERSION
19  
20 version 0.002
21  
22 =head1 DESCRIPTION
23  
24 Provides a client implementation for interacting with SMTP servers.
25  
26 =cut
27  
28 =head1 METHODS
29  
30 =head2 new
31  
32 Instantiates an SMTP client instance.
33  
34 Takes no parameters.
35  
36 =cut
37  
38 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 8 my $class = shift;
40 8 my $self = bless {@_}, $class;
41 8 $self->{auth_methods} = [];
42 8 $self->{task_queue} = [];
43 8 $self->{multi} = [];
44 8 $self
45 }
46  
47 =head2 auth_mechanism_override
48  
49 Set this on instantiation to pick a specific auth method.
50  
51 =cut
52  
53 1 ▶sub auth_mechanism_override { shift->{auth_mechanism_override} }
Subroutine auth_mechanism_override: called CC=1 SCAR=0.0
Pod documented
54  
55 =head2 login
56  
57 Attempts to log in to the server. Takes the following named parameters:
58  
59 =over 4
60  
61 =item * user - the username we're logging in with, might be your email address
62 or a plain username
63  
64 =item * pass - used for password-based auth mechanisms such as PLAIN or MD5
65  
66 =back
67  
68 Note that other auth mechanisms may provide additional fields - this will
69 mostly be determined by how L<Authen::SASL> deals with the authentication
70 process.
71  
72 Returns a L<Future> which resolves once login completes or fails.
73  
74 =cut
75  
76 2 ▶sub login {
Subroutine login: called CC=3 SCAR=14.2
Pod documented
77 2 my $self = shift;
78 2 ▶ return $self->new_future->fail('no auth?') unless my @auth = $self->auth_methods;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@auth) = $self->auth_methods 1 1 2
79  
80 1 my %args = @_;
81 1 my $auth_string = join ' ', @auth;
82 1 $self->debug_printf("Auth mechanisms [%s]", $auth_string);
83 1 ▶ $auth_string = $self->auth_mechanism_override if $self->auth_mechanism_override;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth_mechanism_override 0 1 1
84 1 my $f = $self->new_future;
85 1 ▶ $self->add_task(sub {
Subroutine __ANON__: called CC=3 SCAR=17.8
86 my $sasl = Authen::SASL->new(
87 mechanism => $auth_string,
88 callback => {
89 0 ▶ user => sub { $args{user} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ authname => sub { $args{user} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 ▶ pass => sub { $args{pass} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 },
93 1 );
94 my $client = $sasl->client_new(
95 'smtp',
96 $args{host},
97 1 0,
98 );
99  
100 1 my $rslt = $client->client_start;
101 1 ▶ $rslt = (defined($rslt) && length($rslt)) ? encode_base64($rslt, '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $rslt && length $rslt ? : 0 1 1
Condition33%1/3
defined $rslt and length $rslt
!ll&&!rl&&r
010
MC/DC0%0/2
defined $rslt and length $rslt
defined $rslt length $rslt
Truth table33%1/3
A and B
A= defined $rsltB= length $rslt
ABresult
0X0
100
111
102 1 $self->write_line(join ' ',
103 'AUTH',
104 $client->mechanism,
105 $rslt
106 );
107 0 ▶ $self->{auth_handler} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
108 0 my $code = shift;
109 0 ▶ if($code =~ /^5/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /^5/) { } 0 0 0
elsif ($code =~ /^3/) { } 0 0 0
elsif ($code =~ /2/) { } 0 0 0
110 0 delete $self->{auth_handler};
111 0 return $f->fail(shift);
112 } elsif($code =~ /^3/) {
113 0 my $data = decode_base64(shift);
114 0 $self->write_line(
115 encode_base64(
116 $client->client_step($data),
117 ''
118 )
119 );
120 } elsif($code =~ /2/) {
121 0 delete $self->{auth_handler};
122 0 $f->done
123 }
124 1 };
125 1 $f
126 1 });
127 1 $f;
128 }
129  
130  
131 =head2 send
132  
133 Attempts to send the given email.
134  
135 Expects the following named parameters:
136  
137 =over 4
138  
139 =item * to - single email address or arrayref of recipients
140  
141 =item * from - envelope sender
142  
143 =item * data - the email content itself, currently needs to be 8BITMIME
144 encoded, please raise an RT if other formats are required.
145  
146 =item * content - the email as a Perl Unicode string, this is mutually
147 exclusive with the data parameter
148  
149 =back
150  
151 Returns a L<Future> which will resolve when the send is complete.
152  
153 =cut
154  
155 0 ▶sub send {
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
156 0 my $self = shift;
157 0 my %args = @_;
158 0 my $f = $self->new_future;
159 0 ▶ $self->add_task(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 $self->send_mail(
161 %args
162 )->on_ready($f);
163 0 });
164 0 $f
165 }
166  
167 =head1 INTERNAL METHODS
168  
169 The following are used internally. They are not likely to be of much
170 use to client code, but may need to be called by implementations.
171 See L<Net::Async::SMTP> for a reference.
172  
173 =head2 new_future
174  
175 Instantiates a new L<Future>. Sometimes implementations may want
176 a L<Future> subclass which knows how to C<get>. Defaults to L<Future>.
177  
178 =cut
179  
180 6 ▶sub new_future {
Subroutine new_future: called CC=2 SCAR=6.9
Pod documented
181 6 my $factory = shift->{future_factory};
182 6 ▶ $factory ? $factory->() : Future->new;
Branch: 1 branch
BranchTrueFalseTotal
$factory ? : 4 2 6
183 }
184  
185 =head2 debug_printf
186  
187 Used internally for debugging, returns an empty list.
188  
189 =cut
190  
191 15 ▶sub debug_printf {
Subroutine debug_printf: called CC=2 SCAR=12.4
Pod documented
192 15 ▶ return unless $ENV{'PERL_SMTP_DEBUG'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"PERL_SMTP_DEBUG"} 15 0 15
193 0 my $self = shift;
194 0 my $fmt = shift;
195 0 printf "$fmt\n", @_;
196 0 return;
197 }
198  
199 =head2 write
200  
201 Uses the configured writer to send data to the remote.
202  
203 =cut
204  
205 2 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
206 2 my $self = shift;
207 2 $self->{writer}->(@_);
208 }
209  
210 =head2 have_active_task
211  
212 Returns true if we're partway through processing something.
213  
214 =cut
215  
216 5 ▶sub have_active_task { exists shift->{active_task} }
Subroutine have_active_task: called CC=1 SCAR=0.0
Pod documented
217  
218 =head2 write_line
219  
220 Frames a line appropriately for sending to a remote.
221  
222 =cut
223  
224 2 ▶sub write_line {
Subroutine write_line: called CC=3 SCAR=11.6
Pod documented
225 2 my $self = shift;
226 2 my $line = shift;
227 2 ▶ my $f = defined(wantarray) ? $self->new_future : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 2 2
228 2 $self->debug_printf("Writing %s", $line);
229 $self->write($line . "\x0D\x0A",
230 defined(wantarray)
231 0 ▶ ? (on_flush => sub { $f->done })
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 2 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 2 2
233 );
234 2 $f
235 }
236  
237 =head2 body_encoding
238  
239 Body encoding, currently hardcoded as 8BITMIME.
240  
241 =cut
242  
243 1 ▶sub body_encoding { '8BITMIME' }
Subroutine body_encoding: called CC=1 SCAR=0.0
Pod documented
244  
245 =head2 send_mail
246  
247 Sequence of writes to send email to the remote. Normally you wouldn't use
248 this directly, it'd be queued as a task by L</send>.
249  
250 =cut
251  
252 2 ▶sub send_mail {
Subroutine send_mail: called CC=5 SCAR=30.0
Pod documented
253 2 my $self = shift;
254 2 my %args = @_;
255 0 ▶ my @recipient = (ref($args{to}) eq 'ARRAY') ? @{$args{to}} : $args{to};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'to'} eq 'ARRAY' ? : 0 2 2
256  
257 2 ▶ die "Must specify either data or content" unless 1 == grep exists $args{$_}, qw(data content);
Branch: 1 branch
BranchTrueFalseTotal
unless 1 == grep(exists $args{$_}, ("data", "content")) 2 0 2
258  
259 # We accept pre-encoded data or a Perl Unicode string.
260 0 ▶ $args{data} = Encode::encode('UTF-8' => delete $args{content}) if exists $args{content};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"content"} 0 0 0
261  
262 # TODO Since our email content is not particularly heavy, and we're
263 # dealing with 8bitmime, this uses the naïve split-into-lines approach
264 # with the entire message in memory. Binary attachments, alternative
265 # encodings and larger messages are not well handled here.
266 # This is fixed by using Aliran, of course, but we don't have that option
267 # just yet.
268 0 my @mail = split /\x0D\x0A/, $args{data};
269  
270 {
271 0 my $mail_line = 'MAIL FROM:<' . $args{from} . '>';
272 0 ▶ $mail_line .= ' BODY=' . $self->body_encoding if $self->body_encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $self->body_encoding 0 0 0
273 0 $self->write_line($mail_line);
274 }
275 $self->wait_for(250)
276 0 ▶ ->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 ▶ fmap_void {
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 0 $self->write_line(q{RCPT TO:<} . shift . q{>});
279 # Each recipient line should be acknowledged with 250 if valid.
280 0 $self->wait_for(250)
281 0 } foreach => \@recipient
282 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 0 $self->write_line(q{DATA});
284 0 $self->wait_for(354)
285 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
286 0 $self->{sending_content} = 1;
287 0 ▶ (fmap_void {
Subroutine __ANON__: not called CC=2 SCAR=17.9
288 0 my $line = shift;
289 # RFC2821 section 4.5.2
290 0 ▶ $line = ".$line" if substr($line, 0, 1) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "." 0 0 0
291 0 $self->write_line($line);
292 0 ▶ } generate => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
293 0 ▶ return unless @mail;
Branch: 1 branch
BranchTrueFalseTotal
unless @mail 0 0 0
294 shift @mail
295 0 })
296 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 0 $self->{sending_content} = 0;
298 0 $self->write_line('.');
299 0 $self->wait_for(250)
300 0 });
301 }
302  
303 =head2 check_next_task
304  
305 Called internally to check whether we have any other tasks we could be doing.
306  
307 =cut
308  
309 5 ▶sub check_next_task {
Subroutine check_next_task: called CC=2 SCAR=6.9
Pod documented
310 5 my $self = shift;
311 5 ▶ return 0 unless @{$self->{task_queue}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"task_queue"};} 3 2 5
312  
313 2 my $next = shift(@{$self->{task_queue}});
314 2 $self->{active_task} = 1;
315 2 my $f = $next->();
316 1 ▶ $f->on_ready(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
317 1 delete $self->{active_task};
318 1 $self->check_next_task;
319 1 undef $f
320 2 });
321 2 return 1;
322 }
323  
324 =head2 has_feature
325  
326 Returns true if we have the given feature.
327  
328 =cut
329  
330 0 ▶sub has_feature { $_[0]->{features}{$_[1]} }
Subroutine has_feature: not called CC=1 SCAR=6.9
Pod documented
331  
332 =head2 remote_feature
333  
334 Marks the given feature from EHLO response as supported.
335  
336 Also applies AUTH values.
337  
338 =cut
339  
340 4 ▶sub remote_feature {
Subroutine remote_feature: called CC=4 SCAR=13.9
Pod documented
341 4 my $self = shift;
342 4 my ($feature, $param) = @_;
343 4 ▶ if($feature eq 'AUTH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature eq 'AUTH') { } 1 3 4
344 1 ▶ $self->{auth_methods} = [split ' ', $param] unless $self->{auth_method_override};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auth_method_override"} 1 0 1
345 } else {
346 3 ▶ $self->{features}{$feature} = $param // 1;
Condition100%2/2
$param // 1
l!l
12
MC/DC100%1/1
$param // 1
$param
Truth table100%2/2
A
A= $param
Aresult
11
00
347 }
348 }
349  
350 =head2 auth_methods
351  
352 Internal accessor, returns the list of defined authentication methods.
353  
354 =cut
355  
356 2 ▶sub auth_methods { @{shift->{auth_methods}} }
Subroutine auth_methods: called CC=1 SCAR=0.0
Pod documented
357  
358 =head2 send_greeting
359  
360 Sends the EHLO greeting and handles the resulting feature list.
361  
362 =cut
363  
364 1 ▶sub send_greeting {
Subroutine send_greeting: called CC=1 SCAR=0.0
Pod documented
365 1 my $self = shift;
366 # Start with our greeting, which should receive back a nice list of features
367 1 $self->write_line(
368 q{EHLO localhost}
369 );
370 1 ▶ $self->wait_for(250)->on_done(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
371 1 $self->{remote_domain} = shift;
372 1 for (@_) {
373 4 my ($feature, $param) = /^(\S+)(?: (.*))?$/;
374 4 $self->remote_feature($feature => $param);
375 }
376 1 });
377 }
378  
379 =head2 starttls
380  
381 Switch to TLS mode.
382  
383 =cut
384  
385 0 ▶sub starttls {
Subroutine starttls: not called CC=1 SCAR=6.9
Pod documented
386 0 my $self = shift;
387 0 $self->write_line(q{STARTTLS});
388 0 $self->wait_for(220)
389 }
390  
391 =head2 startup
392  
393 Get initial startup banner.
394  
395 =cut
396  
397 1 ▶sub startup {
Subroutine startup: called CC=1 SCAR=0.0
Pod documented
398 1 my $self = shift;
399 1 ▶ $self->wait_for(220)->on_done(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
400 1 $self->{remote_banner} = shift;
401 1 });
402 }
403  
404 =head2 wait_for
405  
406 Waits for the given status code.
407  
408 If we get something else, will mark as a failure.
409  
410 =cut
411  
412 2 ▶sub wait_for {
Subroutine wait_for: called CC=1 SCAR=0.0
Pod documented
413 2 my $self = shift;
414 2 my $code = shift;
415 2 my $f = $self->new_future;
416 2 push @{$self->{pending}}, [ $code => $f ];
417 2 $f
418 }
419  
420 =head2 handle_line
421  
422 Handle input line from remote.
423  
424 =cut
425  
426 6 ▶sub handle_line {
Subroutine handle_line: called CC=6 SCAR=18.4
Pod documented
427 6 my $self = shift;
428 6 my $line = shift;
429 6 $self->debug_printf("Received line: %s", $line);
430  
431 6 my ($code, $multi, $remainder) = $line =~ /^(\d{3})([- ])(.*)$/;
432 6 ▶ if($self->{auth_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auth_handler"}) 0 6 6
433 0 return $self->{auth_handler}->($code, $remainder);
434 }
435  
436 6 push @{$self->{multi}}, $remainder;
437  
438 6 ▶ if($multi eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi eq " ") 2 4 6
439 2 my $task = shift @{$self->{pending}};
440 2 ▶ if($task->[0] == $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->[0] == $code) { } 2 0 2
441 2 $self->debug_printf("Applying line [$_] for multi-line task") for @{$self->{multi}};
442 2 $task->[1]->done(@{$self->{multi}});
443 } else {
444 0 $self->debug_printf("We had an unexpected code - $code instead of " . $task->[0]);
445 0 $task->[1]->fail($code => $remainder, 'expected ' . $task->[0]);
446 }
447 2 $self->{multi} = [];
448 2 ▶ $self->check_next_task unless @{$self->{pending}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"pending"};} 2 0 2
449 }
450 }
451  
452 =head2 add_task
453  
454 Add another task to the queue.
455  
456 =cut
457  
458 2 ▶sub add_task {
Subroutine add_task: called CC=2 SCAR=7.0
Pod documented
459 2 my $self = shift;
460 2 my $task = shift;
461 2 push @{$self->{task_queue}}, $task;
462 2 ▶ $self->check_next_task unless $self->have_active_task;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->have_active_task 2 0 2
463 }
464  
465 8 1;
466  
467 =head1 AUTHOR
468  
469 Tom Molesworth <cpan@entitymodel.com>
470  
471 =head1 LICENSE
472  
473 Copyright Tom Molesworth 2012-2014. Licensed under the same terms as Perl itself.
↑ Summary SMTP.pm »