#!/usr/bin/perl -w
#
use strict;
use warnings;
# don't forget to install libnet-ssleay-perl
use Net::SMTP::SSL;

# maximum size I allow for incoming emails
my $max_size = 200000; 
# banned extensions
my @banned_ext = qw(pps doc exe ppt);
my $from_email = 'YOUR-EMAIL';
my $smtp_host = 'YOUR-SMTP';
my $smtp_login = 'YOUR-SMTP-LOGIN';
my $smtp_passwd = 'YOUR-PASSWORD';

# incoming email (TODO: enhance and read a directory ?)
my $inputfile = $ARGV[0];
# declarations
my $msg = {
    subject => "",
    from => "",
    date => "",
    size => 0,
    filename => "",
    extension => "",
    isBannedExt => 0
};
my $mail = {
    from => "",
    to => "",
    subject => "",
    body => ""
};

# main program
read_eml($inputfile);
$msg->{isBannedExt} = is_extension_banned( $msg->{extension} );
make_mail();
send_mail();
exit;

# read an email (file name as argument) and retrieve important headers
sub read_eml {
    my $eml_file = shift;
    my $line;
    my $key;
    my $basename;

    open(FILE, "$eml_file") or die "Cant open $eml_file";
    while ($line = ) {
	chomp($line);
	
	if ($line =~ /^Subject/) {
	    ($key, $msg->{subject}) = split(/:/, $line);
	    $msg->{subject} =~ s/^\s+//; #remove leading spaces
        }

	if ($line =~ /^From/) {
	    ($key, $msg->{from}) = split(/:/, $line);
	    $msg->{from} =~ s/^\s+//; 
        }

        if ($line =~ /^Date/) {
	    ($key, $msg->{date}) = split(/:/, $line);
	    $msg->{date} =~ s/^\s+//; 
        }

	# filter attachment size and filename
        if ($line =~ /^Content-Disposition/) {
	    ($key, $basename) = split(/size=/, $line);
	    ($msg->{size}, $key) = split(/;/, $basename);
	    ($key, $msg->{filename}) = split(/filename=/, $line);
	    $msg->{filename} =~ s/^\s+//; 
	    ($basename, $msg->{extension}) = split(/\./, $msg->{filename});
        }
    }
    close(FILE)
}

# return 1 if mail contains a banned extension, 0 otherwise
sub is_extension_banned {
    my $extension = shift;
    
    foreach my $element (@banned_ext) {
	if ($extension eq $element) {  return 1; }
    }

    return 0;
}

# prepare the email. CUSTOMIZE THIS.
sub make_mail {
    $mail->{to} = $msg->{from};
    $mail->{from} = $from_email;
    $mail->{subject} = "Filtered attachment";
    $mail->{body} = "This mail has been generated by an anti-spam engine.\n\n";

    $mail->{body} .= "Your attachment in email '$msg->{date}' ";
    $mail->{body} .= "with subject '$msg->{subject}' has been filtered.\n";

    # special customization for banned or big messages
    if ($msg->{isBannedExt}) { 
	$mail->{body} .= "Extension '$msg->{extension}' is known to propagate hoaxes, spam, malware.\n";
    }

    if ($msg->{size} gt $max_size) {
	$mail->{body} .= "Your attachment exceeds $max_size bytes.\n\n";
    }
}

# send warning email to sender
sub send_mail {
    my $smtp;
    if (not $smtp = Net::SMTP::SSL->new($smtp_host,
					Port => 465,
					Debug => 1)) { # remove debug after a while
	die "Could not connect to $smtp_host\n";
    }

    $smtp->auth($smtp_login, $smtp_passwd) || die "$smtp_host: Authentication failed!\n";
    $smtp->mail($mail->{from} . "\n");
    $smtp->to($mail->{to} . "\n" );
    $smtp->data();
    $smtp->datasend("From: " . $mail->{from} . "\n");
    $smtp->datasend("To: " . $mail->{to} . "\n");
    $smtp->datasend("Subject: " . $mail->{subject} . "\n");
    $smtp->datasend("\n");
    $smtp->datasend($mail->{body} . "\n");
    $smtp->dataend();
    $smtp->quit;
}