File Coverage

blib/lib/DNS/BL/cmds/print.pm
Criterion Covered Total %
statement 40 57 70.1
branch 8 42 19.0
condition 6 24 25.0
subroutine 10 10 100.0
pod 1 1 100.0
total 65 134 48.5


line stmt bran cond sub pod time code
1             package DNS::BL::cmds::print;
2              
3 2     2   1575 use DNS::BL;
  2         4  
  2         60  
4              
5 2     2   58 use 5.006001;
  2         7  
  2         79  
6 2     2   12 use strict;
  2         3  
  2         82  
7 2     2   12 use warnings;
  2         6  
  2         70  
8              
9 2     2   2115 use IO::File;
  2         26251  
  2         398  
10 2     2   24 use NetAddr::IP;
  2         5  
  2         24  
11 2     2   1451 use DNS::BL::cmds;
  2         6  
  2         67  
12 2     2   1031 use DNS::BL::Entry;
  2         4  
  2         62  
13              
14 2     2   11 use vars qw/@ISA/;
  2         5  
  2         1945  
15              
16             @ISA = qw/DNS::BL::cmds/;
17              
18             our $VERSION = '0.00_01';
19             $VERSION = eval $VERSION; # see L
20              
21             # Preloaded methods go here.
22              
23             =pod
24              
25             =head1 NAME
26              
27             DNS::BL::cmds::print - Print entries matching IP ranges
28              
29             =head1 SYNOPSIS
30              
31             use DNS::BL::cmds::print;
32              
33             =head1 DESCRIPTION
34              
35             This module implements the B command, used to lookup entries
36             from a DNSBL managed by L. The general syntax of this
37             command, is as follows
38              
39             print {within|matching} [to ] [as ]
40              
41             where each argument has the following function:
42              
43             =over 4
44              
45             =item Bip-addressE>
46              
47             Controls which entries are to be affected. Only entries that are fully
48             enclosed within the given IP address network range will be processed.
49              
50             =item Bip-addressE>
51              
52             Controls which entries are to be affected. Only entries that fully
53             enclose the given IP address network range will be processed.
54              
55             =item Boutput-fileE>
56              
57             Causes the result to be printed to the file name given as argument.
58              
59             =item BformatE>
60              
61             Influences the format to be used for producing the output of the
62             command. Available formats are:
63              
64             =over 2
65              
66             =item B
67              
68             Suitable for use in DJDNSBL data files.
69              
70             =item B
71              
72             A simple output format, which is the default.
73              
74             =item B
75              
76             A comma-separated format, suitable for import into other programs.
77              
78             =item B
79              
80             Returns the result in a list. This is useful for programs
81             incorporating this module without a CLI.
82              
83             =back
84              
85             =back
86              
87             This functionality is provided by the following method:
88              
89             =over
90              
91             =item C<-Eexecute()>
92              
93             See L for information on this method's general purpose
94             and calling convention.
95              
96             This method implements the behavior specified above.
97              
98             =cut
99              
100             sub execute
101             {
102 7     7 1 12 my $bl = shift;
103 7         12 my $command = shift;
104 7         23 my %args = @_;
105              
106 7         46 my @r = __PACKAGE__->arg_check($bl, 'print', $command,
107             [ qw/within matching to as/ ], \%args);
108 7 0       36 return wantarray ? (@r) : $r[0]
    50          
109             if $r[0] != &DNS::BL::DNSBL_OK;
110              
111 7         24 my $e = new DNS::BL::Entry;
112 7         9 my $ip;
113              
114 7 50 66     107 if (!exists $args{within} and !exists $args{matching})
    50 66        
    50 66        
115             {
116             return wantarray ?
117 0 0       0 (&DNS::BL::DNSBL_ESYNTAX(),
118             "'$command' requires a valid 'within' or 'matching' IP address")
119             : &DNS::BL::DNSBL_ESYNTAX();
120             }
121             elsif (exists $args{within}
122             and not $ip = new NetAddr::IP $args{within})
123             {
124             return wantarray ?
125 0 0       0 (&DNS::BL::DNSBL_ESYNTAX(),
126             "'$command' requires a valid 'within' IP address")
127             : &DNS::BL::DNSBL_ESYNTAX();
128             }
129             elsif (exists $args{matching}
130             and not $ip = new NetAddr::IP $args{matching})
131             {
132             return wantarray ?
133 0 0       0 (&DNS::BL::DNSBL_ESYNTAX(),
134             "'$command' requires a valid 'matching' IP address")
135             : &DNS::BL::DNSBL_ESYNTAX();
136             }
137              
138 7         2253 $e->addr($ip);
139              
140             # Fetch results from the database
141 7 100       21 if (exists $args{within})
142             {
143 5         19 @r = $bl->read($e);
144             }
145             else
146             {
147 2         7 @r = $bl->match($e);
148             }
149              
150 7 50       103 return wantarray ? ($r[0], "'" . __PACKAGE__
    50          
151             . "' failed on read: $r[1]") : $r[0]
152             if $r[0] != &DNS::BL::DNSBL_OK;
153              
154 0           shift @r; # Get rid of OK
155 0           my $msg = shift @r; # Keep our message
156              
157 0           my $fh;
158              
159 0 0         if ($args{to})
160             {
161 0           $fh = new IO::File $args{to}, "w";
162             return wantarray ?
163 0 0         (&DNS::BL::DNSBL_EOTHER(),
    0          
164             "Failed to open output file '$args{to}': $!")
165             : &DNS::BL::DNSBL_EOTHER()
166             unless $fh;
167             }
168             else
169             {
170 0           $fh = \*STDOUT;
171             }
172              
173 0 0 0       if (!defined $args{as} or $args{as} eq 'plain')
    0          
    0          
    0          
174             {
175             print $fh $_->addr . " (" . ($_->value || '127.0.0.1') . ") "
176             . ($_->desc || "No text") . " - " . $_->time . "\n"
177 0   0       for @r;
      0        
178             }
179             elsif ($args{as} eq 'comma')
180             {
181             print $fh
182             '"' . $_->addr . '", "'
183             . ($_->value || '127.0.0.1') . '", "'
184             . ($_->desc || "No text") . '", "'
185             . $_->time . qq{\"\n}
186 0   0       for @r;
      0        
187             }
188             elsif ($args{as} eq 'djdnsbl')
189             {
190             print $fh $_->addr . " :" . ($_->value || '127.0.0.1') . ":\$ "
191             . ($_->desc || "No text") . " - " . $_->time . "\n"
192 0   0       for @r;
      0        
193             }
194             elsif ($args{as} eq 'internal')
195             {
196 0 0         return wantarray ? (&DNS::BL::DNSBL_OK, $msg, @r) :
197             &DNS::BL::DNSBL_OK;
198             }
199             else
200             {
201             return wantarray ?
202 0 0         (&DNS::BL::DNSBL_ESYNTAX(),
203             "'$command as' requires a valid output format")
204             : &DNS::BL::DNSBL_ESYNTAX();
205             }
206              
207 0 0         return wantarray ? (&DNS::BL::DNSBL_OK, $msg) :
208             &DNS::BL::DNSBL_OK;
209             };
210              
211             1;
212             __END__