| 1 | package WWW::Adblock; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.006; | ||
| 6 | use IO::File; | ||
| 7 | use WWW::Adblock::RegexFilter; | ||
| 8 | |||
| 9 | our $VERSION = "0.02"; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | WWW::Adblock - a simple implementation of Adblock Plus in Perl | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use WWW::Adblock; | ||
| 18 | my $adblock = WWW::Adblock->new(); | ||
| 19 | $adblock->load('/path/to/filters.txt'); | ||
| 20 | if ($adblock->filter('http://example.com/1x1.gif')) { | ||
| 21 | # it matched a filter rule | ||
| 22 | } | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This is a very simplified implementation of Adblock Plus (the popular browser extension) in Perl. Currently it only supports URI filtering and does not implement the per-element blocking offered by the extension. | ||
| 27 | |||
| 28 | =head2 Methods | ||
| 29 | |||
| 30 | =head3 new | ||
| 31 | |||
| 32 | my $adblock = WWW::Adblock->new(); | ||
| 33 | my $adblock = WWW::Adblock->new( filters => $target ); | ||
| 34 | |||
| 35 | Creates a new object. If C<$filters> is given it is passed to C<< $adblock->load >>. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | # The constructor of an object is called new() by convention. Any | ||
| 40 | # method may construct an object and you can have as many as you like. | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ( $class, %args ) = @_; | ||
| 44 | |||
| 45 | my $self = bless( {}, $class ); | ||
| 46 | |||
| 47 | $self->{filters} = {}; | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head3 load | ||
| 53 | |||
| 54 | $adblock->load('/path/to/rules.txt'); | ||
| 55 | |||
| 56 | Loads and parses a ruleset. May be called multiple times, new rules are added to the existing ones. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub load { | ||
| 61 | my ( $self, $file ) = @_; | ||
| 62 | |||
| 63 | my $rules = 0; | ||
| 64 | |||
| 65 | my $fh = new IO::File $file, O_RDONLY; | ||
| 66 | if ( defined $fh ) { | ||
| 67 | foreach my $line (<$fh>) { | ||
| 68 | chomp $line; | ||
| 69 | |||
| 70 | next if exists $self->{filters}{$line}; | ||
| 71 | |||
| 72 | # The first line of the file | ||
| 73 | if ( $line =~ m/^\[Adblock/ ) { | ||
| 74 | next; | ||
| 75 | |||
| 76 | # A comment line starts with ! | ||
| 77 | } | ||
| 78 | elsif ( $line =~ m/^!/ ) { | ||
| 79 | next; | ||
| 80 | |||
| 81 | # Element hiding rules, currently unsupported | ||
| 82 | } | ||
| 83 | elsif ( $line =~ | ||
| 84 | /^([^\/\*\|\@"]*?)#(?:([\w\-]+|\*)((?:\([\w\-]+(?:[$^*]?=[^\(\)"]*)?\))*)|#([^{}]+))$/ | ||
| 85 | ) | ||
| 86 | { | ||
| 87 | next; | ||
| 88 | |||
| 89 | } | ||
| 90 | else { | ||
| 91 | my $filter = WWW::Adblock::RegexFilter->new( 'text' => $line ); | ||
| 92 | if ( defined $filter ) { | ||
| 93 | $self->{filters}{$line} = $filter; | ||
| 94 | $rules++; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | undef $fh; # automatically closes the file | ||
| 99 | } | ||
| 100 | |||
| 101 | return $rules; | ||
| 102 | } | ||
| 103 | |||
| 104 | =head3 filter | ||
| 105 | |||
| 106 | # Check a $uri | ||
| 107 | $adblock->filter($uri); | ||
| 108 | |||
| 109 | # Check a $uri that appeared on a page on $domain | ||
| 110 | $adblock->filter($uri, $domain); | ||
| 111 | |||
| 112 | Checks a URI to see whether it matches any rules. Returns 0 for no match, 1 for positive match, 2 for whitelist. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub filter { | ||
| 117 | my ( $self, $uri, $domain ) = @_; | ||
| 118 | |||
| 119 | return 0 unless defined $uri; | ||
| 120 | |||
| 121 | # TODO: Implement a results cache? Would help with frequently | ||
| 122 | # hit filters | ||
| 123 | |||
| 124 | foreach my $f ( keys %{ $self->{filters} } ) { | ||
| 125 | my $filter = $self->{filters}{$f}; | ||
| 126 | my $result = $filter->matches( $uri, $domain ); | ||
| 127 | return $result if $result; | ||
| 128 | } | ||
| 129 | |||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | The idea for this code comes from Adblock Plus. Find out more at http://adblockplus.org/en/. | ||
| 136 | |||
| 137 | Implementation in Perl by David Cannings <david@edeca.net> | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | =head1 COPYRIGHT / LICENSE | ||
| 142 | |||
| 143 | Copyright (c) 2010 David Cannings. All rights reserved. | ||
| 144 | This program is free software; you can redistribute it and/or | ||
| 145 | modify it under the same terms as Perl itself. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 1; |