now working to 1st order OK with two nodes.
[spider.git] / perl / DXProt.pm
index 497a26712b63f64b279b7bde6992f4d4fc2d525b..ba958cfad0418bad5deac0c279c5ea9f5c620527 100644 (file)
@@ -444,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;
        }
 
@@ -453,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) );
        }
 }
@@ -572,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 {
@@ -582,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 {
@@ -666,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;
                                }
@@ -707,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) {
@@ -740,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);
@@ -784,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) {
@@ -843,11 +849,15 @@ sub handle_18
 
        # first clear out any nodes on this dxchannel
        my $node = Route::Node::get($self->{call}) ;
-       my @rout = grep {$_ != $node } $main::routeroot->remove_route($node, $self) if $node;
-       my @rusers;
-       push @rusers, $_->unlink_all_users for @rout;
+       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, @rusers) {
+       for (@rout) {
                $_->delete;
        };
        
@@ -900,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)");
                                
@@ -964,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
@@ -1409,31 +1427,35 @@ sub handle_59
        my $line = shift;
        my $origin = shift;
 
-       return unless eph_dup($line);
-
        my ($sort, $hextime, $ncall) = @_[1,2,3];
        if ($ncall eq $main::mycall) {
                dbg("PCPROT: ignoring PC59 for me") if isdbg('chan');
                return;
        }
 
-       # mark myself as NewRoute if I get a PC59
-       $self->{newroute} = 1 if $ncall eq $self->{call};
-
        # do this once for filtering with a throwaway routing entry if a new node
        my $fnode = Route::Node::get($ncall) || Route::new($ncall);
        return unless $self->in_filter_route($fnode);
 
+       return if eph_dup($line);
+
+       # mark myself as NewRoute if I get a PC59
+       $self->{newroute} = 1 if $ncall eq $self->{call};
+
        # 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;
-       for my $ent (@_[4..-1]) {
+       for my $ent (@_[4..$#_]) {
+               next if $ent =~ /^H\d+$/;
+               
                my ($esort, $ehere, $ecall) = unpack "A A A*", $ent;
                my $ref;
 
+               next unless $esort && defined $ehere && $ecall;
+               
                # create user, if required
                my $user = DXUser->get_current($ecall);
                unless ($user) {
@@ -1456,7 +1478,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);
@@ -1472,7 +1494,7 @@ 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');
@@ -1490,6 +1512,7 @@ sub handle_59
        if ($sort eq 'D') {
                for my $ref (@refs) {
                        next if $ref->call eq $ncall;
+                       next if $ref->call eq $main::mycall;
                        if ($ref->isa('Route::Node')) {
                                push @delnode, $node->unlink_node($ref, $self);
                        } elsif ($ref->isa('Route::User')) {
@@ -1504,6 +1527,7 @@ sub handle_59
        if ($sort eq 'A') {
                for my $ref (@refs) {
                        next if $ref->call eq $ncall;
+                       next if $ref->call eq $main::mycall;
                        if ($ref->isa('Route::Node')) {
                                my $new = $node->link_node($ref, $self);
                                push @addnode, $new if $new;
@@ -1524,6 +1548,8 @@ sub handle_59
                my @au;
                for my $r (map {Route::Node::get($_)} $node->nodes) {
                        next unless $r;
+                       next if $r->call eq $ncall;
+                       next if $r->call eq $main::mycall;
                        push @dn, $r unless grep $_->call eq $r->call, @refs;
                }
                for my $r (map {Route::User::get($_)} $node->users) {
@@ -1532,6 +1558,8 @@ sub handle_59
                }
                for my $r (@refs) {
                        next unless $r;
+                       next if $r->call eq $ncall;
+                       next if $r->call eq $main::mycall;
                        if ($r->isa('Route::Node')) {
                                push @an, $r unless grep $r->call eq $_, $node->nodes;
                        } elsif ($r->isa('Route::User')) {
@@ -1981,7 +2009,9 @@ 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, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, (grep { Route::get($_) } $main::routeroot, @nodes, @users));
+               @localnodes = map { Route::Node::get($_->{call}) } @nodes;
+               my @localusers = map { Route::User::get($_->{call}) } @users;
+               $self->send_route($main::mycall, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, $main::routeroot, @localnodes, @localusers);
        } else {
                # send our nodes
                if ($self->{isolate}) {
@@ -2041,7 +2071,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');
@@ -2217,7 +2247,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 {
@@ -2243,16 +2273,12 @@ sub disconnect
        # do routing stuff, remove me from routing table
        my $node = Route::Node::get($call);
        my @rout;
-       my @rusers;
        if ($node) {
 
                # 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 consequently orphanned users from these nodes
-               push @rusers, $_->unlink_all_users for @rout;
-               
                # remove all my ephemera as well
                for (@rout) {
                        my $c = $_->call;
@@ -2271,7 +2297,7 @@ sub disconnect
        }
 
        # delete all the unwanted nodes
-       $_->delete for @rout, @rusers;
+       $_->delete for @rout;
        
        # remove outstanding pings
        delete $pings{$call};
@@ -2368,8 +2394,12 @@ sub broadcast_route
                        next if $dxchan == $main::me;
                        next unless $dxchan->isa('DXProt');
                        next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
-                       next if ($generate == \&pc19 || $generate==\&pc21) && !$dxchan->user->wantsendpc19;
-                       next if ($generate == \&pc59) && !$dxchan->{newroute};
+                       if ($dxchan->{newroute}) {
+                               next if ($generate == \&pc19 || $generate==\&pc21);
+                       } else {
+                               next if ($generate == \&pc19 || $generate==\&pc21) && !$dxchan->user->wantroutepc19;
+                               next if ($generate == \&pc59);
+                       }
  
                        $dxchan->send_route($origin, $generate, @_);
                }