make export_user much more robust
[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 Error: $key\t$val");
404                                 $dbm->del($key);
405                                 ++$err;
406                                 next;
407                         }
408                         my $ref = decode($val);
409                         if ($ref) {
410                                 print $fh "$key\t" . $ref->encode . "\n";
411                                 ++$count;
412                         } else {
413                                 Log('DXCommand', "Export Error: $key\t$val");
414                                 $dbm->del($key);
415                                 ++$err;
416                         }
417                 } 
418         $fh->close;
419     } 
420         return "$count Users $err Errors ('sh/log Export' for details)";
421 }
422
423 #
424 # group handling
425 #
426
427 # add one or more groups
428 sub add_group
429 {
430         my $self = shift;
431         my $ref = $self->{group} || [ 'local' ];
432         $self->{group} = $ref if !$self->{group};
433         push @$ref, @_ if @_;
434 }
435
436 # remove one or more groups
437 sub del_group
438 {
439         my $self = shift;
440         my $ref = $self->{group} || [ 'local' ];
441         my @in = @_;
442         
443         $self->{group} = $ref if !$self->{group};
444         
445         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
446 }
447
448 # does this thing contain all the groups listed?
449 sub union
450 {
451         my $self = shift;
452         my $ref = $self->{group};
453         my $n;
454         
455         return 0 if !$ref || @_ == 0;
456         return 1 if @$ref == 0 && @_ == 0;
457         for ($n = 0; $n < @_; ) {
458                 for (@$ref) {
459                         my $a = $_;
460                         $n++ if grep $_ eq $a, @_; 
461                 }
462         }
463         return $n >= @_;
464 }
465
466 # simplified group test just for one group
467 sub in_group
468 {
469         my $self = shift;
470         my $s = shift;
471         my $ref = $self->{group};
472         
473         return 0 if !$ref;
474         return grep $_ eq $s, $ref;
475 }
476
477 # set up a default group (only happens for them's that connect direct)
478 sub new_group
479 {
480         my $self = shift;
481         $self->{group} = [ 'local' ];
482 }
483
484 #
485 # return a prompt for a field
486 #
487
488 sub field_prompt
489
490         my ($self, $ele) = @_;
491         return $valid{$ele};
492 }
493
494 # some variable accessors
495 sub sort
496 {
497         my $self = shift;
498         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
499 }
500
501 # some accessors
502 sub _want
503 {
504         my $n = shift;
505         my $self = shift;
506         my $val = shift;
507         my $s = "want$n";
508         $self->{$s} = $val if defined $val;
509         return exists $self->{$s} ? $self->{$s} : 1;
510 }
511
512 sub wantbeep
513 {
514         return _want('beep', @_);
515 }
516
517 sub wantann
518 {
519         return _want('ann', @_);
520 }
521
522 sub wantwwv
523 {
524         return _want('wwv', @_);
525 }
526
527 sub wantwcy
528 {
529         return _want('wcy', @_);
530 }
531
532 sub wantecho
533 {
534         return _want('echo', @_);
535 }
536
537 sub wantwx
538 {
539         return _want('wx', @_);
540 }
541
542 sub wantdx
543 {
544         return _want('dx', @_);
545 }
546
547 sub wanttalk
548 {
549         return _want('talk', @_);
550 }
551
552 sub wantgrid
553 {
554         return _want('grid', @_);
555 }
556
557 sub wantemail
558 {
559         return _want('email', @_);
560 }
561
562 sub wantann_talk
563 {
564         return _want('ann_talk', @_);
565 }
566
567 sub wantlogininfo
568 {
569         my $self = shift;
570         my $val = shift;
571         $self->{wantlogininfo} = $val if defined $val;
572         return $self->{wantlogininfo};
573 }
574
575 sub is_node
576 {
577         my $self = shift;
578         return $self->{sort} =~ /[ACRSX]/;
579 }
580
581 sub is_user
582 {
583         my $self = shift;
584         return $self->{sort} eq 'U';
585 }
586
587 sub is_bbs
588 {
589         my $self = shift;
590         return $self->{sort} eq 'B';
591 }
592
593 sub is_spider
594 {
595         my $self = shift;
596         return $self->{sort} eq 'S';
597 }
598
599 sub is_clx
600 {
601         my $self = shift;
602         return $self->{sort} eq 'C';
603 }
604
605 sub is_dxnet
606 {
607         my $self = shift;
608         return $self->{sort} eq 'X';
609 }
610
611 sub is_arcluster
612 {
613         my $self = shift;
614         return $self->{sort} eq 'R';
615 }
616
617 sub is_ak1a
618 {
619         my $self = shift;
620         return $self->{sort} eq 'A';
621 }
622
623 sub unset_passwd
624 {
625         my $self = shift;
626         delete $self->{passwd};
627 }
628
629 sub unset_passphrase
630 {
631         my $self = shift;
632         delete $self->{passphrase};
633 }
634 1;
635 __END__
636
637
638
639
640