From: Dobrica Pavlinusic Date: Mon, 30 Aug 2010 15:22:58 +0000 (+0000) Subject: join into muc X-Git-Url: http://git.rot13.org/?p=MQR.git;a=commitdiff_plain;h=b34e49371094f206cba05bc5fc2908ad81e87a57 join into muc --- diff --git a/scripts/mqr-xmpp-client.pl b/scripts/mqr-xmpp-client.pl index e41b870..23dbfd6 100755 --- a/scripts/mqr-xmpp-client.pl +++ b/scripts/mqr-xmpp-client.pl @@ -7,6 +7,8 @@ use AnyEvent::XMPP::Client; use AnyEvent::XMPP::Ext::Disco; use AnyEvent::XMPP::Ext::Version; use AnyEvent::XMPP::Namespaces qw/xmpp_ns/; +use AnyEvent::XMPP::Ext::MUC; +use AnyEvent::XMPP::Util qw/node_jid res_jid/; use AnyEvent::Redis; use Data::Dump qw(dump); use Encode; @@ -14,16 +16,19 @@ use Carp qw(confess); binmode STDOUT, ":utf8"; -my $jid = $ENV{XMPP_JID} || die "XMPP_JID"; -my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD"; +my $jid = $ENV{XMPP_JID} || die "XMPP_JID"; +my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD"; +my $room = $ENV{XMPP_ROOM} || die "XMPP_ROOM"; my $j = AnyEvent->condvar; my $cl = AnyEvent::XMPP::Client->new (debug => 1); my $disco = AnyEvent::XMPP::Ext::Disco->new; my $version = AnyEvent::XMPP::Ext::Version->new; +my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco); $cl->add_extension ($disco); $cl->add_extension ($version); +$cl->add_extension ($muc); $cl->set_presence(undef, 'I\'m a talking bot.', 1); @@ -32,7 +37,7 @@ warn "connecting to $jid...\n"; my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } ); -our $contacts; +our $contacts = { $room => 1 }; $sub->psubscribe( 'channel *', sub { my ( $message, $from ) = @_; @@ -52,6 +57,22 @@ $cl->reg_cb ( session_ready => sub { my ($cl, $acc) = @_; warn "connected!\n"; + $muc->join_room ($acc->connection, $room, node_jid ($acc->jid)); + $muc->reg_cb ( + message => sub { + my ($cl, $room, $msg, $is_echo) = @_; + + return if $is_echo; + return if $msg->is_delayed; + my $mynick = res_jid ($room->nick_jid); + if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) { + my $ans = answer_to ($1); + my $repl = $msg->make_reply; + $repl->add_body ($ans); + $repl->send; + } + } + ); }, message => sub { my ($cl, $acc, $msg) = @_;