+
+}
+
+sub new
+{
+ my ($class, $sort, $call, $flag) = @_;
+ $flag = ($flag) ? "in_" : "";
+ return bless {sort => $sort, name => "$flag$call.pl" }, $class;
+}
+
+# standard filename generator
+sub getfn
+{
+ my ($sort, $call, $flag) = @_;
+
+ # first uppercase
+ $flag = ($flag) ? "in_" : "";
+ $call = uc $call;
+ my $fn = "$filterbasefn/$sort/$flag$call.pl";
+
+ # otherwise lowercase
+ unless (-e $fn) {
+ $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 ($fn = getfn($sort, $call, $flag)) {
+ $in = undef;
+ my $s = readfilestr($fn);
+ my $newin = eval $s;
+ dbg('conn', "$@") if $@;
+ 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 rule and then an accept rule.
+#
+# The filter returns 0 if an entry is matched by any reject rule and also if any
+# accept rule fails otherwise it returns 1
+#
+# Either set of rules may be missing meaning an implicit 'opposite' ie if it
+# a reject then ok else if an accept then not ok.
+#
+# you can set a default with either an accept/xxxx all or reject/xxxx all
+#
+# Unlike the old system, this is kept as a hash of hashes so that you can
+# easily change them by program.
+#
+# You can have 10 filter lines (0->9), they are tried in 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
+#
+#
+
+sub it
+{
+ my $self = shift;
+
+ my $hops = undef;
+
+ my $filter;
+ my @keys = sort $self->getfilkeys;
+ my $key;
+ my $r = @keys > 0 ? 0 : 1;
+ foreach $key (@keys) {
+ $filter = $self->{$key};
+ if ($filter->{reject} && exists $filter->{reject}->{code}) {
+ if (&{$filter->{reject}->{code}}(\@_)) {
+ $r = 0;
+ last;
+ } else {
+ $r = 1;
+ }
+ }
+ if ($filter->{accept} && exists $filter->{accept}->{code}) {
+ if (&{$filter->{accept}->{code}}(\@_)) {
+ $r = 1;
+ last;
+ } else {
+ $r = 0;
+ }
+ }
+ }
+
+ # hops are done differently
+ if ($self->{hops}) {
+ my ($comp, $ref);
+ while (($comp, $ref) = each %{$self->{hops}}) {
+ my ($field, $h) = @$ref;
+ if ($_[$field] =~ m{$comp}) {
+ $hops = $h;
+ last;
+ }
+ }
+ }
+ return ($r, $hops);