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);
33 $lastoperinterval = 60*24*60*60;
37 # hash of valid elements and a simple prompt
40 alias => '0,Real Callsign',
43 lat => '0,Latitude,slat',
44 long => '0,Longitude,slong',
46 email => '0,E-mail Address,parray',
47 priv => '9,Privilege Level',
48 lastin => '0,Last Time in,cldatetime',
49 passwd => '9,Password,yesno',
50 passphrase => '9,Pass Phrase,yesno',
51 addr => '0,Full Address',
52 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
53 xpert => '0,Expert Status,yesno',
55 node => '0,Last Node',
56 homenode => '0,Home Node',
57 lockout => '9,Locked out?,yesno', # won't let them in at all
58 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
59 annok => '9,Accept Announces?,yesno', # accept his announces?
61 hmsgno => '0,Highest Msgno',
62 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
63 isolate => '9,Isolate network,yesno',
64 wantbeep => '0,Req Beep,yesno',
65 wantann => '0,Req Announce,yesno',
66 wantwwv => '0,Req WWV,yesno',
67 wantwcy => '0,Req WCY,yesno',
68 wantecho => '0,Req Echo,yesno',
69 wanttalk => '0,Req Talk,yesno',
70 wantwx => '0,Req WX,yesno',
71 wantdx => '0,Req DX Spots,yesno',
72 wantemail => '0,Req Msgs as Email,yesno',
73 pagelth => '0,Current Pagelth',
74 pingint => '9,Node Ping interval',
75 nopings => '9,Ping Obs Count',
76 wantlogininfo => '9,Login info req,yesno',
77 wantgrid => '0,DX Grid Info,yesno',
78 wantann_talk => '0,Talklike Anns,yesno',
79 wantpc90 => '1,Req PC90,yesno',
80 wantnp => '1,Req New Protocol,yesno',
81 lastoper => '9,Last for/oper,cldatetime',
82 nothere => '0,Not Here Text',
83 registered => '9,Registered?,yesno',
84 prompt => '0,Required Prompt',
85 version => '1,Version',
95 return if $name =~ /::DESTROY$/;
98 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
99 # this clever line of code creates a subroutine which takes over from autoload
100 # from OO Perl - Conway
101 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
103 $self->{$name} = shift;
105 return $self->{$name};
111 # initialise the system
115 my ($pkg, $fn, $mode) = @_;
117 confess "need a filename in User" if !$fn;
120 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
122 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
126 $lru = LRU->newbase("DXUser", $lrusize);
133 confess "need a filename in User" if !$fn;
139 # periodic processing
143 if ($main::systime > $lasttime + 15) {
145 $lasttime = $main::systime;
160 # new - create a new user
167 # $call =~ s/-\d+$//o;
169 # confess "can't create existing call $call in User\n!" if $u{$call};
171 my $self = bless {}, $pkg;
172 $self->{call} = $call;
173 $self->{'sort'} = 'U';
179 # get - get an existing user - this seems to return a different reference everytime it is
189 # is it in the LRU cache?
190 my $ref = $lru->get($call);
194 unless ($dbm->get($call, $data)) {
195 $ref = decode($data);
196 $lru->put($call, $ref);
203 # get an existing either from the channel (if there is one) or from the database
205 # It is important to note that if you have done a get (for the channel say) and you
206 # want access or modify that you must use this call (and you must NOT use get's all
207 # over the place willy nilly!)
215 my $dxchan = DXChannel->get($call);
216 return $dxchan->user if $dxchan;
217 my $rref = Route::get($call);
218 return $rref->user if $rref && exists $rref->{user};
219 return $pkg->get($call);
223 # get all callsigns in the database
228 return (sort keys %u);
238 confess "Trying to put nothing!" unless $self && ref $self;
239 my $call = $self->{call};
240 # delete all instances of this
241 # for ($dbm->get_dup($call)) {
242 # $dbm->del_dup($call, $_);
245 delete $self->{annok} if $self->{annok};
246 delete $self->{dxok} if $self->{dxok};
247 $lru->put($call, $self);
248 my $ref = $self->encode;
249 $dbm->put($call, $ref);
253 # create a string from a user reference
258 my $dd = new Data::Dumper([$self]);
261 $dd->Quotekeys($] < 5.005 ? 1 : 0);
266 # create a hash from a string
282 # del - delete a user
288 my $call = $self->{call};
289 # delete all instances of this
290 # for ($dbm->get_dup($call)) {
291 # $dbm->del_dup($call, $_);
298 # close - close down a user
304 $self->{lastin} = time;
318 # return a list of valid elements
328 # export the database to an ascii file
336 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
337 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
338 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
339 rename "$fn.o", "$fn.oo" if -e "$fn.o";
340 rename "$fn", "$fn.o" if -e "$fn";
344 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
349 my $t = scalar localtime;
350 print $fh q{#!/usr/bin/perl
352 # The exported userfile for a DXSpider System
354 # Input file: $filename
360 # search local then perl directories
364 # root of directory tree for this system
366 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
368 unshift @INC, "$root/perl"; # this IS the right way round!
369 unshift @INC, "$root/local";
371 # try to detect a lockfile (this isn't atomic but
373 $lockfn = "$root/perl/cluster.lck"; # lock file name
375 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
378 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
389 $main::userfn = shift @ARGV;
390 print "user filename now $userfn\n";
393 DXUser->del_file($main::userfn);
394 DXUser->init($main::userfn, 1);
401 my $ref = decode($f[1]);
406 print "# Error: $f[0]\t$f[1]\n";
410 DXUser->sync; DXUser->finish;
411 print "There are $count user records and $err errors\n";
413 print $fh "__DATA__\n";
415 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
416 if (!is_callsign($key) || $key =~ /^0/) {
417 Log('DXCommand', "Export Error1: $key\t$val");
418 eval {$dbm->del($key)};
419 dbg(carp("Export Error1: $key\t$val\n$@")) if $@;
423 my $ref = decode($val);
425 print $fh "$key\t" . $ref->encode . "\n";
428 Log('DXCommand', "Export Error2: $key\t$val");
429 eval {$dbm->del($key)};
430 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
436 return "$count Users $err Errors ('sh/log Export' for details)";
443 # add one or more groups
447 my $ref = $self->{group} || [ 'local' ];
448 $self->{group} = $ref if !$self->{group};
449 push @$ref, @_ if @_;
452 # remove one or more groups
456 my $ref = $self->{group} || [ 'local' ];
459 $self->{group} = $ref if !$self->{group};
461 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
464 # does this thing contain all the groups listed?
468 my $ref = $self->{group};
471 return 0 if !$ref || @_ == 0;
472 return 1 if @$ref == 0 && @_ == 0;
473 for ($n = 0; $n < @_; ) {
476 $n++ if grep $_ eq $a, @_;
482 # simplified group test just for one group
487 my $ref = $self->{group};
490 return grep $_ eq $s, $ref;
493 # set up a default group (only happens for them's that connect direct)
497 $self->{group} = [ 'local' ];
501 # return a prompt for a field
506 my ($self, $ele) = @_;
510 # some variable accessors
514 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
524 $self->{$s} = $val if defined $val;
525 return exists $self->{$s} ? $self->{$s} : 1;
530 return _want('beep', @_);
535 return _want('ann', @_);
540 return _want('wwv', @_);
545 return _want('wcy', @_);
550 return _want('echo', @_);
555 return _want('wx', @_);
560 return _want('dx', @_);
565 return _want('talk', @_);
570 return _want('grid', @_);
575 return _want('email', @_);
580 return _want('ann_talk', @_);
587 $self->{wantlogininfo} = $val if defined $val;
588 return $self->{wantlogininfo};
594 return $self->{sort} =~ /[ACRSX]/;
600 return $self->{sort} eq 'U';
606 return $self->{sort} eq 'B';
612 return $self->{sort} eq 'S';
618 return $self->{sort} eq 'C';
624 return $self->{sort} eq 'X';
630 return $self->{sort} eq 'R';
636 return $self->{sort} eq 'A';
642 delete $self->{passwd};
648 delete $self->{passphrase};