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