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";
23 my $j = AnyEvent->condvar;
24 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
25 my $disco = AnyEvent::XMPP::Ext::Disco->new;
26 my $version = AnyEvent::XMPP::Ext::Version->new;
27 my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
29 $cl->add_extension ($disco);
30 $cl->add_extension ($version);
31 $cl->add_extension ($muc);
33 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
35 $cl->add_account ($jid, $pw);
36 warn "connecting to $jid...\n";
38 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
40 our $contacts = { $room => 1 };
42 $sub->psubscribe( 'channel *', sub {
43 my ( $message, $from ) = @_;
44 return unless $from !~ m/\Q$jid\E/; # skip our messages
45 Encode::_utf8_on($message);
46 warn "#Q<< ", dump( $from, $message );
47 my ( undef, $channel, $user ) = split(/ /,$from,3);
48 warn "# contacts ",dump($contacts);
49 foreach my $contact ( keys %$contacts ) {
50 next if $from =~ m/\Q$contact\E/;
51 warn "# $jid [$from] -> [$contact] | $message\n";
52 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
57 session_ready => sub {
60 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
63 my ($cl, $room, $msg, $is_echo) = @_;
66 return if $msg->is_delayed;
67 my $mynick = res_jid ($room->nick_jid);
68 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
69 my $ans = answer_to ($1);
70 my $repl = $msg->make_reply;
71 $repl->add_body ($ans);
78 my ($cl, $acc, $msg) = @_;
80 my $body = $msg->any_body;
86 warn "# contacts ",dump($contacts);
88 my $channel = join(' ', 'channel', $jid, $msg->from);
89 Encode::_utf8_off($body);
90 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
91 $pub->publish( $channel, $body );
92 warn "#X<< ",dump($channel, $body);
94 # my $repl = $msg->make_reply;
95 # $repl->add_body( $response );
97 # warn "#>>> $response\n";
100 contact_request_subscribe => sub {
101 my ($cl, $acc, $roster, $contact) = @_;
102 $contact->send_subscribed;
103 warn "Subscribed to ".$contact->jid."\n";
106 my ($cl, $acc, $error) = @_;
107 warn "Error encountered: ".$error->string."\n";
111 warn "Got disconnected: [@_]\n";
114 roster_update => sub {
115 my ($con,$account,$roster) = @_;
116 warn "XXXXX", ref($account), " | ", ref($roster);
117 foreach my $contact ( $roster->get_contacts ) {
118 $contacts->{ $contact->{jid} }++;
119 warn "# contacts ",dump($contacts);
121 warn "# contacts ",dump($contacts);