add RBN filtering
[spider.git] / perl / Filter.pm
index fbdb4f300fb579162fa34708504bac792f59a1a9..867c8ddfe73533fea29a6e3b09f5f0dcd106e2d5 100644 (file)
@@ -10,7 +10,7 @@
 #
 # Copyright (c) 1999 Dirk Koopman G1TLH
 #
-# $Id$
+#
 #
 # The NEW INSTRUCTIONS
 #
@@ -30,6 +30,7 @@ use DXVars;
 use DXUtil;
 use DXDebug;
 use Data::Dumper;
+use Prefix;
 
 use strict;
 
@@ -85,11 +86,13 @@ sub compile
        my $rr;
        
        if ($ref->{$ar} && exists $ref->{$ar}->{asc}) {
-               $ref->{$ar}->{code} = eval "sub { my \$r=shift; return $ref->{$ar}->{asc}; }" ;
+               my $s = $ref->{$ar}->{asc};     # an optimisation?
+               $s =~ s/\$r/\$_[0]/g;
+               $ref->{$ar}->{code} = eval "sub { $s }" ;
                if ($@) {
                        my $sort = $ref->{sort};
                        my $name = $ref->{name};
-                       dbg('err', "Error compiling $ar $sort $name: $@");
+                       dbg("Error compiling $ar $sort $name: $@");
                        Log('err', "Error compiling $ar $sort $name: $@");
                }
                $rr = $@;
@@ -107,17 +110,26 @@ sub read_in
                $in = undef; 
                my $s = readfilestr($fn);
                my $newin = eval $s;
-               dbg('conn', "$@") if $@;
+               if ($@) {
+                       dbg($@);
+                       unlink($fn);
+                       return undef;
+               }
                if ($in) {
                        $newin = new('Filter::Old', $sort, $call, $flag);
                        $newin->{filter} = $in;
-               } else {
+               } elsif (ref $newin && $newin->can('getfilkeys')) {
                        my $filter;
                        my $key;
                        foreach $key ($newin->getfilkeys) {
                                $newin->compile($key, 'reject');
                                $newin->compile($key, 'accept');
                        }
+               } else {
+                       # error on reading file, delete and exit
+                       dbg("empty or unreadable filter: $fn, deleted");
+                       unlink($fn);
+                       return undef;
                }
                return $newin;
        }
@@ -187,11 +199,16 @@ sub it
        my $filter;
        my @keys = sort $self->getfilkeys;
        my $key;
