3 # This module impliments the handlers for the protocal mode for a dx cluster
5 # Copyright (c) 1998-2007 Dirk Koopman G1TLH
39 no warnings qw(uninitialized);
43 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
44 $last_hour $last10 %eph %pings %rcmds $ann_to_talk
45 $pingint $obscount %pc19list $chatdupeage $chatimportfn
46 $investigation_int $pc19_version $myprot_version
47 %nodehops $baddx $badspotter $badnode $censorpc
48 $allowzero $decode_dk0wcy $send_opernam @checklist
49 $eph_pc15_restime $pc9x_past_age $pc9x_dupe_age
50 $pc10_dupe_age $pc92_slug_changes $last_pc92_slug
51 $pc92Ain $pc92Cin $pc92Din $pc92Kin $pc9x_time_tolerance
52 $pc92filterdef $senderverify $pc11_dwell_time
55 $pc9x_dupe_age = 60; # catch loops of circular (usually) D records
56 $pc10_dupe_age = 45; # just something to catch duplicate PC10->PC93 conversions
57 $pc92_slug_changes = 60*1; # slug any changes going outward for this long
58 $last_pc92_slug = 0; # the last time we sent out any delayed add or del PC92s
59 $pc9x_time_tolerance = 15*60; # the time on a pc9x is allowed to be out by this amount
60 $pc9x_past_age = (122*60)+ # maximum age in the past of a px9x (a config record might be the only
61 $pc9x_time_tolerance; # thing a node might send - once an hour and we allow an extra hour for luck)
62 # this is actually the partition between "yesterday" and "today" but old.
63 $senderverify = 0; # 1 - check for forged PC11 or PC61.
64 # 2 - if forged, dump them.
65 $pc11_dwell_time = 2; # number of seconds to wait for a PC61 to come to substitute the PC11
68 $pc92filterdef = bless ([
69 # tag, sort, field, priv, special parser
78 # this is a place to park an incoming PC11 in the sure and certain hope that
79 # a PC61 will be along soon. This has the side benefit that it will delay a
80 # a PC11 for one second - assuming that it is not removed by a PC61 version
82 # incoming talk commands
91 # this is to catch loops caused by bad software ...
92 if (eph_dup($line, $pc10_dupe_age)) {
97 # is it for me or one of mine?
98 my ($from, $to, $via, $call, $dxchan);
100 if ($pc->[5] gt ' ') {
107 # if this is a 'nodx' node then ignore it
108 if ($badnode->in($pc->[6]) || ($via && $badnode->in($via))) {
109 dbg($line) if isdbg('nologchan');
110 dbg("PCPROT: Bad Node, dropped");
116 $nossid =~ s/-\d+$//;
117 if ($badspotter->in($nossid)) {
118 dbg($line) if isdbg('nologchan');
119 dbg("PCPROT: Bad Spotter, dropped");
123 # if we are converting announces to talk is it a dup?
125 if (AnnTalk::is_talk_candidate($from, $pc->[3]) && AnnTalk::dup($from, $to, $pc->[3])) {
126 dbg("PCPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
131 # will we allow it at all?
134 if (@bad = BadWords::check($pc->[3])) {
135 my $bw = join ', ', @bad;
136 dbg($line) if isdbg('nologchan');
137 dbg("PCPROT: Badwords: '$bw', dropped");
142 # convert this to a PC93, coming from mycall with origin set and process it as such
143 $main::me->normal(pc93($to, $from, $via, $pc->[3], $pc->[6]));
146 my %pc11_saved; # delayed PC11s
147 my $pc11_rx; # no of pc11 rxed
148 my $pc61_rx; # no of pc61 rxed
149 my $pc11_to_61; # no of 'better' pc61, that replaced stored waiting pc11
150 my $rpc11_to_61; # no of pc11s 'promoted' to pc61 by IP captured in routing table
160 my $recurse = shift || 0;
162 # route 'foreign' pc26s
164 if ($pc->[7] ne $main::mycall) {
165 $self->route($pc->[7], $line);
172 dbg("INPUT $self->{call} -> $line origin $origin recurse: $recurse") if isdbg("pc11") || isdbg("pc61");
174 # my ($hops) = $pc->[8] =~ /^H(\d+)/;
176 # is the spotted callsign blank? This should really be trapped earlier but it
177 # could break other protocol sentences. Also check for lower case characters.
178 if ($pc->[2] =~ /^\s*$/) {
179 dbg($line) if isdbg('nologchan');
180 dbg("PCPROT: blank callsign, dropped");
183 if ($pc->[2] =~ /[a-z]/) {
184 dbg($line) if isdbg('nologchan');
185 dbg("PCPROT: lowercase characters, dropped");
190 # if this is a 'nodx' node then ignore it
191 if ($badnode->in($pc->[7])) {
192 dbg($line) if isdbg('nologchan');
193 dbg("PCPROT: Bad Node $pc->[7], dropped");
197 # convert the date to a unix date
198 my $d = cltounix($pc->[3], $pc->[4]);
199 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
200 if (!$d || (($pcno == 11 || $pcno == 61) && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
201 dbg($line) if isdbg('nologchan');
202 dbg("PCPROT: Spot ignored, invalid date or out of range ($pc->[3] $pc->[4])\n");
207 if ($baddx->in($pc->[2])) {
208 dbg("PCPROT: Bad DX spot '$pc->[2]', ignored");
209 dbg($line) if isdbg('nologchan');
213 # is it claiming to be BUST(ed)
214 $pc->[5] =~ s/^\s+//; # take any leading blanks off
215 $pc->[2] = unpad($pc->[2]); # take off leading and trailing blanks from spotted callsign
216 if ($pc->[2] =~ /BUST\w*$/) {
217 dbg($line) if isdbg('nologchan');
218 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
222 my $nossid = $pc->[6];
223 $nossid =~ s/-\d+$//;
225 my @spot = Spot::prepare($pc->[1], $pc->[2], $d, $pc->[5], $nossid, $pc->[7], $pc->[8]);
229 # $f2 = date in unix format
232 # $f5 = spotted dxcc country
233 # $f6 = spotter dxcc country
236 # $f9 = spotted cq zone
238 # $f11 = spotter cq zone
239 # $f12 = spotted us state
240 # $f13 = spotter us state
244 # is this is a 'bad spotter' or an unknown user then ignore it.
245 if ($badspotter->in($nossid)) {
246 dbg($line) if isdbg('nologchan');
247 dbg("PCPROT: Bad Spotter $pc->[6], dropped");
251 # global spot filtering on INPUT
252 if ($self->{inspotsfilter}) {
253 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
255 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
261 # this is where we decide to delay PC11s in the hope that a PC61 will be along soon.
263 my $key = join '|', @spot[0..2,4,7]; # not including text
267 if (Spot::dup_find(@spot[0..4,7])) {
268 dbg("DUPE $pc->[0] $self->{call} -> key: $key dumped") if isdbg('pc11');
274 if ($pc11_saved{$key}) {
276 my $percent = $pc11_rx ? $pc11_to_61 * 100 / $pc11_rx : 0;
277 dbg(sprintf("PROMOTED BETTER $self->{call} -> $pc->[0] $key, using PC61, saved PC11 DUMPED: $pc61_rx pc11: $pc11_rx better pc61: $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
278 delete $pc11_saved{$key};
283 if (Spot::dup_find(@spot[0..4,7])) {
284 dbg("DUPE $pc->[0] $self->{call} -> key: $key dumped") if isdbg('pc11');
290 if ($pc11_saved{$key}) {
291 dbg("DUPE $pc->[0] $self->{call } key: is saved, ignored") if isdbg("pc11");
292 return; # because it's a dup
295 # can we promote this to a PC61?
296 my $r = Route::User::get($spot[4]); # find spotter
297 if ($r && $r->ip) { # do we have an ip addres
300 $pc->[7] = $spot[14] = $r->ip;
302 my $percent = $pc11_rx ? $rpc11_to_61 * 100 / $pc11_rx : 0;
303 dbg(sprintf("PROMOTED ROUTE $self->{call} -> PC11 $key PROMOTED to PC61 with IP $spot[14] pc61: $pc61_rx pc11: $pc11_rx route->pc61 $rpc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
304 delete $pc11_saved{$key};
307 # if it is STILL (despite all efforts to change it) a PC11
308 # save it and wait - it will be called from pc11_process
310 $pc11_saved{$key} = [$main::systime, $self, $pcno, $line, $origin, $pc];
311 dbg("SAVED $self->{call} -> NEW $pc->[0] spot $key waiting for a better offer") if isdbg("pc11");
317 my $count = $pc11_to_61+$rpc11_to_61;
318 my $percent = $pc11_rx ? $count*100 / $pc11_rx : 0;
319 my $pc11_61 = $pc61_rx ? $pc11_rx*100 / $pc61_rx : 0;
320 dbg(sprintf("PASSED $self->{call} -> $pc->[0] spot $key, pc11: $pc11_rx pc61: $pc61_rx PC11/PC61 ratio: %0.1f%% total pc11->pc61: $count (%0.1f%%) ", $pc11_61, $percent)) if isdbg("pc11");
323 # this goes after the input filtering, but before the add
324 # so that if it is input filtered, it isn't added to the dup
325 # list. This allows it to come in from a "legitimate" source
326 if (Spot::dup(@spot[0..4,7])) {
327 dbg("PCPROT: Duplicate Spot $self->{call} -> $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
331 dbg("PROCESSING $self->{call} -> $pc->[0] key: $key") if isdbg('pc11');
333 # we check IP addresses for PC61 - this will also dedupe PC11 copies
334 if (@$pc > 8 && is_ipaddr($pc->[8])) {
338 if (DXCIDR::find($ip)) {
339 dbg($line) if isdbg('nologchan');
340 dbg("PCPROT: $ip in badip list, dropped");
346 # here we verify the spotter is currently connected to the node it says it is one. AKA email sender verify
347 # but without the explicit probe to the node. We are relying on "historical" information, but it very likely
348 # to be current once we have seen the first PC92C from that node.
350 # As for spots generated from non-PC92 nodes, we'll see after about do_pc9x3h20m...
353 my $nroute = Route::Node::get($pc->[7]);
354 my $uroute = Route::Node::get($pc->[6]);
355 my $local = DXChannel::get($pc->[7]);
357 if ($nroute && ($nroute->last_PC92C || ($local && !$local->do_pc9x))) {
359 my $ip = $pcno == 61 ? $pc->[8] : '';
360 # $s .= "User $pc->[6] not logged in, " unless $uroute;
361 $s .= "User $pc->[6] not on node $pc->[7], " unless $nroute->is_user($pc->[6]);
362 # $s .= "Node $pc->[7] at '$ip' not on Node's IP " . $nroute->ip if $ip && $nroute && $nroute->ip && $nroute->ip ne $ip;
364 my $action = $senderverify > 1 ? ", DUMPED" : '';
366 dbg("PCPROT: Suspicious Spot $pc->[2] on $pc->[1] by $pc->[6]($ip)\@$pc->[7] $s$action");
367 return unless $senderverify < 2;
372 # we until here to do any censorship to try and reduce the amount of noise that repeated copies
373 # from other connected nodes cause
376 if (@bad = BadWords::check($pc->[5])) {
377 my $bw = join ', ', @bad;
378 dbg($line) if isdbg('nologchan');
379 dbg("PCPROT: Badwords: '$bw', dropped");
389 $ip ||= $spot[14] if exists $spot[14];
390 if (isdbg('progress')) {
391 my $sip = $ip ? sprintf "($ip)" : '' unless $ip =~ m|[\(\)\*]|;
393 my $d = ztime($spot[2]);
394 my $s = "SPOT: $spot[1] on $spot[0] \@ $d by $spot[4]$sip\@$spot[7]";
395 $s .= $spot[3] ? " '$spot[3]'" : q{ ''};
396 $s .= " route: $origin";
401 # @spot at this point contains:-
402 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
403 # then spotted itu, spotted cq, spotters itu, spotters cq
404 # you should be able to route on any of these
407 # fix up qra locators of known users
408 my $user = DXUser::get_current($spot[4]);
410 my $qra = $user->qra;
411 unless ($qra && is_qra($qra)) {
412 my $lat = $user->lat;
413 my $long = $user->long;
414 if (defined $lat && defined $long) {
415 $user->qra(DXBearing::lltoqra($lat, $long));
420 # send a remote command to a distant cluster if it is visible and there is no
421 # qra locator and we havn't done it for a month.
423 unless ($user->qra) {
425 my $to = $user->homenode;
426 my $last = $user->lastoper || 0;
427 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
428 my $cmd = "forward/opernam $spot[4]";
429 # send the rcmd but we aren't interested in the replies...
430 my $dxchan = $node->dxchan;
431 if ($dxchan && $dxchan->is_clx) {
432 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
434 route(undef, $to, pc34($main::mycall, $to, $cmd));
436 if ($to ne $origin) {
438 $node = Route::Node::get($to);
440 $dxchan = $node->dxchan;
441 if ($dxchan && $dxchan->is_clx) {
442 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
444 route(undef, $to, pc34($main::mycall, $to, $cmd));
448 $user->lastoper($main::systime);
455 if (defined &Local::spot) {
458 $r = Local::spot($self, @spot);
463 # DON'T be silly and send on PC26s!
464 return if $pcno == 26;
466 # send out the filtered spots
467 send_dx_spot($self, $line, @spot) if @spot;
469 # cancel any recursion as we have now processed it
471 if ($pc11_saved{$key}) {
472 dbg("END RECURSED $self->{call} $pc->[0] key: $key removed and finished") if isdbg('pc11');
473 delete $pc11_saved{$key};
475 dbg("END RECURSED $self->{call} $pc->[0] key: $key finished") if isdbg('pc11');
479 dbg("END NORMAL $self->{call} $pc->[0] key: $key finished") if isdbg('pc11');
483 # used to kick outstanding PC11 if required
486 foreach my $key (keys %pc11_saved) {
487 my $r = $pc11_saved{$key};
488 if ($main::systime > $r->[0] + $pc11_dwell_time) {
489 dbg("SAVED PC11 spot $key timed out waiting, recursing") if isdbg("pc11");
491 delete $pc11_saved{$key};
492 $self->handle_11(@$r[2..5], 1);
506 # announce duplicate checking
507 $pc->[3] =~ s/^\s+//; # remove leading blanks
509 # if this is a 'nodx' node then ignore it
510 if ($badnode->in($pc->[5])) {
511 dbg($line) if isdbg('nologchan');
512 dbg("PCPROT: Bad Node, dropped");
516 # if this is a 'bad spotter' user then ignore it
517 my $nossid = $pc->[1];
518 $nossid =~ s/-\d+$//;
519 if ($badspotter->in($nossid)) {
520 dbg($line) if isdbg('nologchan');
521 dbg("PCPROT: Bad Spotter, dropped");
525 # ignore PC12s from origins that use PCxx protocol
526 my $oref = Route::get($origin);
527 if ($oref->do_pc9x) {
528 dbg("PCPROT: PC12 rxed from PC9x node, ignored") if isdbg('chanerr');
534 if (@bad = BadWords::check($pc->[3])) {
535 my $bw = join ', ', @bad;
536 dbg($line) if isdbg('nologchan');
537 dbg("PCPROT: Badwords: '$bw', dropped");
545 if ((($dxchan = DXChannel::get($pc->[2])) && $dxchan->is_user) || $pc->[4] =~ /^[\#\w.]+$/){
546 $self->send_chat(0, $line, @$pc[1..6]);
547 } elsif ($pc->[2] eq '*' || $pc->[2] eq $main::mycall) {
549 # ignore something that looks like a chat line coming in with sysop
550 # flag - this is a kludge...
551 if ($pc->[3] =~ /^\#\d+ / && $pc->[4] eq '*') {
552 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
556 # here's a bit of fun, convert incoming ann with a callsign in the first word
557 # or one saying 'to <call>' to a talk if we can route to the recipient
559 my $call = AnnTalk::is_talk_candidate($pc->[1], $pc->[3]);
561 my $ref = Route::get($call);
563 $dxchan = $ref->dxchan;
564 $dxchan->talk($pc->[1], $call, undef, $pc->[3], $pc->[5]) if $dxchan != $self;
571 $self->send_announce(0, $line, @$pc[1..6]);
573 $self->route($pc->[2], $line);
577 if (defined &Local::ann) {
580 $r = Local::ann($self, $line, @$pc[1..6]);
594 if (eph_dup($line, $eph_pc15_restime)) {
597 unless ($self->{isolate}) {
598 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
614 my $ncall = $pc->[1];
615 my $newline = "PC16^";
617 # dos I want users from this channel?
618 unless ($self->user->wantpc16) {
619 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
624 if ($ncall eq $main::mycall) {
625 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
630 $h = 1 if DXChannel::get($ncall);
631 if ($h && $self->{call} ne $ncall) {
632 dbg("PCPROT: trying to update a local node, ignored") if isdbg('chanerr');
636 if (eph_dup($line)) {
640 # isolate now means only accept stuff from this call only
641 if ($self->{isolate} && $ncall ne $self->{call}) {
642 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
646 my $parent = Route::Node::get($ncall);
649 $dxchan = $parent->dxchan;
650 if ($dxchan && $dxchan ne $self) {
651 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
655 # input filter if required
656 return unless $self->in_filter_route($parent);
658 $parent = Route::Node->new($ncall);
662 if ($parent->via_pc92) {
663 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
670 for ($i = 2; $i < $#$pc; $i++) {
671 my ($call, $conf, $here) = $pc->[$i] =~ /^(\S+) (\S) (\d)/o;
672 next unless $call && $conf && defined $here && is_callsign($call);
673 next if $call eq $main::mycall;
675 eph_del_regex("^PC17\\^$call\\^$ncall");
677 $conf = $conf eq '*';
679 # reject this if we think it is a node already
680 my $r = Route::Node::get($call);
681 my $u = DXUser::get_current($call) unless $r;
682 if ($r || ($u && $u->is_node)) {
683 dbg("PCPROT: $call is a node") if isdbg('chanerr');
687 $r = Route::User::get($call);
688 my $flags = Route::here($here)|Route::conf($conf);
691 my $au = $r->addparent($parent);
692 if ($r->flags != $flags) {
696 push @rout, $r if $h && $au;
698 my @ans = $parent->add_user($call, $flags);
699 push @rout, @ans if $h && @ans;
702 # add this station to the user database, if required
703 my $user = DXUser::get_current($ncall);
704 $user = DXUser->new($call) unless $user;
705 $user->homenode($parent->call) if !$user->homenode;
706 $user->node($parent->call);
707 $user->lastin($main::systime) unless DXChannel::get($call);
710 # send info to all logged in thingies
711 $self->tell_login('loginu', "$ncall: $call") if $user->is_local_node;
712 $self->tell_buddies('loginb', $call, $ncall);
715 $self->route_pc16($origin, $line, $parent, @rout) if @rout;
716 # $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
730 my $ncall = $pc->[2];
731 my $ucall = $pc->[1];
733 eph_del_regex("^PC16\\^$ncall.*$ucall");
735 # do I want users from this channel?
736 unless ($self->user->wantpc16) {
737 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
741 if ($ncall eq $main::mycall) {
742 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
746 # isolate now means only accept stuff from this call only
747 if ($self->{isolate} && $ncall ne $self->{call}) {
748 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
752 my $uref = Route::User::get($ucall);
754 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
757 my $parent = Route::Node::get($ncall);
759 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
763 $dxchan = DXChannel::get($ncall);
764 if ($dxchan && $dxchan ne $self) {
765 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
770 if ($parent->via_pc92) {
771 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
776 if (DXChannel::get($ucall)) {
777 dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
781 # input filter if required and then remove user if present
782 # return unless $self->in_filter_route($parent);
783 $parent->del_user($uref);
785 # send info to all logged in thingies
786 my $user = DXUser::get_current($ncall);
787 $self->tell_login('logoutu', "$ncall: $ucall") if $user && $user->is_local_node;
788 $self->tell_buddies('logoutb', $ucall, $ncall);
790 if (eph_dup($line)) {
794 $self->route_pc17($origin, $line, $parent, $uref);
795 # $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
807 $self->state('init');
809 my $parent = Route::Node::get($self->{call});
811 # record the type and version offered
812 if (my ($software, $version) = $pc->[1] =~ /(DXSpider|CC\s*Cluster)\s+Version: (\d+(?:\.\d+)?)/i) {
814 $version += 53 if $version < 6;
815 $self->{version} = $version;
816 $self->user->version($version);
817 $parent->version($version);
818 my ($build) = $pc->[1] =~ /Build: (\d+(?:\.\d+)?)/;
820 $self->{build} = $build;
821 $self->user->build($build);
822 $parent->build($build);
823 dbg("$self->{call} = $software version $version build $build");
824 unless ($self->is_spider) {
825 dbg("Change U " . $self->user->sort . " C $self->{sort} -> S");
826 $self->user->sort('S');
830 # $self->{handle_xml}++ if DXXml::available() && $pc->[1] =~ /\bxml/;
832 dbg("$self->{call} = Unknown software ($pc->[1] $pc->[2])");
833 $self->version(50.0);
834 $self->version($pc->[2] / 100) if $pc->[2] && $pc->[2] =~ /^\d+$/;
835 $self->user->version($self->version);
838 if ($pc->[1] =~ /CC\*Cluster/i || $pc->[1] =~ /\bpc9x/i) {
839 if ($self->{isolate}) {
840 dbg("$self->{call} pc9x recognised, but node is isolated, using old protocol");
841 } elsif (!$self->user->wantpc9x) {
842 dbg("$self->{call} pc9x explicitly switched off, using old protocol");
844 $self->{do_pc9x} = 1;
845 dbg("$self->{call} Set do PC9x");
849 # first clear out any nodes on this dxchannel
850 my @rout = $parent->del_nodes;
851 $self->route_pc21($origin, $line, @rout, $parent) if @rout;
852 $self->send_local_config();
860 my $homenode = shift;
862 # add this station to the user database, if required (don't remove SSID from nodes)
863 my $user = DXUser::get_current($call);
865 $user = DXUser->new($call);
866 $user->sort($type || 'U');
867 if ($user->is_node) {
868 $user->priv(1); # I have relented and defaulted nodes
869 $user->lockout(1) if $user->is_node;
871 $user->homenode($homenode) if $homenode;
872 $user->node($homenode);
875 $user->lastin($main::systime); # this make it last longer than just this invocation
876 $user->put; # just to make sure it gets written away!!!
877 dbg("DXProt: PC92 new user record for $call created");
880 # this is to fix a problem I introduced some build ago by using this function for users
881 # whereas it was only being used for nodes.
882 if ($user->is_user && $user->lockout && ($user->priv // 0) == 1) {
885 dbg("DXProt: PC92 user record for $call depriv'd and unlocked");
891 # incoming cluster list
901 my $newline = "PC19^";
904 my (@rout, @pc92out);
906 # first get the INTERFACE node
907 my $parent = Route::Node::get($self->{call});
909 dbg("PCPROT: my parent $self->{call} has disappeared");
918 # We are making a major change from now on. We are only going to accept
919 # PC19s from directly connected nodes. This means that we are probably
920 # going to throw away most of the data that we are being sent.
922 # The justification for this is that most of it is wrong or out of date
925 # From now on we are only going to believe PC92 data and locally connected
928 for ($i = 1; $i < $#$pc-1; $i += 4) {
929 my $here = $pc->[$i];
930 my $call = uc $pc->[$i+1];
931 my $conf = $pc->[$i+2];
932 my $ver = $pc->[$i+3];
933 next unless defined $here && defined $conf && is_callsign($call);
935 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
937 # check for sane parameters
938 # $ver = 5000 if $ver eq '0000';
939 next unless $ver && $ver =~ /^\d+$/;
940 next if $ver < 5000; # only works with version 5 software
941 next if length $call < 3; # min 3 letter callsigns
942 next if $call eq $main::mycall || $call eq $main::myalias;
944 # check that this PC19 isn't trying to alter the wrong dxchan
946 my $dxchan = DXChannel::get($call);
948 if ($dxchan == $self) {
951 dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
956 # isolate now means only accept stuff from this call only
957 if ($self->{isolate} && $call ne $self->{call}) {
958 dbg("PCPROT: $self->{call} isolated, $call ignored") if isdbg('chanerr');
962 my $user = check_add_user($call, 'A');
964 # if (eph_dup($genline)) {
965 # dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
971 if ($parent->via_pc92) {
972 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
977 my $r = Route::Node::get($call);
978 my $flags = Route::here($here)|Route::conf($conf);
980 # modify the routing table if it is in it, otherwise store it in the pc19list for now
983 if ($call ne $parent->call) {
984 if ($self->in_filter_route($r)) {
985 $ar = $parent->add($call, $ver, $flags);
986 # push @rout, $ar if $ar;
991 if ($r->version ne $ver || $r->flags != $flags) {
997 if ($call eq $self->{call} || $user->wantroutepc19) {
998 my $new = Route->new($call); # throw away
999 if ($self->in_filter_route($new)) {
1000 my $ar = $parent->add($call, $ver, $flags);
1001 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
1002 push @rout, $ar if $ar;
1003 push @pc92out, $r if $h;
1010 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
1011 my $mref = DXMsg::get_busy($call);
1012 $mref->stop_msg($call) if $mref;
1014 $user->lastin($main::systime) unless DXChannel::get($call);
1018 # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
1019 # but remember there will only be one (pair) these because any extras will be
1022 # $self->route_pc21($self->{call}, $line, @rout);
1023 $self->route_pc19($self->{call}, $line, @rout);
1025 if (@pc92out && !$pc92_slug_changes) {
1026 $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
1030 # send local configuration
1039 if ($self->{do_pc9x} && $self->{state} ne 'init92' && $self->{state} ne 'normal') {
1040 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
1041 $self->{do_pc9x} = 0;
1043 $self->send_local_config;
1044 $self->send(pc22());
1045 $self->state('normal');
1046 $self->{lastping} = 0;
1047 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
1050 # delete a cluster from the list
1052 # This should never occur for directly connected nodes.
1062 my $call = uc $pc->[1];
1064 eph_del_regex("^PC1[679].*$call");
1066 # if I get a PC21 from the same callsign as self then ignore it
1067 if ($call eq $self->{call}) {
1068 dbg("PCPROT: self referencing PC21 from $self->{call}");
1072 # for the above reason and also because of the check for PC21s coming
1073 # in for self->call from outside being ignored further down
1074 # we don't need any isolation code here, because we will never
1075 # act on a PC21 with self->call in it.
1077 my $parent = Route::Node::get($self->{call});
1079 dbg("PCPROT: my parent $self->{call} has disappeared");
1086 if ($call ne $main::mycall && $call ne $main::myalias) { # don't allow malicious buggers to disconnect me!
1087 my $node = Route::Node::get($call);
1090 if ($node->via_pc92) {
1091 dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
1095 my $dxchan = DXChannel::get($call);
1096 if ($dxchan && $dxchan != $self) {
1097 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chan');
1102 return unless $self->in_filter_route($node);
1104 # routing objects, force a PC21 if it is local
1105 push @rout, $node->del($parent);
1106 push @rout, $call if $dxchan && @rout == 0;
1109 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chan');
1113 if (eph_dup($line)) {
1118 $self->route_pc21($origin, $line, @rout);
1119 # $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
1132 if ($self->{do_pc9x}) {
1133 if ($self->{state} ne 'init92' && $self->{state} ne 'normal') {
1134 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
1135 $self->{do_pc9x} = 0;
1138 $self->{lastping} = 0;
1139 $self->state('normal');
1140 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
1152 # route foreign' pc27s
1154 if ($pc->[8] ne $main::mycall) {
1155 $self->route($pc->[8], $line);
1162 my $d = cltounix($pc->[1], sprintf("%02d18Z", $pc->[2]));
1163 my $sfi = unpad($pc->[3]);
1164 my $k = unpad($pc->[4]);
1165 my $i = unpad($pc->[5]);
1166 my ($r) = $pc->[6] =~ /R=(\d+)/;
1168 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1169 dbg("PCPROT: WWV Date ($pc->[1] $pc->[2]) out of range") if isdbg('chanerr');
1173 # global wwv filtering on INPUT
1174 my @dxcc = ((Prefix::cty_data($pc->[7]))[0..2], (Prefix::cty_data($pc->[8]))[0..2]);
1175 if ($self->{inwwvfilter}) {
1176 my ($filter, $hops) = $self->{inwwvfilter}->it(@$pc[7,8], $origin, @dxcc);
1178 dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
1182 $pc->[7] =~ s/-\d+$//o; # remove spotter's ssid
1183 if (Geomag::dup($d,$sfi,$k,$i,$pc->[6],$pc->[7])) {
1184 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1188 # note this only takes the first one it gets
1189 Geomag::update($d, $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1190 dbg("WWV: <$pc->[2]>, sfi=$sfi k=$k info=$i '$pc->[6]' $pc->[7]\@$pc->[8] $r route: $origin") if isdbg('progress');
1192 if (defined &Local::wwv) {
1195 $rep = Local::wwv($self, $pc->[1], $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1200 # DON'T be silly and send on PC27s!
1201 return if $pcno == 27;
1203 # broadcast to the eager world
1204 send_wwv_spot($self, $line, $d, $pc->[2], $sfi, $k, $i, @$pc[6..8]);
1216 my $call = uc $pc->[1];
1218 $nref = Route::Node::get($call);
1219 $uref = Route::User::get($call);
1220 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1222 if (eph_dup($line)) {
1226 $nref->here($pc->[2]) if $nref;
1227 $uref->here($pc->[2]) if $uref;
1228 my $ref = $nref || $uref;
1229 return unless $self->in_filter_route($ref);
1231 $self->route_pc24($origin, $line, $ref, $pc->[3]);
1243 if ($pc->[1] ne $main::mycall) {
1244 $self->route($pc->[1], $line);
1247 if ($pc->[2] eq $main::mycall) {
1248 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chan');
1252 Log('DXProt', "Merge request for $pc->[3] spots and $pc->[4] WWV from $pc->[2]");
1256 my @in = reverse Spot::search(1, undef, undef, 0, $pc->[3]);
1259 $self->send(pc26(@{$in}[0..4], $pc->[2]));
1265 my @in = reverse Geomag::search(0, $pc->[4], time, 1);
1268 $self->send(pc27(@{$in}[0..5], $pc->[2]));
1273 sub handle_26 {goto &handle_11}
1274 sub handle_27 {goto &handle_23}
1276 # mail/file handling
1285 if ($pc->[1] eq $main::mycall) {
1287 my $sub = "DXMsg::handle_$pcno";
1290 $self->route($pc->[1], $line) unless $self->is_clx;
1294 sub handle_29 {goto &handle_28}
1295 sub handle_30 {goto &handle_28}
1296 sub handle_31 {goto &handle_28}
1297 sub handle_32 {goto &handle_28}
1298 sub handle_33 {goto &handle_28}
1308 if (eph_dup($line, $eph_pc34_restime)) {
1311 $self->process_rcmd($pc->[1], $pc->[2], $pc->[2], $pc->[3]);
1315 # remote command replies
1324 eph_del_regex("^PC35\\^$pc->[2]\\^$pc->[1]\\^");
1325 $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[1], $pc->[3]);
1328 sub handle_36 {goto &handle_34}
1339 if ($pc->[1] eq $main::mycall) {
1341 my $sub = "DXDb::handle_$pcno";
1344 $self->route($pc->[1], $line) unless $self->is_clx;
1348 # node connected list from neighbour
1358 # incoming disconnect
1367 if ($pc->[1] eq $self->{call}) {
1368 $self->disconnect(1);
1370 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1374 sub handle_40 {goto &handle_28}
1385 my $call = $pc->[1];
1386 my $sort = $pc->[2];
1389 my $l = "PC41^$call^$sort";
1390 if (eph_dup($l, $eph_info_restime)) {
1394 # input filter if required
1395 # my $ref = Route::get($call) || Route->new($call);
1396 # return unless $self->in_filter_route($ref);
1398 if ($val eq $sort || $val =~ /^\s*$/) {
1399 dbg('PCPROT: invalid value') if isdbg('chanerr');
1403 if ($call eq $main::mycall || $call eq $main::myalias) {
1404 dbg "DXPROT: PC41 trying to update $call from outside via $origin, ignored";
1407 my $chan = DXChannel::get($call);
1409 dbg "DXPROT: PC41 trying to update online $call from outside via $origin, ignored";
1413 # add this station to the user database, if required
1414 my $user = DXUser::get_current($call);
1415 $user = DXUser->new($call) unless $user;
1418 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1419 dbg("PCPROT: invalid name") if isdbg('chanerr');
1423 } elsif ($sort == 2) {
1425 } elsif ($sort == 3) {
1426 if (is_latlong($val)) {
1427 my ($lat, $long) = DXBearing::stoll($val);
1428 $user->lat($lat) if $lat;
1429 $user->long($long) if $long;
1430 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1432 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1435 } elsif ($sort == 4) {
1436 $user->homenode($val);
1437 } elsif ($sort == 5) {
1438 if (is_qra(uc $val)) {
1439 my ($lat, $long) = DXBearing::qratoll(uc $val);
1440 $user->lat($lat) if $lat && !$user->lat;
1441 $user->long($long) if $long && !$user->long;
1442 $user->qra(uc $val);
1444 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1448 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1451 unless ($self->{isolate}) {
1452 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1455 # perhaps this IS what we want after all
1456 # $self->route_pc41($ref, $call, $sort, $val, $pc->[4]);
1459 sub handle_42 {goto &handle_28}
1463 sub handle_44 {goto &handle_37}
1464 sub handle_45 {goto &handle_37}
1465 sub handle_46 {goto &handle_37}
1466 sub handle_47 {goto &handle_37}
1467 sub handle_48 {goto &handle_37}
1469 # message and database
1478 if (eph_dup($line)) {
1482 if ($pc->[1] eq $main::mycall) {
1483 DXMsg::handle_49($self, @$pc);
1485 $self->route($pc->[1], $line) unless $self->is_clx;
1489 # keep alive/user list
1498 return if (eph_dup($line));
1500 my $call = $pc->[1];
1502 my $node = Route::Node::get($call);
1504 return unless $node->call eq $self->{call};
1505 $node->usercount($pc->[2]) unless $node->users;
1507 $node->PC92C_dxchan($self->call, $pc->[-1]);
1509 # input filter if required
1510 # return unless $self->in_filter_route($node);
1512 unless ($self->{isolate}) {
1513 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1515 # $self->route_pc50($origin, $line, $node, $pc->[2], $pc->[3]) unless eph_dup($line);
1519 # incoming ping requests/answers
1529 my $from = $pc->[2];
1530 my $flag = $pc->[3];
1532 if ($to eq $main::myalias) {
1533 dbg("DXPROT: Ping addressed to \$myalias ($main::myalias), ignored") if isdbg('chan');
1538 if ($to eq $main::mycall) {
1540 $self->send(pc51($from, $to, '0'));
1542 DXXml::Ping::handle_ping_reply($self, $from);
1545 if (eph_dup($line)) {
1548 # route down an appropriate thingy
1549 $self->route($to, $line);
1553 sub handle_61 { goto &handle_11; }
1555 # dunno but route it
1564 my $call = $pc->[1];
1565 if ($call ne $main::mycall) {
1566 $self->route($call, $line);
1579 my $call = $pc->[1];
1582 my $d = cltounix($call, sprintf("%02d18Z", $pc->[2]));
1583 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1584 dbg("PCPROT: WCY Date ($call $pc->[2]) out of range") if isdbg('chanerr');
1587 $pc = [ map { unpad($_) } @$pc ];
1589 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1593 my $wcy = WCY::update($d, @$pc[2..12]);
1594 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');
1596 if (defined &Local::wcy) {
1599 $rep = Local::wcy($self, @$pc[1..12]);
1604 # broadcast to the eager world
1605 send_wcy_spot($self, $line, $d, @$pc[2..12]);
1608 # remote commands (incoming)
1617 $self->process_rcmd($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1620 # remote command replies
1629 $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1632 # decode a pc92 call: flag call : version : build
1633 sub _decode_pc92_call
1636 my @part = split /:/, $icall;
1637 my ($flag, $call) = unpack "A A*", $part[0];
1638 unless (defined $flag && $flag ge '0' && $flag le '7') {
1639 dbg("PCPROT: $icall no flag byte (0-7) at front of call, ignored") if isdbg('chanerr');
1642 unless ($call && is_callsign($call)) {
1643 dbg("PCPROT: $icall no recognisable callsign, ignored") if isdbg('chanerr');
1646 my $is_node = $flag & 4;
1647 my $is_extnode = $flag & 2;
1648 my $here = $flag & 1;
1649 my $version = $part[1] || 0;
1650 my $build = $part[2] || 0;
1651 my $ip = $part[3] || '';
1653 # remember that, at this stage, IPV6 addresses have : replaced by ,
1654 if (length $version > 4 && $version =~ /[,\.][\da-f]{1,4}/i) {
1658 $version =~ s/\D//g;
1659 $version = 0 unless $version && $version =~ /^[0-9]+$/;
1662 $build = 0 unless $build && $build =~ /^[0-9]+$/;
1665 $ip =~ s/^::ffff://i;
1666 $ip = '' unless is_ipaddr($ip);
1668 dbg("'$icall' = '" . join("', '", $call, $is_node, $is_extnode, $here, $version, $build, $ip) . "'") if isdbg('pc92');
1669 return ($call, $is_node, $is_extnode, $here, $version+0, $build+0, $ip);
1672 # decode a pc92 call: flag call : version : build
1673 sub _encode_pc92_call
1677 # plain call or value
1678 return $ref unless ref $ref;
1680 my $ext = shift || 0;
1682 my $call = $ref->call;
1684 $flag |= $ref->here ? 1 : 0;
1685 if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1687 my $dxchan = DXChannel::get($call);
1688 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1689 if (($ext & 1) && $ref->version) {
1690 my $version = $ref->version || 1.0;
1691 $version = $version * 100 + 5300 if $version < 50;
1692 $extra .= ":" . $version;
1695 if (($ext & 2) && $ref->ip) {
1698 $extra .= ':' . $ip;
1701 return "$flag$call$extra";
1714 my ($call, $is_node, $is_extnode, $here, $version, $build, $ip) = @$s;
1717 # remove spurious IPV6 prefix on IPV4 addresses
1722 my $ncall = $parent->call;
1723 if ($ncall ne $call) {
1727 # normalise call, delete any unnormalised calls in the users file.
1728 # then ignore this thingy
1729 my $normcall = normalise_call($call);
1730 if ($normcall ne $call) {
1731 next if DXChannel::get($call);
1732 $user = DXUser::get($call);
1733 dbg("DXProt::_add_thingy call $call normalised to $normcall, deleting spurious user $call");
1734 $user->del if $user;
1735 $call = $normcall; # this is safe because a route add will ignore duplicates
1739 dbg("ROUTE: added node $call to $ncall") if isdbg('routelow');
1740 $user = check_add_user($call, 'A');
1741 @rout = $parent->add($call, $version, Route::here($here), $ip);
1742 $r = Route::Node::get($call);
1743 $r->PC92C_dxchan($dxchan->call, $hops) if $r;
1744 if ($version && is_numeric($version) && !$r->K && !$user->K) {
1745 my $old = $user->sort;
1746 if ($user->is_ak1a && (($version >= 5455 && $build > 0) || ($version >= 3000 && $version <= 3500)) ) {
1748 dbg("PCPROT::_add_thingy node $call v: $version b: $build sort ($old) updated to " . $user->sort);
1749 } elsif ($user->is_spider && ($version < 3000 || ($version > 4000 && $version < 5455))) {
1750 unless ($version > 0 && $build == 0) {
1753 dbg("PCPROT::_add_thingy node $call v: $version b: $build sort ($old) downgraded to " . $user->sort);
1757 $r->version($user->version) if $user->version;
1758 $r->build($user->build) if $user->build;
1759 $r->K(1) if $user->K;
1761 dbg("ROUTE: added user $call to $ncall") if isdbg('routelow');
1762 $user = check_add_user($call, 'U', $parent->call);
1763 @rout = $parent->add_user($call, Route::here($here), $ip);
1764 $dxchan->tell_buddies('loginb', $call, $ncall) if $dxchan;
1765 $r = Route::User::get($call);
1769 Log('DXProt', "PC92A $call -> $ip on $ncall");
1771 if ($pc92_slug_changes && $parent == $main::routeroot) {
1772 $things_add{$call} = Route::get($call);
1773 delete $things_del{$call};
1775 $user->close($main::systime, $ip) if $user; # this just updates lastseen and the connlist list containing the IP address
1777 dbgprintring(10) if isdbg('nologchan');
1778 dbg("DXProt::add_thingy: Trying to add parent $call to itself $ncall, ignored");
1790 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1795 $ref = Route::Node::get($call);
1796 dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1797 @rout = $ref->del($parent) if $ref;
1799 dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1800 $ref = Route::User::get($call);
1802 $dxchan->tell_buddies('logoutb', $call, $parent->call) if $dxchan;
1803 @rout = $parent->del_user($ref);
1806 if ($pc92_slug_changes && $parent == $main::routeroot) {
1807 $things_del{$call} = $ref unless exists $things_add{$call};
1808 delete $things_add{$call};
1814 # this will only happen if we are slugging changes and
1815 # there are some changes to be sent, it will create an add or a delete
1817 sub gen_pc92_changes
1819 my @add = values %things_add;
1820 my @del = values %things_del;
1821 return (\@add, \@del);
1824 sub clear_pc92_changes
1826 %things_add = %things_del = ();
1827 $last_pc92_slug = $main::systime;
1836 return $_last_pc9x_id;
1841 if (!$_last_time || $_last_time != $main::systime) {
1842 $_last_time = $main::systime;
1844 return $_last_pc9x_id = $_last_time - $main::systime_daystart;
1847 return $_last_pc9x_id = sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1858 # check that the time is between 0 >= $t < 86400
1859 unless ($t >= 0 && $t < 86400) {
1860 dbg("PCPROT: time invalid t: $t, ignored") if isdbg('chanerr');
1864 # check that the time of this pc9x is within tolerance (default 15 mins either way)
1865 my $now = $main::systime - $main::systime_daystart ;
1866 my $diff = abs($now - $t);
1867 unless ($diff < $pc9x_time_tolerance || 86400 - $diff < $pc9x_time_tolerance) {
1868 my $c = ref $call ? $call->call : $call;
1869 dbg("PC9XERR: $c time out of range t: $t now: $now diff: $diff > $pc9x_time_tolerance, ignored") if isdbg('chan');
1873 my $parent = ref $call ? $call : Route::Node::get($call);
1875 # we only do this for external calls whose routing table
1876 # record come and go. The reference for mycall is permanent
1877 # and not that frequently used, it also never times out, so
1878 # the id on it is completely unreliable. Besides, only commands
1879 # originating on this box will go through this code...
1880 if ($parent->call ne $main::mycall) {
1881 my $lastid = $parent->lastid;
1882 if (defined $lastid) {
1884 # note that this is where we determine whether this pc9x has come in yesterday
1885 # but is still greater (modulo 86400) than the lastid or is simply an old
1886 # duplicate sentence. To determine this we need to do some module 86400
1887 # arithmetic. High numbers mean that this is an old duplicate sentence,
1888 # low numbers that it is a new sentence.
1890 # Typically you will see yesterday being taken on $t = 84, $lastid = 86235
1891 # and old dupes with $t = 234, $lastid = 256 (which give answers 249 and
1892 # 86378 respectively in the calculation below).
1894 if ($t+86400-$lastid > $pc9x_past_age) {
1895 dbg("PCPROT: dup id on $t <= lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1898 } elsif ($t == $lastid) {
1899 dbg("PCPROT: dup id on $t == lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1902 # check that if we have a low number in lastid that yesterday's numbers
1903 # (likely in the 85000+ area) don't override them, thus causing flip flopping
1904 if ($lastid+86400-$t < $pc9x_past_age) {
1905 dbg("PCPROT: dup id on $t in yesterday, lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1912 $parent = Route::Node->new($call);
1914 dbg("PCPROT: $call does not exist, ignored") if isdbg('pc92dedupe');
1917 if (isdbg('pc92dedupe')) {
1918 my $exists = exists $parent->{lastid}; # naughty, naughty :-)
1919 my $val = $parent->{lastid};
1920 my $s = $exists ? (defined $val ? $val : 'exists/undef') : 'undef';
1921 dbg("PCPROT: $call pc92 id lastid $s -> $t");
1923 $parent->lastid($t);
1928 sub pc92_handle_first_slot
1935 my $oparent = $parent;
1939 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$slot;
1940 if ($call && $is_node) {
1941 if ($call eq $main::mycall) {
1942 LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::mycall ($main::mycall), ignored");
1945 if ($call eq $main::myalias) {
1946 LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::myalias ($main::myalias), ignored");
1949 # this is only accepted from my "self".
1950 # this also kills configs from PC92 nodes with external PC19 nodes that are also
1951 # locally connected. Local nodes always take precedence. But we remember the lastid
1952 # to try to reduce the number of dupe PC92s for this external node.
1953 if (DXChannel::get($call) && $call ne $self->{call}) {
1954 $parent = check_pc9x_t($call, $t, 92); # this will update the lastid time
1955 dbg("PCPROT: locally connected node $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1959 # reparent to external node (note that we must have received a 'C' or 'A' record
1960 # from the true parent node for this external before we get one for the this node
1961 unless ($parent = Route::Node::get($call)) {
1962 if ($is_extnode && $oparent) {
1963 @radd = _add_thingy($oparent, $slot, $self, $hops);
1966 dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1970 $parent = check_pc9x_t($call, $t, 92) || return;
1971 $parent->via_pc92(1);
1972 $parent->PC92C_dxchan($self->{call}, $hops);
1975 dbg("PCPROT: must be \$mycall or external node as first entry, ignored") if isdbg('chanerr');
1978 $parent->here(Route::here($here));
1979 $parent->version($version || $pc19_version) if $version;
1980 $parent->build($build) if $build;
1981 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
1982 return ($parent, @radd);
1985 # DXSpider routing entries
1996 my $pcall = $pc->[1];
1998 my $sort = $pc->[3];
1999 my $hops = $pc->[-1];
2001 # this catches loops of A/Ds
2002 # if (eph_dup($line, $pc9x_dupe_age)) {
2006 if ($pcall eq $main::mycall) {
2007 LogDbg('err', "PCPROT: looped back, ignored");
2011 if ($pcall eq $main::myalias) {
2012 LogDbg('err', "PCPROT: looped back to \$myalias ($main::myalias), misconfiguration ignored");
2016 if ($pcall eq $self->{call} && $self->{state} eq 'init') {
2017 if ($self->{isolate}) {
2018 dbg("DXPROT: PC9x received, but $pcall is isolated, ignored");
2020 } elsif (!$self->user->wantpc9x) {
2021 dbg("DXPROT: PC9x explicitly switched off on $pcall, ignored");
2024 $self->state('init92');
2025 $self->{do_pc9x} = 1;
2026 dbg("DXPROT: Do pc9x set on $pcall");
2029 unless ($self->{do_pc9x}) {
2030 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
2034 # don't create routing entries for D records that don't already exist
2035 # this is what causes all those PC92 loops!
2036 my $parent = check_pc9x_t($pcall, $t, 92, $sort ne 'D') || return;
2037 my $oparent = $parent;
2039 $parent->do_pc9x(1);
2040 $parent->via_pc92(1);
2042 if ($sort eq 'F' || $sort eq 'R') {
2044 # this is the route finding section
2045 # here is where the consequences of the 'find' command
2048 my $from = $pc->[4];
2049 my $target = $pc->[5];
2055 if ($ref = DXChannel::get($target)) {
2056 $flag = 1; # we are directly connected
2058 $ref = Route::get($target);
2059 $dxchan = $ref->dxchan;
2062 if ($ref && $flag && $dxchan) {
2063 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
2066 } elsif ($sort eq 'R') {
2067 if (my $dxchan = DXChannel::get($from)) {
2068 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @$pc[6,7]);
2070 my $ref = Route::get($from);
2072 my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
2074 $_->send($line) for @dxchan;
2076 dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2079 dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2085 } elsif ($sort eq 'K') {
2086 $pc92Kin += length $line;
2088 # remember the last channel we arrived on
2089 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2091 my @ent = _decode_pc92_call($pc->[4]);
2096 ($parent, $add) = $self->pc92_handle_first_slot(\@ent, $parent, $t, $hops);
2097 return unless $parent; # dupe
2099 push @radd, $add if $add;
2101 my $call = $parent->call;
2102 my $version = $ent[4] || 0;
2103 my $build = $ent[5] || 0;
2105 my $oldbuild = $parent->build || 0;
2106 my $oldversion = $parent->version || 0;
2107 my $user = check_add_user($parent->call, 'S');
2108 my $oldsort = $user->sort // '';
2110 dbg("PCPROT: PC92 K v: $version ov: $oldversion b: $build ob: $oldbuild pk: " . ($parent->K || '0') . " uk: " . ($user->K || 0)) if isdbg('pc92k');
2112 if (is_numeric($version) || is_numeric($build)) {
2114 if (($oldversion ne $version || $build ne $oldbuild)) {
2115 dbg("PCPROT: PC92 K node $call updated version: $version (was $oldversion) build: $build (was $oldbuild)");
2116 $user->version($parent->version($version));
2117 $user->build($parent->build($build));
2120 if ($oldsort ne 'S') {
2121 dbg("PCPROT: PC92 K node $call updated sort: $sort (was $oldsort)");
2126 dbg("PCPROT: PC92 K node $call updated - marked as PC92 K user");
2130 $user->put if $changed;
2131 $parent->K(1); # mark this as come in on a K
2133 dbg("DXProt PC92 K version call $call: invalid version: '$version' or build: '$version', ignored");
2135 dbg("ROUTE: reset obscount on $call now " . $parent->obscount) if isdbg('obscount');
2137 } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
2139 $pc92Ain += length $line if $sort eq 'A';
2140 $pc92Cin += length $line if $sort eq 'C';
2141 $pc92Din += length $line if $sort eq 'D';
2143 # remember the last channel we arrived on
2144 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2146 # this is the main route section
2147 # here is where all the routes are created and destroyed
2149 # cope with missing duplicate node calls in the first slot
2150 my $me = $pc->[4] || '';
2151 $me ||= _encode_pc92_call($parent) unless $me ;
2153 my @ent = map {my @a = _decode_pc92_call($_); @a ? \@a : ()} grep {$_ && /^[0-7]/} $me, @$pc[5 .. $#$pc];
2157 # look at the first one which will always be a node of some sort
2158 # except in the case of 'A' or 'D' in which the $pcall is used
2159 # otherwise use the node call and update any information
2160 # that needs to be done.
2163 ($parent, $add) = $self->pc92_handle_first_slot($ent[0], $parent, $t, $hops);
2164 return unless $parent; # dupe
2167 push @radd, $add if $add;
2170 # do a pass through removing any references to either mycall
2174 next unless $_ && @$_;
2175 if ($_->[0] eq $main::mycall) {
2176 dbg("PCPROT: $self->{call} : type $sort $_->[0] refers to me, ignored") if isdbg('route');
2179 if ($_->[0] eq $main::myalias && $_->[1] || $_->[0] eq $main::mycall && $_->[1] == 0) {
2180 LogDbg('err',"PCPROT: $self->{call} : type $sort $_->[0] trying to change type to " . $_->[1]?"Node":"User" . ", ignored");
2189 push @radd, _add_thingy($parent, $_, $self, $hops);
2191 } elsif ($sort eq 'D') {
2193 push @rdel, _del_thingy($parent, $_, $self);
2195 } elsif ($sort eq 'C') {
2196 my (@nodes, @users);
2198 # we reset obscounts on config records as well as K records
2200 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('obscount');
2203 foreach my $r (@nent) {
2204 # my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);
2207 push @nodes, $r->[0];
2209 push @users, $r->[0];
2212 dbg("PCPROT: $self->{call} : pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr') || isdbg('route');
2216 my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
2219 foreach my $r (@nent) {
2222 push @radd,_add_thingy($parent, $r, $self, $hops) if grep $call eq $_, (@$nnodes, @$nusers);
2226 foreach my $r (@$dnodes) {
2227 push @rdel,_del_thingy($parent, [$r, 1], $self);
2229 foreach my $r (@$dusers) {
2230 push @rdel,_del_thingy($parent, [$r, 0], $self);
2233 # remember this last PC92C for rebroadcast on demand
2234 $parent->last_PC92C($line);
2236 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
2240 foreach my $r (@rdel) {
2243 $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
2244 $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
2246 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
2247 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
2248 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
2249 $self->route_pc19($pcall, undef, @pc19) if @pc19;
2250 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
2253 # broadcast it if we get here
2254 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2257 # get all the routes for a thing, bearing in mind that the thing (e.g. a user)
2258 # might be on two or more nodes at the same time or that there may be more than
2259 # one equal distance neighbour to a node.
2261 # What this means that if sh/route g1tlh shows that he is on (say) two nodes, then
2262 # a Route::findroutes is done on each of those two nodes, the best route(s) taken from
2263 # each and then combined to give a set of dxchans to send the PC9x record down
2265 sub find_pc9x_routes
2268 my $ref = Route::get($to);
2272 if ($ref->isa('Route::User')) {
2273 my $dxchan = DXChannel::get($to);
2274 push @parent, $to if $dxchan;
2275 push @parent, $ref->parents;
2279 foreach my $p (@parent) {
2281 my @routes = Route::findroutes($p);
2282 foreach my $r (@routes) {
2283 $lasthops = $r->[0] unless defined $lasthops;
2284 if ($r->[0] == $lasthops) {
2285 $cand{$r->[1]->call} = $r->[1];
2291 return values %cand;
2302 # $self->{do_pc9x} ||= 1;
2304 my $pcall = $pc->[1]; # this is now checked earlier
2306 # remember that we are converting PC10->PC93 and self will be $main::me if it
2308 unless ($self->{do_pc9x}) {
2309 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
2314 my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
2316 my $to = uc $pc->[3];
2317 my $from = uc $pc->[4];
2318 my $via = uc $pc->[5];
2319 my $text = $pc->[6];
2320 my $onode = uc $pc->[7];
2321 $onode = $pcall if @$pc <= 8;
2323 # this is catch loops caused by bad software ...
2324 if (eph_dup("PC93|$from|$text|$onode", $pc10_dupe_age)) {
2328 if (isdbg('progress')) {
2329 my $vs = $via ne '*' ? " via $via" : '';
2330 my $s = "ANNTALK: $from\@$onode$vs -> $to '$text' route: $origin";
2334 # if this is a 'bad spotter' user then ignore it
2336 $nossid =~ s/-\d+$//;
2337 if ($badspotter->in($nossid)) {
2338 dbg($line) if isdbg('nologchan');
2339 dbg("PCPROT: Bad Spotter, dropped");
2343 # ignore PC93 coming in from outside this node with a target of local
2344 if ($to eq 'LOCAL' && $self != $main::me) {
2345 dbg("PCPROT: incoming LOCAL chat not from local node, ignored") if isdbg('chanerr');
2349 # will we allow it at all?
2352 if (@bad = BadWords::check($text)) {
2353 my $bw = join ', ', @bad;
2354 dbg($line) if isdbg('nologchan');
2355 dbg("PCPROT: Badwords: '$bw', dropped");
2360 # if it is routeable then then treat it like a talk
2361 my $ref = Route::get($to);
2365 # convert to PC10 or local talks where appropriate
2366 # PC93 capable nodes of the same hop count all get a copy
2367 # if there is a PC10 node then it will get a copy and that
2368 # will be it. Hopefully such a node will not figure highly
2369 # in the route list, unless it is local, 'cos it don't issue PC92s!
2370 # note that both local and PC93s at the same time are possible if the
2371 # user on more than one node.
2372 my @routes = find_pc9x_routes($to);
2374 foreach $dxchan (@routes) {
2375 if (ref $dxchan && $dxchan->isa('DXChannel')) {
2376 if ($dxchan->{do_pc9x}) {
2377 $dxchan->send($line);
2379 $dxchan->talk($from, $to, $via, $text, $onode);
2382 dbg("ERROR: $to -> $dxchan is not a DXChannel! (convert to pc10)");
2387 } elsif ($to eq '*' || $to eq 'SYSOP' || $to eq 'WX') {
2389 my $sysop = $to eq 'SYSOP' ? '*' : ' ';
2390 my $wx = $to eq 'WX' ? '1' : '0';
2391 my $local = $via eq 'LOCAL' ? '*' : $via;
2393 $self->send_announce(1, pc12($from, $text, $local, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
2394 return if $via eq 'LOCAL';
2395 } elsif (!is_callsign($to) && $text =~ /^#\d+ /) {
2396 # chat messages really only locally connected users
2397 $self->send_chat(1, $line, $from, '*', $text, $to, $pcall, '0');
2400 # broadcast this chat sentence everywhere unless it is targetted to 'LOCAL'
2401 $self->broadcast_route_pc9x($pcall, undef, $line, 0) unless $to eq 'LOCAL' || $via eq 'LOCAL';
2404 # if get here then rebroadcast the thing with its Hop count decremented (if
2405 # there is one). If it has a hop count and it decrements to zero then don't
2408 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
2420 unless (eph_dup($line)) {
2422 my $pcall = $pc->[1];
2423 unless (is_callsign($pcall)) {
2424 dbg("PCPROT: invalid callsign string '$pc->[1]', ignored") if isdbg('chanerr');
2428 my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
2429 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2431 unless ($self->{isolate}) {
2432 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me