2 # The User/Sysop Filter module
4 # The way this works is that the filter routine is actually
5 # a predefined function that returns 0 if it is OK and 1 if it
6 # is not when presented with a list of things.
8 # This set of routines provide a means of maintaining the filter
9 # scripts which are compiled in when an entity connects.
11 # Copyright (c) 1999 Dirk Koopman G1TLH
15 # The NEW INSTRUCTIONS
17 # use the commands accept/spot|ann|wwv|wcy and reject/spot|ann|wwv|wcy
18 # also show/filter spot|ann|wwv|wcy
20 # The filters live in a directory tree of their own in $main::root/filter
22 # Each type of filter (e.g. spot, wwv) live in a tree of their own so you
23 # can have different filters for different things for the same callsign.
36 use vars qw ($filterbasefn $in);
38 $filterbasefn = "$main::root/filter";
41 # initial filter system
49 my ($class, $sort, $call, $flag) = @_;
50 $flag = ($flag) ? "in_" : "";
51 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
54 # this reads in a filter statement and returns it as a list
56 # The filter is stored in straight perl so that it can be parsed and read
57 # in with a 'do' statement. The 'do' statement reads the filter into
58 # @in which is a list of references
62 my ($sort, $call, $flag) = @_;
65 $flag = ($flag) ? "in_" : "";
67 my $fn = "$filterbasefn/$sort/$flag$call.pl";
72 $fn = "$filterbasefn/$sort/$flag$call.pl";
78 my $s = readfilestr($fn);
80 dbg('conn', "$@") if $@;
82 $newin = new('Filter::Old', $sort, $call, $flag);
83 $newin->{filter} = $in;
91 # this routine accepts a composite filter with a reject component and then an accept
92 # the filter returns 0 if an entry is matched by any reject rule and also if any
93 # accept rule fails otherwise it returns 1
95 # the either set of rules may be missing meaning an implicit 'ok'
97 # reject rules are implicitly 'or' logic (any reject rules which fires kicks it out)
98 # accept rules are implicitly 'and' logic (all accept rules must pass to indicate a match)
100 # unlike the old system, this is kept as a hash of hashes so that you can
101 # easily change them by program.
103 # you can have a [any] number of 'filters', they are tried in random order until one matches
105 # an example in machine readable form:-
107 # name => 'G7BRN.pl',
114 # by_zone => [11, 'n', 4, 5],
120 # freq => [0, 'r', 0, 30000],
126 # by_zone => '14,15,16',
129 # freq => [0, 'r', 50000,52000,70000,70500,144000,148000],
130 # by_zone => [11, 'n', 14,15,16],
138 # accept/spots 1 freq 0/30000
139 # reject/spots 1 by_zone 4,5
140 # accept/spots 2 freq vhf
141 # accept/spots 2 by_zone 14,15,16
143 # no filter no implies filter 1
145 # The user_* fields are there so that the structure can be listed easily
146 # in human readable form when required. They are not used in the filtering
149 # This defines an HF filter and a VHF filter (as it happens)
160 my ($key, $ref, $field, $fieldsort, $comp);
161 L1: foreach $key (grep {/^filter/ } keys %$self) {
162 my $filter = $self->{$key};
164 if ($filter->{reject}) {
165 foreach $ref (values %{$filter->{reject}}) {
166 ($field, $fieldsort) = @$ref[0,1];
167 my $val = $_[$field];
168 if ($fieldsort eq 'n') {
169 next L1 if grep {$_ == $val} @{$ref}[2..$#$ref];
170 } elsif ($fieldsort eq 'r') {
172 for ($i = 2; $i < @$ref; $i += 2) {
173 next L1 if $val >= $ref->[$i] && $val <= $ref->[$i+1];
175 } elsif ($fieldsort eq 'a') {
176 next L1 if grep { $val =~ m{$_}} @$ref[2..$#$ref];
180 if ($filter->{accept}) {
181 foreach $ref (values %{$filter->{accept}}) {
182 ($field, $fieldsort) = @$ref[0,1];
183 my $val = $_[$field];
184 if ($fieldsort eq 'n') {
185 next L1 unless grep {$_ == $val} @{$ref}[2..$#$ref];
186 } elsif ($fieldsort eq 'r') {
188 for ($i = 2; $i < @$ref; $i += 2) {
189 next L1 unless $val >= $ref->[$i] && $val <= $ref->[$i+1];
191 } elsif ($fieldsort eq 'a') {
192 next L1 unless grep { $val =~ m{$_}} @{$ref}[2..$#$ref];
200 # hops are done differently
203 while (($comp, $ref) = each %{$self->{hops}}) {
204 ($field, $h) = @$ref;
205 if ($_[$field] =~ m{$comp}) {
214 # this writes out the filter in a form suitable to be read in by 'read_in'
215 # It expects a list of references to filter lines
219 my $sort = $self->{sort};
220 my $fn = $self->{name};
221 my $dir = "$filterbasefn/$sort";
222 mkdir $dir, 0775 unless -e $dir;
223 my $fh = new IO::File ">$dir/$fn" or return "$dir/$fn $!";
225 my $dd = new Data::Dumper([ $self ]);
228 $dd->Quotekeys($] < 5.005 ? 1 : 0);
229 $fh->print($dd->Dumpxs);
238 return $self->{name};
247 # the OLD instructions!
249 # Each filter file has the same structure:-
253 # [ action, fieldno, fieldsort, comparison, action data ],
257 # The action is usually 1 or 0 but could be any numeric value
259 # The fieldno is the field no in the list of fields that is presented
262 # The fieldsort is the type of field that we are dealing with which
263 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
264 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
266 # Filter::it basically goes thru the list of comparisons from top to
267 # bottom and when one matches it will return the action and the action data as a list.
269 # are the element nos of the list that is presented to Filter::it. Element
270 # 0 is the first field of the list.
274 # takes the reference to the filter (the first argument) and applies
275 # it to the subsequent arguments and returns the action specified.
280 my $filter = $self->{filter}; # this is now a bless ref of course but so what
282 my ($action, $field, $fieldsort, $comp, $actiondata);
285 # default action is 1
288 return ($action, $actiondata) if !$filter;
290 for $ref (@{$filter}) {
291 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
292 if ($fieldsort eq 'n') {
293 my $val = $_[$field];
294 return ($action, $actiondata) if grep $_ == $val, @{$comp};
295 } elsif ($fieldsort eq 'r') {
296 my $val = $_[$field];
298 my @range = @{$comp};
299 for ($i = 0; $i < @range; $i += 2) {
300 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
302 } elsif ($fieldsort eq 'a') {
303 return ($action, $actiondata) if $_[$field] =~ m{$comp};
305 return ($action, $actiondata); # the default action