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?
54 my ($v, $b) = $s =~ /(\d+\.\d+)(?:\.(\d+\.\d+))?/;
55 $main::build += sprintf "%.3f", $v;
56 $main::branch += sprintf("%.3f", $b) if $b;
117 use POSIX ":sys_wait_h";
124 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
125 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
126 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
130 @inqueue = (); # the main input queue, an array of hashes
131 $systime = 0; # the time now (in seconds)
132 $version = "2.01"; # the version no of the software
133 $starttime = 0; # the starting time of the cluster
134 #@outstanding_connects = (); # list of outstanding connects
135 @listeners = (); # list of listeners
136 $reqreg = 0; # 1 = registration required, 2 = deregister people
137 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
138 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
141 use vars qw($VERSION $BRANCH $build $branch);
143 mkver($VERSION = q$Revision$);
145 #############################################################
147 # The start of the main line of code
149 #############################################################
151 $starttime = $systime = time;
152 $lang = 'en' unless $lang;
154 # open the debug file, set various FHs to be unbuffered
155 dbginit(\&DXCommandmode::broadcast_debug);
159 STDOUT->autoflush(1);
161 # calculate build number
162 $build += $main::version;
163 $build = "$build.$branch" if $branch;
165 Log('cluster', "DXSpider V$version, build $build started");
168 my ($year) = (gmtime)[5];
170 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
171 dbg("DXSpider Version $version, build $build started");
174 dbg("loading prefixes ...");
176 my $r = Prefix::init();
180 dbg("loading band data ...");
183 # initialise User file system
184 dbg("loading user file system ...");
185 DXUser->init($userfn, 1);
187 # look for the sysop and the alias user and complain if they aren't there
189 my $ref = DXUser->get($mycall);
190 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
191 $ref = DXUser->get($myalias);
192 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
195 # start listening for incoming messages/connects
196 dbg("starting listeners ...");
197 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
198 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
199 push @listeners, $conn;
200 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
201 foreach my $l (@main::listen) {
203 my $pkg = $l->[2] || 'ExtMsg';
204 my $login = $l->[3] || 'login';
206 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
207 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
208 push @listeners, $conn;
209 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
212 dbg("AGW Listener") if $AGWMsg::enable;
215 dbg("UDP Listener") if $UDPMsg::enable;
216 UDPMsg::init(\&new_channel);
219 dbg("load badwords: " . (BadWords::load or "Ok"));
222 unless ($DB::VERSION) {
223 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
227 $SIG{HUP} = 'IGNORE';
228 $SIG{CHLD} = sub { $zombies++ };
230 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
231 $SIG{IO} = sub { dbg("SIGIO received"); };
232 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
233 $SIG{KILL} = 'DEFAULT'; # as if it matters....
235 # catch the rest with a hopeful message
238 # dbg("Catching SIG $_") if isdbg('chan');
239 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
245 dbg("Starting Dupe system");
248 # read in system messages
249 dbg("Read in Messages");
252 # read in command aliases
253 dbg("Read in Aliases");
256 # initialise the Geomagnetic data engine
262 # initial the Spot stuff
263 dbg("Starting DX Spot system");
266 # initialise the protocol engine
267 dbg("Start Protocol Engines ...");
271 # put in a DXCluster node for us here so we can add users and take them away
272 $routeroot = Route::Node->new($mycall, int($version*100)+$DXProt::myprot_version, $main::me->here);
274 # make sure that there is a routing OUTPUT node default file
275 #unless (Filter::read_in('route', 'node_default', 0)) {
276 # my $dxcc = $main::me->dxcc;
277 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
280 # read in any existing message headers and clean out old crap
281 dbg("reading existing message headers ...");
285 # read in any cron jobs
286 dbg("reading cron jobs ...");
289 # read in database descriptors
290 dbg("reading database descriptors ...");
293 # starting local stuff
294 dbg("doing local initialisation ...");
299 dbg("Local::init error $@") if $@;
301 # this, such as it is, is the main loop!
302 dbg("orft we jolly well go ...");
303 my $script = new Script "startup";
304 $script->run($main::me) if $script;
306 #open(DB::OUT, "|tee /tmp/aa");
311 Msg->event_loop(10, 0.010);
314 DXChannel::process();
319 # do timed stuff, ongoing processing happens one a second
320 if ($timenow != $systime) {
321 rand(); # keep randomising to reduce (but not eliminate) predictability
324 DXCron::process(); # do cron jobs
325 DXCommandmode::process(); # process ongoing command mode stuff
326 DXProt::process(); # process ongoing ak1a pcxx stuff
328 DXConnect::process();
336 Local::process(); # do any localised processing
338 dbg("Local::process error $@") if $@;
341 last if --$decease <= 0;
348 # send a message to call on conn and disconnect
351 my ($conn, $call, $mess) = @_;
353 $conn->disable_read(1);
354 dbg("-> D $call $mess\n") if isdbg('chan');
355 $conn->send_now("D$call|$mess");
363 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
364 $dxchan->disconnect(1);
367 # handle incoming messages
370 my ($conn, $msg) = @_;
371 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
372 return unless defined $sort;
374 unless (is_callsign($call)) {
375 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
379 # set up the basic channel info
380 # is there one already connected to me - locally?
381 my $user = DXUser->get_current($call);
382 my $dxchan = DXChannel::get($call);
384 if ($user && $user->is_node) {
385 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
388 if ($bumpexisting && $call ne $main::mycall) {
389 my $ip = $conn->{peerhost} || 'unknown';
390 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
391 Log('DXCommand', "$call bumped off by $ip, disconnected");
392 dbg("$call bumped off by $ip, disconnected");
395 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
401 my $basecall = $call;
402 $basecall =~ s/-\d+$//;
403 my $baseuser = DXUser->get_current($basecall);
404 my $lock = $user->lockout if $user;
405 if ($baseuser && $baseuser->lockout || $lock) {
406 if (!$user || !defined $lock || $lock) {
407 my $host = $conn->{peerhost} || "unknown";
408 Log('DXCommand', "$call on $host is locked out, disconnected");
415 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
417 $user = DXUser->new($call);
421 if ($user->is_node) {
422 $dxchan = DXProt->new($call, $conn, $user);
423 } elsif ($user->is_user) {
424 $dxchan = DXCommandmode->new($call, $conn, $user);
425 } elsif ($user->is_bbs) {
426 $dxchan = BBS->new($call, $conn, $user);
428 die "Invalid sort of user on $call = $sort";
431 # check that the conn has a callsign
432 $conn->conns($call) if $conn->isa('IntMsg');
435 $conn->set_error(sub {error_handler($dxchan)});
436 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
443 return \&new_channel;
446 # cease running this program, close down all the connections nicely
452 $SIG{'TERM'} = 'IGNORE';
453 $SIG{'INT'} = 'IGNORE';
459 Local::finish(); # end local processing
461 dbg("Local::finish error $@") if $@;
464 foreach $dxchan (grep {$_->is_node || $_->is_aranea} DXChannel::get_all()) {
465 $dxchan->disconnect(2) unless $dxchan == $main::me;
467 Msg->event_loop(100, 0.01);
470 foreach $dxchan (DXChannel::get_all_users) {
477 # disconnect UDP customers
480 # end everything else
481 Msg->event_loop(100, 0.01);
485 # close all databases
488 # close all listeners
489 foreach my $l (@listeners) {
493 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
494 Log('cluster', "DXSpider V$version, build $build ended");
498 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
502 # the reaper of children
506 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
507 dbg("cpid: $cpid") if isdbg('reap');
508 # Msg->pid_gone($cpid);
509 $zombies-- if $zombies > 0;
511 dbg("cpid: $cpid") if isdbg('reap');
514 # this is where the input queue is dealt with and things are dispatched off to other parts of
519 my $t = $systime - $starttime;
520 my $days = int $t / 86400;
522 my $hours = int $t / 3600;
524 my $mins = int $t / 60;
525 return sprintf "%d %02d:%02d", $days, $hours, $mins;
530 AGWMsg::init(\&new_channel);