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