: O. Yuanying

Ruby で Akismet

前置き

自作のブログ管理システムであるRanaPrunusでは、スパムコメント対策として以下を行っている。

  • ワンタイムトークンによるチェック
  • IPアドレスブラックリストチェック
  • NGワードチェック

だいたい90%くらいのスパムコメントがワンタイムトークンによるチェックではじくことができ、IPアドレスとNGワードのチェックで残りの5%くらいをはじくことができる。

そして残ったスパムコメントをAkismetで完全に遮断してみようかと思う。

Akismet.rb

色々調べてみると、すでにrubyによるAkismetライブラリはあるようで、bedeviled mojo slop - A blog by David Czarneckiなんかが紹介されているが、少々修正が必要なようだ。

akismet.rb

RanaPrunusでは、実際にMephistoで使われているakismet.rbを利用する事にした。

これはDavid CzarneckiさんのAkismet.rbを'rewritten to be more rails-like'されたものの模様。

使い方

require 'akismet'
akismet = Akismet.new(API_KEY, BLOG_URL)
if akismet.verified?
  if akismet.comment_check(:user_ip=>COMMENT_IP, :user_agent=>'USER-AGENT', ...)
    raise 'invalid comment!!'
  end
end

comment_checkメソッドは引数としてハッシュをうけとる。以下がその内容。

user_ip (required)
コメントした人のIPアドレス
user_agent (required)
コメントした人のユーザエージェント
referrer (note spelling)
コメントのリファラー
permalink
コメント対象のウェブページのURL
comment_type
コメントのタイプ、例えば、comment, trackback, pingback もしくは'registration'などの適当な文字列。空白でもよい。
comment_author
コメントした人の名前。
comment_author_email
コメントした人のメールアドレス
comment_author_url
コメントした人のURL
comment_content
コメント本文
Other server enviroment variables
In PHP there is an array of enviroment variables called $_SERVER which contains information about the web server itself as well as a key/value for every HTTP header sent with the request. This data is highly useful to Akismet as how the submited content interacts with the server can be very telling, so please include as much information as possible.