- # set up the basic channel info
- # is there one already connected to me - locally?
- my $user = DXUser->get_current($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 he locked out ?
- my $basecall = $call;
- $basecall =~ s/-\d+$//;
- my $baseuser = DXUser->get_current($basecall);
- my $lock = $user->lockout if $user;
- if ($baseuser && $baseuser->lockout || $lock) {
- if (!$user || !defined $lock || $lock) {
- my $host = $conn->{peerhost} || "unknown";
- Log('DXCommand', "$call on $host is locked out, disconnected");
- $conn->disconnect;
- return;
- }
- }
-
- if ($user) {
- $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
- } else {
- $user = DXUser->new($call);
- }
-
- # create the channel
- if ($user->is_node) {
- $dxchan = DXProt->new($call, $conn, $user);
- } elsif ($user->is_user) {
- $dxchan = DXCommandmode->new($call, $conn, $user);
- } elsif ($user->is_bbs) {
- $dxchan = BBS->new($call, $conn, $user);
- } else {
- die "Invalid sort of user on $call = $sort";
- }
-
- # 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) {
- my $self = bless {}, "inqueue";
- $self->{dxchan} = $dxchan;
- $self->{data} = $msg;
- push @inqueue, $self;
- }
-}
-
-# remove any outstanding entries on the inqueue after a disconnection (usually)
-sub clean_inqueue
-{
- my $dxchan = shift;
- @inqueue = grep {$_->{dxchan} != $dxchan} @inqueue;
-}
-
-sub login
-{
- return \&new_channel;
-}
-
-# cease running this program, close down all the connections nicely
-sub cease
-{
- my $dxchan;
-
- unless ($is_win) {
- $SIG{'TERM'} = 'IGNORE';
- $SIG{'INT'} = 'IGNORE';
- }
-
- DXUser::sync;
-
- eval {
- Local::finish(); # end local processing
- };
- dbg("Local::finish error $@") if $@;
-
- # disconnect nodes
- foreach $dxchan (DXChannel->get_all_nodes) {
- $dxchan->disconnect(2) unless $dxchan == $main::me;
- }
- Msg->event_loop(100, 0.01);
-
- # disconnect users
- foreach $dxchan (DXChannel->get_all_users) {
- $dxchan->disconnect;
- }
-
- # disconnect AGW
- AGWMsg::finish();
-
- # disconnect UDP customers
- UDPMsg::finish();
-
- # end everything else
- Msg->event_loop(100, 0.01);
- DXUser::finish();
- DXDupe::finish();
-
- # close all databases
- DXDb::closeall;
-
- # close all listeners
- foreach my $l (@listeners) {
- $l->close_server;
- }
-
- dbg("DXSpider version $version, build $build ended") if isdbg('chan');
- Log('cluster', "DXSpider V$version, build $build ended");
- dbgclose();
- Logclose();
- unlink $lockfn;
-# $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
- exit(0);
-}
-
-# the reaper of children
-sub reap
-{
- my $cpid;
- while (($cpid = waitpid(-1, WNOHANG)) > 0) {
- dbg("cpid: $cpid") if isdbg('reap');
-# Msg->pid_gone($cpid);
- $zombies-- if $zombies > 0;
- }
- dbg("cpid: $cpid") if isdbg('reap');
-}
-
-# this is where the input queue is dealt with and things are dispatched off to other parts of
-# the cluster
-sub process_inqueue
-{
- while (@inqueue) {
- my $self = shift @inqueue;
- return if !$self;
-
- my $data = $self->{data};
- my $dxchan = $self->{dxchan};
- my $error;
- my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
- return unless defined $sort;
-
- # do the really sexy console interface bit! (Who is going to do the TK interface then?)
- dbg("<- $sort $call $line") if $sort ne 'D' && isdbg('chan');
- if ($self->{disconnecting}) {
- dbg('In disconnection, ignored');
- next;
- }
-
- # handle A records
- my $user = $dxchan->user;
- if ($sort eq 'A' || $sort eq 'O') {
- $dxchan->start($line, $sort);
- } elsif ($sort eq 'I') {
- die "\$user not defined for $call" if !defined $user;
-
- # normal input
- $dxchan->normal($line);
- } elsif ($sort eq 'Z') {
- $dxchan->disconnect;
- } elsif ($sort eq 'D') {
- ; # ignored (an echo)
- } elsif ($sort eq 'G') {
- $dxchan->enhanced($line);
- } else {
- print STDERR atime, " Unknown command letter ($sort) received from $call\n";
- }
- }
-}
-
-sub uptime
-{
- my $t = $systime - $starttime;
- my $days = int $t / 86400;
- $t -= $days * 86400;
- my $hours = int $t / 3600;
- $t -= $hours * 3600;
- my $mins = int $t / 60;
- return sprintf "%d %02d:%02d", $days, $hours, $mins;
-}