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);
19 binmode STDOUT, ":utf8";
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";
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);
32 $cl->add_extension ($disco);
33 $cl->add_extension ($version);
34 $cl->add_extension ($muc);
36 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
38 $cl->add_account ($jid, $pw);
39 warn "connecting to $jid...\n";
41 warn "# jid:$jid root:$room subscribe:$subscribe\n";
48 sub redis { MQR::Redis->redis }
50 redis->psubscribe( "MSG $subscribe", sub {
51 my ( $body, $channel ) = @_;
52 warn "<<<< ", dump( $channel, $body );
53 Encode::_utf8_on($body);
55 my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
57 return if $from eq $jid; # FIXME skip own
59 foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
60 next if $from =~ m/\Q$to\E/; # FIXME
61 my $type = defined $muc_rooms->{$to} ? 'groupchat' : 'chat';
62 publish "SEND $jid $type $user $to" => $body;
66 redis->psubscribe( "SEND $jid *", sub {
67 my ( $body, $channel ) = @_;
68 warn "<<<< ",dump( $channel, $body );
69 Encode::_utf8_on($body);
71 my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
72 warn "SEND $jid $type $to <$user> $body\n";
73 $cl->send_message( "<$user> $body", $to => $jid, $type );
76 #redis->psubscribe( '*' => sub { warn @_, $/ };
79 my ( $channel, $body ) = @_;
80 Encode::_utf8_off($body);
81 MQR::Redis->publish( $channel, $body );
85 session_ready => sub {
88 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
91 my ($cl, $room, $msg, $is_echo) = @_;
92 my $room_jid = $room->nick_jid;
93 $muc_rooms->{ $room_jid }++;
94 redis->sadd( "presence/rooms $jid" => $room_jid );
95 warn "# MUC message ",dump( $room_jid, $msg->any_body, $is_echo );
98 return if $msg->is_delayed;
100 my $from = $msg->from;
103 publish "MSG $room_jid groupchat $user $from" => $msg->any_body;
105 my $mynick = res_jid ($room->nick_jid);
106 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
107 my $ans = answer_to ($1);
108 my $repl = $msg->make_reply;
109 $repl->add_body ($ans);
116 my ($cl, $acc, $msg) = @_;
118 my $from = $msg->from;
119 my $body = $msg->any_body;
121 # $contacts->{ $from }++; # don't push to anyone who sent message
126 my $type = $msg->type;
128 publish "MSG $jid $type $user $from" => $body;
130 # my $repl = $msg->make_reply;
131 # $repl->add_body( $response );
133 # warn "#>>> $response\n";
136 contact_request_subscribe => sub {
137 my ($cl, $acc, $roster, $contact) = @_;
138 $contact->send_subscribed;
139 warn "Subscribed to ".$contact->jid."\n";
142 my ($cl, $acc, $error) = @_;
143 warn "Error encountered: ".$error->string."\n";
147 warn "Got disconnected: [@_]\n";
150 roster_update => sub {
151 my ($con,$account,$roster) = @_;
152 warn "XXXXX", ref($account), " | ", ref($roster);
153 foreach my $contact ( $roster->get_contacts ) {
154 $contacts->{ $contact->jid }++;
155 redis->sadd( "presence/contacts $jid" => $contact->jid );
156 warn "# contacts ",dump($contacts);
158 warn "# contacts ",dump($contacts);