an AFAIK nominally working (for one connection) version
[spider.git] / perl / DXProt.pm
index e272216556579d557f32a3b06c7ecdc8d95ef2d4..0b788ba73c10d0ed68818b09c7c3b1d2ae6290ed 100644 (file)
@@ -199,6 +199,19 @@ sub init
 {
        do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
        confess $@ if $@;
+
+       my $user = DXUser->get($main::mycall);
+       $DXProt::myprot_version += $main::version*100;
+       $main::me = DXProt->SUPER::alloc($main::mycall, 0, $user); 
+       $main::me->{here} = 1;
+       $main::me->{state} = "indifferent";
+       $main::me->{sort} = 'S';    # S for spider
+       $main::me->{priv} = 9;
+       $main::me->{metric} = 0;
+       $main::me->{pingave} = 0;
+       $main::me->{registered} = 1;
+       $main::me->{version} = $main::version;
+       $main::me->{build} = $main::build;
 }
 
 #
@@ -212,8 +225,12 @@ sub new
        # add this node to the table, the values get filled in later
        my $pkg = shift;
        my $call = shift;
-       $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
 
+       my $uref = Route::Node::get($call) || Route::Node->new($call);
+       $uref->here(1);
+       $uref->conf(0);
+       $uref->version(5000);
+       $main::routeroot->link_node($uref, $self);
        return $self;
 }
 
@@ -427,7 +444,7 @@ sub handle_10
        if ($ref = Route::get($to)) {
                $vref = Route::Node::get($via) if $via;
                $vref = undef unless $vref && grep $to eq $_, $vref->users;
-               $ref->dxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
+               $ref->bestdxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
                return;
        }
 
@@ -436,7 +453,7 @@ sub handle_10
        $ref = Route::get($from);
        $vref = $ref = Route::Node::get($_[6]) unless $ref; 
        if ($ref) {
-               $dxchan = $ref->dxchan;
+               $dxchan = $ref->bestdxchan;
                $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
        }
 }
@@ -555,7 +572,7 @@ sub handle_11
                        if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
                                my $cmd = "forward/opernam $spot[4]";
                                # send the rcmd but we aren't interested in the replies...
-                               my $dxchan = $node->dxchan;
+                               my $dxchan = $node->bestdxchan;
                                if ($dxchan && $dxchan->is_clx) {
                                        route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
                                } else {
@@ -565,7 +582,7 @@ sub handle_11
                                        $to = $_[7];
                                        $node = Route::Node::get($to);
                                        if ($node) {
-                                               $dxchan = $node->dxchan;
+                                               $dxchan = $node->bestdxchan;
                                                if ($dxchan && $dxchan->is_clx) {
                                                        route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
                                                } else {
@@ -649,7 +666,7 @@ sub handle_12
                        if ($call) {
                                my $ref = Route::get($call);
                                if ($ref) {
-                                       $dxchan = $ref->dxchan;
+                                       $dxchan = $ref->bestdxchan;
                                        $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
                                        return;
                                }
@@ -690,7 +707,10 @@ sub handle_16
        }
 
        # do we believe this call? 
-       next unless $ncall eq $self->{call} || $self->is_believed($ncall);
+       unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
+               dbg("PCPROT: We don't believe $ncall on $self->{call}");
+               return;
+       }
 
        my $node = Route::Node::get($ncall);
        unless ($node) {
@@ -723,8 +743,8 @@ sub handle_16
                        dbg("PCPROT: $call is a node") if isdbg('chanerr');
                        next;
                }
-                               
-               $r = Route::User::get($call) || Route::User::get($call);
+
+               $r = Route::User::get($call) || Route::User->new($call);
                $r->here($here);
                $r->conf($conf);
                $node->lastseen($main::systime);
@@ -767,7 +787,10 @@ sub handle_17
        }
 
        # do we believe this call? 
-       next unless $ncall eq $self->{call} || $self->is_believed($ncall);
+       unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
+               dbg("PCPROT: We don't believe $ncall on $self->{call}");
+               return;
+       }
 
        my $uref = Route::User::get($ucall);
        unless ($uref) {
@@ -825,9 +848,20 @@ sub handle_18
        $self->newroute( $_[1] =~ /NewRoute/ );
 
        # first clear out any nodes on this dxchannel
-       my $parent = Route::Node::get($self->{call});
-       my @rout = $parent->del_nodes;
-       $self->route_pc21($origin, $line, @rout, $parent) if @rout;
+       my $node = Route::Node::get($self->{call}) ;
+       my @rout;
+       foreach my $n ($node->nodes) {
+               next if $n eq $main::mycall;
+               next if $n eq $self->{call};
+               my $nref = Route::Node::get($n);
+               push @rout, $node->remove_route($nref, $self) if $nref;
+       } 
+       $self->route_pc21($origin, $line, @rout) if @rout;
+       for (@rout) {
+               $_->delete;
+       };
+       
+       # send the new config
        $self->send_local_config();
        $self->send(pc20());
 }
@@ -876,7 +910,10 @@ sub handle_19
                next if $call eq $main::mycall;
 
                # do we believe this call? 
-               next unless $call eq $self->{call} || $self->is_believed($call);
+               unless ($call eq $self->{call} || $self->is_believed($call)) {
+                       dbg("PCPROT: We don't believe $call on $self->{call}");
+                       next;
+               }
 
                eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
                                
@@ -940,6 +977,11 @@ sub handle_21
 
        return if $call eq $main::mycall;  # don't allow malicious buggers to disconnect me (or ignore loops)!
 
