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 if (-e localdata("$fn.v3") || -e localdata("$fn.v2")) {
144 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
145 system('/spider/perl/convert-users-v3-to-v3j.pl');
151 if (-e $filename || $mode) {
152 $lru = LRU->newbase("DXUser", $lrusize);
154 $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?]";
156 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
159 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
163 # delete files with extreme prejudice
166 # with extreme prejudice
167 unlink "$main::data/users.v3j";
168 unlink "$main::local_data/users.v3j";
172 # periodic processing
176 if ($main::systime > $lasttime + 15) {
178 $lasttime = $main::systime;
194 # new - create a new user
201 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
209 # $call =~ s/-\d+$//o;
211 # confess "can't create existing call $call in User\n!" if $u{$call};
213 my $self = $pkg->alloc($call);
214 $self->{lastseen} = $main::systime;
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') {
232 $ref->{lastseen} = $main::systime;
237 unless ($dbm->get($call, $data)) {
238 eval { $ref = decode($data); };
241 if (!UNIVERSAL::isa($ref, 'DXUser')) {
242 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
245 # we have a reference and it *is* a DXUser
248 LogDbg('err', "DXUser::get decode error on $call '$@'");
250 dbg("DXUser::get: no reference returned from decode of $call $!");
254 $ref->{lastseen} = $main::systime;
255 $lru->put($call, $ref);
262 # get an existing either from the channel (if there is one) or from the database
264 # It is important to note that if you have done a get (for the channel say) and you
265 # want access or modify that you must use this call (and you must NOT use get's all
266 # over the place willy nilly!)
273 my $dxchan = DXChannel::get($call);
275 my $ref = $dxchan->user;
276 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
278 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
284 # get all callsigns in the database
289 return (sort keys %u);
299 confess "Trying to put nothing!" unless $self && ref $self;
300 my $call = $self->{call};
303 delete $self->{annok} if $self->{annok};
304 delete $self->{dxok} if $self->{dxok};
306 $lru->put($call, $self);
307 my $ref = $self->encode;
308 $dbm->put($call, $ref);
315 return $json->decode(shift, __PACKAGE__);
321 return $json->encode(shift);
326 # del - delete a user
332 my $call = $self->{call};
338 # close - close down a user
346 $self->{lastseen} = $self->{lastin} = $main::systime;
347 # add a record to the connect list
348 my $ref = [$startt || $self->{startt}, $main::systime];
349 push @$ref, $ip if $ip;
350 push @{$self->{connlist}}, $ref;
351 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
365 # return a list of valid elements
375 # export the database to an ascii file
380 my $name = shift || 'user_json';
381 my $basic_info_only = shift;
383 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
386 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
387 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
388 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
389 move "$fn.o", "$fn.oo" if -e "$fn.o";
390 move "$fn", "$fn.o" if -e "$fn";
392 my $ta = [gettimeofday];
396 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
401 my $t = scalar localtime;
402 print $fh q{#!/usr/bin/perl
404 # The exported userfile for a DXSpider System
406 # Input file: $filename
412 # search local then perl directories
416 # root of directory tree for this system
418 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
420 unshift @INC, "$root/perl"; # this IS the right way round!
421 unshift @INC, "$root/local";
423 # try to detect a lockfile (this isn't atomic but
425 $lockfn = "$root/local_data/cluster.lck"; # lock file name
427 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
430 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
439 use Time::HiRes qw(gettimeofday tv_interval);
442 our $json = JSON->new->canonical(1);
444 my $ta = [gettimeofday];
445 our $filename = "$main::local_data/users.v3j";
446 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
447 print "perl user_json $exists $filename\n";
457 my $ref = decode($f[1]);
462 print "# Error: $f[0]\t$f[1]\n";
466 DXUser::sync(); DXUser::finish();
467 my $diff = _diffms($ta);
468 print "There are $count user records and $err errors in $diff mS\n";
470 print $fh "__DATA__\n";
472 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
473 if (!is_callsign($key) || $key =~ /^0/) {
476 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
477 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
478 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
479 eval {$dbm->del($key)};
480 dbg(carp("Export Error1: delete $key => '$val' $@")) if $@;
485 eval {$ref = decode($val); };
487 my $t = $ref->{lastin} || 0;
488 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
489 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
490 eval {$dbm->del($key)};
491 dbg(carp("Export Error2: delete '$key' => '$val' $@")) if $@;
492 LogDbg('DXCommand', "$ref->{call} deleted, too old");
497 # only store users that are reasonably active or have useful information
498 print $fh "$key\t" . encode($ref) . "\n";
501 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
502 eval {$dbm->del($key)};
503 dbg(carp("Export Error3: delete '$key' => '$val' $@")) if $@;
509 my $diff = _diffms($ta);
510 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors in $diff mS ('sh/log Export' for details)};
511 LogDbg('command', $s);
519 # add one or more groups
523 my $ref = $self->{group} || [ 'local' ];
524 $self->{group} = $ref if !$self->{group};
525 push @$ref, @_ if @_;
528 # remove one or more groups
532 my $ref = $self->{group} || [ 'local' ];
535 $self->{group} = $ref if !$self->{group};
537 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
540 # does this thing contain all the groups listed?
544 my $ref = $self->{group};
547 return 0 if !$ref || @_ == 0;
548 return 1 if @$ref == 0 && @_ == 0;
549 for ($n = 0; $n < @_; ) {
552 $n++ if grep $_ eq $a, @_;
558 # simplified group test just for one group
563 my $ref = $self->{group};
566 return grep $_ eq $s, $ref;
569 # set up a default group (only happens for them's that connect direct)
573 $self->{group} = [ 'local' ];
576 # set up empty buddies (only happens for them's that connect direct)
580 $self->{buddies} = [ ];
584 # return a prompt for a field
589 my ($self, $ele) = @_;
593 # some variable accessors
597 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
602 # want is default = 1
609 $self->{$s} = $val if defined $val;
610 return exists $self->{$s} ? $self->{$s} : 1;
613 # wantnot is default = 0
620 $self->{$s} = $val if defined $val;
621 return exists $self->{$s} ? $self->{$s} : 0;
626 return _want('beep', @_);
631 return _want('ann', @_);
636 return _want('wwv', @_);
641 return _want('wcy', @_);
646 return _want('echo', @_);
651 return _want('wx', @_);
656 return _want('dx', @_);
661 return _want('talk', @_);
666 return _wantnot('grid', @_);
671 return _want('email', @_);
676 return _want('ann_talk', @_);
681 return _want('pc16', @_);
686 return _want('sendpc16', @_);
691 return _want('routepc16', @_);
696 return _want('usstate', @_);
701 return _wantnot('dxcq', @_);
706 return _wantnot('dxitu', @_);
711 return _want('gtk', @_);
716 return _want('pc9x', @_);
723 $self->{wantlogininfo} = $val if defined $val;
724 return $self->{wantlogininfo};
730 return $self->{sort} =~ /^[ACRSX]$/;
736 return grep $_ eq 'local_node', @{$self->{group}};
742 return $self->{sort} =~ /^[UW]$/;
748 return $self->{sort} eq 'W';
754 return $self->{sort} eq 'B';
760 return $self->{sort} eq 'S';
766 return $self->{sort} eq 'C';
772 return $self->{sort} eq 'X';
778 return $self->{sort} eq 'R';
784 return $self->{sort} eq 'A';
790 return $self->{sort} eq 'N'
796 delete $self->{passwd};
802 delete $self->{passphrase};
809 $self->{believe} ||= [];
810 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
817 if (exists $self->{believe}) {
818 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
819 delete $self->{believe} unless @{$self->{believe}};
826 return exists $self->{believe} ? @{$self->{believe}} : ();
833 $self->{lastping} ||= {};
834 $self->{lastping} = {} unless ref $self->{lastping};
835 my $b = $self->{lastping};
836 $b->{$call} = shift if @_;