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