chiark / gitweb /
wip
[nj-awaymsg.git] / AwayMsg.pm
1 module AwayMsg;
2
3 package AwayMsg;
4 use strict;
5 use warnings;
6
7 BEGIN {
8     use Exporter ();
9     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
10     $VERSION     = 1.00;
11     @ISA         = qw(Exporter);
12     @EXPORT      = qw(&db_connect $dhb);
13     %EXPORT_TAGS = ( );
14     @EXPORT_OK   = qw();
15 }
16
17 sub db_connect () {
18     my $h = DBI->connect("dbi:SQLite:away.db",
19                          { AutoCommit=>0,
20                            RaiseError=>1, ShowErrorStatement=>1
21                          })
22         or die "$DBI:errstr ?";
23
24     $dbh->do("BEGIN");
25
26     $dbh->do("CREATE TEMPORARY TABLE config (".
27              " emailaddr TEXT PRIMARY KEY NOT NULL,".
28              " username TEXT NOT NULL,".
29              " forwardfile TEXT NOT NULL".
30              ")");
31
32     open C, "config" or die $!;
33     while (<C>) {
34         s/^\s+//;
35         next if m/^\#/;
36         chomp or die;
37         s/\s+$//;
38         my @s = split;
39         die "$_ ?" unless @s==3;
40         $dbh->do("INSERT INTO config".
41                  " (emailaddr, username, forwardfile)".
42                  " VALUES (?,?,?)",
43                  {}, @s);
44     }
45     $dbh->do("COMMIT");
46
47     return $h;
48 }