make sure touser is passed on
[spider.git] / perl / Aranea.pm
index fc77cc13122fe6f1a7f330c11992e5aedb1ac59f..7765af34fead427e39b4e7a12b2c95324994147d 100644 (file)
@@ -25,20 +25,25 @@ use Script;
 use Verify;
 use DXDupe;
 use Thingy;
+use Thingy::Rt;
+use Thingy::Hello;
+use Thingy::Bye;
 use RouteDB;
+use DXProt;
+use DXCommandmode;
 
 use vars qw($VERSION $BRANCH);
-$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
-$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /^\d+\.\d+(?:\.(\d+)\.(\d+))?$/  || (0,0));
-$main::build += $VERSION;
-$main::branch += $BRANCH;
 
-use vars qw(@ISA $ntpflag $dupeage);
+main::mkver($VERSION = q$Revision$);
+
+use vars qw(@ISA $ntpflag $dupeage $cf_interval $hello_interval);
 
 @ISA = qw(DXChannel);
 
 $ntpflag = 0;                                  # should be set in startup if NTP in use
 $dupeage = 12*60*60;                   # duplicates stored half a day 
+$cf_interval = 30*60;                  # interval between config broadcasts
+$hello_interval = 3*60*60;             # interval between hello broadcasts for me and local users
 
 my $seqno = 0;
 my $dayno = 0;
@@ -56,7 +61,6 @@ sub new
        # add this node to the table, the values get filled in later
        my $pkg = shift;
        my $call = shift;
-       $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
        $self->{'sort'} = 'W';
        return $self;
 }
@@ -70,7 +74,7 @@ sub start
        # log it
        my $host = $self->{conn}->{peerhost} || "unknown";
        Log('Aranea', "$call connected from $host");
-       
+
        # remember type of connection
        $self->{consort} = $line;
        $self->{outbound} = $sort eq 'O';
@@ -118,6 +122,13 @@ sub start
        # send info to all logged in thingies
        $self->tell_login('loginn');
 
+       # broadcast our configuration to the world
+       unless ($self->{outbound}) {
+               my $thing = Thingy::Rt->new_cf;
+               $thing->broadcast;
+               $main::me->lastcf($main::systime);
+       }
+       
        # run a script send the output to the debug file
        my $script = new Script(lc $call) || new Script('node_default');
        $script->run($self) if $script;
@@ -134,9 +145,11 @@ sub normal
 }
 
 #
-# periodic processing
+# periodic processing (every second)
 #
 
+my $lastmin = time;
+
 sub process
 {
 
@@ -146,6 +159,56 @@ sub process
                $dayno = $d;
                $daystart = $main::systime - ($main::systime % 86400);
        }
+       if ($main::systime >= $lastmin + 60) {
+               per_minute();
+               $lastmin = $main::systime;
+       }
+}
+
+sub per_minute
+{
+       # send hello and cf packages periodically
+       foreach my $dxchan (DXChannel::get_all()) {
+               next if $dxchan->is_aranea;
+               if ($main::systime >= $dxchan->lasthello + $hello_interval) {
+                       my $thing = Thingy::Hello->new(h => $dxchan->here);
+                       $thing->{user} = $dxchan->{call} unless $dxchan == $main::me;
+                       if (my $v = $dxchan->{version}) {
+                               if ($dxchan->is_spider) {
+                                       $thing->{sw} = 'DXSp';
+                               }
+                               $thing->{v} = $v;
+                       }
+                       $thing->{b} = $dxchan->{build} if $dxchan->{build};
+                       $thing->broadcast($dxchan);
+                       $dxchan->lasthello($main::systime);
+               }
+               if ($dxchan->is_node) {
+                       if ($main::systime >= $dxchan->lastcf + $cf_interval) {
+                               my $call = $dxchan->call;
+                               if ($dxchan == $main::me) {
+
+                                       # i am special but, currently, still a node
+                                       my $thing = Thingy::Rt->new_cf;
+                                       $thing->broadcast;
+                                       $dxchan->lastcf($main::systime);
+                               } else {
+
+                                       # i am a pc protocol node connected directly
+                                       my $thing = Thingy::Rt->new();
+                                       $thing->{user} = $call unless $dxchan == $main::me;
+                                       if (my $nref = Route::Node::get($call)) {
+                                               $thing->copy_pc16_data($nref);
+                                               $thing->broadcast($dxchan);
+                                               $dxchan->lastcf($main::systime);
+                                       } else {
+                                               dbg("Aranea::per_minute: Route::Node for $call disappeared");
+                                               $dxchan->disconnect;
+                                       }
+                               }
+                       }
+               }
+       }
 }
 
 sub disconnect
@@ -154,9 +217,12 @@ sub disconnect
        my $call = $self->call;
 
        return if $self->{disconnecting}++;
-       
+
+       my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
+       $thing->broadcast($self);
+
        # get rid of any PC16/17/19
-#      eph_del_regex("^PC1[679]*$call");
+       DXProt::eph_del_regex("^PC1[679]*$call");
 
        # do routing stuff, remove me from routing table
        my $node = Route::Node::get($call);
@@ -167,7 +233,7 @@ sub disconnect
                # and all my ephemera as well
                for (@rout) {
                        my $c = $_->call;
-#                      eph_del_regex("^PC1[679].*$c");
+                       DXProt::eph_del_regex("^PC1[679].*$c");
                }
        }
 
@@ -178,10 +244,10 @@ sub disconnect
        $mref->stop_msg($call) if $mref;
        
        # broadcast to all other nodes that all the nodes connected to via me are gone
-#      $self->route_pc21($main::mycall, undef, @rout) if @rout;
+       DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
 
        # remove outstanding pings
