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