2 package IkiWiki::Plugin::blogspam;
9 my $defaulturl='http://test.blogspam.net:9999/';
13 hook(type => "getsetup", id => "blogspam", call => \&getsetup);
14 hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
15 hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
25 blogspam_pagespec => {
27 example => 'postcomment(*)',
28 description => 'PageSpec of pages to check for spam',
29 link => 'ikiwiki/PageSpec',
35 example => "blacklist=1.2.3.4,blacklist=8.7.6.5,max-links=10",
36 description => "options to send to blogspam server",
37 link => "http://blogspam.net/api/2.0/testComment.html#options",
43 default => $defaulturl,
44 description => "blogspam server JSON url",
51 # This is done at checkconfig time because printing an error
52 # if the module is missing when a spam is posted would not
53 # let the admin know about the problem.
60 eval q{use LWPx::ParanoidAgent};
62 $client=LWPx::ParanoidAgent->new();
70 $client=LWP::UserAgent->new(cookie_jar => $config{cookiejar});
74 sub checkcontent (@) {
76 my $session=$params{session};
79 if (exists $config{blogspam_pagespec} &&
80 length $config{blogspam_pagespec}) {
81 $spec.=" and (".$config{blogspam_pagespec}.")";
84 my $user=$session->param("name");
85 return undef unless pagespec_match($params{page}, $spec,
86 (defined $user ? (user => $user) : ()),
87 (defined $session->remote_addr() ? (ip => $session->remote_addr()) : ()),
88 location => $params{page});
91 $url = $config{blogspam_server} if exists $config{blogspam_server};
93 my @options = split(",", $config{blogspam_options})
94 if exists $config{blogspam_options};
96 # Allow short comments and whitespace-only edits, unless the user
97 # has overridden min-words themselves.
98 push @options, "min-words=0"
99 unless grep /^min-words=/i, @options;
100 # Wiki pages can have a lot of urls, unless the user specifically
101 # wants to limit them.
102 push @options, "exclude=lotsaurls"
103 unless grep /^max-links/i, @options;
104 # Unless the user specified a size check, disable such checking.
105 push @options, "exclude=size"
106 unless grep /^(?:max|min)-size/i, @options;
107 # This test has absurd false positives on words like "alpha"
109 push @options, "exclude=stopwords";
112 ip => $session->remote_addr(),
113 comment => encode_utf8(defined $params{diff} ? $params{diff} : $params{content}),
114 subject => encode_utf8(defined $params{subject} ? $params{subject} : ""),
115 name => encode_utf8(defined $params{author} ? $params{author} : ""),
116 link => encode_utf8(exists $params{url} ? $params{url} : ""),
117 options => join(",", @options),
118 site => encode_utf8($config{url}),
119 version => "ikiwiki ".$IkiWiki::version,
121 eval q{use JSON; use HTTP::Request}; # errors handled in checkconfig()
122 my $res = $client->request(
126 [ 'Content-Type' => 'application/json' ],
131 if (! ref $res || ! $res->is_success()) {
132 debug("failed to get response from blogspam server ($url)");
135 my $details = from_json($res->content);
136 if ($details->{result} eq 'SPAM') {
137 eval q{use Data::Dumper};
138 debug("blogspam server reports $details->{reason}: ".Dumper(\%req));
139 return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$details->{reason};
141 elsif ($details->{result} ne 'OK') {
142 debug("blogspam server failure: ".$res->content);