| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/RobotRules/Storage/DB_File.pm 40092 2008-01-24T09:07:44.611388Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | |||
| 5 | package Gungho::Component::RobotRules::Storage::DB_File; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use base qw(Gungho::Component::RobotRules::Storage); | ||
| 9 | use DB_File; | ||
| 10 | use Storable qw(nfreeze thaw); | ||
| 11 | |||
| 12 | sub setup | ||
| 13 | { | ||
| 14 | my $self = shift; | ||
| 15 | my $config = $self->{config}; | ||
| 16 | $config->{filename} ||= File::Spec->catfile(File::Spec->tmpdir, 'robots.db'); | ||
| 17 | if (! exists $config->{flags}) { | ||
| 18 | $config->{flags} = O_CREAT|O_RDWR; | ||
| 19 | } | ||
| 20 | |||
| 21 | if (! exists $config->{mode}) { | ||
| 22 | $config->{mode} = 0666; | ||
| 23 | } | ||
| 24 | |||
| 25 | my %o; | ||
| 26 | my $dbm = tie %o, 'DB_File', $config->{filename}, $config->{flags}, $config->{mode}; | ||
| 27 | if (! $dbm) { | ||
| 28 | die "Failed to tie $config->{filename} to hash: $!"; | ||
| 29 | } | ||
| 30 | |||
| 31 | $self->storage( $dbm ); | ||
| 32 | $self->next::method(@_); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub get_rule | ||
| 36 | { | ||
| 37 | my $self = shift; | ||
| 38 | my $c = shift; | ||
| 39 | my $request = shift; | ||
| 40 | my $v; | ||
| 41 | |||
| 42 | my $uri = $request->original_uri; | ||
| 43 | if ($self->storage->get( $uri->host_port, $v ) == 0) { | ||
| 44 | return thaw($v); | ||
| 45 | } | ||
| 46 | return (); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub put_rule | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | my $c = shift; | ||
| 53 | my $request = shift; | ||
| 54 | my $rule = shift; | ||
| 55 | |||
| 56 | my $uri = $request->original_uri; | ||
| 57 | $self->storage->put( $uri->host_port, nfreeze($rule) ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub get_pending_robots_txt | ||
| 61 | { | ||
| 62 | my ($self, $c, $request) = @_; | ||
| 63 | my $uri = $request->original_uri; | ||
| 64 | delete $c->pending_robots_txt->{ $uri->host_port }; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub push_pending_robots_txt | ||
| 68 | { | ||
| 69 | my ($self, $c, $request) = @_; | ||
| 70 | my $uri = $request->original_uri; | ||
| 71 | my $h = $c->pending_robots_txt->{ $uri->host_port }; | ||
| 72 | if (! $h) { | ||
| 73 | $h = {}; | ||
| 74 | $c->pending_robots_txt->{ $uri->host_port } = $h; | ||
| 75 | } | ||
| 76 | |||
| 77 | if(! exists $h->{ $request->id }) { | ||
| 78 | $c->log->debug("Pushing request " . $request->uri . " to pending list (robot rules)..."); | ||
| 79 | $h->{ $request->id } = $request ; | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | __END__ |