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";
57 my $body = "<$user> $message"; # FIXME
58 $cl->send_message( $body, $to => $jid, defined $muc_rooms->{$to} ? 'groupchat' : 'chat' );
63 my ( $channel, $body ) = @_;
64 $channel = join(' ', @$channel) if ref $channel eq 'ARRAY';
65 Encode::_utf8_off($body);
66 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
67 $pub->publish( $channel, $body );
68 warn "#Q>> ",dump($channel, $body);
72 session_ready => sub {
75 $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
78 my ($cl, $room, $msg, $is_echo) = @_;
79 $muc_rooms->{ $room->nick_jid }++;
80 warn "# MUC message ",dump( $room->nick_jid, $msg->any_body, $is_echo );
83 return if $msg->is_delayed;
85 publish [ 'MSG', $jid, $msg->from ] => $msg->any_body;
87 my $mynick = res_jid ($room->nick_jid);
88 if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
89 my $ans = answer_to ($1);
90 my $repl = $msg->make_reply;
91 $repl->add_body ($ans);
98 my ($cl, $acc, $msg) = @_;
100 my $body = $msg->any_body;
102 $contacts->{ $msg->from }++;
104 publish [ 'MSG', $jid, $msg->from ] => $body;
106 # my $repl = $msg->make_reply;
107 # $repl->add_body( $response );
109 # warn "#>>> $response\n";
112 contact_request_subscribe => sub {
113 my ($cl, $acc, $roster, $contact) = @_;
114 $contact->send_subscribed;
115 warn "Subscribed to ".$contact->jid."\n";
118 my ($cl, $acc, $error) = @_;
119 warn "Error encountered: ".$error->string."\n";
123 warn "Got disconnected: [@_]\n";
126 roster_update => sub {
127 my ($con,$account,$roster) = @_;
128 warn "XXXXX", ref($account), " | ", ref($roster);
129 foreach my $contact ( $roster->get_contacts ) {
130 $contacts->{ $contact->{jid} }++;
131 warn "# contacts ",dump($contacts);
133 warn "# contacts ",dump($contacts);