e41b87014bf34a49e04d07c52deec09993ac6fd0
[MQR.git] / scripts / mqr-xmpp-client.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use utf8;
5 use AnyEvent;
6 use AnyEvent::XMPP::Client;
7 use AnyEvent::XMPP::Ext::Disco;
8 use AnyEvent::XMPP::Ext::Version;
9 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
10 use AnyEvent::Redis;
11 use Data::Dump qw(dump);
12 use Encode;
13 use Carp qw(confess);
14
15 binmode STDOUT, ":utf8";
16
17 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
18 my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
19
20 my $j       = AnyEvent->condvar;
21 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
22 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
23 my $version = AnyEvent::XMPP::Ext::Version->new;
24
25 $cl->add_extension ($disco);
26 $cl->add_extension ($version);
27
28 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
29
30 $cl->add_account ($jid, $pw);
31 warn "connecting to $jid...\n";
32
33 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
34
35 our $contacts;
36
37 $sub->psubscribe( 'channel *', sub {
38         my ( $message, $from ) = @_;
39         return unless $from !~ m/\Q$jid\E/; # skip our messages
40         Encode::_utf8_on($message);
41         warn "#Q<< ", dump( $from, $message );
42         my ( undef, $channel, $user ) = split(/ /,$from,3);
43 warn "# contacts ",dump($contacts);
44         foreach my $contact ( keys %$contacts ) {
45                 next if $from =~ m/\Q$contact\E/;
46                 warn "# $jid [$from] -> [$contact] | $message\n";
47                 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
48         }
49 });
50
51 $cl->reg_cb (
52    session_ready => sub {
53       my ($cl, $acc) = @_;
54       warn "connected!\n";
55    },
56    message => sub {
57                 my ($cl, $acc, $msg) = @_;
58
59                 my $body = $msg->any_body;
60
61                 my $f = $msg->from;
62                 $f =~ s!/.+!!;
63                 $contacts->{ $f }++;
64
65 warn "# contacts ",dump($contacts);
66
67                 my $channel = join(' ', 'channel', $jid, $msg->from);
68                 Encode::_utf8_off($body);
69                 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
70                 $pub->publish( $channel, $body );
71                 warn "#X<< ",dump($channel, $body);
72
73 #               my $repl = $msg->make_reply;
74 #               $repl->add_body( $response );
75 #               $repl->send;
76 #               warn "#>>> $response\n";
77
78    },
79    contact_request_subscribe => sub {
80       my ($cl, $acc, $roster, $contact) = @_;
81       $contact->send_subscribed;
82       warn "Subscribed to ".$contact->jid."\n";
83    },
84    error => sub {
85       my ($cl, $acc, $error) = @_;
86       warn "Error encountered: ".$error->string."\n";
87       $j->broadcast;
88    },
89    disconnect => sub {
90       warn "Got disconnected: [@_]\n";
91       $j->broadcast;
92    },
93         roster_update => sub {
94                 my ($con,$account,$roster) = @_;
95 warn "XXXXX", ref($account), " | ", ref($roster);
96                 foreach my $contact ( $roster->get_contacts ) {
97                         $contacts->{ $contact->{jid} }++;
98                         warn "# contacts ",dump($contacts);
99                 }
100                 warn "# contacts ",dump($contacts);
101         },
102 );
103
104 $cl->start;
105
106 $j->wait;