send unsolicited PC[38]5s to the sysop if online.
[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 || 1;     # other clusters can always be 'normal' users
100         $self->{lang} = $user->lang || 'en';
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                                 } else {
660                                         # send unsolicited ones to the sysop
661                                         my $dxchan = DXChannel->get($main::myalias);
662                                         $dxchan->send($field[3]) if $dxchan;
663                                 }
664                         } else {
665                                 my $ref = DXUser->get_current($field[1]);
666                                 if ($ref && $ref->is_clx) {
667                                         route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
668                                 } else {
669                                         $self->route($field[1], $line);
670                                 }
671                         }
672                         return;
673                 }
674                 
675                 # for pc 37 see 44 onwards
676
677                 if ($pcno == 38) {              # node connected list from neighbour
678                         return;
679                 }
680                 
681                 if ($pcno == 39) {              # incoming disconnect
682                         $self->disconnect(1);
683                         return;
684                 }
685                 
686                 if ($pcno == 41) {              # user info
687                         # add this station to the user database, if required
688                         my $user = DXUser->get_current($field[1]);
689                         if (!$user) {
690                                 # then try without an SSID
691                                 $field[1] =~ s/-\d+$//o;
692                                 $user = DXUser->get_current($field[1]);
693                         }
694                         $user = DXUser->new($field[1]) if !$user;
695                         
696                         if ($field[2] == 1) {
697                                 $user->name($field[3]);
698                         } elsif ($field[2] == 2) {
699                                 $user->qth($field[3]);
700                         } elsif ($field[2] == 3) {
701                                 my ($lat, $long) = DXBearing::stoll($field[3]);
702                                 $user->lat($lat);
703                                 $user->long($long);
704                                 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
705                         } elsif ($field[2] == 4) {
706                                 $user->homenode($field[3]);
707                         }
708                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
709                         $user->put;
710                         last SWITCH;
711                 }
712                 if ($pcno == 43) {
713                         last SWITCH;
714                 }
715                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
716                         DXDb::process($self, $line);
717                         return;
718                 }
719                 
720                 if ($pcno == 50) {              # keep alive/user list
721                         my $node = DXCluster->get_exact($field[1]);
722                         if ($node) {
723                                 return unless $node->isa('DXNode');
724                                 return unless $node->dxchan == $self;
725                                 $node->update_users($field[2]);
726                         }
727                         last SWITCH;
728                 }
729                 
730                 if ($pcno == 51) {              # incoming ping requests/answers
731                         
732                         # is it for us?
733                         if ($field[1] eq $main::mycall) {
734                                 my $flag = $field[3];
735                                 if ($flag == 1) {
736                                         $self->send(pc51($field[2], $field[1], '0'));
737                                 } else {
738                                         # it's a reply, look in the ping list for this one
739                                         my $ref = $pings{$field[2]};
740                                         if ($ref) {
741                                                 my $tochan =  DXChannel->get($field[2]);
742                                                 while (@$ref) {
743                                                         my $r = shift @$ref;
744                                                         my $dxchan = DXChannel->get($r->{call});
745                                                         next unless $dxchan;
746                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
747                                                         if ($dxchan->is_user) {
748                                                                 my $s = sprintf "%.2f", $t; 
749                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
750                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
751                                                         } elsif ($dxchan->is_node) {
752                                                                 if ($tochan) {
753                                                                         $tochan->{nopings} = 2; # pump up the timer
754                                                                         push @{$tochan->{pingtime}}, $t;
755                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
756                                                                         my $st;
757                                                                         for (@{$tochan->{pingtime}}) {
758                                                                                 $st += $_;
759                                                                         }
760                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
761                                                                 }
762                                                         } 
763                                                 }
764                                         }
765                                 }
766                         } else {
767                                 # route down an appropriate thingy
768                                 $self->route($field[1], $line);
769                         }
770                         return;
771                 }
772
773                 if ($pcno == 73) {  # WCY broadcasts
774                         
775                         # do some de-duping
776                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
777                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
778                                 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
779                                 return;
780                         }
781                         @field = map { unpad($_) } @field;
782                         if (WCY::dup($d,@field[3..7])) {
783                                 dbg('chan', "Dup WCY Spot ignored\n");
784                                 return;
785                         }
786                 
787                         my $wcy = WCY::update($d, @field[2..12]);
788
789                         my $rep;
790                         eval {
791                                 $rep = Local::wwv($self, @field[1..12]);
792                         };
793                         # dbg('local', "Local::wcy error $@") if $@;
794                         return if $rep;
795
796                         # broadcast to the eager world
797                         send_wcy_spot($self, $line, $d, @field[2..12]);
798                         return;
799                 }
800
801                 if ($pcno == 84) { # remote commands (incoming)
802                         if ($field[1] eq $main::mycall) {
803                                 my $ref = DXUser->get_current($field[2]);
804                                 my $cref = DXCluster->get($field[2]);
805                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
806                                 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
807                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
808                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
809                                                 my $oldpriv = $self->{priv};
810                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
811                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
812                                                 $self->{priv} = $oldpriv;
813                                                 for (@in) {
814                                                         s/\s*$//og;
815                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
816                                                         Log('rcmd', 'out', $field[2], $_);
817                                                 }
818                                                 delete $self->{remotecmd};
819                                         } else {
820                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
821                                         }
822                                 } else {
823                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
824                                 }
825                         } else {
826                                 my $ref = DXUser->get_current($field[1]);
827                                 if ($ref && $ref->is_clx) {
828                                         $self->route($field[1], $line);
829                                 } else {
830                                         route($field[1], pc34($field[2], $field[1], $field[4]));
831                                 }
832                         }
833                         return;
834                 }
835
836                 if ($pcno == 85) {              # remote command replies
837                         if ($field[1] eq $main::mycall) {
838                                 my $dxchan = DXChannel->get($field[3]);
839                                 if ($dxchan) {
840                                         $dxchan->send($field[4]);
841                                 } else {
842                                         my $s = $rcmds{$field[2]};
843                                         if ($s) {
844                                                 $dxchan = DXChannel->get($s->{call});
845                                                 $dxchan->send($field[4]) if $dxchan;
846                                                 delete $rcmds{$field[2]} if !$dxchan;
847                                         } else {
848                                                 # send unsolicited ones to the sysop
849                                                 my $dxchan = DXChannel->get($main::myalias);
850                                                 $dxchan->send($field[4]) if $dxchan;
851                                         }
852                                 }
853                         } else {
854                                 my $ref = DXUser->get_current($field[1]);
855                                 if ($ref && $ref->is_clx) {
856                                         $self->route($field[1], $line);
857                                 } else {
858                                         route($field[1], pc35($field[2], $field[1], $field[4]));
859                                 }
860                         }
861                         return;
862                 }
863         }
864          
865         # if get here then rebroadcast the thing with its Hop count decremented (if
866         # there is one). If it has a hop count and it decrements to zero then don't
867         # rebroadcast it.
868         #
869         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
870         #        REBROADCAST!!!!
871         #
872          
873         unless ($self->{isolate}) {
874                 broadcast_ak1a($line, $self); # send it to everyone but me
875         }
876 }
877
878 #
879 # This is called from inside the main cluster processing loop and is used
880 # for despatching commands that are doing some long processing job
881 #
882 sub process
883 {
884         my $t = time;
885         my @dxchan = DXChannel->get_all();
886         my $dxchan;
887         
888         foreach $dxchan (@dxchan) {
889                 next unless $dxchan->is_node();
890                 next if $dxchan == $me;
891                 
892                 # send a pc50 out on this channel
893                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
894                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
895                         $dxchan->pc50_t($t);
896                 } 
897
898                 # send a ping out on this channel
899                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
900                         if ($dxchan->{nopings} <= 0) {
901                                 $dxchan->disconnect;
902                         } else {
903                                 addping($main::mycall, $dxchan->call);
904                                 $dxchan->{nopings} -= 1;
905                                 $dxchan->{lastping} = $t;
906                         }
907                 }
908         }
909         
910         my $key;
911         my $val;
912         my $cutoff;
913         if ($main::systime - 3600 > $last_hour) {
914                 Spot::process;
915                 Geomag::process;
916                 AnnTalk::process;
917                 $last_hour = $main::systime;
918         }
919 }
920
921 #
922 # finish up a pc context
923 #
924 sub finish
925 {
926         my $self = shift;
927         my $call = $self->call;
928         my $conn = shift;
929         my $ref = DXCluster->get_exact($call);
930         
931         # unbusy and stop and outgoing mail
932         my $mref = DXMsg::get_busy($call);
933         $mref->stop_msg($call) if $mref;
934         
935         # broadcast to all other nodes that all the nodes connected to via me are gone
936         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
937         my $node;
938         
939         foreach $node (@gonenodes) {
940                 next if $node->call eq $call;
941                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
942                 $node->del();
943         }
944
945         # remove outstanding pings
946         delete $pings{$call};
947         
948         # now broadcast to all other ak1a nodes that I have gone
949         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
950
951         # I was the last node visited
952     $self->user->node($main::mycall);
953
954         # send info to all logged in thingies
955         $self->tell_login('logoutn');
956
957         Log('DXProt', $call . " Disconnected");
958         $ref->del() if $ref;
959 }
960
961 #
962 # some active measures
963 #
964 sub send_dx_spot
965 {
966         my $self = shift;
967         my $line = shift;
968         my @dxchan = DXChannel->get_all();
969         my $dxchan;
970         
971         # send it if it isn't the except list and isn't isolated and still has a hop count
972         # taking into account filtering and so on
973         foreach $dxchan (@dxchan) {
974                 my $routeit;
975                 my ($filter, $hops);
976
977                 if ($dxchan->{spotfilter}) {
978                     ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
979                         next unless $filter;
980                 }
981                 
982                 if ($dxchan->is_node) {
983                         next if $dxchan == $self;
984                         if ($hops) {
985                                 $routeit = $line;
986                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
987                         } else {
988                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
989                                 next unless $routeit;
990                         }
991                         if ($filter) {
992                                 $dxchan->send($routeit) if $routeit;
993                         } else {
994                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
995                         }
996                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
997                         my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
998                         $buf .= "\a\a" if $dxchan->{beep};
999                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1000                                 $dxchan->send($buf);
1001                         } else {
1002                                 $dxchan->delay($buf);
1003                         }
1004                 }                                       
1005         }
1006 }
1007
1008 sub send_wwv_spot
1009 {
1010         my $self = shift;
1011         my $line = shift;
1012         my @dxchan = DXChannel->get_all();
1013         my $dxchan;
1014         
1015         # send it if it isn't the except list and isn't isolated and still has a hop count
1016         # taking into account filtering and so on
1017         foreach $dxchan (@dxchan) {
1018                 my $routeit;
1019                 my ($filter, $hops);
1020
1021                 if ($dxchan->{wwvfilter}) {
1022                          ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1023                          next unless $filter;
1024                 }
1025                 if ($dxchan->is_node) {
1026                         next if $dxchan == $self;
1027                         if ($hops) {
1028                                 $routeit = $line;
1029                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1030                         } else {
1031                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1032                                 next unless $routeit;
1033                         }
1034                         if ($filter) {
1035                                 $dxchan->send($routeit) if $routeit;
1036                         } else {
1037                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1038                                 
1039                         }
1040                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1041                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1042                         $buf .= "\a\a" if $dxchan->{beep};
1043                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1044                                 $dxchan->send($buf);
1045                         } else {
1046                                 $dxchan->delay($buf);
1047                         }
1048                 }                                       
1049         }
1050 }
1051
1052 sub send_wcy_spot
1053 {
1054         my $self = shift;
1055         my $line = shift;
1056         my @dxchan = DXChannel->get_all();
1057         my $dxchan;
1058         
1059         # send it if it isn't the except list and isn't isolated and still has a hop count
1060         # taking into account filtering and so on
1061         foreach $dxchan (@dxchan) {
1062                 my $routeit;
1063                 my ($filter, $hops);
1064
1065                 if ($dxchan->{wcyfilter}) {
1066                          ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1067                          next unless $filter;
1068                 }
1069                 if ($dxchan->is_clx || $dxchan->is_spider) {
1070                         next if $dxchan == $self;
1071                         if ($hops) {
1072                                 $routeit = $line;
1073                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1074                         } else {
1075                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1076                                 next unless $routeit;
1077                         }
1078                         if ($filter) {
1079                                 $dxchan->send($routeit) if $routeit;
1080                         } else {
1081                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1082                         }
1083                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1084                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1085                         $buf .= "\a\a" if $dxchan->{beep};
1086                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1087                                 $dxchan->send($buf);
1088                         } else {
1089                                 $dxchan->delay($buf);
1090                         }
1091                 }                                       
1092         }
1093 }
1094
1095 # send an announce
1096 sub send_announce
1097 {
1098         my $self = shift;
1099         my $line = shift;
1100         my @dxchan = DXChannel->get_all();
1101         my $dxchan;
1102         my $text = unpad($_[2]);
1103         my $target;
1104         my $to = 'To ';
1105                                 
1106         if ($_[3] eq '*') {     # sysops
1107                 $target = "SYSOP";
1108         } elsif ($_[3] gt ' ') { # speciality list handling
1109                 my ($name) = split /\./, $_[3]; 
1110                 $target = "$name"; # put the rest in later (if bothered) 
1111         } 
1112         
1113         if ($_[5] eq '1') {
1114                 $target = "WX"; 
1115                 $to = '';
1116         }
1117         $target = "All" if !$target;
1118         
1119         Log('ann', $target, $_[0], $text);
1120
1121         # send it if it isn't the except list and isn't isolated and still has a hop count
1122         # taking into account filtering and so on
1123         foreach $dxchan (@dxchan) {
1124                 my $routeit;
1125                 my ($filter, $hops);
1126
1127                 if ($dxchan->{annfilter}) {
1128                         ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1129                         next unless $filter;
1130                 } 
1131                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1132                         next if $dxchan == $self;
1133                         if ($hops) {
1134                                 $routeit = $line;
1135                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1136                         } else {
1137                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1138                                 next unless $routeit;
1139                         }
1140                         if ($filter) {
1141                                 $dxchan->send($routeit) if $routeit;
1142                         } else {
1143                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1144                                 
1145                         }
1146                 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1147                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1148                         my $buf = "$to$target de $_[0]: $text";
1149                         $buf .= "\a\a" if $dxchan->{beep};
1150                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1151                                 $dxchan->send($buf);
1152                         } else {
1153                                 $dxchan->delay($buf);
1154                         }
1155                 }                                       
1156         }
1157 }
1158
1159 sub send_local_config
1160 {
1161         my $self = shift;
1162         my $n;
1163         my @nodes;
1164         my @localnodes;
1165         my @remotenodes;
1166                 
1167         # send our nodes
1168         if ($self->{isolate}) {
1169                 @localnodes = (DXCluster->get_exact($main::mycall));
1170         } else {
1171                 # create a list of all the nodes that are not connected to this connection
1172                 # and are not themselves isolated, this to make sure that isolated nodes
1173         # don't appear outside of this node
1174                 @nodes = DXNode::get_all();
1175                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1176                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1177                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1178                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1179                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1180                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1181         }
1182
1183         my @s = $me->pc19(@localnodes, @remotenodes);
1184         for (@s) {
1185                 my $routeit = adjust_hops($self, $_);
1186                 $self->send($routeit) if $routeit;
1187         }
1188         
1189         # get all the users connected on the above nodes and send them out
1190         foreach $n (@localnodes, @remotenodes) {
1191                 my @users = values %{$n->list};
1192                 my @s = pc16($n, @users);
1193                 for (@s) {
1194                         my $routeit = adjust_hops($self, $_);
1195                         $self->send($routeit) if $routeit;
1196                 }
1197         }
1198 }
1199
1200 #
1201 # route a message down an appropriate interface for a callsign
1202 #
1203 # is called route(to, pcline);
1204 #
1205 sub route
1206 {
1207         my ($self, $call, $line) = @_;
1208         my $cl = DXCluster->get_exact($call);
1209         if ($cl) {       # don't route it back down itself
1210                 if (ref $self && $call eq $self->{call}) {
1211                         dbg('chan', "Trying to route back to source, dropped");
1212                         return;
1213                 }
1214                 my $hops;
1215                 my $dxchan = $cl->{dxchan};
1216                 if ($dxchan) {
1217                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1218                         if ($routeit) {
1219                                 $dxchan->send($routeit) if $dxchan;
1220                         }
1221                 }
1222         }
1223 }
1224
1225 # broadcast a message to all clusters taking into account isolation
1226 # [except those mentioned after buffer]
1227 sub broadcast_ak1a
1228 {
1229         my $s = shift;                          # the line to be rebroadcast
1230         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1231         my @dxchan = DXChannel::get_all_ak1a();
1232         my $dxchan;
1233         
1234         # send it if it isn't the except list and isn't isolated and still has a hop count
1235         foreach $dxchan (@dxchan) {
1236                 next if grep $dxchan == $_, @except;
1237                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1238                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1239         }
1240 }
1241
1242 # broadcast a message to all clusters ignoring isolation
1243 # [except those mentioned after buffer]
1244 sub broadcast_all_ak1a
1245 {
1246         my $s = shift;                          # the line to be rebroadcast
1247         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1248         my @dxchan = DXChannel::get_all_ak1a();
1249         my $dxchan;
1250         
1251         # send it if it isn't the except list and isn't isolated and still has a hop count
1252         foreach $dxchan (@dxchan) {
1253                 next if grep $dxchan == $_, @except;
1254                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1255                 $dxchan->send($routeit);
1256         }
1257 }
1258
1259 # broadcast to all users
1260 # storing the spot or whatever until it is in a state to receive it
1261 sub broadcast_users
1262 {
1263         my $s = shift;                          # the line to be rebroadcast
1264         my $sort = shift;           # the type of transmission
1265         my $fref = shift;           # a reference to an object to filter on
1266         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1267         my @dxchan = DXChannel::get_all_users();
1268         my $dxchan;
1269         my @out;
1270         
1271         foreach $dxchan (@dxchan) {
1272                 next if grep $dxchan == $_, @except;
1273                 push @out, $dxchan;
1274         }
1275         broadcast_list($s, $sort, $fref, @out);
1276 }
1277
1278 # broadcast to a list of users
1279 sub broadcast_list
1280 {
1281         my $s = shift;
1282         my $sort = shift;
1283         my $fref = shift;
1284         my $dxchan;
1285         
1286         foreach $dxchan (@_) {
1287                 my $filter = 1;
1288                 
1289                 if ($sort eq 'dx') {
1290                     next unless $dxchan->{dx};
1291                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1292                         next unless $filter;
1293                 }
1294                 next if $sort eq 'ann' && !$dxchan->{ann};
1295                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1296                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1297                 next if $sort eq 'wx' && !$dxchan->{wx};
1298
1299                 $s =~ s/\a//og unless $dxchan->{beep};
1300
1301                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1302                         $dxchan->send($s);      
1303                 } else {
1304                         $dxchan->delay($s);
1305                 }
1306         }
1307 }
1308
1309
1310 #
1311 # obtain the hops from the list for this callsign and pc no 
1312 #
1313
1314 sub get_hops
1315 {
1316         my $pcno = shift;
1317         my $hops = $DXProt::hopcount{$pcno};
1318         $hops = $DXProt::def_hopcount if !$hops;
1319         return "H$hops";       
1320 }
1321
1322
1323 # adjust the hop count on a per node basis using the user loadable 
1324 # hop table if available or else decrement an existing one
1325 #
1326
1327 sub adjust_hops
1328 {
1329         my $self = shift;
1330         my $s = shift;
1331         my $call = $self->{call};
1332         my $hops;
1333         
1334         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1335                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1336                 confess "$call called adjust_hops with '$s'" unless $pcno;
1337                 my $ref = $nodehops{$call} if %nodehops;
1338                 if ($ref) {
1339                         my $newhops = $ref->{$pcno};
1340                         return "" if defined $newhops && $newhops == 0;
1341                         $newhops = $ref->{default} unless $newhops;
1342                         return "" if defined $newhops && $newhops == 0;
1343                         $newhops = $hops if !$newhops;
1344                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1345                 } else {
1346                         # simply decrement it
1347                         $hops--;
1348                         return "" if !$hops;
1349                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1350                 }
1351         }
1352         return $s;
1353 }
1354
1355
1356 # load hop tables
1357 #
1358 sub load_hops
1359 {
1360         my $self = shift;
1361         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1362         do "$main::data/hop_table.pl";
1363         return $@ if $@;
1364         return 0;
1365 }
1366
1367
1368 # add a ping request to the ping queues
1369 sub addping
1370 {
1371         my ($from, $to) = @_;
1372         my $ref = $pings{$to} || [];
1373         my $r = {};
1374         $r->{call} = $from;
1375         $r->{t} = [ gettimeofday ];
1376         route(undef, $to, pc51($to, $main::mycall, 1));
1377         push @$ref, $r;
1378         $pings{$to} = $ref;
1379 }
1380
1381 # add a rcmd request to the rcmd queues
1382 sub addrcmd
1383 {
1384         my ($self, $to, $cmd) = @_;
1385
1386         my $r = {};
1387         $r->{call} = $self->{call};
1388         $r->{t} = $main::systime;
1389         $r->{cmd} = $cmd;
1390         $rcmds{$to} = $r;
1391
1392         my $ref = DXCluster->get_exact($to);
1393     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1394                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1395         } else {
1396                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1397         }
1398 }
1399
1400 sub disconnect
1401 {
1402         my $self = shift;
1403         my $nopc39 = shift;
1404
1405         if ($self->{conn} && !$nopc39) {
1406                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1407         }
1408
1409         $self->SUPER::disconnect;
1410 }
1411 1;
1412 __END__