module AwayMsg;

package AwayMsg;
use strict;
use warnings;

BEGIN {
    use Exporter ();
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
    $VERSION     = 1.00;
    @ISA         = qw(Exporter);
    @EXPORT      = qw(&db_connect $dhb);
    %EXPORT_TAGS = ( );
    @EXPORT_OK   = qw();
}

sub db_connect () {
    my $h = DBI->connect("dbi:SQLite:away.db",
			 { AutoCommit=>0,
			   RaiseError=>1, ShowErrorStatement=>1
			 })
        or die "$DBI:errstr ?";

    $dbh->do("BEGIN");

    $dbh->do("CREATE TEMPORARY TABLE config (".
	     " emailaddr TEXT PRIMARY KEY NOT NULL,".
	     " username TEXT NOT NULL,".
	     " forwardfile TEXT NOT NULL".
	     ")");

    open C, "config" or die $!;
    while (<C>) {
	s/^\s+//;
	next if m/^\#/;
	chomp or die;
	s/\s+$//;
	my @s = split;
	die "$_ ?" unless @s==3;
	$dbh->do("INSERT INTO config".
		 " (emailaddr, username, forwardfile)".
		 " VALUES (?,?,?)",
		 {}, @s);
    }
    $dbh->do("COMMIT");

    return $h;
}
