Box/Parser/Lines.pm

filter: Statementstmt 72.3% 68 / 94 Branchbran 54.7% 23 / 42 Conditioncond 38.2% 13 / 34 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 73.3% 11 / 15 Podpod 87.5% 7 / 8 totaltotal 59.6% 127 / 213 CC 37 SCAR 28.2
CC
37
Cov
61%
_is_good_end
56.0
_read_stripped_lines
17.6
readHeader
12.9
CRAP 16.7
« Message/Body/Construct.pm ↑ Summary Message/Construct/Forward.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::Box::Parser::Lines;{
13 8 our $VERSION = '4.05';
14 }
15  
16 8 ▶use parent 'Mail::Box::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Log::Report 'mail-message', import => [ qw/__x panic warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use Mail::Message::Field ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 sub init(@)
28 16 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.3
Pod undocumented
29 16 $self->SUPER::init($args);
30  
31 16 ▶ $self->{MBPL_lines} = $args->{lines} or panic "No lines";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBPL_lines"} = $args->{"lines"} or &panic("No lines") 16 0 16
32 16 ▶ $self->{MBPL_source} = $args->{source} or panic "No source";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBPL_source"} = $args->{"source"} or &panic("No source") 16 0 16
33 16 $self;
34 }
35  
36 #--------------------
37  
38 110 ▶sub lines() { $_[0]->{MBPL_lines} }
Subroutine lines: called CC=1 SCAR=0.0
Pod documented
39 0 ▶sub source() { $_[0]->{MBPL_source} }
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
40  
41 #--------------------
42  
43 8 my $is_empty_line = qr/^\015?\012?$/;
44  
45 sub readHeader()
46 36 ▶{ my $self = shift;
Subroutine readHeader: called CC=8 SCAR=25.6
Pod documented
47 36 my $lines = $self->lines;
48 36 my @ret;
49  
50 LINE:
51 36 ▶ while(@$lines)
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 138 0 138
52 138 { my $line = shift @$lines;
53 138 ▶ last if $line =~ $is_empty_line;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$is_empty_line/ 36 102 138
54  
55 102 my ($name, $body) = split /\s*\:\s*/, $line, 2;
56  
57 102 ▶ unless(defined $body)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 102 102
58 0 { warning __x"unexpected end of header in {source}:\n {line}", source => $self->source, line => $line;
59  
60 0 ▶ if(@ret && $self->fixHeaderErrors)
Branch: 1 branch
BranchTrueFalseTotal
if (@ret and $self->fixHeaderErrors) 0 0 0
Condition0%0/3
@ret and $self->fixHeaderErrors
!ll&&!rl&&r
000
MC/DC0%0/2
@ret and $self->fixHeaderErrors
@ret $self->fixHeaderErrors
Truth table0%0/3
A and B
A= @retB= $self->fixHeaderErrors
ABresult
0X0
100
111
61 0 { $ret[-1][1] .= ' '.$line; # glue err line to previous field
62 0 next LINE;
63 }
64  
65 0 unshift @$lines, $line;
66 0 last LINE;
67 }
68  
69 # Collect folded lines
70 102 ▶ $body .= shift @$lines
Branch: 1 branch
BranchTrueFalseTotal
@$lines and $lines->[0] =~ /^[ \t]/ and do { $body .= shift(@$lines) } 3 102 105
Condition66%2/3
@$lines and $lines->[0] =~ /^[ \t]/
!ll&&!rl&&r
01023
MC/DC50%1/2
@$lines and $lines->[0] =~ /^[ \t]/
@$lines $lines->[0] =~ /^[ \t]/
Truth table66%2/3
A and B
A= @$linesB= $lines->[0] =~ /^[ \t]/
ABresult
0X0
100
111
71 while @$lines && $lines->[0] =~ m!^[ \t]!;
72  
73 102 push @ret, [ $name, $body ];
74 }
75  
76 36 (undef, undef, @ret);
77 }
78  
79 sub _is_good_end()
80 0 ▶{ my $self = shift;
Subroutine _is_good_end: not called CC=7 SCAR=40.3
81  
82 # No seps, then when have to trust it.
83 0 ▶ my $sep = $self->activeSeparator // return 1;
Condition0%0/2
$self->activeSeparator // (return 1)
l!l
00
MC/DC0%0/1
$self->activeSeparator // (return 1)
$self->activeSeparator
Truth table0%0/2
A
A= $self->activeSeparator
Aresult
11
00
84  
85 # Find first non-empty line on specified location.
86 0 my $lines = $self->lines;
87 0 my $skip = 0;
88 0 ▶ while($skip < @$lines && $lines->[$skip] =~ $is_empty_line) { $skip++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($skip < @$lines and $lines->[$skip] =~ /$is_empty_line/) 0 0 0
Condition0%0/3
$skip < @$lines and $lines->[$skip] =~ /$is_empty_line/
!ll&&!rl&&r
000
MC/DC0%0/2
$skip < @$lines and $lines->[$skip] =~ /$is_empty_line/
$skip < @$lines $lines->[$skip] =~ /$is_empty_line/
Truth table0%0/3
A and B
A= $skip < @$linesB= $lines->[$skip] =~ /$is_empty_line/
ABresult
0X0
100
111
89 0 ▶ $skip < @$lines or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$skip < @$lines or return 1 0 0 0
90  
91 0 my $line = $lines->[$skip];
92  
93 0 ▶ substr($line, 0, length $sep) eq $sep
Branch: 1 branch
BranchTrueFalseTotal
substr($line, 0, length $sep) eq $sep and $sep ne "From " || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/ 0 0 0
Condition0%0/6
substr($line, 0, length $sep) eq $sep && ($sep ne 'From ' || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/)
!ll&&!rl&&r
000
$sep ne "From " or $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
l!l&&r!l&&!r
000
MC/DC0%0/3
substr($line, 0, length $sep) eq $sep && ($sep ne 'From ' || $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/)
substr($line, 0, length $sep) eq $sep $sep ne "From " $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
Truth table0%0/4
A && B or C
A= substr($line, 0, length $sep) eq $sepB= $sep ne "From "C= $line =~ / (?:19[6-9]|20[0-3])[0-9]\b/
ABCresult
0XX0
1000
11X1
1011
94 && ($sep ne 'From ' || $line =~ m/ (?:19[6-9]|20[0-3])[0-9]\b/ );
95 }
96  
97 sub readSeparator()
98 29 ▶{ my $self = shift;
Subroutine readSeparator: called CC=6 SCAR=19.7
Pod documented
99 29 ▶ my $sep = $self->activeSeparator // return ();
Condition50%1/2
$self->activeSeparator // (return ())
l!l
290
MC/DC0%0/1
$self->activeSeparator // (return ())
$self->activeSeparator
Truth table50%1/2
A
A= $self->activeSeparator
Aresult
11
00
100 29 my $lines = $self->lines;
101  
102 29 my $skip = 0;
103 0 ▶ while($skip < @$lines && $lines->[$skip] =~ $is_empty_line) { $skip++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($skip < @$lines and $lines->[$skip] =~ /$is_empty_line/) 0 29 29
Condition33%1/3
$skip < @$lines and $lines->[$skip] =~ /$is_empty_line/
!ll&&!rl&&r
0290
MC/DC0%0/2
$skip < @$lines and $lines->[$skip] =~ /$is_empty_line/
$skip < @$lines $lines->[$skip] =~ /$is_empty_line/
Truth table33%1/3
A and B
A= $skip < @$linesB= $lines->[$skip] =~ /$is_empty_line/
ABresult
0X0
100
111
104  
105 29 ▶ $skip < @$lines
Branch: 1 branch
BranchTrueFalseTotal
$skip < @$lines or return () 29 0 29
106 or return ();
107  
108 29 my $line = $lines->[$skip];
109 29 ▶ substr($line, 0, length $sep) eq $sep
Branch: 1 branch
BranchTrueFalseTotal
substr($line, 0, length $sep) eq $sep or return () 29 0 29
110 or return ();
111  
112 29 splice @$lines, 0, $skip+1;
113 29 (undef, $line);
114 }
115  
116 sub _read_stripped_lines(;$$)
117 45 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine _read_stripped_lines: called CC=16 SCAR=28.7
118 45 my $seps = $self->separators;
119 45 my $lines = $self->lines;
120 45 my $take = [];
121  
122 45 ▶ if(@$seps)
Branch: 1 branch
BranchTrueFalseTotal
if (@$seps) { } 29 16 45
123 {
124 LINE:
125 29 while(1)
126 120 ▶ { my $line = shift @$lines or last LINE;
Branch: 1 branch
BranchTrueFalseTotal
my $line = shift @$lines or last LINE 120 0 120
127  
128 120 foreach my $sep (@$seps)
129 135 ▶ { substr($line, 0, length $sep) eq $sep or next;
Branch: 1 branch
BranchTrueFalseTotal
substr($line, 0, length $sep) eq $sep or next 29 106 135
130  
131 # Some apps fail to escape take starting with From
132 29 ▶ next if $sep eq 'From ' && $line !~ m/ 19[789][0-9]| 20[0-9][0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/ 0 29 29
Condition33%1/3
$sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
!ll&&!rl&&r
2900
MC/DC0%0/2
$sep eq "From " and not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
$sep eq "From " not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
Truth table33%1/3
A and not(B)
A= $sep eq "From "B= not $line =~ / 19[789][0-9]| 20[0-9][0-9]/
ABresult
0X0
100
111
133  
134 29 unshift @$lines, $line; # keep separator
135 29 last LINE;
136 }
137  
138 91 push @$take, $line;
139 }
140  
141 29 ▶ if(@$take && $take->[-1] =~ s/\015?\012\z//)
Branch: 1 branch
BranchTrueFalseTotal
if (@$take and $take->[-1] =~ s/\015?\012\z//) 22 7 29
Condition66%2/3
@$take and $take->[-1] =~ s/\015?\012\z//
!ll&&!rl&&r
7022
MC/DC50%1/2
@$take and $take->[-1] =~ s/\015?\012\z//
@$take $take->[-1] =~ s/\015?\012\z//
Truth table66%2/3
A and B
A= @$takeB= $take->[-1] =~ s/\015?\012\z//
ABresult
0X0
100
111
142 { # Keep an empty line to signal the existence of a preamble, but
143 # remove a second.
144 22 ▶ pop @$take if @$seps==1 && @$take > 1 && length($take->[-1])==0;
Branch: 1 branch
BranchTrueFalseTotal
if @$seps == 1 and @$take > 1 and length $take->[-1] == 0 16 6 22
Condition100%6/6
@$seps == 1 and @$take > 1 and length $take->[-1] == 0
!ll&&!rl&&r
5116
@$seps == 1 and @$take > 1
!ll&&!rl&&r
3217
MC/DC100%3/3
@$seps == 1 and @$take > 1 and length $take->[-1] == 0
@$seps == 1 @$take > 1 length $take->[-1] == 0
Truth table100%4/4
A and B and C
A= @$seps == 1B= @$take > 1C= length $take->[-1] == 0
ABCresult
0XX0
10X0
1100
1111
145 }
146 }
147 else # File without separators.
148 16 { $take = $lines;
149 }
150  
151 45 ▶ if($self->stripGt)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stripGt) 0 45 45
152 0 { s/^\>(\>*From\s)/$1/ for @$take;
153 }
154  
155 45 ▶ unless($self->trusted)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->trusted) 0 45 45
156 0 { s/\015// for @$take; # remove \r, keep \n
157 }
158  
159 45 $take;
160 }
161  
162 sub bodyAsString(;$$)
163 0 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsString: not called CC=1 SCAR=6.9
Pod documented
164 0 my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
165 0 return (undef, undef, join('', @$take));
166 }
167  
168 sub bodyAsList(;$$)
169 45 ▶{ my ($self, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsList: called CC=1 SCAR=0.0
Pod documented
170 45 my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
171 45 (undef, undef, $take);
172 }
173  
174 sub bodyAsFile($;$$)
175 0 ▶{ my ($self, $out, $exp_chars, $exp_lines) = @_;
Subroutine bodyAsFile: not called CC=2 SCAR=17.9
Pod documented
176 0 my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
177 0 $out->print($_) for @$take;
178 0 (undef, undef, scalar @$take);
179 }
180  
181 8 1;
« Message/Body/Construct.pm ↑ Summary Message/Construct/Forward.pm »