implemented !subscribe pattern
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 21:15:58 +0000 (23:15 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 28 Aug 2010 21:15:58 +0000 (23:15 +0200)
scripts/mqr-xmpp-client.pl

index 640fc2e..b07cb93 100755 (executable)
@@ -9,16 +9,6 @@ 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;
-}
-
 binmode STDOUT, ":utf8";
 
 our ($jid, $pw, $inputfile, $redis_host, $redis_port);
@@ -26,8 +16,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 +29,8 @@ $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 );
 
 $cl->reg_cb (
    session_ready => sub {
@@ -49,16 +38,35 @@ $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/!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";
+
+               }
 
-               $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) = @_;