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