download Filter.pm
Language: Perl
Copyright: (c) 1999 Dirk Koopman G1TLH
LOC: 475
Project Info
DXSpider DX Cluster System(dxspider)
Server: SourceForge
Type: cvs
...pider\dxspider\spider\perl\
   AGWConnect.pm
   AGWMsg.pm
   AnnTalk.pm
   BadWords.pm
   Bands.pm
   BBS.pm
   Buck.pm
   call.pl
   callbot.pl
   Chain.pm
   cluster.pl
   CmdAlias.pm
   connect.pl
   console.pl
   Console.pm
   convert_users.pl
   convkeps.pl
   create_prefix.pl
   create_qsl.pl
   create_sysop.pl
   create_usdb.pl
   DB0SDX.pm
   DXBearing.pm
   dxcc.pl
   DXChannel.pm
   DXCommandmode.pm
   DXConnect.pm
   DXCron.pm
   DXDb.pm
   DXDebug.pm
   DXDupe.pm
   DXHash.pm
   DXLog.pm
   DXLogPrint.pm
   DXM.pm
   DXMsg.pm
   dxoldtonew.pl
   DXProt.pm
   DXProtout.pm
   DXProtVars.pm
   DXSql.pm
   DXUser.pm
   DXUtil.pm
   DXVars.pm.issue
   DXXml.pm
   Editable.pm
   export_opernam.pl
   ExtMsg.pm
   Filter.pm
   ForkingServer.pm
   gen_usdb_data.pl
   Geomag.pm
   hlptohtml.pl
   importkeps.pl
   importwwv.pl
   Internet.pm
   IntMsg.pm
   Investigate.pm
   IsoTime.pm
   Julian.pm
   K4UTE.pm
   Keps.pm
   Listeners.pm
   Local.pm
   lock_nodes.pl
   log2csv.pl
   LRU.pm
   Minimuf.pm
   MiscLog.pm
   Mrtg.pm
   Msg.pm
   PC.pm
   Prefix.pm
   process_ursa.pl
   Prot.pm
   proto.html
   QRZ.pm
   QSL.pm
   RingBuf.pm
   Route.pm
   RouteDB.pm
   Script.pm
   Spot.pm
   spot2csv.pl
   Sun.pm
   talias.pl
   Thingy.pm
   Timer.pm
   UDPMsg.pm
   update_sysop.pl
   USDB.pm
   VE7CC.pm
   Verify.pm
   WCY.pm
   winclient.pl
   y2k.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#
# The User/Sysop Filter module
#
# The way this works is that the filter routine is actually
# a predefined function that returns 0 if it is OK and 1 if it
# is not when presented with a list of things.
#
# This set of routines provide a means of maintaining the filter
# scripts which are compiled in when an entity connects.
#
# Copyright (c) 1999 Dirk Koopman G1TLH
#
# $Id: Filter.pm,v 1.38 2006/11/03 13:48:26 minima Exp $
#
# The NEW INSTRUCTIONS
#
# use the commands accept/spot|ann|wwv|wcy and reject/spot|ann|wwv|wcy
# also show/filter spot|ann|wwv|wcy
#
# The filters live in a directory tree of their own in $main::root/filter
#
# Each type of filter (e.g. spot, wwv) live in a tree of their own so you
# can have different filters for different things for the same callsign.
#


package Filter;

use DXVars;
use DXUtil;
use DXDebug;
use Data::Dumper;
use Prefix;

use strict;

use vars qw($VERSION $BRANCH);
$VERSION = sprintf( "%d.%03d", q$Revision: 1.38 $ =~ /(\d+)\.(\d+)/ );
$BRANCH = sprintf( "%d.%03d", q$Revision: 1.38 $ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
$main::build += $VERSION;
$main::branch += $BRANCH;

use vars qw ($filterbasefn $in);

$filterbasefn = "$main::root/filter";
$in = undef;

# initial filter system
sub init
{

}

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 compile
{
	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;
		if ($@) {
			dbg($@);
			unlink($fn);
			return undef;
		}
		if ($in) {
			$newin = new('Filter::Old', $sort, $call, $flag);
			$newin->{filter} = $in;
		} elsif (ref $newin && $newin->can('getfilkeys')) {
			my $filter;
			my $key;
			foreach $key ($newin->getfilkeys) {
				$newin->compile($key, 'reject');
				$newin->compile($key, 'accept');
			}
		} else {
			# error on reading file, delete and exit
			dbg("empty or unreadable filter: $fn, deleted");
			unlink($fn);
			return undef;
		}
		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);
		}
	}
}

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;
			$filter->write;
		} else {
			$filter->install(1);
			unlink $fn;
		}
	}
}

package Filter::Cmd;

use strict;
use DXVars;
use DXUtil;
use DXDebug;
use vars qw(@ISA);
@ISA = qw(Filter);

