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} || die "XMPP_ROOM";
22 my $subscribe = $ENV{XMPP_SUBSCRIBE} || die 'XMPP_SUBSCRIBE';
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";
39 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
41 warn "# jid:$jid root:$room subscribe:$subscribe\n";
46 $sub->psubscribe( "MSG $subscribe", sub {
47 my ( $message, $from ) = @_;
48 return unless $from !~ m/\Q$jid\E/; # skip our messages
49 Encode::_utf8_on($message);
50 warn "#Q<< ", dump( $from, $message );
51 my ( undef, $gw, $user ) = split(/ /,$from,3);
52 foreach my $to ( keys %$contacts, keys %$muc_rooms ) {
53 warn "XXX send to $to\n";
54 next if $from =~ m/\Q$to\E/; # FIXME
55 warn "# $jid [$from] -> [$to] | $message\n";
58 my $body = "<$user$gw> $message"; # FIXME
59 $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
64 my ( $channel, $body ) = @_;
65 $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
66 Encode::_utf8_off($body);
67 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
68 $pub->publish( $channel, $body );
69 warn "#Q>> ",dump($channel, $body);
73 session_ready => sub {
76 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
79 my ($cl, $room, $msg, $is_echo) = @_;
80 $muc_rooms->{ $room->nick_jid }++;
81 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
84 return if $msg->is_delayed;
86 publish [ 'MSG', $jid, $msg->from ] => $msg->any_body;
88 my $mynick = res_jid ($room->nick_jid);
89 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
90 my $ans = answer_to ($1);
91 my $repl = $msg->make_reply;
92 $repl->add_body ($ans);
99 my ($cl, $acc, $msg) = @_;
101 my $body = $msg->any_body;
103 $contacts->{ $msg->from }++;
105 publish [ 'MSG', $jid, $msg->from ] => $body;
107 # my $repl = $msg->make_reply;
108 # $repl->add_body( $response );
110 # warn "#>>> $response\n";
113 contact_request_subscribe => sub {
114 my ($cl, $acc, $roster, $contact) = @_;
115 $contact->send_subscribed;
116 warn "Subscribed to ".$contact->jid."\n";
119 my ($cl, $acc, $error) = @_;
120 warn "Error encountered: ".$error->string."\n";
124 warn "Got disconnected: [@_]\n";
127 roster_update => sub {
128 my ($con,$account,$roster) = @_;
129 warn "XXXXX", ref($account), " | ", ref($roster);
130 foreach my $contact ( $roster->get_contacts ) {
131 $contacts->{ $contact->{jid} }++;
132 warn "# contacts ",dump($contacts);
134 warn "# contacts ",dump($contacts);