| 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::AddressList - manage email address lists | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | It provides a class which is able to manage email address lists, | ||||||||||||||||||||
| 25 | as used in L<deb822(5)> data. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | package Dpkg::Email::AddressList 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 | 2 | ▶ | use List::Util qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 2 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 2 | ▶ | use Dpkg::Email::Address; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 2 | my $addr_regex = Dpkg::Email::Address::REGEX(); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 2 | my $addrlist_elem_regex = qr{ | |||||||||||||||||||
| 43 | \s* $addr_regex (?: \s* , )* | ||||||||||||||||||||
| 44 | }x; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | my $addrlist_regex = qr{ | |||||||||||||||||||
| 47 | ^ | ||||||||||||||||||||
| 48 | # Optional addresses separated by comma. | ||||||||||||||||||||
| 49 | (?: \s* $addr_regex (?: \s* , )+ )* | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Required address followed by an optional trailing comma. | ||||||||||||||||||||
| 52 | (?: \s* $addr_regex ) (?: \s* , )* | ||||||||||||||||||||
| 53 | \s* | ||||||||||||||||||||
| 54 | $ | ||||||||||||||||||||
| 55 | }x; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =head1 FUNCTIONS | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =over 4 | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =item $regex = PARSE_REGEX() | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Returns the regex used to parse email address lists. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | Matches on a single entire email address. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub MATCH_REGEX() | ||||||||||||||||||
|
Subroutine
MATCH_REGEX: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 70 | { | ||||||||||||||||||||
| 71 | 0 | return $addrlist_elem_regex; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =item $regex = CHECK_REGEX() | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | Returns the regex used to validate email address lists. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | sub CHECK_REGEX() | ||||||||||||||||||
|
Subroutine
CHECK_REGEX: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 81 | { | ||||||||||||||||||||
| 82 | 0 | return $addrlist_regex; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head1 METHODS | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =over 4 | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =item $addrlist = Dpkg::Email::AddressList->new($string) | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | Create a new object that can hold an email address list. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 17 | ▶ | sub new($this, $str = undef) | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||
| 98 | 17 | { | |||||||||||||||||||
| 99 | 17 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 100 | 17 | my $self = { | |||||||||||||||||||
| 101 | addrlist => [], | ||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||
| 103 | 17 | bless $self, $class; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 17 | ▶ | $self->parse($str) if defined $str; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 17 | return $self; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =item @addrlist = $addrlist->addrlist() | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Returns a list of L<Dpkg::Email::Address> objects that art part of this | ||||||||||||||||||||
| 113 | email address list object. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 17 | ▶ | sub addrlist($self) | ||||||||||||||||||
|
Subroutine
addrlist: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 118 | { | ||||||||||||||||||||
| 119 | 17 | return @{$self->{addrlist}}; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =item $bool = $addrlist->parse($string) | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Parses $string into the current object replacing the current address list. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Will die on parse errors. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 17 | ▶ | sub parse($self, $str) | ||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||
| 131 | { | ||||||||||||||||||||
| 132 | 17 | my @addrlist; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 17 | ▶ | if ($str !~ m{$addrlist_regex}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | error(g_("invalid email address list '%s'"), $str); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 17 | ▶ | while ($str =~ m{$addrlist_elem_regex}g) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 29 | my $addr = Dpkg::Email::Address->new(); | |||||||||||||||||||
| 140 | 29 | $addr->name($1); | |||||||||||||||||||
| 141 | 29 | $addr->email($2); | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 29 | push @addrlist, $addr; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 17 | ▶ | if (@addrlist == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 0 | error(g_("invalid email address list '%s'"), $str); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 17 | $self->{addrlist} = \@addrlist; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 17 | return; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =item $bool = $addrlist->contains($addr) | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Checks whether the $addr Dpkg::Email::Address is present in the email | ||||||||||||||||||||
| 158 | address list. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | sub contains($self, $addr) | ||||||||||||||||||
|
Subroutine
contains: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 163 | { | ||||||||||||||||||||
| 164 | 0 | my $addr_str = $addr->as_string(); | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 0 | ▶ | return any { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 167 | 0 | $addr_str eq $_->as_string() | |||||||||||||||||||
| 168 | 0 | } @{$self->{addrlist}}; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =item $string = $addrlist->as_string() | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Returns the email address list formatted as a string of email addresses | ||||||||||||||||||||
| 174 | separated by commas. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 17 | ▶ | sub as_string($self) | ||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 179 | { | ||||||||||||||||||||
| 180 | my $str = join q{, }, map { | ||||||||||||||||||||
| 181 | 29 | $_->as_string() | |||||||||||||||||||
| 182 | 17 | } @{$self->{addrlist}}; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 17 | return $str; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =back | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head1 CHANGES | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =head2 Version 0.xx | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | This is a private module. | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 2 | 1; | |||||||||||||||||||