Notify::XMPP 0.02
- 最新版はこのへんにあるす。
- なんとなく日本語も送れるようにした。
- 送信先を複数指定できるようにした。
- それっぽくエラー処理をするようにした。
というわけで $P::P::Notify::XMPP::VERSION = 0.02 です。
package Plagger::Plugin::Notify::XMPP;
our $VERSION = '0.02';
use strict;
use warnings;
use base qw( Plagger::Plugin );
use Data::Dumper;
use Encode;
use Net::XMPP;
sub register {
my($self, $context) = @_;
$context->register_hook(
$self,
'publish.entry' => \¬ify,
);
}
sub notify {
my ($self, $context, $args) = @_;
my $xmpp = Net::XMPP::Client->new(debuglevel=>0);
my $server_host = $self->conf->{server_host} || 'localhost';
my $server_port = $self->conf->{server_port} || '5222';
my $tls = $self->conf->{tls} || 0;
my $username = $self->conf->{username} || do {
$context->log(error => "missing: username");
return;
};
my $password = $self->conf->{password} || do {
$context->log(error => "missing: password");
return;
};
my $to = $self->conf->{to} || do {
$context->log(error => "missing: to");
return;
};
my $body = $self->templatize('xmpp_notify.tt', $args);
utf8::decode($body) if utf8::is_utf8($body);
my $on_auth = sub {
foreach my $a_to (@{$to}) {
$context->log(info => "Notifying <" . $args->{entry}->title . "> to $a_to");
$xmpp->MessageSend(
to => $a_to,
body => $body,
);
$self->xmpp_error($context, $xmpp, 'MessageSend');
}
$xmpp->Disconnect();
$self->xmpp_error($context, $xmpp, 'Disconnect');
};
my $on_authfail = sub {
$context->log(error => "authentication failure");
};
$xmpp->SetCallBacks(
onauth => $on_auth,
onauthfail => $on_authfail,
);
$self->xmpp_error($context, $xmpp, 'SetCallBacks');
$xmpp->Execute(
hostname => $server_host,
port => $server_port,
tls => $tls,
username => $username,
password => $password,
connectattempts => 3,
connectsleep => 1,
resource => 'Plagger',
);
$self->xmpp_error($context, $xmpp, 'Execute');
}
sub xmpp_error {
my ($self, $context, $xmpp, $what) = @_;
if (my $e = $xmpp->GetErrorCode()) {
$context->log(error => "$what: ".(ref $e ? Data::Dumper->Dump([$e],['error']) : $e));
}
}
1;
__END__
# for Emacsen
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# coding: euc-jp
# End:
# vi: set ts=4 sw=4 sts=0 :Notify用のアカウント(たとえばplag_you:plahplahplah)をとってこんなYAMLで。
あらかじめNotify用のアカウントを受信者のコンタクトリストに登録しておかないとダメかも。(よーわからんすけど設定による?)
- module: Notify::XMPP
config:
server_host: jabber.org
username: plag_you
password: plahplahplah
to:
- 送りたい人の@jabber.org
- IDを書いてね@jabber.jp
- はぁと@gmail.comあ、あとテンプレート(assets/plugins/Notify-XMPP/xmpp_notify.tt)はこんなんで。
[%- SET title = feed.title || '(no-title)' -%] [% feed.title_text %]: [% entry.title_text %] - [% entry.link %][% IF entry.author %] by [% entry.author %][% END %]
本家に差し出すにはどうすればいいんだろ。MLかIRCすかね? 調べてみよ。