1. fixed problem with missing DXDebug in DXProt.
[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);
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                         my $i;
248                         
249                         
250                         for ($i = 2; $i < $#field; $i++) {
251                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
252                                 next if length $call < 3;
253                                 next if !$confmode;
254                                 $call = uc $call;
255                                 next if DXCluster->get_exact($call); # we already have this (loop?)
256                                 
257                                 $confmode = $confmode eq '*';
258                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
259                                 
260                                 # add this station to the user database, if required
261                                 $call =~ s/-\d+$//o;        # remove ssid for users
262                                 my $user = DXUser->get_current($call);
263                                 $user = DXUser->new($call) if !$user;
264                                 $user->homenode($node->call) if !$user->homenode;
265                                 $user->node($node->call);
266                                 $user->lastin($main::systime);
267                                 $user->put;
268                         }
269                         
270                         # queue up any messages (look for privates only)
271                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
272                         last SWITCH;
273                 }
274                 
275                 if ($pcno == 17) {              # remove a user
276                         
277                         my $ref = DXCluster->get_exact($field[1]);
278                         $ref->del() if $ref;
279                         last SWITCH;
280                 }
281                 
282                 if ($pcno == 18) {              # link request
283                         $self->send_local_config();
284                         $self->send(pc20());
285                         $self->state('init');   
286                         return;             # we don't pass these on
287                 }
288                 
289                 if ($pcno == 19) {              # incoming cluster list
290                         my $i;
291                         for ($i = 1; $i < $#field-1; $i += 4) {
292                                 my $here = $field[$i];
293                                 my $call = uc $field[$i+1];
294                                 my $confmode = $field[$i+2] eq '*';
295                                 my $ver = $field[$i+3];
296                                 
297                                 # now check the call over
298                                 next if DXCluster->get_exact($call); # we already have this
299                                 
300                                 # check for sane parameters
301                                 next if $ver < 5000; # only works with version 5 software
302                                 next if length $call < 3; # min 3 letter callsigns
303                                 DXNode->new($self, $call, $confmode, $here, $ver);
304                                 
305                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
306                                 my $mref = DXMsg::get_busy($call);
307                                 $mref->stop_msg($self) if $mref;
308                                 
309                                 # add this station to the user database, if required (don't remove SSID from nodes)
310                                 my $user = DXUser->get_current($call);
311                                 if (!$user) {
312                                         $user = DXUser->new($call);
313                                         $user->sort('A');
314                                         $user->priv(1);                   # I have relented and defaulted nodes
315                                         $self->{priv} = 1;                # to user RCMDs allowed
316                                         $user->homenode($call);
317                                         $user->node($call);
318                                 }
319                                 $user->lastin($main::systime);
320                                 $user->put;
321                         }
322                         
323                         # queue up any messages
324                         DXMsg::queue_msg(0) if $self->state eq 'normal';
325                         last SWITCH;
326                 }
327                 
328                 if ($pcno == 20) {              # send local configuration
329                         $self->send_local_config();
330                         $self->send(pc22());
331                         $self->state('normal');
332                         
333                         # queue mail
334                         DXMsg::queue_msg(0);
335                         return;
336                 }
337                 
338                 if ($pcno == 21) {              # delete a cluster from the list
339                         my $call = uc $field[1];
340                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
341                                 my $ref = DXCluster->get_exact($call);
342                                 $ref->del() if $ref;
343                         }
344                         last SWITCH;
345                 }
346                 
347                 if ($pcno == 22) {
348                         $self->state('normal');
349                         
350                         # queue mail
351                         DXMsg::queue_msg(0);
352                         return;
353                 }
354                 
355                 if ($pcno == 23 || $pcno == 27) { # WWV info
356                         # do some de-duping
357                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
358                         my $sfi = unpad($field[3]);
359                         my $k = unpad($field[4]);
360                         my $i = unpad($field[5]);
361                         my $dupkey = "$d.$sfi$k$i";
362                         if ($wwvdup{$dupkey}) {
363                                 dbg('chan', "Dup WWV Spot ignored\n");
364                                 return;
365                         }
366                         
367                         $wwvdup{$dupkey} = $d;
368                         Geomag::update($field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
369
370                         # DON'T be silly and send on PC27s!
371                         return if $pcno == 27;
372                         
373                         last SWITCH;
374                 }
375                 
376                 if ($pcno == 24) {              # set here status
377                         my $call = uc $field[1];
378                         my $ref = DXCluster->get_exact($call);
379                         $ref->here($field[2]) if $ref;
380                         last SWITCH;
381                 }
382                 
383                 if ($pcno == 25) {
384                         last SWITCH;
385                 }
386                 
387                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
388                         DXMsg::process($self, $line);
389                         return;
390                 }
391                 
392                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
393                         if ($field[1] eq $main::mycall) {
394                                 my $ref = DXUser->get_current($field[2]);
395                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
396                                 unless ($field[3] =~ /rcmd/i) {    # not allowed to relay RCMDS!
397                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
398                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
399                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
400                                                 for (@in) {
401                                                         s/\s*$//og;
402                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
403                                                         Log('rcmd', 'out', $field[2], $_);
404                                                 }
405                                                 delete $self->{remotecmd};
406                                         }
407                                 } else {
408                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
409                                 }
410                         } else {
411                                 route($field[1], $line);
412                         }
413                         return;
414                 }
415                 
416                 if ($pcno == 35) {              # remote command replies
417                         if ($field[1] eq $main::mycall) {
418                                 my $s = $rcmds{$field[2]};
419                                 if ($s) {
420                                         my $dxchan = DXChannel->get($s->{call});
421                                         $dxchan->send($field[3]) if $dxchan;
422                                         delete $rcmds{$field[2]} if !$dxchan;
423                                 }
424                         } else {
425                                 route($field[1], $line);
426                         }
427                         return;
428                 }
429                 
430                 if ($pcno == 37) {
431                         last SWITCH;
432                 }
433                 
434                 if ($pcno == 38) {              # node connected list from neighbour
435                         return;
436                 }
437                 
438                 if ($pcno == 39) {              # incoming disconnect
439                         $self->disconnect();
440                         return;
441                 }
442                 
443                 if ($pcno == 41) {              # user info
444                         # add this station to the user database, if required
445                         my $user = DXUser->get_current($field[1]);
446                         if (!$user) {
447                                 # then try without an SSID
448                                 $field[1] =~ s/-\d+$//o;
449                                 $user = DXUser->get_current($field[1]);
450                         }
451                         $user = DXUser->new($field[1]) if !$user;
452                         
453                         if ($field[2] == 1) {
454                                 $user->name($field[3]);
455                         } elsif ($field[2] == 2) {
456                                 $user->qth($field[3]);
457                         } elsif ($field[2] == 3) {
458                                 my ($lat, $long) = DXBearing::stoll($field[3]);
459                                 $user->lat($lat);
460                                 $user->long($long);
461                         } elsif ($field[2] == 4) {
462                                 $user->homenode($field[3]);
463                         }
464                         $user->put;
465                         last SWITCH;
466                 }
467                 if ($pcno == 43) {
468                         last SWITCH;
469                 }
470                 if ($pcno == 44) {
471                         last SWITCH;
472                 }
473                 if ($pcno == 45) {
474                         last SWITCH;
475                 }
476                 if ($pcno == 46) {
477                         last SWITCH;
478                 }
479                 if ($pcno == 47) {
480                         last SWITCH;
481                 }
482                 if ($pcno == 48) {
483                         last SWITCH;
484                 }
485                 
486                 if ($pcno == 50) {              # keep alive/user list
487                         my $ref = DXCluster->get_exact($field[1]);
488                         $ref->update_users($field[2]) if $ref;
489                         last SWITCH;
490                 }
491                 
492                 if ($pcno == 51) {              # incoming ping requests/answers
493                         
494                         # is it for us?
495                         if ($field[1] eq $main::mycall) {
496                                 my $flag = $field[3];
497                                 if ($flag == 1) {
498                                         $self->send(pc51($field[2], $field[1], '0'));
499                                 } else {
500                                         # it's a reply, look in the ping list for this one
501                                         my $ref = $pings{$field[2]};
502                                         if ($ref) {
503                                                 my $r = shift @$ref;
504                                                 my $dxchan = DXChannel->get($r->{call});
505                                                 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
506                                         }
507                                 }
508                                 
509                         } else {
510                                 # route down an appropriate thingy
511                                 route($field[1], $line);
512                         }
513                         return;
514                 }
515         }
516          
517          # if get here then rebroadcast the thing with its Hop count decremented (if
518          # there is one). If it has a hop count and it decrements to zero then don't
519          # rebroadcast it.
520          #
521          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
522          #        REBROADCAST!!!!
523          #
524          
525         if (!$self->{isolate}) {
526                 broadcast_ak1a($line, $self); # send it to everyone but me
527         }
528 }
529
530 #
531 # This is called from inside the main cluster processing loop and is used
532 # for despatching commands that are doing some long processing job
533 #
534 sub process
535 {
536         my $t = time;
537         my @dxchan = DXChannel->get_all();
538         my $dxchan;
539         
540         foreach $dxchan (@dxchan) {
541                 next unless $dxchan->is_ak1a();
542                 next if $dxchan == $me;
543                 
544                 # send a pc50 out on this channel
545                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
546                         $dxchan->send(pc50());
547                         $dxchan->pc50_t($t);
548                 }
549         }
550         
551         my $key;
552         my $val;
553         my $cutoff;
554         if ($main::systime - 3600 > $last_hour) {
555                 $cutoff  = $main::systime - $pc11_dup_age;
556                 while (($key, $val) = each %spotdup) {
557                         delete $spotdup{$key} if $val < $cutoff;
558                 }
559                 $cutoff = $main::systime - $pc23_dup_age;
560                 while (($key, $val) = each %wwvdup) {
561                         delete $wwvdup{$key} if $val < $cutoff;
562                 }
563                 $last_hour = $main::systime;
564         }
565 }
566
567 #
568 # finish up a pc context
569 #
570 sub finish
571 {
572         my $self = shift;
573         my $call = $self->call;
574         my $ref = DXCluster->get_exact($call);
575         
576         # unbusy and stop and outgoing mail
577         my $mref = DXMsg::get_busy($call);
578         $mref->stop_msg($self) if $mref;
579         
580         # broadcast to all other nodes that all the nodes connected to via me are gone
581         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
582         my $node;
583         
584         foreach $node (@gonenodes) {
585                 next if $node->call eq $call;
586                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
587                 $node->del();
588         }
589
590         # remove outstanding pings
591         delete $pings{$call};
592         
593         # now broadcast to all other ak1a nodes that I have gone
594         broadcast_ak1a(pc21($call, 'Gone.'), $self);
595         
596         Log('DXProt', $call . " Disconnected");
597         $ref->del() if $ref;
598 }
599
600 #
601 # some active measures
602 #
603
604 sub send_local_config
605 {
606         my $self = shift;
607         my $n;
608         my @nodes;
609         
610         # send our nodes
611         if ($self->{isolate}) {
612                 @nodes = (DXCluster->get_exact($main::mycall));
613         } else {
614                 # create a list of all the nodes that are not connected to this connection
615                 @nodes = DXNode::get_all();
616                 @nodes = grep { $_->dxchan != $self } @nodes;
617         }
618
619         my @s = $me->pc19(@nodes);
620         for (@s) {
621                 my $routeit = adjust_hops($self, $_);
622                 $self->send($routeit) if $routeit;
623         }
624         
625         # get all the users connected on the above nodes and send them out
626         foreach $n (@nodes) {
627                 my @users = values %{$n->list};
628                 my @s = pc16($n, @users);
629                 for (@s) {
630                         my $routeit = adjust_hops($self, $_);
631                         $self->send($routeit) if $routeit;
632                 }
633         }
634 }
635
636 #
637 # route a message down an appropriate interface for a callsign
638 #
639 # is called route(to, pcline);
640 #
641 sub route
642 {
643         my ($call, $line) = @_;
644         my $cl = DXCluster->get_exact($call);
645         if ($cl) {
646                 my $hops;
647                 my $dxchan = $cl->{dxchan};
648                 if ($dxchan) {
649                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
650                         if ($routeit) {
651                                 $dxchan->send($routeit) if $dxchan;
652                         }
653                 }
654         }
655 }
656
657 # broadcast a message to all clusters [except those mentioned after buffer]
658 sub broadcast_ak1a
659 {
660         my $s = shift;                          # the line to be rebroadcast
661         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
662         my @dxchan = get_all_ak1a();
663         my $dxchan;
664         
665         # send it if it isn't the except list and isn't isolated and still has a hop count
666         foreach $dxchan (@dxchan) {
667                 next if grep $dxchan == $_, @except;
668                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
669                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
670         }
671 }
672
673 # broadcast to all users
674 sub broadcast_users
675 {
676         my $s = shift;                          # the line to be rebroadcast
677         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
678         my @dxchan = get_all_users();
679         my $dxchan;
680         
681         foreach $dxchan (@dxchan) {
682                 next if grep $dxchan == $_, @except;
683                 $s =~ s/\a//og if !$dxchan->{beep};
684                 $dxchan->send($s);              # send it if it isn't the except list or hasn't a passout flag
685         }
686 }
687
688 # broadcast to a list of users
689 sub broadcast_list
690 {
691         my $s = shift;
692         my $dxchan;
693         
694         foreach $dxchan (@_) {
695                 $dxchan->send($s);              # send it 
696         }
697 }
698
699 #
700 # gimme all the ak1a nodes
701 #
702 sub get_all_ak1a
703 {
704         my @list = DXChannel->get_all();
705         my $ref;
706         my @out;
707         foreach $ref (@list) {
708                 push @out, $ref if $ref->is_ak1a;
709         }
710         return @out;
711 }
712
713 # return a list of all users
714 sub get_all_users
715 {
716         my @list = DXChannel->get_all();
717         my $ref;
718         my @out;
719         foreach $ref (@list) {
720                 push @out, $ref if $ref->is_user;
721         }
722         return @out;
723 }
724
725 # return a list of all user callsigns
726 sub get_all_user_calls
727 {
728         my @list = DXChannel->get_all();
729         my $ref;
730         my @out;
731         foreach $ref (@list) {
732                 push @out, $ref->call if $ref->is_user;
733         }
734         return @out;
735 }
736
737 #
738 # obtain the hops from the list for this callsign and pc no 
739 #
740
741 sub get_hops
742 {
743         my ($pcno) = @_;
744         my $hops = $DXProt::hopcount{$pcno};
745         $hops = $DXProt::def_hopcount if !$hops;
746         return "H$hops";       
747 }
748
749
750 # adjust the hop count on a per node basis using the user loadable 
751 # hop table if available or else decrement an existing one
752 #
753
754 sub adjust_hops
755 {
756         my $self = shift;
757         my $s = shift;
758         my $call = $self->{call};
759         my $hops;
760         
761         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
762                 my ($pcno) = $s =~ /^PC(\d\d)/o;
763                 confess "$call called adjust_hops with '$s'" unless $pcno;
764                 my $ref = $nodehops{$call} if %nodehops;
765                 if ($ref) {
766                         my $newhops = $ref->{$pcno};
767                         return "" if defined $newhops && $newhops == 0;
768                         $newhops = $ref->{default} unless $newhops;
769                         return "" if defined $newhops && $newhops == 0;
770                         $newhops = $hops if !$newhops;
771                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
772                 } else {
773                         # simply decrement it
774                         $hops--;
775                         return "" if !$hops;
776                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
777                 }
778         }
779         return $s;
780 }
781
782
783 # load hop tables
784 #
785 sub load_hops
786 {
787         my $self = shift;
788         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
789         do "$main::data/hop_table.pl";
790         return $@ if $@;
791         return 0;
792 }
793
794 # remove leading and trailing spaces from an input string
795 sub unpad
796 {
797         my $s = shift;
798         $s =~ s/^\s+|\s+$//;
799         return $s;
800 }
801
802 # add a ping request to the ping queues
803 sub addping
804 {
805         my ($from, $to) = @_;
806         my $ref = $pings{$to};
807         $ref = $pings{$to} = [] if !$ref;
808         my $r = {};
809         $r->{call} = $from;
810         $r->{t} = $main::systime;
811         route($to, pc51($to, $main::mycall, 1));
812         push @$ref, $r;
813 }
814
815 # add a rcmd request to the rcmd queues
816 sub addrcmd
817 {
818         my ($from, $to, $cmd) = @_;
819         my $r = {};
820         $r->{call} = $from;
821         $r->{t} = $main::systime;
822         $r->{cmd} = $cmd;
823         route($to, pc34($main::mycall, $to, $cmd));
824         $rcmds{$to} = $r;
825 }
826 1;
827 __END__