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