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) $!";
42 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
47 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
51 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
114 use POSIX ":sys_wait_h";
122 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
123 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
124 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
125 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
126 $can_encode $maxconnect_user $maxconnect_node $idle_interval
129 @inqueue = (); # the main input queue, an array of hashes
130 $systime = 0; # the time now (in seconds)
131 $starttime = 0; # the starting time of the cluster
132 @outstanding_connects = (); # list of outstanding connects
133 @listeners = (); # list of listeners
134 $reqreg = 0; # 1 = registration required, 2 = deregister people
135 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
136 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
137 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
138 $maxconnect_node = 0; # Ditto but for nodes. In either case if a new incoming connection
139 # takes the no of references in the routing table above these numbers
140 # then the connection is refused. This only affects INCOMING connections.
141 $idle_interval = 0.500; # the wait between invocations of the main idle loop processing.
142 our $ending; # signal that we are ending;
145 # send a message to call on conn and disconnect
148 my ($conn, $call, $mess) = @_;
150 $conn->disable_read(1);
151 dbg("-> D $call $mess\n") if isdbg('chan');
152 $conn->send_now("D$call|$mess");
157 # handle incoming messages
160 my ($conn, $msg) = @_;
161 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
162 return unless defined $sort;
164 unless (is_callsign($call)) {
165 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
169 # set up the basic channel info
170 # is there one already connected to me - locally?
171 my $user = DXUser::get_current($call);
172 my $dxchan = DXChannel::get($call);
174 if ($user && $user->is_node) {
175 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
179 my $ip = $conn->peerhost || 'unknown';
180 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
181 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
184 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
189 # (fairly) politely disconnect people that are connected to too many other places at once
190 my $r = Route::get($call);
191 if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) {
193 my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
194 my $c = $user->maxconnect;
196 $v = defined $c ? $c : $m;
197 if ($v && @n >= $v) {
198 my $nodes = join ',', @n;
199 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected");
200 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
206 my $basecall = $call;
207 $basecall =~ s/-\d+$//;
208 my $baseuser = DXUser::get_current($basecall);
209 my $lock = $user->lockout if $user;
210 if ($baseuser && $baseuser->lockout || $lock) {
211 if (!$user || !defined $lock || $lock) {
212 my $host = $conn->peerhost || "unknown";
213 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
220 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
222 $user = DXUser->new($call);
226 if ($user->is_node) {
227 $dxchan = DXProt->new($call, $conn, $user);
228 } elsif ($user->is_user) {
229 $dxchan = DXCommandmode->new($call, $conn, $user);
230 # } elsif ($user->is_bbs) { # there is no support so
231 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
233 die "Invalid sort of user on $call = $sort";
236 # check that the conn has a callsign
237 $conn->conns($call) if $conn->isa('IntMsg');
240 $conn->set_error(sub {my $err = shift; error_handler($dxchan, $err)});
241 $conn->set_on_eof(sub {$dxchan->disconnect});
242 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
249 return \&new_channel;
254 # cease running this program, close down all the connections nicely
259 cluck("ceasing") if $ceasing;
261 return if $ceasing++;
264 $SIG{'TERM'} = 'IGNORE';
265 $SIG{'INT'} = 'IGNORE';
270 if (defined &Local::finish) {
272 Local::finish(); # end local processing
274 dbg("Local::finish error $@") if $@;
282 # disconnect UDP customers
285 # end everything else
289 # close all databases
292 # close all listeners
293 foreach my $l (@listeners) {
297 LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) ended");
298 dbg("bye bye everyone - bye bye");
302 $dbh->finish if $dbh;
307 # the reaper of children
311 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
312 dbg("cpid: $cpid") if isdbg('reap');
313 # Msg->pid_gone($cpid);
314 $zombies-- if $zombies > 0;
316 dbg("cpid: $cpid") if isdbg('reap');
319 # this is where the input queue is dealt with and things are dispatched off to other parts of
324 my $t = $systime - $starttime;
325 my $days = int $t / 86400;
327 my $hours = int $t / 3600;
329 my $mins = int $t / 60;
330 return sprintf "%d %02d:%02d", $days, $hours, $mins;
335 AGWMsg::init(\&new_channel);
338 our $io_disconnected;
345 # DXChannel::process();
349 # do timed stuff, ongoing processing happens one a second
350 if ($timenow != $systime) {
353 my $days = int ($systime / 86400);
354 if ($systime_days != $days) {
355 $systime_days = $days;
356 $systime_daystart = $days * 86400;
358 IsoTime::update($systime);
359 DXCron::process(); # do cron jobs
360 DXCommandmode::process(); # process ongoing command mode stuff
362 DXProt::process(); # process ongoing ak1a pcxx stuff
363 DXConnect::process();
368 DXCron::process(); # do cron jobs
369 IsoTime::update($systime);
370 DXProt::process(); # process ongoing ak1a pcxx stuff
371 DXConnect::process();
379 if (defined &Local::process) {
381 Local::process(); # do any localised processing
383 dbg("Local::process error $@") if $@;
389 dbg("DXSpider Ending $ending");
391 unless ($io_disconnected++) {
394 foreach $dxchan (DXChannel::get_all_users) {
399 foreach $dxchan (DXChannel::get_all_nodes) {
400 next if $dxchan == $main::me;
401 $dxchan->disconnect(2);
403 $main::me->disconnect;
406 Mojo::IOLoop->stop if --$ending <= 0;
411 #############################################################
413 # The start of the main line of code
415 #############################################################
417 $starttime = $systime = time;
418 $systime_days = int ($systime / 86400);
419 $systime_daystart = $systime_days * 86400;
420 $lang = 'en' unless $lang;
422 unless ($DB::VERSION) {
423 $SIG{INT} = $SIG{TERM} = \&cease;
426 # open the debug file, set various FHs to be unbuffered
427 dbginit(\&DXCommandmode::broadcast_debug);
431 STDOUT->autoflush(1);
433 # try to load the database
434 if (DXSql::init($dsn)) {
435 $dbh = DXSql->new($dsn);
436 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
442 my $w = $SIG{__DIE__};
443 $SIG{__DIE__} = 'IGNORE';
444 eval { require Encode; };
452 # try to load XML::Simple
456 my ($year) = (gmtime)[5];
458 LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) started");
459 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
462 dbg("loading prefixes ...");
464 my $r = Prefix::init();
468 dbg("loading band data ...");
471 # initialise User file system
472 dbg("loading user file system ...");
473 DXUser->init($userfn, 1);
475 # look for the sysop and the alias user and complain if they aren't there
477 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;
478 my $ref = DXUser::get($mycall);
479 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
480 $ref = DXUser::get($myalias);
481 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
484 # start listening for incoming messages/connects
485 dbg("starting listeners ...");
486 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
487 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
488 push @listeners, $conn;
489 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
490 foreach my $l (@main::listen) {
492 my $pkg = $l->[2] || 'ExtMsg';
493 my $login = $l->[3] || 'login';
495 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
496 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
497 push @listeners, $conn;
498 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
501 dbg("AGW Listener") if $AGWMsg::enable;
504 dbg("BPQ Listener") if $BPQMsg::enable;
505 BPQMsg::init(\&new_channel);
507 dbg("UDP Listener") if $UDPMsg::enable;
508 UDPMsg::init(\&new_channel);
511 dbg("load badwords: " . (BadWords::load or "Ok"));
514 unless ($DB::VERSION) {
515 $SIG{INT} = $SIG{TERM} = sub { $ending = 10; };
519 $SIG{HUP} = 'IGNORE';
520 $SIG{CHLD} = sub { $zombies++ };
522 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
523 $SIG{IO} = sub { dbg("SIGIO received"); };
524 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
525 $SIG{KILL} = 'DEFAULT'; # as if it matters....
527 # catch the rest with a hopeful message
530 # dbg("Catching SIG $_") if isdbg('chan');
531 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
537 dbg("Starting Dupe system");
540 # read in system messages
541 dbg("Read in Messages");
544 # read in command aliases
545 dbg("Read in Aliases");
548 # initialise the Geomagnetic data engine
554 # initial the Spot stuff
555 dbg("Starting DX Spot system");
558 # initialise the protocol engine
559 dbg("Start Protocol Engines ...");
562 # put in a DXCluster node for us here so we can add users and take them away
563 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
564 $routeroot->do_pc9x(1);
565 $routeroot->via_pc92(1);
567 # make sure that there is a routing OUTPUT node default file
568 #unless (Filter::read_in('route', 'node_default', 0)) {
569 # my $dxcc = $main::me->dxcc;
570 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
573 # read in any existing message headers and clean out old crap
574 dbg("reading existing message headers ...");
578 # read in any cron jobs
579 dbg("reading cron jobs ...");
582 # read in database desriptors
583 dbg("reading database descriptors ...");
586 # starting local stuff
587 dbg("doing local initialisation ...");
589 if (defined &Local::init) {
593 dbg("Local::init error $@") if $@;
597 # this, such as it is, is the main loop!
598 dbg("orft we jolly well go ...");
599 my $script = new Script "startup";
600 $script->run($main::me) if $script;
602 #open(DB::OUT, "|tee /tmp/aa");
604 my $main_loop = Mojo::IOLoop->recurring($idle_interval => \&idle_loop);
606 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
608 dbg("After Mojo::IOLoop");