X-Git-Url: http://git.rot13.org/?p=MQR.git;a=blobdiff_plain;f=scripts%2Fmqr-xmpp-client.pl;h=0d988604c14b2191bc5d43d425f81192b980f27e;hp=ba191c2f2b7df572a8b41da4a967403f365cd791;hb=200a29bc95db63e4869b6236d38f9665f3a345a8;hpb=4d9fdd31adf3759f561888045759b8d7bf5234ed diff --git a/scripts/mqr-xmpp-client.pl b/scripts/mqr-xmpp-client.pl index ba191c2..0d98860 100755 --- a/scripts/mqr-xmpp-client.pl +++ b/scripts/mqr-xmpp-client.pl @@ -7,67 +7,119 @@ use AnyEvent::XMPP::Client; use AnyEvent::XMPP::Ext::Disco; use AnyEvent::XMPP::Ext::Version; use AnyEvent::XMPP::Namespaces qw/xmpp_ns/; -use AnyEvent::Redis; +use AnyEvent::XMPP::Ext::MUC; +use AnyEvent::XMPP::Util qw/node_jid res_jid/; 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"; my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD"; +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); my $disco = AnyEvent::XMPP::Ext::Disco->new; my $version = AnyEvent::XMPP::Ext::Version->new; +my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco); $cl->add_extension ($disco); $cl->add_extension ($version); +$cl->add_extension ($muc); $cl->set_presence(undef, 'I\'m a talking bot.', 1); $cl->add_account ($jid, $pw); warn "connecting to $jid...\n"; -my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } ); -my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } ); +warn "# jid:$jid root:$room subscribe:$subscribe\n"; our $contacts; +our $muc_rooms; + +sub publish; + +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 -$sub->psubscribe( 'channel *', 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' ); + foreach my $to ( keys %$contacts, keys %$muc_rooms ) { + next if $from =~ m/\Q$to\E/; # FIXME + my $type = defined $muc_rooms->{$from} ? 'groupchat' : 'chat'; + publish "SEND $jid $type $user $to" => $body; } +}) if $subscribe; + +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 ) = @_; + Encode::_utf8_off($body); + MQR::Redis->publish( $channel, $body ); +} + $cl->reg_cb ( session_ready => sub { my ($cl, $acc) = @_; warn "connected!\n"; + $muc->join_room ($acc->connection, $room, node_jid ($acc->jid)); + $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; + + 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 $ans = answer_to ($1); + my $repl = $msg->make_reply; + $repl->add_body ($ans); + $repl->send; + } + } + ); }, message => sub { my ($cl, $acc, $msg) = @_; + my $from = $msg->from; my $body = $msg->any_body; - my $f = $msg->from; - $f =~ s!/.+!!; - $contacts->{ $f }++; +# $contacts->{ $from }++; # don't push to anyone who sent message -warn "# contacts ",dump($contacts); + my $user = $from; + $user =~ s{\@.+$}{}; - my $channel = join(' ', 'channel', $msg->from); - Encode::_utf8_off($body); - $pub->publish( $channel, $body ); - warn "#X<< ",dump($channel, $body); + publish "MSG $jid chat $user $from" => $body; # my $repl = $msg->make_reply; # $repl->add_body( $response );