#!/usr/bin/env perl
# -----------------------------------------------------------------------
# example_abuse_check.pl  --  demonstrate Email::Abuse::Investigator
#
# Usage:
#   perl example_abuse_check.pl spam.eml
#   perl example_abuse_check.pl < spam.eml
# -----------------------------------------------------------------------
use 5.010;

use strict;
use warnings;
use autodie qw(:all);

use utf8;
use open qw(:std :encoding(UTF-8));
use lib '.';					# find Email/Abuse/Investigator.pm nearby
use Email::Abuse::Investigator;

# ---- Read the raw email ------------------------------------------------
my $raw;
if (@ARGV) {
	# Validate path: reject NUL bytes, bare newlines, and directory-traversal
	# sequences before opening.  This also makes the path taint-safe under -T.
	my ($safe_path) = $ARGV[0] =~ /\A([^\x00\r\n]+)\z/
		or die "Invalid characters in path '$ARGV[0]'\n";
	die "Path traversal attempt in '$safe_path'\n"
		if $safe_path =~ m{(?:^|/)\.\.(?:/|$)};
	open my $fh, '<:raw', $safe_path or die "Cannot open $safe_path: $!";
	local $/;
	$raw = <$fh>;
	close $fh;
} else {
	binmode STDIN, ':raw';
	local $/;
	$raw = <STDIN>;
}
die 'No email data supplied' unless $raw && length $raw;

# ---- Analyse -----------------------------------------------------------
my $analyser = Email::Abuse::Investigator->new(
	verbose => 1,
	timeout => 15,
	# List your own outbound relay IPs/CIDRs here so they are skipped
	# when walking the Received: chain for the true origin:
	# trusted_relays => ['192.0.2.1', '198.51.100.0/24'],
);
$analyser->parse_email($raw);

# ---- Full analyst report -----------------------------------------------
print $analyser->report();

# ---- Send-ready abuse report text and contacts -------------------------
my @contacts = $analyser->abuse_contacts();
if (@contacts) {
	print "\n";
	print '=' x 72, "\n";
	print "  ABUSE REPORT TEXT (send this to each contact below)\n";
	print '=' x 72, "\n\n";
	print $analyser->abuse_report_text();

	print "\n";
	print '=' x 72, "\n";
	print "  SEND TO:\n";
	print '=' x 72, "\n";
	for my $c (@contacts) {
		printf "  %-45s  %s\n", $c->{address}, $c->{role};
	}
	print "\n";
} else {
	print "\n(No abuse contacts could be determined.)\n";
}

exit 0;
