2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
21 use vars qw($VERSION $BRANCH);
22 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
23 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
24 $main::build += $VERSION;
25 $main::branch += $BRANCH;
27 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime);
32 $lastoperinterval = 60*24*60*60;
35 # hash of valid elements and a simple prompt
38 alias => '0,Real Callsign',
41 lat => '0,Latitude,slat',
42 long => '0,Longitude,slong',
44 email => '0,E-mail Address,parray',
45 priv => '9,Privilege Level',
46 lastin => '0,Last Time in,cldatetime',
47 passwd => '9,Password,yesno',
48 passphrase => '9,Pass Phrase,yesno',
49 addr => '0,Full Address',
50 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
51 xpert => '0,Expert Status,yesno',
53 node => '0,Last Node',
54 homenode => '0,Home Node',
55 lockout => '9,Locked out?,yesno', # won't let them in at all
56 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
57 annok => '9,Accept Announces?,yesno', # accept his announces?
59 hmsgno => '0,Highest Msgno',
60 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
61 isolate => '9,Isolate network,yesno',
62 wantbeep => '0,Req Beep,yesno',
63 wantann => '0,Req Announce,yesno',
64 wantwwv => '0,Req WWV,yesno',
65 wantwcy => '0,Req WCY,yesno',
66 wantecho => '0,Req Echo,yesno',
67 wanttalk => '0,Req Talk,yesno',
68 wantwx => '0,Req WX,yesno',
69 wantdx => '0,Req DX Spots,yesno',
70 wantemail => '0,Req Msgs as Email,yesno',
71 pagelth => '0,Current Pagelth',
72 pingint => '9,Node Ping interval',
73 nopings => '9,Ping Obs Count',
74 wantlogininfo => '9,Login info req,yesno',
75 wantgrid => '0,DX Grid Info,yesno',
76 wantann_talk => '0,Talklike Anns,yesno',
77 wantpc90 => '1,Req PC90,yesno',
78 wantnp => '1,Req New Protocol,yesno',
79 lastoper => '9,Last for/oper,cldatetime',
80 nothere => '0,Not Here Text',
81 registered => '9,Registered?,yesno',
82 prompt => '0,Required Prompt',
83 version => '1,Version',
93 return if $name =~ /::DESTROY$/;
96 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
97 # this clever line of code creates a subroutine which takes over from autoload
98 # from OO Perl - Conway
99 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
101 $self->{$name} = shift;
103 return $self->{$name};
109 # initialise the system
113 my ($pkg, $fn, $mode) = @_;
115 confess "need a filename in User" if !$fn;
118 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
120 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
130 confess "need a filename in User" if !$fn;
136 # periodic processing
140 if ($main::systime > $lasttime + 15) {
142 $lasttime = $main::systime;
157 # new - create a new user
164 # $call =~ s/-\d+$//o;
166 # confess "can't create existing call $call in User\n!" if $u{$call};
168 my $self = bless {}, $pkg;
169 $self->{call} = $call;
170 $self->{'sort'} = 'U';
176 # get - get an existing user - this seems to return a different reference everytime it is
185 unless ($dbm->get($call, $data)) {
186 return decode($data);
192 # get an existing either from the channel (if there is one) or from the database
194 # It is important to note that if you have done a get (for the channel say) and you
195 # want access or modify that you must use this call (and you must NOT use get's all
196 # over the place willy nilly!)
204 my $dxchan = DXChannel->get($call);
205 return $dxchan->user if $dxchan;
206 my $rref = Route::get($call);
207 return $rref->user if $rref && exists $rref->{user};
208 return $pkg->get($call);
212 # get all callsigns in the database
217 return (sort keys %u);
227 confess "Trying to put nothing!" unless $self && ref $self;
228 my $call = $self->{call};
229 # delete all instances of this
230 # for ($dbm->get_dup($call)) {
231 # $dbm->del_dup($call, $_);
234 delete $self->{annok} if $self->{annok};
235 delete $self->{dxok} if $self->{dxok};
236 $dbm->put($call, $self->encode);
240 # create a string from a user reference
245 my $dd = new Data::Dumper([$self]);
248 $dd->Quotekeys($] < 5.005 ? 1 : 0);
253 # create a hash from a string
269 # del - delete a user
275 my $call = $self->{call};
276 # delete all instances of this
277 # for ($dbm->get_dup($call)) {
278 # $dbm->del_dup($call, $_);
284 # close - close down a user
290 $self->{lastin} = time;
304 # return a list of valid elements
314 # export the database to an ascii file
322 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
323 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
324 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
325 rename "$fn.o", "$fn.oo" if -e "$fn.o";
326 rename "$fn", "$fn.o" if -e "$fn";
330 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
335 my $t = scalar localtime;
336 print $fh q{#!/usr/bin/perl
338 # The exported userfile for a DXSpider System
340 # Input file: $filename
346 # search local then perl directories
350 # root of directory tree for this system
352 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
354 unshift @INC, "$root/perl"; # this IS the right way round!
355 unshift @INC, "$root/local";
357 # try to detect a lockfile (this isn't atomic but
359 $lockfn = "$root/perl/cluster.lck"; # lock file name
361 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
364 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
375 $main::userfn = shift @ARGV;
376 print "user filename now $userfn\n";
379 DXUser->del_file($main::userfn);
380 DXUser->init($main::userfn, 1);
387 my $ref = decode($f[1]);
392 print "# Error: $f[0]\t$f[1]\n";
396 DXUser->sync; DXUser->finish;
397 print "There are $count user records and $err errors\n";
399 print $fh "__DATA__\n";
401 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
402 if (!is_callsign($key) || $key =~ /^0/) {
403 Log('DXCommand', "Export Error1: $key\t$val");
404 eval {$dbm->del($key)};
405 dbg(carp("Export Error1: $key\t$val\n$@")) if $@;
409 my $ref = decode($val);
411 print $fh "$key\t" . $ref->encode . "\n";
414 Log('DXCommand', "Export Error2: $key\t$val");
415 eval {$dbm->del($key)};
416 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
422 return "$count Users $err Errors ('sh/log Export' for details)";
429 # add one or more groups
433 my $ref = $self->{group} || [ 'local' ];
434 $self->{group} = $ref if !$self->{group};
435 push @$ref, @_ if @_;
438 # remove one or more groups
442 my $ref = $self->{group} || [ 'local' ];
445 $self->{group} = $ref if !$self->{group};
447 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
450 # does this thing contain all the groups listed?
454 my $ref = $self->{group};
457 return 0 if !$ref || @_ == 0;
458 return 1 if @$ref == 0 && @_ == 0;
459 for ($n = 0; $n < @_; ) {
462 $n++ if grep $_ eq $a, @_;
468 # simplified group test just for one group
473 my $ref = $self->{group};
476 return grep $_ eq $s, $ref;
479 # set up a default group (only happens for them's that connect direct)
483 $self->{group} = [ 'local' ];
487 # return a prompt for a field
492 my ($self, $ele) = @_;
496 # some variable accessors
500 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
510 $self->{$s} = $val if defined $val;
511 return exists $self->{$s} ? $self->{$s} : 1;
516 return _want('beep', @_);
521 return _want('ann', @_);
526 return _want('wwv', @_);
531 return _want('wcy', @_);
536 return _want('echo', @_);
541 return _want('wx', @_);
546 return _want('dx', @_);
551 return _want('talk', @_);
556 return _want('grid', @_);
561 return _want('email', @_);
566 return _want('ann_talk', @_);
573 $self->{wantlogininfo} = $val if defined $val;
574 return $self->{wantlogininfo};
580 return $self->{sort} =~ /[ACRSX]/;
586 return $self->{sort} eq 'U';
592 return $self->{sort} eq 'B';
598 return $self->{sort} eq 'S';
604 return $self->{sort} eq 'C';
610 return $self->{sort} eq 'X';
616 return $self->{sort} eq 'R';
622 return $self->{sort} eq 'A';
628 delete $self->{passwd};
634 delete $self->{passphrase};