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
15 # The NEW INSTRUCTIONS
17 # use the commands accept/spot|ann|wwv|wcy and reject/spot|ann|wwv|wcy
18 # also show/filter spot|ann|wwv|wcy
20 # The filters live in a directory tree of their own in $main::root/filter
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.
36 use vars qw ($filterbasefn $in);
38 $filterbasefn = "$main::root/filter";
41 # initial filter system
48 # this reads in a filter statement and returns it as a list
50 # The filter is stored in straight perl so that it can be parsed and read
51 # in with a 'do' statement. The 'do' statement reads the filter into
52 # @in which is a list of references
56 my ($sort, $call, $flag) = @_;
59 $flag = ($flag) ? "in_" : "";
61 my $fn = "$filterbasefn/$sort/$flag$call.pl";
66 $fn = "$filterbasefn/$sort/$flag$call.pl";
72 my $s = readfilestr($fn);
74 dbg('conn', "$@") if $@;
75 return bless [ @$in ], 'Filter::Old' if $in;
81 # this writes out the filter in a form suitable to be read in by 'read_in'
82 # It expects a list of references to filter lines
89 my $fn = "$filterbasefn/$sort";
92 # make the output directory
93 mkdir $fn, 0777 unless -e $fn;
97 unless (open FILTER, ">$fn") {
98 warn "can't open $fn $!" ;
102 my $today = localtime;
103 print FILTER "#!/usr/bin/perl
105 # Filter for $call stored $today
112 my ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
113 print FILTER "\t[ $action, $field, $fieldsort,";
114 if ($fieldsort eq 'n' || $fieldsort eq 'r') {
115 print FILTER "[ ", join (',', $comp), " ],";
116 } elsif ($fieldsort eq 'a') {
120 print FILTER " ],\n";
132 # the OLD instructions!
134 # Each filter file has the same structure:-
138 # [ action, fieldno, fieldsort, comparison, action data ],
142 # The action is usually 1 or 0 but could be any numeric value
144 # The fieldno is the field no in the list of fields that is presented
147 # The fieldsort is the type of field that we are dealing with which
148 # currently can be 'a', 'n', 'r' or 'd'. 'a' is alphanumeric, 'n' is
149 # numeric, 'r' is ranges of pairs of numeric values and 'd' is default.
151 # Filter::it basically goes thru the list of comparisons from top to
152 # bottom and when one matches it will return the action and the action data as a list.
154 # are the element nos of the list that is presented to Filter::it. Element
155 # 0 is the first field of the list.
159 # takes the reference to the filter (the first argument) and applies
160 # it to the subsequent arguments and returns the action specified.
164 my $filter = shift; # this is now a bless ref of course but so what
166 my ($action, $field, $fieldsort, $comp, $actiondata);
169 # default action is 1
172 return ($action, $actiondata) if !$filter;
174 for $ref (@{$filter}) {
175 ($action, $field, $fieldsort, $comp, $actiondata) = @{$ref};
176 if ($fieldsort eq 'n') {
177 my $val = $_[$field];
178 return ($action, $actiondata) if grep $_ == $val, @{$comp};
179 } elsif ($fieldsort eq 'r') {
180 my $val = $_[$field];
182 my @range = @{$comp};
183 for ($i = 0; $i < @range; $i += 2) {
184 return ($action, $actiondata) if $val >= $range[$i] && $val <= $range[$i+1];
186 } elsif ($fieldsort eq 'a') {
187 return ($action, $actiondata) if $_[$field] =~ m{$comp};
189 return ($action, $actiondata); # the default action