added the state filtering stuff
[spider.git] / perl / Route.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the abstracted routing for all protocols and
4 # is probably what I SHOULD have done the first time. 
5 #
6 # Heyho.
7 #
8 # This is just a container class which I expect to subclass 
9 #
10 # Copyright (c) 2001 Dirk Koopman G1TLH
11 #
12 # $Id$
13
14
15 package Route;
16
17 use DXDebug;
18 use DXChannel;
19 use Prefix;
20
21 use strict;
22
23
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
29
30 use vars qw(%list %valid $filterdef);
31
32 %valid = (
33                   call => "0,Callsign",
34                   flags => "0,Flags,phex",
35                   dxcc => '0,Country Code',
36                   itu => '0,ITU Zone',
37                   cq => '0,CQ Zone',
38                   state => '0,State',
39                   city => '0,City',
40                  );
41
42 $filterdef = bless ([
43                           # tag, sort, field, priv, special parser 
44                           ['channel', 'c', 0],
45                           ['channel_dxcc', 'nc', 1],
46                           ['channel_itu', 'ni', 2],
47                           ['channel_zone', 'nz', 3],
48                           ['call', 'c', 4],
49                           ['by', 'c', 4],
50                           ['call_dxcc', 'nc', 5],
51                           ['by_dxcc', 'nc', 5],
52                           ['call_itu', 'ni', 6],
53                           ['by_itu', 'ni', 6],
54                           ['call_zone', 'nz', 7],
55                           ['by_zone', 'nz', 7],
56                           ['channel_state', 'ns', 8],
57                           ['call_state', 'ns', 9],
58                           ['by_state', 'ns', 9],
59                          ], 'Filter::Cmd');
60
61
62 sub new
63 {
64         my ($pkg, $call) = @_;
65         $pkg = ref $pkg if ref $pkg;
66
67         my $self = bless {call => $call}, $pkg;
68         dbg("create $pkg with $call") if isdbg('routelow');
69
70         # add in all the dxcc, itu, zone info
71         my @dxcc = Prefix::extract($call);
72         if (@dxcc > 0) {
73                 $self->{dxcc} = $dxcc[1]->dxcc;
74                 $self->{itu} = $dxcc[1]->itu;
75                 $self->{cq} = $dxcc[1]->cq;
76                 $self->{state} = $dxcc[1]->state;
77                 $self->{city} = $dxcc[1]->city;
78         }
79         $self->{flags} = here(1);
80         
81         return $self; 
82 }
83
84 #
85 # get a callsign from a passed reference or a string
86 #
87
88 sub _getcall
89 {
90         my $self = shift;
91         my $thingy = shift;
92         $thingy = $self unless $thingy;
93         $thingy = $thingy->call if ref $thingy;
94         $thingy = uc $thingy if $thingy;
95         return $thingy;
96 }
97
98
99 # add and delete a callsign to/from a list
100 #
101
102 sub _addlist
103 {
104         my $self = shift;
105         my $field = shift;
106         my @out;
107         foreach my $c (@_) {
108                 confess "Need a ref here" unless ref($c);
109                 
110                 my $call = $c->{call};
111                 unless (grep $_ eq $call, @{$self->{$field}}) {
112                         push @{$self->{$field}}, $call;
113                         dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
114                         push @out, $c;
115                 }
116         }
117         return @out;
118 }
119
120 sub _dellist
121 {
122         my $self = shift;
123         my $field = shift;
124         my @out;
125         foreach my $c (@_) {
126                 confess "Need a ref here" unless ref($c);
127                 my $call = $c->{call};
128                 if (grep $_ eq $call, @{$self->{$field}}) {
129                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
130                         dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
131                         push @out, $c;
132                 }
133         }
134         return @out;
135 }
136
137 sub is_empty
138 {
139         my $self = shift;
140         return @{$self->{$_[0]}} == 0;
141 }
142
143 #
144 # flag field constructors/enquirers
145 #
146 # These can be called in various ways:-
147 #
148 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
149 # Route::here(1) returns 2 (the bit value of the here flag)
150 # $ref->here(1) or $ref->here(0) sets the here flag
151 #
152
153 sub here
154 {
155         my $self = shift;
156         my $r = shift;
157         return $self ? 2 : 0 unless ref $self;
158         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
159         $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
160         return $r ? 1 : 0;
161 }
162
163 sub conf
164 {
165         my $self = shift;
166         my $r = shift;
167         return $self ? 1 : 0 unless ref $self;
168         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
169         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
170         return $r ? 1 : 0;
171 }
172
173 sub parents
174 {
175         my $self = shift;
176         return @{$self->{parent}};
177 }
178
179
180 # display routines
181 #
182
183 sub user_call
184 {
185         my $self = shift;
186         my $call = sprintf "%s", $self->{call};
187         return $self->here ? "$call" : "($call)";
188 }
189
190 sub config
191 {
192         my $self = shift;
193         my $nodes_only = shift;
194         my $level = shift;
195         my $seen = shift;
196         my @out;
197         my $line;
198         my $call = $self->user_call;
199         my $printit = 1;
200
201         # allow ranges
202         if (@_) {
203                 $printit = grep $call =~ m|$_|, @_;
204         }
205
206         if ($printit) {
207                 $line = ' ' x ($level*2) . "$call";
208                 $call = ' ' x length $call; 
209                 
210                 # recursion detector
211                 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
212                         $line .= ' ...';
213                         push @out, $line;
214                         return @out;
215                 }
216                 push @$seen, $self->{call};
217
218                 # print users
219                 unless ($nodes_only) {
220                         if (@{$self->{users}}) {
221                                 $line .= '->';
222                                 foreach my $ucall (sort @{$self->{users}}) {
223                                         my $uref = Route::User::get($ucall);
224                                         my $c;
225                                         if ($uref) {
226                                                 $c = $uref->user_call;
227                                         } else {
228                                                 $c = "$ucall?";
229                                         }
230                                         if ((length $line) + (length $c) + 1 < 79) {
231                                                 $line .= $c . ' ';
232                                         } else {
233                                                 $line =~ s/\s+$//;
234                                                 push @out, $line;
235                                                 $line = ' ' x ($level*2) . "$call->$c ";
236                                         }
237                                 }
238                         }
239                 }
240                 $line =~ s/->$//g;
241                 $line =~ s/\s+$//;
242                 push @out, $line if length $line;
243         }
244         
245         # deal with more nodes
246         foreach my $ncall (sort @{$self->{nodes}}) {
247                 my $nref = Route::Node::get($ncall);
248
249                 if ($nref) {
250                         my $c = $nref->user_call;
251 #                       dbg("recursing from $call -> $c") if isdbg('routec');
252                         push @out, $nref->config($nodes_only, $level+1, $seen, @_);
253                 } else {
254                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_); 
255                 }
256         }
257
258         return @out;
259 }
260
261 sub cluster
262 {
263         my $nodes = Route::Node::count();
264         my $tot = Route::User::count();
265         my $users = scalar DXCommandmode::get_all();
266         my $maxusers = Route::User::max();
267         my $uptime = main::uptime();
268         
269         return " $nodes nodes, $users local / $tot total users  Max users $maxusers  Uptime $uptime";
270 }
271
272 #
273 # routing things
274 #
275
276 sub get
277 {
278         my $call = shift;
279         return Route::Node::get($call) || Route::User::get($call);
280 }
281
282 # find all the possible dxchannels which this object might be on
283 sub alldxchan
284 {
285         my $self = shift;
286         my @dxchan;
287 #       dbg("Trying node $self->{call}") if isdbg('routech');
288
289         my $dxchan = DXChannel->get($self->{call});
290         push @dxchan, $dxchan if $dxchan;
291         
292         # it isn't, build up a list of dxchannels and possible ping times 
293         # for all the candidates.
294         unless (@dxchan) {
295                 foreach my $p (@{$self->{parent}}) {
296 #                       dbg("Trying parent $p") if isdbg('routech');
297                         next if $p eq $main::mycall; # the root
298                         my $dxchan = DXChannel->get($p);
299                         if ($dxchan) {
300                                 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
301                         } else {
302                                 next if grep $p eq $_, @_;
303                                 my $ref = Route::Node::get($p);
304 #                               dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
305                                 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
306                         }
307                 }
308         }
309 #       dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
310         return @dxchan;
311 }
312
313 sub dxchan
314 {
315         my $self = shift;
316         
317         # ALWAYS return the locally connected channel if present;
318         my $dxchan = DXChannel->get($self->call);
319         return $dxchan if $dxchan;
320         
321         my @dxchan = $self->alldxchan;
322         return undef unless @dxchan;
323         
324         # determine the minimum ping channel
325         my $minping = 99999999;
326         foreach my $dxc (@dxchan) {
327                 my $p = $dxc->pingave;
328                 if (defined $p  && $p < $minping) {
329                         $minping = $p;
330                         $dxchan = $dxc;
331                 }
332         }
333         $dxchan = shift @dxchan unless $dxchan;
334         return $dxchan;
335 }
336
337
338
339 #
340 # track destruction
341 #
342
343 sub DESTROY
344 {
345         my $self = shift;
346         my $pkg = ref $self;
347         
348         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
349 }
350
351 no strict;
352 #
353 # return a list of valid elements 
354
355
356 sub fields
357 {
358         my $pkg = shift;
359         $pkg = ref $pkg if ref $pkg;
360     my $val = "${pkg}::valid";
361         my @out = keys %$val;
362         push @out, keys %valid;
363         return @out;
364 }
365
366 #
367 # return a prompt for a field
368 #
369
370 sub field_prompt
371
372         my ($self, $ele) = @_;
373         my $pkg = ref $self;
374     my $val = "${pkg}::valid";
375         return $val->{$ele} || $valid{$ele};
376 }
377
378 #
379 # generic AUTOLOAD for accessors
380 #
381 sub AUTOLOAD
382 {
383         my $self = shift;
384         no strict;
385         my $name = $AUTOLOAD;
386         return if $name =~ /::DESTROY$/;
387         $name =~ s/^.*:://o;
388   
389         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
390
391         # this clever line of code creates a subroutine which takes over from autoload
392         # from OO Perl - Conway
393         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
394         &$AUTOLOAD($self, @_);
395
396 #    @_ ? $self->{$name} = shift : $self->{$name} ;
397 }
398
399 1;