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 my $ref = $u{$call} if exists $u{$call};
276 return $ref if $ref && ref $ref eq 'DXUser';
282 # get an existing either from the channel (if there is one) or from the database
284 # It is important to note that if you have done a get (for the channel say) and you
285 # want access or modify that you must use this call (and you must NOT use get's all
286 # over the place willy nilly!)
293 my $dxchan = DXChannel::get($call);
295 my $ref = $dxchan->user;
296 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
298 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
304 # get all callsigns in the database
309 return (sort keys %u);
319 confess "Trying to put nothing!" unless $self && ref $self;
320 my $call = $self->{call};
321 $self->{lastin} = $main::systime;
327 goto &json_encode if $v4;
328 goto &asc_encode unless $v3;
330 return nfreeze($self);
336 goto &json_decode if $v4;
337 goto &storable_decode if $v3;
341 # should now be obsolete for mojo branch build 238 and above
351 # create a hash from a string (in ascii)
357 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
360 LogDbg('err', "DXUser::asc_decode: on '$s' $@");
370 eval { $ref = $json->decode($s) };
372 return bless $ref, 'DXUser';
374 LogDbg('err', "DXUser::json_decode: on '$s' $@");
383 my $s = $json->encode($ref);
384 bless $ref, 'DXUser';
389 # del - delete a user
395 my $call = $self->{call};
396 # $lru->remove($call);
402 # close - close down a user
410 $self->{lastin} = $main::systime;
411 # add a record to the connect list
412 my $ref = [$startt || $self->{startt}, $main::systime];
413 push @$ref, $ip if $ip;
414 push @{$self->{connlist}}, $ref;
415 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
429 # return a list of valid elements
439 # export the database to an ascii file
446 my $fn = $name || localdata("user_json"); # force use of local_data
449 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
450 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
451 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
452 move "$fn.o", "$fn.oo" if -e "$fn.o";
453 move "$fn", "$fn.o" if -e "$fn";
455 my $json = JSON->new;
461 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
465 foreach my $k (sort keys %u) {
467 if ($r->{sort} eq 'U' && !$r->{priv} && $main::systime > $r->{lastin}+$tooold ) {
468 unless ($r->{lat} || $r->{long} || $r->{qra} || $r->{qth} || $r->{name}) {
469 LogDbg('err', "DXUser::export deleting $k - too old, last in " . cldatetime($r->lastin) . " " . difft([$r->lastin, $main::systime]));
475 eval {$val = json_encode($r);};
477 LogDbg('err', "DXUser::export error encoding call: $k $@");
481 $fh->print("$k\t$val\n");
486 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors ('sh/log Export' for details)};
487 LogDbg('command', $s);
495 # add one or more groups
499 my $ref = $self->{group} || [ 'local' ];
500 $self->{group} = $ref if !$self->{group};
501 push @$ref, @_ if @_;
504 # remove one or more groups
508 my $ref = $self->{group} || [ 'local' ];
511 $self->{group} = $ref if !$self->{group};
513 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
516 # does this thing contain all the groups listed?
520 my $ref = $self->{group};
523 return 0 if !$ref || @_ == 0;
524 return 1 if @$ref == 0 && @_ == 0;
525 for ($n = 0; $n < @_; ) {
528 $n++ if grep $_ eq $a, @_;
534 # simplified group test just for one group
539 my $ref = $self->{group};
542 return grep $_ eq $s, $ref;
545 # set up a default group (only happens for them's that connect direct)
549 $self->{group} = [ 'local' ];
552 # set up empty buddies (only happens for them's that connect direct)
556 $self->{buddies} = [ ];
560 # return a prompt for a field
565 my ($self, $ele) = @_;
569 # some variable accessors
573 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
578 # want is default = 1
585 $self->{$s} = $val if defined $val;
586 return exists $self->{$s} ? $self->{$s} : 1;
589 # wantnot is default = 0
596 $self->{$s} = $val if defined $val;
597 return exists $self->{$s} ? $self->{$s} : 0;
602 return _want('beep', @_);
607 return _want('ann', @_);
612 return _want('wwv', @_);
617 return _want('wcy', @_);
622 return _want('echo', @_);
627 return _want('wx', @_);
632 return _want('dx', @_);
637 return _want('talk', @_);
642 return _want('grid', @_);
647 return _want('email', @_);
652 return _want('ann_talk', @_);
657 return _want('pc16', @_);
662 return _want('sendpc16', @_);
667 return _want('routepc16', @_);
672 return _want('usstate', @_);
677 return _want('dxcq', @_);
682 return _want('dxitu', @_);
687 return _want('gtk', @_);
692 return _want('pc9x', @_);
699 $self->{wantlogininfo} = $val if defined $val;
700 return $self->{wantlogininfo};
706 return $self->{sort} =~ /^[ACRSX]$/;
712 return grep $_ eq 'local_node', @{$self->{group}};
718 return $self->{sort} =~ /^[UW]$/;
724 return $self->{sort} eq 'W';
730 return $self->{sort} eq 'B';
736 return $self->{sort} eq 'S';
742 return $self->{sort} eq 'C';
748 return $self->{sort} eq 'X';
754 return $self->{sort} eq 'R';
760 return $self->{sort} eq 'A';
766 delete $self->{passwd};
772 delete $self->{passphrase};
779 $self->{believe} ||= [];
780 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
787 if (exists $self->{believe}) {
788 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
789 delete $self->{believe} unless @{$self->{believe}};
796 return exists $self->{believe} ? @{$self->{believe}} : ();
803 $self->{lastping} ||= {};
804 $self->{lastping} = {} unless ref $self->{lastping};
805 my $b = $self->{lastping};
806 $b->{$call} = shift if @_;
812 my $fn = shift || $filename;
814 my $ta = [gettimeofday];
820 dbg("DXUser $fn not found - probably about to convert");
824 open DATA, "$fn" or die "$fn read error $!";
829 eval { $ref = json_decode($f[1]); };
834 LogDbg('DXCommand', "# readinjson Error: '$f[0]\t$f[1]' $@");
840 dbg("DXUser::readinjson $count records $s mS");
845 my $fn = shift || $filename;
849 open DATA, ">$fn" or die "$fn write error $!";
850 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
855 foreach my $k (keys %u) { # this is to make it as quick as possible (no sort)
857 $val = json_encode($r);
858 $fh->print("$k\t$val\n");