-#      delete $pings{$call};
+       Thingy::Ping::forget($call);
        
        # I was the last node visited
     $self->user->node($main::mycall);
@@ -199,16 +265,34 @@ sub disconnect
 # because it has to be used before a channel is fully initialised).
 #
 
+sub formathead
+{
+       my $mycall = shift;
+       my $dts = shift;
+       my $hop = shift;
+       my $user = shift;
+       my $group = shift;
+       my $touser = shift;
+       
+       my $s = "$mycall,$dts,$hop";
+       $s .= ",$user" if $user;
+       if ($group) {
+               $s .= "," unless $user;
+               $s .= ",$group" if $group;
+               $s .= ",$touser" if $touser;
+       } 
+       return $s;
+}
+
 sub genheader
 {
        my $mycall = shift;
        my $to = shift;
        my $from = shift;
+       my $touser = shift;
        
        my $date = ((($dayno << 1) | $ntpflag) << 18) |  ($main::systime % 86400);
-       my $r = "$mycall," . sprintf('%6X%04X,0', $date, $seqno);
-       $r .= ",$to" if $to;
-       $r .= ",$from" if $from;
+       my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to, $touser);
        $seqno++;
        $seqno = 0 if $seqno > 0x0ffff;
        return $r;
@@ -221,7 +305,7 @@ sub genheader
 sub decode_dts
 {
        my $dts = shift;
-       my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
+       my ($dt, $seqno) = map {hex} unpack "A6 A4", $dts;
        my $secs = $dt & 0x3FFFF;
        $dt >>= 18;
        my $day = $dt >> 1;
@@ -242,7 +326,7 @@ sub tencode
 {
        my $s = shift;
        $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
-       $s = "'$s'" if $s =~ / /;
+#      $s = "'$s'" if $s =~ / /;
        return $s;
 }
 
@@ -251,22 +335,28 @@ sub tdecode
        my $s = shift;
        $s =~ s/^'(.*)'$/$1/;
        $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
-       return $s;
+       return length $s ? $s : '';
 }
 
 sub genmsg
 {
        my $thing = shift;
-       my $name = shift;
+       my $list = ref $_[0] ? shift : \@_;
+       my ($name) = uc ref $thing;
+       $name =~ /::(\w+)$/;
+       $name = $1;
+       my $group = $thing->{group};
        my $head = genheader($thing->{origin}, 
-                                                ($thing->{group} || $thing->{touser} || $thing->{tonode}),
-                                                ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
+                                                $group,
+                                                ($thing->{user} || $thing->{fromuser} || $thing->{fromnode}),
+                                                $thing->{touser}
                                                );
-       my $data = "$name,";
-       while (@_) {
-               my $k = lc shift;
-               my $v = tencode(shift);
-               $data .= "$k=$v,";
+        
+       my $data = uc $name . ',';
+       while (@$list) {
+               my $k = lc shift @$list;
+               my $v = $thing->{$k};
+               $data .= "$k=" . tencode($v) . ',' if defined $v;
        }
        chop $data;
        return "$head|$data";
@@ -286,7 +376,7 @@ sub input
        my ($head, $data) = split /\|/, $line, 2;
        return unless $head && $data;
 
-       my ($origin, $dts, $hop, $group, $user) = split /,/, $head;
+       my ($origin, $dts, $hop, $user, $group, $tocall) = split /,/, $head;
        return if DXDupe::check("Ara,$origin,$dts", $dupeage);
        my $err;
        $err .= "incomplete header," unless $origin && $dts && defined $hop;
@@ -295,15 +385,16 @@ sub input
        # validate it further
        $err .= "missing cmd or data," unless $cmd && $data;
        $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
-       my ($gp, $tus) = split /:/, $group, 2 if $group;
-               
-       $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
-       $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
+
+       $err .= "from me," if $origin eq $main::mycall;
+       $err .= "invalid group ($group)," if $group && $group !~ /^[-A-Z0-9]{2,}$/;
+       $err .= "invalid tocall ($tocall)," if $tocall && !is_callsign($tocall);
        $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
 
        my $class = 'Thingy::' . ucfirst(lc $cmd);
        my $thing;
        my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
+       dbg("dts: $dts = $ntp $t($main::systime) $seqno") if isdbg('dts');
        $err .= "invalid date/seq," unless $t;
        
        if ($err) {
@@ -314,28 +405,28 @@ sub input
                $thing = $class->new();
 
                # reconstitute the header but wth hop increased by one
-               $head = join(',', $origin, $dts, ++$hop);
-               $head .= ",$group" if $group;
-               $head .= ",$user" if $user;
+               $head = formathead($origin, $dts, ++$hop, $user, $group, $tocall);
                $thing->{Aranea} = "$head|$data";
 
                # store useful data
                $thing->{origin} = $origin;
                $thing->{time} = $t;
-               $thing->{group} = $gp if $gp;
-               $thing->{touser} = $tus if $tus;
+               $thing->{group} = $group if $group;
+               $thing->{touser} = $tocall if $tocall;
                $thing->{user} = $user if $user;
                $thing->{hopsaway} = $hop; 
 
-               for (split(/,/, $rdata)) {
-                       if (/=/) {
-                               my ($k,$v) = split /=/, $_, 2;
-                               $thing->{$k} = tdecode($v);
-                       } else {
-                               $thing->{$_} = 1;
+               if ($rdata) {
+                       for (split(/,/, $rdata)) {
+                               if (/=/) {
+                                       my ($k,$v) = split /=/, $_, 2;
+                                       $thing->{$k} = tdecode($v);
+                               } else {
+                                       $thing->{$_} = 1;
+                               }
                        }
                }
-
+               
                # post process the thing, this generally adds on semantic meaning
                # does parameter checking etc. It also adds / prepares the thingy so
                # this is compatible with older protocol and arranges data so