lib/Text/vCard/Node.pm

filter: Statementstmt 97.7% 256 / 262 Branchbran 94.4% 102 / 108 Conditioncond 76.6% 46 / 60 MC/DCmcdc 62.8% 22 / 35 Subroutinesub 94.4% 34 / 36 Podpod 100% 11 / 11 totaltotal 91.9% 471 / 512 CC 82 SCAR 20.4
CC
82
Cov
94%
new
22.0
_params
8.6
_wrap_utf8
8.0
CRAP 7.7
↑ Summary blib/lib/vCard.pm »
1 package Text::vCard::Node;
2 16 $Text::vCard::Node::VERSION = '3.09';
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use MIME::Base64 3.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use MIME::QuotedPrint 3.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Unicode::LineBreak;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use vars qw ( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Text::vCard::Node - Object for each node (line) of a vCard
16  
17 =head1 SYNOPSIS
18  
19 use Text::vCard::Node;
20  
21 my %data = (
22 'param' => {
23 'HOME,PREF' => 'undef',
24 },
25 'value' => ';;First work address - street;Work city;London;Work PostCode;CountryName',
26 );
27  
28 my $node = Text::vCard::Node->new({
29 node_type => 'address', # Auto upper cased
30 fields => ['po_box','extended','street','city','region','post_code','country'],
31 data => \%data,
32 });
33  
34 =head1 DESCRIPTION
35  
36 Package used by Text::vCard so that each element: ADR, N, TEL etc are objects.
37  
38 You should not need to use this module directly, L<Text::vCard> does it all for you.
39  
40 =head1 METHODS
41  
42 =head2 new()
43  
44 my $node = Text::vCard::Node->new({
45 node_type => 'address', # Auto upper cased
46 fields => \['po_box','extended','street','city','region','post_code','country'],
47 data => \%data,
48 });
49  
50 =head2 value()
51  
52 # Get the value for a standard single value node
53 my $value = $node->value();
54  
55 # Or set the value
56 $node->value('New value');
57
58 =head2 other()'s
59  
60 # The fields supplied in the conf area also methods.
61 my $po_box = $node->po_box(); # if the node was an ADR.
62
63 # Set the value.
64 my $street = $node->street('73 Sesame Street');
65  
66 =cut
67  
68 322 ▶sub new {
Subroutine new: called CC=22 SCAR=30.9
Pod documented
69 322 my ( $proto, $conf ) = @_;
70 322 ▶ my $class = ref($proto) || $proto;
Condition100%3/3
ref $proto || $proto
l!l&&r!l&&!r
13201
MC/DC100%2/2
ref $proto || $proto
ref $proto $proto
Truth table100%3/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
71 322 my $self = {};
72 322 ▶ carp "No fields defined" unless defined $conf->{'fields'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"fields"} 1 321 322
73 carp "fields is not an array ref"
74 321 ▶ unless ref( $conf->{'fields'} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf->{"fields"} eq "ARRAY" 1 320 321
75  
76 320 bless( $self, $class );
77  
78 319 ▶ $self->{encoding_out} = $conf->{encoding_out} || 'UTF-8';
Condition100%2/2
$conf->{'encoding_out'} || 'UTF-8'
l!l
3109
MC/DC100%1/1
$conf->{'encoding_out'} || 'UTF-8'
$conf->{'encoding_out'}
Truth table100%2/2
A
A= $conf->{'encoding_out'}
Aresult
11
00
79  
80 $self->{node_type} = uc( $conf->{node_type} )
81 319 ▶ if defined $conf->{node_type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"node_type"} 313 6 319
82 319 ▶ $self->group( $conf->{group} ) if defined $conf->{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"group"} 311 8 319
83  
84 # Store the field order.
85 319 $self->{'field_order'} = $conf->{'fields'};
86  
87 # store the actual field names so we can look them up
88 319 my %fields;
89 319 map { $fields{$_} = 1 } @{ $self->{'field_order'} };
90 319 $self->{'field_lookup'} = \%fields;
91  
92 319 ▶ if ( defined $conf->{'data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"data"}) 316 3 319
93  
94 # Populate now, rather than later (via AUTOLOAD)
95 # store values into object
96 316 ▶ if ( defined $conf->{'data'}->{'params'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"data"}{"params"}) 139 177 316
97 139 my %params;
98  
99 # Loop through array
100 139 foreach my $param_hash ( @{ $conf->{'data'}->{'params'} } ) {
101 192 ▶ while ( my ( $key, $value ) = each %{$param_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$param_hash;}) 192 192 384
102 192 my $t = 'type';
103  
104 # go through each key/value pair
105 192 my $param_list = $key;
106 192 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 175 17 192
107 175 $t = $key;
108  
109 # use value, not key as its 'type' => 'CELL',
110 # not 'CELL' => undef
111 175 $param_list = $value;
112 }
113  
114 # These values might as well be useful for
115 # something. Also get rid of any whitespace
116 # pollution.
117 192 for my $p ( split /\s*,\s*/, $param_list ) {
118 202 $p =~ s/^\s*(.*?)\s*$/\L$1/;
119 202 $p =~ s/\s+/ /g;
120 202 $params{$p} = lc $t;
121 }
122 }
123 }
124 139 $self->{params} = \%params;
125 }
126  
127 316 ▶ if ( defined $conf->{'data'}->{'value'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"data"}{"value"}) 280 36 316
128  
129 # Store the actual data into the object
130  
131 280 ▶ if ( $self->is_type('q') or $self->is_type('quoted-printable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_type("q") or $self->is_type("quoted-printable")) 9 271 280
Condition66%2/3
$self->is_type("q") or $self->is_type("quoted-printable")
l!l&&r!l&&!r
09271
MC/DC50%1/2
$self->is_type("q") or $self->is_type("quoted-printable")
$self->is_type("q") $self->is_type("quoted-printable")
Truth table66%2/3
A or B
A= $self->is_type("q")B= $self->is_type("quoted-printable")
ABresult
1X1
011
000
132  
133 9 my $value = $conf->{data}{value};
134 9 my $mime_decoded = MIME::QuotedPrint::decode($value);
135 9 my $encode_decoded = Encode::decode( 'UTF-8', $mime_decoded );
136 9 my $unescaped = $self->_unescape($encode_decoded);
137 9 $conf->{'data'}->{'value'} = $unescaped;
138 }
139  
140 280 ▶ if ( $self->is_type('b') or $self->is_type('base64') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_type('b') or $self->is_type('base64')) { } 1 279 280
Condition66%2/3
$self->is_type("b") or $self->is_type("base64")
l!l&&r!l&&!r
10279
MC/DC50%1/2
$self->is_type("b") or $self->is_type("base64")
$self->is_type("b") $self->is_type("base64")
Truth table66%2/3
A or B
A= $self->is_type("b")B= $self->is_type("base64")
ABresult
1X1
011
000
141  
142 # Don't Encode::decode() $mime_decoded because it is usually
143 # (99% of the time) a binary value like a photo and not a
144 # string.
145 #
146 # Also do not escape binary values.
147  
148 1 my $value = $conf->{data}{value};
149 1 my $mime_decoded = MIME::Base64::decode($value);
150 1 $conf->{data}{value} = $mime_decoded;
151  
152 # mimic what goes on below
153 1 @{$self}{ @{ $self->{field_order} } }
154 1 = ( $conf->{data}{value} );
155 } else {
156  
157 # the -1 on split is so ;; values create elements in
158 # the array
159 279 my @elements = split /(?<!\\);/, $conf->{data}{value}, -1;
160 279 ▶ if ( defined $self->{node_type}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'node_type'} and $self->{'node_type'} eq 'ORG') { } 4 275 279
elsif (@elements <= @{$self->{'field_order'};}) { } 274 1 275
Condition100%3/3
defined $self->{"node_type"} and $self->{"node_type"} eq "ORG"
!ll&&!rl&&r
32724
MC/DC100%2/2
defined $self->{"node_type"} and $self->{"node_type"} eq "ORG"
defined $self->{"node_type"} $self->{"node_type"} eq "ORG"
Truth table100%3/3
A and B
A= defined $self->{"node_type"}B= $self->{"node_type"} eq "ORG"
ABresult
0X0
100
111
161 && $self->{node_type} eq 'ORG' )
162 {
163 4 my @unescaped = $self->_unescape_list(@elements);
164  
165 4 $self->{'name'} = shift(@unescaped);
166 4 ▶ $self->{'unit'} = \@unescaped if scalar(@unescaped) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @unescaped > 0 3 1 4
167 }
168  
169 # no need for explicit scalar
170 275 elsif ( @elements <= @{ $self->{field_order} } ) {
171 274 my @unescaped = $self->_unescape_list(@elements);
172  
173 # set the field values as the data
174 # e.g. $self->{street} = 'The street'
175 274 @{$self}{ @{ $self->{field_order} } } = @unescaped;
176 } else {
177 carp sprintf(
178 'Data value had %d elements expecting %d or less.',
179 scalar @elements,
180 1 scalar @{ $self->{field_order} }
181 );
182 }
183 }
184 }
185 }
186 318 return $self;
187 }
188  
189 693 ▶sub _unescape {
Subroutine _unescape: called CC=1 SCAR=0.0
190 693 my ( $self, $value ) = @_;
191 693 $value =~ s|\\([\\,;])|$1|g;
192 693 return $value;
193 }
194  
195 278 ▶sub _unescape_list {
Subroutine _unescape_list: called CC=1 SCAR=0.0
196 278 my ( $self, @values ) = @_;
197 278 return map { $self->_unescape($_) } @values;
198 }
199  
200 =head2 node_type
201  
202 Returns the type of the node itself, e.g. ADR.
203  
204 =cut
205  
206 175 ▶sub node_type {
Subroutine node_type: called CC=1 SCAR=0.0
Pod documented
207 175 $_[0]->{node_type};
208 }
209  
210 =head2 unit()
211  
212 my @units = @{ $org_node->unit() };
213 $org_node->unit( [ 'Division', 'Department', 'Sub-department' ] );
214  
215 As ORG allows unlimited numbers of 'units' as well as and organisation
216 'name', this method is a specific case for accessing those values, they
217 are always returned as an array reference, and should always be set
218 as an array reference.
219  
220 =cut
221  
222 4 ▶sub unit {
Subroutine unit: called CC=4 SCAR=13.9
Pod documented
223 4 my ( $self, $val ) = @_;
224 4 ▶ $self->{'unit'} = $val if $val && ref($val) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $val and ref $val eq "ARRAY" 1 3 4
Condition100%3/3
$val and ref $val eq "ARRAY"
!ll&&!rl&&r
211
MC/DC100%2/2
$val and ref $val eq "ARRAY"
$val ref $val eq "ARRAY"
Truth table100%3/3
A and B
A= $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
225 4 ▶ return $self->{'unit'} if defined $self->{'unit'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"unit"} 3 1 4
226 1 return undef;
227 }
228  
229 =head2 types()
230  
231 my @types = $node->types();
232  
233 # or
234 my $types = $node->types();
235  
236 This method will return an array or an array ref depending
237 on the calling context of types associated with the $node,
238 undef is returned if there are no types.
239  
240 All types returned are lower case.
241  
242 =cut
243  
244 40 ▶sub types {
Subroutine types: called CC=6 SCAR=17.9
Pod documented
245 40 my $self = shift;
246 40 my @types;
247 40 ▶ return undef unless defined $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"params"} 1 39 40
248 39 foreach my $key ( sort keys %{ $self->{params} } ) {
249 56 my $value = $self->{params}->{$key};
250 56 ▶ push @types, lc $key if $value && $value eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $value and $value eq "type" 44 12 56
Condition66%2/3
$value and $value eq "type"
!ll&&!rl&&r
01244
MC/DC50%1/2
$value and $value eq "type"
$value $value eq "type"
Truth table66%2/3
A and B
A= $valueB= $value eq "type"
ABresult
0X0
100
111
251 }
252 39 ▶ return wantarray ? @types : \@types;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 38 39
253 }
254  
255 =head2 is_type()
256  
257 if ( $node->is_type($type) ) {
258  
259 # ...
260 }
261  
262 Given a type (see types() for a list of those set)
263 this method returns 1 if the $node is of that type
264 or undef if it is not.
265  
266 =cut
267  
268 3115 ▶sub is_type {
Subroutine is_type: called CC=4 SCAR=13.9
Pod documented
269 3115 my ( $self, $type ) = @_;
270 3115 ▶ if ( defined $self->{params} && exists $self->{params}->{ lc($type) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"params"} and exists $self->{"params"}{lc $type}) 317 2798 3115
Condition100%3/3
defined $self->{"params"} and exists $self->{"params"}{lc $type}
!ll&&!rl&&r
5652233317
MC/DC100%2/2
defined $self->{"params"} and exists $self->{"params"}{lc $type}
defined $self->{"params"} exists $self->{"params"}{lc $type}
Truth table100%3/3
A and B
A= defined $self->{"params"}B= exists $self->{"params"}{lc $type}
ABresult
0X0
100
111
271  
272 # Make this always return true so as not to change the net
273 # behaviour of the method. if for some wack (and
274 # non-compliant) reason this value is undef, empty string or
275 # zero, tough luck.
276 317 ▶ return $self->{params}{ lc $type } || 1;
Condition50%1/2
$self->{'params'}{lc $type} || 1
l!l
3170
MC/DC0%0/1
$self->{'params'}{lc $type} || 1
$self->{'params'}{lc $type}
Truth table50%1/2
A
A= $self->{'params'}{lc $type}
Aresult
11
00
277 }
278 2798 return undef;
279 }
280  
281 =head2 is_pref();
282  
283 if ( $node->is_pref() ) {
284 print "Preferred node";
285 }
286  
287 This method is the same as is_type (which can take a value of 'pref')
288 but it specific to if it is the preferred node. This method is used
289 to sort when returning lists of nodes.
290  
291 =cut
292  
293 # A preferred node can be indicated in a vcard file 2 ways:
294 #
295 # 1. As 'PREF=1' which makes $self->{params} look like:
296 # { 1 => 'pref', work => 'type' }
297 #
298 # 2. As 'TYPE=PREF' which makes $self->{params} look like:
299 # { pref => 'type', work => 'type' }
300 #
301 39 ▶sub is_pref {
Subroutine is_pref: called CC=5 SCAR=16.1
Pod documented
302 39 my $self = shift;
303 39 my $params = $self->{params};
304 39 ▶ if (( defined $params ) && #
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and (defined $params->{1} and $params->{1} eq "pref") or defined $params->{"pref"}) 13 26 39
Condition88%8/9
defined $params and (defined $params->{1} and $params->{1} eq "pref") or defined $params->{"pref"}
l!l&&r!l&&!r
12126
defined $params and (defined $params->{1} and $params->{1} eq "pref")
!ll&&!rl&&r
12612
defined $params->{1} and $params->{1} eq "pref"
!ll&&!rl&&r
26012
MC/DC75%3/4
defined $params and (defined $params->{1} and $params->{1} eq "pref") or defined $params->{"pref"}
defined $params defined $params->{1} $params->{1} eq "pref" defined $params->{"pref"}
Truth table57%4/7
A and B and C or D
A= defined $paramsB= defined $params->{1}C= $params->{1} eq "pref"D= defined $params->{"pref"}
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
305 ( defined $params->{1} && $params->{1} eq 'pref' ) || #
306 ( defined $params->{pref} )
307 )
308 {
309 13 return 1;
310 }
311 26 return undef;
312 }
313  
314 =head2 add_types()
315  
316 $address->add_types('home');
317  
318 my @types = qw(home work);
319 $address->add_types( \@types );
320  
321 Add a type to an address, it can take a scalar or an array ref.
322  
323 =cut
324  
325 4 ▶sub add_types {
Subroutine add_types: called CC=3 SCAR=11.0
Pod documented
326 4 my ( $self, $type ) = @_;
327 4 ▶ unless ( defined $self->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"params"}) 2 2 4
328  
329 # no params, create a hash ref in there
330 2 my %params;
331 2 $self->{params} = \%params;
332 }
333 4 ▶ if ( ref($type) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 1 3 4
334 1 map { $self->{params}->{ lc($_) } = 'type' } @{$type};
335 } else {
336 3 $self->{params}->{ lc($type) } = 'type';
337 }
338 }
339  
340 =head2 remove_types()
341  
342 $address->remove_types('home');
343  
344 my @types = qw(home work);
345 $address->remove_types( \@types );
346  
347 This method removes a type from an address, it can take a scalar
348 or an array ref.
349  
350 undef is returned when in scalar context and the type does not match,
351 or when in array ref context and none of the types match, true is
352 returned otherwise.
353  
354 =cut
355  
356 5 ▶sub remove_types {
Subroutine remove_types: called CC=6 SCAR=17.9
Pod documented
357 5 my ( $self, $type ) = @_;
358 5 ▶ return undef unless defined $self->{params};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"params"} 1 4 5
359  
360 4 ▶ if ( ref($type) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 2 2 4
361 2 my $to_return = undef;
362 2 foreach my $t ( @{$type} ) {
363 3 ▶ if ( exists $self->{params}->{ lc($t) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"params"}{lc $t}) 2 1 3
364 2 delete $self->{params}->{ lc($t) };
365 2 $to_return = 1;
366 }
367 }
368 2 return $to_return;
369 } else {
370 2 ▶ if ( exists $self->{params}->{ lc($type) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"params"}{lc $type}) 1 1 2
371 1 delete $self->{params}->{ lc($type) };
372 1 return 1;
373 }
374 }
375 1 return undef;
376 }
377  
378 =head2 group()
379  
380 my $group = $node->group();
381  
382 If called without any arguments, this method returns the group
383 name if a node belongs to a group. Otherwise undef is returned.
384  
385 If an argument is supplied then this is set as the group name.
386  
387 All group names are always lowercased.
388  
389 For example, Apple Address book used 'itemN' to group it's
390 custom X-AB... nodes with a TEL or ADR node.
391  
392 =cut
393  
394 524 ▶sub group {
Subroutine group: called CC=3 SCAR=11.0
Pod documented
395 524 my $self = shift;
396 524 ▶ if ( my $val = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = shift()) 16 508 524
397 16 $self->{group} = lc($val);
398 }
399 524 ▶ return $self->{group} if defined $self->{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"group"} 51 473 524
400 473 return undef;
401 }
402  
403 =head2 export_data()
404  
405 NOTE: This method is deprecated and should not be used. It will be removed in
406 a later version.
407  
408 my $value = $node->export_data();
409  
410 This method returns the value string of a node.
411 It is only needs to be called when exporting the information
412 back out to ensure that it has not been altered.
413  
414 =cut
415  
416 2 ▶sub export_data {
Subroutine export_data: called CC=3 SCAR=11.3
Pod documented
417 2 my $self = shift;
418 my @lines = map {
419 14 ▶ if ( defined $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$_}) { } 13 1 14
420 13 ▶ if ( ref( $self->{$_} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$_} eq 'ARRAY') { } 0 13 13
421  
422 # Handle things like org etc which have 'units'
423 0 join( ',', @{ $self->{$_} } );
424 } else {
425 13 $self->{$_};
426 }
427 } else {
428 1 '';
429 }
430 2 } @{ $self->{'field_order'} };
431  
432 # Should escape stuff here really, but waiting to see what
433 # T::vfile::asData does
434 2 return join( ';', @lines );
435  
436 }
437  
438 =head2 as_string
439  
440 Returns the node as a formatted string.
441  
442 =cut
443  
444 175 ▶sub _key_as_string {
Subroutine _key_as_string: called CC=3 SCAR=11.0
445 175 my ($self) = @_;
446  
447 175 my $n = '';
448 175 ▶ $n .= $self->group . '.' if $self->group;
Branch: 1 branch
BranchTrueFalseTotal
if $self->group 8 167 175
449 175 $n .= $self->node_type;
450 175 ▶ $n .= $self->_params if $self->_params;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_params 82 93 175
451  
452 175 return $n;
453 }
454  
455 # returns a string of params formatted for saving to a vcard file
456 # returns false if there are no params
457 257 ▶sub _params {
Subroutine _params: called CC=8 SCAR=21.6
458 257 my ($self) = @_;
459  
460 257 my %t;
461 257 for my $t ( sort keys %{ $self->{params} } ) {
462 250 my $backwards = uc $self->is_type( lc $t );
463 250 ▶ $t{$backwards} ||= [];
Condition100%2/2
$t{$backwards} ||= []
l!l
24226
MC/DC100%1/1
$t{$backwards} ||= []
$t{$backwards}
Truth table100%2/2
A
A= $t{$backwards}
Aresult
11
00
464 250 push @{ $t{$backwards} }, lc $t;
465 }
466  
467 $t{CHARSET} = [ lc $self->{encoding_out} ]
468 if $self->{encoding_out} ne 'none'
469 257 ▶ && $self->{encoding_out} !~ /UTF-8/i
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_out"} ne "none" and not $self->{"encoding_out"} =~ /UTF-8/i and not $self->is_type("b") and not $self->is_type("base64") 0 257 257
Condition44%4/9
$self->{"encoding_out"} ne "none" and not $self->{"encoding_out"} =~ /UTF-8/i and not $self->is_type("b") and not $self->is_type("base64")
!ll&&!rl&&r
25700
$self->{"encoding_out"} ne "none" and not $self->{"encoding_out"} =~ /UTF-8/i and not $self->is_type("b")
!ll&&!rl&&r
25700
$self->{"encoding_out"} ne "none" and not $self->{"encoding_out"} =~ /UTF-8/i
!ll&&!rl&&r
342230
MC/DC0%0/4
$self->{"encoding_out"} ne "none" and not $self->{"encoding_out"} =~ /UTF-8/i and not $self->is_type("b") and not $self->is_type("base64")
$self->{"encoding_out"} ne "none" not $self->{"encoding_out"} =~ /UTF-8/i not $self->is_type("b") not $self->is_type("base64")
Truth table40%2/5
A and not(B) and not(C) and not(D)
A= $self->{"encoding_out"} ne "none"B= not $self->{"encoding_out"} =~ /UTF-8/iC= not $self->is_type("b")D= not $self->is_type("base64")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
470 && !$self->is_type('b')
471 && !$self->is_type('base64');
472  
473 257 my @params = map { sprintf( '%s=%s', $_, join ',', @{ $t{$_} } ) } #
474 sort keys %t;
475  
476 257 ▶ return @params ? ';' . join( ';', @params ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 164 93 257
477 }
478  
479 # The vCard RFC requires commas, semicolons, and backslashes to be escaped.
480 # See http://tools.ietf.org/search/rfc6350#section-3.4
481 #
482 # Line breaks which are part of a value and are intended to be seen by humans
483 # must have a value of '\n'.
484 # See http://tools.ietf.org/search/rfc6350#section-4.1
485 #
486 # Line breaks which happen because the RFC requires a line break after 75
487 # characters have a value of '\r\n'. These line breaks are not handled by
488 # this method. See _newline() and
489 # http://tools.ietf.org/search/rfc6350#section-3.2
490 #
491 # Don't escape anything if this is a base64 node. Escaping only applies to
492 # strings not binary values.
493 #
494 # 'perldoc perlport' says using \r\n is wrong and confusing for a few reasons
495 # but mainly because the value of \n is different on different operating
496 # systems. It recommends \x0D\x0A instead.
497 356 ▶sub _escape {
Subroutine _escape: called CC=3 SCAR=11.0
498 356 my ( $self, $val ) = @_;
499 356 ▶ return $val if ( $self->is_type('b') or $self->is_type('base64') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_type("b") or $self->is_type("base64") 1 355 356
Condition66%2/3
$self->is_type("b") or $self->is_type("base64")
l!l&&r!l&&!r
10355
MC/DC50%1/2
$self->is_type("b") or $self->is_type("base64")
$self->is_type("b") $self->is_type("base64")
Truth table66%2/3
A or B
A= $self->is_type("b")B= $self->is_type("base64")
ABresult
1X1
011
000
500 355 $val =~ s/(\x0D\x0A|\x0D|\x0A)/\x0A/g;
501 355 $val =~ s/([,;|])/\\$1/g;
502 355 return $val;
503 }
504  
505 1 ▶sub _escape_list {
Subroutine _escape_list: called CC=1 SCAR=0.0
506 1 my ( $self, @list ) = @_;
507 1 return map { $self->_escape($_) } @list;
508 }
509  
510 # The vCard RFC says new lines must be \r\n
511 # See http://tools.ietf.org/search/rfc6350#section-3.2
512 #
513 # 'perldoc perlport' says using \r\n is wrong and confusing for a few reasons
514 # but mainly because the value of \n is different on different operating
515 # systems. It recommends \x0D\x0A instead.
516 175 ▶sub _newline {
Subroutine _newline: called CC=2 SCAR=6.9
517 175 my ($self) = @_;
518 175 ▶ return "\x0D\x0A" if $self->{encoding_out} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_out"} eq "none" 22 153 175
519 153 return Encode::encode( $self->{encoding_out}, "\x0D\x0A" );
520 }
521  
522 175 ▶sub _decode_string {
Subroutine _decode_string: called CC=2 SCAR=6.9
523 175 my ( $self, $string ) = @_;
524 175 ▶ return $string if $self->{encoding_out} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_out"} eq "none" 22 153 175
525 153 return Encode::decode( $self->{encoding_out}, $string );
526 }
527  
528 337 ▶sub _encode_string {
Subroutine _encode_string: called CC=2 SCAR=6.9
529 337 my ( $self, $string ) = @_;
530 337 ▶ return $string if $self->{encoding_out} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_out"} eq "none" 22 315 337
531 315 return Encode::encode( $self->{encoding_out}, $string );
532 }
533  
534 0 ▶sub _encode_list {
Subroutine _encode_list: not called CC=2 SCAR=17.9
535 0 my ( $self, @list ) = @_;
536 0 ▶ return @list if $self->{encoding_out} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_out"} eq "none" 0 0 0
537 0 return map { $self->_encode_string($_) } @list;
538 }
539  
540 # The vCard RFC says lines should be wrapped (or 'folded') at 75 octets
541 # excluding the line break. The line is continued on the next line with a
542 # space as the first character. See
543 # http://tools.ietf.org/search/rfc6350#section-3.1 for details.
544 #
545 # Note than an octet is 1 byte (8 bits) and is not necessarily equal to 1
546 # character, 1 grapheme, 1 codepoint, or 1 column of output. Actually none of
547 # those things are necessarily equal. See
548 # http://www.perl.com/pub/2012/05/perlunicook-string-length-in-graphemes.html
549 #
550 # MIME::QuotedPrint does line wrapping but it assumes the line length must be
551 # <= 76 chars which doesn't work for us.
552 #
553 # Can't use Unicode::LineBreak because it wraps by counting characters and the
554 # vCard spec wants us to wrap by counting octets.
555 175 ▶sub _wrap {
Subroutine _wrap: called CC=4 SCAR=13.9
556 175 my ( $self, $key, $value ) = @_;
557  
558 return $self->_wrap_naively( $key, $value )
559 175 ▶ unless $self->{encoding_out} =~ /UTF-8/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"encoding_out"} =~ /UTF-8/i 22 153 175
560  
561 153 ▶ if ( $self->is_type('q') or $self->is_type('quoted-printable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_type("q") or $self->is_type("quoted-printable")) 10 143 153
Condition66%2/3
$self->is_type("q") or $self->is_type("quoted-printable")
l!l&&r!l&&!r
010143
MC/DC50%1/2
$self->is_type("q") or $self->is_type("quoted-printable")
$self->is_type("q") $self->is_type("quoted-printable")
Truth table66%2/3
A or B
A= $self->is_type("q")B= $self->is_type("quoted-printable")
ABresult
1X1
011
000
562 ## See the Quoted-Printable RFC205
563 ## https://tools.ietf.org/html/rfc2045#section-6.7 (rule 5)
564 10 my $newline
565 = $self->_encode_string("=")
566 . $self->_newline
567 . $self->_encode_string(" ");
568 10 my $max
569 = 73; # 75 octets per line max including '=' and ' ' from $newline
570 10 return $self->_wrap_utf8( $key, $value, $max, $newline );
571 }
572  
573 143 my $newline = $self->_newline . $self->_encode_string(" ");
574 143 my $max = 74; # 75 octets per line max including " " from $newline
575 143 return $self->_wrap_utf8( $key, $value, $max, $newline );
576 }
577  
578 153 ▶sub _wrap_utf8 {
Subroutine _wrap_utf8: called CC=8 SCAR=20.8
579 153 my ( $self, $key, $value, $max, $newline ) = @_;
580  
581 153 my $gcs = Unicode::GCString->new(Encode::decode('UTF-8', $key . $value));
582 153 ▶ return $key . $value if bytes::length( $gcs->as_string ) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if &bytes::length(scalar $gcs->as_string) <= $max 142 11 153
583  
584 11 my $start = 0;
585 11 my @wrapped_lines;
586  
587 # first line is 1 character longer than the others because it doesn't
588 # begin with a " "
589 11 my $first_max = $max + 1;
590  
591 11 ▶ while ( $start <= $gcs->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $gcs->length) 47 0 47
592 47 my $len = 1;
593  
594 47 ▶ while ( ( $start + $len ) <= $gcs->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start + $len <= $gcs->length) 2840 0 2840
595  
596 2840 my $line = $gcs->substr( $start, $len );
597 2840 my $bytes = bytes::length( $line->as_string );
598  
599 # is this a good place to line wrap?
600 2840 ▶ if ( $first_max && $bytes <= $first_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_max and $bytes <= $first_max) 727 2113 2840
Condition100%3/3
$first_max and $bytes <= $first_max
!ll&&!rl&&r
210211727
MC/DC100%2/2
$first_max and $bytes <= $first_max
$first_max $bytes <= $first_max
Truth table100%3/3
A and B
A= $first_maxB= $bytes <= $first_max
ABresult
0X0
100
111
601 ## no its not a good place to line wrap
602 ## this if statement is only hit on the first line wrap
603 727 $len++;
604 727 next;
605 }
606 2113 ▶ if ( $bytes <= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes <= $max) 2066 47 2113
607 ## no its not a good place to line wrap
608 2066 $len++;
609 2066 next;
610 }
611  
612 # wrap the line here
613 47 $line = $gcs->substr( $start, $len - 1 )->as_string;
614 47 push @wrapped_lines, Encode::encode('UTF-8',$line);
615 47 $start += $len - 1;
616 47 last;
617 }
618  
619 47 ▶ if ( ( $start + $len - 1 ) >= $gcs->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start + $len - 1 >= $gcs->length) 11 36 47
620 11 my $line = $gcs->substr( $start, $len - 1 )->as_string;
621 11 push @wrapped_lines, Encode::encode('UTF-8',$line);
622 11 last;
623 }
624  
625 36 $first_max = undef;
626 }
627  
628 11 return join $newline, @wrapped_lines;
629 }
630  
631 # This will fail to line wrap properly for wide characters. The problem
632 # is it naively wraps lines by counting the number of characters but the vcard
633 # spec wants us to wrap after 75 octets (bytes). However clever vCard readers
634 # may be able to deal with this.
635 22 ▶sub _wrap_naively {
Subroutine _wrap_naively: called CC=1 SCAR=0.0
636 22 my ( $self, $key, $value ) = @_;
637  
638 22 $Text::Wrap::columns = 75; # wrap after 75 chars
639 22 $Text::Wrap::break = qr/[.]/; # allow lines breaks anywhere
640 22 $Text::Wrap::separator = $self->_newline; # use encoded new lines
641  
642 22 my $first_prefix = $key; # this text is placed before first line
643 22 my $prefix = " "; # this text is placed before all other lines
644 22 return Text::Wrap::wrap( $first_prefix, $prefix, $value );
645 }
646  
647 175 ▶sub _encode {
Subroutine _encode: called CC=5 SCAR=16.2
648 175 my ( $self, $value ) = @_;
649  
650 175 ▶ if ( $self->is_type('q') or $self->is_type('quoted-printable') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_type('q') or $self->is_type('quoted-printable')) { } 10 165 175
elsif ($self->is_type('b') or $self->is_type('base64')) { } 1 164 165
Condition66%2/3
$self->is_type("q") or $self->is_type("quoted-printable")
l!l&&r!l&&!r
010165
MC/DC50%1/2
$self->is_type("q") or $self->is_type("quoted-printable")
$self->is_type("q") $self->is_type("quoted-printable")
Truth table66%2/3
A or B
A= $self->is_type("q")B= $self->is_type("quoted-printable")
ABresult
1X1
011
000
651  
652 # Encode with Encode::encode()
653 10 my $encoded_value = $self->_encode_string($value);
654 10 return MIME::QuotedPrint::encode( $encoded_value, '' );
655  
656 ▶ } elsif ( $self->is_type('b') or $self->is_type('base64') ) {
Condition66%2/3
$self->is_type("b") or $self->is_type("base64")
l!l&&r!l&&!r
10164
MC/DC50%1/2
$self->is_type("b") or $self->is_type("base64")
$self->is_type("b") $self->is_type("base64")
Truth table66%2/3
A or B
A= $self->is_type("b")B= $self->is_type("base64")
ABresult
1X1
011
000
657  
658 # Scenarios where MIME::Base64::encode() works:
659 # - for binary data (photo) -- 99% of cases
660 # - if $value is a string with wide characters and the user has
661 # encoded it as UTF-8.
662 # - if $value is a string with no wide characters
663 #
664 # Scenario where MIME::Base64::encode() will die:
665 # - if $value is a string with wide characters and the user has not
666 # encoded it as UTF-8.
667 1 return MIME::Base64::encode( $value, '' );
668  
669 } else {
670 164 $value = $self->_encode_string($value);
671 }
672  
673 164 return $value;
674 }
675  
676 # This method does the following:
677 # 1. Escape and concatenate values
678 # 2. Encode::encode() values
679 # 3. MIME encode() values
680 # 4. wrap lines to 75 octets
681 # 5. Encode::decode() value
682 #
683 # Assumes there is only one MIME::Quoted-Printable field for a node.
684 # Assumes there is only one MIME::Base64 field for a node.
685 #
686 # If either of the above assumptions is false, line wrapping may be incorrect.
687 # However clever vCard readers may still be able to read vCards with incorrect
688 # line wrapping.
689 175 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=13.9
Pod documented
690 175 my ($self) = @_;
691 175 my $key = $self->_key_as_string();
692  
693 # Build up $raw_value from field values
694 175 my @field_values;
695 175 my $field_names = $self->{field_order};
696 175 foreach my $field_name (@$field_names) {
697 368 ▶ next unless defined( my $field_value = $self->{$field_name} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $field_value = $self->{$field_name}) 13 355 368
698  
699 # escape stuff
700 355 ▶ $field_value = ref $field_value eq 'ARRAY' #
Branch: 1 branch
BranchTrueFalseTotal
ref $field_value eq 'ARRAY' ? : 1 354 355
701 ? join( ';', $self->_escape_list(@$field_value) )
702 : $self->_escape($field_value);
703  
704 355 push @field_values, $field_value;
705 }
706 175 my $raw_value = join ';', @field_values;
707  
708 # MIME::*::encode() value
709 175 my $encoded = $self->_encode($raw_value);
710  
711 # Line wrap everything to 75 octets
712 175 my $wrapped = $self->_wrap( $key . ":", $encoded );
713  
714 # Decode everything
715 175 return $self->_decode_string($wrapped);
716 }
717  
718 # Because we have autoload
719 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
720 }
721  
722 # creates methods for a node object based on the field_names in the config
723 # hash of the node.
724  
725 318 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
726 318 my $name = $AUTOLOAD;
727 318 $name =~ s/.*://;
728  
729 carp "$name method which is not valid for this node"
730 318 ▶ unless defined $_[0]->{field_lookup}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"field_lookup"}{$name} 1 317 318
731  
732 317 ▶ if ( $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 39 278 317
733  
734 # set it
735 39 $_[0]->{$name} = $_[1];
736 }
737  
738 # Return it
739 317 return $_[0]->{$name};
740 }
741  
742 =head2 NOTES
743  
744 If a node has a param of 'quoted-printable' then the
745 value is escaped (basically converting Hex return into \r\n
746 as far as I can see).
747  
748 =head1 AUTHOR
749  
750 Leo Lapworth, LLAP@cuckoo.org
751 Eric Johnson (kablamo), github ~!at!~ iijo dot org
752  
753 =head1 SEE ALSO
754  
755 L<Text::vCard> L<Text::vCard::Addressbook>,
756 L<vCard> L<vCard>,
757 L<vCard::AddressBook> L<vCard::AddressBook>,
758  
759 =cut
760  
761 16 1;
762  
↑ Summary blib/lib/vCard.pm »