added code to isolate things from rest o network on startup
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXCluster;
19 use DXProtVars;
20 use DXCommandmode;
21 use DXLog;
22 use Spot;
23 use DXProtout;
24 use DXDebug;
25 use Local;
26
27 use Carp;
28
29 use strict;
30 use vars qw($me $pc11_max_age $pc11_dup_age $pc23_dup_age 
31                         %spotdup %wwvdup $last_hour %pings %rcmds 
32                         %nodehops @baddx $baddxfn);
33
34 $me = undef;                                    # the channel id for this cluster
35 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
36 $pc11_dup_age = 24*3600;                # the maximum time to keep the spot dup list for
37 $pc23_dup_age = 24*3600;                # the maximum time to keep the wwv dup list for
38 %spotdup = ();                              # the pc11 and 26 dup hash 
39 %wwvdup = ();                               # the pc23 and 27 dup hash 
40 $last_hour = time;                              # last time I did an hourly periodic update
41 %pings = ();                    # outstanding ping requests outbound
42 %rcmds = ();                    # outstanding rcmd requests outbound
43 %nodehops = ();                 # node specific hop control
44 @baddx = ();                    # list of illegal spotted callsigns
45
46 $baddxfn = "$main::data/baddx.pl";
47
48 sub init
49 {
50         my $user = DXUser->get($main::mycall);
51         $DXProt::myprot_version += $main::version*100;
52         $me = DXProt->new($main::mycall, 0, $user); 
53         $me->{here} = 1;
54         $me->{state} = "indifferent";
55         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
56         confess $@ if $@;
57         #  $me->{sort} = 'M';    # M for me
58
59         # now prime the spot duplicates file with today's and yesterday's data
60     my @today = Julian::unixtoj(time);
61         my @spots = Spot::readfile(@today);
62         @today = Julian::sub(@today, 1);
63         push @spots, Spot::readfile(@today);
64         for (@spots) {
65                 my $dupkey = "$_->[0]$_->[1]$_->[2]$_->[3]$_->[4]";
66                 $spotdup{$dupkey} = $_->[2];
67         }
68
69         # now prime the wwv duplicates file with just this month's data
70         my @wwv = Geomag::readfile(time);
71         for (@wwv) {
72                 my $dupkey = "$_->[1].$_->[2]$_->[3]$_->[4]";
73                 $wwvdup{$dupkey} = $_->[1];
74         }
75
76         # load the baddx file
77         do "$baddxfn" if -e "$baddxfn";
78         print "$@\n" if $@;
79 }
80
81 #
82 # obtain a new connection this is derived from dxchannel
83 #
84
85 sub new 
86 {
87         my $self = DXChannel::alloc(@_);
88         $self->{'sort'} = 'A';          # in absence of how to find out what sort of an object I am
89         return $self;
90 }
91
92 # this is how a pc connection starts (for an incoming connection)
93 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
94 # all the crap that comes between).
95 sub start
96 {
97         my ($self, $line, $sort) = @_;
98         my $call = $self->{call};
99         my $user = $self->{user};
100         
101         # remember type of connection
102         $self->{consort} = $line;
103         $self->{outbound} = $sort eq 'O';
104         $self->{priv} = $user->priv;
105         $self->{lang} = $user->lang;
106         $self->{isolate} = $user->{isolate};
107         $self->{consort} = $line;       # save the connection type
108         $self->{here} = 1;
109         
110         # set unbuffered
111         $self->send_now('B',"0");
112         
113         # send initialisation string
114         if (!$self->{outbound}) {
115                 $self->send(pc38()) if DXNode->get_all();
116                 $self->send(pc18());
117         }
118         $self->state('init');
119         $self->pc50_t(time);
120
121         Log('DXProt', "$call connected");
122 }
123
124 #
125 # This is the normal pcxx despatcher
126 #
127 sub normal
128 {
129         my ($self, $line) = @_;
130         my @field = split /\^/, $line;
131         pop @field if $field[-1] eq '~';
132         
133 #       print join(',', @field), "\n";
134                                                 
135         # ignore any lines that don't start with PC
136         return if !$field[0] =~ /^PC/;
137         
138         # process PC frames
139         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
140         return unless $pcno;
141         return if $pcno < 10 || $pcno > 51;
142         
143         # local processing 1
144         my $pcr;
145         eval {
146                 $pcr = Local::pcprot($self, $pcno, @field);
147         };
148 #       dbg('local', "Local::pcprot error $@") if $@;
149         return if $pcr;
150         
151  SWITCH: {
152                 if ($pcno == 10) {              # incoming talk
153                         
154                         # is it for me or one of mine?
155                         my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
156                         if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
157                                 
158                                 # yes, it is
159                                 my $text = unpad($field[3]);
160                                 Log('talk', $call, $field[1], $field[6], $text);
161                                 $call = $main::myalias if $call eq $main::mycall;
162                                 my $ref = DXChannel->get($call);
163                                 $ref->send("$call de $field[1]: $text") if $ref && $ref->{talk};
164                         } else {
165                                 route($field[2], $line); # relay it on its way
166                         }
167                         return;
168                 }
169                 
170                 if ($pcno == 11 || $pcno == 26) { # dx spot
171                         
172                         # if this is a 'nodx' node then ignore it
173                         last SWITCH if grep $field[7] =~ /^$_/,  @DXProt::nodx_node;
174                         
175                         # convert the date to a unix date
176                         my $d = cltounix($field[3], $field[4]);
177                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
178                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
179                                 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
180                                 return;
181                         }
182
183                         # strip off the leading & trailing spaces from the comment
184                         my $text = unpad($field[5]);
185                         
186                         # store it away
187                         my $spotter = $field[6];
188                         $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
189                         
190                         # do some de-duping
191                         my $freq = $field[1] - 0;
192                         my $dupkey = "$freq$field[2]$d$text$spotter";
193                         if ($spotdup{$dupkey}) {
194                                 dbg('chan', "Duplicate Spot ignored\n");
195                                 return;
196                         }
197                         
198                         $spotdup{$dupkey} = $d;
199
200                         # is it 'baddx'
201                         if (grep $field[2] eq $_, @baddx) {
202                                 dbg('chan', "Bad DX spot, ignored");
203                                 return;
204                         }
205                         
206                         my @spot = Spot::add($freq, $field[2], $d, $text, $spotter, $field[7]);
207
208             #
209                         # @spot at this point contains:-
210             # freq, spotted call, time, text, spotter, spotted cc, spotters cc,
211             # orig node, spotted itu, spotted cq, spotters itu, spotters cq
212                         # you should be able to route on any of these
213             #
214                         
215                         # local processing 
216                         my $r;
217                         eval {
218                                 $r = Local::spot($self, $freq, $field[2], $d, $text, $spotter, $field[7]);
219                         };
220 #                       dbg('local', "Local::spot1 error $@") if $@;
221                         return if $r;
222
223                         # send orf to the users
224                         if (@spot && $pcno == 11) {
225                                 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
226                                 broadcast_users("$buf\a\a", 'dx', $spot[0]);
227                         }
228
229                         # DON'T be silly and send on PC26s!
230                         return if $pcno == 26;
231                         
232                         last SWITCH;
233                 }
234                 
235                 if ($pcno == 12) {              # announces
236                         
237                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
238                                 
239                                 # strip leading and trailing stuff
240                                 my $text = unpad($field[3]);
241                                 my $target;
242                                 my $to = 'To ';
243                                 my @list;
244                                 
245                                 if ($field[4] eq '*') { # sysops
246                                         $target = "SYSOP";
247                                         @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
248                                 } elsif ($field[4] gt ' ') { # speciality list handling
249                                         my ($name) = split /\./, $field[4]; 
250                                         $target = "$name"; # put the rest in later (if bothered) 
251                                 } 
252                                 
253                                 if ($field[6] eq '1') {
254                                         $target = "WX"; 
255                                         $to = '';
256                                 }
257                                 $target = "All" if !$target;
258                                 
259                                 if (@list > 0) {
260                                         broadcast_list("$to$target de $field[1]: $text", 'ann', undef, @list);
261                                 } else {
262                                         broadcast_users("$target de $field[1]: $text", 'ann', undef);
263                                 }
264                                 Log('ann', $target, $field[1], $text);
265                                 
266                                 return if $field[2] eq $main::mycall; # it's routed to me
267                         } else {
268                                 route($field[2], $line);
269                                 return;                 # only on a routed one
270                         }
271                         
272                         last SWITCH;
273                 }
274                 
275                 if ($pcno == 13) {
276                         last SWITCH;
277                 }
278                 if ($pcno == 14) {
279                         last SWITCH;
280                 }
281                 if ($pcno == 15) {
282                         last SWITCH;
283                 }
284                 
285                 if ($pcno == 16) {              # add a user
286                         my $node = DXCluster->get_exact($field[1]); 
287                         last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
288                         last SWITCH unless $node->isa('DXNode');
289                         my $i;
290                         
291                         
292                         for ($i = 2; $i < $#field; $i++) {
293                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
294                                 next if !$call || length $call < 3 || length $call > 8;
295                                 next if !$confmode;
296                                 $call = uc $call;
297                                 next if DXCluster->get_exact($call); # we already have this (loop?)
298                                 
299                                 $confmode = $confmode eq '*';
300                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
301                                 
302                                 # add this station to the user database, if required
303                                 $call =~ s/-\d+$//o;        # remove ssid for users
304                                 my $user = DXUser->get_current($call);
305                                 $user = DXUser->new($call) if !$user;
306                                 $user->homenode($node->call) if !$user->homenode;
307                                 $user->node($node->call);
308                                 $user->lastin($main::systime);
309                                 $user->put;
310                         }
311                         
312                         # queue up any messages (look for privates only)
313                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
314                         last SWITCH;
315                 }
316                 
317                 if ($pcno == 17) {              # remove a user
318                         
319                         my $ref = DXCluster->get_exact($field[1]);
320                         $ref->del() if $ref;
321                         last SWITCH;
322                 }
323                 
324                 if ($pcno == 18) {              # link request
325                         $self->send_local_config();
326                         $self->send(pc20());
327                         $self->state('init');   
328                         return;             # we don't pass these on
329                 }
330                 
331                 if ($pcno == 19) {              # incoming cluster list
332                         my $i;
333                         for ($i = 1; $i < $#field-1; $i += 4) {
334                                 my $here = $field[$i];
335                                 my $call = uc $field[$i+1];
336                                 my $confmode = $field[$i+2] eq '*';
337                                 my $ver = $field[$i+3];
338                                 
339                                 # now check the call over
340                                 next if DXCluster->get_exact($call); # we already have this
341                                 
342                                 # check for sane parameters
343                                 next if $ver < 5000; # only works with version 5 software
344                                 next if length $call < 3; # min 3 letter callsigns
345                                 DXNode->new($self, $call, $confmode, $here, $ver);
346                                 
347                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
348                                 my $mref = DXMsg::get_busy($call);
349                                 $mref->stop_msg($self) if $mref;
350                                 
351                                 # add this station to the user database, if required (don't remove SSID from nodes)
352                                 my $user = DXUser->get_current($call);
353                                 if (!$user) {
354                                         $user = DXUser->new($call);
355                                         $user->sort('A');
356                                         $user->priv(1);                   # I have relented and defaulted nodes
357                                         $self->{priv} = 1;                # to user RCMDs allowed
358                                         $user->homenode($call);
359                                         $user->node($call);
360                                 }
361                                 $user->lastin($main::systime);
362                                 $user->put;
363                         }
364                         
365                         # queue up any messages
366                         DXMsg::queue_msg(0) if $self->state eq 'normal';
367                         last SWITCH;
368                 }
369                 
370                 if ($pcno == 20) {              # send local configuration
371                         $self->send_local_config();
372                         $self->send(pc22());
373                         $self->state('normal');
374                         
375                         # queue mail
376                         DXMsg::queue_msg(0);
377                         return;
378                 }
379                 
380                 if ($pcno == 21) {              # delete a cluster from the list
381                         my $call = uc $field[1];
382                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
383                                 my $ref = DXCluster->get_exact($call);
384                                 $ref->del() if $ref;
385                         }
386                         last SWITCH;
387                 }
388                 
389                 if ($pcno == 22) {
390                         $self->state('normal');
391                         
392                         # queue mail
393                         DXMsg::queue_msg(0);
394                         return;
395                 }
396                 
397                 if ($pcno == 23 || $pcno == 27) { # WWV info
398                         # do some de-duping
399                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
400                         my $sfi = unpad($field[3]);
401                         my $k = unpad($field[4]);
402                         my $i = unpad($field[5]);
403                         my $dupkey = "$d.$sfi$k$i";
404                         if ($wwvdup{$dupkey}) {
405                                 dbg('chan', "Dup WWV Spot ignored\n");
406                                 return;
407                         }
408                         if ($d > $main::systime + 900 || $field[2] < 0 || $field[2] > 23) {
409                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
410                                 return;
411                         }
412                         $wwvdup{$dupkey} = $d;
413                         $field[6] =~ s/-\d+$//o;            # remove spotter's ssid
414                 
415                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..$#field]);
416
417                         my $r;
418                         eval {
419                                 $r = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
420                         };
421 #                       dbg('local', "Local::wwv2 error $@") if $@;
422                         return if $r;
423
424                         # DON'T be silly and send on PC27s!
425                         return if $pcno == 27;
426
427                         # broadcast to the eager users
428                         broadcast_users("WWV de $field[7] <$field[2]>:   SFI=$sfi, K=$k, A=$i, $field[6]", 'wwv', $wwv );
429                         last SWITCH;
430                 }
431                 
432                 if ($pcno == 24) {              # set here status
433                         my $call = uc $field[1];
434                         my $ref = DXCluster->get_exact($call);
435                         $ref->here($field[2]) if $ref;
436                         last SWITCH;
437                 }
438                 
439                 if ($pcno == 25) {      # merge request
440                         unless ($field[1] eq $main::mycall) {
441                                 dbg('chan', "merge request to $field[1] from $field[2] ignored");
442                                 return;
443                         }
444
445                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
446                         
447                         # spots
448                         if ($field[3] > 0) {
449                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]-1);
450                                 my $in;
451                                 foreach $in (@in) {
452                                         $self->send(pc26(@{$in}[0..4], $in->[7]));
453                                 }
454                         }
455
456                         # wwv
457                         if ($field[4] > 0) {
458                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
459                                 my $in;
460                                 foreach $in (@in) {
461                                         $self->send(pc27(@{$in}));
462                                 }
463                         }
464                         return;
465                 }
466                 
467                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
468                         DXMsg::process($self, $line);
469                         return;
470                 }
471                 
472                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
473                         if ($field[1] eq $main::mycall) {
474                                 my $ref = DXUser->get_current($field[2]);
475                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
476                                 unless ($field[3] =~ /rcmd/i) {    # not allowed to relay RCMDS!
477                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
478                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
479                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
480                                                 for (@in) {
481                                                         s/\s*$//og;
482                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
483                                                         Log('rcmd', 'out', $field[2], $_);
484                                                 }
485                                                 delete $self->{remotecmd};
486                                         }
487                                 } else {
488                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
489                                 }
490                         } else {
491                                 route($field[1], $line);
492                         }
493                         return;
494                 }
495                 
496                 if ($pcno == 35) {              # remote command replies
497                         if ($field[1] eq $main::mycall) {
498                                 my $s = $rcmds{$field[2]};
499                                 if ($s) {
500                                         my $dxchan = DXChannel->get($s->{call});
501                                         $dxchan->send($field[3]) if $dxchan;
502                                         delete $rcmds{$field[2]} if !$dxchan;
503                                 }
504                         } else {
505                                 route($field[1], $line);
506                         }
507                         return;
508                 }
509                 
510                 if ($pcno == 37) {
511                         last SWITCH;
512                 }
513                 
514                 if ($pcno == 38) {              # node connected list from neighbour
515                         return;
516                 }
517                 
518                 if ($pcno == 39) {              # incoming disconnect
519                         $self->disconnect();
520                         return;
521                 }
522                 
523                 if ($pcno == 41) {              # user info
524                         # add this station to the user database, if required
525                         my $user = DXUser->get_current($field[1]);
526                         if (!$user) {
527                                 # then try without an SSID
528                                 $field[1] =~ s/-\d+$//o;
529                                 $user = DXUser->get_current($field[1]);
530                         }
531                         $user = DXUser->new($field[1]) if !$user;
532                         
533                         if ($field[2] == 1) {
534                                 $user->name($field[3]);
535                         } elsif ($field[2] == 2) {
536                                 $user->qth($field[3]);
537                         } elsif ($field[2] == 3) {
538                                 my ($lat, $long) = DXBearing::stoll($field[3]);
539                                 $user->lat($lat);
540                                 $user->long($long);
541                         } elsif ($field[2] == 4) {
542                                 $user->homenode($field[3]);
543                         }
544                         $user->put;
545                         last SWITCH;
546                 }
547                 if ($pcno == 43) {
548                         last SWITCH;
549                 }
550                 if ($pcno == 44) {
551                         last SWITCH;
552                 }
553                 if ($pcno == 45) {
554                         last SWITCH;
555                 }
556                 if ($pcno == 46) {
557                         last SWITCH;
558                 }
559                 if ($pcno == 47) {
560                         last SWITCH;
561                 }
562                 if ($pcno == 48) {
563                         last SWITCH;
564                 }
565                 
566                 if ($pcno == 50) {              # keep alive/user list
567                         my $ref = DXCluster->get_exact($field[1]);
568                         $ref->update_users($field[2]) if $ref;                  
569                         last SWITCH;
570                 }
571                 
572                 if ($pcno == 51) {              # incoming ping requests/answers
573                         
574                         # is it for us?
575                         if ($field[1] eq $main::mycall) {
576                                 my $flag = $field[3];
577                                 if ($flag == 1) {
578                                         $self->send(pc51($field[2], $field[1], '0'));
579                                 } else {
580                                         # it's a reply, look in the ping list for this one
581                                         my $ref = $pings{$field[2]};
582                                         if ($ref) {
583                                                 my $r = shift @$ref;
584                                                 my $dxchan = DXChannel->get($r->{call});
585                                                 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
586                                         }
587                                 }
588                                 
589                         } else {
590                                 # route down an appropriate thingy
591                                 route($field[1], $line);
592                         }
593                         return;
594                 }
595         }
596          
597          # if get here then rebroadcast the thing with its Hop count decremented (if
598          # there is one). If it has a hop count and it decrements to zero then don't
599          # rebroadcast it.
600          #
601          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
602          #        REBROADCAST!!!!
603          #
604          
605         if (!$self->{isolate}) {
606                 broadcast_ak1a($line, $self); # send it to everyone but me
607         }
608 }
609
610 #
611 # This is called from inside the main cluster processing loop and is used
612 # for despatching commands that are doing some long processing job
613 #
614 sub process
615 {
616         my $t = time;
617         my @dxchan = DXChannel->get_all();
618         my $dxchan;
619         
620         foreach $dxchan (@dxchan) {
621                 next unless $dxchan->is_ak1a();
622                 next if $dxchan == $me;
623                 
624                 # send a pc50 out on this channel
625                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
626                         $dxchan->send(pc50());
627                         $dxchan->pc50_t($t);
628                 }
629         }
630         
631         my $key;
632         my $val;
633         my $cutoff;
634         if ($main::systime - 3600 > $last_hour) {
635                 $cutoff  = $main::systime - $pc11_dup_age;
636                 while (($key, $val) = each %spotdup) {
637                         delete $spotdup{$key} if $val < $cutoff;
638                 }
639                 $cutoff = $main::systime - $pc23_dup_age;
640                 while (($key, $val) = each %wwvdup) {
641                         delete $wwvdup{$key} if $val < $cutoff;
642                 }
643                 $last_hour = $main::systime;
644         }
645 }
646
647 #
648 # finish up a pc context
649 #
650 sub finish
651 {
652         my $self = shift;
653         my $call = $self->call;
654         my $ref = DXCluster->get_exact($call);
655         
656         # unbusy and stop and outgoing mail
657         my $mref = DXMsg::get_busy($call);
658         $mref->stop_msg($self) if $mref;
659         
660         # broadcast to all other nodes that all the nodes connected to via me are gone
661         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
662         my $node;
663         
664         foreach $node (@gonenodes) {
665                 next if $node->call eq $call;
666                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
667                 $node->del();
668         }
669
670         # remove outstanding pings
671         delete $pings{$call};
672         
673         # now broadcast to all other ak1a nodes that I have gone
674         broadcast_ak1a(pc21($call, 'Gone.'), $self);
675         
676         Log('DXProt', $call . " Disconnected");
677         $ref->del() if $ref;
678 }
679
680 #
681 # some active measures
682 #
683
684 sub send_local_config
685 {
686         my $self = shift;
687         my $n;
688         my @nodes;
689         
690         # send our nodes
691         if ($self->{isolate}) {
692                 @nodes = (DXCluster->get_exact($main::mycall));
693         } else {
694                 # create a list of all the nodes that are not connected to this connection
695                 # and are not themselves isolated, this to make sure that isolated nodes
696         # don't appear outside of this node
697                 @nodes = DXNode::get_all(); 
698                 @nodes = grep { $_->dxchan != $self && !$_->{isolate} } @nodes;
699         }
700
701         my @s = $me->pc19(@nodes);
702         for (@s) {
703                 my $routeit = adjust_hops($self, $_);
704                 $self->send($routeit) if $routeit;
705         }
706         
707         # get all the users connected on the above nodes and send them out
708         foreach $n (@nodes) {
709                 my @users = values %{$n->list};
710                 my @s = pc16($n, @users);
711                 for (@s) {
712                         my $routeit = adjust_hops($self, $_);
713                         $self->send($routeit) if $routeit;
714                 }
715         }
716 }
717
718 #
719 # route a message down an appropriate interface for a callsign
720 #
721 # is called route(to, pcline);
722 #
723 sub route
724 {
725         my ($call, $line) = @_;
726         my $cl = DXCluster->get_exact($call);
727         if ($cl) {
728                 my $hops;
729                 my $dxchan = $cl->{dxchan};
730                 if ($dxchan) {
731                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
732                         if ($routeit) {
733                                 $dxchan->send($routeit) if $dxchan;
734                         }
735                 }
736         }
737 }
738
739 # broadcast a message to all clusters [except those mentioned after buffer]
740 sub broadcast_ak1a
741 {
742         my $s = shift;                          # the line to be rebroadcast
743         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
744         my @dxchan = get_all_ak1a();
745         my $dxchan;
746         
747         # send it if it isn't the except list and isn't isolated and still has a hop count
748         foreach $dxchan (@dxchan) {
749                 next if grep $dxchan == $_, @except;
750                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
751                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
752         }
753 }
754
755 # broadcast to all users
756 # storing the spot or whatever until it is in a state to receive it
757 sub broadcast_users
758 {
759         my $s = shift;                          # the line to be rebroadcast
760         my $sort = shift;           # the type of transmission
761         my $fref = shift;           # a reference to an object to filter on
762         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
763         my @dxchan = get_all_users();
764         my $dxchan;
765         my @out;
766         
767         foreach $dxchan (@dxchan) {
768                 next if grep $dxchan == $_, @except;
769                 push @out, $dxchan;
770         }
771         broadcast_list($s, $sort, $fref, @out);
772 }
773
774 # broadcast to a list of users
775 sub broadcast_list
776 {
777         my $s = shift;
778         my $sort = shift;
779         my $fref = shift;
780         my $dxchan;
781         
782         foreach $dxchan (@_) {
783                 
784                 next if $sort eq 'dx' && !$dxchan->{dx};
785                 next if $sort eq 'ann' && !$dxchan->{ann};
786                 next if $sort eq 'wwv' && !$dxchan->{wwv};
787                 next if $sort eq 'wx' && !$dxchan->{wx};
788
789                 $s =~ s/\a//og unless $dxchan->{beep};
790                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
791                         $dxchan->send($s);      
792                 } else {
793                         $dxchan->delay($s);
794                 }
795         }
796 }
797
798 #
799 # gimme all the ak1a nodes
800 #
801 sub get_all_ak1a
802 {
803         my @list = DXChannel->get_all();
804         my $ref;
805         my @out;
806         foreach $ref (@list) {
807                 push @out, $ref if $ref->is_ak1a;
808         }
809         return @out;
810 }
811
812 # return a list of all users
813 sub get_all_users
814 {
815         my @list = DXChannel->get_all();
816         my $ref;
817         my @out;
818         foreach $ref (@list) {
819                 push @out, $ref if $ref->is_user;
820         }
821         return @out;
822 }
823
824 # return a list of all user callsigns
825 sub get_all_user_calls
826 {
827         my @list = DXChannel->get_all();
828         my $ref;
829         my @out;
830         foreach $ref (@list) {
831                 push @out, $ref->call if $ref->is_user;
832         }
833         return @out;
834 }
835
836 #
837 # obtain the hops from the list for this callsign and pc no 
838 #
839
840 sub get_hops
841 {
842         my ($pcno) = @_;
843         my $hops = $DXProt::hopcount{$pcno};
844         $hops = $DXProt::def_hopcount if !$hops;
845         return "H$hops";       
846 }
847
848
849 # adjust the hop count on a per node basis using the user loadable 
850 # hop table if available or else decrement an existing one
851 #
852
853 sub adjust_hops
854 {
855         my $self = shift;
856         my $s = shift;
857         my $call = $self->{call};
858         my $hops;
859         
860         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
861                 my ($pcno) = $s =~ /^PC(\d\d)/o;
862                 confess "$call called adjust_hops with '$s'" unless $pcno;
863                 my $ref = $nodehops{$call} if %nodehops;
864                 if ($ref) {
865                         my $newhops = $ref->{$pcno};
866                         return "" if defined $newhops && $newhops == 0;
867                         $newhops = $ref->{default} unless $newhops;
868                         return "" if defined $newhops && $newhops == 0;
869                         $newhops = $hops if !$newhops;
870                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
871                 } else {
872                         # simply decrement it
873                         $hops--;
874                         return "" if !$hops;
875                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
876                 }
877         }
878         return $s;
879 }
880
881
882 # load hop tables
883 #
884 sub load_hops
885 {
886         my $self = shift;
887         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
888         do "$main::data/hop_table.pl";
889         return $@ if $@;
890         return 0;
891 }
892
893 # remove leading and trailing spaces from an input string
894 sub unpad
895 {
896         my $s = shift;
897         $s =~ s/^\s+|\s+$//;
898         return $s;
899 }
900
901 # add a ping request to the ping queues
902 sub addping
903 {
904         my ($from, $to) = @_;
905         my $ref = $pings{$to};
906         $ref = $pings{$to} = [] if !$ref;
907         my $r = {};
908         $r->{call} = $from;
909         $r->{t} = $main::systime;
910         route($to, pc51($to, $main::mycall, 1));
911         push @$ref, $r;
912 }
913
914 # add a rcmd request to the rcmd queues
915 sub addrcmd
916 {
917         my ($from, $to, $cmd) = @_;
918         my $r = {};
919         $r->{call} = $from;
920         $r->{t} = $main::systime;
921         $r->{cmd} = $cmd;
922         route($to, pc34($main::mycall, $to, $cmd));
923         $rcmds{$to} = $r;
924 }
925 1;
926 __END__