A Day in the Life

Plagger::Plugin::Publish::SSTP

Plagger::Plugin::Publish::SSTP

ぽっくんオタなの><ノ

エンコード周りが全然わからない・・・。

Plagger::Plugin::Publish::SSTP

package Plagger::Plugin::Publish::SSTP;
use strict;
use base qw( Plagger::Plugin );

our $VERSION = '0.0.1';

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.feed' => \&feed,
    );
}

sub feed {
    my($self, $context, $args) = @_;
    my $feed = $args->{feed};
    my $title = $feed->title || '(no-title)';

    my @messages = $title;
    for my $entry ($args->{feed}->entries) {
        push @messages, $self->templatize($context, $entry);
    }
    my $message = join '\x', @messages;
    $context->log(debug => $message);

    my $sstp = Plagger::Plugin::Publish::SSTP::Send->new(
        $self->conf->{host} || 'localhost'
    );
    my $result = $sstp->send($message);
    $context->log(debug => $result);
}

sub templatize {
    my($self, $context, $entry) = @_;
    my $tt = $context->template();
    $tt->process('sstp.tt', {
        entry => $entry,
    }, \my $out) or $context->error($tt->error);
    $out;
}

1;

package Plagger::Plugin::Publish::SSTP::Send;
use strict;
use IO::Socket::INET;
use Encode;

our $VERSION = '0.0.1';

our $SEND = 'SEND SSTP/1.4';
our $SENDER = 'Plagger::Plugin::Publish::SSTP::Send';
our $BREAK = "\r\n";

sub new {
    my $class = shift;
    my $host = shift || 'localhost';
    my $options = shift || {};
    my $socket_options = {
        PeerAddr => $host,
        PeerPort => 9801,
        Proto    => 'tcp',
        %$options,
    };
    bless {
        socket_options => $socket_options,
    }, $class;
}

sub socket {
    my $self = shift;
    IO::Socket::INET->new(%{$self->{socket_options}});
}

sub send {
    my $self = shift;
    my $str = shift;
    $str =~ s/\r?\n/\\n/go;
    utf8::decode($str) unless utf8::is_utf8($str);
    utf8::encode($str);

    $str .= "\\e";
    my $options = shift || {};
    my $send = {
        Sender => $SENDER,
        Script => $str,
        Charset => 'UTF-8',
        %$options,
    };
    my @result = $SEND;
    push @result, map {"$_: $send->{$_}"} keys %$send;
    push @result, '', '';
    my $result = join $BREAK, @result;
    my $socket = $self->socket;
    print $socket $result;
    $socket->flush;

    <$socket>;
}

1;

sstp.tt

[% entry.title || '(no-title)' %]
[% IF entry.author %] by [% entry.author %] [% END %]
[% entry.permalink %]

config.yaml

- module: Publish::SSTP
    config:
      host: 192.168.0.10

sstp

記事の一覧 >