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 MQR::Redis->redis->psubscribe( "MSG $subscribe", sub {
49 my ( $body, $channel ) = @_;
50 warn "<<<< ", dump( $channel, $body );
51 Encode::_utf8_on($body);
53 my ( undef, $gw, $room, $user, $from ) = split(/\s/,$channel);
55 return if $from eq $jid; # FIXME skip own
57 foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
58 next if $from =~ m/\Q$to\E/; # FIXME
59 my $type = defined $muc_rooms->{$from} ? 'groupchat' : 'chat';
60 publish "SEND $jid $type $user $to" => $body;
64 MQR::Redis->redis->psubscribe( "SEND $jid *", sub {
65 my ( $body, $channel ) = @_;
66 warn "<<<< ",dump( $channel, $body );
67 Encode::_utf8_on($body);
69 my ( undef, $gw, $type, $user, $to ) = split(/\s/, $channel);
70 warn "SEND $jid $type $to <$user> $body\n";
71 $cl->send_message( "<$user> $body", $to => $jid, $type );
74 #redis->psubscribe( '*' => sub { warn @_, $/ };
77 my ( $channel, $body ) = @_;
78 Encode::_utf8_off($body);
79 MQR::Redis->publish( $channel, $body );
83 session_ready => sub {
86 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
89 my ($cl, $room, $msg, $is_echo) = @_;
90 $muc_rooms->{ $room->nick_jid }++;
91 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
94 return if $msg->is_delayed;
96 my $from = $msg->from;
99 publish "MSG $jid groupchat $user $from" => $msg->any_body;
101 my $mynick = res_jid ($room->nick_jid);
102 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
103 my $ans = answer_to ($1);
104 my $repl = $msg->make_reply;
105 $repl->add_body ($ans);
112 my ($cl, $acc, $msg) = @_;
114 my $from = $msg->from;
115 my $body = $msg->any_body;
117 # $contacts->{ $from }++; # don't push to anyone who sent message
122 publish "MSG $jid chat $user $from" => $body;
124 # my $repl = $msg->make_reply;
125 # $repl->add_body( $response );
127 # warn "#>>> $response\n";
130 contact_request_subscribe => sub {
131 my ($cl, $acc, $roster, $contact) = @_;
132 $contact->send_subscribed;
133 warn "Subscribed to ".$contact->jid."\n";
136 my ($cl, $acc, $error) = @_;
137 warn "Error encountered: ".$error->string."\n";
141 warn "Got disconnected: [@_]\n";
144 roster_update => sub {
145 my ($con,$account,$roster) = @_;
146 warn "XXXXX", ref($account), " | ", ref($roster);
147 foreach my $contact ( $roster->get_contacts ) {
148 $contacts->{ $contact->{jid} }++;
149 warn "# contacts ",dump($contacts);
151 warn "# contacts ",dump($contacts);