#!/usr/bin/perl # # Respond to incoming mail message on STDIN # # hqm@ai.mit.edu # # modified for posgres by # Steven Caranci # caranci@eecg.toronto.edu # # This script does the following: # sub usage () { print ' usage: queue_message.pl db_datasrc db_user db_passwd destaddr Inserts the data from stdin into a queue table. Assumes the following table and sequence are defined in the db: create table incoming_email_queue ( id int4 not null, destaddr varchar(256), content text, -- the entire raw message content -- including all headers arrival_time datetime ); create sequence incoming_email_queue_sequence; '; } use Pg; ################################################################ # Global Definitions # max string size we allow in db. Truncate any message content beyond this #$MAX_MSGSIZE = 400000; # is limit of text type 1 block? # assume so for now. also assume 8k block. $MAX_MSGSIZE = 8000; $db_datasrc = shift; $db_user = shift; $db_passwd = shift; $destaddr = shift; # we don't support hosts/ports other than the default (localhost/5432). # we probably should. $DEBUG = 1; $debug_logfile = "/tmp/mailhandler-log.txt"; # if (!defined $db_datasrc) { $db_datasrc = 'dbi:Oracle:'; } if (!defined $db_user) { usage(); die("You must pass a db user in the command line"); } if (!defined $db_passwd) { usage(); die("You must pass a db passwd in the command line"); } ################################################################# ## Snarf down incoming msg on STDIN ################################################################# while (<>) { $content .= $_; } # double the single quotes $content =~ s/'/''/g; # limit content length $content = substr($content,0,$MAX_MSGSIZE); if ($DEBUG) { open (LOG, ">>$debug_logfile"); debug("================================================================\n"); debug("Received content:\n$content\n"); } # Open the database connection. $conn = Pg::connectdb("dbname=$db_datasrc user=$db_user password=$db_passwd"); if( $conn->status != PGRES_CONNECTION_OK ) { $errorMessage = $conn->errorMessage; debug( "errorMessage from Pg::connectdb: $errorMessage\n" ); die "Couldn't connect to database:\n $errorMessage\n"; } debug("Status: inserting into email queue\n"); $sql = "INSERT INTO incoming_email_queue (id, destaddr, content, arrival_time) VALUES (nextval('incoming_email_queue_sequence'), '$destaddr', '$content', 'now')"; $result = $conn->exec($sql); if( ($ntuples = $result->cmdTuples) != 1 ) { debug( "\nAck! ntuples != 1 (= $ntuples instead)\n" ); } debug("[closing log]\n"); if ($DEBUG) { close LOG; } sub debug () { my ($msg) = @_; print LOG $msg; }