2 # The User/Sysop Filter module
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.
8 # This set of routines provide a means of maintaining the filter
9 # scripts which are compiled in when an entity connects.
11 # Copyright (c) 1999 Dirk Koopman G1TLH
17 # The filters live in a directory tree of their own in $main::root/filter
19 # Each type of filter (e.g. spot, wwv) live in a tree of their own so you
20 # can have different filters for different things for the same callsign.
22 # Each filter file has the same structure:-
26 # [ action, fieldno, fieldsort, comparison ],
30 # The action is usually 1 or 0 but could be any numeric value
32 # The fieldno is the field no in the list of fields that is presented
35 # The fieldsort is the type of field that we are dealing with which
36 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
37 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
39 # Filter::it basically goes thru the list of comparisons from top to
40 # bottom and when one matches it will return the action. The fields
41 # are the element nos of the list that is presented to Filter::it. Element
42 # 0 is the first field of the list.
54 use vars qw ($filterbasefn $in);
56 $filterbasefn = "$main::root/filter";
59 # initial filter system
66 # takes the reference to the filter (the first argument) and applies
67 # it to the subsequent arguments and returns the action specified.
77 for $ref (@{$filter}) {
78 my ($action, $field, $fieldsort, $comp) = @{$ref};
79 if ($fieldsort eq 'n') {
81 return $action if grep $_ == $val, @{$comp};
82 } elsif ($fieldsort eq 'r') {
86 for ($i = 0; $i < @range; $i += 2) {
87 return $action if $val >= $range[$i] && $val <= $range[$i+1];
89 } elsif ($fieldsort eq 'a') {
90 return $action if $_[$field] =~ m{$comp};
92 return $action; # the default action
97 # this reads in a filter statement and returns it as a list
99 # The filter is stored in straight perl so that it can be parsed and read
100 # in with a 'do' statement. The 'do' statement reads the filter into
101 # @in which is a list of references
105 my ($sort, $call) = @_;
106 my $fn = "$filterbasefn/$sort/$call.pl";
110 dbg('conn', "$@") if $@;
116 # this writes out the filter in a form suitable to be read in by 'read_in'
117 # It expects a list of references to filter lines
122 my $fn = "$filterbasefn/$sort";
125 # make the output directory
126 mkdir $fn, 0777 unless -e $fn;
129 $fn = "$fn/$call.pl";
130 unless (open FILTER, ">$fn") {
131 warn "can't open $fn $!" ;
135 my $today = localtime;
137 # Filter for $call stored $today
144 my ($action, $field, $fieldsort, $comp) = @{$ref};
145 print FILTER "\t[ $action, $field, $fieldsort,";
146 if ($fieldsort eq 'n' || $fieldsort eq 'r') {
147 print FILTER "[ ", join (',', $comp), " ],";
148 } elsif ($fieldsort eq 'a') {
152 print FILTER " ],\n";