implement end over redis queue
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 20:40:00 +0000 (22:40 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 20:40:00 +0000 (22:40 +0200)
- XMPP_ROOM is now optional, for useage without muc
- XMPP_SUBSCRIBE is also optional, defulting to all
- create separate object for each subscribe, as we should with AnyEvent
- make MSG and SEND symetric, including message type
- implement SEND queue and use it
- added echo on private messages

scripts/mqr-xmpp-client.pl

index 2536051..7c2e89f 100755 (executable)
@@ -18,8 +18,8 @@ binmode STDOUT, ":utf8";
 
 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
 my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
-my $room = $ENV{XMPP_ROOM} || die "XMPP_ROOM";
-my $subscribe  = $ENV{XMPP_SUBSCRIBE} || die 'XMPP_SUBSCRIBE';
+my $room = $ENV{XMPP_ROOM} || warn "no XMPP_ROOM - not joining any muc rooms!\n";
+my $subscribe  = $ENV{XMPP_SUBSCRIBE} || warn "no XMPP_SUBSCRIBE - pushing ALL MSG\n";
 
 my $j       = AnyEvent->condvar;
 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
@@ -36,36 +36,48 @@ $cl->set_presence(undef, 'I\'m a talking bot.', 1);
 $cl->add_account ($jid, $pw);
 warn "connecting to $jid...\n";
 
-my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+sub redis {
+       AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+}
 
 warn "# jid:$jid root:$room subscribe:$subscribe\n";
 
 our $contacts;
 our $muc_rooms;
 
-$sub->psubscribe( "MSG $subscribe", sub {
+sub publish;
+
+redis->psubscribe( "MSG $subscribe", sub {
        my ( $message, $from ) = @_;
        return unless $from !~ m/\Q$jid\E/; # skip our messages
        Encode::_utf8_on($message);
-       warn "#Q<< ", dump( $from, $message );
-       my ( undef, $gw, $user ) = split(/ /,$from,3);
+       warn "<<<< ", dump( $from, $message );
+       my ( undef, $gw, $user, $type ) = split(/ /,$from,3);
        foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
 warn "XXX send to $to\n";
                next if $from =~ m/\Q$to\E/; # FIXME
-               warn "# $jid [$from] -> [$to] | $message\n";
                $user =~ s{!.+}{};
                my $body = "<$user> $message"; # FIXME
-               $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
+               publish [ 'SEND', $jid, $to, $type ] => $body;
        }
 });
 
+redis->psubscribe( "SEND $jid *", sub {
+       my ( $body, $from ) = @_;
+       my ( undef, undef, $to, $type ) = split(/\s/, $from);
+       warn "SEND $jid -> $to $type | $body\n";
+       $cl->send_message( $body, $to => $jid, $type );
+});
+
+#redis->psubscribe( '*' => sub { warn @_, $/ };
+
 sub publish {
        my ( $channel, $body ) = @_;
        $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
        Encode::_utf8_off($body);
        my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
        $pub->publish( $channel, $body );
-       warn "#Q>> ",dump($channel, $body);
+       warn ">>>> ",dump($channel, $body);
 }
 
 $cl->reg_cb (
@@ -82,7 +94,7 @@ warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
             return if $is_echo;
             return if $msg->is_delayed;
 
-               publish [ 'MSG', $jid, $msg->from ] => $msg->any_body;
+               publish [ 'MSG', $jid, $msg->from, 'groupchat' ] => $msg->any_body;
 
             my $mynick = res_jid ($room->nick_jid);
             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
@@ -97,11 +109,13 @@ warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
+               my $to   = $msg->from;
                my $body = $msg->any_body;
 
-               $contacts->{ $msg->from }++;
+               $contacts->{ $to }++;
 
-               publish [ 'MSG', $jid, $msg->from ] => $body;
+               publish [ 'MSG',  $jid, $to, 'chat' ] => $body;
+               publish [ 'SEND', $jid, $to, 'chat' ] => "ECHO: $body";
 
 #              my $repl = $msg->make_reply;
 #              $repl->add_body( $response );