File Coverage

blib/lib/dansguardian.pm
Criterion Covered Total %
statement 12 60 20.0
branch 0 24 0.0
condition 0 9 0.0
subroutine 4 13 30.7
pod n/a
total 16 106 15.0


line stmt bran cond sub pod time code
1             package Dansguardian;
2 1     1   23474 use warnings;
  1         2  
  1         34  
3 1     1   4 use strict;
  1         2  
  1         33  
4 1     1   5 use Carp;
  1         5  
  1         97  
5 1     1   1341 use Tie::File;
  1         23597  
  1         913  
6              
7             our $VERSION = '0.05';
8              
9             =head1 NAME
10              
11             Dansguardian - Simple module for administer dansguardian's control files.
12              
13             =head1 SYNOPSIS
14              
15             use Dansguardian;
16              
17             # Make the objet $dg using the contructor new()
18            
19             my $dg = Dansguardian->new(dir => '/etc/dansguardian', group_dir => '/etc/dansguardian/lists');
20              
21             # Save IP's banned in array
22            
23             my @banned_ips = $dg->get('bannediplist');
24             print "The IP address $_ is banned!!\n" foreach @banned_ips;
25              
26             # Add exception web site
27            
28             my $site = 'mogaal.com';
29             $dg->set(file => 'exceptionsitelist', add => $site, comment => "Is not porn site");
30              
31             # Remove banned IP
32            
33             my $ip_free = '192.168.0.2';
34             $dg->remove(file => 'bannediplist', line => $ip_free);
35              
36             # list sites banned
37            
38             my @sites_banned = $dg->get('bannedsitelist');
39             print "The site $_ is banned!!\n" foreach @sites_banned;
40              
41             # list dansguardian's config directory and the current group directory
42            
43             print "Dansguardian's config directory is " . $dg->group() . " and " . $dg->dir() . " is the current group directory\n";
44              
45             # Change group directory;
46            
47             $dg->group("/etc/dansguardian/chiefs");
48             print "Dansguardian's config directory is " . $dg->dir() . " and " . $dg->group() . " is the current group directory\n";
49              
50              
51             =head1 DESCRIPTION
52              
53             "DansGuardian is an award winning Open Source web content filter which currently runs on Linux, FreeBSD, OpenBSD, NetBSD, Mac OS X, HP-UX, and Solaris. It filters the actual content of pages based on many methods including phrase matching, PICS filtering and URL filtering. It does not purely filter based on a banned list of sites like lesser totally commercial filters."
54              
55             Dansguardian Perl module is small module for administer dansguardian's content control files. It let you add, remove and get information from files control across methods.
56              
57              
58             =head1 METHODS
59              
60             Dansguardian perl module provides some basic methods for administer control files, with it you can add, remove and get information about IP's blocked, sites denies, IP exception and other information.
61              
62             =head2 new (constructor)
63              
64             $dg = Dansguardian->new([%attributes])
65              
66             The constructor will create an object. It accepts a list of key => value pairs:
67              
68             =over 3
69              
70             =item dir => 'dansguardian/config/directory'
71              
72             If you don't set up a config directory for dansguardian the module will set up default value: /etc/dansguardian
73              
74             =item group_dir => 'path/to/group_dir/directory'
75              
76             Same that dir hash, and the default value is: /etc/dansguardian/lists
77              
78             =back
79              
80             =cut
81              
82             sub new {
83 0     0     my ($self, %args) = @_;
84 0           my $object = bless {
85             dir => $args{dir},
86             group_dir => $args{group_dir}
87             }, $self;
88 0           return $object;
89             }
90              
91              
92             =pod
93              
94             =head2 $dg->group([$group_dir]);
95              
96             If group method don't have attribute: the function return array with dansguardian current group directory. Is possible change the group directory setting up $group_dir variable.
97              
98             =cut
99              
100             sub group {
101 0     0     my $self = shift;
102 0 0         $self->{group_dir} = shift if @_;
103 0           return $self->{group_dir};
104             }
105              
106             =pod
107              
108              
109             =head2 $dg->dir([$config_dir]);
110              
111             If dir method don't have attribute: the function return array with dansguardian current config directory. Is possible change the group directory setting up $group_dir variable.
112              
113             =cut
114              
115             sub dir {
116 0     0     my $self = shift;
117 0 0         $self->{dir} = shift if @_;
118 0           return $self->{dir};
119             }
120              
121             =pod
122              
123              
124             =head2 $dg->set(%attributes);
125              
126             set method must have hash attributes. So, it accepts a list of key => value pairs:
127              
128             =over 3
129              
130             =item file => 'FILE'
131              
132             The FILE value is the file (locate inside current group dir) where you wish add information. For example: If you want add site to exception.
133              
134             $dg->set(file => 'exceptionsitelist', add => $site, comment => "Is not porn site");
135              
136             This line will add $site to /etc/dansguardian/lists/exceptioniplist assuming that /etc/dansguardian/lists is current group directory
137              
138             =item add => 'INFORMATION'
139              
140             The 'add' value is the information to add in dansguardian control file. For example: If you need add one IP for bannediplist control file, then assign 'IP' value for add hash key.
141              
142             =item comment => 'OPTIONAL COMMENT'
143              
144             =back
145              
146             This key is optional but very usefull for reading control files. It add comment at final line, after the 'add' value.
147              
148             =cut
149              
150             sub set ($;@) {
151 0     0     my ($self, %args) = @_;
152 0 0 0       croak "undefined arguments, yo must set up 'file' and 'add'" unless ($args{add} and $args{file});
153 0           my $file = "$self->{group_dir}/$args{file}";
154 0           &agregar($file,$args{add},$args{comment});
155 0           return 1;
156             }
157              
158             =pod
159              
160             =head2 $dg->remove($file)
161              
162             remove method must have one hash attribute with keys:
163              
164             =over 3
165              
166             =item file => 'FILE'
167              
168             The FILE value is the file (locate inside current group dir) where you wish remove information.
169              
170             =item line => 'LINE/IP/SITE/whatever'
171              
172             The value of hash key line is the information for remove in dansguardian control file. For example: For remove IP address from bannediplist control file you must add 'IP' like value of line hash key.
173              
174             =back
175              
176             =head3 Example
177              
178             $dg->remove(file => 'bannediplist', line => '192.168.24.76');
179              
180             =cut
181              
182             sub remove {
183 0     0     my ($self, %args) = @_;
184 0 0 0       croak "undefined arguments, yo must set up 'file' and 'add'" unless $args{file} and $args{line};
185 0           my $file = "$self->{group_dir}/$args{file}";
186 0           &rm($file,$args{line});
187 0           return 1;
188             }
189              
190             =pod
191              
192             =head2 $dg->get($file)
193              
194             Get method return an array data with information inside content control file. The incoming parameter is the control file name. Example:
195              
196             @ips_banned = $dg->get('bannediplist')
197             print "The IP address $_ is banned!!\n" foreach @banned_ips;
198              
199              
200             =cut
201              
202             sub get ($) {
203 0     0     my ($self, $file) = @_;
204 0 0         croak "undefined arguments, get() method must have file argument" unless $file;
205 0           $file = "$self->{group_dir}/$file";
206 0           my @data = &data($file);
207 0           return @data;
208             }
209            
210             ### Common subrutines
211              
212             sub data {
213 0     0     my ($file) = @_;
214 0           my @data;
215 0 0         open(FILE, "$file") || croak "Can't open $file: $!";
216 0           while (){
217 0           my ($data,$comments) = split((/\#/,$_));
218 0           chomp($data);
219 0 0         chomp $comments if $comments;
220 0           $data =~ s/^\s+//;
221 0           $data =~ s/\s+$//;
222 0 0         push(@data,$data) if $data ne "";
223             }
224 0           close(FILE);
225 0           return @data;
226             }
227              
228             sub agregar {
229 0     0     my ($file, $message, $comment) = @_;
230 0 0 0       croak "You must set up 'file' and 'add' arguments" unless $file and $message;
231 0 0         open(FILE, ">>$file") || croak "Can't open $file: $!";
232 0 0         if ($comment) {
233 0           print FILE "\n$message # $comment\n";
234             } else {
235 0           print FILE "\n$message\n";
236             }
237 0           close(FILE);
238             }
239              
240             sub rm {
241 0     0     my ($file, $line) = @_;
242 0 0         tie my @content, 'Tie::File', $file or die "Can't open $file: $!";
243 0           @content = grep { !/^$line/i } @content;
  0            
244 0           untie @content;
245             }
246              
247             1;
248              
249             =pod
250              
251             =head1 BUGS
252              
253             The package don't have been bugs reported. If you find one notice me.
254              
255             =head1 AUTHOR
256              
257             Alejandro Garrido Mota .
258              
259             =head1 COPYRIGHT AND LICENSE
260              
261             Copyright (c) 2007 Alejandro Garrido Mota. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
262              
263             The full text of the license can be found in the LICENSE file included with this module.
264              
265             =head1 LICENSE
266              
267             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
268              
269             The full text of the license can be found in the LICENSE file included with this module.
270              
271             =cut
272