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