display presence in web browser
[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::XMPP::Ext::MUC;
11 use AnyEvent::XMPP::Util qw/node_jid res_jid/;
12 use Data::Dump qw(dump);
13 use Encode;
14 use Carp qw(confess);
15
16 use lib 'lib';
17 use MQR::Redis;
18
19 binmode STDOUT, ":utf8";
20
21 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
22 my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
23 my $room = $ENV{XMPP_ROOM} || warn "no XMPP_ROOM - not joining any muc rooms!\n";
24 my $subscribe  = $ENV{XMPP_SUBSCRIBE} || warn "no XMPP_SUBSCRIBE - pushing ALL MSG\n";
25
26 my $j       = AnyEvent->condvar;
27 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
28 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
29 my $version = AnyEvent::XMPP::Ext::Version->new;
30 my $muc     = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
31
32 $cl->add_extension ($disco);
33 $cl->add_extension ($version);
34 $cl->add_extension ($muc);
35
36 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
37
38 $cl->add_account ($jid, $pw);
39 warn "connecting to $jid...\n";
40
41 warn "# jid:$jid root:$room subscribe:$subscribe\n";
42
43 our $contacts;
44 our $muc_rooms;
45
46 sub publish;
47
48 sub redis { MQR::Redis->redis }
49
50 redis->psubscribe( "MSG $subscribe", sub {
51         my ( $body, $channel ) = @_;
52         warn "<<<< ", dump( $channel, $body );
53         Encode::_utf8_on($body);
54
55         my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
56
57         return if $from eq $jid; # FIXME skip own
58
59         foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
60                 next if $from =~ m/\Q$to\E/; # FIXME
61                 my $type = defined $muc_rooms->{$to} ? 'groupchat' : 'chat';
62                 publish "SEND $jid $type $user $to" => $body;
63         }
64 }) if $subscribe;
65
66 redis->psubscribe( "SEND $jid *", sub {
67         my ( $body, $channel ) = @_;
68         warn "<<<< ",dump( $channel, $body );
69         Encode::_utf8_on($body);
70
71         my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
72         warn "SEND $jid $type $to <$user> $body\n";
73         $cl->send_message( "<$user> $body", $to => $jid, $type );
74 });
75
76 #redis->psubscribe( '*' => sub { warn @_, $/ };
77
78 sub publish {
79         my ( $channel, $body ) = @_;
80         Encode::_utf8_off($body);
81         MQR::Redis->publish( $channel, $body );
82 }
83
84 $cl->reg_cb (
85    session_ready => sub {
86       my ($cl, $acc) = @_;
87       warn "connected!\n";
88       $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
89       $muc->reg_cb (
90          message => sub {
91             my ($cl, $room, $msg, $is_echo) = @_;
92                 my $room_jid = $room->nick_jid;
93                 $muc_rooms->{ $room_jid }++;
94                 redis->sadd( "presence/rooms $jid" => $room_jid );
95 warn "# MUC message ",dump( $room_jid, $msg->any_body, $is_echo );
96
97             return if $is_echo;
98             return if $msg->is_delayed;
99
100                         my $from = $msg->from;
101                         my $user = $from;
102                         $user =~ s{^.+/}{};
103                         publish "MSG $room_jid groupchat $user $from" => $msg->any_body;
104
105             my $mynick = res_jid ($room->nick_jid);
106             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
107                my $ans = answer_to ($1);
108                my $repl = $msg->make_reply;
109                $repl->add_body ($ans);
110                $repl->send;
111             }
112          }
113       );
114    },
115    message => sub {
116                 my ($cl, $acc, $msg) = @_;
117
118                 my $from = $msg->from;
119                 my $body = $msg->any_body;
120
121 #               $contacts->{ $from }++; # don't push to anyone who sent message
122
123                 my $user = $from;
124                 $user =~ s{\@.+$}{};
125
126                 my $type = $msg->type;
127
128                 publish "MSG $jid $type $user $from" => $body;
129
130 #               my $repl = $msg->make_reply;
131 #               $repl->add_body( $response );
132 #               $repl->send;
133 #               warn "#>>> $response\n";
134
135    },
136    contact_request_subscribe => sub {
137       my ($cl, $acc, $roster, $contact) = @_;
138       $contact->send_subscribed;
139       warn "Subscribed to ".$contact->jid."\n";
140    },
141    error => sub {
142       my ($cl, $acc, $error) = @_;
143       warn "Error encountered: ".$error->string."\n";
144       $j->broadcast;
145    },
146    disconnect => sub {
147       warn "Got disconnected: [@_]\n";
148       $j->broadcast;
149    },
150         roster_update => sub {
151                 my ($con,$account,$roster) = @_;
152 warn "XXXXX", ref($account), " | ", ref($roster);
153                 foreach my $contact ( $roster->get_contacts ) {
154                         $contacts->{ $contact->jid }++;
155                         redis->sadd( "presence/contacts $jid" => $contact->jid );
156                         warn "# contacts ",dump($contacts);
157                 }
158                 warn "# contacts ",dump($contacts);
159         },
160 );
161
162 $cl->start;
163
164 $j->wait;