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/;
13 use Data::Dump qw(dump);
17 binmode STDOUT, ":utf8";
19 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
20 my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
21 my $room = $ENV{XMPP_ROOM} || warn "no XMPP_ROOM - not joining any muc rooms!\n";
22 my $subscribe = $ENV{XMPP_SUBSCRIBE} || warn "no XMPP_SUBSCRIBE - pushing ALL MSG\n";
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);
30 $cl->add_extension ($disco);
31 $cl->add_extension ($version);
32 $cl->add_extension ($muc);
34 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
36 $cl->add_account ($jid, $pw);
37 warn "connecting to $jid...\n";
40 AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
43 warn "# jid:$jid root:$room subscribe:$subscribe\n";
50 redis->psubscribe( "MSG $subscribe", sub {
51 my ( $message, $from ) = @_;
52 return unless $from !~ m/\Q$jid\E/; # skip our messages
53 Encode::_utf8_on($message);
54 warn "<<<< ", dump( $from, $message );
55 my ( undef, $gw, $user, $type ) = split(/ /,$from,3);
56 foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
57 warn "XXX send to $to\n";
58 next if $from =~ m/\Q$to\E/; # FIXME
60 my $body = "<$user> $message"; # FIXME
61 publish [ 'SEND', $jid, $to, $type ] => $body;
65 redis->psubscribe( "SEND $jid *", sub {
66 my ( $body, $from ) = @_;
67 my ( undef, undef, $to, $type ) = split(/\s/, $from);
68 warn "SEND $jid -> $to $type | $body\n";
69 $cl->send_message( $body, $to => $jid, $type );
72 #redis->psubscribe( '*' => sub { warn @_, $/ };
75 my ( $channel, $body ) = @_;
76 $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
77 Encode::_utf8_off($body);
78 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
79 $pub->publish( $channel, $body );
80 warn ">>>> ",dump($channel, $body);
84 session_ready => sub {
87 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
90 my ($cl, $room, $msg, $is_echo) = @_;
91 $muc_rooms->{ $room->nick_jid }++;
92 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
95 return if $msg->is_delayed;
97 publish [ 'MSG', $jid, $msg->from, 'groupchat' ] => $msg->any_body;
99 my $mynick = res_jid ($room->nick_jid);
100 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
101 my $ans = answer_to ($1);
102 my $repl = $msg->make_reply;
103 $repl->add_body ($ans);
110 my ($cl, $acc, $msg) = @_;
113 my $body = $msg->any_body;
115 $contacts->{ $to }++;
117 publish [ 'MSG', $jid, $to, 'chat' ] => $body;
118 publish [ 'SEND', $jid, $to, 'chat' ] => "ECHO: $body";
120 # my $repl = $msg->make_reply;
121 # $repl->add_body( $response );
123 # warn "#>>> $response\n";
126 contact_request_subscribe => sub {
127 my ($cl, $acc, $roster, $contact) = @_;
128 $contact->send_subscribed;
129 warn "Subscribed to ".$contact->jid."\n";
132 my ($cl, $acc, $error) = @_;
133 warn "Error encountered: ".$error->string."\n";
137 warn "Got disconnected: [@_]\n";
140 roster_update => sub {
141 my ($con,$account,$roster) = @_;
142 warn "XXXXX", ref($account), " | ", ref($roster);
143 foreach my $contact ( $roster->get_contacts ) {
144 $contacts->{ $contact->{jid} }++;
145 warn "# contacts ",dump($contacts);
147 warn "# contacts ",dump($contacts);