| 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 | |||
| 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('irc_notify.tt', $args); | ||
| 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 | my $out = $self->templatize(\$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 | }); | ||
| 59 | Encode::_utf8_off($out) if Encode::is_utf8($out); | ||
| 60 | Encode::from_to($out, 'utf-8', $charset) unless uc($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 | 1; | ||
| 74 | |||
| 75 | __END__ |