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