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 # standard filename generator
57 my ($sort, $call, $flag) = @_;
60 $flag = ($flag) ? "in_" : "";
62 my $fn = "$filterbasefn/$sort/$flag$call.pl";
67 $fn = "$filterbasefn/$sort/$flag$call.pl";
69 $fn = undef unless -e $fn;
73 # this reads in a filter statement and returns it as a list
75 # The filter is stored in straight perl so that it can be parsed and read
76 # in with a 'do' statement. The 'do' statement reads the filter into
77 # @in which is a list of references
81 my ($sort, $call, $flag) = @_;
85 if ($fn = getfn($sort, $call, $flag)) {
87 my $s = readfilestr($fn);
89 dbg('conn', "$@") if $@;
91 $newin = new('Filter::Old', $sort, $call, $flag);
92 $newin->{filter} = $in;
96 foreach $key ($newin->getfilkeys) {
97 $filter = $newin->{$key};
98 if ($filter->{reject} && exists $filter->{reject}->{asc}) {
99 $filter->{reject}->{code} = eval $filter->{reject}->{asc} ;
101 my $sort = $newin->{sort};
102 my $name = $newin->{name};
103 dbg('err', "Error compiling reject $sort $key $name: $@");
104 Log('err', "Error compiling reject $sort $key $name: $@");
107 if ($filter->{accept} && exists $filter->{accept}->{asc}) {
108 $filter->{accept}->{code} = eval $filter->{accept}->{asc} ;
110 my $sort = $newin->{sort};
111 my $name = $newin->{name};
112 dbg('err', "Error compiling accept $sort $key $name: $@");
113 Log('err', "Error compiling accept $sort $key $name: $@");
128 foreach $key (grep {/^filter/ } keys %$self) {
129 push @out, $self->{$key};
137 return grep {/^filter/ } keys %$self;
141 # This routine accepts a composite filter with a reject rule and then an accept rule.
143 # The filter returns 0 if an entry is matched by any reject rule and also if any
144 # accept rule fails otherwise it returns 1
146 # Either set of rules may be missing meaning an implicit 'ok'
148 # Unlike the old system, this is kept as a hash of hashes so that you can
149 # easily change them by program.
151 # You can have a [any] number of 'filters', they are tried in random order until
154 # There is a parser that takes a Filter::Cmd object which describes all the possible
155 # things you can filter on and then converts that to a bit of perl which is compiled
156 # and stored as a function.
158 # The result of this is that in theory you can put together an arbritrarily complex
159 # expression involving the things you can filter on including 'and' 'or' 'not' and
164 # accept/spots hf and by_zone 14,15,16 and not by pa,on
166 # accept/spots freq 0/30000 and by_zone 4,5
168 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
170 # no filter no implies filter 1
172 # The field nos are the same as for the 'Old' filters
184 foreach $filter ($self->getfilters) {
186 if ($filter->{reject} && exists $filter->{reject}->{code}) {
187 next if &{$filter->{reject}->{code}}(\@_);
189 if ($filter->{accept} && exists $filter->{accept}->{code}) {
190 next unless &{$filter->{accept}->{code}}(\@_);
196 # hops are done differently
199 while (($comp, $ref) = each %{$self->{hops}}) {
200 my ($field, $h) = @$ref;
201 if ($_[$field] =~ m{$comp}) {
210 # this writes out the filter in a form suitable to be read in by 'read_in'
211 # It expects a list of references to filter lines
215 my $sort = $self->{sort};
216 my $name = $self->{name};
217 my $dir = "$filterbasefn/$sort";
218 my $fn = "$dir/$name";
220 mkdir $dir, 0775 unless -e $dir;
221 rename $fn, "$fn.o" if -e $fn;
222 my $fh = new IO::File ">$fn";
224 my $dd = new Data::Dumper([ $self ]);
227 $dd->Quotekeys($] < 5.005 ? 1 : 0);
228 $fh->print($dd->Dumpxs);
231 rename "$fn.o", $fn if -e "$fn.o";
241 my $name = $self->{name};
244 push @out, join(' ', $name , ':', $self->{sort});
247 foreach $key (sort $self->getfilkeys) {
248 my $filter = $self->{$key};
249 if ($filter->{reject} && exists $filter->{reject}->{user}) {
250 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
252 if ($filter->{accept} && exists $filter->{accept}->{user}) {
253 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
263 my $name = uc $self->{name};
264 my $sort = $self->{sort};
265 my ($in) = $name =~ s/^IN_//;
268 my $dxchan = DXChannel->get($name);
271 my $n = "$in$sort" . "filter";
272 $dxchan->$n($remove ? undef : $self);
278 my ($sort, $call, $flag, $fno) = @_;
281 my $fn = getfn($sort, $call, $flag);
282 my $filter = read_in($sort, $call, $flag);
286 foreach $key ($filter->getfilkeys) {
287 delete $filter->{$key};
289 } elsif (exists $filter->{"filter$fno"}) {
290 delete $filter->{"filter$fno"};
294 if ($filter->{hops} || $filter->getfilkeys) {
309 # the general purpose command processor
310 # this is called as a subroutine not as a method
313 my ($self, $dxchan, $line) = @_;
321 # check the line for non legal characters
322 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\*\/\(\)]/;
324 # add some spaces for ease of parsing
325 $line =~ s/([\(\)])/ $1 /g;
328 my @f = split /\s+/, $line;
334 if (@f && $dxchan->priv >= 8 && (DXUser->get($f[0]) || $f[0] =~ /(?:node|user)_default/)) {
336 if ($f[0] eq 'input') {
341 $call = $dxchan->call;
344 if (@f && $f[0] =~ /^\d$/) {
348 $filter = Filter::read_in('spots', $call, $flag);
349 $filter = Filter->new('spots', $call, $flag) unless $filter;
355 # do the rest of the filter tokens
372 } elsif ($tok eq ')') {
378 } elsif ($tok eq 'or') {
379 $conj = ' || ' if $conj ne ' || ';
381 } elsif ($tok eq 'and') {
382 $conj = ' && ' if $conj ne ' && ';
384 } elsif ($tok eq 'not' || $tok eq '!') {
390 my @val = split /,/, $val;
400 $user .= "$tok $val";
404 foreach $fref (@$self) {
406 if ($fref->[0] eq $tok) {
410 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
414 if ($fref->[1] eq 'a') {
418 push @t, "\$r->[$fref->[2]]=~/$_/i";
420 $s .= "(" . join(' || ', @t) . ")";
421 } elsif ($fref->[1] eq 'c') {
425 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
427 $s .= "(" . join(' || ', @t) . ")";
428 } elsif ($fref->[1] eq 'n') {
431 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
432 push @t, "\$r->[$fref->[2]]==$_";
434 $s .= "(" . join(' || ', @t) . ")";
435 } elsif ($fref->[1] eq 'r') {
438 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
439 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
441 $s .= "(" . join(' || ', @t) . ")";
443 confess("invalid letter $fref->[1]");
449 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
451 return ('no', $dxchan->msg('filter2', $tok));
457 # tidy up the user string
458 $user =~ s/\&\&/ and /g;
459 $user =~ s/\|\|/ or /g;
460 $user =~ s/\!/ not /g;
463 return (0, $filter, $fno, $user, "sub { my \$r = shift; return $s }");
472 # the OLD instructions!
474 # Each filter file has the same structure:-
478 # [ action, fieldno, fieldsort, comparison, action data ],
482 # The action is usually 1 or 0 but could be any numeric value
484 # The fieldno is the field no in the list of fields that is presented
487 # The fieldsort is the type of field that we are dealing with which
488 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
489 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
491 # Filter::it basically goes thru the list of comparisons from top to
492 # bottom and when one matches it will return the action and the action data as a list.
494 # are the element nos of the list that is presented to Filter::it. Element
495 # 0 is the first field of the list.
499 # takes the reference to the filter (the first argument) and applies
500 # it to the subsequent arguments and returns the action specified.
505 my $filter = $self->{filter}; # this is now a bless ref of course but so what
507 my ($action, $field, $fieldsort, $comp, $actiondata);
510 # default action is 1
513 return ($action, $actiondata) if !$filter;
515 for $ref (@{$filter}) {
516 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
517 if ($fieldsort eq 'n') {
518 my $val = $_[$field];
519 return ($action, $actiondata) if grep $_ == $val, @{$comp};
520 } elsif ($fieldsort eq 'r') {
521 my $val = $_[$field];
523 my @range = @{$comp};
524 for ($i = 0; $i < @range; $i += 2) {
525 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
527 } elsif ($fieldsort eq 'a') {
528 return ($action, $actiondata) if $_[$field] =~ m{$comp};
530 return ($action, $actiondata); # the default action