+       unless ($call eq $self->{call} || $self->is_believed($call)) {
+               dbg("PCPROT: We don't believe $call on $self->{call}");
+               return;
+       }
+
        eph_del_regex("^PC1[679].*$call");
                        
        # if I get a PC21 from the same callsign as self then treat it
@@ -1401,7 +1443,8 @@ sub handle_59
        return unless $self->in_filter_route($fnode);
 
        # now do it properly for actions
-       my $node = Route::Node::get($ncall) || Route::Node::new($ncall);
+       my $node = Route::Node::get($ncall) || Route::Node->new($ncall);
+       $node->newroute(1);
 
        # find each of the entries (or create new ones)
        my @refs;
@@ -1431,7 +1474,7 @@ sub handle_59
                                        $user->node($ncall);
                                        $user->put;
                                }
-                               $ref = Route::User::new($ecall, 0); 
+                               $ref = Route::User->new($ecall, 0); 
                        }
                } elsif ($esort eq 'N') {
                        $ref = Route::Node::get($ecall);
@@ -1447,14 +1490,14 @@ sub handle_59
                                        $user->node($ncall);
                                        $user->put;
                                }
-                               $ref = Route::Node::new($ecall, 0); 
+                               $ref = Route::Node->new($ecall, 0); 
                        } 
                } else {
                        dbg("DXPROT: unknown entity type '$esort' on $ecall for node $ncall") if isdbg('chan');
                        next;
                }
                $ref->here($ehere);             # might as well set this here
-               $ref->lastheard($main::systime);
+               $ref->lastseen($main::systime);
                push @refs, $ref;
        }
 
@@ -1519,7 +1562,6 @@ sub handle_59
                push @adduser, $node->add_user($_) for @au;
        }
 
-
        $self->route_pc21($origin, $line, @delnode) if @delnode;
        $self->route_pc19($origin, $line, @addnode) if @addnode;
        $self->route_pc17($origin, $line, @deluser) if @deluser;
@@ -1957,7 +1999,7 @@ sub send_local_config
        if ($self->{newroute}) {
                my @nodes = $self->{isolate} ? ($main::routeroot) : grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
                my @users = DXChannel::get_all_users();
-               $self->send_route($main::mycall, \&pc59c, @nodes+@users+1, (grep { Route::get($_) } $main::routeroot, @nodes, @users));
+               $self->send_route($main::mycall, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, (grep { Route::get($_) } $main::routeroot, @nodes, @users));
        } else {
                # send our nodes
                if ($self->{isolate}) {
@@ -2017,7 +2059,7 @@ sub route
        my $dxchan = DXChannel->get($call);
        unless ($dxchan) {
                my $cl = Route::get($call);
-               $dxchan = $cl->dxchan if $cl;
+               $dxchan = $cl->bestdxchan if $cl;
                if (ref $dxchan) {
                        if (ref $self && $dxchan eq $self) {
                                dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
@@ -2193,7 +2235,7 @@ sub addrcmd
        $rcmds{$to} = $r;
        
        my $ref = Route::Node::get($to);
-       my $dxchan = $ref->dxchan;
+       my $dxchan = $ref->bestdxchan;
        if ($dxchan && $dxchan->is_clx) {
                route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
        } else {
@@ -2220,28 +2262,18 @@ sub disconnect
        my $node = Route::Node::get($call);
        my @rout;
        if ($node) {
-               @rout = $node->del($main::routeroot);
-               
-               # and all my ephemera as well
+
+               # remove the route from this node and return a list
+               # of nodes that have become orphanned as a result. 
+               push @rout, $main::routeroot->remove_route($node, $self);
+
+               # remove all my ephemera as well
                for (@rout) {
                        my $c = $_->call;
                        eph_del_regex("^PC1[679].*$c");
                }
        }
        
-       # remove them from the pc19list as well
-       while (my ($k,$v) = each %pc19list) {
-               my @l = grep {$_->[0] ne $call} @{$pc19list{$k}};
-               if (@l) {
-                       $pc19list{$k} = \@l;
-               } else {
-                       delete $pc19list{$k};
-               }
-               
-               # and the ephemera
-               eph_del_regex("^PC1[679].*$k");
-       }
-
        # unbusy and stop and outgoing mail
        my $mref = DXMsg::get_busy($call);
        $mref->stop_msg($call) if $mref;
@@ -2249,8 +2281,12 @@ sub disconnect
        # broadcast to all other nodes that all the nodes connected to via me are gone
        unless ($pc39flag && $pc39flag == 2) {
                $self->route_pc21($main::mycall, undef, @rout) if @rout;
+               $self->route_pc59('D', 0, $main::mycall, $node);
        }
 
+       # delete all the unwanted nodes
+       $_->delete for @rout;
+       
        # remove outstanding pings
        delete $pings{$call};
        
@@ -2297,7 +2333,6 @@ sub send_route
                # deal with non routing parameters
                unless (ref $r && $r->isa('Route')) {
                        push @rin, $r;
-                       $no++;
                        next;
                }
                
@@ -2419,8 +2454,7 @@ sub route_pc59
        my $origin = shift;
        my $line = shift;
 
-       # @_ - 2 because we start with [ACD], hexstamp
-       broadcast_route($self, $origin, \&pc59, $line, scalar @_ - 2, @_);
+       broadcast_route($self, $origin, \&pc59, $line, scalar @_, @_);
 }
 
 sub in_filter_route