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;
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 $ref->{$ar}->{code} = eval "sub { my \$r=shift; return $ref->{$ar}->{asc}; }" ;
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;
200 my $r = @keys > 0 ? 0 : 1;
201 foreach $key (@keys) {
202 $filter = $self->{$key};
203 if ($filter->{reject} && exists $filter->{reject}->{code}) {
205 $asc = $filter->{reject}->{user};
206 if (&{$filter->{reject}->{code}}(\@_)) {
213 if ($filter->{accept} && exists $filter->{accept}->{code}) {
215 $asc = $filter->{accept}->{user};
216 if (&{$filter->{accept}->{code}}(\@_)) {
225 # hops are done differently (simply)
226 my $hops = $self->{hops} if exists $self->{hops};
228 if (isdbg('filter')) {
229 my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} @_;
230 my $true = $r ? "OK " : "REJ";
231 my $sort = $self->{sort};
232 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
235 dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
240 # this writes out the filter in a form suitable to be read in by 'read_in'
241 # It expects a list of references to filter lines
245 my $sort = $self->{sort};
246 my $name = $self->{name};
247 my $dir = "$filterbasefn/$sort";
248 my $fn = "$dir/$name";
250 mkdir $dir, 0775 unless -e $dir;
251 rename $fn, "$fn.o" if -e $fn;
252 my $fh = new IO::File ">$fn";
254 my $dd = new Data::Dumper([ $self ]);
257 $dd->Quotekeys($] < 5.005 ? 1 : 0);
258 $fh->print($dd->Dumpxs);
261 rename "$fn.o", $fn if -e "$fn.o";
270 my $name = shift || $self->{name};
271 my $sort = shift || $self->{sort};
272 my $flag = shift || "";
276 push @out, join(' ', $name , ':', $sort, $flag);
279 foreach $key (sort $self->getfilkeys) {
280 my $filter = $self->{$key};
281 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
282 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
284 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
285 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
295 my $name = uc $self->{name};
296 my $sort = $self->{sort};
298 $in = "in" if $name =~ s/^IN_//;
303 if ($name eq 'NODE_DEFAULT') {
304 @dxchan = DXChannel::get_all_nodes();
305 } elsif ($name eq 'USER_DEFAULT') {
306 @dxchan = DXChannel::get_all_users();
308 $dxchan = DXChannel->get($name);
309 push @dxchan, $dxchan if $dxchan;
311 foreach $dxchan (@dxchan) {
312 my $n = "$in$sort" . "filter";
313 my $i = $in ? 'IN_' : '';
314 my $ref = $dxchan->$n();
315 if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
316 $dxchan->$n($remove ? undef : $self);
323 my ($sort, $call, $flag, $fno) = @_;
326 my $fn = getfn($sort, $call, $flag);
327 my $filter = read_in($sort, $call, $flag);
331 foreach $key ($filter->getfilkeys) {
332 delete $filter->{$key};
334 } elsif (exists $filter->{"filter$fno"}) {
335 delete $filter->{"filter$fno"};
339 if ($filter->{hops} || $filter->getfilkeys) {
358 # the general purpose command processor
359 # this is called as a subroutine not as a method
362 my ($self, $dxchan, $sort, $line) = @_;
370 # check the line for non legal characters
371 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)!]/;
373 # add some spaces for ease of parsing
374 $line =~ s/([\(\)])/ $1 /g;
377 my @f = split /\s+/, $line;
383 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
385 if ($f[0] eq 'input') {
390 $call = $dxchan->call;
393 if (@f && $f[0] =~ /^\d$/) {
397 $filter = Filter::read_in($sort, $call, $flag);
398 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
404 # do the rest of the filter tokens
421 } elsif ($tok eq ')') {
427 } elsif ($tok eq 'all') {
431 } elsif ($tok eq 'or') {
432 $conj = ' || ' if $conj ne ' || ';
434 } elsif ($tok eq 'and') {
435 $conj = ' && ' if $conj ne ' && ';
437 } elsif ($tok eq 'not' || $tok eq '!') {
443 my @val = split /,/, $val;
457 $user .= "$tok $val";
461 foreach $fref (@$self) {
463 if ($fref->[0] eq $tok) {
467 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
471 if ($fref->[1] eq 'a') {
475 push @t, "\$r->[$fref->[2]]=~/$_/i";
477 $s .= "(" . join(' || ', @t) . ")";
478 } elsif ($fref->[1] eq 'c') {
482 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
484 $s .= "(" . join(' || ', @t) . ")";
485 } elsif ($fref->[1] eq 'n') {
488 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
489 push @t, "\$r->[$fref->[2]]==$_";
491 $s .= "(" . join(' || ', @t) . ")";
492 } elsif ($fref->[1] =~ /^n[ciz]$/ ) { # for DXCC, ITU, CQ Zone
494 my $cmd = $fref->[1];
495 foreach my $v (@val) {
497 push @n, $v unless grep $_ eq $v, @n;
499 my @pre = Prefix::extract($v);
500 return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
502 foreach my $p (@pre) {
503 my $n = $p->dxcc if $cmd eq 'nc' ;
504 $n = $p->itu if $cmd eq 'ni' ;
505 $n = $p->cq if $cmd eq 'nz' ;
506 push @n, $n unless grep $_ eq $n, @n;
510 $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @n) . ")";
511 } elsif ($fref->[1] eq 'r') {
514 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
515 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
517 $s .= "(" . join(' || ', @t) . ")";
518 } elsif ($fref->[1] eq 't') {
522 push @t, "\$r->[$fref->[2]]=~/$_/i";
524 $s .= "(" . join(' || ', @t) . ")";
526 confess("invalid letter $fref->[1]");
532 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
534 return ('no', $dxchan->msg('filter2', $tok));
540 # tidy up the user string
541 $user =~ s/\&\&/ and /g;
542 $user =~ s/\|\|/ or /g;
543 $user =~ s/\!/ not /g;
546 return (0, $filter, $fno, $user, "$s");
549 # a filter accept/reject command
552 my ($self, $dxchan, $sort, $type, $line) = @_;
554 return $dxchan->msg('filter5') unless $line;
556 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
557 my $u = DXUser->get_current($user);
558 return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
559 return (1, $filter) if $r;
561 my $fn = "filter$fno";
563 $filter->{$fn} = {} unless exists $filter->{$fn};
564 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
566 $filter->{$fn}->{$type}->{user} = $user;
567 $filter->{$fn}->{$type}->{asc} = $s;
568 $r = $filter->compile($fn, $type);
576 return (0, $filter, $fno);
588 # the OLD instructions!
590 # Each filter file has the same structure:-
594 # [ action, fieldno, fieldsort, comparison, action data ],
598 # The action is usually 1 or 0 but could be any numeric value
600 # The fieldno is the field no in the list of fields that is presented
603 # The fieldsort is the type of field that we are dealing with which
604 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
605 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
607 # Filter::it basically goes thru the list of comparisons from top to
608 # bottom and when one matches it will return the action and the action data as a list.
610 # are the element nos of the list that is presented to Filter::it. Element
611 # 0 is the first field of the list.
615 # takes the reference to the filter (the first argument) and applies
616 # it to the subsequent arguments and returns the action specified.
621 my $filter = $self->{filter}; # this is now a bless ref of course but so what
623 my ($action, $field, $fieldsort, $comp, $actiondata);
626 # default action is 1
629 return ($action, $actiondata) if !$filter;
631 for $ref (@{$filter}) {
632 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
633 if ($fieldsort eq 'n') {
634 my $val = $_[$field];
635 return ($action, $actiondata) if grep $_ == $val, @{$comp};
636 } elsif ($fieldsort eq 'r') {
637 my $val = $_[$field];
639 my @range = @{$comp};
640 for ($i = 0; $i < @range; $i += 2) {
641 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
643 } elsif ($fieldsort eq 'a') {
644 return ($action, $actiondata) if $_[$field] =~ m{$comp};
646 return ($action, $actiondata); # the default action
656 my $flag = shift || "";
657 return "$call: Old Style Filter $flag $sort";