add pc59 encode and decode
[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                   lastseen => '0,Last Seen,atime',
41                  );
42
43 $filterdef = bless ([
44                           # tag, sort, field, priv, special parser 
45                           ['channel', 'c', 0],
46                           ['channel_dxcc', 'nc', 1],
47                           ['channel_itu', 'ni', 2],
48                           ['channel_zone', 'nz', 3],
49                           ['call', 'c', 4],
50                           ['by', 'c', 4],
51                           ['call_dxcc', 'nc', 5],
52                           ['by_dxcc', 'nc', 5],
53                           ['call_itu', 'ni', 6],
54                           ['by_itu', 'ni', 6],
55                           ['call_zone', 'nz', 7],
56                           ['by_zone', 'nz', 7],
57                           ['channel_state', 'ns', 8],
58                           ['call_state', 'ns', 9],
59                           ['by_state', 'ns', 9],
60                          ], 'Filter::Cmd');
61
62
63 sub new
64 {
65         my ($pkg, $call) = @_;
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} = here(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 is_empty
134 {
135         my $self = shift;
136         return @{$self->{$_[0]}} == 0;
137 }
138
139 #
140 # flag field constructors/enquirers
141 #
142 # These can be called in various ways:-
143 #
144 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
145 # Route::here(1) returns 2 (the bit value of the here flag)
146 # $ref->here(1) or $ref->here(0) sets the here flag
147 #
148
149 sub here
150 {
151         my $self = shift;
152         my $r = shift;
153         return $self ? 2 : 0 unless ref $self;
154         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
155         $self->{flags} &= ~2;
156         $self->{flags} |= $r ? 2 : 0;
157         return $r ? 1 : 0;
158 }
159
160 sub conf
161 {
162         my $self = shift;
163         my $r = shift;
164         return $self ? 1 : 0 unless ref $self;
165         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
166         $self->{flags} &= ~1;
167         $self->{flags} |= $r ? 1 : 0;
168         return $r ? 1 : 0;
169 }
170
171 #
172 # pc59 entity encoding and decoding
173 #
174 sub enc_pc59
175 {
176         my $self = shift;
177         my $sort = shift || 'N';
178         my $out = "$sort$self->{flag}$self->{call}";
179         if ($self->{build}) {
180                 $out .= "b$self->{build}";
181         } elsif ($self->{version}) {
182                 $out .= "v$self->{version}"; 
183         }
184 }
185
186 sub dec_pc59
187 {
188         my $node = shift;
189         my $s = ref($node) ? shift : $node;
190         $node = undef;
191         
192         my ($sort, $here, $call) = unpack "A A A*", $s;
193         return unless is_callsign($call);
194         return unless $here =~ /^[0123]$/;
195         return unless $sort =~ /^[NUE]$/;
196         if ($sort eq 'E' || $sort eq 'N') {
197                 $node = Route::Node::get($call) || Route::Node->new($call);
198                 if ($s =~ /b([\d\.])/) {
199                         $node->{build} = $1;
200                 }
201                 if ($s =~ /v([\d\.])/) {
202                         $node->{version} = $1;
203                 }
204         } elsif ($sort eq 'U') {
205                 $node = Route::User::get($call) || Route::User->new($call);
206         }
207         $node->flags = $here;
208         return $node;
209 }
210
211
212 # display routines
213 #
214
215 sub user_call
216 {
217         my $self = shift;
218         my $call = sprintf "%s", $self->{call};
219         return $self->here ? "$call" : "($call)";
220 }
221
222 sub config
223 {
224         my $self = shift;
225         my $nodes_only = shift;
226         my $level = shift;
227         my $seen = shift;
228         my @out;
229         my $line;
230         my $call = $self->user_call;
231         my $printit = 1;
232
233         # allow ranges
234         if (@_) {
235                 $printit = grep $call =~ m|$_|, @_;
236         }
237
238         if ($printit) {
239                 $line = ' ' x ($level*2) . "$call";
240                 $call = ' ' x length $call; 
241                 
242                 # recursion detector
243                 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
244 #                       $line .= ' ...';
245 #                       push @out, $line;
246                         return @out;
247                 }
248                 push @$seen, $self->{call};
249
250                 # print users
251                 unless ($nodes_only) {
252                         if (@{$self->{users}}) {
253                                 $line .= '->';
254                                 foreach my $ucall (sort @{$self->{users}}) {
255                                         my $uref = Route::User::get($ucall);
256                                         my $c;
257                                         if ($uref) {
258                                                 $c = $uref->user_call;
259                                         } else {
260                                                 $c = "$ucall?";
261                                         }
262                                         if ((length $line) + (length $c) + 1 < 79) {
263                                                 $line .= $c . ' ';
264                                         } else {
265                                                 $line =~ s/\s+$//;
266                                                 push @out, $line;
267                                                 $line = ' ' x ($level*2) . "$call->$c ";
268                                         }
269                                 }
270                         }
271                 }
272                 $line =~ s/->$//g;
273                 $line =~ s/\s+$//;
274                 push @out, $line if length $line;
275         }
276         
277         # deal with more nodes
278         foreach my $ncall (sort @{$self->{nodes}}) {
279                 my $nref = Route::Node::get($ncall);
280
281                 if ($nref) {
282                         my $c = $nref->user_call;
283 #                       dbg("recursing from $call -> $c") if isdbg('routec');
284                         push @out, $nref->config($nodes_only, $level+1, $seen, @_);
285                 } else {
286                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_); 
287                 }
288         }
289
290         return @out;
291 }
292
293 sub cluster
294 {
295         my $nodes = Route::Node::count();
296         my $tot = Route::User::count();
297         my $users = scalar DXCommandmode::get_all();
298         my $maxusers = Route::User::max();
299         my $uptime = main::uptime();
300         
301         return " $nodes nodes, $users local / $tot total users  Max users $maxusers  Uptime $uptime";
302 }
303
304 #
305 # routing things
306 #
307
308 sub get
309 {
310         my $call = shift;
311         return Route::Node::get($call) || Route::User::get($call);
312 }
313
314 sub get_all
315 {
316         return (Route::Node::get_all(), Route::User::get_all());
317 }
318
319 # find all the possible dxchannels which this object might be on
320 sub alldxchan
321 {
322         my $self = shift;
323
324         my $dxchan = DXChannel->get($self->{call});
325         if ($dxchan) {
326                 dbg("alldxchan for $self->{call} = $dxchan->{call}") if isdbg('routelow');
327                 return $dxchan if $dxchan;
328         }
329         
330         my @nodes;
331         if ($self->isa('Route::User')) {
332                 push @nodes, map{Route::Node::get($_)} @{$self->{nodes}};
333         } elsif ($self->isa('Route::Node')) {
334                 push @nodes, $self;
335         }
336         
337         # it isn't, build up a list of dxchannels and possible ping times 
338         # for all the candidates.
339         my @dxchan;
340         foreach my $nref (@nodes) {
341                 next unless $nref;
342                 foreach my $p (@{$nref->{dxchan}}) {
343 #                       dbg("Trying dxchan $p") if isdbg('routech');
344                         next if $p eq $main::mycall; # the root
345                         my $dxchan = DXChannel->get($p);
346                         if ($dxchan) {
347                                 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
348                         } else {
349                                 next if grep $p eq $_, @_;
350                                 my $ref = Route::Node::get($p);
351 #                               dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
352                                 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
353                         }
354                 }
355         }
356         dbg("alldxchan for $self->{call} = (" . join(',', @dxchan) . ")") if isdbg('routelow');
357         return @dxchan;
358 }
359
360 sub bestdxchan
361 {
362         my $self = shift;
363         
364         # ALWAYS return the locally connected channel if present;
365         my $dxchan = DXChannel->get($self->call);
366         return $dxchan if $dxchan;
367         
368         my @dxchan = sort { ($a->pingave || 9999999) <=> ($b->pingave || 9999999) } $self->alldxchan;
369         return undef unless @dxchan;
370         
371         return shift @dxchan;
372 }
373
374 #
375 # track destruction
376 #
377
378 sub DESTROY
379 {
380         my $self = shift;
381         my $pkg = ref $self;
382         
383         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
384 }
385
386 no strict;
387 #
388 # return a list of valid elements 
389
390
391 sub fields
392 {
393         my $pkg = shift;
394         $pkg = ref $pkg if ref $pkg;
395     my $val = "${pkg}::valid";
396         my @out = keys %$val;
397         push @out, keys %valid;
398         return @out;
399 }
400
401 #
402 # return a prompt for a field
403 #
404
405 sub field_prompt
406
407         my ($self, $ele) = @_;
408         my $pkg = ref $self;
409     my $val = "${pkg}::valid";
410         return $val->{$ele} || $valid{$ele};
411 }
412
413 #
414 # generic AUTOLOAD for accessors
415 #
416 sub AUTOLOAD
417 {
418         no strict;
419         my $name = $AUTOLOAD;
420         return if $name =~ /::DESTROY$/;
421         $name =~ s/^.*:://o;
422   
423         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
424
425         # this clever line of code creates a subroutine which takes over from autoload
426         # from OO Perl - Conway
427         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
428        goto &$AUTOLOAD;
429
430 }
431
432 1;