return bless {sort => $sort, name => "$flag$call.pl" }, $class;
}
-# this reads in a filter statement and returns it as a list
-#
-# The filter is stored in straight perl so that it can be parsed and read
-# in with a 'do' statement. The 'do' statement reads the filter into
-# @in which is a list of references
-#
-sub read_in
+# standard filename generator
+sub getfn
{
my ($sort, $call, $flag) = @_;
$call = lc $call;
$fn = "$filterbasefn/$sort/$flag$call.pl";
}
+ $fn = undef unless -e $fn;
+ return $fn;
+}
+
+# this reads in a filter statement and returns it as a list
+#
+# The filter is stored in straight perl so that it can be parsed and read
+# in with a 'do' statement. The 'do' statement reads the filter into
+# @in which is a list of references
+#
+sub read_in
+{
+ my ($sort, $call, $flag) = @_;
+ my $fn;
# load it
- if (-e $fn) {
+ if ($fn = getfn($sort, $call, $flag)) {
$in = undef;
my $s = readfilestr($fn);
my $newin = eval $s;
if ($in) {
$newin = new('Filter::Old', $sort, $call, $flag);
$newin->{filter} = $in;
+ } else {
+ my $filter;
+ my $key;
+ foreach $key ($newin->getfilkeys) {
+ $filter = $newin->{$key};
+ if ($filter->{reject} && exists $filter->{reject}->{asc}) {
+ $filter->{reject}->{code} = eval $filter->{reject}->{asc} ;
+ if ($@) {
+ my $sort = $newin->{sort};
+ my $name = $newin->{name};
+ dbg('err', "Error compiling reject $sort $key $name: $@");
+ Log('err', "Error compiling reject $sort $key $name: $@");
+ }
+ }
+ if ($filter->{accept} && exists $filter->{accept}->{asc}) {
+ $filter->{accept}->{code} = eval $filter->{accept}->{asc} ;
+ if ($@) {
+ my $sort = $newin->{sort};
+ my $name = $newin->{name};
+ dbg('err', "Error compiling accept $sort $key $name: $@");
+ Log('err', "Error compiling accept $sort $key $name: $@");
+ }
+ }
+ }
}
return $newin;
}
return undef;
}
+sub getfilters
+{
+ my $self = shift;
+ my @out;
+ my $key;
+ foreach $key (grep {/^filter/ } keys %$self) {
+ push @out, $self->{$key};
+ }
+ return @out;
+}
+
+sub getfilkeys
+{
+ my $self = shift;
+ return grep {/^filter/ } keys %$self;
+}
+
#
-# this routine accepts a composite filter with a reject component and then an accept
-# the filter returns 0 if an entry is matched by any reject rule and also if any
-# accept rule fails otherwise it returns 1
+# This routine accepts a composite filter with a reject rule and then an accept rule.
#
-# the either set of rules may be missing meaning an implicit 'ok'
+# The filter returns 0 if an entry is matched by any reject rule and also if any
+# accept rule fails otherwise it returns 1
#
-# reject rules are implicitly 'or' logic (any reject rules which fires kicks it out)
-# accept rules are implicitly 'and' logic (all accept rules must pass to indicate a match)
+# Either set of rules may be missing meaning an implicit 'ok'
#
-# unlike the old system, this is kept as a hash of hashes so that you can
+# Unlike the old system, this is kept as a hash of hashes so that you can
# easily change them by program.
#
-# you can have a [any] number of 'filters', they are tried in random order until one matches
-#
-# an example in machine readable form:-
-# bless ({
-# name => 'G7BRN.pl',
-# sort => 'spots',
-# filter1 => {
-# user_rej => {
-# by_dxcc => 'W,VE',
-# },
-# reject => {
-# by_dxcc => [6, 'n', 226,197],
-# },
-# user_acc => {
-# freq => '0/30000',
-# },
-# accept => {
-# freq => [0, 'r', 0, 30000],
-# },
-# },
-# filter2 => {
-# user_acc => {
-# freq => 'vhf',
-# by_zone => '14,15,16',
-# },
-# accept => {
-# freq => [0, 'r', 50000,52000,70000,70500,144000,148000],
-# by_zone => [11, 'n', 14,15,16],
-# }
-# },
-# }, 'Filter');
-#
-# in user commands:-
-#
-# clear/spots 1 2
-# accept/spots 1 freq 0/30000
-# reject/spots 1 by_dxcc W,VE
-# accept/spots 2 freq vhf
-# accept/spots 2 by_zone 14,15,16
+# You can have a [any] number of 'filters', they are tried in random order until
+# one matches
+#
+# There is a parser that takes a Filter::Cmd object which describes all the possible
+# things you can filter on and then converts that to a bit of perl which is compiled
+# and stored as a function.
+#
+# The result of this is that in theory you can put together an arbritrarily complex
+# expression involving the things you can filter on including 'and' 'or' 'not' and
+# 'brackets'.
+#
+# eg:-
+#
+# accept/spots hf and by_zone 14,15,16 and not by pa,on
+#
+# accept/spots freq 0/30000 and by_zone 4,5
+#
+# accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61)
#
# 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.
-#
-# This defines an HF filter and a VHF filter (as it happens)
#
sub it
my $self = shift;
my $hops = undef;
- my $filter;
- my $r;
+ my $r = 1;
- my ($key, $ref, $field, $fieldsort, $comp);
- L1: foreach $key (grep {/^filter/ } keys %$self) {
- my $filter = $self->{$key};
- $r = 0;
- if ($filter->{reject}) {
- foreach $ref (values %{$filter->{reject}}) {
- ($field, $fieldsort) = @$ref[0,1];
- my $val = $_[$field];
- if ($fieldsort eq 'n') {
- 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];
- }
- }
- }
- if ($filter->{accept}) {
- foreach $ref (values %{$filter->{accept}}) {
- ($field, $fieldsort) = @$ref[0,1];
- my $val = $_[$field];
- if ($fieldsort eq 'n') {
- 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];
- }
- }
- }
- $r = 1;
- last;
+ my $filter;
+ foreach $filter ($self->getfilters) {
+ $r = 0;
+ if ($filter->{reject} && exists $filter->{reject}->{code}) {
+ next if &{$filter->{reject}->{code}}(\@_);
+ }
+ if ($filter->{accept} && exists $filter->{accept}->{code}) {
+ next unless &{$filter->{accept}->{code}}(\@_);
+ }
+ $r = 1;
+ last;
}
# hops are done differently
if ($self->{hops}) {
- my $h;
+ my ($comp, $ref);
while (($comp, $ref) = each %{$self->{hops}}) {
- ($field, $h) = @$ref;
+ my ($field, $h) = @$ref;
if ($_[$field] =~ m{$comp}) {
$hops = $h;
last;
my $name = $self->{name};
my $dir = "$filterbasefn/$sort";
my $fn = "$dir/$name";
-
+
mkdir $dir, 0775 unless -e $dir;
rename $fn, "$fn.o" if -e $fn;
my $fh = new IO::File ">$fn";
sub print
{
my $self = shift;
- return $self->{name};
+ my @out;
+ my $name = $self->{name};
+ $name =~ s/.pl$//;
+
+ push @out, join(' ', $name , ':', $self->{sort});
+ my $filter;
+ my $key;
+ foreach $key (sort $self->getfilkeys) {
+ my $filter = $self->{$key};
+ if ($filter->{reject} && exists $filter->{reject}->{user}) {
+ push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
+ }
+ if ($filter->{accept} && exists $filter->{accept}->{user}) {
+ push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
+ }
+ }
+ return @out;
+}
+
+sub install
+{
+ my $self = shift;
+ my $remove = shift;
+ my $name = uc $self->{name};
+ my $sort = $self->{sort};
+ my ($in) = $name =~ s/^IN_//;
+ $name =~ s/.PL$//;
+
+ my $dxchan = DXChannel->get($name);
+ if ($dxchan) {
+ $in = lc $in if $in;
+ my $n = "$in$sort" . "filter";
+ $dxchan->$n($remove ? undef : $self);
+ }
+}
+
+sub delete
+{
+ my ($sort, $call, $flag, $fno) = @_;
+
+ # look for the file
+ my $fn = getfn($sort, $call, $flag);
+ my $filter = read_in($sort, $call, $flag);
+ if ($filter) {
+ if ($fno eq 'all') {
+ my $key;
+ foreach $key ($filter->getfilkeys) {
+ delete $filter->{$key};
+ }
+ } elsif (exists $filter->{"filter$fno"}) {
+ delete $filter->{"filter$fno"};
+ }
+
+ # get rid
+ if ($filter->{hops} || $filter->getfilkeys) {
+ $filter->install;
+ } else {
+ $filter->install(1);
+ unlink $fn;
+ }
+ }
}
package Filter::Cmd;
# the general purpose command processor
# this is called as a subroutine not as a method
-sub process_cmd
+sub parse
{
my ($self, $dxchan, $line) = @_;
my $ntoken = 0;
my $fno = 1;
my $filter;
my ($flag, $call);
-
+ my $s;
+ 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,_\*\/\(\)]/;
- while (my @f = split /\s+/, $line) {
+ # add some spaces for ease of parsing
+ $line =~ s/([\(\)])/ $1 /g;
+ $line = lc $line;
+
+ my @f = split /\s+/, $line;
+ my $conj = ' && ';
+ my $not = "";
+ while (@f) {
if ($ntoken == 0) {
- if (@f && $dxchan->priv >= 9 && DXUser->get($f[0])) {
+ if (@f && $dxchan->priv >= 8 && (DXUser->get($f[0]) || $f[0] =~ /(?:node|user)_default/)) {
$call = shift @f;
if ($f[0] eq 'input') {
shift @f;
$call = $dxchan->call;
}
- if (@f && $f[0] =~ /^\d+$/) {
+ if (@f && $f[0] =~ /^\d$/) {
$fno = shift @f;
}
- $filter = Filter::read_in('spots', $call, $flag) or new Filter ('spots', $call, $flag);
+ $filter = Filter::read_in('spots', $call, $flag);
+ $filter = Filter->new('spots', $call, $flag) unless $filter;
$ntoken++;
next;
# do the rest of the filter tokens
if (@f) {
my $tok = shift @f;
+ if ($tok eq '(') {
+ if ($s) {
+ $s .= $conj;
+ $user .= $conj;
+ $conj = "";
+ }
+ if ($not) {
+ $s .= $not;
+ $user .= $not;
+ $not = "";
+ }
+ $s .= $tok;
+ $user .= $tok;
+ next;
+ } elsif ($tok eq ')') {
+ $conj = ' && ';
+ $not ="";
+ $s .= $tok;
+ $user .= $tok;
+ next;
+ } elsif ($tok eq 'or') {
+ $conj = ' || ' if $conj ne ' || ';
+ next;
+ } elsif ($tok eq 'and') {
+ $conj = ' && ' if $conj ne ' && ';
+ next;
+ } elsif ($tok eq 'not' || $tok eq '!') {
+ $not = '!';
+ next;
+ }
if (@f) {
my $val = shift @f;
-
+ my @val = split /,/, $val;
+
+ if ($s) {
+ $s .= $conj ;
+ $s .= $not;
+ $user .= $conj;
+ $user .= $not;
+ $conj = ' && ';
+ $not = "";
+ }
+ $user .= "$tok $val";
+
my $fref;
+ my $found;
foreach $fref (@$self) {
+
if ($fref->[0] eq $tok) {
-
+ if ($fref->[4]) {
+ my @nval;
+ for (@val) {
+ push @nval, split(',', &{$fref->[4]}($dxchan, $_));
+ }
+ @val = @nval;
+ }
+ if ($fref->[1] eq 'a') {
+ my @t;
+ for (@val) {
+ s/\*//g;
+ push @t, "\$r->[$fref->[2]]=~/$_/i";
+ }
+ $s .= "(" . join(' || ', @t) . ")";
+ } elsif ($fref->[1] eq 'c') {
+ my @t;
+ for (@val) {
+ s/\*//g;
+ push @t, "\$r->[$fref->[2]]=~/^\U$_/";
+ }
+ $s .= "(" . join(' || ', @t) . ")";
+ } elsif ($fref->[1] eq 'n') {
+ my @t;
+ for (@val) {
+ return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
+ push @t, "\$r->[$fref->[2]]==$_";
+ }
+ $s .= "(" . join(' || ', @t) . ")";
+ } elsif ($fref->[1] eq 'r') {
+ my @t;
+ for (@val) {
+ return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
+ push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
+ }
+ $s .= "(" . join(' || ', @t) . ")";
+ } else {
+ confess("invalid letter $fref->[1]");
+ }
+ ++$found;
+ last;
}
}
+ return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
} else {
return ('no', $dxchan->msg('filter2', $tok));
}
}
}
- $flag = $flag ? "in_" : "";
- return (0, $dxchan->msg('filter1', $fno, "$flag$call"));
+
+ # tidy up the user string
+ $user =~ s/\&\&/ and /g;
+ $user =~ s/\|\|/ or /g;
+ $user =~ s/\!/ not /g;
+ $user =~ s/\s+/ /g;
+
+ return (0, $filter, $fno, $user, "sub { my \$r = shift; return $s }");
}
package Filter::Old;