Message/Construct/Rebuild.pm

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