On Fri, Oct 06, 2006 at 11:19:56PM +0200, Paul van Tilburg wrote:
> > On Mon, 2006-10-02 at 13:47 -0400, Bradford R. Bowman wrote:
> > [...] I put together a super quick hack to test
> > akismet's comment spam checking. 

> I'm interested... :)

Ok, here is a diff for hobix/facets/comments.rb:

Index: lib/hobix/facets/comments.rb
===================================================================
--- lib/hobix/facets/comments.rb    (revision 151)
+++ lib/hobix/facets/comments.rb    (working copy)
@@ -54,11 +54,37 @@  
                     c.ipaddress = app.remote_addr
                 end

-                # Save the comment, upgen
-                @weblog.storage.append_to_attachment( entry_id, 'comments', 
comment )
-                @weblog.regenerate :update
-
-                # Redirect
+               # A quick hack to try akismet content spam checking
+               
+               if @weblog.requires.detect{ |i| i['hobix/plugin/akismet'] }     
    
+                   @akismet = Akismet.new(@weblog.link, AkismetKey.key)
+                   if @akismet.verifyAPIKey
+                       if @akismet.commentCheck(
+                                   app.remote_addr,                            
# remote IP
+                                   app.get_request_header('User-Agent'),       
# user agent
+                                   app.get_request_header('Referer'),          
# http referer
+                                   '',                                         
# permalink
+                                   'comment',                                  
# comment type
+                                   app._POST['hobix_comment:author'].to_s,     
# author name
+                                   '',                                         
# author email
+                                   '',                                         
# author url
+                                   app._POST['hobix_comment:comment'].to_s,    
# comment text
+                                   {})                                         
# other
+                           app.puts( "Sorry, that smelled like spam. If wasn't 
meant to, go back and try again" )
+                           return true
+                       end
+                   else
+                       # If the key does not verify, post the comment
+                       # but note the failure in the apache error logs.
+                       $stderr.puts( "Hobix: akismet apikey did not verify." )
+                   end
+               end
+                   
+               # Save the comment, upgen
+               @weblog.storage.append_to_attachment( entry_id, 'comments', 
comment )
+               @weblog.regenerate :update
+               
+               # Redirect
                 link = @weblog.output_entry_map[entry_id]
                 app.setup_redirection( 302, @weblog.expand_path( 
link[:page].link ) )
                 return true

And I have attached my test plugin (hobix/plugin/akismet.rb)

I have no idea how effective akismet is, but I see that it's used by
wordpress, typo and now mephisto and others.  Also, I'm not sure how
this should really be implemented in hobix.

-- 
Bradford R. Bowman <[EMAIL PROTECTED]>
GnuPG public key [0x3EE183C6] 
Available at http://pgp.mit.edu

# Just a quick hack to test akismet as I didn't
# feel like rewriting the Akismet class to make it
# work as a proper plugin.

# Add this file to the hobix/plugins/
# In hobix.yaml add:
#  - hobix/plugin/akismet:
#        apiKey: <your akismet api key>
#
# You can obtain a free akismet api key
# By registering a user account at wordpress.com
# See http://www.akismet.com

module Hobix
        
class AkismetKey < BasePlugin
        
  def initialize(weblog, params = {})
        raise %{The Akismet plugin is not configured. See 
hobix/plugin/akismet.rb.} unless params.member?("apiKey")
    @@key = params["apiKey"] #TODO raise error if no such parameter
  end

  def self.key; @@key; end
  
end

end


# Akismet
#
# Author:: David Czarnecki
# Copyright:: Copyright (c) 2005 - David Czarnecki
# License:: BSD
# Modified by Dieter Komendera, Sparkling Studios:
#   append blog= to data string (Akismet said it is required)
#   changed require 'net/HTTP' to require 'net/http' (to work for me unter 
GNU/Linux)

