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?
109 use POSIX ":sys_wait_h";
116 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
117 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
118 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
119 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
122 @inqueue = (); # the main input queue, an array of hashes
123 $systime = 0; # the time now (in seconds)
124 $version = "1.53"; # the version no of the software
125 $starttime = 0; # the starting time of the cluster
126 #@outstanding_connects = (); # list of outstanding connects
127 @listeners = (); # list of listeners
128 $reqreg = 0; # 1 = registration required, 2 = deregister people
129 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
130 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
133 use vars qw($VERSION $BRANCH $build $branch);
134 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
135 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
136 $main::build += $VERSION;
137 $main::branch += $BRANCH;
138 $main::build -= 3; # fudge (put back for now)
142 # send a message to call on conn and disconnect
145 my ($conn, $call, $mess) = @_;
147 $conn->disable_read(1);
148 dbg("-> D $call $mess\n") if isdbg('chan');
149 $conn->send_now("D$call|$mess");
157 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
158 $dxchan->disconnect(1);
161 # handle incoming messages
164 my ($conn, $msg) = @_;
165 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
166 return unless defined $sort;
168 unless (is_callsign($call)) {
169 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
173 # set up the basic channel info
174 # is there one already connected to me - locally?
175 my $user = DXUser->get_current($call);
176 my $dxchan = DXChannel::get($call);
178 if ($user && $user->is_node) {
179 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
183 my $ip = $conn->{peerhost} || 'unknown';
184 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
185 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
188 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
194 my $basecall = $call;
195 $basecall =~ s/-\d+$//;
196 my $baseuser = DXUser->get_current($basecall);
197 my $lock = $user->lockout if $user;
198 if ($baseuser && $baseuser->lockout || $lock) {
199 if (!$user || !defined $lock || $lock) {
200 my $host = $conn->{peerhost} || "unknown";
201 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
208 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
210 $user = DXUser->new($call);
214 if ($user->is_node) {
215 $dxchan = DXProt->new($call, $conn, $user);
216 } elsif ($user->is_user) {
217 $dxchan = DXCommandmode->new($call, $conn, $user);
218 } elsif ($user->is_bbs) {
219 $dxchan = BBS->new($call, $conn, $user);
221 die "Invalid sort of user on $call = $sort";
224 # check that the conn has a callsign
225 $conn->conns($call) if $conn->isa('IntMsg');
228 $conn->set_error(sub {error_handler($dxchan)});
229 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
236 return \&new_channel;
239 # cease running this program, close down all the connections nicely
245 $SIG{'TERM'} = 'IGNORE';
246 $SIG{'INT'} = 'IGNORE';
252 Local::finish(); # end local processing
254 dbg("Local::finish error $@") if $@;
257 foreach $dxchan (DXChannel::get_all_nodes) {
258 $dxchan->disconnect(2) unless $dxchan == $main::me;
260 Msg->event_loop(100, 0.01);
263 foreach $dxchan (DXChannel::get_all_users) {
270 # disconnect UDP customers
273 # end everything else
274 Msg->event_loop(100, 0.01);
278 # close all databases
281 # close all listeners
282 foreach my $l (@listeners) {
286 LogDbg('cluster', "DXSpider V$version, build $build ended");
290 $dbh->finish if $dbh;
293 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
297 # the reaper of children
301 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
302 dbg("cpid: $cpid") if isdbg('reap');
303 # Msg->pid_gone($cpid);
304 $zombies-- if $zombies > 0;
306 dbg("cpid: $cpid") if isdbg('reap');
309 # this is where the input queue is dealt with and things are dispatched off to other parts of
314 my $t = $systime - $starttime;
315 my $days = int $t / 86400;
317 my $hours = int $t / 3600;
319 my $mins = int $t / 60;
320 return sprintf "%d %02d:%02d", $days, $hours, $mins;
325 AGWMsg::init(\&new_channel);
328 #############################################################
330 # The start of the main line of code
332 #############################################################
334 $starttime = $systime = time;
335 $systime_days = int ($systime / 86400);
336 $systime_daystart = $systime_days * 86400;
337 $lang = 'en' unless $lang;
339 unless ($DB::VERSION) {
340 $SIG{INT} = $SIG{TERM} = \&cease;
343 # open the debug file, set various FHs to be unbuffered
344 dbginit(\&DXCommandmode::broadcast_debug);
348 STDOUT->autoflush(1);
350 # calculate build number
351 $build += $main::version;
352 $build = "$build.$branch" if $branch;
354 # try to load the database
355 if (DXSql::init($dsn)) {
356 $dbh = DXSql->new($dsn);
357 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
360 # try to load XML::Simple
364 my ($year) = (gmtime)[5];
366 LogDbg('cluster', "DXSpider V$version, build $build started");
367 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
370 dbg("loading prefixes ...");
372 my $r = Prefix::init();
376 dbg("loading band data ...");
379 # initialise User file system
380 dbg("loading user file system ...");
381 DXUser->init($userfn, 1);
383 # look for the sysop and the alias user and complain if they aren't there
385 my $ref = DXUser->get($mycall);
386 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
387 $ref = DXUser->get($myalias);
388 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
391 # start listening for incoming messages/connects
392 dbg("starting listeners ...");
393 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
394 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
395 push @listeners, $conn;
396 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
397 foreach my $l (@main::listen) {
399 my $pkg = $l->[2] || 'ExtMsg';
400 my $login = $l->[3] || 'login';
402 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
403 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
404 push @listeners, $conn;
405 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
408 dbg("AGW Listener") if $AGWMsg::enable;
411 dbg("UDP Listener") if $UDPMsg::enable;
412 UDPMsg::init(\&new_channel);
415 dbg("load badwords: " . (BadWords::load or "Ok"));
418 unless ($DB::VERSION) {
419 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
423 $SIG{HUP} = 'IGNORE';
424 $SIG{CHLD} = sub { $zombies++ };
426 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
427 $SIG{IO} = sub { dbg("SIGIO received"); };
428 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
429 $SIG{KILL} = 'DEFAULT'; # as if it matters....
431 # catch the rest with a hopeful message
434 # dbg("Catching SIG $_") if isdbg('chan');
435 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
441 dbg("Starting Dupe system");
444 # read in system messages
445 dbg("Read in Messages");
448 # read in command aliases
449 dbg("Read in Aliases");
452 # initialise the Geomagnetic data engine
458 # initial the Spot stuff
459 dbg("Starting DX Spot system");
462 # initialise the protocol engine
463 dbg("Start Protocol Engines ...");
466 # put in a DXCluster node for us here so we can add users and take them away
467 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
469 # make sure that there is a routing OUTPUT node default file
470 #unless (Filter::read_in('route', 'node_default', 0)) {
471 # my $dxcc = $main::me->dxcc;
472 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
475 # read in any existing message headers and clean out old crap
476 dbg("reading existing message headers ...");
480 # read in any cron jobs
481 dbg("reading cron jobs ...");
484 # read in database descriptors
485 dbg("reading database descriptors ...");
488 # starting local stuff
489 dbg("doing local initialisation ...");
494 dbg("Local::init error $@") if $@;
496 # this, such as it is, is the main loop!
497 dbg("orft we jolly well go ...");
498 my $script = new Script "startup";
499 $script->run($main::me) if $script;
501 #open(DB::OUT, "|tee /tmp/aa");
506 Msg->event_loop(10, 0.010);
509 DXChannel::process();
513 # do timed stuff, ongoing processing happens one a second
514 if ($timenow != $systime) {
517 my $days = int ($systime / 86400);
518 if ($systime_days != $days) {
519 $systime_days = $days;
520 $systime_daystart = $days * 86400;
522 IsoTime::update($systime);
523 DXCron::process(); # do cron jobs
524 DXCommandmode::process(); # process ongoing command mode stuff
526 DXProt::process(); # process ongoing ak1a pcxx stuff
527 DXConnect::process();
535 Local::process(); # do any localised processing
537 dbg("Local::process error $@") if $@;
540 last if --$decease <= 0;