another refactor to use MSG and SEND all over
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 23:06:29 +0000 (01:06 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 23:06:29 +0000 (01:06 +0200)
- use MQR::Redis
- MSG gw root user from
- SEND is client specific at the moment

lib/MQR/Redis.pm
scripts/mqr-irc-client.pl
scripts/mqr-xmpp-client.pl
t/10-Redis.t

index 119cb46..8835471 100644 (file)
@@ -5,11 +5,13 @@ use Carp qw(confess);
 use Data::Dump qw(dump);
 
 sub redis {
 use Data::Dump qw(dump);
 
 sub redis {
-       AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+       my $redis = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
+       $redis->ping;
+       return $redis;
 }
 
 sub publish {
 }
 
 sub publish {
-       my ( $channel, $body ) = @_;
+       my ( $self, $channel, $body ) = @_;
        $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
        my $pub = redis;
        $pub->publish( $channel, $body );
        $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
        my $pub = redis;
        $pub->publish( $channel, $body );
index 03f981a..7055fec 100755 (executable)
@@ -13,25 +13,32 @@ use AnyEvent::Redis;
 use Data::Dump qw(dump);
 use Carp qw(confess);
 
 use Data::Dump qw(dump);
 use Carp qw(confess);
 
+use lib 'lib';
+use MQR::Redis;
+
 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
+my $server = $ENV{IRC_SERVER} || die 'IRC_SERVER';
 my $subscribe  = $ENV{IRC_SUBSCRIBE} || die 'IRC_SUBSCRIBE';
 
 my $c = AnyEvent->condvar;
 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
 my $con = new AnyEvent::IRC::Client;
 
 my $subscribe  = $ENV{IRC_SUBSCRIBE} || die 'IRC_SUBSCRIBE';
 
 my $c = AnyEvent->condvar;
 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
 my $con = new AnyEvent::IRC::Client;
 
-my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
-
 warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";
 
 warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";
 
-$sub->psubscribe( "MSG $subscribe", sub {
+MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
        my ( $message, $from ) = @_;
        my ( $message, $from ) = @_;
+       warn "<<<< ",dump( $from, $message );
        return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
        return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
-       warn "#Q<< ",dump( $from, $message );
-       my ( undef, $gw, $user ) = split(/ /,$from,3);
-       $user =~ s{^.+/}{};
-       $con->send_msg( 'PRIVMSG', $room => "<$user> $message" );
+       my ( undef, $gw, $type, $user ) = split(/ /,$from);
+       MQR::Redis->publish( "SEND $nick\@$server $room" => "<$user> $message" );
+});
+
+MQR::Redis->redis->psubscribe( "SEND $nick\@$server $room", sub {
+       my ( $message, $from ) = @_;
+       warn "<<<< ",dump( $from, $message );
+       $con->send_msg( 'PRIVMSG', $room => $message );
 });
 
 $con->reg_cb (
 });
 
 $con->reg_cb (
@@ -57,10 +64,10 @@ $con->reg_cb (
 
                if ( $msg->{command} eq 'PRIVMSG' ) {
                        my ( $to, $body ) = @{ $msg->{params} };
 
                if ( $msg->{command} eq 'PRIVMSG' ) {
                        my ( $to, $body ) = @{ $msg->{params} };
-                       my $channel = join(' ', 'MSG', "$nick/$to", $msg->{prefix});
-                       warn "#Q>> $channel | $body\n";
-                       my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
-                       $pub->publish( $channel, $body );
+                       my $uid = $msg->{prefix};
+                       my $user = $uid;
+                       $user =~ s{!.+$}{};
+                       MQR::Redis->publish( "MSG $nick\@$server $to $user $uid" => $body );
                } elsif ( $msg->{command} eq 'MODE' ) {
                        $con->send_srv( JOIN => $room );
                }
                } elsif ( $msg->{command} eq 'MODE' ) {
                        $con->send_srv( JOIN => $room );
                }
@@ -113,6 +120,6 @@ warn dump(@msg);
 
 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
 
 
 $con->ctcp_auto_reply('VERSION', ['VERSION', 'MQR-IRC:0.1:Perl']);
 
-$con->connect ($ENV{IRC_SERVER}, $ENV{IRC_PORT} || 6667);
+$con->connect ($server, $ENV{IRC_PORT} || 6667);
 
 $c->wait;
 
 $c->wait;
index 7c2e89f..0d98860 100755 (executable)
@@ -9,11 +9,13 @@ use AnyEvent::XMPP::Ext::Version;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 use AnyEvent::XMPP::Ext::MUC;
 use AnyEvent::XMPP::Util qw/node_jid res_jid/;
 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 use AnyEvent::XMPP::Ext::MUC;
 use AnyEvent::XMPP::Util qw/node_jid res_jid/;
-use AnyEvent::Redis;
 use Data::Dump qw(dump);
 use Encode;
 use Carp qw(confess);
 
 use Data::Dump qw(dump);
 use Encode;
 use Carp qw(confess);
 
+use lib 'lib';
+use MQR::Redis;
+
 binmode STDOUT, ":utf8";
 
 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
 binmode STDOUT, ":utf8";
 
 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
@@ -36,10 +38,6 @@ $cl->set_presence(undef, 'I\'m a talking bot.', 1);
 $cl->add_account ($jid, $pw);
 warn "connecting to $jid...\n";
 
 $cl->add_account ($jid, $pw);
 warn "connecting to $jid...\n";
 
-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;
 warn "# jid:$jid root:$room subscribe:$subscribe\n";
 
 our $contacts;
@@ -47,37 +45,38 @@ our $muc_rooms;
 
 sub publish;
 
 
 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 "<<<< ", dump( $from, $message );
-       my ( undef, $gw, $user, $type ) = split(/ /,$from,3);
+MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
+       my ( $body, $channel ) = @_;
+       warn "<<<< ", dump( $channel, $body );
+       Encode::_utf8_on($body);
+
+       my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
+
+       return if $from eq $jid; # FIXME skip own
+
        foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
        foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
-warn "XXX send to $to\n";
                next if $from =~ m/\Q$to\E/; # FIXME
                next if $from =~ m/\Q$to\E/; # FIXME
-               $user =~ s{!.+}{};
-               my $body = "<$user> $message"; # FIXME
-               publish [ 'SEND', $jid, $to, $type ] => $body;
+               my $type = defined $muc_rooms->{$from} ? 'groupchat' : 'chat';
+               publish "SEND $jid $type $user $to" => $body;
        }
        }
-});
+}) if $subscribe;
 
 
-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 );
+MQR::Redis->redis->psubscribe( "SEND $jid *", sub {
+       my ( $body, $channel ) = @_;
+       warn "<<<< ",dump( $channel, $body );
+       Encode::_utf8_on($body);
+
+       my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
+       warn "# send ", dump( $jid, $type, $to, $user, $body );
+       $cl->send_message( "<$user> $body", $to => $jid, $type );
 });
 
 #redis->psubscribe( '*' => sub { warn @_, $/ };
 
 sub publish {
        my ( $channel, $body ) = @_;
 });
 
 #redis->psubscribe( '*' => sub { warn @_, $/ };
 
 sub publish {
        my ( $channel, $body ) = @_;
-       $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
        Encode::_utf8_off($body);
        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 ">>>> ",dump($channel, $body);
+       MQR::Redis->publish( $channel, $body );
 }
 
 $cl->reg_cb (
 }
 
 $cl->reg_cb (
@@ -94,7 +93,10 @@ warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
             return if $is_echo;
             return if $msg->is_delayed;
 
             return if $is_echo;
             return if $msg->is_delayed;
 
-               publish [ 'MSG', $jid, $msg->from, 'groupchat' ] => $msg->any_body;
+                       my $from = $msg->from;
+                       my $user = $from;
+                       $user =~ s{^.+/}{};
+                       publish "MSG $jid groupchat $user $from" => $msg->any_body;
 
             my $mynick = res_jid ($room->nick_jid);
             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
 
             my $mynick = res_jid ($room->nick_jid);
             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
@@ -109,13 +111,15 @@ warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
    message => sub {
                my ($cl, $acc, $msg) = @_;
 
-               my $to   = $msg->from;
+               my $from = $msg->from;
                my $body = $msg->any_body;
 
                my $body = $msg->any_body;
 
-               $contacts->{ $to }++;
+#              $contacts->{ $from }++; # don't push to anyone who sent message
+
+               my $user = $from;
+               $user =~ s{\@.+$}{};
 
 
-               publish [ 'MSG',  $jid, $to, 'chat' ] => $body;
-               publish [ 'SEND', $jid, $to, 'chat' ] => "ECHO: $body";
+               publish "MSG $jid chat $user $from" => $body;
 
 #              my $repl = $msg->make_reply;
 #              $repl->add_body( $response );
 
 #              my $repl = $msg->make_reply;
 #              $repl->add_body( $response );
index 07b76e5..1b553e1 100755 (executable)
@@ -12,3 +12,5 @@ ok( my $o = MQR::Redis->redis, 'redis' );
 
 ok( MQR::Redis->publish( 'TEST' => $0 ), 'publish' );
 
 
 ok( MQR::Redis->publish( 'TEST' => $0 ), 'publish' );
 
+#AE::cv->recv;
+# this will never send publish to queue