+       my $type = 'Dunno';
+       my $asc = '?';
+
        my $r = @keys > 0 ? 0 : 1;
        foreach $key (@keys) {
                $filter = $self->{$key};
                if ($filter->{reject} && exists $filter->{reject}->{code}) {
-                       if (&{$filter->{reject}->{code}}(\@_)) {
+                       $type = 'reject';
+                       $asc = $filter->{reject}->{user};
+                       if (&{$filter->{reject}->{code}}(ref $_[0] ? $_[0] : \@_)) {
                                $r = 0;
                                last;
                        } else {
@@ -199,7 +216,9 @@ sub it
                        }               
                }
                if ($filter->{accept} && exists $filter->{accept}->{code}) {
-                       if (&{$filter->{accept}->{code}}(\@_)) {
+                       $type = 'accept';
+                       $asc = $filter->{accept}->{user};
+                       if (&{$filter->{accept}->{code}}(ref $_[0] ? $_[0] : \@_)) {
                                $r = 1;
                                last;
                        } else {
@@ -211,6 +230,17 @@ sub it
        # hops are done differently (simply) 
        my $hops = $self->{hops} if exists $self->{hops};
 
+       if (isdbg('filter')) {
+               my $call = $self->{name};
+               my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} (ref $_[0] ? @{$_[0]} : @_);
+               my $true = $r ? "OK " : "REJ";
+               my $sort = $self->{sort};
+               my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
+
+               $call =~ s/\.PL$//i;
+               my $h = $hops || '';
+               dbg("Filter: $call $true $dir: $type/$sort with '$asc' on '$args' $h") if isdbg('filter');
+       }
        return ($r, $hops);
 }
 
@@ -271,13 +301,27 @@ sub install
        my $remove = shift;
        my $name = uc $self->{name};
        my $sort = $self->{sort};
-       my $in = "in" if $name =~ s/^IN_//;
+       my $in = "";
+       $in = "in" if $name =~ s/^IN_//;
        $name =~ s/.PL$//;
                
-       my $dxchan = DXChannel->get($name);
-       if ($dxchan) {
+       my $dxchan;
+       my @dxchan;
+       if ($name eq 'NODE_DEFAULT') {
+               @dxchan = DXChannel::get_all_nodes();
+       } elsif ($name eq 'USER_DEFAULT') {
+               @dxchan = DXChannel::get_all_users();
+       } else {
+               $dxchan = DXChannel::get($name);
+               push @dxchan, $dxchan if $dxchan;
+       }
+       foreach $dxchan (@dxchan) {
                my $n = "$in$sort" . "filter";
-               $dxchan->$n($remove ? undef : $self);
+               my $i = $in ? 'IN_' : '';
+               my $ref = $dxchan->$n();
+               if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
+                       $dxchan->$n($remove ? undef : $self);
+               }
        }
 }
 
@@ -322,7 +366,7 @@ use vars qw(@ISA);
 # this is called as a subroutine not as a method
 sub parse
 {
-       my ($self, $dxchan, $sort, $line) = @_;
+       my ($self, $dxchan, $sort, $line, $forcenew) = @_;
        my $ntoken = 0;
        my $fno = 1;
        my $filter;
@@ -331,19 +375,27 @@ sub parse
        my $user;
        
        # check the line for non legal characters
-       return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)]/;
+       dbg("Filter::parse line: '$line'") if isdbg('filter');
+       return ('ill', $dxchan->msg('e19')) if $line !~ /{.*}/ && $line =~ /[^\s\w,_\-\*\/\(\)\$!]/;
+
+       $line = lc $line;
+
+       # disguise regexes
+       $line =~ s/\{(.*)\}/'{'. unpack('H*', $1) . '}'/eg;
+       dbg("Filter parse line after regex check: '$line'") if isdbg('filter');
        
        # add some spaces for ease of parsing
-       $line =~ s/([\(\)])/ $1 /g;
-       $line = lc $line;
+       $line =~ s/([\(\!\)])/ $1 /g;
        
        my @f = split /\s+/, $line;
+
        my $conj = ' && ';
        my $not = "";
+       my $lasttok = '';
        while (@f) {
                if ($ntoken == 0) {
                        
-                       if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
+                       if (!$forcenew &&  @f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser::get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
                                $call = shift @f;
                                if ($f[0] eq 'input') {
                                        shift @f;
@@ -357,7 +409,7 @@ sub parse
                                $fno = shift @f;
                        }
 
-                       $filter = Filter::read_in($sort, $call, $flag);
+                       $filter = Filter::read_in($sort, $call, $flag) unless $forcenew;
                        $filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
                        
                        $ntoken++;
@@ -369,9 +421,12 @@ sub parse
                        my $tok = shift @f;
                        if ($tok eq '(') {
                                if ($s) {
-                                       $s .= $conj;
-                                       $user .= $conj;
+                                       unless ($lasttok eq '(') {
+                                               $s .= $conj ;
+                                               $user .= $conj;
+                                       }
                                        $conj = "";
+                                       $lasttok = $tok;
                                }
                                if ($not) {
                                        $s .= $not;
@@ -380,12 +435,14 @@ sub parse
                                }
                                $s .= $tok;
                                $user .= $tok;
+                               $lasttok = $tok;
                                next;
                        } elsif ($tok eq ')') {
                                $conj = ' && ';
                                $not ="";
                                $s .= $tok;
                                $user .= $tok;
+                               $lasttok = $tok;
                                next;
                        } elsif ($tok eq 'all') {
                                $s .= '1';
@@ -393,12 +450,14 @@ sub parse
                                last;
                        } elsif ($tok eq 'or') {
                                $conj = ' || ' if $conj ne ' || ';
+                               $lasttok = $tok;
                                next;
                        } elsif ($tok eq 'and') {
                                $conj = ' && ' if $conj ne ' && ';
                                next;
                        } elsif ($tok eq 'not' || $tok eq '!') {
-                               $not = '!';
+                               $not = '! ';
+                               $lasttok = $tok;
                                next;
                        }
                        if (@f) {
@@ -406,13 +465,18 @@ sub parse
                                my @val = split /,/, $val;
 
                                if ($s) {
-                                       $s .= $conj ;
+                                       unless ($lasttok eq '(') {
+                                               $s .= $conj ;
+                                               $user .= $conj;
+                                               $conj = ' && ';
+                                       }
+                               }
+                               if ($not) {
                                        $s .= $not;
-                                       $user .= $conj;
                                        $user .= $not;
-                                       $conj = ' && ';
-                                       $not = "";
+                                       $not = '';
                                }
+
                                $user .= "$tok $val";
                                
                                my $fref;
@@ -427,18 +491,24 @@ sub parse
                                                        }
                                                        @val = @nval;
                                                }
-                                               if ($fref->[1] eq 'a') {
+                                               if ($fref->[1] eq 'a' || $fref->[1] eq 't') {
                                                        my @t;
-                                                       for (@val) {
-                                                               s/\*//g;
-                                                               push @t, "\$r->[$fref->[2]]=~/$_/i";
+                                                       foreach my $v (@val) {
+                                                               $v =~ s/\*//g;        # remove any trailing *
+                                                               if (my ($r) = $v =~ /^\{(.*)\}$/) { # we have a regex
+                                                                       dbg("Filter::parse regex b: '\{$r\}'") if isdbg('filter'); 
+                                                                       $v = pack('H*', $r);
+                                                                       dbg("Filter::parse regex a: '$v'") if isdbg('filter'); 
+                                                                       return  ('regex', $dxchan->msg('e38', $v)) unless (qr{$v});
+                                                               }
+                                                               push @t, "\$r->[$fref->[2]]=~m{$v}i";
                                                        }
                                                        $s .= "(" . join(' || ', @t) . ")";
                                                } elsif ($fref->[1] eq 'c') {
                                                        my @t;
                                                        for (@val) {
                                                                s/\*//g;
-                                                               push @t, "\$r->[$fref->[2]]=~/^\U$_/";
+                                                               push @t, "\$r->[$fref->[2]]=~m{^\U$_}";
                                                        }
                                                        $s .= "(" . join(' || ', @t) . ")";
                                                } elsif ($fref->[1] eq 'n') {
@@ -448,6 +518,16 @@ sub parse
                                                                push @t, "\$r->[$fref->[2]]==$_";
                                                        }
                                                        $s .= "(" . join(' || ', @t) . ")";
+                                               } elsif ($fref->[1] =~ /^n[ciz]$/ ) {    # for DXCC, ITU, CQ Zone    
+                                                       my $cmd = $fref->[1];
+                                                       my @pre = Prefix::to_ciz($cmd, @val);
+                                                       return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
+                                                       $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
+                                               } elsif ($fref->[1] =~ /^ns$/ ) {    # for DXCC, ITU, CQ Zone    
+                                                       my $cmd = $fref->[1];
+                                                       my @pre = Prefix::to_ciz($cmd, @val);
+                                                       return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
+                                                       $s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
                                                } elsif ($fref->[1] eq 'r') {
                                                        my @t;
                                                        for (@val) {
@@ -455,26 +535,19 @@ sub parse
                                                                push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
                                                        }
                                                        $s .= "(" . join(' || ', @t) . ")";
-                                               } elsif ($fref->[1] eq 't') {
-                                                       my @t;
-                                                       for (@val) {
-                                                               s/\*//g;
-                                                               push @t, "\$r->[$fref->[2]]=~/$_/i";
-                                                       }
-                                                       $s .= "(" . join(' || ', @t) . ")";
                                                } else {
-                                                       confess("invalid letter $fref->[1]");
+                                                       confess("invalid filter function $fref->[1]");
                                                }
                                                ++$found;
                                                last;
                                        }
                                }
-                               return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
+                               return (1, $dxchan->msg('e20', $tok)) unless $found;
                        } else {
-                               return ('no', $dxchan->msg('filter2', $tok));
+                               return (1, $dxchan->msg('filter2', $tok));
                        }
+                       $lasttok = $tok;
                }
