FromMail/Message.pm

filter: Statementstmt 18.2% 31 / 170 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 29.0% 9 / 31 Podpod 95.4% 21 / 22 totaltotal 16.7% 61 / 365 CC 55 SCAR 38.4
CC
55
Cov
11%
disposition
182.0
htmlField
72.0
htmlForeachPart
56.0
CRAP 46.4
« FromMail/Head.pm ↑ Summary FromMail/Default/Previewers.pm »
1 # This code is part of Perl distribution HTML-FromMail version 4.00.
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) 2003-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 HTML::FromMail::Message;{
13 1 our $VERSION = '4.00';
14 }
15  
16 1 ▶use base 'HTML::FromMail::Page';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'html-frommail';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use HTML::FromMail::Head ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use HTML::FromMail::Field ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use HTML::FromMail::Default::Previewers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use HTML::FromMail::Default::HTMLifiers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #--------------------
31  
32 sub init($)
33 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=4 SCAR=30.0
Pod undocumented
34 0 ▶ $args->{topic} ||= 'message';
Condition0%0/2
$args->{"topic"} ||= "message"
l!l
00
MC/DC0%0/1
$args->{"topic"} ||= "message"
$args->{"topic"}
Truth table0%0/2
A
A= $args->{"topic"}
Aresult
11
00
35 0 $self->SUPER::init($args);
36  
37 0 $self->{HFM_dispose} = $args->{disposition};
38 0 my $settings = $self->settings;
39  
40 # Collect previewers
41 0 my @prevs = @HTML::FromMail::Default::Previewers::previewers;
42 0 ▶ if(my $prevs = $settings->{previewers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $prevs = $settings->{"previewers"}) 0 0 0
43 0 { unshift @prevs, @$prevs;
44 }
45 0 $self->{HFM_previewers} = \@prevs;
46  
47 # Collect htmlifiers
48 0 my @html = @HTML::FromMail::Default::HTMLifiers::htmlifiers;
49 0 ▶ if(my $html = $settings->{htmlifiers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $html = $settings->{"htmlifiers"}) 0 0 0
50 0 { unshift @html, @$html;
51 }
52 0 $self->{HFM_htmlifiers} = \@html;
53  
54 # We will use header and field formatters
55 0 $self->{HFM_field} = HTML::FromMail::Field->new(settings => $settings);
56 0 $self->{HFM_head} = HTML::FromMail::Head ->new(settings => $settings);
57  
58 0 $self;
59 }
60  
61 #-----------
62  
63 0 ▶sub fields() { $_[0]->{HFM_field} }
Subroutine fields: not called CC=1 SCAR=6.9
Pod documented
64  
65  
66 0 ▶sub header() { $_[0]->{HFM_head} }
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
67  
68 #-----------
69  
70  
71 1 my $attach_id = 0;
72  
73 sub createAttachment($$$)
74 0 ▶{ my ($self, $message, $part, $args) = @_;
Subroutine createAttachment: not called CC=3 SCAR=24.8
Pod documented
75 0 ▶ my $outdir = $args->{outdir} or panic;
Branch: 1 branch
BranchTrueFalseTotal
my $outdir = $args->{"outdir"} or &panic() 0 0 0
76 0 my $decoded = $part->decoded;
77  
78 0 my $filename = $part->label('filename');
79 0 ▶ unless(defined $filename)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
80 0 { $filename = $decoded->dispositionFilename($outdir);
81 0 $part->label(filename => $filename);
82 }
83  
84 0 $decoded->write(filename => $filename);
85  
86 0 +( url => basename($filename),
87 size => (-s $filename),
88 type => $decoded->type->body,
89  
90 filename => $filename, # absolute
91 decoded => $decoded,
92 );
93 }
94  
95  
96 sub htmlField($$)
97 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlField: not called CC=8 SCAR=42.8
Pod documented
98  
99 0 my $name = $args->{name};
100 0 ▶ unless(defined $name)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
101 0 { warning __x"no field name specified in {template}.", template => $args->{input};
102 0 $name = "NONE";
103 }
104  
105 0 my $current = $self->lookup('part_object', $args);
106  
107 0 my $head;
108 0 ▶ for($args->{from} || 'PART')
Condition0%0/2
$args->{'from'} || 'PART'
l!l
00
MC/DC0%0/1
$args->{'from'} || 'PART'
$args->{'from'}
Truth table0%0/2
A
A= $args->{'from'}
Aresult
11
00
109 0 ▶ { my $source = ($_ eq 'PART' ? $current : $_ eq 'PARENT' ? $current->container : undef) || $message;
Branch: 2 branches
BranchTrueFalseTotal
$_ eq 'PART' ? : 0 0 0
$_ eq 'PARENT' ? : 0 0 0
Condition0%0/3
($_ eq 'PART' ? $current : ($_ eq 'PARENT' ? $current->container : undef)) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
($_ eq 'PART' ? $current : ($_ eq 'PARENT' ? $current->container : undef)) || $message
($_ eq 'PART' ? $current : ($_ eq 'PARENT' ? $current->container : undef)) $message
Truth table0%0/3
A || B
A= ($_ eq 'PART' ? $current : ($_ eq 'PARENT' ? $current->container : undef))B= $message
ABresult
1X1
011
000
110 0 $head = $source->head;
111 }
112  
113 0 my @fields = $self->fields->fromHead($head, $name, $args);
114  
115 0 ▶ $args->{formatter}->onFinalToken($args)
Branch: 1 branch
BranchTrueFalseTotal
$args->{"formatter"}->onFinalToken($args) or return [map({"field_object", $_}, @fields)] 0 0 0
116 or return [ map +{ field_object => $_ }, @fields ];
117  
118 0 my $f = $self->fields;
119 0 join "<br />\n", map $f->htmlBody($_, $args), @fields;
120 }
121  
122  
123 sub htmlSubject($$)
124 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlSubject: not called CC=1 SCAR=6.9
Pod documented
125 0 my %args = (%$args, name => 'subject', from => 'NESSAGE');
126 0 $self->htmlField($message, \%args);
127 }
128  
129  
130 sub htmlName($$)
131 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlName: not called CC=2 SCAR=17.9
Pod documented
132  
133 0 ▶ my $field = $self->lookup('field_object', $args)
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->lookup("field_object", $args) or &error(&__x("use of 'name' outside field container.")) 0 0 0
134 or error __x"use of 'name' outside field container."; #XXX better message?
135  
136 0 $self->fields->htmlName($field, $args);
137 }
138  
139  
140 sub htmlBody($$)
141 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlBody: not called CC=2 SCAR=17.9
Pod documented
142  
143 0 ▶ my $field = $self->lookup('field_object', $args)
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->lookup("field_object", $args) or &error(&__x("use of 'body' outside field container")) 0 0 0
144 or error __x"use of 'body' outside field container";
145  
146 0 $self->fields->htmlBody($field, $args);
147 }
148  
149  
150 sub htmlAddresses($$)
151 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlAddresses: not called CC=2 SCAR=17.9
Pod documented
152  
153 0 ▶ my $field = $self->lookup('field_object', $args)
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->lookup("field_object", $args) or &error(&__x("use of 'addresses' outside field container")) 0 0 0
154 or error __x"use of 'addresses' outside field container";
155  
156 0 $self->fields->htmlAddresses($field, $args);
157 }
158  
159  
160 sub htmlHead($$)
161 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlHead: not called CC=4 SCAR=30.0
Pod documented
162  
163 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
164 0 ▶ my $head = $current->head or return;
Branch: 1 branch
BranchTrueFalseTotal
my $head = $current->head or return 0 0 0
165 0 my @fields = $self->header->fields($head, $args);
166  
167 0 ▶ $args->{formatter}->onFinalToken($args)
Branch: 1 branch
BranchTrueFalseTotal
$args->{"formatter"}->onFinalToken($args) or return [map({"field_object", $_}, @fields)] 0 0 0
168 or return [ map +{ field_object => $_ }, @fields ];
169  
170 0 local $" = '';
171 0 "<pre>@{ [ map $_->string, @fields ] }</pre>\n";
172 }
173  
174  
175 sub htmlMessage($$)
176 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlMessage: not called CC=1 SCAR=6.9
Pod documented
177 0 +{ message_text => $args->{formatter}->containerText($args) };
178 }
179  
180  
181 sub htmlMultipart($$)
182 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlMultipart: not called CC=3 SCAR=24.8
Pod documented
183 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
184 0 ▶ $current->isMultipart or return '';
Branch: 1 branch
BranchTrueFalseTotal
$current->isMultipart or return "" 0 0 0
185  
186 0 my $body = $current->body; # un-decoded info is more useful
187 0 +{ type => $body->mimeType->type, size => $body->size };
188 }
189  
190  
191 sub htmlNested($$)
192 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlNested: not called CC=4 SCAR=30.0
Pod documented
193 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
194 0 ▶ $current->isNested or return '';
Branch: 1 branch
BranchTrueFalseTotal
$current->isNested or return "" 0 0 0
195  
196 0 my $partnr = $self->lookup('part_number', $args);
197 0 ▶ $partnr .= '.' if length $partnr;
Branch: 1 branch
BranchTrueFalseTotal
if length $partnr 0 0 0
198  
199 0 [ +{ part_number => $partnr . '1', part_object => $current->body->nested } ];
200 }
201  
202  
203 sub htmlifier($)
204 0 ▶{ my ($self, $type) = @_;
Subroutine htmlifier: not called CC=3 SCAR=24.8
Pod documented
205 0 my $pairs = $self->{HFM_htmlifiers};
206 0 ▶ for(my $i=0; $i < @$pairs; $i+=2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 0 0 0
207 0 ▶ { return $pairs->[$i+1] if $type eq $pairs->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $pairs->[0] 0 0 0
208 }
209 0 undef;
210 }
211  
212  
213 sub previewer($)
214 0 ▶{ my ($self, $type) = @_;
Subroutine previewer: not called CC=4 SCAR=30.0
Pod documented
215 0 my $pairs = $self->{HFM_previewers};
216 0 ▶ for(my $i=0; $i < @$pairs; $i+=2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 0 0 0
217 0 ▶ { return $pairs->[$i+1] if $type eq $pairs->[$i] || $type->mediaType eq $pairs->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $pairs->[$i] or $type->mediaType eq $pairs->[$i] 0 0 0
Condition0%0/3
$type eq $pairs->[$i] or $type->mediaType eq $pairs->[$i]
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq $pairs->[$i] or $type->mediaType eq $pairs->[$i]
$type eq $pairs->[$i] $type->mediaType eq $pairs->[$i]
Truth table0%0/3
A or B
A= $type eq $pairs->[$i]B= $type->mediaType eq $pairs->[$i]
ABresult
1X1
011
000
218 }
219 0 undef;
220 }
221  
222  
223 sub disposition($$$)
224 0 ▶{ my ($self, $message, $part, $args) = @_;
Subroutine disposition: not called CC=13 SCAR=52.0
Pod documented
225 0 ▶ return '' if $part->isMultipart || $part->isNested;
Branch: 1 branch
BranchTrueFalseTotal
if $part->isMultipart or $part->isNested 0 0 0
Condition0%0/3
$part->isMultipart or $part->isNested
l!l&&r!l&&!r
000
MC/DC0%0/2
$part->isMultipart or $part->isNested
$part->isMultipart $part->isNested
Truth table0%0/3
A or B
A= $part->isMultipartB= $part->isNested
ABresult
1X1
011
000
226  
227 0 my $cd = $part->head->get('Content-Disposition');
228  
229 0 ▶ my $sugg = defined $cd ? lc($cd->body) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cd ? : 0 0 0
230 0 ▶ $sugg = 'attach' if $sugg =~ m/^\s*attach/;
Branch: 1 branch
BranchTrueFalseTotal
if $sugg =~ /^\s*attach/ 0 0 0
231  
232 0 my $body = $part->body;
233 0 my $type = $body->mimeType;
234  
235 0 ▶ if($sugg eq 'inline')
Branch: 4 branches
BranchTrueFalseTotal
if ($sugg eq 'inline') { } 0 0 0
elsif ($sugg eq 'attach') { } 0 0 0
elsif ($self->htmlifier($type)) { } 0 0 0
elsif ($self->previewer($type)) { } 0 0 0
236 0 ▶ { $sugg = $self->htmlifier($type) ? 'inline' : $self->previewer($type) ? 'preview' : 'attach';
Branch: 2 branches
BranchTrueFalseTotal
$self->htmlifier($type) ? : 0 0 0
$self->previewer($type) ? : 0 0 0
237 }
238 elsif($sugg eq 'attach')
239 0 ▶ { $sugg = 'preview' if $self->previewer($type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->previewer($type) 0 0 0
240 }
241 0 elsif($self->htmlifier($type)) { $sugg = 'inline' }
242 0 elsif($self->previewer($type)) { $sugg = 'preview' }
243 0 else { $sugg = 'attach' }
244  
245 # User may have a different opinion.
246 0 ▶ my $disp = $self->settings->{disposition} or return $sugg;
Branch: 1 branch
BranchTrueFalseTotal
my $disp = $self->settings->{"disposition"} or return $sugg 0 0 0
247 0 $disp->($message, $part, $sugg, $args)
248 }
249  
250  
251 sub htmlInline($$)
252 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlInline: not called CC=3 SCAR=24.8
Pod documented
253  
254 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
255 0 my $dispose = $self->disposition($message, $current, $args);
256 0 ▶ $dispose eq 'inline' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$dispose eq "inline" or return "" 0 0 0
257  
258 0 my @attach = $self->createAttachment($message, $current, $args);
259 0 my $inliner = $self->htmlifier($current->body->mimeType);
260 0 my $inline = $inliner->($self, $message, $current, $args);
261  
262 0 +{ %$inline, @attach };
263 }
264  
265  
266 sub htmlAttach($$)
267 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlAttach: not called CC=3 SCAR=24.8
Pod documented
268  
269 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
270 0 my $dispose = $self->disposition($message, $current, $args);
271 0 ▶ $dispose eq 'attach' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$dispose eq "attach" or return "" 0 0 0
272  
273 0 my %attach = $self->createAttachment($message, $current, $args);
274 0 \%attach;
275 }
276  
277  
278 sub htmlPreview($$)
279 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlPreview: not called CC=3 SCAR=24.8
Pod documented
280  
281 0 ▶ my $current = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
282 0 my $dispose = $self->disposition($message, $current, $args);
283 0 ▶ $dispose eq 'preview' or return '';
Branch: 1 branch
BranchTrueFalseTotal
$dispose eq "preview" or return "" 0 0 0
284  
285 0 my %attach = $self->createAttachment($message, $current, $args);
286 0 my $previewer = $self->previewer($current->body->mimeType);
287 0 $previewer->($self, $message, $current, \%attach, $args);
288 }
289  
290  
291 sub htmlForeachPart($$)
292 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlForeachPart: not called CC=7 SCAR=40.3
Pod documented
293 0 ▶ my $part = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
294  
295 0 ▶ $part or error __x"foreachPart not used within part.";
Branch: 1 branch
BranchTrueFalseTotal
$part or &error(&__x("foreachPart not used within part.")) 0 0 0
296 0 ▶ $part->isMultipart or error __x"foreachPart outside multipart.";
Branch: 1 branch
BranchTrueFalseTotal
$part->isMultipart or &error(&__x("foreachPart outside multipart.")) 0 0 0
297  
298 0 ▶ my $parentnr = $self->lookup('part_number',$args) || '';
Condition0%0/2
$self->lookup('part_number', $args) || ''
l!l
00
MC/DC0%0/1
$self->lookup('part_number', $args) || ''
$self->lookup('part_number', $args)
Truth table0%0/2
A
A= $self->lookup('part_number', $args)
Aresult
11
00
299 0 ▶ $parentnr .= '.' if length $parentnr;
Branch: 1 branch
BranchTrueFalseTotal
if length $parentnr 0 0 0
300  
301 0 my @parts = $part->parts;
302 0 my @part_data;
303  
304 0 ▶ for(my $partnr = 0; $partnr < @parts; $partnr++)
Branch: 1 branch
BranchTrueFalseTotal
if ($partnr < @parts) 0 0 0
305 0 { push @part_data, +{
306 part_number => $parentnr . ($partnr+1),
307 part_object => $parts[$partnr],
308 };
309 }
310  
311 0 \@part_data;
312 }
313  
314  
315 sub htmlRawText($$)
316 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlRawText: not called CC=2 SCAR=17.9
Pod documented
317 0 ▶ my $part = $self->lookup('part_object', $args) || $message;
Condition0%0/3
$self->lookup('part_object', $args) || $message
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lookup('part_object', $args) || $message
$self->lookup('part_object', $args) $message
Truth table0%0/3
A || B
A= $self->lookup('part_object', $args)B= $message
ABresult
1X1
011
000
318 0 $self->plain2html($part->decoded->string);
319 }
320  
321  
322 sub htmlPart($$)
323 0 ▶{ my ($self, $message, $args) = @_;
Subroutine htmlPart: not called CC=2 SCAR=17.9
Pod documented
324 0 my $format = $args->{formatter};
325 0 my $msg = $format->lookup('message_text', $args);
326  
327 0 ▶ defined $msg or error __x"part outside a 'message' block.";
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or &error(&__x("part outside a 'message' block.")) 0 0 0
328 0 $format->processText($msg, $args);
329 }
330  
331 #--------------------
332  
333 1 1;
« FromMail/Head.pm ↑ Summary FromMail/Default/Previewers.pm »