use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
-use AnyEvent::Redis;
+use AnyEvent::XMPP::Ext::MUC;
+use AnyEvent::XMPP::Util qw/node_jid res_jid/;
+use Data::Dump qw(dump);
+use Encode;
+use Carp qw(confess);
-binmode STDOUT, ":utf8";
+use lib 'lib';
+use MQR::Redis;
-our ($jid, $pw, $inputfile, $redis_host, $redis_port);
-require 'config.pl';
+binmode STDOUT, ":utf8";
-warn "# $jid <- $inputfile\n";
+my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
+my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
+my $room = $ENV{XMPP_ROOM} || warn "no XMPP_ROOM - not joining any muc rooms!\n";
+my $subscribe = $ENV{XMPP_SUBSCRIBE} || warn "no XMPP_SUBSCRIBE - pushing ALL MSG\n";
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->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 => $redis_host, port => $redis_port );
-my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+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 );
+}
$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) = @_;
+ my $room_jid = $room->nick_jid;
+ $muc_rooms->{ $room_jid }++;
+ $redis->sadd( "presence/xmpp/muc $jid" => $room_jid );
+warn "# MUC message ",dump( $room_jid, $msg->any_body, $is_echo );
+
+ return if $is_echo;
+ return if $msg->is_delayed;
+
+ my $from = $msg->from;
+ my $user = $from;
+ $user =~ s{^.+/}{};
+ publish "MSG $room_jid groupchat $user $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) = @_;
- my $response = '...';
-
+ my $from = $msg->from;
my $body = $msg->any_body;
- my $channel = join(' ', 'channel', $msg->from);
- $pub->publish( $channel, $body );
- warn "#X<< $channel | $body\n";
-
- if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
+# $contacts->{ $from }++; # don't push to anyone who sent message
- $sub->psubscribe( "channel $1", sub {
- my ( $message, $from ) = @_;
- return unless $from !~ m/\Q$jid\E/; # skip our messages
- warn "#Q<< $from | $message\n";
- my ( undef, $channal, $user ) = split(/ /,$from,3);
- $cl->send_message( "$channel <$user> $message" => $msg->from, $jid, 'chat' );
- });
+ my $user = $from;
+ $user =~ s{\@.+$}{};
- $response = "subscribed to $1";
+ my $type = $msg->type;
- }
+ publish "MSG $jid $type $user $from" => $body;
- my $repl = $msg->make_reply;
- $repl->add_body( $response );
- $repl->send;
- warn "#>>> $response\n";
+# my $repl = $msg->make_reply;
+# $repl->add_body( $response );
+# $repl->send;
+# warn "#>>> $response\n";
},
contact_request_subscribe => sub {
warn "Got disconnected: [@_]\n";
$j->broadcast;
},
+ roster_update => sub {
+ my ($con,$account,$roster) = @_;
+warn "XXXXX", ref($account), " | ", ref($roster);
+ foreach my $contact ( $roster->get_contacts ) {
+ $contacts->{ $contact->{jid} }++;
+ $redis->sadd( "presence/xmpp/contacts $jid" => $contact->{jid} );
+ warn "# contacts ",dump($contacts);
+ }
+ warn "# contacts ",dump($contacts);
+ },
);
$cl->start;