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