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
428 # add one or more groups
432 my $ref = $self->{group} || [ 'local' ];
433 $self->{group} = $ref if !$self->{group};
434 push @$ref, @_ if @_;
437 # remove one or more groups
441 my $ref = $self->{group} || [ 'local' ];
444 $self->{group} = $ref if !$self->{group};
446 @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
449 # does this thing contain all the groups listed?
453 my $ref = $self->{group};
456 return 0 if !$ref || @_ == 0;
457 return 1 if @$ref == 0 && @_ == 0;
458 for ($n = 0; $n < @_; ) {
461 $n++ if grep $_ eq $a, @_;
467 # simplified group test just for one group
472 my $ref = $self->{group};
475 return grep $_ eq $s, $ref;
478 # set up a default group (only happens for them's that connect direct)
482 $self->{group} = [ 'local' ];
485 # set up empty buddies (only happens for them's that connect direct)
489 $self->{buddies} = [ ];
493 # return a prompt for a field
498 my ($self, $ele) = @_;
502 # some variable accessors
506 @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
511 # want is default = 1
518 $self->{$s} = $val if defined $val;
519 return exists $self->{$s} ? $self->{$s} : 1;
522 # wantnot is default = 0
529 $self->{$s} = $val if defined $val;
530 return exists $self->{$s} ? $self->{$s} : 0;
535 return _want('beep', @_);
540 return _want('ann', @_);
545 return _want('wwv', @_);
550 return _want('wcy', @_);
555 return _want('echo', @_);
560 return _want('wx', @_);
565 return _want('dx', @_);
570 return _want('talk', @_);
575 return _want('grid', @_);
580 return _want('email', @_);
585 return _want('ann_talk', @_);
590 return _want('pc16', @_);
595 return _want('sendpc16', @_);
600 return _want('routepc16', @_);
605 return _want('usstate', @_);
610 return _want('dxcq', @_);
615 return _want('dxitu', @_);
620 return _want('gtk', @_);
625 return _want('pc9x', @_);
632 $self->{wantlogininfo} = $val if defined $val;
633 return $self->{wantlogininfo};
639 return $self->{sort} =~ /[ACRSX]/;
645 return grep $_ eq 'local_node', @{$self->{group}};
651 return $self->{sort} eq 'U';
657 return $self->{sort} eq 'B';
663 return $self->{sort} eq 'S';
669 return $self->{sort} eq 'C';
675 return $self->{sort} eq 'X';
681 return $self->{sort} eq 'R';
687 return $self->{sort} eq 'A';
693 delete $self->{passwd};
699 delete $self->{passphrase};
706 $self->{believe} ||= [];
707 push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
714 if (exists $self->{believe}) {
715 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
716 delete $self->{believe} unless @{$self->{believe}};
723 return exists $self->{believe} ? @{$self->{believe}} : ();
730 $self->{lastping} ||= {};
731 $self->{lastping} = {} unless ref $self->{lastping};
732 my $b = $self->{lastping};
733 $b->{$call} = shift if @_;
738 # export the database to an ascii file
744 my $basic_info_only = shift;
747 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
748 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
749 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
750 rename "$fn.o", "$fn.oo" if -e "$fn.o";
751 rename "$fn", "$fn.o" if -e "$fn";
756 my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
761 my $t = scalar localtime;
763 print $fh export_preamble();
765 for ($action = R_FIRST; !$dbm->seq($key, $val, $action); $action = R_NEXT) {
766 if (!is_callsign($key) || $key =~ /^0/) {
769 $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
770 $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
771 LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
772 eval {$dbm->del($key)};
773 dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
777 my $ref = decode($val);
779 my $t = $ref->{lastin} || 0;
780 if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
781 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
782 eval {$dbm->del($key)};
783 dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
784 LogDbg('DXCommand', "$ref->{call} deleted, too old");
789 # only store users that are reasonably active or have useful information
790 print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
793 LogDbg('DXCommand', "Export Error3: $key\t$val");
794 eval {$dbm->del($key)};
795 dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
801 return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
807 return q{#!/usr/bin/perl
809 # The exported userfile for a DXSpider System
811 # Input file: $filename
817 # search local then perl directories
821 # root of directory tree for this system
823 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
825 unshift @INC, "$root/perl"; # this IS the right way round!
826 unshift @INC, "$root/local";
828 # try to detect a lockfile (this isn't atomic but
830 $lockfn = "$root/local/cluster.lck"; # lock file name
832 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
835 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
846 $main::userfn = shift @ARGV;
847 print "user filename now $userfn\n";
850 DXUser->del_file($main::userfn);
851 DXUser->init($main::userfn, 1);
858 my $ref = asc_decode($f[1]);
863 print "# Error: $f[0]\t$f[1]\n";
867 DXUser->sync; DXUser->finish;
868 print "There are $count user records and $err errors\n";