-# handle incoming messages
-sub new_channel
-{
- my ($conn, $msg) = @_;
- my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
- return unless defined $sort;
-
- # set up the basic channel info
- # 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->{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;
- }
- $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) {
- my $self = bless {}, "inqueue";
- $self->{dxchan} = $dxchan;
- $self->{data} = $msg;
- push @inqueue, $self;
- }
-}
-
-sub login
-{
- return \&new_channel;
-}
-
-# cease running this program, close down all the connections nicely
-sub cease
-{
- my $dxchan;
-
- $SIG{'TERM'} = 'IGNORE';
- $SIG{'INT'} = 'IGNORE';
-
- DXUser::sync;
-
- eval {
- Local::finish(); # end local processing
- };
- dbg('local', "Local::finish error $@") if $@;
-
- # disconnect nodes
- foreach $dxchan (DXChannel->get_all_nodes) {
- $dxchan->disconnect(2) unless $dxchan == $DXProt::me;
- }
- Msg->event_loop(100, 0.01);
-
- # disconnect users
- foreach $dxchan (DXChannel->get_all_users) {
- $dxchan->disconnect;
- }
-
- # disconnect AGW
- AGWMsg::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('chan', "DXSpider version $version, build $build ended");
- 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('reap', "cpid: $cpid");
-# Msg->pid_gone($cpid);
- $zombies-- if $zombies > 0;
- }
- dbg('reap', "cpid: $cpid");
-}
-
-# this is where the input queue is dealt with and things are dispatched off to other parts of
-# the cluster
-sub process_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('chan', "<- $sort $call $line\n") unless $sort eq 'D';
-
- # 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);
- $dxchan->disconnect if ($dxchan->{state} eq 'bye');
- } elsif ($sort eq 'Z') {
- $dxchan->disconnect;
- } elsif ($sort eq 'D') {
- ; # ignored (an echo)
- } 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;
-}
-
-sub AGWrestart
-{
- AGWMsg::init(\&new_channel);
-}