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/;
16 open my $f, $msgs_file
17 or die "Couldn't open messages file: '$msgs_file'\n";
18 (@msgs) = map { chomp; $_ } <$f>;
22 binmode STDOUT, ":utf8";
24 our ($jid, $pw, $inputfile, $redis_host, $redis_port);
27 warn "# $jid <- $inputfile\n";
29 read_messages ($inputfile);
31 my $j = AnyEvent->condvar;
32 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
33 my $disco = AnyEvent::XMPP::Ext::Disco->new;
34 my $version = AnyEvent::XMPP::Ext::Version->new;
36 $cl->add_extension ($disco);
37 $cl->add_extension ($version);
39 $cl->set_presence (undef, 'I\'m a talking bot.', 1);
41 $cl->add_account ($jid, $pw);
42 warn "connecting to $jid...\n";
44 my $redis = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
47 session_ready => sub {
52 my ($cl, $acc, $msg) = @_;
53 my $talkmsg = $msgs[int (rand (@msgs))];
54 my $repl = $msg->make_reply;
55 $repl->add_body ("You said '".$msg->any_body."' but... " . $talkmsg);
56 warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
57 warn "Answered: $talkmsg\n";
59 $redis->publish( join(' ', 'channel', $msg->from), $msg->any_body );
63 contact_request_subscribe => sub {
64 my ($cl, $acc, $roster, $contact) = @_;
65 $contact->send_subscribed;
66 warn "Subscribed to ".$contact->jid."\n";
69 my ($cl, $acc, $error) = @_;
70 warn "Error encountered: ".$error->string."\n";
74 warn "Got disconnected: [@_]\n";