X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=scripts%2Fmqr-xmpp-client.pl;h=d0a7e67a88b1247cddef074da6e00ab547ee505e;hb=6c1ac869a4ae4b42f30281f56156176a621db926;hp=06d4fda6d5a3fc740230c140b3d82f82e294cda3;hpb=aa2937eb15fc29fcbe6d5df0dfe8e443fad40c3a;p=MQR.git diff --git a/scripts/mqr-xmpp-client.pl b/scripts/mqr-xmpp-client.pl index 06d4fda..d0a7e67 100755 --- a/scripts/mqr-xmpp-client.pl +++ b/scripts/mqr-xmpp-client.pl @@ -8,15 +8,12 @@ use AnyEvent::XMPP::Ext::Disco; use AnyEvent::XMPP::Ext::Version; use AnyEvent::XMPP::Namespaces qw/xmpp_ns/; use AnyEvent::Redis; -use Storable; use Data::Dump qw(dump); binmode STDOUT, ":utf8"; -our ($jid, $pw, $inputfile, $redis_host, $redis_port); -require 'config.pl'; - -warn "# $jid <- $inputfile\n"; +my $jid = $ENV{XMPP_JID} || die "XMPP_JID"; +my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD"; my $j = AnyEvent->condvar; my $cl = AnyEvent::XMPP::Client->new (debug => 1); @@ -26,38 +23,28 @@ my $version = AnyEvent::XMPP::Ext::Version->new; $cl->add_extension ($disco); $cl->add_extension ($version); -$cl->set_presence (undef, 'I\'m a talking bot.', 1); +$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 => $redis_host, port => $redis_port ); -my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port ); - -my $subscriptions = eval { retrieve '/tmp/subs' }; - -sub subscribe_channel { - my ( $who, $to ) = @_; - warn "# subscribe_channel $who $to\n"; - - $subscriptions->{$who}->{$to} ||= time; - - $sub->psubscribe( "channel $to", sub { - my ( $message, $from ) = @_; - return unless $from !~ m/\Q($to|$jid)\E/; # skip our messages - warn "#Q<< $from [$jid] | $message\n"; - my ( undef, $channel, $user ) = split(/ /,$from,3); - $cl->send_message( "$channel <$user> $message", $who => $jid, 'chat' ); - }); - - store $subscriptions, '/tmp/subs'; -} - -foreach my $who ( keys %$subscriptions ) { - foreach my $to ( keys %{ $subscriptions->{$who} } ) { - subscribe_channel $who => $to; +my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} ); +my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} ); + +our $contacts; + +$sub->psubscribe( 'channel *', sub { + my ( $message, $from ) = @_; + return unless $from !~ m/\Q$jid\E/; # skip our messages + warn "#Q<< $from [$jid] | $message\n"; + 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' ); } -} +}); $cl->reg_cb ( session_ready => sub { @@ -67,24 +54,22 @@ $cl->reg_cb ( message => sub { my ($cl, $acc, $msg) = @_; - my $response = '...'; - my $body = $msg->any_body; + my $f = $msg->from; + $f =~ s!/.+!!; + $contacts->{ $f }++; + +warn "# contacts ",dump($contacts); + my $channel = join(' ', 'channel', $msg->from); $pub->publish( $channel, $body ); warn "#X<< $channel | $body\n"; - if ( $msg =~ m/!subscribe\s+(\S+)/ ) { - subscribe_channel $msg->from => $1; - $response = "subscribed to: " . dump( $subscriptions->{$msg->from} ); - - } - - my $repl = $msg->make_reply; - $repl->add_body( $response ); - $repl->send; - warn "#>>> $response\n"; +# my $repl = $msg->make_reply; +# $repl->add_body( $response ); +# $repl->send; +# warn "#>>> $response\n"; }, contact_request_subscribe => sub { @@ -101,6 +86,15 @@ $cl->reg_cb ( warn "Got disconnected: [@_]\n"; $j->broadcast; }, + roster_update => sub { + my ($con,$account,$roster) = @_; +warn "XXXXX", ref($account), " | ", ref($roster); + foreach my $contact ( $roster->get_contacts ) { + $contacts->{ $contact->{jid} }++; + warn "# contacts ",dump($contacts); + } + warn "# contacts ",dump($contacts); + }, ); $cl->start;