2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
20 use Data::Structure::Util qw(unbless);
21 use Time::HiRes qw(gettimeofday tv_interval);
26 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3);
31 $lastoperinterval = 60*24*60*60;
34 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
36 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
40 # hash of valid elements and a simple prompt
43 alias => '0,Real Callsign',
46 lat => '0,Latitude,slat',
47 long => '0,Longitude,slong',
49 email => '0,E-mail Address,parray',
50 priv => '9,Privilege Level',
51 lastin => '0,Last Time in,cldatetime',
52 lastseen => '0,Last Seen,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 wantrbn => '0,Want RBN spots,yesno',
93 wantft => '0,Want RBN FT4/8,yesno',
94 wantcw => '0,Want RBN CW,yesno',
95 wantrtty => '0,Want RBN RTTY,yesno',
96 wantpsk => '0,Want RBN PSK,yesno',
97 wantbeacon => '0,Want (RBN) Beacon,yesno',
98 lastoper => '9,Last for/oper,cldatetime',
99 nothere => '0,Not Here Text',
100 registered => '9,Registered?,yesno',
101 prompt => '0,Required Prompt',
102 version => '1,Version',
104 believe => '1,Believable nodes,parray',
105 lastping => '1,Last Ping at,ptimelist',
106 maxconnect => '1,Max Connections',
107 startt => '0,Start Time,cldatetime',
108 connlist => '1,Connections,parraydifft',
115 my $name = $AUTOLOAD;
117 return if $name =~ /::DESTROY$/;
120 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
121 # this clever line of code creates a subroutine which takes over from autoload
122 # from OO Perl - Conway
123 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
130 # initialise the system
136 $json = JSON->new->canonical(1);
138 $filename = localdata("$fn.v3j");
139 unless (-e $filename || $mode == 2) {
140 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
141 system('/spider/perl/convert-users-v3-to-v3j.pl');
146 if (-e $filename || $mode == 2) {
147 $lru = LRU->newbase("DXUser", $lrusize);
149 $dbm = tie (%u, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
151 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
154 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
158 # delete files with extreme prejudice
161 # with extreme prejudice
162 unlink "$main::data/users.v3j";
163 unlink "$main::local_data/users.v3j";
167 # periodic processing
171 if ($main::systime > $lasttime + 15) {
173 $lasttime = $main::systime;
189 # new - create a new user
196 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
204 # $call =~ s/-\d+$//o;
206 # confess "can't create existing call $call in User\n!" if $u{$call};
208 my $self = $pkg->alloc($call);
209 $self->{lastseen} = $main::systime;
215 # get - get an existing user - this seems to return a different reference everytime it is
224 # is it in the LRU cache?
225 my $ref = $lru->get($call);
226 if ($ref && ref $ref eq 'DXUser') {
227 $ref->{lastseen} = $main::systime;
232 unless ($dbm->get($call, $data)) {
233 eval { $ref = decode($data); };
236 if (!UNIVERSAL::isa($ref, 'DXUser')) {
237 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
240 # we have a reference and it *is* a DXUser
243 LogDbg('err', "DXUser::get decode error on $call '$@'");
245 dbg("DXUser::get: no reference returned from decode of $call $!");
249 $ref->{lastseen} = $main::systime;
250 $lru->put($call, $ref);
257 # get an existing either from the channel (if there is one) or from the database
259 # It is important to note that if you have done a get (for the channel say) and you
260 # want access or modify that you must use this call (and you must NOT use get's all
261 # over the place willy nilly!)
268 my $dxchan = DXChannel::get($call);
270 my $ref = $dxchan->user;
271 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
273 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
279 # get all callsigns in the database
284 return (sort keys %u);
294 confess "Trying to put nothing!" unless $self && ref $self;
295 my $call = $self->{call};
298 delete $self->{annok} if $self->{annok};
299 delete $self->{dxok} if $self->{dxok};
301 $lru->put($call, $self);
302 my $ref = $self->encode;
303 $dbm->put($call, $ref);
312 eval { $ref = $json->decode($s) };
314 return bless $ref, 'DXUser';
316 LogDbg('DXUser', "DXUser::json_decode: on '$s' $@");
326 my $s = $json->encode($ref);
327 bless $ref, 'DXUser';
333 # del - delete a user
339 my $call = $self->{call};
345 # close - close down a user
353 $self->{lastseen} = $self->{lastin} = $main::systime;
354 # add a record to the connect list
355 my $ref = [$startt || $self->{startt}, $main::systime];
356 push @$ref, $ip if $ip;
357 push @{$self->{connlist}}, $ref;
358 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
372 # return a list of valid elements
382 # export the database to an ascii file
387 my $name = shift || 'user_json';
388 my $basic_info_only = shift;
390 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
393 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
394 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
395 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
396 move "$fn.o", "$fn.oo" if -e "$fn.o";
397 move "$fn", "$fn.o" if -e "$fn";
399 my $ta = [gettimeofday];
403 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
408 my $t = scalar localtime;
409 print $fh q{#!/usr/bin/perl
411 # The exported userfile for a DXSpider System
413 # Input file: $filename
419 # search local then perl directories
423 # root of directory tree for this system
425 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
427 unshift @INC, "$root/perl"; # this IS the right way round!
428 unshift @INC, "$root/local";
430 # try to detect a lockfile (this isn't atomic but
432 $lockfn = "$root/local_data/cluster.lck"; # lock file name
434 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
437 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
446 use Time::HiRes qw(gettimeofday tv_interval);
449 our $json = JSON->new->canonical(1);
451 my $ta = [gettimeofday];
452 our $filename = "$main::local_data/users.v3j";
453 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
454 print "perl user_json $exists $filename\n";
464 my $ref = decode($f[1]);
469 print "# Error: $f[0]\t$f[1]\n";
473 DXUser::sync(); DXUser::finish();
474 my $diff = _diffms($ta);
475 print "There are $count user records and $err errors in $diff mS\n";
477 print $fh "__DATA__\n";
479 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
480 if (!is_callsign($key) || $key =~ /^0/) {
483 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
484 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
485 LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
486 eval {$dbm->del($key)};
487 dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
492 eval {$ref = decode($val); };
494 my $t = $ref->{lastin} || 0;
495 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
496 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
497 eval {$dbm->del($key)};
498 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
499 LogDbg('DXCommand', "$ref->{call} deleted, too old");
504 # only store users that are reasonably active or have useful information
505 print $fh "$key\t" . encode($ref) . "\n";
508 LogDbg('DXCommand', "Export Error3: $key\t" . carp($val) ."\n$@");
509 eval {$dbm->del($key)};
510 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
516 my $diff = _diffms($ta);
517 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors in $diff mS ('sh/log Export' for details)};
518 LogDbg('command', $s);
526 # add one or more groups
530 my $ref = $self->{group} || [ 'local' ];
531 $self->{group} = $ref if !$self->{group};
532 push @$ref, @_ if @_;
535 # remove one or more groups
539 my $ref = $self->{group} || [ 'local' ];
542 $self->{group} = $ref if !$self->{group};
544 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
547 # does this thing contain all the groups listed?
551 my $ref = $self->{group};
554 return 0 if !$ref || @_ == 0;
555 return 1 if @$ref == 0 && @_ == 0;
556 for ($n = 0; $n < @_; ) {
559 $n++ if grep $_ eq $a, @_;
565 # simplified group test just for one group
570 my $ref = $self->{group};
573 return grep $_ eq $s, $ref;
576 # set up a default group (only happens for them's that connect direct)
580 $self->{group} = [ 'local' ];
583 # set up empty buddies (only happens for them's that connect direct)
587 $self->{buddies} = [ ];
591 # return a prompt for a field
596 my ($self, $ele) = @_;
600 # some variable accessors
604 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
609 # want is default = 1
616 $self->{$s} = $val if defined $val;
617 return exists $self->{$s} ? $self->{$s} : 1;
620 # wantnot is default = 0
627 $self->{$s} = $val if defined $val;
628 return exists $self->{$s} ? $self->{$s} : 0;
633 return _want('beep', @_);
638 return _want('ann', @_);
643 return _want('wwv', @_);
648 return _want('wcy', @_);
653 return _want('echo', @_);
658 return _want('wx', @_);
663 return _want('dx', @_);
668 return _want('talk', @_);
673 return _want('grid', @_);
678 return _want('email', @_);
683 return _want('ann_talk', @_);
688 return _want('pc16', @_);
693 return _want('sendpc16', @_);
698 return _want('routepc16', @_);
703 return _want('usstate', @_);
708 return _want('dxcq', @_);
713 return _want('dxitu', @_);
718 return _want('gtk', @_);
723 return _want('pc9x', @_);
730 $self->{wantlogininfo} = $val if defined $val;
731 return $self->{wantlogininfo};
737 return $self->{sort} =~ /^[ACRSX]$/;
743 return grep $_ eq 'local_node', @{$self->{group}};
749 return $self->{sort} =~ /^[UW]$/;
755 return $self->{sort} eq 'W';
761 return $self->{sort} eq 'B';
767 return $self->{sort} eq 'S';
773 return $self->{sort} eq 'C';
779 return $self->{sort} eq 'X';
785 return $self->{sort} eq 'R';
791 return $self->{sort} eq 'A';
797 return $self->{sort} eq 'N'
803 delete $self->{passwd};
809 delete $self->{passphrase};
816 $self->{believe} ||= [];
817 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
824 if (exists $self->{believe}) {
825 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
826 delete $self->{believe} unless @{$self->{believe}};
833 return exists $self->{believe} ? @{$self->{believe}} : ();
840 $self->{lastping} ||= {};
841 $self->{lastping} = {} unless ref $self->{lastping};
842 my $b = $self->{lastping};
843 $b->{$call} = shift if @_;