Box/IMAP4.pm

filter: Statementstmt 19.6% 36 / 183 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 40.7% 11 / 27 Podpod 87.5% 14 / 16 totaltotal 16.0% 61 / 381 CC 62 SCAR 47.7
CC
62
Cov
12%
init
420.0
createTransporter
90.0
readMessages
56.0
CRAP 118.4
↑ Summary Transport/IMAP4.pm »
1 # Copyrights 2001-2019 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box-IMAP4. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::IMAP4;
10 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '3.007';
12  
13 2 ▶use base 'Mail::Box::Net';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Mail::Box::IMAP4::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Mail::Box::IMAP4::Head;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Mail::Transport::IMAP4;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Mail::Box::Parser::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Mail::Message::Head::Complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Mail::Message::Head::Delayed;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 sub init($)
30 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=20 SCAR=60.4
Pod undocumented
31  
32 0 my $folder = $args->{folder};
33  
34 # MailBox names top folder directory '=', but IMAP needs '/'
35 0 ▶ $folder = '/'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $folder or $folder eq "=" 0 0 0
Condition0%0/3
not defined $folder or $folder eq "="
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $folder or $folder eq "="
not defined $folder $folder eq "="
Truth table0%0/3
not(A) or B
A= not defined $folderB= $folder eq "="
ABresult
1X1
011
000
36 if ! defined $folder || $folder eq '=';
37  
38 # There's a disconnect between the URL parser and this code.
39 # The URL parser always produces a full path (beginning with /)
40 # while this code expects to NOT get a full path. So, we'll
41 # trim the / from the front of the path.
42 # Also, this code can't handle a trailing slash and there's
43 # no reason to ever offer one. Strip that too.
44 0 ▶ if($folder ne '/')
Branch: 1 branch
BranchTrueFalseTotal
if ($folder ne "/") 0 0 0
45 0 { $folder =~ s,^/+,,g;
46 0 $folder =~ s,/+$,,g;
47 }
48  
49 0 $args->{folder} = $folder;
50  
51 0 ▶ my $access = $args->{access} ||= 'r';
Condition0%0/2
$args->{"access"} ||= "r"
l!l
00
MC/DC0%0/1
$args->{"access"} ||= "r"
$args->{"access"}
Truth table0%0/2
A
A= $args->{"access"}
Aresult
11
00
52 0 my $writeable = $access =~ m/w|a/;
53 my $ch = $self->{MBI_c_head}
54 0 ▶ = $args->{cache_head} || ($writeable ? 'NO' : 'DELAY');
Branch: 1 branch
BranchTrueFalseTotal
$writeable ? : 0 0 0
Condition0%0/3
$args->{'cache_head'} || ($writeable ? 'NO' : 'DELAY')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'cache_head'} || ($writeable ? 'NO' : 'DELAY')
$args->{'cache_head'} ($writeable ? 'NO' : 'DELAY')
Truth table0%0/3
A || B
A= $args->{'cache_head'}B= ($writeable ? 'NO' : 'DELAY')
ABresult
1X1
011
000
55  
56 0 ▶ $args->{head_type} ||= 'Mail::Box::IMAP4::Head'
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "NO" or $ch eq "PARTIAL" 0 0 0
Condition0%0/5
$ch eq "NO" or $ch eq "PARTIAL"
l!l&&r!l&&!r
000
$args->{"head_type"} ||= "Mail::Box::IMAP4::Head"
l!l
00
MC/DC0%0/2
$ch eq "NO" or $ch eq "PARTIAL"
$ch eq "NO" $ch eq "PARTIAL"
MC/DC0%0/1
$args->{"head_type"} ||= "Mail::Box::IMAP4::Head"
$args->{"head_type"}
Truth table0%0/3
A or B
A= $ch eq "NO"B= $ch eq "PARTIAL"
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args->{"head_type"}
Aresult
11
00
57 if $ch eq 'NO' || $ch eq 'PARTIAL';
58  
59 0 ▶ $args->{body_type} ||= 'Mail::Message::Body::Lines';
Condition0%0/2
$args->{"body_type"} ||= "Mail::Message::Body::Lines"
l!l
00
MC/DC0%0/1
$args->{"body_type"} ||= "Mail::Message::Body::Lines"
$args->{"body_type"}
Truth table0%0/2
A
A= $args->{"body_type"}
Aresult
11
00
60 0 ▶ $args->{message_type} ||= 'Mail::Box::IMAP4::Message';
Condition0%0/2
$args->{"message_type"} ||= "Mail::Box::IMAP4::Message"
l!l
00
MC/DC0%0/1
$args->{"message_type"} ||= "Mail::Box::IMAP4::Message"
$args->{"message_type"}
Truth table0%0/2
A
A= $args->{"message_type"}
Aresult
11
00
61  
62 0 $self->SUPER::init($args);
63  
64 0 $self->{MBI_domain} = $args->{domain};
65 $self->{MBI_c_labels}
66 0 ▶ = $args->{cache_labels} || ($writeable ? 'NO' : 'DELAY');
Branch: 1 branch
BranchTrueFalseTotal
$writeable ? : 0 0 0
Condition0%0/3
$args->{'cache_labels'} || ($writeable ? 'NO' : 'DELAY')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'cache_labels'} || ($writeable ? 'NO' : 'DELAY')
$args->{'cache_labels'} ($writeable ? 'NO' : 'DELAY')
Truth table0%0/3
A || B
A= $args->{'cache_labels'}B= ($writeable ? 'NO' : 'DELAY')
ABresult
1X1
011
000
67 $self->{MBI_c_body}
68 0 ▶ = $args->{cache_body} || ($writeable ? 'NO' : 'DELAY');
Branch: 1 branch
BranchTrueFalseTotal
$writeable ? : 0 0 0
Condition0%0/3
$args->{'cache_body'} || ($writeable ? 'NO' : 'DELAY')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'cache_body'} || ($writeable ? 'NO' : 'DELAY')
$args->{'cache_body'} ($writeable ? 'NO' : 'DELAY')
Truth table0%0/3
A || B
A= $args->{'cache_body'}B= ($writeable ? 'NO' : 'DELAY')
ABresult
1X1
011
000
69  
70  
71 0 ▶ my $transport = $args->{transporter} || 'Mail::Transport::IMAP4';
Condition0%0/2
$args->{'transporter'} || 'Mail::Transport::IMAP4'
l!l
00
MC/DC0%0/1
$args->{'transporter'} || 'Mail::Transport::IMAP4'
$args->{'transporter'}
Truth table0%0/2
A
A= $args->{'transporter'}
Aresult
11
00
72 0 ▶ $transport = $self->createTransporter($transport, %$args)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $transport 0 0 0
73 unless ref $transport;
74  
75 0 $self->transporter($transport);
76  
77 0 ▶ defined $transport
Branch: 1 branch
BranchTrueFalseTotal
defined $transport or return 0 0 0
78 or return;
79  
80 $args->{create}
81 0 ▶ ? $self->create($transport, $args)
Branch: 1 branch
BranchTrueFalseTotal
$args->{'create'} ? : 0 0 0
82 : $self;
83 }
84  
85 sub create($@)
86 0 ▶{ my($self, $name, $args) = @_;
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
87  
88 0 ▶ if($args->{access} !~ /w|a/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"access"} =~ /w|a/) 0 0 0
89 0 { $self->log(ERROR =>
90 "You must have write access to create folder $name.");
91 0 return undef;
92 }
93  
94 0 $self->transporter->createFolder($name);
95 }
96  
97 sub foundIn(@)
98 0 ▶{ my $self = shift;
Subroutine foundIn: not called CC=5 SCAR=34.0
Pod documented
99 0 ▶ unshift @_, 'folder' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
100 0 my %options = @_;
101  
102 (exists $options{type} && $options{type} =~ m/^imap/i)
103 0 ▶ || (exists $options{folder} && $options{folder} =~ m/^imap/);
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"type"} and $options{"type"} =~ /^imap/i or exists $options{"folder"} and $options{"folder"} =~ /^imap/ 0 0 0
Condition0%0/9
exists $options{'type'} && $options{'type'} =~ /^imap/i || exists $options{'folder'} && $options{'folder'} =~ /^imap/
l!l&&r!l&&!r
000
exists $options{"type"} and $options{"type"} =~ /^imap/i
!ll&&!rl&&r
000
exists $options{"folder"} and $options{"folder"} =~ /^imap/
!ll&&!rl&&r
000
MC/DC0%0/4
exists $options{'type'} && $options{'type'} =~ /^imap/i || exists $options{'folder'} && $options{'folder'} =~ /^imap/
exists $options{"type"} $options{"type"} =~ /^imap/i exists $options{"folder"} $options{"folder"} =~ /^imap/
Truth table0%0/7
A and B || C and D
A= exists $options{"type"}B= $options{"type"} =~ /^imap/iC= exists $options{"folder"}D= $options{"folder"} =~ /^imap/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
104 }
105  
106 sub type() {'imap4'}
107  
108  
109  
110 sub close(@)
111 0 ▶{ my $self = shift;
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
112 0 ▶ $self->SUPER::close(@_) or return ();
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::close(@_) or return () 0 0 0
113 0 $self->transporter(undef);
114 0 $self;
115 }
116  
117 sub listSubFolders(@)
118 0 ▶{ my ($thing, %args) = @_;
Subroutine listSubFolders: not called CC=4 SCAR=30.0
Pod documented
119 0 my $self = $thing;
120  
121 0 ▶ $self = $thing->new(%args) or return () # list toplevel
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thing 0 0 0
Condition0%0/5
ref $thing || (($self = $thing->new(%args)) || (return ()))
l!l&&r!l&&!r
000
$self = $thing->new(%args) or return ()
l!l
00
MC/DC0%0/2
ref $thing || (($self = $thing->new(%args)) || (return ()))
ref $thing $self = $thing->new(%args)
Truth table0%0/3
A || B
A= ref $thingB= $self = $thing->new(%args)
ABresult
1X1
011
000
122 unless ref $thing;
123  
124 0 my $imap = $self->transporter;
125 0 ▶ defined $imap ? $imap->folders($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $imap ? : 0 0 0
126 }
127  
128 0 ▶sub nameOfSubfolder($;$) { $_[1] }
Subroutine nameOfSubfolder: not called CC=1 SCAR=6.9
Pod undocumented
129  
130 #-------------------------------------------
131  
132 sub readMessages(@)
133 0 ▶{ my ($self, %args) = @_;
Subroutine readMessages: not called CC=7 SCAR=40.3
Pod documented
134  
135 0 my $name = $self->name;
136 0 ▶ return $self if $name eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "/" 0 0 0
137  
138 0 my $imap = $self->transporter;
139 0 ▶ defined $imap or return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $imap or return () 0 0 0
140  
141 0 my @log = $self->logSettings;
142 0 my $seqnr = 0;
143  
144 0 my $cl = $self->{MBI_c_labels} ne 'NO';
145 0 my $wl = $self->{MBI_c_labels} ne 'DELAY';
146  
147 0 my $ch = $self->{MBI_c_head};
148 0 ▶ my $ht = $ch eq 'DELAY' ? $args{head_delayed_type} : $args{head_type};
Branch: 1 branch
BranchTrueFalseTotal
$ch eq 'DELAY' ? : 0 0 0
149 0 ▶ my @ho = $ch eq 'PARTIAL' ? (cache_fields => 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$ch eq 'PARTIAL' ? : 0 0 0
150  
151 $self->{MBI_selectable}
152 0 ▶ or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBI_selectable"} or return $self 0 0 0
153  
154 0 foreach my $id ($imap->ids)
155 0 { my $head = $ht->new(@log, @ho);
156 my $message = $args{message_type}->new
157 0 ( head => $head
158 , unique => $id
159 , folder => $self
160 , seqnr => $seqnr++
161  
162 , cache_labels => $cl
163 , write_labels => $wl
164 , cache_head => ($ch eq 'DELAY')
165 , cache_body => ($ch ne 'NO')
166 );
167  
168 my $body = $args{body_delayed_type}
169 0 ->new(@log, message => $message);
170  
171 0 $message->storeBody($body);
172  
173 0 $self->storeMessage($message);
174 }
175  
176 0 $self;
177 }
178
179  
180  
181 sub getHead($)
182 0 ▶{ my ($self, $message) = @_;
Subroutine getHead: not called CC=4 SCAR=30.0
Pod documented
183 0 ▶ my $imap = $self->transporter or return;
Branch: 1 branch
BranchTrueFalseTotal
my $imap = $self->transporter or return 0 0 0
184  
185 0 my $uidl = $message->unique;
186 0 my @fields = $imap->getFields($uidl, 'ALL');
187  
188 0 ▶ unless(@fields)
Branch: 1 branch
BranchTrueFalseTotal
unless (@fields) 0 0 0
189 0 { $self->log(WARNING => "Message $uidl disappeared from $self.");
190 0 return;
191 }
192  
193 0 my $head = $self->{MB_head_type}->new;
194 0 $head->addNoRealize($_) for @fields;
195  
196 0 $self->log(PROGRESS => "Loaded head of $uidl.");
197 0 $head;
198 }
199  
200  
201  
202 sub getHeadAndBody($)
203 0 ▶{ my ($self, $message) = @_;
Subroutine getHeadAndBody: not called CC=5 SCAR=34.0
Pod documented
204 0 ▶ my $imap = $self->transporter or return;
Branch: 1 branch
BranchTrueFalseTotal
my $imap = $self->transporter or return 0 0 0
205 0 my $uid = $message->unique;
206 0 my $lines = $imap->getMessageAsString($uid);
207  
208 0 ▶ unless(defined $lines)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lines) 0 0 0
209 0 { $self->log(WARNING => "Message $uid disappeared from $self.");
210 0 return ();
211 }
212  
213 0 my $parser = Mail::Box::Parser::Perl->new # not parseable by C parser
214 ( filename => "$imap"
215 , file => Mail::Box::FastScalar->new(\$lines)
216 );
217  
218 0 my $head = $message->readHead($parser);
219 0 ▶ unless(defined $head)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head) 0 0 0
220 0 { $self->log(WARNING => "Cannot find head back for $uid in $self.");
221 0 $parser->stop;
222 0 return ();
223 }
224  
225 0 my $body = $message->readBody($parser, $head);
226 0 ▶ unless(defined $body)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 0 0
227 0 { $self->log(WARNING => "Cannot read body for $uid in $self.");
228 0 $parser->stop;
229 0 return ();
230 }
231  
232 0 $parser->stop;
233  
234 0 $self->log(PROGRESS => "Loaded message $uid.");
235 0 ($head, $body->contentInfoFrom($head));
236 }
237  
238  
239  
240 sub body(;$)
241 0 ▶{ my $self = shift;
Subroutine body: not called CC=3 SCAR=24.8
Pod documented
242 0 ▶ unless(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
243 0 ▶ { my $body = $self->{MBI_cache_body} ? $self->SUPER::body : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'MBI_cache_body'} ? : 0 0 0
244 }
245  
246 0 $self->unique();
247 0 $self->SUPER::body(@_);
248 }
249  
250  
251  
252 sub write(@)
253 0 ▶{ my ($self, %args) = @_;
Subroutine write: not called CC=4 SCAR=30.0
Pod documented
254 0 ▶ my $imap = $self->transporter or return;
Branch: 1 branch
BranchTrueFalseTotal
my $imap = $self->transporter or return 0 0 0
255  
256 0 ▶ $self->SUPER::write(%args, transporter => $imap) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::write(%args, "transporter", $imap) or return 0 0 0
257  
258 0 ▶ if($args{save_deleted})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'save_deleted'}) { } 0 0 0
259 0 { $self->log(NOTICE => "Impossible to keep deleted messages in IMAP");
260 }
261 0 else { $imap->destroyDeleted($self->name) }
262  
263 0 $self;
264 }
265  
266 sub delete(@)
267 0 ▶{ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
268 0 my $transp = $self->transporter;
269 0 $self->SUPER::delete(@_); # subfolders
270 0 $transp->deleteFolder($self->name);
271 }
272  
273  
274  
275 sub writeMessages($@)
276 0 ▶{ my ($self, $args) = @_;
Subroutine writeMessages: not called CC=2 SCAR=17.9
Pod documented
277  
278 0 my $imap = $args->{transporter};
279 0 my $fn = $self->name;
280  
281 0 $_->writeDelayed($fn, $imap) for @{$args->{messages}};
282  
283 0 $self;
284 }
285  
286  
287  
288 2 my %transporters;
289 sub createTransporter($@)
290 0 ▶{ my ($self, $class, %args) = @_;
Subroutine createTransporter: not called CC=9 SCAR=45.0
Pod documented
291  
292 0 ▶ my $hostname = $self->{MBN_hostname} || 'localhost';
Condition0%0/2
$self->{'MBN_hostname'} || 'localhost'
l!l
00
MC/DC0%0/1
$self->{'MBN_hostname'} || 'localhost'
$self->{'MBN_hostname'}
Truth table0%0/2
A
A= $self->{'MBN_hostname'}
Aresult
11
00
293 0 ▶ my $port = $self->{MBN_port} || '143';
Condition0%0/2
$self->{'MBN_port'} || '143'
l!l
00
MC/DC0%0/1
$self->{'MBN_port'} || '143'
$self->{'MBN_port'}
Truth table0%0/2
A
A= $self->{'MBN_port'}
Aresult
11
00
294 0 ▶ my $username = $self->{MBN_username} || $ENV{USER};
Condition0%0/3
$self->{'MBN_username'} || $ENV{'USER'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'MBN_username'} || $ENV{'USER'}
$self->{'MBN_username'} $ENV{'USER'}
Truth table0%0/3
A || B
A= $self->{'MBN_username'}B= $ENV{'USER'}
ABresult
1X1
011
000
295  
296 0 ▶ my $join = exists $args{join_connection} ? $args{join_connection} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'join_connection'} ? : 0 0 0
297  
298 0 my $linkid;
299 0 ▶ if($join)
Branch: 1 branch
BranchTrueFalseTotal
if ($join) 0 0 0
300 0 { $linkid = "$hostname:$port:$username";
301 0 ▶ return $transporters{$linkid} if defined $transporters{$linkid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $transporters{$linkid} 0 0 0
302 }
303  
304 my $transporter = $class->new
305 ( %args,
306 , hostname => $hostname, port => $port
307 , username => $username, password => $self->{MBN_password}
308 , domain => $self->{MBI_domain}
309 0 ▶ ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $transporter = $class->new(%args, "hostname", $hostname, "port", $port, "username", $username, "password", $self->{"MBN_password"}, "domain", $self->{"MBI_domain"}) or return undef 0 0 0
310  
311 0 ▶ if(defined $linkid)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $linkid) 0 0 0
312 0 { $transporters{$linkid} = $transporter;
313 0 weaken($transporters{$linkid});
314 }
315  
316 0 $transporter;
317 }
318  
319  
320  
321 sub transporter(;$)
322 0 ▶{ my $self = shift;
Subroutine transporter: not called CC=5 SCAR=34.0
Pod documented
323  
324 0 my $imap;
325 0 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
326 0 { $imap = $self->{MBI_transport} = shift;
327 0 ▶ defined $imap or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $imap or return 0 0 0
328 }
329 else
330 0 { $imap = $self->{MBI_transport};
331 }
332  
333 0 ▶ unless(defined $imap)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $imap) 0 0 0
334 0 { $self->log(ERROR => "No IMAP4 transporter configured");
335 0 return undef;
336 }
337  
338 0 my $name = $self->name;
339  
340 0 $self->{MBI_selectable} = $imap->currentFolder($name);
341 return $imap
342 0 ▶ if defined $self->{MBI_selectable};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"MBI_selectable"} 0 0 0
343  
344 0 $self->log(ERROR => "Couldn't select IMAP4 folder $name");
345 0 undef;
346 }
347  
348  
349  
350 sub fetch($@)
351 0 ▶{ my ($self, $what, @info) = @_;
Subroutine fetch: not called CC=3 SCAR=24.8
Pod documented
352 0 ▶ my $imap = $self->transporter or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $imap = $self->transporter or return [] 0 0 0
353 0 ▶ $what = $self->messages($what) unless ref $what eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $what eq "ARRAY" 0 0 0
354 0 $imap->fetch($what, @info);
355 }
356  
357  
358 #-------------------------------------------
359  
360 2 1;
↑ Summary Transport/IMAP4.pm »