Change DXUser->get* to DXUser::get*
[spider.git] / perl / Filter.pm
index 2c32bf025981465e9ae9b8f374fddeb2fa53527c..b71ee95a5b81833acb4a1fc566592b832071529f 100644 (file)
@@ -10,7 +10,7 @@
 #
 # Copyright (c) 1999 Dirk Koopman G1TLH
 #
-# $Id$
+#
 #
 # The NEW INSTRUCTIONS
 #
@@ -34,12 +34,6 @@ use Prefix;
 
 use strict;
 
-use vars qw($VERSION $BRANCH);
-$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
-$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
-$main::build += $VERSION;
-$main::branch += $BRANCH;
-
 use vars qw ($filterbasefn $in);
 
 $filterbasefn = "$main::root/filter";
@@ -92,7 +86,9 @@ 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};
@@ -114,17 +110,26 @@ sub read_in
                $in = undef; 
                my $s = readfilestr($fn);
                my $newin = eval $s;
-               dbg($@) 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;
        }
@@ -305,7 +310,7 @@ sub install
        } elsif ($name eq 'USER_DEFAULT') {
                @dxchan = DXChannel::get_all_users();
        } else {
-               $dxchan = DXChannel->get($name);
+               $dxchan = DXChannel::get($name);
                push @dxchan, $dxchan if $dxchan;
        }
        foreach $dxchan (@dxchan) {
@@ -368,7 +373,7 @@ sub parse
        my $user;
        
        # check the line for non legal characters
-       return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)]/;
+       return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\-\*\/\(\)!]/;
        
        # add some spaces for ease of parsing
        $line =~ s/([\(\)])/ $1 /g;
@@ -380,7 +385,7 @@ sub parse
        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 (@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;
@@ -444,12 +449,16 @@ sub parse
 
                                if ($s) {
                                        $s .= $conj ;
-                                       $s .= $not;
                                        $user .= $conj;
-                                       $user .= $not;
                                        $conj = ' && ';
-                                       $not = "";
                                }
+
+                               if ($not) {
+                                       $s .= $not;
+                                       $user .= $not;
+                                       $not = '';
+                               }
+
                                $user .= "$tok $val";
                                
                                my $fref;
@@ -486,24 +495,15 @@ sub parse
                                                        }
                                                        $s .= "(" . join(' || ', @t) . ")";
                                                } elsif ($fref->[1] =~ /^n[ciz]$/ ) {    # for DXCC, ITU, CQ Zone    
-                                                       my @n;
                                                        my $cmd = $fref->[1];
-                                                       foreach my $v (@val) {
-                                                               if ($v =~ /^\d+$/) {    
-                                                                       push @n, $v unless grep $_ eq $v, @n;
-                                                               } else {
-                                                                       my @pre = Prefix::extract($v);
-                                                                       return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
-                                                                       shift @pre;
-                                                                       foreach my $p (@pre) {
-                                                                               my $n = $p->dxcc if $cmd eq 'nc' ;
-                                                                               $n = $p->itu if $cmd eq 'ni' ;
-                                                                               $n = $p->cq if $cmd eq 'nz' ;
-                                                                               push @n, $n unless grep $_ eq $n, @n;
-                                                                       }
-                                                               }
-                                                       }
-                                                       $s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @n) . ")";
+                                                       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) {
@@ -550,7 +550,7 @@ sub cmd
        return $dxchan->msg('filter5') unless $line;
 
        my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
-       my $u = DXUser->get_current($user);
+       my $u = DXUser::get_current($user);
        return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
        return (1, $filter) if $r;