massive routing cleanup
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 17:09:12 +0000 (17:09 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 30 Aug 2010 17:09:12 +0000 (17:09 +0000)
this allows us to make ping/pong work from irc #room to xmpp muc
and back for the first time

scripts/mqr-irc-client.pl
scripts/mqr-xmpp-client.pl

index 7873148..5e491be 100755 (executable)
@@ -15,8 +15,7 @@ use Carp qw(confess);
 
 my $nick = $ENV{IRC_NICK} || die "IRC_NICK";
 my $room = $ENV{IRC_ROOM} || die "IRC_ROOM";
-
-warn "# $ENV{IRC_SERVER}:$ENV{IRC_PORT} $room $nick\n";
+my $subscribe  = $ENV{IRC_SUBSCRIBE} || die 'IRC_SUBSCRIBE';
 
 my $c = AnyEvent->condvar;
 my $stdout = AnyEvent::Handle->new (fh => \*STDOUT);
@@ -24,9 +23,11 @@ my $con = new AnyEvent::IRC::Client;
 
 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
 
-$sub->psubscribe( 'channel *', sub {
+warn "# server:$ENV{IRC_SERVER}:$ENV{IRC_PORT} room:$room nick:$nick subscribe:$subscribe\n";
+
+$sub->psubscribe( "MSG $subscribe", sub {
        my ( $message, $from ) = @_;
-       return unless $from !~ m/\Q$nick\E/; # skip our messages
+       return unless $from !~ m/\Q($nick|$room)\E/; # FIXME: skip our messages
        warn "#Q<< ",dump( $from, $message );
        my ( undef, $channel, $user ) = split(/ /,$from,3);
        $con->send_msg( 'PRIVMSG', $room => join(' ',$channel, $user, $message) );
@@ -54,9 +55,11 @@ $con->reg_cb (
       );
 
                if ( $msg->{command} eq 'PRIVMSG' ) {
-                       my $channel = join(' ', 'channel', $msg->{params}->[0], $msg->{prefix});
+                       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, $msg->{params}->[1] );
+                       $pub->publish( $channel, $body );
                } elsif ( $msg->{command} eq 'MODE' ) {
                        $con->send_srv( JOIN => $room );
                }
index 23dbfd6..95d7814 100755 (executable)
@@ -16,9 +16,10 @@ use Carp qw(confess);
 
 binmode STDOUT, ":utf8";
 
-my $jid  = $ENV{XMPP_JID} || die "XMPP_JID";
-my $pw   = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
+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 $j       = AnyEvent->condvar;
 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
@@ -37,22 +38,35 @@ warn "connecting to $jid...\n";
 
 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
 
-our $contacts = { $room => 1 };
+warn "# jid:$jid root:$room subscribe:$subscribe\n";
 
-$sub->psubscribe( 'channel *', sub {
+our $contacts;
+our $muc_rooms;
+
+$sub->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, $channel, $user ) = split(/ /,$from,3);
-warn "# contacts ",dump($contacts);
-       foreach my $contact ( keys %$contacts ) {
-               next if $from =~ m/\Q$contact\E/;
-               warn "# $jid [$from] -> [$contact] | $message\n";
-               $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
+       my ( undef, $gw, $user ) = 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";
+               my $body = "<$user\@$gw> | $message"; # FIXME
+               $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
        }
 });
 
+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);
+}
+
 $cl->reg_cb (
    session_ready => sub {
       my ($cl, $acc) = @_;
@@ -61,9 +75,14 @@ $cl->reg_cb (
       $muc->reg_cb (
          message => sub {
             my ($cl, $room, $msg, $is_echo) = @_;
+               $muc_rooms->{ $room->nick_jid }++;
+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;
+
             my $mynick = res_jid ($room->nick_jid);
             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
                my $ans = answer_to ($1);
@@ -79,17 +98,9 @@ $cl->reg_cb (
 
                my $body = $msg->any_body;
 
-               my $f = $msg->from;
-               $f =~ s!/.+!!;
-               $contacts->{ $f }++;
-
-warn "# contacts ",dump($contacts);
+               $contacts->{ $msg->from }++;
 
-               my $channel = join(' ', 'channel', $jid, $msg->from);
-               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 "#X<< ",dump($channel, $body);
+               publish [ 'MSG', $jid, $msg->from ] => $body;
 
 #              my $repl = $msg->make_reply;
 #              $repl->add_body( $response );