join into muc
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 15:22:58 +0000 (15:22 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 15:22:58 +0000 (15:22 +0000)
scripts/mqr-xmpp-client.pl

index e41b870..23dbfd6 100755 (executable)
@@ -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) = @_;