12. added an 'auto rcmd <node> for/oper <call>' for people I can see on the
[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 require Exporter;
12 @ISA = qw(Exporter);
13
14 use DXLog;
15 use DB_File;
16 use Data::Dumper;
17 use Fcntl;
18 use IO::File;
19 use DXDebug;
20
21 use strict;
22 use vars qw(%u $dbm $filename %valid $lastoperinterval);
23
24 %u = ();
25 $dbm = undef;
26 $filename = undef;
27 $lastoperinterval = 30*24*60*60;
28
29 # hash of valid elements and a simple prompt
30 %valid = (
31                   call => '0,Callsign',
32                   alias => '0,Real Callsign',
33                   name => '0,Name',
34                   qth => '0,Home QTH',
35                   lat => '0,Latitude,slat',
36                   long => '0,Longitude,slong',
37                   qra => '0,Locator',
38                   email => '0,E-mail Address',
39                   priv => '9,Privilege Level',
40                   lastin => '0,Last Time in,cldatetime',
41                   passwd => '9,Password',
42                   addr => '0,Full Address',
43                   'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
44                   xpert => '0,Expert Status,yesno',
45                   bbs => '0,Home BBS',
46                   node => '0,Last Node',
47                   homenode => '0,Home Node',
48                   lockout => '9,Locked out?,yesno',     # won't let them in at all
49                   dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
50                   annok => '9,Accept Announces?,yesno', # accept his announces?
51                   reg => '0,Registered?,yesno', # is this user registered?
52                   lang => '0,Language',
53                   hmsgno => '0,Highest Msgno',
54                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
55                   isolate => '9,Isolate network,yesno',
56                   wantbeep => '0,Rec Beep,yesno',
57                   wantann => '0,Rec Announce,yesno',
58                   wantwwv => '0,Rec WWV,yesno',
59                   wantwcy => '0,Rec WCY,yesno',
60                   wantecho => '0,Rec Echo,yesno',
61                   wanttalk => '0,Rec Talk,yesno',
62                   wantwx => '0,Rec WX,yesno',
63                   wantdx => '0,Rec DX Spots,yesno',
64                   pagelth => '0,Current Pagelth',
65                   pingint => '9,Node Ping interval',
66                   nopings => '9,Ping Obs Count',
67                   wantlogininfo => '9,Login info req,yesno',
68                   wantgrid => '0,DX Grid Info,yesno',
69                   lastoper => '9,Last for/oper,cldatetime',
70                  );
71
72 no strict;
73 sub AUTOLOAD
74 {
75         my $self = shift;
76         my $name = $AUTOLOAD;
77   
78         return if $name =~ /::DESTROY$/;
79         $name =~ s/.*:://o;
80   
81         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
82         if (@_) {
83                 $self->{$name} = shift;
84         }
85         return $self->{$name};
86 }
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 use strict;
107
108 #
109 # close the system
110 #
111
112 sub finish
113 {
114         undef $dbm;
115         untie %u;
116 }
117
118 #
119 # new - create a new user
120 #
121
122 sub new
123 {
124         my $pkg = shift;
125         my $call = uc shift;
126         #  $call =~ s/-\d+$//o;
127   
128 #       confess "can't create existing call $call in User\n!" if $u{$call};
129
130         my $self = bless {}, $pkg;
131         $self->{call} = $call;
132         $self->{'sort'} = 'U';
133         $self->put;
134         return $self;
135 }
136
137 #
138 # get - get an existing user - this seems to return a different reference everytime it is
139 #       called - see below
140 #
141
142 sub get
143 {
144         my $pkg = shift;
145         my $call = uc shift;
146         #  $call =~ s/-\d+$//o;       # strip ssid
147         my $s = $u{$call};
148         return $s ?  decode($s) : undef;
149 }
150
151 #
152 # get all callsigns in the database 
153 #
154
155 sub get_all_calls
156 {
157         return (sort keys %u);
158 }
159
160 #
161 # get an existing either from the channel (if there is one) or from the database
162 #
163 # It is important to note that if you have done a get (for the channel say) and you
164 # want access or modify that you must use this call (and you must NOT use get's all
165 # over the place willy nilly!)
166 #
167
168 sub get_current
169 {
170         my $pkg = shift;
171         my $call = uc shift;
172         #  $call =~ s/-\d+$//o;       # strip ssid
173   
174         my $dxchan = DXChannel->get($call);
175         return $dxchan->user if $dxchan;
176         return get($pkg, $call);
177 }
178
179 #
180 # put - put a user
181 #
182
183 sub put
184 {
185         my $self = shift;
186         confess "Trying to put nothing!" unless $self && ref $self;
187         my $call = $self->{call};
188         # delete all instances of this 
189         for ($dbm->get_dup($call)) {
190                 $dbm->del_dup($call, $_);
191         }
192         delete $self->{annok} if $self->{annok};
193         delete $self->{dxok} if $self->{dxok};
194         $u{$call} = $self->encode();
195         $dbm->sync;
196 }
197
198
199 # create a string from a user reference
200 #
201 sub encode
202 {
203         my $self = shift;
204         my $dd = new Data::Dumper([$self]);
205         $dd->Indent(0);
206         $dd->Terse(1);
207     $dd->Quotekeys($] < 5.005 ? 1 : 0);
208         return $dd->Dumpxs;
209 }
210
211 #
212 # create a hash from a string
213 #
214 sub decode
215 {
216         my $s = shift;
217         my $ref;
218         $s = '$ref = ' . $s;
219         eval $s;
220         Log('DXUser', $@) if $@;
221         $ref = undef if $@;
222         return $ref;
223 }
224
225 #
226 # del - delete a user
227 #
228
229 sub del
230 {
231         my $self = shift;
232         my $call = $self->{call};
233         # delete all instances of this 
234         for ($dbm->get_dup($call)) {
235                 $dbm->del_dup($call, $_);
236         }
237         $dbm->sync;
238 }
239
240 #
241 # close - close down a user
242 #
243
244 sub close
245 {
246         my $self = shift;
247         $self->{lastin} = time;
248         $self->put();
249 }
250
251 #
252 # return a list of valid elements 
253
254
255 sub fields
256 {
257         return keys(%valid);
258 }
259
260
261 #
262 # export the database to an ascii file
263 #
264
265 sub export
266 {
267         my $fn = shift;
268         
269         # save old ones
270         rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
271         rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
272         rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
273         rename "$fn.o", "$fn.oo" if -e "$fn.o";
274         rename "$fn", "$fn.o" if -e "$fn";
275
276         my $count = 0;
277         my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
278         if ($fh) {
279                 my $ref;
280                 my $key;
281                 my $action;
282                 my $t = scalar localtime;
283                 print $fh "#!/usr/bin/perl
284 #
285 # The exported userfile for a DXSpider System
286 #
287 # Input file: $filename
288 #       Time: $t
289 #
290
291 package DXUser;
292
293 %u = (
294 ";
295
296                 for ($action = R_FIRST; !$dbm->seq($key, $ref, $action); $action = R_NEXT) {
297                         print $fh "'$key' => $ref,\n";
298                         ++$count;
299                 } 
300                 print $fh ");\n#\n# there were $count records\n#\n";
301                 $fh->close;
302         } 
303         return $count;
304 }
305
306 #
307 # group handling
308 #
309
310 # add one or more groups
311 sub add_group
312 {
313         my $self = shift;
314         my $ref = $self->{group} || [ 'local' ];
315         $self->{group} = $ref if !$self->{group};
316         push @$ref, @_ if @_;
317 }
318
319 # remove one or more groups
320 sub del_group
321 {
322         my $self = shift;
323         my $ref = $self->{group} || [ 'local' ];
324         my @in = @_;
325         
326         $self->{group} = $ref if !$self->{group};
327         
328         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
329 }
330
331 # does this thing contain all the groups listed?
332 sub union
333 {
334         my $self = shift;
335         my $ref = $self->{group};
336         my $n;
337         
338         return 0 if !$ref || @_ == 0;
339         return 1 if @$ref == 0 && @_ == 0;
340         for ($n = 0; $n < @_; ) {
341                 for (@$ref) {
342                         my $a = $_;
343                         $n++ if grep $_ eq $a, @_; 
344                 }
345         }
346         return $n >= @_;
347 }
348
349 # simplified group test just for one group
350 sub in_group
351 {
352         my $self = shift;
353         my $s = shift;
354         my $ref = $self->{group};
355         
356         return 0 if !$ref;
357         return grep $_ eq $s, $ref;
358 }
359
360 # set up a default group (only happens for them's that connect direct)
361 sub new_group
362 {
363         my $self = shift;
364         $self->{group} = [ 'local' ];
365 }
366
367 #
368 # return a prompt for a field
369 #
370
371 sub field_prompt
372
373         my ($self, $ele) = @_;
374         return $valid{$ele};
375 }
376
377 # some variable accessors
378 sub sort
379 {
380         my $self = shift;
381         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
382 }
383
384 # some accessors
385 sub _want
386 {
387         my $n = shift;
388         my $self = shift;
389         my $val = shift;
390         my $s = "want$n";
391         $self->{$s} = $val if defined $val;
392         return exists $self->{$s} ? $self->{$s} : 1;
393 }
394
395 sub wantbeep
396 {
397         return _want('beep', @_);
398 }
399
400 sub wantann
401 {
402         return _want('ann', @_);
403 }
404
405 sub wantwwv
406 {
407         return _want('wwv', @_);
408 }
409
410 sub wantwcy
411 {
412         return _want('wcy', @_);
413 }
414
415 sub wantecho
416 {
417         return _want('echo', @_);
418 }
419
420 sub wantwx
421 {
422         return _want('wx', @_);
423 }
424
425 sub wantdx
426 {
427         return _want('dx', @_);
428 }
429
430 sub wanttalk
431 {
432         return _want('talk', @_);
433 }
434
435 sub wantgrid
436 {
437         return _want('grid', @_);
438 }
439
440 sub wantlogininfo
441 {
442         my $self = shift;
443         my $n = shift;
444         $self->{wantlogininfo} = $n if $n;
445         return exists $self->{wantlogininfo} ? $self->{wantlogininfo} : 0;
446 }
447
448 sub is_node
449 {
450         my $self = shift;
451         return $self->{sort} =~ /[ACRSX]/;
452 }
453
454 sub is_user
455 {
456         my $self = shift;
457         return $self->{sort} eq 'U';
458 }
459
460 sub is_bbs
461 {
462         my $self = shift;
463         return $self->{sort} eq 'B';
464 }
465
466 sub is_spider
467 {
468         my $self = shift;
469         return $self->{sort} eq 'S';
470 }
471
472 sub is_clx
473 {
474         my $self = shift;
475         return $self->{sort} eq 'C';
476 }
477
478 sub is_dxnet
479 {
480         my $self = shift;
481         return $self->{sort} eq 'X';
482 }
483
484 sub is_arcluster
485 {
486         my $self = shift;
487         return $self->{sort} eq 'R';
488 }
489
490 sub is_ak1a
491 {
492         my $self = shift;
493         return $self->{sort} eq 'A';
494 }
495 1;
496 __END__
497
498
499
500
501