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);
39 main::mkver($VERSION = q$Revision$);
41 use vars qw ($filterbasefn $in);
43 $filterbasefn = "$main::root/filter";
46 # initial filter system
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?
95 $ref->{$ar}->{code} = eval "sub { $s }" ;
97 my $sort = $ref->{sort};
98 my $name = $ref->{name};
99 dbg("Error compiling $ar $sort $name: $@");
100 Log('err', "Error compiling $ar $sort $name: $@");
109 my ($sort, $call, $flag) = @_;
113 if ($fn = getfn($sort, $call, $flag)) {
115 my $s = readfilestr($fn);
119 $newin = new('Filter::Old', $sort, $call, $flag);
120 $newin->{filter} = $in;
124 foreach $key ($newin->getfilkeys) {
125 $newin->compile($key, 'reject');
126 $newin->compile($key, 'accept');
139 foreach $key (grep {/^filter/ } keys %$self) {
140 push @out, $self->{$key};
148 return grep {/^filter/ } keys %$self;
152 # This routine accepts a composite filter with a reject rule and then an accept rule.
154 # The filter returns 0 if an entry is matched by any reject rule and also if any
155 # accept rule fails otherwise it returns 1
157 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
158 # a reject then ok else if an accept then not ok.
160 # you can set a default with either an accept/xxxx all or reject/xxxx all
162 # Unlike the old system, this is kept as a hash of hashes so that you can
163 # easily change them by program.
165 # You can have 10 filter lines (0->9), they are tried in order until
168 # There is a parser that takes a Filter::Cmd object which describes all the possible
169 # things you can filter on and then converts that to a bit of perl which is compiled
170 # and stored as a function.
172 # The result of this is that in theory you can put together an arbritrarily complex
173 # expression involving the things you can filter on including 'and' 'or' 'not' and
178 # accept/spots hf and by_zone 14,15,16 and not by pa,on
180 # accept/spots freq 0/30000 and by_zone 4,5
182 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
184 # no filter no implies filter 1
186 # The field nos are the same as for the 'Old' filters
195 my @keys = sort $self->getfilkeys;
199 my $data = ref $_[0] ? shift : \@_;
201 my $r = @keys > 0 ? 0 : 1;
202 foreach $key (@keys) {
203 $filter = $self->{$key};
204 if ($filter->{reject} && exists $filter->{reject}->{code}) {
206 $asc = $filter->{reject}->{user};
207 if (&{$filter->{reject}->{code}}($data)) {
214 if ($filter->{accept} && exists $filter->{accept}->{code}) {
216 $asc = $filter->{accept}->{user};
217 if (&{$filter->{accept}->{code}}($data)) {
226 # hops are done differently (simply)
227 my $hops = $self->{hops} if exists $self->{hops};
229 if (isdbg('filter')) {
230 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} @$data;
231 my $true = $r ? "OK " : "REJ";
232 my $sort = $self->{sort};
233 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
236 dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
241 # this writes out the filter in a form suitable to be read in by 'read_in'
242 # It expects a list of references to filter lines
246 my $sort = $self->{sort};
247 my $name = $self->{name};
248 my $dir = "$filterbasefn/$sort";
249 my $fn = "$dir/$name";
251 mkdir $dir, 0775 unless -e $dir;
252 rename $fn, "$fn.o" if -e $fn;
253 my $fh = new IO::File ">$fn";
255 my $dd = new Data::Dumper([ $self ]);
258 $dd->Quotekeys($] < 5.005 ? 1 : 0);
259 $fh->print($dd->Dumpxs);
262 rename "$fn.o", $fn if -e "$fn.o";
271 my $name = shift || $self->{name};
272 my $sort = shift || $self->{sort};
273 my $flag = shift || "";
277 push @out, join(' ', $name , ':', $sort, $flag);
280 foreach $key (sort $self->getfilkeys) {
281 my $filter = $self->{$key};
282 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
283 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
285 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
286 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
296 my $name = uc $self->{name};
297 my $sort = $self->{sort};
299 $in = "in" if $name =~ s/^IN_//;
304 if ($name eq 'NODE_DEFAULT') {
305 @dxchan = grep{$_->is_node || $_->is_aranea} DXChannel::get_all();
306 } elsif ($name eq 'USER_DEFAULT') {
307 @dxchan = DXChannel::get_all_users();
309 $dxchan = DXChannel::get($name);
310 push @dxchan, $dxchan if $dxchan;
312 foreach $dxchan (@dxchan) {
313 my $n = "$in$sort" . "filter";
314 my $i = $in ? 'IN_' : '';
315 my $ref = $dxchan->$n();
316 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
317 $dxchan->$n($remove ? undef : $self);
324 my ($sort, $call, $flag, $fno) = @_;
327 my $fn = getfn($sort, $call, $flag);
328 my $filter = read_in($sort, $call, $flag);
332 foreach $key ($filter->getfilkeys) {
333 delete $filter->{$key};
335 } elsif (exists $filter->{"filter$fno"}) {
336 delete $filter->{"filter$fno"};
340 if ($filter->{hops} || $filter->getfilkeys) {
359 # the general purpose command processor
360 # this is called as a subroutine not as a method
363 my ($self, $dxchan, $sort, $line) = @_;
371 # check the line for non legal characters
372 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)!]/;
374 # add some spaces for ease of parsing
375 $line =~ s/([\(\)])/ $1 /g;
378 my @f = split /\s+/, $line;
384 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
386 if ($f[0] eq 'input') {
391 $call = $dxchan->call;
394 if (@f && $f[0] =~ /^\d$/) {
398 $filter = Filter::read_in($sort, $call, $flag);
399 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
405 # do the rest of the filter tokens
422 } elsif ($tok eq ')') {
428 } elsif ($tok eq 'all') {
432 } elsif ($tok eq 'or') {
433 $conj = ' || ' if $conj ne ' || ';
435 } elsif ($tok eq 'and') {
436 $conj = ' && ' if $conj ne ' && ';
438 } elsif ($tok eq 'not' || $tok eq '!') {
444 my @val = split /,/, $val;
458 $user .= "$tok $val";
462 foreach $fref (@$self) {
464 if ($fref->[0] eq $tok) {
468 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
472 if ($fref->[1] eq 'a') {
476 push @t, "\$r->[$fref->[2]]=~/$_/i";
478 $s .= "(" . join(' || ', @t) . ")";
479 } elsif ($fref->[1] eq 'c') {
483 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
485 $s .= "(" . join(' || ', @t) . ")";
486 } elsif ($fref->[1] eq 'n') {
489 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
490 push @t, "\$r->[$fref->[2]]==$_";
492 $s .= "(" . join(' || ', @t) . ")";
493 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
494 my $cmd = $fref->[1];
495 my @pre = Prefix::to_ciz($cmd, @val);
496 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
497 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
498 } elsif ($fref->[1] =~ /^ns$/ ) { # for DXCC, ITU, CQ Zone
499 my $cmd = $fref->[1];
500 my @pre = Prefix::to_ciz($cmd, @val);
501 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
502 $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
503 } elsif ($fref->[1] eq 'r') {
506 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
507 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
509 $s .= "(" . join(' || ', @t) . ")";
510 } elsif ($fref->[1] eq 't') {
514 push @t, "\$r->[$fref->[2]]=~/$_/i";
516 $s .= "(" . join(' || ', @t) . ")";
518 confess("invalid letter $fref->[1]");
524 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
526 return ('no', $dxchan->msg('filter2', $tok));
532 # tidy up the user string
533 $user =~ s/\&\&/ and /g;
534 $user =~ s/\|\|/ or /g;
535 $user =~ s/\!/ not /g;
538 return (0, $filter, $fno, $user, "$s");
541 # a filter accept/reject command
544 my ($self, $dxchan, $sort, $type, $line) = @_;
546 return $dxchan->msg('filter5') unless $line;
548 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
549 my $u = DXUser->get_current($user);
550 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
551 return (1, $filter) if $r;
553 my $fn = "filter$fno";
555 $filter->{$fn} = {} unless exists $filter->{$fn};
556 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
558 $filter->{$fn}->{$type}->{user} = $user;
559 $filter->{$fn}->{$type}->{asc} = $s;
560 $r = $filter->compile($fn, $type);
568 return (0, $filter, $fno);
580 # the OLD instructions!
582 # Each filter file has the same structure:-
586 # [ action, fieldno, fieldsort, comparison, action data ],
590 # The action is usually 1 or 0 but could be any numeric value
592 # The fieldno is the field no in the list of fields that is presented
595 # The fieldsort is the type of field that we are dealing with which
596 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
597 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
599 # Filter::it basically goes thru the list of comparisons from top to
600 # bottom and when one matches it will return the action and the action data as a list.
602 # are the element nos of the list that is presented to Filter::it. Element
603 # 0 is the first field of the list.
607 # takes the reference to the filter (the first argument) and applies
608 # it to the subsequent arguments and returns the action specified.
613 my $filter = $self->{filter}; # this is now a bless ref of course but so what
615 my ($action, $field, $fieldsort, $comp, $actiondata);
618 # default action is 1
621 return ($action, $actiondata) if !$filter;
623 for $ref (@{$filter}) {
624 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
625 if ($fieldsort eq 'n') {
626 my $val = $_[$field];
627 return ($action, $actiondata) if grep $_ == $val, @{$comp};
628 } elsif ($fieldsort eq 'r') {
629 my $val = $_[$field];
631 my @range = @{$comp};
632 for ($i = 0; $i < @range; $i += 2) {
633 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
635 } elsif ($fieldsort eq 'a') {
636 return ($action, $actiondata) if $_[$field] =~ m{$comp};
638 return ($action, $actiondata); # the default action
648 my $flag = shift || "";
649 return "$call: Old Style Filter $flag $sort";