#!/usr/bin/perl -wT
# ==========================================================================
#  spamreport.pl: Simple script to forward  SPAM messages to an antispam service
#  like spamcop (http://spamcop.net/) or any other.
# ------------------------------------------------------------------------
# Version    : 1.1.jim
# Created    : 26.11.2002
# Last Mod   : 05.02.2003
# Author     : heddy Boubaker 
# ==========================================================================
my $VERSION = "1.1";

use strict;
use Getopt::Std qw(getopts);
use vars (
    q!$opt_c!,# Cc
    q!$opt_f!,# From
    q!$opt_h!,# Help
    q!$opt_i!,# Message-ID
    q!$opt_s!,# Subject
    q!$opt_t!,# To
    q!$opt_v!,# Verbose
    q!$opt_D!,# Debug
    q!$opt_X!,# X-Mailer-Infos
);


# Secure
delete $ENV{PATH};


# Try to get sendmail program
my $sendmail = '/usr/sbin/sendmail';
unless ( -x $sendmail ) {
  $sendmail = '/usr/lib/sendmail';
  die "No sendmail program found here!" unless -x $sendmail;
}
my $sendmailargs = "-oi -t";
# Program to use instead of sendmail when using debug mode (-D)
my $sendmaildebug = '/bin/cat';

my ($pgn) = $0 =~ /\/([^\/]+)$/;

my $from = '';
my $to = '';
my $cc = '';
my $msgid = '';
my $subject = '';
my $verbose = 0;
my $DEBUG = 0;
my $XMI = '';

sub usage_exit () {
  print << "EOUSAGE";

 Usage: $pgn -s \"Subject\" -f from_addr -t to_addr -c cc_addr -i msgid -v -D < message

EOUSAGE
  exit 1;
}

### main() ###

# Get command line arguments...
&getopts('s:f:t:c:i:hvDX:') || &usage_exit();
&usage_exit() if ( ${opt_h} );
$verbose = 1  if ( ${opt_v} );
if ( $opt_D ) {
  $DEBUG = $verbose = 1;
  $sendmail     = $sendmaildebug;
  $sendmailargs = '';
  print "[$pgn] DEBUG mode.\n";
}

$msgid   = ${opt_i} || 'no-id';
$subject = ${opt_s} || "[$pgn] $msgid";

$XMI     = ${opt_X};
$XMI    .= '/' if $XMI;
$XMI    .= "$pgn v$VERSION";

if ( $opt_f ) {
  $from = $opt_f;
} else {
  &usage_exit();
}
if ( $opt_t ) {
  $to = $opt_t;
} else {
  &usage_exit();
}

$cc  = ${opt_c};
$cc  = $from if ( $cc eq 'from' || $cc eq 'sender' );
$cc  = ""    if ( $cc eq $to );
#$cc .= ", "  if ( $cc );
#$cc .= $spamarchive_addr;


# Prepare message
my $t         = time();
my $date      = localtime( $t );
my $boundary  = "-=-N-o-S-p-a-m-${t}-N-o-S-p-a-m-=-";
my $username  = (getpwuid($<))[6];

print "[$pgn] Opening |${sendmail} ${sendmailargs}\n" if $verbose;

open( MAIL, "|${sendmail} ${sendmailargs}" ) || die "Cannot open sendmail output: $!";

my $Cc = $cc?"Cc: ${cc}\n":"";

print "[$pgn] Sending mail to $to (cc: $cc) ...\n" if $verbose;

print MAIL  <<"ENDENDEND";
From: ${from}
To: ${to}
Subject: ${subject}
X-Loop: ${from}
X-Mailer-Infos: ${XMI}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="${boundary}"
${Cc}

This is a multi-part message in MIME format.
--${boundary}
Content-Type: text/plain; charset=us-ascii
Content-Description: Description
Content-Transfer-Encoding: 7bit

 Forward spam message is attached.


--${boundary}
Content-Type: message/rfc822
Content-Description: Spam message
Content-Disposition: attachment

ENDENDEND

  # Get message to forward from stdin
  while (  ) { print MAIL; }

print MAIL  <<"ENDENDEND";

--${boundary}--
ENDENDEND

# Send it and exit
close( MAIL );

print "[$pgn] Sending mail ... done\n" if $verbose;

exit 0;

# spamreport.pl ends here  ------------------------------------------------