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?
104 use POSIX ":sys_wait_h";
111 use vars qw(@inqueue $systime $version $starttime $lockfn @outstanding_connects
112 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
113 $clusterport $mycall $decease $is_win $routeroot $me $reqreg
116 @inqueue = (); # the main input queue, an array of hashes
117 $systime = 0; # the time now (in seconds)
118 $version = "1.51"; # the version no of the software
119 $starttime = 0; # the starting time of the cluster
120 #@outstanding_connects = (); # list of outstanding connects
121 @listeners = (); # list of listeners
122 $reqreg = 0; # 1 = registration required, 2 = deregister people
124 use vars qw($VERSION $BRANCH $build $branch);
125 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
126 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
127 $main::build += 7; # add an offset to make it bigger than last system
128 $main::build += $VERSION;
129 $main::branch += $BRANCH;
132 # send a message to call on conn and disconnect
135 my ($conn, $call, $mess) = @_;
137 $conn->disable_read(1);
138 dbg("-> D $call $mess\n") if isdbg('chan');
139 $conn->send_now("D$call|$mess");
147 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
148 $dxchan->disconnect(1);
151 # handle incoming messages
154 my ($conn, $msg) = @_;
155 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
156 return unless defined $sort;
158 unless (is_callsign($call)) {
159 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
163 # set up the basic channel info
164 # is there one already connected to me - locally?
165 my $user = DXUser->get_current($call);
166 my $dxchan = DXChannel->get($call);
168 my $mess = DXM::msg($lang, ($user && $user->is_node) ? 'concluster' : 'conother', $call, $main::mycall);
169 already_conn($conn, $call, $mess);
174 my $basecall = $call;
175 $basecall =~ s/-\d+$//;
176 my $baseuser = DXUser->get_current($basecall);
177 my $lock = $user->lockout if $user;
178 if ($baseuser && $baseuser->lockout || $lock) {
179 if (!$user || !defined $lock || $lock) {
180 my $host = $conn->{peerhost} || "unknown";
181 Log('DXCommand', "$call on $host is locked out, disconnected");
188 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
190 $user = DXUser->new($call);
196 if ($user->passphrase && $main::me->user->passphrase) {
197 $dxchan = QXProt->new($call, $conn, $user);
199 unless ($user->passphrase) {
200 Log('DXCommand', "$call using NP but has no passphrase");
201 dbg("$call using NP but has no passphrase");
203 unless ($main::me->user->passphrase) {
204 Log('DXCommand', "$main::mycall using NP but has no passphrase");
205 dbg("$main::mycall using NP but has no passphrase");
207 already_conn($conn, $call, "Need to exchange passphrases");
210 } elsif ($user->is_node) {
211 $dxchan = DXProt->new($call, $conn, $user);
212 } elsif ($user->is_user) {
213 $dxchan = DXCommandmode->new($call, $conn, $user);
214 } elsif ($user->is_bbs) {
215 $dxchan = BBS->new($call, $conn, $user);
217 die "Invalid sort of user on $call = $sort";
220 # check that the conn has a callsign
221 $conn->conns($call) if $conn->isa('IntMsg');
224 $conn->set_error(sub {error_handler($dxchan)});
225 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
226 rec($dxchan, $conn, $msg);
231 my ($dxchan, $conn, $msg) = @_;
233 # queue the message and the channel object for later processing
235 my $self = bless {}, "inqueue";
236 $self->{dxchan} = $dxchan;
237 $self->{data} = $msg;
238 push @inqueue, $self;
244 return \&new_channel;
247 # cease running this program, close down all the connections nicely
253 $SIG{'TERM'} = 'IGNORE';
254 $SIG{'INT'} = 'IGNORE';
260 Local::finish(); # end local processing
262 dbg("Local::finish error $@") if $@;
265 foreach $dxchan (DXChannel->get_all_nodes) {
266 $dxchan->disconnect(2) unless $dxchan == $main::me;
268 Msg->event_loop(100, 0.01);
271 foreach $dxchan (DXChannel->get_all_users) {
278 # end everything else
279 Msg->event_loop(100, 0.01);
283 # close all databases
286 # close all listeners
287 foreach my $l (@listeners) {
291 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
292 Log('cluster', "DXSpider V$version, build $build ended");
296 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
300 # the reaper of children
304 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
305 dbg("cpid: $cpid") if isdbg('reap');
306 # Msg->pid_gone($cpid);
307 $zombies-- if $zombies > 0;
309 dbg("cpid: $cpid") if isdbg('reap');
312 # this is where the input queue is dealt with and things are dispatched off to other parts of
317 my $self = shift @inqueue;
320 my $data = $self->{data};
321 my $dxchan = $self->{dxchan};
323 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
324 return unless defined $sort;
326 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
327 dbg("<- $sort $call $line\n") if $sort ne 'D' && isdbg('chan');
328 if ($self->{disconnecting}) {
329 dbg('In disconnection, ignored');
334 my $user = $dxchan->user;
335 if ($sort eq 'A' || $sort eq 'O') {
336 $dxchan->start($line, $sort);
337 } elsif ($sort eq 'I') {
338 die "\$user not defined for $call" if !defined $user;
341 $dxchan->normal($line);
342 } elsif ($sort eq 'Z') {
344 } elsif ($sort eq 'D') {
345 ; # ignored (an echo)
346 } elsif ($sort eq 'G') {
347 $dxchan->enhanced($line);
349 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
356 my $t = $systime - $starttime;
357 my $days = int $t / 86400;
359 my $hours = int $t / 3600;
361 my $mins = int $t / 60;
362 return sprintf "%d %02d:%02d", $days, $hours, $mins;
367 AGWMsg::init(\&new_channel);
370 #############################################################
372 # The start of the main line of code
374 #############################################################
376 $starttime = $systime = time;
377 $lang = 'en' unless $lang;
379 # open the debug file, set various FHs to be unbuffered
380 dbginit(\&DXCommandmode::broadcast_debug);
384 STDOUT->autoflush(1);
386 # calculate build number
387 $build += $main::version;
388 $build = "$build.$branch" if $branch;
390 Log('cluster', "DXSpider V$version, build $build started");
393 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
394 dbg("DXSpider Version $version, build $build started");
397 dbg("loading prefixes ...");
402 dbg("loading band data ...");
405 # initialise User file system
406 dbg("loading user file system ...");
407 DXUser->init($userfn, 1);
409 # look for the sysop and the alias user and complain if they aren't there
411 my $ref = DXUser->get($mycall);
412 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
413 $ref = DXUser->get($myalias);
414 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
417 # start listening for incoming messages/connects
418 dbg("starting listeners ...");
419 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
420 $conn->conns("Server $clusteraddr/$clusterport");
421 push @listeners, $conn;
422 dbg("Internal port: $clusteraddr $clusterport");
423 foreach my $l (@main::listen) {
424 $conn = ExtMsg->new_server($l->[0], $l->[1], \&login);
425 $conn->conns("Server $l->[0]/$l->[1]");
426 push @listeners, $conn;
427 dbg("External Port: $l->[0] $l->[1]");
432 dbg("load badwords: " . (BadWords::load or "Ok"));
435 unless ($DB::VERSION) {
436 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
440 $SIG{HUP} = 'IGNORE';
441 $SIG{CHLD} = sub { $zombies++ };
443 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
444 $SIG{IO} = sub { dbg("SIGIO received"); };
445 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
446 $SIG{KILL} = 'DEFAULT'; # as if it matters....
448 # catch the rest with a hopeful message
451 # dbg("Catching SIG $_") if isdbg('chan');
452 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
460 # read in system messages
463 # read in command aliases
466 # initialise the Geomagnetic data engine
470 # initial the Spot stuff
473 # initialise the protocol engine
474 dbg("reading in duplicate spot and WWV info ...");
478 # put in a DXCluster node for us here so we can add users and take them away
479 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
481 # make sure that there is a routing OUTPUT node default file
482 #unless (Filter::read_in('route', 'node_default', 0)) {
483 # my $dxcc = $main::me->dxcc;
484 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
487 # read in any existing message headers and clean out old crap
488 dbg("reading existing message headers ...");
492 # read in any cron jobs
493 dbg("reading cron jobs ...");
496 # read in database descriptors
497 dbg("reading database descriptors ...");
500 # starting local stuff
501 dbg("doing local initialisation ...");
505 dbg("Local::init error $@") if $@;
507 # this, such as it is, is the main loop!
508 dbg("orft we jolly well go ...");
509 my $script = new Script "startup";
510 $script->run($main::me) if $script;
512 #open(DB::OUT, "|tee /tmp/aa");
517 Msg->event_loop(10, 0.010);
519 process_inqueue(); # read in lines from the input queue and despatch them
522 # do timed stuff, ongoing processing happens one a second
523 if ($timenow != $systime) {
526 DXCron::process(); # do cron jobs
527 DXCommandmode::process(); # process ongoing command mode stuff
528 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;