exim-exigrey.pl
changeset 12 de787ecf7c3c
parent 9 417562509dbe
child 16 b47059897e9b
--- a/exim-exigrey.pl	Tue Jan 02 16:59:20 2007 +0000
+++ b/exim-exigrey.pl	Tue Jan 02 19:50:02 2007 +0000
@@ -4,7 +4,14 @@
 
 use strict;
 use warnings;
-use BerkeleyDB;
+use Carp;
+
+# You may choose, but DB_File's footprint is smaller.
+# perl -MDB_File -e 'tie %h, ...':	real    0m0.063s
+# perl -MBerkeleyDB -e 'tie %h, ...':	real	0m0.112s
+# And DB_File is part of the Perl core distribution (?)
+# use BerkeleyDB;
+use DB_File;
 
 my %DEFAULT = (
 	delay => 600,
@@ -13,8 +20,10 @@
 
 sub unseen($;$$);
 
+# some helper functions
 sub getDBDir();
 sub findExim(;$);
+sub connectDB($$);
 sub getDefault() { %DEFAULT };
 
 # Usage:
@@ -33,19 +42,10 @@
 		$db = $DEFAULT{db} unless defined $db;
 
 	my $now = time();
-	my $umask;
 	my $rc;
 
-	$db = getDBDir() . "/$db" unless $db =~ /^\//;
-
-	$umask = umask 0077 if !-f $db;
-
-	my %h; tie %h, "BerkeleyDB::Hash", 
-		-Filename => $db,
-		-Flags => DB_CREATE
-	or die;
-
-	umask $umask if defined $umask;
+	my %h; 
+	$db = connectDB(\%h, $db || $DEFAULT{db});
 
 	if (not exists $h{$item}) {
 		$h{$item} = "$now $now\0";
@@ -83,9 +83,31 @@
 		-x ($exim = "$_/exim") and return $exim;
 		-x ($exim = "$_/exim4") and return $exim;
 	}
-	return undef;
+	die "Can't find exim binary (missing .../sbin dirs in PATH?";
 }
 
+sub connectDB($$) {
+    my ($h, $db) = @_;
+    $db = getDBDir() ."/$db" unless $db =~ /^\//;
+
+    if (exists &BerkeleyDB::Hash::TIEHASH) {
+	no strict;
+	my $umask = umask 077;
+	tie %$h, "BerkeleyDB::Hash", 
+	    -Filename => $db,
+	    -Flags => DB_CREATE
+	or die "$0: $db: $!";
+	return $db;
+    }
+
+    if (exists &DB_File::TIEHASH) {
+	tie %$h, "DB_File", $db, undef, 0600
+	or die "$0: $db: $!";
+	return $db;
+    }
+
+    die "Can't connect to database driver";
+}
 1;
 
 # vim:aw: