summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/blogspam.pm
blob: c4e5cf390cb397a994414bf718c135b4bf498c22 (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. if (exists $config{blogspam_pagespec}) {
  56. return undef
  57. if ! pagespec_match($params{page}, $config{blogspam_pagespec},
  58. location => $params{page});
  59. }
  60. my $url=$defaulturl;
  61. $url = $config{blogspam_server} if exists $config{blogspam_server};
  62. my $client = RPC::XML::Client->new($url);
  63. my @options = split(",", $config{blogspam_options})
  64. if exists $config{blogspam_options};
  65. # Allow short comments and whitespace-only edits, unless the user
  66. # has overridden min-words themselves.
  67. push @options, "min-words=0"
  68. unless grep /^min-words=/i, @options;
  69. # Wiki pages can have a lot of urls, unless the user specifically
  70. # wants to limit them.
  71. push @options, "exclude=lotsaurls"
  72. unless grep /^max-links/i, @options;
  73. # Unless the user specified a size check, disable such checking.
  74. push @options, "exclude=size"
  75. unless grep /^(?:max|min)-size/i, @options;
  76. # This test has absurd false positives on words like "alpha"
  77. # and "buy".
  78. push @options, "exclude=stopwords";
  79. my %req=(
  80. ip => $ENV{REMOTE_ADDR},
  81. comment => defined $params{diff} ? $params{diff} : $params{content},
  82. subject => defined $params{subject} ? $params{subject} : "",
  83. name => defined $params{author} ? $params{author} : "",
  84. link => exists $params{url} ? $params{url} : "",
  85. options => join(",", @options),
  86. site => $config{url},
  87. version => "ikiwiki ".$IkiWiki::version,
  88. );
  89. my $res = $client->send_request('testComment', \%req);
  90. if (! ref $res || ! defined $res->value) {
  91. debug("failed to get response from blogspam server ($url)");
  92. return undef;
  93. }
  94. elsif ($res->value =~ /^SPAM:(.*)/) {
  95. eval q{use Data::Dumper};
  96. debug("blogspam server reports ".$res->value.": ".Dumper(\%req));
  97. return gettext("Sorry, but that looks like spam to <a href=\"http://blogspam.net/\">blogspam</a>: ").$1;
  98. }
  99. elsif ($res->value ne 'OK') {
  100. debug("blogspam server failure: ".$res->value);
  101. return undef;
  102. }
  103. else {
  104. return undef;
  105. }
  106. }
  107. 1