Server/DefaultModel.pm

filter: Statementstmt 36.2% 21 / 58 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 6 / 12 Podpod 100% 6 / 6 totaltotal 34.7% 33 / 95 CC 10 SCAR 27.7
CC
10
Cov
28%
lookup
42.0
init
12.0
split
12.0
CRAP 16.0
« Server/Command/List.pm ↑ Summary Server.pm »
1 package Net::IMAP::Server::DefaultModel;
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 13 __PACKAGE__->mk_accessors(qw(auth root));
8  
9 13 ▶use Net::IMAP::Server::Mailbox;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Encode::IMAPUTF7;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 my %roots;
15  
16 =head1 NAME
17  
18 Net::IMAP::Server::DefaultModel - Encapsulates per-connection
19 information about the layout of IMAP folders.
20  
21 =head1 DESCRIPTION
22  
23 This class represents an abstract model backend to the IMAP server; it
24 it meant to be overridden by server implementations. Primarily,
25 subclasses are expected to override L</init> to set up their folder
26 structure.
27  
28 Methods in the model can C<die> with messages which start with "NO" or
29 "BAD", which will be propagated back to the client immediately. See
30 L<Net::IMAP::Server::Connection/handle_command>.
31  
32 =head1 METHODS
33  
34 =head2 new
35  
36 This class is created when the client has successfully authenticated
37 to the server.
38  
39 =cut
40  
41 sub new {
42 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
43 0 my $self = $class->SUPER::new(@_);
44 0 $self->init;
45 0 return $self;
46 }
47  
48 =head2 init
49  
50 Called when the class is instantiated, with no arguments. Subclasses
51 should override this method to inspect the L</auth> object, and
52 determine what folders the user should have. The primary purpose of
53 this method is to set L</root> to the top level of the mailbox tree.
54 The root is expected to contain a mailbox named C<INBOX>.
55  
56 =cut
57  
58 sub init {
59 0 ▶ my $self = shift;
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
60 0 ▶ my $user = $self->auth->user || 'default';
Condition0%0/2
$self->auth->user || 'default'
l!l
00
MC/DC0%0/1
$self->auth->user || 'default'
$self->auth->user
Truth table0%0/2
A
A= $self->auth->user
Aresult
11
00
61  
62 0 ▶ if ( $roots{$user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roots{$user}) { } 0 0 0
63 0 $self->root( $roots{$user} );
64 } else {
65 0 $self->root( Net::IMAP::Server::Mailbox->new() )
66 ->add_child( name => "INBOX" )
67 ->add_child( name => $user );
68 0 $roots{$user} = $self->root;
69 }
70  
71 0 return $self;
72 }
73  
74 =head2 root MAILBOX
75  
76 Gets or sets the root L<Net::IMAP::Server::Mailbox> for this model.
77 The root mailbox should contain no messages, and have no name -- it
78 exists purely to contain sub-mailboxes, like C<INBOX>. The L</init>
79 method is responsible for setting up the appropriate root mailbox, and
80 all sub-mailboxes for the model.
81  
82 =head2 auth
83  
84 Returns the L<Net::IMAP::Server::DefaultAuth> object for this model;
85 this is set by the connection when the model is created, and will
86 always reference a valid authentication object.
87  
88 =head2 close
89  
90 Called when this model's connection closes, for any reason. By
91 default, does nothing.
92  
93 =cut
94  
95 ▶sub close {
Subroutine close: not called CC=1 SCAR=0.0
Pod documented
96 }
97  
98 =head2 split PATH
99  
100 Utility method which splits a given C<PATH> according to the mailbox
101 separator, as determined by the
102 L<Net::IMAP::Server::Mailbox/separator> of the L</root>. May C<die>
103 if the path (which is expected to be encoded using IMAP-UTF-7) is
104 invalid. See L<Encode::IMAPUTF7>. If the mailbox hierarchy is flat
105 (i.e. the separator is undef), returns the name without change.
106  
107 =cut
108  
109 sub split {
110 0 ▶ my $self = shift;
Subroutine split: not called CC=3 SCAR=24.8
Pod documented
111 0 my $name = shift;
112  
113 0 $name = eval { Encode::decode('IMAP-UTF-7', $name) };
114 0 ▶ die "BAD Invalid UTF-7 encoding\n" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
115  
116 0 ▶ if (defined $self->root->separator) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->root->separator) { } 0 0 0
117 0 return grep {length} split quotemeta $self->root->separator, $name;
118 } else {
119 0 return $name;
120 }
121 }
122  
123 =head2 lookup PATH
124  
125 Given a C<PATH>, returns the L<Net::IMAP::Server::Mailbox> for that
126 path, or undef if none matches. May die if the path contains unvalid
127 IMAP-UTF-7 (see L</split>).
128  
129 =cut
130  
131 sub lookup {
132 0 ▶ my $self = shift;
Subroutine lookup: not called CC=6 SCAR=37.4
Pod documented
133 0 my $name = shift;
134 0 my @parts = $self->split($name);
135 0 my $part = $self->root;
136 0 ▶ return undef unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 0 0
137 0 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 0 0 0
138 0 ▶ return undef unless @{ $part->children };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$part->children;} 0 0 0
139 0 my $find = shift @parts;
140 my @match
141 0 ▶ = grep { $_->is_inbox ? uc $find eq "INBOX" : $_->name eq $find }
Branch: 1 branch
BranchTrueFalseTotal
$_->is_inbox ? : 0 0 0
142 0 @{ $part->children };
143 0 ▶ return undef unless @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 0 0 0
144 0 $part = $match[0];
145 }
146 0 return $part;
147 }
148  
149 =head2 namespaces
150  
151 Returns the namespaces of this model, per RFC 2342. Defaults to
152 "INBOX" being the personal namespace, with no "shared" or "other
153 users" namespaces.
154  
155 =cut
156  
157 sub namespaces {
158 0 ▶ my $self = shift;
Subroutine namespaces: not called CC=1 SCAR=6.9
Pod documented
159 0 return ([["" => $self->root->separator]], undef, undef);
160 }
161  
162 13 1;
« Server/Command/List.pm ↑ Summary Server.pm »