in reply to Help appending data based on location

While I agree that a FSM is what you want and that fully parsing an smtp session is difficult and should be left to the professionals, I think the main thing missing here is keeping track of the RCPT TO addresses and then outputting them where you want them.

I did this with an @rcpts array and appended it after the headers. To do that, you have to keep track of where you are in the conversation so you know when to output the additional headers. Here is a fixed up version that uses your __DATA__

#!/usr/bin/perl use strict; use warnings; my @rcpts = (); my @linesBuffer = (); my $msgUpdateCnt = 0; my $state = 'unknown'; while (<DATA>) { s/\r?\n//; # strip newlines, since we don't know if they are ri +ght if ( $state eq 'headers' or $state eq 'body' ) { if ( $_ eq '.' ) { # end of message, reset $state = 'unknown'; } elsif ( $state eq 'headers' and $_ eq '' ) { # end of heade +rs foreach my $rcpt (@rcpts) { push @linesBuffer, 'To: ' . $rcpt; } $state = 'body'; } } elsif ( $_ eq 'DATA' ) { $state = 'headers'; } elsif (/^RCPT \s+ TO: \s* (.*)$/xms) { push @rcpts, $1; } elsif ( $_ eq 'RSET' or $_ eq 'QUIT' ) { # end of message if (@linesBuffer) { print join "\r\n", @linesBuffer, 'QUIT'; print "\r\n"; $msgUpdateCnt++; } # reset @linesBuffer = (); @rcpts = (); $state = 'unknown'; next; } if ( @linesBuffer || $_ ) { push @linesBuffer, $_; } } if (@linesBuffer) { print join "\r\n", @linesBuffer, 'QUIT'; print "\r\n"; $msgUpdateCnt++; } print "---------------------EQUALS $msgUpdateCnt--------------------\n +"; __DATA__
l8rZ,
--
andrew

Replies are listed 'Best First'.
Re^2: Help appending data based on location
by Anonymous Monk on Jan 27, 2011 at 13:38 UTC
    Thanks for the help/reply. I've been trying to modify your code b/c is currently does not remove the lines for each section prior to line ^DATA So, at the moment the sample data looks like this
    EHLO testdomain.com MAIL FROM:<outsideuser01@outdomain.com> SIZE=1016 BODY=7BIT RCPT TO:<testuser01@testdomain.com> RCPT TO:<testuser02@testdomain.com> DATA Received: from 1.1.1.1(helo=mvastnlufgt.mgrjofpxydauvu.info) by with esmtpa (Exim 4.69) (envelope-from ) id 1MM6HB-1114fm-3T for testuser01@testdomain.com; Sun, 9 Jan 2011 23:07:59 +0100 From: "outsideuser01" <outsideuser01@outdomain.com> To: <testuser01@testdomain.com> Subject: Re: good evening Date: Sun, 9 Jan 2011 23:07:59 +0100 MIME-Version: 1.0 To: <testuser01@testdomain.com> To: <testuser02@testdomain.com>
    It's correctly appending the RCPT lines but not removing the DATA before the line that starts with ^DATA Should look like this, note how it remove all line prior to ^DATA but only for the corresponding section.
    Received: from 1.1.1.1(helo=mvastnlufgt.mgrjofpxydauvu.info) by with esmtpa (Exim 4.69) (envelope-from ) id 1MM6HB-1114fm-3T for testuser01@testdomain.com; Sun, 9 Jan 2011 23:07:59 +0100 From: "outsideuser01" <outsideuser01@outdomain.com> To: <testuser01@testdomain.com> Subject: Re: good evening Date: Sun, 9 Jan 2011 23:07:59 +0100 MIME-Version: 1.0 To: <testuser01@testdomain.com> To: <testuser02@testdomain.com>
      I've been trying to modify your code b/c

      Show your efforts