turn utf8 off before pushing to redis
[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::Redis;
11 use Data::Dump qw(dump);
12 use Encode;
13
14 binmode STDOUT, ":utf8";
15
16 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
17 my $pw  = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
18
19 my $j       = AnyEvent->condvar;
20 my $cl      = AnyEvent::XMPP::Client->new (debug => 1);
21 my $disco   = AnyEvent::XMPP::Ext::Disco->new;
22 my $version = AnyEvent::XMPP::Ext::Version->new;
23
24 $cl->add_extension ($disco);
25 $cl->add_extension ($version);
26
27 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
28
29 $cl->add_account ($jid, $pw);
30 warn "connecting to $jid...\n";
31
32 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } );
33 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { warn @_ } );
34
35 our $contacts;
36
37 $sub->psubscribe( 'channel *', sub {
38         my ( $message, $from ) = @_;
39         return unless $from !~ m/\Q$jid\E/; # skip our messages
40         Encode::_utf8_on($message);
41         warn "#Q<< ", dump( $from, $message );
42         my ( undef, $channel, $user ) = split(/ /,$from,3);
43 warn "# contacts ",dump($contacts);
44         foreach my $contact ( keys %$contacts ) {
45                 next if $from =~ m/\Q$contact\E/;
46                 warn "# $jid [$from] -> [$contact] | $message\n";
47                 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
48         }
49 });
50
51 $cl->reg_cb (
52    session_ready => sub {
53       my ($cl, $acc) = @_;
54       warn "connected!\n";
55    },
56    message => sub {
57                 my ($cl, $acc, $msg) = @_;
58
59                 my $body = $msg->any_body;
60
61                 my $f = $msg->from;
62                 $f =~ s!/.+!!;
63                 $contacts->{ $f }++;
64
65 warn "# contacts ",dump($contacts);
66
67                 my $channel = join(' ', 'channel', $msg->from);
68                 Encode::_utf8_off($body);
69                 $pub->publish( $channel, $body );
70                 warn "#X<< ",dump($channel, $body);
71
72 #               my $repl = $msg->make_reply;
73 #               $repl->add_body( $response );
74 #               $repl->send;
75 #               warn "#>>> $response\n";
76
77    },
78    contact_request_subscribe => sub {
79       my ($cl, $acc, $roster, $contact) = @_;
80       $contact->send_subscribed;
81       warn "Subscribed to ".$contact->jid."\n";
82    },
83    error => sub {
84       my ($cl, $acc, $error) = @_;
85       warn "Error encountered: ".$error->string."\n";
86       $j->broadcast;
87    },
88    disconnect => sub {
89       warn "Got disconnected: [@_]\n";
90       $j->broadcast;
91    },
92         roster_update => sub {
93                 my ($con,$account,$roster) = @_;
94 warn "XXXXX", ref($account), " | ", ref($roster);
95                 foreach my $contact ( $roster->get_contacts ) {
96                         $contacts->{ $contact->{jid} }++;
97                         warn "# contacts ",dump($contacts);
98                 }
99                 warn "# contacts ",dump($contacts);
100         },
101 );
102
103 $cl->start;
104
105 $j->wait;