remove resource from contact jid
[MQR.git] / scripts / mqr-xmpp-client.pl
index b07cb93..d0a7e67 100755 (executable)
@@ -8,13 +8,12 @@ use AnyEvent::XMPP::Ext::Disco;
 use AnyEvent::XMPP::Ext::Version;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 use AnyEvent::Redis;
+use Data::Dump qw(dump);
 
 binmode STDOUT, ":utf8";
 
-our ($jid, $pw, $inputfile, $redis_host, $redis_port);
-require 'config.pl';
-
-warn "# $jid <- $inputfile\n";
+my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
+my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
 
 my $j       = AnyEvent->condvar;
 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
@@ -24,13 +23,28 @@ my $version = AnyEvent::XMPP::Ext::Version->new;
 $cl->add_extension ($disco);
 $cl->add_extension ($version);
 
-$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 );
+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} );
+
+our $contacts;
+
+$sub->psubscribe( 'channel *', 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' );
+       }
+});
 
 $cl->reg_cb (
    session_ready => sub {
@@ -40,32 +54,22 @@ $cl->reg_cb (
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
-               my $response = '...';
-
                my $body = $msg->any_body;
 
+               my $f = $msg->from;
+               $f =~ s!/.+!!;
+               $contacts->{ $f }++;
+
+warn "# contacts ",dump($contacts);
+
                my $channel = join(' ', 'channel', $msg->from);
                $pub->publish( $channel, $body );
                warn "#X<< $channel | $body\n";
 
-               if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
-
-                       $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' );
-                       });
-
-                       $response = "subscribed to $1";
-
-               }
-
-               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 {
@@ -82,6 +86,15 @@ $cl->reg_cb (
       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} }++;
+                       warn "# contacts ",dump($contacts);
+               }
+               warn "# contacts ",dump($contacts);
+       },
 );
 
 $cl->start;