Server/Mailbox.pm

filter: Statementstmt 6.3% 14 / 221 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 11.4% 4 / 35 Podpod 100% 30 / 30 totaltotal 10.7% 48 / 446 CC 73 SCAR 45.8
CC
73
Cov
4%
expunge
380.0
status
90.0
get_uids
90.0
CRAP 97.3
« Server/Message.pm ↑ Summary Server/Connection.pm »
1 package Net::IMAP::Server::Mailbox;
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 Net::IMAP::Server::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 __PACKAGE__->mk_accessors(
10 qw(is_inbox parent children _path uidnext uids uidvalidity messages subscribed is_selectable)
11 );
12  
13 =head1 NAME
14  
15 Net::IMAP::Server::Mailbox - A user's view of a mailbox
16  
17 =head1 DESCRIPTION
18  
19 This class encapsulates the view of messages in a mailbox. You may
20 wish to subclass this class in order to source our messages from, say,
21 a database.
22  
23 =head1 METHODS
24  
25 =head2 Initialization
26  
27 =head3 new
28  
29 Creates a new mailbox; returns C<undef> if a mailbox with the same
30 full path already exists. It calls L</init>, then L</load_data>.
31  
32 =cut
33  
34 sub new {
35 0 ▶ my $class = shift;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
36 0 my $self = $class->SUPER::new(@_);
37 return
38 if $self->parent
39 0 and grep { $self->full_path eq $_->full_path }
40 0 ▶ @{ $self->parent->children };
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent and grep {$self->full_path eq $_->full_path;} @{$self->parent->children;} 0 0 0
Condition0%0/3
$self->parent and grep {$self->full_path eq $_->full_path;} @{$self->parent->children;}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->parent and grep {$self->full_path eq $_->full_path;} @{$self->parent->children;}
$self->parent grep {$self->full_path eq $_->full_path;} @{$self->parent->children;}
Truth table0%0/3
A and B
A= $self->parentB= grep {$self->full_path eq $_->full_path;} @{$self->parent->children;}
ABresult
0X0
100
111
41 0 ▶ $self->is_inbox(1)
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent and not $self->parent->parent and $self->name =~ /^inbox$/i 0 0 0
Condition0%0/6
$self->parent and not $self->parent->parent and $self->name =~ /^inbox$/i
!ll&&!rl&&r
000
$self->parent and not $self->parent->parent
!ll&&!rl&&r
000
MC/DC0%0/3
$self->parent and not $self->parent->parent and $self->name =~ /^inbox$/i
$self->parent not $self->parent->parent $self->name =~ /^inbox$/i
Truth table0%0/4
A and not(B) and C
A= $self->parentB= not $self->parent->parentC= $self->name =~ /^inbox$/i
ABCresult
0XX0
10X0
1100
1111
42 if $self->parent
43 and not $self->parent->parent
44 and $self->name =~ /^inbox$/i;
45 0 $self->init;
46 0 $self->load_data;
47 0 return $self;
48 }
49  
50 =head3 init
51  
52 Sets up basic properties of the mailbox:
53  
54 =over
55  
56 =item *
57  
58 L</uidnext> is set to 1000
59  
60 =item *
61  
62 L</messages> and L</uids> are initialized to an empty list reference
63 and an empty hash reference, respectively.
64  
65 =item *
66  
67 L</children> is set to an empty list reference.
68  
69 =item *
70  
71 L</uidvalidity> is set to the number of seconds since the epoch.
72  
73 =item *
74  
75 L</subscribed> and L</is_selectable> are set true.
76  
77 =back
78  
79 =cut
80  
81 sub init {
82 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
83 0 $self->uidnext(1000);
84 0 $self->messages( [] );
85 0 $self->uids( {} );
86 0 $self->children( [] );
87 0 $self->uidvalidity(time);
88 0 $self->subscribed(1);
89 0 $self->is_selectable(1);
90 }
91  
92 =head3 load_data
93  
94 This default mailbox implementation simply returns an empty mailbox.
95 Subclasses will probably wish to override this method.
96  
97 =cut
98  
99 ▶sub load_data {
Subroutine load_data: not called CC=1 SCAR=0.0
Pod documented
100 }
101  
102 =head3 name
103  
104 Gets or sets the name of the mailbox. This includes a workaround for
105 Zimbra, which doesn't understand mailbox names with colons in them --
106 so we substitute dashes.
107  
108 =cut
109  
110 sub name {
111 0 ▶ my $self = shift;
Subroutine name: not called CC=5 SCAR=34.0
Pod documented
112 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
113 0 $self->{name} = shift;
114 }
115  
116 # Zimbra can't handle mailbox names with colons in them, for no
117 # obvious reason. Handily, it identifies itself as Zimbra before
118 # login, so we know when to perform a colonoscopy. We do this on
119 # get, and not on set, because the same model might be used by
120 # other clients.
121 0 my $name = $self->{name};
122 $name =~ s/:+/-/g
123 if Net::IMAP::Server->connection
124 and exists Net::IMAP::Server->connection->client_id->{vendor}
125 0 ▶ and Net::IMAP::Server->connection->client_id->{vendor} eq "Zimbra";
Branch: 1 branch
BranchTrueFalseTotal
if "Net::IMAP::Server"->connection and exists "Net::IMAP::Server"->connection->client_id->{"vendor"} and "Net::IMAP::Server"->connection->client_id->{"vendor"} eq "Zimbra" 0 0 0
Condition0%0/6
"Net::IMAP::Server"->connection and exists "Net::IMAP::Server"->connection->client_id->{"vendor"} and "Net::IMAP::Server"->connection->client_id->{"vendor"} eq "Zimbra"
!ll&&!rl&&r
000
"Net::IMAP::Server"->connection and exists "Net::IMAP::Server"->connection->client_id->{"vendor"}
!ll&&!rl&&r
000
MC/DC0%0/3
"Net::IMAP::Server"->connection and exists "Net::IMAP::Server"->connection->client_id->{"vendor"} and "Net::IMAP::Server"->connection->client_id->{"vendor"} eq "Zimbra"
"Net::IMAP::Server"->connection exists "Net::IMAP::Server"->connection->client_id->{"vendor"} "Net::IMAP::Server"->connection->client_id->{"vendor"} eq "Zimbra"
Truth table0%0/4
A and B and C
A= "Net::IMAP::Server"->connectionB= exists "Net::IMAP::Server"->connection->client_id->{"vendor"}C= "Net::IMAP::Server"->connection->client_id->{"vendor"} eq "Zimbra"
ABCresult
0XX0
10X0
1100
1111
126  
127 0 return $name;
128 }
129  
130 =head2 Actions
131  
132 =head3 poll
133  
134 Called when the server wishes the mailbox to update its state. By
135 default, does nothing. Subclasses will probably wish to override this
136 method.
137  
138 =cut
139  
140 ▶sub poll { }
Subroutine poll: not called CC=1 SCAR=0.0
Pod documented
141  
142 =head3 add_message MESSAGE
143  
144 Adds the given L<Net::IMAP::Server::Message> C<MESSAGE> to the mailbox,
145 setting its L<Net::IMAP::Server::Message/sequence> and
146 L<Net::IMAP::Server::Message/mailbox>.
147 L<Net::IMAP::Server::Message/uid> is set to L</uidnext> if the message
148 does not already have a C<uid>.
149  
150 =cut
151  
152 sub add_message {
153 0 ▶ my $self = shift;
Subroutine add_message: not called CC=5 SCAR=34.0
Pod documented
154 0 my $message = shift;
155  
156 # Basic message setup first
157 0 $message->mailbox($self);
158 0 $message->sequence( @{ $self->messages } + 1 );
159 0 push @{ $self->messages }, $message;
160  
161 # Some messages may supply their own uids
162 0 ▶ if ( $message->uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->uid) { } 0 0 0
163 0 ▶ $self->uidnext( $message->uid + 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $message->uid >= $self->uidnext 0 0 0
164 if $message->uid >= $self->uidnext;
165 } else {
166 0 $message->uid( $self->uidnext );
167 0 $self->uidnext( $self->uidnext + 1 );
168 }
169 0 $self->uids->{ $message->uid } = $message;
170  
171 # Also need to add it to anyone that has this folder as a
172 # temporary message store
173 0 for my $c ( Net::IMAP::Server->concurrent_mailbox_connections($self) ) {
174 0 ▶ next unless $c->temporary_messages;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->temporary_messages 0 0 0
175  
176 0 push @{ $c->temporary_messages }, $message;
177 $c->temporary_sequence_map->{$message}
178 0 = scalar @{ $c->temporary_messages };
179 }
180 0 return $message;
181 }
182  
183 =head3 add_child [...]
184  
185 Creates a mailbox under this mailbox, of the same class as this
186 mailbox is. Any arguments are passed to L</new>. Returns the newly
187 added subfolder, or undef if a folder with that name already exists.
188  
189 =cut
190  
191 sub add_child {
192 0 ▶ my $self = shift;
Subroutine add_child: not called CC=2 SCAR=17.9
Pod documented
193 0 my $node = ( ref $self )->new( { @_, parent => $self } );
194 0 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
195 0 push @{ $self->children }, $node;
196 0 return $node;
197 }
198  
199 =head3 create [...]
200  
201 Identical to L</add_child>. Should return false if the create is
202 denied or fails.
203  
204 =cut
205  
206 sub create {
207 0 ▶ my $self = shift;
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
208 0 return $self->add_child(@_);
209 }
210  
211 =head3 reparent MAILBOX [NAME]
212  
213 Reparents this mailbox to be a child of the given
214 L<Net::IMAP::Server::Mailbox> C<MAILBOX>, with the given C<NAME>.
215 Should return 0 if the reparenting is denied or fails.
216  
217 =cut
218  
219 sub reparent {
220 0 ▶ my $self = shift;
Subroutine reparent: not called CC=2 SCAR=17.9
Pod documented
221 0 my $parent = shift;
222  
223 $self->parent->children(
224 0 [ grep { $_ ne $self } @{ $self->parent->children } ] );
225 0 push @{ $parent->children }, $self;
226 0 $self->parent($parent);
227 0 ▶ $self->name(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
228 0 $self->full_path( purge => 1 );
229 0 return 1;
230 }
231  
232 =head3 delete
233  
234 Deletes this mailbox, removing it from its parent's list of children.
235 Should return false if the deletion is denied or fails.
236  
237 =cut
238  
239 sub delete {
240 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
241 $self->parent->children(
242 0 [ grep { $_ ne $self } @{ $self->parent->children } ] );
243  
244 0 return 1;
245 }
246  
247 =head3 expunge [ARRAYREF]
248  
249 Expunges messages marked as C<\Deleted>. If an arrayref of message
250 sequence numbers is provided, only expunges message from that set.
251  
252 =cut
253  
254 sub expunge {
255 0 ▶ my $self = shift;
Subroutine expunge: not called CC=19 SCAR=59.4
Pod documented
256 0 my $only = shift;
257 0 ▶ return if $only and not @{$only};
Branch: 1 branch
BranchTrueFalseTotal
if $only and not @{$only;} 0 0 0
Condition0%0/3
$only and not @{$only;}
!ll&&!rl&&r
000
MC/DC0%0/2
$only and not @{$only;}
$only not @{$only;}
Truth table0%0/3
A and not(B)
A= $onlyB= not @{$only;}
ABresult
0X0
100
111
258 0 my %only;
259 0 ▶ $only{$_}++ for @{ $only || [] };
Branch: 1 branch
BranchTrueFalseTotal
$only or [] 0 0 0
260  
261 0 my @ids;
262 0 my $offset = 0;
263 0 my @messages = @{ $self->messages };
264 $self->messages(
265 [ grep {
266 0 not( $_->has_flag('\Deleted')
267 0 ▶ and ( not $only or $only{ $_->sequence } ) )
Condition0%0/6
$_->has_flag('\\Deleted') && (!$only || $only{$_->sequence})
!ll&&!rl&&r
000
not $only or $only{$_->sequence}
l!l&&r!l&&!r
000
MC/DC0%0/3
$_->has_flag('\\Deleted') && (!$only || $only{$_->sequence})
$_->has_flag('\\Deleted') not $only $only{$_->sequence}
Truth table0%0/4
A && not(B) or C
A= $_->has_flag('\\Deleted')B= not $onlyC= $only{$_->sequence}
ABCresult
0XX0
1000
11X1
1011
268 } @messages
269 ]
270 );
271 0 for my $c ( Net::IMAP::Server->concurrent_mailbox_connections($self) ) {
272  
273 # Ensure that all other connections with this selected get a
274 # temporary message list, if they don't already have one
275 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ("Net::IMAP::Server"->connection and $c eq "Net::IMAP::Server"->connection and not $c->in_poll or $c->temporary_messages) 0 0 0
Condition0%0/9
"Net::IMAP::Server"->connection and $c eq "Net::IMAP::Server"->connection and not $c->in_poll or $c->temporary_messages
l!l&&r!l&&!r
000
"Net::IMAP::Server"->connection and $c eq "Net::IMAP::Server"->connection and not $c->in_poll
!ll&&!rl&&r
000
"Net::IMAP::Server"->connection and $c eq "Net::IMAP::Server"->connection
!ll&&!rl&&r
000
MC/DC0%0/4
"Net::IMAP::Server"->connection and $c eq "Net::IMAP::Server"->connection and not $c->in_poll or $c->temporary_messages
"Net::IMAP::Server"->connection $c eq "Net::IMAP::Server"->connection not $c->in_poll $c->temporary_messages
Truth table0%0/7
A and B and not(C) or D
A= "Net::IMAP::Server"->connectionB= $c eq "Net::IMAP::Server"->connectionC= not $c->in_pollD= $c->temporary_messages
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
276 # Except if we find our own connection; if this is
277 # *not* part of a poll, we asked for it, so no need to
278 # set up temporary messages.
279 ( Net::IMAP::Server->connection and
280 $c eq Net::IMAP::Server->connection
281 and not $c->in_poll
282 )
283 or $c->temporary_messages
284 )
285 {
286 0 $c->temporary_messages( [@messages] );
287 0 $c->temporary_sequence_map( {} );
288 0 $c->temporary_sequence_map->{$_} = $_->sequence for @messages;
289 }
290 }
291  
292 0 for my $m (@messages) {
293 0 ▶ if ( $m->has_flag('\Deleted')
Branch: 2 branches
BranchTrueFalseTotal
if ($m->has_flag('\\Deleted') and !$only || $only{$m->sequence}) { } 0 0 0
elsif ($offset) { } 0 0 0
Condition0%0/6
$m->has_flag("\\Deleted") and !$only || $only{$m->sequence}
!ll&&!rl&&r
000
not $only or $only{$m->sequence}
l!l&&r!l&&!r
000
MC/DC0%0/3
$m->has_flag("\\Deleted") and !$only || $only{$m->sequence}
$m->has_flag("\\Deleted") not $only $only{$m->sequence}
Truth table0%0/4
A and not(B) or C
A= $m->has_flag("\\Deleted")B= not $onlyC= $only{$m->sequence}
ABCresult
0XX0
1000
11X1
1011
294 and ( not $only or $only{ $m->sequence } ) )
295 {
296 0 push @ids, $m->sequence - $offset;
297 0 delete $self->uids->{ $m->uid };
298 0 $offset++;
299 0 $m->expunge;
300 } elsif ($offset) {
301 0 $m->sequence( $m->sequence - $offset );
302 }
303 }
304  
305 0 for my $c ( Net::IMAP::Server->concurrent_mailbox_connections($self) ) {
306  
307 # Also, each connection gets these added to their expunge list
308 0 push @{ $c->_unsent_expunge }, @ids;
309 }
310  
311 0 return 1;
312 }
313  
314 =head3 append MESSAGE
315  
316 Appends, and returns, the given C<MESSAGE>, which should be a string
317 containing the message. Returns false is the append is denied or
318 fails.
319  
320 =cut
321  
322 sub append {
323 0 ▶ my $self = shift;
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
324 0 my $m = Net::IMAP::Server::Message->new(@_);
325 0 $m->set_flag( '\Recent', 1 );
326 0 $self->add_message($m);
327 0 return $m;
328 }
329  
330 =head3 close
331  
332 Called when the client selects a different mailbox, or when the
333 client's connection closes. By default, does nothing.
334  
335 =cut
336  
337 ▶sub close { }
Subroutine close: not called CC=1 SCAR=0.0
Pod documented
338  
339 =head2 Inspection
340  
341 =head3 separator
342  
343 Returns the path separator. Note that only the path separator of the
344 root mailbox matters. Defaults to a forward slash.
345  
346 If the function returns is undef, the server supports only flat
347 mailboxes (i.e. no child mailboxes are allowed).
348  
349 =cut
350  
351 sub separator {
352 0 ▶ return "/";
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
353 }
354  
355 =head3 full_path [purge => 1]
356  
357 Returns the full path to this mailbox. This value is cached
358 aggressively on a per-connection basis; passing C<purge> flushes this
359 cache, if the path name has changed.
360  
361 =cut
362  
363 sub full_path {
364 0 ▶ my $self = shift;
Subroutine full_path: not called CC=8 SCAR=42.8
Pod documented
365 0 my %args = @_;
366 my $cache
367 = Net::IMAP::Server->connection
368 ? ( Net::IMAP::Server->connection->{path_cache} ||= {} )
369 0 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
'Net::IMAP::Server'->connection ? : 0 0 0
Condition0%0/2
"Net::IMAP::Server"->connection->{"path_cache"} ||= {}
l!l
00
MC/DC0%0/1
"Net::IMAP::Server"->connection->{"path_cache"} ||= {}
"Net::IMAP::Server"->connection->{"path_cache"}
Truth table0%0/2
A
A= "Net::IMAP::Server"->connection->{"path_cache"}
Aresult
11
00
370  
371 0 ▶ if ($args{purge}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"purge"}) 0 0 0
372 0 my @uncache = ($self);
373 0 ▶ while (@uncache) {
Branch: 1 branch
BranchTrueFalseTotal
if (@uncache) 0 0 0
374 0 my $o = shift @uncache;
375 0 delete $cache->{$o.""};
376 0 push @uncache, @{ $o->children };
377 }
378 }
379  
380 return $cache->{$self.""}
381 0 ▶ if defined $cache->{$self.""};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$cache{$self . ''} 0 0 0
382 0 ▶ $cache->{$self.""} =
Branch: 2 branches
BranchTrueFalseTotal
!$self->parent ? : 0 0 0
!$self->parent->parent ? : 0 0 0
383 !$self->parent ? ""
384 : !$self->parent->parent ? $self->name
385 : $self->parent->full_path . $self->separator . $self->name;
386 0 return $cache->{$self.""};
387 }
388  
389 =head3 flags
390  
391 Returns the list of flags that this mailbox supports.
392  
393 =cut
394  
395 sub flags {
396 0 ▶ my $self = shift;
Subroutine flags: not called CC=1 SCAR=6.9
Pod documented
397 0 return qw(\Answered \Flagged \Deleted \Seen \Draft);
398 }
399  
400 =head3 can_set_flag FLAG
401  
402 Returns true if the client is allowed to set the given flag in this
403 mailbox; this simply scans L</flags> to check.
404  
405 =cut
406  
407 sub can_set_flag {
408 0 ▶ my $self = shift;
Subroutine can_set_flag: not called CC=2 SCAR=17.9
Pod documented
409 0 my $flag = shift;
410  
411 0 ▶ return 1 if grep { lc $_ eq lc $flag } $self->flags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $_ eq lc $flag;} $self->flags 0 0 0
412 0 return;
413 }
414  
415 =head3 exists
416  
417 Returns the number of messages in this mailbox. Observing this also
418 sets the "high water mark" for notifying the client of messages added.
419  
420 =cut
421  
422 sub exists {
423 0 ▶ my $self = shift;
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
424 Net::IMAP::Server->connection->previous_exists(
425 0 ▶ scalar @{ $self->messages } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->selected 0 0 0
426 if $self->selected;
427 0 return scalar @{ $self->messages };
428 }
429  
430 =head3 recent
431  
432 Returns the number of messages which have the C<\Recent> flag set.
433  
434 =cut
435  
436 sub recent {
437 0 ▶ my $self = shift;
Subroutine recent: not called CC=1 SCAR=6.9
Pod documented
438 0 return scalar grep { $_->has_flag('\Recent') } @{ $self->messages };
439 }
440  
441 =head3 first_unseen
442  
443 Returns the sequence number of the first message which does not have
444 the C<\Seen> flag set. Returns 0 if all messages have been marked as
445 C<\Seen>.
446  
447 =cut
448  
449 sub first_unseen {
450 0 ▶ my $self = shift;
Subroutine first_unseen: not called CC=4 SCAR=30.0
Pod documented
451 0 for ( @{ $self->messages } ) {
452 0 ▶ next if $_->has_flag('\Seen');
Branch: 1 branch
BranchTrueFalseTotal
if $_->has_flag("\\Seen") 0 0 0
453 0 ▶ return Net::IMAP::Server->connection
Branch: 1 branch
BranchTrueFalseTotal
'Net::IMAP::Server'->connection ? : 0 0 0
454 ? Net::IMAP::Server->connection->sequence($_)
455 : $_->sequence;
456 }
457 0 return 0;
458 }
459  
460 =head3 unseen
461  
462 Returns the number of messages which do not have the C<\Seen> flag set.
463  
464 =cut
465  
466 sub unseen {
467 0 ▶ my $self = shift;
Subroutine unseen: not called CC=1 SCAR=6.9
Pod documented
468 0 return scalar grep { not $_->has_flag('\Seen') } @{ $self->messages };
469 }
470  
471 =head3 permanentflags
472  
473 Returns the flags which will be stored permanently for this mailbox;
474 defaults to the same set as L</flags> returns.
475  
476 =cut
477  
478 sub permanentflags {
479 0 ▶ my $self = shift;
Subroutine permanentflags: not called CC=1 SCAR=6.9
Pod documented
480 0 return $self->flags;
481 }
482  
483  
484 =head3 status TYPES
485  
486 Called when the clients requests a status update (via
487 L<Net::IMAP::Server::Command::Status>). C<TYPES> should be the types
488 of information requested, chosen from this list:
489  
490 =over
491  
492 =item MESSAGES
493  
494 The number of messages in the mailbox (via L</exists>)
495  
496 =item RECENT
497  
498 The number of messages marked as C<\Recent> (via L</recent>)
499  
500 =item UNSEEN
501  
502 The number of messages not marked as C<\Seen> (via L</unseen>)
503  
504 =item UIDVALIDITY
505  
506 The C</uidvalidity> of the mailbox.
507  
508 =item UIDNEXT
509  
510 The C</uidnext> of the mailbox.
511  
512 =back
513  
514 =cut
515  
516 sub status {
517 0 ▶ my $self = shift;
Subroutine status: not called CC=9 SCAR=45.0
Pod documented
518 0 my (@keys) = @_;
519 0 $self->poll;
520 0 my %items;
521 0 for my $i ( @keys ) {
522 0 ▶ if ( $i eq "MESSAGES" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($i eq 'MESSAGES') { } 0 0 0
elsif ($i eq 'RECENT') { } 0 0 0
elsif ($i eq 'UNSEEN') { } 0 0 0
elsif ($i eq 'UIDVALIDITY') { } 0 0 0
elsif ($i eq 'UIDNEXT') { } 0 0 0
523 0 $items{$i} = $self->exists;
524 } elsif ( $i eq "RECENT" ) {
525 0 $items{$i} = $self->recent;
526 } elsif ( $i eq "UNSEEN" ) {
527 0 $items{$i} = $self->unseen;
528 } elsif ( $i eq "UIDVALIDITY" ) {
529 0 my $uidvalidity = $self->uidvalidity;
530 0 ▶ $items{$i} = $uidvalidity if defined $uidvalidity;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uidvalidity 0 0 0
531 } elsif ( $i eq "UIDNEXT" ) {
532 0 my $uidnext = $self->uidnext;
533 0 ▶ $items{$i} = $uidnext if defined $uidnext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uidnext 0 0 0
534 }
535 }
536 0 return %items;
537 }
538  
539 =head3 read_only
540  
541 Returns true if this mailbox is read-only. By default, the value of
542 this depends on if the mailbox was selected using C<EXAMINE> or
543 C<SELECT> (see L<Net::IMAP::Server::Command::Select> and
544 L<Net::IMAP::Server::Connection/selected_read_only>)
545  
546 =cut
547  
548 sub read_only {
549 0 ▶ my $self = shift;
Subroutine read_only: not called CC=3 SCAR=24.8
Pod documented
550 0 ▶ return unless Net::IMAP::Server->connection;
Branch: 1 branch
BranchTrueFalseTotal
unless "Net::IMAP::Server"->connection 0 0 0
551 0 ▶ return $self->selected
Condition0%0/3
$self->selected && 'Net::IMAP::Server'->connection->selected_read_only
!ll&&!rl&&r
000
MC/DC0%0/2
$self->selected && 'Net::IMAP::Server'->connection->selected_read_only
$self->selected 'Net::IMAP::Server'->connection->selected_read_only
Truth table0%0/3
A && B
A= $self->selectedB= 'Net::IMAP::Server'->connection->selected_read_only
ABresult
0X0
100
111
552 && Net::IMAP::Server->connection->selected_read_only;
553 }
554  
555 =head3 select
556  
557 Called when the mailbox is selected; by default does nothing. Note
558 that this could be called a a result of either a SELECT or an EXAMINE.
559  
560 =cut
561  
562 ▶sub select {}
Subroutine select: not called CC=1 SCAR=0.0
Pod documented
563  
564 =head3 selected
565  
566 Returns true if this mailbox is the mailbox selected by the current
567 L<Net::IMAP::Server::Connection>.
568  
569 =cut
570  
571 sub selected {
572 0 ▶ my $self = shift;
Subroutine selected: not called CC=1 SCAR=6.9
Pod documented
573 0 return Net::IMAP::Server->connection
574 and Net::IMAP::Server->connection->selected
575 and Net::IMAP::Server->connection->selected eq $self;
576 }
577  
578 =for private
579  
580 This method exists to choose the most apppriate strategy to take the
581 intersection of (uids asked for) n (uids we have), by examining the
582 cardinality of each set, and iterating over the smaller of the two.
583 This is particularly important, as many clients try to fetch UIDs 1:*,
584 which will exhaust memory if the naive approach is taken, and there is
585 one message with UID 100_000_000.
586  
587 =cut
588  
589 sub _uids_in_range {
590 0 ▶ my $self = shift;
Subroutine _uids_in_range: not called CC=4 SCAR=30.0
591 0 my ( $low, $high ) = @_;
592 0 ▶ ( $low, $high ) = ( $high, $low ) if $low > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $low > $high 0 0 0
593  
594 0 my $count = scalar @{ $self->messages };
595 0 ▶ if ( $high - $low > $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low > $count) { } 0 0 0
596  
597 # More UIDs to enumerate than we actually have; check each
598 # existing UID for being in the range
599 0 ▶ return grep {$_ >= $low and $_ <= $high} map $_->uid, @{ $self->messages };
Branch: 1 branch
BranchTrueFalseTotal
$_ >= $low and $_ <= $high 0 0 0
600 } else {
601  
602 # More messages than in the UID range; enumerate the range and
603 # check each against UIDs which exist
604 0 my $uids = $self->uids;
605 0 return grep {defined $uids->{$_}} $low .. $high;
606 }
607 }
608  
609 =head3 get_uids STR
610  
611 Parses and returns messages fitting the given UID range.
612  
613 =cut
614  
615 sub get_uids {
616 0 ▶ my $self = shift;
Subroutine get_uids: not called CC=9 SCAR=45.0
Pod documented
617 0 my $str = shift;
618  
619 # Otherwise $self->messages->[-1] explodes
620 0 ▶ return () unless @{ $self->messages };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->messages;} 0 0 0
621  
622 0 my %found;
623 0 my $last = $self->messages->[-1]->uid;
624 0 my $uids = $self->uids;
625 0 for ( split ',', $str ) {
626 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
627 0 @found{ $self->_uids_in_range( $1, $2 ) } = ();
628 ▶ } 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
629 0 $found{$last}++;
630 0 @found{ $self->_uids_in_range( $1, $last ) } = ();
631 } elsif (/^(\d+)$/) {
632 0 ▶ $found{$_}++ if defined $uids->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $uids->{$1} 0 0 0
633 } elsif (/^\*$/) {
634 0 $found{$last}++;
635 }
636 }
637 0 return map { $uids->{$_} } sort { $a <=> $b } keys %found;
638 }
639  
640 =head3 get_messages STR
641  
642 Parses and returns messages fitting the given sequence range. Note
643 that since sequence numbers are connection-dependent, this simply
644 passes the buck to L</Net::IMAP::Server::Connection/get_messages>.
645  
646 =cut
647  
648 sub get_messages {
649 0 ▶ my $self = shift;
Subroutine get_messages: not called CC=2 SCAR=17.9
Pod documented
650 0 ▶ return () unless Net::IMAP::Server->connection;
Branch: 1 branch
BranchTrueFalseTotal
unless "Net::IMAP::Server"->connection 0 0 0
651 0 return Net::IMAP::Server->connection->get_messages(@_);
652 }
653  
654 =head3 update_tree
655  
656 Called before the model's children are listed to the client. This is
657 the right place to hook into for mailboxes whose children shift with
658 time.
659  
660 =cut
661  
662 sub update_tree {
663 0 ▶ my $self = shift;
Subroutine update_tree: not called CC=2 SCAR=17.9
Pod documented
664 0 $_->update_tree for @{ $self->children };
665 }
666  
667 =head3 prep_for_destroy
668  
669 Called before the mailbox is destroyed; this deals with cleaning up
670 the several circular references involved. In turn, it calls
671 L</prep_for_destroy> on all child mailboxes, as well as all messages
672 it has.
673  
674 =cut
675  
676 sub prep_for_destroy {
677 0 ▶ my $self = shift;
Subroutine prep_for_destroy: not called CC=5 SCAR=34.0
Pod documented
678 0 ▶ my @kids = @{ $self->children || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->children or [] 0 0 0
679 0 $self->children( [] );
680 0 $_->prep_for_destroy for @kids;
681 0 ▶ my @messages = @{ $self->messages || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->messages or [] 0 0 0
682 0 $self->messages( [] );
683 0 $self->uids( {} );
684 0 $_->prep_for_destroy for @messages;
685 0 $self->parent(undef);
686 }
687  
688 13 1;
« Server/Message.pm ↑ Summary Server/Connection.pm »