2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
22 use vars qw($VERSION $BRANCH);
23 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
24 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
25 $main::build += $VERSION;
26 $main::branch += $BRANCH;
28 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold);
33 $lastoperinterval = 60*24*60*60;
36 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
38 # hash of valid elements and a simple prompt
41 alias => '0,Real Callsign',
44 lat => '0,Latitude,slat',
45 long => '0,Longitude,slong',
47 email => '0,E-mail Address,parray',
48 priv => '9,Privilege Level',
49 lastin => '0,Last Time in,cldatetime',
50 passwd => '9,Password,yesno',
51 passphrase => '9,Pass Phrase,yesno',
52 addr => '0,Full Address',
53 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
54 xpert => '0,Expert Status,yesno',
56 node => '0,Last Node',
57 homenode => '0,Home Node',
58 lockout => '9,Locked out?,yesno', # won't let them in at all
59 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
60 annok => '9,Accept Announces?,yesno', # accept his announces?
62 hmsgno => '0,Highest Msgno',
63 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
64 isolate => '9,Isolate network,yesno',
65 wantbeep => '0,Req Beep,yesno',
66 wantann => '0,Req Announce,yesno',
67 wantwwv => '0,Req WWV,yesno',
68 wantwcy => '0,Req WCY,yesno',
69 wantecho => '0,Req Echo,yesno',
70 wanttalk => '0,Req Talk,yesno',
71 wantwx => '0,Req WX,yesno',
72 wantdx => '0,Req DX Spots,yesno',
73 wantemail => '0,Req Msgs as Email,yesno',
74 pagelth => '0,Current Pagelth',
75 pingint => '9,Node Ping interval',
76 nopings => '9,Ping Obs Count',
77 wantlogininfo => '9,Login info req,yesno',
78 wantgrid => '0,DX Grid Info,yesno',
79 wantann_talk => '0,Talklike Anns,yesno',
80 wantpc90 => '1,Req PC90,yesno',
81 wantnp => '1,Req New Protocol,yesno',
82 wantpc16 => '9,Want Users from node,yesno',
83 wantsendpc16 => '9,Send PC16,yesno',
84 wantroutepc19 => '9,Route PC19,yesno',
85 lastoper => '9,Last for/oper,cldatetime',
86 nothere => '0,Not Here Text',
87 registered => '9,Registered?,yesno',
88 prompt => '0,Required Prompt',
89 version => '1,Version',
99 return if $name =~ /::DESTROY$/;
102 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
103 # this clever line of code creates a subroutine which takes over from autoload
104 # from OO Perl - Conway
105 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
112 # initialise the system
116 my ($pkg, $fn, $mode) = @_;
118 confess "need a filename in User" if !$fn;
121 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
123 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
127 $lru = LRU->newbase("DXUser", $lrusize);
134 confess "need a filename in User" if !$fn;
140 # periodic processing
144 if ($main::systime > $lasttime + 15) {
146 $lasttime = $main::systime;
161 # new - create a new user
168 # $call =~ s/-\d+$//o;
170 # confess "can't create existing call $call in User\n!" if $u{$call};
172 my $self = bless {}, $pkg;
173 $self->{call} = $call;
174 $self->{'sort'} = 'U';
180 # get - get an existing user - this seems to return a different reference everytime it is
190 # is it in the LRU cache?
191 my $ref = $lru->get($call);
195 unless ($dbm->get($call, $data)) {
196 $ref = decode($data);
197 $lru->put($call, $ref);
204 # get an existing either from the channel (if there is one) or from the database
206 # It is important to note that if you have done a get (for the channel say) and you
207 # want access or modify that you must use this call (and you must NOT use get's all
208 # over the place willy nilly!)
216 my $dxchan = DXChannel->get($call);
217 return $dxchan->user if $dxchan;
218 my $rref = Route::get($call);
219 return $rref->user if $rref && exists $rref->{user};
220 return $pkg->get($call);
224 # get all callsigns in the database
229 return (sort keys %u);
239 confess "Trying to put nothing!" unless $self && ref $self;
240 my $call = $self->{call};
241 # delete all instances of this
242 # for ($dbm->get_dup($call)) {
243 # $dbm->del_dup($call, $_);
246 delete $self->{annok} if $self->{annok};
247 delete $self->{dxok} if $self->{dxok};
248 $lru->put($call, $self);
249 my $ref = $self->encode;
250 $dbm->put($call, $ref);
254 # create a string from a user reference
259 my $dd = new Data::Dumper([$self]);
262 $dd->Quotekeys($] < 5.005 ? 1 : 0);
267 # create a hash from a string
283 # del - delete a user
289 my $call = $self->{call};
290 # delete all instances of this
291 # for ($dbm->get_dup($call)) {
292 # $dbm->del_dup($call, $_);
299 # close - close down a user
305 $self->{lastin} = time;
319 # return a list of valid elements
329 # export the database to an ascii file
337 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
338 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
339 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
340 rename "$fn.o", "$fn.oo" if -e "$fn.o";
341 rename "$fn", "$fn.o" if -e "$fn";
346 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
351 my $t = scalar localtime;
352 print $fh q{#!/usr/bin/perl
354 # The exported userfile for a DXSpider System
356 # Input file: $filename
362 # search local then perl directories
366 # root of directory tree for this system
368 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
370 unshift @INC, "$root/perl"; # this IS the right way round!
371 unshift @INC, "$root/local";
373 # try to detect a lockfile (this isn't atomic but
375 $lockfn = "$root/perl/cluster.lck"; # lock file name
377 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
380 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
391 $main::userfn = shift @ARGV;
392 print "user filename now $userfn\n";
395 DXUser->del_file($main::userfn);
396 DXUser->init($main::userfn, 1);
403 my $ref = decode($f[1]);
408 print "# Error: $f[0]\t$f[1]\n";
412 DXUser->sync; DXUser->finish;
413 print "There are $count user records and $err errors\n";
415 print $fh "__DATA__\n";
417 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
418 if (!is_callsign($key) || $key =~ /^0/) {
419 Log('DXCommand', "Export Error1: $key\t$val");
420 eval {$dbm->del($key)};
421 dbg(carp("Export Error1: $key\t$val\n$@")) if $@;
425 my $ref = decode($val);
427 my $t = $ref->{lastin} || 0;
428 if ($main::systime > $t + $tooold) {
429 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
430 eval {$dbm->del($key)};
431 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
432 Log('DXCommand', "$ref->{call} deleted, too old");
437 # only store users that are reasonably active or have useful information
438 print $fh "$key\t" . $ref->encode . "\n";
441 Log('DXCommand', "Export Error3: $key\t$val");
442 eval {$dbm->del($key)};
443 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
449 return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
456 # add one or more groups
460 my $ref = $self->{group} || [ 'local' ];
461 $self->{group} = $ref if !$self->{group};
462 push @$ref, @_ if @_;
465 # remove one or more groups
469 my $ref = $self->{group} || [ 'local' ];
472 $self->{group} = $ref if !$self->{group};
474 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
477 # does this thing contain all the groups listed?
481 my $ref = $self->{group};
484 return 0 if !$ref || @_ == 0;
485 return 1 if @$ref == 0 && @_ == 0;
486 for ($n = 0; $n < @_; ) {
489 $n++ if grep $_ eq $a, @_;
495 # simplified group test just for one group
500 my $ref = $self->{group};
503 return grep $_ eq $s, $ref;
506 # set up a default group (only happens for them's that connect direct)
510 $self->{group} = [ 'local' ];
514 # return a prompt for a field
519 my ($self, $ele) = @_;
523 # some variable accessors
527 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
537 $self->{$s} = $val if defined $val;
538 return exists $self->{$s} ? $self->{$s} : 1;
543 return _want('beep', @_);
548 return _want('ann', @_);
553 return _want('wwv', @_);
558 return _want('wcy', @_);
563 return _want('echo', @_);
568 return _want('wx', @_);
573 return _want('dx', @_);
578 return _want('talk', @_);
583 return _want('grid', @_);
588 return _want('email', @_);
593 return _want('ann_talk', @_);
598 return _want('pc16', @_);
603 return _want('sendpc16', @_);
608 return _want('routepc16', @_);
615 $self->{wantlogininfo} = $val if defined $val;
616 return $self->{wantlogininfo};
622 return $self->{sort} =~ /[ACRSX]/;
628 return $self->{sort} eq 'U';
634 return $self->{sort} eq 'B';
640 return $self->{sort} eq 'S';
646 return $self->{sort} eq 'C';
652 return $self->{sort} eq 'X';
658 return $self->{sort} eq 'R';
664 return $self->{sort} eq 'A';
670 delete $self->{passwd};
676 delete $self->{passphrase};