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");
195 confess "need a filename in User" if !$fn;
196 $fn .= $v3 ? ".v3" : ".v2";
201 # periodic processing
205 if ($main::systime > $lasttime + 15) {
207 $lasttime = $main::systime;
222 # new - create a new user
229 my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
237 # $call =~ s/-\d+$//o;
239 # confess "can't create existing call $call in User\n!" if $u{$call};
241 my $self = $pkg->alloc($call);
247 # get - get an existing user - this seems to return a different reference everytime it is
256 # is it in the LRU cache?
257 my $ref = $lru->get($call);
258 return $ref if $ref && ref $ref eq 'DXUser';
261 unless ($dbm->get($call, $data)) {
262 $ref = eval{decode($data)};
264 if (!UNIVERSAL::isa($ref, 'DXUser')) {
265 dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
268 # we have a reference and it *is* a DXUser
270 dbg("DXUser::get: no reference returned from decode of $call $! $@");
273 $lru->put($call, $ref);
280 # get an existing either from the channel (if there is one) or from the database
282 # It is important to note that if you have done a get (for the channel say) and you
283 # want access or modify that you must use this call (and you must NOT use get's all
284 # over the place willy nilly!)
291 my $dxchan = DXChannel::get($call);
293 my $ref = $dxchan->user;
294 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
296 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
302 # get all callsigns in the database
307 return (sort keys %u);
317 confess "Trying to put nothing!" unless $self && ref $self;
318 my $call = $self->{call};
321 delete $self->{annok} if $self->{annok};
322 delete $self->{dxok} if $self->{dxok};
324 $lru->put($call, $self);
325 my $ref = $self->encode;
326 $dbm->put($call, $ref);
332 goto &asc_encode unless $v3;
334 return nfreeze($self);
340 goto &asc_decode unless $v3;
345 # create a string from a user reference (in_ascii)
354 my $ref = bless {}, ref $self;
355 foreach my $k (qw(qth lat long qra sort call homenode node lastoper lastin)) {
356 $ref->{$k} = $self->{$k} if exists $self->{$k};
358 $ref->{name} = $self->{name} if exists $self->{name} && $self->{name} !~ /selfspot/i;
367 # create a hash from a string (in ascii)
373 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
383 # del - delete a user
389 my $call = $self->{call};
395 # close - close down a user
401 $self->{lastin} = time;
415 # return a list of valid elements
425 # export the database to an ascii file
431 my $basic_info_only = shift;
434 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
435 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
436 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
437 rename "$fn.o", "$fn.oo" if -e "$fn.o";
438 rename "$fn", "$fn.o" if -e "$fn";
443 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
448 my $t = scalar localtime;
449 print $fh q{#!/usr/bin/perl
451 # The exported userfile for a DXSpider System
453 # Input file: $filename
459 # search local then perl directories
463 # root of directory tree for this system
465 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
467 unshift @INC, "$root/perl"; # this IS the right way round!
468 unshift @INC, "$root/local";
470 # try to detect a lockfile (this isn't atomic but
472 $lockfn = "$root/local/cluster.lck"; # lock file name
474 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
477 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
488 $main::userfn = shift @ARGV;
489 print "user filename now $userfn\n";
492 DXUser->del_file($main::userfn);
493 DXUser->init($main::userfn, 1);
500 my $ref = asc_decode($f[1]);
505 print "# Error: $f[0]\t$f[1]\n";
509 DXUser->sync; DXUser->finish;
510 print "There are $count user records and $err errors\n";
512 print $fh "__DATA__\n";
514 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
515 if (!is_callsign($key) || $key =~ /^0/) {
518 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
519 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
520 LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
521 eval {$dbm->del($key)};
522 dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
526 my $ref = decode($val);
528 my $t = $ref->{lastin} || 0;
529 if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
530 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
531 eval {$dbm->del($key)};
532 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
533 LogDbg('DXCommand', "$ref->{call} deleted, too old");
538 # only store users that are reasonably active or have useful information
539 print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
542 LogDbg('DXCommand', "Export Error3: $key\t$val");
543 eval {$dbm->del($key)};
544 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
550 return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
557 # add one or more groups
561 my $ref = $self->{group} || [ 'local' ];
562 $self->{group} = $ref if !$self->{group};
563 push @$ref, @_ if @_;
566 # remove one or more groups
570 my $ref = $self->{group} || [ 'local' ];
573 $self->{group} = $ref if !$self->{group};
575 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
578 # does this thing contain all the groups listed?
582 my $ref = $self->{group};
585 return 0 if !$ref || @_ == 0;
586 return 1 if @$ref == 0 && @_ == 0;
587 for ($n = 0; $n < @_; ) {
590 $n++ if grep $_ eq $a, @_;
596 # simplified group test just for one group
601 my $ref = $self->{group};
604 return grep $_ eq $s, $ref;
607 # set up a default group (only happens for them's that connect direct)
611 $self->{group} = [ 'local' ];
614 # set up empty buddies (only happens for them's that connect direct)
618 $self->{buddies} = [ ];
622 # return a prompt for a field
627 my ($self, $ele) = @_;
631 # some variable accessors
635 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
640 # want is default = 1
647 $self->{$s} = $val if defined $val;
648 return exists $self->{$s} ? $self->{$s} : 1;
651 # wantnot is default = 0
658 $self->{$s} = $val if defined $val;
659 return exists $self->{$s} ? $self->{$s} : 0;
664 return _want('beep', @_);
669 return _want('ann', @_);
674 return _want('wwv', @_);
679 return _want('wcy', @_);
684 return _want('echo', @_);
689 return _want('wx', @_);
694 return _want('dx', @_);
699 return _want('talk', @_);
704 return _want('grid', @_);
709 return _want('email', @_);
714 return _want('ann_talk', @_);
719 return _want('pc16', @_);
724 return _want('sendpc16', @_);
729 return _want('routepc16', @_);
734 return _want('usstate', @_);
739 return _want('dxcq', @_);
744 return _want('dxitu', @_);
749 return _want('gtk', @_);
754 return _want('pc9x', @_);
761 $self->{wantlogininfo} = $val if defined $val;
762 return $self->{wantlogininfo};
768 return $self->{sort} =~ /[ACRSX]/;
774 return grep $_ eq 'local_node', @{$self->{group}};
780 return $self->{sort} eq 'U';
786 return $self->{sort} eq 'B';
792 return $self->{sort} eq 'S';
798 return $self->{sort} eq 'C';
804 return $self->{sort} eq 'X';
810 return $self->{sort} eq 'R';
816 return $self->{sort} eq 'A';
822 delete $self->{passwd};
828 delete $self->{passphrase};
835 $self->{believe} ||= [];
836 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
843 if (exists $self->{believe}) {
844 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
845 delete $self->{believe} unless @{$self->{believe}};
852 return exists $self->{believe} ? @{$self->{believe}} : ();
859 $self->{lastping} ||= {};
860 $self->{lastping} = {} unless ref $self->{lastping};
861 my $b = $self->{lastping};
862 $b->{$call} = shift if @_;