expire-bans.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /usr/bin/perl
  2. my $usage = q{expire-pans.pl
  3. Usage: this script expects to be run in a directory with a spammer.log file as
  4. produced by the LogBannedContent module.
  5. <https://oddmuse.org/wiki/LogBannedContent_Module>
  6. In the same directory, it expects at least one of BannedContent, BannedHosts or
  7. BannedRegexps. It will work on all three, though. These must be the raw text
  8. files of the wiki.
  9. Here's how you might get them from Emacs Wiki, for example.
  10. wget https://www.emacswiki.org/spammer.log
  11. wget https://www.emacswiki.org/emacs/raw/BannedContent
  12. wget https://www.emacswiki.org/emacs/raw/BannedHosts
  13. wget https://www.emacswiki.org/emacs/raw/BannedRegexps
  14. };
  15. die $usage if ! -f 'spammer.log'
  16. || !(-f 'BannedContent' || -f 'BannedHosts' || -f 'BannedRegexps');
  17. my $fh;
  18. my @bans;
  19. warn "Reading spammer.log...\n";
  20. open($fh, '<:utf8', 'spammer.log') or die "Cannot read spammer.log: $!";
  21. for my $line (<$fh>) {
  22. push(@bans, $line);
  23. }
  24. close($fh);
  25. for my $file (qw(BannedContent BannedHosts BannedRegexps)) {
  26. warn "Reading $file...\n";
  27. if (open($fh, '<:utf8', $file)) {
  28. my $count = 0;
  29. my $used = 0;
  30. my @out;
  31. for my $line (<$fh>) {
  32. if ($line =~ m/^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$/) {
  33. $count++;
  34. my ($regexp, $comment) = ($1, $4);
  35. foreach my $ban (@bans) {
  36. if (index($ban, $regexp) > -1) {
  37. $used++;
  38. push(@out, $line);
  39. last;
  40. }
  41. }
  42. } else {
  43. push(@out, $line);
  44. }
  45. }
  46. close ($fh);
  47. warn "$count regular expressions checked\n";
  48. warn "$used regular expressions were used\n";
  49. warn "Writing $file-new...\n";
  50. open ($fh, '>:utf8', "$file-new")
  51. or die "Cannot write $file-new: $!";
  52. print $fh join("", @out);
  53. close $fh;
  54. }
  55. }