Server/IMAP4/Fetch.pm

filter: Statementstmt 78.4% 109 / 139 Branchbran 71.6% 53 / 74 Conditioncond 40.9% 18 / 44 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 66.6% 10 / 15 Podpod 100% 9 / 9 totaltotal 67.4% 205 / 304 CC 63 SCAR 40.6
CC
63
Cov
70%
printStructure
240.0
new
21.6
fetchBody
18.7
CRAP 57.7
« Box/IMAP4/Head.pm ↑ Summary Server/IMAP4/User.pm »
1 # This code is part of Perl distribution Mail-Box-IMAP4 version 4.01.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Server::IMAP4::Fetch;{
13 2 our $VERSION = '4.01';
14 }
15  
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Log::Report 'mail-box-imap4', import => [];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Date::Parse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Digest::MD5 qw/md5_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 sub new($)
28 7 ▶{ my ($class, $part, %args) = @_;
Subroutine new: called CC=21 SCAR=30.7
Pod documented
29  
30 7 my $head = $part->head;
31 7 my $body = $part->body;
32 7 my $type = $body->type->study;
33  
34 7 my $self = bless +{
35 type => $type->body,
36 typeattr => [ $type->attrPairs ],
37 charset => $body->charset,
38 bodylines => $body->nrLines,
39 bodysize => $body->size
40 }, $class;
41  
42 7 $self->{headbegin} = ($head->fileLocation)[0];
43 7 @{$self}{qw/bodybegin bodyend/} = $body->fileLocation;
44  
45 # The fields use the defined() check, to avoid accidental expensive
46 # stringification by the field objects.
47  
48 7 my ($field, $value);
49 7 ▶ $self->{date} = $field->unfoldedBody
Branch: 1 branch
BranchTrueFalseTotal
if defined($field = $head->get("Date")) 5 2 7
50 if defined($field = $head->get('Date'));
51  
52 7 ▶ $self->{subject} = $field->unfoldedBody
Branch: 1 branch
BranchTrueFalseTotal
if defined($field = $head->get("Subject")) 4 3 7
53 if defined($field = $head->get('Subject'));
54  
55 7 ▶ $self->{description} = $field->unfoldedBody
Branch: 1 branch
BranchTrueFalseTotal
if defined($field = $head->get("Content-Description")) 1 6 7
56 if defined($field = $head->get('Content-Description'));
57  
58 7 ▶ $self->{language} = $field->unfoldedBody
Branch: 1 branch
BranchTrueFalseTotal
if defined($field = $head->get("Content-Language")) 1 6 7
59 if defined($field = $head->get('Content-Language'));
60  
61 7 ▶ $self->{filename} = $value
Branch: 1 branch
BranchTrueFalseTotal
if defined($value = $body->dispositionFilename) 1 6 7
62 if defined($value = $body->dispositionFilename);
63  
64 $self->{bodyMD5} = md5_base64($body->string)
65 7 ▶ if $args{md5checksums};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"md5checksums"} 0 7 7
66  
67 7 ▶ if(defined($field = $body->transferEncoding))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($field = $body->transferEncoding)) 7 0 7
68 7 { my $tf = $field->unfoldedBody;
69 7 ▶ $self->{transferenc} = $tf unless $tf eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
unless $tf eq "none" 5 2 7
70 }
71  
72 # Should become:
73 # $self->{disposition} = [ $field->body, $field->study->attributes ]
74 7 ▶ if(defined($field = $body->disposition))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($field = $body->disposition)) { } 7 0 7
75 7 { my $how = $field->body;
76 7 ▶ $how = $body->isText ? 'inline' : 'attachment' if $how eq 'none';
Branch: 2 branches
BranchTrueFalseTotal
if $how eq "none" 6 1 7
$body->isText ? : 5 1 6
77 7 $self->{disposition} = [ $how, $field->attributes ];
78 }
79 else
80 0 ▶ { $self->{disposition} = [ ($body->isText ? 'inline' : 'attachment') ];
Branch: 1 branch
BranchTrueFalseTotal
$body->isText ? : 0 0 0
81 }
82  
83 7 ▶ my $id = $head->get('Content-Message-ID') || $head->get("Message-ID");
Condition66%2/3
$head->get('Content-Message-ID') || $head->get('Message-ID')
l!l&&r!l&&!r
052
MC/DC50%1/2
$head->get('Content-Message-ID') || $head->get('Message-ID')
$head->get('Content-Message-ID') $head->get('Message-ID')
Truth table66%2/3
A || B
A= $head->get('Content-Message-ID')B= $head->get('Message-ID')
ABresult
1X1
011
000
84 7 ▶ if(defined $id)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 5 2 7
85 5 { my $msgid = $id->unfoldedBody =~ s/^\<*/</r =~ s/\>*$/>/r;
86 5 ▶ $self->{messageid} = $msgid if length $msgid;
Branch: 1 branch
BranchTrueFalseTotal
if length $msgid 5 0 5
87 }
88  
89 7 foreach my $addr ( qw/to from sender reply-to cc bcc/ )
90 42 ▶ { my $addrs = $head->study($addr) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $addrs = $head->study($addr) or next 12 30 42
91 12 foreach my $group ($addrs->groups)
92 11 { my @addrs = map +[ $_->phrase, $_->username, $_->domain ], $group->addresses;
93 11 push @{$self->{$addr}}, [ $group->name, @addrs ];
94 }
95 }
96  
97 7 ▶ if($body->isMultipart)
Branch: 2 branches
BranchTrueFalseTotal
if ($body->isMultipart) { } 1 6 7
elsif ($body->isNested) { } 1 5 6
98 1 { $self->{parts} = +[ map $class->new($_), $body->parts ];
99 }
100 elsif($body->isNested)
101 1 { $self->{nest} = $class->new($body->nested);
102 }
103  
104 7 $self;
105 }
106  
107 #--------------------
108  
109 0 ▶sub headLocation() { @{$_[0]}{ qw/headbegin bodybegin/ } }
Subroutine headLocation: not called CC=1 SCAR=6.9
Pod documented
110 0 ▶sub bodyLocation() { @{$_[0]}{ qw/bodybegin bodyend/ } }
Subroutine bodyLocation: not called CC=1 SCAR=6.9
Pod documented
111 0 ▶sub partLocation() { @{$_[0]}{ qw/headbegin bodyend/ } }
Subroutine partLocation: not called CC=1 SCAR=6.9
Pod documented
112  
113 #--------------------
114  
115 sub fetchBody($)
116 18 ▶{ my ($self, $extended) = @_;
Subroutine fetchBody: called CC=15 SCAR=29.3
Pod documented
117  
118 18 my $type = uc $self->{type};
119 18 my ($mediatype, $subtype) = split m[/], $type;
120  
121 18 ▶ if($self->{parts})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parts"}) 2 16 18
122 { # Multipart message
123 # WARNING: no blanks between part descriptions
124 2 my $parts = join '', map $_->fetchBody($extended), @{$self->{parts}};
125 2 ▶ my @fields = (\$parts, $subtype || 'MIXED');
Condition50%1/2
$subtype || 'MIXED'
l!l
20
MC/DC0%0/1
$subtype || 'MIXED'
$subtype
Truth table50%1/2
A
A= $subtype
Aresult
11
00
126  
127 2 ▶ if($extended) # only included when any valid info
Branch: 1 branch
BranchTrueFalseTotal
if ($extended) 1 1 2
128 1 { my @attr; # don't know what to include here
129 my @disp; # don't know about this either
130  
131 push @fields, \@attr, \@disp, $self->{language}
132 1 ▶ if @attr || @disp || defined $self->{language};
Branch: 1 branch
BranchTrueFalseTotal
if @attr or @disp or defined $self->{"language"} 0 1 1
Condition33%2/6
@attr or @disp or defined $self->{"language"}
l!l&&r!l&&!r
001
@attr or @disp
l!l&&r!l&&!r
001
MC/DC0%0/3
@attr or @disp or defined $self->{"language"}
@attr @disp defined $self->{"language"}
Truth table25%1/4
A or B or C
A= @attrB= @dispC= defined $self->{"language"}
ABCresult
1XX1
01X1
0011
0000
133 }
134  
135 2 return $self->_imapList(@fields);
136 }
137  
138 #
139 # Simple message
140 #
141  
142 my @fields = (
143 ($mediatype || 'TEXT'),
144 ($subtype || 'PLAIN'),
145 $self->{typeattr},
146 $self->{messageid},
147 $self->{description},
148 uc($self->{transferenc} || '8BIT'),
149 16 ▶ \($self->{bodysize}),
Condition66%4/6
$mediatype || 'TEXT'
l!l
160
$subtype || 'PLAIN'
l!l
160
$self->{'transferenc'} || '8BIT'
l!l
142
MC/DC0%0/1
$mediatype || 'TEXT'
$mediatype
MC/DC0%0/1
$subtype || 'PLAIN'
$subtype
MC/DC100%1/1
$self->{'transferenc'} || '8BIT'
$self->{'transferenc'}
Truth table50%1/2
A
A= $mediatype
Aresult
11
00
Truth table50%1/2
A
A= $subtype
Aresult
11
00
Truth table100%2/2
A
A= $self->{'transferenc'}
Aresult
11
00
150 );
151  
152 16 ▶ if(my $nest = $self->{nest})
Branch: 1 branch
BranchTrueFalseTotal
if (my $nest = $self->{"nest"}) 2 14 16
153 { # type MESSAGE (message/rfc822 encapsulated)
154 2 push @fields,
155 \$nest->fetchEnvelope,,
156 \$nest->fetchBody($extended);
157 }
158 16 push @fields, \$self->{bodylines};
159  
160 8 push @fields, @{$self}{ qw/bodyMD5 disposition language/ }
161 16 ▶ if $extended && ($self->{bodyMD5} || $self->{disposition} || $self->{language});
Branch: 1 branch
BranchTrueFalseTotal
if $extended and $self->{"bodyMD5"} || $self->{"disposition"} || $self->{"language"} 8 8 16
Condition44%4/9
$extended and $self->{"bodyMD5"} || $self->{"disposition"} || $self->{"language"}
!ll&&!rl&&r
808
$self->{"bodyMD5"} or $self->{"disposition"} or $self->{"language"}
l!l&&r!l&&!r
800
$self->{"bodyMD5"} or $self->{"disposition"}
l!l&&r!l&&!r
080
MC/DC25%1/4
$extended and $self->{"bodyMD5"} || $self->{"disposition"} || $self->{"language"}
$extended $self->{"bodyMD5"} $self->{"disposition"} $self->{"language"}
Truth table40%2/5
A and B or C or D
A= $extendedB= $self->{"bodyMD5"}C= $self->{"disposition"}D= $self->{"language"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
162  
163 16 $self->_imapList(@fields);
164 }
165  
166  
167 sub fetchEnvelope()
168 8 ▶{ my $self = shift;
Subroutine fetchEnvelope: called CC=5 SCAR=16.1
Pod documented
169 8 my @fields = ($self->{date}, $self->{subject});
170  
171 8 foreach my $addr ( qw/from sender reply-to to cc bcc/ )
172 48 ▶ { unless($self->{$addr})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$addr}) 33 15 48
173 33 { push @fields, undef; # NIL
174 33 next;
175 }
176  
177 # For now, group information is ignored... RFC2060 is very
178 # unclear about it... and seems incompatible with RFC2822
179 15 my $addresses = '';
180 15 foreach my $group (@{$self->{$addr}})
181 15 { my ($name, @addr) = @$group;
182  
183 # addr_adl is obsoleted by rfc2822
184 $addresses .= $self->_imapList($_->[0], undef, $_->[1], $_->[2])
185 15 for @addr;
186 }
187  
188 15 push @fields, \$addresses;
189 }
190  
191 8 push @fields, $self->{'in-reply-to'}, $self->{messageid};
192  
193 8 $self->_imapList(@fields);
194 }
195  
196  
197 0 ▶sub fetchSize() { $_[0]->{bodysize} }
Subroutine fetchSize: not called CC=1 SCAR=6.9
Pod documented
198  
199  
200 sub part(;$)
201 9 ▶{ my $self = shift;
Subroutine part: called CC=7 SCAR=19.5
Pod documented
202 9 ▶ my $nr = shift or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $nr = shift() or return $self 7 2 9
203  
204 7 my @nrs = split /\./, $nr;
205 7 ▶ while(@nrs)
Branch: 1 branch
BranchTrueFalseTotal
if (@nrs) 8 4 12
206 8 { my $take = shift @nrs;
207 8 ▶ if(exists $self->{nest} && $take==1)
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'nest'} and $take == 1) { } 1 7 8
elsif (exists $self->{'parts'} and @{$self->{'parts'};} >= $take) { } 4 3 7
Condition66%2/3
exists $self->{"nest"} and $take == 1
!ll&&!rl&&r
701
MC/DC50%1/2
exists $self->{"nest"} and $take == 1
exists $self->{"nest"} $take == 1
Truth table66%2/3
A and B
A= exists $self->{"nest"}B= $take == 1
ABresult
0X0
100
111
208 1 { $self = $self->{nest} }
209 5 ▶ elsif(exists $self->{parts} && @{$self->{parts}} >= $take)
Condition100%3/3
exists $self->{"parts"} and @{$self->{"parts"};} >= $take
!ll&&!rl&&r
214
MC/DC100%2/2
exists $self->{"parts"} and @{$self->{"parts"};} >= $take
exists $self->{"parts"} @{$self->{"parts"};} >= $take
Truth table100%3/3
A and B
A= exists $self->{"parts"}B= @{$self->{"parts"};} >= $take
ABresult
0X0
100
111
210 4 { $self = $self->{parts}[$take-1] }
211 3 else { return undef }
212 }
213  
214 4 $self;
215 }
216  
217  
218 sub printStructure(;$$)
219 0 ▶{ my $self = shift;
Subroutine printStructure: not called CC=15 SCAR=54.8
Pod documented
220 0 ▶ my $fh = @_ ? shift : select;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
221 0 ▶ my $number = @_ ? shift : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
222  
223 0 my $buffer; # only filled if filehandle==undef
224 0 ▶ defined $fh or open $fh, '>:raw', \$buffer;
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or open $fh, ">:raw", \$buffer 0 0 0
225  
226 0 my $type = $self->{type};
227 0 ▶ my $subject = $self->{subject} || '';
Condition0%0/2
$self->{'subject'} || ''
l!l
00
MC/DC0%0/1
$self->{'subject'} || ''
$self->{'subject'}
Truth table0%0/2
A
A= $self->{'subject'}
Aresult
11
00
228 0 my $text = "$number $type: $subject\n";
229  
230 0 ▶ my $hbegin = $self->{headbegin} || 0;
Condition0%0/2
$self->{'headbegin'} || 0
l!l
00
MC/DC0%0/1
$self->{'headbegin'} || 0
$self->{'headbegin'}
Truth table0%0/2
A
A= $self->{'headbegin'}
Aresult
11
00
231 0 ▶ my $bbegin = $self->{bodybegin} || '?';
Condition0%0/2
$self->{'bodybegin'} || '?'
l!l
00
MC/DC0%0/1
$self->{'bodybegin'} || '?'
$self->{'bodybegin'}
Truth table0%0/2
A
A= $self->{'bodybegin'}
Aresult
11
00
232 0 ▶ my $bodyend = $self->{bodyend} || '?';
Condition0%0/2
$self->{'bodyend'} || '?'
l!l
00
MC/DC0%0/1
$self->{'bodyend'} || '?'
$self->{'bodyend'}
Truth table0%0/2
A
A= $self->{'bodyend'}
Aresult
11
00
233 0 ▶ my $size = $self->{bodysize} // '?';
Condition0%0/2
$self->{"bodysize"} // "?"
l!l
00
MC/DC0%0/1
$self->{"bodysize"} // "?"
$self->{"bodysize"}
Truth table0%0/2
A
A= $self->{"bodysize"}
Aresult
11
00
234 0 ▶ my $lines = $self->{bodylines} // '?';
Condition0%0/2
$self->{"bodylines"} // "?"
l!l
00
MC/DC0%0/1
$self->{"bodylines"} // "?"
$self->{"bodylines"}
Truth table0%0/2
A
A= $self->{"bodylines"}
Aresult
11
00
235  
236 0 $text .= ' ' x (length($number) + 1);
237 0 $text .= "@ $hbegin-$bbegin-$bodyend, $size bytes, $lines lines\n";
238  
239 0 $fh->print($text);
240  
241 0 ▶ if(my $nest = $self->{nest})
Branch: 2 branches
BranchTrueFalseTotal
if (my $nest = $self->{'nest'}) { } 0 0 0
elsif (my $parts = $self->{'parts'}) { } 0 0 0
242 0 ▶ { $nest->printStructure($fh, length($number) ? $number.'.1' :'1');
Branch: 1 branch
BranchTrueFalseTotal
length $number ? : 0 0 0
243 }
244 elsif(my $parts = $self->{parts})
245 0 { my $count = 1;
246 0 ▶ $number .= '.' if length $number;
Branch: 1 branch
BranchTrueFalseTotal
if length $number 0 0 0
247 0 $_->printStructure($fh, $number.$count++) for @$parts;
248 }
249  
250 0 $buffer;
251 }
252  
253 #--------------------
254  
255 # Concatenate the elements of a list, as the IMAP protocol does.
256 # ARRAYS are included a sublist, and normal strings get quoted.
257 # Pass a ref-scalar if something needs to be included without
258 # quoting.
259  
260 sub _imapList(@)
261 65 ▶{ my $self = shift;
Subroutine _imapList: called CC=5 SCAR=16.1
262 65 my @f;
263  
264 65 foreach (@_)
265 328 ▶ { if(ref $_ eq 'ARRAY') { push @f, $self->_imapList(@$_) }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 24 304 328
elsif (ref $_ eq 'SCALAR') { } 53 251 304
elsif (not defined $_) { } 100 151 251
266 53 elsif(ref $_ eq 'SCALAR') { push @f, ${$_} }
267 100 elsif(!defined $_) { push @f, 'NIL' }
268 else
269 151 { my $copy = $_ =~ s/\\/\\\\/gr =~ s/\"/\\"/gr;
270 151 push @f, qq#"$_"#;
271 }
272 }
273  
274 65 local $" = ' ';
275 65 "(@f)";
276 }
277  
278 #--------------------
279  
280 2 1;
« Box/IMAP4/Head.pm ↑ Summary Server/IMAP4/User.pm »