force a config update to *everyone else* on pc20/22
[spider.git] / perl / DXProtHandle.pm
index 045204db1698ba349f7d7a065d5bb8946ffd797d..fcc30cfe2255ce9b71cf9db3d935b8b50b413e6d 100644 (file)
@@ -32,18 +32,21 @@ use DXHash;
 use Route;
 use Route::Node;
 use Script;
-use Investigate;
 use RouteDB;
 
 
 use strict;
 
+use vars qw($VERSION $BRANCH);
+($VERSION, $BRANCH) = dxver(q$Revision$);
+
 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
                        $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
                        $pingint $obscount %pc19list $chatdupeage $chatimportfn
                        $investigation_int $pc19_version $myprot_version
                        %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
                        $allowzero $decode_dk0wcy $send_opernam @checklist
+                       $eph_pc15_restime
                   );
        
 # incoming talk commands
@@ -375,6 +378,22 @@ sub handle_12
                $self->route($_[2], $line);
        }
 }
+
+sub handle_15
+{
+       my $self = shift;
+       my $pcno = shift;
+       my $line = shift;
+       my $origin = shift;
+
+       if (eph_dup($line, $eph_pc15_restime)) {
+               dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
+       } else {
+               unless ($self->{isolate}) {
+                       DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
+               }
+       }
+}
                
 # incoming user                
 sub handle_16
@@ -394,6 +413,7 @@ sub handle_16
                dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
                return;
        }
+
        # is it me?
        if ($ncall eq $main::mycall) {
                dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
@@ -409,11 +429,6 @@ sub handle_16
                return;
        }
 
-       unless ($h) {
-               dbg("PCPROT: non-local PC16, ignored") if isdbg('chanerr');
-               return;
-       }
-
        my $parent = Route::Node::get($ncall); 
 
        if ($parent) {
@@ -425,6 +440,15 @@ sub handle_16
 
                # input filter if required
                return unless $self->in_filter_route($parent);
+       } else {
+               $parent = Route::Node->new($ncall);
+       }
+
+       unless ($h) {
+               if ($parent->via_pc92) {
+                       dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
+                       return;
+               }
        }
 
        my $i;
@@ -455,9 +479,10 @@ sub handle_16
                                $r->flags($flags);
                                $au = $r;
                        }
-                       push @rout, $r if $au;
+                       push @rout, $r if $h && $au;
                } else {
-                       push @rout, $parent->add_user($call, $flags);
+                       my @ans = $parent->add_user($call, $flags);
+                       push @rout, @ans if $h && @ans; 
                }
                
                # send info to all logged in thingies
@@ -473,8 +498,10 @@ sub handle_16
                $user->lastin($main::systime) unless DXChannel::get($call);
                $user->put;
        }
-       $self->route_pc16($origin, $line, $parent, @rout) if @rout;
-       
+       if (@rout) {
+               $self->route_pc16($origin, $line, $parent, @rout) if @rout;
+#              $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
+       }
 }
                
 # remove a user
@@ -495,6 +522,7 @@ sub handle_17
                dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
                return;
        }
+
        if ($ncall eq $main::mycall) {
                dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
                return;
@@ -522,6 +550,19 @@ sub handle_17
                return;
        }
 
+       $dxchan = DXChannel::get($ncall);
+       unless ($dxchan) {
+               if ($parent->via_pc92) {
+                       dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
+                       return;
+               }
+       }
+
+       if (DXChannel::get($ucall)) {
+               dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
+               return;
+       }
+
        # input filter if required and then remove user if present
        if ($parent) {
 #              return unless $self->in_filter_route($parent);  
@@ -541,6 +582,7 @@ sub handle_17
 
        $uref = Route->new($ucall) unless $uref; # throw away
        $self->route_pc17($origin, $line, $parent, $uref);
+#      $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
 }
                
 # link request
@@ -563,8 +605,9 @@ sub handle_18
                        $self->user->put;
                        $self->sort('S');
                }
