File Coverage

blib/lib/WebService/Livedoor/SpamChampuru/DNSBL.pm
Criterion Covered Total %
statement 44 45 97.7
branch 7 14 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 61 71 85.9


line stmt bran cond sub pod time code
1             package WebService::Livedoor::SpamChampuru::DNSBL;
2             # $Id: DNSBL.pm 768 2008-10-02 09:47:37Z i-ihara $
3 2     2   37939 use warnings;
  2         5  
  2         54  
4 2     2   11 use strict;
  2         2  
  2         56  
5              
6 2     2   1663 use Net::DNS::Resolver;
  2         173868  
  2         59  
7 2     2   17 use IO::Select;
  2         4  
  2         939  
8              
9             =head1 NAME
10              
11             WebService::Livedoor::SpamChampuru::DNSBL - Perl interface of SpamChampuru DNSBL WebService
12              
13             =head1 DESCRIPTION
14              
15             Checks if an IP has recently been used for sending spams, via spam-champuru (spam-champloo) DNSBL service (beta).
16              
17             Spam-champuru DNSBL holds list of IPs which have been reported as sources of spams by various services running at livedoor.com.
18              
19             The result may not be suitable for filtering mails, as this service is mainly intended for checking blog comments/trackbacks or bbs posts.
20              
21             * livedoor.com is not responsible for appropriateness of the result, nor for any harms caused by the use of the result.
22              
23             * The service is in a beta status, and may at any time become unavailable.
24              
25             =head1 VERSION
26              
27             Version 0.02
28              
29             =cut
30              
31             our $VERSION = '0.02';
32              
33             my $DNSBL_DOMAIN = "dnsbl.spam-champuru.livedoor.com";
34             my $DNSBL_ANS = "127.0.0.2";
35             my $TIMEOUT = 0.1;
36              
37              
38             =head1 SYNOPSIS
39              
40             use WebService::Livedoor::SpamChampuru::DNSBL;
41             my $dnsbl = WebService::Livedoor::SpamChampuru::DNSBL->new(timeout => 1);
42             my $res = $dnsbl->lookup($ip_addr);
43              
44             =head1 METHODS
45              
46             =head2 new
47              
48             Creates an WebService::Livedoor::SpamChampuru::DNSBL instance.
49              
50             $dnsbl = WebService::Livedoor::SpamChampuru::DNSBL->new(timeout => $timeout, [nameservers => \@nameservers]);
51              
52             =cut
53              
54             sub new {
55 1     1 1 517 my $class = shift;
56 1         5 my $self = bless {
57             @_,
58             }, $class;
59 1   33     9 $self->{timeout} ||= $TIMEOUT;
60 1         4 return $self;
61             }
62              
63             =head2 lookup
64              
65             Sends a DNS query to SpamChampuru DNSBL server and checks if $ip_addr is classified as a source of spam.
66             C returns 1 for SPAM, 0 for HAM.
67              
68             my $res = $dnsbl->lookup($ip_addr);
69              
70             lookup('192.0.2.1') should always return 1 (spam), as SpamChampuru DNSBL uses this IP as a test IP.
71              
72             =cut
73              
74             sub lookup {
75 1     1 1 615 my ($self, $ip_addr) = @_;
76 1         3 my $score = 0;
77              
78 1         7 my $reverse_ip_addr = join('.', reverse split /\./, $ip_addr);
79 1         4 my $dnsbl_request = join('.', $reverse_ip_addr, $DNSBL_DOMAIN);
80              
81 1 50       5 if (my $res = $self->_dnslookup_with_timeout($dnsbl_request, $self->{timeout})) {
82 1 50       8 if ($res eq $DNSBL_ANS) {
83 1         5 $score = 1;
84             }
85             }
86 1         14 return $score;
87             }
88              
89             sub _dnslookup_with_timeout {
90 1     1   2 my ($self, $dnsbl_request, $timeout) = @_;
91 1         2 my $result;
92             my $resolver;
93              
94 1 50       4 if (defined $self->{nameservers}) {
95 0         0 $resolver = Net::DNS::Resolver->new(
96             nameservers => $self->{nameservers},
97             );
98             }
99             else {
100 1         17 $resolver = Net::DNS::Resolver->new;
101             }
102              
103 1         576 my $bgsock = $resolver->bgsend($dnsbl_request);
104 1         4127 my $sel = IO::Select->new($bgsock);
105              
106 1         56 my @ready = $sel->can_read($timeout);
107 1 50       765787 if (@ready) {
108 1         9 foreach my $sock (@ready) {
109 1 50       21 if ($sock == $bgsock) {
110 1         53 my $packet = $resolver->bgread($bgsock);
111 1 50       4923 if ($packet) {
112 1         10 foreach my $rr ($packet->answer) {
113 1 50       20 next unless $rr->type eq "A";
114 1         29 $result = $rr->address;
115 1         23 last;
116             }
117             }
118 1         40 $bgsock = undef;
119             }
120 1         11 $sel->remove($sock);
121 1         158 $sock = undef;
122             }
123             }
124 1         55 return $result;
125             }
126              
127              
128             =head1 AUTHOR
129              
130             Kensuke Kaneko, C<< >>
131              
132             =head1 SEE ALSO
133              
134             L (Japanese text only)
135              
136             L
137              
138             =head1 COPYRIGHT & LICENSE
139              
140             Copyright 2008 livedoor Co., Ltd., all rights reserved.
141              
142             This program is free software; you can redistribute it and/or modify it
143             under the same terms as Perl itself.
144              
145              
146             =cut
147              
148             1; # End of WebService::Livedoor::SpamChampuru::DNSBL