Mail/Qmail/Filter/Util.pm

filter: Statementstmt 100% 29 / 29 Branchbran 85.7% 12 / 14 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 90.0% 54 / 60 SCAR 24.0
CC
11
Cov
92%
addresses_to_hash
7.2
match_address
4.0
split_address
2.0
CRAP 11.1
« Mail/Qmail/Filter/DMARC.pm ↑ Summary Mail/Qmail/Filter/Dump.pm »
1 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Mail::Qmail::Filter::Util;
5  
6 2 our $VERSION = '1.1';
7  
8 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our @EXPORT_OK = qw(addresses_to_hash match_address split_address);
11  
12 sub addresses_to_hash {
13 2 ▶ my $addrs = shift;
Subroutine addresses_to_hash: called CC=7 SCAR=19.7
Pod documented
14 2 ▶ $addrs = [$addrs] unless ref $addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $addrs 1 1 2
15 2 my %struct;
16 2 ▶ for ( ref $addrs ? @$addrs : $addrs ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $addrs ? : 2 0 2
17 4 my ( $localpart, $domain ) = split_address($_);
18 4 ▶ unless ( length $localpart ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $localpart) { } 3 1 4
19 1 $struct{$domain} = ''; # match for whole domain
20 }
21 else {
22 3 ▶ my $slot = $struct{$domain} //= {};
Condition50%1/2
$struct{$domain} //= {}
l!l
03
MC/DC0%0/1
$struct{$domain} //= {}
$struct{$domain}
Truth table50%1/2
A
A= $struct{$domain}
Aresult
11
00
23 3 ▶ $slot->{$localpart} = '' if ref $slot;
Branch: 1 branch
BranchTrueFalseTotal
if ref $slot 3 0 3
24 }
25 }
26 2 \%struct;
27 }
28  
29 sub match_address {
30 4 ▶ my ( $struct, $addr ) = @_;
Subroutine match_address: called CC=4 SCAR=13.9
Pod documented
31 4 my ( $localpart, $domain ) = split_address($addr);
32 4 ▶ defined( my $slot = $struct->{$domain} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $slot = $struct->{$domain}) or return 1 3 4
33 3 ▶ !ref $slot || !length $localpart || defined $slot->{$localpart};
Branch: 1 branch
BranchTrueFalseTotal
not ref $slot && length $localpart or defined $slot->{$localpart} 2 1 3
Condition66%2/3
ref $slot && length $localpart
!ll&&!rl&&r
102
MC/DC50%1/2
ref $slot && length $localpart
ref $slot length $localpart
Truth table66%2/3
A && B
A= ref $slotB= length $localpart
ABresult
0X0
100
111
34 }
35  
36 sub split_address {
37 8 ▶ my $lc_addr = lc shift;
Subroutine split_address: called CC=2 SCAR=6.9
Pod documented
38 8 ▶ if ( $lc_addr =~ /\@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_addr =~ /\@/u) { } 7 1 8
39 7 split /\@/, $lc_addr, 2;
40 }
41 else {
42 1 undef, $lc_addr;
43 }
44 }
45  
46 2 1;
47  
48 =head1 NAME
49  
50 Mail::Qmail::Filter::Util -
51 utility functions for Mail::Qmail::Filter modules
52  
53 =head1 SYNOPSIS
54  
55 use Mail::Qmail::Filter::Util qw(addresses_to_hash match_address);
56 use Mo qw(coerce default);
57  
58 has addresses => coerce => \&addresses_to_hash;
59  
60 sub filter {
61 ...
62 if ( match_address( $self->addresses, $address ) ) {
63 ...
64 }
65 ...
66 }
67  
68 =head1 DESCRIPTION
69  
70 This module is not a filter itself, but provides utility functions
71 for other filters, possibly your own.
72  
73 =head1 EXPORTABLE FUNCTIONS
74  
75 =head2 addresses_to_hash
76  
77 Takes a single e-mail address or domain name as string or an array of such
78 strings and turns it into a data structure you can later pass to
79 L</match_address>.
80 Returns a reference to this data structure.
81  
82 =head2 match_address
83  
84 Expects two arguments:
85  
86 =over 4
87  
88 =item 1.
89  
90 the reference returned by L</addresses_to_hash>
91  
92 =item 2.
93  
94 an e-mail address (as a string)
95  
96 =back
97  
98 Will return a true value if the e-mail address given is one of the
99 addresses you had given to L</addresses_to_hash> or if its domain name
100 is one of the domain names you had given to L</addresses_to_hash>.
101  
102 Everything will be compared case-insensitively, because domain names are
103 not case-sensitive anyway, and presumably no-one uses case-sensitive
104 localparts.
105  
106 =head2 split_address
107  
108 Expects a domain name or an e-mail address as its only argument.
109  
110 Returns two values:
111  
112 =over 4
113  
114 =item 1.
115  
116 the local-part of the e-mail address, or L<undef|perlfunc/undef> for
117 domains
118  
119 =item 2.
120  
121 the domain part, converted to lowercase
122  
123 =back
124  
125 =head1 SEE ALSO
126  
127 L<Mail::Qmail::Filter>
128  
129 =head1 LICENSE AND COPYRIGHT
130  
131 Copyright 2019 Martin Sluka.
132  
133 This module is free software; you can redistribute it and/or modify it
134 under the terms of the the Artistic License (2.0). You may obtain a
135 copy of the full license at:
136  
137 L<http://www.perlfoundation.org/artistic_license_2_0>
138  
139 Any use, modification, and distribution of the Standard or Modified
140 Versions is governed by this Artistic License. By using, modifying or
141 distributing the Package, you accept this license. Do not use, modify,
142 or distribute the Package, if you do not accept this license.
143  
144 If your Modified Version has been derived from a Modified Version made
145 by someone other than you, you are nevertheless required to ensure that
146 your Modified Version complies with the requirements of this license.
147  
148 This license does not grant you the right to use any trademark, service
149 mark, tradename, or logo of the Copyright Holder.
150  
151 This license includes the non-exclusive, worldwide, free-of-charge
152 patent license to make, have made, use, offer to sell, sell, import and
153 otherwise transfer the Package with respect to any patent claims
154 licensable by the Copyright Holder that are necessarily infringed by the
155 Package. If you institute patent litigation (including a cross-claim or
156 counterclaim) against any party alleging that the Package constitutes
157 direct or contributory patent infringement, then this Artistic License
158 to you shall terminate on the date that such litigation is filed.
159  
160 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
161 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
162 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
163 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
164 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
165 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
166 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
167 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168  
169 =cut
« Mail/Qmail/Filter/DMARC.pm ↑ Summary Mail/Qmail/Filter/Dump.pm »