3 # This module impliments the handlers for the protocal mode for a dx cluster
5 # Copyright (c) 1998-2006 Dirk Koopman G1TLH
40 use vars qw($VERSION $BRANCH);
41 ($VERSION, $BRANCH) = dxver(q$Revision$);
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 $rspfcheck
48 $allowzero $decode_dk0wcy $send_opernam @checklist
49 $eph_pc15_restime $pc9x_past_age $pc9x_future_age
52 $pc9x_past_age = 15*60; # maximum age in the past of a px9x
53 $pc9x_future_age = 5*60; # maximum age in the future ditto
55 # incoming talk commands
64 return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
66 # will we allow it at all?
69 if (@bad = BadWords::check($_[3])) {
70 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
75 # is it for me or one of mine?
76 my ($from, $to, $via, $call, $dxchan);
85 # if this is a 'nodx' node then ignore it
86 if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
87 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
91 # if this is a 'bad spotter' user then ignore it
94 if ($badspotter->in($nossid)) {
95 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
99 # if we are converting announces to talk is it a dup?
101 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
102 dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
107 # remember a route to this node and also the node on which this user is
108 RouteDB::update($_[6], $self->{call});
109 # RouteDB::update($to, $_[6]);
111 # convert this to a PC93 and process it as such
112 $self->normal(pc93($to, $from, $via, $_[3], $_[6]));
115 # this is all redundant but kept for now for reference
117 # it is here and logged on
118 $dxchan = DXChannel::get($main::myalias) if $to eq $main::mycall;
119 $dxchan = DXChannel::get($to) unless $dxchan;
120 if ($dxchan && $dxchan->is_user) {
122 $dxchan->talk($from, $to, $via, $_[3]);
126 # is it elsewhere, visible on the cluster via the to address?
127 # note: this discards the via unless the to address is on
130 if ($ref = Route::get($to)) {
131 $vref = Route::Node::get($via) if $via;
132 $vref = undef unless $vref && grep $to eq $_, $vref->users;
133 $ref->dxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
137 # can we see an interface to send it down?
139 # not visible here, send a message of condolence
141 $ref = Route::get($from);
142 $vref = $ref = Route::Node::get($_[6]) unless $ref;
144 $dxchan = $ref->dxchan;
145 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
157 # route 'foreign' pc26s
159 if ($_[7] ne $main::mycall) {
160 $self->route($_[7], $line);
166 # return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
168 # is the spotted callsign blank? This should really be trapped earlier but it
169 # could break other protocol sentences. Also check for lower case characters.
170 if ($_[2] =~ /^\s*$/) {
171 dbg("PCPROT: blank callsign, dropped") if isdbg('chanerr');
174 if ($_[2] =~ /[a-z]/) {
175 dbg("PCPROT: lowercase characters, dropped") if isdbg('chanerr');
180 # if this is a 'nodx' node then ignore it
181 if ($badnode->in($_[7])) {
182 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
186 # if this is a 'bad spotter' user then ignore it
188 $nossid =~ s/-\d+$//;
189 if ($badspotter->in($nossid)) {
190 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
194 # convert the date to a unix date
195 my $d = cltounix($_[3], $_[4]);
196 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
197 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
198 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
203 if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
204 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
209 $_[5] =~ s/^\s+//; # take any leading blanks off
210 $_[2] = unpad($_[2]); # take off leading and trailing blanks from spotted callsign
211 if ($_[2] =~ /BUST\w*$/) {
212 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
217 if (@bad = BadWords::check($_[5])) {
218 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
224 # RouteDB::update($_[7], $self->{call});
225 # RouteDB::update($_[6], $_[7]);
227 my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $nossid, $_[7]);
228 # global spot filtering on INPUT
229 if ($self->{inspotsfilter}) {
230 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
232 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
237 # this goes after the input filtering, but before the add
238 # so that if it is input filtered, it isn't added to the dup
239 # list. This allows it to come in from a "legitimate" source
240 if (Spot::dup(@spot[0..4,5])) {
241 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
249 # @spot at this point contains:-
250 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
251 # then spotted itu, spotted cq, spotters itu, spotters cq
252 # you should be able to route on any of these
255 # fix up qra locators of known users
256 my $user = DXUser->get_current($spot[4]);
258 my $qra = $user->qra;
259 unless ($qra && is_qra($qra)) {
260 my $lat = $user->lat;
261 my $long = $user->long;
262 if (defined $lat && defined $long) {
263 $user->qra(DXBearing::lltoqra($lat, $long));
268 # send a remote command to a distant cluster if it is visible and there is no
269 # qra locator and we havn't done it for a month.
271 unless ($user->qra) {
273 my $to = $user->homenode;
274 my $last = $user->lastoper || 0;
275 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
276 my $cmd = "forward/opernam $spot[4]";
277 # send the rcmd but we aren't interested in the replies...
278 my $dxchan = $node->dxchan;
279 if ($dxchan && $dxchan->is_clx) {
280 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
282 route(undef, $to, pc34($main::mycall, $to, $cmd));
286 $node = Route::Node::get($to);
288 $dxchan = $node->dxchan;
289 if ($dxchan && $dxchan->is_clx) {
290 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
292 route(undef, $to, pc34($main::mycall, $to, $cmd));
296 $user->lastoper($main::systime);
305 $r = Local::spot($self, @spot);
307 # dbg("Local::spot1 error $@") if isdbg('local') if $@;
310 # DON'T be silly and send on PC26s!
311 return if $pcno == 26;
313 # send out the filtered spots
314 send_dx_spot($self, $line, @spot) if @spot;
325 # return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
327 # announce duplicate checking
328 $_[3] =~ s/^\s+//; # remove leading blanks
332 if (@bad = BadWords::check($_[3])) {
333 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
338 # if this is a 'nodx' node then ignore it
339 if ($badnode->in($_[5])) {
340 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
344 # if this is a 'bad spotter' user then ignore it
346 $nossid =~ s/-\d+$//;
347 if ($badspotter->in($nossid)) {
348 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
355 if ((($dxchan = DXChannel::get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
356 $self->send_chat(0, $line, @_[1..6]);
357 } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
360 # RouteDB::update($_[5], $self->{call});
361 # RouteDB::update($_[1], $_[5]);
363 # ignore something that looks like a chat line coming in with sysop
364 # flag - this is a kludge...
365 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
366 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
370 # here's a bit of fun, convert incoming ann with a callsign in the first word
371 # or one saying 'to <call>' to a talk if we can route to the recipient
373 my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
375 my $ref = Route::get($call);
377 $dxchan = $ref->dxchan;
378 $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
385 $self->send_announce(0, $line, @_[1..6]);
387 $self->route($_[2], $line);
398 if (eph_dup($line, $eph_pc15_restime)) {
399 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
401 unless ($self->{isolate}) {
402 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
418 my $newline = "PC16^";
420 # dos I want users from this channel?
421 unless ($self->user->wantpc16) {
422 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
427 if ($ncall eq $main::mycall) {
428 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
433 $h = 1 if DXChannel::get($ncall);
434 RouteDB::update($ncall, $self->{call}, $h);
435 if ($h && $self->{call} ne $ncall) {
436 dbg("PCPROT: trying to update a local node, ignored") if isdbg('chanerr');
440 if (eph_dup($line)) {
441 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
445 my $parent = Route::Node::get($ncall);
448 $dxchan = $parent->dxchan;
449 if ($dxchan && $dxchan ne $self) {
450 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
454 # input filter if required
455 return unless $self->in_filter_route($parent);
457 $parent = Route::Node->new($ncall);
461 if ($parent->via_pc92) {
462 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
469 for ($i = 2; $i < $#_; $i++) {
470 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
471 next unless $call && $conf && defined $here && is_callsign($call);
472 next if $call eq $main::mycall;
474 eph_del_regex("^PC17\\^$call\\^$ncall");
476 $conf = $conf eq '*';
478 # reject this if we think it is a node already
479 my $r = Route::Node::get($call);
480 my $u = DXUser->get_current($call) unless $r;
481 if ($r || ($u && $u->is_node)) {
482 dbg("PCPROT: $call is a node") if isdbg('chanerr');
486 $r = Route::User::get($call);
487 my $flags = Route::here($here)|Route::conf($conf);
490 my $au = $r->addparent($parent);
491 if ($r->flags != $flags) {
495 push @rout, $r if $h && $au;
497 my @ans = $parent->add_user($call, $flags);
498 push @rout, @ans if $h && @ans;
501 # send info to all logged in thingies
502 $self->tell_login('loginu', "$ncall: $call") if DXUser->get_current($ncall)->is_local_node;
503 $self->tell_buddies('loginb', $call, $ncall);
505 # add this station to the user database, if required
506 # $call =~ s/-\d+$//o; # remove ssid for users
507 my $user = DXUser->get_current($call);
508 $user = DXUser->new($call) if !$user;
509 $user->homenode($parent->call) if !$user->homenode;
510 $user->node($parent->call);
511 $user->lastin($main::systime) unless DXChannel::get($call);
515 $self->route_pc16($origin, $line, $parent, @rout) if @rout;
516 # $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
531 eph_del_regex("^PC16\\^$ncall.*$ucall");
533 # do I want users from this channel?
534 unless ($self->user->wantpc16) {
535 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
539 if ($ncall eq $main::mycall) {
540 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
544 RouteDB::delete($ncall, $self->{call});
546 my $uref = Route::User::get($ucall);
548 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
551 my $parent = Route::Node::get($ncall);
553 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
557 $dxchan = DXChannel::get($ncall);
558 if ($dxchan && $dxchan ne $self) {
559 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
564 if ($parent->via_pc92) {
565 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
570 if (DXChannel::get($ucall)) {
571 dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
575 # input filter if required and then remove user if present
576 # return unless $self->in_filter_route($parent);
577 $parent->del_user($uref);
579 # send info to all logged in thingies
580 $self->tell_login('logoutu', "$ncall: $ucall") if DXUser->get_current($ncall)->is_local_node;
581 $self->tell_buddies('logoutb', $ucall, $ncall);
583 if (eph_dup($line)) {
584 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
588 $self->route_pc17($origin, $line, $parent, $uref);
589 # $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
599 $self->state('init');
601 # record the type and version offered
602 if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
603 $self->version(53 + $1);
604 $self->user->version(53 + $1);
605 $self->build(0 + $2);
606 $self->user->build(0 + $2);
607 unless ($self->is_spider) {
608 $self->user->sort('S');
612 $self->{handle_xml}++ if DXXml::available() && $_[1] =~ /\bxml/;
613 if ($_[1] =~ /\bpc9x/) {
614 $self->{do_pc9x} = 1;
615 dbg("Do px9x set on $self->{call}");
618 $self->version(50.0);
619 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
620 $self->user->version($self->version);
623 # first clear out any nodes on this dxchannel
624 my $parent = Route::Node::get($self->{call});
625 my @rout = $parent->del_nodes;
626 $self->route_pc21($origin, $line, @rout, $parent) if @rout;
627 $self->send_local_config();
635 # add this station to the user database, if required (don't remove SSID from nodes)
636 my $user = DXUser->get_current($call);
638 $user = DXUser->new($call);
639 $user->priv(1); # I have relented and defaulted nodes
641 $user->homenode($call);
644 $user->sort('A') unless $user->is_node;
648 # incoming cluster list
657 my $newline = "PC19^";
660 my (@rout, @pc92out);
662 # first get the INTERFACE node
663 my $parent = Route::Node::get($self->{call});
665 dbg("DXPROT: my parent $self->{call} has disappeared");
674 # We are making a major change from now on. We are only going to accept
675 # PC19s from directly connected nodes. This means that we are probably
676 # going to throw away most of the data that we are being sent.
678 # The justification for this is that most of it is wrong or out of date
681 # From now on we are only going to believe PC92 data and locally connected
684 for ($i = 1; $i < $#_-1; $i += 4) {
686 my $call = uc $_[$i+1];
689 next unless defined $here && defined $conf && is_callsign($call);
691 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
693 # check for sane parameters
694 # $ver = 5000 if $ver eq '0000';
695 next unless $ver && $ver =~ /^\d+$/;
696 next if $ver < 5000; # only works with version 5 software
697 next if length $call < 3; # min 3 letter callsigns
698 next if $call eq $main::mycall;
700 # check that this PC19 isn't trying to alter the wrong dxchan
702 my $dxchan = DXChannel::get($call);
704 if ($dxchan == $self) {
707 dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
712 my $user = check_add_node($call);
714 # if (eph_dup($genline)) {
715 # dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
719 RouteDB::update($call, $self->{call}, $dxchan ? 1 : undef);
722 if ($parent->via_pc92) {
723 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
728 my $r = Route::Node::get($call);
729 my $flags = Route::here($here)|Route::conf($conf);
731 # modify the routing table if it is in it, otherwise store it in the pc19list for now
734 if ($call ne $parent->call) {
735 if ($self->in_filter_route($r)) {
736 $ar = $parent->add($call, $ver, $flags);
737 # push @rout, $ar if $ar;
742 if ($r->version ne $ver || $r->flags != $flags) {
748 if ($call eq $self->{call} || $user->wantroutepc19) {
749 my $new = Route->new($call); # throw away
750 if ($self->in_filter_route($new)) {
751 my $ar = $parent->add($call, $ver, $flags);
752 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
753 push @rout, $ar if $ar;
754 push @pc92out, $r if $h;
761 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
762 my $mref = DXMsg::get_busy($call);
763 $mref->stop_msg($call) if $mref;
765 $user->lastin($main::systime) unless DXChannel::get($call);
769 # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
770 # but remember there will only be one (pair) these because any extras will be
773 # $self->route_pc21($self->{call}, $line, @rout);
774 $self->route_pc19($self->{call}, $line, @rout);
777 $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
781 sub send_delayed_pc92
785 # send out new PC92 config to everyone else
786 my $line = gen_my_pc92_config($main::me);
787 $self->broadcast_route_pc9x($main::mycall, undef, $line, 0);
789 # if this is an external node then send out the external config
790 unless ($self->{do_pc9x}) {
791 $line = gen_my_pc92_config(Route::Node::get($self->{call}));
792 $self->broadcast_route_pc9x($main::mycall, undef, $line, 0);
796 # send local configuration
804 if ($self->{do_pc9x} && $self->{state} ne 'init92') {
805 dbg("PCPROT: disconnecting because login call not sent in any pc92") if isdbg('chanerr');
806 $self->send("**** You logged in with $self->{call} but that is NOT your \$mycall");
810 $self->send_local_config();
812 $self->state('normal');
813 $self->{lastping} = 0;
814 $self->send_delayed_pc92;
817 # delete a cluster from the list
819 # This should never occur for directly connected nodes.
829 eph_del_regex("^PC1[679].*$call");
831 # if I get a PC21 from the same callsign as self then ignore it
832 if ($call eq $self->call) {
833 dbg("PCPROT: self referencing PC21 from $self->{call}");
837 RouteDB::delete($call, $self->{call});
839 my $parent = Route::Node::get($self->{call});
841 dbg("PCPROT: my parent $self->{call} has disappeared");
848 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
849 my $node = Route::Node::get($call);
852 if ($node->via_pc92) {
853 dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
857 my $dxchan = DXChannel::get($call);
858 if ($dxchan && $dxchan != $self) {
859 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
864 return unless $self->in_filter_route($node);
866 # routing objects, force a PC21 if it is local
867 push @rout, $node->del($parent);
868 push @rout, $call if $dxchan && @rout == 0;
871 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
875 if (eph_dup($line)) {
876 dbg("PCPROT: dup PC21 detected") if isdbg('chanerr');
881 $self->route_pc21($origin, $line, @rout);
882 # $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
894 if ($self->{do_pc9x}) {
895 if ($self->{state} ne 'init92') {
896 dbg("PCPROT: disconnecting because login call not sent in any pc92") if isdbg('chanerr');
897 $self->send("**** You logged in with $self->{call} but that is NOT your \$mycall");
902 $self->{lastping} = 0;
903 $self->state('normal');
904 $self->send_delayed_pc92;
915 # route foreign' pc27s
917 if ($_[8] ne $main::mycall) {
918 $self->route($_[8], $line);
923 # only do a rspf check on PC23 (not 27)
925 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
929 my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
930 my $sfi = unpad($_[3]);
931 my $k = unpad($_[4]);
932 my $i = unpad($_[5]);
933 my ($r) = $_[6] =~ /R=(\d+)/;
935 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
936 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
940 # global wwv filtering on INPUT
941 my @dxcc = ((Prefix::cty_data($_[7]))[0..2], (Prefix::cty_data($_[8]))[0..2]);
942 if ($self->{inwwvfilter}) {
943 my ($filter, $hops) = $self->{inwwvfilter}->it(@_[7,8], $origin, @dxcc);
945 dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
949 $_[7] =~ s/-\d+$//o; # remove spotter's ssid
950 if (Geomag::dup($d,$sfi,$k,$i,$_[6],$_[7])) {
951 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
955 # note this only takes the first one it gets
956 Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
960 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
962 # dbg("Local::wwv2 error $@") if isdbg('local') if $@;
965 # DON'T be silly and send on PC27s!
966 return if $pcno == 27;
968 # broadcast to the eager world
969 send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
981 $nref = Route::Node::get($call);
982 $uref = Route::User::get($call);
983 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
985 if (eph_dup($line)) {
986 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
990 $nref->here($_[2]) if $nref;
991 $uref->here($_[2]) if $uref;
992 my $ref = $nref || $uref;
993 return unless $self->in_filter_route($ref);
995 $self->route_pc24($origin, $line, $ref, $_[3]);
1005 if ($_[1] ne $main::mycall) {
1006 $self->route($_[1], $line);
1009 if ($_[2] eq $main::mycall) {
1010 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1014 Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1018 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1021 $self->send(pc26(@{$in}[0..4], $_[2]));
1027 my @in = reverse Geomag::search(0, $_[4], time, 1);
1030 $self->send(pc27(@{$in}[0..5], $_[2]));
1035 sub handle_26 {goto &handle_11}
1036 sub handle_27 {goto &handle_23}
1038 # mail/file handling
1045 if ($_[1] eq $main::mycall) {
1047 my $sub = "DXMsg::handle_$pcno";
1050 $self->route($_[1], $line) unless $self->is_clx;
1054 sub handle_29 {goto &handle_28}
1055 sub handle_30 {goto &handle_28}
1056 sub handle_31 {goto &handle_28}
1057 sub handle_32 {goto &handle_28}
1058 sub handle_33 {goto &handle_28}
1066 if (eph_dup($line, $eph_pc34_restime)) {
1067 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1069 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1073 # remote command replies
1080 eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1081 $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1084 sub handle_36 {goto &handle_34}
1093 if ($_[1] eq $main::mycall) {
1095 my $sub = "DXDb::handle_$pcno";
1098 $self->route($_[1], $line) unless $self->is_clx;
1102 # node connected list from neighbour
1111 # incoming disconnect
1118 if ($_[1] eq $self->{call}) {
1119 $self->disconnect(1);
1121 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1125 sub handle_40 {goto &handle_28}
1138 my $l = "PC41^$call^$sort";
1139 if (eph_dup($l, $eph_info_restime)) {
1140 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1144 # input filter if required
1145 # my $ref = Route::get($call) || Route->new($call);
1146 # return unless $self->in_filter_route($ref);
1148 if ($val eq $sort || $val =~ /^\s*$/) {
1149 dbg('PCPROT: invalid value') if isdbg('chanerr');
1153 # add this station to the user database, if required
1154 my $user = DXUser->get_current($call);
1155 $user = DXUser->new($call) unless $user;
1158 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1159 dbg("PCPROT: invalid name") if isdbg('chanerr');
1160 if ($main::mycall eq 'GB7DJK' || $main::mycall eq 'GB7BAA' || $main::mycall eq 'WR3D') {
1161 DXChannel::broadcast_nodes(pc41($_[1], 1, $user->name)); # send it to everyone including me
1166 } elsif ($sort == 2) {
1168 } elsif ($sort == 3) {
1169 if (is_latlong($val)) {
1170 my ($lat, $long) = DXBearing::stoll($val);
1171 $user->lat($lat) if $lat;
1172 $user->long($long) if $long;
1173 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1175 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1178 } elsif ($sort == 4) {
1179 $user->homenode($val);
1180 } elsif ($sort == 5) {
1181 if (is_qra(uc $val)) {
1182 my ($lat, $long) = DXBearing::qratoll(uc $val);
1183 $user->lat($lat) if $lat && !$user->lat;
1184 $user->long($long) if $long && !$user->long;
1185 $user->qra(uc $val);
1187 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1191 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1194 unless ($self->{isolate}) {
1195 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1198 # perhaps this IS what we want after all
1199 # $self->route_pc41($ref, $call, $sort, $val, $_[4]);
1202 sub handle_42 {goto &handle_28}
1206 sub handle_44 {goto &handle_37}
1207 sub handle_45 {goto &handle_37}
1208 sub handle_46 {goto &handle_37}
1209 sub handle_47 {goto &handle_37}
1210 sub handle_48 {goto &handle_37}
1212 # message and database
1220 if (eph_dup($line)) {
1221 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1225 if ($_[1] eq $main::mycall) {
1226 DXMsg::handle_49($self, @_);
1228 $self->route($_[1], $line) unless $self->is_clx;
1232 # keep alive/user list
1242 RouteDB::update($call, $self->{call});
1244 my $node = Route::Node::get($call);
1246 return unless $node->call eq $self->{call};
1247 $node->usercount($_[2]);
1249 # input filter if required
1250 return unless $self->in_filter_route($node);
1252 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1256 # incoming ping requests/answers
1269 if ($to eq $main::mycall) {
1271 $self->send(pc51($from, $to, '0'));
1273 DXXml::Ping::handle_ping_reply($self, $from);
1277 RouteDB::update($from, $self->{call});
1279 if (eph_dup($line)) {
1280 dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1283 # route down an appropriate thingy
1284 $self->route($to, $line);
1288 # dunno but route it
1296 if ($call ne $main::mycall) {
1297 $self->route($call, $line);
1311 my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1312 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1313 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1316 @_ = map { unpad($_) } @_;
1318 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1322 my $wcy = WCY::update($d, @_[2..12]);
1326 $rep = Local::wcy($self, @_[1..12]);
1328 # dbg("Local::wcy error $@") if isdbg('local') if $@;
1331 # broadcast to the eager world
1332 send_wcy_spot($self, $line, $d, @_[2..12]);
1335 # remote commands (incoming)
1342 $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1345 # remote command replies
1352 $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1355 # decode a pc92 call: flag call : version : build
1356 sub _decode_pc92_call
1359 my @part = split /:/, $icall;
1360 my ($flag, $call) = unpack "A A*", $part[0];
1361 return () unless defined $flag && $flag ge '0' && $flag le '7';
1362 return () unless $call && is_callsign($call);
1363 my $is_node = $flag & 4;
1364 my $is_extnode = $flag & 2;
1365 my $here = $flag & 1;
1366 return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
1369 # decode a pc92 call: flag call : version : build
1370 sub _encode_pc92_call
1374 # plain call or value
1375 return $ref unless ref $ref;
1379 my $call = $ref->call;
1381 $flag |= $ref->here ? 1 : 0;
1382 if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1384 my $dxchan = DXChannel::get($call);
1385 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1387 if ($ref->version) {
1388 my $version = $ref->version || 1.0;
1389 $version = $version * 100 + 5300 if $version < 50;
1390 $extra .= ":" . $version;
1394 return "$flag$call$extra";
1401 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1406 dbg("ROUTE: added node $call to " . $parent->call) if isdbg('routelow');
1407 @rout = $parent->add($call, $version, Route::here($here));
1409 dbg("ROUTE: added user $call to " . $parent->call) if isdbg('routelow');
1410 @rout = $parent->add_user($call, Route::here($here));
1420 my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1424 my $nref = Route::Node::get($call);
1425 dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1426 @rout = $nref->del($parent) if $nref;
1428 my $uref = Route::User::get($call);
1429 dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1430 @rout = $parent->del_user($uref) if $uref;
1441 if (!$_last_time || $_last_time != $main::systime) {
1442 $_last_time = $main::systime;
1444 return $_last_time - $main::systime_daystart;
1447 return sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1458 my $parent = ref $call ? $call : Route::Node::get($call);
1460 # we only do this for external calls whose routing table
1461 # record come and go. The reference for mycall is permanent
1462 # and not that frequently used, it also never times out, so
1463 # the id on it is completely unreliable. Besides, only commands
1464 # originating on this box will go through this code...
1465 if ($parent->call ne $main::mycall) {
1466 my $lastid = $parent->lastid->{$pc} || 0;
1468 if (my $d = $lastid-86400+$t > $pc9x_past_age) {
1469 dbg("PCPROT: $call id $t <= $lastid, ignored") if isdbg('chanerr');
1473 if ($lastid == $t) {
1474 dbg("PCPROT: dup id on $call = $lastid, ignored") if isdbg('chanerr');
1479 $parent = Route::Node->new($call);
1481 $parent->lastid->{$pc} = $t;
1486 # DXSpider routing entries
1498 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1504 if ($pcall eq $main::mycall) {
1505 dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
1509 if ($pcall eq $self->{call} && $self->{state} eq 'init') {
1510 $self->state('init92');
1511 $self->{do_pc9x} = 1;
1512 dbg("Do pc9x set on $pcall");
1514 unless ($self->{do_pc9x}) {
1515 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
1519 my $parent = check_pc9x_t($pcall, $t, 92, 1) || return;
1520 my $oparent = $parent;
1522 $parent->do_pc9x(1);
1523 $parent->via_pc92(1);
1525 if ($sort eq 'F' || $sort eq 'R') {
1527 # this is the route finding section
1528 # here is where the consequences of the 'find' command
1538 if ($ref = DXChannel::get($target)) {
1539 $flag = 1; # we are directly connected
1541 $ref = Route::get($target);
1542 $dxchan = $ref->dxchan;
1545 if ($ref && $flag && $dxchan) {
1546 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
1549 } elsif ($sort eq 'R') {
1550 if (my $dxchan = DXChannel::get($from)) {
1551 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @_[6,7]);
1553 my $ref = Route::get($from);
1555 my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
1557 $_->send($line) for @dxchan;
1559 dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1562 dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1567 } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
1569 # this is the main route section
1570 # here is where all the routes are created and destroyed
1572 my @ent = map {[ _decode_pc92_call($_) ]} grep {$_ && /^[0-7]/} @_[4 .. $#_];
1575 # look at the first one which will always be a node of some sort
1576 # and update any information that needs to be done.
1577 my ($call, $is_node, $is_extnode, $here, $version, $build) = @{$ent[0]};
1578 if ($call && $is_node) {
1579 if ($call eq $main::mycall) {
1580 dbg("PCPROT: looped back on node entry, ignored") if isdbg('chanerr');
1584 # this is only accepted from my "self"
1585 if (DXChannel::get($call) && $call ne $self->{call}) {
1586 dbg("PCPROT: locally connected node config for $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1589 # reparent to external node (note that we must have received a 'C' or 'A' record
1590 # from the true parent node for this external before we get one for the this node
1591 unless ($parent = Route::Node::get($call)) {
1592 dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1595 $parent = check_pc9x_t($call, $t, 92) || return;
1596 $parent->via_pc92(1);
1599 dbg("PCPROT: must be mycall or external node as first entry, ignored") if isdbg('chanerr');
1602 $parent->here(Route::here($here));
1603 $parent->version($version) if $version && $version > $parent->version;
1604 $parent->build($build) if $build && $build > $parent->build;
1608 # do a pass through removing any references to either locally connected nodes or mycall
1612 if ($_->[0] eq $main::mycall || DXChannel::get($_->[0])) {
1613 dbg("PCPROT: $_->[0] refers to locally connected node, ignored") if isdbg('chanerr');
1621 push @radd, _add_thingy($parent, $_);
1623 } elsif ($sort eq 'D') {
1625 push @rdel, _del_thingy($parent, $_);
1627 } elsif ($sort eq 'C') {
1628 my (@nodes, @users);
1630 # we only reset obscounts on config records
1631 $oparent->reset_obs;
1632 dbg("ROUTE: reset obscount on $pcall now " . $oparent->obscount) if isdbg('route');
1633 if ($oparent != $parent) {
1635 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('route');
1639 foreach my $r (@nent) {
1640 # my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);
1643 push @nodes, $r->[0];
1645 push @users, $r->[0];
1648 dbg("DXPROT: pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr');
1652 my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
1655 foreach my $r (@nent) {
1658 push @radd,_add_thingy($parent, $r) if grep $call eq $_, (@$nnodes, @$nusers);
1662 foreach my $r (@$dnodes) {
1663 push @rdel,_del_thingy($parent, [$r, 1]);
1665 foreach my $r (@$dusers) {
1666 push @rdel,_del_thingy($parent, [$r, 0]);
1669 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
1673 foreach my $r (@rdel) {
1676 $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
1677 $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
1679 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
1680 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
1681 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
1682 $self->route_pc19($pcall, undef, @pc19) if @pc19;
1683 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
1686 # broadcast it if we get here
1687 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1698 # $self->{do_pc9x} ||= 1;
1701 unless (is_callsign($pcall)) {
1702 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1706 my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
1713 $onode = $pcall if @_ <= 8;
1715 # will we allow it at all?
1718 if (@bad = BadWords::check($text)) {
1719 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
1724 # if this is a 'bad spotter' user then ignore it
1726 $nossid =~ s/-\d+$//;
1727 if ($badspotter->in($nossid)) {
1728 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
1732 if (is_callsign($to)) {
1735 $dxchan = DXChannel::get($main::myalias) if $to eq $main::mycall;
1736 $dxchan = DXChannel::get($to) unless $dxchan;
1737 if ($dxchan && $dxchan->is_user) {
1738 $dxchan->talk($from, $to, $via, $text, $onode);
1742 # convert to PC10 talks where appropriate
1743 my $ref = Route::get($to);
1745 my @dxchan = $ref->alldxchan;
1746 for $dxchan (@dxchan) {
1747 if ($dxchan->{do_pc9x}) {
1748 $dxchan->send($line);
1750 $dxchan->talk($from, $to, $via, $text, $onode);
1756 # otherwise, drop through and allow it to be broadcast
1757 } elsif ($to eq '*' || uc $to eq 'SYSOP' || uc $to eq 'WX') {
1759 my $sysop = uc $to eq 'SYSOP' ? '*' : ' ';
1760 my $wx = uc $to eq 'WX' ? '1' : '0';
1761 my $local = $via eq 'LOCAL' ? '*' : $via;
1763 $self->send_announce(1, pc12($from, $text, $local, $via, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
1764 return if $via eq 'LOCAL';
1766 # chat messages to non-pc9x nodes
1767 $self->send_chat(1, pc12($from, $text, undef, $to, undef, $pcall), $from, '*', $text, $to, $pcall, '0');
1769 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1772 # if get here then rebroadcast the thing with its Hop count decremented (if
1773 # there is one). If it has a hop count and it decrements to zero then don't
1776 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1787 if (eph_dup($line)) {
1788 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1792 unless (is_callsign($pcall)) {
1793 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1797 my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
1798 $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1800 unless ($self->{isolate}) {
1801 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me