| 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 | |
|---|
| 8 | package MT::TypePadAntiSpam; |
|---|
| 9 | |
|---|
| 10 | use strict; |
|---|
| 11 | use base qw( MT::ErrorHandler ); |
|---|
| 12 | |
|---|
| 13 | use Storable; |
|---|
| 14 | use LWP::UserAgent; |
|---|
| 15 | use Carp qw( croak ); |
|---|
| 16 | |
|---|
| 17 | our $VERSION = '1.0'; |
|---|
| 18 | our $API_VERSION = '1.1'; |
|---|
| 19 | our $SERVICE_HOST = 'api.antispam.typepad.com'; |
|---|
| 20 | |
|---|
| 21 | sub check { shift->_typepadantispam('comment-check', 1, @_) } |
|---|
| 22 | sub submit_spam { shift->_typepadantispam('submit-spam', 0, @_) } |
|---|
| 23 | sub submit_ham { shift->_typepadantispam('submit-ham', 0, @_) } |
|---|
| 24 | |
|---|
| 25 | sub 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 | |
|---|
| 47 | my $num_dumped = 0; |
|---|
| 48 | sub _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 | |
|---|
| 71 | our $AGENT; |
|---|
| 72 | sub _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 | |
|---|
| 81 | package MT::TypePadAntiSpam::Signature; |
|---|
| 82 | use base qw( Class::Accessor::Fast ); |
|---|
| 83 | MT::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 | |
|---|
| 89 | package MT::TypePadAntiSpam::Response; |
|---|
| 90 | use base qw( Class::Accessor::Fast ); |
|---|
| 91 | MT::TypePadAntiSpam::Response->mk_accessors(qw( status http_status http_response )); |
|---|
| 92 | |
|---|
| 93 | 1; |
|---|