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
122 @inqueue = (); # the main input queue, an array of hashes
123 $systime = 0; # the time now (in seconds)
124 $version = "1.52"; # 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 # send a message to call on conn and disconnect
136 my ($conn, $call, $mess) = @_;
138 $conn->disable_read(1);
139 dbg("-> D $call $mess\n") if isdbg('chan');
140 $conn->send_now("D$call|$mess");
148 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
149 $dxchan->disconnect(1);
152 # handle incoming messages
155 my ($conn, $msg) = @_;
156 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
157 return unless defined $sort;
159 unless (is_callsign($call)) {
160 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
164 # set up the basic channel info
165 # is there one already connected to me - locally?
166 my $user = DXUser->get_current($call);
167 my $dxchan = DXChannel::get($call);
169 if ($user && $user->is_node) {
170 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
174 my $ip = $conn->{peerhost} || 'unknown';
175 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
176 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
179 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
185 my $basecall = $call;
186 $basecall =~ s/-\d+$//;
187 my $baseuser = DXUser->get_current($basecall);
188 my $lock = $user->lockout if $user;
189 if ($baseuser && $baseuser->lockout || $lock) {
190 if (!$user || !defined $lock || $lock) {
191 my $host = $conn->{peerhost} || "unknown";
192 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
199 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
201 $user = DXUser->new($call);
205 if ($user->is_node) {
206 $dxchan = DXProt->new($call, $conn, $user);
207 } elsif ($user->is_user) {
208 $dxchan = DXCommandmode->new($call, $conn, $user);
209 } elsif ($user->is_bbs) {
210 $dxchan = BBS->new($call, $conn, $user);
212 die "Invalid sort of user on $call = $sort";
215 # check that the conn has a callsign
216 $conn->conns($call) if $conn->isa('IntMsg');
219 $conn->set_error(sub {error_handler($dxchan)});
220 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
227 return \&new_channel;
230 # cease running this program, close down all the connections nicely
236 $SIG{'TERM'} = 'IGNORE';
237 $SIG{'INT'} = 'IGNORE';
243 Local::finish(); # end local processing
245 dbg("Local::finish error $@") if $@;
248 foreach $dxchan (DXChannel::get_all_nodes) {
249 $dxchan->disconnect(2) unless $dxchan == $main::me;
251 Msg->event_loop(100, 0.01);
254 foreach $dxchan (DXChannel::get_all_users) {
261 # disconnect UDP customers
264 # end everything else
265 Msg->event_loop(100, 0.01);
269 # close all databases
272 # close all listeners
273 foreach my $l (@listeners) {
277 LogDbg('cluster', "DXSpider V$version, build $build ended");
281 $dbh->finish if $dbh;
284 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
288 # the reaper of children
292 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
293 dbg("cpid: $cpid") if isdbg('reap');
294 # Msg->pid_gone($cpid);
295 $zombies-- if $zombies > 0;
297 dbg("cpid: $cpid") if isdbg('reap');
300 # this is where the input queue is dealt with and things are dispatched off to other parts of
305 my $t = $systime - $starttime;
306 my $days = int $t / 86400;
308 my $hours = int $t / 3600;
310 my $mins = int $t / 60;
311 return sprintf "%d %02d:%02d", $days, $hours, $mins;
316 AGWMsg::init(\&new_channel);
319 #############################################################
321 # The start of the main line of code
323 #############################################################
325 $starttime = $systime = time;
326 $lang = 'en' unless $lang;
328 unless ($DB::VERSION) {
329 $SIG{INT} = $SIG{TERM} = \&cease;
332 # open the debug file, set various FHs to be unbuffered
333 dbginit(\&DXCommandmode::broadcast_debug);
337 STDOUT->autoflush(1);
339 # calculate build number
340 $build += $main::version;
341 $build = "$build.$branch" if $branch;
343 # try to load the database
344 if (DXSql::init($dsn)) {
345 $dbh = DXSql->new($dsn);
346 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
349 # try to load XML::Simple
353 my ($year) = (gmtime)[5];
355 LogDbg('cluster', "DXSpider V$version, build $build started");
356 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
359 dbg("loading prefixes ...");
361 my $r = Prefix::init();
365 dbg("loading band data ...");
368 # initialise User file system
369 dbg("loading user file system ...");
370 DXUser->init($userfn, 1);
372 # look for the sysop and the alias user and complain if they aren't there
374 my $ref = DXUser->get($mycall);
375 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
376 $ref = DXUser->get($myalias);
377 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
380 # start listening for incoming messages/connects
381 dbg("starting listeners ...");
382 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
383 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
384 push @listeners, $conn;
385 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
386 foreach my $l (@main::listen) {
388 my $pkg = $l->[2] || 'ExtMsg';
389 my $login = $l->[3] || 'login';
391 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
392 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
393 push @listeners, $conn;
394 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
397 dbg("AGW Listener") if $AGWMsg::enable;
400 dbg("UDP Listener") if $UDPMsg::enable;
401 UDPMsg::init(\&new_channel);
404 dbg("load badwords: " . (BadWords::load or "Ok"));
407 unless ($DB::VERSION) {
408 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
412 $SIG{HUP} = 'IGNORE';
413 $SIG{CHLD} = sub { $zombies++ };
415 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
416 $SIG{IO} = sub { dbg("SIGIO received"); };
417 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
418 $SIG{KILL} = 'DEFAULT'; # as if it matters....
420 # catch the rest with a hopeful message
423 # dbg("Catching SIG $_") if isdbg('chan');
424 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
430 dbg("Starting Dupe system");
433 # read in system messages
434 dbg("Read in Messages");
437 # read in command aliases
438 dbg("Read in Aliases");
441 # initialise the Geomagnetic data engine
447 # initial the Spot stuff
448 dbg("Starting DX Spot system");
451 # initialise the protocol engine
452 dbg("Start Protocol Engines ...");
455 # put in a DXCluster node for us here so we can add users and take them away
456 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
458 # make sure that there is a routing OUTPUT node default file
459 #unless (Filter::read_in('route', 'node_default', 0)) {
460 # my $dxcc = $main::me->dxcc;
461 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
464 # read in any existing message headers and clean out old crap
465 dbg("reading existing message headers ...");
469 # read in any cron jobs
470 dbg("reading cron jobs ...");
473 # read in database descriptors
474 dbg("reading database descriptors ...");
477 # starting local stuff
478 dbg("doing local initialisation ...");
483 dbg("Local::init error $@") if $@;
485 # this, such as it is, is the main loop!
486 dbg("orft we jolly well go ...");
487 my $script = new Script "startup";
488 $script->run($main::me) if $script;
490 #open(DB::OUT, "|tee /tmp/aa");
495 Msg->event_loop(10, 0.010);
498 DXChannel::process();
502 # do timed stuff, ongoing processing happens one a second
503 if ($timenow != $systime) {
505 IsoTime::update($systime = $timenow);
506 DXCron::process(); # do cron jobs
507 DXCommandmode::process(); # process ongoing command mode stuff
509 DXProt::process(); # process ongoing ak1a pcxx stuff
510 DXConnect::process();
518 Local::process(); # do any localised processing
520 dbg("Local::process error $@") if $@;
523 last if --$decease <= 0;