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/;
12 binmode STDOUT, ":utf8";
14 our ($jid, $pw, $inputfile, $redis_host, $redis_port);
17 warn "# $jid <- $inputfile\n";
19 my $j = AnyEvent->condvar;
20 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
21 my $disco = AnyEvent::XMPP::Ext::Disco->new;
22 my $version = AnyEvent::XMPP::Ext::Version->new;
24 $cl->add_extension ($disco);
25 $cl->add_extension ($version);
27 $cl->set_presence (undef, 'I\'m a talking bot.', 1);
29 $cl->add_account ($jid, $pw);
30 warn "connecting to $jid...\n";
32 my $pub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
33 my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
36 session_ready => sub {
41 my ($cl, $acc, $msg) = @_;
45 my $body = $msg->any_body;
47 my $channel = join(' ', 'channel', $msg->from);
48 $pub->publish( $channel, $body );
49 warn "#X<< $channel | $body\n";
51 if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
53 $sub->psubscribe( "channel $1", sub {
54 my ( $message, $from ) = @_;
55 return unless $from !~ m/\Q$jid\E/; # skip our messages
56 warn "#Q<< $from | $message\n";
57 my ( undef, $channal, $user ) = split(/ /,$from,3);
58 $cl->send_message( "$channel <$user> $message" => $msg->from, $jid, 'chat' );
61 $response = "subscribed to $1";
65 my $repl = $msg->make_reply;
66 $repl->add_body( $response );
68 warn "#>>> $response\n";
71 contact_request_subscribe => sub {
72 my ($cl, $acc, $roster, $contact) = @_;
73 $contact->send_subscribed;
74 warn "Subscribed to ".$contact->jid."\n";
77 my ($cl, $acc, $error) = @_;
78 warn "Error encountered: ".$error->string."\n";
82 warn "Got disconnected: [@_]\n";