put field 2 check for PC11 back to 'm'
[spider.git] / perl / Filter.pm
index 7884052990c64630dbcdbb355f1b5a150ccb5508..060f7ea8d75e7637b6931ff02032624db26f574f 100644 (file)
@@ -108,13 +108,13 @@ sub read_in
 #      sort => 'spots',
 #      filter1 => {
 #                      user_rej => {
-#                              by_zone => '4,5',
+#                              by_dxcc => 'W,VE',
 #                      },
 #              reject => {
-#                      by_zone => [11, 'n', 4, 5],
+#                      by_dxcc => [6, 'n', 226,197],
 #              },
 #                      user_acc => {
-#                              freq => 'hf',
+#                              freq => '0/30000',
 #                      },
 #              accept => {
 #                      freq => [0, 'r', 0, 30000],
@@ -136,12 +136,14 @@ sub read_in
 #
 #   clear/spots 1 2
 #   accept/spots 1 freq 0/30000
-#   reject/spots 1 by_zone 4,5
+#   reject/spots 1 by_dxcc W,VE
 #   accept/spots 2 freq vhf 
 #   accept/spots 2 by_zone 14,15,16
 #
 # no filter no implies filter 1
 #
+# The field nos are the same as for the 'Old' filters
+#
 # The user_* fields are there so that the structure can be listed easily
 # in human readable form when required. They are not used in the filtering
 # process itself.
@@ -166,14 +168,14 @@ sub it
                                        ($field, $fieldsort) = @$ref[0,1];
                                        my $val = $_[$field];
                                        if ($fieldsort eq 'n') {
-                                               next L1 if grep {$_ == $val} @{$ref}[2..$#$ref];
+                                               next L1 if grep $_ == $val, @{$ref}[2..$#$ref];
                                        } elsif ($fieldsort eq 'r') {
                                                my $i;
                                                for ($i = 2; $i < @$ref; $i += 2) {
                                                        next L1 if $val >= $ref->[$i] && $val <= $ref->[$i+1];
                                                }
                                        } elsif ($fieldsort eq 'a') {
-                                               next L1  if grep { $val =~ m{$_}} @$ref[2..$#$ref];  
+                                               next L1  if grep $val =~ m{$_}, @$ref[2..$#$ref];  
                                        } 
                                }
                        }
@@ -182,14 +184,14 @@ sub it
                                        ($field, $fieldsort) = @$ref[0,1];
                                        my $val = $_[$field];
                                        if ($fieldsort eq 'n') {
-                                               next L1 unless grep {$_ == $val} @{$ref}[2..$#$ref];
+                                               next L1 unless grep $_ == $val, @{$ref}[2..$#$ref];
                                        } elsif ($fieldsort eq 'r') {
                                                my $i;
                                                for ($i = 2; $i < @$ref; $i += 2) {
                                                        next L1 unless $val >= $ref->[$i] && $val <= $ref->[$i+1];
                                                }
                                        } elsif ($fieldsort eq 'a') {
-                                               next L1 unless grep { $val =~ m{$_}} @{$ref}[2..$#$ref];  
+                                               next L1 unless grep $val =~ m{$_}, @{$ref}[2..$#$ref];  
                                        } 
                                }
                        } 
@@ -217,10 +219,13 @@ sub write
 {
        my $self = shift;
        my $sort = $self->{sort};
-       my $fn = $self->{name};
+       my $name = $self->{name};
        my $dir = "$filterbasefn/$sort";
+       my $fn = "$dir/$name";
+       
        mkdir $dir, 0775 unless -e $dir; 
-       my $fh = new IO::File ">$dir/$fn" or return "$dir/$fn $!";
+    rename $fn, "$fn.o" if -e $fn;
+       my $fh = new IO::File ">$fn";
        if ($fh) {
                my $dd = new Data::Dumper([ $self ]);
                $dd->Indent(1);
@@ -228,6 +233,9 @@ sub write
                $dd->Quotekeys($] < 5.005 ? 1 : 0);
                $fh->print($dd->Dumpxs);
                $fh->close;
+       } else {
+               rename "$fn.o", $fn if -e "$fn.o";
+               return "$fn $!";
        }
        return undef;
 }
@@ -238,6 +246,70 @@ sub print
        return $self->{name};
 }
 
+package Filter::Cmd;
+
+use strict;
+use vars qw(@ISA);
+@ISA = qw(Filter);
+
+# the general purpose command processor
+# this is called as a subroutine not as a method
+sub process_cmd
+{
+       my ($self, $dxchan, $line) = @_;
+       my $ntoken = 0;
+       my $fno = 1;
+       my $filter;
+       my ($flag, $call);
+
+       # check the line for non legal characters
+       return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\/]/;
+       
+       while (my @f = split /\s+/, $line) {
+               if ($ntoken == 0) {
+                       
+                       if (@f && $dxchan->priv >= 9 && DXUser->get($f[0])) {
+                               $call = shift @f;
+                               if ($f[0] eq 'input') {
+                                       shift @f;
+                                       $flag++;
+                               }
+                       } else {
+                               $call = $dxchan->call;
+                       }
+
+                       if (@f && $f[0] =~ /^\d+$/) {
+                               $fno = shift @f;
+                       }
+
+                       $filter = Filter::read_in('spots', $call, $flag) or new Filter ('spots', $call, $flag);
+                       
+                       $ntoken++;
+                       next;
+               }
+
+               # do the rest of the filter tokens
+               if (@f) {
+                       my $tok = shift @f;
+                       if (@f) {
+                               my $val = shift @f;
+
+                               my $fref;
+                               foreach $fref (@$self) {
+                                       if ($fref->[0] eq $tok) {
+                                               
+                                       }
+                               }
+                       } else {
+                               return ('no', $dxchan->msg('filter2', $tok));
+                       }
+               }
+               
+       }
+       $flag = $flag ? "in_" : "";
+       return (0, $dxchan->msg('filter1', $fno, "$flag$call"));
+}
+
 package Filter::Old;
 
 use strict;