--- a/mail2db Sun Jul 20 21:27:45 2008 +0000
+++ b/mail2db Sun Jul 20 21:37:00 2008 +0000
@@ -21,10 +21,10 @@
my $OUTPUT_CHARSET = "UTF8";
my $DEFAULT_INPUT_CHARSET = "ASCII";
-my $opt_help = 0;
-my $opt_man = 0;
-my $opt_dry = 0;
-my $opt_dsn = "";
+my $opt_help = 0;
+my $opt_man = 0;
+my $opt_dry = 0;
+my $opt_dsn = "";
my $opt_dbuser = "";
my $opt_dbpass = "";
@@ -36,27 +36,28 @@
sub decode_headers($$);
MAIN: {
- openlog("mail2db", LOG_PID | (-t STDERR ? LOG_PERROR : 0), LOG_MAIL);
+ openlog("mail2db", LOG_PID | (-t STDERR ? LOG_PERROR: 0), LOG_MAIL);
- if (-f ($_ = "/etc/mail2db.conf")) {
- open(X, $_) or die "Can't open $_: $!\n";
- unshift @ARGV, map { chomp; $_ } <X>;
- }
+ if (-f ($_ = "/etc/mail2db.conf")) {
+ open(X, $_) or die "Can't open $_: $!\n";
+ unshift @ARGV, map { chomp; $_ } <X>;
+ }
GetOptions(
- "dsn=s" => \$opt_dsn,
- "dbuser=s" => \$opt_dbuser,
- "dbpass=s" => \$opt_dbpass,
- "h|help" => \$opt_help,
- "m|man" => \$opt_man,
- "n|dry" => \$opt_dry,
+ "dsn=s" => \$opt_dsn,
+ "dbuser=s" => \$opt_dbuser,
+ "dbpass=s" => \$opt_dbpass,
+ "h|help" => \$opt_help,
+ "m|man" => \$opt_man,
+ "n|dry" => \$opt_dry,
) or pod2usage();
pod2usage(-verbose => 1, -exitval => 0) if $opt_help;
pod2usage(-verbose => 2, -exitval => 0) if $opt_man;
- $DBH = DBI->connect($opt_dsn, $opt_dbuser, $opt_dbpass,
- { RaiseError => 1, FetchHashKeyName => "NAME_lc", AutoCommit => 0 })
- or die;
+ $DBH =
+ DBI->connect($opt_dsn, $opt_dbuser, $opt_dbpass,
+ { RaiseError => 1, FetchHashKeyName => "NAME_lc", AutoCommit => 0 })
+ or die;
my ($tmpfile, $message) = get_message();
decode_headers($message, $OUTPUT_CHARSET);
@@ -69,52 +70,59 @@
# database job starts
- my $insert_message = $DBH->prepare(qq{
+ my $insert_message = $DBH->prepare(
+ qq{
INSERT INTO message (id, content, timestamp) VALUES(NULL, ?, NOW())
- });
+ }
+ );
- my $insert_header_id = $DBH->prepare(qq{
+ my $insert_header_id = $DBH->prepare(
+ qq{
INSERT INTO header_field (id, name) VALUES(NULL, ?)
- });
- my $insert_message_header = $DBH->prepare(qq{
+ }
+ );
+ my $insert_message_header = $DBH->prepare(
+ qq{
INSERT INTO message_header
(message_id, header_field_id, idx, content)
VALUES(?, (SELECT id FROM header_field WHERE name = ?), ?, ?)
- });
+ }
+ );
- # first insert the message and get the database message id
+ # first insert the message and get the database message id
my $msg_id;
{
seek($tmpfile, 0, 0);
local $/ = undef;
$insert_message->execute(<$tmpfile>);
$msg_id = $DBH->last_insert_id(undef, undef, message => "id");
- syslog(LOG_DEBUG, "message id: $msg_id");
+ syslog(LOG_DEBUG, "message id: $msg_id");
}
- # now insert the message headers
+ # now insert the message headers
foreach my $tag (map { lc } $message->head->tags) {
for (my $idx = 0 ;
my $header = $message->head->get($tag, $idx) ; ++$idx)
{
$header =~ s/\s*$//;
- syslog(LOG_DEBUG, "$tag\[$idx]\n");
+ syslog(LOG_DEBUG, "$tag\[$idx]\n");
- # first we'll give it a try, but it may fail, because the
- # header_field.id is missing
- eval {
- local $insert_message_header->{PrintError} = 0;
- $insert_message_header->execute($msg_id, $tag, $idx, $header);
- };
- # if there was a problem, we'll try to insert the header_field
- if ($@) {
- $insert_header_id->execute($tag);
- $insert_message_header->execute($msg_id, $tag, $idx, $header);
- }
+ # first we'll give it a try, but it may fail, because the
+ # header_field.id is missing
+ eval {
+ local $insert_message_header->{PrintError} = 0;
+ $insert_message_header->execute($msg_id, $tag, $idx, $header);
+ };
+
+ # if there was a problem, we'll try to insert the header_field
+ if ($@) {
+ $insert_header_id->execute($tag);
+ $insert_message_header->execute($msg_id, $tag, $idx, $header);
+ }
}
}
- $DBH->commit if not $opt_dry;
+ $DBH->commit if not $opt_dry;
}