3 # Database Handler module for DXSpider
5 # Copyright (c) 1999 Dirk Koopman G1TLH
17 use vars qw($opentime $dbbase %avail %valid $lastprocesstime $nextstream %stream);
19 $opentime = 5*60; # length of time a database stays open after last access
20 $dbbase = "$main::root/db"; # where all the databases are kept;
21 %avail = (); # The hash contains a list of all the databases
23 accesst => '9,Last Accs Time,atime',
24 createt => '9,Create Time,atime',
25 lastt => '9,Last Upd Time,atime',
27 db => '9,DB Tied hash',
28 remote => '0,Remote Database',
29 pre => '0,Heading txt',
31 chain => '0,Search these,parray',
32 disable => '0,Disabled?,yesno',
33 nf => '0,Not Found txt',
34 cal => '0,No Key txt',
35 allowread => '9,Allowed read,parray',
36 denyread => '9,Deny read,parray',
37 allowupd => '9,Allow upd,parray',
38 denyupd => '9,Deny upd,parray',
39 fwdupd => '9,Forw upd to,parray',
40 template => '9,Upd Templates,parray',
41 te => '9,End Upd txt',
42 tae => '9,End App txt',
43 atemplate => '9,App Templates,parray',
44 help => '0,Help txt,parray',
45 localcmd => '0,Local Command',
48 $lastprocesstime = time;
52 use vars qw($VERSION $BRANCH);
53 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
54 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
55 $main::build += $VERSION;
56 $main::branch += $BRANCH;
58 # allocate a new stream for this request
62 my $n = ++$nextstream;
63 $stream{$n} = { n=>$n, call=>$call, t=>$main::systime };
81 # load all the database descriptors
84 my $s = readfilestr($dbbase, "dbs", "pl");
89 %avail = ( %$a ) if ref $a;
93 # save all the database descriptors
97 writefilestr($dbbase, "dbs", "pl", \%avail);
100 # get the descriptor of the database you want.
103 return undef unless %avail;
106 my $r = $avail{$name};
108 # search for a partial if not found direct
110 for (sort { $a->{name} cmp $b->{name} }values %avail) {
111 if ($_->{name} =~ /^$name/) {
124 $self->{accesst} = $main::systime;
125 return $self->{db} if $self->{db};
127 $self->{db} = tie %hash, 'DB_File', "$dbbase/$self->{name}";
146 for (values %avail) {
152 # get a value from the database
160 $key =~ s/[\@\$\&\%\*]+//g;
161 $key =~ s/^[\.\/]+//g;
163 # make sure we are open
165 if ($self->{localcmd}) {
166 my $dxchan = $main::me;
167 $dxchan->{remotecmd} = 1; # for the benefit of any command that needs to know
168 my $oldpriv = $dxchan->{priv};
170 my @in = (DXCommandmode::run_cmd($dxchan, "$self->{localcmd} $key"));
171 $dxchan->{priv} = $oldpriv;
172 delete $dxchan->{remotecmd};
173 return @in ? join("\n", @in) : undef;
174 } elsif ($self->{db}) {
175 my $s = $self->{db}->get($key, $value);
176 return $s ? undef : $value;
181 # put a value to the database
188 # make sure we are open
191 my $s = $self->{db}->put($key, $value);
192 return $s ? undef : 1;
197 # create a new database params: <name> [<remote node call>]
206 $self->{name} = lc $name;
207 $self->{remote} = uc $remote if $remote;
208 $self->{chain} = $chain if $chain && ref $chain;
209 $self->{accesst} = $self->{createt} = $self->{lastt} = $main::systime;
210 $self->{localcmd} = lc $cmd if $cmd;
212 $avail{$self->{name}} = $self;
213 mkdir $dbbase, 02775 unless -e $dbbase;
223 unlink "$dbbase/$self->{name}";
224 delete $avail{$self->{name}};
229 # process intermediate lines for an update
230 # NOTE THAT THIS WILL BE CALLED FROM DXCommandmode and the
231 # object will be a DXChannel (actually DXCommandmode)
239 # periodic maintenance
241 # just close any things that haven't been accessed for the default
247 if ($main::systime - $lastprocesstime >= 60) {
249 for (values %avail) {
250 if ($main::systime - $_->{accesst} > $opentime) {
255 $lastprocesstime = $main::systime;
268 # incoming DB Request
269 my @in = DXCommandmode::run_cmd($self, "dbshow $_[4] $_[5]");
270 sendremote($self, $_[2], $_[3], @in);
277 # incoming DB Information
278 my $n = getstream($_[3]);
280 my $mchan = DXChannel::get($n->{call});
281 $mchan->send($_[2] . ":$_[4]") if $mchan;
289 # incoming DB Complete
301 # send back a trache of data to the remote
302 # remember $dxchan is a dxchannel
310 $dxchan->send(DXProt::pc45($main::mycall, $tonode, $stream, $_));
312 $dxchan->send(DXProt::pc46($main::mycall, $tonode, $stream));
315 # print a value from the db reference
320 return $self->{$s} ? $self->{$s} : undef;
323 # various access routines
326 # return a list of valid elements
335 # return a prompt for a field
340 my ($self, $ele) = @_;
348 my $name = $AUTOLOAD;
349 return if $name =~ /::DESTROY$/;
352 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
353 # this clever line of code creates a subroutine which takes over from autoload
354 # from OO Perl - Conway
355 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};