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