1. added export_user.pl to export user files (for interest and safety)
[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 MLDBM qw(DB_File);
15 use Fcntl;
16 use Carp;
17
18 use strict;
19 use vars qw(%u $dbm $filename %valid);
20
21 %u = ();
22 $dbm = undef;
23 $filename = undef;
24
25 # hash of valid elements and a simple prompt
26 %valid = (
27                   call => '0,Callsign',
28                   alias => '0,Real Callsign',
29                   name => '0,Name',
30                   qth => '0,Home QTH',
31                   lat => '0,Latitude,slat',
32                   long => '0,Longitude,slong',
33                   qra => '0,Locator',
34                   email => '0,E-mail Address',
35                   priv => '9,Privilege Level',
36                   lastin => '0,Last Time in,cldatetime',
37                   passwd => '9,Password',
38                   addr => '0,Full Address',
39                   'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
40                   xpert => '0,Expert Status,yesno',
41                   bbs => '0,Home BBS',
42                   node => '0,Last Node',
43                   homenode => '0,Home Node',
44                   lockout => '9,Locked out?,yesno',     # won't let them in at all
45                   dxok => '9,DX Spots?,yesno', # accept his dx spots?
46                   annok => '9,Announces?,yesno', # accept his announces?
47                   reg => '0,Registered?,yesno', # is this user registered?
48                   lang => '0,Language',
49                   hmsgno => '0,Highest Msgno',
50                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
51                   isolate => '9,Isolate network,yesno',
52                  );
53
54 no strict;
55 sub AUTOLOAD
56 {
57         my $self = shift;
58         my $name = $AUTOLOAD;
59   
60         return if $name =~ /::DESTROY$/;
61         $name =~ s/.*:://o;
62   
63         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
64         if (@_) {
65                 $self->{$name} = shift;
66                 #       $self->put();
67         }
68         return $self->{$name};
69 }
70
71 #
72 # initialise the system
73 #
74 sub init
75 {
76         my ($pkg, $fn, $mode) = @_;
77   
78         confess "need a filename in User" if !$fn;
79         if ($mode) {
80                 $dbm = tie (%u, MLDBM, $fn, O_CREAT|O_RDWR, 0666) or confess "can't open user file: $fn ($!)";
81         } else {
82                 $dbm = tie (%u, MLDBM, $fn, O_RDONLY) or confess "can't open user file: $fn ($!)";
83         }
84         
85         $filename = $fn;
86 }
87
88 use strict;
89
90 #
91 # close the system
92 #
93
94 sub finish
95 {
96         $dbm = undef;
97         untie %u;
98 }
99
100 #
101 # new - create a new user
102 #
103
104 sub new
105 {
106         my $pkg = shift;
107         my $call = uc shift;
108         #  $call =~ s/-\d+$//o;
109   
110         confess "can't create existing call $call in User\n!" if $u{$call};
111
112         my $self = bless {}, $pkg;
113         $self->{call} = $call;
114         $self->{'sort'} = 'U';
115         $self->{dxok} = 1;
116         $self->{annok} = 1;
117         $self->{lang} = $main::lang;
118         $u{call} = $self;
119         return $self;
120 }
121
122 #
123 # get - get an existing user - this seems to return a different reference everytime it is
124 #       called - see below
125 #
126
127 sub get
128 {
129         my $pkg = shift;
130         my $call = uc shift;
131         #  $call =~ s/-\d+$//o;       # strip ssid
132         return $u{$call};
133 }
134
135 #
136 # get all callsigns in the database 
137 #
138
139 sub get_all_calls
140 {
141         return (sort keys %u);
142 }
143
144 #
145 # get an existing either from the channel (if there is one) or from the database
146 #
147 # It is important to note that if you have done a get (for the channel say) and you
148 # want access or modify that you must use this call (and you must NOT use get's all
149 # over the place willy nilly!)
150 #
151
152 sub get_current
153 {
154         my $pkg = shift;
155         my $call = uc shift;
156         #  $call =~ s/-\d+$//o;       # strip ssid
157   
158         my $dxchan = DXChannel->get($call);
159         return $dxchan->user if $dxchan;
160         return $u{$call};
161 }
162
163 #
164 # put - put a user
165 #
166
167 sub put
168 {
169         my $self = shift;
170         my $call = $self->{call};
171         $u{$call} = $self;
172 }
173
174 #
175 # del - delete a user
176 #
177
178 sub del
179 {
180         my $self = shift;
181         my $call = $self->{call};
182         delete $u{$call};
183 }
184
185 #
186 # close - close down a user
187 #
188
189 sub close
190 {
191         my $self = shift;
192         $self->{lastin} = time;
193         $self->put();
194 }
195
196 #
197 # return a list of valid elements 
198
199
200 sub fields
201 {
202         return keys(%valid);
203 }
204
205 #
206 # group handling
207 #
208
209 # add one or more groups
210 sub add_group
211 {
212         my $self = shift;
213         my $ref = $self->{group} || [ 'local' ];
214         $self->{group} = $ref if !$self->{group};
215         push @$ref, @_ if @_;
216 }
217
218 # remove one or more groups
219 sub del_group
220 {
221         my $self = shift;
222         my $ref = $self->{group} || [ 'local' ];
223         my @in = @_;
224         
225         $self->{group} = $ref if !$self->{group};
226         
227         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
228 }
229
230 # does this thing contain all the groups listed?
231 sub union
232 {
233         my $self = shift;
234         my $ref = $self->{group};
235         my $n;
236         
237         return 0 if !$ref || @_ == 0;
238         return 1 if @$ref == 0 && @_ == 0;
239         for ($n = 0; $n < @_; ) {
240                 for (@$ref) {
241                         my $a = $_;
242                         $n++ if grep $_ eq $a, @_; 
243                 }
244         }
245         return $n >= @_;
246 }
247
248 # simplified group test just for one group
249 sub in_group
250 {
251         my $self = shift;
252         my $s = shift;
253         my $ref = $self->{group};
254         
255         return 0 if !$ref;
256         return grep $_ eq $s, $ref;
257 }
258
259 # set up a default group (only happens for them's that connect direct)
260 sub new_group
261 {
262         my $self = shift;
263         $self->{group} = [ 'local' ];
264 }
265
266 #
267 # return a prompt for a field
268 #
269
270 sub field_prompt
271
272         my ($self, $ele) = @_;
273         return $valid{$ele};
274 }
275
276 # some variable accessors
277 sub sort
278 {
279         my $self = shift;
280         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
281 }
282 1;
283 __END__