add eval blocks around the ivalid key deletes in export_user
[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 use DXUtil;
18
19 use strict;
20
21 use vars qw($VERSION $BRANCH);
22 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
23 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
24 $main::build += $VERSION;
25 $main::branch += $BRANCH;
26
27 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime);
28
29 %u = ();
30 $dbm = undef;
31 $filename = undef;
32 $lastoperinterval = 60*24*60*60;
33 $lasttime = 0;
34
35 # hash of valid elements and a simple prompt
36 %valid = (
37                   call => '0,Callsign',
38                   alias => '0,Real Callsign',
39                   name => '0,Name',
40                   qth => '0,Home QTH',
41                   lat => '0,Latitude,slat',
42                   long => '0,Longitude,slong',
43                   qra => '0,Locator',
44                   email => '0,E-mail Address,parray',
45                   priv => '9,Privilege Level',
46                   lastin => '0,Last Time in,cldatetime',
47                   passwd => '9,Password,yesno',
48                   passphrase => '9,Pass Phrase,yesno',
49                   addr => '0,Full Address',
50                   'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
51                   xpert => '0,Expert Status,yesno',
52                   bbs => '0,Home BBS',
53                   node => '0,Last Node',
54                   homenode => '0,Home Node',
55                   lockout => '9,Locked out?,yesno',     # won't let them in at all
56                   dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
57                   annok => '9,Accept Announces?,yesno', # accept his announces?
58                   lang => '0,Language',
59                   hmsgno => '0,Highest Msgno',
60                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
61                   isolate => '9,Isolate network,yesno',
62                   wantbeep => '0,Req Beep,yesno',
63                   wantann => '0,Req Announce,yesno',
64                   wantwwv => '0,Req WWV,yesno',
65                   wantwcy => '0,Req WCY,yesno',
66                   wantecho => '0,Req Echo,yesno',
67                   wanttalk => '0,Req Talk,yesno',
68                   wantwx => '0,Req WX,yesno',
69                   wantdx => '0,Req DX Spots,yesno',
70                   wantemail => '0,Req Msgs as Email,yesno',
71                   pagelth => '0,Current Pagelth',
72                   pingint => '9,Node Ping interval',
73                   nopings => '9,Ping Obs Count',
74                   wantlogininfo => '9,Login info req,yesno',
75           wantgrid => '0,DX Grid Info,yesno',
76                   wantann_talk => '0,Talklike Anns,yesno',
77                   wantpc90 => '1,Req PC90,yesno',
78                   wantnp => '1,Req New Protocol,yesno',
79                   lastoper => '9,Last for/oper,cldatetime',
80                   nothere => '0,Not Here Text',
81                   registered => '9,Registered?,yesno',
82                   prompt => '0,Required Prompt',
83                   version => '1,Version',
84                   build => '1,Build',
85                  );
86
87 no strict;
88 sub AUTOLOAD
89 {
90         my $self = shift;
91         my $name = $AUTOLOAD;
92   
93         return if $name =~ /::DESTROY$/;
94         $name =~ s/.*:://o;
95   
96         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
97         # this clever line of code creates a subroutine which takes over from autoload
98         # from OO Perl - Conway
99         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
100         if (@_) {
101                 $self->{$name} = shift;
102         }
103         return $self->{$name};
104 }
105
106 use strict;
107
108 #
109 # initialise the system
110 #
111 sub init
112 {
113         my ($pkg, $fn, $mode) = @_;
114   
115         confess "need a filename in User" if !$fn;
116         $fn .= ".v2";
117         if ($mode) {
118                 $dbm = tie (%u, 'DB_File', $fn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
119         } else {
120                 $dbm = tie (%u, 'DB_File', $fn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!)";
121         }
122         
123         $filename = $fn;
124 }
125
126 sub del_file
127 {
128         my ($pkg, $fn) = @_;
129   
130         confess "need a filename in User" if !$fn;
131         $fn .= ".v2";
132         unlink $fn;
133 }
134
135 #
136 # periodic processing
137 #
138 sub process
139 {
140         if ($main::systime > $lasttime + 15) {
141                 $dbm->sync;
142                 $lasttime = $main::systime;
143         }
144 }
145
146 #
147 # close the system
148 #
149
150 sub finish
151 {
152         undef $dbm;
153         untie %u;
154 }
155
156 #
157 # new - create a new user
158 #
159
160 sub new
161 {
162         my $pkg = shift;
163         my $call = uc shift;
164         #  $call =~ s/-\d+$//o;
165   
166 #       confess "can't create existing call $call in User\n!" if $u{$call};
167
168         my $self = bless {}, $pkg;
169         $self->{call} = $call;
170         $self->{'sort'} = 'U';
171         $self->put;
172         return $self;
173 }
174
175 #
176 # get - get an existing user - this seems to return a different reference everytime it is
177 #       called - see below
178 #
179
180 sub get
181 {
182         my $pkg = shift;
183         my $call = uc shift;
184         my $data;
185         unless ($dbm->get($call, $data)) {
186                 return decode($data);
187         }
188         return undef;
189 }
190
191 #
192 # get an existing either from the channel (if there is one) or from the database
193 #
194 # It is important to note that if you have done a get (for the channel say) and you
195 # want access or modify that you must use this call (and you must NOT use get's all
196 # over the place willy nilly!)
197 #
198
199 sub get_current
200 {
201         my $pkg = shift;
202         my $call = uc shift;
203   
204         my $dxchan = DXChannel->get($call);
205         return $dxchan->user if $dxchan;
206         my $rref = Route::get($call);
207         return $rref->user if $rref && exists $rref->{user};
208         return $pkg->get($call);
209 }
210
211 #
212 # get all callsigns in the database 
213 #
214
215 sub get_all_calls
216 {
217         return (sort keys %u);
218 }
219
220 #
221 # put - put a user
222 #
223
224 sub put
225 {
226         my $self = shift;
227         confess "Trying to put nothing!" unless $self && ref $self;
228         my $call = $self->{call};
229         # delete all instances of this 
230 #       for ($dbm->get_dup($call)) {
231 #               $dbm->del_dup($call, $_);
232 #       }
233         $dbm->del($call);
234         delete $self->{annok} if $self->{annok};
235         delete $self->{dxok} if $self->{dxok};
236         $dbm->put($call, $self->encode);
237 }
238
239
240 # create a string from a user reference
241 #
242 sub encode
243 {
244         my $self = shift;
245         my $dd = new Data::Dumper([$self]);
246         $dd->Indent(0);
247         $dd->Terse(1);
248     $dd->Quotekeys($] < 5.005 ? 1 : 0);
249         return $dd->Dumpxs;
250 }
251
252 #
253 # create a hash from a string
254 #
255 sub decode
256 {
257         my $s = shift;
258         my $ref;
259         eval '$ref = ' . $s;
260         if ($@) {
261                 dbg($@);
262                 Log('err', $@);
263                 $ref = undef;
264         }
265         return $ref;
266 }
267
268 #
269 # del - delete a user
270 #
271
272 sub del
273 {
274         my $self = shift;
275         my $call = $self->{call};
276         # delete all instances of this 
277 #       for ($dbm->get_dup($call)) {
278 #               $dbm->del_dup($call, $_);
279 #       }
280         $dbm->del($call);
281 }
282
283 #
284 # close - close down a user
285 #
286
287 sub close
288 {
289         my $self = shift;
290         $self->{lastin} = time;
291         $self->put();
292 }
293
294 #
295 # sync the database
296 #
297
298 sub sync
299 {
300         $dbm->sync;
301 }
302
303 #
304 # return a list of valid elements 
305
306
307 sub fields
308 {
309         return keys(%valid);
310 }
311
312
313 #
314 # export the database to an ascii file
315 #
316
317 sub export
318 {
319         my $fn = shift;
320         
321         # save old ones
322         rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
323         rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
324         rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
325         rename "$fn.o", "$fn.oo" if -e "$fn.o";
326         rename "$fn", "$fn.o" if -e "$fn";
327
328         my $count = 0;
329         my $err = 0;
330         my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
331         if ($fh) {
332                 my $key = 0;
333                 my $val = undef;
334                 my $action;
335                 my $t = scalar localtime;
336                 print $fh q{#!/usr/bin/perl
337 #
338 # The exported userfile for a DXSpider System
339 #
340 # Input file: $filename
341 #       Time: $t
342 #
343                         
344 package main;
345                         
346 # search local then perl directories
347 BEGIN {
348         umask 002;
349                                 
350         # root of directory tree for this system
351         $root = "/spider"; 
352         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
353         
354         unshift @INC, "$root/perl";     # this IS the right way round!
355         unshift @INC, "$root/local";
356         
357         # try to detect a lockfile (this isn't atomic but 
358         # should do for now
359         $lockfn = "$root/perl/cluster.lck";       # lock file name
360         if (-e $lockfn) {
361                 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
362                 my $pid = <CLLOCK>;
363                 chomp $pid;
364                 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
365                 close CLLOCK;
366         }
367 }
368
369 package DXUser;
370
371 use DXVars;
372 use DXUser;
373
374 if (@ARGV) {
375         $main::userfn = shift @ARGV;
376         print "user filename now $userfn\n";
377 }
378
379 DXUser->del_file($main::userfn);
380 DXUser->init($main::userfn, 1);
381 %u = ();
382 my $count = 0;
383 my $err = 0;
384 while (<DATA>) {
385         chomp;
386         my @f = split /\t/;
387         my $ref = decode($f[1]);
388         if ($ref) {
389                 $ref->put();
390                 $count++;
391         } else {
392                 print "# Error: $f[0]\t$f[1]\n";
393                 $err++
394         }
395 }
396 DXUser->sync; DXUser->finish;
397 print "There are $count user records and $err errors\n";
398 };
399                 print $fh "__DATA__\n";
400
401         for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
402                         if (!is_callsign($key) || $key =~ /^0/) {
403                                 Log('DXCommand', "Export Error1: $key\t$val");
404                                 eval {$dbm->del($key)};
405                                 dbg(carp("Export Error1: $key\t$val\n$@")) if $@;
406                                 ++$err;
407                                 next;
408                         }
409                         my $ref = decode($val);
410                         if ($ref) {
411                                 print $fh "$key\t" . $ref->encode . "\n";
412                                 ++$count;
413                         } else {
414                                 Log('DXCommand', "Export Error2: $key\t$val");
415                                 eval {$dbm->del($key)};
416                                 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
417                                 ++$err;
418                         }
419                 } 
420         $fh->close;
421     } 
422         return "$count Users $err Errors ('sh/log Export' for details)";
423 }
424
425 #
426 # group handling
427 #
428
429 # add one or more groups
430 sub add_group
431 {
432         my $self = shift;
433         my $ref = $self->{group} || [ 'local' ];
434         $self->{group} = $ref if !$self->{group};
435         push @$ref, @_ if @_;
436 }
437
438 # remove one or more groups
439 sub del_group
440 {
441         my $self = shift;
442         my $ref = $self->{group} || [ 'local' ];
443         my @in = @_;
444         
445         $self->{group} = $ref if !$self->{group};
446         
447         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
448 }
449
450 # does this thing contain all the groups listed?
451 sub union
452 {
453         my $self = shift;
454         my $ref = $self->{group};
455         my $n;
456         
457         return 0 if !$ref || @_ == 0;
458         return 1 if @$ref == 0 && @_ == 0;
459         for ($n = 0; $n < @_; ) {
460                 for (@$ref) {
461                         my $a = $_;
462                         $n++ if grep $_ eq $a, @_; 
463                 }
464         }
465         return $n >= @_;
466 }
467
468 # simplified group test just for one group
469 sub in_group
470 {
471         my $self = shift;
472         my $s = shift;
473         my $ref = $self->{group};
474         
475         return 0 if !$ref;
476         return grep $_ eq $s, $ref;
477 }
478
479 # set up a default group (only happens for them's that connect direct)
480 sub new_group
481 {
482         my $self = shift;
483         $self->{group} = [ 'local' ];
484 }
485
486 #
487 # return a prompt for a field
488 #
489
490 sub field_prompt
491
492         my ($self, $ele) = @_;
493         return $valid{$ele};
494 }
495
496 # some variable accessors
497 sub sort
498 {
499         my $self = shift;
500         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
501 }
502
503 # some accessors
504 sub _want
505 {
506         my $n = shift;
507         my $self = shift;
508         my $val = shift;
509         my $s = "want$n";
510         $self->{$s} = $val if defined $val;
511         return exists $self->{$s} ? $self->{$s} : 1;
512 }
513
514 sub wantbeep
515 {
516         return _want('beep', @_);
517 }
518
519 sub wantann
520 {
521         return _want('ann', @_);
522 }
523
524 sub wantwwv
525 {
526         return _want('wwv', @_);
527 }
528
529 sub wantwcy
530 {
531         return _want('wcy', @_);
532 }
533
534 sub wantecho
535 {
536         return _want('echo', @_);
537 }
538
539 sub wantwx
540 {
541         return _want('wx', @_);
542 }
543
544 sub wantdx
545 {
546         return _want('dx', @_);
547 }
548
549 sub wanttalk
550 {
551         return _want('talk', @_);
552 }
553
554 sub wantgrid
555 {
556         return _want('grid', @_);
557 }
558
559 sub wantemail
560 {
561         return _want('email', @_);
562 }
563
564 sub wantann_talk
565 {
566         return _want('ann_talk', @_);
567 }
568
569 sub wantlogininfo
570 {
571         my $self = shift;
572         my $val = shift;
573         $self->{wantlogininfo} = $val if defined $val;
574         return $self->{wantlogininfo};
575 }
576
577 sub is_node
578 {
579         my $self = shift;
580         return $self->{sort} =~ /[ACRSX]/;
581 }
582
583 sub is_user
584 {
585         my $self = shift;
586         return $self->{sort} eq 'U';
587 }
588
589 sub is_bbs
590 {
591         my $self = shift;
592         return $self->{sort} eq 'B';
593 }
594
595 sub is_spider
596 {
597         my $self = shift;
598         return $self->{sort} eq 'S';
599 }
600
601 sub is_clx
602 {
603         my $self = shift;
604         return $self->{sort} eq 'C';
605 }
606
607 sub is_dxnet
608 {
609         my $self = shift;
610         return $self->{sort} eq 'X';
611 }
612
613 sub is_arcluster
614 {
615         my $self = shift;
616         return $self->{sort} eq 'R';
617 }
618
619 sub is_ak1a
620 {
621         my $self = shift;
622         return $self->{sort} eq 'A';
623 }
624
625 sub unset_passwd
626 {
627         my $self = shift;
628         delete $self->{passwd};
629 }
630
631 sub unset_passphrase
632 {
633         my $self = shift;
634         delete $self->{passphrase};
635 }
636 1;
637 __END__
638
639
640
641
642