rename operations to !(un)track pattern
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 22:43:29 +0000 (00:43 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 22:43:29 +0000 (00:43 +0200)
scripts/mqr-xmpp-client.pl

index 06d4fda..f7379a4 100755 (executable)
@@ -34,28 +34,35 @@ 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 $subscriptions = eval { retrieve '/tmp/subs' };
+my $track = eval { retrieve '/tmp/track' };
 
-sub subscribe_channel {
-       my ( $who, $to ) = @_;
-       warn "# subscribe_channel $who $to\n";
+sub track {
+       my ( $who, $to, $value ) = @_;
+       warn "# ${value}track $who $to\n";
 
-       $subscriptions->{$who}->{$to} ||= time;
+       if ( $value ) {
 
-       $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' );
-       });
+               $track->{$who}->{$to} = time;
 
-       store $subscriptions, '/tmp/subs';
+               $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 %$subscriptions ) {
-       foreach my $to ( keys %{ $subscriptions->{$who} } ) {
-               subscribe_channel $who => $to;
+foreach my $who ( keys %$track ) {
+       foreach my $to ( keys %{ $track->{$who} } ) {
+               track $who => $to, time;
        }
 }
 
@@ -67,7 +74,7 @@ $cl->reg_cb (
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
-               my $response = '...';
+               my $response;
 
                my $body = $msg->any_body;
 
@@ -75,10 +82,11 @@ $cl->reg_cb (
                $pub->publish( $channel, $body );
                warn "#X<< $channel | $body\n";
 
-               if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
-                       subscribe_channel $msg->from => $1;
-                       $response = "subscribed to: " . dump( $subscriptions->{$msg->from} );
-
+               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";
                }
 
                my $repl = $msg->make_reply;