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 lat => '0,Latitude,slat',
77 lockout => '9,Locked out?,yesno', # won't let them in at all
78 long => '0,Longitude,slong',
79 long => '0,Longitude,slong',
80 maxconnect => '1,Max Connections',
81 maxconnect => '1,Max Connections',
84 node => '0,Last Node',
85 nopings => '9,Ping Obs Count',
86 nothere => '0,Not Here Text',
87 nothere => '0,Not Here Text',
88 pagelth => '0,Current Pagelth',
89 passphrase => '9,Pass Phrase,yesno',
90 passphrase => '9,Pass Phrase,yesno',
91 passwd => '9,Password,yesno',
92 passwd => '9,Password,yesno',
93 pingint => '9,Node Ping interval',
94 priv => '9,Privilege Level',
95 priv => '9,Privilege Level',
96 prompt => '0,Required Prompt',
97 prompt => '0,Required Prompt',
102 rbnseeme => '0,RBN See Me',
103 registered => '9,Registered?,yesno',
104 registered => '9,Registered?,yesno',
105 startt => '0,Start Time,cldatetime',
106 version => '1,Version',
107 version => '1,Version',
108 wantann => '0,Req Announce,yesno',
109 wantann_talk => '0,Talklike Anns,yesno',
110 wantbeacon => '0,Want RBN Beacon,yesno',
111 wantbeep => '0,Req Beep,yesno',
112 wantcw => '0,Want RBN CW,yesno',
113 wantdx => '0,Req DX Spots,yesno',
114 wantdxcq => '0,Show CQ Zone,yesno',
115 wantdxitu => '0,Show ITU Zone,yesno',
116 wantecho => '0,Req Echo,yesno',
117 wantemail => '0,Req Msgs as Email,yesno',
118 wantft => '0,Want RBN FT4/8,yesno',
119 wantgtk => '0,Want GTK interface,yesno',
120 wantlogininfo => '0,Login Info Req,yesno',
121 wantpc16 => '9,Want Users from node,yesno',
122 wantpc9x => '0,Want PC9X interface,yesno',
123 wantpsk => '0,Want RBN PSK,yesno',
124 wantrbn => '0,Want RBN spots,yesno',
125 wantroutepc19 => '9,Route PC19,yesno',
126 wantrtty => '0,Want RBN RTTY,yesno',
127 wantsendpc16 => '9,Send PC16,yesno',
128 wanttalk => '0,Req Talk,yesno',
129 wantusstate => '0,Show US State,yesno',
130 wantwcy => '0,Req WCY,yesno',
131 wantwwv => '0,Req WWV,yesno',
132 wantwx => '0,Req WX,yesno',
133 width => '0,Preferred Width',
134 width => '0,Preferred Width',
135 xpert => '0,Expert Status,yesno',
136 wantgrid => '0,Show DX Grid,yesno',
143 my $name = $AUTOLOAD;
145 return if $name =~ /::DESTROY$/;
148 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
149 # this clever line of code creates a subroutine which takes over from autoload
150 # from OO Perl - Conway
151 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
158 # initialise the system
164 $json = DXJSON->new->canonical(1);
166 $filename = localdata("$fn.v3j");
167 unless (-e $filename || $mode == 2 ) {
168 if (-e localdata("$fn.v3") || -e localdata("$fn.v2")) {
169 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
170 system('/spider/perl/convert-users-v3-to-v3j.pl');
176 if (-e $filename || $mode) {
177 $lru = LRU->newbase("DXUser", $lrusize);
179 $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?]";
181 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
184 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
188 # delete files with extreme prejudice
191 # with extreme prejudice
192 unlink "$main::data/users.v3j";
193 unlink "$main::local_data/users.v3j";
197 # periodic processing
201 if ($main::systime > $lasttime + 15) {
203 $lasttime = $main::systime;
219 # new - create a new user
226 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
234 # $call =~ s/-\d+$//o;
236 # confess "can't create existing call $call in User\n!" if $u{$call};
238 my $self = $pkg->alloc($call);
244 # get - get an existing user - this seems to return a different reference everytime it is
253 # is it in the LRU cache?
254 my $ref = $lru->get($call);
255 if ($ref && ref $ref eq 'DXUser') {
260 unless ($dbm->get($call, $data)) {
261 eval { $ref = decode($data); };
263 if (!UNIVERSAL::isa($ref, 'DXUser')) {
264 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
267 # we have a reference and it *is* a DXUser
270 LogDbg('err', "DXUser::get decode error on $call '$@'");
272 dbg("DXUser::get: no reference returned from decode of $call $!");
276 $lru->put($call, $ref);
283 # get an existing either from the channel (if there is one) or from the database
285 # It is important to note that if you have done a get (for the channel say) and you
286 # want access or modify that you must use this call (and you must NOT use get's all
287 # over the place willy nilly!)
294 my $dxchan = DXChannel::get($call);
296 my $ref = $dxchan->user;
297 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
299 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
305 # get all callsigns in the database
310 return (sort keys %u);
320 confess "Trying to put nothing!" unless $self && ref $self;
321 my $call = $self->{call};
324 delete $self->{annok};
325 delete $self->{dxok};
326 $self->{lastseen} = $main::systime;
327 $lru->put($call, $self);
328 my $ref = $self->encode;
329 $dbm->put($call, $ref);
336 return $json->decode(shift, __PACKAGE__);
342 return $json->encode(shift);
347 # del - delete a user
353 my $call = $self->{call};
359 # close - close down a user
367 # add a record to the connect list
368 $self->{lastin} = $main::systime;
369 my $ref = [$startt || $self->{startt}, $main::systime];
370 push @$ref, $ip if $ip;
371 push @{$self->{connlist}}, $ref;
372 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
386 # return a list of valid elements
396 # export the database to an ascii file
401 my $name = shift || 'user_json';
402 my $basic_info_only = shift;
404 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
407 copy $fn, "$fn.keep" unless -e "$fn.keep";
408 copy "$fn.ooooo", "$fn.backstop" unless -e "$fn,backstop";
410 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
411 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
412 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
413 move "$fn.o", "$fn.oo" if -e "$fn.o";
414 move "$fn", "$fn.o" if -e "$fn";
417 my $ta = [gettimeofday];
428 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
433 my $t = scalar localtime;
434 print $fh q{#!/usr/bin/perl
436 # The exported userfile for a DXSpider System
438 # Input file: $filename
444 # search local then perl directories
448 # root of directory tree for this system
450 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
452 unshift @INC, "$root/perl"; # this IS the right way round!
453 unshift @INC, "$root/local";
455 # try to detect a lockfile (this isn't atomic but
457 $lockfn = "$root/local_data/cluster.lck"; # lock file name
459 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
462 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
471 use Time::HiRes qw(gettimeofday tv_interval);
474 our $json = JSON->new->canonical(1);
476 my $ta = [gettimeofday];
477 our $filename = "$main::local_data/users.v3j";
478 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
479 print "perl user_json $exists $filename\n";
489 my $ref = decode($f[1]);
494 print "# Error: $f[0]\t$f[1]\n";
498 DXUser::sync(); DXUser::finish();
499 my $diff = _diffms($ta);
500 print "There are $count user records and $err errors in $diff mS\n";
502 print $fh "__DATA__\n";
504 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
505 if (!is_callsign($key) || $key =~ /^0/) {
508 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
509 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
510 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
511 eval {$dbm->del($key)};
512 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
517 eval {$ref = decode($val); };
519 my $t = $ref->{lastseen} if exists $ref->{lastseen};
520 $t ||= $ref->{lastin} if exists $ref->{lastin};
521 $t ||= $ref->{lastoper} if exists $ref->{lastoper};
525 if (!$ref->{priv} && $main::systime > $t + $tooold) {
526 unless (($ref->{lat} && $ref->{long}) || $ref->{qth} || $ref->{name} || $ref->{qra}) {
527 LogDbg('DXCommand', sprintf("$ref->{call} deleted, empty and too Old at %s", difft($t, ' ')));
530 eval {$dbm->del($key)};
531 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
535 if ($main::systime > $t + $veryold) {
536 LogDbg('DXCommand', sprintf("$ref->{call} deleted, POSITIVELY ANCIENT at %s", difft($t, ' ')));
539 eval {$dbm->del($key)};
540 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
543 if (exists $ref->{lockout} && $ref->{lockout} == 1 && exists $ref->{priv} && $ref->{priv} == 1) {
544 LogDbg('DXCommand', "$ref->{call} depriv'd and unlocked");
545 $ref->{lockout} = $ref->{priv} = 0;
549 if ($ref->is_node && $main::systime > $t + $veryold) {
550 LogDbg('DXCommand', sprintf("NODE $ref->{call} deleted (%s) old", difft($t, ' ')));
553 eval {$dbm->del($key)};
554 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
558 my $normcall = normalise_call($key);
559 if ($normcall ne $key) {
560 # if the normalised call does not exist, create it from the duff call.
561 my $nref = DXUser::get_current($normcall);
563 $ref->{call} = $normcall;
565 LogDbg('DXCommand', "DXProt: spurious call $key normalises to $normcall renaming $key -> $normcall");
568 LogDbg('DXCommand', "DXProt: spurious call $key (should be $normcall), removing");
569 eval {$dbm->del($key)};
570 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
577 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
578 eval {$dbm->del($key)};
579 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
584 # only store users that are reasonably active or have useful information
585 print $fh "$key\t" . encode($ref) . "\n";
591 my $diff = _diffms($ta);
592 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)};
593 LogDbg('command', $s);
601 # add one or more groups
605 my $ref = $self->{group} || [ 'local' ];
606 $self->{group} = $ref if !$self->{group};
607 push @$ref, @_ if @_;
610 # remove one or more groups
614 my $ref = $self->{group} || [ 'local' ];
617 $self->{group} = $ref if !$self->{group};
619 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
622 # does this thing contain all the groups listed?
626 my $ref = $self->{group};
629 return 0 if !$ref || @_ == 0;
630 return 1 if @$ref == 0 && @_ == 0;
631 for ($n = 0; $n < @_; ) {
634 $n++ if grep $_ eq $a, @_;
640 # simplified group test just for one group
645 my $ref = $self->{group};
648 return grep $_ eq $s, $ref;
651 # set up a default group (only happens for them's that connect direct)
655 $self->{group} = [ 'local' ];
658 # set up empty buddies (only happens for them's that connect direct)
662 $self->{buddies} = [ ];
666 # return a prompt for a field
671 my ($self, $ele) = @_;
675 # some variable accessors
679 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
684 # want is default = 1
691 $self->{$s} = $val if defined $val;
692 return exists $self->{$s} ? $self->{$s} : 1;
695 # wantnot is default = 0
702 $self->{$s} = $val if defined $val;
703 return exists $self->{$s} ? $self->{$s} : 0;
708 return _want('beep', @_);
713 return _want('ann', @_);
718 return _want('wwv', @_);
723 return _want('wcy', @_);
728 return _want('echo', @_);
733 return _want('wx', @_);
738 return _want('dx', @_);
743 return _want('talk', @_);
748 return _wantnot('grid', @_);
753 return _want('email', @_);
758 return _want('ann_talk', @_);
763 return _want('pc16', @_);
768 return _want('sendpc16', @_);
773 return _want('routepc16', @_);
778 return _want('usstate', @_);
783 return _wantnot('dxcq', @_);
788 return _wantnot('dxitu', @_);
793 return _want('gtk', @_);
798 return _want('pc9x', @_);
805 $self->{wantlogininfo} = $val if defined $val;
806 return $self->{wantlogininfo};
812 return $self->{sort} =~ /^[ACRSX]$/;
818 return grep $_ eq 'local_node', @{$self->{group}};
824 return $self->{sort} =~ /^[UW]$/;
830 return $self->{sort} eq 'W';
836 return $self->{sort} eq 'B';
842 return $self->{sort} eq 'S';
848 return $self->{sort} eq 'C';
854 return $self->{sort} eq 'X';
860 return $self->{sort} eq 'R';
866 return $self->{sort} eq 'A';
872 return $self->{sort} eq 'N'
878 delete $self->{passwd};
884 delete $self->{passphrase};
891 $self->{believe} ||= [];
892 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
899 if (exists $self->{believe}) {
900 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
901 delete $self->{believe} unless @{$self->{believe}};
908 return exists $self->{believe} ? @{$self->{believe}} : ();
915 $self->{lastping} ||= {};
916 $self->{lastping} = {} unless ref $self->{lastping};
917 my $b = $self->{lastping};
918 $b->{$call} = shift if @_;
928 $self->{registered} = $val;
930 if (exists $self->{registered}) {
931 $val = $self->{registered} // 0;
933 return $val // 0 ; # to stop undef warnings