3 # This module impliments the handlers for the protocal mode for a dx cluster
5 # Copyright (c) 1998-2007 Dirk Koopman G1TLH
40 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
41 $last_hour $last10 %eph %pings %rcmds $ann_to_talk
42 $pingint $obscount %pc19list $chatdupeage $chatimportfn
43 $investigation_int $pc19_version $myprot_version
44 %nodehops $baddx $badspotter $badnode $censorpc
45 $allowzero $decode_dk0wcy $send_opernam @checklist
46 $eph_pc15_restime $pc9x_past_age $pc9x_dupe_age
47 $pc10_dupe_age $pc92_slug_changes $last_pc92_slug
48 $pc92Ain $pc92Cin $pc92Din $pc92Kin $pc9x_time_tolerance
49 $pc92filterdef $senderverify
52 $pc9x_dupe_age = 60; # catch loops of circular (usually) D records
53 $pc10_dupe_age = 45; # just something to catch duplicate PC10->PC93 conversions
54 $pc92_slug_changes = 60*1; # slug any changes going outward for this long
55 $last_pc92_slug = 0; # the last time we sent out any delayed add or del PC92s
56 $pc9x_time_tolerance = 15*60; # the time on a pc9x is allowed to be out by this amount
57 $pc9x_past_age = (122*60)+ # maximum age in the past of a px9x (a config record might be the only
58 $pc9x_time_tolerance; # thing a node might send - once an hour and we allow an extra hour for luck)
59 # this is actually the partition between "yesterday" and "today" but old.
60 $senderverify = 0; # 1 - check for forged PC11 or PC61.
61 # 2 - if forged, dump them.
64 $pc92filterdef = bless ([
65 # tag, sort, field, priv, special parser
74 # this is a place to park an incoming PC11 in the sure and certain hope that
75 # a PC61 will be along soon. This has the side benefit that it will delay a
76 # a PC11 for one second - assuming that it is not removed by a PC61 version
78 # incoming talk commands
87 # this is to catch loops caused by bad software ...
88 if (eph_dup($line, $pc10_dupe_age)) {
93 # is it for me or one of mine?
94 my ($from, $to, $via, $call, $dxchan);
96 if ($pc->[5] gt ' ') {
103 # if this is a 'nodx' node then ignore it
104 if ($badnode->in($pc->[6]) || ($via && $badnode->in($via))) {
105 dbg($line) if isdbg('nologchan');
106 dbg("PCPROT: Bad Node, dropped");
110 # if this is a 'bad spotter' user then ignore it
112 $nossid =~ s/-\d+$//;
113 if ($badspotter->in($nossid)) {
114 dbg($line) if isdbg('nologchan');
115 dbg("PCPROT: Bad Spotter, dropped");
119 # if we are converting announces to talk is it a dup?
121 if (AnnTalk::is_talk_candidate($from, $pc->[3]) && AnnTalk::dup($from, $to, $pc->[3])) {
122 dbg("PCPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
127 # will we allow it at all?
130 if (@bad = BadWords::check($pc->[3])) {
131 my $bw = join ', ', @bad;
132 dbg($line) if isdbg('nologchan');
133 dbg("PCPROT: Badwords: '$bw', dropped");
138 # convert this to a PC93, coming from mycall with origin set and process it as such
139 $main::me->normal(pc93($to, $from, $via, $pc->[3], $pc->[6]));
154 my $recurse = shift || 0;
156 # route 'foreign' pc26s
158 if ($pc->[7] ne $main::mycall) {
159 $self->route($pc->[7], $line);
164 dbg("INPUT PC$pcno $line origin $origin recurse: $recurse") if isdbg("pc11");
166 # my ($hops) = $pc->[8] =~ /^H(\d+)/;
168 # is the spotted callsign blank? This should really be trapped earlier but it
169 # could break other protocol sentences. Also check for lower case characters.
170 if ($pc->[2] =~ /^\s*$/) {
171 dbg($line) if isdbg('nologchan');
172 dbg("PCPROT: blank callsign, dropped");
175 if ($pc->[2] =~ /[a-z]/) {
176 dbg($line) if isdbg('nologchan');
177 dbg("PCPROT: lowercase characters, dropped");
182 # if this is a 'nodx' node then ignore it
183 if ($badnode->in($pc->[7])) {
184 dbg($line) if isdbg('nologchan');
185 dbg("PCPROT: Bad Node $pc->[7], dropped");
189 # if this is a 'bad spotter' or an unknown user then ignore it. BUT if it's got an IP address then allow it through
190 my $nossid = $pc->[6];
191 $nossid =~ s/-\d+$//;
192 if ($badspotter->in($nossid)) {
193 dbg($line) if isdbg('nologchan');
194 dbg("PCPROT: Bad Spotter $pc->[6], dropped");
199 if ($pc->[8] && is_ipaddr($pc->[8])) {
203 if (DXCIDR::find($ip)) {
208 # convert the date to a unix date
209 my $d = cltounix($pc->[3], $pc->[4]);
210 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
211 if (!$d || (($pcno == 11 || $pcno == 61) && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
212 dbg("PCPROT: Spot ignored, invalid date or out of range ($pc->[3] $pc->[4])\n");
217 if ($baddx->in($pc->[2])) {
218 dbg("PCPROT: Bad DX spot '$pc->[2]', ignored");
223 $pc->[5] =~ s/^\s+//; # take any leading blanks off
224 $pc->[2] = unpad($pc->[2]); # take off leading and trailing blanks from spotted callsign
225 if ($pc->[2] =~ /BUST\w*$/) {
226 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
231 my @spot = Spot::prepare($pc->[1], $pc->[2], $d, $pc->[5], $nossid, $pc->[7], $pc->[8]);
233 # global spot filtering on INPUT
234 if ($self->{inspotsfilter}) {
235 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
237 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
242 # this is where we decide to delay PC11s in the hope that a PC61 will be along soon.
244 my $key = join '|', @spot[0..2,4,7]; # not including text
248 if ($key eq $pc11_saved->[0]) {
249 dbg("saved PC11 spot $key dumped, better pc61 received") if isdbg("pc11");
256 if ($key eq $pc11_saved->[0]) {
257 dbg("saved PC11 spot $key, dupe pc11 received and dumped") if isdbg("pc11");
258 return; # because it's a dup
262 # can we promote this to a PC61?
263 my $r = Route::User::get($spot[4]); # find spotter
264 if ($r && $r->ip) { # do we have an ip addres
265 $pcno = 61; # now turn this into a PC61
267 dbg("PC11 spot $key promoted to pc61 ip $spot[14]") if isdbg("pc11");
272 if ($pc11_saved && $key ne $pc11_saved) {
273 dbg("saved PC11 spot $pc11_saved->[0] ne new key $key, recursing") if isdbg("pc11");
274 shift @$pc11_saved; # saved key
275 my $self = shift @$pc11_saved;
276 my @saved = @$pc11_saved;
278 $self->handle_11(@saved, 1);
281 # if we are still a PC11, save it for a better offer
283 $pc11_saved = [$key, $self, $pcno, $line, $origin, $pc];
284 $pc11_saved_time = $main::systime;
285 dbg("saved new PC11 spot $key for a better offer") if isdbg("pc11");
288 dbg("PC61 spot $key passed onward") if isdbg("pc11");
293 # this goes after the input filtering, but before the add
294 # so that if it is input filtered, it isn't added to the dup
295 # list. This allows it to come in from a "legitimate" source
296 if (Spot::dup(@spot[0..4,7])) {
297 dbg("PCPROT: Duplicate Spot $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot');
301 # here we verify the spotter is currently connected to the node it says it is one. AKA email sender verify
302 # but without the explicit probe to the node. We are relying on "historical" information, but it very likely
303 # to be current once we have seen the first PC92C from that node.
305 # As for spots generated from non-PC92 nodes, we'll see after about do_pc9x3h20m...
308 my $nroute = Route::Node::get($pc->[7]);
309 my $uroute = Route::Node::get($pc->[6]);
310 my $local = DXChannel::get($pc->[7]);
312 if ($nroute && ($nroute->last_PC92C || ($local && !$local->do_pc9x))) {
314 my $ip = $pcno == 61 ? $pc->[8] : '';
315 # $s .= "User $pc->[6] not logged in, " unless $uroute;
316 $s .= "User $pc->[6] not on node $pc->[7], " unless $nroute->is_user($pc->[6]);
317 # $s .= "Node $pc->[7] at '$ip' not on Node's IP " . $nroute->ip if $ip && $nroute && $nroute->ip && $nroute->ip ne $ip;
319 my $action = $senderverify > 1 ? ", DUMPED" : '';
321 dbg("PCProt: Suspicious Spot $pc->[2] on $pc->[1] by $pc->[6]($ip)\@$pc->[7] $s$action");
322 return unless $senderverify < 2;
327 # we until here to do any censorship to try and reduce the amount of noise that repeated copies
328 # from other connected nodes cause
331 if (@bad = BadWords::check($pc->[5])) {
332 my $bw = join ', ', @bad;
333 dbg($line) if isdbg('nologchan');
334 dbg("PCPROT: Badwords: '$bw', dropped");
339 # If is a new PC11, store it, releasing the one that is there (if any),
340 # if a PC61 comes along then dump the stored PC11
341 # If there is a different PC11 stored, release that one and store this PC11 instead,
347 $ip ||= $spot[14] if exists $spot[14];
348 if (isdbg('progress')) {
349 my $sip = $ip ? sprintf "($ip)" : '' unless $ip =~ m|[\(\)\*]|;
351 my $d = ztime($spot[2]);
352 my $s = "SPOT: $spot[1] on $spot[0] \@ $d by $spot[4]$sip\@$spot[7]";
353 $s .= $spot[3] ? " '$spot[3]'" : q{ ''};
354 $s .= " route: $origin";
359 # @spot at this point contains:-
360 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
361 # then spotted itu, spotted cq, spotters itu, spotters cq
362 # you should be able to route on any of these
365 # fix up qra locators of known users
366 my $user = DXUser::get_current($spot[4]);
368 my $qra = $user->qra;
369 unless ($qra && is_qra($qra)) {
370 my $lat = $user->lat;
371 my $long = $user->long;
372 if (defined $lat && defined $long) {
373 $user->qra(DXBearing::lltoqra($lat, $long));
378 # send a remote command to a distant cluster if it is visible and there is no
379 # qra locator and we havn't done it for a month.
381 unless ($user->qra) {
383 my $to = $user->homenode;
384 my $last = $user->lastoper || 0;
385 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
386 my $cmd = "forward/opernam $spot[4]";
387 # send the rcmd but we aren't interested in the replies...
388 my $dxchan = $node->dxchan;
389 if ($dxchan && $dxchan->is_clx) {
390 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
392 route(undef, $to, pc34($main::mycall, $to, $cmd));
394 if ($to ne $origin) {
396 $node = Route::Node::get($to);
398 $dxchan = $node->dxchan;
399 if ($dxchan && $dxchan->is_clx) {
400 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
402 route(undef, $to, pc34($main::mycall, $to, $cmd));
406 $user->lastoper($main::systime);
413 if (defined &Local::spot) {
416 $r = Local::spot($self, @spot);
421 # DON'T be silly and send on PC26s!
422 return if $pcno == 26;
424 # send out the filtered spots
425 send_dx_spot($self, $line, @spot) if @spot;
428 # used to kick outstanding PC11 if required
431 if ($pc11_saved && $main::systime > $pc11_saved_time) {
432 dbg("saved PC11 spot $pc11_saved->[0] timed out waiting, recursing") if isdbg("pc11");
433 shift @$pc11_saved; # saved key
434 my $self = shift @$pc11_saved;
435 my @saved = @$pc11_saved;
437 $self->handle_11(@saved, 1);
450 # announce duplicate checking
451 $pc->[3] =~ s/^\s+//; # remove leading blanks
453 # if this is a 'nodx' node then ignore it
454 if ($badnode->in($pc->[5])) {
455 dbg($line) if isdbg('nologchan');
456 dbg("PCPROT: Bad Node, dropped");
460 # if this is a 'bad spotter' user then ignore it
461 my $nossid = $pc->[1];
462 $nossid =~ s/-\d+$//;
463 if ($badspotter->in($nossid)) {
464 dbg($line) if isdbg('nologchan');
465 dbg("PCPROT: Bad Spotter, dropped");
469 # ignore PC12s from origins that use PCxx protocol
470 my $oref = Route::get($origin);
471 if ($oref->do_pc9x) {
472 dbg("PCPROT: PC12 rxed from PC9x node, ignored") if isdbg('chanerr');
478 if (@bad = BadWords::check($pc->[3])) {
479 my $bw = join ', ', @bad;
480 dbg($line) if isdbg('nologchan');
481 dbg("PCPROT: Badwords: '$bw', dropped");
489 if ((($dxchan = DXChannel::get($pc->[2])) && $dxchan->is_user) || $pc->[4] =~ /^[\#\w.]+$/){
490 $self->send_chat(0, $line, @$pc[1..6]);
491 } elsif ($pc->[2] eq '*' || $pc->[2] eq $main::mycall) {
493 # ignore something that looks like a chat line coming in with sysop
494 # flag - this is a kludge...
495 if ($pc->[3] =~ /^\#\d+ / && $pc->[4] eq '*') {
496 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
500 # here's a bit of fun, convert incoming ann with a callsign in the first word
501 # or one saying 'to <call>' to a talk if we can route to the recipient
503 my $call = AnnTalk::is_talk_candidate($pc->[1], $pc->[3]);
505 my $ref = Route::get($call);
507 $dxchan = $ref->dxchan;
508 $dxchan->talk($pc->[1], $call, undef, $pc->[3], $pc->[5]) if $dxchan != $self;
515 $self->send_announce(0, $line, @$pc[1..6]);
517 $self->route($pc->[2], $line);
521 if (defined &Local::ann) {
524 $r = Local::ann($self, $line, @$pc[1..6]);
538 if (eph_dup($line, $eph_pc15_restime)) {
541 unless ($self->{isolate}) {
542 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
558 my $ncall = $pc->[1];
559 my $newline = "PC16^";
561 # dos I want users from this channel?
562 unless ($self->user->wantpc16) {
563 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
568 if ($ncall eq $main::mycall) {
569 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
574 $h = 1 if DXChannel::get($ncall);
575 if ($h && $self->{call} ne $ncall) {
576 dbg("PCPROT: trying to update a local node, ignored") if isdbg('chanerr');
580 if (eph_dup($line)) {
584 # isolate now means only accept stuff from this call only
585 if ($self->{isolate} && $ncall ne $self->{call}) {
586 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
590 my $parent = Route::Node::get($ncall);
593 $dxchan = $parent->dxchan;
594 if ($dxchan && $dxchan ne $self) {
595 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
599 # input filter if required
600 return unless $self->in_filter_route($parent);
602 $parent = Route::Node->new($ncall);
606 if ($parent->via_pc92) {
607 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
614 for ($i = 2; $i < $#$pc; $i++) {
615 my ($call, $conf, $here) = $pc->[$i] =~ /^(\S+) (\S) (\d)/o;
616 next unless $call && $conf && defined $here && is_callsign($call);
617 next if $call eq $main::mycall;
619 eph_del_regex("^PC17\\^$call\\^$ncall");
621 $conf = $conf eq '*';
623 # reject this if we think it is a node already
624 my $r = Route::Node::get($call);
625 my $u = DXUser::get_current($call) unless $r;
626 if ($r || ($u && $u->is_node)) {
627 dbg("PCPROT: $call is a node") if isdbg('chanerr');
631 $r = Route::User::get($call);
632 my $flags = Route::here($here)|Route::conf($conf);
635 my $au = $r->addparent($parent);
636 if ($r->flags != $flags) {
640 push @rout, $r if $h && $au;
642 my @ans = $parent->add_user($call, $flags);
643 push @rout, @ans if $h && @ans;
646 # add this station to the user database, if required
647 my $user = DXUser::get_current($ncall);
648 $user = DXUser->new($call) unless $user;
649 $user->homenode($parent->call) if !$user->homenode;
650 $user->node($parent->call);
651 $user->lastin($main::systime) unless DXChannel::get($call);
654 # send info to all logged in thingies
655 $self->tell_login('loginu', "$ncall: $call") if $user->is_local_node;
656 $self->tell_buddies('loginb', $call, $ncall);
659 $self->route_pc16($origin, $line, $parent, @rout) if @rout;
660 # $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
674 my $ncall = $pc->[2];
675 my $ucall = $pc->[1];
677 eph_del_regex("^PC16\\^$ncall.*$ucall");
679 # do I want users from this channel?
680 unless ($self->user->wantpc16) {
681 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
685 if ($ncall eq $main::mycall) {
686 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
690 # isolate now means only accept stuff from this call only
691 if ($self->{isolate} && $ncall ne $self->{call}) {
692 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
696 my $uref = Route::User::get($ucall);
698 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
701 my $parent = Route::Node::get($ncall);
703 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
707 $dxchan = DXChannel::get($ncall);
708 if ($dxchan && $dxchan ne $self) {
709 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
714 if ($parent->via_pc92) {
715 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
720 if (DXChannel::get($ucall)) {
721 dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
725 # input filter if required and then remove user if present
726 # return unless $self->in_filter_route($parent);
727 $parent->del_user($uref);
729 # send info to all logged in thingies
730 my $user = DXUser::get_current($ncall);
731 $self->tell_login('logoutu', "$ncall: $ucall") if $user && $user->is_local_node;
732 $self->tell_buddies('logoutb', $ucall, $ncall);
734 if (eph_dup($line)) {
738 $self->route_pc17($origin, $line, $parent, $uref);
739 # $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
751 $self->state('init');
753 my $parent = Route::Node::get($self->{call});
755 # record the type and version offered
756 if (my ($version) = $pc->[1] =~ /DXSpider Version: (\d+\.\d+)/) {
757 $self->{version} = 53 + $version;
758 $self->user->version(53 + $version);
759 $parent->version(0 + $version);
760 my ($build) = $pc->[1] =~ /Build: (\d+(?:\.\d+)?)/;
761 $self->{build} = 0 + $build;
762 $self->user->build(0 + $build);
763 $parent->build(0 + $build);
764 dbg("$self->{call} = DXSpider version $version build $build");
765 unless ($self->is_spider) {
766 dbg("Change U " . $self->user->sort . " C $self->{sort} -> S");
767 $self->user->sort('S');
771 # $self->{handle_xml}++ if DXXml::available() && $pc->[1] =~ /\bxml/;
773 dbg("$self->{call} = Unknown software ($pc->[1] $pc->[2])");
774 $self->version(50.0);
775 $self->version($pc->[2] / 100) if $pc->[2] && $pc->[2] =~ /^\d+$/;
776 $self->user->version($self->version);
779 if ($pc->[1] =~ /\bpc9x/) {
780 if ($self->{isolate}) {
781 dbg("$self->{call} pc9x recognised, but node is isolated, using old protocol");
782 } elsif (!$self->user->wantpc9x) {
783 dbg("$self->{call} pc9x explicitly switched off, using old protocol");
785 $self->{do_pc9x} = 1;
786 dbg("$self->{call} Set do PC9x");
790 # first clear out any nodes on this dxchannel
791 my @rout = $parent->del_nodes;
792 $self->route_pc21($origin, $line, @rout, $parent) if @rout;
793 $self->send_local_config();
801 my $homenode = shift;
803 # add this station to the user database, if required (don't remove SSID from nodes)
804 my $user = DXUser::get_current($call);
806 $user = DXUser->new($call);
807 $user->sort($type || 'U');
808 if ($user->is_node) {
809 $user->priv(1); # I have relented and defaulted nodes
810 $user->lockout(1) if $user->is_node;
812 $user->homenode($homenode) if $homenode;
813 $user->node($homenode);
816 $user->lastin($main::systime); # this make it last longer than just this invocation
817 $user->put; # just to make sure it gets written away!!!
818 dbg("DXProt: PC92 new user record for $call created");
821 # this is to fix a problem I introduced some build ago by using this function for users
822 # whereas it was only being used for nodes.
823 if ($user->is_user && $user->lockout && ($user->priv // 0) == 1) {
826 dbg("DXProt: PC92 user record for $call depriv'd and unlocked");
832 # incoming cluster list
842 my $newline = "PC19^";
845 my (@rout, @pc92out);
847 # first get the INTERFACE node
848 my $parent = Route::Node::get($self->{call});
850 dbg("PCPROT: my parent $self->{call} has disappeared");
859 # We are making a major change from now on. We are only going to accept
860 # PC19s from directly connected nodes. This means that we are probably
861 # going to throw away most of the data that we are being sent.
863 # The justification for this is that most of it is wrong or out of date
866 # From now on we are only going to believe PC92 data and locally connected
869 for ($i = 1; $i < $#$pc-1; $i += 4) {
870 my $here = $pc->[$i];
871 my $call = uc $pc->[$i+1];
872 my $conf = $pc->[$i+2];
873 my $ver = $pc->[$i+3];
874 next unless defined $here && defined $conf && is_callsign($call);
876 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
878 # check for sane parameters
879 # $ver = 5000 if $ver eq '0000';
880 next unless $ver && $ver =~ /^\d+$/;
881 next if $ver < 5000; # only works with version 5 software
882 next if length $call < 3; # min 3 letter callsigns
883 next if $call eq $main::mycall || $call eq $main::myalias;
885 # check that this PC19 isn't trying to alter the wrong dxchan
887 my $dxchan = DXChannel::get($call);
889 if ($dxchan == $self) {
892 dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
897 # isolate now means only accept stuff from this call only
898 if ($self->{isolate} && $call ne $self->{call}) {
899 dbg("PCPROT: $self->{call} isolated, $call ignored") if isdbg('chanerr');
903 my $user = check_add_user($call, 'A');
905 # if (eph_dup($genline)) {
906 # dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
912 if ($parent->via_pc92) {
913 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
918 my $r = Route::Node::get($call);
919 my $flags = Route::here($here)|Route::conf($conf);
921 # modify the routing table if it is in it, otherwise store it in the pc19list for now
924 if ($call ne $parent->call) {
925 if ($self->in_filter_route($r)) {
926 $ar = $parent->add($call, $ver, $flags);
927 # push @rout, $ar if $ar;
932 if ($r->version ne $ver || $r->flags != $flags) {
938 if ($call eq $self->{call} || $user->wantroutepc19) {
939 my $new = Route->new($call); # throw away
940 if ($self->in_filter_route($new)) {
941 my $ar = $parent->add($call, $ver, $flags);
942 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
943 push @rout, $ar if $ar;
944 push @pc92out, $r if $h;
951 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
952 my $mref = DXMsg::get_busy($call);
953 $mref->stop_msg($call) if $mref;
955 $user->lastin($main::systime) unless DXChannel::get($call);
959 # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
960 # but remember there will only be one (pair) these because any extras will be
963 # $self->route_pc21($self->{call}, $line, @rout);
964 $self->route_pc19($self->{call}, $line, @rout);
966 if (@pc92out && !$pc92_slug_changes) {
967 $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
971 # send local configuration
980 if ($self->{do_pc9x} && $self->{state} ne 'init92') {
981 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
982 $self->{do_pc9x} = 0;
984 $self->send_local_config;
986 $self->state('normal');
987 $self->{lastping} = 0;
988 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
991 # delete a cluster from the list
993 # This should never occur for directly connected nodes.
1003 my $call = uc $pc->[1];
1005 eph_del_regex("^PC1[679].*$call");
1007 # if I get a PC21 from the same callsign as self then ignore it
1008 if ($call eq $self->{call}) {
1009 dbg("PCPROT: self referencing PC21 from $self->{call}");
1013 # for the above reason and also because of the check for PC21s coming
1014 # in for self->call from outside being ignored further down
1015 # we don't need any isolation code here, because we will never
1016 # act on a PC21 with self->call in it.
1018 my $parent = Route::Node::get($self->{call});
1020 dbg("PCPROT: my parent $self->{call} has disappeared");
1027 if ($call ne $main::mycall && $call ne $main::myalias) { # don't allow malicious buggers to disconnect me!
1028 my $node = Route::Node::get($call);
1031 if ($node->via_pc92) {
1032 dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
1036 my $dxchan = DXChannel::get($call);
1037 if ($dxchan && $dxchan != $self) {
1038 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chan');
1043 return unless $self->in_filter_route($node);
1045 # routing objects, force a PC21 if it is local
1046 push @rout, $node->del($parent);
1047 push @rout, $call if $dxchan && @rout == 0;
1050 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chan');
1054 if (eph_dup($line)) {
1059 $self->route_pc21($origin, $line, @rout);
1060 # $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
1073 if ($self->{do_pc9x}) {
1074 if ($self->{state} ne 'init92') {
1075 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
1076 $self->{do_pc9x} = 0;
1079 $self->{lastping} = 0;
1080 $self->state('normal');
1081 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
1093 # route foreign' pc27s
1095 if ($pc->[8] ne $main::mycall) {
1096 $self->route($pc->[8], $line);
1103 my $d = cltounix($pc->[1], sprintf("%02d18Z", $pc->[2]));
1104 my $sfi = unpad($pc->[3]);
1105 my $k = unpad($pc->[4]);
1106 my $i = unpad($pc->[5]);
1107 my ($r) = $pc->[6] =~ /R=(\d+)/;
1109 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1110 dbg("PCPROT: WWV Date ($pc->[1] $pc->[2]) out of range") if isdbg('chanerr');
1114 # global wwv filtering on INPUT
1115 my @dxcc = ((Prefix::cty_data($pc->[7]))[0..2], (Prefix::cty_data($pc->[8]))[0..2]);
1116 if ($self->{inwwvfilter}) {
1117 my ($filter, $hops) = $self->{inwwvfilter}->it(@$pc[7,8], $origin, @dxcc);
1119 dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
1123 $pc->[7] =~ s/-\d+$//o; # remove spotter's ssid
1124 if (Geomag::dup($d,$sfi,$k,$i,$pc->[6],$pc->[7])) {
1125 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1129 # note this only takes the first one it gets
1130 Geomag::update($d, $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1131 dbg("WWV: <$pc->[2]>, sfi=$sfi k=$k info=$i '$pc->[6]' $pc->[7]\@$pc->[8] $r route: $origin") if isdbg('progress');
1133 if (defined &Local::wwv) {
1136 $rep = Local::wwv($self, $pc->[1], $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1141 # DON'T be silly and send on PC27s!
1142 return if $pcno == 27;
1144 # broadcast to the eager world
1145 send_wwv_spot($self, $line, $d, $pc->[2], $sfi, $k, $i, @$pc[6..8]);
1157 my $call = uc $pc->[1];
1159 $nref = Route::Node::get($call);
1160 $uref = Route::User::get($call);
1161 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1163 if (eph_dup($line)) {
1167 $nref->here($pc->[2]) if $nref;
1168 $uref->here($pc->[2]) if $uref;
1169 my $ref = $nref || $uref;
1170 return unless $self->in_filter_route($ref);
1172 $self->route_pc24($origin, $line, $ref, $pc->[3]);
1184 if ($pc->[1] ne $main::mycall) {
1185 $self->route($pc->[1], $line);
1188 if ($pc->[2] eq $main::mycall) {
1189 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chan');
1193 Log('DXProt', "Merge request for $pc->[3] spots and $pc->[4] WWV from $pc->[2]");
1197 my @in = reverse Spot::search(1, undef, undef, 0, $pc->[3]);
1200 $self->send(pc26(@{$in}[0..4], $pc->[2]));
1206 my @in = reverse Geomag::search(0, $pc->[4], time, 1);
1209 $self->send(pc27(@{$in}[0..5], $pc->[2]));
1214 sub handle_26 {goto &handle_11}
1215 sub handle_27 {goto &handle_23}
1217 # mail/file handling
1226 if ($pc->[1] eq $main::mycall) {
1228 my $sub = "DXMsg::handle_$pcno";
1231 $self->route($pc->[1], $line) unless $self->is_clx;
1235 sub handle_29 {goto &handle_28}
1236 sub handle_30 {goto &handle_28}
1237 sub handle_31 {goto &handle_28}
1238 sub handle_32 {goto &handle_28}
1239 sub handle_33 {goto &handle_28}
1249 if (eph_dup($line, $eph_pc34_restime)) {
1252 $self->process_rcmd($pc->[1], $pc->[2], $pc->[2], $pc->[3]);
1256 # remote command replies
1265 eph_del_regex("^PC35\\^$pc->[2]\\^$pc->[1]\\^");
1266 $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[1], $pc->[3]);
1269 sub handle_36 {goto &handle_34}
1280 if ($pc->[1] eq $main::mycall) {
1282 my $sub = "DXDb::handle_$pcno";
1285 $self->route($pc->[1], $line) unless $self->is_clx;
1289 # node connected list from neighbour
1299 # incoming disconnect
1308 if ($pc->[1] eq $self->{call}) {
1309 $self->disconnect(1);
1311 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1315 sub handle_40 {goto &handle_28}
1326 my $call = $pc->[1];
1327 my $sort = $pc->[2];
1330 my $l = "PC41^$call^$sort";
1331 if (eph_dup($l, $eph_info_restime)) {
1335 # input filter if required
1336 # my $ref = Route::get($call) || Route->new($call);
1337 # return unless $self->in_filter_route($ref);
1339 if ($val eq $sort || $val =~ /^\s*$/) {
1340 dbg('PCPROT: invalid value') if isdbg('chanerr');
1344 if ($call eq $main::mycall || $call eq $main::myalias) {
1345 dbg "DXPROT: PC41 trying to update $call from outside via $origin, ignored";
1348 my $chan = DXChannel::get($call);
1350 dbg "DXPROT: PC41 trying to update online $call from outside via $origin, ignored";
1354 # add this station to the user database, if required
1355 my $user = DXUser::get_current($call);
1356 $user = DXUser->new($call) unless $user;
1359 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1360 dbg("PCPROT: invalid name") if isdbg('chanerr');
1364 } elsif ($sort == 2) {
1366 } elsif ($sort == 3) {
1367 if (is_latlong($val)) {
1368 my ($lat, $long) = DXBearing::stoll($val);
1369 $user->lat($lat) if $lat;
1370 $user->long($long) if $long;
1371 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1373 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1376 } elsif ($sort == 4) {
1377 $user->homenode($val);
1378 } elsif ($sort == 5) {
1379 if (is_qra(uc $val)) {
1380 my ($lat, $long) = DXBearing::qratoll(uc $val);
1381 $user->lat($lat) if $lat && !$user->lat;
1382 $user->long($long) if $long && !$user->long;
1383 $user->qra(uc $val);
1385 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1389 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1392 unless ($self->{isolate}) {
1393 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1396 # perhaps this IS what we want after all
1397 # $self->route_pc41($ref, $call, $sort, $val, $pc->[4]);
1400 sub handle_42 {goto &handle_28}
1404 sub handle_44 {goto &handle_37}
1405 sub handle_45 {goto &handle_37}
1406 sub handle_46 {goto &handle_37}
1407 sub handle_47 {goto &handle_37}
1408 sub handle_48 {goto &handle_37}
1410 # message and database
1419 if (eph_dup($line)) {
1423 if ($pc->[1] eq $main::mycall) {
1424 DXMsg::handle_49($self, @$pc);
1426 $self->route($pc->[1], $line) unless $self->is_clx;
1430 # keep alive/user list
1439 return if (eph_dup($line));
1441 my $call = $pc->[1];
1443 my $node = Route::Node::get($call);
1445 return unless $node->call eq $self->{call};
1446 $node->usercount($pc->[2]) unless $node->users;
1448 $node->PC92C_dxchan($self->call, $pc->[-1]);
1450 # input filter if required
1451 # return unless $self->in_filter_route($node);
1453 unless ($self->{isolate}) {
1454 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1456 # $self->route_pc50($origin, $line, $node, $pc->[2], $pc->[3]) unless eph_dup($line);
1460 # incoming ping requests/answers
1470 my $from = $pc->[2];
1471 my $flag = $pc->[3];
1473 if ($to eq $main::myalias) {
1474 dbg("DXPROT: Ping addressed to \$myalias ($main::myalias), ignored") if isdbg('chan');
1479 if ($to eq $main::mycall) {
1481 $self->send(pc51($from, $to, '0'));
1483 DXXml::Ping::handle_ping_reply($self, $from);
1486 if (eph_dup($line)) {
1489 # route down an appropriate thingy
1490 $self->route($to, $line);
1494 sub handle_61 { goto &handle_11; }
1496 # dunno but route it
1505 my $call = $pc->[1];
1506 if ($call ne $main::mycall) {
1507 $self->route($call, $line);
1520 my $call = $pc->[1];
1523 my $d = cltounix($call, sprintf("%02d18Z", $pc->[2]));
1524 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1525 dbg("PCPROT: WCY Date ($call $pc->[2]) out of range") if isdbg('chanerr');
1528 $pc = [ map { unpad($_) } @$pc ];
1530 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1534 my $wcy = WCY::update($d, @$pc[2..12]);
1535 dbg("WCY: <$pc->[2]> K=$pc->[5] expK=$pc->[6] A=$pc->[4] R=$pc->[7] SFI=$pc->[3] SA=$pc->[8] GMF=$pc->[9] Au=$pc->[10] $pc->[11]\@$pc->[12] route: $origin") if isdbg('progress');
1537 if (defined &Local::wcy) {
1540 $rep = Local::wcy($self, @$pc[1..12]);
1545 # broadcast to the eager world
1546 send_wcy_spot($self, $line, $d, @$pc[2..12]);
1549 # remote commands (incoming)
1558 $self->process_rcmd($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1561 # remote command replies
1570 $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1573 # decode a pc92 call: flag call : version : build
1574 sub _decode_pc92_call
1577 my @part = split /:/, $icall;
1578 my ($flag, $call) = unpack "A A*", $part[0];
1579 unless (defined $flag && $flag ge '0' && $flag le '7') {
1580 dbg("PCPROT: $icall no flag byte (0-7) at front of call, ignored") if isdbg('chanerr');
1583 unless ($call && is_callsign($call)) {
1584 dbg("PCPROT: $icall no recognisable callsign, ignored") if isdbg('chanerr');
1587 my $is_node = $flag & 4;
1588 my $is_extnode = $flag & 2;
1589 my $here = $flag & 1;
1590 my $version = $part[1] || 0;
1591 my $build = $part[2] || 0;
1592 my $ip = $part[3] || '';
1594 # remember that, at this stage, IPV6 addresses have : replaced by ,
1595 if (length $version > 4 && $version =~ /[,\.][\da-f]{1,4}/i) {
1599 $version =~ s/\D//g;
1600 $version = 0 unless $version && $version =~ /^[0-9]+$/;
1603 $build = 0 unless $build && $build =~ /^[0-9]+$/;
1606 $ip =~ s/^::ffff://i;
1607 $ip = '' unless is_ipaddr($ip);
1609 dbg("'$icall' = '" . join("', '", $call, $is_node, $is_extnode, $here, $version, $build, $ip) . "'") if isdbg('pc92');
1610 return ($call, $is_node, $is_extnode, $here, $version+0, $build+0, $ip);
1613 # decode a pc92 call: flag call : version : build
1614 sub _encode_pc92_call
1618 # plain call or value
1619 return $ref unless ref $ref;
1621 my $ext = shift || 0;
1623 my $call = $ref->call;
1625 $flag |= $ref->here ? 1 : 0;
1626 if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1628 my $dxchan = DXChannel::get($call);
1629 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1630 if (($ext & 1) && $ref->version) {
1631 my $version = $ref->version || 1.0;
1632 $version = $version * 100 + 5300 if $version < 50;
1633 $extra .= ":" . $version;
1636 if (($ext & 2) && $ref->ip) {
1639 $extra .= ':' . $ip;
1642 return "$flag$call$extra";
1655 my ($call, $is_node, $is_extnode, $here, $version, $build, $ip) = @$s;
1658 # remove spurious IPV6 prefix on IPV4 addresses
1663 my $ncall = $parent->call;
1664 if ($ncall ne $call) {
1668 # normalise call, delete any unnormalised calls in the users file.
1669 # then ignore this thingy
1670 my $normcall = normalise_call($call);
1671 if ($normcall ne $call) {
1672 next if DXChannel::get($call);
1673 $user = DXUser::get($call);
1674 dbg("DXProt::_add_thingy call $call normalised to $normcall, deleting spurious user $call");
1675 $user->del if $user;
1676 $call = $normcall; # this is safe because a route add will ignore duplicates
1680 dbg("ROUTE: added node $call to $ncall") if isdbg('routelow');
1681 $user = check_add_user($call, 'A');
1682 @rout = $parent->add($call, $version, Route::here($here), $ip);
1683 $r = Route::Node::get($call);
1684 $r->PC92C_dxchan($dxchan->call, $hops) if $r;
1685 if ($version && is_numeric($version) && !$r->K && !$user->K) {
1686 my $old = $user->sort;
1687 if ($user->is_ak1a && (($version >= 5455 && $build > 0) || ($version >= 3000 && $version <= 3500)) ) {
1689 dbg("PCProt::_add_thingy node $call v: $version b: $build sort ($old) updated to " . $user->sort);
1690 } elsif ($user->is_spider && ($version < 3000 || ($version > 4000 && $version < 5455))) {
1691 unless ($version > 0 && $build == 0) {
1694 dbg("PCProt::_add_thingy node $call v: $version b: $build sort ($old) downgraded to " . $user->sort);
1698 $r->version($user->version) if $user->version;
1699 $r->build($user->build) if $user->build;
1700 $r->K(1) if $user->K;
1702 dbg("ROUTE: added user $call to $ncall") if isdbg('routelow');
1703 $user = check_add_user($call, 'U', $parent->call);
1704 @rout = $parent->add_user($call, Route::here($here), $ip);
1705 $dxchan->tell_buddies('loginb', $call, $ncall) if $dxchan;
1706 $r = Route::User::get($call);
1710 Log('DXProt', "PC92A $call -> $ip on $ncall");
1712 if ($pc92_slug_changes && $parent == $main::routeroot) {
1713 $things_add{$call} = Route::get($call);
1714 delete $things_del{$call};
1716 $user->close($main::systime, $ip) if $user; # this just updates lastseen and the connlist list containing the IP address
1718 dbgprintring(10) if isdbg('nologchan');
1719 dbg("DXProt::add_thingy: Trying to add parent $call to itself $ncall, ignored");
1731 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1736 $ref = Route::Node::get($call);
1737 dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1738 @rout = $ref->del($parent) if $ref;
1740 dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1741 $ref = Route::User::get($call);
1743 $dxchan->tell_buddies('logoutb', $call, $parent->call) if $dxchan;
1744 @rout = $parent->del_user($ref);
1747 if ($pc92_slug_changes && $parent == $main::routeroot) {
1748 $things_del{$call} = $ref unless exists $things_add{$call};
1749 delete $things_add{$call};
1755 # this will only happen if we are slugging changes and
1756 # there are some changes to be sent, it will create an add or a delete
1758 sub gen_pc92_changes
1760 my @add = values %things_add;
1761 my @del = values %things_del;
1762 return (\@add, \@del);
1765 sub clear_pc92_changes
1767 %things_add = %things_del = ();
1768 $last_pc92_slug = $main::systime;
1777 return $_last_pc9x_id;
1782 if (!$_last_time || $_last_time != $main::systime) {
1783 $_last_time = $main::systime;
1785 return $_last_pc9x_id = $_last_time - $main::systime_daystart;
1788 return $_last_pc9x_id = sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1799 # check that the time is between 0 >= $t < 86400
1800 unless ($t >= 0 && $t < 86400) {
1801 dbg("PCPROT: time invalid t: $t, ignored") if isdbg('chanerr');
1805 # check that the time of this pc9x is within tolerance (default 15 mins either way)
1806 my $now = $main::systime - $main::systime_daystart ;
1807 my $diff = abs($now - $t);
1808 unless ($diff < $pc9x_time_tolerance || 86400 - $diff < $pc9x_time_tolerance) {
1809 my $c = ref $call ? $call->call : $call;
1810 dbg("PC9XERR: $c time out of range t: $t now: $now diff: $diff > $pc9x_time_tolerance, ignored") if isdbg('chan');
1814 my $parent = ref $call ? $call : Route::Node::get($call);
1816 # we only do this for external calls whose routing table
1817 # record come and go. The reference for mycall is permanent
1818 # and not that frequently used, it also never times out, so
1819 # the id on it is completely unreliable. Besides, only commands
1820 # originating on this box will go through this code...
1821 if ($parent->call ne $main::mycall) {
1822 my $lastid = $parent->lastid;
1823 if (defined $lastid) {
1825 # note that this is where we determine whether this pc9x has come in yesterday
1826 # but is still greater (modulo 86400) than the lastid or is simply an old
1827 # duplicate sentence. To determine this we need to do some module 86400
1828 # arithmetic. High numbers mean that this is an old duplicate sentence,
1829 # low numbers that it is a new sentence.
1831 # Typically you will see yesterday being taken on $t = 84, $lastid = 86235
1832 # and old dupes with $t = 234, $lastid = 256 (which give answers 249 and
1833 # 86378 respectively in the calculation below).
1835 if ($t+86400-$lastid > $pc9x_past_age) {
1836 dbg("PCPROT: dup id on $t <= lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1839 } elsif ($t == $lastid) {
1840 dbg("PCPROT: dup id on $t == lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1843 # check that if we have a low number in lastid that yesterday's numbers
1844 # (likely in the 85000+ area) don't override them, thus causing flip flopping
1845 if ($lastid+86400-$t < $pc9x_past_age) {
1846 dbg("PCPROT: dup id on $t in yesterday, lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1853 $parent = Route::Node->new($call);
1855 dbg("PCPROT: $call does not exist, ignored") if isdbg('pc92dedupe');
1858 if (isdbg('pc92dedupe')) {
1859 my $exists = exists $parent->{lastid}; # naughty, naughty :-)
1860 my $val = $parent->{lastid};
1861 my $s = $exists ? (defined $val ? $val : 'exists/undef') : 'undef';
1862 dbg("PCPROT: $call pc92 id lastid $s -> $t");
1864 $parent->lastid($t);
1869 sub pc92_handle_first_slot
1876 my $oparent = $parent;
1880 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$slot;
1881 if ($call && $is_node) {
1882 if ($call eq $main::mycall) {
1883 LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::mycall ($main::mycall), ignored");
1886 if ($call eq $main::myalias) {
1887 LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::myalias ($main::myalias), ignored");
1890 # this is only accepted from my "self".
1891 # this also kills configs from PC92 nodes with external PC19 nodes that are also
1892 # locally connected. Local nodes always take precedence. But we remember the lastid
1893 # to try to reduce the number of dupe PC92s for this external node.
1894 if (DXChannel::get($call) && $call ne $self->{call}) {
1895 $parent = check_pc9x_t($call, $t, 92); # this will update the lastid time
1896 dbg("PCPROT: locally connected node $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1900 # reparent to external node (note that we must have received a 'C' or 'A' record
1901 # from the true parent node for this external before we get one for the this node
1902 unless ($parent = Route::Node::get($call)) {
1903 if ($is_extnode && $oparent) {
1904 @radd = _add_thingy($oparent, $slot, $self, $hops);
1907 dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1911 $parent = check_pc9x_t($call, $t, 92) || return;
1912 $parent->via_pc92(1);
1913 $parent->PC92C_dxchan($self->{call}, $hops);
1916 dbg("PCPROT: must be \$mycall or external node as first entry, ignored") if isdbg('chanerr');
1919 $parent->here(Route::here($here));
1920 $parent->version($version || $pc19_version) if $version;
1921 $parent->build($build) if $build;
1922 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
1923 return ($parent, @radd);
1926 # DXSpider routing entries
1937 my $pcall = $pc->[1];
1939 my $sort = $pc->[3];
1940 my $hops = $pc->[-1];
1942 # this catches loops of A/Ds
1943 # if (eph_dup($line, $pc9x_dupe_age)) {
1947 if ($pcall eq $main::mycall) {
1948 LogDbg('err', "PCPROT: looped back, ignored");
1952 if ($pcall eq $main::myalias) {
1953 LogDbg('err', "PCPROT: looped back to \$myalias ($main::myalias), misconfiguration ignored");
1957 if ($pcall eq $self->{call} && $self->{state} eq 'init') {
1958 if ($self->{isolate}) {
1959 dbg("DXPROT: PC9x received, but $pcall is isolated, ignored");
1961 } elsif (!$self->user->wantpc9x) {
1962 dbg("DXPROT: PC9x explicitly switched off on $pcall, ignored");
1965 $self->state('init92');
1966 $self->{do_pc9x} = 1;
1967 dbg("DXPROT: Do pc9x set on $pcall");
1970 unless ($self->{do_pc9x}) {
1971 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
1975 # don't create routing entries for D records that don't already exist
1976 # this is what causes all those PC92 loops!
1977 my $parent = check_pc9x_t($pcall, $t, 92, $sort ne 'D') || return;
1978 my $oparent = $parent;
1980 $parent->do_pc9x(1);
1981 $parent->via_pc92(1);
1983 if ($sort eq 'F' || $sort eq 'R') {
1985 # this is the route finding section
1986 # here is where the consequences of the 'find' command
1989 my $from = $pc->[4];
1990 my $target = $pc->[5];
1996 if ($ref = DXChannel::get($target)) {
1997 $flag = 1; # we are directly connected
1999 $ref = Route::get($target);
2000 $dxchan = $ref->dxchan;
2003 if ($ref && $flag && $dxchan) {
2004 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
2007 } elsif ($sort eq 'R') {
2008 if (my $dxchan = DXChannel::get($from)) {
2009 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @$pc[6,7]);
2011 my $ref = Route::get($from);
2013 my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
2015 $_->send($line) for @dxchan;
2017 dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2020 dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2026 } elsif ($sort eq 'K') {
2027 $pc92Kin += length $line;
2029 # remember the last channel we arrived on
2030 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2032 my @ent = _decode_pc92_call($pc->[4]);
2037 ($parent, $add) = $self->pc92_handle_first_slot(\@ent, $parent, $t, $hops);
2038 return unless $parent; # dupe
2040 push @radd, $add if $add;
2042 my $call = $parent->call;
2043 my $version = $ent[4] || 0;
2044 my $build = $ent[5] || 0;
2046 my $oldbuild = $parent->build || 0;
2047 my $oldversion = $parent->version || 0;
2048 my $user = check_add_user($parent->call, 'S');
2049 my $oldsort = $user->sort // '';
2051 dbg("PCProt PC92 K v: $version ov: $oldversion b: $build ob: $oldbuild pk: " . ($parent->K || '0') . " uk: " . ($user->K || 0)) if isdbg('pc92k');
2053 if (is_numeric($version) || is_numeric($build)) {
2055 if (($oldversion ne $version || $build ne $oldbuild)) {
2056 dbg("PCProt PC92 K node $call updated version: $version (was $oldversion) build: $build (was $oldbuild)");
2057 $user->version($parent->version($version));
2058 $user->build($parent->build($build));
2061 if ($oldsort ne 'S') {
2062 dbg("PCProt PC92 K node $call updated sort: $sort (was $oldsort)");
2067 dbg("PCProt PC92 K node $call updated - marked as PC92 K user");
2071 $user->put if $changed;
2072 $parent->K(1); # mark this as come in on a K
2074 dbg("DXProt PC92 K version call $call: invalid version: '$version' or build: '$version', ignored");
2076 dbg("ROUTE: reset obscount on $call now " . $parent->obscount) if isdbg('obscount');
2078 } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
2080 $pc92Ain += length $line if $sort eq 'A';
2081 $pc92Cin += length $line if $sort eq 'C';
2082 $pc92Din += length $line if $sort eq 'D';
2084 # remember the last channel we arrived on
2085 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2087 # this is the main route section
2088 # here is where all the routes are created and destroyed
2090 # cope with missing duplicate node calls in the first slot
2091 my $me = $pc->[4] || '';
2092 $me ||= _encode_pc92_call($parent) unless $me ;
2094 my @ent = map {my @a = _decode_pc92_call($_); @a ? \@a : ()} grep {$_ && /^[0-7]/} $me, @$pc[5 .. $#$pc];
2098 # look at the first one which will always be a node of some sort
2099 # except in the case of 'A' or 'D' in which the $pcall is used
2100 # otherwise use the node call and update any information
2101 # that needs to be done.
2104 ($parent, $add) = $self->pc92_handle_first_slot($ent[0], $parent, $t, $hops);
2105 return unless $parent; # dupe
2108 push @radd, $add if $add;
2111 # do a pass through removing any references to either mycall
2115 next unless $_ && @$_;
2116 if ($_->[0] eq $main::mycall) {
2117 dbg("PCPROT: $self->{call} : type $sort $_->[0] refers to me, ignored") if isdbg('route');
2120 if ($_->[0] eq $main::myalias && $_->[1] || $_->[0] eq $main::mycall && $_->[1] == 0) {
2121 LogDbg('err',"PCPROT: $self->{call} : type $sort $_->[0] trying to change type to " . $_->[1]?"Node":"User" . ", ignored");
2130 push @radd, _add_thingy($parent, $_, $self, $hops);
2132 } elsif ($sort eq 'D') {
2134 push @rdel, _del_thingy($parent, $_, $self);
2136 } elsif ($sort eq 'C') {
2137 my (@nodes, @users);
2139 # we reset obscounts on config records as well as K records
2141 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('obscount');
2144 foreach my $r (@nent) {
2145 # my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);
2148 push @nodes, $r->[0];
2150 push @users, $r->[0];
2153 dbg("PCPROT: $self->{call} : pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr') || isdbg('route');
2157 my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
2160 foreach my $r (@nent) {
2163 push @radd,_add_thingy($parent, $r, $self, $hops) if grep $call eq $_, (@$nnodes, @$nusers);
2167 foreach my $r (@$dnodes) {
2168 push @rdel,_del_thingy($parent, [$r, 1], $self);
2170 foreach my $r (@$dusers) {
2171 push @rdel,_del_thingy($parent, [$r, 0], $self);
2174 # remember this last PC92C for rebroadcast on demand
2175 $parent->last_PC92C($line);
2177 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
2181 foreach my $r (@rdel) {
2184 $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
2185 $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
2187 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
2188 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
2189 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
2190 $self->route_pc19($pcall, undef, @pc19) if @pc19;
2191 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
2194 # broadcast it if we get here
2195 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2198 # get all the routes for a thing, bearing in mind that the thing (e.g. a user)
2199 # might be on two or more nodes at the same time or that there may be more than
2200 # one equal distance neighbour to a node.
2202 # What this means that if sh/route g1tlh shows that he is on (say) two nodes, then
2203 # a Route::findroutes is done on each of those two nodes, the best route(s) taken from
2204 # each and then combined to give a set of dxchans to send the PC9x record down
2206 sub find_pc9x_routes
2209 my $ref = Route::get($to);
2213 if ($ref->isa('Route::User')) {
2214 my $dxchan = DXChannel::get($to);
2215 push @parent, $to if $dxchan;
2216 push @parent, $ref->parents;
2220 foreach my $p (@parent) {
2222 my @routes = Route::findroutes($p);
2223 foreach my $r (@routes) {
2224 $lasthops = $r->[0] unless defined $lasthops;
2225 if ($r->[0] == $lasthops) {
2226 $cand{$r->[1]->call} = $r->[1];
2232 return values %cand;
2243 # $self->{do_pc9x} ||= 1;
2245 my $pcall = $pc->[1]; # this is now checked earlier
2247 # remember that we are converting PC10->PC93 and self will be $main::me if it
2249 unless ($self->{do_pc9x}) {
2250 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
2255 my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
2257 my $to = uc $pc->[3];
2258 my $from = uc $pc->[4];
2259 my $via = uc $pc->[5];
2260 my $text = $pc->[6];
2261 my $onode = uc $pc->[7];
2262 $onode = $pcall if @$pc <= 8;
2264 # this is catch loops caused by bad software ...
2265 if (eph_dup("PC93|$from|$text|$onode", $pc10_dupe_age)) {
2269 if (isdbg('progress')) {
2270 my $vs = $via ne '*' ? " via $via" : '';
2271 my $s = "ANNTALK: $from\@$onode$vs -> $to '$text' route: $origin";
2275 # if this is a 'bad spotter' user then ignore it
2277 $nossid =~ s/-\d+$//;
2278 if ($badspotter->in($nossid)) {
2279 dbg($line) if isdbg('nologchan');
2280 dbg("PCPROT: Bad Spotter, dropped");
2284 # ignore PC93 coming in from outside this node with a target of local
2285 if ($to eq 'LOCAL' && $self != $main::me) {
2286 dbg("PCPROT: incoming LOCAL chat not from local node, ignored") if isdbg('chanerr');
2290 # will we allow it at all?
2293 if (@bad = BadWords::check($text)) {
2294 my $bw = join ', ', @bad;
2295 dbg($line) if isdbg('nologchan');
2296 dbg("PCPROT: Badwords: '$bw', dropped");
2301 # if it is routeable then then treat it like a talk
2302 my $ref = Route::get($to);
2306 # convert to PC10 or local talks where appropriate
2307 # PC93 capable nodes of the same hop count all get a copy
2308 # if there is a PC10 node then it will get a copy and that
2309 # will be it. Hopefully such a node will not figure highly
2310 # in the route list, unless it is local, 'cos it don't issue PC92s!
2311 # note that both local and PC93s at the same time are possible if the
2312 # user on more than one node.
2313 my @routes = find_pc9x_routes($to);
2315 foreach $dxchan (@routes) {
2316 if (ref $dxchan && $dxchan->isa('DXChannel')) {
2317 if ($dxchan->{do_pc9x}) {
2318 $dxchan->send($line);
2320 $dxchan->talk($from, $to, $via, $text, $onode);
2323 dbg("ERROR: $to -> $dxchan is not a DXChannel! (convert to pc10)");
2328 } elsif ($to eq '*' || $to eq 'SYSOP' || $to eq 'WX') {
2330 my $sysop = $to eq 'SYSOP' ? '*' : ' ';
2331 my $wx = $to eq 'WX' ? '1' : '0';
2332 my $local = $via eq 'LOCAL' ? '*' : $via;
2334 $self->send_announce(1, pc12($from, $text, $local, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
2335 return if $via eq 'LOCAL';
2336 } elsif (!is_callsign($to) && $text =~ /^#\d+ /) {
2337 # chat messages really only locally connected users
2338 $self->send_chat(1, $line, $from, '*', $text, $to, $pcall, '0');
2341 # broadcast this chat sentence everywhere unless it is targetted to 'LOCAL'
2342 $self->broadcast_route_pc9x($pcall, undef, $line, 0) unless $to eq 'LOCAL' || $via eq 'LOCAL';
2345 # if get here then rebroadcast the thing with its Hop count decremented (if
2346 # there is one). If it has a hop count and it decrements to zero then don't
2349 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
2361 unless (eph_dup($line)) {
2363 my $pcall = $pc->[1];
2364 unless (is_callsign($pcall)) {
2365 dbg("PCPROT: invalid callsign string '$pc->[1]', ignored") if isdbg('chanerr');
2369 my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
2370 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2372 unless ($self->{isolate}) {
2373 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me