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 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
169 dbg("DXSpider Version $version, build $build started");
172 dbg("loading prefixes ...");
174 my $r = Prefix::init();
178 dbg("loading band data ...");
181 # initialise User file system
182 dbg("loading user file system ...");
183 DXUser->init($userfn, 1);
185 # look for the sysop and the alias user and complain if they aren't there
187 my $ref = DXUser->get($mycall);
188 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
189 $ref = DXUser->get($myalias);
190 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
193 # start listening for incoming messages/connects
194 dbg("starting listeners ...");
195 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
196 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
197 push @listeners, $conn;
198 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
199 foreach my $l (@main::listen) {
201 my $pkg = $l->[2] || 'ExtMsg';
202 my $login = $l->[3] || 'login';
204 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
205 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
206 push @listeners, $conn;
207 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
210 dbg("AGW Listener") if $AGWMsg::enable;
213 dbg("UDP Listener") if $UDPMsg::enable;
214 UDPMsg::init(\&new_channel);
217 dbg("load badwords: " . (BadWords::load or "Ok"));
220 unless ($DB::VERSION) {
221 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
225 $SIG{HUP} = 'IGNORE';
226 $SIG{CHLD} = sub { $zombies++ };
228 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
229 $SIG{IO} = sub { dbg("SIGIO received"); };
230 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
231 $SIG{KILL} = 'DEFAULT'; # as if it matters....
233 # catch the rest with a hopeful message
236 # dbg("Catching SIG $_") if isdbg('chan');
237 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
243 dbg("Starting Dupe system");
246 # read in system messages
247 dbg("Read in Messages");
250 # read in command aliases
251 dbg("Read in Aliases");
254 # initialise the Geomagnetic data engine
260 # initial the Spot stuff
261 dbg("Starting DX Spot system");
264 # initialise the protocol engine
265 dbg("Start Protocol Engines ...");
269 # put in a DXCluster node for us here so we can add users and take them away
270 $routeroot = Route::Node->new($mycall, int($version*100)+$DXProt::myprot_version, $main::me->here);
272 # make sure that there is a routing OUTPUT node default file
273 #unless (Filter::read_in('route', 'node_default', 0)) {
274 # my $dxcc = $main::me->dxcc;
275 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
278 # read in any existing message headers and clean out old crap
279 dbg("reading existing message headers ...");
283 # read in any cron jobs
284 dbg("reading cron jobs ...");
287 # read in database descriptors
288 dbg("reading database descriptors ...");
291 # starting local stuff
292 dbg("doing local initialisation ...");
297 dbg("Local::init error $@") if $@;
299 # this, such as it is, is the main loop!
300 dbg("orft we jolly well go ...");
301 my $script = new Script "startup";
302 $script->run($main::me) if $script;
304 #open(DB::OUT, "|tee /tmp/aa");
309 Msg->event_loop(10, 0.010);
312 DXChannel::process();
317 # do timed stuff, ongoing processing happens one a second
318 if ($timenow != $systime) {
319 rand(); # keep randomising to reduce (but not eliminate) predictability
322 DXCron::process(); # do cron jobs
323 DXCommandmode::process(); # process ongoing command mode stuff
324 DXProt::process(); # process ongoing ak1a pcxx stuff
326 DXConnect::process();
334 Local::process(); # do any localised processing
336 dbg("Local::process error $@") if $@;
339 last if --$decease <= 0;
346 # send a message to call on conn and disconnect
349 my ($conn, $call, $mess) = @_;
351 $conn->disable_read(1);
352 dbg("-> D $call $mess\n") if isdbg('chan');
353 $conn->send_now("D$call|$mess");
361 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
362 $dxchan->disconnect(1);
365 # handle incoming messages
368 my ($conn, $msg) = @_;
369 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
370 return unless defined $sort;
372 unless (is_callsign($call)) {
373 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
377 # set up the basic channel info
378 # is there one already connected to me - locally?
379 my $user = DXUser->get_current($call);
380 my $dxchan = DXChannel::get($call);
382 if ($user && $user->is_node) {
383 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
386 if ($bumpexisting && $call ne $main::mycall) {
387 my $ip = $conn->{peerhost} || 'unknown';
388 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
389 Log('DXCommand', "$call bumped off by $ip, disconnected");
390 dbg("$call bumped off by $ip, disconnected");
393 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
399 my $basecall = $call;
400 $basecall =~ s/-\d+$//;
401 my $baseuser = DXUser->get_current($basecall);
402 my $lock = $user->lockout if $user;
403 if ($baseuser && $baseuser->lockout || $lock) {
404 if (!$user || !defined $lock || $lock) {
405 my $host = $conn->{peerhost} || "unknown";
406 Log('DXCommand', "$call on $host is locked out, disconnected");
413 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
415 $user = DXUser->new($call);
419 if ($user->is_node) {
420 $dxchan = DXProt->new($call, $conn, $user);
421 } elsif ($user->is_user) {
422 $dxchan = DXCommandmode->new($call, $conn, $user);
423 } elsif ($user->is_bbs) {
424 $dxchan = BBS->new($call, $conn, $user);
426 die "Invalid sort of user on $call = $sort";
429 # check that the conn has a callsign
430 $conn->conns($call) if $conn->isa('IntMsg');
433 $conn->set_error(sub {error_handler($dxchan)});
434 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
441 return \&new_channel;
444 # cease running this program, close down all the connections nicely
450 $SIG{'TERM'} = 'IGNORE';
451 $SIG{'INT'} = 'IGNORE';
457 Local::finish(); # end local processing
459 dbg("Local::finish error $@") if $@;
462 foreach $dxchan (grep {$_->is_node || $_->is_aranea} DXChannel::get_all()) {
463 $dxchan->disconnect(2) unless $dxchan == $main::me;
465 Msg->event_loop(100, 0.01);
468 foreach $dxchan (DXChannel::get_all_users) {
475 # disconnect UDP customers
478 # end everything else
479 Msg->event_loop(100, 0.01);
483 # close all databases
486 # close all listeners
487 foreach my $l (@listeners) {
491 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
492 Log('cluster', "DXSpider V$version, build $build ended");
496 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
500 # the reaper of children
504 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
505 dbg("cpid: $cpid") if isdbg('reap');
506 # Msg->pid_gone($cpid);
507 $zombies-- if $zombies > 0;
509 dbg("cpid: $cpid") if isdbg('reap');
512 # this is where the input queue is dealt with and things are dispatched off to other parts of
517 my $t = $systime - $starttime;
518 my $days = int $t / 86400;
520 my $hours = int $t / 3600;
522 my $mins = int $t / 60;
523 return sprintf "%d %02d:%02d", $days, $hours, $mins;
528 AGWMsg::init(\&new_channel);