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