root/SpamSubmission/trunk/SpamSubmission.pl

Revision 1676 (checked in by miyagawa, 14 years ago)

API error handling

Line 
1 package MT::Plugin::SpamSubmission;
2 # $Id$
3 use strict;
4 use base qw(MT::Plugin);
5
6 use MT::Comment;
7 use MT::TBPing;
8
9 our $VERSION = "0.90";
10
11 my $plugin = MT::Plugin::SpamSubmission->new({
12     name => "SpamSubmission",
13     version => $VERSION,
14     description => "Submit URLs and IPs found in your junked feedbacks (Comments/Trackbacks) via Bulkfeeds SPAM Submission API",
15     author_name => 'Tatsuhiko Miyagawa',
16     author_link => 'http://blog.bulknews.net/mt/',
17     config_template => 'spam_submission.tmpl',
18     settings => MT::PluginSettings->new([
19         ['auth_type' => { Default => 'apikey' }],
20         ['apikey'],
21         ['typekey_username'],
22         ['typekey_password'],
23     ]),
24 });
25
26 MT->add_plugin($plugin);
27
28 sub instance { $plugin }
29
30 sub uniq {
31     my @list = @_;
32     my %uniq;
33     $uniq{$_}++ for @list;
34     keys %uniq;
35 }
36
37 for my $class (qw(MT::Comment MT::TBPing)) {
38     no strict 'refs';
39     local $SIG{__WARN__} = sub { };
40     my $old = $class->can('junk');
41     *{$class . "::junk"} = sub {
42         my $obj = shift;
43         my @urls = uniq(find_uris($obj->all_text));
44         submit_spams(\@urls);
45         $obj->$old(@_);
46     };
47 }
48
49 sub find_uris {
50     my $text = shift;
51     return $text =~ /https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g; #']/;
52 }
53
54 sub submit_spams {
55     my $urls_ref = shift;
56
57     ## prepare API key
58     my %apikeys = prepare_apikeys();
59     %apikeys or do {
60         MT::log("No API key configuration for SpamSubmission");
61         return;
62     };
63
64     MT::log("Submitting " . join(" ", @$urls_ref) . " as Blacklist");
65     my $ua = MT->new_ua;
66     $ua->agent($plugin->name . "/" . $plugin->version);
67     $ua->timeout(5);
68
69     my $res = $ua->post("http://bulkfeeds.net/app/submit_spam.xml", {
70         url => join("\n", @$urls_ref),
71         %apikeys,
72     });
73
74     if ($res->is_success) {
75         my $xml = $res->content;
76         if ($xml =~ m!<error>(.*)</error>!) {
77             MT::log("error: $1");
78         } else {
79             MT::log("success");
80         }
81     } else {
82         MT::log("Submit API failure: " . $res->code);
83     }
84 }
85
86 sub prepare_apikeys {
87     my $config = MT::Plugin::SpamSubmission->instance->get_config_hash;
88     if ($config->{auth_type} eq 'apikey') {
89         return $config->{apikey} ? (apikey => $config->{apikey}) : ();
90     } else {
91         # request TypeKey auth
92         my($user, $pass) = @{$config}{qw(typekey_username typekey_password)};
93         return unless $user && $pass;
94
95         require HTTP::Request::Common;
96         my $req = HTTP::Request::Common::POST(
97             "https://www.typekey.com/t/typekey/login", [
98                 __mode => 'save_login',
99                 _return => 'http://bulkfeeds.net/app/submit_spam.xml?__tk=1',
100                 t => 'pIP85xqmwm2l37HOgBrA',
101                 v => '1.1',
102                 need_email => 1,
103                 username => $user,
104                 password => $pass,
105             ],
106         );
107
108         my $ua = MT->new_ua;
109         $ua->agent($plugin->name . "/" . $plugin->version);
110         my $res = $ua->simple_request($req);
111         if ($res->is_redirect and my $loc = $res->header('Location')) {
112             my $uri = URI->new($loc);
113             return $uri->query_form;
114         }
115     }
116
117     return;
118 }
119
120 1;
121
Note: See TracBrowser for help on using the browser.