try to prevent $myalias calls being set to nodes
[spider.git] / perl / DXProtHandle.pm
index 2fa4095fa381c0a1b9ddb94829405a7cd6fd9173..e642bb3cf4b3020e2b19e231a3a6c4d2873994b3 100644 (file)
@@ -49,7 +49,7 @@ use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restim
 
 $pc9x_dupe_age = 60;                   # catch loops of circular (usually) D records
 $pc10_dupe_age = 45;                   # just something to catch duplicate PC10->PC93 conversions
-$pc92_slug_changes = 60;               # slug any changes going outward for this long
+$pc92_slug_changes = 60*5;             # slug any changes going outward for this long
 $last_pc92_slug = 0;                   # the last time we sent out any delayed add or del PC92s
 $pc9x_time_tolerance = 15*60;  # the time on a pc9x is allowed to be out by this amount
 $pc9x_past_age = (122*60)+             # maximum age in the past of a px9x (a config record might be the only
@@ -171,13 +171,13 @@ sub handle_11
        # convert the date to a unix date
        my $d = cltounix($_[3], $_[4]);
        # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
-       if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
+       if (!$d || (($pcno == 11 || $pcno == 61) && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
                dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
                return;
        }
 
        # is it 'baddx'
-       if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
+       if ($baddx->in($_[2]) || BadWords::check($_[2])) {
                dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
                return;
        }
@@ -197,7 +197,7 @@ sub handle_11
                }
        }
 
-       my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $nossid, $_[7]);
+       my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $nossid, $_[7], $_[8]);
        # global spot filtering on INPUT
        if ($self->{inspotsfilter}) {
                my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
@@ -321,6 +321,12 @@ sub handle_12
                return;
        }
 
+       # ignore PC12s from origins that use PCxx protocol
+       my $oref = Route::get($origin);
+       if ($oref->do_pc9x) {
+               dbg("PCPROT: PC12 rxed from PC9x node, ignored") if isdbg('chanerr');
+               return;
+       }
 
        my $dxchan;
 
@@ -638,8 +644,8 @@ sub check_add_node
                $user->lockout(1);
                $user->homenode($call);
                $user->node($call);
+               $user->sort('A');
        }
-       $user->sort('A') unless $user->is_node;
        return $user;
 }
 
@@ -693,7 +699,7 @@ sub handle_19
                next unless $ver && $ver =~ /^\d+$/;
                next if $ver < 5000;    # only works with version 5 software
                next if length $call < 3; # min 3 letter callsigns
-               next if $call eq $main::mycall;
+               next if $call eq $main::mycall || $call eq $main::myalias;
 
                # check that this PC19 isn't trying to alter the wrong dxchan
                $h = 0;
@@ -834,7 +840,7 @@ sub handle_21
 
        my @rout;
 
-       if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
+       if ($call ne $main::mycall && $call ne $main::myalias) { # don't allow malicious buggers to disconnect me!
                my $node = Route::Node::get($call);
                if ($node) {
 
@@ -845,7 +851,7 @@ sub handle_21
 
                        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');
+                               dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chan');
                                return;
                        }
 
@@ -857,7 +863,7 @@ sub handle_21
                        push @rout, $call if $dxchan && @rout == 0;
                }
        } else {
-               dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
+               dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chan');
                return;
        }
 
@@ -989,7 +995,7 @@ sub handle_25
                return;
        }
        if ($_[2] eq $main::mycall) {
-               dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
+               dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chan');
                return;
        }
 
@@ -1131,6 +1137,16 @@ sub handle_41
                return;
        }
 
+       if ($call eq $main::mycall || $call eq $main::myalias) {
+               dbg "DXPROT: PC41 trying to update $call from outside via $origin, ignored";
+               return;
+       }
+       my $chan = DXChannel::get($call);
+       if ($chan) {
+               dbg "DXPROT: PC41 trying to update online $call from outside via $origin, ignored";
+               return;
+       }
+
        # add this station to the user database, if required
        my $user = DXUser::get_current($call);
        $user = DXUser->new($call) unless $user;
@@ -1138,9 +1154,6 @@ sub handle_41
        if ($sort == 1) {
                if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
                        dbg("PCPROT: invalid name") if isdbg('chanerr');
-                       if ($main::mycall eq 'GB7DJK' || $main::mycall eq 'GB7BAA' || $main::mycall eq 'WR3D') {
-                               DXChannel::broadcast_nodes(pc41($_[1], 1, $user->name)); # send it to everyone including me
-                       }
                        return;
                }
                $user->name($val);
