diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/mail-to-sql.pl | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/scripts/mail-to-sql.pl b/scripts/mail-to-sql.pl index 9085c9af2..6fcb89631 100644 --- a/scripts/mail-to-sql.pl +++ b/scripts/mail-to-sql.pl @@ -2,37 +2,52 @@ use DBI; -# database settings: +# Database settings: my $db_name = 'drop'; my $db_user = 'drop'; my $db_pass = 'drop'; -# read data from stdin: +# Read data from stdin: my @data = <STDIN>; my $data = join '', @data; my @chunks = split(/\n\n/, $data); -# parse the header into an associative array: +# Parse the header into an associative array: foreach $line (split(/\n/, $chunks[0])) { - if ($line =~ /(.*?):\s(.*)/) { - $header{lc($1)} = $2; + # The field-body can be split into a multiple-line representation, + # which is called "folding". According to RFC 822, the rule is that + # wherever there may be linear-white-space (not simply LWSP-chars), + # a CRLF immediately followed by at least one LWSP-char may instead + # be inserted. + + if ($line =~ /^\s(.*?)/) { + $data = $1; + } + elsif ($line =~ /(.*?):\s(.*)/) { + $key = lc($1); + $data = $2; } - $header{data} .= "$line\n"; -} -$chunks[0] = ""; + if ($key && $data) { + $header{$key} .= $data; + } +} -# debug output: +# Debug output: # foreach $key (sort keys %header) { - # print "$key: $header{$key}\n"; + # print "$key: $header{$key}\n--------\n"; # } -# construct the mail body: +# Store the complete header into a field: +$header{header} = $chunks[0]; +$chunks[0] = ""; + +# Construct the mail body: foreach $line (@chunks) { $body .= "$line\n\n"; } my $db = DBI->connect("DBI:mysql:$db_name", "$db_user", "$db_pass") or die "Couldn't connect recepient database: " . DBI->errstr; -$db->do("INSERT INTO mail (subject, sender, recepient, header, body, timestamp) VALUES (". $db->quote($header{subject}) .", ". $db->quote($header{from}) .", ". $db->quote($header{to}) .", ". $db->quote($header{data}) .", ". $db->quote($body) .", ". $db->quote(time()) .")") or die "Couldn't execute query: " . $db->errstr; +$db->do("INSERT INTO mail (subject, header_from, header_to, header_cc, header_reply_to, header, body, timestamp) VALUES (". $db->quote($header{"subject"}) .", ". $db->quote($header{"from"}) .", ". $db->quote($header{"to"}) .", ". $db->quote($header{"cc"}) .", ". $db->quote($header{"reply-to"}) .", ". $db->quote($header{"header"}) .", ". $db->quote($body) .", ". $db->quote(time()) .")") or die "Couldn't execute query: " . $db->errstr; $db->disconnect(); |