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