Plagger/Plugin/Filter/POPFile.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 73 CC 4 SCAR 15.5
CC
4
Cov
0%
filter
12.0
connect_popfile
6.0
register
2.0
CRAP 4.7
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__