@@ -1249,6 +1262,10 @@ sub handle_51
        my $from = $_[2];
        my $flag = $_[3];
 
+       if ($to eq $main::myalias) {
+               dbg("DXPROT: Ping addressed to \$myalias ($main::myalias), ignored") if isdbg('chan');
+               return;
+       }
 
        # is it for us?
        if ($to eq $main::mycall) {
@@ -1353,7 +1370,10 @@ sub _decode_pc92_call
        my $is_node = $flag & 4;
        my $is_extnode = $flag & 2;
        my $here = $flag & 1;
-       return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
+       my $ip  = $part[3];
+       $ip ||= $part[1] if $part[1] && ($part[1] =~ /^(?:\d+\.)+/ || $part[1] =~ /^(?:(?:[abcdef\d]+)?,)+/);
+       $ip =~ s/,/:/g if $ip;
+       return ($call, $is_node, $is_extnode, $here, $part[1], $part[2], $ip);
 }
 
 # decode a pc92 call: flag call : version : build
@@ -1364,7 +1384,7 @@ sub _encode_pc92_call
        # plain call or value
        return $ref unless ref $ref;
 
-       my $ext = shift;
+       my $ext = shift || 0;
        my $flag = 0;
        my $call = $ref->call;
        my $extra = '';
@@ -1373,14 +1393,17 @@ sub _encode_pc92_call
                $flag |= 4;
                my $dxchan = DXChannel::get($call);
                $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
-               if ($ext) {
-                       if ($ref->version) {
-                               my $version = $ref->version || 1.0;
-                               $version =  $version * 100 + 5300 if $version < 50;
-                               $extra .= ":" . $version;
-                       }
+               if (($ext & 1) && $ref->version) {
+                       my $version = $ref->version || 1.0;
+                       $version =  $version * 100 + 5300 if $version < 50;
+                       $extra .= ":" . $version;
                }
        }
+       if (($ext & 2) && $ref->ip) {
+               my $ip = $ref->ip;
+               $ip =~ s/:/,/g;
+               $extra .= ':' . $ip;
+       }
        return "$flag$call$extra";
 }
 
@@ -1394,18 +1417,29 @@ sub _add_thingy
        my $dxchan = shift;
        my $hops = shift;
 
