Code:
#!/usr/bin/perl
use DBI;
$driver ="mysql";
$user = "myuser";
$password = "mypwd";
$munpack = "/path/to/munpack";
$msgstore = "/path/to/msgstore";
$reldir = "msgstore";
umask( 0 );
while ( $line = <STDIN> )
{
@tmp = split( ": ", $line );
if ( lc($tmp[0]) eq "to" )
{
chop( $tmp[1] );
$to = $tmp[1];
}
elsif ( lc($tmp[0]) eq 'from' )
{
chop( $tmp[1] );
$from = $tmp[1];
}
elsif ( lc($tmp[0]) eq 'subject' )
{
chop( $tmp[1] );
$subject = $tmp[1];
}
elsif ( lc($tmp[0]) eq 'content-type' )
{
last;
}
}
$dsn = "DBI:$driver:database=mydbase;host=localhost";
$dbh = DBI->connect( $dsn, $user, $password );
$id = isValid( $to, $from ) || die( "Not a valid user $to $from" );
$dirname = $msgstore . "/" . $$ . "_email";
mkdir( $dirname, 0775 );
open(UNPACK,"| $munpack -C $dirname > $dirname/info.txt 2> /dev/null" )
|| die( "Could not start unpacker program");
print( UNPACK $line );
while ( $line = <STDIN> )
{
print( UNPACK $line );
}
close( UNPACK );
$info = $dirname . '/info.txt';
open( INFO, $info ) || die( "Could not open info file [$!]" );
$size = 0;
while ( $line = <INFO> )
{
@tmp = split( " ", $line );
$tmp[1] = substr( $tmp[1], 1, length($tmp[1])-2 );
$content{$tmp[0]} = $tmp[1];
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat( $dirname . "/" . $tmp[0] );
$size += $blksize * $blocks;
$content_size{$tmp[0]} = $size;
}
close( INFO );
$mid = storeMessage( $to, $id, $subject, int( $size / 1024 ) );
foreach $file ( keys(%content) )
{
storeContent( $mid, $file, $content{$file}, $content_size{$file} );
}
$txt = getTextForMid( $mid );
if ( strlen( $txt ) > 0 )
{
setTextForMid( $mid, $txt );
}
$dbh->disconnect();
sub storeContent
{
($mid, $file, $type, $size) = @_;
$fname = $reldir . "/" . $$ . "_email/". $file;
$query = "INSERT INTO content VALUES " .
"(NULL, $mid, '$type', '$fname', $size )";
chmod( 0775, $msgstore . "/" . $$ . "_email/". $fname );
$sth = $dbh->prepare($query) || die;
$sth->execute();
}
sub setTextForMid
{
( $mid, $txt ) = @_;
$query = "UPDATE messages SET textmsg='$txt' WHERE id=$mid";
$sth = $dbh->prepare($query) || die;
$sth->execute();
}
sub getTextForMid
{
($mid) = @_;
$query = "SELECT fname FROM content WHERE mid=$mid && ".
"contenttype='text/plain'";
$sth = $dbh->prepare($query) || die;
$sth->execute();
if ( @row = $sth->fetchrow() )
{
$fname = $row[0];
open( TXTFILE, $msgstore . "../" . $fname );
@content = <TXTFILE>;
$txt = join('', @content);
return $txt;
}
return "";
}
sub storeMessage
{
($to, $sid, $subject, $size ) = @_;
$subject = $dbh->quote($subject);
$query = "INSERT INTO messages VALUES " .
"(NULL, $sid, 1, '$to', $subject, NOW(), 'email', '0.0.0.0', 0, 0, '', 0 )";
$sth = $dbh->prepare($query) || die;
$sth->execute();
$query = "SELECT LAST_INSERT_ID() FROM messages";
$sth = $dbh->prepare($query) || die;
$sth->execute();
@row = $sth->fetchrow();
$id = $row[0];
$query = "UPDATE users SET nkbytes=nkbytes+$size, ".
" lastupload=NOW(), ".
" nuploads=nuploads+1 WHERE id=$sid";
$sth = $dbh->prepare($query) || die;
$sth->execute();
return $id;
}
#isValid
#to-address is on form p<number>@domain.com
#from-address is on form <MSISDN>@domain.com
sub isValid
{
($to, $from) = @_;
@tos = split( "@", $to );
$pwd = substr( $tos[0], 2 );
if ( ($index = index( $pwd, "<" ) ) >= 0 )
{
$pwd = substr( $pwd, $index + 3 );
}
@froms = split( "@", $from );
$msisdn = $froms[0];
if ( ($index = index( $msisdn, "<" )) >= 0 )
{
$msisdn = substr( $msisdn, $index + 1 );
}
$query = "SELECT id FROM users WHERE msisdn='$msisdn' && phonepwd='$pwd'";
$sth = $dbh->prepare($query) || die;
$sth->execute();
$id = 0;
if ( @row = $sth->fetchrow() )
{
$id = $row[0];
}
$sth->finish();
$id || die( "msisdn is $msisdn and pwd is $pwd" );
return $id;
}
Cheers all!