2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
21 use Data::Structure::Util qw(unbless);
22 use Time::HiRes qw(gettimeofday tv_interval);
26 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3 $v4);
31 $lastoperinterval = 60*24*60*60;
34 $tooold = 86400 * 365 + 31; # this marks an old user who hasn't given enough info to be useful
39 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
41 # hash of valid elements and a simple prompt
44 alias => '0,Real Callsign',
47 lat => '0,Latitude,slat',
48 long => '0,Longitude,slong',
50 email => '0,E-mail Address,parray',
51 priv => '9,Privilege Level',
52 lastin => '0,Last Time in,cldatetime',
53 passwd => '9,Password,yesno',
54 passphrase => '9,Pass Phrase,yesno',
55 addr => '0,Full Address',
56 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
57 xpert => '0,Expert Status,yesno',
59 node => '0,Last Node',
60 homenode => '0,Home Node',
61 lockout => '9,Locked out?,yesno', # won't let them in at all
62 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
63 annok => '9,Accept Announces?,yesno', # accept his announces?
65 hmsgno => '0,Highest Msgno',
66 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
67 buddies => '0,Buddies,parray',
68 isolate => '9,Isolate network,yesno',
69 wantbeep => '0,Req Beep,yesno',
70 wantann => '0,Req Announce,yesno',
71 wantwwv => '0,Req WWV,yesno',
72 wantwcy => '0,Req WCY,yesno',
73 wantecho => '0,Req Echo,yesno',
74 wanttalk => '0,Req Talk,yesno',
75 wantwx => '0,Req WX,yesno',
76 wantdx => '0,Req DX Spots,yesno',
77 wantemail => '0,Req Msgs as Email,yesno',
78 pagelth => '0,Current Pagelth',
79 pingint => '9,Node Ping interval',
80 nopings => '9,Ping Obs Count',
81 wantlogininfo => '0,Login Info Req,yesno',
82 wantgrid => '0,Show DX Grid,yesno',
83 wantann_talk => '0,Talklike Anns,yesno',
84 wantpc16 => '9,Want Users from node,yesno',
85 wantsendpc16 => '9,Send PC16,yesno',
86 wantroutepc19 => '9,Route PC19,yesno',
87 wantusstate => '0,Show US State,yesno',
88 wantdxcq => '0,Show CQ Zone,yesno',
89 wantdxitu => '0,Show ITU Zone,yesno',
90 wantgtk => '0,Want GTK interface,yesno',
91 wantpc9x => '0,Want PC9X interface,yesno',
92 lastoper => '9,Last for/oper,cldatetime',
93 nothere => '0,Not Here Text',
94 registered => '9,Registered?,yesno',
95 prompt => '0,Required Prompt',
96 version => '1,Version',
98 believe => '1,Believable nodes,parray',
99 lastping => '1,Last Ping at,ptimelist',
100 maxconnect => '1,Max Connections',
101 startt => '0,Start Time,cldatetime',
102 connlist => '1,Connections,parraydifft',
109 my $name = $AUTOLOAD;
111 return if $name =~ /::DESTROY$/;
114 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
115 # this clever line of code creates a subroutine which takes over from autoload
116 # from OO Perl - Conway
117 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
124 # initialise the system
136 $filename = $ufn = localdata("$fn.json");
138 if (-e localdata("$fn.json")) {
146 if ( ! -e localdata("users.v3") && -e localdata("users.v2") ) {
149 LogDbg('',"the module Storable appears to be missing!!");
150 LogDbg('',"trying to continue in compatibility mode (this may fail)");
151 LogDbg('',"please install Storable from CPAN as soon as possible");
154 import Storable qw(nfreeze thaw);
155 $convert = 3 if -e localdata("users.v3") && !-e $ufn;
159 # do a conversion if required
161 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
162 my $ta = [gettimeofday];
165 LogDbg('',"Converting the User File from V$convert to $fn.json ");
166 LogDbg('',"This will take a while, I suggest you go and have cup of strong tea");
167 my $odbm = tie (%oldu, 'DB_File', localdata("users.v$convert"), O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v$convert ($!) [rebuild it from user_asc?]";
168 for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
171 eval { $ref = storable_decode($val) };
173 eval { $ref = asc_decode($val) };
183 Log('err', "DXUser: error decoding $@");
188 my $t = _diffms($ta);
189 LogDbg('',"Conversion from users.v$convert to users.json completed $count records $err errors $t mS");
191 # now write it away for future use
192 $ta = [gettimeofday];
194 $count = writeoutjson();
196 LogDbg('',"New Userfile users.json write completed $count records $err errors $t mS");
197 LogDbg('',"Now restarting..");
200 # otherwise (i.e normally) slurp it in
208 # with extreme prejudice
210 unlink "$main::data/users.v3";
211 unlink "$main::local_data/users.v3";
214 unlink "$main::data/users.v4";
215 unlink "$main::local_data/users.v4";
220 # periodic processing
224 # if ($main::systime > $lasttime + 15) {
225 # #$dbm->sync if $dbm;
226 # $lasttime = $main::systime;
241 # new - create a new user
248 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
256 # $call =~ s/-\d+$//o;
258 # confess "can't create existing call $call in User\n!" if $u{$call};
260 my $self = $pkg->alloc($call);
266 # get - get an existing user - this seems to return a different reference everytime it is
275 # is it in the LRU cache?
276 my $ref = $u{$call} if exists $u{$call};
277 # my $ref = $lru->get($call);
278 return $ref if $ref && ref $ref eq 'DXUser';
281 # unless ($dbm->get($call, $data)) {
282 # eval { $ref = decode($data); };
285 # if (!UNIVERSAL::isa($ref, 'DXUser')) {
286 # dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
289 # # we have a reference and it *is* a DXUser
292 # LogDbg('err', "DXUser::get decode error on $call '$@'");
294 # dbg("DXUser::get: no reference returned from decode of $call $!");
298 # $lru->put($call, $ref);
305 # get an existing either from the channel (if there is one) or from the database
307 # It is important to note that if you have done a get (for the channel say) and you
308 # want access or modify that you must use this call (and you must NOT use get's all
309 # over the place willy nilly!)
316 my $dxchan = DXChannel::get($call);
318 my $ref = $dxchan->user;
319 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
321 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
327 # get all callsigns in the database
332 return (sort keys %u);
342 confess "Trying to put nothing!" unless $self && ref $self;
343 my $call = $self->{call};
344 $self->{lastin} = $main::systime;
350 goto &json_encode if $v4;
351 goto &asc_encode unless $v3;
353 return nfreeze($self);
359 goto &json_decode if $v4;
360 goto &storable_decode if $v3;
364 # should now be obsolete for mojo branch build 238 and above
374 # create a hash from a string (in ascii)
380 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
383 LogDbg('err', "DXUser::asc_decode: on '$s' $@");
393 eval { $ref = $json->decode($s) };
395 return bless $ref, 'DXUser';
397 LogDbg('err', "DXUser::json_decode: on '$s' $@");
406 my $s = $json->encode($ref);
407 bless $ref, 'DXUser';
412 # del - delete a user
418 my $call = $self->{call};
419 # $lru->remove($call);
425 # close - close down a user
433 $self->{lastin} = $main::systime;
434 # add a record to the connect list
435 my $ref = [$startt || $self->{startt}, $main::systime];
436 push @$ref, $ip if $ip;
437 push @{$self->{connlist}}, $ref;
438 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
452 # return a list of valid elements
462 # export the database to an ascii file
469 my $fn = $name || "$main::local_data/user_json"; # force use of local_data
472 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
473 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
474 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
475 move "$fn.o", "$fn.oo" if -e "$fn.o";
476 move "$fn", "$fn.o" if -e "$fn";
478 my $json = JSON->new;
484 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
488 foreach my $k (sort keys %u) {
490 if ($r->{sort} eq 'U' && !$r->{priv} && $main::systime > $r->{lastin}+$tooold ) {
491 unless ($r->{lat} || $r->{long} || $r->{qra} || $r->{qth} || $r->{name}) {
492 LogDbg('err', "DXUser::export deleting $k - too old, last in " . cldatetime($r->lastin) . " " . difft([$r->lastin, $main::systime]));
498 eval {$val = json_encode($r);};
500 LogDbg('err', "DXUser::export error encoding call: $k $@");
504 $fh->print("$k\t$val\n");
509 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors ('sh/log Export' for details)};
510 LogDbg('command', $s);
518 # add one or more groups
522 my $ref = $self->{group} || [ 'local' ];
523 $self->{group} = $ref if !$self->{group};
524 push @$ref, @_ if @_;
527 # remove one or more groups
531 my $ref = $self->{group} || [ 'local' ];
534 $self->{group} = $ref if !$self->{group};
536 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
539 # does this thing contain all the groups listed?
543 my $ref = $self->{group};
546 return 0 if !$ref || @_ == 0;
547 return 1 if @$ref == 0 && @_ == 0;
548 for ($n = 0; $n < @_; ) {
551 $n++ if grep $_ eq $a, @_;
557 # simplified group test just for one group
562 my $ref = $self->{group};
565 return grep $_ eq $s, $ref;
568 # set up a default group (only happens for them's that connect direct)
572 $self->{group} = [ 'local' ];
575 # set up empty buddies (only happens for them's that connect direct)
579 $self->{buddies} = [ ];
583 # return a prompt for a field
588 my ($self, $ele) = @_;
592 # some variable accessors
596 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
601 # want is default = 1
608 $self->{$s} = $val if defined $val;
609 return exists $self->{$s} ? $self->{$s} : 1;
612 # wantnot is default = 0
619 $self->{$s} = $val if defined $val;
620 return exists $self->{$s} ? $self->{$s} : 0;
625 return _want('beep', @_);
630 return _want('ann', @_);
635 return _want('wwv', @_);
640 return _want('wcy', @_);
645 return _want('echo', @_);
650 return _want('wx', @_);
655 return _want('dx', @_);
660 return _want('talk', @_);
665 return _want('grid', @_);
670 return _want('email', @_);
675 return _want('ann_talk', @_);
680 return _want('pc16', @_);
685 return _want('sendpc16', @_);
690 return _want('routepc16', @_);
695 return _want('usstate', @_);
700 return _want('dxcq', @_);
705 return _want('dxitu', @_);
710 return _want('gtk', @_);
715 return _want('pc9x', @_);
722 $self->{wantlogininfo} = $val if defined $val;
723 return $self->{wantlogininfo};
729 return $self->{sort} =~ /^[ACRSX]$/;
735 return grep $_ eq 'local_node', @{$self->{group}};
741 return $self->{sort} =~ /^[UW]$/;
747 return $self->{sort} eq 'W';
753 return $self->{sort} eq 'B';
759 return $self->{sort} eq 'S';
765 return $self->{sort} eq 'C';
771 return $self->{sort} eq 'X';
777 return $self->{sort} eq 'R';
783 return $self->{sort} eq 'A';
789 delete $self->{passwd};
795 delete $self->{passphrase};
802 $self->{believe} ||= [];
803 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
810 if (exists $self->{believe}) {
811 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
812 delete $self->{believe} unless @{$self->{believe}};
819 return exists $self->{believe} ? @{$self->{believe}} : ();
826 $self->{lastping} ||= {};
827 $self->{lastping} = {} unless ref $self->{lastping};
828 my $b = $self->{lastping};
829 $b->{$call} = shift if @_;
835 my $fn = shift || $filename;
837 my $ta = [gettimeofday];
843 dbg("DXUser $fn not found - probably about to convert");
847 open DATA, "$fn" or die "$fn read error $!";
852 eval { $ref = json_decode($f[1]); };
857 LogDbg('DXCommand', "# readinjson Error: '$f[0]\t$f[1]' $@");
863 dbg("DXUser::readinjson $count records $s mS");
868 my $fn = shift || $filename;
872 open DATA, ">$fn" or die "$fn write error $!";
873 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
878 foreach my $k (keys %u) { # this is to make it as quick as possible (no sort)
880 $val = json_encode($r);
881 $fh->print("$k\t$val\n");