+ my $self = shift;
+ my $fname = shift;
+ my $ar = shift;
+ my $ref = $self->{$fname};
+ my $rr;
+
+ if ($ref->{$ar} && exists $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("Error compiling $ar $sort $name: $@");
+ Log('err', "Error compiling $ar $sort $name: $@");
+ }
+ $rr = $@;
+ }
+ return $rr;
+}
+
+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($@) if $@;
+ if ($in) {
+ $newin = new('Filter::Old', $sort, $call, $flag);
+ $newin->{filter} = $in;
+ } else {
+ my $filter;
+ my $key;
+ foreach $key ($newin->getfilkeys) {
+ $newin->compile($key, 'reject');
+ $newin->compile($key, 'accept');
+ }
+ }
+ 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 $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}) {
+ $type = 'reject';
+ $asc = $filter->{reject}->{user};
+ if (&{$filter->{reject}->{code}}(\@_)) {
+ $r = 0;
+ last;
+ } else {
+ $r = 1;
+ }
+ }
+ if ($filter->{accept} && exists $filter->{accept}->{code}) {
+ $type = 'accept';
+ $asc = $filter->{accept}->{user};
+ if (&{$filter->{accept}->{code}}(\@_)) {
+ $r = 1;
+ last;
+ } else {
+ $r = 0;
+ }
+ }
+ }
+
+ # hops are done differently (simply)
+ my $hops = $self->{hops} if exists $self->{hops};
+
+ if (isdbg('filter')) {
+ my $args = join '\',\'', map {defined $_ ? $_ : 'undef'} @_;
+ my $true = $r ? "OK " : "REJ";
+ my $sort = $self->{sort};
+ my $dir = $self->{name} =~ /^in_/i ? "IN " : "OUT";
+
+ my $h = $hops || '';
+ dbg("$true $dir: $type/$sort with $asc on '$args' $h") if isdbg('filter');
+ }
+ return ($r, $hops);
+}
+
+# this writes out the filter in a form suitable to be read in by 'read_in'
+# It expects a list of references to filter lines
+sub write
+{
+ my $self = shift;
+ my $sort = $self->{sort};
+ 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";
+ if ($fh) {
+ my $dd = new Data::Dumper([ $self ]);
+ $dd->Indent(1);
+ $dd->Terse(1);
+ $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;
+}
+
+sub print
+{
+ my $self = shift;
+ my $name = shift || $self->{name};
+ my $sort = shift || $self->{sort};
+ my $flag = shift || "";
+ my @out;
+ $name =~ s/.pl$//;
+
+ push @out, join(' ', $name , ':', $sort, $flag);
+ my $filter;
+ my $key;
+ foreach $key (sort $self->getfilkeys) {
+ my $filter = $self->{$key};
+ if (exists $filter->{reject} && exists $filter->{reject}->{user}) {
+ push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
+ }
+ if (exists $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 = "";
+ $in = "in" if $name =~ s/^IN_//;
+ $name =~ s/.PL$//;
+
+ 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";
+ my $i = $in ? 'IN_' : '';
+ my $ref = $dxchan->$n();
+ if (!$ref || ($ref && uc $ref->{name} eq "$i$name.PL")) {
+ $dxchan->$n($remove ? undef : $self);
+ }
+ }
+}