join into muc
[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
23 my $j       = AnyEvent->condvar;
24 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
25 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
26 my $version = AnyEvent::XMPP::Ext::Version->new;
27 my $muc     = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
28
29 $cl->add_extension ($disco);
30 $cl->add_extension ($version);
31 $cl->add_extension ($muc);
32
33 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
34
35 $cl->add_account ($jid, $pw);
36 warn "connecting to $jid...\n";
37
38 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
39
40 our $contacts = { $room => 1 };
41
42 $sub->psubscribe( 'channel *', sub {
43         my ( $message, $from ) = @_;
44         return unless $from !~ m/\Q$jid\E/; # skip our messages
45         Encode::_utf8_on($message);
46         warn "#Q<< ", dump( $from, $message );
47         my ( undef, $channel, $user ) = split(/ /,$from,3);
48 warn "# contacts ",dump($contacts);
49         foreach my $contact ( keys %$contacts ) {
50                 next if $from =~ m/\Q$contact\E/;
51                 warn "# $jid [$from] -> [$contact] | $message\n";
52                 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
53         }
54 });
55
56 $cl->reg_cb (
57    session_ready => sub {
58       my ($cl, $acc) = @_;
59       warn "connected!\n";
60       $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
61       $muc->reg_cb (
62          message => sub {
63             my ($cl, $room, $msg, $is_echo) = @_;
64
65             return if $is_echo;
66             return if $msg->is_delayed;
67             my $mynick = res_jid ($room->nick_jid);
68             if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
69                my $ans = answer_to ($1);
70                my $repl = $msg->make_reply;
71                $repl->add_body ($ans);
72                $repl->send;
73             }
74          }
75       );
76    },
77    message => sub {
78                 my ($cl, $acc, $msg) = @_;
79
80                 my $body = $msg->any_body;
81
82                 my $f = $msg->from;
83                 $f =~ s!/.+!!;
84                 $contacts->{ $f }++;
85
86 warn "# contacts ",dump($contacts);
87
88                 my $channel = join(' ', 'channel', $jid, $msg->from);
89                 Encode::_utf8_off($body);
90                 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
91                 $pub->publish( $channel, $body );
92                 warn "#X<< ",dump($channel, $body);
93
94 #               my $repl = $msg->make_reply;
95 #               $repl->add_body( $response );
96 #               $repl->send;
97 #               warn "#>>> $response\n";
98
99    },
100    contact_request_subscribe => sub {
101       my ($cl, $acc, $roster, $contact) = @_;
102       $contact->send_subscribed;
103       warn "Subscribed to ".$contact->jid."\n";
104    },
105    error => sub {
106       my ($cl, $acc, $error) = @_;
107       warn "Error encountered: ".$error->string."\n";
108       $j->broadcast;
109    },
110    disconnect => sub {
111       warn "Got disconnected: [@_]\n";
112       $j->broadcast;
113    },
114         roster_update => sub {
115                 my ($con,$account,$roster) = @_;
116 warn "XXXXX", ref($account), " | ", ref($roster);
117                 foreach my $contact ( $roster->get_contacts ) {
118                         $contacts->{ $contact->{jid} }++;
119                         warn "# contacts ",dump($contacts);
120                 }
121                 warn "# contacts ",dump($contacts);
122         },
123 );
124
125 $cl->start;
126
127 $j->wait;