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
17 # set default paths, these should be overwritten by DXVars.pm
18 use vars qw($data $system $cmd $localcmd $userfn);
20 # make sure that modules are searched in the order local then perl
24 # root of directory tree for this system
26 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
28 unshift @INC, "$root/perl"; # this IS the right way round!
29 unshift @INC, "$root/local";
31 # do some validation of the input
32 die "The directory $root doesn't exist, please RTFM" unless -d $root;
33 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
34 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
36 mkdir "$root/local_cmd", 0777 unless -d "$root/local_cmd";
39 $system = "$root/sys";
41 $localcmd = "$root/local_cmd";
42 $userfn = "$data/users";
44 # try to create and lock a lockfile (this isn't atomic but
46 $lockfn = "$root/local/cluster.lck"; # lock file name
48 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
52 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
57 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
61 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
122 use POSIX ":sys_wait_h";
130 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
131 $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
132 $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
133 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
134 $can_encode $maxconnect_user $maxconnect_node
137 @inqueue = (); # the main input queue, an array of hashes
138 $systime = 0; # the time now (in seconds)
139 $starttime = 0; # the starting time of the cluster
140 @outstanding_connects = (); # list of outstanding connects
141 @listeners = (); # list of listeners
142 $reqreg = 0; # 1 = registration required, 2 = deregister people
143 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
144 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
145 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
146 $maxconnect_node = 0; # Ditto but for nodes. In either case if a new incoming connection
147 # takes the no of references in the routing table above these numbers
148 # then the connection is refused. This only affects INCOMING connections.
150 # send a message to call on conn and disconnect
153 my ($conn, $call, $mess) = @_;
155 $conn->disable_read(1);
156 dbg("-> D $call $mess\n") if isdbg('chan');
157 $conn->send_now("D$call|$mess");
165 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
166 $dxchan->disconnect(1);
169 # handle incoming messages
172 my ($conn, $msg) = @_;
173 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
174 return unless defined $sort;
176 unless (is_callsign($call)) {
177 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
181 # set up the basic channel info
182 # is there one already connected to me - locally?
183 my $user = DXUser::get_current($call);
184 my $dxchan = DXChannel::get($call);
186 if ($user && $user->is_node) {
187 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
191 my $ip = $conn->peerhost || 'unknown';
192 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
193 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
196 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
201 # (fairly) politely disconnect people that are connected to too many other places at once
202 my $r = Route::get($call);
203 if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) {
205 my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
206 my $c = $user->maxconnect;
208 $v = defined $c ? $c : $m;
209 if ($v && @n >= $v) {
210 my $nodes = join ',', @n;
211 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected");
212 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
218 my $basecall = $call;
219 $basecall =~ s/-\d+$//;
220 my $baseuser = DXUser::get_current($basecall);
221 my $lock = $user->lockout if $user;
222 if ($baseuser && $baseuser->lockout || $lock) {
223 if (!$user || !defined $lock || $lock) {
224 my $host = $conn->peerhost || "unknown";
225 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
232 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
234 $user = DXUser->new($call);
238 if ($user->is_node) {
239 $dxchan = DXProt->new($call, $conn, $user);
240 } elsif ($user->is_user) {
241 $dxchan = DXCommandmode->new($call, $conn, $user);
242 # } elsif ($user->is_bbs) { # there is no support so
243 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
245 die "Invalid sort of user on $call = $sort";
248 # check that the conn has a callsign
249 $conn->conns($call) if $conn->isa('IntMsg');
252 $conn->set_error(sub {error_handler($dxchan)});
253 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
260 return \&new_channel;
263 # cease running this program, close down all the connections nicely
269 $SIG{'TERM'} = 'IGNORE';
270 $SIG{'INT'} = 'IGNORE';
275 if (defined &Local::finish) {
277 Local::finish(); # end local processing
279 dbg("Local::finish error $@") if $@;
283 foreach $dxchan (DXChannel::get_all_nodes) {
284 $dxchan->disconnect(2) unless $dxchan == $main::me;
286 Msg->event_loop(100, 0.01);
289 foreach $dxchan (DXChannel::get_all_users) {
297 # disconnect UDP customers
300 # end everything else
301 Msg->event_loop(100, 0.01);
305 # close all databases
308 # close all listeners
309 foreach my $l (@listeners) {
313 LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) ended");
317 $dbh->finish if $dbh;
320 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
324 # the reaper of children
328 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
329 dbg("cpid: $cpid") if isdbg('reap');
330 # Msg->pid_gone($cpid);
331 $zombies-- if $zombies > 0;
333 dbg("cpid: $cpid") if isdbg('reap');
336 # this is where the input queue is dealt with and things are dispatched off to other parts of
341 my $t = $systime - $starttime;
342 my $days = int $t / 86400;
344 my $hours = int $t / 3600;
346 my $mins = int $t / 60;
347 return sprintf "%d %02d:%02d", $days, $hours, $mins;
352 AGWMsg::init(\&new_channel);
355 #############################################################
357 # The start of the main line of code
359 #############################################################
361 $starttime = $systime = time;
362 $systime_days = int ($systime / 86400);
363 $systime_daystart = $systime_days * 86400;
364 $lang = 'en' unless $lang;
366 unless ($DB::VERSION) {
367 $SIG{INT} = $SIG{TERM} = \&cease;
370 # open the debug file, set various FHs to be unbuffered
371 dbginit(\&DXCommandmode::broadcast_debug);
375 STDOUT->autoflush(1);
377 # try to load the database
378 if (DXSql::init($dsn)) {
379 $dbh = DXSql->new($dsn);
380 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
383 # try to load Encode and Git
386 my $w = $SIG{__DIE__};
387 $SIG{__DIE__} = 'IGNORE';
388 eval { require Encode; };
393 eval { require Git; };
397 # determine the real version number
398 my $repo = Git->repository(Directory => "$root/.git");
400 my $desc = $repo->command_oneline(['describe'], STDERR => 0);
402 my ($v, $s, $b, $g) = $desc =~ /^([\d.]+)(?:\.(\d+))?-(\d+)-g([0-9a-f]+)/;
404 $subversion = $s || 0;
406 $gitversion = "$g\[r]";
413 # try to load XML::Simple
417 my ($year) = (gmtime)[5];
419 LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) started");
420 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
423 dbg("loading prefixes ...");
425 my $r = Prefix::init();
429 dbg("loading band data ...");
432 # initialise User file system
433 dbg("loading user file system ...");
434 DXUser->init($userfn, 1);
437 # look for the sysop and the alias user and complain if they aren't there
439 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;
440 my $ref = DXUser::get($mycall);
441 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
442 my $oldsort = $ref->sort;
443 if ($oldsort ne 'S') {
445 dbg "Resetting node type from $oldsort -> DXSpider ('S')";
447 $ref = DXUser::get($myalias);
448 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
449 $oldsort = $ref->sort;
450 if ($oldsort ne 'U') {
452 dbg "Resetting sysop user type from $oldsort -> User ('U')";
456 # start listening for incoming messages/connects
457 dbg("starting listeners ...");
458 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
459 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
460 push @listeners, $conn;
461 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
462 foreach my $l (@main::listen) {
464 my $pkg = $l->[2] || 'ExtMsg';
465 my $login = $l->[3] || 'login';
467 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
468 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
469 push @listeners, $conn;
470 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
473 dbg("AGW Listener") if $AGWMsg::enable;
476 dbg("BPQ Listener") if $BPQMsg::enable;
477 BPQMsg::init(\&new_channel);
479 dbg("UDP Listener") if $UDPMsg::enable;
480 UDPMsg::init(\&new_channel);
483 dbg("load badwords: " . (BadWords::load or "Ok"));
486 unless ($DB::VERSION) {
487 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
491 $SIG{HUP} = 'IGNORE';
492 $SIG{CHLD} = sub { $zombies++ };
494 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
495 $SIG{IO} = sub { dbg("SIGIO received"); };
496 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
497 $SIG{KILL} = 'DEFAULT'; # as if it matters....
499 # catch the rest with a hopeful message
502 # dbg("Catching SIG $_") if isdbg('chan');
503 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
509 dbg("Starting Dupe system");
512 # read in system messages
513 dbg("Read in Messages");
516 # read in command aliases
517 dbg("Read in Aliases");
520 # initialise the Geomagnetic data engine
526 # initial the Spot stuff
527 dbg("Starting DX Spot system");
530 # initialise the protocol engine
531 dbg("Start Protocol Engines ...");
534 # put in a DXCluster node for us here so we can add users and take them away
535 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
536 $routeroot->do_pc9x(1);
537 $routeroot->via_pc92(1);
539 # make sure that there is a routing OUTPUT node default file
540 #unless (Filter::read_in('route', 'node_default', 0)) {
541 # my $dxcc = $main::me->dxcc;
542 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
545 # read in any existing message headers and clean out old crap
546 dbg("reading existing message headers ...");
550 # read in any cron jobs
551 dbg("reading cron jobs ...");
554 # read in database desriptors
555 dbg("reading database descriptors ...");
558 # starting local stuff
559 dbg("doing local initialisation ...");
561 if (defined &Local::init) {
565 dbg("Local::init error $@") if $@;
569 # this, such as it is, is the main loop!
570 dbg("orft we jolly well go ...");
571 my $script = new Script "startup";
572 $script->run($main::me) if $script;
574 #open(DB::OUT, "|tee /tmp/aa");
579 Msg->event_loop(10, 0.010);
582 DXChannel::process();
586 # do timed stuff, ongoing processing happens one a second
587 if ($timenow != $systime) {
590 my $days = int ($systime / 86400);
591 if ($systime_days != $days) {
592 $systime_days = $days;
593 $systime_daystart = $days * 86400;
595 IsoTime::update($systime);
596 DXCron::process(); # do cron jobs
597 DXCommandmode::process(); # process ongoing command mode stuff
599 DXProt::process(); # process ongoing ak1a pcxx stuff
600 DXConnect::process();
610 if (defined &Local::process) {
612 Local::process(); # do any localised processing
614 dbg("Local::process error $@") if $@;
618 last if --$decease <= 0;