Server/Command.pm

filter: Statementstmt 13.1% 17 / 129 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.8% 5 / 24 Podpod 100% 18 / 18 totaltotal 17.3% 40 / 231 CC 31 SCAR 38.5
CC
31
Cov
9%
data_out
132.0
parse_options
90.0
__ANON__
20.0
CRAP 46.9
« Server/Connection.pm ↑ Summary Server/Command/Copy.pm »
1 package Net::IMAP::Server::Command;
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 13 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Regexp::Common qw/delimited balanced/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 __PACKAGE__->mk_accessors(
10 qw(server connection command_id options_str command _parsed_options _literals _pending_literal)
11 );
12  
13 =head1 NAME
14  
15 Net::IMAP::Server::Command - A command in the IMAP server
16  
17 =head1 DESCRIPTION
18  
19 Commands the IMAP server knows about should be subclasses of this.
20 They will want to override the L</validate> and L</run> methods.
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 Called by the connection to create a new command.
27  
28 =cut
29  
30 sub new {
31 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
32 0 my $self = $class->SUPER::new(@_);
33 0 $self->_parsed_options( [] );
34 0 $self->_literals( [] );
35 0 return $self;
36 }
37  
38 =head2 server
39  
40 Gets or sets the L<Net::IMAP::Server> associated with this command.
41  
42 =cut
43  
44 =head2 connection
45  
46 Gets or sets the L<Net::IMAP::Server::Connection> associated with this
47 command.
48  
49 =cut
50  
51 =head2 validate
52  
53 Called before the command is run. If it returns a false value, the
54 command is not run; it will probably want to inspect
55 L</parsed_options>. If C<validate> returns a false value, it is
56 responsible for calling L</no_command> or L</bad_command> to notify
57 the client of the failure. Handily, these return a false value.
58  
59 =cut
60  
61 sub validate {
62 0 ▶ return 1;
Subroutine validate: not called CC=1 SCAR=6.9
Pod documented
63 }
64  
65 =head2 run
66  
67 Does the guts of the command. The return value is ignored; the
68 command is in charge of eventually sending one of L</ok_command>,
69 L</bad_command>, or L</no_command> to the client.
70  
71 The default implementation simply always response with
72 L</bad_command>.
73  
74 =cut
75  
76 sub run {
77 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
78  
79 0 $self->bad_command( "command '" . uc($self->command) . "' not recognized" );
80 }
81  
82 =head2 has_literal
83  
84 Analyzes the options line, and returns true if the line has literals
85 (as defined in the RFC, a literal is of the form C<{42}>). If the
86 line has literals, installs a L<Net::IMAP::Server::Connection/pending>
87 callback to continue the parsing, and returns true.
88  
89 =cut
90  
91 sub has_literal {
92 0 ▶ my $self = shift;
Subroutine has_literal: not called CC=3 SCAR=24.8
Pod documented
93 0 ▶ unless ( $self->options_str =~ /\{(\d+)(\+)?\}[\r\n]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->options_str =~ /\{(\d+)(\+)?\}[\r\n]*$/) 0 0 0
94 0 $self->parse_options;
95 0 return;
96 }
97  
98 0 my $options = $self->options_str;
99 0 my $next = $#{ $self->_literals } + 1;
100 0 $options =~ s/\{(\d+)(\+)?\}[\r\n]*$/{{$next}}/;
101 0 $self->_pending_literal($1);
102 0 $self->options_str($options);
103  
104 # Pending
105 $self->connection->pending(
106 sub {
107 0 ▶ my $content = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
108 0 ▶ if ( length $content <= $self->_pending_literal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content <= $self->_pending_literal) { } 0 0 0
109 0 $self->_literals->[$next] .= $content;
110 0 $self->_pending_literal(
111 $self->_pending_literal - length $content );
112 } else {
113 0 $self->_literals->[$next]
114 .= substr( $content, 0, $self->_pending_literal, "" );
115 0 $self->connection->pending(undef);
116 0 $self->options_str( $self->options_str . $content );
117 0 ▶ return if $self->has_literal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_literal 0 0 0
118 0 ▶ $self->run if $self->validate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validate 0 0 0
119 }
120 }
121 0 );
122 0 ▶ $self->out("+ Continue") unless $2;
Branch: 1 branch
BranchTrueFalseTotal
unless $2 0 0 0
123 0 return 1;
124 }
125  
126 =head2 parse_options
127  
128 Parses the options, and puts the results (which may be a data
129 structure) into L<parsed_options>.
130  
131 =cut
132  
133 sub parse_options {
134 0 ▶ my $self = shift;
Subroutine parse_options: not called CC=9 SCAR=45.0
Pod documented
135 0 my $str = shift;
136  
137 0 ▶ return $self->_parsed_options
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str or defined $self->options_str 0 0 0
Condition0%0/3
defined $str or defined $self->options_str
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $str or defined $self->options_str
defined $str defined $self->options_str
Truth table0%0/3
A or B
A= defined $strB= defined $self->options_str
ABresult
1X1
011
000
138 if not defined $str and not defined $self->options_str;
139  
140 0 ▶ my $to_parse = defined $str ? $str : $self->options_str;
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 0 0 0
141  
142 0 my $token = qr/^($RE{delimited}{-delim=>'"'}{-esc=>'\\'}
143 |$RE{balanced}{-parens=>'()'}
144 |\S+$RE{balanced}{-parens=>'()[]<>'}
145 |\S+)\s*/x;
146  
147 0 my @parsed;
148 0 ▶ while ($to_parse =~ s/$token//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_parse =~ s/$token//) 0 0 0
149 0 my $term = $1;
150 0 ▶ if ( $term =~ /^$RE{delimited}{-delim=>'"'}{-esc=>'\\'}{-keep}$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($term =~ /^$Net::IMAP::Server::Command::RE{'delimited'}{join $;, '-delim', '"'}{join $;, '-esc', '\\'}{'-keep'}$/) { } 0 0 0
elsif ($term =~ /^$Net::IMAP::Server::Command::RE{'balanced'}{join $;, '-parens', '()'}$/) { } 0 0 0
elsif ($term =~ /^\{\{(\d+)\}\}$/) { } 0 0 0
151 0 my $value = $3;
152 0 $value =~ s/\\([\\"])/$1/g;
153 0 push @parsed, $value;
154 } elsif ( $term =~ /^$RE{balanced}{-parens=>'()'}$/ ) {
155 0 $term =~ s/^\((.*)\)$/$1/;
156 0 push @parsed, [ $self->parse_options($term) ];
157 } elsif ( $term =~ /^\{\{(\d+)\}\}$/ ) {
158 0 push @parsed, $self->_literals->[$1];
159 } else {
160 0 push @parsed, $term;
161 }
162 }
163 0 ▶ return @parsed if defined $str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 0 0 0
164  
165 0 $self->options_str(undef);
166 0 $self->_parsed_options( [ @{ $self->_parsed_options }, @parsed ] );
167 }
168  
169 =head2 command_id
170  
171 Returns the (arbitrary) string that the client identified the command with.
172  
173 =cut
174  
175 =head2 parsed_options
176  
177 Returns the list of options to the command.
178  
179 =cut
180  
181 sub parsed_options {
182 0 ▶ my $self = shift;
Subroutine parsed_options: not called CC=1 SCAR=6.9
Pod documented
183 0 return @{ $self->_parsed_options(@_) };
184 }
185  
186 =head2 options_str
187  
188 Returns the flat string representation of the options the client gave.
189  
190 =cut
191  
192 =head2 data_out DATA
193  
194 Returns a string representing the most probable IMAP string that
195 conveys the C<DATA>.
196  
197 =over
198  
199 =item *
200  
201 Array references are converted into "parenthesized lists," and each
202 element is recursively output.
203  
204 =item *
205  
206 Scalar references are dereferenced and returned as-is.
207  
208 =item *
209  
210 C<undef> is output as C<NIL>.
211  
212 =item *
213  
214 Scalar values containing special characters are output as literals
215  
216 =item *
217  
218 Purely numerical scalar values are output with no change
219  
220 =item *
221  
222 All other scalar values are output within quotes.
223  
224 =back
225  
226 Since the IMAP specification contains nothing which is similar to a
227 hash, hash references are treated specially; specifically, the C<type>
228 key is taken to be how the C<value> key should be output. Options for
229 C<type> are C<string> or C<literal>.
230  
231 =cut
232  
233 sub data_out {
234 0 ▶ my $self = shift;
Subroutine data_out: not called CC=11 SCAR=48.8
Pod documented
235 0 my $data = shift;
236 0 ▶ if ( ref $data eq "ARRAY" ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data eq 'SCALAR') { } 0 0 0
elsif (ref $data eq 'HASH') { } 0 0 0
elsif (not ref $data) { } 0 0 0
237 0 return "(" . join( " ", map { $self->data_out($_) } @{$data} ) . ")";
238 } elsif ( ref $data eq "SCALAR" ) {
239 0 return $$data;
240 } elsif ( ref $data eq "HASH" ) {
241 0 ▶ if ( $data->{type} eq "string" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{'type'} eq 'string') { } 0 0 0
elsif ($data->{'type'} eq 'literal') { } 0 0 0
242 0 ▶ if ( $data =~ /[{"\r\n%*\\\[]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /[{"\r\n%*\\\[]/) { } 0 0 0
243 0 return "{" . ( length( $data->{value} ) ) . "}\r\n$data";
244 } else {
245 0 return '"' . $data->{value} . '"';
246 }
247 } elsif ( $data->{type} eq "literal" ) {
248 0 return "{" . ( length( $data->{value} ) ) . "}\r\n$data";
249 }
250 } elsif ( not ref $data ) {
251 0 ▶ if ( not defined $data ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $data) { } 0 0 0
elsif ($data =~ /[{"\r\n%*\\\[]/) { } 0 0 0
elsif ($data =~ /^\d+$/) { } 0 0 0
252 0 return "NIL";
253 } elsif ( $data =~ /[{"\r\n%*\\\[]/ ) {
254 0 return "{" . ( length($data) ) . "}\r\n$data";
255 } elsif ( $data =~ /^\d+$/ ) {
256 0 return $data;
257 } else {
258 0 return qq{"$data"};
259 }
260 }
261 0 return "";
262 }
263  
264 =head2 untagged_response STRING
265  
266 Sends an untagged response to the client.
267  
268 =cut
269  
270 sub untagged_response {
271 0 ▶ my $self = shift;
Subroutine untagged_response: not called CC=1 SCAR=6.9
Pod documented
272 0 $self->connection->untagged_response(@_);
273 }
274  
275 =head2 tagged_response
276  
277 Sends a tagged response to the client.
278  
279 =cut
280  
281 sub tagged_response {
282 0 ▶ my $self = shift;
Subroutine tagged_response: not called CC=2 SCAR=17.9
Pod documented
283 $self->untagged_response( uc( $self->command ) . " $_" )
284 0 for grep defined, @_;
285 }
286  
287 =head2 poll_after
288  
289 Returns a true value if the command should send untagged updates about
290 the selected mailbox after the command completes. Defaults to always
291 true.
292  
293 =cut
294  
295 0 ▶sub poll_after {1}
Subroutine poll_after: not called CC=1 SCAR=6.9
Pod documented
296  
297 =head2 send_untagged
298  
299 Sends untagged updates about the currently selected inbox to the
300 client using L<Net::IMAP::Server::Connection/send_untagged>, but only
301 if the command has a true L</poll_after>.
302  
303 =cut
304  
305 sub send_untagged {
306 0 ▶ my $self = shift;
Subroutine send_untagged: not called CC=2 SCAR=17.9
Pod documented
307 0 ▶ $self->connection->send_untagged(@_) if $self->poll_after;
Branch: 1 branch
BranchTrueFalseTotal
if $self->poll_after 0 0 0
308 }
309  
310 =head2 ok_command MESSAGE [, RESPONSECODE => STRING, ...]
311  
312 Sends untagged OK responses for any C<RESPONSECODE> pairs, then
313 outputs untagged messages via L</send_untagged>, then sends a tagged
314 OK with the given C<MESSAGE>.
315  
316 =cut
317  
318 sub ok_command {
319 0 ▶ my $self = shift;
Subroutine ok_command: not called CC=2 SCAR=17.9
Pod documented
320 0 my $message = shift;
321 0 my %extra_responses = (@_);
322 0 for ( keys %extra_responses ) {
323 $self->untagged_response(
324 0 "OK [" . uc($_) . "] " . $extra_responses{$_} );
325 }
326 0 $self->send_untagged;
327 0 $self->out( $self->command_id . " OK $message" );
328 0 return 1;
329 }
330  
331 =head2 ok_completed [RESPONSECODE => STRING]
332  
333 Sends an C<OK COMPLETED> tagged response to the client.
334  
335 =cut
336  
337 sub ok_completed {
338 0 ▶ my $self = shift;
Subroutine ok_completed: not called CC=1 SCAR=6.9
Pod documented
339 0 my %extra_responses = (@_);
340 0 $self->ok_command( uc( $self->command ) . " COMPLETED",
341 %extra_responses );
342 }
343  
344 =head2 no_command MESSAGE [, RESPONSECODE => STRING, ...]
345  
346 Sends untagged NO responses for any C<RESPONSECODE> pairs, then
347 outputs untagged messages via L</send_untagged>, then sends a tagged
348 OK with the given C<MESSAGE>.
349  
350 =cut
351  
352 sub no_command {
353 0 ▶ my $self = shift;
Subroutine no_command: not called CC=2 SCAR=17.9
Pod documented
354 0 my $message = shift;
355 0 my %extra_responses = (@_);
356 0 for ( keys %extra_responses ) {
357 $self->untagged_response(
358 0 "NO [" . uc($_) . "] " . $extra_responses{$_} );
359 }
360 0 $self->out( $self->command_id . " NO $message" );
361 0 return 0;
362 }
363  
364 =head2 bad_command REASON
365  
366 Sends any untagged updates to the client using L</send_untagged>, then
367 sends a tagged C<BAD> response with the given C<REASON>.
368  
369 =cut
370  
371 sub bad_command {
372 0 ▶ my $self = shift;
Subroutine bad_command: not called CC=1 SCAR=6.9
Pod documented
373 0 my $reason = shift;
374 0 $self->send_untagged;
375 0 $self->out( $self->command_id . " BAD $reason" );
376 0 return 0;
377 }
378  
379 =head2 valid_mailbox NAME
380  
381 Returns false and calls L</bad_command> if the given C<NAME> is a valid
382 name for a mailbox. This only checks that is passes UTF-7 encoding
383 checks, and that it contains no 8-bit characters. If the name is valid,
384 simply returns 1.
385  
386 =cut
387  
388 sub valid_mailbox {
389 0 ▶ my $self = shift;
Subroutine valid_mailbox: not called CC=4 SCAR=30.0
Pod documented
390 0 my ($name) = @_;
391  
392 # Check for high-bit characters
393 0 ▶ return $self->bad_command("Mailbox name contains 8-bit data")
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[\x80-\xFF]/ 0 0 0
394 if $name =~ /[\x80-\xFF]/;
395  
396 # This both ensures that the mailbox path is valid UTF-7, and that
397 # there aren't bogusly encoded characters (like '/' -> '&AC8-')
398 0 my $roundtrip = eval {
399 0 Encode::encode( 'IMAP-UTF-7',
400 Encode::decode( 'IMAP-UTF-7', $name ) );
401 };
402 0 ▶ return $self->bad_command("Invalid UTF-7 encoding")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roundtrip and $roundtrip eq $name 0 0 0
Condition0%0/3
defined $roundtrip and $roundtrip eq $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $roundtrip and $roundtrip eq $name
defined $roundtrip $roundtrip eq $name
Truth table0%0/3
A and B
A= defined $roundtripB= $roundtrip eq $name
ABresult
0X0
100
111
403 unless defined $roundtrip and $roundtrip eq $name;
404  
405 0 return 1;
406 }
407  
408 =head2 log SEVERITY, MESSAGE
409  
410 Defers to L<Net::IMAP::Server::Connection/log>.
411  
412 =cut
413  
414 sub log {
415 0 ▶ my $self = shift;
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
416 0 $self->connection->log(@_);
417 }
418  
419 =head2 out MESSAGE
420  
421 Identical to L<Net::IMAP::Server::Connection/out>.
422  
423 =cut
424  
425 sub out {
426 0 ▶ my $self = shift;
Subroutine out: not called CC=1 SCAR=6.9
Pod documented
427 0 $self->connection->out(@_);
428 }
429  
430 13 1;
« Server/Connection.pm ↑ Summary Server/Command/Copy.pm »