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 ($filterbasefn $in);
38 $filterbasefn = "$main::root/filter";
41 # initial filter system
49 my ($class, $sort, $call, $flag) = @_;
50 $flag = ($flag) ? "in_" : "";
51 return bless {sort => $sort, name => "$flag$call.pl" }, $class;
54 # standard filename generator
57 my ($sort, $call, $flag) = @_;
60 $flag = ($flag) ? "in_" : "";
62 my $fn = "$filterbasefn/$sort/$flag$call.pl";
67 $fn = "$filterbasefn/$sort/$flag$call.pl";
69 $fn = undef unless -e $fn;
73 # this reads in a filter statement and returns it as a list
75 # The filter is stored in straight perl so that it can be parsed and read
76 # in with a 'do' statement. The 'do' statement reads the filter into
77 # @in which is a list of references
84 my $ref = $self->{$fname};
87 if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
88 $ref->{$ar}->{code} = eval "sub { my \$r=shift; return $ref->{$ar}->{asc}; }" ;
90 my $sort = $ref->{sort};
91 my $name = $ref->{name};
92 dbg('err', "Error compiling $ar $sort $name: $@");
93 Log('err', "Error compiling $ar $sort $name: $@");
102 my ($sort, $call, $flag) = @_;
106 if ($fn = getfn($sort, $call, $flag)) {
108 my $s = readfilestr($fn);
110 dbg('conn', "$@") if $@;
112 $newin = new('Filter::Old', $sort, $call, $flag);
113 $newin->{filter} = $in;
117 foreach $key ($newin->getfilkeys) {
118 $newin->compile($key, 'reject');
119 $newin->compile($key, 'accept');
132 foreach $key (grep {/^filter/ } keys %$self) {
133 push @out, $self->{$key};
141 return grep {/^filter/ } keys %$self;
145 # This routine accepts a composite filter with a reject rule and then an accept rule.
147 # The filter returns 0 if an entry is matched by any reject rule and also if any
148 # accept rule fails otherwise it returns 1
150 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
151 # a reject then ok else if an accept then not ok.
153 # you can set a default with either an accept/xxxx all or reject/xxxx all
155 # Unlike the old system, this is kept as a hash of hashes so that you can
156 # easily change them by program.
158 # You can have 10 filter lines (0->9), they are tried in order until
161 # There is a parser that takes a Filter::Cmd object which describes all the possible
162 # things you can filter on and then converts that to a bit of perl which is compiled
163 # and stored as a function.
165 # The result of this is that in theory you can put together an arbritrarily complex
166 # expression involving the things you can filter on including 'and' 'or' 'not' and
171 # accept/spots hf and by_zone 14,15,16 and not by pa,on
173 # accept/spots freq 0/30000 and by_zone 4,5
175 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
177 # no filter no implies filter 1
179 # The field nos are the same as for the 'Old' filters
188 my @keys = sort $self->getfilkeys;
190 my $r = @keys > 0 ? 0 : 1;
191 foreach $key (@keys) {
192 $filter = $self->{$key};
193 if ($filter->{reject} && exists $filter->{reject}->{code}) {
194 if (&{$filter->{reject}->{code}}(\@_)) {
201 if ($filter->{accept} && exists $filter->{accept}->{code}) {
202 if (&{$filter->{accept}->{code}}(\@_)) {
211 # hops are done differently (simply)
212 my $hops = $self->{hops} if exists $self->{hops};
217 # this writes out the filter in a form suitable to be read in by 'read_in'
218 # It expects a list of references to filter lines
222 my $sort = $self->{sort};
223 my $name = $self->{name};
224 my $dir = "$filterbasefn/$sort";
225 my $fn = "$dir/$name";
227 mkdir $dir, 0775 unless -e $dir;
228 rename $fn, "$fn.o" if -e $fn;
229 my $fh = new IO::File ">$fn";
231 my $dd = new Data::Dumper([ $self ]);
234 $dd->Quotekeys($] < 5.005 ? 1 : 0);
235 $fh->print($dd->Dumpxs);
238 rename "$fn.o", $fn if -e "$fn.o";
247 my $name = shift || $self->{name};
248 my $sort = shift || $self->{sort};
249 my $flag = shift || "";
253 push @out, join(' ', $name , ':', $sort, $flag);
256 foreach $key (sort $self->getfilkeys) {
257 my $filter = $self->{$key};
258 if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
259 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
261 if (exists $filter->{accept} && exists $filter->{accept}->{user}) {
262 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
272 my $name = uc $self->{name};
273 my $sort = $self->{sort};
274 my $in = "in" if $name =~ s/^IN_//;
277 my $dxchan = DXChannel->get($name);
279 my $n = "$in$sort" . "filter";
280 $dxchan->$n($remove ? undef : $self);
286 my ($sort, $call, $flag, $fno) = @_;
289 my $fn = getfn($sort, $call, $flag);
290 my $filter = read_in($sort, $call, $flag);
294 foreach $key ($filter->getfilkeys) {
295 delete $filter->{$key};
297 } elsif (exists $filter->{"filter$fno"}) {
298 delete $filter->{"filter$fno"};
302 if ($filter->{hops} || $filter->getfilkeys) {
321 # the general purpose command processor
322 # this is called as a subroutine not as a method
325 my ($self, $dxchan, $sort, $line) = @_;
333 # check the line for non legal characters
334 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)]/;
336 # add some spaces for ease of parsing
337 $line =~ s/([\(\)])/ $1 /g;
340 my @f = split /\s+/, $line;
346 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
348 if ($f[0] eq 'input') {
353 $call = $dxchan->call;
356 if (@f && $f[0] =~ /^\d$/) {
360 $filter = Filter::read_in($sort, $call, $flag);
361 $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
367 # do the rest of the filter tokens
384 } elsif ($tok eq ')') {
390 } elsif ($tok eq 'all') {
394 } elsif ($tok eq 'or') {
395 $conj = ' || ' if $conj ne ' || ';
397 } elsif ($tok eq 'and') {
398 $conj = ' && ' if $conj ne ' && ';
400 } elsif ($tok eq 'not' || $tok eq '!') {
406 my @val = split /,/, $val;
416 $user .= "$tok $val";
420 foreach $fref (@$self) {
422 if ($fref->[0] eq $tok) {
426 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
430 if ($fref->[1] eq 'a') {
434 push @t, "\$r->[$fref->[2]]=~/$_/i";
436 $s .= "(" . join(' || ', @t) . ")";
437 } elsif ($fref->[1] eq 'c') {
441 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
443 $s .= "(" . join(' || ', @t) . ")";
444 } elsif ($fref->[1] eq 'n') {
447 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
448 push @t, "\$r->[$fref->[2]]==$_";
450 $s .= "(" . join(' || ', @t) . ")";
451 } elsif ($fref->[1] eq 'r') {
454 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
455 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
457 $s .= "(" . join(' || ', @t) . ")";
458 } elsif ($fref->[1] eq 't') {
462 push @t, "\$r->[$fref->[2]]=~/$_/i";
464 $s .= "(" . join(' || ', @t) . ")";
466 confess("invalid letter $fref->[1]");
472 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
474 return ('no', $dxchan->msg('filter2', $tok));
480 # tidy up the user string
481 $user =~ s/\&\&/ and /g;
482 $user =~ s/\|\|/ or /g;
483 $user =~ s/\!/ not /g;
486 return (0, $filter, $fno, $user, "$s");
489 # a filter accept/reject command
492 my ($self, $dxchan, $sort, $type, $line) = @_;
494 return $dxchan->msg('filter5') unless $line;
496 my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
497 return (1,$filter) if $r;
499 my $fn = "filter$fno";
501 $filter->{$fn} = {} unless exists $filter->{$fn};
502 $filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};
504 $filter->{$fn}->{$type}->{user} = $user;
505 $filter->{$fn}->{$type}->{asc} = $s;
506 $r = $filter->compile($fn, $type);
514 return (0, $filter, $fno);
526 # the OLD instructions!
528 # Each filter file has the same structure:-
532 # [ action, fieldno, fieldsort, comparison, action data ],
536 # The action is usually 1 or 0 but could be any numeric value
538 # The fieldno is the field no in the list of fields that is presented
541 # The fieldsort is the type of field that we are dealing with which
542 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
543 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
545 # Filter::it basically goes thru the list of comparisons from top to
546 # bottom and when one matches it will return the action and the action data as a list.
548 # are the element nos of the list that is presented to Filter::it. Element
549 # 0 is the first field of the list.
553 # takes the reference to the filter (the first argument) and applies
554 # it to the subsequent arguments and returns the action specified.
559 my $filter = $self->{filter}; # this is now a bless ref of course but so what
561 my ($action, $field, $fieldsort, $comp, $actiondata);
564 # default action is 1
567 return ($action, $actiondata) if !$filter;
569 for $ref (@{$filter}) {
570 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
571 if ($fieldsort eq 'n') {
572 my $val = $_[$field];
573 return ($action, $actiondata) if grep $_ == $val, @{$comp};
574 } elsif ($fieldsort eq 'r') {
575 my $val = $_[$field];
577 my @range = @{$comp};
578 for ($i = 0; $i < @range; $i += 2) {
579 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
581 } elsif ($fieldsort eq 'a') {
582 return ($action, $actiondata) if $_[$field] =~ m{$comp};
584 return ($action, $actiondata); # the default action
594 my $flag = shift || "";
595 return "$call: Old Style Filter $flag $sort";