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
44 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
45 K => '9,Seen on PC92 K,yesno',
46 addr => '0,Full Address',
47 addr => '0,Full Address',
48 alias => '0,Real Callsign',
49 alias => '0,Real Callsign',
50 annok => '9,Accept Announces?,yesno', # accept his announces?
52 believe => '1,Believable nodes,parray',
53 believe => '1,Believable nodes,parray',
54 buddies => '0,Buddies,parray',
59 connlist => '1,Connections,parraydifft',
60 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
61 email => '0,E-mail Address,parray',
62 email => '0,E-mail Address,parray',
63 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
64 hmsgno => '0,Highest Msgno',
65 homenode => '0,Home Node',
66 isolate => '9,Isolate network,yesno',
68 lastin => '0,Last Time in,cldatetime',
69 lastin => '0,Last Time in,cldatetime',
70 lastoper => '9,Last for/oper,cldatetime',
71 lastoper => '9,Last for/oper,cldatetime',
72 lastping => '1,Last Ping at,ptimelist',
73 lastping => '1,Last Ping at,ptimelist',
74 lastseen => '0,Last Seen,cldatetime',
75 lat => '0,Latitude,slat',
76 lockout => '9,Locked out?,yesno', # won't let them in at all
77 long => '0,Longitude,slong',
78 maxconnect => '1,Max Connections',
80 node => '0,Last Node',
81 nopings => '9,Ping Obs Count',
82 nothere => '0,Not Here Text',
83 pagelth => '0,Current Pagelth',
84 passphrase => '9,Pass Phrase,yesno',
85 passwd => '9,Password,yesno',
86 pingint => '9,Node Ping interval',
87 priv => '9,Privilege Level',
88 prompt => '0,Required Prompt',
91 rbnseeme => '0,RBN See Me',
92 registered => '9,Registered?,yesno',
93 startt => '0,Start Time,cldatetime',
94 version => '1,Version',
95 wantann => '0,Req Announce,yesno',
96 wantann_talk => '0,Talklike Anns,yesno',
97 wantbeacon => '0,Want RBN Beacon,yesno',
98 wantbeep => '0,Req Beep,yesno',
99 wantcw => '0,Want RBN CW,yesno',
100 wantdx => '0,Req DX Spots,yesno',
101 wantdxcq => '0,Show CQ Zone,yesno',
102 wantdxitu => '0,Show ITU Zone,yesno',
103 wantecho => '0,Req Echo,yesno',
104 wantemail => '0,Req Msgs as Email,yesno',
105 wantft => '0,Want RBN FT4/8,yesno',
106 wantgtk => '0,Want GTK interface,yesno',
107 wantlogininfo => '0,Login Info Req,yesno',
108 wantpc16 => '9,Want Users from node,yesno',
109 wantpc9x => '0,Want PC9X interface,yesno',
110 wantpsk => '0,Want RBN PSK,yesno',
111 wantrbn => '0,Want RBN spots,yesno',
112 wantroutepc19 => '9,Route PC19,yesno',
113 wantrtty => '0,Want RBN RTTY,yesno',
114 wantsendpc16 => '9,Send PC16,yesno',
115 wanttalk => '0,Req Talk,yesno',
116 wantusstate => '0,Show US State,yesno',
117 wantwcy => '0,Req WCY,yesno',
118 wantwwv => '0,Req WWV,yesno',
119 wantwx => '0,Req WX,yesno',
120 width => '0,Preferred Width',
121 xpert => '0,Expert Status,yesno',
122 wantgrid => '0,Show DX Grid,yesno',
129 my $name = $AUTOLOAD;
131 return if $name =~ /::DESTROY$/;
134 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
135 # this clever line of code creates a subroutine which takes over from autoload
136 # from OO Perl - Conway
137 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
144 # initialise the system
150 $json = DXJSON->new->canonical(1);
152 $filename = localdata("$fn.v3j");
153 unless (-e $filename || $mode == 2 ) {
154 if (-e localdata("$fn.v3") || -e localdata("$fn.v2")) {
155 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
156 system('/spider/perl/convert-users-v3-to-v3j.pl');
162 if (-e $filename || $mode) {
163 $lru = LRU->newbase("DXUser", $lrusize);
165 $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?]";
167 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
170 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
174 # delete files with extreme prejudice
177 # with extreme prejudice
178 unlink "$main::data/users.v3j";
179 unlink "$main::local_data/users.v3j";
183 # periodic processing
187 if ($main::systime > $lasttime + 15) {
189 $lasttime = $main::systime;
205 # new - create a new user
212 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
220 # $call =~ s/-\d+$//o;
222 # confess "can't create existing call $call in User\n!" if $u{$call};
224 my $self = $pkg->alloc($call);
230 # get - get an existing user - this seems to return a different reference everytime it is
239 # is it in the LRU cache?
240 my $ref = $lru->get($call);
241 if ($ref && ref $ref eq 'DXUser') {
246 unless ($dbm->get($call, $data)) {
247 eval { $ref = decode($data); };
249 if (!UNIVERSAL::isa($ref, 'DXUser')) {
250 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
253 # we have a reference and it *is* a DXUser
256 LogDbg('err', "DXUser::get decode error on $call '$@'");
258 dbg("DXUser::get: no reference returned from decode of $call $!");
262 $lru->put($call, $ref);
269 # get an existing either from the channel (if there is one) or from the database
271 # It is important to note that if you have done a get (for the channel say) and you
272 # want access or modify that you must use this call (and you must NOT use get's all
273 # over the place willy nilly!)
280 my $dxchan = DXChannel::get($call);
282 my $ref = $dxchan->user;
283 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
285 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
291 # get all callsigns in the database
296 return (sort keys %u);
306 confess "Trying to put nothing!" unless $self && ref $self;
307 my $call = $self->{call};
310 delete $self->{annok};
311 delete $self->{dxok};
312 $self->{lastseen} = $main::systime;
313 $lru->put($call, $self);
314 my $ref = $self->encode;
315 $dbm->put($call, $ref);
322 return $json->decode(shift, __PACKAGE__);
328 return $json->encode(shift);
333 # del - delete a user
339 my $call = $self->{call};
345 # close - close down a user
353 # add a record to the connect list
354 $self->{lastin} = $main::systime;
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 copy $fn, "$fn.keep" unless -e "$fn.keep";
394 copy "$fn.ooooo", "$fn.backstop" unless -e "$fn,backstop";
396 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
397 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
398 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
399 move "$fn.o", "$fn.oo" if -e "$fn.o";
400 move "$fn", "$fn.o" if -e "$fn";
403 my $ta = [gettimeofday];
414 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
419 my $t = scalar localtime;
420 print $fh q{#!/usr/bin/perl
422 # The exported userfile for a DXSpider System
424 # Input file: $filename
430 # search local then perl directories
434 # root of directory tree for this system
436 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
438 unshift @INC, "$root/perl"; # this IS the right way round!
439 unshift @INC, "$root/local";
441 # try to detect a lockfile (this isn't atomic but
443 $lockfn = "$root/local_data/cluster.lck"; # lock file name
445 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
448 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
457 use Time::HiRes qw(gettimeofday tv_interval);
460 our $json = JSON->new->canonical(1);
462 my $ta = [gettimeofday];
463 our $filename = "$main::local_data/users.v3j";
464 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
465 print "perl user_json $exists $filename\n";
475 my $ref = decode($f[1]);
480 print "# Error: $f[0]\t$f[1]\n";
484 DXUser::sync(); DXUser::finish();
485 my $diff = _diffms($ta);
486 print "There are $count user records and $err errors in $diff mS\n";
488 print $fh "__DATA__\n";
490 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
491 if (!is_callsign($key) || $key =~ /^0/) {
494 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
495 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
496 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
497 eval {$dbm->del($key)};
498 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
503 eval {$ref = decode($val); };
505 my $t = $ref->{lastseen} if exists $ref->{lastseen};
506 $t ||= $ref->{lastin} if exists $ref->{lastin};
507 $t ||= $ref->{lastoper} if exists $ref->{lastoper};
511 if (!$ref->{priv} && $main::systime > $t + $tooold) {
512 unless (($ref->{lat} && $ref->{long}) || $ref->{qth} || $ref->{name} || $ref->{qra}) {
513 LogDbg('DXCommand', sprintf("$ref->{call} deleted, empty and too Old at %s", difft($t, ' ')));
516 eval {$dbm->del($key)};
517 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
521 if ($main::systime > $t + $veryold) {
522 LogDbg('DXCommand', sprintf("$ref->{call} deleted, POSITIVELY ANCIENT at %s", difft($t, ' ')));
525 eval {$dbm->del($key)};
526 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
529 if (exists $ref->{lockout} && $ref->{lockout} == 1 && exists $ref->{priv} && $ref->{priv} == 1) {
530 LogDbg('DXCommand', "$ref->{call} depriv'd and unlocked");
531 $ref->{lockout} = $ref->{priv} = 0;
535 if ($ref->is_node && $main::systime > $t + $veryold) {
536 LogDbg('DXCommand', sprintf("NODE $ref->{call} deleted (%s) old", difft($t, ' ')));
539 eval {$dbm->del($key)};
540 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
544 my $normcall = normalise_call($key);
545 if ($normcall ne $key) {
546 # if the normalised call does not exist, create it from the duff call.
547 my $nref = DXUser::get_current($normcall);
549 $ref->{call} = $normcall;
551 LogDbg('DXCommand', "DXProt: spurious call $key normalises to $normcall renaming $key -> $normcall");
554 LogDbg('DXCommand', "DXProt: spurious call $key (should be $normcall), removing");
555 eval {$dbm->del($key)};
556 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
563 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
564 eval {$dbm->del($key)};
565 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
570 # only store users that are reasonably active or have useful information
571 print $fh "$key\t" . encode($ref) . "\n";
577 my $diff = _diffms($ta);
578 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)};
579 LogDbg('command', $s);
587 # add one or more groups
591 my $ref = $self->{group} || [ 'local' ];
592 $self->{group} = $ref if !$self->{group};
593 push @$ref, @_ if @_;
596 # remove one or more groups
600 my $ref = $self->{group} || [ 'local' ];
603 $self->{group} = $ref if !$self->{group};
605 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
608 # does this thing contain all the groups listed?
612 my $ref = $self->{group};
615 return 0 if !$ref || @_ == 0;
616 return 1 if @$ref == 0 && @_ == 0;
617 for ($n = 0; $n < @_; ) {
620 $n++ if grep $_ eq $a, @_;
626 # simplified group test just for one group
631 my $ref = $self->{group};
634 return grep $_ eq $s, $ref;
637 # set up a default group (only happens for them's that connect direct)
641 $self->{group} = [ 'local' ];
644 # set up empty buddies (only happens for them's that connect direct)
648 $self->{buddies} = [ ];
652 # return a prompt for a field
657 my ($self, $ele) = @_;
661 # some variable accessors
665 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
670 # want is default = 1
677 $self->{$s} = $val if defined $val;
678 return exists $self->{$s} ? $self->{$s} : 1;
681 # wantnot is default = 0
688 $self->{$s} = $val if defined $val;
689 return exists $self->{$s} ? $self->{$s} : 0;
694 return _want('beep', @_);
699 return _want('ann', @_);
704 return _want('wwv', @_);
709 return _want('wcy', @_);
714 return _want('echo', @_);
719 return _want('wx', @_);
724 return _want('dx', @_);
729 return _want('talk', @_);
734 return _wantnot('grid', @_);
739 return _want('email', @_);
744 return _want('ann_talk', @_);
749 return _want('pc16', @_);
754 return _want('sendpc16', @_);
759 return _want('routepc16', @_);
764 return _want('usstate', @_);
769 return _wantnot('dxcq', @_);
774 return _wantnot('dxitu', @_);
779 return _want('gtk', @_);
784 return _want('pc9x', @_);
791 $self->{wantlogininfo} = $val if defined $val;
792 return $self->{wantlogininfo};
798 return $self->{sort} =~ /^[ACRSX]$/;
804 return grep $_ eq 'local_node', @{$self->{group}};
810 return $self->{sort} =~ /^[UW]$/;
816 return $self->{sort} eq 'W';
822 return $self->{sort} eq 'B';
828 return $self->{sort} eq 'S';
834 return $self->{sort} eq 'C';
840 return $self->{sort} eq 'X';
846 return $self->{sort} eq 'R';
852 return $self->{sort} eq 'A';
858 return $self->{sort} eq 'N'
864 delete $self->{passwd};
870 delete $self->{passphrase};
877 $self->{believe} ||= [];
878 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
885 if (exists $self->{believe}) {
886 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
887 delete $self->{believe} unless @{$self->{believe}};
894 return exists $self->{believe} ? @{$self->{believe}} : ();
901 $self->{lastping} ||= {};
902 $self->{lastping} = {} unless ref $self->{lastping};
903 my $b = $self->{lastping};
904 $b->{$call} = shift if @_;
912 # if (defined $_[0]) {
913 # $val = unpad($_[0]);
914 # $self->{registered} = $val;
916 # if (exists $self->{registered}) {
917 # $val = $self->{registered} // 0;
919 # return $val // 0 ; # to stop undef warnings