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