3 # This is the DX cluster 'daemon'. It sits in the middle of its little
4 # web of client routines sucking and blowing data where it may.
6 # Hence the name of 'spider' (although it may become 'dxspider')
8 # Copyright (c) 1998 Dirk Koopman G1TLH
15 # make sure that modules are searched in the order local then perl
19 # root of directory tree for this system
21 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
23 unshift @INC, "$root/perl"; # this IS the right way round!
24 unshift @INC, "$root/local";
26 # do some validation of the input
27 die "The directory $root doesn't exist, please RTFM" unless -d $root;
28 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
29 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
31 mkdir "$root/local_cmd", 0777 unless -d "$root/local_cmd";
34 # try to create and lock a lockfile (this isn't atomic but
36 $lockfn = "$root/local/cluster.lck"; # lock file name
38 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
41 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
44 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
48 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
106 use POSIX ":sys_wait_h";
113 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
114 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
115 $clusterport $mycall $decease $is_win $routeroot $me $reqreg
118 @inqueue = (); # the main input queue, an array of hashes
119 $systime = 0; # the time now (in seconds)
120 $version = "1.51"; # the version no of the software
121 $starttime = 0; # the starting time of the cluster
122 #@outstanding_connects = (); # list of outstanding connects
123 @listeners = (); # list of listeners
124 $reqreg = 0; # 1 = registration required, 2 = deregister people
126 use vars qw($VERSION $BRANCH $build $branch);
127 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
128 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
129 $main::build += 5; # add an offset to make it bigger than last system
130 $main::build += $VERSION;
131 $main::branch += $BRANCH;
134 # send a message to call on conn and disconnect
137 my ($conn, $call, $mess) = @_;
139 $conn->disable_read(1);
140 dbg("-> D $call $mess\n") if isdbg('chan');
141 $conn->send_now("D$call|$mess");
149 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
150 $dxchan->disconnect(1);
153 # handle incoming messages
156 my ($conn, $msg) = @_;
157 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
158 return unless defined $sort;
160 unless (is_callsign($call)) {
161 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
165 # set up the basic channel info
166 # is there one already connected to me - locally?
167 my $user = DXUser->get_current($call);
168 my $dxchan = DXChannel->get($call);
170 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
171 already_conn($conn, $call, $mess);
176 my $basecall = $call;
177 $basecall =~ s/-\d+$//;
178 my $baseuser = DXUser->get_current($basecall);
179 my $lock = $user->lockout if $user;
180 if ($baseuser && $baseuser->lockout || $lock) {
181 if (!$user || !defined $lock || $lock) {
182 my $host = $conn->{peerhost} || "unknown";
183 Log('DXCommand', "$call on $host is locked out, disconnected");
190 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
192 $user = DXUser->new($call);
198 if ($user->passphrase && $main::me->user->passphrase) {
199 $dxchan = QXProt->new($call, $conn, $user);
201 unless ($user->passphrase) {
202 Log('DXCommand', "$call using NP but has no passphrase");
203 dbg("$call using NP but has no passphrase");
205 unless ($main::me->user->passphrase) {
206 Log('DXCommand', "$main::mycall using NP but has no passphrase");
207 dbg("$main::mycall using NP but has no passphrase");
209 already_conn($conn, $call, "Need to exchange passphrases");
212 } elsif ($user->is_node) {
213 $dxchan = DXProt->new($call, $conn, $user);
214 } elsif ($user->is_user) {
215 $dxchan = DXCommandmode->new($call, $conn, $user);
216 } elsif ($user->is_bbs) {
217 $dxchan = BBS->new($call, $conn, $user);
219 die "Invalid sort of user on $call = $sort";
222 # check that the conn has a callsign
223 $conn->conns($call) if $conn->isa('IntMsg');
226 $conn->set_error(sub {error_handler($dxchan)});
227 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
228 rec($dxchan, $conn, $msg);
233 my ($dxchan, $conn, $msg) = @_;
235 # queue the message and the channel object for later processing
237 my $self = bless {}, "inqueue";
238 $self->{dxchan} = $dxchan;
239 $self->{data} = $msg;
240 push @inqueue, $self;
244 # remove any outstanding entries on the inqueue after a disconnection (usually)
248 @inqueue = grep {$_->{dxchan} != $dxchan} @inqueue;
253 return \&new_channel;
256 # cease running this program, close down all the connections nicely
262 $SIG{'TERM'} = 'IGNORE';
263 $SIG{'INT'} = 'IGNORE';
269 Local::finish(); # end local processing
271 dbg("Local::finish error $@") if $@;
274 foreach $dxchan (DXChannel->get_all_nodes) {
275 $dxchan->disconnect(2) unless $dxchan == $main::me;
277 Msg->event_loop(100, 0.01);
280 foreach $dxchan (DXChannel->get_all_users) {
287 # disconnect UDP customers
290 # end everything else
291 Msg->event_loop(100, 0.01);
295 # close all databases
298 # close all listeners
299 foreach my $l (@listeners) {
303 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
304 Log('cluster', "DXSpider V$version, build $build ended");
308 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
312 # the reaper of children
316 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
317 dbg("cpid: $cpid") if isdbg('reap');
318 # Msg->pid_gone($cpid);
319 $zombies-- if $zombies > 0;
321 dbg("cpid: $cpid") if isdbg('reap');
324 # this is where the input queue is dealt with and things are dispatched off to other parts of
329 my $self = shift @inqueue;
332 my $data = $self->{data};
333 my $dxchan = $self->{dxchan};
335 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
336 return unless defined $sort;
338 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
339 dbg("<- $sort $call $line") if $sort ne 'D' && isdbg('chan');
340 if ($self->{disconnecting}) {
341 dbg('In disconnection, ignored');
346 my $user = $dxchan->user;
347 if ($sort eq 'A' || $sort eq 'O') {
348 $dxchan->start($line, $sort);
349 } elsif ($sort eq 'I') {
350 die "\$user not defined for $call" if !defined $user;
353 $dxchan->normal($line);
354 } elsif ($sort eq 'Z') {
356 } elsif ($sort eq 'D') {
357 ; # ignored (an echo)
358 } elsif ($sort eq 'G') {
359 $dxchan->enhanced($line);
361 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
368 my $t = $systime - $starttime;
369 my $days = int $t / 86400;
371 my $hours = int $t / 3600;
373 my $mins = int $t / 60;
374 return sprintf "%d %02d:%02d", $days, $hours, $mins;
379 AGWMsg::init(\&new_channel);
382 #############################################################
384 # The start of the main line of code
386 #############################################################
388 $starttime = $systime = time;
389 $lang = 'en' unless $lang;
391 # open the debug file, set various FHs to be unbuffered
392 dbginit(\&DXCommandmode::broadcast_debug);
396 STDOUT->autoflush(1);
398 # calculate build number
399 $build += $main::version;
400 $build = "$build.$branch" if $branch;
402 Log('cluster', "DXSpider V$version, build $build started");
405 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
406 dbg("DXSpider Version $version, build $build started");
409 dbg("loading prefixes ...");
410 my $r = Prefix::init();
415 dbg("loading band data ...");
418 # initialise User file system
419 dbg("loading user file system ...");
420 DXUser->init($userfn, 1);
422 # look for the sysop and the alias user and complain if they aren't there
424 my $ref = DXUser->get($mycall);
425 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
426 $ref = DXUser->get($myalias);
427 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
430 # start listening for incoming messages/connects
431 dbg("starting listeners ...");
432 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
433 $conn->conns("Server $clusteraddr/$clusterport");
434 push @listeners, $conn;
435 dbg("Internal port: $clusteraddr $clusterport");
436 foreach my $l (@main::listen) {
437 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
438 $conn->conns("Server $l->[0]/$l->[1]");
439 push @listeners, $conn;
440 dbg("External Port: $l->[0] $l->[1]");
443 dbg("AGW Listener") if $AGWMsg::enable;
446 dbg("UDP Listener") if $UDPMsg::enable;
447 UDPMsg::init(\&new_channel);
450 dbg("load badwords: " . (BadWords::load or "Ok"));
453 unless ($DB::VERSION) {
454 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
458 $SIG{HUP} = 'IGNORE';
459 $SIG{CHLD} = sub { $zombies++ };
461 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
462 $SIG{IO} = sub { dbg("SIGIO received"); };
463 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
464 $SIG{KILL} = 'DEFAULT'; # as if it matters....
466 # catch the rest with a hopeful message
469 # dbg("Catching SIG $_") if isdbg('chan');
470 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
476 dbg("Starting Dupe system");
479 # read in system messages
480 dbg("Read in Messages");
483 # read in command aliases
484 dbg("Read in Aliases");
487 # initialise the Geomagnetic data engine
493 # initial the Spot stuff
494 dbg("Starting DX Spot system");
497 # initialise the protocol engine
498 dbg("Start Protocol Engines ...");
502 # put in a DXCluster node for us here so we can add users and take them away
503 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
505 # make sure that there is a routing OUTPUT node default file
506 #unless (Filter::read_in('route', 'node_default', 0)) {
507 # my $dxcc = $main::me->dxcc;
508 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
511 # read in any existing message headers and clean out old crap
512 dbg("reading existing message headers ...");
516 # read in any cron jobs
517 dbg("reading cron jobs ...");
520 # read in database descriptors
521 dbg("reading database descriptors ...");
524 # starting local stuff
525 dbg("doing local initialisation ...");
530 dbg("Local::init error $@") if $@;
532 # this, such as it is, is the main loop!
533 dbg("orft we jolly well go ...");
534 my $script = new Script "startup";
535 $script->run($main::me) if $script;
537 #open(DB::OUT, "|tee /tmp/aa");
542 Msg->event_loop(10, 0.010);
544 process_inqueue(); # read in lines from the input queue and despatch them
547 # do timed stuff, ongoing processing happens one a second
548 if ($timenow != $systime) {
551 DXCron::process(); # do cron jobs
552 DXCommandmode::process(); # process ongoing command mode stuff
553 DXProt::process(); # process ongoing ak1a pcxx stuff
555 DXConnect::process();
563 Local::process(); # do any localised processing
565 dbg("Local::process error $@") if $@;
568 last if --$decease <= 0;