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