-               $self->{handle_xml}++ if DXXml::available() && $_[1] =~ /\bxml\b/;
-               $self->{do_pc92}++ if $_[1] =~ /\bpc92\b/;
+               $self->{handle_xml}++ if DXXml::available() && $_[1] =~ /\bxml/;
+               my ($pc9x) = $_[1] =~ /\bpc9\[(\d+)\]/;
+               $self->{do_pc92}++ if defined $pc9x && $pc9x =~ /2/;
        } else {
                $self->version(50.0);
                $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
@@ -579,6 +622,23 @@ sub handle_18
        $self->send(pc20());
 }
                
+sub check_add_node
+{
+       my $call = shift;
+       
+       # add this station to the user database, if required (don't remove SSID from nodes)
+       my $user = DXUser->get_current($call);
+       if (!$user) {
+               $user = DXUser->new($call);
+               $user->priv(1);         # I have relented and defaulted nodes
+               $user->lockout(1);
+               $user->homenode($call);
+               $user->node($call);
+       }
+       $user->sort('A') unless $user->is_node;
+       return $user;
+}
+
 # incoming cluster list
 sub handle_19
 {
@@ -591,7 +651,7 @@ sub handle_19
        my $newline = "PC19^";
 
        # new routing list
-       my @rout;
+       my (@rout, @pc92out);
 
        # first get the INTERFACE node
        my $parent = Route::Node::get($self->{call});
@@ -601,6 +661,8 @@ sub handle_19
                return;
        }
 
+       my $h;
+       
        # parse the PC19
        # 
        # We are making a major change from now on. We are only going to accept
@@ -610,7 +672,8 @@ sub handle_19
        # The justification for this is that most of it is wrong or out of date
        # anyway. 
        # 
