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