-       my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
+       my ($call, $is_node, $is_extnode, $here, $version, $build, $ip) = @$s;
        my @rout;
 
        if ($call) {
+               my $ncall = $parent->call;
                if ($is_node) {
-                       dbg("ROUTE: added node $call to " . $parent->call) if isdbg('routelow');
-                       @rout = $parent->add($call, $version, Route::here($here));
+                       dbg("ROUTE: added node $call to $ncall") if isdbg('routelow');
+                       @rout = $parent->add($call, $version, Route::here($here), $ip);
                        my $r = Route::Node::get($call);
                        $r->PC92C_dxchan($dxchan->call, $hops) if $r;
+                       if ($ip) {
+                               $r->ip($ip);
+                               Log('DXProt', "PC92A $call -> $ip on $ncall");
+                       }
                } else {
-                       dbg("ROUTE: added user $call to " . $parent->call) if isdbg('routelow');
-                       @rout = $parent->add_user($call, Route::here($here));
+                       dbg("ROUTE: added user $call to $ncall") if isdbg('routelow');
+                       @rout = $parent->add_user($call, Route::here($here), $ip);
+                       $dxchan->tell_buddies('loginb', $call, $ncall) if $dxchan;
+                       my $r = Route::User::get($call);
+                       if ($ip) {
+                               $r->ip($ip);
+                               Log('DXProt', "PC92A $call -> $ip on $ncall");
+                       }
                }
                if ($pc92_slug_changes && $parent == $main::routeroot) {
                        $things_add{$call} = Route::get($call);
@@ -1419,6 +1453,7 @@ sub _del_thingy
 {
        my $parent = shift;
        my $s = shift;
+       my $dxchan = shift;
        my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
        my @rout;
        if ($call) {
@@ -1428,9 +1463,12 @@ sub _del_thingy
                        dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
                        @rout = $ref->del($parent) if $ref;
                } else {
-                       $ref = Route::User::get($call);
                        dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
-                       @rout = $parent->del_user($ref) if $ref;
+                       $ref = Route::User::get($call);
+                       if ($ref) {
+                               $dxchan->tell_buddies('logoutb', $call, $parent->call) if $dxchan;
+                               @rout = $parent->del_user($ref);
+                       }
                }
                if ($pc92_slug_changes && $parent == $main::routeroot) {
                        $things_del{$call} = $ref unless exists $things_add{$call};
@@ -1568,7 +1606,11 @@ sub pc92_handle_first_slot
        my ($call, $is_node, $is_extnode, $here, $version, $build) = @$slot;
        if ($call && $is_node) {
                if ($call eq $main::mycall) {
-                       dbg("PCPROT: $call looped back onto $main::mycall, ignored") if isdbg('chanerr');
+                       dbg("PCPROT: $call looped back onto \$main::mycall ($main::mycall), ignored") if isdbg('chan');
+                       return;
+               }
+               if ($call eq $main::myalias) {
+                       dbg("PCPROT: $call looped back onto \$main::myalias ($main::myalias), ignored") if isdbg('chan');
                        return;
                }
                # this is only accepted from my "self".
@@ -1628,21 +1670,26 @@ sub handle_92
 #      }
 
        if ($pcall eq $main::mycall) {
-               dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
+               dbg("PCPROT: looped back, ignored") if isdbg('chan');
+               return;
+       }
+
+       if ($pcall eq $main::myalias) {
+               dbg("PCPROT: looped back to \$myalias ($main::myalias), misconfiguration ignored") if isdbg('chan');
                return;
        }
 
        if ($pcall eq $self->{call} && $self->{state} eq 'init') {
                if ($self->{isolate}) {
-                       dbg("PC9x received, but $pcall is isolated, ignored");
+                       dbg("DXPROT: PC9x received, but $pcall is isolated, ignored");
                        return;
                } elsif (!$self->user->wantpc9x) {
-                       dbg("PC9x explicitly switched off on $pcall, ignored");
+                       dbg("DXPROT: PC9x explicitly switched off on $pcall, ignored");
                        return;
                } else {
                        $self->state('init92');
                        $self->{do_pc9x} = 1;
-                       dbg("Do pc9x set on $pcall");
+                       dbg("DXPROT: Do pc9x set on $pcall");
                }
        }
        unless ($self->{do_pc9x}) {
@@ -1773,7 +1820,7 @@ sub handle_92
                        }
                } elsif ($sort eq 'D') {
                        for (@nent) {
-                               push @rdel, _del_thingy($parent, $_);
+                               push @rdel, _del_thingy($parent, $_, $self);
                        }
                } elsif ($sort eq 'C') {
                        my (@nodes, @users);
@@ -1807,10 +1854,10 @@ sub handle_92
                        }
                        # del users here
                        foreach my $r (@$dnodes) {
-                               push @rdel,_del_thingy($parent, [$r, 1]);
+                               push @rdel,_del_thingy($parent, [$r, 1], $self);
                        }
                        foreach my $r (@$dusers) {
-                               push @rdel,_del_thingy($parent, [$r, 0]);
+                               push @rdel,_del_thingy($parent, [$r, 0], $self);
                        }
 
                        # remember this last PC92C for rebroadcast on demand
@@ -1855,7 +1902,7 @@ sub find_pc9x_routes
        if ($ref->isa('Route::User')) {
                my $dxchan = DXChannel::get($to);
                push @parent, $to if $dxchan;
-               push @parent, @{$ref->parent};
+               push @parent, $ref->parents;
        } else {
                @parent = $to;
        }
@@ -1966,8 +2013,8 @@ sub handle_93
                $self->send_announce(1, pc12($from, $text, $local, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
                return if $via eq 'LOCAL';
        } elsif (!is_callsign($to) && $text =~ /^#\d+ /) {
-               # chat messages to non-pc9x nodes
-               $self->send_chat(1, pc12($from, $text, undef, $to, undef, $pcall), $from, '*', $text, $to, $pcall, '0');
+               # chat messages really only locally connected users
+               $self->send_chat(1, $line, $from, '*', $text, $to, $pcall, '0');
        }
 
        # broadcast this chat sentence everywhere unless it is targetted to 'LOCAL'