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 $v3);
32 $lastoperinterval = 60*24*60*60;
35 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
37 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 lastseen => '0,Last Seen,cldatetime',
54 passwd => '9,Password,yesno',
55 passphrase => '9,Pass Phrase,yesno',
56 addr => '0,Full Address',
57 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
58 xpert => '0,Expert Status,yesno',
60 node => '0,Last Node',
61 homenode => '0,Home Node',
62 lockout => '9,Locked out?,yesno', # won't let them in at all
63 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
64 annok => '9,Accept Announces?,yesno', # accept his announces?
66 hmsgno => '0,Highest Msgno',
67 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
68 buddies => '0,Buddies,parray',
69 isolate => '9,Isolate network,yesno',
70 wantbeep => '0,Req Beep,yesno',
71 wantann => '0,Req Announce,yesno',
72 wantwwv => '0,Req WWV,yesno',
73 wantwcy => '0,Req WCY,yesno',
74 wantecho => '0,Req Echo,yesno',
75 wanttalk => '0,Req Talk,yesno',
76 wantwx => '0,Req WX,yesno',
77 wantdx => '0,Req DX Spots,yesno',
78 wantemail => '0,Req Msgs as Email,yesno',
79 pagelth => '0,Current Pagelth',
80 pingint => '9,Node Ping interval',
81 nopings => '9,Ping Obs Count',
82 wantlogininfo => '0,Login Info Req,yesno',
83 wantgrid => '0,Show DX Grid,yesno',
84 wantann_talk => '0,Talklike Anns,yesno',
85 wantpc16 => '9,Want Users from node,yesno',
86 wantsendpc16 => '9,Send PC16,yesno',
87 wantroutepc19 => '9,Route PC19,yesno',
88 wantusstate => '0,Show US State,yesno',
89 wantdxcq => '0,Show CQ Zone,yesno',
90 wantdxitu => '0,Show ITU Zone,yesno',
91 wantgtk => '0,Want GTK interface,yesno',
92 wantpc9x => '0,Want PC9X interface,yesno',
93 wantrbn => '0,Want RBN spots,yesno',
94 wantft => '0,Want RBN FT4/8,yesno',
95 wantcw => '0,Want RBN CW,yesno',
96 wantrtty => '0,Want RBN RTTY,yesno',
97 wantpsk => '0,Want RBN PSK,yesno',
98 wantbeacon => '0,Want (RBN) Beacon,yesno',
99 lastoper => '9,Last for/oper,cldatetime',
100 nothere => '0,Not Here Text',
101 registered => '9,Registered?,yesno',
102 prompt => '0,Required Prompt',
103 version => '1,Version',
105 believe => '1,Believable nodes,parray',
106 lastping => '1,Last Ping at,ptimelist',
107 maxconnect => '1,Max Connections',
108 startt => '0,Start Time,cldatetime',
109 connlist => '1,Connections,parraydifft',
110 width => '0,Preferred Width'
117 my $name = $AUTOLOAD;
119 return if $name =~ /::DESTROY$/;
122 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
123 # this clever line of code creates a subroutine which takes over from autoload
124 # from OO Perl - Conway
125 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
132 # initialise the system
138 $json = JSON->new->canonical(1);
140 $filename = localdata("$fn.v3j");
141 unless (-e $filename || $mode == 2) {
142 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
143 system('/spider/perl/convert-users-v3-to-v3j.pl');
148 if (-e $filename || $mode == 2) {
149 $lru = LRU->newbase("DXUser", $lrusize);
151 $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?]";
153 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
156 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
160 # delete files with extreme prejudice
163 # with extreme prejudice
164 unlink "$main::data/users.v3j";
165 unlink "$main::local_data/users.v3j";
169 # periodic processing
173 if ($main::systime > $lasttime + 15) {
175 $lasttime = $main::systime;
191 # new - create a new user
198 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
206 # $call =~ s/-\d+$//o;
208 # confess "can't create existing call $call in User\n!" if $u{$call};
210 my $self = $pkg->alloc($call);
211 $self->{lastseen} = $main::systime;
217 # get - get an existing user - this seems to return a different reference everytime it is
226 # is it in the LRU cache?
227 my $ref = $lru->get($call);
228 if ($ref && ref $ref eq 'DXUser') {
229 $ref->{lastseen} = $main::systime;
234 unless ($dbm->get($call, $data)) {
235 eval { $ref = decode($data); };
238 if (!UNIVERSAL::isa($ref, 'DXUser')) {
239 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
242 # we have a reference and it *is* a DXUser
245 LogDbg('err', "DXUser::get decode error on $call '$@'");
247 dbg("DXUser::get: no reference returned from decode of $call $!");
251 $ref->{lastseen} = $main::systime;
252 $lru->put($call, $ref);
259 # get an existing either from the channel (if there is one) or from the database
261 # It is important to note that if you have done a get (for the channel say) and you
262 # want access or modify that you must use this call (and you must NOT use get's all
263 # over the place willy nilly!)
270 my $dxchan = DXChannel::get($call);
272 my $ref = $dxchan->user;
273 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
275 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
281 # get all callsigns in the database
286 return (sort keys %u);
296 confess "Trying to put nothing!" unless $self && ref $self;
297 my $call = $self->{call};
300 delete $self->{annok} if $self->{annok};
301 delete $self->{dxok} if $self->{dxok};
303 $lru->put($call, $self);
304 my $ref = $self->encode;
305 $dbm->put($call, $ref);
314 eval { $ref = $json->decode($s) };
316 return bless $ref, 'DXUser';
318 LogDbg('DXUser', "DXUser::json_decode: on '$s' $@");
330 eval {$s = $json->encode($ref) };
332 bless $ref, 'DXUser';
335 LogDbg('DXUser', "DXUser::json_encode $ref->{call}, $@");
341 # del - delete a user
347 my $call = $self->{call};
353 # close - close down a user
361 $self->{lastseen} = $self->{lastin} = $main::systime;
362 # add a record to the connect list
363 my $ref = [$startt || $self->{startt}, $main::systime];
364 push @$ref, $ip if $ip;
365 push @{$self->{connlist}}, $ref;
366 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
380 # return a list of valid elements
390 # export the database to an ascii file
395 my $name = shift || 'user_json';
396 my $basic_info_only = shift;
398 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
401 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
402 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
403 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
404 move "$fn.o", "$fn.oo" if -e "$fn.o";
405 move "$fn", "$fn.o" if -e "$fn";
407 my $ta = [gettimeofday];
411 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
416 my $t = scalar localtime;
417 print $fh q{#!/usr/bin/perl
419 # The exported userfile for a DXSpider System
421 # Input file: $filename
427 # search local then perl directories
431 # root of directory tree for this system
433 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
435 unshift @INC, "$root/perl"; # this IS the right way round!
436 unshift @INC, "$root/local";
438 # try to detect a lockfile (this isn't atomic but
440 $lockfn = "$root/local_data/cluster.lck"; # lock file name
442 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
445 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
454 use Time::HiRes qw(gettimeofday tv_interval);
457 our $json = JSON->new->canonical(1);
459 my $ta = [gettimeofday];
460 our $filename = "$main::local_data/users.v3j";
461 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
462 print "perl user_json $exists $filename\n";
472 my $ref = decode($f[1]);
477 print "# Error: $f[0]\t$f[1]\n";
481 DXUser::sync(); DXUser::finish();
482 my $diff = _diffms($ta);
483 print "There are $count user records and $err errors in $diff mS\n";
485 print $fh "__DATA__\n";
487 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
488 if (!is_callsign($key) || $key =~ /^0/) {
491 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
492 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
493 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
494 eval {$dbm->del($key)};
495 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
500 eval {$ref = decode($val); };
502 my $t = $ref->{lastin} || 0;
503 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
504 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
505 eval {$dbm->del($key)};
506 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
507 LogDbg('DXCommand', "$ref->{call} deleted, too old");
512 # only store users that are reasonably active or have useful information
513 print $fh "$key\t" . encode($ref) . "\n";
516 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
517 eval {$dbm->del($key)};
518 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
524 my $diff = _diffms($ta);
525 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors in $diff mS ('sh/log Export' for details)};
526 LogDbg('command', $s);
534 # add one or more groups
538 my $ref = $self->{group} || [ 'local' ];
539 $self->{group} = $ref if !$self->{group};
540 push @$ref, @_ if @_;
543 # remove one or more groups
547 my $ref = $self->{group} || [ 'local' ];
550 $self->{group} = $ref if !$self->{group};
552 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
555 # does this thing contain all the groups listed?
559 my $ref = $self->{group};
562 return 0 if !$ref || @_ == 0;
563 return 1 if @$ref == 0 && @_ == 0;
564 for ($n = 0; $n < @_; ) {
567 $n++ if grep $_ eq $a, @_;
573 # simplified group test just for one group
578 my $ref = $self->{group};
581 return grep $_ eq $s, $ref;
584 # set up a default group (only happens for them's that connect direct)
588 $self->{group} = [ 'local' ];
591 # set up empty buddies (only happens for them's that connect direct)
595 $self->{buddies} = [ ];
599 # return a prompt for a field
604 my ($self, $ele) = @_;
608 # some variable accessors
612 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
617 # want is default = 1
624 $self->{$s} = $val if defined $val;
625 return exists $self->{$s} ? $self->{$s} : 1;
628 # wantnot is default = 0
635 $self->{$s} = $val if defined $val;
636 return exists $self->{$s} ? $self->{$s} : 0;
641 return _want('beep', @_);
646 return _want('ann', @_);
651 return _want('wwv', @_);
656 return _want('wcy', @_);
661 return _want('echo', @_);
666 return _want('wx', @_);
671 return _want('dx', @_);
676 return _want('talk', @_);
681 return _wantnot('grid', @_);
686 return _want('email', @_);
691 return _want('ann_talk', @_);
696 return _want('pc16', @_);
701 return _want('sendpc16', @_);
706 return _want('routepc16', @_);
711 return _want('usstate', @_);
716 return _wantnot('dxcq', @_);
721 return _wantnot('dxitu', @_);
726 return _want('gtk', @_);
731 return _want('pc9x', @_);
738 $self->{wantlogininfo} = $val if defined $val;
739 return $self->{wantlogininfo};
745 return $self->{sort} =~ /^[ACRSX]$/;
751 return grep $_ eq 'local_node', @{$self->{group}};
757 return $self->{sort} =~ /^[UW]$/;
763 return $self->{sort} eq 'W';
769 return $self->{sort} eq 'B';
775 return $self->{sort} eq 'S';
781 return $self->{sort} eq 'C';
787 return $self->{sort} eq 'X';
793 return $self->{sort} eq 'R';
799 return $self->{sort} eq 'A';
805 return $self->{sort} eq 'N'
811 delete $self->{passwd};
817 delete $self->{passphrase};
824 $self->{believe} ||= [];
825 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
832 if (exists $self->{believe}) {
833 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
834 delete $self->{believe} unless @{$self->{believe}};
841 return exists $self->{believe} ? @{$self->{believe}} : ();
848 $self->{lastping} ||= {};
849 $self->{lastping} = {} unless ref $self->{lastping};
850 my $b = $self->{lastping};
851 $b->{$call} = shift if @_;