Mail/ESMTP.pm

filter: Statementstmt 70.6% 65 / 92 Branchbran 35.7% 10 / 28 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.3% 15 / 18 Podpod 9.0% 1 / 11 totaltotal 55.8% 96 / 172 CC 27 SCAR 19.3
CC
27
Cov
60%
sub_option
20.0
register
10.6
handle_options
10.0
CRAP 6.9
« Mail/ESMTP/STARTTLS.pm ↑ Summary Mail/LMTP.pm »
1 package Net::Server::Mail::ESMTP;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base qw(Net::Server::Mail::SMTP);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = "0.26";
10  
11 =pod
12  
13 =head1 NAME
14  
15 Net::Server::Mail::ESMTP - A module to implement the ESMTP protocol
16  
17 =head1 SYNOPSIS
18  
19 use Net::Server::Mail::ESMTP;
20  
21 my @local_domains = qw(example.com example.org);
22 my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 );
23  
24 my $conn;
25 while($conn = $server->accept)
26 {
27 my $esmtp = Net::Server::Mail::ESMTP->new( socket => $conn );
28 # activate some extensions
29 $esmtp->register('Net::Server::Mail::ESMTP::8BITMIME');
30 $esmtp->register('Net::Server::Mail::ESMTP::PIPELINING');
31 # adding some handlers
32 $esmtp->set_callback(RCPT => \&validate_recipient);
33 $esmtp->set_callback(DATA => \&queue_message);
34 $esmtp->process();
35 $conn->close();
36 }
37  
38 sub validate_recipient
39 {
40 my($session, $recipient) = @_;
41  
42 my $domain;
43 if($recipient =~ /\@(.*)>\s*$/)
44 {
45 $domain = $1;
46 }
47  
48 if(not defined $domain)
49 {
50 return(0, 513, 'Syntax error.');
51 }
52 elsif(not(grep $domain eq $_, @local_domains))
53 {
54 return(0, 554, "$recipient: Recipient address rejected: Relay access denied");
55 }
56  
57 return(1);
58 }
59  
60 sub queue_message
61 {
62 my($session, $data) = @_;
63  
64 my $sender = $session->get_sender();
65 my @recipients = $session->get_recipients();
66  
67 return(0, 554, 'Error: no valid recipients')
68 unless(@recipients);
69  
70 my $msgid = add_queue($sender, \@recipients, $data)
71 or return(0);
72  
73 return(1, 250, "message queued $msgid");
74 }
75  
76 =head1 DESCRIPTION
77  
78 This class implement the ESMTP (RFC 2821) protocol.
79  
80 This class inherit from Net::Server::Mail::SMTP. Please see
81 L<Net::Server::Mail::SMTP> for documentation of common methods.
82  
83 =head1 METHODS
84  
85 ESMTP specific methods.
86  
87 =cut
88  
89 7 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
90 7 my ( $self, @args ) = @_;
91 7 my $rv = $self->SUPER::init(@args);
92 7 ▶ return $rv unless $rv eq $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv eq $self 0 7 7
93  
94 7 $self->def_verb( EHLO => 'ehlo' );
95  
96 7 $self->{extend_mode} = 0;
97  
98 7 return $self;
99 }
100  
101 7 ▶sub get_protoname {
Subroutine get_protoname: called CC=1 SCAR=0.0
Pod undocumented
102 7 return 'ESMTP';
103 }
104  
105 9 ▶sub get_extensions {
Subroutine get_extensions: called CC=2 SCAR=7.1
Pod undocumented
106 9 my ($self) = @_;
107 9 ▶ return ( @{ $self->{extensions} || [] } );
Condition50%1/2
$self->{'extensions'} || []
l!l
90
MC/DC0%0/1
$self->{'extensions'} || []
$self->{'extensions'}
Truth table50%1/2
A
A= $self->{'extensions'}
Aresult
11
00
108 }
109  
110 =pod
111  
112 =head2 register
113  
114 Activate an ESMTP extension. This method takes a module's name as
115 argument. This module must implement certain methods. See
116 L<Net::Server::Mail::ESMTP::Extension> for more details.
117  
118 =cut
119  
120 7 ▶sub register {
Subroutine register: called CC=9 SCAR=23.6
Pod documented
121 7 my ( $self, $class ) = @_;
122  
123 # try to import class
124 7 ▶ eval "require $class" or croak("can't register module `$class'");
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or croak("can't register module `${class}'") 7 0 7
125  
126 # test mandatory methods
127 7 foreach my $method (qw(new verb keyword parameter option reply)) {
128 42 ▶ confess(
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 42 42
129 "Extension class `$class' doesn't implement mandatory method `$method'"
130 ) unless ( $class->can($method) );
131 }
132  
133 7 ▶ my $extend = $class->new($self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $extend = $class->new($self) or return 7 0 7
134 7 foreach my $verb_def ( $extend->verb ) {
135 7 ▶ $self->def_verb(@$verb_def) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->def_verb(@$verb_def) or return 7 0 7
136 }
137  
138 7 foreach my $option_def ( $extend->option ) {
139 0 $self->sub_option(@$option_def);
140 }
141  
142 7 foreach my $reply_def ( $extend->reply ) {
143 0 $self->sub_reply(@$reply_def);
144 }
145  
146 7 push( @{ $self->{extensions} }, $extend );
147 7 return 1;
148 }
149  
150 0 ▶sub sub_option {
Subroutine sub_option: not called CC=4 SCAR=30.0
Pod undocumented
151 0 my ( $self, $verb, $option_key, $code ) = @_;
152 0 ▶ confess("can't subscribe to option for verb `$verb'")
Branch: 1 branch
BranchTrueFalseTotal
unless $verb eq "MAIL" or $verb eq "RCPT" 0 0 0
Condition0%0/3
$verb eq "MAIL" or $verb eq "RCPT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$verb eq "MAIL" or $verb eq "RCPT"
$verb eq "MAIL" $verb eq "RCPT"
Truth table0%0/3
A or B
A= $verb eq "MAIL"B= $verb eq "RCPT"
ABresult
1X1
011
000
153 unless ( $verb eq 'MAIL' or $verb eq 'RCPT' );
154 confess("allready subscribed `$option_key'")
155 0 ▶ if ( exists $self->{xoption}->{$verb}->{$option_key} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"xoption"}{$verb}{$option_key} 0 0 0
156 0 $self->{xoption}->{$verb}->{$option_key} = $code;
157 }
158  
159 0 ▶sub sub_reply {
Subroutine sub_reply: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my ( $self, $verb, $code ) = @_;
161 0 ▶ confess("trying to subscribe to an unsupported verb `$verb'")
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($verb eq $_), $self->list_verb) 0 0 0
162 unless ( grep( $verb eq $_, $self->list_verb ) );
163 0 push( @{ $self->{xreply}->{$verb} }, $code );
164 }
165  
166 9 ▶sub extend_mode {
Subroutine extend_mode: called CC=3 SCAR=11.4
Pod undocumented
167 9 my ( $self, $mode ) = @_;
168 9 $self->{extend_mode} = $mode;
169 9 for my $extend ( @{ $self->{extensions} } ) {
170 9 ▶ if ( $extend->can('extend_mode') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extend->can("extend_mode")) 0 9 9
171 0 $extend->extend_mode($mode);
172 }
173 }
174 }
175  
176 =pod
177  
178 =head1 EVENTS
179  
180 Descriptions of callback who's can be used with set_callback
181 method. All handle takes the Net::Server::Mail::ESMTP object as first
182 argument and specific callback's arguments.
183  
184 =head2 EHLO
185  
186 Takes the hostname given as argument. Engage the reverse path step on
187 success. RFC 2821 requires that EHLO command return the list of
188 supported extension. Default success reply implement this, so it is
189 deprecated to override this reply.
190  
191 You can rebuild extension list with get_extensions() method.
192  
193 Example:
194  
195 my @extends;
196 foreach my $extend ($esmtp->get_extensions())
197 {
198 push(@extends, join(' ', $extend->keyword(), $extend->parameter()));
199 }
200 my $extends_string = join("\n", @extends);
201  
202 =cut
203  
204 9 ▶sub ehlo {
Subroutine ehlo: called CC=4 SCAR=14.6
Pod undocumented
205 9 my ( $self, $hostname ) = @_;
206  
207 9 ▶ unless ( defined $hostname && length $hostname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname and length $hostname) 0 9 9
Condition33%1/3
defined $hostname and length $hostname
!ll&&!rl&&r
009
MC/DC0%0/2
defined $hostname and length $hostname
defined $hostname length $hostname
Truth table33%1/3
A and B
A= defined $hostnameB= length $hostname
ABresult
0X0
100
111
208 0 $self->reply( 501, 'Syntax error in parameters or arguments' );
209 0 return;
210 }
211  
212 9 my $response = $self->get_hostname . ' Service ready';
213  
214 9 my @extends;
215 9 foreach my $extend ( $self->get_extensions ) {
216 9 push( @extends, join( ' ', $extend->keyword, $extend->parameter ) );
217 }
218  
219 9 $self->extend_mode(1);
220 $self->make_event(
221 name => 'EHLO',
222 arguments => [ $hostname, \@extends ],
223 9 ▶ on_success => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
224  
225 # according to the RFC, EHLO ensures "that both the SMTP client
226 # and the SMTP server are in the initial state"
227 9 $self->step_reverse_path(1);
228 9 $self->step_forward_path(0);
229 9 $self->step_maildata_path(0);
230 },
231 9 success_reply => [ 250, [ $response, @extends ] ],
232 );
233  
234 9 return;
235 }
236  
237 0 ▶sub helo {
Subroutine helo: not called CC=1 SCAR=6.9
Pod undocumented
238 0 my ( $self, $hostname ) = @_;
239 0 $self->{extend_mode} = 0;
240 0 $self->SUPER::helo($hostname);
241 }
242  
243 6 ▶sub handle_options {
Subroutine handle_options: called CC=5 SCAR=23.0
Pod undocumented
244 6 my ( $self, $verb, $address, @options ) = @_;
245  
246 6 ▶ if ( @options && !$self->{extend_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options and not $self->{"extend_mode"}) 0 6 6
Condition33%1/3
@options and not $self->{"extend_mode"}
!ll&&!rl&&r
600
MC/DC0%0/2
@options and not $self->{"extend_mode"}
@options not $self->{"extend_mode"}
Truth table33%1/3
A and not(B)
A= @optionsB= not $self->{"extend_mode"}
ABresult
0X0
100
111
247 0 $self->reply( 555, "Unsupported option: $options[0]" );
248 0 return 0;
249 }
250  
251 6 ▶ for ( my $i = $#options ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 6 6
252 0 my ( $key, $value ) = split( /=/, $options[$i], 2 );
253 0 my $handler = $self->{xoption}->{$verb}->{$key};
254 0 ▶ if ( defined $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler) { } 0 0 0
255 3 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
256 0 &$handler( $self, $verb, $address, $key, $value );
257 }
258 else {
259 0 $self->reply( 555, "Unsupported option: $key" );
260 0 return 0;
261 }
262 }
263  
264 6 return 1;
265 }
266  
267 31 ▶sub handle_reply {
Subroutine handle_reply: called CC=4 SCAR=17.1
Pod undocumented
268 31 my ( $self, $verb, $success, $code, $msg ) = @_;
269  
270 31 ▶ if ( $self->{extend_mode} && exists $self->{xreply}->{$verb} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extend_mode"} and exists $self->{"xreply"}{$verb}) 0 31 31
Condition66%2/3
$self->{"extend_mode"} and exists $self->{"xreply"}{$verb}
!ll&&!rl&&r
7240
MC/DC0%0/2
$self->{"extend_mode"} and exists $self->{"xreply"}{$verb}
$self->{"extend_mode"} exists $self->{"xreply"}{$verb}
Truth table66%2/3
A and B
A= $self->{"extend_mode"}B= exists $self->{"xreply"}{$verb}
ABresult
0X0
100
111
271 0 foreach my $handler ( @{ $self->{xreply}->{$verb} } ) {
272 0 ( $code, $msg ) = &$handler( $self, $verb, $success, $code, $msg );
273 }
274 }
275  
276 31 $self->reply( $code, $msg );
277 }
278  
279 =pod
280  
281 =head1 SEE ALSO
282  
283 Please, see L<Net::Server::Mail>, L<Net::Server::Mail::SMTP>
284 and L<Net::Server::Mail::LMTP>.
285  
286 =head1 AUTHOR
287  
288 Olivier Poitrey E<lt>rs@rhapsodyk.netE<gt>
289  
290 =head1 AVAILABILITY
291  
292 Available on CPAN.
293  
294 anonymous Git repository:
295  
296 git clone git://github.com/rs/net-server-mail.git
297  
298 Git repository on the web:
299  
300 L<https://github.com/rs/net-server-mail>
301  
302 =head1 BUGS
303  
304 Please use CPAN system to report a bug (http://rt.cpan.org/).
305  
306 =head1 LICENCE
307  
308 This library is free software; you can redistribute it and/or modify
309 it under the terms of the GNU Lesser General Public License as
310 published by the Free Software Foundation; either version 2.1 of the
311 License, or (at your option) any later version.
312  
313 This library is distributed in the hope that it will be useful, but
314 WITHOUT ANY WARRANTY; without even the implied warranty of
315 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
316 Lesser General Public License for more details.
317  
318 You should have received a copy of the GNU Lesser General Public
319 License along with this library; if not, write to the Free Software
320 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
321 USA
322  
323 =head1 COPYRIGHT
324  
325 Copyright (C) 2002 - Olivier Poitrey, 2007 - Xavier Guimard
326  
327 =cut
328  
329 3 1;
« Mail/ESMTP/STARTTLS.pm ↑ Summary Mail/LMTP.pm »