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

Revision 269 (checked in by secondlife, 15 years ago)

Added Notify::SSTP

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($context, $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 sub templatize {
37     my($self, $context, $entry) = @_;
38     my $tt = $context->template();
39     $tt->process('sstp.tt', {
40         entry => $entry,
41     }, \my $out) or $context->error($tt->error);
42     $out;
43 }
44
45 1;
46
47 package Plagger::Plugin::Notify::SSTP::Send;
48 use strict;
49 use IO::Socket::INET;
50 use Encode;
51
52 our $VERSION = '0.0.1';
53
54 our $SEND = 'SEND SSTP/1.4';
55 our $SENDER = 'Plagger::Plugin::SSTP::Send';
56 our $BREAK = "\r\n";
57
58 sub new {
59     my $class = shift;
60     my $host = shift || 'localhost';
61     my $port = shift || 9801;
62     my $options = shift || {};
63     my $socket_options = {
64         PeerAddr => $host,
65         PeerPort => $port,
66         Proto    => 'tcp',
67         %$options,
68     };
69     bless {
70         socket_options => $socket_options,
71     }, $class;
72 }
73
74 sub socket {
75     my $self = shift;
76     IO::Socket::INET->new(%{$self->{socket_options}});
77 }
78
79 sub send {
80     my $self = shift;
81     my $str = shift;
82     $str =~ s/\r?\n/\\n/go;
83     utf8::decode($str) unless utf8::is_utf8($str);
84     utf8::encode($str);
85
86     $str .= "\\e";
87     my $options = shift || {};
88     my $send = {
89         Sender => $SENDER,
90         Script => $str,
91         Charset => 'UTF-8',
92         %$options,
93     };
94     my @result = $SEND;
95     push @result, map {"$_: $send->{$_}"} keys %$send;
96     push @result, '', '';
97     my $result = join $BREAK, @result;
98     my $socket = $self->socket;
99     print $socket $result;
100     $socket->flush;
101
102     <$socket>;
103 }
104
105 1;
106
107 __END__
108
109 =head1 NAME
110
111 Plagger::Plugin::Notify::SSTP - Notify feed updates to SSTP
112
113 =head1 SYNOPSIS
114
115   - module: Notify::SSTP
116     config:
117       host: 192.168.10.215 # default localhost
118       port: 9821 # default 9801
119
120 =head1 DESCRIPTION
121
122 This plugin publish feed updates to SSTP(Sakura Script Transfer Protocol)
123
124 =head1 AUTHOR
125
126 Yuichi Tateno (id:secondlife)
127
128 =head1 SEE ALSO
129
130 L<Plagger>, L<IO::Socket::INET>
131
132 =cut
Note: See TracBrowser for help on using the browser.