-       # From now on we are only going to believe PC92 data.
+       # From now on we are only going to believe PC92 data and locally connected
+       # non-pc92 nodes.
        #
        for ($i = 1; $i < $#_-1; $i += 4) {
                my $here = $_[$i];
@@ -629,23 +692,19 @@ sub handle_19
                next if $call eq $main::mycall;
 
                # check that this PC19 isn't trying to alter the wrong dxchan
+               $h = 0;
                my $dxchan = DXChannel::get($call);
-               if ($dxchan && $dxchan != $self) {
-                       dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
-                       next;
-               }
-
-               # add this station to the user database, if required (don't remove SSID from nodes)
-               my $user = DXUser->get_current($call);
-               if (!$user) {
-                       $user = DXUser->new($call);
-                       $user->priv(1);         # I have relented and defaulted nodes
-                       $user->lockout(1);
-                       $user->homenode($call);
-                       $user->node($call);
+               if ($dxchan) {
+                       if ($dxchan == $self) {
+                               $h = 1;
+                       } else {
+                               dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
+                               next;
+                       }
                }
-               $user->sort('A') unless $user->is_node;
 
+               my $user = check_add_node($call);
+               
 #              if (eph_dup($genline)) {
 #                      dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
 #                      next;
@@ -653,9 +712,11 @@ sub handle_19
 
                RouteDB::update($call, $self->{call}, $dxchan ? 1 : undef);
 
-               unless ($dxchan) {
-                       dbg("PCPROT: PC19 not directly connected, ignored") if isdbg('chanerr');
-                       next;
+               unless ($h) {
+                       if ($parent->via_pc92) {
+                               dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
+                               next;
+                       }
                }
 
                my $r = Route::Node::get($call);
@@ -684,6 +745,7 @@ sub handle_19
                                        my $ar = $parent->add($call, $ver, $flags);
                                        $user->wantroutepc19(1) unless defined $user->wantroutepc19;
                                        push @rout, $ar if $ar;
+                                       push @pc92out, $r if $h;
                                } else {
                                        next;
                                }
@@ -704,10 +766,21 @@ sub handle_19
        if (@rout) {
                $self->route_pc21($self->{call}, $line, @rout);
                $self->route_pc19($self->{call}, $line, @rout);
-               $self->route_pc92a($main::mycall, $line, @rout);
+       }
+       if (@pc92out) {
+               $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
        }
 }
                
+sub send_delayed_pc92
+{
+       my $self = shift;
+       
+       # send out delayed PC92 config for this node if it is external
+       my $line = $main::me->gen_my_pc92_config;
+       $self->broadcast_route_pc9x($main::mycall, undef, $line, 0);
+}
+
 # send local configuration
 sub handle_20
 {
@@ -715,10 +788,18 @@ sub handle_20
        my $pcno = shift;
        my $line = shift;
        my $origin = shift;
+
+       if ($self->{do_pc92} && $self->{state} ne 'init92') {
+               dbg("PCPROT: disconnecting because login call not sent in any pc92") if isdbg('chanerr');
+               $self->send("**** You logged in with $self->{call} but that is NOT your \$mycall");
+               $self->disconnect;
+               return;
+       }
        $self->send_local_config();
        $self->send(pc22());
        $self->state('normal');
        $self->{lastping} = 0;
+       $self->send_delayed_pc92;
 }
                
 # delete a cluster from the list
@@ -756,6 +837,11 @@ sub handle_21
                my $node = Route::Node::get($call);
                if ($node) {
                        
+                       if ($node->via_pc92) {
+                               dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
+                               return;
+                       }
+
                        my $dxchan = DXChannel::get($call);
                        if ($dxchan && $dxchan != $self) {
                                dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
@@ -776,7 +862,7 @@ sub handle_21
 
        if (@rout) {
                $self->route_pc21($origin, $line, @rout);
-               $self->route_pc92d($main::mycall, $line, @rout);
+#              $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
        }
 }
                
@@ -787,8 +873,27 @@ sub handle_22
        my $pcno = shift;
        my $line = shift;
        my $origin = shift;
-       $self->state('normal');
+
+       if ($self->{do_pc92}) {
+               if ($self->{state} ne 'init92') {
+                       dbg("PCPROT: disconnecting because login call not sent in any pc92") if isdbg('chanerr');
+                       $self->send("**** You logged in with $self->{call} but that is NOT your \$mycall");
+                       $self->disconnect;
+                       return;
+               }
+#              my $ref = Route::Node::get($self->{call});
+#              if ($ref) {
+#                      $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref);
+#              } else {
+#                      dbg("PCPROT: disconnecting because pc92 for $self->{call} received") if isdbg('chanerr');
+#                      $self->disconnect;
+#                      return;
+#              }
+#      } else {
+               $self->send_delayed_pc92;
+       }
        $self->{lastping} = 0;
+       $self->state('normal');
 }
                                
 # WWV info
@@ -1237,7 +1342,7 @@ sub _decode_pc92_call
        my $icall = shift;
        my @part = split /:/, $icall;
        my ($flag, $call) = unpack "A A*", $part[0];
-       return () unless $flag && $flag ge '0' && $flag le '7';
+       return () unless defined $flag && $flag ge '0' && $flag le '7';
        return () unless $call && is_callsign($call);
        my $is_node = $flag & 4;
        my $is_extnode = $flag & 2;
@@ -1254,19 +1359,97 @@ sub _encode_pc92_call
        my $call = $ref->call; 
        my $extra = '';
        $flag |= $ref->here ? 1 : 0;
-       if ($ext && ($ref->isa('Route::Node') || $ref->isa('DXProt'))) {
+       if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
                $flag |= 4;
-#              if ($ref->version || $ref->build) {
-               if ($ref->version) {
-                       my $version = $ref->version || 1.0;
-                       $version =  $version * 100 + 5300 if $version > 50;
-                       $extra .= ":" . $version;
-#                      $extra .= (":" . ($ref->build ? $ref->build : '')) if $ref->build;
+               my $dxchan = DXChannel::get($call);
+               $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc92};
+               if ($ext) {
+                       if ($ref->version) {
+                               my $version = $ref->version || 1.0;
+                               $version =  $version * 100 + 5300 if $version < 50;
+                               $extra .= ":" . $version;
+                       }
                }
        }
        return "$flag$call$extra";
 }
 
