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

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

Filter::POPFile: oops. used wrong separator

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