SMTP/Server.pm

filter: Statementstmt 69.7% 145 / 208 Branchbran 38.4% 30 / 78 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.5% 31 / 40 Podpod 50.0% 4 / 8 totaltotal 60.9% 211 / 346 CC 46 SCAR 28.1
CC
46
Cov
60%
__ANON__
110.0
__ANON__
9.5
__ANON__
9.3
CRAP 16.6
↑ Summary SMTP/Client.pm »
1 package AnyEvent::SMTP::Server;
2  
3 =head1 NAME
4  
5 AnyEvent::SMTP::Server - Simple asyncronous SMTP Server
6  
7 =cut
8  
9 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 m{# trying to cheat with cpants game ;)
13 use strict;
14 use warnings;
15 }x;
16  
17 12 ▶use base 'Object::Event';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use AnyEvent::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 12 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 12 ▶use AnyEvent::SMTP::Conn;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 12 ▶our $VERSION = $AnyEvent::SMTP::VERSION;use AnyEvent::SMTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 12 our %CMD = map { $_ => 1 } qw( HELO EHLO MAIL RCPT QUIT DATA EXPN VRFY NOOP HELP RSET );
31  
32 =head1 SYNOPSIS
33  
34 use AnyEvent::SMTP::Server 'smtp_server';
35  
36 smtp_server undef, 2525, sub {
37 my $mail = shift;
38 warn "Received mail from $mail->{from} to $mail->{to}\n$mail->{data}\n";
39 };
40
41 # or
42
43 use AnyEvent::SMTP::Server;
44
45 my $server = AnyEvent::SMTP::Server->new(
46 port => 2525,
47 mail_validate => sub {
48 my ($m,$addr) = @_;
49 if ($good) { return 1 } else { return 0, 513, 'Bad sender.' }
50 },
51 rcpt_validate => sub {
52 my ($m,$addr) = @_;
53 if ($good) { return 1 } else { return 0, 513, 'Bad recipient.' }
54 },
55 data_validate => sub {
56 my ($m,$data) = @_;
57 my $size = length $data;
58 if ($size > $max_email_size) {
59 return 0, 552, 'REJECTED: message size limit exceeded';
60 } else {
61 return 1;
62 }
63 },
64 );
65  
66 $server->reg_cb(
67 client => sub {
68 my ($s,$con) = @_;
69 warn "Client from $con->{host}:$con->{port} connected\n";
70 },
71 disconnect => sub {
72 my ($s,$con) = @_;
73 warn "Client from $con->{host}:$con->{port} gone\n";
74 },
75 mail => sub {
76 my ($s,$mail) = @_;
77 warn "Received mail from ($mail->{host}:$mail->{port}) $mail->{from} to $mail->{to}\n$mail->{data}\n";
78 },
79 );
80  
81 $server->start;
82 AnyEvent->condvar->recv;
83  
84 =head1 DESCRIPTION
85  
86 Simple asyncronous SMTP server. Authorization not implemented yet. Patches are welcome
87  
88 =head1 FUNCTIONS
89  
90 =head2 smtp_server $host, $port, $cb->(MAIL)
91  
92 =head1 METHODS
93  
94 =head2 new %args;
95  
96 =over 4
97  
98 =item hosthame
99  
100 Server FQDN
101  
102 =item host
103  
104 Address to listen on. by default - undef (0.0.0.0)
105  
106 =item port
107  
108 Port to listen on
109  
110 =back
111  
112 =head2 start
113  
114 Creates tcp server and starts to listen
115  
116 =head2 stop
117  
118 Closes all opened connections and shutdown server
119  
120 =head1 EVENTS
121  
122 =over 4
123  
124 =item ready()
125  
126 Invoked when server is ready
127  
128 =item client($connection)
129  
130 Invoked when client connects
131  
132 =item disconnect($connection)
133  
134 Invoked when client disconnects
135  
136 =item mail($mail)
137  
138 Invoked when server received complete mail message
139  
140 $mail = {
141 from => ...,
142 to => [ ... ],
143 data => '...',
144 host => 'remote addr',
145 port => 'remote port',
146 helo => 'HELO/EHLO string',
147 };
148  
149 =back
150  
151 =cut
152  
153 2 ▶sub import {
Subroutine import: called CC=4 SCAR=21.7
154 2 my $me = shift;
155 2 my $pkg = caller;
156
157 2 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 0 2 2
158 0 for (@_) {
159 0 ▶ if ( $_ eq 'smtp_server') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'smtp_server') { } 0 0 0
160 0 *{$pkg.'::'.$_} = \&$_;
161 } else {
162 0 croak "$_ is not exported by $me";
163 }
164 }
165 }
166  
167 5 ▶sub smtp_server {
Subroutine smtp_server: called CC=2 SCAR=7.5
Pod documented
168 5 my ($host,$port,$cb) = @_;
169 5 my $server = AnyEvent::SMTP::Server->new(
170 host => $host,
171 port => $port,
172 );
173 $server->reg_cb(
174 41 ▶ mail => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
175 41 $cb->($_[1]);
176 },
177 5 );
178 5 $server->start;
179 defined wantarray
180 0 ▶ ? AnyEvent::Util::guard { $server->stop; %$server = (); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 : ()
182 5 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 5 5
183  
184 7 ▶sub new {
Subroutine new: called CC=2 SCAR=10.0
Pod documented
185 7 my $pkg = shift;
186 7 my $self = bless { @_ }, $pkg;
187 7 ▶ $self->{hostname} = hostname() unless defined $self->{hostname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"hostname"} 7 0 7
188 0 ▶ $self->set_exception_cb( sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
189 0 my ($e, $event, @args) = @_;
190 0 my $ex = $@;
191 0 ▶ if (exists $self->{event_failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"event_failed"}) 0 0 0
192 0 $self->{event_failed} = $ex;
193 0 return;
194 }
195 #warn "exception: $self, $self->{current_con} (@args) [$@]";
196 0 my $con = $self->{current_con};
197 0 ▶ if (!$con) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($con) 0 0 0
198 0 local $::self = $self;
199 0 local $::con;
200 0 local $::event = $event;
201 {
202 0 package DB;
203 0 my $i = 0;
204 0 ▶ while (my @c = caller(++$i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@c) = caller ++$i) 0 0 0
205 0 warn "$i. [@DB::args]";
206 0 ▶ next if @DB::args < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @DB::args < 2 0 0 0
207 0 ▶ last if $DB::args[0] == $::self and $DB::args[1] eq $::event and UNIVERSAL::isa($DB::args[2], 'AnyEvent::SMTP::Conn');
Branch: 1 branch
BranchTrueFalseTotal
if $DB::args[0] == $main::self and $DB::args[1] eq $main::event and UNIVERSAL::isa($DB::args[2], "AnyEvent::SMTP::Conn") 0 0 0
Condition0%0/6
$DB::args[0] == $main::self and $DB::args[1] eq $main::event and UNIVERSAL::isa($DB::args[2], "AnyEvent::SMTP::Conn")
!ll&&!rl&&r
000
$DB::args[0] == $main::self and $DB::args[1] eq $main::event
!ll&&!rl&&r
000
MC/DC0%0/3
$DB::args[0] == $main::self and $DB::args[1] eq $main::event and UNIVERSAL::isa($DB::args[2], "AnyEvent::SMTP::Conn")
$DB::args[0] == $main::self $DB::args[1] eq $main::event UNIVERSAL::isa($DB::args[2], "AnyEvent::SMTP::Conn")
Truth table0%0/4
A and B and C
A= $DB::args[0] == $main::selfB= $DB::args[1] eq $main::eventC= UNIVERSAL::isa($DB::args[2], "AnyEvent::SMTP::Conn")
ABCresult
0XX0
10X0
1100
1111
208 }
209 0 $::con = $DB::args[2];
210 }
211 0 $con = $::con;
212 }
213 0 ▶ if ($con) {
Branch: 1 branch
BranchTrueFalseTotal
if ($con) 0 0 0
214 0 my $msg = "500 INTERNAL ERROR";
215 0 ▶ if ($self->{devel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"devel"}) 0 0 0
216 0 $ex =~ s{(?:\r?\n)+}{ }sg;
217 0 $ex =~ s{\s+$}{}s;
218 0 $msg .= ": ".$ex;
219 }
220 0 $con->reply($msg);
221 }
222 0 warn "exception during $event : $ex";
223 7 } );
224 $self->reg_cb(
225 164 ▶ command => sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
226 164 my ($s,$con,$com) = @_;
227 164 my ($cmd, @args);
228 164 for ($com) {
229 164 s/^\s+//;s/\s+$//;
230 164 ▶ length or last;
Branch: 1 branch
BranchTrueFalseTotal
length $_ or last 164 0 164
231 164 ($cmd, @args) = split /\s+/;
232 164 $cmd = uc $cmd;
233 }
234 164 ▶ if (exists $CMD{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $AnyEvent::SMTP::Server::CMD{$cmd}) { } 164 0 164
235 164 $s->handle( $con, $cmd, @args );
236 } else {
237 0 warn "$cmd @args";
238 0 $con->reply("500 Learn to type!");
239 }
240 #warn "Got command @_";
241 },
242 41 ▶ HELO => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
243 41 my ($s,$con,@args) = @_;
244 41 $con->{helo} = "@args";
245 41 $con->new_m();
246 41 $con->ok("I'm ready.");
247 },
248 0 ▶ EHLO => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 my ($s,$con,@args) = @_;
250 0 $con->{helo} = "@args";
251 0 $con->new_m();
252 0 $con->ok("Go on.");
253 },
254 0 ▶ RSET => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 0 my ($s,$con,@args) = @_;
256 0 $con->new_m();
257 0 $con->ok;
258 },
259 41 ▶ MAIL => sub {
Subroutine __ANON__: called CC=7 SCAR=22.5
260 41 my ($s,$con,@args) = @_;
261 41 my $from = join ' ',@args;
262 41 ▶ $from =~ s{^from:}{}i or return $con->reply('501 Usage: MAIL FROM:<mail addr>');
Branch: 1 branch
BranchTrueFalseTotal
$from =~ s/^from://iu or return $con->reply("501 Usage: MAIL FROM:<mail addr>") 41 0 41
263 41 ▶ $con->{helo} or return $con->reply("503 Error: send HELO/EHLO first");
Branch: 1 branch
BranchTrueFalseTotal
$con->{"helo"} or return $con->reply("503 Error: send HELO/EHLO first") 41 0 41
264 41 my @addrs;
265 41 ▶ if ($from !~ /^\s*<>\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $from =~ /^\s*<>\s*$/u) { } 41 0 41
266 41 @addrs = map { $_->address } Mail::Address->parse($from);
267 41 ▶ @addrs == 1 or return $con->reply('501 Usage: MAIL FROM:<mail addr>');
Branch: 1 branch
BranchTrueFalseTotal
@addrs == 1 or return $con->reply("501 Usage: MAIL FROM:<mail addr>") 41 0 41
268 } else {
269 0 @addrs = ('');
270 }
271 41 ▶ if ($self->{mail_validate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mail_validate"}) 0 41 41
272 0 my ($res,$err,$errstr) = $self->{mail_validate}->($con->{m}, $addrs[0]);
273 0 ▶ $res or return $con->reply("$err $errstr");
Branch: 1 branch
BranchTrueFalseTotal
$res or return $con->reply("$err $errstr") 0 0 0
274 }
275 41 $con->{m}{from} = $addrs[0];
276 41 $con->ok;
277 },
278 41 ▶ RCPT => sub {
Subroutine __ANON__: called CC=7 SCAR=22.3
279 41 my ($s,$con,@args) = @_;
280 41 my $to = join ' ',@args;
281 41 ▶ $to =~ s{^to:}{}i or return $con->reply('501 Usage: RCPT TO:<mail addr>');
Branch: 1 branch
BranchTrueFalseTotal
$to =~ s/^to://iu or return $con->reply("501 Usage: RCPT TO:<mail addr>") 41 0 41
282 41 ▶ defined $con->{m}{from} or return $con->reply("503 Error: need MAIL command");
Branch: 1 branch
BranchTrueFalseTotal
defined $con->{"m"}{"from"} or return $con->reply("503 Error: need MAIL command") 41 0 41
283 41 my @addrs = map { $_->address } Mail::Address->parse($to);
284 41 ▶ @addrs or return $con->reply('501 Usage: RCPT TO:<mail addr>');
Branch: 1 branch
BranchTrueFalseTotal
@addrs or return $con->reply("501 Usage: RCPT TO:<mail addr>") 41 0 41
285 41 ▶ if ($self->{rcpt_validate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rcpt_validate"}) 0 41 41
286 0 my ($res,$err,$errstr) = $self->{rcpt_validate}->($con->{m}, $addrs[0]);
287 0 ▶ $res or return $con->reply("$err $errstr");
Branch: 1 branch
BranchTrueFalseTotal
$res or return $con->reply("$err $errstr") 0 0 0
288 }
289 41 ▶ push @{ $con->{m}{to} ||= [] }, $addrs[0];
Condition50%1/2
$con->{"m"}{"to"} ||= []
l!l
041
MC/DC0%0/1
$con->{"m"}{"to"} ||= []
$con->{"m"}{"to"}
Truth table50%1/2
A
A= $con->{"m"}{"to"}
Aresult
11
00
290 41 $con->ok;
291 },
292 41 ▶ DATA => sub {
Subroutine __ANON__: called CC=3 SCAR=12.2
293 41 my ($s,$con) = @_;
294 41 ▶ defined $con->{m}{from} or return $con->reply("503 Error: need MAIL command");
Branch: 1 branch
BranchTrueFalseTotal
defined $con->{"m"}{"from"} or return $con->reply("503 Error: need MAIL command") 41 0 41
295 41 ▶ $con->{m}{to} or return $con->reply("554 Error: need RCPT command");
Branch: 1 branch
BranchTrueFalseTotal
$con->{"m"}{"to"} or return $con->reply("554 Error: need RCPT command") 41 0 41
296 41 $con->reply("354 End data with <CR><LF>.<CR><LF>");
297 41 ▶ $con->data(cb => sub {
Subroutine __ANON__: called CC=4 SCAR=16.3
298 41 my $data = shift;
299 41 ▶ if ($self->{data_validate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data_validate"}) 0 41 41
300 0 my ($res,$err,$errstr) = $self->{data_validate}->($con->{m}, $data);
301 0 ▶ $res or return $con->reply("$err $errstr");
Branch: 1 branch
BranchTrueFalseTotal
$res or return $con->reply("$err $errstr") 0 0 0
302 }
303 41 $con->{m}{data} = $data;
304 41 local $s->{event_failed} = 0;
305 41 local $s->{current_con} = $con;
306 41 $s->event( mail => delete $con->{m} );
307 41 ▶ if ($s->{event_failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'event_failed'}) { } 0 41 41
308 0 $con->reply("500 Internal Server Error");
309 } else {
310 41 $con->ok("I'll take it");
311 }
312 41 });
313 },
314 0 ▶ QUIT => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
315 0 my ($s,$con,$to,@args) = @_;
316 0 $con->reply("221 Bye.");
317 0 $con->close;
318 0 return;
319 },
320 0 ▶ HELP => sub { $_[1]->reply("214 No help available.") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 ▶ NOOP => sub { $_[1]->reply("252 Ok.") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 0 ▶ EXPN => sub { $_[1]->reply("252 Nice try.") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 ▶ VRFY => sub { $_[1]->reply("252 Nice try.") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 7 );
325 7 $self;
326 }
327  
328 2 ▶sub stop {
Subroutine stop: called CC=3 SCAR=11.0
Pod documented
329 2 my $self = shift;
330 2 for (keys %{ $self->{c} }) {
331 1 ▶ $self->{c}{$_} and $self->{c}{$_}->close;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"c"}{$_} and $self->{"c"}{$_}->close 1 0 1
332 }
333 2 delete $self->{c};
334 2 delete $self->{s};
335 2 return;
336 }
337  
338 7 ▶sub start {
Subroutine start: called CC=2 SCAR=7.4
Pod documented
339 7 my $self = shift;
340 7 ▶ $self->eventcan('command') or croak "Server implementation $self doesn't parses commands";
Branch: 1 branch
BranchTrueFalseTotal
$self->eventcan("command") or croak("Server implementation $self doesn't parses commands") 7 0 7
341 #$self->{engine} or croak "Server implementation $self doesn't have engine";
342 47 ▶ $self->{s} = tcp_server $self->{host}, $self->{port}, sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
343 47 my ($fh,$host,$port) = @_;
344 47 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 47 47
345 0 $self->event( error => "couldn't accept client: $!" );
346 0 return;
347 }
348 47 $self->accept_connection(@_);
349 7 ▶ }, sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
350 7 my ($sock,$host,$port) = @_;
351 #$self->{sock} = $sock;
352 7 ▶ $self->{host} = $host unless defined $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"host"} 7 0 7
353 7 ▶ $self->{port} = $port unless defined $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 0 7 7
354 7 ▶ warn "Server started on port $self->{port}\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 7 7
355 7 $self->event(ready => ());
356 7 return undef;
357 7 };
358
359 }
360  
361 47 ▶sub accept_connection {
Subroutine accept_connection: called CC=2 SCAR=6.9
Pod undocumented
362 47 my ($self,$fh,$host,$port) = @_;
363 47 ▶ warn "Client connected $host:$port \n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 47 47
364 my $con = AnyEvent::SMTP::Conn->new(
365 fh => $fh,
366 host => $host,
367 port => $port,
368 debug => $self->{debug},
369 47 );
370 47 $self->{c}{int $con} = $con;
371 $con->reg_cb(
372 47 ▶ disconnect => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
373 47 delete $self->{c}{int $_[0]};
374 47 $self->event( disconnect => $_[0], $_[1] );
375 },
376 164 ▶ command => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
377 164 $self->event( command => @_ )
378 },
379 47 );
380 47 $self->eventif( client => $con );
381 47 $con->reply("220 $self->{hostname} AnyEvent::SMTP Ready.");
382 47 $con->want_command;
383 }
384  
385 211 ▶sub eventif {
Subroutine eventif: called CC=2 SCAR=6.9
Pod undocumented
386 #my ($self,$name) = @_;
387 211 my $self = shift;my $name = shift;
388 211 ▶ return 0 unless $self->eventcan($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->eventcan($name) 46 165 211
389 165 $self->event($name => @_);
390 165 return 1;
391 #goto &{ $self->can('event') };
392 }
393  
394 218 ▶sub eventcan {
Subroutine eventcan: called CC=2 SCAR=6.9
Pod undocumented
395 218 my $self = shift;
396 218 my $name = shift;
397 218 ▶ return undef unless exists $self->{__oe_events}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__oe_events"}{$name} 46 172 218
398 172 return scalar @{ $self->{__oe_events}{$name} };
399 }
400  
401 164 ▶sub handle {
Subroutine handle: called CC=2 SCAR=10.1
Pod undocumented
402 164 my ($self,$con, $cmd, @args ) = @_;
403 $self->eventif( $cmd => $con, @args )
404 164 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->eventif($cmd, $con, @args) or do { $con->reply("500 Not Supported"); warn "$cmd event not handled ($cmd @args)"; 0 } 164 0 164
405 0 $con->reply("500 Not Supported");
406 0 warn "$cmd event not handled ($cmd @args)";
407 0 0;
408 };
409 }
410  
411 =head1 BUGS
412  
413 Bug reports are welcome in CPAN's request tracker L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-SMTP>
414  
415 =head1 AUTHOR
416  
417 Mons Anderson, C<< <mons at cpan.org> >>
418  
419 =head1 COPYRIGHT & LICENSE
420  
421 Copyright 2009 Mons Anderson, all rights reserved.
422  
423 This program is free software; you can redistribute it and/or modify it
424 under the same terms as Perl itself.
425  
426 =cut
427  
428 12 1;
↑ Summary SMTP/Client.pm »