Added --stdin option to mail_to_db.pl

parent cc8a3770
heikki@donna.mysql.fi jani@janikt.pp.saunalahti.fi
jcole@abel.spaceapes.com
sasha@mysql.sashanet.com
tim@threads.polyesthetic.msg
...@@ -17,7 +17,7 @@ use DBI; ...@@ -17,7 +17,7 @@ use DBI;
use Getopt::Long; use Getopt::Long;
$| = 1; $| = 1;
$VER = "2.1"; $VER = "2.2";
$opt_help = 0; $opt_help = 0;
$opt_version = 0; $opt_version = 0;
...@@ -34,6 +34,7 @@ $opt_create = 0; ...@@ -34,6 +34,7 @@ $opt_create = 0;
$opt_test = 0; $opt_test = 0;
$opt_no_path = 0; $opt_no_path = 0;
$opt_stop_on_error = 0; $opt_stop_on_error = 0;
$opt_stdin = 0;
my ($dbh, $progname, $mail_no_from_f, $mail_no_txt_f, $mail_too_big, my ($dbh, $progname, $mail_no_from_f, $mail_no_txt_f, $mail_too_big,
$mail_forwarded, $mail_duplicates, $mail_no_subject_f, $mail_inserted); $mail_forwarded, $mail_duplicates, $mail_no_subject_f, $mail_inserted);
...@@ -98,13 +99,14 @@ sub main ...@@ -98,13 +99,14 @@ sub main
} }
GetOptions("help","version","host=s","port=i","socket=s","db=s","table=s", GetOptions("help","version","host=s","port=i","socket=s","db=s","table=s",
"user=s","password=s","max_mail_size=i","create","test", "user=s","password=s","max_mail_size=i","create","test",
"no_path","debug","stop_on_error") "no_path","debug","stop_on_error","stdin")
|| die "Wrong option! See $progname --help\n"; || die "Wrong option! See $progname --help\n";
usage($VER) if ($opt_help || $opt_version || (!$ARGV[0] && !$opt_create)); usage($VER) if ($opt_help || $opt_version ||
(!$ARGV[0] && !$opt_create && !$opt_stdin));
# Check that the given inbox files exist and are regular files # Check that the given inbox files exist and are regular files
for ($i = 0; defined($ARGV[$i]); $i++) for ($i = 0; ! $opt_stdin && defined($ARGV[$i]); $i++)
{ {
die "FATAL: Can't find inbox file: $ARGV[$i]\n" if (! -f $ARGV[$i]); die "FATAL: Can't find inbox file: $ARGV[$i]\n" if (! -f $ARGV[$i]);
} }
...@@ -125,18 +127,26 @@ sub main ...@@ -125,18 +127,26 @@ sub main
create_table($dbh) if ($opt_create); create_table($dbh) if ($opt_create);
foreach (@ARGV) if ($opt_stdin)
{ {
# Check if the file is compressed open(FILE, "-");
if (/^(.*)\.(gz|Z)$/) process_mail_file($dbh, "READ-FROM-STDIN");
{ }
open(FILE, "zcat $_ |"); else
process_mail_file($dbh, $1); {
} foreach (@ARGV)
else
{ {
open(FILE, $_); # Check if the file is compressed
process_mail_file($dbh, $_); if (/^(.*)\.(gz|Z)$/)
{
open(FILE, "zcat $_ |");
process_mail_file($dbh, $1);
}
else
{
open(FILE, $_);
process_mail_file($dbh, $_);
}
} }
} }
$dbh->disconnect if (!$opt_test); $dbh->disconnect if (!$opt_test);
...@@ -190,7 +200,7 @@ EOF ...@@ -190,7 +200,7 @@ EOF
} }
#### ####
#### inbox processing #### inbox processing. Can be either a real file, or standard input.
#### ####
sub process_mail_file sub process_mail_file
...@@ -483,31 +493,36 @@ sub usage ...@@ -483,31 +493,36 @@ sub usage
print <<EOF; print <<EOF;
$progname version $VER $progname version $VER
Description: Insert mails from inbox file(s) into a table. Description: Insert mails from inbox file(s) into a table. This program
This program can read group [mail_to_db] from the my.cnf can read group [mail_to_db] from the my.cnf file. You may want to have db
file. You may want to have db and table set there at least. and table set there at least.
Usage: $progname [options] file1 [file2 file3 ...]
or: $progname [options] --create [file1 file2...]
or: cat inbox | $progname [options] --stdin
Usage: $progname [options] file1 [file2 file3 ...] [>& /path/to/log.txt] The last example can be used to read mails from standard input and can
or: $progname [options] --create [file1 file2...] [>& /path/to/log.txt] useful when inserting mails to database via a program 'on-the-fly'.
The filename will be 'READ-FROM-STDIN' in this case.
Options: Options:
--help Show this help and exit. --help Show this help and exit.
--version Show the version number and exit. --version Show the version number and exit.
--debug Print some extra information during the run. --debug Print some extra information during the run.
--host=... Hostname to be used. (Using: $opt_host) --host=... Hostname to be used.
--port=# TCP/IP port to be used with connection. (Using: $opt_port) --port=# TCP/IP port to be used with connection.
--socket=... MySQL UNIX socket to be used with connection. --socket=... MySQL UNIX socket to be used with connection.
(Using: $opt_socket) --db=... Database to be used.
--db=... Database to be used. (Using: $opt_db) --table=... Table name for mails.
--table=... Table name for mails. (Using: $opt_table) --user=... Username for connecting.
--user=... Username for connecting. (Using: $opt_user)
--password=... Password for the user. --password=... Password for the user.
--stdin Read mails from stdin.
--max_mail_size=# Maximum size of a mail. --max_mail_size=# Maximum size of a mail.
Beware of the downside letting this variable be too big; Beware of the downside letting this variable be too big;
you may easily end up inserting a lot of attached you may easily end up inserting a lot of attached
binary files (like MS Word documents etc), which take binary files (like MS Word documents etc), which take
space, make the database slower and are not really space, make the database slower and are not really
searchable anyway. (Default: $opt_max_mail_size) searchable anyway.
--create Create the mails table. This can be done with the first run. --create Create the mails table. This can be done with the first run.
--test Dry run. Print the queries and the result as it would be. --test Dry run. Print the queries and the result as it would be.
--no_path When inserting the file name, leave out any paths of --no_path When inserting the file name, leave out any paths of
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment