blib/lib/Mail/RBL.pm

filter: Statementstmt 93.0% 67 / 72 Branchbran 58.3% 14 / 24 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 76.6% 102 / 133 CC 22 SCAR 14.9
CC
22
Cov
79%
check_rhsbl
6.0
new
5.8
_do_txt
5.3
CRAP 4.4
↑ Summary
1 package Mail::RBL;
2  
3 2 require 5.005_62;
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use NetAddr::IP ':aton';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # $Id: RBL.pm,v 1.10 2007/04/25 04:22:04 lem Exp $
12  
13 2 our $VERSION = do { sprintf " %d.%02d", (q$Revision: 1.10 $ =~ /\d+/g) };
14  
15 =pod
16  
17 =head1 NAME
18  
19 Mail::RBL - Perl extension to access RBL-style host verification services
20  
21 =head1 SYNOPSIS
22  
23 use Mail::RBL;
24  
25 my $list = new Mail::RBL('list.org');
26  
27 # You can also specify a resolver to use with Net::DNS::Resolver
28  
29 my $list = new Mail::RBL('list.org', $res);
30  
31 if ($list->check($host)) {
32 print "$host is in the list";
33 }
34  
35 my ($ip_result, $optional_info_txt) = $list->check($host);
36 # $optional_info_txt will be undef if the list does not provide TXT
37 # RRs along with the A RRs.
38  
39 print "The list says ", ($list->check($host))[1], " in its TXT RR\n";
40  
41 my ($ip_result, $optional_info_txt) = $list->check_rhsbl($hostname);
42  
43 =head1 DESCRIPTION
44  
45 This module eases the task of checking if a given host is in the
46 list. The methods available are described below:
47  
48 =over
49  
50 =item C<-E<gt>new(suffix, resolver)>
51  
52 Creates a list handle. The C<suffix> parameter is mandatory and
53 specifies which suffix to append to the queries. If left unspecified,
54 defaults to C<bl.spamcop.net>.
55  
56 An optional DNS resolver can be specified. An object of the
57 Net::DNS::Resolver(3) class is expected.
58  
59 =cut
60  
61 20 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod documented
62 20 my $type = shift;
63 20 ▶ my $class = ref($type) || $type || "Mail::RBL";
Condition40%2/5
ref $type || $type || 'Mail::RBL'
l!l
200
ref $type or $type
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $type || $type || 'Mail::RBL'
ref $type $type
Truth table33%1/3
A or B
A= ref $typeB= $type
ABresult
1X1
011
000
64 20 my $suffix = shift;
65 20 ▶ my $res = shift || Net::DNS::Resolver->new;
Condition66%2/3
shift() || 'Net::DNS::Resolver'->new
l!l&&r!l&&!r
10100
MC/DC0%0/2
shift() || 'Net::DNS::Resolver'->new
shift() 'Net::DNS::Resolver'->new
Truth table66%2/3
A || B
A= shift()B= 'Net::DNS::Resolver'->new
ABresult
1X1
011
000
66
67 20 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
defined $suffix ? : 20 0 20
68 suffix => defined $suffix ? $suffix : 'bl.spamcop.net',
69 res => $res,
70 };
71  
72 20 bless $self, $class;
73 }
74  
75 =pod
76  
77 =item C<-E<gt>check($host)>
78  
79 C<$host> can be either a hostname or an IP address. In the case of an
80 IP Address. In the case of a hostname, all the IP addresses will be
81 looked up and checked against the list. If B<any> of the addresses is
82 in the list, the host will be considered in the list as a whole.
83  
84 Returns either a C<NetAddr::IP> object as returned by the RBL itself,
85 or C<undef> in case the RBL does not supply an answer. This is
86 important because many lists inject some semantics on the DNS response
87 value, which now can be recovered easily with the program that uses
88 this module.
89  
90 In array context, any IP addresses are returned, followed by any TXT
91 RR (or undef if none). If no match is found, an empty list is returned
92 instead. In scalar context, only the first IP address (or undef) is
93 returned.
94  
95 =back
96  
97 =cut
98  
99 sub check ($$)
100 14 ▶{
Subroutine check: called CC=5 SCAR=16.1
Pod documented
101 14 my $self = shift;
102 14 my $host = shift;
103  
104 14 ▶ croak "Must call ->check() with a host to check"
Branch: 1 branch
BranchTrueFalseTotal
unless length $host 0 14 14
105 unless length $host;
106  
107 14 foreach my $addr (_inverted_addresses($host)) {
108 14 ▶ if (my $val = $self->_do_check($addr))
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->_do_check($addr)) 3 11 14
109 {
110 3 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 1 3
111 {
112 2 return ($val, $self->_do_txt($addr));
113 }
114 else
115 {
116 1 return $val;
117 }
118 }
119 }
120  
121 11 return;
122 }
123  
124 =pod
125  
126 =item C<-E<gt>check_rhsbl($host)>
127  
128 Analogous to C<-E<gt>check()>, but queries RHSBLs instead of IP-based
129 lists. This is useful for using lists such as some of
130 B<http://www.rfc-ignorant.org/>.
131  
132 Results and return values are the same as C<-E<gt>check()>.
133  
134 =cut
135  
136 sub check_rhsbl ($$)
137 32 ▶{
Subroutine check_rhsbl: called CC=4 SCAR=17.9
Pod documented
138 32 my $self = shift;
139 32 my $host = shift;
140  
141 32 ▶ croak "Must call ->check_rhsbl() with a host to check"
Branch: 1 branch
BranchTrueFalseTotal
unless length $host 0 32 32
142 unless length $host;
143  
144 32 ▶ if (my $val = $self->_do_check($host))
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->_do_check($host)) 0 32 32
145 {
146 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
147 {
148 0 return ($val, $self->_do_txt($host));
149 }
150 else
151 {
152 0 return $val;
153 }
154 }
155  
156 32 return;
157 }
158  
159 2 ▶sub _do_txt {
Subroutine _do_txt: called CC=5 SCAR=16.7
160 2 my $self = shift;
161 2 my $host = shift;
162  
163 2 my $res = $self->{res};
164 2 my $q = $res->query($host . '.' . $self->{suffix}, "TXT");
165 2 my @txt = ();
166  
167 2 ▶ if ($q)
Branch: 1 branch
BranchTrueFalseTotal
if ($q) 2 0 2
168 {
169 2 for my $rr ($q->answer)
170 {
171 2 ▶ next unless $rr->class eq 'IN' and $rr->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->class eq "IN" and $rr->type eq "TXT" 0 2 2
Condition33%1/3
$rr->class eq "IN" and $rr->type eq "TXT"
!ll&&!rl&&r
002
MC/DC0%0/2
$rr->class eq "IN" and $rr->type eq "TXT"
$rr->class eq "IN" $rr->type eq "TXT"
Truth table33%1/3
A and B
A= $rr->class eq "IN"B= $rr->type eq "TXT"
ABresult
0X0
100
111
172 2 push @txt, $rr->rdatastr;
173 }
174 }
175  
176 2 return @txt;
177 }
178  
179 46 ▶sub _do_check {
Subroutine _do_check: called CC=5 SCAR=16.4
180 46 my $self = shift;
181 46 my $host = shift;
182  
183 46 my $res = $self->{res};
184 46 my $q = $res->query($host . '.' . $self->{suffix}, "A");
185  
186 46 ▶ if ($q)
Branch: 1 branch
BranchTrueFalseTotal
if ($q) 3 43 46
187 {
188 3 for my $rr ($q->answer)
189 {
190 3 ▶ next unless $rr->class eq 'IN' and $rr->type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->class eq "IN" and $rr->type eq "A" 0 3 3
Condition33%1/3
$rr->class eq "IN" and $rr->type eq "A"
!ll&&!rl&&r
003
MC/DC0%0/2
$rr->class eq "IN" and $rr->type eq "A"
$rr->class eq "IN" $rr->type eq "A"
Truth table33%1/3
A and B
A= $rr->class eq "IN"B= $rr->type eq "A"
ABresult
0X0
100
111
191 3 return NetAddr::IP->new($rr->address);
192 }
193 }
194 43 return;
195 }
196  
197 14 ▶sub _inverted_addresses {
Subroutine _inverted_addresses: called CC=3 SCAR=11.8
198 14 my $host = shift;
199 14 my @addresses;
200 my @ret;
201  
202 14 ▶ if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) { } 14 0 14
203 14 push @ret, join('.', reverse split(/\./, $host));
204 }
205 else {
206 0 @addresses = (gethostbyname($host))[4];
207 }
208
209 14 foreach my $addr (@addresses) {
210 0 push @ret, join('.', reverse unpack('C4', $addr));
211 }
212
213 14 return @ret;
214 }
215  
216 2 1;
217 __END__
↑ Summary