root/SpamSubmission/trunk/SpamSubmission.pl

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

Add Action and template

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 init_app {
31     my $plugin = shift;
32     $plugin->SUPER::init_app(@_);
33     my($app) = @_;
34
35     return unless $app->isa('MT::App::CMS');
36
37     $app->add_itemset_action({
38         type => 'comment',
39         key  => 'spam_submission_comment',
40         label => 'Report SPAM Comment(s)',
41         code => sub { $plugin->submit_spams_action('MT::Comment', @_) },
42     });
43
44     $app->add_itemset_action({
45         type => 'ping',
46         key  => 'spam_submission_ping',
47         label => 'Report SPAM TrackBack(s)',
48         code => sub { $plugin->submit_spams_action('MT::TBPing', @_) },
49     });
50 }
51
52 sub submit_spams_action {
53     my $plugin = shift;
54     my($class, $app) = @_;
55     my @obj = map $class->load($_), $app->param('id');
56     my @urls;
57     for my $obj (@obj) {
58         push @urls, uniq(find_uris($obj->all_text));
59     }
60     submit_spams(\@urls) if @urls;
61     $app->build_page($plugin->load_tmpl('results.tmpl'), { urls => [ map { +{value => $_} } @urls ], return_url => $app->return_uri });
62 }
63
64 sub uniq {
65     my @list = @_;
66     my %uniq;
67     $uniq{$_}++ for @list;
68     keys %uniq;
69 }
70
71 for my $class (qw(MT::Comment MT::TBPing)) {
72     no strict 'refs';
73     local $SIG{__WARN__} = sub { };
74     my $old = $class->can('junk');
75     *{$class . "::junk"} = sub {
76         my $obj = shift;
77         my $config = MT::Plugin::SpamSubmission->instance->get_config_hash;
78         my @urls = uniq(find_uris($obj->all_text));
79         submit_spams(\@urls);
80         $obj->$old(@_);
81     };
82 }
83
84 sub find_uris {
85     my $text = shift;
86     return $text =~ /https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g; #']/;
87 }
88
89 sub submit_spams {
90     my $urls_ref = shift;
91
92     ## prepare API key
93     my %apikeys = prepare_apikeys();
94     %apikeys or do {
95         MT::log("No API key configuration for SpamSubmission");
96         return;
97     };
98
99     MT::log("Submitting " . join(" ", @$urls_ref) . " as Blacklist");
100     my $ua = MT->new_ua;
101     $ua->agent($plugin->name . "/" . $plugin->version);
102     $ua->timeout(5);
103
104     my $res = $ua->post("http://bulkfeeds.net/app/submit_spam.xml", {
105         url => join("\n", @$urls_ref),
106         %apikeys,
107     });
108
109     if ($res->is_success) {
110         my $xml = $res->content;
111         if ($xml =~ m!<error>(.*)</error>!) {
112             MT::log("error: $1");
113         }
114     } else {
115         MT::log("Submit API failure: " . $res->code);
116     }
117 }
118
119 sub prepare_apikeys {
120     my $config = MT::Plugin::SpamSubmission->instance->get_config_hash;
121     if ($config->{auth_type} eq 'apikey') {
122         return $config->{apikey} ? (apikey => $config->{apikey}) : ();
123     } else {
124         # request TypeKey auth
125         my($user, $pass) = @{$config}{qw(typekey_username typekey_password)};
126         return unless $user && $pass;
127
128         require HTTP::Request::Common;
129         my $req = HTTP::Request::Common::POST(
130             "https://www.typekey.com/t/typekey/login", [
131                 __mode => 'save_login',
132                 _return => 'http://bulkfeeds.net/app/submit_spam.xml?__tk=1',
133                 t => 'pIP85xqmwm2l37HOgBrA',
134                 v => '1.1',
135                 need_email => 1,
136                 username => $user,
137                 password => $pass,
138             ],
139         );
140
141         my $ua = MT->new_ua;
142         $ua->agent($plugin->name . "/" . $plugin->version);
143         my $res = $ua->simple_request($req);
144         if ($res->is_redirect and my $loc = $res->header('Location')) {
145             my $uri = URI->new($loc);
146             return $uri->query_form;
147         }
148     }
149
150     return;
151 }
152
153 1;
154
Note: See TracBrowser for help on using the browser.