Refactor for multiple emails per session (#305)

* Refactor for multiple emails per session

* Add close_callback to milter
This commit is contained in:
Shawn Iverson 2018-11-21 16:13:38 -05:00 committed by GitHub
parent e9773ffe44
commit 5521a030ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -165,6 +165,45 @@ sub envrcpt_callback
# Watch for second callback
if ( ${$message_ref} !~ /MailScanner Milter/ ) {
# Is this a subsequent message? Grab previous message id to reconstruct header
if ( ${$message_ref} =~ /^[0-9A-F]{7,20}|[0-9B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z]{8,20}$/ ) {
# Read envelope
my $queuehandle = new FileHandle;
my $incoming = MailScanner::Config::Value('inqueuedir');
MailScanner::Log::DebugLog("eoh_callback: incoming = " . @{$incoming}[0]);
if ($incoming eq '') {
MailScanner::Log::WarnLog("Unable to determine incoming queue!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $file = @{$incoming}[0] . '/temp-' . ${$message_ref};
my $file2 = @{$incoming}[0] . '/' . ${$message_ref};
MailScanner::Log::DebugLog("envrcpt_callback: file = $file");
my $ret;
$ret = MailScanner::Lock::openlock($queuehandle,'+<' . $file, 'w');
if ($ret != 1) {
MailScanner::Log::WarnLog("Unable to to open temp queue file for reading!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $data;
my $pos;
# Locate predata header
while($data = readline $queuehandle) {
last if $data !~ /^(O<|S<)/;
}
${$message_ref} = $data;
MailScanner::Lock::unlockclose($queuehandle);
# Done with previous message, make ready for mailscanner
move($file, $file2);
}
# Capture the Mail From address for further processing
if (defined($symbols->{'M'}) && defined($symbols->{'M'}->{'{mail_addr}'})) {
$mailfrom=$symbols->{'M'}->{'{mail_addr}'};
@ -387,24 +426,36 @@ sub eom_callback
{
my $ctx = shift;
my $message_ref = $ctx->getpriv();
my $incoming = MailScanner::Config::Value('inqueuedir');
MailScanner::Log::DebugLog("eom_callback: incoming = " . @{$incoming}[0]);
if ($incoming eq '') {
MailScanner::Log::WarnLog("Unable to determine incoming queue!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $file = @{$incoming}[0] . '/temp-' . ${$message_ref};
my $file2 = @{$incoming}[0] . '/' . ${$message_ref};
move($file, $file2);
$ctx->setpriv(undef);
# Store reference for subsequent messages in connection or for connection close
$ctx->setpriv($message_ref);
# Send DISCARD signal to accept message and drop from postfix
# for mailscanner processing
Sendmail::PMilter::SMFIS_DISCARD;
}
sub close_callback
{
my $ctx = shift;
my $message_ref = $ctx->getpriv();
my $incoming = MailScanner::Config::Value('inqueuedir');
MailScanner::Log::DebugLog("eom_callback: incoming = " . @{$incoming}[0]);
if ($incoming eq '') {
MailScanner::Log::WarnLog("Unable to determine incoming queue!");
Sendmail::PMilter::SMFIS_TEMPFAIL;
return;
}
my $file = @{$incoming}[0] . '/temp-' . ${$message_ref};
my $file2 = @{$incoming}[0] . '/' . ${$message_ref};
move($file, $file2);
$ctx->setpriv(undef);
Sendmail::PMilter::SMFIS_CONTINUE;
}
#
@ -512,6 +563,7 @@ BEGIN {
'eoh' => \&eoh_callback,
'body' => \&body_callback,
'eom' => \&eom_callback,
'close' => \&close_callback,
);
my $conn = 'inet:'. $port . '@' . $bind;