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);
121 $newin = new('Filter::Old', $sort, $call, $flag);
122 $newin->{filter} = $in;
126 foreach $key ($newin->getfilkeys) {
127 $newin->compile($key, 'reject');
128 $newin->compile($key, 'accept');
141 foreach $key (grep {/^filter/ } keys %$self) {
142 push @out, $self->{$key};
150 return grep {/^filter/ } keys %$self;
154 # This routine accepts a composite filter with a reject rule and then an accept rule.
156 # The filter returns 0 if an entry is matched by any reject rule and also if any
157 # accept rule fails otherwise it returns 1
159 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
160 # a reject then ok else if an accept then not ok.
162 # you can set a default with either an accept/xxxx all or reject/xxxx all
164 # Unlike the old system, this is kept as a hash of hashes so that you can
165 # easily change them by program.
167 # You can have 10 filter lines (0->9), they are tried in order until
170 # There is a parser that takes a Filter::Cmd object which describes all the possible
171 # things you can filter on and then converts that to a bit of perl which is compiled
172 # and stored as a function.
174 # The result of this is that in theory you can put together an arbritrarily complex
175 # expression involving the things you can filter on including 'and' 'or' 'not' and
180 # accept/spots hf and by_zone 14,15,16 and not by pa,on
182 # accept/spots freq 0/30000 and by_zone 4,5
184 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
186 # no filter no implies filter 1
188 # The field nos are the same as for the 'Old' filters
197 my @keys = sort $self->getfilkeys;
201 my $data = ref $_[0] ? shift : \@_;
203 my $r = @keys > 0 ? 0 : 1;
204 foreach $key (@keys) {
205 $filter = $self->{$key};
206 if ($filter->{reject} && exists $filter->{reject}->{code}) {
208 $asc = $filter->{reject}->{user};
209 if (&{$filter->{reject}->{code}}($data)) {
216 if ($filter->{accept} && exists $filter->{accept}->{code}) {
218 $asc = $filter->{accept}->{user};
219 if (&{$filter->{accept}->{code}}($data)) {
228 # hops are done differently (simply)
229 my $hops = $self->{hops} if exists $self->{hops};
231 if (isdbg('filter')) {
232 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} @$data;
233 my $true = $r ? "OK " : "REJ";
234 my $sort = $self->{sort};
235 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
238 dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
243 # this writes out the filter in a form suitable to be read in by 'read_in'
244 # It expects a list of references to filter lines
248 my $sort = $self->{sort};
249 my $name = $self->{name};
250 my $dir = "$filterbasefn/$sort";
251 my $fn = "$dir/$name";
253 mkdir $dir, 0775 unless -e $dir;
254 rename $fn, "$fn.o" if -e $fn;
255 my $fh = new IO::File ">$fn";
257 my $dd = new Data::Dumper([ $self ]);
260 $dd->Quotekeys($] < 5.005 ? 1 : 0);
261 $fh->print($dd->Dumpxs);
264 rename "$fn.o", $fn if -e "$fn.o";
273 my $name = shift || $self->{name};
274 my $sort = shift || $self->{sort};
275 my $flag = shift || "";
279 push @out, join(' ', $name , ':', $sort, $flag);
282 foreach $key (sort $self->getfilkeys) {
283 my $filter = $self->{$key};
284 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
285 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
287 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
288 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
298 my $name = uc $self->{name};
299 my $sort = $self->{sort};
301 $in = "in" if $name =~ s/^IN_//;
306 if ($name eq 'NODE_DEFAULT') {
307 @dxchan = DXChannel::get_all_nodes();
308 } elsif ($name eq 'USER_DEFAULT') {
309 @dxchan = DXChannel::get_all_users();
311 $dxchan = DXChannel->get($name);
312 push @dxchan, $dxchan if $dxchan;
314 foreach $dxchan (@dxchan) {
315 my $n = "$in$sort" . "filter";
316 my $i = $in ? 'IN_' : '';
317 my $ref = $dxchan->$n();
318 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
319 $dxchan->$n($remove ? undef : $self);
326 my ($sort, $call, $flag, $fno) = @_;
329 my $fn = getfn($sort, $call, $flag);
330 my $filter = read_in($sort, $call, $flag);
334 foreach $key ($filter->getfilkeys) {
335 delete $filter->{$key};
337 } elsif (exists $filter->{"filter$fno"}) {
338 delete $filter->{"filter$fno"};
342 if ($filter->{hops} || $filter->getfilkeys) {
361 # the general purpose command processor
362 # this is called as a subroutine not as a method
365 my ($self, $dxchan, $sort, $line) = @_;
373 # check the line for non legal characters
374 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)!]/;
376 # add some spaces for ease of parsing
377 $line =~ s/([\(\)])/ $1 /g;
380 my @f = split /\s+/, $line;
386 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
388 if ($f[0] eq 'input') {
393 $call = $dxchan->call;
396 if (@f && $f[0] =~ /^\d$/) {
400 $filter = Filter::read_in($sort, $call, $flag);
401 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
407 # do the rest of the filter tokens
424 } elsif ($tok eq ')') {
430 } elsif ($tok eq 'all') {
434 } elsif ($tok eq 'or') {
435 $conj = ' || ' if $conj ne ' || ';
437 } elsif ($tok eq 'and') {
438 $conj = ' && ' if $conj ne ' && ';
440 } elsif ($tok eq 'not' || $tok eq '!') {
446 my @val = split /,/, $val;
460 $user .= "$tok $val";
464 foreach $fref (@$self) {
466 if ($fref->[0] eq $tok) {
470 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
474 if ($fref->[1] eq 'a') {
478 push @t, "\$r->[$fref->[2]]=~/$_/i";
480 $s .= "(" . join(' || ', @t) . ")";
481 } elsif ($fref->[1] eq 'c') {
485 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
487 $s .= "(" . join(' || ', @t) . ")";
488 } elsif ($fref->[1] eq 'n') {
491 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
492 push @t, "\$r->[$fref->[2]]==$_";
494 $s .= "(" . join(' || ', @t) . ")";
495 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
496 my $cmd = $fref->[1];
497 my @pre = Prefix::to_ciz($cmd, @val);
498 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
499 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
500 } elsif ($fref->[1] =~ /^ns$/ ) { # for DXCC, ITU, CQ Zone
501 my $cmd = $fref->[1];
502 my @pre = Prefix::to_ciz($cmd, @val);
503 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
504 $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
505 } elsif ($fref->[1] eq 'r') {
508 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
509 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
511 $s .= "(" . join(' || ', @t) . ")";
512 } elsif ($fref->[1] eq 't') {
516 push @t, "\$r->[$fref->[2]]=~/$_/i";
518 $s .= "(" . join(' || ', @t) . ")";
520 confess("invalid letter $fref->[1]");
526 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
528 return ('no', $dxchan->msg('filter2', $tok));
534 # tidy up the user string
535 $user =~ s/\&\&/ and /g;
536 $user =~ s/\|\|/ or /g;
537 $user =~ s/\!/ not /g;
540 return (0, $filter, $fno, $user, "$s");
543 # a filter accept/reject command
546 my ($self, $dxchan, $sort, $type, $line) = @_;
548 return $dxchan->msg('filter5') unless $line;
550 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
551 my $u = DXUser->get_current($user);
552 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
553 return (1, $filter) if $r;
555 my $fn = "filter$fno";
557 $filter->{$fn} = {} unless exists $filter->{$fn};
558 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
560 $filter->{$fn}->{$type}->{user} = $user;
561 $filter->{$fn}->{$type}->{asc} = $s;
562 $r = $filter->compile($fn, $type);
570 return (0, $filter, $fno);
582 # the OLD instructions!
584 # Each filter file has the same structure:-
588 # [ action, fieldno, fieldsort, comparison, action data ],
592 # The action is usually 1 or 0 but could be any numeric value
594 # The fieldno is the field no in the list of fields that is presented
597 # The fieldsort is the type of field that we are dealing with which
598 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
599 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
601 # Filter::it basically goes thru the list of comparisons from top to
602 # bottom and when one matches it will return the action and the action data as a list.
604 # are the element nos of the list that is presented to Filter::it. Element
605 # 0 is the first field of the list.
609 # takes the reference to the filter (the first argument) and applies
610 # it to the subsequent arguments and returns the action specified.
615 my $filter = $self->{filter}; # this is now a bless ref of course but so what
617 my ($action, $field, $fieldsort, $comp, $actiondata);
620 # default action is 1
623 return ($action, $actiondata) if !$filter;
625 for $ref (@{$filter}) {
626 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
627 if ($fieldsort eq 'n') {
628 my $val = $_[$field];
629 return ($action, $actiondata) if grep $_ == $val, @{$comp};
630 } elsif ($fieldsort eq 'r') {
631 my $val = $_[$field];
633 my @range = @{$comp};
634 for ($i = 0; $i < @range; $i += 2) {
635 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
637 } elsif ($fieldsort eq 'a') {
638 return ($action, $actiondata) if $_[$field] =~ m{$comp};
640 return ($action, $actiondata); # the default action
650 my $flag = shift || "";
651 return "$call: Old Style Filter $flag $sort";