Added DB::Flatfile for DB:: database functions.

This commit is contained in:
Elijah Perrault committed 2011-01-21 20:54:51 -07:00
1 parent 23b0a4e627
commit 352c8d3e12
3 files changed
+128 -1

No files matched your search

+6 -1
View File
@@ -18,7 +18,7 @@ our $Bin = $Bin;
BEGIN { unshift(@INC, "$Bin/../src"); }
use API::Std qw(conf_get err);
use API::Log qw(println alog dbug);
#use DB::Flatfile;
use DB::Flatfile;
use Parser::Config;
use Parser::Lang;
use Parser::IRC;
@@ -119,6 +119,9 @@ println " Success";
# Expire old logs.
API::Log::expire_logs();
# Load database.
DB::load();
# Successful startup.
our $STARTTIME = time;
println "* Auto successfully started at ".POSIX::strftime("%Y-%m-%d %I:%M:%S %p", localtime).".";
@@ -275,6 +278,7 @@ sub signal_TERM
{
API::Std::event_run("on_sigterm");
API::IRC::quit($_, "Caught SIGTERM") foreach (keys %SOCKET);
DB::flush();
dbug "!!! Caught SIGTERM; terminating...";
alog "!!! Caught SIGTERM; terminating...";
if (-e "$Bin/auto.pid") {
@@ -289,6 +293,7 @@ sub signal_INT
{
API::Std::event_run("on_sigint");
API::IRC::quit($_, "Caught SIGINT") foreach (keys %SOCKET);
DB::flush();
dbug "!!! Caught SIGINT; terminating...";
alog "!!! Caught SIGINT; terminating...";
if (-e "$Bin/auto.pid") {
+1
View File
@@ -3,6 +3,7 @@ Auto IRC Bot 3.0: Change Log
3.0 Indev
==================================================================
* Added DB::Flatfile for DB:: database functions.
* Added umode() to API::IRC.
* Added events on_ucjoin, on_rcjoin and on_nick.
* Parse numerics 465, 471-475 and 477.
+121
View File
@@ -0,0 +1,121 @@
# Auto IRC Bot. An advanced, lightweight and powerful IRC bot.
# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS)
# This program is free software; rights to this code are stated in doc/LICENSE.
# Database subroutines for the Auto-Flatfile database format.
package DB;
use strict;
use warnings;
my (%MEM);
# Database initial loading.
sub load
{
# Open, read and close the database.
open FILE, "<$Auto::Bin/../etc/auto.db" or return 0;
my @fbuf = <FILE> or return 0;
close FILE or return 0;
# Make sure the database version is compatible.
if ($fbuf[0] ne "DBV Auto-Flatfile_1.0\n") {
# It is not. Bail.
return 0;
}
# Iterate the buffer.
foreach my $buff (@fbuf) {
# Check if the buffer is defined.
if (defined $buff) {
# Space buffer.
my @lbuf = split(' ', $buff);
# Check if the first two values are defined.
if (defined $lbuf[0] and defined $lbuf[1]) {
# Make sure it isn't DBV.
if ($lbuf[0] eq "DBV") {
next;
}
# Get a count of the values in the buffer minus one.
my $c = scalar(@lbuf) - 1;
# Create an array of the values excluding the first.
my @vs;
for (my $i = $c; $i <= $c; $i++) {
push(@vs, $lbuf[$i]);
}
# Insert data into memory.
if (defined $MEM{$lbuf[0]}) {
# There is a database entry of the same name already, add to existing array.
push(@{ $MEM{$lbuf[0]} }, [ @vs ]);
}
else {
# This is the first database entry of this name, create an array.
@{ $MEM{$lbuf[0]} } = [ @vs ];
}
}
}
}
return 1;
}
# Database flush to disk.
sub flush
{
my $wd = "DBV Auto-Flatfile_1.0\n";
# Iterate through all the database data in memory.
foreach my $mek (keys %MEM) {
foreach my $mas (@{ $MEM{$mek} }) {
$wd .= $mek;
foreach my $masl (@{ $mas }) {
$wd .= " ".$masl;
}
$wd .= "\n";
}
}
# Write to disk.
unless (-e "$Auto::Bin/../etc/auto.db") {
`touch $Auto::Bin/../etc/auto.db`;
}
open FDB, ">$Auto::Bin/../etc/auto.db" or return 0;
print FDB $wd or return 0;
close FDB or return 0;
return 1;
}
# Get database value.
sub get
{
my ($name) = @_;
if (defined $MEM{$name}) {
return $MEM{$name};
}
else {
return 0;
}
}
# Write to database.
sub write
{
my ($name, @values) = @_;
if (defined $MEM{$name}) {
# There is a database entry of the same name already, add to existing array.
push(@{ $MEM{$name} }, [ @values ]);
}
else {
# This is the first database entry of this name, create an array.
@{ $MEM{$name} } = [ @values ];
}
return 1;
}
1;