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.
36 use vars qw($VERSION $BRANCH);
37 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
38 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
39 $main::build += $VERSION;
40 $main::branch += $BRANCH;
42 use vars qw ($filterbasefn $in);
44 $filterbasefn = "$main::root/filter";
47 # initial filter system
55 my ($class, $sort, $call, $flag) = @_;
56 $flag = ($flag) ? "in_" : "";
57 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
60 # standard filename generator
63 my ($sort, $call, $flag) = @_;
66 $flag = ($flag) ? "in_" : "";
68 my $fn = "$filterbasefn/$sort/$flag$call.pl";
73 $fn = "$filterbasefn/$sort/$flag$call.pl";
75 $fn = undef unless -e $fn;
79 # this reads in a filter statement and returns it as a list
81 # The filter is stored in straight perl so that it can be parsed and read
82 # in with a 'do' statement. The 'do' statement reads the filter into
83 # @in which is a list of references
90 my $ref = $self->{$fname};
93 if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
94 $ref->{$ar}->{code} = eval "sub { my \$r=shift; return $ref->{$ar}->{asc}; }" ;
96 my $sort = $ref->{sort};
97 my $name = $ref->{name};
98 dbg("Error compiling $ar $sort $name: $@");
99 Log('err', "Error compiling $ar $sort $name: $@");
108 my ($sort, $call, $flag) = @_;
112 if ($fn = getfn($sort, $call, $flag)) {
114 my $s = readfilestr($fn);
118 $newin = new('Filter::Old', $sort, $call, $flag);
119 $newin->{filter} = $in;
123 foreach $key ($newin->getfilkeys) {
124 $newin->compile($key, 'reject');
125 $newin->compile($key, 'accept');
138 foreach $key (grep {/^filter/ } keys %$self) {
139 push @out, $self->{$key};
147 return grep {/^filter/ } keys %$self;
151 # This routine accepts a composite filter with a reject rule and then an accept rule.
153 # The filter returns 0 if an entry is matched by any reject rule and also if any
154 # accept rule fails otherwise it returns 1
156 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
157 # a reject then ok else if an accept then not ok.
159 # you can set a default with either an accept/xxxx all or reject/xxxx all
161 # Unlike the old system, this is kept as a hash of hashes so that you can
162 # easily change them by program.
164 # You can have 10 filter lines (0->9), they are tried in order until
167 # There is a parser that takes a Filter::Cmd object which describes all the possible
168 # things you can filter on and then converts that to a bit of perl which is compiled
169 # and stored as a function.
171 # The result of this is that in theory you can put together an arbritrarily complex
172 # expression involving the things you can filter on including 'and' 'or' 'not' and
177 # accept/spots hf and by_zone 14,15,16 and not by pa,on
179 # accept/spots freq 0/30000 and by_zone 4,5
181 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
183 # no filter no implies filter 1
185 # The field nos are the same as for the 'Old' filters
194 my @keys = sort $self->getfilkeys;
199 my $r = @keys > 0 ? 0 : 1;
200 foreach $key (@keys) {
201 $filter = $self->{$key};
202 if ($filter->{reject} && exists $filter->{reject}->{code}) {
204 $asc = $filter->{reject}->{user};
205 if (&{$filter->{reject}->{code}}(\@_)) {
212 if ($filter->{accept} && exists $filter->{accept}->{code}) {
214 $asc = $filter->{accept}->{user};
215 if (&{$filter->{accept}->{code}}(\@_)) {
224 # hops are done differently (simply)
225 my $hops = $self->{hops} if exists $self->{hops};
227 if (isdbg('filter')) {
228 my $args = join '\',\'', @_;
229 my $true = $r ? "OK " : "REJ";
230 my $sort = $self->{sort};
231 my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
234 dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
239 # this writes out the filter in a form suitable to be read in by 'read_in'
240 # It expects a list of references to filter lines
244 my $sort = $self->{sort};
245 my $name = $self->{name};
246 my $dir = "$filterbasefn/$sort";
247 my $fn = "$dir/$name";
249 mkdir $dir, 0775 unless -e $dir;
250 rename $fn, "$fn.o" if -e $fn;
251 my $fh = new IO::File ">$fn";
253 my $dd = new Data::Dumper([ $self ]);
256 $dd->Quotekeys($] < 5.005 ? 1 : 0);
257 $fh->print($dd->Dumpxs);
260 rename "$fn.o", $fn if -e "$fn.o";
269 my $name = shift || $self->{name};
270 my $sort = shift || $self->{sort};
271 my $flag = shift || "";
275 push @out, join(' ', $name , ':', $sort, $flag);
278 foreach $key (sort $self->getfilkeys) {
279 my $filter = $self->{$key};
280 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
281 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
283 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
284 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
294 my $name = uc $self->{name};
295 my $sort = $self->{sort};
297 $in = "in" if $name =~ s/^IN_//;
300 my $dxchan = DXChannel->get($name);
302 my $n = "$in$sort" . "filter";
303 $dxchan->$n($remove ? undef : $self);
309 my ($sort, $call, $flag, $fno) = @_;
312 my $fn = getfn($sort, $call, $flag);
313 my $filter = read_in($sort, $call, $flag);
317 foreach $key ($filter->getfilkeys) {
318 delete $filter->{$key};
320 } elsif (exists $filter->{"filter$fno"}) {
321 delete $filter->{"filter$fno"};
325 if ($filter->{hops} || $filter->getfilkeys) {
344 # the general purpose command processor
345 # this is called as a subroutine not as a method
348 my ($self, $dxchan, $sort, $line) = @_;
356 # check the line for non legal characters
357 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)]/;
359 # add some spaces for ease of parsing
360 $line =~ s/([\(\)])/ $1 /g;
363 my @f = split /\s+/, $line;
369 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
371 if ($f[0] eq 'input') {
376 $call = $dxchan->call;
379 if (@f && $f[0] =~ /^\d$/) {
383 $filter = Filter::read_in($sort, $call, $flag);
384 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
390 # do the rest of the filter tokens
407 } elsif ($tok eq ')') {
413 } elsif ($tok eq 'all') {
417 } elsif ($tok eq 'or') {
418 $conj = ' || ' if $conj ne ' || ';
420 } elsif ($tok eq 'and') {
421 $conj = ' && ' if $conj ne ' && ';
423 } elsif ($tok eq 'not' || $tok eq '!') {
429 my @val = split /,/, $val;
439 $user .= "$tok $val";
443 foreach $fref (@$self) {
445 if ($fref->[0] eq $tok) {
449 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
453 if ($fref->[1] eq 'a') {
457 push @t, "\$r->[$fref->[2]]=~/$_/i";
459 $s .= "(" . join(' || ', @t) . ")";
460 } elsif ($fref->[1] eq 'c') {
464 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
466 $s .= "(" . join(' || ', @t) . ")";
467 } elsif ($fref->[1] eq 'n') {
470 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
471 push @t, "\$r->[$fref->[2]]==$_";
473 $s .= "(" . join(' || ', @t) . ")";
474 } elsif ($fref->[1] eq 'r') {
477 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
478 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
480 $s .= "(" . join(' || ', @t) . ")";
481 } elsif ($fref->[1] eq 't') {
485 push @t, "\$r->[$fref->[2]]=~/$_/i";
487 $s .= "(" . join(' || ', @t) . ")";
489 confess("invalid letter $fref->[1]");
495 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
497 return ('no', $dxchan->msg('filter2', $tok));
503 # tidy up the user string
504 $user =~ s/\&\&/ and /g;
505 $user =~ s/\|\|/ or /g;
506 $user =~ s/\!/ not /g;
509 return (0, $filter, $fno, $user, "$s");
512 # a filter accept/reject command
515 my ($self, $dxchan, $sort, $type, $line) = @_;
517 return $dxchan->msg('filter5') unless $line;
519 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
520 return (1,$filter) if $r;
522 my $fn = "filter$fno";
524 $filter->{$fn} = {} unless exists $filter->{$fn};
525 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
527 $filter->{$fn}->{$type}->{user} = $user;
528 $filter->{$fn}->{$type}->{asc} = $s;
529 $r = $filter->compile($fn, $type);
537 return (0, $filter, $fno);
549 # the OLD instructions!
551 # Each filter file has the same structure:-
555 # [ action, fieldno, fieldsort, comparison, action data ],
559 # The action is usually 1 or 0 but could be any numeric value
561 # The fieldno is the field no in the list of fields that is presented
564 # The fieldsort is the type of field that we are dealing with which
565 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
566 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
568 # Filter::it basically goes thru the list of comparisons from top to
569 # bottom and when one matches it will return the action and the action data as a list.
571 # are the element nos of the list that is presented to Filter::it. Element
572 # 0 is the first field of the list.
576 # takes the reference to the filter (the first argument) and applies
577 # it to the subsequent arguments and returns the action specified.
582 my $filter = $self->{filter}; # this is now a bless ref of course but so what
584 my ($action, $field, $fieldsort, $comp, $actiondata);
587 # default action is 1
590 return ($action, $actiondata) if !$filter;
592 for $ref (@{$filter}) {
593 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
594 if ($fieldsort eq 'n') {
595 my $val = $_[$field];
596 return ($action, $actiondata) if grep $_ == $val, @{$comp};
597 } elsif ($fieldsort eq 'r') {
598 my $val = $_[$field];
600 my @range = @{$comp};
601 for ($i = 0; $i < @range; $i += 2) {
602 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
604 } elsif ($fieldsort eq 'a') {
605 return ($action, $actiondata) if $_[$field] =~ m{$comp};
607 return ($action, $actiondata); # the default action
617 my $flag = shift || "";
618 return "$call: Old Style Filter $flag $sort";