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