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