#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;

# PODNAME: xmpp
# ABSTRACT: Bit-rotted XMPP client for exobrain

use AnyEvent::XMPP::Client;
use AnyEvent;
use IO::Handle;

my $user  = $ENV{HXMPP_USER}  || shift || die "Usage $0 user pass owner\n";
my $pass  = $ENV{HXMPP_PASS}  || shift || die "Usage $0 user pass owner\n";
my $owner = $ENV{HXMPP_OWNER} || shift || die "Usage $0 user pass owner\n";

use Exobrain::Bus;

my $bus = Exobrain::Bus->new(
    type => 'SUB',
);

my $xmpp = AnyEvent::XMPP::Client->new( debug => 1 );

$xmpp->add_account($user, $pass);

$xmpp->reg_cb(
    session_ready => sub {
        say "Exobrain connected";
        $xmpp->send_message("<start> Exobrain connected <end>", $owner);

        # This shouldn't be an idle event. It should depend
        # upon data being available from our bus.

        state $idle_w = AnyEvent->idle( cb => sub {
            my $msg = $bus->get;
            say $msg->summary;
            my $cooked = $msg->summary;
            $xmpp->send_message($cooked, $owner);
        });
    }
);

$xmpp->start;

# Go do our main loop.
AnyEvent->condvar->wait;

__END__

=pod

=head1 NAME

xmpp - Bit-rotted XMPP client for exobrain

=head1 VERSION

version 0.05

=head1 AUTHOR

Paul Fenwick <pjf@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Paul Fenwick.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
