try again
[spider.git] / perl / cluster.pl
index eef7a40cd02d4fa5c970517192c35d3aba9aa689..0c12e3a9cfa332047fbb5025b12c0efde3ff2fbf 100755 (executable)
@@ -36,11 +36,15 @@ BEGIN {
        open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
        print CLLOCK "$$\n";
        close CLLOCK;
+
+       $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
+
 }
 
 use Msg;
 use IntMsg;
 use ExtMsg;
+use AGWMsg;
 use DXVars;
 use DXDebug;
 use DXLog;
@@ -67,6 +71,7 @@ use DXDupe;
 use BadWords;
 
 use Data::Dumper;
+use IO::File;
 use Fcntl ':flock'; 
 use POSIX ":sys_wait_h";
 
@@ -74,9 +79,11 @@ use Local;
 
 package main;
 
-#use strict;
-#use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects $zombies $root
-#                 $lang $myalias @debug $userfn $clusteraddr $clusterport $mycall $decease );
+use strict;
+use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects 
+                       $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr 
+                       $clusterport $mycall $decease $build $is_win
+                  );
 
 @inqueue = ();                                 # the main input queue, an array of hashes
 $systime = 0;                                  # the time now (in seconds)
@@ -94,73 +101,80 @@ sub already_conn
        
        dbg('chan', "-> D $call $mess\n"); 
        $conn->send_now("D$call|$mess");
-       sleep(2);
+       Msg->sleep(2);
        $conn->disconnect;
 }
 
 sub error_handler
 {
        my $dxchan = shift;
-       $dxchan->disconnect;
+       $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
+       $dxchan->disconnect(1);
 }
 
 # handle incoming messages
