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.100; # 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;
344 DXChannel::process();
348 # do timed stuff, ongoing processing happens one a second
349 if ($timenow != $systime) {
352 my $days = int ($systime / 86400);
353 if ($systime_days != $days) {
354 $systime_days = $days;
355 $systime_daystart = $days * 86400;
357 IsoTime::update($systime);
358 DXCron::process(); # do cron jobs
359 DXCommandmode::process(); # process ongoing command mode stuff
361 DXProt::process(); # process ongoing ak1a pcxx stuff
362 DXConnect::process();
367 $systime_days = $days;
368 $systime_daystart = $days * 86400;
370 IsoTime::update($systime);
371 DXCron::process(); # do cron jobs
372 DXCommandmode::process(); # process ongoing command mode stuff
374 DXProt::process(); # process ongoing ak1a pcxx stuff
375 DXConnect::process();
385 if (defined &Local::process) {
387 Local::process(); # do any localised processing
389 dbg("Local::process error $@") if $@;
395 dbg("DXSpider Ending $ending");
397 unless ($io_disconnected++) {
400 foreach $dxchan (DXChannel::get_all_users) {
405 foreach $dxchan (DXChannel::get_all_nodes) {
406 next if $dxchan == $main::me;
407 $dxchan->disconnect(2);
409 $main::me->disconnect;
412 Mojo::IOLoop->stop if --$ending <= 0;
417 #############################################################
419 # The start of the main line of code
421 #############################################################
423 $starttime = $systime = time;
424 $systime_days = int ($systime / 86400);
425 $systime_daystart = $systime_days * 86400;
426 $lang = 'en' unless $lang;
428 unless ($DB::VERSION) {
429 $SIG{INT} = $SIG{TERM} = \&cease;
432 # open the debug file, set various FHs to be unbuffered
433 dbginit(\&DXCommandmode::broadcast_debug);
437 STDOUT->autoflush(1);
439 # try to load the database
440 if (DXSql::init($dsn)) {
441 $dbh = DXSql->new($dsn);
442 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
448 my $w = $SIG{__DIE__};
449 $SIG{__DIE__} = 'IGNORE';
450 eval { require Encode; };
458 # try to load XML::Simple
462 my ($year) = (gmtime)[5];
464 LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) started");
465 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
468 dbg("loading prefixes ...");
470 my $r = Prefix::init();
474 dbg("loading band data ...");
477 # initialise User file system
478 dbg("loading user file system ...");
479 DXUser->init($userfn, 1);
481 # look for the sysop and the alias user and complain if they aren't there
483 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;
484 my $ref = DXUser::get($mycall);
485 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
486 $ref = DXUser::get($myalias);
487 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
490 # start listening for incoming messages/connects
491 dbg("starting listeners ...");
492 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
493 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
494 push @listeners, $conn;
495 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
496 foreach my $l (@main::listen) {
498 my $pkg = $l->[2] || 'ExtMsg';
499 my $login = $l->[3] || 'login';
501 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
502 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
503 push @listeners, $conn;
504 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
507 dbg("AGW Listener") if $AGWMsg::enable;
510 dbg("BPQ Listener") if $BPQMsg::enable;
511 BPQMsg::init(\&new_channel);
513 dbg("UDP Listener") if $UDPMsg::enable;
514 UDPMsg::init(\&new_channel);
517 dbg("load badwords: " . (BadWords::load or "Ok"));
520 unless ($DB::VERSION) {
521 $SIG{INT} = $SIG{TERM} = sub { $ending = 10; };
525 $SIG{HUP} = 'IGNORE';
526 $SIG{CHLD} = sub { $zombies++ };
528 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
529 $SIG{IO} = sub { dbg("SIGIO received"); };
530 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
531 $SIG{KILL} = 'DEFAULT'; # as if it matters....
533 # catch the rest with a hopeful message
536 # dbg("Catching SIG $_") if isdbg('chan');
537 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
543 dbg("Starting Dupe system");
546 # read in system messages
547 dbg("Read in Messages");
550 # read in command aliases
551 dbg("Read in Aliases");
554 # initialise the Geomagnetic data engine
560 # initial the Spot stuff
561 dbg("Starting DX Spot system");
564 # initialise the protocol engine
565 dbg("Start Protocol Engines ...");
568 # put in a DXCluster node for us here so we can add users and take them away
569 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
570 $routeroot->do_pc9x(1);
571 $routeroot->via_pc92(1);
573 # make sure that there is a routing OUTPUT node default file
574 #unless (Filter::read_in('route', 'node_default', 0)) {
575 # my $dxcc = $main::me->dxcc;
576 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
579 # read in any existing message headers and clean out old crap
580 dbg("reading existing message headers ...");
584 # read in any cron jobs
585 dbg("reading cron jobs ...");
588 # read in database desriptors
589 dbg("reading database descriptors ...");
592 # starting local stuff
593 dbg("doing local initialisation ...");
595 if (defined &Local::init) {
599 dbg("Local::init error $@") if $@;
603 # this, such as it is, is the main loop!
604 dbg("orft we jolly well go ...");
605 my $script = new Script "startup";
606 $script->run($main::me) if $script;
608 #open(DB::OUT, "|tee /tmp/aa");
610 my $main_loop = Mojo::IOLoop->recurring($idle_interval => \&idle_loop);
612 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
614 dbg("After Mojo::IOLoop");