allow pc73 for DXNet
[spider.git] / perl / Filter.pm
1 #
2 # The User/Sysop Filter module
3 #
4 # The way this works is that the filter routine is actually
5 # a predefined function that returns 0 if it is OK and 1 if it
6 # is not when presented with a list of things.
7 #
8 # This set of routines provide a means of maintaining the filter
9 # scripts which are compiled in when an entity connects.
10 #
11 # Copyright (c) 1999 Dirk Koopman G1TLH
12 #
13 # $Id$
14 #
15 # The NEW INSTRUCTIONS
16 #
17 # use the commands accept/spot|ann|wwv|wcy and reject/spot|ann|wwv|wcy
18 # also show/filter spot|ann|wwv|wcy
19 #
20 # The filters live in a directory tree of their own in $main::root/filter
21 #
22 # Each type of filter (e.g. spot, wwv) live in a tree of their own so you
23 # can have different filters for different things for the same callsign.
24 #
25
26
27 package Filter;
28
29 use DXVars;
30 use DXUtil;
31 use DXDebug;
32 use Data::Dumper;
33
34 use strict;
35
36 use vars qw ($filterbasefn $in);
37
38 $filterbasefn = "$main::root/filter";
39 $in = undef;
40
41 # initial filter system
42 sub init
43 {
44
45 }
46
47 sub new
48 {
49         my ($class, $sort, $call, $flag) = @_;
50         $flag = ($flag) ? "in_" : "";
51         return bless {sort => $sort, name => "$flag$call.pl" }, $class;
52 }
53
54 # standard filename generator
55 sub getfn
56 {
57         my ($sort, $call, $flag) = @_;
58
59     # first uppercase
60         $flag = ($flag) ? "in_" : "";
61         $call = uc $call;
62         my $fn = "$filterbasefn/$sort/$flag$call.pl";
63
64         # otherwise lowercase
65         unless (-e $fn) {
66                 $call = lc $call;
67                 $fn = "$filterbasefn/$sort/$flag$call.pl";
68         }
69         $fn = undef unless -e $fn;
70         return $fn;
71 }
72
73 # this reads in a filter statement and returns it as a list
74
75 # The filter is stored in straight perl so that it can be parsed and read
76 # in with a 'do' statement. The 'do' statement reads the filter into
77 # @in which is a list of references
78 #
79 sub read_in
80 {
81         my ($sort, $call, $flag) = @_;
82         my $fn;
83         
84         # load it
85         if ($fn = getfn($sort, $call, $flag)) {
86                 $in = undef; 
87                 my $s = readfilestr($fn);
88                 my $newin = eval $s;
89                 dbg('conn', "$@") if $@;
90                 if ($in) {
91                         $newin = new('Filter::Old', $sort, $call, $flag);
92                         $newin->{filter} = $in;
93                 } else {
94                         my $filter;
95                         my $key;
96                         foreach $key ($newin->getfilkeys) {
97                                 $filter = $newin->{$key};
98                                 if ($filter->{reject} && exists $filter->{reject}->{asc}) {
99                                         $filter->{reject}->{code} = eval $filter->{reject}->{asc} ;
100                                         if ($@) {
101                                                 my $sort = $newin->{sort};
102                                                 my $name = $newin->{name};
103                                                 dbg('err', "Error compiling reject $sort $key $name: $@");
104                                                 Log('err', "Error compiling reject $sort $key $name: $@");
105                                         }
106                                 }
107                                 if ($filter->{accept} && exists $filter->{accept}->{asc}) {
108                                         $filter->{accept}->{code} = eval $filter->{accept}->{asc} ;
109                                         if ($@) {
110                                                 my $sort = $newin->{sort};
111                                                 my $name = $newin->{name};
112                                                 dbg('err', "Error compiling accept $sort $key $name: $@");
113                                                 Log('err', "Error compiling accept $sort $key $name: $@");
114                                         }
115                                 } 
116                         }
117                 }
118                 return $newin;
119         }
120         return undef;
121 }
122
123 sub getfilters
124 {
125         my $self = shift;
126         my @out;
127         my $key;
128         foreach $key (grep {/^filter/ } keys %$self) {
129                 push @out, $self->{$key};
130         }
131         return @out;
132 }
133
134 sub getfilkeys
135 {
136         my $self = shift;
137         return grep {/^filter/ } keys %$self;
138 }
139
140 #
141 # This routine accepts a composite filter with a reject rule and then an accept rule.
142 #
143 # The filter returns 0 if an entry is matched by any reject rule and also if any
144 # accept rule fails otherwise it returns 1
145 #
146 # Either set of rules may be missing meaning an implicit 'opposite' ie if it
147 # a reject then ok else if an accept then not ok.
148 #
149 # you can set a default with either an accept/xxxx all or reject/xxxx all
150 #
151 # Unlike the old system, this is kept as a hash of hashes so that you can
152 # easily change them by program.
153 #
154 # You can have 10 filter lines (0->9), they are tried in order until 
155 # one matches
156 #
157 # There is a parser that takes a Filter::Cmd object which describes all the possible
158 # things you can filter on and then converts that to a bit of perl which is compiled
159 # and stored as a function.
160 #
161 # The result of this is that in theory you can put together an arbritrarily complex 
162 # expression involving the things you can filter on including 'and' 'or' 'not' and 
163 # 'brackets'.
164 #
165 # eg:-
166 #
167 # accept/spots hf and by_zone 14,15,16 and not by pa,on
168 #  
169 # accept/spots freq 0/30000 and by_zone 4,5
170
171 # accept/spots 2 vhf and (by_zone 14,15,16 or call_dxcc 61) 
172 #
173 # no filter no implies filter 1
174 #
175 # The field nos are the same as for the 'Old' filters
176 #
177
178
179 sub it
180 {
181         my $self = shift;
182         
183         my $hops = undef;
184                 
185         my $filter;
186         my @keys = sort $self->getfilkeys;
187         my $key;
188         my $r = @keys > 0 ? 0 : 1;
189         foreach $key (@keys) {
190                 $filter = $self->{$key};
191                 if ($filter->{reject} && exists $filter->{reject}->{code}) {
192                         if (&{$filter->{reject}->{code}}(\@_)) {
193                                 $r = 0;
194                                 last;
195                         } else {
196                                 $r = 1;
197                         }               
198                 }
199                 if ($filter->{accept} && exists $filter->{accept}->{code}) {
200                         if (&{$filter->{accept}->{code}}(\@_)) {
201                                 $r = 1;
202                                 last;
203                         } else {
204                                 $r = 0;
205                         }                       
206                 } 
207         }
208
209         # hops are done differently 
210         if ($self->{hops}) {
211                 my ($comp, $ref);
212                 while (($comp, $ref) = each %{$self->{hops}}) {
213                         my ($field, $h) = @$ref;
214                         if ($_[$field] =~ m{$comp}) {
215                                 $hops = $h;
216                                 last;
217                         } 
218                 }               
219         }
220         return ($r, $hops);
221 }
222
223 # this writes out the filter in a form suitable to be read in by 'read_in'
224 # It expects a list of references to filter lines
225 sub write
226 {
227         my $self = shift;
228         my $sort = $self->{sort};
229         my $name = $self->{name};
230         my $dir = "$filterbasefn/$sort";
231         my $fn = "$dir/$name";
232
233         mkdir $dir, 0775 unless -e $dir; 
234     rename $fn, "$fn.o" if -e $fn;
235         my $fh = new IO::File ">$fn";
236         if ($fh) {
237                 my $dd = new Data::Dumper([ $self ]);
238                 $dd->Indent(1);
239                 $dd->Terse(1);
240                 $dd->Quotekeys($] < 5.005 ? 1 : 0);
241                 $fh->print($dd->Dumpxs);
242                 $fh->close;
243         } else {
244                 rename "$fn.o", $fn if -e "$fn.o";
245                 return "$fn $!";
246         }
247         return undef;
248 }
249
250 sub print
251 {
252         my $self = shift;
253         my @out;
254         my $name = $self->{name};
255         $name =~ s/.pl$//;
256         
257         push @out, join(' ',  $name , ':', $self->{sort});
258         my $filter;
259         my $key;
260         foreach $key (sort $self->getfilkeys) {
261                 my $filter = $self->{$key};
262                 if ($filter->{reject} && exists $filter->{reject}->{user}) {
263                         push @out, ' ' . join(' ', $key, 'reject', $filter->{reject}->{user});
264                 }
265                 if ($filter->{accept} && exists $filter->{accept}->{user}) {
266                         push @out, ' ' . join(' ', $key, 'accept', $filter->{accept}->{user});
267                 } 
268         }
269         return @out;
270 }
271
272 sub install
273 {
274         my $self = shift;
275         my $remove = shift;
276         my $name = uc $self->{name};
277         my $sort = $self->{sort};
278         my ($in) = $name =~ s/^IN_//;
279         $name =~ s/.PL$//;
280                 
281         my $dxchan = DXChannel->get($name);
282         if ($dxchan) {
283                 $in = lc $in if $in;
284                 my $n = "$in$sort" . "filter";
285                 $dxchan->$n($remove ? undef : $self);
286         }
287 }
288
289 sub delete
290 {
291         my ($sort, $call, $flag, $fno) = @_;
292         
293         # look for the file
294         my $fn = getfn($sort, $call, $flag);
295         my $filter = read_in($sort, $call, $flag);
296         if ($filter) {
297                 if ($fno eq 'all') {
298                         my $key;
299                         foreach $key ($filter->getfilkeys) {
300                                 delete $filter->{$key};
301                         }
302                 } elsif (exists $filter->{"filter$fno"}) {
303                         delete $filter->{"filter$fno"}; 
304                 }
305                 
306                 # get rid 
307                 if ($filter->{hops} || $filter->getfilkeys) {
308                         $filter->install;
309                         $filter->write;
310                 } else {
311                         $filter->install(1);
312                         unlink $fn;
313                 }
314         }
315 }
316
317 package Filter::Cmd;
318
319 use strict;
320 use DXVars;
321 use DXUtil;
322 use DXDebug;
323 use vars qw(@ISA);
324 @ISA = qw(Filter);
325
326 # the general purpose command processor
327 # this is called as a subroutine not as a method
328 sub parse
329 {
330         my ($self, $dxchan, $line) = @_;
331         my $ntoken = 0;
332         my $fno = 1;
333         my $filter;
334         my ($flag, $call);
335         my $s;
336         my $user;
337         
338         # check the line for non legal characters
339         return ('ill', $dxchan->msg('e19')) if $line =~ /[^\s\w,_\*\/\(\)]/;
340         
341         # add some spaces for ease of parsing
342         $line =~ s/([\(\)])/ $1 /g;
343         $line = lc $line;
344         
345         my @f = split /\s+/, $line;
346         my $conj = ' && ';
347         my $not = "";
348         while (@f) {
349                 if ($ntoken == 0) {
350                         
351                         if (@f && $dxchan->priv >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
352                                 $call = shift @f;
353                                 if ($f[0] eq 'input') {
354                                         shift @f;
355                                         $flag++;
356                                 }
357                         } else {
358                                 $call = $dxchan->call;
359                         }
360
361                         if (@f && $f[0] =~ /^\d$/) {
362                                 $fno = shift @f;
363                         }
364
365                         $filter = Filter::read_in('spots', $call, $flag);
366                         $filter = Filter->new('spots', $call, $flag) unless $filter;
367                         
368                         $ntoken++;
369                         next;
370                 }
371
372                 # do the rest of the filter tokens
373                 if (@f) {
374                         my $tok = shift @f;
375                         if ($tok eq '(') {
376                                 if ($s) {
377                                         $s .= $conj;
378                                         $user .= $conj;
379                                         $conj = "";
380                                 }
381                                 if ($not) {
382                                         $s .= $not;
383                                         $user .= $not;
384                                         $not = "";
385                                 }
386                                 $s .= $tok;
387                                 $user .= $tok;
388                                 next;
389                         } elsif ($tok eq ')') {
390                                 $conj = ' && ';
391                                 $not ="";
392                                 $s .= $tok;
393                                 $user .= $tok;
394                                 next;
395                         } elsif ($tok eq 'all') {
396                                 $s .= '1';
397                                 $user .= $tok;
398                                 last;
399                         } elsif ($tok eq 'or') {
400                                 $conj = ' || ' if $conj ne ' || ';
401                                 next;
402                         } elsif ($tok eq 'and') {
403                                 $conj = ' && ' if $conj ne ' && ';
404                                 next;
405                         } elsif ($tok eq 'not' || $tok eq '!') {
406                                 $not = '!';
407                                 next;
408                         }
409                         if (@f) {
410                                 my $val = shift @f;
411                                 my @val = split /,/, $val;
412
413                                 if ($s) {
414                                         $s .= $conj ;
415                                         $s .= $not;
416                                         $user .= $conj;
417                                         $user .= $not;
418                                         $conj = ' && ';
419                                         $not = "";
420                                 }
421                                 $user .= "$tok $val";
422                                 
423                                 my $fref;
424                                 my $found;
425                                 foreach $fref (@$self) {
426                                         
427                                         if ($fref->[0] eq $tok) {
428                                                 if ($fref->[4]) {
429                                                         my @nval;
430                                                         for (@val) {
431                                                                 push @nval, split(',', &{$fref->[4]}($dxchan, $_));
432                                                         }
433                                                         @val = @nval;
434                                                 }
435                                                 if ($fref->[1] eq 'a') {
436                                                         my @t;
437                                                         for (@val) {
438                                                                 s/\*//g;
439                                                                 push @t, "\$r->[$fref->[2]]=~/$_/i";
440                                                         }
441                                                         $s .= "(" . join(' || ', @t) . ")";
442                                                 } elsif ($fref->[1] eq 'c') {
443                                                         my @t;
444                                                         for (@val) {
445                                                                 s/\*//g;
446                                                                 push @t, "\$r->[$fref->[2]]=~/^\U$_/";
447                                                         }
448                                                         $s .= "(" . join(' || ', @t) . ")";
449                                                 } elsif ($fref->[1] eq 'n') {
450                                                         my @t;
451                                                         for (@val) {
452                                                                 return ('num', $dxchan->msg('e21', $_)) unless /^\d+$/;
453                                                                 push @t, "\$r->[$fref->[2]]==$_";
454                                                         }
455                                                         $s .= "(" . join(' || ', @t) . ")";
456                                                 } elsif ($fref->[1] eq 'r') {
457                                                         my @t;
458                                                         for (@val) {
459                                                                 return ('range', $dxchan->msg('e23', $_)) unless /^(\d+)\/(\d+)$/;
460                                                                 push @t, "(\$r->[$fref->[2]]>=$1 && \$r->[$fref->[2]]<=$2)";
461                                                         }
462                                                         $s .= "(" . join(' || ', @t) . ")";
463                                                 } else {
464                                                         confess("invalid letter $fref->[1]");
465                                                 }
466                                                 ++$found;
467                                                 last;
468                                         }
469                                 }
470                                 return ('unknown', $dxchan->msg('e20', $tok)) unless $found;
471                         } else {
472                                 return ('no', $dxchan->msg('filter2', $tok));
473                         }
474                 }
475                 
476         }
477
478         # tidy up the user string
479         $user =~ s/\&\&/ and /g;
480         $user =~ s/\|\|/ or /g;
481         $user =~ s/\!/ not /g;
482         $user =~ s/\s+/ /g;
483         
484         return (0, $filter, $fno, $user, "sub { my \$r = shift; return ($s) ? 1 : 0 }");
485 }
486
487 package Filter::Old;
488
489 use strict;
490 use DXVars;
491 use DXUtil;
492 use DXDebug;
493 use vars qw(@ISA);
494 @ISA = qw(Filter);
495
496 # the OLD instructions!
497 #
498 # Each filter file has the same structure:-
499 #
500 # <some comment>
501 # @in = (
502 #      [ action, fieldno, fieldsort, comparison, action data ],
503 #      ...
504 # );
505 #
506 # The action is usually 1 or 0 but could be any numeric value
507 #
508 # The fieldno is the field no in the list of fields that is presented
509 # to 'Filter::it' 
510 #
511 # The fieldsort is the type of field that we are dealing with which 
512 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is 
513 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
514 #
515 # Filter::it basically goes thru the list of comparisons from top to
516 # bottom and when one matches it will return the action and the action data as a list. 
517 # The fields
518 # are the element nos of the list that is presented to Filter::it. Element
519 # 0 is the first field of the list.
520 #
521
522 #
523 # takes the reference to the filter (the first argument) and applies
524 # it to the subsequent arguments and returns the action specified.
525 #
526 sub it
527 {
528         my $self = shift;
529         my $filter = $self->{filter};            # this is now a bless ref of course but so what
530         
531         my ($action, $field, $fieldsort, $comp, $actiondata);
532         my $ref;
533
534         # default action is 1
535         $action = 1;
536         $actiondata = "";
537         return ($action, $actiondata) if !$filter;
538
539         for $ref (@{$filter}) {
540                 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
541                 if ($fieldsort eq 'n') {
542                         my $val = $_[$field];
543                         return ($action, $actiondata)  if grep $_ == $val, @{$comp};
544                 } elsif ($fieldsort eq 'r') {
545                         my $val = $_[$field];
546                         my $i;
547                         my @range = @{$comp};
548                         for ($i = 0; $i < @range; $i += 2) {
549                                 return ($action, $actiondata)  if $val >= $range[$i] && $val <= $range[$i+1];
550                         }
551                 } elsif ($fieldsort eq 'a') {
552                         return ($action, $actiondata)  if $_[$field] =~ m{$comp};
553                 } else {
554                         return ($action, $actiondata);      # the default action
555                 }
556         }
557 }
558
559
560 1;
561 __END__