class Akismet

  require 'net/http'
  require 'uri'
  
  STANDARD_HEADERS = {
    'User-Agent' => 'Akismet Ruby API/1.0',
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
  
  # Instance variables
  @apiKey
  @blog
  @verifiedKey
  @proxyPort = nil
  @proxyHost = nil

  # Create a new instance of the Akismet class
  #
  # apiKey 
  #   Your Akismet API key
  # blog 
  #   The blog associated with your api key
  
  def initialize(blog, apiKey)
    @apiKey = apiKey
    @blog = blog
    @verifiedKey = false
  end
  
  # Set proxy information 
  #
  # proxyHost
  #   Hostname for the proxy to use
  # proxyPort
  #   Port for the proxy
  def setProxy(proxyHost, proxyPort) 
    @proxyPort = proxyPort
    @proxyHost = proxyHost
  end
    
  # Call to check and verify your API key. You may then call the 
#hasVerifiedKey method to see if your key has been validated.
  def verifyAPIKey()
    http = Net::HTTP.new('rest.akismet.com', 80, @proxyHost, @proxyPort)
    path = '/1.1/verify-key'
    
    data="[EMAIL PROTECTED]&[EMAIL PROTECTED]"
    
    resp, data = http.post(path, data, STANDARD_HEADERS)
    @verifiedKey = (data == "valid")
  end
 
  # Returns <tt>true</tt> if the API key has been verified, <tt>false</tt> 
otherwise
  def hasVerifiedKey()
    return @verifiedKey
  end
  
  # Internal call to Akismet. Prepares the data for posting to the Akismet 
service.
  #
  # akismet_function
  #   The Akismet function that should be called
  # user_ip (required)
  #    IP address of the comment submitter.
  # user_agent (required)
  #    User agent information.
  # referrer (note spelling)
  #    The content of the HTTP_REFERER header should be sent here.
  # permalink
  #    The permanent location of the entry the comment was submitted to.
  # comment_type
  #    May be blank, comment, trackback, pingback, or a made up value like 
"registration".
  # comment_author
  #    Submitted name with the comment
  # comment_author_email
  #    Submitted email address
  # comment_author_url
  #    Commenter URL.
  # comment_content
  #    The content that was submitted.
  # 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.  
  def callAkismet(akismet_function, user_ip, user_agent, referrer, permalink, 
comment_type, comment_author, comment_author_email, comment_author_url, 
comment_content, other)
    http = Net::HTTP.new("[EMAIL PROTECTED]", 80, @proxyHost, @proxyPort)
    path = "/1.1/#{akismet_function}"        
    
    data = "[EMAIL 
PROTECTED]&user_ip=#{user_ip}&user_agent=#{user_agent}&referrer=#{referrer}&permalink=#{permalink}&comment_type=#{comment_type}&comment_author=#{comment_author}&comment_author_email=#{comment_author_email}&comment_author_url=#{comment_author_url}&comment_content=#{comment_content}"
    if (other != nil) 
      other.each_pair {|key, value| data.concat("&#{key}=#{value}")}
    end
            
    resp, data = http.post(path, data, STANDARD_HEADERS)

    return (data != "false")
  end
  
  protected :callAkismet

  # This is basically the core of everything. This call takes a number of 
arguments and characteristics about the submitted content and then returns a 
thumbs up or thumbs down. Almost everything is optional, but performance can 
drop dramatically if you exclude certain elements.
  #
  # user_ip (required)
  #    IP address of the comment submitter.
  # user_agent (required)
  #    User agent information.
  # referrer (note spelling)
  #    The content of the HTTP_REFERER header should be sent here.
  # permalink
  #    The permanent location of the entry the comment was submitted to.
  # comment_type
  #    May be blank, comment, trackback, pingback, or a made up value like 
"registration".
  # comment_author
  #    Submitted name with the comment
  # comment_author_email
  #    Submitted email address
  # comment_author_url
  #    Commenter URL.
  # comment_content
  #    The content that was submitted.
  # 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.
  def commentCheck(user_ip, user_agent, referrer, permalink, comment_type, 
comment_author, comment_author_email, comment_author_url, comment_content, 
other)
    return callAkismet('comment-check', user_ip, user_agent, referrer, 
permalink, comment_type, comment_author, comment_author_email, 
comment_author_url, comment_content, other)
  end
  
  # This call is for submitting comments that weren't marked as spam but should 
have been. It takes identical arguments as comment check.
  # The call parameters are the same as for the #commentCheck method.
  def submitSpam(user_ip, user_agent, referrer, permalink, comment_type, 
comment_author, comment_author_email, comment_author_url, comment_content, 
other)
    callAkismet('submit-spam', user_ip, user_agent, referrer, permalink, 
comment_type, comment_author, comment_author_email, comment_author_url, 
comment_content, other)
  end
  
  # This call is intended for the marking of false positives, things that were 
incorrectly marked as spam. It takes identical arguments as comment check and 
submit spam.
  # The call parameters are the same as for the #commentCheck method.
  def submitHam(user_ip, user_agent, referrer, permalink, comment_type, 
comment_author, comment_author_email, comment_author_url, comment_content, 
other)
    callAkismet('submit-ham', user_ip, user_agent, referrer, permalink, 
comment_type, comment_author, comment_author_email, comment_author_url, 
comment_content, other)
  end
end

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Hobix-is-the-way mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/hobix-is-the-way

Reply via email to