2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
22 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3);
27 $lastoperinterval = 60*24*60*60;
30 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
33 # hash of valid elements and a simple prompt
35 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
36 addr => '0,Full Address',
37 alias => '0,Real Callsign',
38 annok => '9,Accept Announces?,yesno', # accept his announces?
40 believe => '1,Believable nodes,parray',
41 buddies => '0,Buddies,parray',
44 connlist => '1,Connections,parraydifft',
45 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
46 email => '0,E-mail Address,parray',
47 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
48 hmsgno => '0,Highest Msgno',
49 homenode => '0,Home Node',
50 isolate => '9,Isolate network,yesno',
51 K => '9,Seen on PC92 K,yesno',
53 lastin => '0,Last Time in,cldatetime',
54 lastoper => '9,Last for/oper,cldatetime',
55 lastping => '1,Last Ping at,ptimelist',
56 lastseen => '0,Last Seen,cldatetime',
57 lat => '0,Latitude,slat',
58 lockout => '9,Locked out?,yesno', # won't let them in at all
59 long => '0,Longitude,slong',
60 maxconnect => '1,Max Connections',
62 node => '0,Last Node',
63 nopings => '9,Ping Obs Count',
64 nothere => '0,Not Here Text',
65 pagelth => '0,Current Pagelth',
66 passphrase => '9,Pass Phrase,yesno',
67 passwd => '9,Password,yesno',
68 pingint => '9,Node Ping interval',
69 priv => '9,Privilege Level',
70 prompt => '0,Required Prompt',
73 rbnseeme => '0,RBN See Me',
74 registered => '9,Registered?,yesno',
75 startt => '0,Start Time,cldatetime',
76 version => '1,Version',
77 wantann => '0,Req Announce,yesno',
78 wantann_talk => '0,Talklike Anns,yesno',
79 wantbeacon => '0,Want RBN Beacon,yesno',
80 wantbeep => '0,Req Beep,yesno',
81 wantcw => '0,Want RBN CW,yesno',
82 wantdx => '0,Req DX Spots,yesno',
83 wantdxcq => '0,Show CQ Zone,yesno',
84 wantdxitu => '0,Show ITU Zone,yesno',
85 wantecho => '0,Req Echo,yesno',
86 wantemail => '0,Req Msgs as Email,yesno',
87 wantft => '0,Want RBN FT4/8,yesno',
88 wantgtk => '0,Want GTK interface,yesno',
89 wantlogininfo => '0,Login Info Req,yesno',
90 wantpc16 => '9,Want Users from node,yesno',
91 wantpc9x => '0,Want PC9X interface,yesno',
92 wantpsk => '0,Want RBN PSK,yesno',
93 wantrbn => '0,Want RBN spots,yesno',
94 wantroutepc19 => '9,Route PC19,yesno',
95 wantrtty => '0,Want RBN RTTY,yesno',
96 wantsendpc16 => '9,Send PC16,yesno',
97 wanttalk => '0,Req Talk,yesno',
98 wantusstate => '0,Show US State,yesno',
99 wantwcy => '0,Req WCY,yesno',
100 wantwwv => '0,Req WWV,yesno',
101 wantwx => '0,Req WX,yesno',
102 width => '0,Preferred Width',
103 xpert => '0,Expert Status,yesno',
104 wantgrid => '0,Show DX Grid,yesno',
111 my $name = $AUTOLOAD;
113 return if $name =~ /::DESTROY$/;
116 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
117 # this clever line of code creates a subroutine which takes over from autoload
118 # from OO Perl - Conway
119 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
126 # initialise the system
130 my ($pkg, $fn, $mode) = @_;
132 confess "need a filename in User" if !$fn;
141 # eval "use Storable qw(nfreeze thaw)";
146 dbg("the module Storable appears to be missing!!");
147 dbg("trying to continue in compatibility mode (this may fail)");
148 dbg("please install Storable from CPAN as soon as possible");
150 import Storable qw(nfreeze thaw);
154 $convert++ if -e "$fn.v2" && !-e $ufn;
158 $dbm = tie (%u, 'DB_File', $ufn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
160 $dbm = tie (%u, 'DB_File', $ufn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
163 die "Cannot open $ufn ($!)\n" unless $dbm;
165 $lru = LRU->newbase("DXUser", $lrusize);
167 # do a conversion if required
168 if ($dbm && $convert) {
169 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
172 dbg("Converting the User File to V3 ");
173 dbg("This will take a while, I suggest you go and have cup of strong tea");
174 my $odbm = tie (%oldu, 'DB_File', "$fn.v2", O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v2 ($!) [rebuild it from user_asc?]";
175 for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
176 my $ref = asc_decode($val);
186 dbg("Conversion completed $count records $err errors");
200 confess "need a filename in User" if !$fn;
201 $fn .= $v3 ? ".v3" : ".v2";
206 # periodic processing
210 if ($main::systime > $lasttime + 15) {
212 $lasttime = $main::systime;
222 print "DXUser Finishing\n";
228 # new - create a new user
235 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
243 # $call =~ s/-\d+$//o;
245 # confess "can't create existing call $call in User\n!" if $u{$call};
247 my $self = $pkg->alloc($call);
253 # get - get an existing user - this seems to return a different reference everytime it is
262 # is it in the LRU cache?
263 my $ref = $lru->get($call);
264 return $ref if $ref && ref $ref eq 'DXUser';
267 unless ($dbm->get($call, $data)) {
268 $ref = eval{decode($data)};
270 if (!UNIVERSAL::isa($ref, 'DXUser')) {
271 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
274 # we have a reference and it *is* a DXUser
276 dbg("DXUser::get: no reference returned from decode of $call $! $@");
279 $lru->put($call, $ref);
286 # get an existing either from the channel (if there is one) or from the database
288 # It is important to note that if you have done a get (for the channel say) and you
289 # want access or modify that you must use this call (and you must NOT use get's all
290 # over the place willy nilly!)
297 my $dxchan = DXChannel::get($call);
299 my $ref = $dxchan->user;
300 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
302 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
308 # get all callsigns in the database
313 return (sort keys %u);
323 confess "Trying to put nothing!" unless $self && ref $self;
324 my $call = $self->{call};
327 delete $self->{annok} if $self->{annok};
328 delete $self->{dxok} if $self->{dxok};
330 $lru->put($call, $self);
331 my $ref = $self->encode;
332 $dbm->put($call, $ref);
338 goto &asc_encode unless $v3;
340 return nfreeze($self);
346 goto &asc_decode unless $v3;
351 # create a string from a user reference (in_ascii)
360 my $ref = bless {}, ref $self;
361 foreach my $k (qw(qth lat long qra sort call homenode node lastoper lastin)) {
362 $ref->{$k} = $self->{$k} if exists $self->{$k};
364 $ref->{name} = $self->{name} if exists $self->{name} && $self->{name} !~ /selfspot/i;
373 # create a hash from a string (in ascii)
379 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
389 # del - delete a user
395 my $call = $self->{call};
401 # close - close down a user
407 $self->{lastin} = time;
421 # return a list of valid elements
434 # add one or more groups
438 my $ref = $self->{group} || [ 'local' ];
439 $self->{group} = $ref if !$self->{group};
440 push @$ref, @_ if @_;
443 # remove one or more groups
447 my $ref = $self->{group} || [ 'local' ];
450 $self->{group} = $ref if !$self->{group};
452 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
455 # does this thing contain all the groups listed?
459 my $ref = $self->{group};
462 return 0 if !$ref || @_ == 0;
463 return 1 if @$ref == 0 && @_ == 0;
464 for ($n = 0; $n < @_; ) {
467 $n++ if grep $_ eq $a, @_;
473 # simplified group test just for one group
478 my $ref = $self->{group};
481 return grep $_ eq $s, $ref;
484 # set up a default group (only happens for them's that connect direct)
488 $self->{group} = [ 'local' ];
491 # set up empty buddies (only happens for them's that connect direct)
495 $self->{buddies} = [ ];
499 # return a prompt for a field
504 my ($self, $ele) = @_;
508 # some variable accessors
512 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
517 # want is default = 1
524 $self->{$s} = $val if defined $val;
525 return exists $self->{$s} ? $self->{$s} : 1;
528 # wantnot is default = 0
535 $self->{$s} = $val if defined $val;
536 return exists $self->{$s} ? $self->{$s} : 0;
541 return _want('beep', @_);
546 return _want('ann', @_);
551 return _want('wwv', @_);
556 return _want('wcy', @_);
561 return _want('echo', @_);
566 return _want('wx', @_);
571 return _want('dx', @_);
576 return _want('talk', @_);
581 return _want('grid', @_);
586 return _want('email', @_);
591 return _want('ann_talk', @_);
596 return _want('pc16', @_);
601 return _want('sendpc16', @_);
606 return _want('routepc16', @_);
611 return _want('usstate', @_);
616 return _want('dxcq', @_);
621 return _want('dxitu', @_);
626 return _want('gtk', @_);
631 return _want('pc9x', @_);
638 $self->{wantlogininfo} = $val if defined $val;
639 return $self->{wantlogininfo};
645 return $self->{sort} =~ /[ACRSX]/;
651 return grep $_ eq 'local_node', @{$self->{group}};
657 return $self->{sort} eq 'U';
663 return $self->{sort} eq 'B';
669 return $self->{sort} eq 'S';
675 return $self->{sort} eq 'C';
681 return $self->{sort} eq 'X';
687 return $self->{sort} eq 'R';
693 return $self->{sort} eq 'A';
699 delete $self->{passwd};
705 delete $self->{passphrase};
712 $self->{believe} ||= [];
713 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
720 if (exists $self->{believe}) {
721 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
722 delete $self->{believe} unless @{$self->{believe}};
729 return exists $self->{believe} ? @{$self->{believe}} : ();
736 $self->{lastping} ||= {};
737 $self->{lastping} = {} unless ref $self->{lastping};
738 my $b = $self->{lastping};
739 $b->{$call} = shift if @_;
744 # export the database to an ascii file
750 my $basic_info_only = shift;
753 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
754 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
755 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
756 rename "$fn.o", "$fn.oo" if -e "$fn.o";
757 rename "$fn", "$fn.o" if -e "$fn";
762 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
767 my $t = scalar localtime;
769 print $fh export_preamble();
771 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
772 if (!is_callsign($key) || $key =~ /^0/) {
775 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
776 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
777 LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
778 eval {$dbm->del($key)};
779 dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
783 my $ref = decode($val);
785 my $t = $ref->{lastin} || 0;
786 if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
787 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
788 eval {$dbm->del($key)};
789 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
790 LogDbg('DXCommand', "$ref->{call} deleted, too old");
795 # only store users that are reasonably active or have useful information
796 print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
799 LogDbg('DXCommand', "Export Error3: $key\t$val");
800 eval {$dbm->del($key)};
801 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
807 return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
813 return q{#!/usr/bin/perl
815 # The exported userfile for a DXSpider System
817 # Input file: $filename
823 # search local then perl directories
827 # root of directory tree for this system
829 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
831 unshift @INC, "$root/perl"; # this IS the right way round!
832 unshift @INC, "$root/local";
834 # try to detect a lockfile (this isn't atomic but
836 $lockfn = "$root/local/cluster.lck"; # lock file name
838 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
841 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
852 $main::userfn = shift @ARGV;
853 print "user filename now $userfn\n";
856 DXUser->del_file($main::userfn);
857 DXUser->init($main::userfn, 1);
864 my $ref = asc_decode($f[1]);
869 print "# Error: $f[0]\t$f[1]\n";
873 DXUser->sync; DXUser->finish;
874 print "There are $count user records and $err errors\n";