1. I believe I have fixed all the login/logout 'broken pipe' errors
[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 $conn = shift;
882         my $ref = DXCluster->get_exact($call);
883         
884         # unbusy and stop and outgoing mail
885         my $mref = DXMsg::get_busy($call);
886         $mref->stop_msg($call) if $mref;
887         
888         # broadcast to all other nodes that all the nodes connected to via me are gone
889         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
890         my $node;
891         
892         foreach $node (@gonenodes) {
893                 next if $node->call eq $call;
894                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
895                 $node->del();
896         }
897
898         # remove outstanding pings
899         delete $pings{$call};
900         
901         # now broadcast to all other ak1a nodes that I have gone
902         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
903
904         # I was the last node visited
905     $self->user->node($main::mycall);
906
907         # send info to all logged in thingies
908         $self->tell_login('logoutn');
909
910         Log('DXProt', $call . " Disconnected");
911         $ref->del() if $ref;
912 }
913
914 #
915 # some active measures
916 #
917 sub send_dx_spot
918 {
919         my $self = shift;
920         my $line = shift;
921         my @dxchan = DXChannel->get_all();
922         my $dxchan;
923         
924         # send it if it isn't the except list and isn't isolated and still has a hop count
925         # taking into account filtering and so on
926         foreach $dxchan (@dxchan) {
927                 my $routeit;
928                 my ($filter, $hops);
929
930                 if ($dxchan->{spotfilter}) {
931                     ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
932                         next unless $filter;
933                 }
934                 
935                 if ($dxchan->is_node) {
936                         next if $dxchan == $self;
937                         if ($hops) {
938                                 $routeit = $line;
939                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
940                         } else {
941                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
942                                 next unless $routeit;
943                         }
944                         if ($filter) {
945                                 $dxchan->send($routeit) if $routeit;
946                         } else {
947                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
948                         }
949                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
950                         my $buf = Spot::formatb($_[0], $_[1], $_[2], $_[3], $_[4]);
951                         $buf .= "\a\a" if $dxchan->{beep};
952                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
953                                 $dxchan->send($buf);
954                         } else {
955                                 $dxchan->delay($buf);
956                         }
957                 }                                       
958         }
959 }
960
961 sub send_wwv_spot
962 {
963         my $self = shift;
964         my $line = shift;
965         my @dxchan = DXChannel->get_all();
966         my $dxchan;
967         
968         # send it if it isn't the except list and isn't isolated and still has a hop count
969         # taking into account filtering and so on
970         foreach $dxchan (@dxchan) {
971                 my $routeit;
972                 my ($filter, $hops);
973
974                 if ($dxchan->{wwvfilter}) {
975                          ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
976                          next unless $filter;
977                 }
978                 if ($dxchan->is_node) {
979                         next if $dxchan == $self;
980                         if ($hops) {
981                                 $routeit = $line;
982                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
983                         } else {
984                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
985                                 next unless $routeit;
986                         }
987                         if ($filter) {
988                                 $dxchan->send($routeit) if $routeit;
989                         } else {
990                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
991                                 
992                         }
993                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
994                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
995                         $buf .= "\a\a" if $dxchan->{beep};
996                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
997                                 $dxchan->send($buf);
998                         } else {
999                                 $dxchan->delay($buf);
1000                         }
1001                 }                                       
1002         }
1003 }
1004
1005 sub send_wcy_spot
1006 {
1007         my $self = shift;
1008         my $line = shift;
1009         my @dxchan = DXChannel->get_all();
1010         my $dxchan;
1011         
1012         # send it if it isn't the except list and isn't isolated and still has a hop count
1013         # taking into account filtering and so on
1014         foreach $dxchan (@dxchan) {
1015                 my $routeit;
1016                 my ($filter, $hops);
1017
1018                 if ($dxchan->{wcyfilter}) {
1019                          ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1020                          next unless $filter;
1021                 }
1022                 if ($dxchan->is_clx || $dxchan->is_spider) {
1023                         next if $dxchan == $self;
1024                         if ($hops) {
1025                                 $routeit = $line;
1026                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1027                         } else {
1028                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1029                                 next unless $routeit;
1030                         }
1031                         if ($filter) {
1032                                 $dxchan->send($routeit) if $routeit;
1033                         } else {
1034                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1035                         }
1036                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1037                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1038                         $buf .= "\a\a" if $dxchan->{beep};
1039                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1040                                 $dxchan->send($buf);
1041                         } else {
1042                                 $dxchan->delay($buf);
1043                         }
1044                 }                                       
1045         }
1046 }
1047
1048 # send an announce
1049 sub send_announce
1050 {
1051         my $self = shift;
1052         my $line = shift;
1053         my @dxchan = DXChannel->get_all();
1054         my $dxchan;
1055         my $text = unpad($_[2]);
1056         my $target;
1057         my $to = 'To ';
1058                                 
1059         if ($_[3] eq '*') {     # sysops
1060                 $target = "SYSOP";
1061         } elsif ($_[3] gt ' ') { # speciality list handling
1062                 my ($name) = split /\./, $_[3]; 
1063                 $target = "$name"; # put the rest in later (if bothered) 
1064         } 
1065         
1066         if ($_[5] eq '1') {
1067                 $target = "WX"; 
1068                 $to = '';
1069         }
1070         $target = "All" if !$target;
1071         
1072         Log('ann', $target, $_[0], $text);
1073
1074         # send it if it isn't the except list and isn't isolated and still has a hop count
1075         # taking into account filtering and so on
1076         foreach $dxchan (@dxchan) {
1077                 my $routeit;
1078                 my ($filter, $hops);
1079
1080                 if ($dxchan->{annfilter}) {
1081                         ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1082                         next unless $filter;
1083                 } 
1084                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1085                         next if $dxchan == $self;
1086                         if ($hops) {
1087                                 $routeit = $line;
1088                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1089                         } else {
1090                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1091                                 next unless $routeit;
1092                         }
1093                         if ($filter) {
1094                                 $dxchan->send($routeit) if $routeit;
1095                         } else {
1096                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1097                                 
1098                         }
1099                 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1100                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1101                         my $buf = "$to$target de $_[0]: $text";
1102                         $buf .= "\a\a" if $dxchan->{beep};
1103                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1104                                 $dxchan->send($buf);
1105                         } else {
1106                                 $dxchan->delay($buf);
1107                         }
1108                 }                                       
1109         }
1110 }
1111
1112 sub send_local_config
1113 {
1114         my $self = shift;
1115         my $n;
1116         my @nodes;
1117         my @localnodes;
1118         my @remotenodes;
1119                 
1120         # send our nodes
1121         if ($self->{isolate}) {
1122                 @localnodes = (DXCluster->get_exact($main::mycall));
1123         } else {
1124                 # create a list of all the nodes that are not connected to this connection
1125                 # and are not themselves isolated, this to make sure that isolated nodes
1126         # don't appear outside of this node
1127                 @nodes = DXNode::get_all();
1128                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1129                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1130                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1131                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1132                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1133                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1134         }
1135
1136         my @s = $me->pc19(@localnodes, @remotenodes);
1137         for (@s) {
1138                 my $routeit = adjust_hops($self, $_);
1139                 $self->send($routeit) if $routeit;
1140         }
1141         
1142         # get all the users connected on the above nodes and send them out
1143         foreach $n (@localnodes, @remotenodes) {
1144                 my @users = values %{$n->list};
1145                 my @s = pc16($n, @users);
1146                 for (@s) {
1147                         my $routeit = adjust_hops($self, $_);
1148                         $self->send($routeit) if $routeit;
1149                 }
1150         }
1151 }
1152
1153 #
1154 # route a message down an appropriate interface for a callsign
1155 #
1156 # is called route(to, pcline);
1157 #
1158 sub route
1159 {
1160         my ($self, $call, $line) = @_;
1161         my $cl = DXCluster->get_exact($call);
1162         if ($cl) {       # don't route it back down itself
1163                 if (ref $self && $call eq $self->{call}) {
1164                         dbg('chan', "Trying to route back to source, dropped");
1165                         return;
1166                 }
1167                 my $hops;
1168                 my $dxchan = $cl->{dxchan};
1169                 if ($dxchan) {
1170                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1171                         if ($routeit) {
1172                                 $dxchan->send($routeit) if $dxchan;
1173                         }
1174                 }
1175         }
1176 }
1177
1178 # broadcast a message to all clusters taking into account isolation
1179 # [except those mentioned after buffer]
1180 sub broadcast_ak1a
1181 {
1182         my $s = shift;                          # the line to be rebroadcast
1183         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1184         my @dxchan = DXChannel::get_all_ak1a();
1185         my $dxchan;
1186         
1187         # send it if it isn't the except list and isn't isolated and still has a hop count
1188         foreach $dxchan (@dxchan) {
1189                 next if grep $dxchan == $_, @except;
1190                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1191                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1192         }
1193 }
1194
1195 # broadcast a message to all clusters ignoring isolation
1196 # [except those mentioned after buffer]
1197 sub broadcast_all_ak1a
1198 {
1199         my $s = shift;                          # the line to be rebroadcast
1200         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1201         my @dxchan = DXChannel::get_all_ak1a();
1202         my $dxchan;
1203         
1204         # send it if it isn't the except list and isn't isolated and still has a hop count
1205         foreach $dxchan (@dxchan) {
1206                 next if grep $dxchan == $_, @except;
1207                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1208                 $dxchan->send($routeit);
1209         }
1210 }
1211
1212 # broadcast to all users
1213 # storing the spot or whatever until it is in a state to receive it
1214 sub broadcast_users
1215 {
1216         my $s = shift;                          # the line to be rebroadcast
1217         my $sort = shift;           # the type of transmission
1218         my $fref = shift;           # a reference to an object to filter on
1219         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1220         my @dxchan = DXChannel::get_all_users();
1221         my $dxchan;
1222         my @out;
1223         
1224         foreach $dxchan (@dxchan) {
1225                 next if grep $dxchan == $_, @except;
1226                 push @out, $dxchan;
1227         }
1228         broadcast_list($s, $sort, $fref, @out);
1229 }
1230
1231 # broadcast to a list of users
1232 sub broadcast_list
1233 {
1234         my $s = shift;
1235         my $sort = shift;
1236         my $fref = shift;
1237         my $dxchan;
1238         
1239         foreach $dxchan (@_) {
1240                 my $filter = 1;
1241                 
1242                 if ($sort eq 'dx') {
1243                     next unless $dxchan->{dx};
1244                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1245                         next unless $filter;
1246                 }
1247                 next if $sort eq 'ann' && !$dxchan->{ann};
1248                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1249                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1250                 next if $sort eq 'wx' && !$dxchan->{wx};
1251
1252                 $s =~ s/\a//og unless $dxchan->{beep};
1253
1254                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1255                         $dxchan->send($s);      
1256                 } else {
1257                         $dxchan->delay($s);
1258                 }
1259         }
1260 }
1261
1262
1263 #
1264 # obtain the hops from the list for this callsign and pc no 
1265 #
1266
1267 sub get_hops
1268 {
1269         my $pcno = shift;
1270         my $hops = $DXProt::hopcount{$pcno};
1271         $hops = $DXProt::def_hopcount if !$hops;
1272         return "H$hops";       
1273 }
1274
1275
1276 # adjust the hop count on a per node basis using the user loadable 
1277 # hop table if available or else decrement an existing one
1278 #
1279
1280 sub adjust_hops
1281 {
1282         my $self = shift;
1283         my $s = shift;
1284         my $call = $self->{call};
1285         my $hops;
1286         
1287         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1288                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1289                 confess "$call called adjust_hops with '$s'" unless $pcno;
1290                 my $ref = $nodehops{$call} if %nodehops;
1291                 if ($ref) {
1292                         my $newhops = $ref->{$pcno};
1293                         return "" if defined $newhops && $newhops == 0;
1294                         $newhops = $ref->{default} unless $newhops;
1295                         return "" if defined $newhops && $newhops == 0;
1296                         $newhops = $hops if !$newhops;
1297                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1298                 } else {
1299                         # simply decrement it
1300                         $hops--;
1301                         return "" if !$hops;
1302                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1303                 }
1304         }
1305         return $s;
1306 }
1307
1308
1309 # load hop tables
1310 #
1311 sub load_hops
1312 {
1313         my $self = shift;
1314         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1315         do "$main::data/hop_table.pl";
1316         return $@ if $@;
1317         return 0;
1318 }
1319
1320
1321 # add a ping request to the ping queues
1322 sub addping
1323 {
1324         my ($from, $to) = @_;
1325         my $ref = $pings{$to} || [];
1326         my $r = {};
1327         $r->{call} = $from;
1328         $r->{t} = [ gettimeofday ];
1329         route(undef, $to, pc51($to, $main::mycall, 1));
1330         push @$ref, $r;
1331         $pings{$to} = $ref;
1332 }
1333
1334 # add a rcmd request to the rcmd queues
1335 sub addrcmd
1336 {
1337         my ($self, $to, $cmd) = @_;
1338
1339         my $r = {};
1340         $r->{call} = $self->{call};
1341         $r->{t} = $main::systime;
1342         $r->{cmd} = $cmd;
1343         $rcmds{$to} = $r;
1344
1345         my $ref = DXCluster->get_exact($to);
1346     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1347                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1348         } else {
1349                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1350         }
1351 }
1352
1353 sub disconnect
1354 {
1355         my $self = shift;
1356         my $nopc39 = shift;
1357
1358         if ($self->{conn} && !$nopc39) {
1359                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1360         }
1361
1362         $self->SUPER::disconnect;
1363 }
1364 1;
1365 __END__