Mail/Header.pm

filter: Statementstmt 63.8% 201 / 315 Branchbran 44.0% 81 / 184 Conditioncond 42.5% 40 / 94 MC/DCmcdc 23.6% 13 / 55 Subroutinesub 76.6% 23 / 30 Podpod 100% 22 / 22 totaltotal 54.2% 380 / 700 CC 136 SCAR 32.9
CC
136
Cov
54%
read
90.0
_tidy_header
72.0
replace
56.0
CRAP 26.7
« Mail/Util.pm ↑ Summary Mail/Mailer/rfc822.pm »
1 # Copyrights 1995-2024 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 the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Header;{
10 3 our $VERSION = '2.22';
11 }
12  
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 my $MAIL_FROM = 'KEEP';
18 3 my %HDR_LENGTHS = ();
19  
20 3 our $FIELD_NAME = '[^\x00-\x1f\x7f-\xff :]+:';
21  
22  
23 ##
24 ## Private functions
25 ##
26  
27 1 ▶sub _error { warn @_; () }
Subroutine _error: called CC=1 SCAR=0.0
28  
29 # tidy up internal hash table and list
30  
31 sub _tidy_header
32 0 ▶{ my $self = shift;
Subroutine _tidy_header: not called CC=8 SCAR=42.8
33 0 my $deleted = 0;
34  
35 0 ▶ for(my $i = 0 ; $i < @{$self->{mail_hdr_list}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mail_hdr_list"};}) 0 0 0
36 0 ▶ { next if defined $self->{mail_hdr_list}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mail_hdr_list"}[$i] 0 0 0
37  
38 0 splice @{$self->{mail_hdr_list}}, $i, 1;
39 0 $deleted++;
40 0 $i--;
41 }
42  
43 0 ▶ if($deleted)
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted) 0 0 0
44 0 { local $_;
45 0 my @del;
46  
47 0 ▶ while(my ($key,$ref) = each %{$self->{mail_hdr_hash}} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"mail_hdr_hash"};}) 0 0 0
48 { push @del, $key
49 0 ▶ unless @$ref = grep { ref $_ && defined $$_ } @$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ref = grep({ref $_ and defined $$_;} @$ref) 0 0 0
Condition0%0/3
ref $_ && defined $$_
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ && defined $$_
ref $_ defined $$_
Truth table0%0/3
A && B
A= ref $_B= defined $$_
ABresult
0X0
100
111
50 }
51  
52 0 delete $self->{'mail_hdr_hash'}{$_} for @del;
53 }
54 }
55  
56 # fold the line to the given length
57  
58 3 my %STRUCTURE = map { (lc $_ => undef) }
59 qw{ To Cc Bcc From Date Reply-To Sender
60 Resent-Date Resent-From Resent-Sender Resent-To Return-Path
61 list-help list-post list-unsubscribe Mailing-List
62 Received References Message-ID In-Reply-To
63 Content-Length Content-Type Content-Disposition
64 Delivered-To
65 Lines
66 MIME-Version
67 Precedence
68 Status
69 };
70  
71 sub _fold_line
72 27 ▶{ my($ln,$maxlen) = @_;
Subroutine _fold_line: called CC=7 SCAR=19.5
73  
74 27 ▶ $maxlen = 20
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen < 20 0 27 27
75 if $maxlen < 20;
76  
77 27 my $max = int($maxlen - 5); # 4 for leading spcs + 1 for [\,\;]
78 27 my $min = int($maxlen * 4 / 5) - 4;
79  
80 27 $_[0] =~ s/[\r\n]+//og; # Remove new-lines
81 27 $_[0] =~ s/\s*\Z/\n/so; # End line with an EOLN
82  
83 27 ▶ return if $_[0] =~ /^From\s/io;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^From\s/io 0 27 27
84  
85 27 ▶ if(length($_[0]) > $maxlen)
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] > $maxlen) 14 13 27
86 14 ▶ { if($_[0] =~ /^([-\w]+)/ && exists $STRUCTURE{ lc $1 } )
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([-\w]+)/ and exists $STRUCTURE{lc $1}) { } 9 5 14
Condition66%2/3
$_[0] =~ /^([-\w]+)/ and exists $STRUCTURE{lc $1}
!ll&&!rl&&r
059
MC/DC50%1/2
$_[0] =~ /^([-\w]+)/ and exists $STRUCTURE{lc $1}
$_[0] =~ /^([-\w]+)/ exists $STRUCTURE{lc $1}
Truth table66%2/3
A and B
A= $_[0] =~ /^([-\w]+)/B= exists $STRUCTURE{lc $1}
ABresult
0X0
100
111
87 { #Split the line up
88 # first bias towards splitting at a , or a ; >4/5 along the line
89 # next split a whitespace
90 # else we are looking at a single word and probably don't want to split
91 9 my $x = "";
92 9 ▶ $x .= "$1\n " while $_[0] =~
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ s/^\s* ( [^"]{$min,$max} [,;] | [^"]{1,$max} [,;\s] | [^\s"]*(?:"[^"]*"[ \t]?[^\s"]*)+\s ) //x and do { $x .= "$1\n " } 22 9 31
93 s/^\s*
94 ( [^"]{$min,$max} [,;]
95 | [^"]{1,$max} [,;\s]
96 | [^\s"]*(?:"[^"]*"[ \t]?[^\s"]*)+\s
97 ) //x;
98  
99 9 $x .= $_[0];
100 9 $_[0] = $x;
101 9 $_[0] =~ s/(\A\s+|[\t ]+\Z)//sog;
102 9 $_[0] =~ s/\s+\n/\n/sog;
103 }
104 else
105 5 { $_[0] =~ s/(.{$min,$max})(\s)/$1\n$2/g;
106 5 $_[0] =~ s/\s*$/\n/s;
107 }
108 }
109  
110 27 $_[0] =~ s/\A(\S+)\n\s*(?=\S)/$1 /so;
111 }
112  
113 # Tags are case-insensitive, but there is a (slightly) preferred construction
114 # being all characters are lowercase except the first of each word. Also
115 # if the word is an `acronym' then all characters are uppercase. We decide
116 # a word is an acronym if it does not contain a vowel.
117 # In general, this change of capitalization is a bad idea, but it is in
118 # the code for ages, and therefore probably crucial for existing
119 # applications.
120  
121 sub _tag_case
122 61 ▶{ my $tag = shift;
Subroutine _tag_case: called CC=2 SCAR=6.9
123 61 $tag =~ s/\:$//;
124 join '-'
125 61 ▶ , map { /^[b-df-hj-np-tv-z]+$|^(?:MIME|SWE|SOAP|LDAP|ID)$/i
Branch: 1 branch
BranchTrueFalseTotal
/^[b-df-hj-np-tv-z]+$|^(?:MIME|SWE|SOAP|LDAP|ID)$/i ? : 3 66 69
126 ? uc($_) : ucfirst(lc($_))
127 } split m/\-/, $tag, -1;
128 }
129  
130 # format a complete line
131 # ensure line starts with the given tag
132 # ensure tag is correct case
133 # change the 'From ' tag as required
134 # fold the line
135  
136 sub _fmt_line
137 25 ▶{ my ($self, $tag, $line, $modify) = @_;
Subroutine _fmt_line: called CC=22 SCAR=38.0
138 25 ▶ $modify ||= $self->{mail_hdr_modify};
Condition66%2/3
$modify ||= $self->{"mail_hdr_modify"}
l!l&&r!l&&!r
0916
MC/DC50%1/2
$modify ||= $self->{"mail_hdr_modify"}
$modify $self->{"mail_hdr_modify"}
Truth table66%2/3
A ||= B
A= $modifyB= $self->{"mail_hdr_modify"}
ABresult
1X1
011
000
139 25 my $ctag = undef;
140  
141 25 ▶ ($tag) = $line =~ /^($FIELD_NAME|From )/oi
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 25 25
142 unless defined $tag;
143  
144 25 ▶ if(defined $tag && $tag =~ /^From /io && $self->{mail_hdr_mail_from} ne 'KEEP')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag and $tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP") 0 25 25
Condition66%4/6
defined $tag and $tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP"
!ll&&!rl&&r
2320
defined $tag and $tag =~ /^From /io
!ll&&!rl&&r
0232
MC/DC0%0/3
defined $tag and $tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP"
defined $tag $tag =~ /^From /io $self->{"mail_hdr_mail_from"} ne "KEEP"
Truth table50%2/4
A and B and C
A= defined $tagB= $tag =~ /^From /ioC= $self->{"mail_hdr_mail_from"} ne "KEEP"
ABCresult
0XX0
10X0
1100
1111
145 0 ▶ { if($self->{mail_hdr_mail_from} eq 'COERCE')
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'mail_hdr_mail_from'} eq 'COERCE') { } 0 0 0
elsif ($self->{'mail_hdr_mail_from'} eq 'IGNORE') { } 0 0 0
elsif ($self->{'mail_hdr_mail_from'} eq 'ERROR') { } 0 0 0
146 0 { $line =~ s/^From /Mail-From: /o;
147 0 $tag = "Mail-From:";
148 }
149 elsif($self->{mail_hdr_mail_from} eq 'IGNORE')
150 0 { return ();
151 }
152 elsif($self->{mail_hdr_mail_from} eq 'ERROR')
153 0 { return _error "unadorned 'From ' ignored: <$line>";
154 }
155 }
156  
157 25 ▶ if(defined $tag)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 25 0 25
158 25 { $tag = _tag_case($ctag = $tag);
159 25 ▶ $ctag = $tag if $modify;
Branch: 1 branch
BranchTrueFalseTotal
if $modify 9 16 25
160 25 ▶ $ctag =~ s/([^ :])$/$1:/o if defined $ctag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ctag 25 0 25
161 }
162  
163 25 ▶ defined $ctag && $ctag =~ /^($FIELD_NAME|From )/oi
Branch: 1 branch
BranchTrueFalseTotal
defined $ctag and $ctag =~ /^($Mail::Header::FIELD_NAME|From )/io or croak("Bad RFC822 field name '${tag}'\n") 25 0 25
Condition33%1/3
defined $ctag and $ctag =~ /^($Mail::Header::FIELD_NAME|From )/io
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $ctag and $ctag =~ /^($Mail::Header::FIELD_NAME|From )/io
defined $ctag $ctag =~ /^($Mail::Header::FIELD_NAME|From )/io
Truth table33%1/3
A and B
A= defined $ctagB= $ctag =~ /^($Mail::Header::FIELD_NAME|From )/io
ABresult
0X0
100
111
164 or croak "Bad RFC822 field name '$tag'\n";
165  
166 # Ensure the line starts with tag
167 25 ▶ if(defined $ctag && ($modify || $line !~ /^\Q$ctag\E/i))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctag and $modify || !($line =~ /^\Q$ctag\E/i)) 15 10 25
Condition83%5/6
defined $ctag and $modify || !($line =~ /^\Q$ctag\E/i)
!ll&&!rl&&r
01015
$modify or not $line =~ /^\Q$ctag\E/i
l!l&&r!l&&!r
9610
MC/DC66%2/3
defined $ctag and $modify || !($line =~ /^\Q$ctag\E/i)
defined $ctag $modify not $line =~ /^\Q$ctag\E/i
Truth table75%3/4
A and B or not(C)
A= defined $ctagB= $modifyC= not $line =~ /^\Q$ctag\E/i
ABCresult
0XX0
1000
11X1
1011
168 15 { (my $xtag = $ctag) =~ s/\s*\Z//o;
169 15 $line =~ s/^(\Q$ctag\E)?\s*/$xtag /i;
170 }
171  
172 my $maxlen = $self->{mail_hdr_lengths}{$tag}
173 25 ▶ || $HDR_LENGTHS{$tag}
Condition33%2/6
$self->{'mail_hdr_lengths'}{$tag} || $HDR_LENGTHS{$tag} || $self->fold_length
l!l&&r!l&&!r
0250
$self->{"mail_hdr_lengths"}{$tag} or $HDR_LENGTHS{$tag}
l!l&&r!l&&!r
0025
MC/DC0%0/3
$self->{'mail_hdr_lengths'}{$tag} || $HDR_LENGTHS{$tag} || $self->fold_length
$self->{"mail_hdr_lengths"}{$tag} $HDR_LENGTHS{$tag} $self->fold_length
Truth table25%1/4
A or B || C
A= $self->{"mail_hdr_lengths"}{$tag}B= $HDR_LENGTHS{$tag}C= $self->fold_length
ABCresult
1XX1
01X1
0011
0000
174 || $self->fold_length;
175  
176 25 ▶ if ($modify && defined $maxlen)
Branch: 2 branches
BranchTrueFalseTotal
if ($modify and defined $maxlen) { } 9 16 25
elsif ($line =~ /\r?\n\S/) { } 1 15 16
Condition66%2/3
$modify and defined $maxlen
!ll&&!rl&&r
1609
MC/DC50%1/2
$modify and defined $maxlen
$modify defined $maxlen
Truth table66%2/3
A and B
A= $modifyB= defined $maxlen
ABresult
0X0
100
111
177 { # folding will fix bad header continuations for us
178 9 _fold_line $line, $maxlen;
179 }
180 elsif($line =~ /\r?\n\S/)
181 1 { return _error "Bad header continuation, skipping '$tag': ",
182 "no space after newline in '$line'\n";
183 }
184  
185  
186 24 $line =~ s/\n*$/\n/so;
187 24 ($tag, $line);
188 }
189  
190 sub _insert
191 24 ▶{ my ($self, $tag, $line, $where) = @_;
Subroutine _insert: called CC=11 SCAR=30.4
192  
193 24 ▶ if($where < 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($where < 0) { } 24 0 24
elsif ($where >= @{$self->{'mail_hdr_list'};}) { } 0 0 0
194 24 { $where = @{$self->{mail_hdr_list}} + $where + 1;
195 24 ▶ $where = 0 if $where < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $where < 0 0 24 24
196 }
197 0 elsif($where >= @{$self->{mail_hdr_list}})
198 0 { $where = @{$self->{mail_hdr_list}};
199 }
200  
201 24 my $atend = $where == @{$self->{mail_hdr_list}};
202 24 splice @{$self->{mail_hdr_list}}, $where, 0, $line;
203  
204 24 ▶ $self->{mail_hdr_hash}{$tag} ||= [];
Condition100%2/2
$self->{"mail_hdr_hash"}{$tag} ||= []
l!l
816
MC/DC100%1/1
$self->{"mail_hdr_hash"}{$tag} ||= []
$self->{"mail_hdr_hash"}{$tag}
Truth table100%2/2
A
A= $self->{"mail_hdr_hash"}{$tag}
Aresult
11
00
205 24 my $ref = \${$self->{mail_hdr_list}}[$where];
206  
207 24 my $def = $self->{mail_hdr_hash}{$tag};
208 24 ▶ if($def && $where)
Branch: 1 branch
BranchTrueFalseTotal
if ($def and $where) { } 18 6 24
Condition66%2/3
$def and $where
!ll&&!rl&&r
0618
MC/DC50%1/2
$def and $where
$def $where
Truth table66%2/3
A and B
A= $defB= $where
ABresult
0X0
100
111
209 18 ▶ { if($atend) { push @$def, $ref }
Branch: 1 branch
BranchTrueFalseTotal
if ($atend) { } 18 0 18
210 else
211 0 { my $i = 0;
212 0 foreach my $ln (@{$self->{mail_hdr_list}})
213 0 { my $r = \$ln;
214 0 ▶ last if $r == $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $r == $ref 0 0 0
215 0 ▶ $i++ if $r == $def->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $r == $def->[$i] 0 0 0
216 }
217 0 splice @$def, $i, 0, $ref;
218 }
219 }
220 else
221 6 { unshift @$def, $ref;
222 }
223 }
224  
225 #------------
226  
227 sub new
228 11 ▶{ my $call = shift;
Subroutine new: called CC=10 SCAR=25.0
Pod documented
229 11 ▶ my $class = ref($call) || $call;
Condition33%1/3
ref $call || $call
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $call || $call
ref $call $call
Truth table33%1/3
A || B
A= ref $callB= $call
ABresult
1X1
011
000
230 11 ▶ my $arg = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 5 6 11
231 11 my %opt = @_;
232  
233 $opt{Modify} = delete $opt{Reformat}
234 11 ▶ unless exists $opt{Modify};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"Modify"} 8 3 11
235  
236 my $self = bless
237 { mail_hdr_list => []
238 , mail_hdr_hash => {}
239 11 ▶ , mail_hdr_modify => (delete $opt{Modify} || 0)
Condition100%2/2
delete $opt{'Modify'} || 0
l!l
29
MC/DC100%1/1
delete $opt{'Modify'} || 0
delete $opt{'Modify'}
Truth table100%2/2
A
A= delete $opt{'Modify'}
Aresult
11
00
240 , mail_hdr_foldlen => 79
241 , mail_hdr_lengths => {}
242 }, $class;
243  
244 11 ▶ $self->mail_from( uc($opt{MailFrom} || $MAIL_FROM) );
Condition33%1/3
$opt{'MailFrom'} || $MAIL_FROM
l!l&&r!l&&!r
0110
MC/DC0%0/2
$opt{'MailFrom'} || $MAIL_FROM
$opt{'MailFrom'} $MAIL_FROM
Truth table33%1/3
A || B
A= $opt{'MailFrom'}B= $MAIL_FROM
ABresult
1X1
011
000
245  
246 $self->fold_length($opt{FoldLength})
247 11 ▶ if exists $opt{FoldLength};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"FoldLength"} 0 11 11
248  
249 11 ▶ if(!ref $arg) {}
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $arg) { } 6 5 11
elsif (ref $arg eq 'ARRAY') { } 5 0 5
elsif (defined fileno $arg) { } 0 0 0
250 5 elsif(ref($arg) eq 'ARRAY') { $self->extract( [ @$arg ] ) }
251 0 elsif(defined fileno($arg)) { $self->read($arg) }
252  
253 11 $self;
254 }
255  
256  
257 sub dup
258 2 ▶{ my $self = shift;
Subroutine dup: called CC=2 SCAR=6.9
Pod documented
259 2 my $dup = ref($self)->new;
260  
261 2 %$dup = %$self;
262 2 $dup->empty; # rebuild tables
263  
264 2 $dup->{mail_hdr_list} = [ @{$self->{mail_hdr_list}} ];
265  
266 2 foreach my $ln ( @{$dup->{mail_hdr_list}} )
267 8 { my $tag = _tag_case +($ln =~ /^($FIELD_NAME|From )/oi)[0];
268 8 push @{$dup->{mail_hdr_hash}{$tag}}, \$ln;
269 }
270  
271 2 $dup;
272 }
273  
274 #------------
275  
276 sub extract
277 6 ▶{ my ($self, $lines) = @_;
Subroutine extract: called CC=7 SCAR=19.5
Pod documented
278 6 $self->empty;
279  
280 6 ▶ while(@$lines)
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 21 4 25
281 21 { my $line = shift @$lines;
282 21 ▶ last if $line =~ /^\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\r?$/ 2 19 21
283  
284 19 ▶ $line =~ /^($FIELD_NAME|From )/o or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^($Mail::Header::FIELD_NAME|From )/o or next 19 0 19
285 19 my $tag = $1;
286  
287 19 ▶ $line .= shift @$lines
Branch: 1 branch
BranchTrueFalseTotal
@$lines and $lines->[0] =~ /^[ \t]+/ and do { $line .= shift(@$lines) } 5 19 24
Condition100%3/3
@$lines and $lines->[0] =~ /^[ \t]+/
!ll&&!rl&&r
4155
MC/DC100%2/2
@$lines and $lines->[0] =~ /^[ \t]+/
@$lines $lines->[0] =~ /^[ \t]+/
Truth table100%3/3
A and B
A= @$linesB= $lines->[0] =~ /^[ \t]+/
ABresult
0X0
100
111
288 while @$lines && $lines->[0] =~ /^[ \t]+/;
289  
290 19 ($tag, $line) = _fmt_line $self, $tag, $line;
291  
292 19 ▶ _insert $self, $tag, $line, -1
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 18 1 19
293 if defined $line;
294 }
295  
296 6 $self;
297 }
298  
299  
300 sub read
301 0 ▶{ my ($self, $fd) = @_;
Subroutine read: not called CC=9 SCAR=45.0
Pod documented
302 0 $self->empty;
303  
304 0 my ($ln, $tag, $line);
305 0 while(1)
306 0 { $ln = <$fd>;
307  
308 0 ▶ if(defined $ln && defined $line && $ln =~ /^[ \t]+/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ln and defined $line and $ln =~ /^[ \t]+/) 0 0 0
Condition0%0/6
defined $ln and defined $line and $ln =~ /^[ \t]+/
!ll&&!rl&&r
000
defined $ln and defined $line
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ln and defined $line and $ln =~ /^[ \t]+/
defined $ln defined $line $ln =~ /^[ \t]+/
Truth table0%0/4
A and B and C
A= defined $lnB= defined $lineC= $ln =~ /^[ \t]+/
ABCresult
0XX0
10X0
1100
1111
309 0 { $line .= $ln; # folded line
310 0 next;
311 }
312  
313 0 ▶ if(defined $line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
314 0 { ($tag, $line) = _fmt_line $self, $tag, $line;
315 0 ▶ _insert $self, $tag, $line, -1
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
316 if defined $line;
317 0 ($tag, $line) = ();
318 }
319  
320 0 ▶ last if !defined $ln || $ln =~ m/^\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ln or $ln =~ /^\r?$/ 0 0 0
Condition0%0/3
not defined $ln or $ln =~ /^\r?$/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ln or $ln =~ /^\r?$/
not defined $ln $ln =~ /^\r?$/
Truth table0%0/3
not(A) or B
A= not defined $lnB= $ln =~ /^\r?$/
ABresult
1X1
011
000
321  
322 0 ▶ $ln =~ /^($FIELD_NAME|From )/o or next;
Branch: 1 branch
BranchTrueFalseTotal
$ln =~ /^($Mail::Header::FIELD_NAME|From )/o or next 0 0 0
323 0 ($tag, $line) = ($1, $ln);
324 }
325  
326 0 $self;
327 }
328  
329  
330 sub empty
331 8 ▶{ my $self = shift;
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
332 8 $self->{mail_hdr_list} = [];
333 8 $self->{mail_hdr_hash} = {};
334 8 $self;
335 }
336  
337  
338 sub header
339 1 ▶{ my $self = shift;
Subroutine header: called CC=3 SCAR=11.3
Pod documented
340  
341 1 ▶ $self->extract(@_)
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
342 if @_;
343  
344 $self->fold
345 1 ▶ if $self->{mail_hdr_modify};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mail_hdr_modify"} 0 1 1
346  
347 1 [ @{$self->{mail_hdr_list}} ];
348 }
349  
350  
351 sub header_hashref
352 2 ▶{ my ($self, $hashref) = @_;
Subroutine header_hashref: called CC=6 SCAR=17.9
Pod documented
353  
354 2 ▶ while(my ($key, $value) = each %$hashref)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hashref) 2 2 4
355 2 ▶ { $self->add($key, $_) for ref $value ? @$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 1 2
356 }
357  
358 $self->fold
359 2 ▶ if $self->{mail_hdr_modify};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mail_hdr_modify"} 0 2 2
360  
361 defined wantarray # MO, added minimal optimization
362 2 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or return 1 1 2
363  
364 3 +{ map { ($_ => [$self->get($_)] ) } # MO: Eh?
365 1 keys %{$self->{mail_hdr_hash}}
366 };
367 }
368  
369 #------------
370  
371 sub modify
372 3 ▶{ my $self = shift;
Subroutine modify: called CC=2 SCAR=7.0
Pod documented
373 3 my $old = $self->{mail_hdr_modify};
374  
375 3 ▶ $self->{mail_hdr_modify} = 0 + shift
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 0 3
376 if @_;
377  
378 3 $old;
379 }
380  
381  
382 sub mail_from
383 11 ▶{ my $thing = shift;
Subroutine mail_from: called CC=3 SCAR=11.6
Pod documented
384 11 my $choice = uc shift;
385  
386 11 ▶ $choice =~ /^(IGNORE|ERROR|COERCE|KEEP)$/
Branch: 1 branch
BranchTrueFalseTotal
$choice =~ /^(IGNORE|ERROR|COERCE|KEEP)$/ or die "bad Mail-From choice: '${choice}'" 11 0 11
387 or die "bad Mail-From choice: '$choice'";
388  
389 11 ▶ if(ref $thing) { $thing->{mail_hdr_mail_from} = $choice }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing) { } 11 0 11
390 0 else { $MAIL_FROM = $choice }
391  
392 11 $thing;
393 }
394  
395  
396 sub fold_length
397 28 ▶{ my $thing = shift;
Subroutine fold_length: called CC=7 SCAR=24.2
Pod documented
398 28 my $old;
399  
400 28 ▶ if(@_ == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 28 28
401 0 { my $tag = _tag_case shift;
402 0 my $len = shift;
403  
404 0 ▶ my $hash = ref $thing ? $thing->{mail_hdr_lengths} : \%HDR_LENGTHS;
Branch: 1 branch
BranchTrueFalseTotal
ref $thing ? : 0 0 0
405 0 $old = $hash->{$tag};
406 0 ▶ $hash->{$tag} = $len > 20 ? $len : 20;
Branch: 1 branch
BranchTrueFalseTotal
$len > 20 ? : 0 0 0
407 }
408 else
409 28 { my $self = $thing;
410 28 my $len = shift;
411 28 $old = $self->{mail_hdr_foldlen};
412  
413 28 ▶ if(defined $len)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 3 25 28
414 3 ▶ { $self->{mail_hdr_foldlen} = $len > 20 ? $len : 20;
Branch: 1 branch
BranchTrueFalseTotal
$len > 20 ? : 3 0 3
415 3 ▶ $self->fold if $self->{mail_hdr_modify};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mail_hdr_modify"} 0 3 3
416 }
417 }
418  
419 28 $old;
420 }
421  
422 #------------
423  
424 sub fold
425 3 ▶{ my ($self, $maxlen) = @_;
Subroutine fold: called CC=7 SCAR=22.1
Pod documented
426  
427 3 ▶ while(my ($tag, $list) = each %{$self->{mail_hdr_hash}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"mail_hdr_hash"};}) 9 3 12
428 { my $len = $maxlen
429 || $self->{mail_hdr_lengths}{$tag}
430 9 ▶ || $HDR_LENGTHS{$tag}
Condition33%3/9
$maxlen || $self->{'mail_hdr_lengths'}{$tag} || $HDR_LENGTHS{$tag} || $self->fold_length
l!l&&r!l&&!r
900
$maxlen or $self->{"mail_hdr_lengths"}{$tag} or $HDR_LENGTHS{$tag}
l!l&&r!l&&!r
900
$maxlen or $self->{"mail_hdr_lengths"}{$tag}
l!l&&r!l&&!r
900
MC/DC0%0/4
$maxlen || $self->{'mail_hdr_lengths'}{$tag} || $HDR_LENGTHS{$tag} || $self->fold_length
$maxlen $self->{"mail_hdr_lengths"}{$tag} $HDR_LENGTHS{$tag} $self->fold_length
Truth table20%1/5
A or B or C || D
A= $maxlenB= $self->{"mail_hdr_lengths"}{$tag}C= $HDR_LENGTHS{$tag}D= $self->fold_length
ABCDresult
1XXX1
01XX1
001X1
00011
00000
431 || $self->fold_length;
432  
433 9 foreach my $ln (@$list)
434 18 ▶ { _fold_line $$ln, $len
Branch: 1 branch
BranchTrueFalseTotal
if defined $ln 18 0 18
435 if defined $ln;
436 }
437 }
438  
439 3 $self;
440 }
441  
442  
443 sub unfold
444 1 ▶{ my $self = shift;
Subroutine unfold: called CC=10 SCAR=33.6
Pod documented
445  
446 1 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
447 0 { my $tag = _tag_case shift;
448 0 ▶ my $list = $self->{mail_hdr_hash}{$tag}
Branch: 1 branch
BranchTrueFalseTotal
my $list = $self->{"mail_hdr_hash"}{$tag} or return $self 0 0 0
449 or return $self;
450  
451 0 foreach my $ln (@$list)
452 0 ▶ { $$ln =~ s/\r?\n\s+/ /sog
Branch: 1 branch
BranchTrueFalseTotal
if defined $ln and defined $$ln 0 0 0
Condition0%0/3
defined $ln and defined $$ln
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ln and defined $$ln
defined $ln defined $$ln
Truth table0%0/3
A and B
A= defined $lnB= defined $$ln
ABresult
0X0
100
111
453 if defined $ln && defined $$ln;
454 }
455  
456 0 return $self;
457 }
458  
459 1 ▶ while( my ($tag, $list) = each %{$self->{mail_hdr_hash}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"mail_hdr_hash"};}) 3 1 4
460 3 { foreach my $ln (@$list)
461 6 ▶ { $$ln =~ s/\r?\n\s+/ /sog
Branch: 1 branch
BranchTrueFalseTotal
if defined $ln and defined $$ln 6 0 6
Condition33%1/3
defined $ln and defined $$ln
!ll&&!rl&&r
006
MC/DC0%0/2
defined $ln and defined $$ln
defined $ln defined $$ln
Truth table33%1/3
A and B
A= defined $lnB= defined $$ln
ABresult
0X0
100
111
462 if defined $ln && defined $$ln;
463 }
464 }
465  
466 1 $self;
467 }
468  
469  
470 sub add
471 6 ▶{ my ($self, $tag, $text, $where) = @_;
Subroutine add: called CC=4 SCAR=14.8
Pod documented
472 6 ($tag, my $line) = _fmt_line $self, $tag, $text;
473  
474 6 ▶ defined $tag && defined $line
Branch: 1 branch
BranchTrueFalseTotal
defined $tag and defined $line or return undef 6 0 6
Condition33%1/3
defined $tag and defined $line
!ll&&!rl&&r
006
MC/DC0%0/2
defined $tag and defined $line
defined $tag defined $line
Truth table33%1/3
A and B
A= defined $tagB= defined $line
ABresult
0X0
100
111
475 or return undef;
476  
477 6 ▶ defined $where
Branch: 1 branch
BranchTrueFalseTotal
defined $where or $where = -1 0 6 6
478 or $where = -1;
479  
480 6 _insert $self, $tag, $line, $where;
481  
482 6 $line =~ /^\S+\s(.*)/os;
483 6 $1;
484 }
485  
486  
487 sub replace
488 0 ▶{ my $self = shift;
Subroutine replace: not called CC=7 SCAR=40.3
Pod documented
489 0 ▶ my $idx = @_ % 2 ? pop @_ : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
490  
491 0 my ($tag, $line);
492 TAG:
493 0 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
494 0 { ($tag,$line) = _fmt_line $self, splice(@_,0,2);
495  
496 0 ▶ defined $tag && defined $line
Branch: 1 branch
BranchTrueFalseTotal
defined $tag and defined $line or return undef 0 0 0
Condition0%0/3
defined $tag and defined $line
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag and defined $line
defined $tag defined $line
Truth table0%0/3
A and B
A= defined $tagB= defined $line
ABresult
0X0
100
111
497 or return undef;
498  
499 0 my $field = $self->{mail_hdr_hash}{$tag};
500 0 ▶ if($field && defined $field->[$idx])
Branch: 1 branch
BranchTrueFalseTotal
if ($field and defined $field->[$idx]) { } 0 0 0
Condition0%0/3
$field and defined $field->[$idx]
!ll&&!rl&&r
000
MC/DC0%0/2
$field and defined $field->[$idx]
$field defined $field->[$idx]
Truth table0%0/3
A and B
A= $fieldB= defined $field->[$idx]
ABresult
0X0
100
111
501 0 { ${$field->[$idx]} = $line }
502 0 else { _insert $self, $tag, $line, -1 }
503 }
504  
505 0 $line =~ /^\S+\s*(.*)/os;
506 0 $1;
507 }
508  
509  
510 sub combine
511 0 ▶{ my $self = shift;
Subroutine combine: not called CC=6 SCAR=37.4
Pod documented
512 0 my $tag = _tag_case shift;
513 0 ▶ my $with = shift || ' ';
Condition0%0/2
shift() || ' '
l!l
00
MC/DC0%0/1
shift() || ' '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
514  
515 0 ▶ $tag =~ /^From /io && $self->{mail_hdr_mail_from} ne 'KEEP'
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP" and return _error("unadorned 'From ' ignored") 0 0 0
Condition0%0/3
$tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP"
!ll&&!rl&&r
000
MC/DC0%0/2
$tag =~ /^From /io and $self->{"mail_hdr_mail_from"} ne "KEEP"
$tag =~ /^From /io $self->{"mail_hdr_mail_from"} ne "KEEP"
Truth table0%0/3
A and B
A= $tag =~ /^From /ioB= $self->{"mail_hdr_mail_from"} ne "KEEP"
ABresult
0X0
100
111
516 and return _error "unadorned 'From ' ignored";
517  
518 0 ▶ my $def = $self->{mail_hdr_hash}{$tag}
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->{"mail_hdr_hash"}{$tag} or return undef 0 0 0
519 or return undef;
520  
521 0 ▶ return $def->[0]
Branch: 1 branch
BranchTrueFalseTotal
if @$def <= 1 0 0 0
522 if @$def <= 1;
523  
524 0 my @lines = $self->get($tag);
525 0 chomp @lines;
526  
527 0 my $line = (_fmt_line $self, $tag, join($with,@lines), 1)[1];
528  
529 0 $self->{mail_hdr_hash}{$tag} = [ \$line ];
530 0 $line;
531 }
532  
533  
534 sub get
535 26 ▶{ my $self = shift;
Subroutine get: called CC=8 SCAR=20.8
Pod documented
536 26 my $tag = _tag_case shift;
537 26 my $idx = shift;
538  
539 26 ▶ my $def = $self->{mail_hdr_hash}{$tag}
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->{"mail_hdr_hash"}{$tag} or return () 23 3 26
540 or return ();
541  
542 23 my $l = length $tag;
543 23 ▶ $l += 1 if $tag !~ / $/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ / $/o 23 0 23
544  
545 23 ▶ if(defined $idx || !wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx or not wantarray) 20 3 23
Condition100%3/3
defined $idx or not wantarray
l!l&&r!l&&!r
1643
MC/DC100%2/2
defined $idx or not wantarray
defined $idx not wantarray
Truth table100%3/3
A or not(B)
A= defined $idxB= not wantarray
ABresult
1X1
011
000
546 20 ▶ { $idx ||= 0;
Condition100%2/2
$idx ||= 0
l!l
119
MC/DC100%1/1
$idx ||= 0
$idx
Truth table100%2/2
A
A= $idx
Aresult
11
00
547 20 ▶ defined $def->[$idx] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $def->[$idx] or return undef 20 0 20
548 20 my $val = ${$def->[$idx]};
549 20 ▶ defined $val or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return undef 20 0 20
550  
551 20 $val = substr $val, $l;
552 20 $val =~ s/^\s+//;
553 20 return $val;
554 }
555  
556 3 map { my $tmp = substr $$_,$l; $tmp =~ s/^\s+//; $tmp } @$def;
557 }
558  
559  
560  
561 sub count
562 0 ▶{ my $self = shift;
Subroutine count: not called CC=2 SCAR=17.9
Pod documented
563 0 my $tag = _tag_case shift;
564 0 my $def = $self->{mail_hdr_hash}{$tag};
565 0 ▶ defined $def ? scalar(@$def) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $def ? : 0 0 0
566 }
567  
568  
569  
570 sub delete
571 2 ▶{ my $self = shift;
Subroutine delete: called CC=5 SCAR=27.2
Pod documented
572 2 my $tag = _tag_case shift;
573 2 my $idx = shift;
574 2 my @val;
575  
576 2 ▶ if(my $def = $self->{mail_hdr_hash}{$tag})
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $self->{"mail_hdr_hash"}{$tag}) 0 2 2
577 0 { my $l = length $tag;
578 0 ▶ $l += 2 if $tag !~ / $/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ / $/ 0 0 0
579  
580 0 ▶ if(defined $idx)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) { } 0 0 0
581 0 ▶ { if(defined $def->[$idx])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->[$idx]) 0 0 0
582 0 { push @val, substr ${$def->[$idx]}, $l;
583 0 undef ${$def->[$idx]};
584 }
585 }
586 else
587 0 { @val = map {my $x = substr $$_,$l; undef $$_; $x } @$def;
588 }
589  
590 0 _tidy_header($self);
591 }
592  
593 2 @val;
594 }
595  
596  
597  
598 sub print
599 1 ▶{ my $self = shift;
Subroutine print: called CC=5 SCAR=16.7
Pod documented
600 1 ▶ my $fd = shift || \*STDOUT;
Condition50%1/2
shift() || \*STDOUT
l!l
01
MC/DC0%0/1
shift() || \*STDOUT
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
601  
602 1 foreach my $ln (@{$self->{mail_hdr_list}})
603 6 ▶ { defined $ln or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $ln or next 6 0 6
604 6 ▶ print $fd $ln or return 0;
Branch: 1 branch
BranchTrueFalseTotal
print $fd $ln or return 0 6 0 6
605 }
606  
607 1 1;
608 }
609  
610  
611 6 ▶sub as_string { join '', grep {defined} @{shift->{mail_hdr_list}} }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
612  
613  
614 0 ▶sub tags { keys %{shift->{mail_hdr_hash}} }
Subroutine tags: not called CC=1 SCAR=6.9
Pod documented
615  
616  
617 sub cleanup
618 0 ▶{ my $self = shift;
Subroutine cleanup: not called CC=6 SCAR=37.4
Pod documented
619 0 my $deleted = 0;
620  
621 0 ▶ foreach my $key (@_ ? @_ : keys %{$self->{mail_hdr_hash}})
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
622 0 { my $fields = $self->{mail_hdr_hash}{$key};
623 0 foreach my $field (@$fields)
624 0 ▶ { next if $$field =~ /^\S+\s+\S/s;
Branch: 1 branch
BranchTrueFalseTotal
if $$field =~ /^\S+\s+\S/s 0 0 0
625 0 undef $$field;
626 0 $deleted++;
627 }
628 }
629  
630 0 ▶ _tidy_header $self
Branch: 1 branch
BranchTrueFalseTotal
if $deleted 0 0 0
631 if $deleted;
632  
633 0 $self;
634 }
635  
636 3 1;
« Mail/Util.pm ↑ Summary Mail/Mailer/rfc822.pm »