public
Fork of halorgium/mephisto
Description: A refactored Mephisto that has multiple spam detection engines.
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/francois/mephisto.git
Search Repo:
francois (author)
Tue Mar 11 12:33:33 -0700 2008
commit  f1068ff008b7da9263fd9037d1a37b3813e70991
tree    4331244d9d66851e681030c5dbe0acb35e91fe9e
parent  c43f6c9cb9626e4cec786bf81a53ca1afc9d6a44 parent  281dd17cb2b7a970260b27bbdc5e7d24e7220633
mephisto / lib / mephisto / spam_detection_engines / akismet_engine.rb
100644 72 lines (60 sloc) 2.354 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Mephisto
  module SpamDetectionEngines
    class AkismetEngine < Mephisto::SpamDetectionEngine::Base
      Site.register_spam_detection_engine "Akismet", self
 
      class << self
        def settings_template(site)
          load_template(File.join(File.dirname(__FILE__), "akismet_settings.html.erb")).render(:site => site, :options => site.spam_engine_options)
        end
      end
 
      def statistics_template
        "<p>The Akismet API does not extract statistics about it's performance.</p>"
      end
 
      # Akismet doesn't care about real articles.
      def announce_article(permalink_url, article)
      end
 
      def ham?(permalink_url, comment, options={})
        check_valid!
        !akismet.comment_check(comment_spam_options(permalink_url, comment))
      end
 
      def mark_as_ham(permalink_url, comment)
        check_valid!
        akismet.submit_ham(comment_spam_options(permalink_url, comment))
      end
 
      def mark_as_spam(permalink_url, comment)
        check_valid!
        akismet.submit_spam(comment_spam_options(permalink_url, comment))
      end
 
      def valid?
        [:akismet_key, :akismet_url].all? { |attr| !options[attr].blank? }
      end
 
      def valid_key?
        self.valid? && akismet.verified?
      end
 
      def errors
        returning([]) do |es|
          es << "The Akismet key is missing" if options[:akismet_key].blank?
          es << "The Akismet url is missing" if options[:akismet_url].blank?
          es << "The Akismet API denied the key" unless akismet.verified?
        end
      end
 
      protected
      def akismet
        @akismet ||= Viking.connect("akismet", :api_key => options[:akismet_key], :blog => options[:akismet_url])
      end
 
      def comment_spam_options(permalink_url, comment)
        { :user_ip => comment.author_ip,
          :user_agent => comment.user_agent,
          :referrer => comment.referrer,
          :permalink => permalink_url,
          :comment_author => comment.author,
          :comment_author_email => comment.author_email,
          :comment_author_url => comment.author_url,
          :comment_content => comment.body}
      end
 
      def check_valid!
        raise Mephisto::SpamDetectionEngine::NotConfigured unless self.valid?
      end
    end
  end
end