massive routing cleanup
[MQR.git] / scripts / mqr-xmpp-client.pl
index 84acdd1..95d7814 100755 (executable)
@@ -7,49 +7,91 @@ 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;
+use Carp qw(confess);
 
 binmode STDOUT, ":utf8";
 
 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 $subscribe  = $ENV{XMPP_SUBSCRIBE} || die 'XMPP_SUBSCRIBE';
 
 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);
 
 $cl->add_account ($jid, $pw);
 warn "connecting to $jid...\n";
 
-my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
-my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
+my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+
+warn "# jid:$jid root:$room subscribe:$subscribe\n";
 
 our $contacts;
+our $muc_rooms;
 
-$sub->psubscribe( 'channel *', sub {
+$sub->psubscribe( "MSG $subscribe", sub {
        my ( $message, $from ) = @_;
        return unless $from !~ m/\Q$jid\E/; # skip our messages
-       warn "#Q<< $from [$jid] | $message\n";
-       my ( undef, $channel, $user ) = split(/ /,$from,3);
-warn "# contacts ",dump($contacts);
-       foreach my $contact ( keys %$contacts ) {
-               next if $from =~ m/\Q$contact\E/;
-               warn "# $jid [$from] -> [$contact] | $message\n";
-               $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
+       Encode::_utf8_on($message);
+       warn "#Q<< ", dump( $from, $message );
+       my ( undef, $gw, $user ) = split(/ /,$from,3);
+       foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
+warn "XXX send to $to\n";
+               next if $from =~ m/\Q$to\E/; # FIXME
+               warn "# $jid [$from] -> [$to] | $message\n";
+               my $body = "<$user\@$gw> | $message"; # FIXME
+               $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
        }
 });
 
+sub publish {
+       my ( $channel, $body ) = @_;
+       $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
+       Encode::_utf8_off($body);
+       my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+       $pub->publish( $channel, $body );
+       warn "#Q>> ",dump($channel, $body);
+}
+
 $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) = @_;
+               $muc_rooms->{ $room->nick_jid }++;
+warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
+
+            return if $is_echo;
+            return if $msg->is_delayed;
+
+               publish [ 'MSG', $jid, $msg->from ] => $msg->any_body;
+
+            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) = @_;
@@ -58,11 +100,7 @@ $cl->reg_cb (
 
                $contacts->{ $msg->from }++;
 
-warn "# contacts ",dump($contacts);
-
-               my $channel = join(' ', 'channel', $msg->from);
-               $pub->publish( $channel, $body );
-               warn "#X<< $channel | $body\n";
+               publish [ 'MSG', $jid, $msg->from ] => $body;
 
 #              my $repl = $msg->make_reply;
 #              $repl->add_body( $response );