2 package IkiWiki::Plugin::blogspam;
9 my $defaulturl='http://test.blogspam.net:8888/';
12 hook(type => "getsetup", id => "blogspam", call => \&getsetup);
13 hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
14 hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
24 blogspam_pagespec => {
26 example => 'postcomment(*)',
27 description => 'PageSpec of pages to check for spam',
28 link => 'ikiwiki/PageSpec',
34 example => "blacklist=1.2.3.4,blacklist=8.7.6.5,max-links=10",
35 description => "options to send to blogspam server",
36 link => "http://blogspam.net/api/testComment.html#options",
42 default => $defaulturl,
43 description => "blogspam server XML-RPC url",
50 # This is done at checkconfig time because printing an error
51 # if the module is missing when a spam is posted would not
52 # let the admin know about the problem.
60 sub checkcontent (@) {
62 my $session=$params{session};
65 if (exists $config{blogspam_pagespec} &&
66 length $config{blogspam_pagespec}) {
67 $spec.=" and (".$config{blogspam_pagespec}.")";
70 my $user=$session->param("name");
71 return undef unless pagespec_match($params{page}, $spec,
72 (defined $user ? (user => $user) : ()),
73 (defined $session->remote_addr() ? (ip => $session->remote_addr()) : ()),
74 location => $params{page});
77 $url = $config{blogspam_server} if exists $config{blogspam_server};
79 my $client = RPC::XML::Client->new($url);
81 my @options = split(",", $config{blogspam_options})
82 if exists $config{blogspam_options};
84 # Allow short comments and whitespace-only edits, unless the user
85 # has overridden min-words themselves.
86 push @options, "min-words=0"
87 unless grep /^min-words=/i, @options;
88 # Wiki pages can have a lot of urls, unless the user specifically
89 # wants to limit them.
90 push @options, "exclude=lotsaurls"
91 unless grep /^max-links/i, @options;
92 # Unless the user specified a size check, disable such checking.
93 push @options, "exclude=size"
94 unless grep /^(?:max|min)-size/i, @options;
95 # This test has absurd false positives on words like "alpha"
97 push @options, "exclude=stopwords";
100 ip => $session->remote_addr(),
101 comment => encode_utf8(defined $params{diff} ? $params{diff} : $params{content}),
102 subject => encode_utf8(defined $params{subject} ? $params{subject} : ""),
103 name => encode_utf8(defined $params{author} ? $params{author} : ""),
104 link => encode_utf8(exists $params{url} ? $params{url} : ""),
105 options => join(",", @options),
106 site => encode_utf8($config{url}),
107 version => "ikiwiki ".$IkiWiki::version,
109 my $res = $client->send_request('testComment', \%req);
111 if (! ref $res || ! defined $res->value) {
112 debug("failed to get response from blogspam server ($url)");
115 elsif ($res->value =~ /^SPAM:(.*)/) {
116 eval q{use Data::Dumper};
117 debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
118 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
120 elsif ($res->value ne 'OK') {
121 debug("blogspam server failure: ".$res->value);