#!/usr/bin/perl 
#
# Basic script to reinject amavis blocked mail into the postfix 
# non_content_filter. 
#
# version 0.06
# 
# History 
#  		- [0.02] Add support for final destination  ($finaldomain)
#		- [0.02] Support for Cc: also
#		- [0.02] Don't send to forwarded destination (variable following MUA)
# 		- [0.03] Add queue_id (thanks louis)
#		- [0.04] Multiline To & Bcc
#		- [0.05] To issue
#		- [0.06] Syslog info (id) and case sensivity of email
#
# Copyright (C) 2002 Alexandre Dulaunoy <adulau@foo.be>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

#
# sending mail, postfix SMTP version

# mail is piped back to postfix through a specific port

my $id = shift @ARGV;
my $refid = $id;
if ($id eq "")
{
 print "\nYou MUST re-enter an amavis id to reinject mail WITHOUT filtering\n";
 print "usage is :\n";
 print "./postfix_amavis_reinject <amavis-id>\n\n";
 die;
}
use Mail::Address;
use Unix::Syslog qw(:macros :subs);

my $finaldomain = "end-mail.mail-server.com|mail-server.com";
my $smtp_port = "10025";
my $amavis_path = "/var/virusmails";
my $SENDER = "reinject\@your.mail.server.com";
my (@RECIPS, $i);

open (FILETOINJECT, "$amavis_path/$id");
while (<FILETOINJECT>) {

if (/^-----Original/i) {$tag = 1;}
if (!($tag))
{
 #if (/^To: /) {if(/$finaldomain/){print "Reinject mail for : $_"; s/To: //; $RECIPS[$i] = $_; $i++;}}
 #if (/^Cc: /) {if(/$finaldomain/){print "Reinject mail for : $_"; s/Cc: //; $RECIPS[$i] = $_; $i++;}}

 if ( !( (/^Received/) || (/^From/) || (/^Message/) ) )
  {

   my $q = (Mail::Address->parse($_))[0];
   if (defined($q))
   {
    if (($q->address =~ /@/) && (/$finaldomain/i))
    {
	my $tmp = $q->address;
        $tmp =~ s/To://;
   	print "Reinject mail for : ".$tmp."\n";
	$RECIPS[$i] = $tmp; 
	$i++;
    }
   }
  
  }

}
}

close (FILETOINJECT);

open (FILETOINJECT, "$amavis_path/$id");
my $rv;
use Net::SMTP;
my $SMTP_HANDLE = Net::SMTP->new("127.0.0.1:$smtp_port",
			Hello => 'localhost',
			Timeout => 30,
			Debug => 0
			);
if (!defined($SMTP_HANDLE)) {
	print "Failure to open local SMTP port";
	die;
}
$SMTP_HANDLE->mail($SENDER);
$SMTP_HANDLE->to(@RECIPS);
$SMTP_HANDLE->data();
while (<FILETOINJECT>) {
	$SMTP_HANDLE->datasend($_);
}
$SMTP_HANDLE->dataend();
my $id = $SMTP_HANDLE->message();
print $id;
#my $id = $SMTP_HANDLE->message();
# Returns true for success
$rv = $SMTP_HANDLE->quit;
if (!$rv) {
	print "Unable to reinject mail : smtp quit returns $rv";
	die;
}

if ($rv) {
	#use Sys::Syslog;
	my @tmp = split(/queued as/,$id);
	my $intmsg = "reinject of $refid in queue as message-id:".@tmp[1]." ";
	openlog("postfix-reinject", LOG_PID, LOG_MAIL);
	syslog('info', $intmsg);
	closelog();
}
close (FILETOINJECT);

# End postfix        
