remove resource from contact jid
[MQR.git] / scripts / mqr-xmpp-client.pl
index 4981d07..d0a7e67 100755 (executable)
@@ -7,25 +7,13 @@ use AnyEvent::XMPP::Client;
 use AnyEvent::XMPP::Ext::Disco;
 use AnyEvent::XMPP::Ext::Version;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
-
-my @msgs;
-
-sub read_messages {
-   my ($msgs_file) = @_;
-   open my $f, $msgs_file
-      or die "Couldn't open messages file: '$msgs_file'\n";
-   (@msgs) = map { chomp; $_ } <$f>;
-   close $f;
-}
+use AnyEvent::Redis;
+use Data::Dump qw(dump);
 
 binmode STDOUT, ":utf8";
 
-our ($jid, $pw, $inputfile);
-require 'config.pl';
-
-warn "# $jid <- $inputfile\n";
-
-read_messages ($inputfile);
+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);
@@ -35,24 +23,54 @@ 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 => $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 {
       my ($cl, $acc) = @_;
       warn "connected!\n";
    },
    message => sub {
-      my ($cl, $acc, $msg) = @_;
-      my $talkmsg = $msgs[int (rand (@msgs))];
-      my $repl = $msg->make_reply;
-      $repl->add_body ("You said '".$msg->any_body."' but... " . $talkmsg);
-      warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
-      warn "Answered: $talkmsg\n";
-      $repl->send;
+               my ($cl, $acc, $msg) = @_;
+
+               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";
+
+#              my $repl = $msg->make_reply;
+#              $repl->add_body( $response );
+#              $repl->send;
+#              warn "#>>> $response\n";
+
    },
    contact_request_subscribe => sub {
       my ($cl, $acc, $roster, $contact) = @_;
@@ -68,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;