fix <user> prefix before messages
[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} || die "XMPP_ROOM";
22 my $subscribe  = $ENV{XMPP_SUBSCRIBE} || die 'XMPP_SUBSCRIBE';
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 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
40
41 warn "# jid:$jid root:$room subscribe:$subscribe\n";
42
43 our $contacts;
44 our $muc_rooms;
45
46 $sub->psubscribe( "MSG $subscribe", sub {
47         my ( $message, $from ) = @_;
48         return unless $from !~ m/\Q$jid\E/; # skip our messages
49         Encode::_utf8_on($message);
50         warn "#Q<< ", dump( $from, $message );
51         my ( undef, $gw, $user ) = split(/ /,$from,3);
52         foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
53 warn "XXX send to $to\n";
54                 next if $from =~ m/\Q$to\E/; # FIXME
55                 warn "# $jid [$from] -> [$to] | $message\n";
56                 $user =~ s{!.+}{};
57                 $gw   =~ s{^.+\/#}{#};
58                 my $body = "<$user$gw> $message"; # FIXME
59                 $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
60         }
61 });
62
63 sub publish {
64         my ( $channel, $body ) = @_;
65         $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
66         Encode::_utf8_off($body);
67         my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
68         $pub->publish( $channel, $body );
69         warn "#Q>> ",dump($channel, $body);
70 }
71
72 $cl->reg_cb (
73    session_ready => sub {
74       my ($cl, $acc) = @_;
75       warn "connected!\n";
76       $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
77       $muc->reg_cb (
78          message => sub {
79             my ($cl, $room, $msg, $is_echo) = @_;
80                 $muc_rooms->{ $room->nick_jid }++;
81 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
82
83             return if $is_echo;
84             return if $msg->is_delayed;
85
86                 publish [ 'MSG', $jid, $msg->from ] => $msg->any_body;
87
88             my $mynick = res_jid ($room->nick_jid);
89             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
90                my $ans = answer_to ($1);
91                my $repl = $msg->make_reply;
92                $repl->add_body ($ans);
93                $repl->send;
94             }
95          }
96       );
97    },
98    message => sub {
99                 my ($cl, $acc, $msg) = @_;
100
101                 my $body = $msg->any_body;
102
103                 $contacts->{ $msg->from }++;
104
105                 publish [ 'MSG', $jid, $msg->from ] => $body;
106
107 #               my $repl = $msg->make_reply;
108 #               $repl->add_body( $response );
109 #               $repl->send;
110 #               warn "#>>> $response\n";
111
112    },
113    contact_request_subscribe => sub {
114       my ($cl, $acc, $roster, $contact) = @_;
115       $contact->send_subscribed;
116       warn "Subscribed to ".$contact->jid."\n";
117    },
118    error => sub {
119       my ($cl, $acc, $error) = @_;
120       warn "Error encountered: ".$error->string."\n";
121       $j->broadcast;
122    },
123    disconnect => sub {
124       warn "Got disconnected: [@_]\n";
125       $j->broadcast;
126    },
127         roster_update => sub {
128                 my ($con,$account,$roster) = @_;
129 warn "XXXXX", ref($account), " | ", ref($roster);
130                 foreach my $contact ( $roster->get_contacts ) {
131                         $contacts->{ $contact->{jid} }++;
132                         warn "# contacts ",dump($contacts);
133                 }
134                 warn "# contacts ",dump($contacts);
135         },
136 );
137
138 $cl->start;
139
140 $j->wait;