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 ($filterbasefn $in);
39 $filterbasefn = "$main::root/filter";
42 # initial filter system
50 my ($class, $sort, $call, $flag) = @_;
51 $flag = ($flag) ? "in_" : "";
52 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
55 # standard filename generator
58 my ($sort, $call, $flag) = @_;
61 $flag = ($flag) ? "in_" : "";
63 my $fn = "$filterbasefn/$sort/$flag$call.pl";
68 $fn = "$filterbasefn/$sort/$flag$call.pl";
70 $fn = undef unless -e $fn;
74 # this reads in a filter statement and returns it as a list
76 # The filter is stored in straight perl so that it can be parsed and read
77 # in with a 'do' statement. The 'do' statement reads the filter into
78 # @in which is a list of references
85 my $ref = $self->{$fname};
88 if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
89 my $s = $ref->{$ar}->{asc}; # an optimisation?
91 $ref->{$ar}->{code} = eval "sub { $s }" ;
93 my $sort = $ref->{sort};
94 my $name = $ref->{name};
95 dbg("Error compiling $ar $sort $name: $@");
96 Log('err', "Error compiling $ar $sort $name: $@");
105 my ($sort, $call, $flag) = @_;
109 if ($fn = getfn($sort, $call, $flag)) {
111 my $s = readfilestr($fn);
119 $newin = new('Filter::Old', $sort, $call, $flag);
120 $newin->{filter} = $in;
121 } elsif (ref $newin && $newin->can('getfilkeys')) {
124 foreach $key ($newin->getfilkeys) {
125 $newin->compile($key, 'reject');
126 $newin->compile($key, 'accept');
129 # error on reading file, delete and exit
130 dbg("empty or unreadable filter: $fn, deleted");
144 foreach $key (grep {/^filter/ } keys %$self) {
145 push @out, $self->{$key};
153 return grep {/^filter/ } keys %$self;
157 # This routine accepts a composite filter with a reject rule and then an accept rule.
159 # The filter returns 0 if an entry is matched by any reject rule and also if any
160 # accept rule fails otherwise it returns 1
162 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
163 # a reject then ok else if an accept then not ok.
165 # you can set a default with either an accept/xxxx all or reject/xxxx all
167 # Unlike the old system, this is kept as a hash of hashes so that you can
168 # easily change them by program.
170 # You can have 10 filter lines (0->9), they are tried in order until
173 # There is a parser that takes a Filter::Cmd object which describes all the possible
174 # things you can filter on and then converts that to a bit of perl which is compiled
175 # and stored as a function.
177 # The result of this is that in theory you can put together an arbritrarily complex
178 # expression involving the things you can filter on including 'and' 'or' 'not' and
183 # accept/spots hf and by_zone 14,15,16 and not by pa,on
185 # accept/spots freq 0/30000 and by_zone 4,5
187 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
189 # no filter no implies filter 1
191 # The field nos are the same as for the 'Old' filters
200 my @keys = sort $self->getfilkeys;
205 my $r = @keys > 0 ? 0 : 1;
206 foreach $key (@keys) {
207 $filter = $self->{$key};
208 if ($filter->{reject} && exists $filter->{reject}->{code}) {
210 $asc = $filter->{reject}->{user};
211 if (&{$filter->{reject}->{code}}(ref $_[0] ? $_[0] : \@_)) {
218 if ($filter->{accept} && exists $filter->{accept}->{code}) {
220 $asc = $filter->{accept}->{user};
221 if (&{$filter->{accept}->{code}}(ref $_[0] ? $_[0] : \@_)) {
230 # hops are done differently (simply)
231 my $hops = $self->{hops} if exists $self->{hops};
233 if (isdbg('filter')) {
234 my $call = $self->{name};
235 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} (ref $_[0] ? @{$_[0]} : @_);
236 my $true = $r ? "OK " : "REJ";
237 my $sort = $self->{sort};
238 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
242 dbg("Filter: $call $true $dir: $type/$sort with '$asc' on '$args' $h") if isdbg('filter');
247 # this writes out the filter in a form suitable to be read in by 'read_in'
248 # It expects a list of references to filter lines
252 my $sort = $self->{sort};
253 my $name = $self->{name};
254 my $dir = "$filterbasefn/$sort";
255 my $fn = "$dir/$name";
257 mkdir $dir, 0775 unless -e $dir;
258 rename $fn, "$fn.o" if -e $fn;
259 my $fh = new IO::File ">$fn";
261 my $dd = new Data::Dumper([ $self ]);
264 $dd->Quotekeys($] < 5.005 ? 1 : 0);
265 $fh->print($dd->Dumpxs);
268 rename "$fn.o", $fn if -e "$fn.o";
277 my $name = shift || $self->{name};
278 my $sort = shift || $self->{sort};
279 my $flag = shift || "";
283 push @out, join(' ', $name , ':', $sort, $flag);
286 foreach $key (sort $self->getfilkeys) {
287 my $filter = $self->{$key};
288 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
289 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
291 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
292 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
302 my $name = uc $self->{name};
303 my $sort = $self->{sort};
305 $in = "in" if $name =~ s/^IN_//;
310 if ($name eq 'NODE_DEFAULT') {
311 @dxchan = DXChannel::get_all_nodes();
312 } elsif ($name eq 'USER_DEFAULT') {
313 @dxchan = DXChannel::get_all_users();
315 $dxchan = DXChannel::get($name);
316 push @dxchan, $dxchan if $dxchan;
318 foreach $dxchan (@dxchan) {
319 my $n = "$in$sort" . "filter";
320 my $i = $in ? 'IN_' : '';
321 my $ref = $dxchan->$n();
322 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
323 $dxchan->$n($remove ? undef : $self);
330 my ($sort, $call, $flag, $fno) = @_;
333 my $fn = getfn($sort, $call, $flag);
334 my $filter = read_in($sort, $call, $flag);
338 foreach $key ($filter->getfilkeys) {
339 delete $filter->{$key};
341 } elsif (exists $filter->{"filter$fno"}) {
342 delete $filter->{"filter$fno"};
346 if ($filter->{hops} || $filter->getfilkeys) {
365 # the general purpose command processor
366 # this is called as a subroutine not as a method
369 my ($self, $dxchan, $sort, $line, $forcenew) = @_;
377 # check the line for non legal characters
378 dbg("Filter::parse line: '$line'") if isdbg('filter');
379 return ('ill', $dxchan->msg('e19')) if $line !~ /{.*}/ && $line =~ /[^\s\w,_\-\*\/\(\)\$!]/;
384 $line =~ s/\{(.*)\}/'{'. unpack('H*', $1) . '}'/eg;
385 dbg("Filter parse line after regex check: '$line'") if isdbg('filter');
387 # add some spaces for ease of parsing
388 $line =~ s/([\(\!\)])/ $1 /g;
390 my @f = split /\s+/, $line;
398 if (!$forcenew && @f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser::get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
400 if ($f[0] eq 'input') {
405 $call = $dxchan->call;
408 if (@f && $f[0] =~ /^\d$/) {
412 $filter = Filter::read_in($sort, $call, $flag) unless $forcenew;
413 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
419 # do the rest of the filter tokens
424 unless ($lasttok eq '(') {
440 } elsif ($tok eq ')') {
447 } elsif ($tok eq 'all') {
451 } elsif ($tok eq 'or') {
452 $conj = ' || ' if $conj ne ' || ';
455 } elsif ($tok eq 'and') {
456 $conj = ' && ' if $conj ne ' && ';
458 } elsif ($tok eq 'not' || $tok eq '!') {
465 my @val = split /,/, $val;
468 unless ($lasttok eq '(') {
480 $user .= "$tok $val";
484 foreach $fref (@$self) {
486 if ($fref->[0] eq $tok) {
490 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
494 if ($fref->[1] eq 'a' || $fref->[1] eq 't') {
496 foreach my $v (@val) {
497 $v =~ s/\*//g; # remove any trailing *
498 if (my ($r) = $v =~ /^\{(.*)\}$/) { # we have a regex
499 dbg("Filter::parse regex b: '\{$r\}'") if isdbg('filter');
501 dbg("Filter::parse regex a: '$v'") if isdbg('filter');
502 return ('regex', $dxchan->msg('e38', $v)) unless (qr{$v});
504 push @t, "\$r->[$fref->[2]]=~m{$v}i";
506 $s .= "(" . join(' || ', @t) . ")";
507 } elsif ($fref->[1] eq 'c') {
511 push @t, "\$r->[$fref->[2]]=~m{^\U$_}";
513 $s .= "(" . join(' || ', @t) . ")";
514 } elsif ($fref->[1] eq 'n') {
517 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
518 push @t, "\$r->[$fref->[2]]==$_";
520 $s .= "(" . join(' || ', @t) . ")";
521 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
522 my $cmd = $fref->[1];
523 my @pre = Prefix::to_ciz($cmd, @val);
524 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
525 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
526 } elsif ($fref->[1] =~ /^ns$/ ) { # for DXCC, ITU, CQ Zone
527 my $cmd = $fref->[1];
528 my @pre = Prefix::to_ciz($cmd, @val);
529 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
530 $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
531 } elsif ($fref->[1] eq 'r') {
534 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
535 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
537 $s .= "(" . join(' || ', @t) . ")";
539 confess("invalid filter function $fref->[1]");
545 return (1, $dxchan->msg('e20', $tok)) unless $found;
547 return (1, $dxchan->msg('filter2', $tok));
553 # tidy up the user string (why I have to stick in an if statement when I have initialised it I have no idea! 5.28 bug?
555 $user =~ s/\&\&/ and /g;
556 $user =~ s/\|\|/ or /g;
557 $user =~ s/\!/ not /g;
561 return (0, $filter, $fno, $user, $s);
564 # a filter accept/reject command
567 my ($self, $dxchan, $sort, $type, $line) = @_;
568 return $dxchan->msg('filter5') unless $line;
570 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
571 return (1, $filter) if $r;
573 my $u = DXUser::get_current($user);
574 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
576 my $fn = "filter$fno";
578 $filter->{$fn} = {} unless exists $filter->{$fn};
579 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
581 $filter->{$fn}->{$type}->{user} = $user;
582 $filter->{$fn}->{$type}->{asc} = $s;
583 $r = $filter->compile($fn, $type);
589 $filter->install(1); # 'delete'
592 return (0, $filter, $fno);
604 # the OLD instructions!
606 # Each filter file has the same structure:-
610 # [ action, fieldno, fieldsort, comparison, action data ],
614 # The action is usually 1 or 0 but could be any numeric value
616 # The fieldno is the field no in the list of fields that is presented
619 # The fieldsort is the type of field that we are dealing with which
620 # currently can be 'a', 'n', 'r' or 'd'.
621 # 'a' is alphanumeric
623 # 'r' is ranges of pairs of numeric values
624 # 'd' is default (effectively, don't filter)
626 # Filter::it basically goes thru the list of comparisons from top to
627 # bottom and when one matches it will return the action and the action data as a list.
629 # are the element nos of the list that is presented to Filter::it. Element
630 # 0 is the first field of the list.
634 # takes the reference to the filter (the first argument) and applies
635 # it to the subsequent arguments and returns the action specified.
640 my $filter = $self->{filter}; # this is now a bless ref of course but so what
642 my ($action, $field, $fieldsort, $comp, $actiondata);
645 # default action is 1
648 return ($action, $actiondata) if !$filter;
650 for $ref (@{$filter}) {
651 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
652 if ($fieldsort eq 'n') {
653 my $val = $_[$field];
654 return ($action, $actiondata) if grep $_ == $val, @{$comp};
655 } elsif ($fieldsort eq 'r') {
656 my $val = $_[$field];
658 my @range = @{$comp};
659 for ($i = 0; $i < @range; $i += 2) {
660 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
662 } elsif ($fieldsort eq 'a') {
663 return ($action, $actiondata) if $_[$field] =~ m{$comp}i;
665 return ($action, $actiondata); # the default action (just pass through)
675 my $flag = shift || "";
676 return "$call: Old Style Filter $flag $sort";