Updated 2018-01-07 11:29:14 by pooryorick

Here's a script to filter spam submissions (emails, pages, comments, whatever) through the Akismet filtering service - see http://akismet.com/ for details:
# Copyright (c) 2007 Jean-Claude Wippler
# http://www.opensource.org/licenses/mit-license.php

package provide akismet 0.1
package require http

# see http://akismet.com/development/api/
#
#   blog (required)
#     The front page or home URL of the instance making the request. For a blog
#     or wiki this would be the front page. Note: Must be a full URI, including
#     http://.
#
#   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 environment variables
#     In PHP there is an array of environment 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.

namespace eval akismet {
    variable key
    variable blog
    variable app

    proc setup {key_ blog_ {app_ ""}} {
        variable key $key_
        variable blog $blog_
        variable app $app_
        if {[_connect verify-key "" key $key] ne "valid"} {
            error "Akismet: invalid key"
        }
    }

    proc comment-check {uip uagent args} {
        variable key
        _connect comment-check $key. user_ip $uip user_agent $uagent {*}$args
    }

    proc submit-spam {uip uagent args} {
        variable key
        _connect submit-spam $key. user_ip $uip user_agent $uagent {*}$args
    }

    proc submit-ham {uip uagent args} {
        variable key
        _connect submit-ham $key. user_ip $uip user_agent $uagent {*}$args
    }

    namespace export *
    namespace ensemble create

    proc _connect {type prefix args} {
        variable blog
        variable app
        set ver [package require akismet]
        set token [::http::geturl http://${prefix}rest.akismet.com/1.1/$type \
                                                -headers [list User-Agent "$app | akismet.tcl/$ver"] \
                                                -query [http::formatQuery blog $blog {*}$args]]
        set ncode [http::ncode $token]
        set data [http::data $token]
        http::cleanup $token
        if {$ncode != 200} { error "Akismet: $type failed (code $ncode)" }
        return $data
    }
}

It's quite effective at tagging things we all are tired of having to clean up...

LV Too bad something like this couldn't be plugged into the Wiki...

jcw - Don't see why not...

jdc Is it possible to give a usage example? I've tried the following but always get the true return, indicating my string is spam (setup completed without errors):
set ip <my ip address>
set ua "Mozilla/5.0"
set rt [akismet::comment-check $ip $ua comment_content "An example of using this packages."]
puts "Check: $rt"