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
81 my ($sort, $call, $flag) = @_;
85 if ($fn = getfn($sort, $call, $flag)) {
87 my $s = readfilestr($fn);
89 dbg('conn', "$@") if $@;
91 $newin = new('Filter::Old', $sort, $call, $flag);
92 $newin->{filter} = $in;
96 foreach $key ($newin->getfilkeys) {
97 $filter = $newin->{$key};
98 if ($filter->{reject} && exists $filter->{reject}->{asc}) {
99 $filter->{reject}->{code} = eval $filter->{reject}->{asc} ;
101 my $sort = $newin->{sort};
102 my $name = $newin->{name};
103 dbg('err', "Error compiling reject $sort $key $name: $@");
104 Log('err', "Error compiling reject $sort $key $name: $@");
107 if ($filter->{accept} && exists $filter->{accept}->{asc}) {
108 $filter->{accept}->{code} = eval $filter->{accept}->{asc} ;
110 my $sort = $newin->{sort};
111 my $name = $newin->{name};
112 dbg('err', "Error compiling accept $sort $key $name: $@");
113 Log('err', "Error compiling accept $sort $key $name: $@");
128 foreach $key (grep {/^filter/ } keys %$self) {
129 push @out, $self->{$key};
137 return grep {/^filter/ } keys %$self;
141 # This routine accepts a composite filter with a reject rule and then an accept rule.
143 # The filter returns 0 if an entry is matched by any reject rule and also if any
144 # accept rule fails otherwise it returns 1
146 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
147 # a reject then ok else if an accept then not ok.
149 # you can set a default with either an accept/xxxx all or reject/xxxx all
151 # Unlike the old system, this is kept as a hash of hashes so that you can
152 # easily change them by program.
154 # You can have 10 filter lines (0->9), they are tried in order until
157 # There is a parser that takes a Filter::Cmd object which describes all the possible
158 # things you can filter on and then converts that to a bit of perl which is compiled
159 # and stored as a function.
161 # The result of this is that in theory you can put together an arbritrarily complex
162 # expression involving the things you can filter on including 'and' 'or' 'not' and
167 # accept/spots hf and by_zone 14,15,16 and not by pa,on
169 # accept/spots freq 0/30000 and by_zone 4,5
171 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
173 # no filter no implies filter 1
175 # The field nos are the same as for the 'Old' filters
186 my @keys = sort $self->getfilkeys;
188 my $r = @keys > 0 ? 0 : 1;
189 foreach $key (@keys) {
190 $filter = $self->{$key};
191 if ($filter->{reject} && exists $filter->{reject}->{code}) {
192 if (&{$filter->{reject}->{code}}(\@_)) {
199 if ($filter->{accept} && exists $filter->{accept}->{code}) {
200 if (&{$filter->{accept}->{code}}(\@_)) {
209 # hops are done differently
212 while (($comp, $ref) = each %{$self->{hops}}) {
213 my ($field, $h) = @$ref;
214 if ($_[$field] =~ m{$comp}) {
223 # this writes out the filter in a form suitable to be read in by 'read_in'
224 # It expects a list of references to filter lines
228 my $sort = $self->{sort};
229 my $name = $self->{name};
230 my $dir = "$filterbasefn/$sort";
231 my $fn = "$dir/$name";
233 mkdir $dir, 0775 unless -e $dir;
234 rename $fn, "$fn.o" if -e $fn;
235 my $fh = new IO::File ">$fn";
237 my $dd = new Data::Dumper([ $self ]);
240 $dd->Quotekeys($] < 5.005 ? 1 : 0);
241 $fh->print($dd->Dumpxs);
244 rename "$fn.o", $fn if -e "$fn.o";
254 my $name = $self->{name};
257 push @out, join(' ', $name , ':', $self->{sort});
260 foreach $key (sort $self->getfilkeys) {
261 my $filter = $self->{$key};
262 if ($filter->{reject} && exists $filter->{reject}->{user}) {
263 push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
265 if ($filter->{accept} && exists $filter->{accept}->{user}) {
266 push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
276 my $name = uc $self->{name};
277 my $sort = $self->{sort};
278 my ($in) = $name =~ s/^IN_//;
281 my $dxchan = DXChannel->get($name);
284 my $n = "$in$sort" . "filter";
285 $dxchan->$n($remove ? undef : $self);
291 my ($sort, $call, $flag, $fno) = @_;
294 my $fn = getfn($sort, $call, $flag);
295 my $filter = read_in($sort, $call, $flag);
299 foreach $key ($filter->getfilkeys) {
300 delete $filter->{$key};
302 } elsif (exists $filter->{"filter$fno"}) {
303 delete $filter->{"filter$fno"};
307 if ($filter->{hops} || $filter->getfilkeys) {
326 # the general purpose command processor
327 # this is called as a subroutine not as a method
330 my ($self, $dxchan, $line) = @_;
338 # check the line for non legal characters
339 return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\*\/\(\)]/;
341 # add some spaces for ease of parsing
342 $line =~ s/([\(\)])/ $1 /g;
345 my @f = split /\s+/, $line;
351 if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
353 if ($f[0] eq 'input') {
358 $call = $dxchan->call;
361 if (@f && $f[0] =~ /^\d$/) {
365 $filter = Filter::read_in('spots', $call, $flag);
366 $filter = Filter->new('spots', $call, $flag) unless $filter;
372 # do the rest of the filter tokens
389 } elsif ($tok eq ')') {
395 } elsif ($tok eq 'all') {
399 } elsif ($tok eq 'or') {
400 $conj = ' || ' if $conj ne ' || ';
402 } elsif ($tok eq 'and') {
403 $conj = ' && ' if $conj ne ' && ';
405 } elsif ($tok eq 'not' || $tok eq '!') {
411 my @val = split /,/, $val;
421 $user .= "$tok $val";
425 foreach $fref (@$self) {
427 if ($fref->[0] eq $tok) {
431 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
435 if ($fref->[1] eq 'a') {
439 push @t, "\$r->[$fref->[2]]=~/$_/i";
441 $s .= "(" . join(' || ', @t) . ")";
442 } elsif ($fref->[1] eq 'c') {
446 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
448 $s .= "(" . join(' || ', @t) . ")";
449 } elsif ($fref->[1] eq 'n') {
452 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
453 push @t, "\$r->[$fref->[2]]==$_";
455 $s .= "(" . join(' || ', @t) . ")";
456 } elsif ($fref->[1] eq 'r') {
459 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
460 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
462 $s .= "(" . join(' || ', @t) . ")";
464 confess("invalid letter $fref->[1]");
470 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
472 return ('no', $dxchan->msg('filter2', $tok));
478 # tidy up the user string
479 $user =~ s/\&\&/ and /g;
480 $user =~ s/\|\|/ or /g;
481 $user =~ s/\!/ not /g;
484 return (0, $filter, $fno, $user, "sub { my \$r = shift; return ($s) ? 1 : 0 }");
496 # the OLD instructions!
498 # Each filter file has the same structure:-
502 # [ action, fieldno, fieldsort, comparison, action data ],
506 # The action is usually 1 or 0 but could be any numeric value
508 # The fieldno is the field no in the list of fields that is presented
511 # The fieldsort is the type of field that we are dealing with which
512 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
513 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
515 # Filter::it basically goes thru the list of comparisons from top to
516 # bottom and when one matches it will return the action and the action data as a list.
518 # are the element nos of the list that is presented to Filter::it. Element
519 # 0 is the first field of the list.
523 # takes the reference to the filter (the first argument) and applies
524 # it to the subsequent arguments and returns the action specified.
529 my $filter = $self->{filter}; # this is now a bless ref of course but so what
531 my ($action, $field, $fieldsort, $comp, $actiondata);
534 # default action is 1
537 return ($action, $actiondata) if !$filter;
539 for $ref (@{$filter}) {
540 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
541 if ($fieldsort eq 'n') {
542 my $val = $_[$field];
543 return ($action, $actiondata) if grep $_ == $val, @{$comp};
544 } elsif ($fieldsort eq 'r') {
545 my $val = $_[$field];
547 my @range = @{$comp};
548 for ($i = 0; $i < @range; $i += 2) {
549 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
551 } elsif ($fieldsort eq 'a') {
552 return ($action, $actiondata) if $_[$field] =~ m{$comp};
554 return ($action, $actiondata); # the default action