##################################################
# An 'pemf' filter
# http://marginalhacks.com/Hacks/pemf/
##################################################

# The subroutines are overriding the brainless defaults built into pemf

# Some subs are commented out here because the defaults will work fine,
# you only need to uncomment them to modify them

##################################################
# Personal mailbox settings
##################################################
## Mailbox where we receive mail
#sub inbox {
#  my ($home,$user) = @_;
#  -d "/var/spool/mail" ? "/var/spool/mail/$user" : "/usr/mail/$user";
#}

## Where we save mail folders
#sub save_mail { my ($home,$user) = @_; "$home/Mail"; }

###################################################
## Logging file (if -l specified)
###################################################
#sub logfile { my ($home,$user) = @_; "$home/Mail/pemf.log"; }

##################################################
# Save inbound mail
##################################################
# Uncomment if you want to save inbound mail, and do: 'mkdir ~/Mail/INBOUND'

#my @TIME = localtime(time);
## Path to save inbound mail (0 to not save)
#sub save_inbound {
#  # Save every month
#  sprintf(save_mail(@_)."/INBOUND/%d.%0.2d",$TIME[5]+1900,$TIME[4]+1);
#}
## Number of lines to save (-1 for unlimited)
#sub save_inbound_lines { 100; }

##################################################
# Auto responder
##################################################
# For the auto responder message:  "MY_NAME's Auto Responder"
sub my_name { my ($home,$user) = @_;  $user; }

# We may have multiple email addresses.  If we are creating an auto_response,
# we generally want to pick the email address based on which of our email
# addresses they originally mailed to ($addr), but in some cases we may
# not want to reveal some addresses to them.  At least I don't.  So, you
# figure out what you want to do here if it's more complicated than this.
#
# This takes a list of addresses that the message was or may have been
# addressed to (in some order of importance, "to:" fields first and
# "Received: for" fields last.
#
# As a simple example, we'll just return the first address (that would
# be the first address in the "To:" list).  Unfortunately this may not
# be ours as the SPAM may have gone to many recipients.
#
# Two possible solutions:
# 1) Just specify your email address (or a respond-to-spam email address)
# 2) Search the list for one of your email addresses and use that
sub auto_respond_from {
  my (@addr) = @_;
  return $addr[0];		# YOU PROBABLY WANT TO CHANGE THIS!!
}

# A unique key that we put in autoresponse subjects so we can recognize bounces
sub respond_key { "[pemf_resp]"; }

##################################################
# SPAM settings
##################################################

# Some addresses I have have been sold-out and only receive spam, pretty much.
# Is an address (or list of addresses) one that I only receive spam at?
sub is_spam_to {
  # Can be comma-separated-list of all possible addresses this mail
  # was received for:
  my ($to) = @_;
  
  # Simple example, webmaster@... is generally spam, and I've seen some
  # forged spam that purports to be "to" some email @post.com
  grep($to =~ /(^|,)$_/i, qw(
	webmaster@
	@post.com
	)) ? 1 : 0;
}

# Address regexps of spamming bastards!
sub is_spam_from {
  my ($from) = @_;
  grep($from =~ /$_/i, qw(
	news?picks@ @mailpride @safe-bill @paid4survey verticalresponse.com
	@thetravelclub @firesale.com compu-zilla.com 163.net
	)) ? 1 : 0;
}

# Common spam subjects?
#   See: http://www.annexia.org/spam/files-index.html
sub is_spam_subject {
  my ($subject) = @_;
  grep($subject =~ /$_/, (
	"^ADV:",
	#"contest confirmation",
	#"think about this\.\.",
	"Got Debt\?",
	"Get listed 10 Major Engines",
	"(?i)programmers available now\!",
	"(?i)low cost merchant accounts",
	"(?i)Expand Your Business",
	"(?i)Double Your Sales",
	"(?i)advertise .* for free",
	"(?i)free.*motorola.*pager",
	"(?i)find.*missing.*people",
	"(?i)free.*satellite.*t.v.",
	"(?i)protect computer data",
	)) ? 1 : 0;
}

# Any other spam checks, based on the header hash
sub is_spam_hdr {
  my ($msg) = @_;
  return 1 if $msg->{headers}{received} =~ /outblaze\.com/;	# Spam creators galore..
  #return 1 if $msg->{headers}{received} =~ /may be forged/i;
  return 0;
}

# Senders that are *NOT* spam
# (For example, if we have our whois addresses as spam, we may still
#  want to get email at that address if it's from our registrar)
# (Although I'm starting to question whether mail from netsol is spam  ;-)
sub never_spam_from {
  my ($from) = @_;
  grep($from =~ /$_/i, qw(
	networksolutions.com netsol.com	verisign.com
	dotster registerapi.com
	)) ? 1 : 0;
}

sub mail_filter {
  my ($msg) = @_;

  # Simple examples:

  # Send to a file:
#  send_to($msg,"test") if ($msg->{headers}{subject} =~ /test/i);

  # Forward to another address
#  send_to($msg,"root\@localhost") if ($msg->{headers}{subject} =~ /cron/i);

  # Pipe to a command
#  send_to($msg,"|/home/madison/bin/wango") if ($msg->{headers}{subject} eq "wongo");

  # send_to will usually exit unless we give a third arg of 1
#  send_to($msg,"EXTRA_MAIL",1);	# Save to EXTRA_MAIL and keep processing

  ##################################################
  # Mailer Daemon, ignore autoresponse bounces
  ##################################################
  if ($msg->{headers}{FROM} =~ /mailer.daemon/i || $msg->{headers}{from} =~ /postmaster/i) {
    # See if it's just a spam bounce
    if (is_auto_respond($msg)) {
      print STDERR "[bounced auto-respond] DELETED\n";
      exit;
    }
  }

  ##################################################
  # SPAM
  ##################################################
  if (is_spam($msg)) {
    $msg->{no_save_inbound} = 1;	# Don't save this in my INBOUND

#-------------
    auto_respond_mail($msg,"SPAM WARNING - REMOVE [re: $msg->{headers}{subject}]",<<END_MESSAGE);

My automated mail filter determined that email you sent me was
unsolicited commercial email ("spam")

From:     $msg->{headers}{FROM}
Date:     $msg->{headers}{DATE}
Subject:  $msg->{headers}{subject}


IF THE MAIL YOU SENT WAS NOT SPAM:

  Many apologies for the error!  My filter software is not perfect!
  I have saved (but not read the mail you sent).  You can send me a new
  mail with the subject "CONTACT REQUEST" asking me to read the original,
  and I'll try to fix whatever caused the email to get flagged as spam.

IF THE MAIL YOU SENT WAS SPAM:

  This is in violation of state law because it violates
  the Terms Of Use of my mail server.

  Do not *ever* mail me again.

END_MESSAGE
#-------------

    print STDERR "Determined to be SPAM\n";
    send_to($msg,"SPAM");	# I like to save my SPAM, it cracks me up
  }


  # Send copies of CRITICAL email to my cell
#  send_to($msg,"MY_CELLPHONE_EMAIL",1)
#    if ($msg->{headers}{subject} =~ /CRITICAL/);

  # Ignore quoted printable crap, great for text mail readers like elm
#  $msg->{clean_equals} = 1
#    if ($msg->{headers}{'content-transfer-encoding'} =~ /quoted.*printable/i);

  ##################################################
  # Default MBOX
  ##################################################
  send_to($msg);
}

1;
