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";
117 use vars qw(@inqueue $systime $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 $do_xml $systime_days $systime_daystart
124 @inqueue = (); # the main input queue, an array of hashes
125 $systime = 0; # the time now (in seconds)
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 # 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 if ($user && $user->is_node) {
171 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
175 my $ip = $conn->{peerhost} || 'unknown';
176 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
177 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
180 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
186 my $basecall = $call;
187 $basecall =~ s/-\d+$//;
188 my $baseuser = DXUser->get_current($basecall);
189 my $lock = $user->lockout if $user;
190 if ($baseuser && $baseuser->lockout || $lock) {
191 if (!$user || !defined $lock || $lock) {
192 my $host = $conn->{peerhost} || "unknown";
193 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
200 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
202 $user = DXUser->new($call);
206 if ($user->is_node) {
207 $dxchan = DXProt->new($call, $conn, $user);
208 } elsif ($user->is_user) {
209 $dxchan = DXCommandmode->new($call, $conn, $user);
210 } elsif ($user->is_bbs) {
211 $dxchan = BBS->new($call, $conn, $user);
213 die "Invalid sort of user on $call = $sort";
216 # check that the conn has a callsign
217 $conn->conns($call) if $conn->isa('IntMsg');
220 $conn->set_error(sub {error_handler($dxchan)});
221 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
228 return \&new_channel;
231 # cease running this program, close down all the connections nicely
237 $SIG{'TERM'} = 'IGNORE';
238 $SIG{'INT'} = 'IGNORE';
244 Local::finish(); # end local processing
246 dbg("Local::finish error $@") if $@;
249 foreach $dxchan (DXChannel::get_all_nodes) {
250 $dxchan->disconnect(2) unless $dxchan == $main::me;
252 Msg->event_loop(100, 0.01);
255 foreach $dxchan (DXChannel::get_all_users) {
262 # disconnect UDP customers
265 # end everything else
266 Msg->event_loop(100, 0.01);
270 # close all databases
273 # close all listeners
274 foreach my $l (@listeners) {
278 LogDbg('cluster', "DXSpider V$version, build $build ended");
282 $dbh->finish if $dbh;
285 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
289 # the reaper of children
293 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
294 dbg("cpid: $cpid") if isdbg('reap');
295 # Msg->pid_gone($cpid);
296 $zombies-- if $zombies > 0;
298 dbg("cpid: $cpid") if isdbg('reap');
301 # this is where the input queue is dealt with and things are dispatched off to other parts of
306 my $t = $systime - $starttime;
307 my $days = int $t / 86400;
309 my $hours = int $t / 3600;
311 my $mins = int $t / 60;
312 return sprintf "%d %02d:%02d", $days, $hours, $mins;
317 AGWMsg::init(\&new_channel);
320 #############################################################
322 # The start of the main line of code
324 #############################################################
326 $starttime = $systime = time;
327 $systime_days = int ($systime / 86400);
328 $systime_daystart = $systime_days * 86400;
329 $lang = 'en' unless $lang;
331 unless ($DB::VERSION) {
332 $SIG{INT} = $SIG{TERM} = \&cease;
335 # open the debug file, set various FHs to be unbuffered
336 dbginit(\&DXCommandmode::broadcast_debug);
340 STDOUT->autoflush(1);
342 # try to load the database
343 if (DXSql::init($dsn)) {
344 $dbh = DXSql->new($dsn);
345 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
351 my $w = $SIG{__DIE__};
352 $SIG{__DIE__} = 'IGNORE';
353 eval { require Encode; };
361 # try to load XML::Simple
365 my ($year) = (gmtime)[5];
367 LogDbg('cluster', "DXSpider V$version, build $build started");
368 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
371 dbg("loading prefixes ...");
373 my $r = Prefix::init();
377 dbg("loading band data ...");
380 # initialise User file system
381 dbg("loading user file system ...");
382 DXUser->init($userfn, 1);
384 # look for the sysop and the alias user and complain if they aren't there
386 my $ref = DXUser->get($mycall);
387 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
388 $ref = DXUser->get($myalias);
389 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
392 # start listening for incoming messages/connects
393 dbg("starting listeners ...");
394 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
395 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
396 push @listeners, $conn;
397 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
398 foreach my $l (@main::listen) {
400 my $pkg = $l->[2] || 'ExtMsg';
401 my $login = $l->[3] || 'login';
403 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
404 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
405 push @listeners, $conn;
406 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
409 dbg("AGW Listener") if $AGWMsg::enable;
412 dbg("UDP Listener") if $UDPMsg::enable;
413 UDPMsg::init(\&new_channel);
416 dbg("load badwords: " . (BadWords::load or "Ok"));
419 unless ($DB::VERSION) {
420 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
424 $SIG{HUP} = 'IGNORE';
425 $SIG{CHLD} = sub { $zombies++ };
427 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
428 $SIG{IO} = sub { dbg("SIGIO received"); };
429 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
430 $SIG{KILL} = 'DEFAULT'; # as if it matters....
432 # catch the rest with a hopeful message
435 # dbg("Catching SIG $_") if isdbg('chan');
436 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
442 dbg("Starting Dupe system");
445 # read in system messages
446 dbg("Read in Messages");
449 # read in command aliases
450 dbg("Read in Aliases");
453 # initialise the Geomagnetic data engine
459 # initial the Spot stuff
460 dbg("Starting DX Spot system");
463 # initialise the protocol engine
464 dbg("Start Protocol Engines ...");
467 # put in a DXCluster node for us here so we can add users and take them away
468 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
469 $routeroot->do_pc9x(1);
470 $routeroot->via_pc92(1);
472 # make sure that there is a routing OUTPUT node default file
473 #unless (Filter::read_in('route', 'node_default', 0)) {
474 # my $dxcc = $main::me->dxcc;
475 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
478 # read in any existing message headers and clean out old crap
479 dbg("reading existing message headers ...");
483 # read in any cron jobs
484 dbg("reading cron jobs ...");
487 # read in database descriptors
488 dbg("reading database descriptors ...");
491 # starting local stuff
492 dbg("doing local initialisation ...");
497 dbg("Local::init error $@") if $@;
499 # this, such as it is, is the main loop!
500 dbg("orft we jolly well go ...");
501 my $script = new Script "startup";
502 $script->run($main::me) if $script;
504 #open(DB::OUT, "|tee /tmp/aa");
509 Msg->event_loop(10, 0.010);
512 DXChannel::process();
516 # do timed stuff, ongoing processing happens one a second
517 if ($timenow != $systime) {
520 my $days = int ($systime / 86400);
521 if ($systime_days != $days) {
522 $systime_days = $days;
523 $systime_daystart = $days * 86400;
525 IsoTime::update($systime);
526 DXCron::process(); # do cron jobs
527 DXCommandmode::process(); # process ongoing command mode stuff
529 DXProt::process(); # process ongoing ak1a pcxx stuff
530 DXConnect::process();
538 Local::process(); # do any localised processing
540 dbg("Local::process error $@") if $@;
543 last if --$decease <= 0;