# the general purpose command processor
# this is called as a subroutine not as a method
sub parse
{
	my ($self, $dxchan, $sort, $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,_\-\*\/\(\)!]/;
	
	# 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 >= 8 && ((is_callsign(uc $f[0]) && DXUser->get(uc $f[0])) || $f[0] =~ /(?:node|user)_default/)) {
				$call = shift @f;
				if ($f[0] eq 'input') {
					shift @f;
					$flag++;
				}
			} else {
				$call = $dxchan->call;
			}

			if (@f && $f[0] =~ /^\d$/) {
				$fno = shift @f;
			}

			$filter = Filter::read_in($sort, $call, $flag);
			$filter = Filter->new($sort, $call, $flag) if !$filter || $filter->isa('Filter::Old');
			
			$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 'all') {
				$s .= '1';
				$user .= $tok;
				last;
			} 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 ;
					$user .= $conj;
					$conj = ' && ';
				}

				if ($not) {
					$s .= $not;
					$user .= $not;
					$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] =~ /^n[ciz]$/ ) {    # for DXCC, ITU, CQ Zone    
							my $cmd = $fref->[1];
							my @pre = Prefix::to_ciz($cmd, @val);
							return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
							$s .= "(" . join(' || ', map {"\$r->[$fref->[2]]==$_"} @pre) . ")";
						} elsif ($fref->[1] =~ /^ns$/ ) {    # for DXCC, ITU, CQ Zone    
							my $cmd = $fref->[1];
							my @pre = Prefix::to_ciz($cmd, @val);
							return ('numpre', $dxchan->msg('e27', $_)) unless @pre;
							$s .= "(" . "!\$USDB::present || grep \$r->[$fref->[2]] eq \$_, qw(" . join(' ' ,map {uc} @pre) . "))";
						} 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) . ")";
						} elsif ($fref->[1] eq 't') {
							my @t;
							for (@val) {
								s/\*//g;
								push @t, "\$r->[$fref->[2]]=~/$_/i";
							}
							$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));
			}
		}
		
	}

	# 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, "$s");
}

# a filter accept/reject command
sub cmd
{
	my ($self, $dxchan, $sort, $type, $line) = @_;
	
	return $dxchan->msg('filter5') unless $line;

	my ($r, $filter, $fno, $user, $s) = $self->parse($dxchan, $sort, $line);
	my $u = DXUser->get_current($user);
	return (1, $dxchan->msg('isow', $user)) if $u && $u->isolate;
	return (1, $filter) if $r;

	my $fn = "filter$fno";

	$filter->{$fn} = {} unless exists $filter->{$fn};
	$filter->{$fn}->{$type} = {} unless exists $filter->{$fn}->{$type};

	$filter->{$fn}->{$type}->{user} = $user;
	$filter->{$fn}->{$type}->{asc} = $s;
	$r = $filter->compile($fn, $type);
	return (1,$r) if $r;
	
	$r = $filter->write;
	return (1,$r) if $r;
	
	$filter->install;

    return (0, $filter, $fno);
}

package Filter::Old;

use strict;
use DXVars;
use DXUtil;
use DXDebug;
use vars qw(@ISA);
@ISA = qw(Filter);

# the OLD instructions!
#
# Each filter file has the same structure:-
#
# <some comment>
# @in = (
#      [ action, fieldno, fieldsort, comparison, action data ],
#      ...
# );
#
# The action is usually 1 or 0 but could be any numeric value
#
# The fieldno is the field no in the list of fields that is presented
# to 'Filter::it' 
#
# The fieldsort is the type of field that we are dealing with which 
# currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is 
# numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
#
# Filter::it basically goes thru the list of comparisons from top to
# bottom and when one matches it will return the action and the action data as a list. 
# The fields
# are the element nos of the list that is presented to Filter::it. Element
# 0 is the first field of the list.
#

#
# takes the reference to the filter (the first argument) and applies
# it to the subsequent arguments and returns the action specified.
#
sub it
{
	my $self = shift;
	my $filter = $self->{filter};            # this is now a bless ref of course but so what
	
	my ($action, $field, $fieldsort, $comp, $actiondata);
	my $ref;

	# default action is 1
	$action = 1;
	$actiondata = "";
	return ($action, $actiondata) if !$filter;

	for $ref (@{$filter}) {
		($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
		if ($fieldsort eq 'n') {
			my $val = $_[$field];
			return ($action, $actiondata)  if grep $_ == $val, @{$comp};
		} elsif ($fieldsort eq 'r') {
			my $val = $_[$field];
			my $i;
			my @range = @{$comp};
			for ($i = 0; $i < @range; $i += 2) {
				return ($action, $actiondata)  if $val >= $range[$i] && $val <= $range[$i+1];
			}
		} elsif ($fieldsort eq 'a') {
			return ($action, $actiondata)  if $_[$field] =~ m{$comp};
		} else {
			return ($action, $actiondata);      # the default action
		}
	}
}

sub print
{
	my $self = shift;
	my $call = shift;
	my $sort = shift;
	my $flag = shift || "";
	return "$call: Old Style Filter $flag $sort";
}

1;
__END__

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us