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
51 $pc9x_dupe_age = 60; # catch loops of circular (usually) D records
52 $pc10_dupe_age = 45; # just something to catch duplicate PC10->PC93 conversions
53 $pc92_slug_changes = 60; # slug any changes going outward for this long
54 $last_pc92_slug = 0; # the last time we sent out any delayed add or del PC92s
55 $pc9x_time_tolerance = 15*60; # the time on a pc9x is allowed to be out by this amount
56 $pc9x_past_age = (122*60)+ # maximum age in the past of a px9x (a config record might be the only
57 $pc9x_time_tolerance; # thing a node might send - once an hour and we allow an extra hour for luck)
58 # this is actually the partition between "yesterday" and "today" but old.
61 # incoming talk commands
69 # this is to catch loops caused by bad software ...
70 if (eph_dup($line, $pc10_dupe_age)) {
74 # will we allow it at all?
77 if (@bad = BadWords::check($_[3])) {
78 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
83 # is it for me or one of mine?
84 my ($from, $to, $via, $call, $dxchan);
93 # if this is a 'nodx' node then ignore it
94 if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
95 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
99 # if this is a 'bad spotter' user then ignore it
101 $nossid =~ s/-\d+$//;
102 if ($badspotter->in($nossid)) {
103 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
107 # if we are converting announces to talk is it a dup?
109 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
110 dbg("PCPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
115 # remember a route to this node and also the node on which this user is
116 RouteDB::update($_[6], $self->{call});
117 # RouteDB::update($to, $_[6]);
119 # convert this to a PC93, coming from mycall with origin set and process it as such
120 $main::me->normal(pc93($to, $from, $via, $_[3], $_[6]));
131 # route 'foreign' pc26s
133 if ($_[7] ne $main::mycall) {
134 $self->route($_[7], $line);
139 # my ($hops) = $_[8] =~ /^H(\d+)/;
141 # is the spotted callsign blank? This should really be trapped earlier but it
142 # could break other protocol sentences. Also check for lower case characters.
143 if ($_[2] =~ /^\s*$/) {
144 dbg("PCPROT: blank callsign, dropped") if isdbg('chanerr');
147 if ($_[2] =~ /[a-z]/) {
148 dbg("PCPROT: lowercase characters, dropped") if isdbg('chanerr');
153 # if this is a 'nodx' node then ignore it
154 if ($badnode->in($_[7])) {
155 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
159 # if this is a 'bad spotter' user then ignore it
161 $nossid =~ s/-\d+$//;
162 if ($badspotter->in($nossid)) {
163 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
167 # convert the date to a unix date
168 my $d = cltounix($_[3], $_[4]);
169 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
170 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
171 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
176 if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
177 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
182 $_[5] =~ s/^\s+//; # take any leading blanks off
183 $_[2] = unpad($_[2]); # take off leading and trailing blanks from spotted callsign
184 if ($_[2] =~ /BUST\w*$/) {
185 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
190 if (@bad = BadWords::check($_[5])) {
191 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
197 # RouteDB::update($_[7], $self->{call});
198 # RouteDB::update($_[6], $_[7]);
200 my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $nossid, $_[7]);
201 # global spot filtering on INPUT
202 if ($self->{inspotsfilter}) {
203 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
205 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
210 # this goes after the input filtering, but before the add
211 # so that if it is input filtered, it isn't added to the dup
212 # list. This allows it to come in from a "legitimate" source
213 if (Spot::dup(@spot[0..4,5])) {
214 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
222 # @spot at this point contains:-
223 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
224 # then spotted itu, spotted cq, spotters itu, spotters cq
225 # you should be able to route on any of these
228 # fix up qra locators of known users
229 my $user = DXUser->get_current($spot[4]);
231 my $qra = $user->qra;
232 unless ($qra && is_qra($qra)) {
233 my $lat = $user->lat;
234 my $long = $user->long;
235 if (defined $lat && defined $long) {
236 $user->qra(DXBearing::lltoqra($lat, $long));
241 # send a remote command to a distant cluster if it is visible and there is no
242 # qra locator and we havn't done it for a month.
244 unless ($user->qra) {
246 my $to = $user->homenode;
247 my $last = $user->lastoper || 0;
248 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
249 my $cmd = "forward/opernam $spot[4]";
250 # send the rcmd but we aren't interested in the replies...
251 my $dxchan = $node->dxchan;
252 if ($dxchan && $dxchan->is_clx) {
253 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
255 route(undef, $to, pc34($main::mycall, $to, $cmd));
259 $node = Route::Node::get($to);
261 $dxchan = $node->dxchan;
262 if ($dxchan && $dxchan->is_clx) {
263 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
265 route(undef, $to, pc34($main::mycall, $to, $cmd));
269 $user->lastoper($main::systime);
278 $r = Local::spot($self, @spot);
280 # dbg("Local::spot1 error $@") if isdbg('local') if $@;
283 # DON'T be silly and send on PC26s!
284 return if $pcno == 26;
286 # send out the filtered spots
287 send_dx_spot($self, $line, @spot) if @spot;
298 # announce duplicate checking
299 $_[3] =~ s/^\s+//; # remove leading blanks
303 if (@bad = BadWords::check($_[3])) {
304 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
309 # if this is a 'nodx' node then ignore it
310 if ($badnode->in($_[5])) {
311 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
315 # if this is a 'bad spotter' user then ignore it
317 $nossid =~ s/-\d+$//;
318 if ($badspotter->in($nossid)) {
319 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
326 if ((($dxchan = DXChannel::get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
327 $self->send_chat(0, $line, @_[1..6]);
328 } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
331 # RouteDB::update($_[5], $self->{call});
332 # RouteDB::update($_[1], $_[5]);
334 # ignore something that looks like a chat line coming in with sysop
335 # flag - this is a kludge...
336 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
337 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
341 # here's a bit of fun, convert incoming ann with a callsign in the first word
342 # or one saying 'to <call>' to a talk if we can route to the recipient
344 my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
346 my $ref = Route::get($call);
348 $dxchan = $ref->dxchan;
349 $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
356 $self->send_announce(0, $line, @_[1..6]);
358 $self->route($_[2], $line);
369 if (eph_dup($line, $eph_pc15_restime)) {
372 unless ($self->{isolate}) {
373 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
389 my $newline = "PC16^";
391 # dos I want users from this channel?
392 unless ($self->user->wantpc16) {
393 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
398 if ($ncall eq $main::mycall) {
399 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
404 $h = 1 if DXChannel::get($ncall);
405 RouteDB::update($ncall, $self->{call}, $h);
406 if ($h && $self->{call} ne $ncall) {
407 dbg("PCPROT: trying to update a local node, ignored") if isdbg('chanerr');
411 if (eph_dup($line)) {
415 # isolate now means only accept stuff from this call only
416 if ($self->{isolate} && $ncall ne $self->{call}) {
417 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
421 my $parent = Route::Node::get($ncall);
424 $dxchan = $parent->dxchan;
425 if ($dxchan && $dxchan ne $self) {
426 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
430 # input filter if required
431 return unless $self->in_filter_route($parent);
433 $parent = Route::Node->new($ncall);
437 if ($parent->via_pc92) {
438 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
445 for ($i = 2; $i < $#_; $i++) {
446 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
447 next unless $call && $conf && defined $here && is_callsign($call);
448 next if $call eq $main::mycall;
450 eph_del_regex("^PC17\\^$call\\^$ncall");
452 $conf = $conf eq '*';
454 # reject this if we think it is a node already
455 my $r = Route::Node::get($call);
456 my $u = DXUser->get_current($call) unless $r;
457 if ($r || ($u && $u->is_node)) {
458 dbg("PCPROT: $call is a node") if isdbg('chanerr');
462 $r = Route::User::get($call);
463 my $flags = Route::here($here)|Route::conf($conf);
466 my $au = $r->addparent($parent);
467 if ($r->flags != $flags) {
471 push @rout, $r if $h && $au;
473 my @ans = $parent->add_user($call, $flags);
474 push @rout, @ans if $h && @ans;
477 # add this station to the user database, if required
478 my $user = DXUser->get_current($ncall);
479 $user = DXUser->new($call) unless $user;
480 $user->homenode($parent->call) if !$user->homenode;
481 $user->node($parent->call);
482 $user->lastin($main::systime) unless DXChannel::get($call);
485 # send info to all logged in thingies
486 $self->tell_login('loginu', "$ncall: $call") if $user->is_local_node;
487 $self->tell_buddies('loginb', $call, $ncall);
490 $self->route_pc16($origin, $line, $parent, @rout) if @rout;
491 # $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
506 eph_del_regex("^PC16\\^$ncall.*$ucall");
508 # do I want users from this channel?
509 unless ($self->user->wantpc16) {
510 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
514 if ($ncall eq $main::mycall) {
515 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
519 # isolate now means only accept stuff from this call only
520 if ($self->{isolate} && $ncall ne $self->{call}) {
521 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
525 RouteDB::delete($ncall, $self->{call});
527 my $uref = Route::User::get($ucall);
529 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
532 my $parent = Route::Node::get($ncall);
534 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
538 $dxchan = DXChannel::get($ncall);
539 if ($dxchan && $dxchan ne $self) {
540 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
545 if ($parent->via_pc92) {
546 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
551 if (DXChannel::get($ucall)) {
552 dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
556 # input filter if required and then remove user if present
557 # return unless $self->in_filter_route($parent);
558 $parent->del_user($uref);
560 # send info to all logged in thingies
561 my $user = DXUser->get_current($ncall);
562 $self->tell_login('logoutu', "$ncall: $ucall") if $user && $user->is_local_node;
563 $self->tell_buddies('logoutb', $ucall, $ncall);
565 if (eph_dup($line)) {
569 $self->route_pc17($origin, $line, $parent, $uref);
570 # $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
580 $self->state('init');
582 my $parent = Route::Node::get($self->{call});
584 # record the type and version offered
585 if (my ($version) = $_[1] =~ /DXSpider Version: (\d+\.\d+)/) {
586 $self->{version} = 53 + $version;
587 $self->user->version(53 + $version);
588 $parent->version(0 + $version);
589 my ($build) = $_[1] =~ /Build: (\d+(?:\.\d+)?)/;
590 $self->{build} = 0 + $build;
591 $self->user->build(0 + $build);
592 $parent->build(0 + $build);
593 dbg("DXSpider version $version build $build");
594 unless ($self->is_spider) {
595 dbg("Change U " . $self->user->sort . " C $self->{sort} -> S");
596 $self->user->sort('S');
600 # $self->{handle_xml}++ if DXXml::available() && $_[1] =~ /\bxml/;
602 dbg("Unknown software");
603 $self->version(50.0);
604 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
605 $self->user->version($self->version);
608 if ($_[1] =~ /\bpc9x/) {
609 if ($self->{isolate}) {
610 dbg("pc9x recognised, but $self->{call} is isolated, using old protocol");
611 } elsif (!$self->user->wantpc9x) {
612 dbg("pc9x explicitly switched off on $self->{call}, using old protocol");
614 $self->{do_pc9x} = 1;
615 dbg("Do px9x set on $self->{call}");
619 # first clear out any nodes on this dxchannel
620 my @rout = $parent->del_nodes;
621 $self->route_pc21($origin, $line, @rout, $parent) if @rout;
622 $self->send_local_config();
630 # add this station to the user database, if required (don't remove SSID from nodes)
631 my $user = DXUser->get_current($call);
633 $user = DXUser->new($call);
634 $user->priv(1); # I have relented and defaulted nodes
636 $user->homenode($call);
639 $user->sort('A') unless $user->is_node;
643 # incoming cluster list
652 my $newline = "PC19^";
655 my (@rout, @pc92out);
657 # first get the INTERFACE node
658 my $parent = Route::Node::get($self->{call});
660 dbg("PCPROT: my parent $self->{call} has disappeared");
669 # We are making a major change from now on. We are only going to accept
670 # PC19s from directly connected nodes. This means that we are probably
671 # going to throw away most of the data that we are being sent.
673 # The justification for this is that most of it is wrong or out of date
676 # From now on we are only going to believe PC92 data and locally connected
679 for ($i = 1; $i < $#_-1; $i += 4) {
681 my $call = uc $_[$i+1];
684 next unless defined $here && defined $conf && is_callsign($call);
686 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
688 # check for sane parameters
689 # $ver = 5000 if $ver eq '0000';
690 next unless $ver && $ver =~ /^\d+$/;
691 next if $ver < 5000; # only works with version 5 software
692 next if length $call < 3; # min 3 letter callsigns
693 next if $call eq $main::mycall;
695 # check that this PC19 isn't trying to alter the wrong dxchan
697 my $dxchan = DXChannel::get($call);
699 if ($dxchan == $self) {
702 dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
707 # isolate now means only accept stuff from this call only
708 if ($self->{isolate} && $call ne $self->{call}) {
709 dbg("PCPROT: $self->{call} isolated, $call ignored") if isdbg('chanerr');
713 my $user = check_add_node($call);
715 # if (eph_dup($genline)) {
716 # dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
720 RouteDB::update($call, $self->{call}, $dxchan ? 1 : undef);
723 if ($parent->via_pc92) {
724 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
729 my $r = Route::Node::get($call);
730 my $flags = Route::here($here)|Route::conf($conf);
732 # modify the routing table if it is in it, otherwise store it in the pc19list for now
735 if ($call ne $parent->call) {
736 if ($self->in_filter_route($r)) {
737 $ar = $parent->add($call, $ver, $flags);
738 # push @rout, $ar if $ar;
743 if ($r->version ne $ver || $r->flags != $flags) {
749 if ($call eq $self->{call} || $user->wantroutepc19) {
750 my $new = Route->new($call); # throw away
751 if ($self->in_filter_route($new)) {
752 my $ar = $parent->add($call, $ver, $flags);
753 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
754 push @rout, $ar if $ar;
755 push @pc92out, $r if $h;
762 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
763 my $mref = DXMsg::get_busy($call);
764 $mref->stop_msg($call) if $mref;
766 $user->lastin($main::systime) unless DXChannel::get($call);
770 # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
771 # but remember there will only be one (pair) these because any extras will be
774 # $self->route_pc21($self->{call}, $line, @rout);
775 $self->route_pc19($self->{call}, $line, @rout);
777 if (@pc92out && !$pc92_slug_changes) {
778 $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
782 # send local configuration
790 if ($self->{do_pc9x} && $self->{state} ne 'init92') {
791 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
792 $self->{do_pc9x} = 0;
794 $self->send_local_config;
796 $self->state('normal');
797 $self->{lastping} = 0;
798 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
801 # delete a cluster from the list
803 # This should never occur for directly connected nodes.
813 eph_del_regex("^PC1[679].*$call");
815 # if I get a PC21 from the same callsign as self then ignore it
816 if ($call eq $self->{call}) {
817 dbg("PCPROT: self referencing PC21 from $self->{call}");
821 # for the above reason and also because of the check for PC21s coming
822 # in for self->call from outside being ignored further down
823 # we don't need any isolation code here, because we will never
824 # act on a PC21 with self->call in it.
826 RouteDB::delete($call, $self->{call});
828 my $parent = Route::Node::get($self->{call});
830 dbg("PCPROT: my parent $self->{call} has disappeared");
837 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
838 my $node = Route::Node::get($call);
841 if ($node->via_pc92) {
842 dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
846 my $dxchan = DXChannel::get($call);
847 if ($dxchan && $dxchan != $self) {
848 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
853 return unless $self->in_filter_route($node);
855 # routing objects, force a PC21 if it is local
856 push @rout, $node->del($parent);
857 push @rout, $call if $dxchan && @rout == 0;
860 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
864 if (eph_dup($line)) {
869 $self->route_pc21($origin, $line, @rout);
870 # $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
882 if ($self->{do_pc9x}) {
883 if ($self->{state} ne 'init92') {
884 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
885 $self->{do_pc9x} = 0;
888 $self->{lastping} = 0;
889 $self->state('normal');
890 $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
901 # route foreign' pc27s
903 if ($_[8] ne $main::mycall) {
904 $self->route($_[8], $line);
911 my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
912 my $sfi = unpad($_[3]);
913 my $k = unpad($_[4]);
914 my $i = unpad($_[5]);
915 my ($r) = $_[6] =~ /R=(\d+)/;
917 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
918 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
922 # global wwv filtering on INPUT
923 my @dxcc = ((Prefix::cty_data($_[7]))[0..2], (Prefix::cty_data($_[8]))[0..2]);
924 if ($self->{inwwvfilter}) {
925 my ($filter, $hops) = $self->{inwwvfilter}->it(@_[7,8], $origin, @dxcc);
927 dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
931 $_[7] =~ s/-\d+$//o; # remove spotter's ssid
932 if (Geomag::dup($d,$sfi,$k,$i,$_[6],$_[7])) {
933 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
937 # note this only takes the first one it gets
938 Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
942 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
944 # dbg("Local::wwv2 error $@") if isdbg('local') if $@;
947 # DON'T be silly and send on PC27s!
948 return if $pcno == 27;
950 # broadcast to the eager world
951 send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
963 $nref = Route::Node::get($call);
964 $uref = Route::User::get($call);
965 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
967 if (eph_dup($line)) {
971 $nref->here($_[2]) if $nref;
972 $uref->here($_[2]) if $uref;
973 my $ref = $nref || $uref;
974 return unless $self->in_filter_route($ref);
976 $self->route_pc24($origin, $line, $ref, $_[3]);
986 if ($_[1] ne $main::mycall) {
987 $self->route($_[1], $line);
990 if ($_[2] eq $main::mycall) {
991 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
995 Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
999 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1002 $self->send(pc26(@{$in}[0..4], $_[2]));
1008 my @in = reverse Geomag::search(0, $_[4], time, 1);
1011 $self->send(pc27(@{$in}[0..5], $_[2]));
1016 sub handle_26 {goto &handle_11}
1017 sub handle_27 {goto &handle_23}
1019 # mail/file handling
1026 if ($_[1] eq $main::mycall) {
1028 my $sub = "DXMsg::handle_$pcno";
1031 $self->route($_[1], $line) unless $self->is_clx;
1035 sub handle_29 {goto &handle_28}
1036 sub handle_30 {goto &handle_28}
1037 sub handle_31 {goto &handle_28}
1038 sub handle_32 {goto &handle_28}
1039 sub handle_33 {goto &handle_28}
1047 if (eph_dup($line, $eph_pc34_restime)) {
1050 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1054 # remote command replies
1061 eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1062 $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1065 sub handle_36 {goto &handle_34}
1074 if ($_[1] eq $main::mycall) {
1076 my $sub = "DXDb::handle_$pcno";
1079 $self->route($_[1], $line) unless $self->is_clx;
1083 # node connected list from neighbour
1092 # incoming disconnect
1099 if ($_[1] eq $self->{call}) {
1100 $self->disconnect(1);
1102 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1106 sub handle_40 {goto &handle_28}
1119 my $l = "PC41^$call^$sort";
1120 if (eph_dup($l, $eph_info_restime)) {
1124 # input filter if required
1125 # my $ref = Route::get($call) || Route->new($call);
1126 # return unless $self->in_filter_route($ref);
1128 if ($val eq $sort || $val =~ /^\s*$/) {
1129 dbg('PCPROT: invalid value') if isdbg('chanerr');
1133 # add this station to the user database, if required
1134 my $user = DXUser->get_current($call);
1135 $user = DXUser->new($call) unless $user;
1138 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1139 dbg("PCPROT: invalid name") if isdbg('chanerr');
1140 if ($main::mycall eq 'GB7DJK' || $main::mycall eq 'GB7BAA' || $main::mycall eq 'WR3D') {
1141 DXChannel::broadcast_nodes(pc41($_[1], 1, $user->name)); # send it to everyone including me
1146 } elsif ($sort == 2) {
1148 } elsif ($sort == 3) {
1149 if (is_latlong($val)) {
1150 my ($lat, $long) = DXBearing::stoll($val);
1151 $user->lat($lat) if $lat;
1152 $user->long($long) if $long;
1153 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1155 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1158 } elsif ($sort == 4) {
1159 $user->homenode($val);
1160 } elsif ($sort == 5) {
1161 if (is_qra(uc $val)) {
1162 my ($lat, $long) = DXBearing::qratoll(uc $val);
1163 $user->lat($lat) if $lat && !$user->lat;
1164 $user->long($long) if $long && !$user->long;
1165 $user->qra(uc $val);
1167 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1171 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1174 unless ($self->{isolate}) {
1175 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1178 # perhaps this IS what we want after all
1179 # $self->route_pc41($ref, $call, $sort, $val, $_[4]);
1182 sub handle_42 {goto &handle_28}
1186 sub handle_44 {goto &handle_37}
1187 sub handle_45 {goto &handle_37}
1188 sub handle_46 {goto &handle_37}
1189 sub handle_47 {goto &handle_37}
1190 sub handle_48 {goto &handle_37}
1192 # message and database
1200 if (eph_dup($line)) {
1204 if ($_[1] eq $main::mycall) {
1205 DXMsg::handle_49($self, @_);
1207 $self->route($_[1], $line) unless $self->is_clx;
1211 # keep alive/user list
1219 return if (eph_dup($line));
1223 RouteDB::update($call, $self->{call});
1225 my $node = Route::Node::get($call);
1227 return unless $node->call eq $self->{call};
1228 $node->usercount($_[2]) unless $node->users;
1231 # input filter if required
1232 # return unless $self->in_filter_route($node);
1234 unless ($self->{isolate}) {
1235 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1237 # $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1241 # incoming ping requests/answers
1254 if ($to eq $main::mycall) {
1256 $self->send(pc51($from, $to, '0'));
1258 DXXml::Ping::handle_ping_reply($self, $from);
1262 RouteDB::update($from, $self->{call});
1264 if (eph_dup($line)) {
1267 # route down an appropriate thingy
1268 $self->route($to, $line);
1272 sub handle_61 { goto &handle_11; }
1274 # dunno but route it
1282 if ($call ne $main::mycall) {
1283 $self->route($call, $line);
1297 my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1298 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1299 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1302 @_ = map { unpad($_) } @_;
1304 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1308 my $wcy = WCY::update($d, @_[2..12]);
1312 $rep = Local::wcy($self, @_[1..12]);
1314 # dbg("Local::wcy error $@") if isdbg('local') if $@;
1317 # broadcast to the eager world
1318 send_wcy_spot($self, $line, $d, @_[2..12]);
1321 # remote commands (incoming)
1328 $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1331 # remote command replies
1338 $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1341 # decode a pc92 call: flag call : version : build
1342 sub _decode_pc92_call
1345 my @part = split /:/, $icall;
1346 my ($flag, $call) = unpack "A A*", $part[0];
1347 unless (defined $flag && $flag ge '0' && $flag le '7') {
1348 dbg("PCPROT: $icall no flag byte (0-7) at front of call, ignored") if isdbg('chanerr');
1351 unless ($call && is_callsign($call)) {
1352 dbg("PCPROT: $icall no recognisable callsign, ignored") if isdbg('chanerr');
1355 my $is_node = $flag & 4;
1356 my $is_extnode = $flag & 2;
1357 my $here = $flag & 1;
1358 return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
1361 # decode a pc92 call: flag call : version : build
1362 sub _encode_pc92_call
1366 # plain call or value
1367 return $ref unless ref $ref;
1371 my $call = $ref->call;
1373 $flag |= $ref->here ? 1 : 0;
1374 if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1376 my $dxchan = DXChannel::get($call);
1377 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1379 if ($ref->version) {
1380 my $version = $ref->version || 1.0;
1381 $version = $version * 100 + 5300 if $version < 50;
1382 $extra .= ":" . $version;
1386 return "$flag$call$extra";
1396 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1401 dbg("ROUTE: added node $call to " . $parent->call) if isdbg('routelow');
1402 @rout = $parent->add($call, $version, Route::here($here));
1404 dbg("ROUTE: added user $call to " . $parent->call) if isdbg('routelow');
1405 @rout = $parent->add_user($call, Route::here($here));
1407 if ($pc92_slug_changes && $parent == $main::routeroot) {
1408 $things_add{$call} = Route::get($call);
1409 delete $things_del{$call};
1419 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1424 $ref = Route::Node::get($call);
1425 dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1426 @rout = $ref->del($parent) if $ref;
1428 $ref = Route::User::get($call);
1429 dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1430 @rout = $parent->del_user($ref) if $ref;
1432 if ($pc92_slug_changes && $parent == $main::routeroot) {
1433 $things_del{$call} = $ref unless exists $things_add{$call};
1434 delete $things_add{$call};
1440 # this will only happen if we are slugging changes and
1441 # there are some changes to be sent, it will create an add or a delete
1443 sub gen_pc92_changes
1445 my @add = values %things_add;
1446 my @del = values %things_del;
1447 return (\@add, \@del);
1450 sub clear_pc92_changes
1452 %things_add = %things_del = ();
1453 $last_pc92_slug = $main::systime;
1462 return $_last_pc9x_id;
1467 if (!$_last_time || $_last_time != $main::systime) {
1468 $_last_time = $main::systime;
1470 return $_last_pc9x_id = $_last_time - $main::systime_daystart;
1473 return $_last_pc9x_id = sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1484 # check that the time is between 0 >= $t < 86400
1485 unless ($t >= 0 && $t < 86400) {
1486 dbg("PCPROT: time invalid t: $t, ignored") if isdbg('chanerr');
1490 # check that the time of this pc9x is within tolerance (default 15 mins either way)
1491 my $now = $main::systime - $main::systime_daystart ;
1492 my $diff = abs($now - $t);
1493 unless ($diff < $pc9x_time_tolerance || 86400 - $diff < $pc9x_time_tolerance) {
1494 my $c = ref $call ? $call->call : $call;
1495 dbg("PC9XERR: $c time out of range t: $t now: $now diff: $diff > $pc9x_time_tolerance, ignored") if isdbg('chan');
1499 my $parent = ref $call ? $call : Route::Node::get($call);
1501 # we only do this for external calls whose routing table
1502 # record come and go. The reference for mycall is permanent
1503 # and not that frequently used, it also never times out, so
1504 # the id on it is completely unreliable. Besides, only commands
1505 # originating on this box will go through this code...
1506 if ($parent->call ne $main::mycall) {
1507 my $lastid = $parent->lastid;
1508 if (defined $lastid) {
1510 # note that this is where we determine whether this pc9x has come in yesterday
1511 # but is still greater (modulo 86400) than the lastid or is simply an old
1512 # duplicate sentence. To determine this we need to do some module 86400
1513 # arithmetic. High numbers mean that this is an old duplicate sentence,
1514 # low numbers that it is a new sentence.
1516 # Typically you will see yesterday being taken on $t = 84, $lastid = 86235
1517 # and old dupes with $t = 234, $lastid = 256 (which give answers 249 and
1518 # 86378 respectively in the calculation below).
1520 if ($t+86400-$lastid > $pc9x_past_age) {
1521 dbg("PCPROT: dup id on $t <= lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1524 } elsif ($t == $lastid) {
1525 dbg("PCPROT: dup id on $t == lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1531 $parent = Route::Node->new($call);
1533 dbg("PCPROT: $call does not exist, ignored") if isdbg('pc92dedupe');
1536 if (isdbg('pc92dedupe')) {
1537 my $exists = exists $parent->{lastid}; # naughty, naughty :-)
1538 my $val = $parent->{lastid};
1539 my $s = $exists ? (defined $val ? $val : 'exists/undef') : 'undef';
1540 dbg("PCPROT: $call pc92 id lastid $s -> $t");
1542 $parent->lastid($t);
1547 sub pc92_handle_first_slot
1553 my $oparent = $parent;
1557 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$slot;
1558 if ($call && $is_node) {
1559 if ($call eq $main::mycall) {
1560 dbg("PCPROT: $call looped back onto $main::mycall, ignored") if isdbg('chanerr');
1563 # this is only accepted from my "self".
1564 # this also kills configs from PC92 nodes with external PC19 nodes that are also
1565 # locally connected. Local nodes always take precedence. But we remember the lastid
1566 # to try to reduce the number of dupe PC92s for this external node.
1567 if (DXChannel::get($call) && $call ne $self->{call}) {
1568 $parent = check_pc9x_t($call, $t, 92); # this will update the lastid time
1569 dbg("PCPROT: locally connected node $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1573 # reparent to external node (note that we must have received a 'C' or 'A' record
1574 # from the true parent node for this external before we get one for the this node
1575 unless ($parent = Route::Node::get($call)) {
1576 if ($is_extnode && $oparent) {
1577 @radd = _add_thingy($oparent, $slot);
1580 dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1584 $parent = check_pc9x_t($call, $t, 92) || return;
1585 $parent->via_pc92(1);
1586 $parent->PC92C_dxchan($self->{call});
1589 dbg("PCPROT: must be \$mycall or external node as first entry, ignored") if isdbg('chanerr');
1592 $parent->here(Route::here($here));
1593 $parent->version($version || $pc19_version) if $version;
1594 $parent->build($build) if $build;
1595 $parent->PC92C_dxchan($self->{call}) unless $self->{call} eq $parent->call;
1596 return ($parent, @radd);
1599 # DXSpider routing entries
1613 # this catches loops of A/Ds
1614 # if (eph_dup($line, $pc9x_dupe_age)) {
1618 if ($pcall eq $main::mycall) {
1619 dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
1623 if ($pcall eq $self->{call} && $self->{state} eq 'init') {
1624 if ($self->{isolate}) {
1625 dbg("PC9x received, but $pcall is isolated, ignored");
1627 } elsif (!$self->user->wantpc9x) {
1628 dbg("PC9x explicitly switched off on $pcall, ignored");
1631 $self->state('init92');
1632 $self->{do_pc9x} = 1;
1633 dbg("Do pc9x set on $pcall");
1636 unless ($self->{do_pc9x}) {
1637 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
1641 # don't create routing entries for D records that don't already exist
1642 # this is what causes all those PC92 loops!
1643 my $parent = check_pc9x_t($pcall, $t, 92, $sort ne 'D') || return;
1644 my $oparent = $parent;
1646 $parent->do_pc9x(1);
1647 $parent->via_pc92(1);
1649 if ($sort eq 'F' || $sort eq 'R') {
1651 # this is the route finding section
1652 # here is where the consequences of the 'find' command
1662 if ($ref = DXChannel::get($target)) {
1663 $flag = 1; # we are directly connected
1665 $ref = Route::get($target);
1666 $dxchan = $ref->dxchan;
1669 if ($ref && $flag && $dxchan) {
1670 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
1673 } elsif ($sort eq 'R') {
1674 if (my $dxchan = DXChannel::get($from)) {
1675 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @_[6,7]);
1677 my $ref = Route::get($from);
1679 my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
1681 $_->send($line) for @dxchan;
1683 dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1686 dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1692 } elsif ($sort eq 'K') {
1693 $pc92Kin += length $line if $sort eq 'K';
1695 # remember the last channel we arrived on
1696 $parent->PC92C_dxchan($self->{call}) unless $self->{call} eq $parent->call;
1698 my @ent = _decode_pc92_call($_[4]);
1703 ($parent, $add) = $self->pc92_handle_first_slot(\@ent, $parent, $t);
1704 return unless $parent; # dupe
1706 push @radd, $add if $add;
1708 $parent->version($ent[4]) if $ent[4];
1709 $parent->build($ent[5]) if $ent[5];
1711 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('obscount');
1713 } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
1715 $pc92Ain += length $line if $sort eq 'A';
1716 $pc92Cin += length $line if $sort eq 'C';
1717 $pc92Din += length $line if $sort eq 'D';
1719 # remember the last channel we arrived on
1720 $parent->PC92C_dxchan($self->{call}) unless $self->{call} eq $parent->call;
1722 # this is the main route section
1723 # here is where all the routes are created and destroyed
1725 # cope with missing duplicate node calls in the first slot
1726 my $me = $_[4] || '';
1727 $me ||= _encode_pc92_call($parent) unless $me ;
1729 my @ent = map {my @a = _decode_pc92_call($_); @a ? \@a : ()} grep {$_ && /^[0-7]/} $me, @_[5 .. $#_];
1733 # look at the first one which will always be a node of some sort
1734 # except in the case of 'A' or 'D' in which the $pcall is used
1735 # otherwise use the node call and update any information
1736 # that needs to be done.
1739 ($parent, $add) = $self->pc92_handle_first_slot($ent[0], $parent, $t);
1740 return unless $parent; # dupe
1743 push @radd, $add if $add;
1746 # do a pass through removing any references to either locally connected nodes or mycall
1749 next unless $_ && @$_;
1750 if ($_->[0] eq $main::mycall || DXChannel::get($_->[0])) {
1751 dbg("PCPROT: $_->[0] refers to locally connected node, ignored") if isdbg('chanerr');
1759 push @radd, _add_thingy($parent, $_);
1761 } elsif ($sort eq 'D') {
1763 push @rdel, _del_thingy($parent, $_);
1765 } elsif ($sort eq 'C') {
1766 my (@nodes, @users);
1768 # we reset obscounts on config records as well as K records
1770 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('obscount');
1773 foreach my $r (@nent) {
1774 # my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);
1777 push @nodes, $r->[0];
1779 push @users, $r->[0];
1782 dbg("PCPROT: pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr');
1786 my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
1789 foreach my $r (@nent) {
1792 push @radd,_add_thingy($parent, $r) if grep $call eq $_, (@$nnodes, @$nusers);
1796 foreach my $r (@$dnodes) {
1797 push @rdel,_del_thingy($parent, [$r, 1]);
1799 foreach my $r (@$dusers) {
1800 push @rdel,_del_thingy($parent, [$r, 0]);
1803 # remember this last PC92C for rebroadcast on demand
1804 $parent->last_PC92C($line);
1806 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
1810 foreach my $r (@rdel) {
1813 $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
1814 $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
1816 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
1817 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
1818 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
1819 $self->route_pc19($pcall, undef, @pc19) if @pc19;
1820 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
1823 # broadcast it if we get here
1824 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1835 # $self->{do_pc9x} ||= 1;
1837 my $pcall = $_[1]; # this is now checked earlier
1839 # remember that we are converting PC10->PC93 and self will be $main::me if it
1841 unless ($self->{do_pc9x}) {
1842 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
1847 my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
1850 my $from = uc $_[4];
1853 my $onode = uc $_[7];
1854 $onode = $pcall if @_ <= 8;
1856 # this is catch loops caused by bad software ...
1857 if (eph_dup("PC93|$from|$text|$onode", $pc10_dupe_age)) {
1861 # will we allow it at all?
1864 if (@bad = BadWords::check($text)) {
1865 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
1870 # if this is a 'bad spotter' user then ignore it
1872 $nossid =~ s/-\d+$//;
1873 if ($badspotter->in($nossid)) {
1874 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
1878 # if it is routeable then then treat it like a talk
1879 my $ref = Route::get($to);
1883 $dxchan = DXChannel::get($main::myalias) if $to eq $main::mycall;
1884 $dxchan = DXChannel::get($to) unless $dxchan;
1887 if (ref $dxchan && $dxchan->isa('DXChannel')) {
1888 if ($dxchan->is_user) {
1889 $dxchan->talk($from, $to, $via, $text, $onode);
1893 dbg("ERROR: $to -> $dxchan is not a DXChannel! (local talk)");
1897 # convert to PC10 talks where appropriate
1898 # just go for the "best" one for now (rather than broadcast)
1899 $dxchan = $ref->dxchan;
1902 if (ref $dxchan && $dxchan->isa('DXChannel')) {
1903 if ($dxchan->{do_pc9x}) {
1904 $dxchan->send($line);
1906 $dxchan->talk($from, $to, $via, $text, $onode);
1909 dbg("ERROR: $to -> $dxchan is not a DXChannel! (convert to pc10)");
1913 } elsif ($to eq '*' || $to eq 'SYSOP' || $to eq 'WX') {
1915 my $sysop = $to eq 'SYSOP' ? '*' : ' ';
1916 my $wx = $to eq 'WX' ? '1' : '0';
1917 my $local = $via eq 'LOCAL' ? '*' : $via;
1919 $self->send_announce(1, pc12($from, $text, $local, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
1920 return if $via eq 'LOCAL';
1921 } elsif (!is_callsign($to) && $text =~ /^#\d+ /) {
1922 # chat messages to non-pc9x nodes
1923 $self->send_chat(1, pc12($from, $text, undef, $to, undef, $pcall), $from, '*', $text, $to, $pcall, '0');
1925 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1928 # if get here then rebroadcast the thing with its Hop count decremented (if
1929 # there is one). If it has a hop count and it decrements to zero then don't
1932 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1943 unless (eph_dup($line)) {
1946 unless (is_callsign($pcall)) {
1947 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1951 my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
1952 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1954 unless ($self->{isolate}) {
1955 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me