Server.pm

filter: Statementstmt 45.8% 39 / 85 Branchbran 19.4% 7 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 47.0% 8 / 17 Podpod 100% 10 / 10 totaltotal 39.2% 64 / 163 CC 24 SCAR 27.6
CC
24
Cov
35%
concurrent_mailbox_connections
42.0
concurrent_user_connections
30.0
connection
20.0
CRAP 15.8
« Server/DefaultModel.pm ↑ Summary Server/DefaultAuth.pm »
1 package Net::IMAP::Server;
2  
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use base qw/Net::Server::Coro Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use 5.008_008;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 our $VERSION = '1.38';
13  
14 =head1 NAME
15  
16 Net::IMAP::Server - A single-threaded multiplexing IMAP server
17 implementation, using L<Net::Server::Coro>.
18  
19 =head1 SYNOPSIS
20  
21 use Net::IMAP::Server;
22 Net::IMAP::Server->new(
23 port => 193,
24 ssl_port => 993,
25 auth_class => "Your::Auth::Class",
26 model_class => "Your::Model::Class",
27 user => "nobody",
28 group => "nobody",
29 )->run;
30  
31 =head1 DESCRIPTION
32  
33 This model provides a complete implementation of the C<RFC 3501>
34 specification, along with several IMAP4rev1 extensions. It provides
35 separation of the mailbox and message store from the client
36 interaction loop.
37  
38 Note that, following RFC suggestions, login is not allowed except
39 under a either SSL or TLS. Thus, you are required to have a F<certs/>
40 directory under the current working directory, containing files
41 F<server-cert.pem> and C<server-key.pem>. Failure to do so will cause
42 the server to fail to start. Note that if the default paths suit your
43 needs, you can specify different ones using the L</server_cert> and
44 L</server_key> arguments to L</new>.
45  
46 =head1 INTERFACE
47  
48 The primary method of using this module is to supply your own model
49 and auth classes, which inherit from
50 L<Net::IMAP::Server::DefaultModel> and
51 L<Net::IMAP::Server::DefaultAuth>. This allows you to back your
52 messages from arbitrary data sources, or provide your own
53 authorization backend. For the most part, the implementation of the
54 IMAP components should be opaque.
55  
56 =head1 METHODS
57  
58 =cut
59  
60 14 __PACKAGE__->mk_accessors(
61 qw/port ssl_port
62 auth_class model_class connection_class
63 command_class
64 poll_every
65 unauth_idle auth_idle unauth_commands
66 /
67 );
68  
69 =head2 new PARAMHASH
70  
71 Creates a new IMAP server object. This doesn't even bind to the
72 sockets; it merely initializes the object. It will C<die> if it
73 cannot find the appropriate certificate files. Valid arguments to
74 C<new> include:
75  
76 =over
77  
78 =item port
79  
80 The port to bind to. Defaults to port 1430.
81  
82 =item ssl_port
83  
84 The port to open an SSL listener on; by default, this is disabled, and
85 any true value enables it.
86  
87 =item auth_class
88  
89 The name of the class which implements authentication. This must be a
90 subclass of L<Net::IMAP::Server::DefaultAuth>.
91  
92 =item model_class
93  
94 The name of the class which implements the model backend. This must
95 be a subclass of L<Net::IMAP::Server::DefaultModel>.
96  
97 =item connection_class
98  
99 On rare occasions, you may wish to subclass the connection class; this
100 class must be a subclass of L<Net::IMAP::Server::Connection>.
101  
102 =item poll_every
103  
104 How often the current mailbox should be polled, in seconds; defaults
105 to 0, which means it will be polled after every client command.
106  
107 =item unauth_commands
108  
109 The number of commands before unauthenticated users are disconnected.
110 The default is 10; set to zero to disable.
111  
112 =item unauth_idle
113  
114 How long, in seconds, to wait before disconnecting idle connections
115 which have not authenticated yet. The default is 5 minutes; set to
116 zero to disable (which is not advised).
117  
118 =item auth_idle
119  
120 How long, in seconds, to wait before disconnecting authenticated
121 connections. By RFC specification, this B<must> be longer than 30
122 minutes. The default is an hour; set to zero to disable.
123  
124 =item server_cert
125  
126 Path to the SSL certificate that the server should use. This can be
127 either a relative or absolute path.
128  
129 =item server_key
130  
131 Path to the SSL certificate key that the server should use. This can
132 be either a relative or absolute path.
133  
134 =back
135  
136 It also accepts the following L<Net::Server> arguments -- see its
137 documentation for details on their use.
138  
139 =over
140  
141 =item L<Net::Server/log_level>
142  
143 =item L<Net::Server/log_file>
144  
145 =item L<Net::Server/syslog_logsock>
146  
147 =item L<Net::Server/syslog_ident>
148  
149 =item L<Net::Server/syslog_logopt>
150  
151 =item L<Net::Server/syslog_facility>
152  
153 =item L<Net::Server/pid_file>
154  
155 =item L<Net::Server/chroot>
156  
157 =item L<Net::Server/user>
158  
159 =item L<Net::Server/group>
160  
161 =item L<Net::Server/reverse_lookups>
162  
163 =item L<Net::Server/allow>
164  
165 =item L<Net::Server/deny>
166  
167 =item L<Net::Server/cidr_allow>
168  
169 =item L<Net::Server/cidr_deny>
170  
171 =back
172  
173 =cut
174  
175 sub new {
176 12 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=23.7
Pod documented
177  
178 12 my $self = Class::Accessor::new(
179 $class,
180 { port => 1430,
181 ssl_port => 0,
182 auth_class => "Net::IMAP::Server::DefaultAuth",
183 model_class => "Net::IMAP::Server::DefaultModel",
184 connection_class => "Net::IMAP::Server::Connection",
185 poll_every => 0,
186 unauth_idle => 5*60,
187 auth_idle => 60*60,
188 unauth_commands => 10,
189 @_,
190 command_class => {},
191 connection => {},
192 }
193 );
194  
195 $self->{server}{$_} = $self->{$_}
196 12 for grep {defined $self->{$_}}
197 qw/log_level log_file
198 syslog_logsock syslog_ident syslog_logopt syslog_facility
199 pid_file chroot user group
200 reverse_lookups allow deny cidr_allow cidr_deny
201 /;
202  
203 12 ▶ UNIVERSAL::require( $self->auth_class )
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::require($self->auth_class) or die "Can't require auth class: $@\n" 0 12 12
204 or die "Can't require auth class: $@\n";
205 12 ▶ $self->auth_class->isa("Net::IMAP::Server::DefaultAuth")
Branch: 1 branch
BranchTrueFalseTotal
$self->auth_class->isa("Net::IMAP::Server::DefaultAuth") or die "Auth class (@{[$self->auth_class];}) doesn't inherit from Net::IMAP::Server::DefaultAuth\n" 0 12 12
206 or die
207 0 "Auth class (@{[$self->auth_class]}) doesn't inherit from Net::IMAP::Server::DefaultAuth\n";
208  
209 12 ▶ UNIVERSAL::require( $self->model_class )
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::require($self->model_class) or die "Can't require model class: $@\n" 0 12 12
210 or die "Can't require model class: $@\n";
211 12 ▶ $self->model_class->isa("Net::IMAP::Server::DefaultModel")
Branch: 1 branch
BranchTrueFalseTotal
$self->model_class->isa("Net::IMAP::Server::DefaultModel") or die "Model class (@{[$self->model_class];}) doesn't inherit from Net::IMAP::Server::DefaultModel\n" 0 12 12
212 or die
213 0 "Model class (@{[$self->model_class]}) doesn't inherit from Net::IMAP::Server::DefaultModel\n";
214  
215 12 ▶ UNIVERSAL::require( $self->connection_class )
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::require($self->connection_class) or die "Can't require connection class: $@\n" 0 12 12
216 or die "Can't require connection class: $@\n";
217 12 ▶ $self->connection_class->isa("Net::IMAP::Server::Connection")
Branch: 1 branch
BranchTrueFalseTotal
$self->connection_class->isa("Net::IMAP::Server::Connection") or die "Connection class (@{[$self->connection_class];}) doesn't inherit from Net::IMAP::Server::Connection\n" 0 12 12
218 or die
219 0 "Connection class (@{[$self->connection_class]}) doesn't inherit from Net::IMAP::Server::Connection\n";
220  
221 12 return $self;
222 }
223  
224 =head2 run
225  
226 Starts the server; this method shouldn't be expected to return.
227 Within this method, C<$Net::IMAP::Server::Server> is set to the object
228 that this was called on; thus, all IMAP objects have a way of
229 referring to the server -- and though L</connection>, whatever parts
230 of the IMAP internals they need.
231  
232 Any arguments are passed through to L<Net::Server/run>.
233  
234 =cut
235  
236 sub run {
237 12 ▶ my $self = shift;
Subroutine run: called CC=2 SCAR=7.0
Pod documented
238 12 my @proto = qw/TCP/;
239 12 my @port = $self->port;
240 12 ▶ if ( $self->ssl_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl_port) 12 0 12
241 12 push @proto, "SSL";
242 12 push @port, $self->ssl_port;
243 }
244 12 local $Net::IMAP::Server::Server = $self;
245 12 $self->SUPER::run(
246 @_,
247 proto => \@proto,
248 port => \@port,
249 );
250 }
251  
252 =head2 process_request
253  
254 Accepts a client connection; this method is needed for the
255 L<Net::Server> infrastructure.
256  
257 =cut
258  
259 sub process_request {
260 0 ▶ my $self = shift;
Subroutine process_request: not called CC=1 SCAR=6.9
Pod documented
261 0 my $handle = $self->{server}{client};
262 0 my $conn = $self->connection_class->new(
263 io_handle => $handle,
264 server => $self,
265 );
266 0 $self->connection($conn);
267 0 $conn->handle_lines;
268 }
269  
270 =head2 DESTROY
271  
272 On destruction, ensure that we close all client connections and
273 listening sockets.
274  
275 =cut
276  
277 DESTROY {
278 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
279 0 $_->close for grep { defined $_ } @{ $self->connections };
280 0 $self->server_close;
281 }
282  
283 =head2 connections
284  
285 Returns an arrayref of L<Net::IMAP::Server::Connection> objects which
286 are currently connected to the server.
287  
288 =cut
289  
290 sub connections {
291 0 ▶ my $self = shift;
Subroutine connections: not called CC=1 SCAR=6.9
Pod documented
292 0 return [ values %{$self->{connection}} ];
293 }
294  
295 =head2 connection
296  
297 Returns the currently active L<Net::IMAP::Server::Connection> object,
298 if there is one. This is determined by examining the current
299 coroutine.
300  
301 =cut
302  
303 sub connection {
304 0 ▶ my $class = shift;
Subroutine connection: not called CC=4 SCAR=30.0
Pod documented
305 0 ▶ my $self = ref $class ? $class : $Net::IMAP::Server::Server;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
306 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
307 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
308 0 $self->{connection}{$Coro::current . ""} = shift;
309 } else {
310 0 delete $self->{connection}{$Coro::current . ""};
311 }
312 }
313 0 return $self->{connection}{$Coro::current . ""};
314 }
315  
316 =head2 concurrent_mailbox_connections [MAILBOX]
317  
318 This can be called as either a class method or an instance method; it
319 returns the set of connections which are concurrently connected to the
320 given mailbox object (which defaults to the current connection's
321 selected mailbox)
322  
323 =cut
324  
325 sub concurrent_mailbox_connections {
326 0 ▶ my $class = shift;
Subroutine concurrent_mailbox_connections: not called CC=6 SCAR=37.4
Pod documented
327 0 ▶ my $self = ref $class ? $class : $Net::IMAP::Server::Server;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
328 0 ▶ my $selected = shift || $self->connection->selected;
Condition0%0/3
shift() || $self->connection->selected
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->connection->selected
shift() $self->connection->selected
Truth table0%0/3
A || B
A= shift()B= $self->connection->selected
ABresult
1X1
011
000
329  
330 0 ▶ return () unless $selected;
Branch: 1 branch
BranchTrueFalseTotal
unless $selected 0 0 0
331 return
332 0 ▶ grep { $_->is_auth and $_->is_selected and $_->selected eq $selected }
Branch: 1 branch
BranchTrueFalseTotal
$_->is_auth and $_->is_selected and $_->selected eq $selected 0 0 0
Condition0%0/3
$_->is_auth and $_->is_selected
!ll&&!rl&&r
000
MC/DC0%0/2
$_->is_auth and $_->is_selected
$_->is_auth $_->is_selected
Truth table0%0/3
A and B
A= $_->is_authB= $_->is_selected
ABresult
0X0
100
111
333 0 @{ $self->connections };
334 }
335  
336 =head2 concurrent_user_connections [USER]
337  
338 This can be called as either a class method or an instance method; it
339 returns the set of connections whose
340 L<Net::IMAP::Server::DefaultAuth/user> is the same as the given
341 L<USER> (which defaults to the current connection's user)
342  
343 =cut
344  
345 sub concurrent_user_connections {
346 0 ▶ my $class = shift;
Subroutine concurrent_user_connections: not called CC=5 SCAR=34.0
Pod documented
347 0 ▶ my $self = ref $class ? $class : $Net::IMAP::Server::Server;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
348 0 ▶ my $user = shift || $self->connection->auth->user;
Condition0%0/3
shift() || $self->connection->auth->user
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->connection->auth->user
shift() $self->connection->auth->user
Truth table0%0/3
A || B
A= shift()B= $self->connection->auth->user
ABresult
1X1
011
000
349  
350 0 ▶ return () unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
351 return
352 0 ▶ grep { $_->is_auth and $_->auth->user eq $user }
Branch: 1 branch
BranchTrueFalseTotal
$_->is_auth and $_->auth->user eq $user 0 0 0
353 0 @{ $self->connections };
354 }
355  
356 =head2 capability
357  
358 Returns the C<CAPABILITY> string for the server. This string my be
359 modified by the connection before being sent to the client (see
360 L<Net::IMAP::Server::Connection/capability>).
361  
362 =cut
363  
364 sub capability {
365 0 ▶ my $self = shift;
Subroutine capability: not called CC=1 SCAR=6.9
Pod documented
366 0 return "IMAP4rev1 STARTTLS CHILDREN LITERAL+ UIDPLUS ID NAMESPACE";
367 }
368  
369 =head2 id
370  
371 Returns a hash of properties to be conveyed to the client, should they
372 ask the server's identity.
373  
374 =cut
375  
376 sub id {
377 return (
378 0 ▶ name => "Net-IMAP-Server",
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
379 version => $Net::IMAP::Server::VERSION,
380 );
381 }
382  
383 =head2 add_command NAME => PACKAGE
384  
385 Adds the given command C<NAME> to the server's list of known commands.
386 C<PACKAGE> should be the name of a class which inherits from
387 L<Net::IMAP::Server::Command>.
388  
389 =cut
390  
391 sub add_command {
392 0 ▶ my $self = shift;
Subroutine add_command: not called CC=3 SCAR=24.8
Pod documented
393 0 my ($name, $package) = @_;
394 0 ▶ if (not $package->require) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $package->require) { } 0 0 0
elsif (not $package->isa('Net::IMAP::Server::Command')) { } 0 0 0
395 0 $self->log( 1, $@ );
396 } elsif (not $package->isa('Net::IMAP::Server::Command')) {
397 0 $self->log( 1, "$package is not a Net::IMAP::Server::Command!" );
398 } else {
399 0 $self->command_class->{uc $name} = $package;
400 }
401 }
402  
403 =head2 log SEVERITY, MESSAGE
404  
405 By default, defers to L<Net::Server/log>, which outputs to syslog, a
406 logfile, or STDERR, depending how it was configured. L<Net::Server>'s
407 default is to print to STDERR. If you have custom logging needs,
408 override this method, or L<Net::Server/write_to_log_hook>.
409  
410 =cut
411  
412 14 1; # Magic true value required at end of module
413 __END__
414  
415 =head1 Object model
416  
417 An ASCII model of the relationship between objects is below. In it,
418 single lines represent scalar values, and lines made of other
419 characters denote array references or relations.
420  
421 +----------------------------------------------+
422 | |
423 | Server |
424 | |
425 +1-----2---------------------------------------+
426 # | ^ ^ ^ ^
427 # | | | | |
428 # v | | | |
429 # +--------1-------+ | +------1------+ |
430 ###>| Connection |<------2 Command | |
431 # +--4-----3------2+ | +-------------+ |
432 ,-#------' | `--------------. |
433 | # v | v |
434 | # +----------------+ | +-------------+ |
435 | # | Model 2------>| Auth | |
436 | # +--------1-------+ | +-------------+ |
437 | # `---------------------------------.
438 | # | | |
439 | # ,---' ,---' |
440 | # +--------------1-+ +-----------1-+ |
441 | ###>| Connection |<------2 Command | |
442 | +--4-5---3------2+ +-------------+ |
443 | ,------' * | `--------------. |
444 | | ******** v v |
445 | | * +----------------+ +-------------+ |
446 | | * | Model 2------>| Auth | |
447 | | * +--------1-------+ +-------------+ |
448 | | * | |
449 | | * | ,------------------------------'
450 | | * | | ^ SERVER
451 .|.|.*..........|..|................................
452 | | * | | v MODEL
453 | | * v v
454 | '-*---->+-------------+<------------.
455 '---*---->| Mailbox |<----------. |
456 * +-1------2-3--+<----. | |
457 * @ ^ $ % | | |
458 * @ | $$$$>+-----1---+ | |
459 * @ | $ % | | | |
460 * @ | $ %%>| Message | | |
461 * @ | $ % | | | |
462 ********@***|******>+---------+ | |
463 * @ | $ % | |
464 * @ | $$$$>+---------+ | |
465 * @ | % | | | |
466 * @ | %%>| Message 1-' |
467 * @ | | | |
468 ********@***|******>+---------+ |
469 * @ | |
470 * @ | +---------+ |
471 * @ | | Message 1---'
472 ********@***|******>+---------+
473 @ |
474 @ +4----------+
475 @@>| Mailbox |
476 +-----------+
477  
478 The top half consists of the parts which implement the IMAP protocol
479 itself; the bottom contains the models for the backing store. Note
480 that, for the most part, the backing store is unaware of the framework
481 of the server itself.
482  
483 Each model has references to others, as follows:
484  
485 =over
486  
487 =item Server
488  
489 Contains references to the set of C<connections> (1). It also has a
490 sense of the I<current> C<connection> (2), based on the active L<Coro>
491 thread.
492  
493 =item Connection
494  
495 Connections hold a reference to their C<server> (1). If the
496 connection has authenticated, they hold a reference to the C<auth>
497 object (2), and to their C<model> (3). If a mailbox is C<selected>
498 (4), they hold a pointer to that, as well. Infrequently, the
499 connection will need to temporarily store references to the set of
500 C<temporary_messages> (5) which have been expunged in other
501 connections, but we have been unable to notify this connection of.
502  
503 =item Command
504  
505 Commands store their C<server> (1) and C<connection> (2).
506  
507 =item Model
508  
509 Models store a reference to the C<root> (1) of their mailbox tree, as
510 well as to the C<auth> (2) which gives them access to such.
511  
512 =item Mailbox
513  
514 Mailboxes store a list of C<children> mailboxes (1), and C<messages>
515 (2) contained within them, which are stored in sequence order. They
516 also contain a hash of C<uids> (3) for fast UID retrieval of
517 messages. If they are not the root mailbox, they also store a
518 reference to their C<parent> mailbox (4).
519  
520 =item Message
521  
522 Messages store the C<mailbox> (1) in which they are contained.
523  
524 =back
525  
526 =head1 DEPENDENCIES
527  
528 L<Coro>, L<Net::Server::Coro>
529  
530 =head1 BUGS AND LIMITATIONS
531  
532 No bugs have been reported.
533  
534 Please report any bugs or feature requests to
535 C<bug-net-imap-server@rt.cpan.org>, or through the web interface at
536 L<http://rt.cpan.org>.
537  
538 A low-traffic mailing list exists for discussion on how to (ab)use
539 this module, at
540 L<http://lists.bestpractical.com/cgi-bin/mailman/listinfo/net-imap-server>.
541  
542 =head1 AUTHOR
543  
544 Alex Vandiver C<< <alexmv@bestpractical.com> >>
545  
546 =head1 LICENCE AND COPYRIGHT
547  
548 Copyright (c) 2010, Best Practical Solutions, LLC. All rights reserved.
549  
550 This module is free software; you can redistribute it and/or
551 modify it under the same terms as Perl itself. See L<perlartistic>.
552  
553 =head1 DISCLAIMER OF WARRANTY
554  
555 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
556 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
557 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
558 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
559 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
560 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
561 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
562 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
563 NECESSARY SERVICING, REPAIR, OR CORRECTION.
564  
565 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
566 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
567 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
568 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
569 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
570 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
571 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
572 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
573 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
574 SUCH DAMAGES.
« Server/DefaultModel.pm ↑ Summary Server/DefaultAuth.pm »