rename operations to !(un)track pattern
[MQR.git] / scripts / mqr-xmpp-client.pl
index 640fc2e..f7379a4 100755 (executable)
@@ -8,16 +8,8 @@ use AnyEvent::XMPP::Ext::Disco;
 use AnyEvent::XMPP::Ext::Version;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 use AnyEvent::Redis;
-
-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 Storable;
+use Data::Dump qw(dump);
 
 binmode STDOUT, ":utf8";
 
@@ -26,8 +18,6 @@ require 'config.pl';
 
 warn "# $jid <- $inputfile\n";
 
-read_messages ($inputfile);
-
 my $j       = AnyEvent->condvar;
 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
@@ -41,7 +31,40 @@ $cl->set_presence (undef, 'I\'m a talking bot.', 1);
 $cl->add_account ($jid, $pw);
 warn "connecting to $jid...\n";
 
-my $redis = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+my $pub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
+
+my $track = eval { retrieve '/tmp/track' };
+
+sub track {
+       my ( $who, $to, $value ) = @_;
+       warn "# ${value}track $who $to\n";
+
+       if ( $value ) {
+
+               $track->{$who}->{$to} = time;
+
+               $sub->psubscribe( "channel $to", sub {
+                       my ( $message, $from ) = @_;
+                       return unless $from !~ m/\Q($to|$jid)\E/; # skip our messages
+                       warn "#Q<< $from [$jid] | $message\n";
+                       my ( undef, $channel, $user ) = split(/ /,$from,3);
+                       $cl->send_message( "$channel <$user> $message", $who => $jid, 'chat' );
+               });
+
+       } else {
+               delete $track->{$who}->{$to};
+               $sub->punsubscribe( "channel $to");
+       }
+
+       store $track, '/tmp/track';
+}
+
+foreach my $who ( keys %$track ) {
+       foreach my $to ( keys %{ $track->{$who} } ) {
+               track $who => $to, time;
+       }
+}
 
 $cl->reg_cb (
    session_ready => sub {
@@ -49,16 +72,28 @@ $cl->reg_cb (
       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";
+               my ($cl, $acc, $msg) = @_;
+
+               my $response;
+
+               my $body = $msg->any_body;
+
+               my $channel = join(' ', 'channel', $msg->from);
+               $pub->publish( $channel, $body );
+               warn "#X<< $channel | $body\n";
+
+               if ( $msg =~ m/^!(un)?track\s+(\S+)/ ) {
+                       track $msg->from => $2, $1 ? 0 : 1;
+                       $response = "track: " . dump( $track->{$msg->from} );
+               } elsif ( $msg =~ m/^!/ ) {
+                       $response = "UNKNOWN $msg";
+               }
 
-               $redis->publish( join(' ', 'channel', $msg->from), $msg->any_body );
+               my $repl = $msg->make_reply;
+               $repl->add_body( $response );
+               $repl->send;
+               warn "#>>> $response\n";
 
-      $repl->send;
    },
    contact_request_subscribe => sub {
       my ($cl, $acc, $roster, $contact) = @_;