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);
26 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3);
31 $lastoperinterval = 60*24*60*60;
34 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
36 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
40 # hash of valid elements and a simple prompt
43 alias => '0,Real Callsign',
46 lat => '0,Latitude,slat',
47 long => '0,Longitude,slong',
49 email => '0,E-mail Address,parray',
50 priv => '9,Privilege Level',
51 lastin => '0,Last Time in,cldatetime',
52 lastseen => '0,Last Seen,cldatetime',
53 passwd => '9,Password,yesno',
54 passphrase => '9,Pass Phrase,yesno',
55 addr => '0,Full Address',
56 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
57 xpert => '0,Expert Status,yesno',
59 node => '0,Last Node',
60 homenode => '0,Home Node',
61 lockout => '9,Locked out?,yesno', # won't let them in at all
62 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
63 annok => '9,Accept Announces?,yesno', # accept his announces?
65 hmsgno => '0,Highest Msgno',
66 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
67 buddies => '0,Buddies,parray',
68 isolate => '9,Isolate network,yesno',
69 wantbeep => '0,Req Beep,yesno',
70 wantann => '0,Req Announce,yesno',
71 wantwwv => '0,Req WWV,yesno',
72 wantwcy => '0,Req WCY,yesno',
73 wantecho => '0,Req Echo,yesno',
74 wanttalk => '0,Req Talk,yesno',
75 wantwx => '0,Req WX,yesno',
76 wantdx => '0,Req DX Spots,yesno',
77 wantemail => '0,Req Msgs as Email,yesno',
78 pagelth => '0,Current Pagelth',
79 pingint => '9,Node Ping interval',
80 nopings => '9,Ping Obs Count',
81 wantlogininfo => '0,Login Info Req,yesno',
82 wantgrid => '0,Show DX Grid,yesno',
83 wantann_talk => '0,Talklike Anns,yesno',
84 wantpc16 => '9,Want Users from node,yesno',
85 wantsendpc16 => '9,Send PC16,yesno',
86 wantroutepc19 => '9,Route PC19,yesno',
87 wantusstate => '0,Show US State,yesno',
88 wantdxcq => '0,Show CQ Zone,yesno',
89 wantdxitu => '0,Show ITU Zone,yesno',
90 wantgtk => '0,Want GTK interface,yesno',
91 wantpc9x => '0,Want PC9X interface,yesno',
92 wantrbn => '0,Want RBN spots,yesno',
93 wantft => '0,Want RBN FT4/8,yesno',
94 wantcw => '0,Want RBN CW,yesno',
95 wantrtty => '0,Want RBN RTTY,yesno',
96 wantpsk => '0,Want RBN PSK,yesno',
97 wantbeacon => '0,Want (RBN) Beacon,yesno',
98 lastoper => '9,Last for/oper,cldatetime',
99 nothere => '0,Not Here Text',
100 registered => '9,Registered?,yesno',
101 prompt => '0,Required Prompt',
102 version => '1,Version',
104 believe => '1,Believable nodes,parray',
105 lastping => '1,Last Ping at,ptimelist',
106 maxconnect => '1,Max Connections',
107 startt => '0,Start Time,cldatetime',
108 connlist => '1,Connections,parraydifft',
115 my $name = $AUTOLOAD;
117 return if $name =~ /::DESTROY$/;
120 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
121 # this clever line of code creates a subroutine which takes over from autoload
122 # from OO Perl - Conway
123 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
130 # initialise the system
136 $json = JSON->new->canonical(1);
138 $filename = localdata("$fn.v3j");
139 unless (-e $filename || $mode == 2) {
140 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
141 system('/spider/perl/convert-users-v3-to-v3j.pl');
146 if (-e $filename || $mode == 2) {
147 $lru = LRU->newbase("DXUser", $lrusize);
149 $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?]";
151 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
154 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
158 # delete files with extreme prejudice
161 # with extreme prejudice
162 unlink "$main::data/users.v3j";
163 unlink "$main::local_data/users.v3j";
167 # periodic processing
171 if ($main::systime > $lasttime + 15) {
173 $lasttime = $main::systime;
189 # new - create a new user
196 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
204 # $call =~ s/-\d+$//o;
206 # confess "can't create existing call $call in User\n!" if $u{$call};
208 my $self = $pkg->alloc($call);
209 $self->{lastseen} = $main::systime;
215 # get - get an existing user - this seems to return a different reference everytime it is
224 # is it in the LRU cache?
225 my $ref = $lru->get($call);
226 if ($ref && ref $ref eq 'DXUser') {
227 $ref->{lastseen} = $main::systime;
232 unless ($dbm->get($call, $data)) {
233 eval { $ref = decode($data); };
236 if (!UNIVERSAL::isa($ref, 'DXUser')) {
237 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
240 # we have a reference and it *is* a DXUser
243 LogDbg('err', "DXUser::get decode error on $call '$@'");
245 dbg("DXUser::get: no reference returned from decode of $call $!");
249 $ref->{lastseen} = $main::systime;
250 $lru->put($call, $ref);
257 # get an existing either from the channel (if there is one) or from the database
259 # It is important to note that if you have done a get (for the channel say) and you
260 # want access or modify that you must use this call (and you must NOT use get's all
261 # over the place willy nilly!)
268 my $dxchan = DXChannel::get($call);
270 my $ref = $dxchan->user;
271 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
273 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
279 # get all callsigns in the database
284 return (sort keys %u);
294 confess "Trying to put nothing!" unless $self && ref $self;
295 my $call = $self->{call};
298 delete $self->{annok} if $self->{annok};
299 delete $self->{dxok} if $self->{dxok};
301 $lru->put($call, $self);
302 my $ref = $self->encode;
303 $dbm->put($call, $ref);
312 eval { $ref = $json->decode($s) };
314 return bless $ref, 'DXUser';
316 LogDbg('DXUser', "DXUser::json_decode: on '$s' $@");
328 eval {$s = $json->encode($ref) };
330 bless $ref, 'DXUser';
333 LogDbg('DXUser', "DXUser::json_encode $ref->{call}, $@");
339 # del - delete a user
345 my $call = $self->{call};
351 # close - close down a user
359 $self->{lastseen} = $self->{lastin} = $main::systime;
360 # add a record to the connect list
361 my $ref = [$startt || $self->{startt}, $main::systime];
362 push @$ref, $ip if $ip;
363 push @{$self->{connlist}}, $ref;
364 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
378 # return a list of valid elements
388 # export the database to an ascii file
393 my $name = shift || 'user_json';
394 my $basic_info_only = shift;
396 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
399 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
400 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
401 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
402 move "$fn.o", "$fn.oo" if -e "$fn.o";
403 move "$fn", "$fn.o" if -e "$fn";
405 my $ta = [gettimeofday];
409 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
414 my $t = scalar localtime;
415 print $fh q{#!/usr/bin/perl
417 # The exported userfile for a DXSpider System
419 # Input file: $filename
425 # search local then perl directories
429 # root of directory tree for this system
431 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
433 unshift @INC, "$root/perl"; # this IS the right way round!
434 unshift @INC, "$root/local";
436 # try to detect a lockfile (this isn't atomic but
438 $lockfn = "$root/local_data/cluster.lck"; # lock file name
440 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
443 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
452 use Time::HiRes qw(gettimeofday tv_interval);
455 our $json = JSON->new->canonical(1);
457 my $ta = [gettimeofday];
458 our $filename = "$main::local_data/users.v3j";
459 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
460 print "perl user_json $exists $filename\n";
470 my $ref = decode($f[1]);
475 print "# Error: $f[0]\t$f[1]\n";
479 DXUser::sync(); DXUser::finish();
480 my $diff = _diffms($ta);
481 print "There are $count user records and $err errors in $diff mS\n";
483 print $fh "__DATA__\n";
485 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
486 if (!is_callsign($key) || $key =~ /^0/) {
489 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
490 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
491 LogDbg('DXCommand', "Export Error1: invalid callsign($ekey) => '$eval'");
492 eval {$dbm->del($key)};
493 dbg(carp("Export Error1: delete call $ekey => '$eval' $@")) if $@;
498 eval {$ref = decode($val); };
500 my $t = $ref->{lastin} || 0;
501 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
502 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
503 eval {$dbm->del($key)};
504 dbg(carp("Export Error2: delete $key => '$val' $@")) if $@;
505 LogDbg('DXCommand', "$ref->{call} deleted, too old");
510 # only store users that are reasonably active or have useful information
511 print $fh "$key\t" . encode($ref) . "\n";
514 LogDbg('DXCommand', "Export Error3: $key\t" . carp($val) ."\n$@");
515 eval {$dbm->del($key)};
516 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
522 my $diff = _diffms($ta);
523 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors in $diff mS ('sh/log Export' for details)};
524 LogDbg('command', $s);
532 # add one or more groups
536 my $ref = $self->{group} || [ 'local' ];
537 $self->{group} = $ref if !$self->{group};
538 push @$ref, @_ if @_;
541 # remove one or more groups
545 my $ref = $self->{group} || [ 'local' ];
548 $self->{group} = $ref if !$self->{group};
550 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
553 # does this thing contain all the groups listed?
557 my $ref = $self->{group};
560 return 0 if !$ref || @_ == 0;
561 return 1 if @$ref == 0 && @_ == 0;
562 for ($n = 0; $n < @_; ) {
565 $n++ if grep $_ eq $a, @_;
571 # simplified group test just for one group
576 my $ref = $self->{group};
579 return grep $_ eq $s, $ref;
582 # set up a default group (only happens for them's that connect direct)
586 $self->{group} = [ 'local' ];
589 # set up empty buddies (only happens for them's that connect direct)
593 $self->{buddies} = [ ];
597 # return a prompt for a field
602 my ($self, $ele) = @_;
606 # some variable accessors
610 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
615 # want is default = 1
622 $self->{$s} = $val if defined $val;
623 return exists $self->{$s} ? $self->{$s} : 1;
626 # wantnot is default = 0
633 $self->{$s} = $val if defined $val;
634 return exists $self->{$s} ? $self->{$s} : 0;
639 return _want('beep', @_);
644 return _want('ann', @_);
649 return _want('wwv', @_);
654 return _want('wcy', @_);
659 return _want('echo', @_);
664 return _want('wx', @_);
669 return _want('dx', @_);
674 return _want('talk', @_);
679 return _want('grid', @_);
684 return _want('email', @_);
689 return _want('ann_talk', @_);
694 return _want('pc16', @_);
699 return _want('sendpc16', @_);
704 return _want('routepc16', @_);
709 return _want('usstate', @_);
714 return _want('dxcq', @_);
719 return _want('dxitu', @_);
724 return _want('gtk', @_);
729 return _want('pc9x', @_);
736 $self->{wantlogininfo} = $val if defined $val;
737 return $self->{wantlogininfo};
743 return $self->{sort} =~ /^[ACRSX]$/;
749 return grep $_ eq 'local_node', @{$self->{group}};
755 return $self->{sort} =~ /^[UW]$/;
761 return $self->{sort} eq 'W';
767 return $self->{sort} eq 'B';
773 return $self->{sort} eq 'S';
779 return $self->{sort} eq 'C';
785 return $self->{sort} eq 'X';
791 return $self->{sort} eq 'R';
797 return $self->{sort} eq 'A';
803 return $self->{sort} eq 'N'
809 delete $self->{passwd};
815 delete $self->{passphrase};
822 $self->{believe} ||= [];
823 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
830 if (exists $self->{believe}) {
831 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
832 delete $self->{believe} unless @{$self->{believe}};
839 return exists $self->{believe} ? @{$self->{believe}} : ();
846 $self->{lastping} ||= {};
847 $self->{lastping} = {} unless ref $self->{lastping};
848 my $b = $self->{lastping};
849 $b->{$call} = shift if @_;