tDiary 用 SpamLookup フィルタ

http://elpeo.jp/diary/20051013.html#p01


コメントSPAMが大量に来てウザイ件だが、
時代は SpamLookup フィルタらしいので入れてみた。
ついでに tb.rb も元に戻した。

tdiary-comment-spam-lookup

ついでに過去の奴も一層しようというわけで、
高林さんの tdiary-comment-clean とくっつけてみた。
#! /usr/bin/env ruby
#
# Copyright (C) 2004 Satoru Takabayashi
# You can redistribute it and/or modify it under GPL2.
#

require 'resolv'

def black_domain?( domain )
begin
Resolv.getaddress( "#{domain}.rbl.bulkfeeds.jp" )
return true
rescue
end
false
end

def black_url?( body )
body.scan( %r|http://([^/]+)/| ) do |s|
return true if black_domain?( s[0] )
end
false
end

if ARGV.length == 0
puts "Usage: tdiary-comment-spam-lookup FILE..."
exit
end

file_names = ARGV

deleted_comments =
file_names.each {|file_name|
i = File.open(file_name)
first_line = i.gets

comments =

comment = ""
while line = i.gets
if line == ".\n"
comments.push(comment)
comment = ""
else
comment << line
end
end
i.close

tmp_name = "tmp.#{Process.pid}"
File.open(tmp_name, "w") {|o|
o.print first_line
comments.each {|comment|
if black_url?(comment)
deleted_comments.push(comment)
else
o.print comment
o.puts "."
end
}
}
File.rename(file_name, file_name + ".bak")
File.rename(tmp_name, file_name)
}

deleted_comments.each {|comment|
print comment
puts "."
}

tdiary-referer-spam-lookup

ついでに referer のも。
#! /usr/bin/env ruby
#
# Copyright (C) 2004 Satoru Takabayashi
# You can redistribute it and/or modify it under GPL2.
#

require 'resolv'

def black_domain?( domain )
begin
Resolv.getaddress( "#{domain}.rbl.bulkfeeds.jp" )
return true
rescue
end
false
end

def black_url?( body )
body.scan( %r|http://([^/]+)/| ) do |s|
return true if black_domain?( s[0] )
end
false
end

if ARGV.length == 0
puts "Usage: tdiary-referer-spam-lookup FILE..."
exit
end

file_names = ARGV

deleted_referers = []
file_names.each {|file_name|
tmp_name = "tmp.#{Process.pid}"
i = File.open(file_name)
o = File.open(tmp_name, "w")

first_line = i.gets
o.print first_line

while true
date_line = i.gets
break if date_line.nil?
raise unless /^Date: /.match(date_line)
blank_line = i.gets
raise unless blank_line == "\n"

o.print date_line
o.print blank_line
while line = i.gets
if line == ".\n"
o.print line
next
end
if black_url?(line)
deleted_referers.push(line)
else
o.print line
end
end
end
i.close
o.close

File.rename(file_name, file_name + ".bak")
File.rename(tmp_name, file_name)
}
deleted_referers.each {|referer| print referer }