public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
francois (author)
Mon Mar 10 21:11:16 -0700 2008
commit  0b691e1b7ec087a49e4a1083ae318d7dc83d62c9
tree    42601966532f24d265228330a675cdbfe53d631c
parent  86c8abf9e9f207e6dcf84df6544968eafbf2b566
mephisto / lib / mephisto / spam_detection_engines / defensio_engine.rb
100644 142 lines (118 sloc) 4.355 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module Mephisto
  module SpamDetectionEngines
    class DefensioEngine < Mephisto::SpamDetectionEngine::Base
      Site.register_spam_detection_engine "Defensio", self
 
      class << self
        def settings_template(site)
          load_template(File.join(File.dirname(__FILE__), "defensio_settings.html.erb")).render(:site => site, :options => site.spam_engine_options)
        end
      end
 
      def valid?
        [:defensio_url, :defensio_key].all? {|key| !options[key].blank?}
      end
 
      def valid_key?
        self.validate_key
      end
 
      class Stats
        def initialize(response)
          @response = response
        end
 
        def spam
          @response[:spam]
        end
 
        def ham
          @response[:ham]
        end
 
        def accuracy
          @response[:accuracy]
        end
 
        def false_negatives
          @response[:"false-negatives"]
        end
 
        def false_positives
          @response[:"false-positives"]
        end
      end
 
      def statistics_template
        stats = Stats.new(defensio.stats)
        return self.class.load_template(File.join(File.dirname(__FILE__), "defensio_statistics.html.erb")).render(:site => site, :options => site.spam_engine_options, :statistics => stats) if valid_key?
        return ""
      end
 
      def announce_article(permalink_url, article)
        response = defensio.check_article(
          :article_author => article.updater.login,
          :article_author_email => article.updater.email,
          :article_title => article.title,
          :article_content => article.body,
          :permalink => permalink_url
        )
      end
 
      def info(comment)
        return "" if comment.spam_engine_data.blank?
        signature = comment.spam_engine_data[:signature] || ""
        spaminess = comment.spam_engine_data[:spaminess] || 0
        spaminess *= 100
        "Spaminess: %.1f%%, Signature: %s" % [spaminess, signature]
      end
 
      def classes(comment)
        return "" if comment.spam_engine_data.blank?
        case (comment.spam_engine_data[:spaminess] || 0) * 100
        when 0
          "spam0"
        when 0...30
          "spam30"
        when 30...75
          "spam75"
        else
          "spam100"
        end
      end
 
      def ham?(permalink_url, comment, options={})
        response = defensio.check_comment(
          # Required parameters
          :user_ip => comment.author_ip,
          :article_date => comment.article.published_at.strftime("%Y/%m/%d"),
          :comment_author => comment.author,
          :comment_type => "comment",
 
          # Optional parameters
          :comment_content => comment.body,
          :comment_author_email => comment.author_email,
          :comment_author_url => comment.author_url,
          :permalink => permalink_url,
          :referrer => comment.referrer,
          :user_logged_in => options[:authenticated],
          :trusted_user => options[:authenticated]
        )
 
        comment.update_attribute(:spam_engine_data, {:signature => response["signature"], :spaminess => response["spaminess"].to_f})
        !response["spam"]
      end
 
      def mark_as_ham(permalink_url, comment)
        return if comment.spam_engine_data.blank? || comment.spam_engine_data[:signature].blank?
        defensio.mark_as_ham(:signatures => [comment.spam_engine_data[:signature]])
      end
 
      def mark_as_spam(permalink_url, comment)
        return if comment.spam_engine_data.blank? || comment.spam_engine_data[:signature].blank?
        defensio.mark_as_spam(:signatures => [comment.spam_engine_data[:signature]])
      end
 
      def sort_block
        lambda {|c| 1.0 - (c.spam_engine_data.blank? ? 0 : (c.spam_engine_data[:spaminess] || 0))}
      end
 
      def errors
        returning([]) do |es|
          es << "The Defensio key is missing" if options[:defensio_key].blank?
          es << "The Defensio url is missing" if options[:defensio_url].blank?
 
          unless self.valid_key?
            es << "The Defensio API says your key is invalid"
          end
        end
      end
 
      protected
      def defensio
        @defensio ||= Viking.connect("defensio", :api_key => options[:defensio_key], :blog => options[:defensio_url])
      end
 
      def validate_key
        @verified ||= defensio.verified?
      end
    end
  end
end