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/perl/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?
103 use POSIX ":sys_wait_h";
110 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
111 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
112 $clusterport $mycall $decease $is_win $routeroot $me $reqreg
115 @inqueue = (); # the main input queue, an array of hashes
116 $systime = 0; # the time now (in seconds)
117 $version = "1.50"; # the version no of the software
118 $starttime = 0; # the starting time of the cluster
119 #@outstanding_connects = (); # list of outstanding connects
120 @listeners = (); # list of listeners
121 $reqreg = 0; # 1 = registration required, 2 = deregister people
123 use vars qw($VERSION $BRANCH $build $branch);
124 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
125 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
126 $main::build += 10; # add an offset to make it bigger than last system
127 $main::build += $VERSION;
128 $main::branch += $BRANCH;
131 # send a message to call on conn and disconnect
134 my ($conn, $call, $mess) = @_;
136 $conn->disable_read(1);
137 dbg("-> D $call $mess\n") if isdbg('chan');
138 $conn->send_now("D$call|$mess");
146 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
147 $dxchan->disconnect(1);
150 # handle incoming messages
153 my ($conn, $msg) = @_;
154 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
155 return unless defined $sort;
157 unless (is_callsign($call)) {
158 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
162 # set up the basic channel info
163 # is there one already connected to me - locally?
164 my $user = DXUser->get_current($call);
165 my $dxchan = DXChannel->get($call);
167 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
168 already_conn($conn, $call, $mess);
173 my $basecall = $call;
174 $basecall =~ s/-\d+$//;
175 my $baseuser = DXUser->get_current($basecall);
176 my $lock = $user->lockout if $user;
177 if ($baseuser && $baseuser->lockout || $lock) {
178 if (!$user || !defined $lock || $lock) {
179 my $host = $conn->{peerhost} || "unknown";
180 Log('DXCommand', "$call on $host is locked out, disconnected");
187 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
189 $user = DXUser->new($call);
195 if ($user->passphrase && $main::me->user->passphrase) {
196 $dxchan = QXProt->new($call, $conn, $user);
198 unless ($user->passphrase) {
199 Log('DXCommand', "$call using NP but has no passphrase");
200 dbg("$call using NP but has no passphrase");
202 unless ($main::me->user->passphrase) {
203 Log('DXCommand', "$main::mycall using NP but has no passphrase");
204 dbg("$main::mycall using NP but has no passphrase");
206 already_conn($conn, $call, "Need to exchange passphrases");
209 } elsif ($user->is_node) {
210 $dxchan = DXProt->new($call, $conn, $user);
211 } elsif ($user->is_user) {
212 $dxchan = DXCommandmode->new($call, $conn, $user);
213 } elsif ($user->is_bbs) {
214 $dxchan = BBS->new($call, $conn, $user);
216 die "Invalid sort of user on $call = $sort";
219 # check that the conn has a callsign
220 $conn->conns($call) if $conn->isa('IntMsg');
223 $conn->set_error(sub {error_handler($dxchan)});
224 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
225 rec($dxchan, $conn, $msg);
230 my ($dxchan, $conn, $msg) = @_;
232 # queue the message and the channel object for later processing
234 my $self = bless {}, "inqueue";
235 $self->{dxchan} = $dxchan;
236 $self->{data} = $msg;
237 push @inqueue, $self;
243 return \&new_channel;
246 # cease running this program, close down all the connections nicely
252 $SIG{'TERM'} = 'IGNORE';
253 $SIG{'INT'} = 'IGNORE';
259 Local::finish(); # end local processing
261 dbg("Local::finish error $@") if $@;
264 foreach $dxchan (DXChannel->get_all_nodes) {
265 $dxchan->disconnect(2) unless $dxchan == $main::me;
267 Msg->event_loop(100, 0.01);
270 foreach $dxchan (DXChannel->get_all_users) {
277 # end everything else
278 Msg->event_loop(100, 0.01);
282 # close all databases
285 # close all listeners
286 foreach my $l (@listeners) {
290 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
291 Log('cluster', "DXSpider V$version, build $build ended");
295 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
299 # the reaper of children
303 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
304 dbg("cpid: $cpid") if isdbg('reap');
305 # Msg->pid_gone($cpid);
306 $zombies-- if $zombies > 0;
308 dbg("cpid: $cpid") if isdbg('reap');
311 # this is where the input queue is dealt with and things are dispatched off to other parts of
316 my $self = shift @inqueue;
319 my $data = $self->{data};
320 my $dxchan = $self->{dxchan};
322 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
323 return unless defined $sort;
325 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
326 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
327 if ($self->{disconnecting}) {
328 dbg('In disconnection, ignored');
333 my $user = $dxchan->user;
334 if ($sort eq 'A' || $sort eq 'O') {
335 $dxchan->start($line, $sort);
336 } elsif ($sort eq 'I') {
337 die "\$user not defined for $call" if !defined $user;
340 $dxchan->normal($line);
341 } elsif ($sort eq 'Z') {
343 } elsif ($sort eq 'D') {
344 ; # ignored (an echo)
345 } elsif ($sort eq 'G') {
346 $dxchan->enhanced($line);
348 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
355 my $t = $systime - $starttime;
356 my $days = int $t / 86400;
358 my $hours = int $t / 3600;
360 my $mins = int $t / 60;
361 return sprintf "%d %02d:%02d", $days, $hours, $mins;
366 AGWMsg::init(\&new_channel);
369 #############################################################
371 # The start of the main line of code
373 #############################################################
375 $starttime = $systime = time;
376 $lang = 'en' unless $lang;
378 # open the debug file, set various FHs to be unbuffered
379 dbginit(\&DXCommandmode::broadcast_debug);
383 STDOUT->autoflush(1);
385 # calculate build number
386 $build += $main::version;
387 $build = "$build.$branch" if $branch;
389 Log('cluster', "DXSpider V$version, build $build started");
392 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
393 dbg("DXSpider Version $version, build $build started");
396 dbg("loading prefixes ...");
400 dbg("loading band data ...");
403 # initialise User file system
404 dbg("loading user file system ...");
405 DXUser->init($userfn, 1);
407 # look for the sysop and the alias user and complain if they aren't there
409 my $ref = DXUser->get($mycall);
410 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
411 $ref = DXUser->get($myalias);
412 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
415 # start listening for incoming messages/connects
416 dbg("starting listeners ...");
417 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
418 $conn->conns("Server $clusteraddr/$clusterport");
419 push @listeners, $conn;
420 dbg("Internal port: $clusteraddr $clusterport");
421 foreach my $l (@main::listen) {
422 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
423 $conn->conns("Server $l->[0]/$l->[1]");
424 push @listeners, $conn;
425 dbg("External Port: $l->[0] $l->[1]");
430 dbg("load badwords: " . (BadWords::load or "Ok"));
433 unless ($DB::VERSION) {
434 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
438 $SIG{HUP} = 'IGNORE';
439 $SIG{CHLD} = sub { $zombies++ };
441 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
442 $SIG{IO} = sub { dbg("SIGIO received"); };
443 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
444 $SIG{KILL} = 'DEFAULT'; # as if it matters....
446 # catch the rest with a hopeful message
449 # dbg("Catching SIG $_") if isdbg('chan');
450 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
458 # read in system messages
461 # read in command aliases
464 # initialise the Geomagnetic data engine
468 # initial the Spot stuff
471 # initialise the protocol engine
472 dbg("reading in duplicate spot and WWV info ...");
476 # put in a DXCluster node for us here so we can add users and take them away
477 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
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 descriptors
495 dbg("reading database descriptors ...");
498 # starting local stuff
499 dbg("doing local initialisation ...");
503 dbg("Local::init error $@") if $@;
505 # this, such as it is, is the main loop!
506 dbg("orft we jolly well go ...");
507 my $script = new Script "startup";
508 $script->run($main::me) if $script;
510 #open(DB::OUT, "|tee /tmp/aa");
515 Msg->event_loop(10, 0.010);
517 process_inqueue(); # read in lines from the input queue and despatch them
520 # do timed stuff, ongoing processing happens one a second
521 if ($timenow != $systime) {
524 DXCron::process(); # do cron jobs
525 DXCommandmode::process(); # process ongoing command mode stuff
526 DXProt::process(); # process ongoing ak1a pcxx stuff
528 DXConnect::process();
536 Local::process(); # do any localised processing
538 dbg("Local::process error $@") if $@;
541 last if --$decease <= 0;