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