Spam-guarding email addresses


I’ve been playing with jQuery recently. The major project I’m just about ready to roll out is a significant improvement to my pop-up furigana-izer. If native tooltips actually worked reliably in browsers, it would be fine, but they don’t, so I spent a day sorting out all of the issues, and while I was at it, I also added optional kana-to-romaji conversion.

I’ll probably roll that out this weekend, updating a bunch of my old Japanese entries to use it, but while I was finishing it up I had another idea: effective spam-protection for email addresses on my comment pages. The idea is simple. Replace all links that look like this: email with something like this: email, and use jQuery to reassemble the real address from a hash table when the page is loaded, inserting it into the HREF attribute.

A full working example looks like this:





 
J Greely
 

Seeding the hash with random values makes it hard to pick out the components of real email addresses (there’s another one in this example…). Creating the hash was trivial in Perl, and I can swipe code from my old Movable Type plugins to get it working there. Here’s the Perl:

foreach (1..50) {
        $mailto{randomstring()} = randomstring();
}
foreach (@ARGV) {
        my $address = hideaddress($_);
}
sub hideaddress {
        my ($address) = @_;
        my $hidden = "";
        while ($address ne "") {
                my $key = randomstring();
                redo if defined $mailto{$key};
                my $n = rand(2)+3;
                my $val = substr($address,0,$n);
                $mailto{$key} = $val;
                substr($address,0,$n) = "";
                $hidden .= "$key ";
        }
        chop($hidden);
        return $hidden;
}
sub randomstring {
        my $chars = "abcdefghijklmnopqrstuvwxyz01234567890@.";
        my $n = rand(2)+2;
        my $s;
        while ($n-->0) {
                $s .= substr($chars,rand(length($chars)),1);
        }
        return $s;
}