-               
        }
 
        # tidy up the user string
@@ -483,18 +556,20 @@ sub parse
        $user =~ s/\!/ not /g;
        $user =~ s/\s+/ /g;
        
-       return (0, $filter, $fno, $user, "$s");
+       return (0, $filter, $fno, $user, $s);
 }
 
 # a filter accept/reject command
 sub cmd
 {
        my ($self, $dxchan, $sort, $type, $line) = @_;
-       
        return $dxchan->msg('filter5') unless $line;
 
        my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
-       return (1,$filter) if $r;
+       return (1, $filter) if $r;
+       
+       my $u = DXUser::get_current($user);
+       return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
 
        my $fn = "filter$fno";
 
@@ -508,7 +583,8 @@ sub cmd
        
        $r = $filter->write;
        return (1,$r) if $r;
-       
+
+       $filter->install(1);            # 'delete'
        $filter->install;
 
     return (0, $filter, $fno);
@@ -539,8 +615,11 @@ use vars qw(@ISA);
 # to 'Filter::it' 
 #
 # The fieldsort is the type of field that we are dealing with which 
-# currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is 
-# numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
+# currently can be 'a', 'n', 'r' or 'd'.
+#    'a' is alphanumeric
+#    'n' is# numeric
+#    'r' is ranges of pairs of numeric values
+#    'd' is default (effectively, don't filter)
 #
 # Filter::it basically goes thru the list of comparisons from top to
 # bottom and when one matches it will return the action and the action data as a list. 
@@ -579,9 +658,9 @@ sub it
                                return ($action, $actiondata)  if $val >= $range[$i] && $val <= $range[$i+1];
                        }
                } elsif ($fieldsort eq 'a') {
-                       return ($action, $actiondata)  if $_[$field] =~ m{$comp};
+                       return ($action, $actiondata)  if $_[$field] =~ m{$comp}i;
                } else {
-                       return ($action, $actiondata);      # the default action
+                       return ($action, $actiondata);      # the default action (just pass through)
                }
        }
 }