| 1 |
package Plagger::Plugin::Filter::POPFile; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use XMLRPC::Lite; |
|---|
| 6 |
use File::Temp (); |
|---|
| 7 |
use Encode; |
|---|
| 8 |
|
|---|
| 9 |
sub register { |
|---|
| 10 |
my ($self, $context) = @_; |
|---|
| 11 |
$context->register_hook( |
|---|
| 12 |
$self, |
|---|
| 13 |
'plugin.init' => \&connect_popfile, |
|---|
| 14 |
'update.entry.fixup' => \&filter, |
|---|
| 15 |
'update.fixup' => \&disconnect_popfile, |
|---|
| 16 |
); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sub rule_hook { 'update.entry.fixup' } |
|---|
| 20 |
|
|---|
| 21 |
sub filter { |
|---|
| 22 |
my ($self, $context, $args) = @_; |
|---|
| 23 |
|
|---|
| 24 |
my $entry = $args->{entry}; |
|---|
| 25 |
my $filename = write_tmpfile($self, $context, $args); |
|---|
| 26 |
my $training = $self->conf->{training}; |
|---|
| 27 |
$training = 1 unless defined $training; |
|---|
| 28 |
|
|---|
| 29 |
my $bucket; |
|---|
| 30 |
if ($training) { |
|---|
| 31 |
$bucket = $self->{popfile}->call( |
|---|
| 32 |
'POPFile/API.handle_message', |
|---|
| 33 |
$self->{popfile_session}, |
|---|
| 34 |
$filename, |
|---|
| 35 |
"$filename.out" |
|---|
| 36 |
)->result; |
|---|
| 37 |
} |
|---|
| 38 |
else { |
|---|
| 39 |
$bucket = $self->{popfile}->call( |
|---|
| 40 |
'POPFile/API.classify', |
|---|
| 41 |
$self->{popfile_session}, |
|---|
| 42 |
$filename |
|---|
| 43 |
)->result; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
$context->log(debug => $entry->permalink . ": $bucket"); |
|---|
| 47 |
|
|---|
| 48 |
$entry->add_tag($bucket); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
sub connect_popfile { |
|---|
| 52 |
my ($self, $context, $args) = @_; |
|---|
| 53 |
|
|---|
| 54 |
$context->log(debug => "hello, POPFile"); |
|---|
| 55 |
$self->{popfile} = XMLRPC::Lite->proxy($self->conf->{proxy}); |
|---|
| 56 |
$self->{popfile_session} = $self->{popfile}->call( |
|---|
| 57 |
'POPFile/API.get_session_key', |
|---|
| 58 |
'admin', |
|---|
| 59 |
'' |
|---|
| 60 |
)->result; |
|---|
| 61 |
|
|---|
| 62 |
$context->log(debug => "session: $self->{popfile_session}"); |
|---|
| 63 |
|
|---|
| 64 |
$self->{popfile_tempdir} = File::Temp::tempdir( |
|---|
| 65 |
$self->conf->{tempdir} ? ( DIR => $self->conf->{tempdir} ) : (), |
|---|
| 66 |
CLEANUP => 1, |
|---|
| 67 |
); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
sub disconnect_popfile { |
|---|
| 71 |
my ($self, $context, $args) = @_; |
|---|
| 72 |
|
|---|
| 73 |
$context->log(debug => "good-bye, POPFile"); |
|---|
| 74 |
$self->{popfile}->call( |
|---|
| 75 |
'POPFile/API.release_session_key', |
|---|
| 76 |
$self->{popfile_session} |
|---|
| 77 |
); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
sub write_tmpfile { |
|---|
| 81 |
my ($self, $context, $args) = @_; |
|---|
| 82 |
|
|---|
| 83 |
my $encoding = $self->conf->{encoding} || 'utf8'; |
|---|
| 84 |
my $entry = $args->{entry}; |
|---|
| 85 |
my $text = $entry->body_text; |
|---|
| 86 |
|
|---|
| 87 |
my ($fh, $filename) = File::Temp::tempfile( |
|---|
| 88 |
DIR => $self->{popfile_tempdir}, |
|---|
| 89 |
); |
|---|
| 90 |
|
|---|
| 91 |
print $fh |
|---|
| 92 |
'From: (', $entry->permalink, ') <plagger@localhost>', "\n", |
|---|
| 93 |
'To: <plagger@localhost>', "\n", |
|---|
| 94 |
'Subject: ', encode($encoding, $entry->title), "\n\n", |
|---|
| 95 |
encode($encoding, $text), "\n"; |
|---|
| 96 |
close $fh; |
|---|
| 97 |
|
|---|
| 98 |
return $filename; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
1; |
|---|
| 102 |
|
|---|
| 103 |
__END__ |
|---|
| 104 |
|
|---|
| 105 |
=head1 NAME |
|---|
| 106 |
|
|---|
| 107 |
Plagger::Plugin::Filter::POPFile - Categorize entries as spam et al |
|---|
| 108 |
|
|---|
| 109 |
=head1 SYNOPSIS |
|---|
| 110 |
|
|---|
| 111 |
- module: Filter::POPFile |
|---|
| 112 |
rule: |
|---|
| 113 |
module: Fresh |
|---|
| 114 |
mtime: |
|---|
| 115 |
path: /home/ishigaki/.plagger/fresh_rule |
|---|
| 116 |
autoupdate: 1 |
|---|
| 117 |
config: |
|---|
| 118 |
proxy: http://localhost:8081/RPC2 |
|---|
| 119 |
encoding: euc-jp |
|---|
| 120 |
training: 1 |
|---|
| 121 |
tempdir: /tmp |
|---|
| 122 |
|
|---|
| 123 |
=head1 CONFIG |
|---|
| 124 |
|
|---|
| 125 |
=over 4 |
|---|
| 126 |
|
|---|
| 127 |
=item proxy |
|---|
| 128 |
|
|---|
| 129 |
Your POPFile proxy URL. |
|---|
| 130 |
|
|---|
| 131 |
=item encoding |
|---|
| 132 |
|
|---|
| 133 |
Your POPFile encoding. Specify 'euc-jp' for Nihongo users. |
|---|
| 134 |
|
|---|
| 135 |
=item training |
|---|
| 136 |
|
|---|
| 137 |
Enables POPFile training (i.e. Adds entries to POPFile history). |
|---|
| 138 |
Defaults to true. |
|---|
| 139 |
|
|---|
| 140 |
=item tempdir (Optional) |
|---|
| 141 |
|
|---|
| 142 |
Temporary directory POPFile uses. Network directory might work, |
|---|
| 143 |
though I haven't tried yet. If you want to communicate with a |
|---|
| 144 |
remote POPFile (via Samba etc), try this. |
|---|
| 145 |
|
|---|
| 146 |
=back |
|---|
| 147 |
|
|---|
| 148 |
=head1 CAVEATS |
|---|
| 149 |
|
|---|
| 150 |
Don't forget to use Fresh rule while you're training POPFile. |
|---|
| 151 |
Otherwise your POPFile history would have a lot of duplicates. |
|---|
| 152 |
|
|---|
| 153 |
=head1 THANKS TO |
|---|
| 154 |
|
|---|
| 155 |
Tatsuhiko Miyagawa |
|---|
| 156 |
|
|---|
| 157 |
=head1 AUTHOR |
|---|
| 158 |
|
|---|
| 159 |
Kenichi Ishigaki |
|---|
| 160 |
|
|---|
| 161 |
=head1 SEE ALSO |
|---|
| 162 |
|
|---|
| 163 |
L<Plagger>, L<Plagger::Rule::Fresh>, POPFile |
|---|
| 164 |
|
|---|
| 165 |
=cut |
|---|