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',
111 rbnseeme => '0,RBN See Me',
118 my $name = $AUTOLOAD;
120 return if $name =~ /::DESTROY$/;
123 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
124 # this clever line of code creates a subroutine which takes over from autoload
125 # from OO Perl - Conway
126 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
133 # initialise the system
139 $json = DXJSON->new->canonical(1);
141 $filename = localdata("$fn.v3j");
142 unless (-e $filename || $mode == 2) {
143 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
144 system('/spider/perl/convert-users-v3-to-v3j.pl');
149 if (-e $filename || $mode == 2) {
150 $lru = LRU->newbase("DXUser", $lrusize);
152 $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?]";
154 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
157 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
161 # delete files with extreme prejudice
164 # with extreme prejudice
165 unlink "$main::data/users.v3j";
166 unlink "$main::local_data/users.v3j";
170 # periodic processing
174 if ($main::systime > $lasttime + 15) {
176 $lasttime = $main::systime;
192 # new - create a new user
199 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
207 # $call =~ s/-\d+$//o;
209 # confess "can't create existing call $call in User\n!" if $u{$call};
211 my $self = $pkg->alloc($call);
212 $self->{lastseen} = $main::systime;
218 # get - get an existing user - this seems to return a different reference everytime it is
227 # is it in the LRU cache?
228 my $ref = $lru->get($call);
229 if ($ref && ref $ref eq 'DXUser') {
230 $ref->{lastseen} = $main::systime;
235 unless ($dbm->get($call, $data)) {
236 eval { $ref = decode($data); };
239 if (!UNIVERSAL::isa($ref, 'DXUser')) {
240 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
243 # we have a reference and it *is* a DXUser
246 LogDbg('err', "DXUser::get decode error on $call '$@'");
248 dbg("DXUser::get: no reference returned from decode of $call $!");
252 $ref->{lastseen} = $main::systime;
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} if $self->{annok};
302 delete $self->{dxok} if $self->{dxok};
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 $self->{lastseen} = $self->{lastin} = $main::systime;
345 # add a record to the connect list
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 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
385 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
386 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
387 move "$fn.o", "$fn.oo" if -e "$fn.o";
388 move "$fn", "$fn.o" if -e "$fn";
390 my $ta = [gettimeofday];
394 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
399 my $t = scalar localtime;
400 print $fh q{#!/usr/bin/perl
402 # The exported userfile for a DXSpider System
404 # Input file: $filename
410 # search local then perl directories
414 # root of directory tree for this system
416 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
418 unshift @INC, "$root/perl"; # this IS the right way round!
419 unshift @INC, "$root/local";
421 # try to detect a lockfile (this isn't atomic but
423 $lockfn = "$root/local_data/cluster.lck"; # lock file name
425 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
428 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
437 use Time::HiRes qw(gettimeofday tv_interval);
440 our $json = JSON->new->canonical(1);
442 my $ta = [gettimeofday];
443 our $filename = "$main::local_data/users.v3j";
444 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
445 print "perl user_json $exists $filename\n";
455 my $ref = decode($f[1]);
460 print "# Error: $f[0]\t$f[1]\n";
464 DXUser::sync(); DXUser::finish();
465 my $diff = _diffms($ta);
466 print "There are $count user records and $err errors in $diff mS\n";
468 print $fh "__DATA__\n";
470 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
471 if (!is_callsign($key) || $key =~ /^0/) {
474 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
475 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
476 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
477 eval {$dbm->del($key)};
478 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
483 eval {$ref = decode($val); };
485 my $t = $ref->{lastin} || 0;
486 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
487 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
488 eval {$dbm->del($key)};
489 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
490 LogDbg('DXCommand', "$ref->{call} deleted, too old");
495 # only store users that are reasonably active or have useful information
496 print $fh "$key\t" . encode($ref) . "\n";
499 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
500 eval {$dbm->del($key)};
501 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
507 my $diff = _diffms($ta);
508 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors in $diff mS ('sh/log Export' for details)};
509 LogDbg('command', $s);
517 # add one or more groups
521 my $ref = $self->{group} || [ 'local' ];
522 $self->{group} = $ref if !$self->{group};
523 push @$ref, @_ if @_;
526 # remove one or more groups
530 my $ref = $self->{group} || [ 'local' ];
533 $self->{group} = $ref if !$self->{group};
535 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
538 # does this thing contain all the groups listed?
542 my $ref = $self->{group};
545 return 0 if !$ref || @_ == 0;
546 return 1 if @$ref == 0 && @_ == 0;
547 for ($n = 0; $n < @_; ) {
550 $n++ if grep $_ eq $a, @_;
556 # simplified group test just for one group
561 my $ref = $self->{group};
564 return grep $_ eq $s, $ref;
567 # set up a default group (only happens for them's that connect direct)
571 $self->{group} = [ 'local' ];
574 # set up empty buddies (only happens for them's that connect direct)
578 $self->{buddies} = [ ];
582 # return a prompt for a field
587 my ($self, $ele) = @_;
591 # some variable accessors
595 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
600 # want is default = 1
607 $self->{$s} = $val if defined $val;
608 return exists $self->{$s} ? $self->{$s} : 1;
611 # wantnot is default = 0
618 $self->{$s} = $val if defined $val;
619 return exists $self->{$s} ? $self->{$s} : 0;
624 return _want('beep', @_);
629 return _want('ann', @_);
634 return _want('wwv', @_);
639 return _want('wcy', @_);
644 return _want('echo', @_);
649 return _want('wx', @_);
654 return _want('dx', @_);
659 return _want('talk', @_);
664 return _wantnot('grid', @_);
669 return _want('email', @_);
674 return _want('ann_talk', @_);
679 return _want('pc16', @_);
684 return _want('sendpc16', @_);
689 return _want('routepc16', @_);
694 return _want('usstate', @_);
699 return _wantnot('dxcq', @_);
704 return _wantnot('dxitu', @_);
709 return _want('gtk', @_);
714 return _want('pc9x', @_);
721 $self->{wantlogininfo} = $val if defined $val;
722 return $self->{wantlogininfo};
728 return $self->{sort} =~ /^[ACRSX]$/;
734 return grep $_ eq 'local_node', @{$self->{group}};
740 return $self->{sort} =~ /^[UW]$/;
746 return $self->{sort} eq 'W';
752 return $self->{sort} eq 'B';
758 return $self->{sort} eq 'S';
764 return $self->{sort} eq 'C';
770 return $self->{sort} eq 'X';
776 return $self->{sort} eq 'R';
782 return $self->{sort} eq 'A';
788 return $self->{sort} eq 'N'
794 delete $self->{passwd};
800 delete $self->{passphrase};
807 $self->{believe} ||= [];
808 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
815 if (exists $self->{believe}) {
816 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
817 delete $self->{believe} unless @{$self->{believe}};
824 return exists $self->{believe} ? @{$self->{believe}} : ();
831 $self->{lastping} ||= {};
832 $self->{lastping} = {} unless ref $self->{lastping};
833 my $b = $self->{lastping};
834 $b->{$call} = shift if @_;