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