| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/RobotRules/Rule.pm 31623 2007-12-01T04:12:45.036041Z lestrrat $ | ||
| 2 | |||
| 3 | package Gungho::Component::RobotRules::Rule; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw(Gungho::Base); | ||
| 7 | use URI; | ||
| 8 | |||
| 9 | __PACKAGE__->mk_accessors($_) for qw(rules); | ||
| 10 | |||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my $class = shift; | ||
| 14 | my $self = $class->next::method(); | ||
| 15 | $self->setup(@_); | ||
| 16 | $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub setup | ||
| 20 | { | ||
| 21 | my $self = shift; | ||
| 22 | my $rules = shift; | ||
| 23 | $self->rules($rules); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub allowed | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $c = shift; | ||
| 30 | my $uri = shift; | ||
| 31 | |||
| 32 | $uri = URI->new($uri) unless ref $uri; | ||
| 33 | my $str = $uri->path_query || '/'; | ||
| 34 | my $rules = $self->rules; | ||
| 35 | |||
| 36 | # XXX - There seems to be a problem where each %$rules doesn't get | ||
| 37 | # reset when we get out of the while loop in the middle of execution. | ||
| 38 | # We do this stupid hack to make sure that the context is reset correctly | ||
| 39 | keys %$rules; | ||
| 40 | while (my ($key, $list) = each %$rules) { | ||
| 41 | next unless $self->is_me($c, $key); | ||
| 42 | |||
| 43 | foreach my $rule (@$list) { | ||
| 44 | return 1 unless length $rule; | ||
| 45 | return 0 if index($str, $rule) == 0; | ||
| 46 | } | ||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub is_me | ||
| 53 | { | ||
| 54 | my $self = shift; | ||
| 55 | my $c = shift; | ||
| 56 | my $name = shift; | ||
| 57 | |||
| 58 | return $name eq '*' || index(lc($c->user_agent), lc($name)) >= 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |