root/trunk/plagger/lib/Plagger/Plugin/Notify/SSTP.pm

Revision 1173 (checked in by miyagawa, 14 years ago)

Merge from refactoring-planet. YAY!

Line 
1 package Plagger::Plugin::Notify::SSTP;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 our $VERSION = '0.0.1';
6
7 sub register {
8     my($self, $context) = @_;
9     $context->register_hook(
10         $self,
11         'publish.feed' => \&feed,
12     );
13 }
14
15 sub feed {
16     my($self, $context, $args) = @_;
17     my $feed = $args->{feed};
18     my $title = $feed->title || '(no-title)';
19
20     my @messages = $title;
21     for my $entry ($args->{feed}->entries) {
22         push @messages, $self->templatize('sstp.tt', { entry => $entry });
23     }
24     my $message = join '\x', @messages;
25     $context->log(debug => $message);
26    
27     my $sstp = Plagger::Plugin::Notify::SSTP::Send->new(
28         $self->conf->{host} || 'localhost',
29         $self->conf->{port} || 9801,
30         $self->conf->{options} || {},
31     );
32     my $result = $sstp->send($message);
33     $context->log(debug => $result);
34 }
35
36 1;
37
38 package Plagger::Plugin::Notify::SSTP::Send;
39 use strict;
40 use IO::Socket::INET;
41 use Encode;
42
43 our $VERSION = '0.0.1';
44
45 our $SEND = 'SEND SSTP/1.4';
46 our $SENDER = 'Plagger::Plugin::SSTP::Send';
47 our $BREAK = "\r\n";
48
49 sub new {
50     my $class = shift;
51     my $host = shift || 'localhost';
52     my $port = shift || 9801;
53     my $options = shift || {};
54     my $socket_options = {
55         PeerAddr => $host,
56         PeerPort => $port,
57         Proto    => 'tcp',
58         %$options,
59     };
60     bless {
61         socket_options => $socket_options,
62     }, $class;
63 }
64
65 sub socket {
66     my $self = shift;
67     IO::Socket::INET->new(%{$self->{socket_options}});
68 }
69
70 sub send {
71     my $self = shift;
72     my $str = shift;
73     $str =~ s/\r?\n/\\n/go;
74     utf8::decode($str) unless utf8::is_utf8($str);
75     utf8::encode($str);
76
77     $str .= "\\e";
78     my $options = shift || {};
79     my $send = {
80         Sender => $SENDER,
81         Script => $str,
82         Charset => 'UTF-8',
83         %$options,
84     };
85     my @result = $SEND;
86     push @result, map {"$_: $send->{$_}"} keys %$send;
87     push @result, '', '';
88     my $result = join $BREAK, @result;
89     my $socket = $self->socket;
90     print $socket $result;
91     $socket->flush;
92
93     <$socket>;
94 }
95
96 1;
97
98 __END__
99
100 =head1 NAME
101
102 Plagger::Plugin::Notify::SSTP - Notify feed updates to SSTP
103
104 =head1 SYNOPSIS
105
106   - module: Notify::SSTP
107     config:
108       host: 192.168.10.215 # default localhost
109       port: 9821 # default 9801
110
111 =head1 DESCRIPTION
112
113 This plugin publish feed updates to SSTP(Sakura Script Transfer Protocol)
114
115 =head1 AUTHOR
116
117 Yuichi Tateno (id:secondlife)
118
119 =head1 SEE ALSO
120
121 L<Plagger>, L<IO::Socket::INET>
122
123 =cut
Note: See TracBrowser for help on using the browser.