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

Revision 791 (checked in by topia, 14 years ago)

new plugin Notify::Tiarra

Line 
1 package Plagger::Plugin::Notify::Tiarra;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use IO::Socket::UNIX;
7 use Time::HiRes;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'publish.feed' => \&update,
14     );
15 }
16
17 sub update {
18     my($self, $context, $args) = @_;
19
20     $context->log(info => "Notifying " . $args->{feed}->title . " to IRC");
21
22     my $protocol = 'TIARRACONTROL/1.0';
23     my $tt = $context->template();
24     my $request_template = <<END;
25 NOTIFY System::SendMessage [% protocol %]\r
26 Sender: [% sender %]\r
27 Notice: [% use_notice %]\r
28 Channel: [% channel %]\r
29 Charset: [% charset %]\r
30 Text: [% text %]\r
31 \r
32 END
33
34     # be able to set charset except UTF-8,
35     # but anyway Tiarra processing message with UTF-8.
36     my $charset = $self->conf->{charset} || 'UTF-8';
37
38     my $body = $self->templatize($context, $args->{feed});
39
40     for my $line (split("\n", $body)) {
41         my $remote = IO::Socket::UNIX->new(
42             Type => SOCK_STREAM,
43             Peer => '/tmp/tiarra-control/' . $self->conf->{socketname},
44            );
45
46         unless ($remote) {
47             $context->log(error => "cannot open sock: $!");
48             return;
49         }
50
51         $tt->process(\$request_template, {
52             protocol => $protocol,
53             charset => $charset,
54             channel => $self->conf->{channel},
55             sender => $self->conf->{sender} || "Plagger/$Plagger::VERSION (http://plagger.bulknews.net/)",
56             use_notice => ($self->conf->{use_notice} ? 'yes' : 'no'),
57             text => $line,
58         }, \my $out) or $context->error($tt->error);
59         Encode::_utf8_off($out) if Encode::is_utf8($out);
60         Encode::from_to($out, 'utf-8', $charset) unless $charset eq 'UTF-8';
61         $remote->print($out);
62
63         my $resp = <$remote>;
64         if ($resp !~ /$protocol 200 OK/) {
65             $context->log(error => $resp);
66         }
67
68         $remote->close;
69         Time::HiRes::sleep( $self->conf->{send_interval} || 2 );
70     }
71 }
72
73 sub templatize {
74     my($self, $context, $feed) = @_;
75     my $tt = $context->template();
76     $tt->process('irc_notify.tt', {
77         feed => $feed,
78     }, \my $out) or $context->error($tt->error);
79     $out;
80 }
81
82 1;
83
84 __END__
85
86 =head1 NAME
87
88 Plagger::Plugin::Notify::Tiarra - Notify feed updates to Tiraa IRC Proxy
89
90 =head1 SYNOPSIS
91
92   - module: Notify::Tiarra
93     config:
94       socketname: foobar
95       channel: #plagger-test
96       use_notice: 1
97
98 =head1 DESCRIPTION
99
100 This plugin allows you to notify feed updates to IRC channels using
101 Tiarra IRC Proxy. This module uses Tiarra ControlPort feature and
102 System::SendMessage module to send notify.
103
104 =head1 AUTHOR
105
106 Tatsuya Noda
107
108 This module is based on Plagger::Plugin::Notify::IRC.
109
110 =head1 SEE ALSO
111
112 L<Plagger>, L<Plagger::Plugin::Notify::IRC>
113
114 =cut
115
Note: See TracBrowser for help on using the browser.