blib/lib/Email/Address/XS.pm

filter: Statementstmt 94.7% 145 / 153 Branchbran 89.3% 84 / 94 Conditioncond 79.4% 31 / 39 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 88.0% 22 / 25 Podpod 83.3% 15 / 18 totaltotal 88.6% 311 / 351 CC 62 SCAR 21.6
CC
62
Cov
91%
new
19.2
address
10.1
comment
10.0
CRAP 8.6
↑ Summary dovecot-parser.c »
1 # Copyright (c) 2015-2018 by Pali <pali@cpan.org>
2  
3 package Email::Address::XS;
4  
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '1.06';
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @EXPORT_OK = qw(parse_email_addresses parse_email_groups format_email_addresses format_email_groups compose_address split_address);
15  
16 2 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 XSLoader::load(__PACKAGE__, $VERSION);
18  
19 =head1 NAME
20  
21 Email::Address::XS - Parse and format RFC 5322 email addresses and groups
22  
23 =head1 SYNOPSIS
24  
25 use Email::Address::XS;
26  
27 my $winstons_address = Email::Address::XS->new(phrase => 'Winston Smith', user => 'winston.smith', host => 'recdep.minitrue', comment => 'Records Department');
28 print $winstons_address->address();
29 # winston.smith@recdep.minitrue
30  
31 my $julias_address = Email::Address::XS->new('Julia', 'julia@ficdep.minitrue');
32 print $julias_address->format();
33 # Julia <julia@ficdep.minitrue>
34  
35 my $users_address = Email::Address::XS->parse('user <user@oceania>');
36 print $users_address->host();
37 # oceania
38  
39 my $goldsteins_address = Email::Address::XS->parse_bare_address('goldstein@brotherhood.oceania');
40 print $goldsteins_address->user();
41 # goldstein
42  
43 my @addresses = Email::Address::XS->parse('"Winston Smith" <winston.smith@recdep.minitrue> (Records Department), Julia <julia@ficdep.minitrue>');
44 # ($winstons_address, $julias_address)
45  
46  
47 use Email::Address::XS qw(format_email_addresses format_email_groups parse_email_addresses parse_email_groups);
48  
49 my $addresses_string = format_email_addresses($winstons_address, $julias_address, $users_address);
50 # "Winston Smith" <winston.smith@recdep.minitrue> (Records Department), Julia <julia@ficdep.minitrue>, user <user@oceania>
51  
52 my @addresses = map { $_->address() } parse_email_addresses($addresses_string);
53 # ('winston.smith@recdep.minitrue', 'julia@ficdep.minitrue', 'user@oceania')
54  
55 my $groups_string = format_email_groups('Brotherhood' => [ $winstons_address, $julias_address ], undef() => [ $users_address ]);
56 # Brotherhood: "Winston Smith" <winston.smith@recdep.minitrue> (Records Department), Julia <julia@ficdep.minitrue>;, user <user@oceania>
57  
58 my @groups = parse_email_groups($groups_string);
59 # ('Brotherhood' => [ $winstons_address, $julias_address ], undef() => [ $users_address ])
60  
61  
62 use Email::Address::XS qw(compose_address split_address);
63  
64 my ($user, $host) = split_address('julia(outer party)@ficdep.minitrue');
65 # ('julia', 'ficdep.minitrue')
66  
67 my $string = compose_address('charrington"@"shop', 'thought.police.oceania');
68 # "charrington\"@\"shop"@thought.police.oceania
69  
70 =head1 DESCRIPTION
71  
72 This module implements L<RFC 5322|https://tools.ietf.org/html/rfc5322>
73 parser and formatter of email addresses and groups. It parses an input
74 string from email headers which contain a list of email addresses or
75 a groups of email addresses (like From, To, Cc, Bcc, Reply-To, Sender,
76 ...). Also it can generate a string value for those headers from a
77 list of email addresses objects. Module is backward compatible with
78 L<RFC 2822|https://tools.ietf.org/html/rfc2822> and
79 L<RFC 822|https://tools.ietf.org/html/rfc822>.
80  
81 This module supports also I<Internationalized Email Headers>
82 compatible with L<RFC 6532|https://tools.ietf.org/html/rfc6532>.
83 All I<internationalized> strings should be expressed in UNICODE
84 (sequence of ordinals / code points), which is the correct Perl way
85 and not in a some encoded form (UTF-8, UTF-16, legacy 8-bit, etc...).
86 Note that all UNICODE code points above C<U+FF> are accepted.
87  
88 Parser and formatter functionality is implemented in XS and uses
89 shared code from Dovecot IMAP server.
90  
91 It is a drop-in replacement for L<the Email::Address module|Email::Address>
92 which has several security issues. E.g. issue L<CVE-2015-7686 (Algorithmic complexity vulnerability)|https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-7686>,
93 which allows remote attackers to cause denial of service, is still
94 present in L<Email::Address|Email::Address> version 1.908.
95  
96 Email::Address::XS module was created to finally fix CVE-2015-7686.
97  
98 Existing applications that use Email::Address module could be easily
99 switched to Email::Address::XS module. In most cases only changing
100 C<use Email::Address> to C<use Email::Address::XS> and replacing every
101 C<Email::Address> occurrence with C<Email::Address::XS> is sufficient.
102  
103 So unlike L<Email::Address|Email::Address>, this module does not use
104 regular expressions for parsing but instead native XS implementation
105 parses input string sequentially according to RFC 5322 grammar.
106  
107 Additionally it has support also for named groups and so can be use
108 instead of L<the Email::Address::List module|Email::Address::List>.
109  
110 If you are looking for the module which provides object representation
111 for the list of email addresses suitable for the MIME email headers,
112 see L<Email::MIME::Header::AddressList|Email::MIME::Header::AddressList>.
113  
114 =head2 EXPORT
115  
116 None by default. Exportable functions are:
117 L<C<parse_email_addresses>|/parse_email_addresses>,
118 L<C<parse_email_groups>|/parse_email_groups>,
119 L<C<format_email_addresses>|/format_email_addresses>,
120 L<C<format_email_groups>|/format_email_groups>,
121 L<C<compose_address>|/compose_address>,
122 L<C<split_address>|/split_address>.
123  
124 =head2 Exportable Functions
125  
126 =over 4
127  
128 =item format_email_addresses
129  
130 use Email::Address::XS qw(format_email_addresses);
131  
132 my $winstons_address = Email::Address::XS->new(phrase => 'Winston Smith', address => 'winston@recdep.minitrue');
133 my $julias_address = Email::Address::XS->new(phrase => 'Julia', address => 'julia@ficdep.minitrue');
134 my @addresses = ($winstons_address, $julias_address);
135 my $string = format_email_addresses(@addresses);
136 print $string;
137 # "Winston Smith" <winston@recdep.minitrue>, Julia <julia@ficdep.minitrue>
138  
139 Takes a list of email address objects and returns one formatted string
140 of those email addresses.
141  
142 =cut
143  
144 54 ▶sub format_email_addresses {
Subroutine format_email_addresses: called CC=1 SCAR=0.0
Pod documented
145 54 my (@args) = @_;
146 54 return format_email_groups(undef, \@args);
147 }
148  
149 =item format_email_groups
150  
151 use Email::Address::XS qw(format_email_groups);
152  
153 my $winstons_address = Email::Address::XS->new(phrase => 'Winston Smith', user => 'winston.smith', host => 'recdep.minitrue');
154 my $julias_address = Email::Address::XS->new('Julia', 'julia@ficdep.minitrue');
155 my $users_address = Email::Address::XS->new(address => 'user@oceania');
156  
157 my $groups_string = format_email_groups('Brotherhood' => [ $winstons_address, $julias_address ], undef() => [ $users_address ]);
158 print $groups_string;
159 # Brotherhood: "Winston Smith" <winston.smith@recdep.minitrue>, Julia <julia@ficdep.minitrue>;, user@oceania
160  
161 my $undisclosed_string = format_email_groups('undisclosed-recipients' => []);
162 print $undisclosed_string;
163 # undisclosed-recipients:;
164  
165 Like L<C<format_email_addresses>|/format_email_addresses> but this
166 method takes pairs which consist of a group display name and a
167 reference to address list. If a group is not undef then address
168 list is formatted inside named group.
169  
170 =item parse_email_addresses
171  
172 use Email::Address::XS qw(parse_email_addresses);
173  
174 my $string = '"Winston Smith" <winston.smith@recdep.minitrue>, Julia <julia@ficdep.minitrue>, user@oceania';
175 my @addresses = parse_email_addresses($string);
176 # @addresses now contains three Email::Address::XS objects, one for each address
177  
178 Parses an input string and returns a list of Email::Address::XS
179 objects. Optional second string argument specifies class name for
180 blessing new objects.
181  
182 =cut
183  
184 38 ▶sub parse_email_addresses {
Subroutine parse_email_addresses: called CC=1 SCAR=0.0
Pod documented
185 38 my (@args) = @_;
186 38 my $t = 1;
187 38 return map { @{$_} } grep { $t ^= 1 } parse_email_groups(@args);
188 }
189  
190 =item parse_email_groups
191  
192 use Email::Address::XS qw(parse_email_groups);
193  
194 my $string = 'Brotherhood: "Winston Smith" <winston.smith@recdep.minitrue>, Julia <julia@ficdep.minitrue>;, user@oceania, undisclosed-recipients:;';
195 my @groups = parse_email_groups($string);
196 # @groups now contains list ('Brotherhood' => [ $winstons_object, $julias_object ], undef() => [ $users_object ], 'undisclosed-recipients' => [])
197  
198 Like L<C<parse_email_addresses>|/parse_email_addresses> but this
199 function returns a list of pairs: a group display name and a
200 reference to a list of addresses which belongs to that named group.
201 An undef value for a group means that a following list of addresses
202 is not inside any named group. An output is in a same format as an
203 input for the function L<C<format_email_groups>|/format_email_groups>.
204 This function preserves order of groups and does not do any
205 de-duplication or merging.
206  
207 =item compose_address
208  
209 use Email::Address::XS qw(compose_address);
210 my $string_address = compose_address($user, $host);
211  
212 Takes an unescaped user part and unescaped host part of an address
213 and returns escaped address.
214  
215 Available since version 1.01.
216  
217 =item split_address
218  
219 use Email::Address::XS qw(split_address);
220 my ($user, $host) = split_address($string_address);
221  
222 Takes an escaped address and split it into pair of unescaped user
223 part and unescaped host part of address. If splitting input address
224 into these two parts is not possible then this function returns
225 pair of undefs.
226  
227 Available since version 1.01.
228  
229 =back
230  
231 =head2 Class Methods
232  
233 =over 4
234  
235 =item new
236  
237 my $empty_address = Email::Address::XS->new();
238 my $winstons_address = Email::Address::XS->new(phrase => 'Winston Smith', user => 'winston.smith', host => 'recdep.minitrue', comment => 'Records Department');
239 my $julias_address = Email::Address::XS->new('Julia', 'julia@ficdep.minitrue');
240 my $users_address = Email::Address::XS->new(address => 'user@oceania');
241 my $only_name = Email::Address::XS->new(phrase => 'Name');
242 my $copy_of_winstons_address = Email::Address::XS->new(copy => $winstons_address);
243  
244 Constructs and returns a new C<Email::Address::XS> object. Takes named
245 list of arguments: phrase, address, user, host, comment and copy.
246 An argument address takes precedence over user and host.
247  
248 When an argument copy is specified then it is expected an
249 Email::Address::XS object and a cloned copy of that object is
250 returned. All other parameters are ignored.
251  
252 Old syntax L<from the Email::Address module|Email::Address/new> is
253 supported too. Takes one to four positional arguments: phrase, address
254 comment, and original string. Passing an argument original is
255 deprecated, ignored and throws a warning.
256  
257 =cut
258  
259 158 ▶sub new {
Subroutine new: called CC=19 SCAR=29.5
Pod documented
260 158 my ($class, @args) = @_;
261  
262 158 my %hash_keys = (phrase => 1, address => 1, user => 1, host => 1, comment => 1, copy => 1);
263 158 my $is_hash;
264 158 ▶ if ( scalar @args == 2 and defined $args[0] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @args == 2 and defined $args[0]) { } 24 134 158
elsif (scalar @args == 4 and defined $args[0] and defined $args[2]) { } 68 66 134
elsif (scalar @args > 4) { } 29 37 66
Condition100%3/3
scalar @args == 2 and defined $args[0]
!ll&&!rl&&r
133124
MC/DC100%2/2
scalar @args == 2 and defined $args[0]
scalar @args == 2 defined $args[0]
Truth table100%3/3
A and B
A= scalar @args == 2B= defined $args[0]
ABresult
0X0
100
111
265 24 ▶ $is_hash = 1 if exists $hash_keys{$args[0]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash_keys{$args[0]} 21 3 24
266 ▶ } elsif ( scalar @args == 4 and defined $args[0] and defined $args[2] ) {
Condition66%4/6
scalar @args == 4 and defined $args[0] and defined $args[2]
!ll&&!rl&&r
66068
scalar @args == 4 and defined $args[0]
!ll&&!rl&&r
66068
MC/DC33%1/3
scalar @args == 4 and defined $args[0] and defined $args[2]
scalar @args == 4 defined $args[0] defined $args[2]
Truth table50%2/4
A and B and C
A= scalar @args == 4B= defined $args[0]C= defined $args[2]
ABCresult
0XX0
10X0
1100
1111
267 68 ▶ $is_hash = 1 if exists $hash_keys{$args[0]} and exists $hash_keys{$args[2]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash_keys{$args[0]} and exists $hash_keys{$args[2]} 67 1 68
Condition66%2/3
exists $hash_keys{$args[0]} and exists $hash_keys{$args[2]}
!ll&&!rl&&r
1067
MC/DC50%1/2
exists $hash_keys{$args[0]} and exists $hash_keys{$args[2]}
exists $hash_keys{$args[0]} exists $hash_keys{$args[2]}
Truth table66%2/3
A and B
A= exists $hash_keys{$args[0]}B= exists $hash_keys{$args[2]}
ABresult
0X0
100
111
268 } elsif ( scalar @args > 4 ) {
269 29 $is_hash = 1;
270 }
271  
272 158 my %args;
273 158 ▶ if ( $is_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hash) { } 117 41 158
274 117 %args = @args;
275 } else {
276 41 ▶ carp 'Argument original is deprecated and ignored' if scalar @args > 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args > 3 1 40 41
277 41 ▶ $args{comment} = $args[2] if scalar @args > 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args > 2 2 39 41
278 41 ▶ $args{address} = $args[1] if scalar @args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args > 1 6 35 41
279 41 ▶ $args{phrase} = $args[0] if scalar @args > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args > 0 7 34 41
280 }
281  
282 158 my $invalid;
283 my $original;
284 158 ▶ if ( exists $args{copy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"copy"}) 2 156 158
285 2 ▶ if ( $class->is_obj($args{copy}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_obj($args{'copy'})) { } 2 0 2
286 2 $args{phrase} = $args{copy}->phrase();
287 2 $args{comment} = $args{copy}->comment();
288 2 $args{user} = $args{copy}->user();
289 2 $args{host} = $args{copy}->host();
290 2 $invalid = $args{copy}->{invalid};
291 2 $original = $args{copy}->{original};
292 2 delete $args{address};
293 } else {
294 0 carp 'Named argument copy does not contain a valid object';
295 }
296 }
297  
298 158 my $self = bless {}, $class;
299  
300 158 $self->phrase($args{phrase});
301 158 $self->comment($args{comment});
302  
303 158 ▶ if ( exists $args{address} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'address'}) { } 75 83 158
304 75 $self->address($args{address});
305 } else {
306 83 $self->user($args{user});
307 83 $self->host($args{host});
308 }
309  
310 158 ▶ $self->{invalid} = 1 if $invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $invalid 0 158 158
311 158 $self->{original} = $original;
312  
313 158 return $self;
314 }
315  
316 =item parse
317  
318 my $winstons_address = Email::Address::XS->parse('"Winston Smith" <winston.smith@recdep.minitrue> (Records Department)');
319 my @users_addresses = Email::Address::XS->parse('user1@oceania, user2@oceania');
320  
321 Parses an input string and returns a list of an Email::Address::XS
322 objects. Same as the function L<C<parse_email_addresses>|/parse_email_addresses>
323 but this one is class method.
324  
325 In scalar context this function returns just first parsed object.
326 If more then one object was parsed then L<C<is_valid>|/is_valid>
327 method on returned object returns false. If no object was parsed
328 then empty Email::Address::XS object is returned.
329  
330 Prior to version 1.01 return value in scalar context is undef when
331 no object was parsed.
332  
333 =cut
334  
335 13 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
336 13 my ($class, $string) = @_;
337 13 my @addresses = parse_email_addresses($string, $class);
338 13 ▶ return @addresses if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 5 8 13
339 8 ▶ my $self = @addresses ? $addresses[0] : Email::Address::XS->new();
Branch: 1 branch
BranchTrueFalseTotal
@addresses ? : 7 1 8
340 8 ▶ $self->{invalid} = 1 if scalar @addresses != 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @addresses != 1 2 6 8
341 8 ▶ $self->{original} = $string unless defined $self->{original};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"original"} 1 7 8
342 8 return $self;
343 }
344  
345 =item parse_bare_address
346  
347 my $winstons_address = Email::Address::XS->parse_bare_address('winston.smith@recdep.minitrue');
348  
349 Parses an input string as one bare email address (addr spec) which
350 does not allow phrase part or angle brackets around email address and
351 returns an Email::Address::XS object. It is just a wrapper around
352 L<C<address>|/address> method. Method L<C<is_valid>|/is_valid> can be
353 used to check if parsing was successful.
354  
355 Available since version 1.01.
356  
357 =cut
358  
359 17 ▶sub parse_bare_address {
Subroutine parse_bare_address: called CC=2 SCAR=6.9
Pod documented
360 17 my ($class, $string) = @_;
361 17 my $self = $class->new();
362 17 ▶ if ( defined $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) { } 15 2 17
363 15 $self->address($string);
364 15 $self->{original} = $string;
365 } else {
366 2 carp 'Use of uninitialized value for string';
367 }
368 17 return $self;
369 }
370  
371 =back
372  
373 =head2 Object Methods
374  
375 =over 4
376  
377 =item format
378  
379 my $string = $address->format();
380  
381 Returns formatted Email::Address::XS object as a string. This method
382 throws a warning when L<C<user>|/user> or L<C<host>|/host> part of
383 the email address is invalid or empty string.
384  
385 =cut
386  
387 40 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod documented
388 40 my ($self) = @_;
389 40 return format_email_addresses($self);
390 }
391  
392 =item is_valid
393  
394 my $is_valid = $address->is_valid();
395  
396 Returns true if the parse function or method which created this
397 Email::Address::XS object had not received any syntax error on input
398 string and also that L<C<user>|/user> and L<C<host>|/host> part of
399 the email address are not empty strings.
400  
401 Thus this function can be used for checking if Email::Address::XS
402 object is valid before calling L<C<format>|/format> method on it.
403  
404 Available since version 1.01.
405  
406 =cut
407  
408 46 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=13.9
Pod documented
409 46 my ($self) = @_;
410 46 my $user = $self->user();
411 46 my $host = $self->host();
412 46 ▶ return (defined $user and defined $host and length $host and not $self->{invalid});
Condition88%8/9
defined $user && defined $host && length $host && !$self->{'invalid'}
!ll&&!rl&&r
15427
defined $user and defined $host and length $host
!ll&&!rl&&r
15031
defined $user and defined $host
!ll&&!rl&&r
14131
MC/DC75%3/4
defined $user && defined $host && length $host && !$self->{'invalid'}
defined $user defined $host length $host !$self->{'invalid'}
Truth table80%4/5
A and B and C && not(D)
A= defined $userB= defined $hostC= length $hostD= !$self->{'invalid'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
413 }
414  
415 =item phrase
416  
417 my $phrase = $address->phrase();
418 $address->phrase('Winston Smith');
419  
420 Accessor and mutator for the phrase (display name).
421  
422 =cut
423  
424 250 ▶sub phrase {
Subroutine phrase: called CC=3 SCAR=11.0
Pod documented
425 250 my ($self, @args) = @_;
426 250 ▶ return $self->{phrase} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 77 173 250
427 173 ▶ delete $self->{invalid} if exists $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"invalid"} 0 173 173
428 173 return $self->{phrase} = $args[0];
429 }
430  
431 =item user
432  
433 my $user = $address->user();
434 $address->user('winston.smith');
435  
436 Accessor and mutator for the unescaped user (local/mailbox) part of
437 an address.
438  
439 =cut
440  
441 222 ▶sub user {
Subroutine user: called CC=4 SCAR=13.9
Pod documented
442 222 my ($self, @args) = @_;
443 222 ▶ return $self->{user} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 128 94 222
444 94 ▶ delete $self->{cached_address} if exists $self->{cached_address};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cached_address"} 5 89 94
445 94 ▶ delete $self->{invalid} if exists $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"invalid"} 0 94 94
446 94 return $self->{user} = $args[0];
447 }
448  
449 =item host
450  
451 my $host = $address->host();
452 $address->host('recdep.minitrue');
453  
454 Accessor and mutator for the unescaped host (domain) part of an address.
455  
456 Since version 1.03 this method checks if setting a new value is syntactically
457 valid. If not undef is set and returned.
458  
459 =cut
460  
461 204 ▶sub host {
Subroutine host: called CC=6 SCAR=17.9
Pod documented
462 204 my ($self, @args) = @_;
463 204 ▶ return $self->{host} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 114 90 204
464 90 ▶ delete $self->{cached_address} if exists $self->{cached_address};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cached_address"} 1 89 90
465 90 ▶ delete $self->{invalid} if exists $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"invalid"} 0 90 90
466 90 ▶ if (defined $args[0] and $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;@\\,"]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[0] and $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;\@\\,"]+)$/) { } 43 47 90
Condition100%3/3
defined $args[0] and $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;\@\\,"]+)$/
!ll&&!rl&&r
44343
MC/DC100%2/2
defined $args[0] and $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;\@\\,"]+)$/
defined $args[0] $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;\@\\,"]+)$/
Truth table100%3/3
A and B
A= defined $args[0]B= $args[0] =~ /^(?:\[.*\]|[^\x00-\x20\x7F()<>\[\]:;\@\\,"]+)$/
ABresult
0X0
100
111
467 43 return $self->{host} = $args[0];
468 } else {
469 47 return $self->{host} = undef;
470 }
471 }
472  
473 =item address
474  
475 my $string_address = $address->address();
476 $address->address('winston.smith@recdep.minitrue');
477  
478 Accessor and mutator for the escaped address (addr spec).
479  
480 Internally this module stores a user and a host part of an address
481 separately. Function L<C<compose_address>|/compose_address> is used
482 for composing full address and function L<C<split_address>|/split_address>
483 for splitting into a user and a host parts. If splitting new address
484 into these two parts is not possible then this method returns undef
485 and sets both parts to undef.
486  
487 =cut
488  
489 163 ▶sub address {
Subroutine address: called CC=10 SCAR=23.1
Pod documented
490 163 my ($self, @args) = @_;
491 163 my $user;
492 my $host;
493 163 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 105 58 163
494 105 ▶ delete $self->{invalid} if exists $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"invalid"} 0 105 105
495 105 ▶ ($user, $host) = split_address($args[0]) if defined $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $args[0] 100 5 105
496 105 ▶ if ( not defined $user or not defined $host ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user and defined $host) 13 92 105
Condition66%2/3
defined $user and defined $host
!ll&&!rl&&r
13092
MC/DC50%1/2
defined $user and defined $host
defined $user defined $host
Truth table66%2/3
A and B
A= defined $userB= defined $host
ABresult
0X0
100
111
497 13 $user = undef;
498 13 $host = undef;
499 }
500 105 $self->{user} = $user;
501 105 $self->{host} = $host;
502 } else {
503 58 ▶ return $self->{cached_address} if exists $self->{cached_address};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cached_address"} 34 24 58
504 24 $user = $self->user();
505 24 $host = $self->host();
506 }
507 129 ▶ if ( defined $user and defined $host and length $host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and defined $host and length $host) { } 109 20 129
Condition83%5/6
defined $user and defined $host and length $host
!ll&&!rl&&r
200109
defined $user and defined $host
!ll&&!rl&&r
191109
MC/DC66%2/3
defined $user and defined $host and length $host
defined $user defined $host length $host
Truth table75%3/4
A and B and C
A= defined $userB= defined $hostC= length $host
ABCresult
0XX0
10X0
1100
1111
508 109 return $self->{cached_address} = compose_address($user, $host);
509 } else {
510 20 return $self->{cached_address} = undef;
511 }
512 }
513  
514 =item comment
515  
516 my $comment = $address->comment();
517 $address->comment('Records Department');
518  
519 Accessor and mutator for the comment which is formatted after an
520 address. A comment can contain another nested comments in round
521 brackets. When setting new comment this method check if brackets are
522 balanced. If not undef is set and returned.
523  
524 =cut
525  
526 243 ▶sub comment {
Subroutine comment: called CC=10 SCAR=23.0
Pod documented
527 243 my ($self, @args) = @_;
528 243 ▶ return $self->{comment} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 63 180 243
529 180 ▶ delete $self->{invalid} if exists $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"invalid"} 0 180 180
530 180 ▶ return $self->{comment} = undef unless defined $args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args[0] 145 35 180
531 35 my $count = 0;
532 35 my $cleaned = $args[0];
533 35 $cleaned =~ s/(?:\\.|[^\(\)\x00])//g;
534 35 foreach ( split //, $cleaned ) {
535 34 ▶ $count++ if $_ eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "(" 17 17 34
536 34 ▶ $count-- if $_ eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq ")" 16 18 34
537 34 ▶ $count = -1 if $_ eq "\x00";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "\0" 1 33 34
538 34 ▶ last if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 3 31 34
539 }
540 35 ▶ return $self->{comment} = undef if $count != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count != 0 6 29 35
541 29 return $self->{comment} = $args[0];
542 }
543  
544 =item name
545  
546 my $name = $address->name();
547  
548 This method tries to return a name which belongs to the address. It
549 returns either L<C<phrase>|/phrase> or L<C<comment>|/comment> or
550 L<C<user>|/user> part of the address or empty string (first defined
551 value in this order). But it never returns undef.
552  
553 =cut
554  
555 38 ▶sub name {
Subroutine name: called CC=6 SCAR=18.0
Pod documented
556 38 my ($self) = @_;
557 38 my $phrase = $self->phrase();
558 38 ▶ return $phrase if defined $phrase and length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $phrase and length $phrase 22 16 38
Condition66%2/3
defined $phrase and length $phrase
!ll&&!rl&&r
16022
MC/DC50%1/2
defined $phrase and length $phrase
defined $phrase length $phrase
Truth table66%2/3
A and B
A= defined $phraseB= length $phrase
ABresult
0X0
100
111
559 16 my $comment = $self->comment();
560 16 ▶ return $comment if defined $comment and length $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment and length $comment 2 14 16
Condition66%2/3
defined $comment and length $comment
!ll&&!rl&&r
1402
MC/DC50%1/2
defined $comment and length $comment
defined $comment length $comment
Truth table66%2/3
A and B
A= defined $commentB= length $comment
ABresult
0X0
100
111
561 14 my $user = $self->user();
562 14 ▶ return $user if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 10 4 14
563 4 return '';
564 }
565  
566 =item as_string
567  
568 my $address = Email::Address::XS->new(phrase => 'Winston Smith', address => 'winston.smith@recdep.minitrue');
569 my $stringified = $address->as_string();
570  
571 This method is used for object L<stringification|/stringify>. It
572 returns string representation of object. By default object is
573 stringified to L<C<format>|/format>.
574  
575 Available since version 1.01.
576  
577 =cut
578  
579 2 our $STRINGIFY; # deprecated
580  
581 5 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=18.1
Pod documented
582 5 my ($self) = @_;
583 5 ▶ return $self->format() unless defined $STRINGIFY;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Email::Address::XS::STRINGIFY 5 0 5
584 0 carp 'Variable $Email::Address::XS::STRINGIFY is deprecated; subclass instead';
585 0 my $method = $self->can($STRINGIFY);
586 0 ▶ croak 'Stringify method ' . $STRINGIFY . ' does not exist' unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
587 0 return $method->($self);
588 }
589  
590 =item original
591  
592 my $address = Email::Address::XS->parse('(Winston) "Smith" <winston.smith@recdep.minitrue> (Minitrue)');
593 my $original = $address->original();
594 # (Winston) "Smith" <winston.smith@recdep.minitrue> (Minitrue)
595 my $format = $address->format();
596 # Smith <winston.smith@recdep.minitrue> (Minitrue)
597  
598 This method returns original part of the string which was used for
599 parsing current Email::Address::XS object. If object was not created
600 by parsing input string, then this method returns undef.
601  
602 Note that L<C<format>|/format> method does not have to return same
603 original string.
604  
605 Available since version 1.01.
606  
607 =cut
608  
609 29 ▶sub original {
Subroutine original: called CC=1 SCAR=0.0
Pod documented
610 29 my ($self) = @_;
611 29 return $self->{original};
612 }
613  
614 =back
615  
616 =head2 Overloaded Operators
617  
618 =over 4
619  
620 =item stringify
621  
622 my $address = Email::Address::XS->new(phrase => 'Winston Smith', address => 'winston.smith@recdep.minitrue');
623 print "Winston's address is $address.";
624 # Winston's address is "Winston Smith" <winston.smith@recdep.minitrue>.
625  
626 Stringification is done by method L<C<as_string>|/as_string>.
627  
628 =cut
629  
630 2 ▶use overload '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
631  
632 =back
633  
634 =head2 Deprecated Functions and Variables
635  
636 For compatibility with L<the Email::Address module|Email::Address>
637 there are defined some deprecated functions and variables.
638 Do not use them in new code. Their usage throws warnings.
639  
640 Altering deprecated variable C<$Email::Address::XS::STRINGIFY> changes
641 method which is called for objects stringification.
642  
643 Deprecated cache functions C<purge_cache>, C<disable_cache> and
644 C<enable_cache> are noop and do nothing.
645  
646 =cut
647  
648 0 ▶sub purge_cache {
Subroutine purge_cache: not called CC=1 SCAR=6.9
Pod undocumented
649 0 carp 'Function purge_cache is deprecated and does nothing';
650 }
651  
652 0 ▶sub disable_cache {
Subroutine disable_cache: not called CC=1 SCAR=6.9
Pod undocumented
653 0 carp 'Function disable_cache is deprecated and does nothing';
654 }
655  
656 0 ▶sub enable_cache {
Subroutine enable_cache: not called CC=1 SCAR=6.9
Pod undocumented
657 0 carp 'Function enable_cache is deprecated and does nothing';
658 }
659  
660 =head1 SEE ALSO
661  
662 L<RFC 822|https://tools.ietf.org/html/rfc822>,
663 L<RFC 2822|https://tools.ietf.org/html/rfc2822>,
664 L<RFC 5322|https://tools.ietf.org/html/rfc5322>,
665 L<Email::MIME::Header::AddressList>,
666 L<Email::Address>,
667 L<Email::Address::List>,
668 L<Email::AddressParser>
669  
670 =head1 AUTHOR
671  
672 Pali E<lt>pali@cpan.orgE<gt>
673  
674 =head1 COPYRIGHT AND LICENSE
675  
676 Copyright (C) 2015-2018 by Pali E<lt>pali@cpan.orgE<gt>
677  
678 This library is free software; you can redistribute it and/or modify
679 it under the same terms as Perl itself, either Perl version 5.6.0 or,
680 at your option, any later version of Perl 5 you may have available.
681  
682 Dovecot parser is licensed under The MIT License and copyrighted by
683 Dovecot authors.
684  
685 =cut
686  
687 2 1;
↑ Summary dovecot-parser.c »