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