summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/blogspam.pm
blob: f0b6cb2a2742519a2796bce8a863500f34d0d5f0 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::blogspam;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. use Encode;
  7. my $defaulturl='http://test.blogspam.net:8888/';
  8. sub import {
  9. hook(type => "getsetup", id => "blogspam", call => \&getsetup);
  10. hook(type => "checkconfig", id => "blogspam", call => \&checkconfig);
  11. hook(type => "checkcontent", id => "blogspam", call => \&checkcontent);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => 0,
  18. section => "auth",
  19. },
  20. blogspam_pagespec => {
  21. type => 'pagespec',
  22. example => 'postcomment(*)',
  23. description => 'PageSpec of pages to check for spam',
  24. link => 'ikiwiki/PageSpec',
  25. safe => 1,
  26. rebuild => 0,
  27. },
  28. blogspam_options => {
  29. type => "string",
  30. example => "blacklist=1.2.3.4,blacklist=8.7.6.5,max-links=10",
  31. description => "options to send to blogspam server",
  32. link => "http://blogspam.net/api/testComment.html#options",
  33. safe => 1,
  34. rebuild => 0,
  35. },
  36. blogspam_server => {
  37. type => "string",
  38. default => $defaulturl,
  39. description => "blogspam server XML-RPC url",
  40. safe => 1,
  41. rebuild => 0,
  42. },
  43. }
  44. sub checkconfig () {
  45. # This is done at checkconfig time because printing an error
  46. # if the module is missing when a spam is posted would not
  47. # let the admin know about the problem.
  48. eval q{
  49. use RPC::XML;
  50. use RPC::XML::Client;
  51. };
  52. error $@ if $@;
  53. }
  54. sub checkcontent (@) {
  55. my %params=@_;
  56. my $session=$params{session};
  57. if (exists $config{blogspam_pagespec}) {
  58. return undef
  59. if ! pagespec_match($params{page}, $config{blogspam_pagespec},
  60. location => $params{page});
  61. }
  62. my $url=$defaulturl;
  63. $url = $config{blogspam_server} if exists $config{blogspam_server};
  64. my $client = RPC::XML::Client->new($url);
  65. my @options = split(",", $config{blogspam_options})
  66. if exists $config{blogspam_options};
  67. # Allow short comments and whitespace-only edits, unless the user
  68. # has overridden min-words themselves.
  69. push @options, "min-words=0"
  70. unless grep /^min-words=/i, @options;
  71. # Wiki pages can have a lot of urls, unless the user specifically
  72. # wants to limit them.
  73. push @options, "exclude=lotsaurls"
  74. unless grep /^max-links/i, @options;
  75. # Unless the user specified a size check, disable such checking.
  76. push @options, "exclude=size"
  77. unless grep /^(?:max|min)-size/i, @options;
  78. # This test has absurd false positives on words like "alpha"
  79. # and "buy".
  80. push @options, "exclude=stopwords";
  81. my %req=(
  82. ip => $session->remote_addr(),
  83. comment => encode_utf8(defined $params{diff} ? $params{diff} : $params{content}),
  84. subject => encode_utf8(defined $params{subject} ? $params{subject} : ""),
  85. name => encode_utf8(defined $params{author} ? $params{author} : ""),
  86. link => encode_utf8(exists $params{url} ? $params{url} : ""),
  87. options => join(",", @options),
  88. site => encode_utf8($config{url}),
  89. version => "ikiwiki ".$IkiWiki::version,
  90. );
  91. my $res = $client->send_request('testComment', \%req);
  92. if (! ref $res || ! defined $res->value) {
  93. debug("failed to get response from blogspam server ($url)");
  94. return undef;
  95. }
  96. elsif ($res->value =~ /^SPAM:(.*)/) {
  97. eval q{use Data::Dumper};
  98. debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
  99. return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
  100. }
  101. elsif ($res->value ne 'OK') {
  102. debug("blogspam server failure: ".$res->value);
  103. return undef;
  104. }
  105. else {
  106. return undef;
  107. }
  108. }
  109. 1