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 use Data::Dump qw(dump);
14 binmode STDOUT, ":utf8";
16 our ($jid, $pw, $inputfile, $redis_host, $redis_port);
19 warn "# $jid <- $inputfile\n";
21 my $j = AnyEvent->condvar;
22 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
23 my $disco = AnyEvent::XMPP::Ext::Disco->new;
24 my $version = AnyEvent::XMPP::Ext::Version->new;
26 $cl->add_extension ($disco);
27 $cl->add_extension ($version);
29 $cl->set_presence (undef, 'I\'m a talking bot.', 1);
31 $cl->add_account ($jid, $pw);
32 warn "connecting to $jid...\n";
34 my $pub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
35 my $sub = AnyEvent::Redis->new( host => $redis_host, port => $redis_port );
37 my $subscriptions = eval { retrieve '/tmp/subs' };
39 sub subscribe_channel {
40 my ( $who, $to ) = @_;
41 warn "# subscribe_channel $who $to\n";
43 $subscriptions->{$who}->{$to} ||= time;
45 $sub->psubscribe( "channel $to", sub {
46 my ( $message, $from ) = @_;
47 return unless $from !~ m/\Q($to|$jid)\E/; # skip our messages
48 warn "#Q<< $from [$jid] | $message\n";
49 my ( undef, $channel, $user ) = split(/ /,$from,3);
50 $cl->send_message( "$channel <$user> $message", $who => $jid, 'chat' );
53 store $subscriptions, '/tmp/subs';
56 foreach my $who ( keys %$subscriptions ) {
57 foreach my $to ( keys %{ $subscriptions->{$who} } ) {
58 subscribe_channel $who => $to;
63 session_ready => sub {
68 my ($cl, $acc, $msg) = @_;
72 my $body = $msg->any_body;
74 my $channel = join(' ', 'channel', $msg->from);
75 $pub->publish( $channel, $body );
76 warn "#X<< $channel | $body\n";
78 if ( $msg =~ m/!subscribe\s+(\S+)/ ) {
79 subscribe_channel $msg->from => $1;
80 $response = "subscribed to: " . dump( $subscriptions->{$msg->from} );
84 my $repl = $msg->make_reply;
85 $repl->add_body( $response );
87 warn "#>>> $response\n";
90 contact_request_subscribe => sub {
91 my ($cl, $acc, $roster, $contact) = @_;
92 $contact->send_subscribed;
93 warn "Subscribed to ".$contact->jid."\n";
96 my ($cl, $acc, $error) = @_;
97 warn "Error encountered: ".$error->string."\n";
101 warn "Got disconnected: [@_]\n";