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
18 # make sure that modules are searched in the order local then perl
22 # root of directory tree for this system
24 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
26 unshift @INC, "$root/perl"; # this IS the right way round!
27 unshift @INC, "$root/local";
29 # do some validation of the input
30 die "The directory $root doesn't exist, please RTFM" unless -d $root;
31 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
32 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
34 # create some directories
35 mkdir "$root/local_cmd", 02777 unless -d "$root/local_cmd";
36 mkdir "$root/local_data", 02777 unless -d "$root/local_data";
38 # try to create and lock a lockfile (this isn't atomic but
40 $lockfn = "$root/local_data/cluster.lck"; # lock file name
42 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
47 warn "Lockfile ($lockfn) and process $pid exist, another cluster running?\n";
54 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
58 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
124 use POSIX ":sys_wait_h";
133 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
134 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
135 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
136 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
137 $can_encode $maxconnect_user $maxconnect_node $idle_interval $log_flush_interval
141 @inqueue = (); # the main input queue, an array of hashes
142 $systime = 0; # the time now (in seconds)
143 $starttime = 0; # the starting time of the cluster
144 @outstanding_connects = (); # list of outstanding connects
145 @listeners = (); # list of listeners
146 $reqreg = 0; # 1 = registration required, 2 = deregister people
147 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
148 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
149 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
150 $maxconnect_node = 0; # Ditto but for nodes. In either case if a new incoming connection
151 # takes the no of references in the routing table above these numbers
152 # then the connection is refused. This only affects INCOMING connections.
153 $idle_interval = 0.500; # the wait between invocations of the main idle loop processing.
154 $log_flush_interval = 2; # interval to wait between log flushes
156 our $ending; # signal that we are ending;
157 our $broadcast_debug; # allow broadcasting of debug info down "enhanced" user connections
160 # send a message to call on conn and disconnect
163 my ($conn, $call, $mess) = @_;
165 $conn->disable_read(1);
166 dbg("-> D $call $mess\n") if isdbg('chan');
167 $conn->send_now("D$call|$mess");
172 # handle incoming messages
175 my ($conn, $msg) = @_;
176 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
177 return unless defined $sort;
179 unless (is_callsign($call)) {
180 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
184 # set up the basic channel info
185 # is there one already connected to me - locally?
186 my $user = DXUser::get_current($call);
187 my $dxchan = DXChannel::get($call);
189 if ($user && $user->is_node) {
190 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
194 my $ip = $conn->peerhost || 'unknown';
195 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
196 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
199 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
204 # (fairly) politely disconnect people that are connected to too many other places at once
205 my $r = Route::get($call);
206 if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) {
208 my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
209 my $c = $user->maxconnect;
211 $v = defined $c ? $c : $m;
212 if ($v && @n >= $v) {
213 my $nodes = join ',', @n;
214 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected");
215 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
221 my $basecall = $call;
222 $basecall =~ s/-\d+$//;
223 my $baseuser = DXUser::get_current($basecall);
224 my $lock = $user->lockout if $user;
225 if ($baseuser && $baseuser->lockout || $lock) {
226 if (!$user || !defined $lock || $lock) {
227 my $host = $conn->peerhost || "unknown";
228 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
235 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
237 $user = DXUser->new($call);
241 if ($user->is_node) {
242 $dxchan = DXProt->new($call, $conn, $user);
243 } elsif ($user->is_user) {
244 $dxchan = DXCommandmode->new($call, $conn, $user);
245 # } elsif ($user->is_bbs) { # there is no support so
246 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
248 die "Invalid sort of user on $call = $sort";
251 # check that the conn has a callsign
252 $conn->conns($call) if $conn->isa('IntMsg');
255 $conn->set_error(sub {my $err = shift; LogDbg('DXCommand', "Comms error '$err' received for call $dxchan->{call}"); $dxchan->disconnect(1);});
256 $conn->set_on_eof(sub {$dxchan->disconnect});
257 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
264 return \&new_channel;
269 # cease running this program, close down all the connections nicely
274 cluck("ceasing") if $ceasing;
276 return if $ceasing++;
279 $SIG{'TERM'} = 'IGNORE';
280 $SIG{'INT'} = 'IGNORE';
285 if (defined &Local::finish) {
287 Local::finish(); # end local processing
289 dbg("Local::finish error $@") if $@;
297 # disconnect UDP customers
300 # end everything else
304 # close all databases
307 # close all listeners
308 foreach my $l (@listeners) {
312 LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) ended");
313 dbg("bye bye everyone - bye bye");
317 $dbh->finish if $dbh;
322 # the reaper of children
326 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
327 dbg("cpid: $cpid") if isdbg('reap');
328 # Msg->pid_gone($cpid);
329 $zombies-- if $zombies > 0;
331 dbg("cpid: $cpid") if isdbg('reap');
334 # this is where the input queue is dealt with and things are dispatched off to other parts of
339 my $t = $systime - $starttime;
340 my $days = int $t / 86400;
342 my $hours = int $t / 3600;
344 my $mins = int $t / 60;
345 return sprintf "%d %02d:%02d", $days, $hours, $mins;
350 AGWMsg::init(\&new_channel);
353 our $io_disconnected;
360 # DXChannel::process();
364 # do timed stuff, ongoing processing happens one a second
365 if ($timenow != $systime) {
368 my $days = int ($systime / 86400);
369 if ($systime_days != $days) {
370 $systime_days = $days;
371 $systime_daystart = $days * 86400;
373 IsoTime::update($systime);
374 DXCron::process(); # do cron jobs
375 DXCommandmode::process(); # process ongoing command mode stuff
377 DXProt::process(); # process ongoing ak1a pcxx stuff
378 DXConnect::process();
383 DXCron::process(); # do cron jobs
384 IsoTime::update($systime);
385 DXProt::process(); # process ongoing ak1a pcxx stuff
386 DXConnect::process();
394 if (defined &Local::process) {
396 Local::process(); # do any localised processing
398 dbg("Local::process error $@") if $@;
404 dbg("DXSpider Ending $ending");
406 unless ($io_disconnected++) {
409 foreach $dxchan (DXChannel::get_all_users) {
414 foreach $dxchan (DXChannel::get_all_nodes) {
415 next if $dxchan == $main::me;
416 $dxchan->disconnect(2);
418 $main::me->disconnect;
421 Mojo::IOLoop->stop if --$ending <= 0;
428 #############################################################
430 # The start of the main line of code
432 #############################################################
434 $starttime = $systime = time;
435 $systime_days = int ($systime / 86400);
436 $systime_daystart = $systime_days * 86400;
437 $lang = 'en' unless $lang;
439 unless ($DB::VERSION) {
440 $SIG{INT} = $SIG{TERM} = \&cease;
443 # open the debug file, set various FHs to be unbuffered
444 dbginit($broadcast_debug ? \&DXCommandmode::broadcast_debug : undef);
448 STDOUT->autoflush(1);
451 # try to load the database
452 if (DXSql::init($dsn)) {
453 $dbh = DXSql->new($dsn);
454 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
457 # try to load Encode and Git
460 my $w = $SIG{__DIE__};
461 $SIG{__DIE__} = 'IGNORE';
462 eval { require Encode; };
467 eval { require Git; };
471 # determine the real version number
472 my $repo = Git->repository(Directory => "$root/.git");
474 my $desc = $repo->command_oneline(['describe', '--long'], STDERR => 0);
476 my ($v, $s, $b, $g) = $desc =~ /^([\d.]+)(?:\.(\d+))?-(\d+)-g([0-9a-f]+)/;
479 $gitversion = "$g\[r]";
486 # try to load XML::Simple
490 my ($year) = (gmtime)[5];
492 LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) started");
493 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
496 dbg("loading prefixes ...");
498 my $r = Prefix::init();
502 dbg("loading band data ...");
505 # initialise User file system
506 dbg("loading user file system ...");
509 # look for the sysop and the alias user and complain if they aren't there
511 die "\$myalias \& \$mycall are the same ($mycall)!, they must be different (hint: make \$mycall = '${mycall}-2';). Oh and don't forget to rerun create_sysop.pl!" if $mycall eq $myalias;
512 my $ref = DXUser::get($mycall);
513 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
514 my $oldsort = $ref->sort;
515 if ($oldsort ne 'S') {
517 dbg "Resetting node type from $oldsort -> DXSpider ('S')";
519 $ref = DXUser::get($myalias);
520 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
521 $oldsort = $ref->sort;
522 if ($oldsort ne 'U') {
524 dbg "Resetting sysop user type from $oldsort -> User ('U')";
528 # start listening for incoming messages/connects
529 dbg("starting listeners ...");
530 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
531 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
532 push @listeners, $conn;
533 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
534 foreach my $l (@main::listen) {
536 my $pkg = $l->[2] || 'ExtMsg';
537 my $login = $l->[3] || 'login';
539 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
540 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
541 push @listeners, $conn;
542 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
545 dbg("AGW Listener") if $AGWMsg::enable;
548 dbg("BPQ Listener") if $BPQMsg::enable;
549 BPQMsg::init(\&new_channel);
551 dbg("UDP Listener") if $UDPMsg::enable;
552 UDPMsg::init(\&new_channel);
555 dbg("load badwords: " . (BadWords::load or "Ok"));
558 unless ($DB::VERSION) {
559 $SIG{INT} = $SIG{TERM} = sub { $ending = 10; };
563 $SIG{HUP} = 'IGNORE';
564 $SIG{CHLD} = sub { $zombies++ };
566 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
567 $SIG{IO} = sub { dbg("SIGIO received"); };
568 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
569 $SIG{KILL} = 'DEFAULT'; # as if it matters....
571 # catch the rest with a hopeful message
574 # dbg("Catching SIG $_") if isdbg('chan');
575 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
581 dbg("Starting Dupe system");
584 # read in system messages
585 dbg("Read in Messages");
588 # read in command aliases
589 dbg("Read in Aliases");
592 # initialise the Geomagnetic data engine
598 # initial the Spot stuff
599 dbg("Starting DX Spot system");
602 # initialise the protocol engine
603 dbg("Start Protocol Engines ...");
606 # put in a DXCluster node for us here so we can add users and take them away
607 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
608 $routeroot->do_pc9x(1);
609 $routeroot->via_pc92(1);
611 # make sure that there is a routing OUTPUT node default file
612 #unless (Filter::read_in('route', 'node_default', 0)) {
613 # my $dxcc = $main::me->dxcc;
614 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
617 # read in any existing message headers and clean out old crap
618 dbg("reading existing message headers ...");
622 # read in any cron jobs
623 dbg("reading cron jobs ...");
626 # read in database desriptors
627 dbg("reading database descriptors ...");
630 # starting local stuff
631 dbg("doing local initialisation ...");
633 if (defined &Local::init) {
637 dbg("Local::init error $@") if $@;
641 # this, such as it is, is the main loop!
642 dbg("orft we jolly well go ...");
643 my $script = new Script "startup";
644 $script->run($main::me) if $script;
646 #open(DB::OUT, "|tee /tmp/aa");
652 my $main_loop = Mojo::IOLoop->recurring($idle_interval => \&idle_loop);
653 my $log_flush_loop = Mojo::IOLoop->recurring($log_flush_interval => \&DXLog::flushall);