blib/lib/dansguardian.pm

filter: Statementstmt 22.5% 14 / 62 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 30.7% 4 / 13 Podpod n/a totaltotal 15.5% 18 / 116 CC 17 SCAR 26.9
CC
17
Cov
14%
data
30.0
agregar
30.0
set
12.0
CRAP 14.7
↑ Summary
1 package Dansguardian;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 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 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
83 0 my ($self, %args) = @_;
84 my $object = bless {
85 dir => $args{dir},
86 group_dir => $args{group_dir}
87 0 }, $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 0 ▶sub group {
Subroutine group: not called CC=2 SCAR=17.9
101 0 my $self = shift;
102 0 ▶ $self->{group_dir} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
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 0 ▶sub dir {
Subroutine dir: not called CC=2 SCAR=17.9
116 0 my $self = shift;
117 0 ▶ $self->{dir} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
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 0 ▶sub set ($;@) {
Subroutine set: not called CC=3 SCAR=24.8
151 0 my ($self, %args) = @_;
152 0 ▶ croak "undefined arguments, yo must set up 'file' and 'add'" unless ($args{add} and $args{file});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"add"} and $args{"file"} 0 0 0
Condition0%0/3
$args{"add"} and $args{"file"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"add"} and $args{"file"}
$args{"add"} $args{"file"}
Truth table0%0/3
A and B
A= $args{"add"}B= $args{"file"}
ABresult
0X0
100
111
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 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
183 0 my ($self, %args) = @_;
184 0 ▶ croak "undefined arguments, yo must set up 'file' and 'add'" unless $args{file} and $args{line};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"file"} and $args{"line"} 0 0 0
Condition0%0/3
$args{"file"} and $args{"line"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"file"} and $args{"line"}
$args{"file"} $args{"line"}
Truth table0%0/3
A and B
A= $args{"file"}B= $args{"line"}
ABresult
0X0
100
111
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 0 ▶sub get ($) {
Subroutine get: not called CC=2 SCAR=17.9
203 0 my ($self, $file) = @_;
204 0 ▶ croak "undefined arguments, get() method must have file argument" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
205 0 $file = "$self->{group_dir}/$file";
206 0 my @data = &data($file);
207 0 return @data;
208 }
209
210 ### Common subrutines
211  
212 0 ▶sub data {
Subroutine data: not called CC=5 SCAR=34.0
213 0 my ($file) = @_;
214 0 my @data;
215 0 ▶ open(FILE, "$file") || croak "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "$file" or croak("Can't open ${file}: $!") 0 0 0
216 0 ▶ while (<FILE>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
217 0 my ($data,$comments) = split((/\#/,$_));
218 0 chomp($data);
219 0 ▶ chomp $comments if $comments;
Branch: 1 branch
BranchTrueFalseTotal
if $comments 0 0 0
220 0 $data =~ s/^\s+//;
221 0 $data =~ s/\s+$//;
222 0 ▶ push(@data,$data) if $data ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 0 0 0
223 }
224 0 close(FILE);
225 0 return @data;
226 }
227  
228 0 ▶sub agregar {
Subroutine agregar: not called CC=5 SCAR=34.0
229 0 my ($file, $message, $comment) = @_;
230 0 ▶ croak "You must set up 'file' and 'add' arguments" unless $file and $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $message 0 0 0
Condition0%0/3
$file and $message
!ll&&!rl&&r
000
MC/DC0%0/2
$file and $message
$file $message
Truth table0%0/3
A and B
A= $fileB= $message
ABresult
0X0
100
111
231 0 ▶ open(FILE, ">>$file") || croak "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">>$file" or croak("Can't open ${file}: $!") 0 0 0
232 0 ▶ if ($comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) { } 0 0 0
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 0 ▶sub rm {
Subroutine rm: not called CC=2 SCAR=17.9
241 0 my ($file, $line) = @_;
242 0 ▶ tie my @content, 'Tie::File', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
tie my @content, "Tie::File", $file or die "Can't open ${file}: $!" 0 0 0
243 0 @content = grep { !/^$line/i } @content;
244 0 untie @content;
245 }
246  
247 1 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 <garridomota@gmail.com>.
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  
↑ Summary