Server/Connection.pm

filter: Statementstmt 13.6% 34 / 249 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 62 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 28.5% 10 / 35 Podpod 100% 24 / 24 totaltotal 13.2% 68 / 513 CC 85 SCAR 43.4
CC
85
Cov
8%
handle_lines
156.0
handle_command
156.0
send_untagged
132.0
CRAP 76.7
« Server/Mailbox.pm ↑ Summary Server/Command.pm »
1 package Net::IMAP::Server::Connection;
2  
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use Net::IMAP::Server::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Net::IMAP::Server::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 __PACKAGE__->mk_accessors(
16 qw(server coro io_handle model auth
17 timer commands pending
18 selected_read_only
19 _selected
20  
21 temporary_messages temporary_sequence_map
22 ignore_flags
23 _session_flags
24  
25 last_poll previous_exists in_poll
26 _unsent_expunge _unsent_fetch
27 )
28 );
29  
30 =head1 NAME
31  
32 Net::IMAP::Server::Connection - Connection to a client
33  
34 =head1 DESCRIPTION
35  
36 Maintains all of the state for a client connection to the IMAP server.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 Creates a new connection; the server will take care of this step.
43  
44 =cut
45  
46 sub new {
47 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
48 0 my $self = $class->SUPER::new(
49 { @_,
50 state => "unauth",
51 _unsent_expunge => [],
52 _unsent_fetch => {},
53 last_poll => time,
54 commands => 0,
55 coro => $Coro::current,
56 _session_flags => {},
57 }
58 );
59 0 $self->update_timer;
60 0 return $self;
61 }
62  
63 =head2 server
64  
65 Returns the L<Net::IMAP::Server> that this connection is on.
66  
67 =head2 coro
68  
69 Returns the L<Coro> process associated with this connection. For
70 things interacting with this connection, it will probably be the
71 current coroutine, except for interactions coming from event loops.
72  
73 =head2 io_handle
74  
75 Returns the IO handle that can be used to read from or write to the
76 client.
77  
78 =head2 model
79  
80 Gets or sets the L<Net::IMAP::Server::DefaultModel> or descendant
81 associated with this connection. Note that connections which have not
82 authenticated yet do not have a model.
83  
84 =head2 auth
85  
86 Gets or sets the L<Net::IMAP::Server::DefaultAuth> or descendant
87 associated with this connection. Note that connections which have not
88 authenticated yet do not have an auth object.
89  
90 =cut
91  
92 sub auth {
93 0 ▶ my $self = shift;
Subroutine auth: not called CC=3 SCAR=24.8
Pod documented
94 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
95 0 $self->{auth} = shift;
96 0 ▶ $self->server->model_class->require || $self->log(1, $@);
Branch: 1 branch
BranchTrueFalseTotal
$self->server->model_class->require or $self->log(1, $@) 0 0 0
97 0 $self->update_timer;
98 $self->model(
99 0 $self->server->model_class->new( { auth => $self->{auth} } ) );
100 }
101 0 return $self->{auth};
102 }
103  
104 =head2 client_id
105  
106 When called with no arguments, returns a hashref of identifying
107 information provided by the client. When key-value pairs are
108 provided, sets the client properties. See RFC 2971.
109  
110 =cut
111  
112 sub client_id {
113 0 ▶ my $self = shift;
Subroutine client_id: not called CC=4 SCAR=30.0
Pod documented
114 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
115 0 ▶ $self->{client} = {%{$self->{client} || {}}, @_};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"client"} or {} 0 0 0
116 }
117 0 ▶ return $self->{client} || {};
Condition0%0/2
$self->{'client'} || {}
l!l
00
MC/DC0%0/1
$self->{'client'} || {}
$self->{'client'}
Truth table0%0/2
A
A= $self->{'client'}
Aresult
11
00
118 }
119  
120 =head2 selected [MAILBOX], [READ_ONLY]
121  
122 Gets or sets the currently selected mailbox for this connection.
123 Changing mailboxes triggers the sending of untagged notifications to
124 the client, as well as calling L<Net::IMAP::Server::Mailbox/close> and
125 L<Net::IMAP::Server::Mailbox/select>.
126  
127 =cut
128  
129 sub selected {
130 0 ▶ my $self = shift;
Subroutine selected: not called CC=10 SCAR=47.0
Pod documented
131 0 my ($mailbox, $read_only) = @_;
132  
133 # This is just being called as a getter
134 0 ▶ return $self->_selected unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
135  
136 # This is a setter, but isn't actually changing the mailbox, nor
137 # changing the read-only-ness.
138 0 ▶ return $self->_selected if ($mailbox || "") eq ($self->_selected || "")
Branch: 1 branch
BranchTrueFalseTotal
if ($mailbox || "") eq ($self->_selected || "") and ($self->selected_read_only || 0) == ($read_only || 0) 0 0 0
Condition0%0/11
($mailbox || "") eq ($self->_selected || "") and ($self->selected_read_only || 0) == ($read_only || 0)
!ll&&!rl&&r
000
$mailbox || ''
l!l
00
$self->_selected || ''
l!l
00
$self->selected_read_only || 0
l!l
00
$read_only || 0
l!l
00
MC/DC0%0/2
($mailbox || "") eq ($self->_selected || "") and ($self->selected_read_only || 0) == ($read_only || 0)
$mailbox $self->selected_read_only
MC/DC0%0/1
$self->_selected || ''
$self->_selected
MC/DC0%0/1
$read_only || 0
$read_only
Truth table0%0/3
A and B
A= $mailboxB= $self->selected_read_only
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->_selected
Aresult
11
00
Truth table0%0/2
A
A= $read_only
Aresult
11
00
139 and ($self->selected_read_only || 0) == ($read_only || 0);
140  
141 # Otherwise, flush any untagged messages, close the old, and open
142 # the new.
143 0 $self->send_untagged;
144 0 ▶ $self->_selected->close if $self->_selected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_selected 0 0 0
145 0 $self->_selected( $mailbox );
146 0 ▶ if ($self->_selected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_selected) 0 0 0
147 0 $self->selected_read_only( $read_only );
148 0 $self->_selected->select;
149 }
150  
151 0 return $self->_selected;
152 }
153  
154 =head2 selected_read_only
155  
156 Returns true of the currently selected mailbox has been forced into
157 read-only mode. Note that the mailbox may be read-only for other
158 reasons, so checking L<Net::IMAP::Server::Mailbox/read_only> is
159 suggested instead.
160  
161 =head2 greeting
162  
163 Sends out a one-line untagged greeting to the client.
164  
165 =cut
166  
167 sub greeting {
168 0 ▶ my $self = shift;
Subroutine greeting: not called CC=1 SCAR=6.9
Pod documented
169 0 $self->untagged_response('OK IMAP4rev1 Server');
170 }
171  
172 =head2 handle_lines
173  
174 The main line handling loop. Since we are using L<Coro>, this cedes
175 to other coroutines whenever we block, given them a chance to run. We
176 additionally cede after handling every command.
177  
178 =cut
179  
180 sub handle_lines {
181 0 ▶ my $self = shift;
Subroutine handle_lines: not called CC=12 SCAR=50.5
Pod documented
182 0 $self->coro->prio(-4);
183  
184 0 eval {
185 0 $self->greeting;
186 0 ▶ while ( $self->io_handle and $_ = $self->io_handle->getline() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io_handle and $_ = $self->io_handle->getline) 0 0 0
Condition0%0/3
$self->io_handle and $_ = $self->io_handle->getline
!ll&&!rl&&r
000
MC/DC0%0/2
$self->io_handle and $_ = $self->io_handle->getline
$self->io_handle $_ = $self->io_handle->getline
Truth table0%0/3
A and B
A= $self->io_handleB= $_ = $self->io_handle->getline
ABresult
0X0
100
111
187 0 $self->handle_command($_);
188 0 $self->commands( $self->commands + 1 );
189 0 ▶ if ( $self->is_unauth
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_unauth and $self->server->unauth_commands and $self->commands >= $self->server->unauth_commands) 0 0 0
Condition0%0/6
$self->is_unauth and $self->server->unauth_commands and $self->commands >= $self->server->unauth_commands
!ll&&!rl&&r
000
$self->is_unauth and $self->server->unauth_commands
!ll&&!rl&&r
000
MC/DC0%0/3
$self->is_unauth and $self->server->unauth_commands and $self->commands >= $self->server->unauth_commands
$self->is_unauth $self->server->unauth_commands $self->commands >= $self->server->unauth_commands
Truth table0%0/4
A and B and C
A= $self->is_unauthB= $self->server->unauth_commandsC= $self->commands >= $self->server->unauth_commands
ABCresult
0XX0
10X0
1100
1111
190 and $self->server->unauth_commands
191 and $self->commands >= $self->server->unauth_commands )
192 {
193 0 $self->out(
194 "* BYE Don't noodle around so much before logging in!");
195 0 last;
196 }
197 0 $self->update_timer;
198 0 cede;
199 }
200  
201 0 $self->log( 4,
202 0 ▶ "-(@{[$self]},@{[$self->auth ? $self->auth->user : '???']},@{[$self->is_selected ? $self->selected->full_path : 'unselected']}): Connection closed by remote host"
Branch: 2 branches
BranchTrueFalseTotal
$self->auth ? : 0 0 0
$self->is_selected ? : 0 0 0
203 );
204 };
205 0 my $err = $@;
206 0 ▶ $self->log(1, $err)
Branch: 1 branch
BranchTrueFalseTotal
if $err and not $err eq "Error printing\n" || $err eq "Timeout\n" 0 0 0
Condition0%0/6
$err and not $err eq "Error printing\n" || $err eq "Timeout\n"
!ll&&!rl&&r
000
$err eq "Error printing\n" || $err eq "Timeout\n"
l!l&&r!l&&!r
000
MC/DC0%0/3
$err and not $err eq "Error printing\n" || $err eq "Timeout\n"
$err $err eq "Error printing\n" $err eq "Timeout\n"
Truth table0%0/4
A and not(B || C)
A= $errB= $err eq "Error printing\n"C= $err eq "Timeout\n"
ABCresult
0XX0
11X0
1010
1001
207 if $err and not( $err eq "Error printing\n" or $err eq "Timeout\n" );
208 0 ▶ eval { $self->out("* BYE Idle timeout; I fell asleep.") if $err eq "Timeout\n"; };
Branch: 1 branch
BranchTrueFalseTotal
if $err eq "Timeout\n" 0 0 0
209 0 $self->close;
210 }
211  
212 =head2 update_timer
213  
214 Updates the inactivity timer.
215  
216 =cut
217  
218 sub update_timer {
219 0 ▶ my $self = shift;
Subroutine update_timer: not called CC=4 SCAR=30.0
Pod documented
220 0 $self->timer(undef);
221 0 my $weakself = $self;
222 0 weaken($weakself);
223 my $timeout = sub {
224 0 ▶ $weakself->coro->throw("Timeout\n");
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 $weakself->coro->ready;
226 0 };
227 0 ▶ if ( $self->is_unauth and $self->server->unauth_idle ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_unauth and $self->server->unauth_idle) { } 0 0 0
elsif ($self->server->auth_idle) { } 0 0 0
Condition0%0/3
$self->is_unauth and $self->server->unauth_idle
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_unauth and $self->server->unauth_idle
$self->is_unauth $self->server->unauth_idle
Truth table0%0/3
A and B
A= $self->is_unauthB= $self->server->unauth_idle
ABresult
0X0
100
111
228 0 $self->timer( AnyEvent->timer( after => $self->server->unauth_idle, cb => $timeout ) );
229 } elsif ( $self->server->auth_idle ) {
230 0 $self->timer( AnyEvent->timer( after => $self->server->auth_idle, cb => $timeout ) );
231 }
232 }
233  
234 =head2 timer [AnyEvent watcher]
235  
236 Returns the L<AnyEvent> watcher in charge of the inactivity timer.
237  
238 =head2 commands
239  
240 Returns the number of client commands the connection has processed.
241  
242 =head2 handle_command
243  
244 Handles a single line from the client. This is not quite the same as
245 handling a command, because of client literals and continuation
246 commands. This also handles dispatch of client commands to
247 L<Net::IMAP::Server::Command> subclasses (see L</class_for>).
248  
249 Any errors generated while running commands will cause a C<NO Server
250 error> to be sent to the client -- unless the error message starts
251 with C<NO> or c<BAD>, in which case it will be relayed to the client.
252  
253 Returns the L<Net::IMAP::Server::Command> instance that was run, or
254 C<undef> if it was a continuation line or pending interactive command.
255  
256 =cut
257  
258 sub handle_command {
259 0 ▶ my $self = shift;
Subroutine handle_command: not called CC=12 SCAR=50.5
Pod documented
260 0 my $content = shift;
261  
262 0 my $output = $content;
263 0 $output =~ s/[\r\n]+$//;
264 0 $self->log( 4,
265 0 ▶ "C(@{[$self]},@{[$self->auth ? $self->auth->user : '???']},@{[$self->is_selected ? $self->selected->full_path : 'unselected']}): $output"
Branch: 2 branches
BranchTrueFalseTotal
$self->auth ? : 0 0 0
$self->is_selected ? : 0 0 0
266 );
267  
268 0 ▶ if ( $self->pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pending) 0 0 0
269 0 $self->pending->($content);
270 0 return;
271 }
272  
273 0 my ( $id, $cmd, $options ) = $self->parse_command($content);
274 0 ▶ return unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
275  
276 0 my $handler = $self->class_for($cmd)->new(
277 { server => $self->server,
278 connection => $self,
279 options_str => $options,
280 command_id => $id,
281 command => $cmd
282 }
283 );
284 0 ▶ return if $handler->has_literal;
Branch: 1 branch
BranchTrueFalseTotal
if $handler->has_literal 0 0 0
285  
286 0 ▶ eval { $handler->run() if $handler->validate; };
Branch: 1 branch
BranchTrueFalseTotal
if $handler->validate 0 0 0
287 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
288 0 ▶ if ($error eq "Timeout\n" or $error eq "Error printing\n") {
Branch: 3 branches
BranchTrueFalseTotal
if ($error eq "Timeout\n" or $error eq "Error printing\n") { } 0 0 0
elsif ($error =~ /^NO (.*)/) { } 0 0 0
elsif ($error =~ /^BAD (.*)/) { } 0 0 0
Condition0%0/3
$error eq "Timeout\n" or $error eq "Error printing\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
$error eq "Timeout\n" or $error eq "Error printing\n"
$error eq "Timeout\n" $error eq "Error printing\n"
Truth table0%0/3
A or B
A= $error eq "Timeout\n"B= $error eq "Error printing\n"
ABresult
1X1
011
000
289 0 die $error;
290 } elsif ($error =~ /^NO (.*)/) {
291 0 $handler->no_command($1);
292 } elsif ($error =~ /^BAD (.*)/) {
293 0 $handler->bad_command($1);
294 } else {
295 0 $handler->no_command("Server error");
296 0 $self->log(1, $error);
297 }
298 }
299 0 return $handler;
300 }
301  
302 =head2 class_for COMMAND
303  
304 Returns the package name that implements the given C<COMMAND>. See
305 L<Net::IMAP::Server/add_command>.
306  
307 =cut
308  
309 sub class_for {
310 0 ▶ my $self = shift;
Subroutine class_for: not called CC=7 SCAR=40.3
Pod documented
311 0 my $cmd = shift;
312 0 my $classref = $self->server->command_class;
313 0 ▶ my $cmd_class = $classref->{lc $cmd} || $classref->{$cmd} || $classref->{uc $cmd}
Condition0%0/8
$$classref{lc $cmd} || $classref->{$cmd} || $$classref{uc $cmd} || "Net::IMAP::Server::Command::$cmd"
l!l
00
$$classref{lc $cmd} or $classref->{$cmd} or $$classref{uc $cmd}
l!l&&r!l&&!r
000
$$classref{lc $cmd} or $classref->{$cmd}
l!l&&r!l&&!r
000
MC/DC0%0/3
$$classref{lc $cmd} || $classref->{$cmd} || $$classref{uc $cmd} || "Net::IMAP::Server::Command::$cmd"
$$classref{lc $cmd} $classref->{$cmd} $$classref{uc $cmd}
Truth table0%0/4
A or B or C
A= $$classref{lc $cmd}B= $classref->{$cmd}C= $$classref{uc $cmd}
ABCresult
1XX1
01X1
0011
0000
314 || "Net::IMAP::Server::Command::$cmd";
315 0 my $class_path = $cmd_class;
316 0 $class_path =~ s{::}{/}g;
317  
318 0 $cmd_class->require();
319 0 my $err = $@;
320 0 ▶ if ($err and $err !~ /^Can't locate $class_path.pm in \@INC/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and not $err =~ /^Can't locate $class_path.pm in \@INC/) 0 0 0
Condition0%0/3
$err and not $err =~ /^Can't locate $class_path.pm in \@INC/
!ll&&!rl&&r
000
MC/DC0%0/2
$err and not $err =~ /^Can't locate $class_path.pm in \@INC/
$err not $err =~ /^Can't locate $class_path.pm in \@INC/
Truth table0%0/3
A and not(B)
A= $errB= not $err =~ /^Can't locate $class_path.pm in \@INC/
ABresult
0X0
100
111
321 0 $self->log(1, $@);
322 0 $cmd_class = "Net::IMAP::Server::Error";
323 }
324  
325 0 ▶ return $cmd_class->can('run') ? $cmd_class : "Net::IMAP::Server::Command";
Branch: 1 branch
BranchTrueFalseTotal
$cmd_class->can('run') ? : 0 0 0
326 }
327  
328 =head2 pending
329  
330 If a connection has pending state, contains the callback that will
331 receive the next line of input.
332  
333 =cut
334  
335 =head2 close
336  
337 Shuts down this connection, also closing the model and mailboxes.
338  
339 =cut
340  
341 sub close {
342 0 ▶ my $self = shift;
Subroutine close: not called CC=6 SCAR=37.4
Pod documented
343 0 ▶ if ( $self->io_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io_handle) 0 0 0
344 0 $self->io_handle->close;
345 0 $self->io_handle(undef);
346 }
347 0 ▶ $self->timer( undef ) if $self->timer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->timer 0 0 0
348 0 ▶ $self->selected->close if $self->selected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->selected 0 0 0
349 0 ▶ $self->model->close if $self->model;
Branch: 1 branch
BranchTrueFalseTotal
if $self->model 0 0 0
350 0 ▶ $self->server->connection(undef) if $self->server;
Branch: 1 branch
BranchTrueFalseTotal
if $self->server 0 0 0
351 0 $self->coro(undef);
352 }
353  
354 =head2 parse_command LINE
355  
356 Parses the line into the C<tag>, C<command>, and C<options>. Returns
357 undef if parsing fails for some reason.
358  
359 =cut
360  
361 sub parse_command {
362 0 ▶ my $self = shift;
Subroutine parse_command: not called CC=4 SCAR=30.0
Pod documented
363 0 my $line = shift;
364 0 $line =~ s/[\r\n]+$//;
365 0 my $TAG = qr/([^\(\)\{ \*\%"\\\+}]+)/;
366 0 ▶ unless ( $line =~ /^$TAG\s+(\w+)(?:\s+(.+?))?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^$TAG\s+(\w+)(?:\s+(.+?))?$/) 0 0 0
367 0 ▶ if ( $line !~ /^$TAG\s+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^$TAG\s+/) { } 0 0 0
368 0 $self->out("* BAD Invalid tag");
369 } else {
370 0 $self->out("* BAD Null command ('$line')");
371 }
372 0 return undef;
373 }
374  
375 0 my $id = $1;
376 0 my $cmd = $2;
377 0 ▶ my $args = $3 || '';
Condition0%0/2
$3 || ''
l!l
00
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
378 0 $cmd = ucfirst( lc($cmd) );
379 0 return ( $id, $cmd, $args );
380 }
381  
382 =head2 is_unauth
383  
384 Returns true if the connection is unauthenticated.
385  
386 =cut
387  
388 sub is_unauth {
389 0 ▶ my $self = shift;
Subroutine is_unauth: not called CC=1 SCAR=6.9
Pod documented
390 0 return not defined $self->auth;
391 }
392  
393 =head2 is_auth
394  
395 Returns true if the connection is authenticated.
396  
397 =cut
398  
399 sub is_auth {
400 0 ▶ my $self = shift;
Subroutine is_auth: not called CC=1 SCAR=6.9
Pod documented
401 0 return defined $self->auth;
402 }
403  
404 =head2 is_selected
405  
406 Returns true if the connection has selected a mailbox.
407  
408 =cut
409  
410 sub is_selected {
411 0 ▶ my $self = shift;
Subroutine is_selected: not called CC=1 SCAR=6.9
Pod documented
412 0 return defined $self->selected;
413 }
414  
415 =head2 is_encrypted
416  
417 Returns true if the connection is protected by SSL or TLS.
418  
419 =cut
420  
421 sub is_encrypted {
422 0 ▶ my $self = shift;
Subroutine is_encrypted: not called CC=1 SCAR=6.9
Pod documented
423 0 return $self->io_handle->is_ssl;
424 }
425  
426 =head2 poll
427  
428 Polls the currently selected mailbox, and resets the poll timer.
429  
430 =cut
431  
432 sub poll {
433 0 ▶ my $self = shift;
Subroutine poll: not called CC=1 SCAR=6.9
Pod documented
434 0 $self->selected->poll;
435 0 $self->last_poll(time);
436 }
437  
438 =head2 force_poll
439  
440 Forces a poll of the selected mailbox the next chance we get.
441  
442 =cut
443  
444 sub force_poll {
445 0 ▶ my $self = shift;
Subroutine force_poll: not called CC=1 SCAR=6.9
Pod documented
446 0 $self->last_poll(0);
447 }
448  
449 =head2 last_poll
450  
451 Gets or sets the last time the selected mailbox was polled, in seconds
452 since the epoch.
453  
454 =head2 previous_exists
455  
456 The high-water mark of how many messages the client has been told are
457 in the mailbox.
458  
459 =head2 send_untagged
460  
461 Sends any untagged updates about the current mailbox to the client.
462  
463 =cut
464  
465 sub send_untagged {
466 0 ▶ my $self = shift;
Subroutine send_untagged: not called CC=11 SCAR=48.8
Pod documented
467 0 my %args = (
468 expunged => 1,
469 @_
470 );
471 0 ▶ return unless $self->is_auth and $self->is_selected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_auth and $self->is_selected 0 0 0
Condition0%0/3
$self->is_auth and $self->is_selected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_auth and $self->is_selected
$self->is_auth $self->is_selected
Truth table0%0/3
A and B
A= $self->is_authB= $self->is_selected
ABresult
0X0
100
111
472  
473 0 ▶ if ( time >= $self->last_poll + $self->server->poll_every ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time >= $self->last_poll + $self->server->poll_every) 0 0 0
474 # We record that we're in a poll so that EXPUNGE knows that
475 # this connection should get a temporary message store if need
476 # be.
477 0 $self->in_poll(1);
478 0 $self->poll;
479 0 $self->in_poll(0);
480 }
481  
482 0 for my $s ( keys %{ $self->_unsent_fetch } ) {
483 0 my ($m) = $self->get_messages($s);
484 $self->untagged_response(
485 $s
486 . " FETCH "
487 . Net::IMAP::Server::Command->data_out(
488 0 [ $m->fetch( [ keys %{ $self->_unsent_fetch->{$s} } ] ) ]
489 )
490 );
491 }
492 0 $self->_unsent_fetch( {} );
493  
494 0 ▶ if ( $args{expunged} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"expunged"}) 0 0 0
495  
496 # Make sure that they know of at least the existence of what's being expunged.
497 0 my $max = 0;
498 0 ▶ $max = $max < $_ ? $_ : $max for @{ $self->_unsent_expunge };
Branch: 1 branch
BranchTrueFalseTotal
$max < $_ ? : 0 0 0
499 0 ▶ $self->untagged_response("$max EXISTS")
Branch: 1 branch
BranchTrueFalseTotal
if $max > $self->previous_exists 0 0 0
500 if $max > $self->previous_exists;
501  
502 # Send the expunges, clear out the temporary message store
503 $self->previous_exists(
504 0 $self->previous_exists - @{ $self->_unsent_expunge } );
505 0 $self->untagged_response( map {"$_ EXPUNGE"}
506 0 @{ $self->_unsent_expunge } );
507 0 $self->_unsent_expunge( [] );
508 0 $self->temporary_messages(undef);
509 }
510  
511 # Let them know of more EXISTS
512 0 my $expected = $self->previous_exists;
513 0 ▶ my $now = @{ $self->temporary_messages || $self->selected->messages };
Branch: 1 branch
BranchTrueFalseTotal
$self->temporary_messages or $self->selected->messages 0 0 0
514 0 ▶ $self->untagged_response( $now . ' EXISTS' ) if $expected != $now;
Branch: 1 branch
BranchTrueFalseTotal
if $expected != $now 0 0 0
515 0 $self->previous_exists($now);
516 }
517  
518 =head2 get_messages STR
519  
520 Parses and returns messages fitting the given sequence range. This is
521 on the connection and not the mailbox because messages have
522 connection-dependent sequence numbers.
523  
524 =cut
525  
526 13 ▶use constant SEQUENCE_TOKEN => qr/(?:\d+:\d+|\d+:\*|\*:\d+|\d+|\*)/;
Subroutine BEGIN: called CC=1 SCAR=0.0
527 13 ▶use constant SEQUENCE_STRING => qr/^@{[SEQUENCE_TOKEN]}(,@{[SEQUENCE_TOKEN]})*$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
528 sub get_messages {
529 0 ▶ my $self = shift;
Subroutine get_messages: not called CC=11 SCAR=48.8
Pod documented
530 0 my $str = shift;
531  
532 0 ▶ my $messages = $self->temporary_messages || $self->selected->messages;
Condition0%0/3
$self->temporary_messages || $self->selected->messages
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->temporary_messages || $self->selected->messages
$self->temporary_messages $self->selected->messages
Truth table0%0/3
A || B
A= $self->temporary_messagesB= $self->selected->messages
ABresult
1X1
011
000
533  
534 0 my %ids;
535 0 for ( split ',', $str ) {
536 0 ▶ if (/^(\d+):(\d+)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^(\d+):(\d+)$/) { } 0 0 0
elsif (/^(\d+):\*$/ or /^\*:(\d+)$/) { } 0 0 0
elsif (/^(\d+)$/) { } 0 0 0
elsif (/^\*$/) { } 0 0 0
537 0 ▶ $ids{$_}++ for $2 > $1 ? $1 .. $2 : $2 .. $1;
Branch: 1 branch
BranchTrueFalseTotal
$2 > $1 ? : 0 0 0
538 ▶ } elsif ( /^(\d+):\*$/ or /^\*:(\d+)$/ ) {
Condition0%0/3
/^(\d+):\*$/ or /^\*:(\d+)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^(\d+):\*$/ or /^\*:(\d+)$/
/^(\d+):\*$/ /^\*:(\d+)$/
Truth table0%0/3
A or B
A= /^(\d+):\*$/B= /^\*:(\d+)$/
ABresult
1X1
011
000
539 0 $ids{$_}++ for @{$messages} + 0, $1 .. @{$messages} + 0;
540 } elsif (/^(\d+)$/) {
541 0 $ids{$1}++;
542 } elsif (/^\*$/) {
543 0 $ids{ @{$messages} + 0 }++;
544 }
545 }
546 0 return grep {defined}
547 0 map { $messages->[ $_ - 1 ] } sort { $a <=> $b } keys %ids;
548 }
549  
550 =head2 sequence MESSAGE
551  
552 Returns the sequence number for the given message.
553  
554 =cut
555  
556 sub sequence {
557 0 ▶ my $self = shift;
Subroutine sequence: not called CC=2 SCAR=17.9
Pod documented
558 0 my $message = shift;
559  
560 0 ▶ return $message->sequence unless $self->temporary_messages;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->temporary_messages 0 0 0
561 0 return $self->temporary_sequence_map->{$message};
562 }
563  
564 =head2 capability
565  
566 Returns the current capability list for this connection, as a string.
567 Connections not under TLS or SSL always have the C<LOGINDISABLED>
568 capability, and no authentication capabilities. The
569 L<Net::IMAP::Server/auth_class>'s
570 L<Net::IMAP::Server::DefaultAuth/sasl_provides> method is used to list
571 known C<AUTH=> types.
572  
573 =cut
574  
575 sub capability {
576 0 ▶ my $self = shift;
Subroutine capability: not called CC=5 SCAR=34.0
Pod documented
577  
578 0 my $base = $self->server->capability;
579 0 my @words = split " ", $base;
580  
581 # Skip STARTTLS if we're encrpyted
582 0 ▶ @words = grep {$_ ne "STARTTLS"} @words
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_encrypted 0 0 0
583 if $self->is_encrypted;
584  
585 # If we're auth'd, no need to list any AUTH or LOGINDISABLED
586 0 ▶ unless ($self->auth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->auth) 0 0 0
587 0 ▶ my $auth = $self->auth || $self->server->auth_class->new;
Condition0%0/3
$self->auth || $self->server->auth_class->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->auth || $self->server->auth_class->new
$self->auth $self->server->auth_class->new
Truth table0%0/3
A || B
A= $self->authB= $self->server->auth_class->new
ABresult
1X1
011
000
588 0 my @auth = $auth->sasl_provides;
589 0 ▶ unless ($self->is_encrypted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_encrypted) 0 0 0
590 # Lack of encrpytion makes us turn off all plaintext auth
591 0 push @words, "LOGINDISABLED";
592 0 @auth = grep {$_ ne "PLAIN"} @auth;
593 }
594 0 push @words, map {"AUTH=$_"} @auth;
595 }
596  
597 0 return join(" ", @words);
598 }
599  
600 =head2 log SEVERITY, MESSAGE
601  
602 Defers to L<Net::IMAP::Server/log>.
603  
604 =cut
605  
606 sub log {
607 0 ▶ my $self = shift;
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
608 0 $self->server->log(@_);
609 }
610  
611 =head2 untagged_response STRING
612  
613 Sends an untagged response to the client; a newline ia automatically
614 appended.
615  
616 =cut
617  
618 sub untagged_response {
619 0 ▶ my $self = shift;
Subroutine untagged_response: not called CC=2 SCAR=17.9
Pod documented
620 0 $self->out("* $_") for grep defined, @_;
621 }
622  
623 =head2 out STRING
624  
625 Sends the message to the client. If the client's connection has
626 dropped, or the send fails for whatever reason, L</close> the
627 connection and then die, which is caught by L</handle_lines>.
628  
629 =cut
630  
631 sub out {
632 0 ▶ my $self = shift;
Subroutine out: not called CC=6 SCAR=37.4
Pod documented
633 0 my $msg = shift;
634 0 ▶ if ( $self->io_handle and $self->io_handle->peerport ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io_handle and $self->io_handle->peerport) { } 0 0 0
Condition0%0/3
$self->io_handle and $self->io_handle->peerport
!ll&&!rl&&r
000
MC/DC0%0/2
$self->io_handle and $self->io_handle->peerport
$self->io_handle $self->io_handle->peerport
Truth table0%0/3
A and B
A= $self->io_handleB= $self->io_handle->peerport
ABresult
0X0
100
111
635 0 ▶ if ( $self->io_handle->print( $msg . "\r\n" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io_handle->print($msg . "\r\n")) { } 0 0 0
636 0 $self->log( 4,
637 0 ▶ "S(@{[$self]},@{[$self->auth ? $self->auth->user : '???']},@{[$self->is_selected ? $self->selected->full_path : 'unselected']}): $msg"
Branch: 2 branches
BranchTrueFalseTotal
$self->auth ? : 0 0 0
$self->is_selected ? : 0 0 0
638 );
639 } else {
640 0 $self->close;
641 0 die "Error printing\n";
642 }
643 } else {
644 0 $self->close;
645 0 die "Error printing\n";
646 }
647 }
648  
649 13 1;
« Server/Mailbox.pm ↑ Summary Server/Command.pm »