]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/blogspam.pm
load rpc xml lib on the fly
[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 => "checkcontent", id => "blogspam", call => \&checkcontent);
13 }
14
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 }
45
46 sub checkcontent (@) {
47         my %params=@_;
48
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         }
63
64         my $url=$defaulturl;
65         $url = $params{blogspam_server} if exists $params{blogspam_server};
66         my $client = RPC::XML::Client->new($url);
67
68         my @options = split(",", $params{blogspam_options})
69                 if exists $params{blogspam_options};
70
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";
85
86         # blogspam API does not have a field for author url, so put it in
87         # the content to be checked.
88         if (exists $params{url}) {
89                 $params{content}.="\n".$params{url};
90         }
91
92         my $res = $client->send_request('testComment', {
93                 ip => $ENV{REMOTE_ADDR},
94                 comment => $params{content},
95                 subject => defined $params{subject} ? $params{subject} : "",
96                 name => defined $params{author} ? $params{author} : "",
97                 options => join(",", @options),
98                 site => $config{url},
99                 version => "ikiwiki ".$IkiWiki::version,
100         });
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                 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
108         }
109         elsif ($res->value ne 'OK') {
110                 debug(gettext("blogspam server failure: ").$res->value);
111                 return undef;
112         }
113         else {
114                 return undef;
115         }
116 }
117
118 1