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, $BRANCH) = dxver( q$Revision$);
135 $main::build -= 2; # fudge (put back for now)
139 # send a message to call on conn and disconnect
142 my ($conn, $call, $mess) = @_;
144 $conn->disable_read(1);
145 dbg("-> D $call $mess\n") if isdbg('chan');
146 $conn->send_now("D$call|$mess");
154 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
155 $dxchan->disconnect(1);
158 # handle incoming messages
161 my ($conn, $msg) = @_;
162 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
163 return unless defined $sort;
165 unless (is_callsign($call)) {
166 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
170 # set up the basic channel info
171 # is there one already connected to me - locally?
172 my $user = DXUser->get_current($call);
173 my $dxchan = DXChannel::get($call);
175 if ($user && $user->is_node) {
176 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
180 my $ip = $conn->{peerhost} || 'unknown';
181 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
182 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
185 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
191 my $basecall = $call;
192 $basecall =~ s/-\d+$//;
193 my $baseuser = DXUser->get_current($basecall);
194 my $lock = $user->lockout if $user;
195 if ($baseuser && $baseuser->lockout || $lock) {
196 if (!$user || !defined $lock || $lock) {
197 my $host = $conn->{peerhost} || "unknown";
198 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
205 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
207 $user = DXUser->new($call);
211 if ($user->is_node) {
212 $dxchan = DXProt->new($call, $conn, $user);
213 } elsif ($user->is_user) {
214 $dxchan = DXCommandmode->new($call, $conn, $user);
215 } elsif ($user->is_bbs) {
216 $dxchan = BBS->new($call, $conn, $user);
218 die "Invalid sort of user on $call = $sort";
221 # check that the conn has a callsign
222 $conn->conns($call) if $conn->isa('IntMsg');
225 $conn->set_error(sub {error_handler($dxchan)});
226 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
233 return \&new_channel;
236 # cease running this program, close down all the connections nicely
242 $SIG{'TERM'} = 'IGNORE';
243 $SIG{'INT'} = 'IGNORE';
249 Local::finish(); # end local processing
251 dbg("Local::finish error $@") if $@;
254 foreach $dxchan (DXChannel::get_all_nodes) {
255 $dxchan->disconnect(2) unless $dxchan == $main::me;
257 Msg->event_loop(100, 0.01);
260 foreach $dxchan (DXChannel::get_all_users) {
267 # disconnect UDP customers
270 # end everything else
271 Msg->event_loop(100, 0.01);
275 # close all databases
278 # close all listeners
279 foreach my $l (@listeners) {
283 LogDbg('cluster', "DXSpider V$version, build $build ended");
287 $dbh->finish if $dbh;
290 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
294 # the reaper of children
298 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
299 dbg("cpid: $cpid") if isdbg('reap');
300 # Msg->pid_gone($cpid);
301 $zombies-- if $zombies > 0;
303 dbg("cpid: $cpid") if isdbg('reap');
306 # this is where the input queue is dealt with and things are dispatched off to other parts of
311 my $t = $systime - $starttime;
312 my $days = int $t / 86400;
314 my $hours = int $t / 3600;
316 my $mins = int $t / 60;
317 return sprintf "%d %02d:%02d", $days, $hours, $mins;
322 AGWMsg::init(\&new_channel);
325 #############################################################
327 # The start of the main line of code
329 #############################################################
331 $starttime = $systime = time;
332 $systime_days = int ($systime / 86400);
333 $systime_daystart = $systime_days * 86400;
334 $lang = 'en' unless $lang;
336 unless ($DB::VERSION) {
337 $SIG{INT} = $SIG{TERM} = \&cease;
340 # open the debug file, set various FHs to be unbuffered
341 dbginit(\&DXCommandmode::broadcast_debug);
345 STDOUT->autoflush(1);
347 # calculate build number
348 $build += $main::version;
349 $build = "$build.$branch" if $branch;
351 # try to load the database
352 if (DXSql::init($dsn)) {
353 $dbh = DXSql->new($dsn);
354 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
357 # try to load XML::Simple
361 my ($year) = (gmtime)[5];
363 LogDbg('cluster', "DXSpider V$version, build $build started");
364 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
367 dbg("loading prefixes ...");
369 my $r = Prefix::init();
373 dbg("loading band data ...");
376 # initialise User file system
377 dbg("loading user file system ...");
378 DXUser->init($userfn, 1);
380 # look for the sysop and the alias user and complain if they aren't there
382 my $ref = DXUser->get($mycall);
383 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
384 $ref = DXUser->get($myalias);
385 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
388 # start listening for incoming messages/connects
389 dbg("starting listeners ...");
390 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
391 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
392 push @listeners, $conn;
393 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
394 foreach my $l (@main::listen) {
396 my $pkg = $l->[2] || 'ExtMsg';
397 my $login = $l->[3] || 'login';
399 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
400 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
401 push @listeners, $conn;
402 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
405 dbg("AGW Listener") if $AGWMsg::enable;
408 dbg("UDP Listener") if $UDPMsg::enable;
409 UDPMsg::init(\&new_channel);
412 dbg("load badwords: " . (BadWords::load or "Ok"));
415 unless ($DB::VERSION) {
416 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
420 $SIG{HUP} = 'IGNORE';
421 $SIG{CHLD} = sub { $zombies++ };
423 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
424 $SIG{IO} = sub { dbg("SIGIO received"); };
425 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
426 $SIG{KILL} = 'DEFAULT'; # as if it matters....
428 # catch the rest with a hopeful message
431 # dbg("Catching SIG $_") if isdbg('chan');
432 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
438 dbg("Starting Dupe system");
441 # read in system messages
442 dbg("Read in Messages");
445 # read in command aliases
446 dbg("Read in Aliases");
449 # initialise the Geomagnetic data engine
455 # initial the Spot stuff
456 dbg("Starting DX Spot system");
459 # initialise the protocol engine
460 dbg("Start Protocol Engines ...");
463 # put in a DXCluster node for us here so we can add users and take them away
464 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
465 $routeroot->do_pc92(1);
466 $routeroot->via_pc92(1);
468 # make sure that there is a routing OUTPUT node default file
469 #unless (Filter::read_in('route', 'node_default', 0)) {
470 # my $dxcc = $main::me->dxcc;
471 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
474 # read in any existing message headers and clean out old crap
475 dbg("reading existing message headers ...");
479 # read in any cron jobs
480 dbg("reading cron jobs ...");
483 # read in database descriptors
484 dbg("reading database descriptors ...");
487 # starting local stuff
488 dbg("doing local initialisation ...");
493 dbg("Local::init error $@") if $@;
495 # this, such as it is, is the main loop!
496 dbg("orft we jolly well go ...");
497 my $script = new Script "startup";
498 $script->run($main::me) if $script;
500 #open(DB::OUT, "|tee /tmp/aa");
505 Msg->event_loop(10, 0.010);
508 DXChannel::process();
512 # do timed stuff, ongoing processing happens one a second
513 if ($timenow != $systime) {
516 my $days = int ($systime / 86400);
517 if ($systime_days != $days) {
518 $systime_days = $days;
519 $systime_daystart = $days * 86400;
521 IsoTime::update($systime);
522 DXCron::process(); # do cron jobs
523 DXCommandmode::process(); # process ongoing command mode stuff
525 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;