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