2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
20 use vars qw($VERSION $BRANCH);
21 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
22 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
23 $main::build += $VERSION;
24 $main::branch += $BRANCH;
26 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime);
31 $lastoperinterval = 60*24*60*60;
34 # hash of valid elements and a simple prompt
37 alias => '0,Real Callsign',
40 lat => '0,Latitude,slat',
41 long => '0,Longitude,slong',
43 email => '0,E-mail Address,parray',
44 priv => '9,Privilege Level',
45 lastin => '0,Last Time in,cldatetime',
46 passwd => '9,Password,yesno',
47 addr => '0,Full Address',
48 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
49 xpert => '0,Expert Status,yesno',
51 node => '0,Last Node',
52 homenode => '0,Home Node',
53 lockout => '9,Locked out?,yesno', # won't let them in at all
54 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
55 annok => '9,Accept Announces?,yesno', # accept his announces?
57 hmsgno => '0,Highest Msgno',
58 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
59 isolate => '9,Isolate network,yesno',
60 wantbeep => '0,Rec Beep,yesno',
61 wantann => '0,Rec Announce,yesno',
62 wantwwv => '0,Rec WWV,yesno',
63 wantwcy => '0,Rec WCY,yesno',
64 wantecho => '0,Rec Echo,yesno',
65 wanttalk => '0,Rec Talk,yesno',
66 wantwx => '0,Rec WX,yesno',
67 wantdx => '0,Rec DX Spots,yesno',
68 wantemail => '0,Rec Msgs as Email,yesno',
69 pagelth => '0,Current Pagelth',
70 pingint => '9,Node Ping interval',
71 nopings => '9,Ping Obs Count',
72 wantlogininfo => '9,Login info req,yesno',
73 wantgrid => '0,DX Grid Info,yesno',
74 wantann_talk => '0,Talklike Anns,yesno',
75 lastoper => '9,Last for/oper,cldatetime',
76 nothere => '0,Not Here Text',
77 registered => '9,Registered?,yesno',
78 prompt => '0,Required Prompt',
87 return if $name =~ /::DESTROY$/;
90 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
91 # this clever line of code creates a subroutine which takes over from autoload
92 # from OO Perl - Conway
93 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
95 $self->{$name} = shift;
97 return $self->{$name};
103 # initialise the system
107 my ($pkg, $fn, $mode) = @_;
109 confess "need a filename in User" if !$fn;
112 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
114 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
124 confess "need a filename in User" if !$fn;
130 # periodic processing
134 if ($main::systime > $lasttime + 15) {
136 $lasttime = $main::systime;
151 # new - create a new user
158 # $call =~ s/-\d+$//o;
160 # confess "can't create existing call $call in User\n!" if $u{$call};
162 my $self = bless {}, $pkg;
163 $self->{call} = $call;
164 $self->{'sort'} = 'U';
170 # get - get an existing user - this seems to return a different reference everytime it is
179 unless ($dbm->get($call, $data)) {
180 return decode($data);
186 # get an existing either from the channel (if there is one) or from the database
188 # It is important to note that if you have done a get (for the channel say) and you
189 # want access or modify that you must use this call (and you must NOT use get's all
190 # over the place willy nilly!)
198 my $dxchan = DXChannel->get($call);
199 return $dxchan->user if $dxchan;
201 unless ($dbm->get($call, $data)) {
202 return decode($data);
208 # get all callsigns in the database
213 return (sort keys %u);
223 confess "Trying to put nothing!" unless $self && ref $self;
224 my $call = $self->{call};
225 # delete all instances of this
226 # for ($dbm->get_dup($call)) {
227 # $dbm->del_dup($call, $_);
230 delete $self->{annok} if $self->{annok};
231 delete $self->{dxok} if $self->{dxok};
232 $dbm->put($call, $self->encode);
236 # create a string from a user reference
241 my $dd = new Data::Dumper([$self]);
244 $dd->Quotekeys($] < 5.005 ? 1 : 0);
249 # create a hash from a string
265 # del - delete a user
271 my $call = $self->{call};
272 # delete all instances of this
273 # for ($dbm->get_dup($call)) {
274 # $dbm->del_dup($call, $_);
280 # close - close down a user
286 $self->{lastin} = time;
300 # return a list of valid elements
310 # export the database to an ascii file
318 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
319 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
320 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
321 rename "$fn.o", "$fn.oo" if -e "$fn.o";
322 rename "$fn", "$fn.o" if -e "$fn";
325 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
330 my $t = scalar localtime;
331 print $fh q{#!/usr/bin/perl
333 # The exported userfile for a DXSpider System
335 # Input file: $filename
341 # search local then perl directories
345 # root of directory tree for this system
347 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
349 unshift @INC, "$root/perl"; # this IS the right way round!
350 unshift @INC, "$root/local";
352 # try to detect a lockfile (this isn't atomic but
354 $lockfn = "$root/perl/cluster.lck"; # lock file name
356 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
359 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
370 $main::userfn = shift @ARGV;
371 print "user filename now $userfn\n";
374 DXUser->del_file($main::userfn);
375 DXUser->init($main::userfn, 1);
380 for ($action = R_FIRST; !$dbm->seq($key, $ref, $action); $action = R_NEXT) {
381 print $fh "'$key' => q{$ref},\n";
384 print $fh ");\n#\nprint \"there were $count records\\n\";\n#\n";
385 print $fh "DXUser->sync; DXUser->finish;\n#\n";
395 # add one or more groups
399 my $ref = $self->{group} || [ 'local' ];
400 $self->{group} = $ref if !$self->{group};
401 push @$ref, @_ if @_;
404 # remove one or more groups
408 my $ref = $self->{group} || [ 'local' ];
411 $self->{group} = $ref if !$self->{group};
413 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
416 # does this thing contain all the groups listed?
420 my $ref = $self->{group};
423 return 0 if !$ref || @_ == 0;
424 return 1 if @$ref == 0 && @_ == 0;
425 for ($n = 0; $n < @_; ) {
428 $n++ if grep $_ eq $a, @_;
434 # simplified group test just for one group
439 my $ref = $self->{group};
442 return grep $_ eq $s, $ref;
445 # set up a default group (only happens for them's that connect direct)
449 $self->{group} = [ 'local' ];
453 # return a prompt for a field
458 my ($self, $ele) = @_;
462 # some variable accessors
466 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
476 $self->{$s} = $val if defined $val;
477 return exists $self->{$s} ? $self->{$s} : 1;
482 return _want('beep', @_);
487 return _want('ann', @_);
492 return _want('wwv', @_);
497 return _want('wcy', @_);
502 return _want('echo', @_);
507 return _want('wx', @_);
512 return _want('dx', @_);
517 return _want('talk', @_);
522 return _want('grid', @_);
527 return _want('email', @_);
532 return _want('ann_talk', @_);
539 $self->{wantlogininfo} = $val if defined $val;
540 return $self->{wantlogininfo};
546 return $self->{sort} =~ /[ACRSX]/;
552 return $self->{sort} eq 'U';
558 return $self->{sort} eq 'B';
564 return $self->{sort} eq 'S';
570 return $self->{sort} eq 'C';
576 return $self->{sort} eq 'X';
582 return $self->{sort} eq 'R';
588 return $self->{sort} eq 'A';
594 delete $self->{passwd};