root/branches/release-39/plugins/TypePadAntiSpam/lib/MT/TypePadAntiSpam.pm @ 2461

Revision 2461, 2.8 kB (checked in by bchoate, 18 months ago)

Checkin of TypePad AntiSpam plugin; MT version bump.

Line 
1# MT::TypePadAntiSpam, (C) 2008 Six Apart, Ltd.
2# Based on Akismet plugin by Tim Appnel
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id: TypePadAntiSpam.pm 81617 2008-05-23 20:11:13Z bchoate $
7
8package MT::TypePadAntiSpam;
9
10use strict;
11use base qw( MT::ErrorHandler );
12
13use Storable;
14use LWP::UserAgent;
15use Carp qw( croak );
16
17our $VERSION      = '1.0';
18our $API_VERSION  = '1.1';
19our $SERVICE_HOST = 'api.antispam.typepad.com';
20
21sub check       { shift->_typepadantispam('comment-check', 1, @_) }
22sub submit_spam { shift->_typepadantispam('submit-spam',   0, @_) }
23sub submit_ham  { shift->_typepadantispam('submit-ham',    0, @_) }
24
25sub verify {
26    my ($class, $key, $url) = @_;
27    return $class->error("API key is a required parameter.")
28      unless $key;
29    return $class->error("The URL of the site is a required parameter.")
30      unless $url;
31    my $agent = $class->_get_agent;
32    my $r =
33    $agent->post("http://$SERVICE_HOST/$API_VERSION/verify-key",
34        {key => $key, blog => $url});
35
36    # Could be more RESTful in that status code should indicate success
37    # or failure not a string in the content of the response.
38    my $res = MT::TypePadAntiSpam::Response->new;
39    $res->http_response($r);
40    $res->http_status($r->code);
41    $res->status($r->is_success && $r->content =~ /valid/i ? 1 : 0);
42    $res;
43}
44
45#--- internals
46
47my $num_dumped = 0;
48sub _typepadantispam {
49    my ($class, $meth, $is_true, $sig, $key) = @_;
50    unless ($key) {
51        my $plugin = MT::Plugin::TypePadAntiSpam->instance;
52        # print STDERR "DEBUG: processing\n" if MT->config->DebugMode;
53        return $class->error(
54            $plugin->translate("API key is a required parameter.")
55        );
56    }
57    my $agent = $class->_get_agent;
58    my $r =
59    $agent->post("http://$key.$SERVICE_HOST/$API_VERSION/$meth", [%ENV, %$sig]);
60    my $res = MT::TypePadAntiSpam::Response->new;
61    $res->http_response($r);
62    $res->http_status($r->code);
63    my $status = 0;
64    if ($r->is_success) {
65        $status = $is_true ? $r->content =~ /false/i : 1;
66    }
67    $res->status($status);    # 1 (true) means valid api key or not spam.
68    $res;
69}
70
71our $AGENT;
72sub _get_agent {
73    my $class = shift;
74    return $AGENT if $AGENT;
75    $AGENT = MT->new_ua;
76    $AGENT->agent(join '/', $class, $class->VERSION);
77    $AGENT->timeout(10);
78    $AGENT;
79}
80
81package MT::TypePadAntiSpam::Signature;
82use base qw( Class::Accessor::Fast );
83MT::TypePadAntiSpam::Signature->mk_accessors(
84    qw( blog user_ip user_agent referrer permalink comment_type
85      comment_author comment_author_email comment_author_url
86      comment_content article_date )
87);
88
89package MT::TypePadAntiSpam::Response;
90use base qw( Class::Accessor::Fast );
91MT::TypePadAntiSpam::Response->mk_accessors(qw( status http_status http_response ));
92
931;
Note: See TracBrowser for help on using the browser.