Merge branch 'master' into templatemove
[ikiwiki] / IkiWiki / Plugin / blogspam.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::blogspam;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 my $defaulturl='http://test.blogspam.net:8888/';
9
10 sub import {
11         hook(type => "getsetup", id => "blogspam",  call => \&getsetup);
12         hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
13         hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "auth",
22                 },
23                 blogspam_pagespec => {
24                         type => 'pagespec',
25                         example => 'postcomment(*)',
26                         description => 'PageSpec of pages to check for spam',
27                         link => 'ikiwiki/PageSpec',
28                         safe => 1,
29                         rebuild => 0,
30                 },
31                 blogspam_options => {
32                         type => "string",
33                         example => "blacklist=1.2.3.4,blacklist=8.7.6.5,max-links=10",
34                         description => "options to send to blogspam server",
35                         link => "http://blogspam.net/api/testComment.html#options",
36                         safe => 1,
37                         rebuild => 0,
38                 },
39                 blogspam_server => {
40                         type => "string",
41                         default => $defaulturl,
42                         description => "blogspam server XML-RPC url",
43                         safe => 1,
44                         rebuild => 0,
45                 },
46 }
47
48 sub checkconfig () {
49         # This is done at checkconfig time because printing an error
50         # if the module is missing when a spam is posted would not
51         # let the admin know about the problem.
52         eval q{
53                 use RPC::XML;
54                 use RPC::XML::Client;
55         };
56         error $@ if $@;
57 }
58
59 sub checkcontent (@) {
60         my %params=@_;
61         
62         if (exists $config{blogspam_pagespec}) {
63                 return undef
64                         if ! pagespec_match($params{page}, $config{blogspam_pagespec},
65                                 location => $params{page});
66         }
67
68         my $url=$defaulturl;
69         $url = $config{blogspam_server} if exists $config{blogspam_server};
70         my $client = RPC::XML::Client->new($url);
71
72         my @options = split(",", $config{blogspam_options})
73                 if exists $config{blogspam_options};
74
75         # Allow short comments and whitespace-only edits, unless the user
76         # has overridden min-words themselves.
77         push @options, "min-words=0"
78                 unless grep /^min-words=/i, @options;
79         # Wiki pages can have a lot of urls, unless the user specifically
80         # wants to limit them.
81         push @options, "exclude=lotsaurls"
82                 unless grep /^max-links/i, @options;
83         # Unless the user specified a size check, disable such checking.
84         push @options, "exclude=size"
85                 unless grep /^(?:max|min)-size/i, @options;
86         # This test has absurd false positives on words like "alpha"
87         # and "buy".
88         push @options, "exclude=stopwords";
89
90         my %req=(
91                 ip => $ENV{REMOTE_ADDR},
92                 comment => defined $params{diff} ? $params{diff} : $params{content},
93                 subject => defined $params{subject} ? $params{subject} : "",
94                 name => defined $params{author} ? $params{author} : "",
95                 link => exists $params{url} ? $params{url} : "",
96                 options => join(",", @options),
97                 site => $config{url},
98                 version => "ikiwiki ".$IkiWiki::version,
99         );
100         my $res = $client->send_request('testComment', \%req);
101
102         if (! ref $res || ! defined $res->value) {
103                 debug("failed to get response from blogspam server ($url)");
104                 return undef;
105         }
106         elsif ($res->value =~ /^SPAM:(.*)/) {
107                 eval q{use Data::Dumper};
108                 debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
109                 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
110         }
111         elsif ($res->value ne 'OK') {
112                 debug("blogspam server failure: ".$res->value);
113                 return undef;
114         }
115         else {
116                 return undef;
117         }
118 }
119
120 1