#!/usr/bin/perl -w
#
# sa-remailer:  read a message and send as an attachment
# to SpamAssassin training addrs
#
#     usage:  sa-remailer (ham|spam) sender@addr
#
# Originally was for use with dovecot-antispam plugin,
# sa-remailer is to complement DTC's spam training,
# it accepts a message on stdin and sends it as an attachment
# to the SpamAssassin training addresses DTC sets up.
#
#  Copyright 2012 Jesse Norell <jesse@kci.net>
#  Copyright 2012 Kentec Communications, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

$ENV{"PATH"}="/bin:/usr/bin:/sbin:/usr/sbin";
umask 0640;

use strict;
use File::Temp;

my $DEBUG = 0;
my $REFORMIME = 'reformime';
my $SENDMAIL = 'sendmail';

my $spamham = "";
my $sender = "";
my $sender_args = "";
my $train_addr = "";
my $debug_file = "";

# get the system name from /etc/mailname for the spam training address
my $file='/etc/mailname';
open (FH, "< $file") or die "Can't open $file for read: $!\n";
chomp (my $mailname = <FH>);
close FH or die "Cannot close $file: $!\n"; 

($spamham, $sender) = @ARGV;

if (defined $spamham && $spamham =~ /^spam$|^ham$/) {
	$train_addr = "$spamham\@$mailname";
} elsif (defined $sender && $sender =~ /^spam$|^ham$/) {
	# dovecot config might have args swapped
	my $tmp = $spamham;
	$spamham = $sender;
	$sender = $tmp;
	$train_addr = "$spamham\@$mailname";
}  else {
	die "usage: $0 (spam|ham) [sender\@addr]\n";
}

# basic regex to match email addrs
if (defined $sender && $sender =~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i) {
	$sender_args = "-f $sender";
}

$file = mktemp("/var/tmp/sa-remailer.XXXXXX");
open (FH, "> $file") or die "Can't open $file for writing: $!\n";
while (<STDIN>) {
	print FH $_;
}
close FH or die "Cannot close $file: $!\n"; 

my $cmd = "$REFORMIME -m $file | $SENDMAIL $sender_args $train_addr";

if ($DEBUG) {
	$debug_file = mktemp("/var/tmp/sa-remailer.debug.XXXXXX");
	open (F, "> $debug_file") or die "Can't open debug file $debug_file for writing: $!\n";
	print F "env is:\n";
	my $k;
	foreach $k (keys %ENV) {
		print F "$k=$ENV{$k}\n";
	}
	print F "\n\nargv is:\n";
	my $i=0;
	foreach (@ARGV) {
		print F $i++."=$_\n";
	}
	print F "\nspamham is:  $spamham\n";
	print F "sender is:  $sender\n";
	print F "train_addr is:  $train_addr\n";
	print F "sender_args is:  $sender_args\n";
	print F "message content (stdin) written to:  $file\n";
	print F "remail command is:  $cmd\n";
	close F;
}

(system("$cmd") < 0) && die "Cannot execute reformime|sendmail: $!\n";

if (! $DEBUG) {
	unlink $file or warn "Could not unlink $file\n";
}

exit(0);

