File Coverage

blib/lib/DNS/BL/cmds/add.pm
Criterion Covered Total %
statement 45 50 90.0
branch 11 32 34.3
condition 2 5 40.0
subroutine 10 10 100.0
pod 1 1 100.0
total 69 98 70.4


line stmt bran cond sub pod time code
1             package DNS::BL::cmds::add;
2              
3 2     2   1289 use DNS::BL;
  2         5  
  2         50  
4              
5 2     2   54 use 5.006001;
  2         6  
  2         71  
6 2     2   10 use strict;
  2         3  
  2         65  
7 2     2   10 use warnings;
  2         4  
  2         74  
8              
9 2     2   10 use NetAddr::IP;
  2         5  
  2         15  
10 2     2   231 use DNS::BL::cmds;
  2         4  
  2         49  
11 2     2   10 use DNS::BL::Entry;
  2         3  
  2         47  
12              
13 2     2   16 use vars qw/@ISA/;
  2         3  
  2         119  
14              
15             @ISA = qw/DNS::BL::cmds/;
16              
17 2     2   11 use Carp;
  2         3  
  2         994  
18              
19             our $VERSION = '0.00_01';
20             $VERSION = eval $VERSION; # see L
21              
22             # Preloaded methods go here.
23              
24             =pod
25              
26             =head1 NAME
27              
28             DNS::BL::cmds::add - Add an entry to the database
29              
30             =head1 SYNOPSIS
31              
32             use DNS::BL::cmds::add;
33              
34             =head1 DESCRIPTION
35              
36             This module implements the B command, used to include entries
37             into a DNSBL managed by L. The general syntax of this
38             command, is as follows
39              
40             add ip [code ] [text ] [time ]
41              
42             where each argument has the following function:
43              
44             =over 4
45              
46             =item Bip-addressE>
47              
48             Specifies which IP address or network this command refers
49             to. Essentially, anything that L will understand.
50              
51             =item Breturn-codeE>
52              
53             The value returned by the DNSBL when a match with this entry is
54             found. Usually, this is something that can be returned in a DNS A RR,
55             an IP address. If not specified, '127.0.0.1' will be used as a
56             default.
57              
58             =item BtextE>
59              
60             The text associated with this entry in the DNSBL. Usually this is
61             associated with a DNS TXT RR. Defaults to an empty string.
62              
63             =item B
64              
65             The time associated with this entry in seconds since the
66             epoch. Defaults to the current time. Some converters might add this
67             item to the text description.
68              
69             =back
70              
71             This functionality is provided by the following method:
72              
73             =over
74              
75             =item C<-Eexecute()>
76              
77             See L for information on this method's general purpose
78             and calling convention.
79              
80             This method implements the behavior specified above.
81              
82             =cut
83              
84             sub execute
85             {
86 29     29 1 61 my $bl = shift;
87 29         41 my $command = shift;
88 29         102 my %args = @_;
89              
90 29         73 my @known = qw/ip code text time/;
91              
92 29         167 my @r = __PACKAGE__->arg_check($bl, 'add', $command,
93             [ qw/ip code text time without/ ], \%args);
94 29 0       134 return wantarray ? (@r) : $r[0]
    50          
95             if $r[0] != &DNS::BL::DNSBL_OK;
96            
97 29         114 my $e = new DNS::BL::Entry;
98 29         46 my $ip;
99              
100 29 50 33     177 unless (exists $args{ip} and $ip = new NetAddr::IP $args{ip})
101             {
102 0 0       0 return wantarray ? (&DNS::BL::DNSBL_ESYNTAX(),
103             "'add' requires a valid 'ip' address")
104             : &DNS::BL::DNSBL_ESYNTAX();
105             }
106              
107 29         16146 $e->addr($args{ip});
108              
109             # Check wether we can add this entry to the database or not
110              
111 29 100       83 if (!exists $args{without})
    50          
112             {
113 27         135 @r = $bl->read($e);
114             return wantarray ?
115 27 50       433 (&DNS::BL::DNSBL_ECOLLISSION,
    50          
116             "Collision with existing entry - Use 'print' to locate") :
117             &DNS::BL::DNSBL_ECOLLISSION
118             if $r[0] != &DNS::BL::DNSBL_ENOTFOUND;
119              
120 0         0 @r = $bl->match($e);
121             return wantarray ?
122 0 0       0 (&DNS::BL::DNSBL_ECOLLISSION,
    0          
123             "Collision with existing entry - Use 'print' to locate") :
124             &DNS::BL::DNSBL_ECOLLISSION
125             if $r[0] != &DNS::BL::DNSBL_ENOTFOUND;
126             }
127             elsif ($args{without} ne 'checking')
128             {
129             return wantarray ?
130 0 0       0 (&DNS::BL::DNSBL_ESYNTAX(),
131             "'add' checks can be spared using 'without checking'")
132             : &DNS::BL::DNSBL_ESYNTAX();
133             }
134              
135 2 50       20 $e->desc($args{text}) if exists $args{text};
136 2 50       7 $e->time($args{time}) if exists $args{time};
137 2   50     17 $e->value($args{code} || '127.0.0.1');
138              
139             # At this point, we should store the entry in the database
140 2         9 @r = $bl->write($e);
141              
142 2 50       42 return wantarray ? ($r[0], "'add' failed on write: $r[1]") : $r[0]
    50          
143             if $r[0] != &DNS::BL::DNSBL_OK;
144              
145 0 0         return wantarray ? (&DNS::BL::DNSBL_OK, "Entry added") :
146             &DNS::BL::DNSBL_OK;
147             };
148              
149             1;
150             __END__