-sub rec
+sub new_channel
 {
        my ($conn, $msg) = @_;
-       my $dxchan = DXChannel->get_by_cnum($conn); # get the dxconnnect object for this message
        my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
        return unless defined $sort;
        
        # set up the basic channel info
-       if (!defined $dxchan) {
-
-               # is there one already connected to me - locally? 
-               my $user = DXUser->get($call);
-               if ($sort ne 'O' && Msg->conns($call)) {
-                       my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
-                       already_conn($conn, $call, $mess);
-                       return;
-               }
-               
-               # is there one already connected elsewhere in the cluster?
-               if ($user) {
-                       if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
-                               ;
-                       } else {
-                               if (my $ref = DXCluster->get_exact($call)) {
-                                       my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
-                                       already_conn($conn, $call, $mess);
-                                       return;
-                               }
-                       }
-                       $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
+       # is there one already connected to me - locally? 
+       my $user = DXUser->get($call);
+       my $dxchan = DXChannel->get($call);
+       if ($dxchan) {
+               my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
+               already_conn($conn, $call, $mess);
+               return;
+       }
+       
+       # is there one already connected elsewhere in the cluster?
+       if ($user) {
+               if (($user->is_node || $call eq $myalias) && !DXCluster->get_exact($call)) {
+                       ;
                } else {
                        if (my $ref = DXCluster->get_exact($call)) {
                                my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
                                already_conn($conn, $call, $mess);
                                return;
                        }
-                       $user = DXUser->new($call);
                }
-
-               # is he locked out ?
-               if ($user->lockout) {
-                       Log('DXCommand', "$call is locked out, disconnected");
-                       $conn->disconnect;
+               $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
+       } else {
+               if (my $ref = DXCluster->get_exact($call)) {
+                       my $mess = DXM::msg($lang, 'concluster', $call, $ref->mynode->call);
+                       already_conn($conn, $call, $mess);
                        return;
                }
-
-               # mark him up
-               $conn->conns($call) unless $sort eq 'O';
-               $conn->set_error(sub {error_handler($dxchan)});
-               
-               # create the channel
-               $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
-               $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
-               $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
-               die "Invalid sort of user on $call = $sort" if !$dxchan;
+               $user = DXUser->new($call);
+       }
+       
+       # is he locked out ?
+       if ($user->lockout) {
+               Log('DXCommand', "$call is locked out, disconnected");
+               $conn->disconnect;
+               return;
        }
+
+       # create the channel
+       $dxchan = DXCommandmode->new($call, $conn, $user) if $user->is_user;
+       $dxchan = DXProt->new($call, $conn, $user) if $user->is_node;
+       $dxchan = BBS->new($call, $conn, $user) if $user->is_bbs;
+       die "Invalid sort of user on $call = $sort" if !$dxchan;
+
+       # check that the conn has a callsign
+       $conn->conns($call) if $conn->isa('IntMsg');
+
+       # set callbacks
+       $conn->set_error(sub {error_handler($dxchan)});
+       $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
+       rec($dxchan, $conn, $msg);
+}
+
+sub rec        
+{
+       my ($dxchan, $conn, $msg) = @_;
        
        # queue the message and the channel object for later processing
        if (defined $msg) {
@@ -173,7 +187,7 @@ sub rec
 
 sub login
 {
-       return \&rec;
+       return \&new_channel;
 }
 
 # cease running this program, close down all the connections nicely
@@ -204,6 +218,10 @@ sub cease
                next if $dxchan->is_node;
                $dxchan->disconnect unless $dxchan == $DXProt::me;
        }
+
+       # disconnect AGW
+       AGWMsg::finish();
+       
        Msg->event_loop(1, 0.05);
        Msg->event_loop(1, 0.05);
        Msg->event_loop(1, 0.05);
@@ -221,8 +239,8 @@ sub cease
                $_->close_server;
        }
 
-       dbg('chan', "DXSpider version $version ended");
-       Log('cluster', "DXSpider V$version stopped");
+       dbg('chan', "DXSpider version $version, build $build ended");
+       Log('cluster', "DXSpider V$version, build $build ended");
        dbgclose();
        Logclose();
        unlink $lockfn;
@@ -268,7 +286,6 @@ sub process_inqueue
                $dxchan->normal($line);
                $dxchan->disconnect if ($dxchan->{state} eq 'bye');
        } elsif ($sort eq 'Z') {
-               $dxchan->conn(undef);
                $dxchan->disconnect;
        } elsif ($sort eq 'D') {
                ;                       # ignored (an echo)
@@ -303,10 +320,30 @@ foreach (@debug) {
 }
 STDOUT->autoflush(1);
 
-Log('cluster', "DXSpider V$version started");
+# calculate build number
+$build = $main::version;
+
+if (opendir(DIR, "$main::root/perl")) {
+       my @d = readdir(DIR);
+       closedir(DIR);
+       foreach my $fn (@d) {
+               if ($fn =~ /^cluster\.pl$/ || $fn =~ /\.pm$/) {
+                       my $f = new IO::File "$main::root/perl/$fn" or next;
+                       while (<$f>) {
+                               if (/^#\s+\$Id:\s+[\w\._]+,v\s+(\d+\.\d+)/ ) {
+                                       $build += $1;
+                                       last;
+                               }
+                       }
+                       $f->close;
+               }
+       }
+}
+
+Log('cluster', "DXSpider V$version, build $build started");
 
 # banner
-dbg('err', "DXSpider DX Cluster Version $version", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
+dbg('err', "DXSpider Version $version, build $build started", "Copyright (c) 1998-2001 Dirk Koopman G1TLH");
 
 # load Prefixes
 dbg('err', "loading prefixes ...");
@@ -334,12 +371,13 @@ for (@main::listen) {
        push @listeners, $conn;
        dbg('err', "External Port: $_->[0] $_->[1]");
 }
+AGWMsg::init(\&new_channel);
 
 # load bad words
 dbg('err', "load badwords: " . (BadWords::load or "Ok"));
 
 # prime some signals
-unless ($^O =~ /^MS/) {
+unless ($is_win) {
        unless ($DB::VERSION) {
                $SIG{INT} = \&cease;
                $SIG{TERM} = \&cease;
@@ -415,7 +453,7 @@ dbg('err', "orft we jolly well go ...");
 for (;;) {
 #      $DB::trace = 1;
        
-       Msg->event_loop(10, 0.001);
+       Msg->event_loop(10, 0.010);
        my $timenow = time;
        process_inqueue();                      # read in lines from the input queue and despatch them
 #      $DB::trace = 0;
@@ -432,7 +470,8 @@ for (;;) {
                DXDb::process();
                DXUser::process();
                DXDupe::process();
-               
+               AGWMsg::process();
+                               
                eval { 
                        Local::process();       # do any localised processing
                };