Mail/Qmail/Filter/Util.pm

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