Home > Uncategorized > A Simply Ruby-based Blacklist to Selectively Bounce Mail in QMail

A Simply Ruby-based Blacklist to Selectively Bounce Mail in QMail

April 1st, 2009

After irritation at e-mail list keepers who don’t respect unsubscribe requests, but who’s messages I don’t want to introduce into my spam filter, I hacked out this little mechanism to selectively bounce e-mail back to them. I never see it. They get bounces. My spam filter is unaldultered. Perfect.

I typically publish a single e-mail address which is hosted by a Linux server running qmail. I collect e-mail from diverse sources here and forward it to GMail, which I use mostly as a client, preferring my own domain(s) to the @gmail.com domain.

The solution is simple, effective, and might it be useful for you, too?

First, my ~/.qmail file.

|bouncesaying ‘Your message failed to deliver. If you believe there has been an error, please contact the intended recipient by another means to verify their information.’ ~dulles/bin/blacklist.rb
&a_gmail_acct_where_i_read_mail@gmail.com

Next, my blacklist, kept in ~/.blacklist

xyz@bad_man.com
you_know_who_you_are@nutjob.net

Last, the little ruby script mentioned above.

#!/usr/bin/ruby

def send_it
   # puts “Sending it!”
   exit 1
end

def bounce_it
   # puts “Bouncing it!”
   exit 0
end

# Initial blacklist
blacklist = Array.new

# Open the blacklist file and grab the addresses
blacklist_source = File.open(File.expand_path(”~dulles/.blacklist”,”r”))
blacklist_source.each_line { |addr|
   blacklist.push(addr.chomp)
}
blacklist_source.close

# Grab the message and parse out the from line
message = STDIN.readlines.join
message =~ /^from:\s+.*?(\w+@\w+\.\w+).*?$/i
sender = $1
#puts “Sender is “+sender

# No sender? Just continue sending the message
if(sender.nil?) then send_it end

# If the sender matches anybody on the blacklist, bounce!
blacklist.each { |blocked_addr|
   if sender.downcase == blocked_addr.downcase then bounce_it end
}

# default action: send the message
send_it

Share and Enjoy:
  • Facebook
  • E-mail this story to a friend!
  • Reddit
  • Digg
  • del.icio.us

dulles

  1. No comments yet.
  1. No trackbacks yet.
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States