2 package IkiWiki::Plugin::blogspam;
8 my $defaulturl='http://test.blogspam.net:8888/';
11 hook(type => "getsetup", id => "blogspam", call => \&getsetup);
12 hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
13 hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
23 blogspam_pagespec => {
25 example => 'postcomment(*)',
26 description => 'PageSpec of pages to check for spam',
27 link => 'ikiwiki/PageSpec',
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",
41 default => $defaulturl,
42 description => "blogspam server XML-RPC url",
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.
59 sub checkcontent (@) {
61 my $session=$params{session};
63 if (exists $config{blogspam_pagespec}) {
65 if ! pagespec_match($params{page}, $config{blogspam_pagespec},
66 location => $params{page});
70 $url = $config{blogspam_server} if exists $config{blogspam_server};
71 my $client = RPC::XML::Client->new($url);
73 my @options = split(",", $config{blogspam_options})
74 if exists $config{blogspam_options};
76 # Allow short comments and whitespace-only edits, unless the user
77 # has overridden min-words themselves.
78 push @options, "min-words=0"
79 unless grep /^min-words=/i, @options;
80 # Wiki pages can have a lot of urls, unless the user specifically
81 # wants to limit them.
82 push @options, "exclude=lotsaurls"
83 unless grep /^max-links/i, @options;
84 # Unless the user specified a size check, disable such checking.
85 push @options, "exclude=size"
86 unless grep /^(?:max|min)-size/i, @options;
87 # This test has absurd false positives on words like "alpha"
89 push @options, "exclude=stopwords";
92 ip => $session->remote_addr(),
93 comment => defined $params{diff} ? $params{diff} : $params{content},
94 subject => defined $params{subject} ? $params{subject} : "",
95 name => defined $params{author} ? $params{author} : "",
96 link => exists $params{url} ? $params{url} : "",
97 options => join(",", @options),
99 version => "ikiwiki ".$IkiWiki::version,
101 my $res = $client->send_request('testComment', \%req);
103 if (! ref $res || ! defined $res->value) {
104 debug("failed to get response from blogspam server ($url)");
107 elsif ($res->value =~ /^SPAM:(.*)/) {
108 eval q{use Data::Dumper};
109 debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
110 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
112 elsif ($res->value ne 'OK') {
113 debug("blogspam server failure: ".$res->value);