root/trunk/plagger/lib/Plagger/Plugin/Filter/SpamAssassin.pm

Revision 1517 (checked in by charsbar, 14 years ago)

Filter::SpamAssassin?: added tests and sample rules (and a few fixes for the plugin itself)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 package Plagger::Plugin::Filter::SpamAssassin;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Mail::SpamAssassin;
6 use MIME::Lite;
7 use Encode;
8 use Encode::MIME::Header;
9
10 sub register {
11     my($self, $context) = @_;
12     $context->register_hook(
13         $self,
14         'plugin.init'        => \&init_spamassassin,
15         'update.entry.fixup' => \&filter,
16     );
17 }
18
19 sub init_spamassassin {
20     my($self, $context, $args) = @_;
21
22     $context->log(debug => "initializing SpamAssassin");
23     $self->{spamassassin} = Mail::SpamAssassin->new($self->conf->{new});
24 }
25
26 sub filter {
27     my($self, $context, $args) = @_;
28
29     my $sa    = $self->{spamassassin};
30     my $entry = $args->{entry};
31     my $tag   = $self->conf->{spam_tag} || 'spam';
32
33     # create a pseudo mail header to skip some of the sa's default tests
34
35     my $mail  = MIME::Lite->new(
36         From       => 'plagger@localhost',
37         To         => 'plagger@localhost',
38         Subject    => encode('MIME-Header', $entry->title_text),
39         'X-Mailer' => 'plagger',
40         Data       => $entry->body_text,
41     )->as_string;
42
43     my $status = $sa->check_message_text( $mail );
44
45     if ($status->is_spam) {
46         $context->log(debug => "spam found");
47         $entry->body($entry->body . $status->get_report) if $self->conf->{add_report};
48         $entry->add_tag($tag);
49     }
50
51     $status->finish;
52 }
53
54 1;
55
56 __END__
57
58 =head1 NAME
59
60 Plagger::Plugin::Filter::SpamAssassin - Find spam entries
61
62 =head1 SYNOPSIS
63
64   - module: Filter::SpamAssassin
65     config:
66       spam_tag: spam
67       new:
68         local_tests_only: 1
69         site_rules_filename: some_rule.cf
70
71 =head1 CONFIG
72
73 =over 4
74
75 =item spam_tag
76
77 A string that will be added to the entry's tag. Defaults to 'spam'.
78
79 =item add_report (for debugging)
80
81 If set to true, the SpamAssassin's report will be added to spam
82 entry's body.
83
84 =item new
85
86 Options passed to Mail::SpamAssassin->new(). See L<Mail::SpamAssassin>
87 for details.
88
89 =back
90
91 =head1 AUTHOR
92
93 Kenichi Ishigaki
94
95 =head1 SEE ALSO
96
97 L<Plagger>, L<Mail::SpamAssassin>
98
99 =cut
Note: See TracBrowser for help on using the browser.