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
122 @inqueue = (); # the main input queue, an array of hashes
123 $systime = 0; # the time now (in seconds)
124 $version = "1.51"; # 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 = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
135 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
136 $main::build += 1; # add an offset to make it bigger than last system
137 $main::build += $VERSION;
138 $main::branch += $BRANCH;
141 # send a message to call on conn and disconnect
144 my ($conn, $call, $mess) = @_;
146 $conn->disable_read(1);
147 dbg("-> D $call $mess\n") if isdbg('chan');
148 $conn->send_now("D$call|$mess");
156 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
157 $dxchan->disconnect(1);
160 # handle incoming messages
163 my ($conn, $msg) = @_;
164 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
165 return unless defined $sort;
167 unless (is_callsign($call)) {
168 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
172 # set up the basic channel info
173 # is there one already connected to me - locally?
174 my $user = DXUser->get_current($call);
175 my $dxchan = DXChannel->get($call);
177 if ($user && $user->is_node) {
178 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
182 my $ip = $conn->{peerhost} || 'unknown';
183 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
184 Log('DXCommand', "$call bumped off by $ip, disconnected");
185 dbg("$call bumped off by $ip, disconnected");
188 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
194 my $basecall = $call;
195 $basecall =~ s/-\d+$//;
196 my $baseuser = DXUser->get_current($basecall);
197 my $lock = $user->lockout if $user;
198 if ($baseuser && $baseuser->lockout || $lock) {
199 if (!$user || !defined $lock || $lock) {
200 my $host = $conn->{peerhost} || "unknown";
201 Log('DXCommand', "$call on $host is locked out, disconnected");
208 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
210 $user = DXUser->new($call);
214 if ($user->is_node) {
215 $dxchan = DXProt->new($call, $conn, $user);
216 } elsif ($user->is_user) {
217 $dxchan = DXCommandmode->new($call, $conn, $user);
218 } elsif ($user->is_bbs) {
219 $dxchan = BBS->new($call, $conn, $user);
221 die "Invalid sort of user on $call = $sort";
224 # check that the conn has a callsign
225 $conn->conns($call) if $conn->isa('IntMsg');
228 $conn->set_error(sub {error_handler($dxchan)});
229 $conn->set_rproc(sub {my ($conn,$msg) = @_; rec($dxchan, $conn, $msg);});
230 rec($dxchan, $conn, $msg);
235 my ($dxchan, $conn, $msg) = @_;
237 # queue the message and the channel object for later processing
239 my $self = bless {}, "inqueue";
240 $self->{dxchan} = $dxchan;
241 $self->{data} = $msg;
242 push @inqueue, $self;
246 # remove any outstanding entries on the inqueue after a disconnection (usually)
250 @inqueue = grep {$_->{dxchan} != $dxchan} @inqueue;
255 return \&new_channel;
258 # cease running this program, close down all the connections nicely
264 $SIG{'TERM'} = 'IGNORE';
265 $SIG{'INT'} = 'IGNORE';
271 Local::finish(); # end local processing
273 dbg("Local::finish error $@") if $@;
276 foreach $dxchan (DXChannel->get_all_nodes) {
277 $dxchan->disconnect(2) unless $dxchan == $main::me;
279 Msg->event_loop(100, 0.01);
282 foreach $dxchan (DXChannel->get_all_users) {
289 # disconnect UDP customers
292 # end everything else
293 Msg->event_loop(100, 0.01);
297 # close all databases
300 # close all listeners
301 foreach my $l (@listeners) {
305 dbg("DXSpider version $version, build $build ended") if isdbg('chan');
306 Log('cluster', "DXSpider V$version, build $build ended");
310 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
314 # the reaper of children
318 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
319 dbg("cpid: $cpid") if isdbg('reap');
320 # Msg->pid_gone($cpid);
321 $zombies-- if $zombies > 0;
323 dbg("cpid: $cpid") if isdbg('reap');
326 # this is where the input queue is dealt with and things are dispatched off to other parts of
331 my $self = shift @inqueue;
334 my $data = $self->{data};
335 my $dxchan = $self->{dxchan};
337 my ($sort, $call, $line) = DXChannel::decode_input($dxchan, $data);
338 return unless defined $sort;
340 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
341 dbg("<- $sort $call $line") if $sort ne 'D' && isdbg('chan');
342 if ($self->{disconnecting}) {
343 dbg('In disconnection, ignored');
348 my $user = $dxchan->user;
349 if ($sort eq 'A' || $sort eq 'O') {
350 $dxchan->start($line, $sort);
351 } elsif ($sort eq 'I') {
352 die "\$user not defined for $call" if !defined $user;
355 $dxchan->normal($line);
356 } elsif ($sort eq 'Z') {
358 } elsif ($sort eq 'D') {
359 ; # ignored (an echo)
360 } elsif ($sort eq 'G') {
361 $dxchan->enhanced($line);
363 print STDERR atime, " Unknown command letter ($sort) received from $call\n";
370 my $t = $systime - $starttime;
371 my $days = int $t / 86400;
373 my $hours = int $t / 3600;
375 my $mins = int $t / 60;
376 return sprintf "%d %02d:%02d", $days, $hours, $mins;
381 AGWMsg::init(\&new_channel);
384 #############################################################
386 # The start of the main line of code
388 #############################################################
390 $starttime = $systime = time;
391 $lang = 'en' unless $lang;
393 # open the debug file, set various FHs to be unbuffered
394 dbginit(\&DXCommandmode::broadcast_debug);
398 STDOUT->autoflush(1);
400 # calculate build number
401 $build += $main::version;
402 $build = "$build.$branch" if $branch;
404 Log('cluster', "DXSpider V$version, build $build started");
407 dbg("Copyright (c) 1998-2002 Dirk Koopman G1TLH");
408 dbg("DXSpider Version $version, build $build started");
411 dbg("loading prefixes ...");
413 my $r = Prefix::init();
417 dbg("loading band data ...");
420 # initialise User file system
421 dbg("loading user file system ...");
422 DXUser->init($userfn, 1);
424 # look for the sysop and the alias user and complain if they aren't there
426 my $ref = DXUser->get($mycall);
427 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
428 $ref = DXUser->get($myalias);
429 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
432 # start listening for incoming messages/connects
433 dbg("starting listeners ...");
434 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
435 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
436 push @listeners, $conn;
437 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
438 foreach my $l (@main::listen) {
440 my $pkg = $l->[2] || 'ExtMsg';
441 $conn = $pkg->new_server($l->[0], $l->[1], \&login);
442 $conn->conns("Server $l->[0]/$l->[1] using $pkg");
443 push @listeners, $conn;
444 dbg("External Port: $l->[0] $l->[1] using $pkg");
447 dbg("AGW Listener") if $AGWMsg::enable;
450 dbg("UDP Listener") if $UDPMsg::enable;
451 UDPMsg::init(\&new_channel);
454 dbg("load badwords: " . (BadWords::load or "Ok"));
457 unless ($DB::VERSION) {
458 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
462 $SIG{HUP} = 'IGNORE';
463 $SIG{CHLD} = sub { $zombies++ };
465 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
466 $SIG{IO} = sub { dbg("SIGIO received"); };
467 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
468 $SIG{KILL} = 'DEFAULT'; # as if it matters....
470 # catch the rest with a hopeful message
473 # dbg("Catching SIG $_") if isdbg('chan');
474 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
480 dbg("Starting Dupe system");
483 # read in system messages
484 dbg("Read in Messages");
487 # read in command aliases
488 dbg("Read in Aliases");
491 # initialise the Geomagnetic data engine
497 # initial the Spot stuff
498 dbg("Starting DX Spot system");
501 # initialise the protocol engine
502 dbg("Start Protocol Engines ...");
506 # put in a DXCluster node for us here so we can add users and take them away
507 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
509 # make sure that there is a routing OUTPUT node default file
510 #unless (Filter::read_in('route', 'node_default', 0)) {
511 # my $dxcc = $main::me->dxcc;
512 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
515 # read in any existing message headers and clean out old crap
516 dbg("reading existing message headers ...");
520 # read in any cron jobs
521 dbg("reading cron jobs ...");
524 # read in database descriptors
525 dbg("reading database descriptors ...");
528 # starting local stuff
529 dbg("doing local initialisation ...");
534 dbg("Local::init error $@") if $@;
536 # this, such as it is, is the main loop!
537 dbg("orft we jolly well go ...");
538 my $script = new Script "startup";
539 $script->run($main::me) if $script;
541 #open(DB::OUT, "|tee /tmp/aa");
546 Msg->event_loop(10, 0.010);
548 process_inqueue(); # read in lines from the input queue and despatch them
551 # do timed stuff, ongoing processing happens one a second
552 if ($timenow != $systime) {
555 DXCron::process(); # do cron jobs
556 DXCommandmode::process(); # process ongoing command mode stuff
557 DXProt::process(); # process ongoing ak1a pcxx stuff
559 DXConnect::process();
566 # this where things really start to happen (in DXSpider 2)
570 Local::process(); # do any localised processing
572 dbg("Local::process error $@") if $@;
575 last if --$decease <= 0;