+warn "# jid:$jid root:$room subscribe:$subscribe\n";
+
+our $contacts;
+our $muc_rooms;
+
+our $redis = MQR::Redis->redis;
+
+sub publish;
+
+MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
+ my ( $body, $channel ) = @_;
+ warn "<<<< ", dump( $channel, $body );
+ Encode::_utf8_on($body);
+
+ my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
+
+ return if $from eq $jid; # FIXME skip own
+
+ foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
+ next if $from =~ m/\Q$to\E/; # FIXME
+ my $type = defined $muc_rooms->{$to} ? 'groupchat' : 'chat';
+ publish "SEND $jid $type $user $to" => $body;
+ }
+}) if $subscribe;
+
+MQR::Redis->redis->psubscribe( "SEND $jid *", sub {
+ my ( $body, $channel ) = @_;
+ warn "<<<< ",dump( $channel, $body );
+ Encode::_utf8_on($body);
+
+ my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
+ warn "SEND $jid $type $to <$user> $body\n";
+ $cl->send_message( "<$user> $body", $to => $jid, $type );
+});
+
+#redis->psubscribe( '*' => sub { warn @_, $/ };
+
+sub publish {
+ my ( $channel, $body ) = @_;
+ Encode::_utf8_off($body);
+ MQR::Redis->publish( $channel, $body );
+}