| 1 | package Email::Sender::Transport::Redirect::Recipients; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Email::Valid; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Types::Standard qw/ArrayRef Str/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Email::Sender::Transport::Redirect::Recipients - handle email address redirect replacements | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | This is a class used internally by | ||||||||||||||||||||||||||||
| 16 | L<Email::Sender::Transport::Redirect> and shouldn't be used directly. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $rec = Email::Sender::Transport::Redirect::Recipients->new($string_or_hashref); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | print $rec->to; | ||||||||||||||||||||||||||||
| 21 | print Dumper($rec->exclude); | ||||||||||||||||||||||||||||
| 22 | print $rec->replace('myemail@example'); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head2 BUILDARGS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 new($string_or_hashref) | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Either a single email as string, or an hashref which are used to | ||||||||||||||||||||||||||||
| 31 | initialize the accessors (see above). If a string is provided, then | ||||||||||||||||||||||||||||
| 32 | just C<to> will be set and no exclusions are set. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 ACCESSORS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 to | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | The main, required email address to use as a redirect. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head2 exclude | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | An arrayref of emails or wildcard expressions. E.g. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | [ 'mail@example.org', '*@example.org', 'racke@*' ] | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | These emails will not get redirected. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 METHODS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 replace($string) | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Main method. When a string is passed, it's checked against the | ||||||||||||||||||||||||||||
| 53 | exclusion list. If there is a match, the address passed as argument | ||||||||||||||||||||||||||||
| 54 | will be returned, otherwise the C<to> address set in the object will | ||||||||||||||||||||||||||||
| 55 | be returned. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | has to => (is => 'ro', isa => Str, required => 1); | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | has exclude => (is => 'ro', isa => ArrayRef[Str], default => sub { [] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub BUILDARGS { | ||||||||||||||||||||||||||||
| 64 | 9 | ▶ | my ($class, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 9 | ▶ | die "Only one argument is supported!" unless @args == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 8 | my $arg = shift @args; | |||||||||||||||||||||||||||
| 67 | 8 | ▶ | if (my $kind = ref($arg)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | if ($kind eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 5 | my %hash = %$arg; | |||||||||||||||||||||||||||
| 70 | 5 | foreach my $k (keys %hash) { | |||||||||||||||||||||||||||
| 71 | 11 | ▶ | die "Extra argument $k" unless $k eq 'to' || $k eq 'exclude'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k eq "to" or $k eq "exclude"
MC/DC100%2/2
$k eq "to" or $k eq "exclude"
$k eq "to"
$k eq "exclude"
Truth table100%3/3
A or B
A= $k eq "to"B= $k eq "exclude"
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 4 | return \%hash; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 1 | die "Argument must be an hashref with to and exclude keys, you passed a $kind"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 2 | return { to => $arg }; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | has excludes_regexps => (is => 'lazy', isa => ArrayRef); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub _build_excludes_regexps { | ||||||||||||||||||||||||||||
| 85 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_excludes_regexps: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 86 | 5 | my @out; | |||||||||||||||||||||||||||
| 87 | 5 | foreach my $exclusion (@{$self->exclude}) { | |||||||||||||||||||||||||||
| 88 | 6 | ▶ | if ($exclusion =~ m/\*/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 3 | my $re = $exclusion; | |||||||||||||||||||||||||||
| 90 | # http://blogs.perl.org/users/mauke/2015/08/converting-glob-patterns-to-regular-expressions.html | ||||||||||||||||||||||||||||
| 91 | 3 | $re =~ s{(\W)}{ | |||||||||||||||||||||||||||
| 92 | 9 | ▶ | $1 eq '?' ? '.' : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 93 | $1 eq '*' ? '.*' : | ||||||||||||||||||||||||||||
| 94 | '\\' . $1 | ||||||||||||||||||||||||||||
| 95 | }eg; | ||||||||||||||||||||||||||||
| 96 | 3 | push @out, qr{$re}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif (my $address = Email::Valid->address($exclusion)) { | ||||||||||||||||||||||||||||
| 99 | 3 | push @out, qr{\Q$address\E}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 0 | die "Exclusion contains an invalid string: $exclusion, nor a wildcard, nor a valid address: $exclusion"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 5 | return \@out; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub replace { | ||||||||||||||||||||||||||||
| 111 | 21 | ▶ | my ($self, $mail) = @_; | ||||||||||||||||||||||||||
|
Subroutine
replace: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 21 | ▶ | if ($mail) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 20 | ▶ | if (my @exclusions = @{$self->excludes_regexps}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | # an alternate approach could be Email::Address to allow multiple addresses | ||||||||||||||||||||||||||||
| 115 | 18 | ▶ | if (my $address = Email::Valid->address($mail)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 18 | my $real = $address . ''; # stringify | |||||||||||||||||||||||||||
| 117 | 18 | foreach my $re (@exclusions) { | |||||||||||||||||||||||||||
| 118 | # print "Checking $real against $re\n"; | ||||||||||||||||||||||||||||
| 119 | 24 | ▶ | if ($real =~ m/\A$re\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | # print "Found, returning $real\n"; | ||||||||||||||||||||||||||||
| 121 | 12 | return $real; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | # fall back | ||||||||||||||||||||||||||||
| 128 | # print "Falling back\n"; | ||||||||||||||||||||||||||||
| 129 | 9 | return $self->to; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | 1; | |||||||||||||||||||||||||||