3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
31 use Time::HiRes qw(gettimeofday tv_interval);
34 use vars qw($me $pc11_max_age $pc23_max_age
35 $last_hour %pings %rcmds
36 %nodehops @baddx $baddxfn
37 $allowzero $decode_dk0wcy);
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
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
50 $baddxfn = "$main::data/baddx.pl";
54 my $user = DXUser->get($main::mycall);
55 $DXProt::myprot_version += $main::version*100;
56 $me = DXProt->new($main::mycall, 0, $user);
58 $me->{state} = "indifferent";
59 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
61 # $me->{sort} = 'M'; # M for me
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]);
68 for (Geomag::readfile(time)) {
69 Geomag::dup(@{$_}[1..5]);
73 do "$baddxfn" if -e "$baddxfn";
78 # obtain a new connection this is derived from dxchannel
83 my $self = DXChannel::alloc(@_);
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).
92 my ($self, $line, $sort) = @_;
93 my $call = $self->{call};
94 my $user = $self->{user};
96 # remember type of connection
97 $self->{consort} = $line;
98 $self->{outbound} = $sort eq 'O';
99 $self->{priv} = $user->priv;
100 $self->{lang} = $user->lang;
101 $self->{isolate} = $user->{isolate};
102 $self->{consort} = $line; # save the connection type
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);
111 # set unbuffered and no echo
112 $self->send_now('B',"0");
113 $self->send_now('E',"0");
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;
123 # send initialisation string
124 unless ($self->{outbound}) {
125 $self->send(pc38()) if DXNode->get_all();
127 $self->{lastping} = $main::systime;
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;
133 $self->state('init');
136 # send info to all logged in thingies
137 $self->tell_login('loginn');
139 Log('DXProt', "$call connected");
143 # This is the normal pcxx despatcher
147 my ($self, $line) = @_;
148 my @field = split /\^/, $line;
149 pop @field if $field[-1] eq '~';
151 # print join(',', @field), "\n";
153 # ignore any lines that don't start with PC
154 return if !$field[0] =~ /^PC/;
157 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
159 return if $pcno < 10 || $pcno > 99;
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");
170 $pcr = Local::pcprot($self, $pcno, @field);
172 # dbg('local', "Local::pcprot error $@") if $@;
176 if ($pcno == 10) { # incoming talk
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()) {
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};
189 $self->route($field[2], $line); # relay it on its way
194 if ($pcno == 11 || $pcno == 26) { # dx spot
196 # route 'foreign' pc26s
198 if ($field[7] ne $main::mycall) {
199 $self->route($field[7], $line);
204 # if this is a 'nodx' node then ignore it
205 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
206 dbg('chan', "Bad DXNode, dropped");
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");
219 if (grep $field[2] eq $_, @baddx) {
220 dbg('chan', "Bad DX spot, ignored");
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");
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");
237 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
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
246 # fix up qra locators of known users
247 my $user = DXUser->get_current($spot[4]);
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));
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.
262 unless ($user->qra) {
264 my $to = $user->homenode;
265 my $last = $user->lastoper || 0;
266 if ($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));
272 route(undef, $to, pc34($main::mycall, $to, $cmd));
274 $user->lastoper($main::systime);
283 $r = Local::spot($self, @spot);
285 # dbg('local', "Local::spot1 error $@") if $@;
288 # DON'T be silly and send on PC26s!
289 return if $pcno == 26;
291 # send out the filtered spots
292 send_dx_spot($self, $line, @spot) if @spot;
296 if ($pcno == 12) { # announces
297 # announce duplicate checking
298 $field[3] =~ s/^\s+//; # remove leading blanks
299 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
300 dbg('chan', "Duplicate Announce ignored\n");
304 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
306 # global ann filtering on INPUT
307 if ($self->{inannfilter}) {
308 my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
310 dbg('chan', "Rejected by filter");
316 $self->send_announce($line, @field[1..6]);
318 $self->route($field[2], $line);
334 if ($pcno == 16) { # add a user
335 my $node = DXCluster->get_exact($field[1]);
337 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
338 # add it to the node table if it isn't present and it's
340 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
341 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
344 return unless $node; # ignore if havn't seen a PC19 for this one yet
345 return unless $node->isa('DXNode');
346 if ($node->dxchan != $self) {
347 dbg('chan', "LOOP: $field[1] came in on wrong channel");
350 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
351 dbg('chan', "LOOP: $field[1] connected locally");
356 for ($i = 2; $i < $#field; $i++) {
357 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
358 next if !$call || length $call < 3 || length $call > 8;
361 next if DXCluster->get_exact($call); # we already have this (loop?)
363 $confmode = $confmode eq '*';
364 DXNodeuser->new($self, $node, $call, $confmode, $here);
366 # add this station to the user database, if required
367 $call =~ s/-\d+$//o; # remove ssid for users
368 my $user = DXUser->get_current($call);
369 $user = DXUser->new($call) if !$user;
370 $user->homenode($node->call) if !$user->homenode;
371 $user->node($node->call);
372 $user->lastin($main::systime) unless DXChannel->get($call);
376 # queue up any messages (look for privates only)
377 DXMsg::queue_msg(1) if $self->state eq 'normal';
381 if ($pcno == 17) { # remove a user
382 my $node = DXCluster->get_exact($field[2]);
384 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
385 # add it to the node table if it isn't present and it's
387 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
388 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
392 return unless $node->isa('DXNode');
393 if ($node->dxchan != $self) {
394 dbg('chan', "LOOP: $field[2] came in on wrong channel");
397 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
398 dbg('chan', "LOOP: $field[2] connected locally");
401 my $ref = DXCluster->get_exact($field[1]);
406 if ($pcno == 18) { # link request
407 $self->state('init');
409 # first clear out any nodes on this dxchannel
410 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
411 foreach my $node (@gonenodes) {
412 next if $node->dxchan == $DXProt::me;
413 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
416 $self->send_local_config();
418 return; # we don't pass these on
421 if ($pcno == 19) { # incoming cluster list
423 my $newline = "PC19^";
424 for ($i = 1; $i < $#field-1; $i += 4) {
425 my $here = $field[$i];
426 my $call = uc $field[$i+1];
427 my $confmode = $field[$i+2];
428 my $ver = $field[$i+3];
430 $ver = 5400 if !$ver && $allowzero;
432 # now check the call over
433 my $node = DXCluster->get_exact($call);
436 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
437 dbg('chan', "LOOP: $call connected locally");
439 if ($node->dxchan != $self) {
440 dbg('chan', "LOOP: $call come in on wrong channel");
443 dbg('chan', "already have $call");
447 # check for sane parameters
448 next if $ver < 5000; # only works with version 5 software
449 next if length $call < 3; # min 3 letter callsigns
451 # add it to the nodes table and outgoing line
452 $newline .= "$here^$call^$confmode^$ver^";
453 DXNode->new($self, $call, $confmode, $here, $ver);
455 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
456 my $mref = DXMsg::get_busy($call);
457 $mref->stop_msg($call) if $mref;
459 # add this station to the user database, if required (don't remove SSID from nodes)
460 my $user = DXUser->get_current($call);
462 $user = DXUser->new($call);
464 $user->priv(1); # I have relented and defaulted nodes
465 $self->{priv} = 1; # to user RCMDs allowed
466 $user->homenode($call);
469 $user->lastin($main::systime) unless DXChannel->get($call);
473 return if $newline eq "PC19^";
476 $newline .= get_hops(19) . "^";
481 if ($pcno == 20) { # send local configuration
482 $self->send_local_config();
484 $self->state('normal');
488 if ($pcno == 21) { # delete a cluster from the list
489 my $call = uc $field[1];
490 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
491 my $node = DXCluster->get_exact($call);
493 if ($node->dxchan != $self) {
494 dbg('chan', "LOOP: $call come in on wrong channel");
498 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
499 dbg('chan', "LOOP: $call connected locally");
504 dbg('chan', "$call not in table, dropped");
512 $self->state('normal');
516 if ($pcno == 23 || $pcno == 27) { # WWV info
518 # route 'foreign' pc27s
520 if ($field[8] ne $main::mycall) {
521 $self->route($field[8], $line);
527 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
528 my $sfi = unpad($field[3]);
529 my $k = unpad($field[4]);
530 my $i = unpad($field[5]);
531 my ($r) = $field[6] =~ /R=(\d+)/;
533 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
534 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
537 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
538 dbg('chan', "Dup WWV Spot ignored\n");
541 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
543 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
547 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
549 # dbg('local', "Local::wwv2 error $@") if $@;
552 # DON'T be silly and send on PC27s!
553 return if $pcno == 27;
555 # broadcast to the eager world
556 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
560 if ($pcno == 24) { # set here status
561 my $call = uc $field[1];
562 my $ref = DXCluster->get_exact($call);
563 $ref->here($field[2]) if $ref;
567 if ($pcno == 25) { # merge request
568 if ($field[1] ne $main::mycall) {
569 $self->route($field[1], $line);
572 if ($field[2] eq $main::mycall) {
573 dbg('chan', "Trying to merge to myself, ignored");
577 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
581 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
584 $self->send(pc26(@{$in}[0..4], $field[2]));
590 my @in = reverse Geomag::search(0, $field[4], time, 1);
593 $self->send(pc27(@{$in}[0..5], $field[2]));
599 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
600 if ($pcno == 49 || $field[1] eq $main::mycall) {
601 DXMsg::process($self, $line);
603 $self->route($field[1], $line);
608 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
609 if ($field[1] eq $main::mycall) {
610 my $ref = DXUser->get_current($field[2]);
611 my $cref = DXCluster->get($field[2]);
612 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
613 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
614 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
615 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
616 my $oldpriv = $self->{priv};
617 $self->{priv} = $ref->{priv}; # assume the user's privilege level
618 my @in = (DXCommandmode::run_cmd($self, $field[3]));
619 $self->{priv} = $oldpriv;
622 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
623 Log('rcmd', 'out', $field[2], $_);
625 delete $self->{remotecmd};
627 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
630 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
633 my $ref = DXUser->get_current($field[1]);
634 if ($ref && $ref->is_clx) {
635 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
637 $self->route($field[1], $line);
643 if ($pcno == 35) { # remote command replies
644 if ($field[1] eq $main::mycall) {
645 my $s = $rcmds{$field[2]};
647 my $dxchan = DXChannel->get($s->{call});
648 $dxchan->send($field[3]) if $dxchan;
649 delete $rcmds{$field[2]} if !$dxchan;
652 my $ref = DXUser->get_current($field[1]);
653 if ($ref && $ref->is_clx) {
654 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
656 $self->route($field[1], $line);
662 # for pc 37 see 44 onwards
664 if ($pcno == 38) { # node connected list from neighbour
668 if ($pcno == 39) { # incoming disconnect
669 $self->disconnect(1);
673 if ($pcno == 41) { # user info
674 # add this station to the user database, if required
675 my $user = DXUser->get_current($field[1]);
677 # then try without an SSID
678 $field[1] =~ s/-\d+$//o;
679 $user = DXUser->get_current($field[1]);
681 $user = DXUser->new($field[1]) if !$user;
683 if ($field[2] == 1) {
684 $user->name($field[3]);
685 } elsif ($field[2] == 2) {
686 $user->qth($field[3]);
687 } elsif ($field[2] == 3) {
688 my ($lat, $long) = DXBearing::stoll($field[3]);
691 my $qra = $user->qra || DXBearing::lltoqra($lat, $long);
692 $qra = DXBearing::lltoqra($lat, $long) unless $qra && DXBearing::is_qra($qra);
693 $user->qra($qra) if $qra ne $user->qra;
694 } elsif ($field[2] == 4) {
695 $user->homenode($field[3]);
703 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
704 DXDb::process($self, $line);
708 if ($pcno == 50) { # keep alive/user list
709 my $node = DXCluster->get_exact($field[1]);
711 return unless $node->isa('DXNode');
712 return unless $node->dxchan == $self;
713 $node->update_users($field[2]);
718 if ($pcno == 51) { # incoming ping requests/answers
721 if ($field[1] eq $main::mycall) {
722 my $flag = $field[3];
724 $self->send(pc51($field[2], $field[1], '0'));
726 # it's a reply, look in the ping list for this one
727 my $ref = $pings{$field[2]};
729 my $tochan = DXChannel->get($field[2]);
732 my $dxchan = DXChannel->get($r->{call});
734 my $t = tv_interval($r->{t}, [ gettimeofday ]);
735 if ($dxchan->is_user) {
736 my $s = sprintf "%.2f", $t;
737 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
738 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
739 } elsif ($dxchan->is_node) {
741 $tochan->{nopings} = 2; # pump up the timer
742 push @{$tochan->{pingtime}}, $t;
743 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
745 for (@{$tochan->{pingtime}}) {
748 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
755 # route down an appropriate thingy
756 $self->route($field[1], $line);
761 if ($pcno == 73) { # WCY broadcasts
764 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
765 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
766 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
769 @field = map { unpad($_) } @field;
770 if (WCY::dup($d,@field[3..7])) {
771 dbg('chan', "Dup WCY Spot ignored\n");
775 my $wcy = WCY::update($d, @field[2..12]);
779 $rep = Local::wwv($self, @field[1..12]);
781 # dbg('local', "Local::wcy error $@") if $@;
784 # broadcast to the eager world
785 send_wcy_spot($self, $line, $d, @field[2..12]);
789 if ($pcno == 84) { # remote commands (incoming)
790 if ($field[1] eq $main::mycall) {
791 my $ref = DXUser->get_current($field[2]);
792 my $cref = DXCluster->get($field[2]);
793 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
794 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
795 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
796 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
797 my $oldpriv = $self->{priv};
798 $self->{priv} = $ref->{priv}; # assume the user's privilege level
799 my @in = (DXCommandmode::run_cmd($self, $field[4]));
800 $self->{priv} = $oldpriv;
803 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
804 Log('rcmd', 'out', $field[2], $_);
806 delete $self->{remotecmd};
808 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
811 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
814 my $ref = DXUser->get_current($field[1]);
815 if ($ref && $ref->is_clx) {
816 $self->route($field[1], $line);
818 route($field[1], pc34($field[2], $field[1], $field[3]));
824 if ($pcno == 85) { # remote command replies
825 if ($field[1] eq $main::mycall) {
826 my $dxchan = DXChannel->get($field[3]);
828 $dxchan->send($field[4]);
830 my $s = $rcmds{$field[2]};
832 $dxchan = DXChannel->get($s->{call});
833 $dxchan->send($field[4]) if $dxchan;
834 delete $rcmds{$field[2]} if !$dxchan;
838 my $ref = DXUser->get_current($field[1]);
839 if ($ref && $ref->is_clx) {
840 $self->route($field[1], $line);
842 route($field[1], pc35($field[2], $field[1], $field[3]));
849 # if get here then rebroadcast the thing with its Hop count decremented (if
850 # there is one). If it has a hop count and it decrements to zero then don't
853 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
857 unless ($self->{isolate}) {
858 broadcast_ak1a($line, $self); # send it to everyone but me
863 # This is called from inside the main cluster processing loop and is used
864 # for despatching commands that are doing some long processing job
869 my @dxchan = DXChannel->get_all();
872 foreach $dxchan (@dxchan) {
873 next unless $dxchan->is_node();
874 next if $dxchan == $me;
876 # send a pc50 out on this channel
877 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
878 $dxchan->send(pc50(scalar DXChannel::get_all_users));
882 # send a ping out on this channel
883 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
884 if ($dxchan->{nopings} <= 0) {
887 addping($main::mycall, $dxchan->call);
888 $dxchan->{nopings} -= 1;
889 $dxchan->{lastping} = $t;
897 if ($main::systime - 3600 > $last_hour) {
901 $last_hour = $main::systime;
906 # finish up a pc context
911 my $call = $self->call;
913 my $ref = DXCluster->get_exact($call);
915 # unbusy and stop and outgoing mail
916 my $mref = DXMsg::get_busy($call);
917 $mref->stop_msg($call) if $mref;
919 # broadcast to all other nodes that all the nodes connected to via me are gone
920 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
923 foreach $node (@gonenodes) {
924 next if $node->call eq $call;
925 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
929 # remove outstanding pings
930 delete $pings{$call};
932 # now broadcast to all other ak1a nodes that I have gone
933 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
935 # I was the last node visited
936 $self->user->node($main::mycall);
938 # send info to all logged in thingies
939 $self->tell_login('logoutn');
941 Log('DXProt', $call . " Disconnected");
946 # some active measures
952 my @dxchan = DXChannel->get_all();
955 # send it if it isn't the except list and isn't isolated and still has a hop count
956 # taking into account filtering and so on
957 foreach $dxchan (@dxchan) {
961 if ($dxchan->{spotfilter}) {
962 ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
966 if ($dxchan->is_node) {
967 next if $dxchan == $self;
970 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
972 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
973 next unless $routeit;
976 $dxchan->send($routeit) if $routeit;
978 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
980 } elsif ($dxchan->is_user && $dxchan->{dx}) {
981 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
982 $buf .= "\a\a" if $dxchan->{beep};
983 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
986 $dxchan->delay($buf);
996 my @dxchan = DXChannel->get_all();
999 # send it if it isn't the except list and isn't isolated and still has a hop count
1000 # taking into account filtering and so on
1001 foreach $dxchan (@dxchan) {
1003 my ($filter, $hops);
1005 if ($dxchan->{wwvfilter}) {
1006 ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1007 next unless $filter;
1009 if ($dxchan->is_node) {
1010 next if $dxchan == $self;
1013 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1015 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1016 next unless $routeit;
1019 $dxchan->send($routeit) if $routeit;
1021 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1024 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1025 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1026 $buf .= "\a\a" if $dxchan->{beep};
1027 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1028 $dxchan->send($buf);
1030 $dxchan->delay($buf);
1040 my @dxchan = DXChannel->get_all();
1043 # send it if it isn't the except list and isn't isolated and still has a hop count
1044 # taking into account filtering and so on
1045 foreach $dxchan (@dxchan) {
1047 my ($filter, $hops);
1049 if ($dxchan->{wcyfilter}) {
1050 ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1051 next unless $filter;
1053 if ($dxchan->is_clx || $dxchan->is_spider) {
1054 next if $dxchan == $self;
1057 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1059 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1060 next unless $routeit;
1063 $dxchan->send($routeit) if $routeit;
1065 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1067 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1068 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1069 $buf .= "\a\a" if $dxchan->{beep};
1070 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1071 $dxchan->send($buf);
1073 $dxchan->delay($buf);
1084 my @dxchan = DXChannel->get_all();
1086 my $text = unpad($_[2]);
1090 if ($_[3] eq '*') { # sysops
1092 } elsif ($_[3] gt ' ') { # speciality list handling
1093 my ($name) = split /\./, $_[3];
1094 $target = "$name"; # put the rest in later (if bothered)
1101 $target = "All" if !$target;
1103 Log('ann', $target, $_[0], $text);
1105 # send it if it isn't the except list and isn't isolated and still has a hop count
1106 # taking into account filtering and so on
1107 foreach $dxchan (@dxchan) {
1109 my ($filter, $hops);
1111 if ($dxchan->{annfilter}) {
1112 ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1113 next unless $filter;
1115 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1116 next if $dxchan == $self;
1119 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1121 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1122 next unless $routeit;
1125 $dxchan->send($routeit) if $routeit;
1127 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1130 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1131 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1132 my $buf = "$to$target de $_[0]: $text";
1133 $buf .= "\a\a" if $dxchan->{beep};
1134 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1135 $dxchan->send($buf);
1137 $dxchan->delay($buf);
1143 sub send_local_config
1152 if ($self->{isolate}) {
1153 @localnodes = (DXCluster->get_exact($main::mycall));
1155 # create a list of all the nodes that are not connected to this connection
1156 # and are not themselves isolated, this to make sure that isolated nodes
1157 # don't appear outside of this node
1158 @nodes = DXNode::get_all();
1159 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1160 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1161 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1162 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1163 unshift @localnodes, DXCluster->get_exact($main::mycall);
1164 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1167 my @s = $me->pc19(@localnodes, @remotenodes);
1169 my $routeit = adjust_hops($self, $_);
1170 $self->send($routeit) if $routeit;
1173 # get all the users connected on the above nodes and send them out
1174 foreach $n (@localnodes, @remotenodes) {
1175 my @users = values %{$n->list};
1176 my @s = pc16($n, @users);
1178 my $routeit = adjust_hops($self, $_);
1179 $self->send($routeit) if $routeit;
1185 # route a message down an appropriate interface for a callsign
1187 # is called route(to, pcline);
1191 my ($self, $call, $line) = @_;
1192 my $cl = DXCluster->get_exact($call);
1193 if ($cl) { # don't route it back down itself
1194 if (ref $self && $call eq $self->{call}) {
1195 dbg('chan', "Trying to route back to source, dropped");
1199 my $dxchan = $cl->{dxchan};
1201 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1203 $dxchan->send($routeit) if $dxchan;
1209 # broadcast a message to all clusters taking into account isolation
1210 # [except those mentioned after buffer]
1213 my $s = shift; # the line to be rebroadcast
1214 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1215 my @dxchan = DXChannel::get_all_ak1a();
1218 # send it if it isn't the except list and isn't isolated and still has a hop count
1219 foreach $dxchan (@dxchan) {
1220 next if grep $dxchan == $_, @except;
1221 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1222 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1226 # broadcast a message to all clusters ignoring isolation
1227 # [except those mentioned after buffer]
1228 sub broadcast_all_ak1a
1230 my $s = shift; # the line to be rebroadcast
1231 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1232 my @dxchan = DXChannel::get_all_ak1a();
1235 # send it if it isn't the except list and isn't isolated and still has a hop count
1236 foreach $dxchan (@dxchan) {
1237 next if grep $dxchan == $_, @except;
1238 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1239 $dxchan->send($routeit);
1243 # broadcast to all users
1244 # storing the spot or whatever until it is in a state to receive it
1247 my $s = shift; # the line to be rebroadcast
1248 my $sort = shift; # the type of transmission
1249 my $fref = shift; # a reference to an object to filter on
1250 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1251 my @dxchan = DXChannel::get_all_users();
1255 foreach $dxchan (@dxchan) {
1256 next if grep $dxchan == $_, @except;
1259 broadcast_list($s, $sort, $fref, @out);
1262 # broadcast to a list of users
1270 foreach $dxchan (@_) {
1273 if ($sort eq 'dx') {
1274 next unless $dxchan->{dx};
1275 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1276 next unless $filter;
1278 next if $sort eq 'ann' && !$dxchan->{ann};
1279 next if $sort eq 'wwv' && !$dxchan->{wwv};
1280 next if $sort eq 'wcy' && !$dxchan->{wcy};
1281 next if $sort eq 'wx' && !$dxchan->{wx};
1283 $s =~ s/\a//og unless $dxchan->{beep};
1285 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1295 # obtain the hops from the list for this callsign and pc no
1301 my $hops = $DXProt::hopcount{$pcno};
1302 $hops = $DXProt::def_hopcount if !$hops;
1307 # adjust the hop count on a per node basis using the user loadable
1308 # hop table if available or else decrement an existing one
1315 my $call = $self->{call};
1318 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1319 my ($pcno) = $s =~ /^PC(\d\d)/o;
1320 confess "$call called adjust_hops with '$s'" unless $pcno;
1321 my $ref = $nodehops{$call} if %nodehops;
1323 my $newhops = $ref->{$pcno};
1324 return "" if defined $newhops && $newhops == 0;
1325 $newhops = $ref->{default} unless $newhops;
1326 return "" if defined $newhops && $newhops == 0;
1327 $newhops = $hops if !$newhops;
1328 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1330 # simply decrement it
1332 return "" if !$hops;
1333 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1345 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1346 do "$main::data/hop_table.pl";
1352 # add a ping request to the ping queues
1355 my ($from, $to) = @_;
1356 my $ref = $pings{$to} || [];
1359 $r->{t} = [ gettimeofday ];
1360 route(undef, $to, pc51($to, $main::mycall, 1));
1365 # add a rcmd request to the rcmd queues
1368 my ($self, $to, $cmd) = @_;
1371 $r->{call} = $self->{call};
1372 $r->{t} = $main::systime;
1376 my $ref = DXCluster->get_exact($to);
1377 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1378 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1380 route(undef, $to, pc34($main::mycall, $to, $cmd));
1389 if ($self->{conn} && !$nopc39) {
1390 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1393 $self->SUPER::disconnect;