use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
+use AnyEvent::XMPP::Ext::MUC;
+use AnyEvent::XMPP::Util qw/node_jid res_jid/;
use AnyEvent::Redis;
use Data::Dump qw(dump);
use Encode;
binmode STDOUT, ":utf8";
-my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
-my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
+my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
+my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
+my $room = $ENV{XMPP_ROOM} || die "XMPP_ROOM";
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new (debug => 1);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $version = AnyEvent::XMPP::Ext::Version->new;
+my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
$cl->add_extension ($disco);
$cl->add_extension ($version);
+$cl->add_extension ($muc);
$cl->set_presence(undef, 'I\'m a talking bot.', 1);
my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
-our $contacts;
+our $contacts = { $room => 1 };
$sub->psubscribe( 'channel *', sub {
my ( $message, $from ) = @_;
session_ready => sub {
my ($cl, $acc) = @_;
warn "connected!\n";
+ $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
+ $muc->reg_cb (
+ message => sub {
+ my ($cl, $room, $msg, $is_echo) = @_;
+
+ return if $is_echo;
+ return if $msg->is_delayed;
+ my $mynick = res_jid ($room->nick_jid);
+ if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
+ my $ans = answer_to ($1);
+ my $repl = $msg->make_reply;
+ $repl->add_body ($ans);
+ $repl->send;
+ }
+ }
+ );
},
message => sub {
my ($cl, $acc, $msg) = @_;