1. Added RCMD for clx
[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 Filter;
26 use Local;
27 use DXDb;
28 use AnnTalk;
29 use Geomag;
30 use WCY;
31 use Time::HiRes qw(gettimeofday tv_interval);
32
33 use strict;
34 use vars qw($me $pc11_max_age $pc23_max_age
35                         $last_hour %pings %rcmds
36                         %nodehops @baddx $baddxfn 
37                         $allowzero $decode_dk0wcy);
38
39 $me = undef;                                    # the channel id for this cluster
40 $decode_dk0wcy = undef;                 # if set use this callsign to decode announces from the EU WWV data beacon
41 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
42 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
43
44 $last_hour = time;                              # last time I did an hourly periodic update
45 %pings = ();                    # outstanding ping requests outbound
46 %rcmds = ();                    # outstanding rcmd requests outbound
47 %nodehops = ();                 # node specific hop control
48 @baddx = ();                    # list of illegal spotted callsigns
49
50
51 $baddxfn = "$main::data/baddx.pl";
52
53 sub init
54 {
55         my $user = DXUser->get($main::mycall);
56         $DXProt::myprot_version += $main::version*100;
57         $me = DXProt->new($main::mycall, 0, $user); 
58         $me->{here} = 1;
59         $me->{state} = "indifferent";
60         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
61         confess $@ if $@;
62         #  $me->{sort} = 'M';    # M for me
63
64         # now prime the spot and wwv  duplicates file with data
65     my @today = Julian::unixtoj(time);
66         for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
67                 Spot::dup(@{$_}[0..3]);
68         }
69         for (Geomag::readfile(time)) {
70                 Geomag::dup(@{$_}[1..5]);
71         }
72
73         # load the baddx file
74         do "$baddxfn" if -e "$baddxfn";
75         print "$@\n" if $@;
76 }
77
78 #
79 # obtain a new connection this is derived from dxchannel
80 #
81
82 sub new 
83 {
84         my $self = DXChannel::alloc(@_);
85         return $self;
86 }
87
88 # this is how a pc connection starts (for an incoming connection)
89 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
90 # all the crap that comes between).
91 sub start
92 {
93         my ($self, $line, $sort) = @_;
94         my $call = $self->{call};
95         my $user = $self->{user};
96         
97         # remember type of connection
98         $self->{consort} = $line;
99         $self->{outbound} = $sort eq 'O';
100         $self->{priv} = $user->priv;
101         $self->{lang} = $user->lang;
102         $self->{isolate} = $user->{isolate};
103         $self->{consort} = $line;       # save the connection type
104         $self->{here} = 1;
105
106         # get the INPUT filters (these only pertain to Clusters)
107         $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
108         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
109         $self->{inannfilter} = Filter::read_in('ann', $call, 1);
110         
111         # set unbuffered and no echo
112         $self->send_now('B',"0");
113         $self->send_now('E',"0");
114         
115         # ping neighbour node stuff
116         my $ping = $user->pingint;
117         $ping = 5*60 unless defined $ping;
118         $self->{pingint} = $ping;
119         $self->{nopings} = $user->nopings || 2;
120         $self->{pingtime} = [ ];
121         $self->{pingave} = 0;
122
123         # send initialisation string
124         unless ($self->{outbound}) {
125                 $self->send(pc38()) if DXNode->get_all();
126                 $self->send(pc18());
127                 $self->{lastping} = $main::systime;
128         } else {
129                 # remove from outstanding connects queue
130                 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
131                 $self->{lastping} = $main::systime + $self->pingint / 2;
132         }
133         $self->state('init');
134         $self->pc50_t(time);
135
136         # send info to all logged in thingies
137         $self->tell_login('loginn');
138
139         Log('DXProt', "$call connected");
140 }
141
142 #
143 # This is the normal pcxx despatcher
144 #
145 sub normal
146 {
147         my ($self, $line) = @_;
148         my @field = split /\^/, $line;
149         pop @field if $field[-1] eq '~';
150         
151 #       print join(',', @field), "\n";
152                                                 
153         # ignore any lines that don't start with PC
154         return if !$field[0] =~ /^PC/;
155         
156         # process PC frames
157         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
158         return unless $pcno;
159         return if $pcno < 10 || $pcno > 99;
160
161         # dump bad protocol messages unless it is a PC29
162         if ($line =~ /\%[0-9A-F][0-9A-F]/o && $pcno != 29) {
163                 dbg('chan', "CORRUPT protocol message - dumped");
164                 return;
165         }
166
167         # local processing 1
168         my $pcr;
169         eval {
170                 $pcr = Local::pcprot($self, $pcno, @field);
171         };
172 #       dbg('local', "Local::pcprot error $@") if $@;
173         return if $pcr;
174         
175  SWITCH: {
176                 if ($pcno == 10) {              # incoming talk
177                         
178                         # is it for me or one of mine?
179                         my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
180                         if ($call eq $main::mycall || grep $_ eq $call, DXChannel::get_all_user_calls()) {
181                                 
182                                 # yes, it is
183                                 my $text = unpad($field[3]);
184                                 Log('talk', $call, $field[1], $field[6], $text);
185                                 $call = $main::myalias if $call eq $main::mycall;
186                                 my $ref = DXChannel->get($call);
187                                 $ref->send("$call de $field[1]: $text") if $ref && $ref->{talk};
188                         } else {
189                                 $self->route($field[2], $line); # relay it on its way
190                         }
191                         return;
192                 }
193                 
194                 if ($pcno == 11 || $pcno == 26) { # dx spot
195
196                         # route 'foreign' pc26s 
197                         if ($pcno == 26) {
198                                 if ($field[7] ne $main::mycall) {
199                                         $self->route($field[7], $line);
200                                         return;
201                                 }
202                         }
203                         
204                         # if this is a 'nodx' node then ignore it
205                         if (grep $field[7] =~ /^$_/,  @DXProt::nodx_node) {
206                                 dbg('chan', "Bad DXNode, dropped");
207                                 return;
208                         }
209                         
210                         # convert the date to a unix date
211                         my $d = cltounix($field[3], $field[4]);
212                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
213                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
214                                 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
215                                 return;
216                         }
217
218                         # is it 'baddx'
219                         if (grep $field[2] eq $_, @baddx) {
220                                 dbg('chan', "Bad DX spot, ignored");
221                                 return;
222                         }
223
224                         # are any of the crucial fields invalid?
225             if ($field[2] =~ /[a-z]/ || $field[6] =~ /[a-z]/ || $field[7] =~ /[a-z]/) {
226                                 dbg('chan', "Spot contains lower case callsigns, rejected");
227                                 return;
228                         }
229                         
230                         # do some de-duping
231                         if (Spot::dup($field[1], $field[2], $d, $field[5])) {
232                                 dbg('chan', "Duplicate Spot ignored\n");
233                                 return;
234                         }
235                         
236                         my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
237
238             #
239                         # @spot at this point contains:-
240             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
241                         # then  spotted itu, spotted cq, spotters itu, spotters cq
242                         # you should be able to route on any of these
243             #
244                         
245                         # local processing 
246                         my $r;
247                         eval {
248                                 $r = Local::spot($self, @spot);
249                         };
250 #                       dbg('local', "Local::spot1 error $@") if $@;
251                         return if $r;
252
253                         # DON'T be silly and send on PC26s!
254                         return if $pcno == 26;
255
256                         # send out the filtered spots
257                         send_dx_spot($self, $line, @spot) if @spot;
258                         return;
259                 }
260                 
261                 if ($pcno == 12) {              # announces
262                         # announce duplicate checking
263                         if (AnnTalk::dup($field[1], $field[2], $field[3])) {
264                                 dbg('chan', "Duplicate Announce ignored\n");
265                                 return;
266                         }
267                         
268                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
269                                 
270                                 # global ann filtering on INPUT
271                                 if ($self->{inannfilter}) {
272                                         my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
273                                         unless ($filter) {
274                                                 dbg('chan', "Rejected by filter");
275                                                 return;
276                                         }
277                                 }
278
279                                 # send it
280                                 $self->send_announce($line, @field[1..6]);
281                                 
282                                 if ($decode_dk0wcy && $field[1] eq $decode_dk0wcy) {
283                                         my ($hour, $k, $next, $a, $r, $sfi, $alarm) = $field[3] =~ /^Aurora Beacon\s+(\d+)UTC,\s+Kiel\s+K=(\d+),.*ed\s+K=(\d+),\s+A=(\d+),\s+R=(\d+),\s+SFI=(\d+),.*larm:\s+(\w+)/;
284                                         $alarm = ($alarm =~ /^Y/i) ? ', Aurora in DE' : ''; 
285                                         my $wwv = Geomag::update($main::systime, $hour, $sfi, $a, $k, "R=$r, Next K=$next$alarm", $decode_dk0wcy, $field[5], $r) if $sfi && $r;
286                                 }
287                                 
288                         } else {
289                                 $self->route($field[2], $line);
290                         }
291                         
292                         return;
293                 }
294                 
295                 if ($pcno == 13) {
296                         last SWITCH;
297                 }
298                 if ($pcno == 14) {
299                         last SWITCH;
300                 }
301                 if ($pcno == 15) {
302                         last SWITCH;
303                 }
304                 
305                 if ($pcno == 16) {              # add a user
306                         my $node = DXCluster->get_exact($field[1]); 
307                         my $dxchan;
308                         if (!$node && ($dxchan = DXChannel->get($field[1]))) {
309                                 # add it to the node table if it isn't present and it's
310                                 # connected locally
311                                 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
312                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
313                                 
314                         }
315                         return unless $node; # ignore if havn't seen a PC19 for this one yet
316                         return unless $node->isa('DXNode');
317                         if ($node->dxchan != $self) {
318                                 dbg('chan', "LOOP: $field[1] came in on wrong channel");
319                                 return;
320                         }
321                         if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
322                                 dbg('chan', "LOOP: $field[1] connected locally");
323                                 return;
324                         }
325                         my $i;
326                                                 
327                         for ($i = 2; $i < $#field; $i++) {
328                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
329                                 next if !$call || length $call < 3 || length $call > 8;
330                                 next if !$confmode;
331                                 $call = uc $call;
332                                 next if DXCluster->get_exact($call); # we already have this (loop?)
333                                 
334                                 $confmode = $confmode eq '*';
335                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
336                                 
337                                 # add this station to the user database, if required
338                                 $call =~ s/-\d+$//o;        # remove ssid for users
339                                 my $user = DXUser->get_current($call);
340                                 $user = DXUser->new($call) if !$user;
341                                 $user->homenode($node->call) if !$user->homenode;
342                                 $user->node($node->call);
343                                 $user->lastin($main::systime) unless DXChannel->get($call);
344                                 $user->put;
345                         }
346                         
347                         # queue up any messages (look for privates only)
348                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
349                         last SWITCH;
350                 }
351                 
352                 if ($pcno == 17) {              # remove a user
353                         my $node = DXCluster->get_exact($field[2]);
354                         my $dxchan;
355                         if (!$node && ($dxchan = DXChannel->get($field[2]))) {
356                                 # add it to the node table if it isn't present and it's
357                                 # connected locally
358                                 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
359                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
360                                 return;
361                         }
362                         return unless $node;
363                         return unless $node->isa('DXNode');
364                         if ($node->dxchan != $self) {
365                                 dbg('chan', "LOOP: $field[2] came in on wrong channel");
366                                 return;
367                         }
368                         if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
369                                 dbg('chan', "LOOP: $field[2] connected locally");
370                                 return;
371                         }
372                         my $ref = DXCluster->get_exact($field[1]);
373                         $ref->del() if $ref;
374                         last SWITCH;
375                 }
376                 
377                 if ($pcno == 18) {              # link request
378                         $self->state('init');   
379
380                         # first clear out any nodes on this dxchannel
381                         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
382                         foreach my $node (@gonenodes) {
383                                 next if $node->dxchan == $DXProt::me;
384                                 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate}; 
385                                 $node->del();
386                         }
387                         $self->send_local_config();
388                         $self->send(pc20());
389                         return;             # we don't pass these on
390                 }
391                 
392                 if ($pcno == 19) {              # incoming cluster list
393                         my $i;
394                         my $newline = "PC19^";
395                         for ($i = 1; $i < $#field-1; $i += 4) {
396                                 my $here = $field[$i];
397                                 my $call = uc $field[$i+1];
398                                 my $confmode = $field[$i+2];
399                                 my $ver = $field[$i+3];
400
401                                 $ver = 5400 if !$ver && $allowzero;
402                                 
403                                 # now check the call over
404                                 my $node = DXCluster->get_exact($call);
405                                 if ($node) {
406                                         my $dxchan;
407                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
408                                                 dbg('chan', "LOOP: $call connected locally");
409                                         }
410                                     if ($node->dxchan != $self) {
411                                                 dbg('chan', "LOOP: $call come in on wrong channel");
412                                                 next;
413                                         }
414                                         dbg('chan', "already have $call");
415                                         next;
416                                 }
417                                 
418                                 # check for sane parameters
419                                 next if $ver < 5000; # only works with version 5 software
420                                 next if length $call < 3; # min 3 letter callsigns
421
422                                 # add it to the nodes table and outgoing line
423                                 $newline .= "$here^$call^$confmode^$ver^";
424                                 DXNode->new($self, $call, $confmode, $here, $ver);
425                                 
426                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
427                                 my $mref = DXMsg::get_busy($call);
428                                 $mref->stop_msg($call) if $mref;
429                                 
430                                 # add this station to the user database, if required (don't remove SSID from nodes)
431                                 my $user = DXUser->get_current($call);
432                                 if (!$user) {
433                                         $user = DXUser->new($call);
434                                         $user->sort('A');
435                                         $user->priv(1);                   # I have relented and defaulted nodes
436                                         $self->{priv} = 1;                # to user RCMDs allowed
437                                         $user->homenode($call);
438                                         $user->node($call);
439                                 }
440                                 $user->lastin($main::systime) unless DXChannel->get($call);
441                                 $user->put;
442                         }
443                         
444                         return if $newline eq "PC19^";
445
446                         # add hop count 
447                         $newline .=  get_hops(19) . "^";
448                         $line = $newline;
449                         last SWITCH;
450                 }
451                 
452                 if ($pcno == 20) {              # send local configuration
453                         $self->send_local_config();
454                         $self->send(pc22());
455                         $self->state('normal');
456                         return;
457                 }
458                 
459                 if ($pcno == 21) {              # delete a cluster from the list
460                         my $call = uc $field[1];
461                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
462                                 my $node = DXCluster->get_exact($call);
463                                 if ($node) {
464                                         if ($node->dxchan != $self) {
465                                                 dbg('chan', "LOOP: $call come in on wrong channel");
466                                                 return;
467                                         }
468                                         my $dxchan;
469                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
470                                                 dbg('chan', "LOOP: $call connected locally");
471                                                 return;
472                                         }
473                                         $node->del();
474                                 } else {
475                                         dbg('chan', "$call not in table, dropped");
476                                         return;
477                                 }
478                         }
479                         last SWITCH;
480                 }
481                 
482                 if ($pcno == 22) {
483                         $self->state('normal');
484                         return;
485                 }
486                                 
487                 if ($pcno == 23 || $pcno == 27) { # WWV info
488                         
489                         # route 'foreign' pc27s 
490                         if ($pcno == 27) {
491                                 if ($field[8] ne $main::mycall) {
492                                         $self->route($field[8], $line);
493                                         return;
494                                 }
495                         }
496
497                         # do some de-duping
498                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
499                         my $sfi = unpad($field[3]);
500                         my $k = unpad($field[4]);
501                         my $i = unpad($field[5]);
502                         my ($r) = $field[6] =~ /R=(\d+)/;
503                         $r = 0 unless $r;
504                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
505                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
506                                 return;
507                         }
508                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
509                                 dbg('chan', "Dup WWV Spot ignored\n");
510                                 return;
511                         }
512                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
513                 
514                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
515
516                         my $rep;
517                         eval {
518                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
519                         };
520 #                       dbg('local', "Local::wwv2 error $@") if $@;
521                         return if $rep;
522
523                         # DON'T be silly and send on PC27s!
524                         return if $pcno == 27;
525
526                         # broadcast to the eager world
527                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
528                         return;
529                 }
530                 
531                 if ($pcno == 24) {              # set here status
532                         my $call = uc $field[1];
533                         my $ref = DXCluster->get_exact($call);
534                         $ref->here($field[2]) if $ref;
535                         last SWITCH;
536                 }
537                 
538                 if ($pcno == 25) {      # merge request
539                         if ($field[1] ne $main::mycall) {
540                                 $self->route($field[1], $line);
541                                 return;
542                         }
543                         if ($field[2] eq $main::mycall) {
544                                 dbg('chan', "Trying to merge to myself, ignored");
545                                 return;
546                         }
547
548                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
549                         
550                         # spots
551                         if ($field[3] > 0) {
552                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
553                                 my $in;
554                                 foreach $in (@in) {
555                                         $self->send(pc26(@{$in}[0..4], $field[2]));
556                                 }
557                         }
558
559                         # wwv
560                         if ($field[4] > 0) {
561                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
562                                 my $in;
563                                 foreach $in (@in) {
564                                         $self->send(pc27(@{$in}[0..5], $field[2]));
565                                 }
566                         }
567                         return;
568                 }
569
570                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
571                         if ($pcno == 49 || $field[1] eq $main::mycall) {
572                                 DXMsg::process($self, $line);
573                         } else {
574                                 $self->route($field[1], $line);
575                         }
576                         return;
577                 }
578                 
579                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
580                         if ($field[1] eq $main::mycall) {
581                                 my $ref = DXUser->get_current($field[2]);
582                                 my $cref = DXCluster->get($field[2]);
583                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
584                                 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
585                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
586                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
587                                                 my $oldpriv = $self->{priv};
588                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
589                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
590                                                 $self->{priv} = $oldpriv;
591                                                 for (@in) {
592                                                         s/\s*$//og;
593                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
594                                                         Log('rcmd', 'out', $field[2], $_);
595                                                 }
596                                                 delete $self->{remotecmd};
597                                         } else {
598                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
599                                         }
600                                 } else {
601                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
602                                 }
603                         } else {
604                                 my $ref = DXUser->get_current($field[1]);
605                                 if ($ref && $ref->is_clx) {
606                                         route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
607                                 } else {
608                                         $self->route($field[1], $line);
609                                 }
610                         }
611                         return;
612                 }
613                 
614                 if ($pcno == 35) {              # remote command replies
615                         if ($field[1] eq $main::mycall) {
616                                 my $s = $rcmds{$field[2]};
617                                 if ($s) {
618                                         my $dxchan = DXChannel->get($s->{call});
619                                         $dxchan->send($field[3]) if $dxchan;
620                                         delete $rcmds{$field[2]} if !$dxchan;
621                                 }
622                         } else {
623                                 my $ref = DXUser->get_current($field[1]);
624                                 if ($ref && $ref->is_clx) {
625                                         route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
626                                 } else {
627                                         $self->route($field[1], $line);
628                                 }
629                         }
630                         return;
631                 }
632                 
633                 # for pc 37 see 44 onwards
634
635                 if ($pcno == 38) {              # node connected list from neighbour
636                         return;
637                 }
638                 
639                 if ($pcno == 39) {              # incoming disconnect
640                         $self->disconnect(1);
641                         return;
642                 }
643                 
644                 if ($pcno == 41) {              # user info
645                         # add this station to the user database, if required
646                         my $user = DXUser->get_current($field[1]);
647                         if (!$user) {
648                                 # then try without an SSID
649                                 $field[1] =~ s/-\d+$//o;
650                                 $user = DXUser->get_current($field[1]);
651                         }
652                         $user = DXUser->new($field[1]) if !$user;
653                         
654                         if ($field[2] == 1) {
655                                 $user->name($field[3]);
656                         } elsif ($field[2] == 2) {
657                                 $user->qth($field[3]);
658                         } elsif ($field[2] == 3) {
659                                 my ($lat, $long) = DXBearing::stoll($field[3]);
660                                 $user->lat($lat);
661                                 $user->long($long);
662                         } elsif ($field[2] == 4) {
663                                 $user->homenode($field[3]);
664                         }
665                         $user->put;
666                         last SWITCH;
667                 }
668                 if ($pcno == 43) {
669                         last SWITCH;
670                 }
671                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
672                         DXDb::process($self, $line);
673                         return;
674                 }
675                 
676                 if ($pcno == 50) {              # keep alive/user list
677                         my $node = DXCluster->get_exact($field[1]);
678                         if ($node) {
679                                 return unless $node->isa('DXNode');
680                                 return unless $node->dxchan == $self;
681                                 $node->update_users($field[2]);
682                         }
683                         last SWITCH;
684                 }
685                 
686                 if ($pcno == 51) {              # incoming ping requests/answers
687                         
688                         # is it for us?
689                         if ($field[1] eq $main::mycall) {
690                                 my $flag = $field[3];
691                                 if ($flag == 1) {
692                                         $self->send(pc51($field[2], $field[1], '0'));
693                                 } else {
694                                         # it's a reply, look in the ping list for this one
695                                         my $ref = $pings{$field[2]};
696                                         if ($ref) {
697                                                 my $tochan =  DXChannel->get($field[2]);
698                                                 while (@$ref) {
699                                                         my $r = shift @$ref;
700                                                         my $dxchan = DXChannel->get($r->{call});
701                                                         next unless $dxchan;
702                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
703                                                         if ($dxchan->is_user) {
704                                                                 my $s = sprintf "%.2f", $t; 
705                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
706                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
707                                                         } elsif ($dxchan->is_node) {
708                                                                 if ($tochan) {
709                                                                         $tochan->{nopings} = 2; # pump up the timer
710                                                                         push @{$tochan->{pingtime}}, $t;
711                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
712                                                                         my $st;
713                                                                         for (@{$tochan->{pingtime}}) {
714                                                                                 $st += $_;
715                                                                         }
716                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
717                                                                 }
718                                                         } 
719                                                 }
720                                         }
721                                 }
722                         } else {
723                                 # route down an appropriate thingy
724                                 $self->route($field[1], $line);
725                         }
726                         return;
727                 }
728
729                 if ($pcno == 73) {  # WCY broadcasts
730                         
731                         # do some de-duping
732                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
733                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
734                                 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
735                                 return;
736                         }
737                         @field = map { unpad($_) } @field;
738                         if (WCY::dup($d,@field[3..7])) {
739                                 dbg('chan', "Dup WCY Spot ignored\n");
740                                 return;
741                         }
742                 
743                         my $wcy = WCY::update($d, @field[2..12]);
744
745                         my $rep;
746                         eval {
747                                 $rep = Local::wwv($self, @field[1..12]);
748                         };
749                         # dbg('local', "Local::wcy error $@") if $@;
750                         return if $rep;
751
752                         # broadcast to the eager world
753                         send_wcy_spot($self, $line, $d, @field[2..12]);
754                         return;
755                 }
756
757                 if ($pcno == 84) { # remote commands (incoming)
758                         if ($field[1] eq $main::mycall) {
759                                 my $ref = DXUser->get_current($field[2]);
760                                 my $cref = DXCluster->get($field[2]);
761                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
762                                 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
763                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
764                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
765                                                 my $oldpriv = $self->{priv};
766                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
767                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
768                                                 $self->{priv} = $oldpriv;
769                                                 for (@in) {
770                                                         s/\s*$//og;
771                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
772                                                         Log('rcmd', 'out', $field[2], $_);
773                                                 }
774                                                 delete $self->{remotecmd};
775                                         } else {
776                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
777                                         }
778                                 } else {
779                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
780                                 }
781                         } else {
782                                 my $ref = DXUser->get_current($field[1]);
783                                 if ($ref && $ref->is_clx) {
784                                         $self->route($field[1], $line);
785                                 } else {
786                                         route($field[1], pc34($field[2], $field[1], $field[3]));
787                                 }
788                         }
789                         return;
790                 }
791
792                 if ($pcno == 85) {              # remote command replies
793                         if ($field[1] eq $main::mycall) {
794                                 my $dxchan = DXChannel->get($field[3]);
795                                 if ($dxchan) {
796                                         $dxchan->send($field[4]);
797                                 } else {
798                                         my $s = $rcmds{$field[2]};
799                                         if ($s) {
800                                                 $dxchan = DXChannel->get($s->{call});
801                                                 $dxchan->send($field[4]) if $dxchan;
802                                                 delete $rcmds{$field[2]} if !$dxchan;
803                                         }
804                                 }
805                         } else {
806                                 my $ref = DXUser->get_current($field[1]);
807                                 if ($ref && $ref->is_clx) {
808                                         $self->route($field[1], $line);
809                                 } else {
810                                         route($field[1], pc35($field[2], $field[1], $field[3]));
811                                 }
812                         }
813                         return;
814                 }
815         }
816          
817         # if get here then rebroadcast the thing with its Hop count decremented (if
818         # there is one). If it has a hop count and it decrements to zero then don't
819         # rebroadcast it.
820         #
821         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
822         #        REBROADCAST!!!!
823         #
824          
825         unless ($self->{isolate}) {
826                 broadcast_ak1a($line, $self); # send it to everyone but me
827         }
828 }
829
830 #
831 # This is called from inside the main cluster processing loop and is used
832 # for despatching commands that are doing some long processing job
833 #
834 sub process
835 {
836         my $t = time;
837         my @dxchan = DXChannel->get_all();
838         my $dxchan;
839         
840         foreach $dxchan (@dxchan) {
841                 next unless $dxchan->is_node();
842                 next if $dxchan == $me;
843                 
844                 # send a pc50 out on this channel
845                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
846                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
847                         $dxchan->pc50_t($t);
848                 } 
849
850                 # send a ping out on this channel
851                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
852                         if ($dxchan->{nopings} <= 0) {
853                                 $dxchan->disconnect;
854                         } else {
855                                 addping($main::mycall, $dxchan->call);
856                                 $dxchan->{nopings} -= 1;
857                                 $dxchan->{lastping} = $t;
858                         }
859                 }
860         }
861         
862         my $key;
863         my $val;
864         my $cutoff;
865         if ($main::systime - 3600 > $last_hour) {
866                 Spot::process;
867                 Geomag::process;
868                 AnnTalk::process;
869                 $last_hour = $main::systime;
870         }
871 }
872
873 #
874 # finish up a pc context
875 #
876 sub finish
877 {
878         my $self = shift;
879         my $call = $self->call;
880         my $nopc39 = shift;
881         my $ref = DXCluster->get_exact($call);
882         
883         $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op"))) unless $nopc39;
884         
885         # unbusy and stop and outgoing mail
886         my $mref = DXMsg::get_busy($call);
887         $mref->stop_msg($call) if $mref;
888         
889         # broadcast to all other nodes that all the nodes connected to via me are gone
890         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
891         my $node;
892         
893         foreach $node (@gonenodes) {
894                 next if $node->call eq $call;
895                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
896                 $node->del();
897         }
898
899         # remove outstanding pings
900         delete $pings{$call};
901         
902         # now broadcast to all other ak1a nodes that I have gone
903         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
904
905         # I was the last node visited
906     $self->user->node($main::mycall);
907
908         # send info to all logged in thingies
909         $self->tell_login('logoutn');
910
911         Log('DXProt', $call . " Disconnected");
912         $ref->del() if $ref;
913 }
914
915 #
916 # some active measures
917 #
918 sub send_dx_spot
919 {
920         my $self = shift;
921         my $line = shift;
922         my @dxchan = DXChannel->get_all();
923         my $dxchan;
924         
925         # send it if it isn't the except list and isn't isolated and still has a hop count
926         # taking into account filtering and so on
927         foreach $dxchan (@dxchan) {
928                 my $routeit;
929                 my ($filter, $hops);
930
931                 if ($dxchan->{spotfilter}) {
932                     ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
933                         next unless $filter;
934                 }
935                 
936                 if ($dxchan->is_node) {
937                         next if $dxchan == $self;
938                         if ($hops) {
939                                 $routeit = $line;
940                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
941                         } else {
942                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
943                                 next unless $routeit;
944                         }
945                         if ($filter) {
946                                 $dxchan->send($routeit) if $routeit;
947                         } else {
948                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
949                         }
950                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
951                         my $buf = Spot::formatb($_[0], $_[1], $_[2], $_[3], $_[4]);
952                         $buf .= "\a\a" if $dxchan->{beep};
953                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
954                                 $dxchan->send($buf);
955                         } else {
956                                 $dxchan->delay($buf);
957                         }
958                 }                                       
959         }
960 }
961
962 sub send_wwv_spot
963 {
964         my $self = shift;
965         my $line = shift;
966         my @dxchan = DXChannel->get_all();
967         my $dxchan;
968         
969         # send it if it isn't the except list and isn't isolated and still has a hop count
970         # taking into account filtering and so on
971         foreach $dxchan (@dxchan) {
972                 my $routeit;
973                 my ($filter, $hops);
974
975                 if ($dxchan->{wwvfilter}) {
976                          ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
977                          next unless $filter;
978                 }
979                 if ($dxchan->is_node) {
980                         next if $dxchan == $self;
981                         if ($hops) {
982                                 $routeit = $line;
983                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
984                         } else {
985                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
986                                 next unless $routeit;
987                         }
988                         if ($filter) {
989                                 $dxchan->send($routeit) if $routeit;
990                         } else {
991                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
992                                 
993                         }
994                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
995                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
996                         $buf .= "\a\a" if $dxchan->{beep};
997                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
998                                 $dxchan->send($buf);
999                         } else {
1000                                 $dxchan->delay($buf);
1001                         }
1002                 }                                       
1003         }
1004 }
1005
1006 sub send_wcy_spot
1007 {
1008         my $self = shift;
1009         my $line = shift;
1010         my @dxchan = DXChannel->get_all();
1011         my $dxchan;
1012         
1013         # send it if it isn't the except list and isn't isolated and still has a hop count
1014         # taking into account filtering and so on
1015         foreach $dxchan (@dxchan) {
1016                 my $routeit;
1017                 my ($filter, $hops);
1018
1019                 if ($dxchan->{wcyfilter}) {
1020                          ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1021                          next unless $filter;
1022                 }
1023                 if ($dxchan->is_clx || $dxchan->is_spider) {
1024                         next if $dxchan == $self;
1025                         if ($hops) {
1026                                 $routeit = $line;
1027                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1028                         } else {
1029                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1030                                 next unless $routeit;
1031                         }
1032                         if ($filter) {
1033                                 $dxchan->send($routeit) if $routeit;
1034                         } else {
1035                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1036                         }
1037                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1038                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1039                         $buf .= "\a\a" if $dxchan->{beep};
1040                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1041                                 $dxchan->send($buf);
1042                         } else {
1043                                 $dxchan->delay($buf);
1044                         }
1045                 }                                       
1046         }
1047 }
1048
1049 # send an announce
1050 sub send_announce
1051 {
1052         my $self = shift;
1053         my $line = shift;
1054         my @dxchan = DXChannel->get_all();
1055         my $dxchan;
1056         my $text = unpad($_[2]);
1057         my $target;
1058         my $to = 'To ';
1059                                 
1060         if ($_[3] eq '*') {     # sysops
1061                 $target = "SYSOP";
1062         } elsif ($_[3] gt ' ') { # speciality list handling
1063                 my ($name) = split /\./, $_[3]; 
1064                 $target = "$name"; # put the rest in later (if bothered) 
1065         } 
1066         
1067         if ($_[5] eq '1') {
1068                 $target = "WX"; 
1069                 $to = '';
1070         }
1071         $target = "All" if !$target;
1072         
1073         Log('ann', $target, $_[0], $text);
1074
1075         # send it if it isn't the except list and isn't isolated and still has a hop count
1076         # taking into account filtering and so on
1077         foreach $dxchan (@dxchan) {
1078                 my $routeit;
1079                 my ($filter, $hops);
1080
1081                 if ($dxchan->{annfilter}) {
1082                         ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1083                         next unless $filter;
1084                 } 
1085                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1086                         next if $dxchan == $self;
1087                         if ($hops) {
1088                                 $routeit = $line;
1089                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1090                         } else {
1091                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1092                                 next unless $routeit;
1093                         }
1094                         if ($filter) {
1095                                 $dxchan->send($routeit) if $routeit;
1096                         } else {
1097                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1098                                 
1099                         }
1100                 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1101                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1102                         my $buf = "$to$target de $_[0]: $text";
1103                         $buf .= "\a\a" if $dxchan->{beep};
1104                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1105                                 $dxchan->send($buf);
1106                         } else {
1107                                 $dxchan->delay($buf);
1108                         }
1109                 }                                       
1110         }
1111 }
1112
1113 sub send_local_config
1114 {
1115         my $self = shift;
1116         my $n;
1117         my @nodes;
1118         my @localnodes;
1119         my @remotenodes;
1120                 
1121         # send our nodes
1122         if ($self->{isolate}) {
1123                 @localnodes = (DXCluster->get_exact($main::mycall));
1124         } else {
1125                 # create a list of all the nodes that are not connected to this connection
1126                 # and are not themselves isolated, this to make sure that isolated nodes
1127         # don't appear outside of this node
1128                 @nodes = DXNode::get_all();
1129                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1130                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1131                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1132                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1133                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1134                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1135         }
1136
1137         my @s = $me->pc19(@localnodes, @remotenodes);
1138         for (@s) {
1139                 my $routeit = adjust_hops($self, $_);
1140                 $self->send($routeit) if $routeit;
1141         }
1142         
1143         # get all the users connected on the above nodes and send them out
1144         foreach $n (@localnodes, @remotenodes) {
1145                 my @users = values %{$n->list};
1146                 my @s = pc16($n, @users);
1147                 for (@s) {
1148                         my $routeit = adjust_hops($self, $_);
1149                         $self->send($routeit) if $routeit;
1150                 }
1151         }
1152 }
1153
1154 #
1155 # route a message down an appropriate interface for a callsign
1156 #
1157 # is called route(to, pcline);
1158 #
1159 sub route
1160 {
1161         my ($self, $call, $line) = @_;
1162         my $cl = DXCluster->get_exact($call);
1163         if ($cl) {       # don't route it back down itself
1164                 if (ref $self && $call eq $self->{call}) {
1165                         dbg('chan', "Trying to route back to source, dropped");
1166                         return;
1167                 }
1168                 my $hops;
1169                 my $dxchan = $cl->{dxchan};
1170                 if ($dxchan) {
1171                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1172                         if ($routeit) {
1173                                 $dxchan->send($routeit) if $dxchan;
1174                         }
1175                 }
1176         }
1177 }
1178
1179 # broadcast a message to all clusters taking into account isolation
1180 # [except those mentioned after buffer]
1181 sub broadcast_ak1a
1182 {
1183         my $s = shift;                          # the line to be rebroadcast
1184         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1185         my @dxchan = DXChannel::get_all_ak1a();
1186         my $dxchan;
1187         
1188         # send it if it isn't the except list and isn't isolated and still has a hop count
1189         foreach $dxchan (@dxchan) {
1190                 next if grep $dxchan == $_, @except;
1191                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1192                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1193         }
1194 }
1195
1196 # broadcast a message to all clusters ignoring isolation
1197 # [except those mentioned after buffer]
1198 sub broadcast_all_ak1a
1199 {
1200         my $s = shift;                          # the line to be rebroadcast
1201         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1202         my @dxchan = DXChannel::get_all_ak1a();
1203         my $dxchan;
1204         
1205         # send it if it isn't the except list and isn't isolated and still has a hop count
1206         foreach $dxchan (@dxchan) {
1207                 next if grep $dxchan == $_, @except;
1208                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1209                 $dxchan->send($routeit);
1210         }
1211 }
1212
1213 # broadcast to all users
1214 # storing the spot or whatever until it is in a state to receive it
1215 sub broadcast_users
1216 {
1217         my $s = shift;                          # the line to be rebroadcast
1218         my $sort = shift;           # the type of transmission
1219         my $fref = shift;           # a reference to an object to filter on
1220         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1221         my @dxchan = DXChannel::get_all_users();
1222         my $dxchan;
1223         my @out;
1224         
1225         foreach $dxchan (@dxchan) {
1226                 next if grep $dxchan == $_, @except;
1227                 push @out, $dxchan;
1228         }
1229         broadcast_list($s, $sort, $fref, @out);
1230 }
1231
1232 # broadcast to a list of users
1233 sub broadcast_list
1234 {
1235         my $s = shift;
1236         my $sort = shift;
1237         my $fref = shift;
1238         my $dxchan;
1239         
1240         foreach $dxchan (@_) {
1241                 my $filter = 1;
1242                 
1243                 if ($sort eq 'dx') {
1244                     next unless $dxchan->{dx};
1245                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1246                         next unless $filter;
1247                 }
1248                 next if $sort eq 'ann' && !$dxchan->{ann};
1249                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1250                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1251                 next if $sort eq 'wx' && !$dxchan->{wx};
1252
1253                 $s =~ s/\a//og unless $dxchan->{beep};
1254
1255                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1256                         $dxchan->send($s);      
1257                 } else {
1258                         $dxchan->delay($s);
1259                 }
1260         }
1261 }
1262
1263
1264 #
1265 # obtain the hops from the list for this callsign and pc no 
1266 #
1267
1268 sub get_hops
1269 {
1270         my $pcno = shift;
1271         my $hops = $DXProt::hopcount{$pcno};
1272         $hops = $DXProt::def_hopcount if !$hops;
1273         return "H$hops";       
1274 }
1275
1276
1277 # adjust the hop count on a per node basis using the user loadable 
1278 # hop table if available or else decrement an existing one
1279 #
1280
1281 sub adjust_hops
1282 {
1283         my $self = shift;
1284         my $s = shift;
1285         my $call = $self->{call};
1286         my $hops;
1287         
1288         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1289                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1290                 confess "$call called adjust_hops with '$s'" unless $pcno;
1291                 my $ref = $nodehops{$call} if %nodehops;
1292                 if ($ref) {
1293                         my $newhops = $ref->{$pcno};
1294                         return "" if defined $newhops && $newhops == 0;
1295                         $newhops = $ref->{default} unless $newhops;
1296                         return "" if defined $newhops && $newhops == 0;
1297                         $newhops = $hops if !$newhops;
1298                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1299                 } else {
1300                         # simply decrement it
1301                         $hops--;
1302                         return "" if !$hops;
1303                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1304                 }
1305         }
1306         return $s;
1307 }
1308
1309
1310 # load hop tables
1311 #
1312 sub load_hops
1313 {
1314         my $self = shift;
1315         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1316         do "$main::data/hop_table.pl";
1317         return $@ if $@;
1318         return 0;
1319 }
1320
1321
1322 # add a ping request to the ping queues
1323 sub addping
1324 {
1325         my ($from, $to) = @_;
1326         my $ref = $pings{$to} || [];
1327         my $r = {};
1328         $r->{call} = $from;
1329         $r->{t} = [ gettimeofday ];
1330         route(undef, $to, pc51($to, $main::mycall, 1));
1331         push @$ref, $r;
1332         $pings{$to} = $ref;
1333 }
1334
1335 # add a rcmd request to the rcmd queues
1336 sub addrcmd
1337 {
1338         my ($self, $to, $cmd) = @_;
1339
1340         my $r = {};
1341         $r->{call} = $self->{call};
1342         $r->{t} = $main::systime;
1343         $r->{cmd} = $cmd;
1344         $rcmds{$to} = $r;
1345
1346         my $ref = DXCluster->get_exact($to);
1347     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1348                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1349         } else {
1350                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1351         }
1352 }
1353 1;
1354 __END__