<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::blogspam;

use warnings;
use strict;
use IkiWiki 3.00;
use Encode;

my $defaulturl='http://test.blogspam.net:8888/';

sub import {
	hook(type =&gt; "getsetup", id =&gt; "blogspam",  call =&gt; \&amp;getsetup);
	hook(type =&gt; "checkconfig", id =&gt; "blogspam", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "checkcontent", id =&gt; "blogspam", call =&gt; \&amp;checkcontent);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 0,
			section =&gt; "auth",
		},
		blogspam_pagespec =&gt; {
			type =&gt; 'pagespec',
			example =&gt; 'postcomment(*)',
			description =&gt; 'PageSpec of pages to check for spam',
			link =&gt; 'ikiwiki/PageSpec',
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		blogspam_options =&gt; {
			type =&gt; "string",
			example =&gt; "blacklist=1.2.3.4,blacklist=8.7.6.5,max-links=10",
			description =&gt; "options to send to blogspam server",
			link =&gt; "http://blogspam.net/api/testComment.html#options",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		blogspam_server =&gt; {
			type =&gt; "string",
			default =&gt; $defaulturl,
			description =&gt; "blogspam server XML-RPC url",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
}

sub checkconfig () {
	# This is done at checkconfig time because printing an error
	# if the module is missing when a spam is posted would not
	# let the admin know about the problem.
	eval q{
		use RPC::XML;
		use RPC::XML::Client;
	};
	error $@ if $@;
}

sub checkcontent (@) {
	my %params=@_;
	my $session=$params{session};
	
 	if (exists $config{blogspam_pagespec}) {
		return undef
			if ! pagespec_match($params{page}, $config{blogspam_pagespec},
	                	location =&gt; $params{page});
	}

	my $url=$defaulturl;
	$url = $config{blogspam_server} if exists $config{blogspam_server};

	my $client = RPC::XML::Client-&gt;new($url);

	my @options = split(",", $config{blogspam_options})
		if exists $config{blogspam_options};

	# Allow short comments and whitespace-only edits, unless the user
	# has overridden min-words themselves.
	push @options, "min-words=0"
		unless grep /^min-words=/i, @options;
	# Wiki pages can have a lot of urls, unless the user specifically
	# wants to limit them.
	push @options, "exclude=lotsaurls"
		unless grep /^max-links/i, @options;
	# Unless the user specified a size check, disable such checking.
	push @options, "exclude=size"
		unless grep /^(?:max|min)-size/i, @options;
	# This test has absurd false positives on words like "alpha"
	# and "buy".
	push @options, "exclude=stopwords";

	my %req=(
		ip =&gt; $session-&gt;remote_addr(),
		comment =&gt; encode_utf8(defined $params{diff} ? $params{diff} : $params{content}),
		subject =&gt; encode_utf8(defined $params{subject} ? $params{subject} : ""),
		name =&gt; encode_utf8(defined $params{author} ? $params{author} : ""),
		link =&gt; encode_utf8(exists $params{url} ? $params{url} : ""),
		options =&gt; join(",", @options),
		site =&gt; encode_utf8($config{url}),
		version =&gt; "ikiwiki ".$IkiWiki::version,
	);
	my $res = $client-&gt;send_request('testComment', \%req);

	if (! ref $res || ! defined $res-&gt;value) {
		debug("failed to get response from blogspam server ($url)");
		return undef;
	}
	elsif ($res-&gt;value =~ /^SPAM:(.*)/) {
		eval q{use Data::Dumper};
		debug("blogspam server reports ".$res-&gt;value.": ".Dumper(\%req));
		return gettext("Sorry, but that looks like spam to &lt;a href=\"http://blogspam.net/\"&gt;blogspam&lt;/a&gt;: ").$1;
	}
	elsif ($res-&gt;value ne 'OK') {
		debug("blogspam server failure: ".$res-&gt;value);
		return undef;
	}
	else {
		return undef;
	}
}

1
</pre></body></html>