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);
15 binmode STDOUT, ":utf8";
17 my $jid = $ENV{XMPP_JID} || die "XMPP_JID";
18 my $pw = $ENV{XMPP_PASSWD} || die "XMPP_PASSWD";
20 my $j = AnyEvent->condvar;
21 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
22 my $disco = AnyEvent::XMPP::Ext::Disco->new;
23 my $version = AnyEvent::XMPP::Ext::Version->new;
25 $cl->add_extension ($disco);
26 $cl->add_extension ($version);
28 $cl->set_presence(undef, 'I\'m a talking bot.', 1);
30 $cl->add_account ($jid, $pw);
31 warn "connecting to $jid...\n";
33 my $sub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
37 $sub->psubscribe( 'channel *', sub {
38 my ( $message, $from ) = @_;
39 return unless $from !~ m/\Q$jid\E/; # skip our messages
40 Encode::_utf8_on($message);
41 warn "#Q<< ", dump( $from, $message );
42 my ( undef, $channel, $user ) = split(/ /,$from,3);
43 warn "# contacts ",dump($contacts);
44 foreach my $contact ( keys %$contacts ) {
45 next if $from =~ m/\Q$contact\E/;
46 warn "# $jid [$from] -> [$contact] | $message\n";
47 $cl->send_message( join(' ',$channel, $user, $message), $contact => $jid, 'chat' );
52 session_ready => sub {
57 my ($cl, $acc, $msg) = @_;
59 my $body = $msg->any_body;
65 warn "# contacts ",dump($contacts);
67 my $channel = join(' ', 'channel', $jid, $msg->from);
68 Encode::_utf8_off($body);
69 my $pub = AnyEvent::Redis->new( host => $ENV{REDIS_HOST}, port => $ENV{REDIS_PORT}, on_error => sub { confess @_ } );
70 $pub->publish( $channel, $body );
71 warn "#X<< ",dump($channel, $body);
73 # my $repl = $msg->make_reply;
74 # $repl->add_body( $response );
76 # warn "#>>> $response\n";
79 contact_request_subscribe => sub {
80 my ($cl, $acc, $roster, $contact) = @_;
81 $contact->send_subscribed;
82 warn "Subscribed to ".$contact->jid."\n";
85 my ($cl, $acc, $error) = @_;
86 warn "Error encountered: ".$error->string."\n";
90 warn "Got disconnected: [@_]\n";
93 roster_update => sub {
94 my ($con,$account,$roster) = @_;
95 warn "XXXXX", ref($account), " | ", ref($roster);
96 foreach my $contact ( $roster->get_contacts ) {
97 $contacts->{ $contact->{jid} }++;
98 warn "# contacts ",dump($contacts);
100 warn "# contacts ",dump($contacts);