Mail.pm

filter: Statementstmt 78.0% 181 / 232 Branchbran 57.0% 65 / 114 Conditioncond 44.8% 26 / 58 MC/DCmcdc 13.8% 5 / 36 Subroutinesub 81.0% 30 / 37 Podpod 21.4% 6 / 28 totaltotal 61.9% 313 / 505 CC 79 SCAR 30.6
CC
79
Cov
67%
make_event
69.4
process
26.8
init
16.0
CRAP 21.3
↑ Summary Mail/ESMTP/PIPELINING.pm »
1 package Net::Server::Mail;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use constant HOSTNAME => hostname();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 $Net::Server::Mail::VERSION = '0.28';
15  
16 =pod
17  
18 =head1 NAME
19  
20 Net::Server::Mail - Class to easily create a mail server
21  
22 =head1 SYNOPSIS
23  
24 use Net::Server::Mail::SMTP;
25  
26 my @local_domains = qw(example.com example.org);
27 my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 );
28
29 my $conn;
30 while($conn = $server->accept)
31 {
32 my $smtp = Net::Server::Mail::SMTP->new( socket => $conn );
33 $smtp->set_callback(RCPT => \&validate_recipient);
34 $smtp->set_callback(DATA => \&queue_message);
35 $smtp->process();
36 $conn->close();
37 }
38  
39 sub validate_recipient
40 {
41 my($session, $recipient) = @_;
42
43 my $domain;
44 if($recipient =~ /\@(.*)>\s*$/)
45 {
46 $domain = $1;
47 }
48  
49 if(not defined $domain)
50 {
51 return(0, 513, 'Syntax error.');
52 }
53 elsif(not(grep $domain eq $_, @local_domains))
54 {
55 return(0, 554, "$recipient: Recipient address rejected: Relay access denied");
56 }
57  
58 return(1);
59 }
60  
61 sub queue_message
62 {
63 my($session, $data) = @_;
64  
65 my $sender = $session->get_sender();
66 my @recipients = $session->get_recipients();
67  
68 return(0, 554, 'Error: no valid recipients')
69 unless(@recipients);
70
71 my $msgid = add_queue($sender, \@recipients, $data)
72 or return(0);
73  
74 return(1, 250, "message queued $msgid");
75 }
76  
77 =head1 DESCRIPTION
78  
79 This module is a versatile and extensible implementation of the SMTP
80 protocol and its different evolutions like ESMTP and LMTP. The event
81 driven object-oriented API makes easy to incorporate the SMTP protocol
82 to your programs.
83  
84 Other SMTPd implementations don't support useful ESMTP extensions and
85 the LMTP protocol. Their interface design precludes adding them
86 later. So I've decided to rewrite a complete implementation with
87 extensibility in mind.
88  
89 It provides mechanism to easy addition future or not yet implemented
90 ESMTP extensions. Developers can hook code at each SMTP session state
91 and change the module's behaviors by registering event call-backs. The
92 class is designed to be easily inherited from.
93  
94 This class is the base class for mail service protocols such as
95 B<Net::Server::Mail::SMTP>, B<Net::Server::Mail::ESMTP> and
96 B<Net::Server::Mail::LMTP>. Refer to the documentation provided with
97 each of these modules.
98  
99 =head1 METHODS
100  
101 =head2 new
102  
103 $instance = Net::Server::Mail->new( [option => 'value', ...] )
104  
105 options:
106  
107 =over 4
108  
109 =item handle_in
110  
111 Sets the input handle, from which the server reads data. Defaults to
112 STDIN.
113  
114 =item handle_out
115  
116 Sets the output handle, to which the server writes data. Defaults to
117 STDOUT.
118  
119 =item socket
120  
121 Sets a socket to be used for server reads and writes instead of
122 handles.
123  
124 =item error_sleep_time
125  
126 Number of seconds to wait for before printing an error message. This
127 avoids some DoS attacks that attempt to flood the server with bogus
128 commands. A value of 0 turns this feature off. Defaults to 0.
129  
130 =item idle_timeout
131  
132 Number of seconds a connection must remain idle before it is closed.
133 A value of 0 turns this feature off. Defaults to 0.
134  
135 =back
136  
137 =cut
138  
139 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
140 7 my ( $proto, @args ) = @_;
141 7 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
142 7 my $self = {};
143 7 bless( $self, $class );
144 7 return $self->init(@args);
145 }
146  
147 7 ▶sub init {
Subroutine init: called CC=8 SCAR=27.7
Pod undocumented
148 7 my $self = shift;
149 7 ▶ confess("odd number of arguments") if ( @_ % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 7 7
150 my $options = $self->{options} = {
151 7 handle_in => undef,
152 handle_out => undef,
153 socket => undef,
154 error_sleep_time => 0,
155 idle_timeout => 0,
156 };
157 7 ▶ for ( my $i = 0 ; $i < @_ ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 21 7 28
158 21 $options->{ lc( $_[$i] ) } = $_[ $i + 1 ];
159 }
160  
161 7 ▶ if ( defined $options->{handle_in} && defined $options->{handle_out} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $options->{'handle_in'} and defined $options->{'handle_out'}) { } 0 7 7
elsif (defined $options->{'socket'}) { } 7 0 7
Condition33%1/3
defined $options->{"handle_in"} and defined $options->{"handle_out"}
!ll&&!rl&&r
700
MC/DC0%0/2
defined $options->{"handle_in"} and defined $options->{"handle_out"}
defined $options->{"handle_in"} defined $options->{"handle_out"}
Truth table33%1/3
A and B
A= defined $options->{"handle_in"}B= defined $options->{"handle_out"}
ABresult
0X0
100
111
162 0 ▶ if ( UNIVERSAL::isa( $options->{handle_in}, 'IO::Handle' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options->{'handle_in'}, 'IO::Handle')) { } 0 0 0
163 0 $self->{in} = $options->{handle_in};
164 }
165 else {
166 $self->{in} =
167 0 IO::Handle->new->fdopen( fileno( $options->{handle_in} ), "r" );
168 }
169 0 ▶ if ( UNIVERSAL::isa( $options->{handle_out}, 'IO::Handle' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options->{'handle_out'}, 'IO::Handle')) { } 0 0 0
170 0 $self->{out} = $options->{handle_out};
171 }
172 else {
173 $self->{out} =
174 0 IO::Handle->new->fdopen( fileno( $options->{handle_out} ), "w" );
175 }
176 }
177 elsif ( defined $options->{'socket'} ) {
178 7 $self->{in} = $options->{'socket'};
179 7 $self->{out} = $options->{'socket'};
180 }
181 else {
182 0 $self->{in} = IO::Handle->new->fdopen( fileno(STDIN), "r" );
183 0 $self->{out} = IO::Handle->new->fdopen( fileno(STDOUT), "w" );
184 }
185  
186 7 $self->{out}->autoflush(1);
187 7 $self->{process_operation} = \&process_operation;
188  
189 7 return $self;
190 }
191  
192 =pod
193  
194 =head2 dojob
195  
196 Some commands need to do a job after the handler call. The handler
197 may want to override this to prevent the job from being executed.
198  
199 By calling this method with a (defined) false value as an argument,
200 the expected job isn't executed. Defaults to true.
201  
202 =cut
203  
204 33 ▶sub init_dojob { shift->{_dojob} = 1; }
Subroutine init_dojob: called CC=1 SCAR=0.0
Pod undocumented
205  
206 33 ▶sub dojob {
Subroutine dojob: called CC=2 SCAR=7.1
Pod documented
207 33 my ( $self, $bool ) = @_;
208 33 ▶ $self->{_dojob} = $bool if ( defined $bool );
Branch: 1 branch
BranchTrueFalseTotal
if defined $bool 0 33 33
209 33 return $self->{_dojob};
210 }
211  
212 33 ▶sub make_event {
Subroutine make_event: called CC=23 SCAR=42.4
Pod undocumented
213 33 my $self = shift;
214 33 ▶ confess('odd number of arguments') if ( @_ % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 33 33
215 33 my %args = @_;
216  
217 33 ▶ my $name = $args{'name'} || confess('missing argument: \'name\'');
Condition50%1/2
$args{'name'} || confess(q[missing argument: 'name'])
l!l
330
MC/DC0%0/1
$args{'name'} || confess(q[missing argument: 'name'])
$args{'name'}
Truth table50%1/2
A
A= $args{'name'}
Aresult
11
00
218 my $args = defined $args{'arguments'}
219 33 ▶ && ref $args{'arguments'} eq 'ARRAY' ? $args{'arguments'} : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'arguments'} && ref $args{'arguments'} eq 'ARRAY' ? : 20 13 33
Condition66%2/3
defined $args{"arguments"} and ref $args{"arguments"} eq "ARRAY"
!ll&&!rl&&r
13020
MC/DC50%1/2
defined $args{"arguments"} and ref $args{"arguments"} eq "ARRAY"
defined $args{"arguments"} ref $args{"arguments"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $args{"arguments"}B= ref $args{"arguments"} eq "ARRAY"
ABresult
0X0
100
111
220  
221 33 $self->init_dojob();
222 33 my ( $success, $code, $msg ) = $self->callback( $name, @{$args} );
223  
224 # we have to take a proper decision if successness is undefined
225 33 ▶ if ( not defined $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 0 33 33
226 0 ▶ if ( exists $args{'default_reply'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'default_reply'}) { } 0 0 0
227 0 ▶ if ( ref $args{'default_reply'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'default_reply'} eq 'ARRAY') { } 0 0 0
228 0 ( $success, $code, $msg ) = $args{'default_reply'};
229 0 ▶ $success = 0 unless defined $success;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $success 0 0 0
230 }
231 else {
232 0 $success = $args{'default_reply'};
233 }
234 }
235 else {
236 0 $success = 1; # default
237 }
238 }
239  
240 # command may have some job to do regarding to the result. handler
241 # can avoid it by calling dojob() method with a false value.
242 33 ▶ if ( $self->dojob() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dojob) 33 0 33
243 33 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 33 0 33
244 33 ▶ if ( defined $args{'on_success'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"on_success"} and ref $args{"on_success"} eq "CODE") 19 14 33
Condition66%2/3
defined $args{"on_success"} and ref $args{"on_success"} eq "CODE"
!ll&&!rl&&r
14019
MC/DC50%1/2
defined $args{"on_success"} and ref $args{"on_success"} eq "CODE"
defined $args{"on_success"} ref $args{"on_success"} eq "CODE"
Truth table66%2/3
A and B
A= defined $args{"on_success"}B= ref $args{"on_success"} eq "CODE"
ABresult
0X0
100
111
245 and ref $args{'on_success'} eq 'CODE' )
246 {
247 19 &{ $args{'on_success'} };
248 }
249 }
250 else {
251 0 ▶ if ( defined $args{'on_failure'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"on_failure"} and ref $args{"on_failure"} eq "CODE") 0 0 0
Condition0%0/3
defined $args{"on_failure"} and ref $args{"on_failure"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"on_failure"} and ref $args{"on_failure"} eq "CODE"
defined $args{"on_failure"} ref $args{"on_failure"} eq "CODE"
Truth table0%0/3
A and B
A= defined $args{"on_failure"}B= ref $args{"on_failure"} eq "CODE"
ABresult
0X0
100
111
252 and ref $args{'on_failure'} eq 'CODE' )
253 {
254 0 &{ $args{'on_failure'} };
255 }
256 }
257 }
258  
259 # ensure that a reply is sent, all SMTP command need at most 1 reply.
260 # some events such as 'stop_session' don't require sending reply.
261 33 ▶ unless ( defined $code && !$args{'no_reply'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code and not $args{"no_reply"}) 31 2 33
Condition66%2/3
defined $code and not $args{"no_reply"}
!ll&&!rl&&r
3102
MC/DC50%1/2
defined $code and not $args{"no_reply"}
defined $code not $args{"no_reply"}
Truth table66%2/3
A and not(B)
A= defined $codeB= not $args{"no_reply"}
ABresult
0X0
100
111
262 31 ▶ if ( defined $success && $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $success and $success) { } 31 0 31
Condition33%1/3
defined $success and $success
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $success and $success
defined $success $success
Truth table33%1/3
A and B
A= defined $successB= $success
ABresult
0X0
100
111
263 ( $code, $msg ) =
264 31 $self->get_default_reply( $args{'success_reply'}, 250 );
265 }
266 else {
267 ( $code, $msg ) =
268 0 $self->get_default_reply( $args{'failure_reply'}, 550 );
269 }
270 }
271  
272 33 ▶ die "return code `$code' isn't numeric"
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and $code =~ /\D/ 0 33 33
Condition33%1/3
defined $code and $code =~ /\D/
!ll&&!rl&&r
0330
MC/DC0%0/2
defined $code and $code =~ /\D/
defined $code $code =~ /\D/
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\D/
ABresult
0X0
100
111
273 if ( defined $code && $code =~ /\D/ );
274  
275 33 ▶ $self->handle_reply( $name, $success, $code, $msg )
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and length $code 31 2 33
Condition66%2/3
defined $code and length $code
!ll&&!rl&&r
0231
MC/DC50%1/2
defined $code and length $code
defined $code length $code
Truth table66%2/3
A and B
A= defined $codeB= length $code
ABresult
0X0
100
111
276 if defined $code and length $code;
277  
278 33 return $success;
279 }
280  
281 31 ▶sub get_default_reply {
Subroutine get_default_reply: called CC=4 SCAR=14.0
Pod undocumented
282 31 my ( $self, $config, $default ) = @_;
283  
284 31 my ( $code, $msg );
285 31 ▶ if ( defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) { } 30 1 31
286 30 ▶ if ( ref $config eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $config eq 'ARRAY') { } 28 2 30
elsif (not ref $config) { } 2 0 2
287 28 ( $code, $msg ) = @$config;
288 }
289 elsif ( not ref $config ) {
290 2 $code = $config;
291 }
292 else {
293 0 confess("unexpected format for reply");
294 }
295 }
296 else {
297 1 $code = $default;
298 }
299  
300 31 return ( $code, $msg );
301 }
302  
303 0 ▶sub handle_reply {
Subroutine handle_reply: not called CC=2 SCAR=17.9
Pod undocumented
304 0 my ( $self, $verb, $success, $code, $msg ) = @_;
305  
306 # don't reply anything if code is empty
307 0 ▶ $self->reply( $code, $msg ) if ( length $code );
Branch: 1 branch
BranchTrueFalseTotal
if length $code 0 0 0
308 }
309  
310 33 ▶sub callback {
Subroutine callback: called CC=3 SCAR=11.0
Pod undocumented
311 33 my ( $self, $name, @args ) = @_;
312  
313 33 ▶ if ( defined $self->{callback}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"callback"}{$name}) 2 31 33
314 2 my @rv;
315 2 eval {
316 2 my ( $code, $context ) = @{ $self->{callback}->{$name} };
317 2 $self->set_context($context);
318 2 @rv = &{$code}( $self, @args );
319 };
320 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
321 0 confess $@;
322 }
323 2 return @rv;
324 }
325  
326 31 return 1;
327 }
328  
329 2 ▶sub set_context {
Subroutine set_context: called CC=1 SCAR=0.0
Pod undocumented
330 2 my ( $self, $context ) = @_;
331 2 $self->{_context} = $context;
332 }
333  
334 0 ▶sub get_context {
Subroutine get_context: not called CC=1 SCAR=6.9
Pod undocumented
335 0 my ($self) = @_;
336 0 return $self->{_context};
337 }
338  
339 =pod
340  
341 =head2 set_callback
342  
343 ($success, $code, $msg) = $obj->set_callback(VERB, \&function, $context)>
344  
345 Sets the callback code to be called on a particular event. The function should
346 return 1 to 3 values: (success, [return_code, ["message"]]).
347  
348 $mailserver->set_callback
349 (
350 'RCPT', sub
351 {
352 my($address) = @_;
353 if(is_relayed($address))
354 {
355 # default success code/message will be used
356 return 1;
357 }
358 else
359 {
360 return(0, 513, 'Relaying denied.');
361 }
362 }
363 );
364  
365 =cut
366  
367 7 ▶sub set_callback {
Subroutine set_callback: called CC=3 SCAR=12.5
Pod documented
368 7 my ( $self, $name, $code, $context ) = @_;
369 7 ▶ confess('bad callback() invocation')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and ref $code eq "CODE" 0 7 7
Condition33%1/3
defined $code and ref $code eq "CODE"
!ll&&!rl&&r
007
MC/DC0%0/2
defined $code and ref $code eq "CODE"
defined $code ref $code eq "CODE"
Truth table33%1/3
A and B
A= defined $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
370 unless defined $code && ref $code eq 'CODE';
371 7 $self->{callback}->{$name} = [ $code, $context ];
372 }
373  
374 112 ▶sub def_verb {
Subroutine def_verb: called CC=1 SCAR=0.0
Pod undocumented
375 112 my ( $self, $verb, $coderef ) = @_;
376 112 $self->{verb}->{ uc $verb } = $coderef;
377 }
378  
379 0 ▶sub undef_verb {
Subroutine undef_verb: not called CC=2 SCAR=17.9
Pod undocumented
380 0 my ( $self, $verb ) = @_;
381 delete $self->{verb}->{$verb}
382 0 ▶ if defined $self->{verb};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"verb"} 0 0 0
383 }
384  
385 0 ▶sub list_verb {
Subroutine list_verb: not called CC=1 SCAR=6.9
Pod undocumented
386 0 my ($self) = @_;
387 0 return keys %{ $self->{verb} };
388 }
389  
390 35 ▶sub next_input_to {
Subroutine next_input_to: called CC=2 SCAR=6.9
Pod undocumented
391 35 my ( $self, $method_ref ) = @_;
392 35 ▶ $self->{next_input} = $method_ref
Branch: 1 branch
BranchTrueFalseTotal
if defined $method_ref 4 31 35
393 if ( defined $method_ref );
394 35 return $self->{next_input};
395 }
396  
397 4 ▶sub tell_next_input_method {
Subroutine tell_next_input_method: called CC=1 SCAR=0.0
Pod undocumented
398  
399 4 my ( $self, $input ) = @_;
400  
401 # calling the method and reinitialize. Note: we have to reinit
402 # before calling the code, because code can resetup this variable.
403 4 my $code = $self->{next_input};
404 4 undef $self->{next_input};
405 4 my $rv = &{$code}( $self, $input );
406 4 return $rv;
407 }
408  
409 =pod
410  
411 =head2 process
412  
413 $mailserver->process;
414  
415 Start a new session.
416  
417 =cut
418  
419 7 ▶sub process {
Subroutine process: called CC=16 SCAR=32.9
Pod documented
420 7 my ($self) = @_;
421  
422 7 my $in = $self->{in};
423 7 my $sel = IO::Select->new;
424 7 $sel->add($in);
425  
426 7 $self->banner;
427  
428 # switch to non-blocking socket to handle PIPELINING
429 # ESMTP extension. See RFC 2920 for more details.
430 7 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 7 7
431  
432 # win32 platforms don't support nonblocking IO
433 0 ioctl( $in, 2147772030, 1 );
434 }
435 else {
436 7 ▶ defined( $in->blocking(0) ) or die "Couldn't set nonblocking: $^E";
Branch: 1 branch
BranchTrueFalseTotal
defined $in->blocking(0) or die "Couldn't set nonblocking: $^E" 7 0 7
437 }
438  
439 7 my $buffer = "";
440 7 while (1) {
441  
442 # wait for data and read it
443 32 my $rv = undef;
444  
445 32 ▶ if ( $sel->can_read( $self->{options}->{idle_timeout} || undef ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->can_read($self->{'options'}{'idle_timeout'} || undef)) { } 32 0 32
Condition50%1/2
$self->{'options'}{'idle_timeout'} || undef
l!l
320
MC/DC0%0/1
$self->{'options'}{'idle_timeout'} || undef
$self->{'options'}{'idle_timeout'}
Truth table50%1/2
A
A= $self->{'options'}{'idle_timeout'}
Aresult
11
00
446 32 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 32 32
447  
448 # see how much data is available to read
449 0 my $size = pack( "L", 0 );
450 0 ioctl( $in, 1074030207, $size );
451 0 $size = unpack( "L", $size );
452  
453 # read the data to $buffer
454 0 $rv = sysread( $in, $buffer, $size, length($buffer) );
455 }
456 else {
457 32 $rv = sysread( $in, $buffer, 512 * 1024, length($buffer) );
458 }
459 }
460 else {
461 # timeout
462 0 return $self->timeout;
463 }
464  
465 # No data available at the moment
466 next
467 if ( not defined $rv
468 32 ▶ and ( $! =~ /Resource temporarily unavailable/ or $!{'EAGAIN'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rv and $! =~ /Resource temporarily unavailable/ || $!{"EAGAIN"} 0 32 32
Condition16%1/6
not defined $rv and $! =~ /Resource temporarily unavailable/ || $!{"EAGAIN"}
!ll&&!rl&&r
3200
$! =~ /Resource temporarily unavailable/ or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $rv and $! =~ /Resource temporarily unavailable/ || $!{"EAGAIN"}
not defined $rv $! =~ /Resource temporarily unavailable/ $!{"EAGAIN"}
Truth table25%1/4
not(A) and B or C
A= not defined $rvB= $! =~ /Resource temporarily unavailable/C= $!{"EAGAIN"}
ABCresult
0XX0
1000
11X1
1011
469 32 ▶ if ( ( not defined $rv ) or ( $rv == 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv or $rv == 0) 1 31 32
Condition66%2/3
not defined $rv or $rv == 0
l!l&&r!l&&!r
0131
MC/DC50%1/2
not defined $rv or $rv == 0
not defined $rv $rv == 0
Truth table66%2/3
not(A) or B
A= not defined $rvB= $rv == 0
ABresult
1X1
011
000
470  
471 # read error or connection closed
472 1 ▶ return $self->stop_session( ( not defined $rv ) ? ($!) : () );
Branch: 1 branch
BranchTrueFalseTotal
!defined($rv) ? : 0 1 1
473 }
474  
475 # process all terminated lines
476 # Note: Should accept only CRLF according to RFC. We accept
477 # plain LFs anyway because its more liberal and works as well.
478 31 my $newline_idx = rindex( $buffer, "\n" );
479 31 ▶ if ( $newline_idx >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newline_idx >= 0) 31 0 31
480  
481 # one or more lines, terminated with \r?\n
482 31 my $chunk = substr( $buffer, 0, $newline_idx + 1 );
483  
484 # remaining buffer
485 31 $buffer = substr( $buffer, $newline_idx + 1 );
486  
487 31 my $rv;
488 31 ▶ if ( defined $self->next_input_to() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->next_input_to) { } 4 27 31
489 4 $rv = $self->tell_next_input_method($chunk);
490 }
491 else {
492 27 $rv = $self->{process_operation}( $self, $chunk );
493 }
494  
495 # if $rv is defined, we have to close the connection
496 31 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 6 25 31
497 6 return $rv;
498 }
499 }
500  
501 # limit the size of lines to protect from excessive memory consumption
502 # (RFC specifies 1000 bytes including \r\n)
503 25 ▶ if ( length($buffer) > 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer > 1000) 0 25 25
504 0 $self->make_event(
505 name => 'linetobig',
506 success_reply => [ 552, 'line too long' ]
507 );
508 0 return 1;
509 }
510 }
511  
512 0 return 1;
513 }
514  
515 0 ▶sub process_once {
Subroutine process_once: not called CC=2 SCAR=17.9
Pod undocumented
516 0 my ( $self, $operation ) = @_;
517 0 ▶ if ( $self->next_input_to() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_input_to) { } 0 0 0
518 0 return $self->tell_next_input_method($operation);
519 }
520 else {
521 0 return $self->{process_operation}( $self, $operation );
522 }
523 }
524  
525 27 ▶sub process_operation {
Subroutine process_operation: called CC=3 SCAR=12.3
Pod undocumented
526 27 my ( $self, $operation ) = @_;
527 27 my ( $verb, $params ) = $self->tokenize_command($operation);
528 27 ▶ if ( defined $params && $params =~ /[\r\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and $params =~ /[\r\n]/) 0 27 27
Condition66%2/3
defined $params and $params =~ /[\r\n]/
!ll&&!rl&&r
11160
MC/DC0%0/2
defined $params and $params =~ /[\r\n]/
defined $params $params =~ /[\r\n]/
Truth table66%2/3
A and B
A= defined $paramsB= $params =~ /[\r\n]/
ABresult
0X0
100
111
529  
530 # doesn't support grouping of operations
531 0 $self->reply( 453,
532 "Command received prior to completion of"
533 . " previous command sequence" );
534 0 return;
535 }
536 27 $self->process_command( $verb, $params );
537 }
538  
539 27 ▶sub process_command {
Subroutine process_command: called CC=4 SCAR=14.7
Pod undocumented
540 27 my ( $self, $verb, $params ) = @_;
541  
542 27 ▶ if ( defined $verb && exists $self->{verb}->{$verb} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verb and exists $self->{'verb'}{$verb}) { } 27 0 27
Condition33%1/3
defined $verb and exists $self->{"verb"}{$verb}
!ll&&!rl&&r
0027
MC/DC0%0/2
defined $verb and exists $self->{"verb"}{$verb}
defined $verb exists $self->{"verb"}{$verb}
Truth table33%1/3
A and B
A= defined $verbB= exists $self->{"verb"}{$verb}
ABresult
0X0
100
111
543 27 my $action = $self->{verb}->{$verb};
544 27 my $rv;
545 27 ▶ if ( ref $action eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $action eq 'CODE') { } 6 21 27
546 6 $rv = &{ $self->{verb}->{$verb} }( $self, $params );
547 }
548 else {
549 21 $rv = $self->$action($params);
550 }
551 27 return $rv;
552 }
553 else {
554 0 $self->reply( 500, 'Syntax error: unrecognized command' );
555 0 return;
556 }
557 }
558  
559 27 ▶sub tokenize_command {
Subroutine tokenize_command: called CC=2 SCAR=7.0
Pod undocumented
560 27 my ( $self, $line ) = @_;
561 27 $line =~ s/\r?\n$//s;
562 27 $line =~ s/^\s+|\s+$//g;
563 27 my ( $verb, $params ) = split ' ', $line, 2;
564 27 ▶ $verb = uc($verb) if defined($verb);
Branch: 1 branch
BranchTrueFalseTotal
if defined $verb 27 0 27
565 27 return ( $verb, $params );
566 }
567  
568 39 ▶sub reply {
Subroutine reply: called CC=9 SCAR=22.2
Pod undocumented
569 39 my ( $self, $code, $msg ) = @_;
570 39 my $out = $self->{out};
571  
572 # tempo on error
573 sleep $self->{options}->{error_sleep_time}
574 39 ▶ if ( $code >= 400 && $self->{options}->{error_sleep_time} );
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 400 and $self->{"options"}{"error_sleep_time"} 0 39 39
Condition66%2/3
$code >= 400 and $self->{"options"}{"error_sleep_time"}
!ll&&!rl&&r
3630
MC/DC0%0/2
$code >= 400 and $self->{"options"}{"error_sleep_time"}
$code >= 400 $self->{"options"}{"error_sleep_time"}
Truth table66%2/3
A and B
A= $code >= 400B= $self->{"options"}{"error_sleep_time"}
ABresult
0X0
100
111
575  
576 # default message
577 39 ▶ $msg = $code >= 400 ? 'Failure' : 'Ok'
Branch: 2 branches
BranchTrueFalseTotal
unless defined $msg 1 38 39
$code >= 400 ? : 0 1 1
578 unless defined $msg;
579  
580 # handle multiple lines
581 39 my @lines;
582  
583 39 ▶ if ( ref $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg) { } 9 30 39
584 9 ▶ confess "bad argument" unless ref $msg eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg eq "ARRAY" 0 9 9
585 9 @lines = @$msg;
586 }
587 else {
588 30 @lines = split( /\r?\n/, $msg );
589 }
590 39 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 48 39 87
591  
592 # RFC says that all lines but the last must
593 # split the code and the message with a dash (-)
594 48 ▶ my $sep = $i == $#lines ? ' ' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$i == $#lines ? : 39 9 48
595 48 print $out "$code$sep$lines[$i]\r\n";
596 }
597 }
598  
599 20 ▶sub get_hostname {
Subroutine get_hostname: called CC=1 SCAR=0.0
Pod undocumented
600 20 my ($self) = @_;
601 20 return HOSTNAME;
602 }
603  
604 0 ▶sub get_protoname {
Subroutine get_protoname: not called CC=1 SCAR=6.9
Pod undocumented
605 0 my ($self) = @_;
606 0 return 'NOPROTO';
607 }
608  
609 7 ▶sub get_appname {
Subroutine get_appname: called CC=1 SCAR=0.0
Pod undocumented
610 7 my ($self) = @_;
611 7 return 'Net::Server::Mail (Perl)';
612 }
613  
614 ###########################################################
615  
616 =pod
617  
618 =head2 banner
619  
620 Send the introduction banner. You have to call it manually when are
621 using process_once() method. Don't use it with process() method.
622  
623 =head1 EVENTS
624  
625 =head2 banner
626  
627 Append at the opening of a new connection.
628  
629 Handler takes no argument.
630  
631 =cut
632  
633 7 ▶sub banner {
Subroutine banner: called CC=8 SCAR=22.2
Pod documented
634 7 my ($self) = @_;
635  
636 7 ▶ unless ( defined $self->{banner_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"banner_string"}) 7 0 7
637 7 ▶ my $hostname = $self->get_hostname || '';
Condition50%1/2
$self->get_hostname || ''
l!l
70
MC/DC0%0/1
$self->get_hostname || ''
$self->get_hostname
Truth table50%1/2
A
A= $self->get_hostname
Aresult
11
00
638 7 ▶ my $protoname = $self->get_protoname || '';
Condition50%1/2
$self->get_protoname || ''
l!l
70
MC/DC0%0/1
$self->get_protoname || ''
$self->get_protoname
Truth table50%1/2
A
A= $self->get_protoname
Aresult
11
00
639 7 ▶ my $appname = $self->get_appname || '';
Condition50%1/2
$self->get_appname || ''
l!l
70
MC/DC0%0/1
$self->get_appname || ''
$self->get_appname
Truth table50%1/2
A
A= $self->get_appname
Aresult
11
00
640  
641 7 my $str;
642 7 ▶ $str = $hostname . ' ' if length $hostname;
Branch: 1 branch
BranchTrueFalseTotal
if length $hostname 7 0 7
643 7 ▶ $str .= $protoname . ' ' if length $protoname;
Branch: 1 branch
BranchTrueFalseTotal
if length $protoname 7 0 7
644 7 ▶ $str .= $appname . ' ' if length $appname;
Branch: 1 branch
BranchTrueFalseTotal
if length $appname 7 0 7
645 7 $str .= 'Service ready';
646 7 $self->{banner_string} = $str;
647 }
648  
649 $self->make_event(
650 name => 'banner',
651 7 success_reply => [ 220, $self->{banner_string} ],
652 failure_reply => [ '', '' ],
653 );
654 }
655  
656 =pod
657  
658 =head2 timeout
659  
660 This event append where timeout is exceeded.
661  
662 Handler takes no argument.
663  
664 =cut
665  
666 0 ▶sub timeout {
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
667 0 my ($self) = @_;
668  
669 0 $self->make_event(
670 name => 'timeout',
671 success_reply => [
672 421,
673 $self->get_hostname
674 . ' Timeout exceeded, closing transmission channel'
675 ],
676 );
677  
678 0 return 1;
679 }
680  
681 =pod
682  
683 =head2 timeout
684  
685 This event append where connection is closed or an error occurs during reading from socket.
686  
687 Takes the error description as an argument if an error occurred and the argument is undefined if the session was closed by peer.
688  
689 $mailserver->set_callback
690 (
691 'stop_session', sub
692 {
693 my($session, $err) = @_;
694 if( defined $err )
695 {
696 print "Error occurred during processing: $err\n";
697 }
698 else
699 {
700 print "Session closed py peer\n";
701 }
702 return 1;
703 }
704 );
705  
706 =cut
707  
708 1 ▶sub stop_session {
Subroutine stop_session: called CC=1 SCAR=0.0
Pod undocumented
709 1 my ( $self, $err ) = @_;
710  
711 1 $self->make_event(
712 name => 'stop_session',
713 arguments => [$err],
714 no_reply => 1,
715 );
716  
717 1 return 1;
718 }
719  
720 =pod
721  
722 =head1 SEE ALSO
723  
724 Please, see L<Net::Server::Mail::SMTP>, L<Net::Server::Mail::ESMTP>
725 and L<Net::Server::Mail::LMTP>.
726  
727 =head1 AUTHOR
728  
729 Olivier Poitrey E<lt>rs@rhapsodyk.netE<gt>
730  
731 =head1 AVAILABILITY
732  
733 Available on CPAN.
734  
735 anonymous Git repository:
736  
737 git clone git://github.com/rs/net-server-mail.git
738  
739 Git repository on the web:
740  
741 L<https://github.com/rs/net-server-mail>
742  
743 =head1 BUGS
744  
745 Please use CPAN system to report a bug (http://rt.cpan.org/).
746  
747 =head1 LICENCE
748  
749 This library is free software; you can redistribute it and/or modify
750 it under the terms of the GNU Lesser General Public License as
751 published by the Free Software Foundation; either version 2.1 of the
752 License, or (at your option) any later version.
753  
754 This library is distributed in the hope that it will be useful, but
755 WITHOUT ANY WARRANTY; without even the implied warranty of
756 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
757 Lesser General Public License for more details.
758  
759 You should have received a copy of the GNU Lesser General Public
760 License along with this library; if not, write to the Free Software
761 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
762 USA
763  
764 =head1 COPYRIGHT
765  
766 =over
767  
768 =item Copyright (C) 2002 - Olivier Poitrey
769  
770 =item Copyright (C) 2007-2013 - Xavier Guimard
771  
772 =back
773  
774 =head2 STARTTLS
775  
776 =over
777  
778 =item Copyright (C) 2009 - Dan Moore <dan at moore.cx>
779  
780 =item Copyright (C) 2013 - Mytram <rmytram@gmail.com>
781  
782 =item Copyright (C) 2013 - Xavier Guimard <x.guimard@free.fr>
783  
784 =back
785  
786 =head2 Contributors
787  
788 =over
789  
790 =item 2012 - Georg Hoesch (patch to reduce memory consumption)
791  
792 =back
793  
794 =cut
795  
796 4 1;
↑ Summary Mail/ESMTP/PIPELINING.pm »