RobotRules/DB_File.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 120 CC 12 SCAR 19.5
CC
12
Cov
0%
expires
12.0
roboturl
12.0
visit
12.0
CRAP 7.0
1 package WWW::RobotRules::DB_File;
2 use strict;
3  
4 use WWW::RobotRules ();
5 our @ISA = qw(WWW::RobotRules);
6 our $VERSION = '6.03';
7  
8 use Carp ();
9 use DB_File;
10 use Fcntl qw( O_CREAT O_RDWR );
11  
12 sub new {
13 my ($class, $name, $file) = @_;
14 Carp::croak('WWW::RobotRules::DB_File cache file required') unless $file;
15  
16 my $self = WWW::RobotRules->new($name);
17 $self = bless $self, $class;
18  
19 tie %{$self->{'rules'}}, DB_File, $file, O_CREAT | O_RDWR, 0640, $DB_HASH;
20  
21 $self;
22 }
23  
24 sub expires {
25 my ($self, $hostport, $expires) = @_;
26 my $old = $self->{'rules'}{"$hostport##expires"};
27 $old = 0 unless (defined $old);
28 if (defined $expires) {
29 $self->{'rules'}{"$hostport##expires"} = $expires;
30 }
31 $old;
32 }
33  
34  
35 sub roboturl {
36 my ($self, $hostport, $url) = @_;
37  
38 $url = $url->as_string if ref($url);
39  
40 my $old = $self->{'rules'}{"$hostport##url"};
41 if ($url) {
42 $self->{'rules'}{"$hostport##url"} = $url;
43 }
44 $old;
45 }
46  
47 sub host_count {
48 my ($self, $hostport) = @_;
49 $self->{'rules'}{"$hostport##count"};
50 }
51  
52 sub last_visit {
53 my ($self, $hostport) = @_;
54  
55 $self->{'rules'}{"$hostport##last"};
56 }
57  
58 sub visit {
59 my ($self, $hostport, $time) = @_;
60 $time = time unless defined $time;
61  
62 $self->{'rules'}{"$hostport##last"} = $time;
63 if (defined $self->{"rules##$hostport##count"}) {
64 $self->{'rules'}{"$hostport##count"}++;
65 }
66 else {
67 $self->{'rules'}{"$hostport##count"} = 1;
68 }
69 }
70  
71 sub push_rule {
72 my ($self, $hostport, $rule) = @_;
73  
74 my $cnt = 0;
75 foreach (keys %{$self->{'rules'}}) {
76 if (/^$hostport\#\#rule\#\#\d+$/) { $cnt++; }
77 }
78 $self->{'rules'}{"$hostport##rule##$cnt"} = $rule;
79 }
80  
81 sub clear_rules {
82 my ($self, $hostport) = @_;
83  
84 foreach (keys %{$self->{'rules'}}) {
85 if (/^($hostport\#\#rule\#\#\d+)$/) {
86 delete $self->{'rules'}{$1};
87 }
88 }
89 }
90  
91 sub rules {
92 my ($self, $hostport) = @_;
93  
94 my @rules = [];
95 foreach (keys %{$self->{'rules'}}) {
96 if (/^($hostport\#\#rule\#\#\d+)$/) {
97 push(@rules, $self->{'rules'}{$1});
98 }
99 }
100 return \@rules;
101 }
102  
103 sub hosts {
104 my ($self) = @_;
105  
106 my @hosts;
107 foreach (keys %{$self->{'rules'}}) {
108 if (/^([^\#]+)\#\#count/) {
109 push(@hosts, $1);
110 }
111 }
112 return \@hosts;
113 }
114  
115 1;
116 __END__