Message/Construct/Rebuild.pm

filter: Statementstmt 87.5% 134 / 153 Branchbran 72.8% 51 / 70 Conditioncond 40.0% 14 / 35 MC/DCmcdc 13.6% 3 / 22 Subroutinesub 88.0% 22 / 25 Podpod 14.2% 2 / 14 totaltotal 70.8% 226 / 319 CC 52 SCAR 20.2
CC
52
Cov
77%
removeExtraAlternativeText
30.0
replaceDeletedParts
11.8
rebuild
11.1
CRAP 7.5
« Message/Field/Addresses.pm ↑ Summary Message/Construct/Read.pm »
1 # Copyrights 2001-2022 by [Mark Overmeer <markov@cpan.org>].
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.03.
5 # This code is part of distribution Mail-Message. 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::Message;
10 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '3.012';
12  
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Mail::Message::Head::Complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Mail::Message::Body::Lines;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Mail::Message::Body::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Mail::Box::FastScalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 2 my @default_rules =
28 qw/replaceDeletedParts descendMultiparts descendNested
29 flattenMultiparts flattenEmptyMultiparts/;
30  
31 sub rebuild(@)
32 14 ▶{ my ($self, %args) = @_;
Subroutine rebuild: called CC=10 SCAR=24.1
Pod documented
33 14 my $keep = delete $args{keep_message_id};
34  
35 # Collect the rules to be run
36 14 ▶ my @rules = $args{rules} ? @{delete $args{rules}} : @default_rules;
Branch: 1 branch
BranchTrueFalseTotal
$args{'rules'} ? : 6 8 14
37 14 ▶ unshift @rules, @{delete $args{extra_rules}} if $args{extra_rules};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"extra_rules"} 0 14 14
38 14 ▶ unshift @rules, @{delete $args{extraRules}} if $args{extraRules}; #old name
Branch: 1 branch
BranchTrueFalseTotal
if $args{"extraRules"} 6 8 14
39  
40 14 foreach my $rule (@rules)
41 65 ▶ { next if ref $rule;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule 0 65 65
42 65 ▶ unless($self->can($rule))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($rule)) 0 65 65
43 0 { $self->log(ERROR => "No rebuild rule '$rule' defined.\n");
44 0 return 1;
45 }
46 }
47  
48 # Start off with the message
49  
50 14 ▶ my $rebuild = $self->recursiveRebuildPart($self, %args, rules => \@rules)
Branch: 1 branch
BranchTrueFalseTotal
my $rebuild = $self->recursiveRebuildPart($self, %args, "rules", \@rules) or return 0 14 14
51 or return;
52  
53 # Be sure we end-up with a message
54  
55 14 ▶ if($rebuild->isa('Mail::Message::Part'))
Branch: 1 branch
BranchTrueFalseTotal
if ($rebuild->isa("Mail::Message::Part")) 4 10 14
56 { # a bit too much information is lost: we are left without the
57 # main message headers....
58 4 my $clone = Mail::Message->new(head => $self->head->clone);
59 4 $clone->body($rebuild->body); # to update the Content lines
60 4 $rebuild = $clone;
61 }
62  
63 14 ▶ $keep or $rebuild->takeMessageId;
Branch: 1 branch
BranchTrueFalseTotal
$keep or $rebuild->takeMessageId 14 0 14
64 14 $rebuild;
65 }
66  
67 #------------------------------------------
68 # The general rules
69  
70 sub flattenNesting($@)
71 4 ▶{ my ($self, $part) = @_;
Subroutine flattenNesting: called CC=2 SCAR=6.9
Pod undocumented
72 4 ▶ $part->isNested ? $part->body->nested : $part;
Branch: 1 branch
BranchTrueFalseTotal
$part->isNested ? : 1 3 4
73 }
74  
75 sub flattenMultiparts($@)
76 35 ▶{ my ($self, $part) = @_;
Subroutine flattenMultiparts: called CC=3 SCAR=11.0
Pod undocumented
77 35 ▶ return $part unless $part->isMultipart;
Branch: 1 branch
BranchTrueFalseTotal
unless $part->isMultipart 25 10 35
78 10 my @active = $part->parts('ACTIVE');
79 10 ▶ @active==1 ? $active[0] : $part;
Branch: 1 branch
BranchTrueFalseTotal
@active == 1 ? : 3 7 10
80 }
81  
82 sub removeEmptyMultiparts($@)
83 14 ▶{ my ($self, $part) = @_;
Subroutine removeEmptyMultiparts: called CC=3 SCAR=11.7
Pod undocumented
84 14 ▶ $part->isMultipart && $part->body->parts==0 ? undef : $part;
Branch: 1 branch
BranchTrueFalseTotal
$part->isMultipart && $part->body->parts == 0 ? : 0 14 14
Condition66%2/3
$part->isMultipart and $part->body->parts == 0
!ll&&!rl&&r
950
MC/DC0%0/2
$part->isMultipart and $part->body->parts == 0
$part->isMultipart $part->body->parts == 0
Truth table66%2/3
A and B
A= $part->isMultipartB= $part->body->parts == 0
ABresult
0X0
100
111
85 }
86  
87 sub flattenEmptyMultiparts($@)
88 25 ▶{ my ($self, $part) = @_;
Subroutine flattenEmptyMultiparts: called CC=4 SCAR=13.9
Pod undocumented
89  
90 25 ▶ $part->isMultipart && $part->parts('ACTIVE')==0
Branch: 1 branch
BranchTrueFalseTotal
$part->isMultipart and $part->parts("ACTIVE") == 0 or return $part 23 2 25
Condition100%3/3
$part->isMultipart and $part->parts("ACTIVE") == 0
!ll&&!rl&&r
1942
MC/DC100%2/2
$part->isMultipart and $part->parts("ACTIVE") == 0
$part->isMultipart $part->parts("ACTIVE") == 0
Truth table100%3/3
A and B
A= $part->isMultipartB= $part->parts("ACTIVE") == 0
ABresult
0X0
100
111
91 or return $part;
92  
93 2 my $body = $part->body;
94 2 ▶ my $preamble = $body->preamble || Mail::Message::Body::Lines->new(data=>'');
Condition33%1/3
$body->preamble || 'Mail::Message::Body::Lines'->new('data', '')
l!l&&r!l&&!r
020
MC/DC0%0/2
$body->preamble || 'Mail::Message::Body::Lines'->new('data', '')
$body->preamble 'Mail::Message::Body::Lines'->new('data', '')
Truth table33%1/3
A || B
A= $body->preambleB= 'Mail::Message::Body::Lines'->new('data', '')
ABresult
1X1
011
000
95 2 my $epilogue = $body->epilogue;
96 2 my $newbody = $preamble->concatenate($preamble, <<NO_PARTS, $epilogue);
97 * PLEASE NOTE:
98 * This multipart did not contain any parts (anymore)
99 * and was therefore flattened.
100  
101 NO_PARTS
102  
103 2 my $rebuild = Mail::Message::Part->new
104 ( head => $part->head->clone
105 , container => undef
106 );
107 2 $rebuild->body($newbody);
108 2 $rebuild;
109 }
110  
111 sub removeEmptyBodies($@)
112 0 ▶{ my ($self, $part) = @_;
Subroutine removeEmptyBodies: not called CC=2 SCAR=17.9
Pod undocumented
113 0 ▶ $part->body->lines==0 ? undef : $part;
Branch: 1 branch
BranchTrueFalseTotal
$part->body->lines == 0 ? : 0 0 0
114 }
115  
116 sub descendMultiparts($@)
117 45 ▶{ my ($self, $part, %args) = @_;
Subroutine descendMultiparts: called CC=6 SCAR=18.0
Pod undocumented
118 45 ▶ return $part unless $part->isMultipart;
Branch: 1 branch
BranchTrueFalseTotal
unless $part->isMultipart 26 19 45
119  
120 19 my $body = $part->body;
121 19 my $changed = 0;
122 19 my @newparts;
123  
124 19 foreach my $part ($body->parts)
125 28 { my $new = $self->recursiveRebuildPart($part, %args);
126 28 ▶ if(!defined $new) { $changed++ }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $new) { } 9 19 28
elsif ($new == $part) { } 19 0 19
127 19 elsif($new==$part) { push @newparts, $part }
128 0 else { push @newparts, $new; $changed++ }
129 }
130  
131 19 ▶ $changed or return $part;
Branch: 1 branch
BranchTrueFalseTotal
$changed or return $part 12 7 19
132  
133 7 my $newbody = ref($body)->new
134 ( based_on => $body
135 , parts => \@newparts
136 );
137  
138 7 my $rebuild = ref($part)->new
139 ( head => $part->head->clone
140 , container => undef
141 );
142  
143 7 $rebuild->body($newbody); # update Content-* lines
144 7 $rebuild;
145 }
146  
147 sub descendNested($@)
148 32 ▶{ my ($self, $part, %args) = @_;
Subroutine descendNested: called CC=4 SCAR=13.9
Pod undocumented
149 32 ▶ $part->isNested or return $part;
Branch: 1 branch
BranchTrueFalseTotal
$part->isNested or return $part 25 7 32
150  
151 7 my $body = $part->body;
152 7 my $srcnested = $body->nested;
153 7 my $newnested = $self->recursiveRebuildPart($srcnested, %args);
154  
155 7 ▶ defined $newnested or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $newnested or return undef 0 7 7
156 7 ▶ return $part if $newnested==$srcnested;
Branch: 1 branch
BranchTrueFalseTotal
if $newnested == $srcnested 4 3 7
157  
158 # Changes in the encapsulated message
159 3 my $newbody = ref($body)->new(based_on => $body, nested => $newnested);
160 3 my $rebuild = ref($part)->new(head => $part->head->clone
161 , container => undef);
162  
163 3 $rebuild->body($newbody);
164 3 $rebuild;
165 }
166  
167 sub removeDeletedParts($@)
168 30 ▶{ my ($self, $part) = @_;
Subroutine removeDeletedParts: called CC=2 SCAR=6.9
Pod undocumented
169 30 ▶ $part->isDeleted ? undef : $part;
Branch: 1 branch
BranchTrueFalseTotal
$part->isDeleted ? : 7 23 30
170 }
171  
172 sub replaceDeletedParts($@)
173 23 ▶{ my ($self, $part) = @_;
Subroutine replaceDeletedParts: called CC=5 SCAR=24.7
Pod undocumented
174  
175 23 ▶ ($part->isNested && $part->body->nested->isDeleted) || $part->isDeleted
Branch: 1 branch
BranchTrueFalseTotal
$part->isNested and $part->body->nested->isDeleted or $part->isDeleted or return $part 23 0 23
Condition50%3/6
$part->isNested and $part->body->nested->isDeleted or $part->isDeleted
l!l&&r!l&&!r
0023
$part->isNested and $part->body->nested->isDeleted
!ll&&!rl&&r
2030
MC/DC0%0/3
$part->isNested and $part->body->nested->isDeleted or $part->isDeleted
$part->isNested $part->body->nested->isDeleted $part->isDeleted
Truth table40%2/5
A and B or C
A= $part->isNestedB= $part->body->nested->isDeletedC= $part->isDeleted
ABCresult
11X1
0X11
1011
0X00
1000
176 or return $part;
177  
178 0 my $structure = '';
179 0 my $output = Mail::Box::FastScalar->new(\$structure, ' ');
180 0 $part->printStructure($output);
181  
182 0 ▶ my $dispfn = $part->body->dispositionFilename || '';
Condition0%0/2
$part->body->dispositionFilename || ''
l!l
00
MC/DC0%0/1
$part->body->dispositionFilename || ''
$part->body->dispositionFilename
Truth table0%0/2
A
A= $part->body->dispositionFilename
Aresult
11
00
183 0 Mail::Message::Part->build
184 ( data => "Removed content:\n\n$structure\n$dispfn"
185 );
186 }
187  
188 #------------------------------------------
189 # The more complex rules
190  
191 sub removeHtmlAlternativeToText($@)
192 8 ▶{ my ($self, $part) = @_;
Subroutine removeHtmlAlternativeToText: called CC=6 SCAR=18.8
Pod undocumented
193 8 ▶ $part->body->mimeType eq 'text/html'
Branch: 1 branch
BranchTrueFalseTotal
$part->body->mimeType eq "text/html" or return $part 6 2 8
194 or return $part;
195  
196 2 my $container = $part->container;
197  
198 2 ▶ return $part
Branch: 1 branch
BranchTrueFalseTotal
unless defined $container and $container->mimeType eq "multipart/alternative" 0 2 2
Condition33%1/3
defined $container and $container->mimeType eq "multipart/alternative"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $container and $container->mimeType eq "multipart/alternative"
defined $container $container->mimeType eq "multipart/alternative"
Truth table33%1/3
A and B
A= defined $containerB= $container->mimeType eq "multipart/alternative"
ABresult
0X0
100
111
199 unless defined $container
200 && $container->mimeType eq 'multipart/alternative';
201  
202 # The HTML $part will be nulled when a plain text part is found
203 2 foreach my $subpart ($container->parts)
204 4 ▶ { return undef if $subpart->body->mimeType eq 'text/plain';
Branch: 1 branch
BranchTrueFalseTotal
if $subpart->body->mimeType eq "text/plain" 2 2 4
205 }
206  
207 0 $part;
208 }
209  
210 sub removeExtraAlternativeText($@)
211 0 ▶{ my ($self, $part) = @_;
Subroutine removeExtraAlternativeText: not called CC=5 SCAR=34.0
Pod undocumented
212  
213 0 my $container = $part->container;
214 0 ▶ $container && $container->mimeType eq 'multipart/alternative'
Branch: 1 branch
BranchTrueFalseTotal
$container and $container->mimeType eq "multipart/alternative" or return $part 0 0 0
Condition0%0/3
$container and $container->mimeType eq "multipart/alternative"
!ll&&!rl&&r
000
MC/DC0%0/2
$container and $container->mimeType eq "multipart/alternative"
$container $container->mimeType eq "multipart/alternative"
Truth table0%0/3
A and B
A= $containerB= $container->mimeType eq "multipart/alternative"
ABresult
0X0
100
111
215 or return $part;
216  
217 # The last part is the preferred part (as per RFC2046)
218 0 my $last = ($container->parts)[-1];
219 0 ▶ $last && $part==$last ? $part : undef;
Branch: 1 branch
BranchTrueFalseTotal
$last && $part == $last ? : 0 0 0
Condition0%0/3
$last and $part == $last
!ll&&!rl&&r
000
MC/DC0%0/2
$last and $part == $last
$last $part == $last
Truth table0%0/3
A and B
A= $lastB= $part == $last
ABresult
0X0
100
111
220 }
221  
222 2 my $has_hft;
223 sub textAlternativeForHtml($@)
224 3 ▶{ my ($self, $part, %args) = @_;
Subroutine textAlternativeForHtml: called CC=8 SCAR=21.5
Pod undocumented
225  
226 3 my $hft = 'Mail::Message::Convert::HtmlFormatText';
227 3 ▶ unless(defined $has_hft)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $has_hft) 1 2 3
228 1 { eval "require $hft";
229 1 $has_hft = $hft->can('format');
230 }
231  
232 3 ▶ return $part
Branch: 1 branch
BranchTrueFalseTotal
unless $has_hft and $part->body->mimeType eq "text/html" 2 1 3
Condition66%2/3
$has_hft and $part->body->mimeType eq "text/html"
!ll&&!rl&&r
021
MC/DC50%1/2
$has_hft and $part->body->mimeType eq "text/html"
$has_hft $part->body->mimeType eq "text/html"
Truth table66%2/3
A and B
A= $has_hftB= $part->body->mimeType eq "text/html"
ABresult
0X0
100
111
233 unless $has_hft && $part->body->mimeType eq 'text/html';
234  
235 1 my $container = $part->container;
236 1 ▶ my $in_alt = defined $container
Condition33%1/3
defined $container && $container->mimeType eq 'multipart/alternative'
!ll&&!rl&&r
100
MC/DC0%0/2
defined $container && $container->mimeType eq 'multipart/alternative'
defined $container $container->mimeType eq 'multipart/alternative'
Truth table33%1/3
A && B
A= defined $containerB= $container->mimeType eq 'multipart/alternative'
ABresult
0X0
100
111
237 && $container->mimeType eq 'multipart/alternative';
238  
239 return $part
240 if $in_alt
241 1 ▶ && first { $_->body->mimeType eq 'text/plain' } $container->parts;
Subroutine __ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if $in_alt and &first(sub { $_->body->mimeType eq "text/plain"; } , $container->parts) 0 1 1
Condition33%1/3
$in_alt and &first(sub { $_->body->mimeType eq "text/plain"; } , $container->parts)
!ll&&!rl&&r
100
MC/DC0%0/2
$in_alt and &first(sub { $_->body->mimeType eq "text/plain"; } , $container->parts)
$in_alt &first(sub { $_->body->mimeType eq "text/plain"; } , $container->parts)
Truth table33%1/3
A and B
A= $in_altB= &first(sub { $_->body->mimeType eq "text/plain"; } , $container->parts)
ABresult
0X0
100
111
242  
243  
244 # Create the plain part
245  
246 1 my $html_body = $part->body;
247 1 my $plain_body = $hft->new(%args)->format($html_body);
248  
249 1 my $plain_part = Mail::Message::Part->new(container => undef);
250 1 $plain_part->body($plain_body);
251  
252 1 ▶ return $container->attach($plain_part)
Branch: 1 branch
BranchTrueFalseTotal
if $in_alt 0 1 1
253 if $in_alt;
254  
255 # Recreate the html part to loose some header lines
256  
257 1 my $html_part = Mail::Message::Part->new(container => undef);
258 1 $html_part->body($html_body);
259  
260 # Create the new part, with the headers of the html part
261  
262 1 my $mp = Mail::Message::Body::Multipart->new
263 ( mime_type => 'multipart/alternative'
264 , parts => [ $plain_part, $html_part ]
265 );
266  
267 1 my $newpart = ref($part)->new
268 ( head => $part->head->clone # Subject field, and such
269 , container => undef
270 );
271 1 $newpart->body($mp);
272 1 $newpart;
273 }
274  
275 #------------------------------------------
276  
277  
278 sub recursiveRebuildPart($@)
279 49 ▶{ my ($self, $part, %args) = @_;
Subroutine recursiveRebuildPart: called CC=5 SCAR=16.1
Pod documented
280  
281 RULES:
282 49 foreach my $rule (@{$args{rules}})
283 219 ▶ { my %params = ( %args, %{$args{$rule} || {}} );
Branch: 1 branch
BranchTrueFalseTotal
$args{$rule} or {} 219 0 219
284 219 ▶ my $rebuild = $self->$rule($part, %params)
Branch: 1 branch
BranchTrueFalseTotal
my $rebuild = $self->$rule($part, %params) or return undef 9 210 219
285 or return undef;
286  
287 210 ▶ if($part != $rebuild)
Branch: 1 branch
BranchTrueFalseTotal
if ($part != $rebuild) 17 193 210
288 17 { $part = $rebuild;
289 17 redo RULES;
290 }
291 }
292  
293 40 $part;
294 }
295  
296 #------------------------------------------
297  
298  
299  
300 2 1;
« Message/Field/Addresses.pm ↑ Summary Message/Construct/Read.pm »