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.
37 use vars qw($VERSION $BRANCH);
38 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
39 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
40 $main::build += $VERSION;
41 $main::branch += $BRANCH;
43 use vars qw ($filterbasefn $in);
45 $filterbasefn = "$main::root/filter";
48 # initial filter system
56 my ($class, $sort, $call, $flag) = @_;
57 $flag = ($flag) ? "in_" : "";
58 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
61 # standard filename generator
64 my ($sort, $call, $flag) = @_;
67 $flag = ($flag) ? "in_" : "";
69 my $fn = "$filterbasefn/$sort/$flag$call.pl";
74 $fn = "$filterbasefn/$sort/$flag$call.pl";
76 $fn = undef unless -e $fn;
80 # this reads in a filter statement and returns it as a list
82 # The filter is stored in straight perl so that it can be parsed and read
83 # in with a 'do' statement. The 'do' statement reads the filter into
84 # @in which is a list of references
91 my $ref = $self->{$fname};
94 if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
95 my $s = $ref->{$ar}->{asc}; # an optimisation?
97 $ref->{$ar}->{code} = eval "sub { $s }" ;
99 my $sort = $ref->{sort};
100 my $name = $ref->{name};
101 dbg("Error compiling $ar $sort $name: $@");
102 Log('err', "Error compiling $ar $sort $name: $@");
111 my ($sort, $call, $flag) = @_;
115 if ($fn = getfn($sort, $call, $flag)) {
117 my $s = readfilestr($fn);
125 $newin = new('Filter::Old', $sort, $call, $flag);
126 $newin->{filter} = $in;
127 } elsif (ref $newin && $newin->can('getfilkeys')) {
130 foreach $key ($newin->getfilkeys) {
131 $newin->compile($key, 'reject');
132 $newin->compile($key, 'accept');
135 # error on reading file, delete and exit
149 foreach $key (grep {/^filter/ } keys %$self) {
150 push @out, $self->{$key};
158 return grep {/^filter/ } keys %$self;
162 # This routine accepts a composite filter with a reject rule and then an accept rule.
164 # The filter returns 0 if an entry is matched by any reject rule and also if any
165 # accept rule fails otherwise it returns 1
167 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
168 # a reject then ok else if an accept then not ok.
170 # you can set a default with either an accept/xxxx all or reject/xxxx all
172 # Unlike the old system, this is kept as a hash of hashes so that you can
173 # easily change them by program.
175 # You can have 10 filter lines (0->9), they are tried in order until
178 # There is a parser that takes a Filter::Cmd object which describes all the possible
179 # things you can filter on and then converts that to a bit of perl which is compiled
180 # and stored as a function.
182 # The result of this is that in theory you can put together an arbritrarily complex
183 # expression involving the things you can filter on including 'and' 'or' 'not' and
188 # accept/spots hf and by_zone 14,15,16 and not by pa,on
190 # accept/spots freq 0/30000 and by_zone 4,5
192 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
194 # no filter no implies filter 1
196 # The field nos are the same as for the 'Old' filters
205 my @keys = sort $self->getfilkeys;
210 my $r = @keys > 0 ? 0 : 1;
211 foreach $key (@keys) {
212 $filter = $self->{$key};
213 if ($filter->{reject} && exists $filter->{reject}->{code}) {
215 $asc = $filter->{reject}->{user};
216 if (&{$filter->{reject}->{code}}(\@_)) {
223 if ($filter->{accept} && exists $filter->{accept}->{code}) {
225 $asc = $filter->{accept}->{user};
226 if (&{$filter->{accept}->{code}}(\@_)) {
235 # hops are done differently (simply)
236 my $hops = $self->{hops} if exists $self->{hops};
238 if (isdbg('filter')) {
239 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} @_;
240 my $true = $r ? "OK " : "REJ";
241 my $sort = $self->{sort};
242 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
245 dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
250 # this writes out the filter in a form suitable to be read in by 'read_in'
251 # It expects a list of references to filter lines
255 my $sort = $self->{sort};
256 my $name = $self->{name};
257 my $dir = "$filterbasefn/$sort";
258 my $fn = "$dir/$name";
260 mkdir $dir, 0775 unless -e $dir;
261 rename $fn, "$fn.o" if -e $fn;
262 my $fh = new IO::File ">$fn";
264 my $dd = new Data::Dumper([ $self ]);
267 $dd->Quotekeys($] < 5.005 ? 1 : 0);
268 $fh->print($dd->Dumpxs);
271 rename "$fn.o", $fn if -e "$fn.o";
280 my $name = shift || $self->{name};
281 my $sort = shift || $self->{sort};
282 my $flag = shift || "";
286 push @out, join(' ', $name , ':', $sort, $flag);
289 foreach $key (sort $self->getfilkeys) {
290 my $filter = $self->{$key};
291 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
292 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
294 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
295 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
305 my $name = uc $self->{name};
306 my $sort = $self->{sort};
308 $in = "in" if $name =~ s/^IN_//;
313 if ($name eq 'NODE_DEFAULT') {
314 @dxchan = DXChannel::get_all_nodes();
315 } elsif ($name eq 'USER_DEFAULT') {
316 @dxchan = DXChannel::get_all_users();
318 $dxchan = DXChannel::get($name);
319 push @dxchan, $dxchan if $dxchan;
321 foreach $dxchan (@dxchan) {
322 my $n = "$in$sort" . "filter";
323 my $i = $in ? 'IN_' : '';
324 my $ref = $dxchan->$n();
325 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
326 $dxchan->$n($remove ? undef : $self);
333 my ($sort, $call, $flag, $fno) = @_;
336 my $fn = getfn($sort, $call, $flag);
337 my $filter = read_in($sort, $call, $flag);
341 foreach $key ($filter->getfilkeys) {
342 delete $filter->{$key};
344 } elsif (exists $filter->{"filter$fno"}) {
345 delete $filter->{"filter$fno"};
349 if ($filter->{hops} || $filter->getfilkeys) {
368 # the general purpose command processor
369 # this is called as a subroutine not as a method
372 my ($self, $dxchan, $sort, $line) = @_;
380 # check the line for non legal characters
381 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)!]/;
383 # add some spaces for ease of parsing
384 $line =~ s/([\(\)])/ $1 /g;
387 my @f = split /\s+/, $line;
393 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
395 if ($f[0] eq 'input') {
400 $call = $dxchan->call;
403 if (@f && $f[0] =~ /^\d$/) {
407 $filter = Filter::read_in($sort, $call, $flag);
408 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
414 # do the rest of the filter tokens
431 } elsif ($tok eq ')') {
437 } elsif ($tok eq 'all') {
441 } elsif ($tok eq 'or') {
442 $conj = ' || ' if $conj ne ' || ';
444 } elsif ($tok eq 'and') {
445 $conj = ' && ' if $conj ne ' && ';
447 } elsif ($tok eq 'not' || $tok eq '!') {
453 my @val = split /,/, $val;
467 $user .= "$tok $val";
471 foreach $fref (@$self) {
473 if ($fref->[0] eq $tok) {
477 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
481 if ($fref->[1] eq 'a') {
485 push @t, "\$r->[$fref->[2]]=~/$_/i";
487 $s .= "(" . join(' || ', @t) . ")";
488 } elsif ($fref->[1] eq 'c') {
492 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
494 $s .= "(" . join(' || ', @t) . ")";
495 } elsif ($fref->[1] eq 'n') {
498 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
499 push @t, "\$r->[$fref->[2]]==$_";
501 $s .= "(" . join(' || ', @t) . ")";
502 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
503 my $cmd = $fref->[1];
504 my @pre = Prefix::to_ciz($cmd, @val);
505 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
506 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
507 } elsif ($fref->[1] =~ /^ns$/ ) { # for DXCC, ITU, CQ Zone
508 my $cmd = $fref->[1];
509 my @pre = Prefix::to_ciz($cmd, @val);
510 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
511 $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
512 } elsif ($fref->[1] eq 'r') {
515 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
516 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
518 $s .= "(" . join(' || ', @t) . ")";
519 } elsif ($fref->[1] eq 't') {
523 push @t, "\$r->[$fref->[2]]=~/$_/i";
525 $s .= "(" . join(' || ', @t) . ")";
527 confess("invalid letter $fref->[1]");
533 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
535 return ('no', $dxchan->msg('filter2', $tok));
541 # tidy up the user string
542 $user =~ s/\&\&/ and /g;
543 $user =~ s/\|\|/ or /g;
544 $user =~ s/\!/ not /g;
547 return (0, $filter, $fno, $user, "$s");
550 # a filter accept/reject command
553 my ($self, $dxchan, $sort, $type, $line) = @_;
555 return $dxchan->msg('filter5') unless $line;
557 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
558 my $u = DXUser->get_current($user);
559 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
560 return (1, $filter) if $r;
562 my $fn = "filter$fno";
564 $filter->{$fn} = {} unless exists $filter->{$fn};
565 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
567 $filter->{$fn}->{$type}->{user} = $user;
568 $filter->{$fn}->{$type}->{asc} = $s;
569 $r = $filter->compile($fn, $type);
577 return (0, $filter, $fno);
589 # the OLD instructions!
591 # Each filter file has the same structure:-
595 # [ action, fieldno, fieldsort, comparison, action data ],
599 # The action is usually 1 or 0 but could be any numeric value
601 # The fieldno is the field no in the list of fields that is presented
604 # The fieldsort is the type of field that we are dealing with which
605 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
606 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
608 # Filter::it basically goes thru the list of comparisons from top to
609 # bottom and when one matches it will return the action and the action data as a list.
611 # are the element nos of the list that is presented to Filter::it. Element
612 # 0 is the first field of the list.
616 # takes the reference to the filter (the first argument) and applies
617 # it to the subsequent arguments and returns the action specified.
622 my $filter = $self->{filter}; # this is now a bless ref of course but so what
624 my ($action, $field, $fieldsort, $comp, $actiondata);
627 # default action is 1
630 return ($action, $actiondata) if !$filter;
632 for $ref (@{$filter}) {
633 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
634 if ($fieldsort eq 'n') {
635 my $val = $_[$field];
636 return ($action, $actiondata) if grep $_ == $val, @{$comp};
637 } elsif ($fieldsort eq 'r') {
638 my $val = $_[$field];
640 my @range = @{$comp};
641 for ($i = 0; $i < @range; $i += 2) {
642 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
644 } elsif ($fieldsort eq 'a') {
645 return ($action, $actiondata) if $_[$field] =~ m{$comp};
647 return ($action, $actiondata); # the default action
657 my $flag = shift || "";
658 return "$call: Old Style Filter $flag $sort";