#!/usr/bin/perl
#
# Conceived and implemented by Peter Kaminski <kaminski@istori.com>
# and Eugene Eric Kim <eekim@blueoxen.com> on October 16, 2005 at
# WikiSym.  Thanks to the good folks who attended the WikiSpam
# workshop.
#
# This code is public domain.  Do your worst.

use strict;
use LWP::UserAgent;
use URI::Escape;

my $BLACKLIST_URL = 'http://purplewiki.blueoxen.net/spam-blacklist.txt';
my $REAL_SCRIPT = 'wiki.pl';

undef $/;
my $postContent = <STDIN>;

my $ua = new LWP::UserAgent(agent => 'antispam wrapper v0.1');
my $request = new HTTP::Request('GET', $BLACKLIST_URL);
my $result = $ua->request($request);
    
my $blacklist;
if ($result->is_success) {
    $blacklist = $result->content;
} else {
    die "unable to retrieve content: " . $result->code();
}
my @regexps = &parseBlacklist($blacklist);
my $joinedRe = join('|', @regexps);

my $decodedPostContent = uri_unescape($postContent);
if ($decodedPostContent =~ /$joinedRe/) {
    foreach my $re (@regexps) {
	if ($decodedPostContent =~ /$re/) {
	    die "spammer posting $re!  dying intentionally...";
	}
    }
}
else {
    open FH, "|$REAL_SCRIPT" or die "$!";
    print FH $postContent;
    close FH;
}

### fini

sub parseBlacklist {
    my $rawBlacklist = shift;

    return split(/\n/, $rawBlacklist);
}

