From 352c8d3e120e69c96ab1f47c8f985a0131d5be0a Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Fri, 21 Jan 2011 20:54:51 -0700 Subject: [PATCH] Added DB::Flatfile for DB:: database functions. --- bin/auto | 7 ++- doc/CHANGELOG | 1 + src/DB/Flatfile.pm | 121 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/DB/Flatfile.pm diff --git a/bin/auto b/bin/auto index 0a0d14a..50f36d0 100755 --- a/bin/auto +++ b/bin/auto @@ -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") { diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 2567fc0..227c656 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -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. diff --git a/src/DB/Flatfile.pm b/src/DB/Flatfile.pm new file mode 100644 index 0000000..3f59206 --- /dev/null +++ b/src/DB/Flatfile.pm @@ -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 = 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;