]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/blogspam.pm
not a plugin anymore
[git.ikiwiki.info.git] / IkiWiki / Plugin / blogspam.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::blogspam;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
9 my $defaulturl='http://test.blogspam.net:8888/';
11 sub import {
12         hook(type => "getsetup", id => "blogspam",  call => \&getsetup);
13         hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
14         hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 0,
22                         section => "auth",
23                 },
24                 blogspam_pagespec => {
25                         type => 'pagespec',
26                         example => 'postcomment(*)',
27                         description => 'PageSpec of pages to check for spam',
28                         link => 'ikiwiki/PageSpec',
29                         safe => 1,
30                         rebuild => 0,
31                 },
32                 blogspam_options => {
33                         type => "string",
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",
37                         safe => 1,
38                         rebuild => 0,
39                 },
40                 blogspam_server => {
41                         type => "string",
42                         default => $defaulturl,
43                         description => "blogspam server XML-RPC url",
44                         safe => 1,
45                         rebuild => 0,
46                 },
47 }
49 sub checkconfig () {
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.
53         eval q{
54                 use RPC::XML;
55                 use RPC::XML::Client;
56                 $RPC::XML::ENCODING = 'utf-8';
57         };
58         error $@ if $@;
59 }
61 sub checkcontent (@) {
62         my %params=@_;
63         my $session=$params{session};
64         
65         my $spec='!admin()';
66         if (exists $config{blogspam_pagespec} &&
67             length $config{blogspam_pagespec}) {
68                 $spec.=" and (".$config{blogspam_pagespec}.")";
69         }
71         my $user=$session->param("name");
72         return undef unless pagespec_match($params{page}, $spec,
73                 (defined $user ? (user => $user) : ()),
74                 (defined $session->remote_addr() ? (ip => $session->remote_addr()) : ()),
75                 location => $params{page});
77         my $url=$defaulturl;
78         $url = $config{blogspam_server} if exists $config{blogspam_server};
80         my $client = RPC::XML::Client->new($url);
82         my @options = split(",", $config{blogspam_options})
83                 if exists $config{blogspam_options};
85         # Allow short comments and whitespace-only edits, unless the user
86         # has overridden min-words themselves.
87         push @options, "min-words=0"
88                 unless grep /^min-words=/i, @options;
89         # Wiki pages can have a lot of urls, unless the user specifically
90         # wants to limit them.
91         push @options, "exclude=lotsaurls"
92                 unless grep /^max-links/i, @options;
93         # Unless the user specified a size check, disable such checking.
94         push @options, "exclude=size"
95                 unless grep /^(?:max|min)-size/i, @options;
96         # This test has absurd false positives on words like "alpha"
97         # and "buy".
98         push @options, "exclude=stopwords";
100         my %req=(
101                 ip => $session->remote_addr(),
102                 comment => encode_utf8(defined $params{diff} ? $params{diff} : $params{content}),
103                 subject => encode_utf8(defined $params{subject} ? $params{subject} : ""),
104                 name => encode_utf8(defined $params{author} ? $params{author} : ""),
105                 link => encode_utf8(exists $params{url} ? $params{url} : ""),
106                 options => join(",", @options),
107                 site => encode_utf8($config{url}),
108                 version => "ikiwiki ".$IkiWiki::version,
109         );
110         my $res = $client->send_request('testComment', \%req);
112         if (! ref $res || ! defined $res->value) {
113                 debug("failed to get response from blogspam server ($url)");
114                 return undef;
115         }
116         elsif ($res->value =~ /^SPAM:(.*)/) {
117                 eval q{use Data::Dumper};
118                 debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
119                 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
120         }
121         elsif ($res->value ne 'OK') {
122                 debug("blogspam server failure: ".$res->value);
123                 return undef;
124         }
125         else {
126                 return undef;
127         }