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";
118 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
119 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
120 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
121 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
125 @inqueue = (); # the main input queue, an array of hashes
126 $systime = 0; # the time now (in seconds)
127 $starttime = 0; # the starting time of the cluster
128 @outstanding_connects = (); # list of outstanding connects
129 @listeners = (); # list of listeners
130 $reqreg = 0; # 1 = registration required, 2 = deregister people
131 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
132 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
135 # send a message to call on conn and disconnect
138 my ($conn, $call, $mess) = @_;
140 $conn->disable_read(1);
141 dbg("-> D $call $mess\n") if isdbg('chan');
142 $conn->send_now("D$call|$mess");
150 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
151 $dxchan->disconnect(1);
154 # handle incoming messages
157 my ($conn, $msg) = @_;
158 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
159 return unless defined $sort;
161 unless (is_callsign($call)) {
162 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
166 # set up the basic channel info
167 # is there one already connected to me - locally?
168 my $user = DXUser->get_current($call);
169 my $dxchan = DXChannel::get($call);
171 if ($user && $user->is_node) {
172 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
176 my $ip = $conn->{peerhost} || 'unknown';
177 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
178 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
181 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
187 my $basecall = $call;
188 $basecall =~ s/-\d+$//;
189 my $baseuser = DXUser->get_current($basecall);
190 my $lock = $user->lockout if $user;
191 if ($baseuser && $baseuser->lockout || $lock) {
192 if (!$user || !defined $lock || $lock) {
193 my $host = $conn->{peerhost} || "unknown";
194 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
201 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
203 $user = DXUser->new($call);
207 if ($user->is_node) {
208 $dxchan = DXProt->new($call, $conn, $user);
209 } elsif ($user->is_user) {
210 $dxchan = DXCommandmode->new($call, $conn, $user);
211 } elsif ($user->is_bbs) {
212 $dxchan = BBS->new($call, $conn, $user);
214 die "Invalid sort of user on $call = $sort";
217 # check that the conn has a callsign
218 $conn->conns($call) if $conn->isa('IntMsg');
221 $conn->set_error(sub {error_handler($dxchan)});
222 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
229 return \&new_channel;
232 # cease running this program, close down all the connections nicely
238 $SIG{'TERM'} = 'IGNORE';
239 $SIG{'INT'} = 'IGNORE';
244 if (defined &Local::finish) {
246 Local::finish(); # end local processing
248 dbg("Local::finish error $@") if $@;
252 foreach $dxchan (DXChannel::get_all_nodes) {
253 $dxchan->disconnect(2) unless $dxchan == $main::me;
255 Msg->event_loop(100, 0.01);
258 foreach $dxchan (DXChannel::get_all_users) {
266 # disconnect UDP customers
269 # end everything else
270 Msg->event_loop(100, 0.01);
274 # close all databases
277 # close all listeners
278 foreach my $l (@listeners) {
282 LogDbg('cluster', "DXSpider V$version, build $subversion.$build ended");
286 $dbh->finish if $dbh;
289 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
293 # the reaper of children
297 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
298 dbg("cpid: $cpid") if isdbg('reap');
299 # Msg->pid_gone($cpid);
300 $zombies-- if $zombies > 0;
302 dbg("cpid: $cpid") if isdbg('reap');
305 # this is where the input queue is dealt with and things are dispatched off to other parts of
310 my $t = $systime - $starttime;
311 my $days = int $t / 86400;
313 my $hours = int $t / 3600;
315 my $mins = int $t / 60;
316 return sprintf "%d %02d:%02d", $days, $hours, $mins;
321 AGWMsg::init(\&new_channel);
324 #############################################################
326 # The start of the main line of code
328 #############################################################
330 $starttime = $systime = time;
331 $systime_days = int ($systime / 86400);
332 $systime_daystart = $systime_days * 86400;
333 $lang = 'en' unless $lang;
335 unless ($DB::VERSION) {
336 $SIG{INT} = $SIG{TERM} = \&cease;
339 # open the debug file, set various FHs to be unbuffered
340 dbginit(\&DXCommandmode::broadcast_debug);
344 STDOUT->autoflush(1);
346 # try to load the database
347 if (DXSql::init($dsn)) {
348 $dbh = DXSql->new($dsn);
349 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
355 my $w = $SIG{__DIE__};
356 $SIG{__DIE__} = 'IGNORE';
357 eval { require Encode; };
365 # try to load XML::Simple
369 my ($year) = (gmtime)[5];
371 LogDbg('cluster', "DXSpider V$version, build $subversion.$build started");
372 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
375 dbg("loading prefixes ...");
377 my $r = Prefix::init();
381 dbg("loading band data ...");
384 # initialise User file system
385 dbg("loading user file system ...");
386 DXUser->init($userfn, 1);
388 # look for the sysop and the alias user and complain if they aren't there
390 my $ref = DXUser->get($mycall);
391 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
392 $ref = DXUser->get($myalias);
393 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
396 # start listening for incoming messages/connects
397 dbg("starting listeners ...");
398 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
399 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
400 push @listeners, $conn;
401 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
402 foreach my $l (@main::listen) {
404 my $pkg = $l->[2] || 'ExtMsg';
405 my $login = $l->[3] || 'login';
407 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
408 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
409 push @listeners, $conn;
410 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
413 dbg("AGW Listener") if $AGWMsg::enable;
416 dbg("BPQ Listener") if $BPQMsg::enable;
417 BPQMsg::init(\&new_channel);
419 dbg("UDP Listener") if $UDPMsg::enable;
420 UDPMsg::init(\&new_channel);
423 dbg("load badwords: " . (BadWords::load or "Ok"));
426 unless ($DB::VERSION) {
427 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
431 $SIG{HUP} = 'IGNORE';
432 $SIG{CHLD} = sub { $zombies++ };
434 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
435 $SIG{IO} = sub { dbg("SIGIO received"); };
436 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
437 $SIG{KILL} = 'DEFAULT'; # as if it matters....
439 # catch the rest with a hopeful message
442 # dbg("Catching SIG $_") if isdbg('chan');
443 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
449 dbg("Starting Dupe system");
452 # read in system messages
453 dbg("Read in Messages");
456 # read in command aliases
457 dbg("Read in Aliases");
460 # initialise the Geomagnetic data engine
466 # initial the Spot stuff
467 dbg("Starting DX Spot system");
470 # initialise the protocol engine
471 dbg("Start Protocol Engines ...");
474 # put in a DXCluster node for us here so we can add users and take them away
475 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
476 $routeroot->do_pc9x(1);
477 $routeroot->via_pc92(1);
479 # make sure that there is a routing OUTPUT node default file
480 #unless (Filter::read_in('route', 'node_default', 0)) {
481 # my $dxcc = $main::me->dxcc;
482 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
485 # read in any existing message headers and clean out old crap
486 dbg("reading existing message headers ...");
490 # read in any cron jobs
491 dbg("reading cron jobs ...");
494 # read in database desriptors
495 dbg("reading database descriptors ...");
498 # starting local stuff
499 dbg("doing local initialisation ...");
501 if (defined &Local::init) {
505 dbg("Local::init error $@") if $@;
509 # this, such as it is, is the main loop!
510 dbg("orft we jolly well go ...");
511 my $script = new Script "startup";
512 $script->run($main::me) if $script;
514 #open(DB::OUT, "|tee /tmp/aa");
519 Msg->event_loop(10, 0.010);
522 DXChannel::process();
526 # do timed stuff, ongoing processing happens one a second
527 if ($timenow != $systime) {
530 my $days = int ($systime / 86400);
531 if ($systime_days != $days) {
532 $systime_days = $days;
533 $systime_daystart = $days * 86400;
535 IsoTime::update($systime);
536 DXCron::process(); # do cron jobs
537 DXCommandmode::process(); # process ongoing command mode stuff
539 DXProt::process(); # process ongoing ak1a pcxx stuff
540 DXConnect::process();
548 if (defined &Local::process) {
550 Local::process(); # do any localised processing
552 dbg("Local::process error $@") if $@;
556 last if --$decease <= 0;