| 1 | package Plagger::Plugin::Notify::IRC; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | use POE::Component::IKC::ClientLite; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my($self, $context) = @_; | ||
| 10 | $context->register_hook( | ||
| 11 | $self, | ||
| 12 | 'publish.entry' => \&update, | ||
| 13 | 'plugin.init' => \&initialize, | ||
| 14 | ); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub initialize { | ||
| 18 | my($self, $context, $args) = @_; | ||
| 19 | |||
| 20 | my $host = $self->conf->{daemon_host} || 'localhost', | ||
| 21 | my $port = $self->conf->{daemon_port} || 9999; | ||
| 22 | |||
| 23 | $self->{remote} = POE::Component::IKC::ClientLite::create_ikc_client( | ||
| 24 | port => $port, | ||
| 25 | ip => $host, | ||
| 26 | name => 'Plagger' . $$, | ||
| 27 | timeout => 5, | ||
| 28 | ); | ||
| 29 | |||
| 30 | unless ($self->{remote}) { | ||
| 31 | my $msg = q{unable to connect to plagger-ircbot process on } | ||
| 32 | . "$host:$port" | ||
| 33 | . q{, if you're not running plagger-ircbot, you should be able } | ||
| 34 | . q{to start it with the same Notify::IRC config you passed to } | ||
| 35 | . q{plagger. }; | ||
| 36 | $context->log( error => $msg ); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub update { | ||
| 42 | my($self, $context, $args) = @_; | ||
| 43 | |||
| 44 | $context->log(info => "Notifying " . $args->{entry}->title . " to IRC"); | ||
| 45 | |||
| 46 | my $body = $self->templatize('irc_notify.tt', $args); | ||
| 47 | Encode::_utf8_off($body) if Encode::is_utf8($body); | ||
| 48 | Encode::from_to($body, 'utf-8', $self->conf->{charset}) | ||
| 49 | if $self->conf->{charset} && $self->conf->{charset} ne 'utf-8'; | ||
| 50 | for my $line (split("\n", $body)) { | ||
| 51 | $self->{remote}->post( 'notify_irc/update', $line ); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | |||
| 57 | __END__ |