mollom 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +25 -0
  2. data/lib/mollom.rb +199 -0
  3. metadata +54 -0
data/README ADDED
@@ -0,0 +1,25 @@
1
+ == Mollom
2
+
3
+ This is a Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.
4
+
5
+ == Usage
6
+
7
+ After you have requested a public/private keypair from Mollom (on http://www.mollom.com), you can start using this class.
8
+
9
+ require 'lib/mollom'
10
+
11
+ m = Mollom.new(:private_key => 'azkaozdkoakdzkodazdz', :public_key => '34342ikdiozeezioez')
12
+
13
+ content = m.check_content(:post_title => 'Mollem is an open API',
14
+ :post_body => "Mollem is an open API for spam detection and content quality assessment.",
15
+ :author_name => 'Jan De Poorter',
16
+ :author_url => 'http://blog.defv.be')
17
+ if content.unsure? or content.spam?
18
+ puts "Captcha: " + m.image_captcha(:session_id => content.session_id)["url"]
19
+ print "Captcha is: "
20
+ puts m.check_captcha(:session_id => content.session_id, :solution => STDIN.gets.chomp)
21
+ else
22
+ puts "The post is perfect! No spam!"
23
+ end
24
+
25
+ Copyright (c) 2008 Jan De Poorter - Openminds BVBA, released under the MIT license
data/lib/mollom.rb ADDED
@@ -0,0 +1,199 @@
1
+ require 'xmlrpc/client'
2
+ require 'openssl'
3
+ require 'base64'
4
+
5
+ class Mollom
6
+ API_VERSION = '1.0'
7
+ module Errors
8
+ Standard = 1000
9
+ Refresh = 1100
10
+ Redirect = 1200
11
+ end
12
+
13
+ attr_accessor :private_key, :public_key
14
+
15
+ # Creates a new Mollom object. Takes +private_key+ and +public_key+ as keys.
16
+ #
17
+ # Mollom.new(:private_key => 'qopzalnzanzajlazjna', :public_key => 'aksakzaddazidzaodjaz')
18
+ # # => #<Mollom:0x5b6454 @public_key="aksakzaddazidzaodjaz", @private_key="qopzalnzanzajlazjna">
19
+
20
+ def initialize options = {}
21
+ @private_key = options[:private_key]
22
+ @public_key = options[:public_key]
23
+ end
24
+
25
+ # Checks the content whether it is spam, ham (not spam), or undecided, and gives a quality assessment of the content.
26
+ # Possible content keys are:
27
+ # session_id # => If you allready have a session_id
28
+ # post_title # => The title
29
+ # post_body # => The main content of the post.
30
+ # author_name # => The name of the post author
31
+ # author_url # => The url the author enters
32
+ # author_mail # => The author's email address
33
+ # author_ip # => The author's IP address
34
+ # author_openid # => The author's OpenID
35
+ # author_id # => The author's ID
36
+ #
37
+ # Only the +post_body+ key is required, all other keys are optional.
38
+ # This function returns a ContentResponse object.
39
+ #
40
+ # response = mollom.check_content :post_title => 'Mollom rules!',
41
+ # :post_body => 'I think that mollom is so cool!',
42
+ # :author_name => 'Jan De Poorter',
43
+ # :author_url => 'http://www.openminds.be'
44
+ # response.spam? # => false
45
+ # response.ham? # => true
46
+ def check_content content = {}
47
+ return ContentResponse.new(send_command('mollom.checkContent', content))
48
+ end
49
+
50
+ # Requests an Image captcha from Mollom. It takes the optional <tt>session_id</tt> and <tt>author_ip</tt> keys, if you allready have a session.
51
+ # It returns a hash with the URL where the captcha can be found, and the session_id, to keep track of the current session (Needed later in <tt>Mollom#check_captcha</tt>)
52
+ #
53
+ # captcha = mollom.image_captcha :author_ip => '172.16.0.1'
54
+ # captcha['url'] # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.png
55
+ # captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d
56
+ def image_captcha info = {}
57
+ return send_command('mollom.getImageCaptcha', info)
58
+ end
59
+
60
+ # Requests an Audio captcha from Mollom. It takes the optional +session_id+ and +author_ip+ keys, if you allready have a session.
61
+ # It returns a hash with the URL where the captcha can be found, and the session_id, to keep track of the current session (Needed later in <tt>Mollom#check_captcha</tt>)
62
+ #
63
+ # captcha = mollom.audio_captcha :author_ip => '172.16.0.2', :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d'
64
+ # captcha['url'] # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.mp3
65
+ # captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d
66
+ def audio_captcha info = {}
67
+ return send_command('mollom.getAudioCaptcha', info)
68
+ end
69
+
70
+ # Checks with mollom if the given captcha (by the user) is correct. Takes +session_id+ and +solution+ keys. Both keys are required.
71
+ # Returns true if the captcha is valid, false if it is incorrect
72
+ #
73
+ # captcha = mollom.image_captcha :author_ip => '172.16.0.1'
74
+ # # show to user... input from user
75
+ # return = mollom.valid_captcha? :session_id => captcha['session_id'], :solution => 'abcDe9'
76
+ # return # => true
77
+ def valid_captcha? info = {}
78
+ return send_command('mollom.checkCaptcha', info)
79
+ end
80
+
81
+ # Standard check to see if your public/private keypair are recognized. Takes no options
82
+ def key_ok?
83
+ return send_command('mollom.verifyKey')
84
+ end
85
+
86
+ # Gets some statistics from Mollom about your site.
87
+ #
88
+ # The type has to be passed. Possible types:
89
+ # total_days
90
+ # total_accepted
91
+ # total_rejected
92
+ # yesterday_accepted
93
+ # yesterday_rejected
94
+ # today_accepted
95
+ # today_rejected
96
+ #
97
+ # mollom.statistics :type => 'total_accepted' # => 123
98
+ def statistics options = {}
99
+ return send_command('mollom.getStatistics', options)
100
+ end
101
+
102
+ # Send feedback to Mollom about a certain content. Required keys are +session_id+ and +feedback+.
103
+ #
104
+ # Feedback can be any of
105
+ # spam
106
+ # profanity
107
+ # low-quality
108
+ # unwanted
109
+ #
110
+ # mollow.send_feedback :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :feedback => 'unwanted'
111
+ def send_feedback feedback = {}
112
+ return send_command('mollom.sendFeedback', feedback)
113
+ end
114
+
115
+ private
116
+ def send_command(command, data = {})
117
+ server_list.each do |server|
118
+ begin
119
+ return XMLRPC::Client.new(server, "/#{API_VERSION}").call(command, data.merge(authentication_hash))
120
+ rescue XMLRPC::FaultException => error
121
+ case error.faultCode
122
+ when Errors::Standard
123
+ raise Error.new(error.faultString)
124
+ when Errors::Refresh
125
+ # TO IMPLEMENT
126
+ when Errors::Redirect
127
+ next
128
+ else
129
+ raise
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ # Gets a list of servers from Mollom
136
+ def server_list
137
+ # Mollom prepends 'http://' to the IP.. Ruby doesn't like that
138
+ @server_list ||= XMLRPC::Client.new("xmlrpc.mollom.com", "/#{API_VERSION}").call('mollom.getServerList', authentication_hash).collect { |s| s.sub('http://', '') }
139
+ end
140
+
141
+ # Creates a HMAC-SHA1 Hash with the current timestamp, and your private key.
142
+ def authentication_hash
143
+ now = Time.now.gmtime.strftime('%Y-%m-%dT%H:%M:%S.000+0000')
144
+
145
+ hash = Base64.encode64(
146
+ OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @private_key, now)
147
+ )
148
+
149
+ return :public_key=> @public_key, :time => now, :hash => hash
150
+ end
151
+
152
+ class ContentResponse
153
+ attr_reader :session_id, :quality
154
+
155
+ Unknown = 0
156
+ Ham = 1
157
+ Unsure = 2
158
+ Spam = 3
159
+
160
+ # This class should only be initialized from within the +check_content+ command.
161
+ def initialize(hash)
162
+ @spam_response = hash["spam"]
163
+ @session_id = hash["session_id"]
164
+ @quality = hash["quality"]
165
+ end
166
+
167
+ # Is the content Spam?
168
+ def spam?
169
+ @spam_response == Spam
170
+ end
171
+
172
+ # Is the content Ham?
173
+ def ham?
174
+ @spam_response == Ham
175
+ end
176
+
177
+ # is Mollom unsure about the content?
178
+ def unsure?
179
+ @spam_response == Unsure
180
+ end
181
+
182
+ # is the content unknown?
183
+ def unknown?
184
+ @spam_response == Unknown
185
+ end
186
+
187
+ # Returns 'unknown', 'ham', 'unsure' or 'spam', depending on what the content is.
188
+ def to_s
189
+ case @spam_response
190
+ when Unknown: 'unknown'
191
+ when Ham: 'ham'
192
+ when Unsure: 'unsure'
193
+ when Spam: 'spam'
194
+ end
195
+ end
196
+ end
197
+
198
+ class Error < StandardError; end
199
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mollom
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Jan De Poorter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-01 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mollom@openminds.be
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/mollom.rb
26
+ - README
27
+ has_rdoc: true
28
+ homepage: mollom.rubyforge.com
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.0.1
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.
53
+ test_files: []
54
+