#!/usr/bin/perl
# $Header: /usr/home/johnl/web/abuse/RCS/mspam.txt,v 1.1 1997/03/07 01:12:29 johnl Exp $ 
# -*- perl -*-

# create a form complaint from a spam message
# John Levine, Jan 97
# dedicated to the public domain, distributed free with a money-back
# guarantee

require "getopts.pl";

&Getopts("fd");

$hostname = `hostname`;	# put your system name here

# ADJUST THE LINE BELOW IF YOUR SYSTEM USES A DIFFERENT MAIL TRANSFER PROGRAM
#  THAN SENDMAIL OR SMAIL
@mail = ("/usr/lib/sendmail", "-i", $ENV{'LOGNAME'});	

# read the headers
@headers = ();
while(<STDIN>) {
    last if /^$/;
    next if /^From /o;
    next if /^>From /o;
    if(/^\s/) {
	push @headers, (pop(@headers) . $_);
    } else {
	push @headers, $_;
    }

    $received = 1 if /^Received:/oi;

    # not quite right in case of continuation, but close enough
    $subject = $1 if /^Subject: +(.*)/oi;
    $from = $1 if /^From: +(.*)/oi;
    $replyto = $1 if /^Reply-To: +(.*)/oi;
    $sender = $1 if /^Sender: +(.*)/oi;
    $realsender = $1 if /^Return-Path: +<(.*)>/oi;
}

die "No received lines, trace unlikely.\n" unless $received;

# figure out who it's from
$from = $sender unless $from;

chomp($from);
chomp($subject);

# now strip out the crud
if( $from =~ /<(.*)>/) {
    $from = $1;
} else {
    $from =~ s/\s*\([^)]*\)\s*//g; # strip comments
}

# now read in the body
@body = ();

while(<STDIN>) {
    push @body, $_;
}

# spamize the addresses

unless($opt_f) {
    @to = (&spamize($from));
    push (@to, &spamize($replyto)) if $replyto;
}

for $i (@ARGV) {
    push (@to, &spamize($i));
}

$| = 1;

open(MAIL, "|-") || exec (@mail,@to);

$tolist = join(", ", @to);
print MAIL <<"EOF";
To: $tolist
Subject: Spam alert

I recently received the following message, which appears to be from
one of your users.  It looks to me like a spam, unsolicited commercial
e-mail.  Such mail is very annoying and widely considered to be abusive.

Could you encourage him/her/it to cut it out?  Thanks.

---spam follows---
EOF

print MAIL join("", @headers);
print MAIL "\n";

foreach $i (0..100) {
	last if $i > $#body;
	print MAIL $body[$i];
}

close(MAIL);

print STDERR "Notified $tolist\n" unless $?;

##########

sub spamize {
    my ($in) = @_;

    $in =~ s/.*\@//o;
    $in . "\@abuse.net";
}
