#!/usr/bin/perl use strict; use warnings; my @rcpts = (); my @linesBuffer = (); my $msgUpdateCnt = 0; my $state = 'unknown'; while () { s/\r?\n//; # strip newlines, since we don't know if they are right if ( $state eq 'headers' or $state eq 'body' ) { if ( $_ eq '.' ) { # end of message, reset $state = 'unknown'; } elsif ( $state eq 'headers' and $_ eq '' ) { # end of headers 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__