Dpkg/Email/Address.pm

filter: Statementstmt 95.1% 39 / 41 Branchbran 80.0% 8 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 89.4% 68 / 76 CC 10 SCAR 8.9
CC
10
Cov
89%
new
4.1
parse
3.4
name
3.0
CRAP 2.4
« Dpkg/Email/AddressList.pm ↑ Summary Dpkg/BuildTypes.pm »
1 # Copyright © 2025 Guillem Jover <guillem@debian.org>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Dpkg::Email::Address - manage an email address
21  
22 =head1 DESCRIPTION
23  
24 It provides a class which is able to manage an email address,
25 as used in L<deb822(5)> data.
26  
27 =cut
28  
29 package Dpkg::Email::Address 0.01;
30  
31 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 use overload
39 2 ▶ '""' => sub { return $_[0]->as_string(); };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 2 my $addr_name_regex = qr{
42 # Composed of possibly multiple quoted and not-quoted parts.
43 (?:
44 # Either non-quoted name, without comma.
45 [^<>"[:cntrl:],]+
46 |
47 # Or quoted name, with optional comma.
48 " [^<>"[:cntrl:]]+ "
49 )*
50 }xa;
51  
52 2 my $addr_email_regex = qr{
53 # Local part.
54 [^@<>"\s[:cntrl:],]+
55 # Separator.
56 @
57 # Domain part.
58 [^@<>"\s[:cntrl:],]+
59 }xa;
60  
61 2 my $addr_regex = qr{
62 \s*
63 # Address name.
64 (
65 $addr_name_regex
66 )
67 # Separated by at least one space.
68 \s+
69 # Address email in angle brackets.
70 <
71 (
72 $addr_email_regex
73 )
74 >
75 \s*
76 }x;
77  
78 =head1 FUNCTIONS
79  
80 =over 4
81  
82 =item $regex = REGEX()
83  
84 Returns the regex used to parse and validate an email address.
85  
86 It matches two parts, the name and the email.
87  
88 =cut
89  
90 2 ▶sub REGEX()
Subroutine REGEX: called CC=1 SCAR=0.0
Pod documented
91 {
92 2 return $addr_regex;
93 }
94  
95 =back
96  
97 =head1 METHODS
98  
99 =over 4
100  
101 =item $addr = Dpkg::Email::Address->new($string)
102  
103 Create a new object that can hold an email address.
104  
105 =cut
106  
107 33 ▶sub new($this, $str = undef)
Subroutine new: called CC=4 SCAR=14.1
Pod documented
108 33 {
109 33 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0330
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
110 33 my $self = {};
111 33 bless $self, $class;
112  
113 33 ▶ $self->parse($str) if defined $str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 3 30 33
114  
115 33 return $self;
116 }
117  
118 =item $name = $addr->name([$name])
119  
120 Get and optionally set the email address name.
121  
122 Returns the current or new email name.
123  
124 =cut
125  
126 33 ▶sub name($self, $name = undef)
Subroutine name: called CC=3 SCAR=11.0
Pod documented
127 33 {
128 33 ▶ $self->{name} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 29 4 33
129 33 return $self->{name};
130 }
131  
132 =item $email = $addr->email([$email])
133  
134 Get and optionally set the email address email.
135  
136 Returns the current or new email.
137  
138 =cut
139  
140 33 ▶sub email($self, $email = undef)
Subroutine email: called CC=3 SCAR=11.0
Pod documented
141 33 {
142 33 ▶ $self->{email} = $email if defined $email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email 29 4 33
143 33 return $self->{email};
144 }
145  
146 =item $bool = $addr->parse($string)
147  
148 Parses $string into the current object replacing the current address.
149  
150 Will die on parse errors.
151  
152 =cut
153  
154 7 ▶sub parse($self, $str)
Subroutine parse: called CC=3 SCAR=12.3
Pod documented
155 {
156 7 ▶ if ($str =~ m{^$addr_regex$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$addr_regex$/u) 7 0 7
157 7 $self->{name} = $1;
158 7 $self->{email} = $2;
159  
160 7 ▶ if ($self->{email} !~ m{@.*\.}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"email"} =~ /\@.*\./u) 0 7 7
161 0 warning(g_("email address '%s' with single label domain"), $str);
162 }
163  
164 7 return;
165 }
166  
167 0 error(g_("invalid email address '%s'"), $str);
168 }
169  
170 =item $string = $addr->as_string()
171  
172 Returns the email address formatted as a string.
173  
174 =cut
175  
176 39 ▶sub as_string($self)
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
177 {
178 39 return "$self->{name} <$self->{email}>";
179 }
180  
181 =item $hashref = $addr->as_struct()
182  
183 Returns the email address in a hashref with name and email keys.
184  
185 =cut
186  
187 29 ▶sub as_struct($self)
Subroutine as_struct: called CC=1 SCAR=0.0
Pod documented
188 {
189 return {
190 name => $self->{name},
191 email => $self->{email},
192 29 };
193 }
194  
195 =back
196  
197 =head1 CHANGES
198  
199 =head2 Version 0.xx
200  
201 This is a private module.
202  
203 =cut
204  
205 2 1;
« Dpkg/Email/AddressList.pm ↑ Summary Dpkg/BuildTypes.pm »