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.
39 use vars qw ($filterbasefn $in);
41 $filterbasefn = "$main::root/filter";
46 # initial filter system
49 $json = DXJSON->new->indent(1);
54 my ($class, $sort, $call, $flag) = @_;
55 $flag = ($flag) ? "in_" : "";
56 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
59 # standard filename generator
62 my ($sort, $call, $flag) = @_;
65 $flag = ($flag) ? "in_" : "";
67 my $fn = "$filterbasefn/$sort/$flag$call.pl";
72 $fn = "$filterbasefn/$sort/$flag$call.pl";
74 $fn = undef unless -e $fn;
78 # this reads in a filter statement and returns it as a list
80 # The filter is stored in straight perl so that it can be parsed and read
81 # in with a 'do' statement. The 'do' statement reads the filter into
82 # @in which is a list of references
89 my $ref = $self->{$fname};
92 if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
93 my $s = $ref->{$ar}->{asc}; # an optimisation?
96 $ref->{$ar}->{code} = eval "sub { $s }" ;
98 my $sort = $ref->{sort};
99 my $name = $ref->{name};
100 dbg("Error compiling $ar $sort $name: $@");
101 Log('err', "Error compiling $ar $sort $name: $@");
110 my ($sort, $call, $flag) = @_;
114 if ($fn = getfn($sort, $call, $flag)) {
116 my $s = readfilestr($fn);
119 eval {$newin = $json->decode($s, __PACKAGE__)};
129 $newin = new('Filter::Old', $sort, $call, $flag);
130 $newin->{filter} = $in;
131 } elsif (ref $newin && $newin->can('getfilkeys')) {
134 foreach $key ($newin->getfilkeys) {
135 $newin->compile($key, 'reject');
136 $newin->compile($key, 'accept');
139 # error on reading file, delete and exit
140 dbg("empty or unreadable filter: $fn, deleted");
150 # this writes out the filter in a form suitable to be read in by 'read_in'
151 # It expects a list of references to filter lines
155 my $sort = $self->{sort};
156 my $name = $self->{name};
157 my $dir = "$filterbasefn/$sort";
158 my $fn = "$dir/$name";
160 mkdir $dir, 0775 unless -e $dir;
161 rename $fn, "$fn.o" if -e $fn;
162 my $fh = new IO::File ">$fn";
164 # my $dd = new Data::Dumper([ $self ]);
167 # $dd->Quotekeys($] < 5.005 ? 1 : 0);
168 # $fh->print($dd->Dumpxs);
170 # remove code references, do the encode, then put them back again (they can't be represented anyway)
172 foreach $key ($self->getfilkeys) {
173 $self->{$key}->{reject}->{code} = undef if exists $self->{$key}->{reject};
174 $self->{$key}->{accept}->{code} = undef if exists $self->{$key}->{accept};
176 $fh->print($json->encode($self));
177 foreach $key ($self->getfilkeys) {
178 $self->compile($key, 'reject');
179 $self->compile($key, 'accept');
183 rename "$fn.o", $fn if -e "$fn.o";
194 foreach $key (grep {/^filter/ } keys %$self) {
195 push @out, $self->{$key};
203 return grep {/^filter/ } keys %$self;
207 # This routine accepts a composite filter with a reject rule and then an accept rule.
209 # The filter returns 0 if an entry is matched by any reject rule and also if any
210 # accept rule fails otherwise it returns 1
212 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
213 # a reject then ok else if an accept then not ok.
215 # you can set a default with either an accept/xxxx all or reject/xxxx all
217 # Unlike the old system, this is kept as a hash of hashes so that you can
218 # easily change them by program.
220 # You can have 10 filter lines (0->9), they are tried in order until
223 # There is a parser that takes a Filter::Cmd object which describes all the possible
224 # things you can filter on and then converts that to a bit of perl which is compiled
225 # and stored as a function.
227 # The result of this is that in theory you can put together an arbritrarily complex
228 # expression involving the things you can filter on including 'and' 'or' 'not' and
233 # accept/spots hf and by_zone 14,15,16 and not by pa,on
235 # accept/spots freq 0/30000 and by_zone 4,5
237 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
239 # no filter no implies filter 1
241 # The field nos are the same as for the 'Old' filters
250 my @keys = sort $self->getfilkeys;
255 my $r = @keys > 0 ? 0 : 1;
256 foreach $key (@keys) {
257 $filter = $self->{$key};
258 if ($filter->{reject} && exists $filter->{reject}->{code}) {
260 $asc = $filter->{reject}->{user};
261 if (&{$filter->{reject}->{code}}(ref $_[0] ? $_[0] : \@_)) {
268 if ($filter->{accept} && exists $filter->{accept}->{code}) {
270 $asc = $filter->{accept}->{user};
271 if (&{$filter->{accept}->{code}}(ref $_[0] ? $_[0] : \@_)) {
280 # hops are done differently (simply)
281 my $hops = $self->{hops} if exists $self->{hops};
283 if (isdbg('filter')) {
284 my $call = $self->{name};
285 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} (ref $_[0] ? @{$_[0]} : @_);
286 my $true = $r ? "OK " : "REJ";
287 my $sort = $self->{sort};
288 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
292 dbg("Filter: $call $true $dir: $type/$sort with '$asc' on '$args' $h") if isdbg('filter');
300 my $name = shift || $self->{name};
301 my $sort = shift || $self->{sort};
302 my $flag = shift || "";
306 push @out, join(' ', $name , ':', $sort, $flag);
309 foreach $key (sort $self->getfilkeys) {
310 my $filter = $self->{$key};
311 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
312 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
314 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
315 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
325 my $name = uc $self->{name};
326 my $sort = $self->{sort};
328 $in = "in" if $name =~ s/^IN_//;
333 if ($name eq 'NODE_DEFAULT') {
334 @dxchan = DXChannel::get_all_nodes();
335 } elsif ($name eq 'USER_DEFAULT') {
336 @dxchan = DXChannel::get_all_users();
338 $dxchan = DXChannel::get($name);
339 push @dxchan, $dxchan if $dxchan;
341 foreach $dxchan (@dxchan) {
342 my $n = "$in$sort" . "filter";
343 my $i = $in ? 'IN_' : '';
344 my $ref = $dxchan->$n();
345 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
346 $dxchan->$n($remove ? undef : $self);
353 my ($sort, $call, $flag, $fno) = @_;
356 my $fn = getfn($sort, $call, $flag);
357 my $filter = read_in($sort, $call, $flag);
361 foreach $key ($filter->getfilkeys) {
362 delete $filter->{$key};
364 } elsif (exists $filter->{"filter$fno"}) {
365 delete $filter->{"filter$fno"};
369 if ($filter->{hops} || $filter->getfilkeys) {
393 $s =~ s/\{(.*?)\}/'{'. unpack('H*', $1) . '}'/eg if $s;
400 my ($v) = $r =~ /^\{(.*?)}$/;
401 return pack('H*', $v);
405 # the general purpose command processor
406 # this is called as a subroutine not as a method
409 my ($self, $dxchan, $sort, $line, $forcenew) = @_;
417 # check the line for non legal characters
418 dbg("Filter::parse line: '$line'") if isdbg('filter');
419 my @ch = $line =~ m|([^\s\w,_\.:\/\-\*\(\)\$!])|g;
420 return ('ill', $dxchan->msg('e19', join(' ', @ch))) if $line !~ /{.*}/ && @ch;
426 dbg("Filter parse line after regex check: '$line'") if isdbg('filter');
427 $line = encode_regex($line);
429 # add some spaces for ease of parsing
430 $line =~ s/([\(\!\)])/ $1 /g;
432 my @f = split /\s+/, $line;
433 dbg("filter parse: tokens '" . join("' '", @f) . "'") if isdbg('filter');
439 if (!$forcenew && @f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser::get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
441 if ($f[0] eq 'input') {
446 $call = $dxchan->call;
449 if (@f && $f[0] =~ /^\d$/) {
453 $filter = Filter::read_in($sort, $call, $flag) unless $forcenew;
454 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
460 # do the rest of the filter tokens
464 dbg("filter::parse: tok '$tok'") if isdbg('filter');
470 } elsif (grep $tok eq $_, qw{and or not ( )}) {
471 $s .= ' && ' if $tok eq 'and';
472 $s .= ' || ' if $tok eq 'or';
473 $s .= ' !' if $tok eq 'not';
474 $s .= $tok if $tok eq '(' or $tok eq ')';
477 } elsif ($tok eq '') {
483 my @val = split /,/, $val;
485 dbg("filter::parse: tok '$tok' val '$val'") if isdbg('filter');
486 $user .= " $tok $val";
490 foreach $fref (@$self) {
492 if ($fref->[0] eq $tok) {
496 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
500 if ($fref->[1] eq 'a' || $fref->[1] eq 't') {
502 foreach my $v (@val) {
503 $v =~ s/\*//g; # remove any trailing *
504 if (my ($r) = $v =~ /^\{(.*)\}$/) { # we have a regex
505 dbg("Filter::parse regex b: '\{$r\}'") if isdbg('filter');
506 $v = decode_regex($v);
507 dbg("Filter::parse regex a: '$v'") if isdbg('filter');
508 return ('regex', $dxchan->msg('e38', $v)) unless (qr{$v});
509 push @t, "\$r->[$fref->[2]]=~m{$v}i";
510 $v = "{$r}"; # put it back together again for humans
512 push @t, "\$r->[$fref->[2]]=~m{$v}i";
515 $s .= "(" . join(' || ', @t) . ")";
516 dbg("filter parse: s '$s'") if isdbg('filter');
517 } elsif ($fref->[1] eq 'c') {
521 push @t, "\$r->[$fref->[2]]=~m{^\U$_}";
523 $s .= "(" . join(' || ', @t) . ")";
524 dbg("filter parse: s '$s'") if isdbg('filter');
525 } elsif ($fref->[1] eq 'n') {
528 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
529 push @t, "\$r->[$fref->[2]]==$_";
531 $s .= "(" . join(' || ', @t) . ")";
532 dbg("filter parse: s '$s'") if isdbg('filter');
533 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
534 my $cmd = $fref->[1];
535 my @pre = Prefix::to_ciz($cmd, @val);
536 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
537 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
538 dbg("filter parse: s '$s'") if isdbg('filter');
539 } elsif ($fref->[1] =~ /^ns$/ ) { # for DXCC, ITU, CQ Zone
540 my $cmd = $fref->[1];
541 my @pre = Prefix::to_ciz($cmd, @val);
542 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
543 $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
544 dbg("filter parse: s '$s'") if isdbg('filter');
545 } elsif ($fref->[1] eq 'r') {
548 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
549 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
551 $s .= "(" . join(' || ', @t) . ")";
552 dbg("filter parse: s '$s'") if isdbg('filter');
554 confess("invalid filter function $fref->[1]");
560 return (1, $dxchan->msg('e20', $lasttok)) unless $found;
562 my $s = '{' . decode_regex($tok) . '}' if $tok =~ /^{.*}$/;
563 return (1, $dxchan->msg('filter2', $s));
569 # 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?
571 $user =~ s/\)\s*\(/ and /g;
572 $user =~ s/\&\&/ and /g;
573 $user =~ s/\|\|/ or /g;
574 $user =~ s/\!/ not /g;
576 $user =~ s/\{(.*?)\}/'{'. pack('H*', $1) . '}'/eg;
578 dbg("filter parse: user '$user'") if isdbg('filter');
582 $s =~ s/\)\s*\(/ && /g;
583 dbg("filter parse: s '$s'") if isdbg('filter');
587 return (0, $filter, $fno, $user, $s);
590 # a filter accept/reject command
593 my ($self, $dxchan, $sort, $type, $line) = @_;
594 return $dxchan->msg('filter5') unless $line;
596 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
597 return (1, $filter) if $r;
599 my $u = DXUser::get_current($user);
600 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
602 my $fn = "filter$fno";
604 $filter->{$fn} = {} unless exists $filter->{$fn};
605 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
607 $filter->{$fn}->{$type}->{user} = $user;
608 $filter->{$fn}->{$type}->{asc} = $s;
609 $r = $filter->compile($fn, $type);
615 $filter->install(1); # 'delete'
618 return (0, $filter, $fno);
630 # the OLD instructions!
632 # Each filter file has the same structure:-
636 # [ action, fieldno, fieldsort, comparison, action data ],
640 # The action is usually 1 or 0 but could be any numeric value
642 # The fieldno is the field no in the list of fields that is presented
645 # The fieldsort is the type of field that we are dealing with which
646 # currently can be 'a', 'n', 'r' or 'd'.
647 # 'a' is alphanumeric
649 # 'r' is ranges of pairs of numeric values
650 # 'd' is default (effectively, don't filter)
652 # Filter::it basically goes thru the list of comparisons from top to
653 # bottom and when one matches it will return the action and the action data as a list.
655 # are the element nos of the list that is presented to Filter::it. Element
656 # 0 is the first field of the list.
660 # takes the reference to the filter (the first argument) and applies
661 # it to the subsequent arguments and returns the action specified.
666 my $filter = $self->{filter}; # this is now a bless ref of course but so what
668 my ($action, $field, $fieldsort, $comp, $actiondata);
671 # default action is 1
674 return ($action, $actiondata) if !$filter;
676 for $ref (@{$filter}) {
677 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
678 if ($fieldsort eq 'n') {
679 my $val = $_[$field];
680 return ($action, $actiondata) if grep $_ == $val, @{$comp};
681 } elsif ($fieldsort eq 'r') {
682 my $val = $_[$field];
684 my @range = @{$comp};
685 for ($i = 0; $i < @range; $i += 2) {
686 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
688 } elsif ($fieldsort eq 'a') {
689 return ($action, $actiondata) if $_[$field] =~ m{$comp}i;
691 return ($action, $actiondata); # the default action (just pass through)
701 my $flag = shift || "";
702 return "$call: Old Style Filter $flag $sort";