Plagger::Plugin::Publish::POPFile v0.01

同じくPlaggerConネタ。実際には::Action::POPFileとか::Train::POPFileとかいう内容。

package Plagger::Plugin::Publish::POPFile;

use strict;
use base qw( Plagger::Plugin );

our $VERSION = '0.01';

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.init'     => \&connect_popfile,
        'publish.entry'    => \&train_popfile,
        'publish.finalize' => \&disconnect_popfile,
    );
}

sub train_popfile {
    my ($self, $context, $args) = @_;

    my $entry    = $args->{entry};
    my $filename = write_tmpfile($self, $context, $args);

    my $bucket = $self->{popfile}->call(
        'POPFile/API.handle_message',
        $self->{popfile_session},
        $filename,
        "$filename.out"
    )->result;

    $context->log(debug => $entry->permalink . ": $bucket");
}

# XXX: these three functions are in common and should be separate.

use XMLRPC::Lite;
use File::Temp ();
use Encode;

sub connect_popfile {
    my ($self, $context, $args) = @_;

    $context->log(debug => "hello, POPFile");
    $self->{popfile} = XMLRPC::Lite->proxy($self->conf->{proxy});
    $self->{popfile_session} = $self->{popfile}->call(
        'POPFile/API.get_session_key',
        'admin',
        ''
    )->result;

    $context->log(debug => "session: $self->{popfile_session}");

    $self->{popfile_tempdir} = File::Temp::tempdir(
        CLEANUP => 1,
    );
}

sub disconnect_popfile {
    my ($self, $context, $args) = @_;

    $context->log(debug => "good-bye, POPFile");
    $self->{popfile}->call(
         'POPFile/API.release_session_key',
         $self->{popfile_session}
    );
}

sub write_tmpfile {
    my ($self, $context, $args) = @_;

    my $encoding  = $self->conf->{encoding} || 'utf8';
    my $entry     = $args->{entry};
    my $text      = $entry->body_text;

    my ($fh, $filename) = File::Temp::tempfile(
        DIR => $self->{popfile_tempdir},
    );

    print $fh
        'From: (', $entry->permalink, ') <plagger@localhost>', "\n",
        'To: <plagger@localhost>', "\n",
        'Subject: ', encode($encoding, $entry->title), "\n\n",
        encode($encoding, $text), "\n";
    close $fh;

    return $filename;
}

1;

__END__

=head1

Plagger::Plugin::Publish::POPFile - train POPFile

=head1 SYNOPSYS

  - module: Publish::POPFile
    config:
      proxy: http://localhost:8081/RPC2
      encoding: euc-jp

=head1 CONFIG

=over 4

=item proxy

Your POPFile proxy URL.

=item encoding

Your POPFile encoding. Specify 'euc-jp' for Nihongo users.

=back

=head1 AUTHOR

Kenichi Ishigaki

=head1 SEE ALSO

C<Plagger>, POPFile

=cut