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);
28 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $veryold $v3);
33 $lastoperinterval = 60*24*60*60;
36 $tooold = 86400 * 365 * 2; # this marks an old user who hasn't given enough info to be useful
37 $veryold = $tooold * 6; # Ancient default 12 years
39 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
43 # hash of valid elements and a simple prompt
45 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
46 addr => '0,Full Address',
47 alias => '0,Real Callsign',
48 annok => '9,Accept Announces?,yesno', # accept his announces?
50 believe => '1,Believable nodes,parray',
51 buddies => '0,Buddies,parray',
54 clientoutput => '0,User OUT Format',
55 clientinput => '0,User IN Format',
56 connlist => '1,Connections,parraydifft',
57 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
58 email => '0,E-mail Address,parray',
59 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
60 hmsgno => '0,Highest Msgno',
61 homenode => '0,Home Node',
62 isolate => '9,Isolate network,yesno',
63 K => '9,Seen on PC92 K,yesno',
65 lastin => '0,Last Time in,cldatetime',
66 lastoper => '9,Last for/oper,cldatetime',
67 lastping => '1,Last Ping at,ptimelist',
68 lastseen => '0,Last Seen,cldatetime',
69 lat => '0,Latitude,slat',
70 lockout => '9,Locked out?,yesno', # won't let them in at all
71 long => '0,Longitude,slong',
72 maxconnect => '1,Max Connections',
74 node => '0,Last Node',
75 nopings => '9,Ping Obs Count',
76 nothere => '0,Not Here Text',
77 pagelth => '0,Current Pagelth',
78 passphrase => '9,Pass Phrase,yesno',
79 passwd => '9,Password,yesno',
80 pingint => '9,Node Ping interval',
81 priv => '9,Privilege Level',
82 prompt => '0,Required Prompt',
85 rbnseeme => '0,RBN See Me,yesno',
86 registered => '9,Registered?,yesno',
87 startt => '0,Start Time,cldatetime',
88 version => '1,Version',
89 wantann => '0,Req Announce,yesno',
90 wantann_talk => '0,Talklike Anns,yesno',
91 wantbeacon => '0,Want RBN Beacon,yesno',
92 wantbeep => '0,Req Beep,yesno',
93 wantcw => '0,Want RBN CW,yesno',
94 wantdx => '0,Req DX Spots,yesno',
95 wantdxcq => '0,Show CQ Zone,yesno',
96 wantdxitu => '0,Show ITU Zone,yesno',
97 wantecho => '0,Req Echo,yesno',
98 wantemail => '0,Req Msgs as Email,yesno',
99 wantft => '0,Want RBN FT4/8,yesno',
100 wantgtk => '0,Want GTK interface,yesno',
101 wantlogininfo => '0,Login Info Req,yesno',
102 wantpc16 => '9,Want Users from node,yesno',
103 wantpc9x => '0,Want PC9X interface,yesno',
104 wantpsk => '0,Want RBN PSK,yesno',
105 wantrbn => '0,Want RBN spots,yesno',
106 wantroutepc19 => '9,Route PC19,yesno',
107 wantrtty => '0,Want RBN RTTY,yesno',
108 wantsendpc16 => '9,Send PC16,yesno',
109 wanttalk => '0,Req Talk,yesno',
110 wantusstate => '0,Show US State,yesno',
111 wantwcy => '0,Req WCY,yesno',
112 wantwwv => '0,Req WWV,yesno',
113 wantwx => '0,Req WX,yesno',
114 width => '0,Preferred Width',
115 xpert => '0,Expert Status,yesno',
116 wantgrid => '0,Show DX Grid,yesno',
123 my $name = $AUTOLOAD;
125 return if $name =~ /::DESTROY$/;
128 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
129 # this clever line of code creates a subroutine which takes over from autoload
130 # from OO Perl - Conway
131 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
138 # initialise the system
144 $json = DXJSON->new->canonical(1);
146 $filename = localdata("$fn.v3j");
147 unless (-e $filename || $mode == 2 ) {
148 if (-e localdata("$fn.v3") || -e localdata("$fn.v2")) {
149 LogDbg('DXUser', "New User File version $filename does not exist, running conversion from users.v3 or v2, please wait");
150 system('/spider/perl/convert-users-v3-to-v3j.pl');
156 if (-e $filename || $mode) {
157 $lru = LRU->newbase("DXUser", $lrusize);
159 $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?]";
161 $dbm = tie (%u, 'DB_File', $filename, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_json?]";
164 die "Cannot open $filename ($!)\n" unless $dbm || $mode == 2;
168 # delete files with extreme prejudice
171 # with extreme prejudice
172 unlink "$main::data/users.v3j";
173 unlink "$main::local_data/users.v3j";
177 # periodic processing
181 if ($main::systime > $lasttime + 15) {
183 $lasttime = $main::systime;
193 dbg('DXUser finished');
200 # new - create a new user
207 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
215 # $call =~ s/-\d+$//o;
217 # confess "can't create existing call $call in User\n!" if $u{$call};
219 my $self = $pkg->alloc($call);
225 # get - get an existing user - this seems to return a different reference everytime it is
234 # is it in the LRU cache?
235 my $ref = $lru->get($call);
236 if ($ref && ref $ref eq 'DXUser') {
241 unless ($dbm->get($call, $data)) {
242 eval { $ref = decode($data); };
244 if (!UNIVERSAL::isa($ref, 'DXUser')) {
245 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
248 # we have a reference and it *is* a DXUser
251 LogDbg('err', "DXUser::get decode error on $call '$@'");
253 dbg("DXUser::get: no reference returned from decode of $call $!");
257 $lru->put($call, $ref);
264 # get an existing either from the channel (if there is one) or from the database
266 # It is important to note that if you have done a get (for the channel say) and you
267 # want access or modify that you must use this call (and you must NOT use get's all
268 # over the place willy nilly!)
275 my $dxchan = DXChannel::get($call);
277 my $ref = $dxchan->user;
278 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
280 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
286 # get all callsigns in the database
291 return (sort keys %u);
301 confess "Trying to put nothing!" unless $self && ref $self;
302 my $call = $self->{call};
305 delete $self->{annok};
306 delete $self->{dxok};
307 $self->{lastseen} = $main::systime;
308 $lru->put($call, $self);
309 my $ref = $self->encode;
310 $dbm->put($call, $ref);
311 DXChannel::refresh_user($call, $ref);
319 return $json->decode(shift, __PACKAGE__);
325 return $json->encode(shift);
330 # del - delete a user
336 my $call = $self->{call};
342 # close - close down a user
350 # add a record to the connect list
351 $self->{lastin} = $main::systime;
352 my $ref = [$startt || $self->{startt}, $main::systime];
353 push @$ref, $ip if $ip;
354 push @{$self->{connlist}}, $ref;
355 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
369 # return a list of valid elements
382 # add one or more groups
386 my $ref = $self->{group} || [ 'local' ];
387 $self->{group} = $ref if !$self->{group};
388 push @$ref, @_ if @_;
391 # remove one or more groups
395 my $ref = $self->{group} || [ 'local' ];
398 $self->{group} = $ref if !$self->{group};
400 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
403 # does this thing contain all the groups listed?
407 my $ref = $self->{group};
410 return 0 if !$ref || @_ == 0;
411 return 1 if @$ref == 0 && @_ == 0;
412 for ($n = 0; $n < @_; ) {
415 $n++ if grep $_ eq $a, @_;
421 # simplified group test just for one group
426 my $ref = $self->{group};
429 return grep $_ eq $s, $ref;
432 # set up a default group (only happens for them's that connect direct)
436 $self->{group} = [ 'local' ];
439 # set up empty buddies (only happens for them's that connect direct)
443 $self->{buddies} = [ ];
447 # return a prompt for a field
452 my ($self, $ele) = @_;
456 # some variable accessors
460 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
465 # want is default = 1
472 $self->{$s} = $val if defined $val;
473 return exists $self->{$s} ? $self->{$s} : 1;
476 # wantnot is default = 0
483 $self->{$s} = $val if defined $val;
484 return exists $self->{$s} ? $self->{$s} : 0;
489 return _want('beep', @_);
494 return _want('ann', @_);
499 return _want('wwv', @_);
504 return _want('wcy', @_);
509 return _want('echo', @_);
514 return _want('wx', @_);
519 return _want('dx', @_);
524 return _want('talk', @_);
529 return _wantnot('grid', @_);
534 return _want('email', @_);
539 return _want('ann_talk', @_);
544 return _want('pc16', @_);
549 return _want('sendpc16', @_);
554 return _want('routepc16', @_);
559 return _want('usstate', @_);
564 return _wantnot('dxcq', @_);
569 return _wantnot('dxitu', @_);
574 return _want('gtk', @_);
579 return _want('pc9x', @_);
586 $self->{wantlogininfo} = $val if defined $val;
587 return $self->{wantlogininfo};
593 return $self->{sort} =~ /^[ACRSX]$/;
599 return grep $_ eq 'local_node', @{$self->{group}};
605 return $self->{sort} =~ /^[UW]$/;
611 return $self->{sort} eq 'W';
617 return $self->{sort} eq 'B';
623 return $self->{sort} eq 'S';
629 return $self->{sort} eq 'C';
635 return $self->{sort} eq 'X';
641 return $self->{sort} eq 'R';
647 return $self->{sort} eq 'A';
653 return $self->{sort} eq 'N'
659 delete $self->{passwd};
665 delete $self->{passphrase};
672 $self->{believe} ||= [];
673 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
680 if (exists $self->{believe}) {
681 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
682 delete $self->{believe} unless @{$self->{believe}};
689 return exists $self->{believe} ? @{$self->{believe}} : ();
696 $self->{lastping} ||= {};
697 $self->{lastping} = {} unless ref $self->{lastping};
698 my $b = $self->{lastping};
699 $b->{$call} = shift if @_;
705 # export the database to an ascii file
710 my $name = shift || 'user_json';
712 my $fn = $name ne 'user_json' ? $name : "$main::local_data/$name"; # force use of local
715 copy $fn, "$fn.keep" unless -e "$fn.keep";
716 copy "$fn.ooooo", "$fn.backstop" unless -e "$fn,backstop";
718 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
719 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
720 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
721 move "$fn.o", "$fn.oo" if -e "$fn.o";
722 move "$fn", "$fn.o" if -e "$fn";
725 my $ta = [gettimeofday];
738 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
743 my $t = scalar localtime;
744 print $fh export_preamble();
747 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
748 if (!is_callsign($key) || $key =~ /^0/) {
751 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
752 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
753 LogDbg('DXCommand', "Export Error1: invalid call '$key' => '$val'");
760 eval {$ref = decode($val); };
762 my $t = $ref->{lastseen} if exists $ref->{lastseen};
763 $t ||= $ref->{lastin} if exists $ref->{lastin};
764 $t ||= $ref->{lastoper} if exists $ref->{lastoper};
768 if (!$ref->{priv} && $main::systime > $t + $tooold) {
769 unless (($ref->{lat} && $ref->{long}) || $ref->{qth} || $ref->{name} || $ref->{qra}) {
770 LogDbg('DXCommand', sprintf("$ref->{call} deleted, empty and too Old at %s", difft($t, ' ')));
777 if ($main::systime > $t + $veryold) {
778 LogDbg('DXCommand', sprintf("$ref->{call} deleted, POSITIVELY ANCIENT at %s", difft($t, ' ')));
784 if (exists $ref->{lockout} && $ref->{lockout} == 1 && exists $ref->{priv} && $ref->{priv} == 1) {
785 LogDbg('DXCommand', "$ref->{call} depriv'd and unlocked");
786 $ref->{lockout} = $ref->{priv} = 0;
790 if ($ref->is_node && $main::systime > $t + $veryold) {
791 LogDbg('DXCommand', sprintf("NODE $ref->{call} deleted (%s) old", difft($t, ' ')));
798 my $normcall = normalise_call($key);
799 if ($normcall ne $key) {
800 # if the normalised call does not exist, create it from the duff call.
801 my $nref = DXUser::get_current($normcall);
803 $ref->{call} = $normcall;
805 LogDbg('DXCommand', "DXProt: spurious call $key normalises to $normcall renaming $key -> $normcall");
808 LogDbg('DXCommand', "DXProt: spurious call $key (should be $normcall), removing");
816 LogDbg('DXCommand', "Export Error3: '$key'\t" . carp($val) ."\n$@");
822 # only store users that are reasonably active or have useful information
823 print $fh "$key\t" . encode($ref) . "\n";
829 while (my ($k, $v) = each %del) {
830 eval {$dbm->del($k)};
831 LogDbg('DXCommand', "Error deleting key: $k value: $v error: $@") if $@;
834 my $diff = _diffms($ta);
835 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)};
836 LogDbg('command', $s);
842 return q{#!/usr/bin/perl
844 # The exported userfile for a DXSpider System
846 # Input file: $filename
852 # search local then perl directories
856 # root of directory tree for this system
858 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
860 unshift @INC, "$root/perl"; # this IS the right way round!
861 unshift @INC, "$root/local";
863 # try to detect a lockfile (this isn't atomic but
865 $lockfn = "$root/local_data/cluster.lck"; # lock file name
867 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
870 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
880 use Time::HiRes qw(gettimeofday tv_interval);
883 our $json = JSON->new->canonical(1);
885 my $ta = [gettimeofday];
886 our $filename = "$main::local_data/users.v3j";
887 my $exists = -e $filename ? "OVERWRITING" : "CREATING";
888 print "perl user_json $exists $filename\n";
899 my $ref = decode($f[1]);
904 print "# Error: $f[0]\t$f[1]\n";
908 DXUser::sync(); DXUser::finish();
909 my $diff = _diffms($ta);
910 print "There are $count user records and $err errors in $diff mS\n";
921 my $name = shift || 'recover_json';
923 my $fn = $name ne 'recover_json' ? $name : "$main::local_data/$name"; # force use of local
926 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
927 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
928 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
929 move "$fn.o", "$fn.oo" if -e "$fn.o";
930 move "$fn", "$fn.o" if -e "$fn";
932 my $ta = [gettimeofday];
937 my $strings = "strings $filename";
938 my $ifh = new IO::File "$strings |" or return "cannot open input $filename ($!)";
939 my $fh = new IO::File ">$fn" or return "cannot open output $fn ($!)";
944 my $t = scalar localtime;
945 print $fh export_preamble();
951 while (defined ($l = $ifh->getline)) {
952 next unless $l =~ /^{"call":"[-\d\w\/]+"/;
959 eval{ $v = decode($data); };
964 next if $data eq $last;
965 print $fh "$v->{call}\t$l\n";
976 my $diff = _diffms($ta);
977 my $s = qq{Recovered users to $fn - $count Users, $errs errors $total possible records read in $diff mS ('sh/log recover' for details)};
978 LogDbg('command', $s);
985 print "DXUser Ended\n";