2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
19 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime);
24 $lastoperinterval = 30*24*60*60;
27 # hash of valid elements and a simple prompt
30 alias => '0,Real Callsign',
33 lat => '0,Latitude,slat',
34 long => '0,Longitude,slong',
36 email => '0,E-mail Address',
37 priv => '9,Privilege Level',
38 lastin => '0,Last Time in,cldatetime',
39 passwd => '9,Password',
40 addr => '0,Full Address',
41 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
42 xpert => '0,Expert Status,yesno',
44 node => '0,Last Node',
45 homenode => '0,Home Node',
46 lockout => '9,Locked out?,yesno', # won't let them in at all
47 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
48 annok => '9,Accept Announces?,yesno', # accept his announces?
49 reg => '0,Registered?,yesno', # is this user registered?
51 hmsgno => '0,Highest Msgno',
52 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
53 isolate => '9,Isolate network,yesno',
54 wantbeep => '0,Rec Beep,yesno',
55 wantann => '0,Rec Announce,yesno',
56 wantwwv => '0,Rec WWV,yesno',
57 wantwcy => '0,Rec WCY,yesno',
58 wantecho => '0,Rec Echo,yesno',
59 wanttalk => '0,Rec Talk,yesno',
60 wantwx => '0,Rec WX,yesno',
61 wantdx => '0,Rec DX Spots,yesno',
62 pagelth => '0,Current Pagelth',
63 pingint => '9,Node Ping interval',
64 nopings => '9,Ping Obs Count',
65 wantlogininfo => '9,Login info req,yesno',
66 wantgrid => '0,DX Grid Info,yesno',
67 lastoper => '9,Last for/oper,cldatetime',
76 return if $name =~ /::DESTROY$/;
79 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
80 # this clever line of code creates a subroutine which takes over from autoload
81 # from OO Perl - Conway
82 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
84 $self->{$name} = shift;
86 return $self->{$name};
92 # initialise the system
96 my ($pkg, $fn, $mode) = @_;
98 confess "need a filename in User" if !$fn;
101 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
103 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
113 confess "need a filename in User" if !$fn;
119 # periodic processing
123 if ($main::systime > $lasttime + 15) {
125 $lasttime = $main::systime;
140 # new - create a new user
147 # $call =~ s/-\d+$//o;
149 # confess "can't create existing call $call in User\n!" if $u{$call};
151 my $self = bless {}, $pkg;
152 $self->{call} = $call;
153 $self->{'sort'} = 'U';
159 # get - get an existing user - this seems to return a different reference everytime it is
168 unless ($dbm->get($call, $data)) {
169 return decode($data);
175 # get an existing either from the channel (if there is one) or from the database
177 # It is important to note that if you have done a get (for the channel say) and you
178 # want access or modify that you must use this call (and you must NOT use get's all
179 # over the place willy nilly!)
187 my $dxchan = DXChannel->get($call);
188 return $dxchan->user if $dxchan;
190 unless ($dbm->get($call, $data)) {
191 return decode($data);
197 # get all callsigns in the database
202 return (sort keys %u);
212 confess "Trying to put nothing!" unless $self && ref $self;
213 my $call = $self->{call};
214 # delete all instances of this
215 # for ($dbm->get_dup($call)) {
216 # $dbm->del_dup($call, $_);
219 delete $self->{annok} if $self->{annok};
220 delete $self->{dxok} if $self->{dxok};
221 $dbm->put($call, $self->encode);
225 # create a string from a user reference
230 my $dd = new Data::Dumper([$self]);
233 $dd->Quotekeys($] < 5.005 ? 1 : 0);
238 # create a hash from a string
246 dbg('err', $@) if $@;
247 Log('err', $@) if $@;
254 # del - delete a user
260 my $call = $self->{call};
261 # delete all instances of this
262 # for ($dbm->get_dup($call)) {
263 # $dbm->del_dup($call, $_);
269 # close - close down a user
275 $self->{lastin} = time;
289 # return a list of valid elements
299 # export the database to an ascii file
307 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
308 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
309 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
310 rename "$fn.o", "$fn.oo" if -e "$fn.o";
311 rename "$fn", "$fn.o" if -e "$fn";
314 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
319 my $t = scalar localtime;
320 print $fh q{#!/usr/bin/perl
322 # The exported userfile for a DXSpider System
324 # Input file: $filename
330 # search local then perl directories
334 # root of directory tree for this system
336 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
338 unshift @INC, "$root/perl"; # this IS the right way round!
339 unshift @INC, "$root/local";
341 # try to detect a lockfile (this isn't atomic but
343 $lockfn = "$root/perl/cluster.lck"; # lock file name
345 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
348 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
359 $main::userfn = shift @ARGV;
360 print "user filename now $userfn\n";
363 DXUser->del_file($main::userfn);
364 DXUser->init($main::userfn, 1);
369 for ($action = R_FIRST; !$dbm->seq($key, $ref, $action); $action = R_NEXT) {
370 print $fh "'$key' => q{$ref},\n";
373 print $fh ");\n#\nprint \"there were $count records\\n\";\n#\n";
374 print $fh "DXUser->sync; DXUser->finish;\n#\n";
384 # add one or more groups
388 my $ref = $self->{group} || [ 'local' ];
389 $self->{group} = $ref if !$self->{group};
390 push @$ref, @_ if @_;
393 # remove one or more groups
397 my $ref = $self->{group} || [ 'local' ];
400 $self->{group} = $ref if !$self->{group};
402 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
405 # does this thing contain all the groups listed?
409 my $ref = $self->{group};
412 return 0 if !$ref || @_ == 0;
413 return 1 if @$ref == 0 && @_ == 0;
414 for ($n = 0; $n < @_; ) {
417 $n++ if grep $_ eq $a, @_;
423 # simplified group test just for one group
428 my $ref = $self->{group};
431 return grep $_ eq $s, $ref;
434 # set up a default group (only happens for them's that connect direct)
438 $self->{group} = [ 'local' ];
442 # return a prompt for a field
447 my ($self, $ele) = @_;
451 # some variable accessors
455 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
465 $self->{$s} = $val if defined $val;
466 return exists $self->{$s} ? $self->{$s} : 1;
471 return _want('beep', @_);
476 return _want('ann', @_);
481 return _want('wwv', @_);
486 return _want('wcy', @_);
491 return _want('echo', @_);
496 return _want('wx', @_);
501 return _want('dx', @_);
506 return _want('talk', @_);
511 return _want('grid', @_);
518 $self->{wantlogininfo} = $n if $n;
519 return exists $self->{wantlogininfo} ? $self->{wantlogininfo} : 0;
525 return $self->{sort} =~ /[ACRSX]/;
531 return $self->{sort} eq 'U';
537 return $self->{sort} eq 'B';
543 return $self->{sort} eq 'S';
549 return $self->{sort} eq 'C';
555 return $self->{sort} eq 'X';
561 return $self->{sort} eq 'R';
567 return $self->{sort} eq 'A';