hellkvist.org Forum Index hellkvist.org
Discussions about the free software on hellkvist.org
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Email method problems

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    hellkvist.org Forum Index -> Peffisaur
View previous topic :: View next topic  
Author Message
mwright



Joined: 04 Mar 2004
Posts: 33

PostPosted: Wed Mar 31, 2004 11:23 am    Post subject: Email method problems Reply with quote

I seem to be having problems getting the email interface set up.

I have setup procmail to forward all the emails to the specified account and recmail.pl runs for each message.

Here is the log for the last couple of runs

==================
From xxx@yyy Fri Mar 26 16:15:16 2004
Subject:
Folder: /home/peffisaur/recmail.pl 25737
Undefined subroutine &main::strlen called at /home/peffisaur/recmail.pl line 88.
From xxx@yyy Mon Mar 29 15:16:46 2004
Subject:
Folder: /home/peffisaur/recmail.pl 25502
DBI::db=HASH(0x81f88c4)->disconnect invalidates 1 active statement handle (either destroy statement handles or call finish on them before disconnecting) at /home/peffisaur/recmail.pl line 94.
====================

The first one seems to be a bug in recmail.pl - I changed "strlen" to "length" and then I get the second result.

Any ideas?


Last edited by mwright on Wed Mar 31, 2004 3:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Wed Mar 31, 2004 3:22 pm    Post subject: Reply with quote

Ok, seems like it depends on the version of perl. Which one are you using?
If strlen is missing in your system you can use length instead. So on line 88 you change strlen to length.

The second one you could simply remove. So remove line 94 with the disconnect. The connection to the database will be disconnected anyway when the process exits. I don't know why it complains though as it has never done it for me.

/Stefan
Back to top
View user's profile Send private message Visit poster's website
mwright



Joined: 04 Mar 2004
Posts: 33

PostPosted: Wed Mar 31, 2004 3:40 pm    Post subject: Reply with quote

OK - perl reports "This is perl, v5.6.1 built for i386-linux"

I have made the changes and will test again later.

Cheers
Back to top
View user's profile Send private message
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Wed Mar 31, 2004 3:44 pm    Post subject: Reply with quote

Ok, to be honest I don't know why I used strlen. Perhaps I guessed since it exists in C. But I don't know why it works in my script and not in yours. My version is v.5.6.0. But length is really what you SHOULD use.

/S
Back to top
View user's profile Send private message Visit poster's website
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Wed Mar 31, 2004 4:26 pm    Post subject: Reply with quote

It seems the recmail.pl sure had a few issues when I looked throug it now. It has worked for me for some time but you could not have texts under the image when using email. I've fixed the script and I add it here (might release a new version of Peffisaur with some bug fixes):
Code:

#!/usr/bin/perl

use DBI;

$driver ="mysql";
$user = "peffisaur";
$password = "hush-hush";
$munpack = "/usr/bin/munpack";
$msgstore = "/var/www/html/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=peffisaur;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 -q -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 ( length( $txt ) > 0 )
{
    setTextForMid( $mid, $txt );
}


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;
}
Back to top
View user's profile Send private message Visit poster's website
mwright



Joined: 04 Mar 2004
Posts: 33

PostPosted: Wed Mar 31, 2004 9:11 pm    Post subject: Reply with quote

FORGET THIS - I had forgotten to edit the recmail.pl file!!
SORRY!!

With the new recmail I get

=============
From 447947999999@orangemms.net Wed Mar 31 21:17:47 2004
Subject: Multimedia message
Folder: /home/peffisaur/recmail.pl 7950
sh: /var/www/html/msgstore/24563_email/info.txt: No such file or directory
Could not open info file [No such file or directory] at /home/peffisaur/recmail.pl line 62, <STDIN> line 142.
=============

Note that the "old" version works fine with my T610!!

HELP!
Back to top
View user's profile Send private message
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Wed Mar 31, 2004 9:30 pm    Post subject: Reply with quote

Really? Perhaps your munpack is located at somewhere else and you had edited that in your old file? (perhaps it's not in /usr/bin as in my file above).

/S
Back to top
View user's profile Send private message Visit poster's website
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Wed Mar 31, 2004 9:31 pm    Post subject: Reply with quote

You should however take a look and compare the files because I did fix some bugs in this one (you could for instance not send text attachmetns with the old one, at least they did not appear on the site).

/S
Back to top
View user's profile Send private message Visit poster's website
mwright



Joined: 04 Mar 2004
Posts: 33

PostPosted: Wed Mar 31, 2004 9:38 pm    Post subject: Reply with quote

Yep - it works fine from my desktop email client.

Just one small detail though:-

From the phone the email address has to be p-xxxxxxxx, but from the desktop (Outlook) it needs to be pxxxxxxx (one character shorter before the start of the real password)

Go figure!!
Back to top
View user's profile Send private message
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Thu Apr 01, 2004 9:19 am    Post subject: Reply with quote

That sounds really strange to me...I think there might be a problem in how it treats the addresses. I send emails from my email client (Mozilla) and then I use p-xxx@peffis.com so it works then. What I suspect is that when you send it with Outlook your email client uses the more complex address scheme with name and all liike "Stefan Hellkvist" <stefan@....> and there might simply be an error in how I handle that. That's the only explanation I can give why it should be different from your phone (that I guess uses the simple addressing scheme) and your Outlook that uses the more complex one.

I'll look into it
Back to top
View user's profile Send private message Visit poster's website
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Thu Apr 01, 2004 10:02 am    Post subject: Reply with quote

You could try changing the isValid method to:

Code:

sub isValid
{
     ($to, $from) = @_;
      $end = index( $to, "@" );
      $start = index( $to, "p-" );
      $pwd = substr( $to, 2, $end - $start - 2 );

      @froms = split( "@", $from );

      ...
}
Back to top
View user's profile Send private message Visit poster's website
mwright



Joined: 04 Mar 2004
Posts: 33

PostPosted: Thu Apr 01, 2004 10:20 am    Post subject: Reply with quote

Almost - try this

($to, $from) = @_;
$end = index( $to, "@" );
$start = index( $to, "p-" );
$pwd = substr( $to, $start+2, $end - $start - 2 );
Back to top
View user's profile Send private message
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Thu Apr 01, 2004 11:17 am    Post subject: Reply with quote

Yes, that was what I meant Smile
Back to top
View user's profile Send private message Visit poster's website
Peffis
Site Admin


Joined: 09 Sep 2003
Posts: 324
Location: Sweden

PostPosted: Thu Apr 01, 2004 3:20 pm    Post subject: Reply with quote

I found some other problems as well though when I tried it. Some fields are not quoted before they go into the data base so if you had ':s and ":s in the to-field or from-field (Outlook puts them in the to-field if you just type an address) the message did not go in. I fixed all that as well so now I have a file that hopefully works for all addressing schemes.

Check it out if you like, but change the first lines according to your system setup (like your password, where you have your munpack and where your msgstore is located)

Code:
#!/usr/bin/perl

use DBI;

$driver ="mysql";
$user = "peffisaur";
$password = "hush-hush";
$munpack = "/usr/bin/munpack";
$msgstore = "/var/www/html/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=peffisaur;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 -q -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 ( length( $txt ) > 0 )
{
    setTextForMid( $mid, $txt );
}


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 ) = @_;
    $txt = $dbh->quote( $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);
    $to = $dbh->quote($to);
    $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) = @_;
    $end = index( $to, "@" );
    $start = index( $to, "p-" );
    $pwd = substr( $to, $start + 2, $end - $start - 2 );
   
    @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;
}

Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    hellkvist.org Forum Index -> Peffisaur All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group