removed calls to $dbm->del_dup from DXUser.pm
[spider.git] / perl / DXUser.pm
1 #
2 # DX cluster user routines
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package DXUser;
10
11 use DXLog;
12 use DB_File;
13 use Data::Dumper;
14 use Fcntl;
15 use IO::File;
16 use DXDebug;
17
18 use strict;
19 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime);
20
21 %u = ();
22 $dbm = undef;
23 $filename = undef;
24 $lastoperinterval = 30*24*60*60;
25 $lasttime = 0;
26
27 # hash of valid elements and a simple prompt
28 %valid = (
29                   call => '0,Callsign',
30                   alias => '0,Real Callsign',
31                   name => '0,Name',
32                   qth => '0,Home QTH',
33                   lat => '0,Latitude,slat',
34                   long => '0,Longitude,slong',
35                   qra => '0,Locator',
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',
43                   bbs => '0,Home BBS',
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?
50                   lang => '0,Language',
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',
68                  );
69
70 no strict;
71 sub AUTOLOAD
72 {
73         my $self = shift;
74         my $name = $AUTOLOAD;
75   
76         return if $name =~ /::DESTROY$/;
77         $name =~ s/.*:://o;
78   
79         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
80         if (@_) {
81                 $self->{$name} = shift;
82         }
83         return $self->{$name};
84 }
85
86 use strict;
87
88 #
89 # initialise the system
90 #
91 sub init
92 {
93         my ($pkg, $fn, $mode) = @_;
94   
95         confess "need a filename in User" if !$fn;
96         $fn .= ".v2";
97         if ($mode) {
98                 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
99         } else {
100                 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
101         }
102         
103         $filename = $fn;
104 }
105
106 sub del_file
107 {
108         my ($pkg, $fn) = @_;
109   
110         confess "need a filename in User" if !$fn;
111         $fn .= ".v2";
112         unlink $fn;
113 }
114
115 #
116 # periodic processing
117 #
118 sub process
119 {
120         if ($main::systime > $lasttime + 15) {
121                 $dbm->sync;
122                 $lasttime = $main::systime;
123         }
124 }
125
126 #
127 # close the system
128 #
129
130 sub finish
131 {
132         undef $dbm;
133         untie %u;
134 }
135
136 #
137 # new - create a new user
138 #
139
140 sub new
141 {
142         my $pkg = shift;
143         my $call = uc shift;
144         #  $call =~ s/-\d+$//o;
145   
146 #       confess "can't create existing call $call in User\n!" if $u{$call};
147
148         my $self = bless {}, $pkg;
149         $self->{call} = $call;
150         $self->{'sort'} = 'U';
151         $self->put;
152         return $self;
153 }
154
155 #
156 # get - get an existing user - this seems to return a different reference everytime it is
157 #       called - see below
158 #
159
160 sub get
161 {
162         my $pkg = shift;
163         my $call = uc shift;
164         my $data;
165         unless ($dbm->get($call, $data)) {
166                 return decode($data);
167         }
168         return undef;
169 }
170
171 #
172 # get an existing either from the channel (if there is one) or from the database
173 #
174 # It is important to note that if you have done a get (for the channel say) and you
175 # want access or modify that you must use this call (and you must NOT use get's all
176 # over the place willy nilly!)
177 #
178
179 sub get_current
180 {
181         my $pkg = shift;
182         my $call = uc shift;
183   
184         my $dxchan = DXChannel->get($call);
185         return $dxchan->user if $dxchan;
186         my $data;
187         unless ($dbm->get($call, $data)) {
188                 return decode($data);
189         }
190         return undef;
191 }
192
193 #
194 # get all callsigns in the database 
195 #
196
197 sub get_all_calls
198 {
199         return (sort keys %u);
200 }
201
202 #
203 # put - put a user
204 #
205
206 sub put
207 {
208         my $self = shift;
209         confess "Trying to put nothing!" unless $self && ref $self;
210         my $call = $self->{call};
211         # delete all instances of this 
212 #       for ($dbm->get_dup($call)) {
213 #               $dbm->del_dup($call, $_);
214 #       }
215         $dbm->del($call);
216         delete $self->{annok} if $self->{annok};
217         delete $self->{dxok} if $self->{dxok};
218         $dbm->put($call, $self->encode);
219 }
220
221
222 # create a string from a user reference
223 #
224 sub encode
225 {
226         my $self = shift;
227         my $dd = new Data::Dumper([$self]);
228         $dd->Indent(0);
229         $dd->Terse(1);
230     $dd->Quotekeys($] < 5.005 ? 1 : 0);
231         return $dd->Dumpxs;
232 }
233
234 #
235 # create a hash from a string
236 #
237 sub decode
238 {
239         my $s = shift;
240         my $ref;
241         eval '$ref = ' . $s;
242         if ($@) {
243                 dbg('err', $@) if $@;
244                 Log('err', $@) if $@;
245                 $ref = undef;
246         }
247         return $ref;
248 }
249
250 #
251 # del - delete a user
252 #
253
254 sub del
255 {
256         my $self = shift;
257         my $call = $self->{call};
258         # delete all instances of this 
259 #       for ($dbm->get_dup($call)) {
260 #               $dbm->del_dup($call, $_);
261 #       }
262         $dbm->del($call);
263 }
264
265 #
266 # close - close down a user
267 #
268
269 sub close
270 {
271         my $self = shift;
272         $self->{lastin} = time;
273         $self->put();
274 }
275
276 #
277 # sync the database
278 #
279
280 sub sync
281 {
282         $dbm->sync;
283 }
284
285 #
286 # return a list of valid elements 
287
288
289 sub fields
290 {
291         return keys(%valid);
292 }
293
294
295 #
296 # export the database to an ascii file
297 #
298
299 sub export
300 {
301         my $fn = shift;
302         
303         # save old ones
304         rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
305         rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
306         rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
307         rename "$fn.o", "$fn.oo" if -e "$fn.o";
308         rename "$fn", "$fn.o" if -e "$fn";
309
310         my $count = 0;
311         my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
312         if ($fh) {
313                 my $ref = 0;
314                 my $key = 0;
315                 my $action;
316                 my $t = scalar localtime;
317                 print $fh q{#!/usr/bin/perl
318 #
319 # The exported userfile for a DXSpider System
320 #
321 # Input file: $filename
322 #       Time: $t
323 #
324                         
325 package main;
326                         
327 # search local then perl directories
328 BEGIN {
329         umask 002;
330                                 
331         # root of directory tree for this system
332         $root = "/spider"; 
333         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
334         
335         unshift @INC, "$root/perl";     # this IS the right way round!
336         unshift @INC, "$root/local";
337         
338         # try to detect a lockfile (this isn't atomic but 
339         # should do for now
340         $lockfn = "$root/perl/cluster.lock";       # lock file name
341         if (-e $lockfn) {
342                 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
343                 my $pid = <CLLOCK>;
344                 chomp $pid;
345                 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
346                 close CLLOCK;
347         }
348 }
349
350 package DXUser;
351
352 use DXVars;
353 use DXUser;
354
355 if (@ARGV) {
356         $main::userfn = shift @ARGV;
357         print "user filename now $userfn\n";
358 }
359
360 DXUser->del_file($main::userfn);
361 DXUser->init($main::userfn, 1);
362
363 %u = (
364   };
365
366         for ($action = R_FIRST; !$dbm->seq($key, $ref, $action); $action = R_NEXT) {
367                         print $fh "'$key' => q{$ref},\n";
368                         ++$count;
369                 } 
370         print $fh ");\n#\nprint \"there were $count records\\n\";\n#\n";
371         print $fh "DXUser->sync; DXUser->finish;\n#\n";
372         $fh->close;
373     } 
374         return $count;
375 }
376
377 #
378 # group handling
379 #
380
381 # add one or more groups
382 sub add_group
383 {
384         my $self = shift;
385         my $ref = $self->{group} || [ 'local' ];
386         $self->{group} = $ref if !$self->{group};
387         push @$ref, @_ if @_;
388 }
389
390 # remove one or more groups
391 sub del_group
392 {
393         my $self = shift;
394         my $ref = $self->{group} || [ 'local' ];
395         my @in = @_;
396         
397         $self->{group} = $ref if !$self->{group};
398         
399         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
400 }
401
402 # does this thing contain all the groups listed?
403 sub union
404 {
405         my $self = shift;
406         my $ref = $self->{group};
407         my $n;
408         
409         return 0 if !$ref || @_ == 0;
410         return 1 if @$ref == 0 && @_ == 0;
411         for ($n = 0; $n < @_; ) {
412                 for (@$ref) {
413                         my $a = $_;
414                         $n++ if grep $_ eq $a, @_; 
415                 }
416         }
417         return $n >= @_;
418 }
419
420 # simplified group test just for one group
421 sub in_group
422 {
423         my $self = shift;
424         my $s = shift;
425         my $ref = $self->{group};
426         
427         return 0 if !$ref;
428         return grep $_ eq $s, $ref;
429 }
430
431 # set up a default group (only happens for them's that connect direct)
432 sub new_group
433 {
434         my $self = shift;
435         $self->{group} = [ 'local' ];
436 }
437
438 #
439 # return a prompt for a field
440 #
441
442 sub field_prompt
443
444         my ($self, $ele) = @_;
445         return $valid{$ele};
446 }
447
448 # some variable accessors
449 sub sort
450 {
451         my $self = shift;
452         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
453 }
454
455 # some accessors
456 sub _want
457 {
458         my $n = shift;
459         my $self = shift;
460         my $val = shift;
461         my $s = "want$n";
462         $self->{$s} = $val if defined $val;
463         return exists $self->{$s} ? $self->{$s} : 1;
464 }
465
466 sub wantbeep
467 {
468         return _want('beep', @_);
469 }
470
471 sub wantann
472 {
473         return _want('ann', @_);
474 }
475
476 sub wantwwv
477 {
478         return _want('wwv', @_);
479 }
480
481 sub wantwcy
482 {
483         return _want('wcy', @_);
484 }
485
486 sub wantecho
487 {
488         return _want('echo', @_);
489 }
490
491 sub wantwx
492 {
493         return _want('wx', @_);
494 }
495
496 sub wantdx
497 {
498         return _want('dx', @_);
499 }
500
501 sub wanttalk
502 {
503         return _want('talk', @_);
504 }
505
506 sub wantgrid
507 {
508         return _want('grid', @_);
509 }
510
511 sub wantlogininfo
512 {
513         my $self = shift;
514         my $n = shift;
515         $self->{wantlogininfo} = $n if $n;
516         return exists $self->{wantlogininfo} ? $self->{wantlogininfo} : 0;
517 }
518
519 sub is_node
520 {
521         my $self = shift;
522         return $self->{sort} =~ /[ACRSX]/;
523 }
524
525 sub is_user
526 {
527         my $self = shift;
528         return $self->{sort} eq 'U';
529 }
530
531 sub is_bbs
532 {
533         my $self = shift;
534         return $self->{sort} eq 'B';
535 }
536
537 sub is_spider
538 {
539         my $self = shift;
540         return $self->{sort} eq 'S';
541 }
542
543 sub is_clx
544 {
545         my $self = shift;
546         return $self->{sort} eq 'C';
547 }
548
549 sub is_dxnet
550 {
551         my $self = shift;
552         return $self->{sort} eq 'X';
553 }
554
555 sub is_arcluster
556 {
557         my $self = shift;
558         return $self->{sort} eq 'R';
559 }
560
561 sub is_ak1a
562 {
563         my $self = shift;
564         return $self->{sort} eq 'A';
565 }
566 1;
567 __END__
568
569
570
571
572