display MSG, even ignored ones
[MQR.git] / scripts / mqr-irc-client.pl
index fdfae80..408b2db 100755 (executable)
@@ -13,24 +13,32 @@ use AnyEvent::Redis;
 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 $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 $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";
 
-$sub->psubscribe( "MSG $subscribe", sub {
+MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
        my ( $message, $from ) = @_;
+       warn "<<<< ",dump( $from, $message );
        return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
-       warn "#Q<< ",dump( $from, $message );
-       my ( undef, $gw, $user ) = split(/ /,$from,3);
-       $con->send_msg( 'PRIVMSG', $room => "<$user> $message" );
+       my ( undef, $gw, $type, $user ) = split(/ /,$from);
+       MQR::Redis->publish( "SEND $nick\@$server $room $user" => "<$user> $message" );
+});
+
+MQR::Redis->redis->psubscribe( "SEND $nick\@$server $room *", sub {
+       my ( $message, $from ) = @_;
+       warn "PRIVMSG $room $message\n";
+       $con->send_msg( 'PRIVMSG', $room => $message );
 });
 
 $con->reg_cb (
@@ -56,10 +64,10 @@ $con->reg_cb (
 
                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 );
                }
@@ -112,6 +120,6 @@ warn dump(@msg);
 
 $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;