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 $clusteraddr $clusterport $yes $no $user_interval $lang);
20 $lang = 'en'; # default language
21 $yes = 'Yes'; # visual representation of yes
22 $no = 'No'; # ditto for no
23 $user_interval = 11*60; # the interval between unsolicited prompts if no traffic
26 # make sure that modules are searched in the order local then perl
30 # root of directory tree for this system
32 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
34 unshift @INC, "$root/perl"; # this IS the right way round!
35 unshift @INC, "$root/local";
37 # do some validation of the input
38 die "The directory $root doesn't exist, please RTFM" unless -d $root;
39 die "$root/local doesn't exist, please RTFM" unless -d "$root/local";
40 die "$root/local/DXVars.pm doesn't exist, please RTFM" unless -e "$root/local/DXVars.pm";
42 mkdir "$root/local_cmd", 0777 unless -d "$root/local_cmd";
45 $system = "$root/sys";
47 $localcmd = "$root/local_cmd";
48 $userfn = "$data/users";
50 # try to create and lock a lockfile (this isn't atomic but
52 $lockfn = "$root/local/cluster.lck"; # lock file name
54 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
58 die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
63 open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
67 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
129 use POSIX ":sys_wait_h";
136 use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
137 $zombies $root @listeners $lang $myalias @debug $userfn
138 $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
139 $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
140 $can_encode $maxconnect_user $maxconnect_node
144 $clusteraddr ||= '127.0.0.1'; # cluster tcp host address - used for things like console.pl
145 $clusterport ||= 27754; # cluster tcp port
146 @inqueue = (); # the main input queue, an array of hashes
147 $systime = 0; # the time now (in seconds)
148 $starttime = 0; # the starting time of the cluster
149 @outstanding_connects = (); # list of outstanding connects
150 @listeners = (); # list of listeners
151 $reqreg = 0; # 1 = registration required, 2 = deregister people
152 $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it
153 $allowdxby = 0; # 1 = allow "dx by <othercall>", 0 - don't allow it
154 $maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time
155 $maxconnect_node = 0; # Ditto but for nodes. In either case if a new incoming connection
156 # takes the no of references in the routing table above these numbers
157 # then the connection is refused. This only affects INCOMING connections.
159 use vars qw($version $subversion $build $gitversion $gitbranch);
161 # send a message to call on conn and disconnect
164 my ($conn, $call, $mess) = @_;
166 $conn->disable_read(1);
167 dbg("-> D $call $mess\n") if isdbg('chan');
168 $conn->send_now("D$call|$mess");
176 $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn};
177 $dxchan->disconnect(1);
180 # handle incoming messages
183 my ($conn, $msg) = @_;
184 my ($sort, $call, $line) = DXChannel::decode_input(0, $msg);
185 return unless defined $sort;
187 unless (is_callsign($call)) {
188 already_conn($conn, $call, DXM::msg($lang, "illcall", $call));
192 # set up the basic channel info
193 # is there one already connected to me - locally?
194 my $user = DXUser::get_current($call);
195 my $dxchan = DXChannel::get($call);
197 if ($user && $user->is_node) {
198 already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall));
202 my $ip = $conn->peerhost || 'unknown';
203 $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
204 LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
207 already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall));
212 # (fairly) politely disconnect people that are connected to too many other places at once
213 my $r = Route::get($call);
214 if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) {
216 my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user;
217 my $c = $user->maxconnect;
219 $v = defined $c ? $c : $m;
220 if ($v && @n >= $v) {
221 my $nodes = join ',', @n;
222 LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected");
223 already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes));
229 my $basecall = $call;
230 $basecall =~ s/-\d+$//;
231 my $baseuser = DXUser::get_current($basecall);
232 my $lock = $user->lockout if $user;
233 if ($baseuser && $baseuser->lockout || $lock) {
234 if (!$user || !defined $lock || $lock) {
235 my $host = $conn->peerhost || "unknown";
236 LogDbg('DXCommand', "$call on $host is locked out, disconnected");
243 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
245 $user = DXUser->new($call);
249 if ($user->is_node) {
250 $dxchan = DXProt->new($call, $conn, $user);
251 } elsif ($user->is_user) {
252 $dxchan = DXCommandmode->new($call, $conn, $user);
253 # } elsif ($user->is_bbs) { # there is no support so
254 # $dxchan = BBS->new($call, $conn, $user); # don't allow it!!!
256 die "Invalid sort of user on $call = $sort";
259 # check that the conn has a callsign
260 $conn->conns($call) if $conn->isa('IntMsg');
263 $conn->set_error(sub {error_handler($dxchan)});
264 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);});
271 return \&new_channel;
274 # cease running this program, close down all the connections nicely
281 cluck("ceasing") if $is_ceasing;
283 return if $is_ceasing++;
286 $SIG{'TERM'} = 'IGNORE';
287 $SIG{'INT'} = 'IGNORE';
292 if (defined &Local::finish) {
294 Local::finish(); # end local processing
296 dbg("Local::finish error $@") if $@;
300 foreach $dxchan (DXChannel::get_all_nodes) {
301 $dxchan->disconnect(2) unless $dxchan == $main::me;
305 foreach $dxchan (DXChannel::get_all_users) {
309 Msg->event_loop(100, 0.01);
315 # disconnect UDP customers
318 # end everything else
319 Msg->event_loop(100, 0.01);
324 # close all databases
327 # close all listeners
328 foreach my $l (@listeners) {
332 $dbh->finish if $dbh;
334 LogDbg("DXSpider v$version build $build (git: $gitbranch/$gitversion) using perl $^V on $^O ended");
339 # $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); };
343 # the reaper of children
347 while (($cpid = waitpid(-1, WNOHANG)) > 0) {
348 dbg("cpid: $cpid") if isdbg('reap');
349 # Msg->pid_gone($cpid);
350 $zombies-- if $zombies > 0;
352 dbg("cpid: $cpid") if isdbg('reap');
355 # this is where the input queue is dealt with and things are dispatched off to other parts of
360 my $t = $systime - $starttime;
361 my $days = int $t / 86400;
363 my $hours = int $t / 3600;
365 my $mins = int $t / 60;
366 return sprintf "%d %02d:%02d", $days, $hours, $mins;
371 AGWMsg::init(\&new_channel);
374 #############################################################
376 # The start of the main line of code
378 #############################################################
382 $starttime = $systime = time;
383 $systime_days = int ($systime / 86400);
384 $systime_daystart = $systime_days * 86400;
385 $lang = 'en' unless $lang;
387 unless ($DB::VERSION) {
388 $SIG{INT} = $SIG{TERM} = \&cease;
391 # open the debug file, set various FHs to be unbuffered
392 dbginit(\&DXCommandmode::broadcast_debug);
396 STDOUT->autoflush(1);
398 # try to load the database
399 if (DXSql::init($dsn)) {
400 $dbh = DXSql->new($dsn);
401 $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh;
404 # try to load Encode and Git
407 my $w = $SIG{__DIE__};
408 $SIG{__DIE__} = 'IGNORE';
409 eval { require Encode; };
416 $gitversion = 'none';
418 # determine the real Git build number and branch
420 eval {$desc = `git --git-dir=$root/.git describe --long`};
422 my ($v, $s, $b, $g) = $desc =~ /^([\d\.]+)(?:\.(\d+))?-(\d+)-g([0-9a-f]+)/;
424 $subversion = $s || 0;
426 $gitversion = "$g\[r]";
431 eval {@branch = `git --git-dir=$root/.git branch`};
434 my ($star, $b) = split /\s+/;
446 # try to load XML::Simple
450 my ($year) = (gmtime)[5];
452 LogDbg('cluster', "DXSpider v$version build $build (git: $gitbranch/$gitversion) using perl $^V on $^O started");
453 LogDbg('cluster', "Copyright (c) 1998-$year Dirk Koopman G1TLH");
454 LogDbg('cluster', "Capabilities: ve7cc rbn");
457 dbg("loading prefixes ...");
459 my $r = Prefix::init();
463 dbg("loading band data ...");
466 # initialise User file system
467 dbg("loading user file system ...");
468 DXUser->init($userfn, 1);
471 # look for the sysop and the alias user and complain if they aren't there
473 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;
474 my $ref = DXUser::get($mycall);
475 die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
476 my $oldsort = $ref->sort;
477 if ($oldsort ne 'S') {
479 dbg "Resetting node type from $oldsort -> DXSpider ('S')";
481 $ref = DXUser::get($myalias);
482 die "$myalias missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9;
483 $oldsort = $ref->sort;
484 if ($oldsort ne 'U') {
486 dbg "Resetting sysop user type from $oldsort -> User ('U')";
493 # start listening for incoming messages/connects
494 dbg("starting listeners ...");
495 my $conn = IntMsg->new_server($clusteraddr, $clusterport, \&login);
496 $conn->conns("Server $clusteraddr/$clusterport using IntMsg");
497 push @listeners, $conn;
498 dbg("Internal port: $clusteraddr $clusterport using IntMsg");
499 foreach my $l (@main::listen) {
501 my $pkg = $l->[2] || 'ExtMsg';
502 my $login = $l->[3] || 'login';
504 $conn = $pkg->new_server($l->[0], $l->[1], \&{"${pkg}::${login}"});
505 $conn->conns("Server $l->[0]/$l->[1] using ${pkg}::${login}");
506 push @listeners, $conn;
507 dbg("External Port: $l->[0] $l->[1] using ${pkg}::${login}");
510 dbg("AGW Listener") if $AGWMsg::enable;
513 dbg("BPQ Listener") if $BPQMsg::enable;
514 BPQMsg::init(\&new_channel);
516 dbg("UDP Listener") if $UDPMsg::enable;
517 UDPMsg::init(\&new_channel);
520 dbg("load badwords: " . (BadWords::load or "Ok"));
523 unless ($DB::VERSION) {
524 $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
528 $SIG{HUP} = 'IGNORE';
529 $SIG{CHLD} = sub { $zombies++ };
531 $SIG{PIPE} = sub { dbg("Broken PIPE signal received"); };
532 $SIG{IO} = sub { dbg("SIGIO received"); };
533 $SIG{WINCH} = $SIG{STOP} = $SIG{CONT} = 'IGNORE';
534 $SIG{KILL} = 'DEFAULT'; # as if it matters....
536 # catch the rest with a hopeful message
539 # dbg("Catching SIG $_") if isdbg('chan');
540 $SIG{$_} = sub { my $sig = shift; DXDebug::confess("Caught signal $sig"); };
546 dbg("Starting Dupe system");
549 # read in system messages
550 dbg("Read in Messages");
553 # read in command aliases
554 dbg("Read in Aliases");
557 # initialise the Geomagnetic data engine
563 # initial the Spot stuff
564 dbg("Starting DX Spot system");
567 # initialise the protocol engine
568 dbg("Start Protocol Engines ...");
571 # put in a DXCluster node for us here so we can add users and take them away
572 $routeroot = Route::Node->new($mycall, $version*100+5300, Route::here($main::me->here)|Route::conf($main::me->conf));
573 $routeroot->do_pc9x(1);
574 $routeroot->via_pc92(1);
576 # make sure that there is a routing OUTPUT node default file
577 #unless (Filter::read_in('route', 'node_default', 0)) {
578 # my $dxcc = $main::me->dxcc;
579 # $Route::filterdef->cmd($main::me, 'route', 'accept', "node_default call $mycall" );
582 # read in any existing message headers and clean out old crap
583 dbg("reading existing message headers ...");
587 # read in any cron jobs
588 dbg("reading cron jobs ...");
591 # read in database desriptors
592 dbg("reading database descriptors ...");
595 # starting local stuff
596 dbg("doing local initialisation ...");
598 if (defined &Local::init) {
602 dbg("Local::init error $@") if $@;
606 # this, such as it is, is the main loop!
607 dbg("orft we jolly well go ...");
608 my $script = new Script "startup";
609 $script->run($main::me) if $script;
611 #open(DB::OUT, "|tee /tmp/aa");
616 Msg->event_loop(10, 0.010);
619 DXChannel::process();
623 # do timed stuff, ongoing processing happens one a second
624 if ($timenow != $systime) {
627 my $days = int ($systime / 86400);
628 if ($systime_days != $days) {
629 $systime_days = $days;
630 $systime_daystart = $days * 86400;
632 IsoTime::update($systime);
633 DXCron::process(); # do cron jobs
634 DXCommandmode::process(); # process ongoing command mode stuff
636 DXProt::process(); # process ongoing ak1a pcxx stuff
637 DXConnect::process();
647 if (defined &Local::process) {
649 Local::process(); # do any localised processing
651 dbg("Local::process error $@") if $@;
655 last if --$decease <= 0;
658 cease(0) unless $is_ceasing;
665 cease(0) unless $is_ceasing;