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?
110 use POSIX ":sys_wait_h";
117 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
118 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
119 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
120 $allowdxby $dbh $dsn $dbuser $dbpass
123 @inqueue = (); # the main input queue, an array of hashes
124 $systime = 0; # the time now (in seconds)
125 $version = "1.52"; # the version no of the software
126 $starttime = 0; # the starting time of the cluster
127 #@outstanding_connects = (); # list of outstanding connects
128 @listeners = (); # list of listeners
129 $reqreg = 0; # 1 = registration required, 2 = deregister people
130 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
131 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
134 use vars qw($VERSION $BRANCH $build $branch);
135 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
136 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
137 $main::build += $VERSION;
138 $main::branch += $BRANCH;
139 $main::build += 1; # fudge (put back for now)
143 # send a message to call on conn and disconnect
146 my ($conn, $call, $mess) = @_;
148 $conn->disable_read(1);
149 dbg("-> D $call $mess\n") if isdbg('chan');
150 $conn->send_now("D$call|$mess");
158 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
159 $dxchan->disconnect(1);
162 # handle incoming messages
165 my ($conn, $msg) = @_;
166 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
167 return unless defined $sort;
169 unless (is_callsign($call)) {
170 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
174 # set up the basic channel info
175 # is there one already connected to me - locally?
176 my $user = DXUser->get_current($call);
177 my $dxchan = DXChannel::get($call);
179 if ($user && $user->is_node) {
180 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
184 my $ip = $conn->{peerhost} || 'unknown';
185 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
186 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
189 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
195 my $basecall = $call;
196 $basecall =~ s/-\d+$//;
197 my $baseuser = DXUser->get_current($basecall);
198 my $lock = $user->lockout if $user;
199 if ($baseuser && $baseuser->lockout || $lock) {
200 if (!$user || !defined $lock || $lock) {
201 my $host = $conn->{peerhost} || "unknown";
202 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
209 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
211 $user = DXUser->new($call);
215 if ($user->is_node) {
216 $dxchan = DXProt->new($call, $conn, $user);
217 } elsif ($user->is_user) {
218 $dxchan = DXCommandmode->new($call, $conn, $user);
219 } elsif ($user->is_bbs) {
220 $dxchan = BBS->new($call, $conn, $user);
222 die "Invalid sort of user on $call = $sort";
225 # check that the conn has a callsign
226 $conn->conns($call) if $conn->isa('IntMsg');
229 $conn->set_error(sub {error_handler($dxchan)});
230 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
237 return \&new_channel;
240 # cease running this program, close down all the connections nicely
246 $SIG{'TERM'} = 'IGNORE';
247 $SIG{'INT'} = 'IGNORE';
253 Local::finish(); # end local processing
255 dbg("Local::finish error $@") if $@;
258 foreach $dxchan (DXChannel::get_all_nodes) {
259 $dxchan->disconnect(2) unless $dxchan == $main::me;
261 Msg->event_loop(100, 0.01);
264 foreach $dxchan (DXChannel::get_all_users) {
271 # disconnect UDP customers
274 # end everything else
275 Msg->event_loop(100, 0.01);
279 # close all databases
282 # close all listeners
283 foreach my $l (@listeners) {
287 LogDbg('cluster', "DXSpider V$version, build $build ended");
291 $dbh->finish if $dbh;
294 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
298 # the reaper of children
302 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
303 dbg("cpid: $cpid") if isdbg('reap');
304 # Msg->pid_gone($cpid);
305 $zombies-- if $zombies > 0;
307 dbg("cpid: $cpid") if isdbg('reap');
310 # this is where the input queue is dealt with and things are dispatched off to other parts of
315 my $t = $systime - $starttime;
316 my $days = int $t / 86400;
318 my $hours = int $t / 3600;
320 my $mins = int $t / 60;
321 return sprintf "%d %02d:%02d", $days, $hours, $mins;
326 AGWMsg::init(\&new_channel);
329 #############################################################
331 # The start of the main line of code
333 #############################################################
335 $starttime = $systime = time;
336 $lang = 'en' unless $lang;
338 unless ($DB::VERSION) {
339 $SIG{INT} = $SIG{TERM} = \&cease;
342 # open the debug file, set various FHs to be unbuffered
343 dbginit(\&DXCommandmode::broadcast_debug);
347 STDOUT->autoflush(1);
349 # calculate build number
350 $build += $main::version;
351 $build = "$build.$branch" if $branch;
353 # try to load the database
354 if ($dsn && -e "$root/perl/DXSql.pm") {
359 $dbh = DXSql->new($dsn);
360 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
364 # try to load XML::Simple
368 my ($year) = (gmtime)[5];
370 LogDbg('cluster', "DXSpider V$version, build $build started");
371 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
374 dbg("loading prefixes ...");
376 my $r = Prefix::init();
380 dbg("loading band data ...");
383 # initialise User file system
384 dbg("loading user file system ...");
385 DXUser->init($userfn, 1);
387 # look for the sysop and the alias user and complain if they aren't there
389 my $ref = DXUser->get($mycall);
390 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
391 $ref = DXUser->get($myalias);
392 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
395 # start listening for incoming messages/connects
396 dbg("starting listeners ...");
397 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
398 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
399 push @listeners, $conn;
400 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
401 foreach my $l (@main::listen) {
403 my $pkg = $l->[2] || 'ExtMsg';
404 my $login = $l->[3] || 'login';
406 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
407 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
408 push @listeners, $conn;
409 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
412 dbg("AGW Listener") if $AGWMsg::enable;
415 dbg("UDP Listener") if $UDPMsg::enable;
416 UDPMsg::init(\&new_channel);
419 dbg("load badwords: " . (BadWords::load or "Ok"));
422 unless ($DB::VERSION) {
423 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
427 $SIG{HUP} = 'IGNORE';
428 $SIG{CHLD} = sub { $zombies++ };
430 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
431 $SIG{IO} = sub { dbg("SIGIO received"); };
432 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
433 $SIG{KILL} = 'DEFAULT'; # as if it matters....
435 # catch the rest with a hopeful message
438 # dbg("Catching SIG $_") if isdbg('chan');
439 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
445 dbg("Starting Dupe system");
448 # read in system messages
449 dbg("Read in Messages");
452 # read in command aliases
453 dbg("Read in Aliases");
456 # initialise the Geomagnetic data engine
462 # initial the Spot stuff
463 dbg("Starting DX Spot system");
466 # initialise the protocol engine
467 dbg("Start Protocol Engines ...");
471 # put in a DXCluster node for us here so we can add users and take them away
472 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
474 # make sure that there is a routing OUTPUT node default file
475 #unless (Filter::read_in('route', 'node_default', 0)) {
476 # my $dxcc = $main::me->dxcc;
477 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
480 # read in any existing message headers and clean out old crap
481 dbg("reading existing message headers ...");
485 # read in any cron jobs
486 dbg("reading cron jobs ...");
489 # read in database descriptors
490 dbg("reading database descriptors ...");
493 # starting local stuff
494 dbg("doing local initialisation ...");
499 dbg("Local::init error $@") if $@;
501 # this, such as it is, is the main loop!
502 dbg("orft we jolly well go ...");
503 my $script = new Script "startup";
504 $script->run($main::me) if $script;
506 #open(DB::OUT, "|tee /tmp/aa");
511 Msg->event_loop(10, 0.010);
514 DXChannel::process();
518 # do timed stuff, ongoing processing happens one a second
519 if ($timenow != $systime) {
522 DXCron::process(); # do cron jobs
523 DXCommandmode::process(); # process ongoing command mode stuff
524 DXProt::process(); # process ongoing ak1a pcxx stuff
526 DXConnect::process();
534 Local::process(); # do any localised processing
536 dbg("Local::process error $@") if $@;
539 last if --$decease <= 0;