2 # DX cluster user routines
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
23 use vars qw(%u $dbm $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3);
28 $lastoperinterval = 60*24*60*60;
31 $tooold = 86400 * 365; # this marks an old user who hasn't given enough info to be useful
33 our $maxconnlist = 3; # remember this many connection time (duration) [start, end] pairs
35 # hash of valid elements and a simple prompt
38 alias => '0,Real Callsign',
41 lat => '0,Latitude,slat',
42 long => '0,Longitude,slong',
44 email => '0,E-mail Address,parray',
45 priv => '9,Privilege Level',
46 lastin => '0,Last Time in,cldatetime',
47 passwd => '9,Password,yesno',
48 passphrase => '9,Pass Phrase,yesno',
49 addr => '0,Full Address',
50 'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
51 xpert => '0,Expert Status,yesno',
53 node => '0,Last Node',
54 homenode => '0,Home Node',
55 lockout => '9,Locked out?,yesno', # won't let them in at all
56 dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
57 annok => '9,Accept Announces?,yesno', # accept his announces?
59 hmsgno => '0,Highest Msgno',
60 group => '0,Group,parray', # used to create a group of users/nodes for some purpose or other
61 buddies => '0,Buddies,parray',
62 isolate => '9,Isolate network,yesno',
63 wantbeep => '0,Req Beep,yesno',
64 wantann => '0,Req Announce,yesno',
65 wantwwv => '0,Req WWV,yesno',
66 wantwcy => '0,Req WCY,yesno',
67 wantecho => '0,Req Echo,yesno',
68 wanttalk => '0,Req Talk,yesno',
69 wantwx => '0,Req WX,yesno',
70 wantdx => '0,Req DX Spots,yesno',
71 wantemail => '0,Req Msgs as Email,yesno',
72 pagelth => '0,Current Pagelth',
73 pingint => '9,Node Ping interval',
74 nopings => '9,Ping Obs Count',
75 wantlogininfo => '0,Login Info Req,yesno',
76 wantgrid => '0,Show DX Grid,yesno',
77 wantann_talk => '0,Talklike Anns,yesno',
78 wantpc16 => '9,Want Users from node,yesno',
79 wantsendpc16 => '9,Send PC16,yesno',
80 wantroutepc19 => '9,Route PC19,yesno',
81 wantusstate => '0,Show US State,yesno',
82 wantdxcq => '0,Show CQ Zone,yesno',
83 wantdxitu => '0,Show ITU Zone,yesno',
84 wantgtk => '0,Want GTK interface,yesno',
85 wantpc9x => '0,Want PC9X interface,yesno',
86 wantrbn => '0,Want RBN spots,yesno',
87 wantft => '0,Want FT4/8 spots,yesno',
88 wantcw => '0,Want (RBN) CW spots,yesno',
89 lastoper => '9,Last for/oper,cldatetime',
90 nothere => '0,Not Here Text',
91 registered => '9,Registered?,yesno',
92 prompt => '0,Required Prompt',
93 version => '1,Version',
95 believe => '1,Believable nodes,parray',
96 lastping => '1,Last Ping at,ptimelist',
97 maxconnect => '1,Max Connections',
98 startt => '0,Start Time,cldatetime',
99 connlist => '1,Connections,parraydifft',
106 my $name = $AUTOLOAD;
108 return if $name =~ /::DESTROY$/;
111 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
112 # this clever line of code creates a subroutine which takes over from autoload
113 # from OO Perl - Conway
114 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
121 # initialise the system
137 $ufn = localdata("users.v2");
139 dbg("the module Storable appears to be missing!!");
140 dbg("trying to continue in compatibility mode (this may fail)");
141 dbg("please install Storable from CPAN as soon as possible");
143 import Storable qw(nfreeze thaw);
145 $ufn = localdata("users.v3");
147 $convert++ if -e localdata("users.v2") && !-e $ufn;
151 $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?]";
153 $dbm = tie (%u, 'DB_File', $ufn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
156 die "Cannot open $ufn ($!)\n" unless $dbm;
158 $lru = LRU->newbase("DXUser", $lrusize);
160 # do a conversion if required
161 if ($dbm && $convert) {
162 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
165 dbg("Converting the User File to V3 ");
166 dbg("This will take a while, I suggest you go and have cup of strong tea");
167 my $odbm = tie (%oldu, 'DB_File', localdata("users.v2"), O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v2 ($!) [rebuild it from user_asc?]";
168 for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
170 eval { $ref = asc_decode($val) };
179 Log('err', "DXUser: error decoding $@");
184 dbg("Conversion completed $count records $err errors");
191 # with extreme prejudice
192 unlink "$main::data/users.v3";
193 unlink "$main::local_data/users.v3";
197 # periodic processing
201 if ($main::systime > $lasttime + 15) {
203 $lasttime = $main::systime;
218 # new - create a new user
225 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
233 # $call =~ s/-\d+$//o;
235 # confess "can't create existing call $call in User\n!" if $u{$call};
237 my $self = $pkg->alloc($call);
243 # get - get an existing user - this seems to return a different reference everytime it is
252 # is it in the LRU cache?
253 my $ref = $lru->get($call);
254 return $ref if $ref && ref $ref eq 'DXUser';
257 unless ($dbm->get($call, $data)) {
258 eval { $ref = decode($data); };
261 if (!UNIVERSAL::isa($ref, 'DXUser')) {
262 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
265 # we have a reference and it *is* a DXUser
268 LogDbg('err', "DXUser::get decode error on $call '$@'");
270 dbg("DXUser::get: no reference returned from decode of $call $!");
274 $lru->put($call, $ref);
281 # get an existing either from the channel (if there is one) or from the database
283 # It is important to note that if you have done a get (for the channel say) and you
284 # want access or modify that you must use this call (and you must NOT use get's all
285 # over the place willy nilly!)
292 my $dxchan = DXChannel::get($call);
294 my $ref = $dxchan->user;
295 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
297 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
303 # get all callsigns in the database
308 return (sort keys %u);
318 confess "Trying to put nothing!" unless $self && ref $self;
319 my $call = $self->{call};
322 delete $self->{annok} if $self->{annok};
323 delete $self->{dxok} if $self->{dxok};
325 $lru->put($call, $self);
326 my $ref = $self->encode;
327 $dbm->put($call, $ref);
333 goto &asc_encode unless $v3;
335 return nfreeze($self);
341 goto &asc_decode unless $v3;
348 # create a string from a user reference (in_ascii)
357 my $ref = bless {}, ref $self;
358 foreach my $k (qw(qth lat long qra sort call homenode node lastoper lastin)) {
359 $ref->{$k} = $self->{$k} if exists $self->{$k};
361 $ref->{name} = $self->{name} if exists $self->{name} && $self->{name} !~ /selfspot/i;
370 # create a hash from a string (in ascii)
376 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
379 LogDbg('err', "DXUser::asc_decode: on '$s' $@");
386 # del - delete a user
392 my $call = $self->{call};
398 # close - close down a user
406 $self->{lastin} = $main::systime;
407 # add a record to the connect list
408 my $ref = [$startt || $self->{startt}, $main::systime];
409 push @$ref, $ip if $ip;
410 push @{$self->{connlist}}, $ref;
411 shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
425 # return a list of valid elements
435 # export the database to an ascii file
440 my $name = shift || 'user_asc';
441 my $basic_info_only = shift;
443 my $fn = $name ne 'user_asc' ? $name : "$main::local_data/$name"; # force use of local
446 move "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
447 move "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
448 move "$fn.oo", "$fn.ooo" if -e "$fn.oo";
449 move "$fn.o", "$fn.oo" if -e "$fn.o";
450 move "$fn", "$fn.o" if -e "$fn";
455 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
460 my $t = scalar localtime;
461 print $fh q{#!/usr/bin/perl
463 # The exported userfile for a DXSpider System
465 # Input file: $filename
471 # search local then perl directories
475 # root of directory tree for this system
477 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
479 unshift @INC, "$root/perl"; # this IS the right way round!
480 unshift @INC, "$root/local";
482 # try to detect a lockfile (this isn't atomic but
484 $lockfn = "$root/local_data/cluster.lck"; # lock file name
486 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
489 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
498 $main::userfn = shift @ARGV;
499 print "user filename now $userfn\n";
512 my $ref = asc_decode($f[1]);
516 DXUser::sync() unless $count % 10000;
518 print "# Error: $f[0]\t$f[1]\n";
522 DXUser::sync(); DXUser::finish();
523 print "There are $count user records and $err errors\n";
525 print $fh "__DATA__\n";
527 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
528 if (!is_callsign($key) || $key =~ /^0/) {
531 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
532 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
533 LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
534 eval {$dbm->del($key)};
535 dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
540 eval {$ref = decode($val); };
542 my $t = $ref->{lastin} || 0;
543 if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
544 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
545 eval {$dbm->del($key)};
546 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
547 LogDbg('DXCommand', "$ref->{call} deleted, too old");
552 # only store users that are reasonably active or have useful information
553 print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
556 LogDbg('DXCommand', "Export Error3: $key\t" . carp($val) ."\n$@");
557 eval {$dbm->del($key)};
558 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
564 my $s = qq{Exported users to $fn - $count Users $del Deleted $err Errors ('sh/log Export' for details)};
565 LogDbg('command', $s);
573 # add one or more groups
577 my $ref = $self->{group} || [ 'local' ];
578 $self->{group} = $ref if !$self->{group};
579 push @$ref, @_ if @_;
582 # remove one or more groups
586 my $ref = $self->{group} || [ 'local' ];
589 $self->{group} = $ref if !$self->{group};
591 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
594 # does this thing contain all the groups listed?
598 my $ref = $self->{group};
601 return 0 if !$ref || @_ == 0;
602 return 1 if @$ref == 0 && @_ == 0;
603 for ($n = 0; $n < @_; ) {
606 $n++ if grep $_ eq $a, @_;
612 # simplified group test just for one group
617 my $ref = $self->{group};
620 return grep $_ eq $s, $ref;
623 # set up a default group (only happens for them's that connect direct)
627 $self->{group} = [ 'local' ];
630 # set up empty buddies (only happens for them's that connect direct)
634 $self->{buddies} = [ ];
638 # return a prompt for a field
643 my ($self, $ele) = @_;
647 # some variable accessors
651 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
656 # want is default = 1
663 $self->{$s} = $val if defined $val;
664 return exists $self->{$s} ? $self->{$s} : 1;
667 # wantnot is default = 0
674 $self->{$s} = $val if defined $val;
675 return exists $self->{$s} ? $self->{$s} : 0;
680 return _want('beep', @_);
685 return _want('ann', @_);
690 return _want('wwv', @_);
695 return _want('wcy', @_);
700 return _want('echo', @_);
705 return _want('wx', @_);
710 return _want('dx', @_);
715 return _want('talk', @_);
720 return _want('grid', @_);
725 return _want('email', @_);
730 return _want('ann_talk', @_);
735 return _want('pc16', @_);
740 return _want('sendpc16', @_);
745 return _want('routepc16', @_);
750 return _want('usstate', @_);
755 return _want('dxcq', @_);
760 return _want('dxitu', @_);
765 return _want('gtk', @_);
770 return _want('pc9x', @_);
777 $self->{wantlogininfo} = $val if defined $val;
778 return $self->{wantlogininfo};
784 return $self->{sort} =~ /^[ACRSX]$/;
790 return grep $_ eq 'local_node', @{$self->{group}};
796 return $self->{sort} =~ /^[UW]$/;
802 return $self->{sort} eq 'W';
808 return $self->{sort} eq 'B';
814 return $self->{sort} eq 'S';
820 return $self->{sort} eq 'C';
826 return $self->{sort} eq 'X';
832 return $self->{sort} eq 'R';
838 return $self->{sort} eq 'A';
844 return $self->{sort} eq 'N'
850 delete $self->{passwd};
856 delete $self->{passphrase};
863 $self->{believe} ||= [];
864 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
871 if (exists $self->{believe}) {
872 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
873 delete $self->{believe} unless @{$self->{believe}};
880 return exists $self->{believe} ? @{$self->{believe}} : ();
887 $self->{lastping} ||= {};
888 $self->{lastping} = {} unless ref $self->{lastping};
889 my $b = $self->{lastping};
890 $b->{$call} = shift if @_;