+sub _add_thingy
+{
+       my $parent = shift;
+       my $s = shift;
+       my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
+       my @rout;
+
+       if ($call) {
+               if ($is_node) {
+                       dbg("ROUTE: added node $call to " . $parent->call) if isdbg('routelow');
+                       @rout = $parent->add($call, $version, Route::here($here));
+               } else {
+                       dbg("ROUTE: added user $call to " . $parent->call) if isdbg('routelow');
+                       @rout = $parent->add_user($call, Route::here($here));
+               }
+       }
+       return @rout;
+}
+
+sub _del_thingy
+{
+       my $parent = shift;
+       my $s = shift;
+       my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
+       my @rout;
+       if ($call) {
+               if ($is_node) {
+                       my $nref = Route::Node::get($call);
+                       dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
+                       @rout = $nref->del($parent) if $nref;
+               } else {
+                       my $uref = Route::User::get($call);
+                       dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
+                       @rout = $parent->del_user($uref) if $uref;
+               }
+       }
+       return @rout;
+}
+
+my $_last_time;
+my $_last_occurs;
+
+sub gen_pc9x_t
+{
+       if (!$_last_time || $_last_time != $main::systime) {
+               $_last_time = $main::systime;
+               $_last_occurs = 0;
+               return $_last_time - $main::systime_daystart;
+       } else {
+               $_last_occurs++;
+               return sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
+       }
+}
+
+sub check_pc9x_t
+{
+       my $call = shift;
+       my $t = shift;
+       my $pc = shift;
+       my $create = shift;
+
+       my $parent = ref $call ? $call : Route::Node::get($call);
+       if ($parent) {
+               my $lastid = $parent->lastid->{$pc} || 0;
+               if ($lastid + $main::systime_daystart >= $t + $main::systime_daystart) {
+                       dbg("PCPROT: dup / old id on $call <= $lastid, ignored") if isdbg('chanerr');
+                       return;
+               }
+       } elsif ($create) {
+               $parent = Route::Node->new($call);
+       }
+       $parent->lastid->{$pc} = $t;
+
+       return $parent;
+}
+
 # DXSpider routing entries
 sub handle_92
 {
@@ -1275,43 +1458,145 @@ sub handle_92
        my $line = shift;
        my $origin = shift;
 
+       my (@radd, @rdel);
+       
        $self->{do_pc92} ||= 1;
 
-       my ($ncall, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_[1]);
-       unless ($ncall) {
+       my $pcall = $_[1];
+       unless ($pcall) {
                dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
                return;
        }
        my $t = $_[2];
        my $sort = $_[3];
+       my @ent = map {[ _decode_pc92_call($_) ]} grep {$_ && /^[0-7]/} @_[4 .. $#_];
        
-       if ($ncall eq $main::mycall) {
+       if ($pcall eq $main::mycall) {
                dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
                return;
        }
 
-       my $nref = Route::Node::get($ncall);
-       if ($nref) {
-               my $lastid = $nref->lastid->{92} || 0;
-               if ($lastid > $t) {
-                       dbg("PCPROT: dup / old id <= $lastid, ignored") if isdbg('chanerr');
+       if ($pcall eq $self->{call} && $self->{state} eq 'init') {
+               $self->state('init92');
+       }
+
+       my $parent = check_pc9x_t($pcall, $t, 92, 1) || return;
+       my $oparent = $parent;
+       
+       $parent->lastid->{92} = $t;
+       $parent->do_pc92(1);
+       $parent->via_pc92(1);
+
+       if (@ent) {
+
+               # look at the first one which will always be a node of some sort
+               # and update any information that needs to be done. 
+               my ($call, $is_node, $is_extnode, $here, $version, $build) = @{$ent[0]}; 
+               if ($call && $is_node) {
+                       if ($call eq $main::mycall) {
+                               dbg("PCPROT: looped back on node entry, ignored") if isdbg('chanerr');
+                               return;
+                       }
+                       if ($is_extnode) {
+                               # this is only accepted from my "self"
+                               if (DXChannel::get($call) && $call ne $self->{call}) {
+                                       dbg("PCPROT: locally connected node config for $call from other another node $self->{call}, ignored") if isdbg('chanerr');
+                                       return;
+                               }
+                               # reparent to external node (note that we must have received a 'C' or 'A' record
+                               # from the true parent node for this external before we get one for the this node
+                               unless ($parent = Route::Node::get($call)) {
+                                       dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
+                                       return;
+                               }
+                               $parent = check_pc9x_t($call, $t, 92) || return;
+                               $parent->via_pc92(1);
+                       }
+               } else {
+                       dbg("PCPROT: must be mycall or external node as first entry, ignored") if isdbg('chanerr');
                        return;
                }
-               $nref->flags(Route::flags($here));
-       } else {
-               $nref = Route::Node->new($ncall, $version, Route::here($here));
+               $parent->here(Route::here($here));
+               $parent->version($version) if $version && $version > $parent->version;
+               $parent->build($build) if $build && $build > $parent->build;
+               shift @ent;
+       }
+
+       # do a pass through removing any references to either locally connected nodes or mycall
+       my @nent;
+       for (@ent) {
+               next unless $_;
+               if ($_->[0] eq $main::mycall || DXChannel::get($_->[0])) {
+                       dbg("PCPROT: $_->[0] refers to locally connected node, ignored") if isdbg('chanerr');
+                       next;
+               } 
+               push @nent, $_;
        }
-       $nref->lastid->{92} = $t;
 
        if ($sort eq 'A') {
-               
+               for (@nent) {
+                       push @radd, _add_thingy($parent, $_);
+               }
        } elsif ($sort eq 'D') {
-
+               for (@nent) {
+                       push @rdel, _del_thingy($parent, $_);
+               }
        } elsif ($sort eq 'C') {
+               my (@nodes, @users);
+
+               # we only reset obscounts on config records
+               $oparent->reset_obs;
+               dbg("ROUTE: reset obscount on $pcall now " . $oparent->obscount) if isdbg('route');
+               if ($oparent != $parent) {
+                       $parent->reset_obs;
+                       dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('route');
+               }
 
+               foreach my $r (@nent) {
+#                      my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);                     
+                       if ($r->[0]) {
+                               if ($r->[1]) {
+                                       push @nodes, $r->[0];
+                               } else {
+                                       push @users, $r->[0];
+                               }
+                       } else {
+                               dbg("DXPROT: pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr'); 
+                       }
+               }
+
+               my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
+
+               # add users here
+               foreach my $r (@nent) {
+                       my $call = $r->[0];
+                       if ($call) {
+                               push @radd,_add_thingy($parent, $r) if grep $call eq $_, (@$nnodes, @$nusers);
+                       }
+               }
+               # del users here
+               foreach my $r (@$dnodes) {
+                       push @rdel,_del_thingy($parent, [$r, 1]);
+               }
+               foreach my $r (@$dusers) {
+                       push @rdel,_del_thingy($parent, [$r, 0]);
+               }
        } else {
                dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
+               return;
        }
+
+       $self->broadcast_route_pc9x($pcall, undef, $line, 0);
+       foreach my $r (@rdel) {
+               next unless $r;
+               
+               $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
+               $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
+       }
+       my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
+       my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
+       $self->route_pc19($pcall, undef, @pc19) if @pc19;
+       $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
 }
 
 # if get here then rebroadcast the thing with its Hop count decremented (if
@@ -1337,3 +1622,5 @@ sub handle_default
                }
        }
 }
+
+1;