another refactor to use MSG and SEND all over
[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 MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
49         my ( $body, $channel ) = @_;
50         warn "<<<< ", dump( $channel, $body );
51         Encode::_utf8_on($body);
52
53         my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
54
55         return if $from eq $jid; # FIXME skip own
56
57         foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
58                 next if $from =~ m/\Q$to\E/; # FIXME
59                 my $type = defined $muc_rooms->{$from} ? 'groupchat' : 'chat';
60                 publish "SEND $jid $type $user $to" => $body;
61         }
62 }) if $subscribe;
63
64 MQR::Redis->redis->psubscribe( "SEND $jid *", sub {
65         my ( $body, $channel ) = @_;
66         warn "<<<< ",dump( $channel, $body );
67         Encode::_utf8_on($body);
68
69         my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
70         warn "# send ", dump( $jid, $type, $to, $user, $body );
71         $cl->send_message( "<$user> $body", $to => $jid, $type );
72 });
73
74 #redis->psubscribe( '*' => sub { warn @_, $/ };
75
76 sub publish {
77         my ( $channel, $body ) = @_;
78         Encode::_utf8_off($body);
79         MQR::Redis->publish( $channel, $body );
80 }
81
82 $cl->reg_cb (
83    session_ready => sub {
84       my ($cl, $acc) = @_;
85       warn "connected!\n";
86       $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
87       $muc->reg_cb (
88          message => sub {
89             my ($cl, $room, $msg, $is_echo) = @_;
90                 $muc_rooms->{ $room->nick_jid }++;
91 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
92
93             return if $is_echo;
94             return if $msg->is_delayed;
95
96                         my $from = $msg->from;
97                         my $user = $from;
98                         $user =~ s{^.+/}{};
99                         publish "MSG $jid groupchat $user $from" => $msg->any_body;
100
101             my $mynick = res_jid ($room->nick_jid);
102             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
103                my $ans = answer_to ($1);
104                my $repl = $msg->make_reply;
105                $repl->add_body ($ans);
106                $repl->send;
107             }
108          }
109       );
110    },
111    message => sub {
112                 my ($cl, $acc, $msg) = @_;
113
114                 my $from = $msg->from;
115                 my $body = $msg->any_body;
116
117 #               $contacts->{ $from }++; # don't push to anyone who sent message
118
119                 my $user = $from;
120                 $user =~ s{\@.+$}{};
121
122                 publish "MSG $jid chat $user $from" => $body;
123
124 #               my $repl = $msg->make_reply;
125 #               $repl->add_body( $response );
126 #               $repl->send;
127 #               warn "#>>> $response\n";
128
129    },
130    contact_request_subscribe => sub {
131       my ($cl, $acc, $roster, $contact) = @_;
132       $contact->send_subscribed;
133       warn "Subscribed to ".$contact->jid."\n";
134    },
135    error => sub {
136       my ($cl, $acc, $error) = @_;
137       warn "Error encountered: ".$error->string."\n";
138       $j->broadcast;
139    },
140    disconnect => sub {
141       warn "Got disconnected: [@_]\n";
142       $j->broadcast;
143    },
144         roster_update => sub {
145                 my ($con,$account,$roster) = @_;
146 warn "XXXXX", ref($account), " | ", ref($roster);
147                 foreach my $contact ( $roster->get_contacts ) {
148                         $contacts->{ $contact->{jid} }++;
149                         warn "# contacts ",dump($contacts);
150                 }
151                 warn "# contacts ",dump($contacts);
152         },
153 );
154
155 $cl->start;
156
157 $j->wait;