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);
27 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $veryold $v3);
32 $lastoperinterval = 60*24*60*60;
35 $tooold = 86400 * 365 * 2; # this marks an old user who hasn't given enough info to be useful
36 $veryold = $tooold * 6; # Ancient default 12 years
38 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
42 # hash of valid elements and a simple prompt
45 alias => '0,Real Callsign',
48 lat => '0,Latitude,slat',
49 long => '0,Longitude,slong',
51 email => '0,E-mail Address,parray',
52 priv => '9,Privilege Level',
53 lastin => '0,Last Time in,cldatetime',
54 lastseen => '0,Last Seen,cldatetime',
55 passwd => '9,Password,yesno',
56 passphrase => '9,Pass Phrase,yesno',
57 addr => '0,Full Address',
58 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
59 xpert => '0,Expert Status,yesno',
61 node => '0,Last Node',
62 homenode => '0,Home Node',
63 lockout => '9,Locked out?,yesno', # won't let them in at all
64 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
65 annok => '9,Accept Announces?,yesno', # accept his announces?
67 hmsgno => '0,Highest Msgno',
68 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
69 buddies => '0,Buddies,parray',
70 isolate => '9,Isolate network,yesno',
71 wantbeep => '0,Req Beep,yesno',
72 wantann => '0,Req Announce,yesno',
73 wantwwv => '0,Req WWV,yesno',
74 wantwcy => '0,Req WCY,yesno',
75 wantecho => '0,Req Echo,yesno',
76 wanttalk => '0,Req Talk,yesno',
77 wantwx => '0,Req WX,yesno',
78 wantdx => '0,Req DX Spots,yesno',
79 wantemail => '0,Req Msgs as Email,yesno',
80 pagelth => '0,Current Pagelth',
81 pingint => '9,Node Ping interval',
82 nopings => '9,Ping Obs Count',
83 wantlogininfo => '0,Login Info Req,yesno',
84 wantgrid => '0,Show DX Grid,yesno',
85 wantann_talk => '0,Talklike Anns,yesno',
86 wantpc16 => '9,Want Users from node,yesno',
87 wantsendpc16 => '9,Send PC16,yesno',
88 wantroutepc19 => '9,Route PC19,yesno',
89 wantusstate => '0,Show US State,yesno',
90 wantdxcq => '0,Show CQ Zone,yesno',
91 wantdxitu => '0,Show ITU Zone,yesno',
92 wantgtk => '0,Want GTK interface,yesno',
93 wantpc9x => '0,Want PC9X interface,yesno',
94 wantrbn => '0,Want RBN spots,yesno',
95 wantft => '0,Want RBN FT4/8,yesno',
96 wantcw => '0,Want RBN CW,yesno',
97 wantrtty => '0,Want RBN RTTY,yesno',
98 wantpsk => '0,Want RBN PSK,yesno',
99 wantbeacon => '0,Want RBN Beacon,yesno',
100 lastoper => '9,Last for/oper,cldatetime',
101 nothere => '0,Not Here Text',
102 registered => '9,Registered?,yesno',
103 prompt => '0,Required Prompt',
104 version => '1,Version',
106 believe => '1,Believable nodes,parray',
107 lastping => '1,Last Ping at,ptimelist',
108 maxconnect => '1,Max Connections',
109 startt => '0,Start Time,cldatetime',
110 connlist => '1,Connections,parraydifft',
111 width => '0,Preferred Width',
112 rbnseeme => '0,RBN See Me',
119 my $name = $AUTOLOAD;
121 return if $name =~ /::DESTROY$/;
124 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
125 # this clever line of code creates a subroutine which takes over from autoload
126 # from OO Perl - Conway
127 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
134 # initialise the system
140 $json = DXJSON->new->canonical(1);
142 $filename = localdata("$fn.v3j");
143 unless (-e $filename || $mode == 2 ) {
144 if (-e localdata("$fn.v3") || -e localdata("$fn.v2")) {
145 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
146 system('/spider/perl/convert-users-v3-to-v3j.pl');
152 if (-e $filename || $mode) {
153 $lru = LRU->newbase("DXUser", $lrusize);
155 $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?]";
157 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
160 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
164 # delete files with extreme prejudice
167 # with extreme prejudice
168 unlink "$main::data/users.v3j";
169 unlink "$main::local_data/users.v3j";
173 # periodic processing
177 if ($main::systime > $lasttime + 15) {
179 $lasttime = $main::systime;
195 # new - create a new user
202 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
210 # $call =~ s/-\d+$//o;
212 # confess "can't create existing call $call in User\n!" if $u{$call};
214 my $self = $pkg->alloc($call);
220 # get - get an existing user - this seems to return a different reference everytime it is
229 # is it in the LRU cache?
230 my $ref = $lru->get($call);
231 if ($ref && ref $ref eq 'DXUser') {
236 unless ($dbm->get($call, $data)) {
237 eval { $ref = decode($data); };
240 if (!UNIVERSAL::isa($ref, 'DXUser')) {
241 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
244 # we have a reference and it *is* a DXUser
247 LogDbg('err', "DXUser::get decode error on $call '$@'");
249 dbg("DXUser::get: no reference returned from decode of $call $!");
253 $lru->put($call, $ref);
260 # get an existing either from the channel (if there is one) or from the database
262 # It is important to note that if you have done a get (for the channel say) and you
263 # want access or modify that you must use this call (and you must NOT use get's all
264 # over the place willy nilly!)
271 my $dxchan = DXChannel::get($call);
273 my $ref = $dxchan->user;
274 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
276 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
282 # get all callsigns in the database
287 return (sort keys %u);
297 confess "Trying to put nothing!" unless $self && ref $self;
298 my $call = $self->{call};
301 delete $self->{annok};
302 delete $self->{dxok};
303 $self->{lastseen} = $main::systime;
304 $lru->put($call, $self);
305 my $ref = $self->encode;
306 $dbm->put($call, $ref);
313 return $json->decode(shift, __PACKAGE__);
319 return $json->encode(shift);
324 # del - delete a user
330 my $call = $self->{call};
336 # close - close down a user
344 # add a record to the connect list
345 $self->{lastin} = $main::systime;
346 my $ref = [$startt || $self->{startt}, $main::systime];
347 push @$ref, $ip if $ip;
348 push @{$self->{connlist}}, $ref;
349 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
363 # return a list of valid elements
373 # export the database to an ascii file
378 my $name = shift || 'user_json';
379 my $basic_info_only = shift;
381 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
384 copy $fn, "$fn.keep" unless -e "$fn.keep";
385 copy "$fn.ooooo", "$fn.backstop" unless -e "$fn,backstop";
387 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
388 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
389 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
390 move "$fn.o", "$fn.oo" if -e "$fn.o";
391 move "$fn", "$fn.o" if -e "$fn";
394 my $ta = [gettimeofday];
405 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
410 my $t = scalar localtime;
411 print $fh q{#!/usr/bin/perl
413 # The exported userfile for a DXSpider System
415 # Input file: $filename
421 # search local then perl directories
425 # root of directory tree for this system
427 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
429 unshift @INC, "$root/perl"; # this IS the right way round!
430 unshift @INC, "$root/local";
432 # try to detect a lockfile (this isn't atomic but
434 $lockfn = "$root/local_data/cluster.lck"; # lock file name
436 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
439 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
448 use Time::HiRes qw(gettimeofday tv_interval);
451 our $json = JSON->new->canonical(1);
453 my $ta = [gettimeofday];
454 our $filename = "$main::local_data/users.v3j";
455 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
456 print "perl user_json $exists $filename\n";
466 my $ref = decode($f[1]);
471 print "# Error: $f[0]\t$f[1]\n";
475 DXUser::sync(); DXUser::finish();
476 my $diff = _diffms($ta);
477 print "There are $count user records and $err errors in $diff mS\n";
479 print $fh "__DATA__\n";
481 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
482 if (!is_callsign($key) || $key =~ /^0/) {
485 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
486 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
487 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
488 eval {$dbm->del($key)};
489 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
494 eval {$ref = decode($val); };
496 my $t = $ref->{lastseen} if exists $ref->{lastseen};
497 $t ||= $ref->{lastin} if exists $ref->{lastin};
498 $t ||= $ref->{lastoper} if exists $ref->{lastoper};
502 if (!$ref->{priv} && $main::systime > $t + $tooold) {
503 unless (($ref->{lat} && $ref->{long}) || $ref->{qth} || $ref->{name} || $ref->{qra}) {
504 LogDbg('DXCommand', sprintf("$ref->{call} deleted, empty and too Old at %s", difft($t, ' ')));
507 eval {$dbm->del($key)};
508 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
512 if ($main::systime > $t + $veryold) {
513 LogDbg('DXCommand', sprintf("$ref->{call} deleted, POSITIVELY ANCIENT at %s", difft($t, ' ')));
516 eval {$dbm->del($key)};
517 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
520 if (exists $ref->{lockout} && $ref->{lockout} == 1 && exists $ref->{priv} && $ref->{priv} == 1) {
521 LogDbg('DXCommand', "$ref->{call} depriv'd and unlocked");
522 $ref->{lockout} = $ref->{priv} = 0;
526 if ($ref->is_node && $main::systime > $t + $veryold) {
527 LogDbg('DXCommand', sprintf("NODE $ref->{call} deleted (%s) old", difft($t, ' ')));
530 eval {$dbm->del($key)};
531 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
535 my $normcall = normalise_call($key);
536 if ($normcall ne $key) {
537 # if the normalised call does not exist, create it from the duff call.
538 my $nref = DXUser::get_current($normcall);
540 $ref->{call} = $normcall;
542 LogDbg('DXCommand', "DXProt: spurious call $key normalises to $normcall renaming $key -> $normcall");
545 LogDbg('DXCommand', "DXProt: spurious call $key (should be $normcall), removing");
546 eval {$dbm->del($key)};
547 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
554 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
555 eval {$dbm->del($key)};
556 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
561 # only store users that are reasonably active or have useful information
562 print $fh "$key\t" . encode($ref) . "\n";
568 my $diff = _diffms($ta);
569 my $s = qq{Exported users to $fn - $count Users, $del Deleted ($old empty \& too old, $ancient ancient, $nodes nodes, $spurious spurious), $renamed renamed, $unlocked Unlocked, $err Errors in $diff mS ('sh/log Export' for details)};
570 LogDbg('command', $s);
578 # add one or more groups
582 my $ref = $self->{group} || [ 'local' ];
583 $self->{group} = $ref if !$self->{group};
584 push @$ref, @_ if @_;
587 # remove one or more groups
591 my $ref = $self->{group} || [ 'local' ];
594 $self->{group} = $ref if !$self->{group};
596 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
599 # does this thing contain all the groups listed?
603 my $ref = $self->{group};
606 return 0 if !$ref || @_ == 0;
607 return 1 if @$ref == 0 && @_ == 0;
608 for ($n = 0; $n < @_; ) {
611 $n++ if grep $_ eq $a, @_;
617 # simplified group test just for one group
622 my $ref = $self->{group};
625 return grep $_ eq $s, $ref;
628 # set up a default group (only happens for them's that connect direct)
632 $self->{group} = [ 'local' ];
635 # set up empty buddies (only happens for them's that connect direct)
639 $self->{buddies} = [ ];
643 # return a prompt for a field
648 my ($self, $ele) = @_;
652 # some variable accessors
656 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
661 # want is default = 1
668 $self->{$s} = $val if defined $val;
669 return exists $self->{$s} ? $self->{$s} : 1;
672 # wantnot is default = 0
679 $self->{$s} = $val if defined $val;
680 return exists $self->{$s} ? $self->{$s} : 0;
685 return _want('beep', @_);
690 return _want('ann', @_);
695 return _want('wwv', @_);
700 return _want('wcy', @_);
705 return _want('echo', @_);
710 return _want('wx', @_);
715 return _want('dx', @_);
720 return _want('talk', @_);
725 return _wantnot('grid', @_);
730 return _want('email', @_);
735 return _want('ann_talk', @_);
740 return _want('pc16', @_);
745 return _want('sendpc16', @_);
750 return _want('routepc16', @_);
755 return _want('usstate', @_);
760 return _wantnot('dxcq', @_);
765 return _wantnot('dxitu', @_);
770 return _want('gtk', @_);
775 return _want('pc9x', @_);
782 $self->{wantlogininfo} = $val if defined $val;
783 return $self->{wantlogininfo};
789 return $self->{sort} =~ /^[ACRSX]$/;
795 return grep $_ eq 'local_node', @{$self->{group}};
801 return $self->{sort} =~ /^[UW]$/;
807 return $self->{sort} eq 'W';
813 return $self->{sort} eq 'B';
819 return $self->{sort} eq 'S';
825 return $self->{sort} eq 'C';
831 return $self->{sort} eq 'X';
837 return $self->{sort} eq 'R';
843 return $self->{sort} eq 'A';
849 return $self->{sort} eq 'N'
855 delete $self->{passwd};
861 delete $self->{passphrase};
868 $self->{believe} ||= [];
869 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
876 if (exists $self->{believe}) {
877 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
878 delete $self->{believe} unless @{$self->{believe}};
885 return exists $self->{believe} ? @{$self->{believe}} : ();
892 $self->{lastping} ||= {};
893 $self->{lastping} = {} unless ref $self->{lastping};
894 my $b = $self->{lastping};
895 $b->{$call} = shift if @_;
905 $self->{registered} = $val;
907 if (exists $self->{registered}) {
908 $val = $self->{registered} // 0;
910 return $val // 0 ; # to stop undef warnings