Message/Field/Attribute.pm

filter: Statementstmt 97.4% 115 / 118 Branchbran 90.3% 56 / 62 Conditioncond 76.1% 16 / 21 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 19 / 19 Podpod 90.9% 10 / 11 totaltotal 91.8% 224 / 244 CC 42 SCAR 19.4
CC
42
Cov
93%
encode
14.2
init
8.0
addComponent
7.0
CRAP 6.9
« Message/Head/ResentGroup.pm ↑ Summary Message/Field.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::Field::Attribute;{
13 28 our $VERSION = '4.05';
14 }
15  
16 28 ▶use parent 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 28 ▶use Log::Report 'mail-message', import => [ qw/__x error warning/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 28 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 use overload
28 345 ▶ '""' => sub { $_[0]->value },
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine __ANON__: called CC=1 SCAR=0.0
29 203 ▶ cmp => sub {
Subroutine __ANON__: called CC=3 SCAR=15.4
30 203 my ($self, $other) = @_;
31 203 ▶ UNIVERSAL::isa($other, 'Mail::Message::Field')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($other, 'Mail::Message::Field') ? : 0 203 203
Condition0%0/3
lc($_[0])->name cmp lc $_[1]->name or $_[0]->value cmp $_[1]->value
l!l&&r!l&&!r
000
MC/DC0%0/2
lc($_[0])->name cmp lc $_[1]->name or $_[0]->value cmp $_[1]->value
lc($_[0])->name cmp lc $_[1]->name $_[0]->value cmp $_[1]->value
Truth table0%0/3
A or B
A= lc($_[0])->name cmp lc $_[1]->nameB= $_[0]->value cmp $_[1]->value
ABresult
1X1
011
000
32 ? (lc($_[0])->name cmp lc($_[1]->name) || $_[0]->value cmp $_[1]->value)
33 : $_[0]->value cmp $_[1];
34 },
35 28 fallback => 1;
36  
37  
38 #--------------------
39  
40 sub new($$@)
41 133 ▶{ my ($class, $attr) = (shift, shift);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
42 133 ▶ my $value = @_ % 2 == 1 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 48 85 133
43 133 $class->SUPER::new(attr => $attr, value => $value, @_);
44 }
45  
46 sub init($$)
47 133 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=20.8
Pod undocumented
48 133 $self->SUPER::init($args);
49  
50 133 my ($attr, $value, $cont) = @$args{ qw/attr value use_continuations/ };
51  
52 133 ▶ my $name = ($attr =~ m/^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? $1 : $attr);
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? : 77 56 133
53 133 ▶ warning __x"illegal character in parameter name '{name}'.", name => $name
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[!#-'*+\-.0-9A-Z^-~]+$/ 0 133 133
54 if $name !~ m/^[!#-'*+\-.0-9A-Z^-~]+$/;
55  
56 133 $self->{MMFF_name} = $name;
57 133 ▶ $self->{MMFF_usecont} = $cont // 1;
Condition100%2/2
$cont // 1
l!l
6127
MC/DC100%1/1
$cont // 1
$cont
Truth table100%2/2
A
A= $cont
Aresult
11
00
58 133 ▶ $self->{MMFF_charset} = $args->{charset} if defined $args->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"charset"} 3 130 133
59 133 ▶ $self->{MMFF_language} = $args->{language} if defined $args->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"language"} 1 132 133
60  
61 133 ▶ $self->value(defined $value ? "$value" : ''); # enforce stringification
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 48 85 133
62 133 ▶ $self->addComponent($attr) if $attr ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attr ne $name 77 56 133
63  
64 133 $self;
65 }
66  
67 #--------------------
68  
69 79 ▶sub name() { $_[0]->{MMFF_name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
70  
71  
72 sub value(;$)
73 719 ▶{ my $self = shift;
Subroutine value: called CC=3 SCAR=11.0
Pod documented
74 719 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 141 578 719
75 141 { delete $self->{MMFF_cont};
76 141 return $self->{MMFF_value} = shift;
77 }
78 578 ▶ exists $self->{MMFF_value} ? $self->{MMFF_value} : $self->decode;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'MMFF_value'} ? : 488 90 578
79 }
80  
81  
82 sub addComponent($)
83 91 ▶{ my ($self, $component) = @_;
Subroutine addComponent: called CC=7 SCAR=19.5
Pod documented
84 91 ▶ defined $component or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $component or return 91 0 91
85 91 delete $self->{MMFF_value};
86  
87 91 my ($name, $value) = split /\=/, $component, 2;
88 91 ▶ if( substr($name, -1) eq '*' && $value =~ m/^([^']*)\'([^']*)\'/ )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/) 6 85 91
Condition100%3/3
substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/
!ll&&!rl&&r
8146
MC/DC100%2/2
substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/
substr($name, -1) eq "*" $value =~ /^([^']*)\'([^']*)\'/
Truth table100%3/3
A and B
A= substr($name, -1) eq "*"B= $value =~ /^([^']*)\'([^']*)\'/
ABresult
0X0
100
111
89 6 ▶ { $self->{MMFF_charset} = length $1 ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 4 2 6
90 6 ▶ $self->{MMFF_language} = length $2 ? $2 : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $2 ? : 2 4 6
91 }
92  
93 91 ▶ if( $name =~ m/\*([0-9]+)\*?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\*([0-9]+)\*?$/) { } 8 83 91
94 8 { $self->{MMFF_cont}[$1] = $component }
95 83 else { $self->{MMFF_cont} = [ $component ] }
96  
97 91 $component;
98 }
99  
100  
101 7 ▶sub charset() { $_[0]->{MMFF_charset} }
Subroutine charset: called CC=1 SCAR=0.0
Pod documented
102  
103  
104 7 ▶sub language() { $_[0]->{MMFF_language} }
Subroutine language: called CC=1 SCAR=0.0
Pod documented
105  
106  
107 sub string()
108 66 ▶{ my $self = shift;
Subroutine string: called CC=3 SCAR=11.0
Pod documented
109 66 ▶ my $cont = $self->{MMFF_cont} || $self->encode;
Condition66%2/3
$self->{'MMFF_cont'} || $self->encode
l!l&&r!l&&!r
15510
MC/DC0%0/2
$self->{'MMFF_cont'} || $self->encode
$self->{'MMFF_cont'} $self->encode
Truth table66%2/3
A || B
A= $self->{'MMFF_cont'}B= $self->encode
ABresult
1X1
011
000
110 66 ▶ wantarray? @$cont : (join '; ', '', @$cont);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 57 9 66
111 }
112  
113 #--------------------
114  
115 sub encode()
116 51 ▶{ my $self = shift;
Subroutine encode: called CC=14 SCAR=26.5
Pod documented
117 51 my $value = $self->{MMFF_value};
118  
119 51 my @lines;
120 51 my ($pre, $encoded);
121  
122 51 ▶ my $charset = $self->{MMFF_charset} || '';
Condition100%2/2
$self->{'MMFF_charset'} || ''
l!l
645
MC/DC100%1/1
$self->{'MMFF_charset'} || ''
$self->{'MMFF_charset'}
Truth table100%2/2
A
A= $self->{'MMFF_charset'}
Aresult
11
00
123 51 ▶ my $lang = $self->{MMFF_language} || '';
Condition100%2/2
$self->{'MMFF_language'} || ''
l!l
150
MC/DC100%1/1
$self->{'MMFF_language'} || ''
$self->{'MMFF_language'}
Truth table100%2/2
A
A= $self->{'MMFF_language'}
Aresult
11
00
124 51 my $name = $self->{MMFF_name};
125 51 my $cont = $self->{MMFF_usecont};
126  
127 51 ▶ if($charset || $lang)
Branch: 2 branches
BranchTrueFalseTotal
if ($charset or $lang) { } 6 45 51
elsif (grep /[^\x20-\x7E]/, $value) { } 0 45 45
Condition66%2/3
$charset or $lang
l!l&&r!l&&!r
6045
MC/DC50%1/2
$charset or $lang
$charset $lang
Truth table66%2/3
A or B
A= $charsetB= $lang
ABresult
1X1
011
000
128 6 { $pre = "$name*0*=$charset'$lang'";
129 6 $value = Encode::encode($charset, $value, 0);
130 6 $encoded = 1;
131 }
132 elsif(grep m/[^\x20-\x7E]/, $value)
133 0 { $pre = "$name*0*=''";
134 0 $encoded = 1;
135 }
136 else
137 45 { $pre = "$name*0=";
138 45 $value =~ s/"/\\"/g;
139 45 $encoded = 0;
140 }
141  
142 51 ▶ if($encoded)
Branch: 2 branches
BranchTrueFalseTotal
if ($encoded) { } 6 45 51
elsif ($cont) { } 45 0 45
143 { # Use encoding
144 6 my @c = split //, $value;
145 6 ▶ while(@c)
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 169 6 175
146 169 { my $c = shift @c;
147 169 ▶ $c = '%'. sprintf "%02X", ord $c
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /[a-zA-Z0-9]/ 66 103 169
148 unless $c =~ m/[a-zA-Z0-9]/;
149  
150 169 ▶ if($cont && length($pre) + length($c)> 76)
Branch: 1 branch
BranchTrueFalseTotal
if ($cont and length($pre) + length($c) > 76) { } 2 167 169
Condition100%3/3
$cont and length($pre) + length($c) > 76
!ll&&!rl&&r
83842
MC/DC100%2/2
$cont and length($pre) + length($c) > 76
$cont length($pre) + length($c) > 76
Truth table100%3/3
A and B
A= $contB= length($pre) + length($c) > 76
ABresult
0X0
100
111
151 2 { push @lines, $pre;
152 2 $pre = $name . '*' . @lines . '*=' . $c;
153 }
154 167 else { $pre .= $c }
155 }
156 6 push @lines, $pre;
157 }
158 elsif($cont)
159 { # Simple string, but with continuations
160 45 while(1)
161 46 { push @lines, $pre.'"'. substr($value, 0, 75-length($pre), '') .'"';
162 46 ▶ length $value or last;
Branch: 1 branch
BranchTrueFalseTotal
length $value or last 1 45 46
163 1 $pre = $name . '*' . @lines . '=';
164 }
165  
166 }
167 else
168 { # Single string only
169 0 push @lines, $pre . $value;
170 }
171  
172 51 ▶ $lines[0] =~ s/\*0// if @lines==1;
Branch: 1 branch
BranchTrueFalseTotal
if @lines == 1 49 2 51
173 51 $self->{MMFF_cont} = \@lines;
174 }
175  
176  
177 sub decode()
178 90 ▶{ my $self = shift;
Subroutine decode: called CC=7 SCAR=19.5
Pod documented
179 90 my $value = '';
180  
181 90 foreach my $cont ( @{$self->{MMFF_cont}} )
182 96 ▶ { unless(defined $cont)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cont) 1 95 96
183 1 { $value .= "[continuation missing]";
184 1 next;
185 }
186  
187 95 (my $name, local $_) = split /\=/, $cont, 2;
188  
189 95 ▶ if(substr($name, -1) eq '*')
Branch: 3 branches
BranchTrueFalseTotal
if (substr($name, -1) eq '*') { } 12 83 95
elsif (s/^\"(.*)\"$/$1/) { } 1 82 83
elsif (s/^\'(.*)\'$/$1/) { } 2 80 82
190 12 { s/^[^']*\'[^']*\'//;
191 12 s/\%([a-fA-F0-9]{2})/chr hex $1/ge;
192 }
193 1 elsif( s/^\"(.*)\"$/$1/ ) { s/\\\"/"/g }
194 2 elsif( s/^\'(.*)\'$/$1/ ) { s/\\\'/'/g }
195  
196 95 $value .= $_;
197 }
198  
199 90 my $charset = $self->{MMFF_charset};
200 90 ▶ $value = Encode::decode($charset, $value, 0) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 5 85 90
201  
202 90 $self->{MMFF_value} = $value;
203 }
204  
205 #--------------------
206  
207 sub mergeComponent($)
208 1 ▶{ my ($self, $comp) = @_;
Subroutine mergeComponent: called CC=3 SCAR=11.1
Pod documented
209 my $cont = $self->{MMFF_cont}
210 1 ▶ or error __x"too late to merge: value already changed.";
Branch: 1 branch
BranchTrueFalseTotal
my $cont = $self->{"MMFF_cont"} or &error(&__x("too late to merge: value already changed.")) 1 0 1
211  
212 1 $self->addComponent($_) for @{$comp->{MMFF_cont}};
213 1 $self;
214 }
215  
216 28 1;
« Message/Head/ResentGroup.pm ↑ Summary Message/Field.pm »