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/;
11 use Data::Dump qw(dump);
13 binmode STDOUT, ":utf8";
15 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
16 my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
18 my $j = AnyEvent->condvar;
19 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
20 my $disco = AnyEvent::XMPP::Ext::Disco->new;
21 my $version = AnyEvent::XMPP::Ext::Version->new;
23 $cl->add_extension ($disco);
24 $cl->add_extension ($version);
26 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
28 $cl->add_account ($jid, $pw);
29 warn "connecting to $jid...\n";
31 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
32 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT} );
36 $sub->psubscribe( 'channel *', sub {
37 my ( $message, $from ) = @_;
38 return unless $from !~ m/\Q$jid\E/; # skip our messages
39 warn "#Q<< $from [$jid] | $message\n";
40 my ( undef, $channel, $user ) = split(/ /,$from,3);
41 warn "# contacts ",dump($contacts);
42 foreach my $contact ( keys %$contacts ) {
43 next if $from =~ m/\Q$contact\E/;
44 warn "# $jid [$from] -> [$contact] | $message\n";
45 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
50 session_ready => sub {
55 my ($cl, $acc, $msg) = @_;
57 my $body = $msg->any_body;
63 warn "# contacts ",dump($contacts);
65 my $channel = join(' ', 'channel', $msg->from);
66 $pub->publish( $channel, $body );
67 warn "#X<< $channel | $body\n";
69 # my $repl = $msg->make_reply;
70 # $repl->add_body( $response );
72 # warn "#>>> $response\n";
75 contact_request_subscribe => sub {
76 my ($cl, $acc, $roster, $contact) = @_;
77 $contact->send_subscribed;
78 warn "Subscribed to ".$contact->jid."\n";
81 my ($cl, $acc, $error) = @_;
82 warn "Error encountered: ".$error->string."\n";
86 warn "Got disconnected: [@_]\n";
89 roster_update => sub {
90 my ($con,$account,$roster) = @_;
91 warn "XXXXX", ref($account), " | ", ref($roster);
92 foreach my $contact ( $roster->get_contacts ) {
93 $contacts->{ $contact->{jid} }++;
94 warn "# contacts ",dump($contacts);
96 warn "# contacts ",dump($contacts);