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