#!/usr/bin/perl

use strict;
use utf8;
use AnyEvent;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::IM::Account;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
# use diagnostics;

BEGIN { $ENV{PROTOCOL_OTR_ENABLE_QUICK_RANDOM} = 1 }

use Protocol::OTR;

binmode STDOUT, ":utf8";

my $jid="robot\@domena.tld";
my $pw='hasełkorobota'; 

my $jid2="robot\@drugadomena.tld";
my $pw2='hasełkoxmpp'; 

my $j       = AnyEvent->condvar;
my $konto1  = AnyEvent::XMPP::Client->new( debug => 0 );
my $konto2  = AnyEvent::XMPP::Client->new( debug => 0 );

my $disco   = AnyEvent::XMPP::Ext::Disco->new;

# my $otr = Protocol::OTR->new({
#	       privkeys_file => "/var/lib/robot/otr.private_key",
#           contacts_file => "/var/lib/robot/otr.fingerprints",
#          instance_tags_file => "/var/lib/robot/otr.instance_tags",
#          }
#      );

# my $act = $otr->account( $jid, 'prpl-jabber' );
# my $act2 = $otr->account( $jid2, 'prpl-jabber' );

$konto1 ->add_extension($disco);
$konto1 ->set_presence( undef, 'Robocik melduje posłusznie pełną gotowość bojową ;)...', 1 );


$konto2 ->add_extension($disco);
$konto2 ->set_presence( undef, 'Robocik melduje posłusznie pełną gotowość bojową ;)...', 1 );

$konto1 ->add_account( $jid, $pw );
print "connecting to $jid...\n";

$konto2 ->add_account( $jid2, $pw2);
print "connecting to $jid2...\n";

$konto1->reg_cb(
    session_ready => sub {
        my ( $account, $acc ) = @_;
        
    },
   connect => sub {
      print "Robot jest połączony...;\) \n";
   },
   message => sub {
    my ($cl, $acc, $msg) = @_;
    my $from = $msg->from;
    my $message = $msg ->any_body; 
    my $repl = $msg ->make_reply;
    $repl->add_body ( "Robot usłyszał:  $message\n");
    $repl->send;
      print "\nRobot - połącznie od:  $from\n";
      print "Robot usłyszał:  $message\n";
   },
     contact_request_subscribe => sub {
        my ( $account, $acc, $roster, $contact ) = @_;
        $contact->send_subscribed;
        warn "Subscribed to " . $contact->jid . "\n";
    },
    error => sub {
        my ( $konto1, $acc, $error ) = @_;
        warn "Error encountered: " . $error->string . "\n";
        $j->broadcast;
    },
    disconnect => sub {
        warn "Got disconnected: [@_]\n";
        $j->broadcast;
    }, 
);

$konto2->reg_cb(
    session_ready => sub {
        my ( $account, $acc ) = @_;
        
    },
   connect => sub {
      print "XMPP jest połączony...;\)\n";
   },
   message => sub {
    my ($cl, $acc, $msg) = @_;
    my $from = $msg->from;
    my $message = $msg ->any_body; 
    if ($message ne ""){
		my $repl = $msg ->make_reply;
		$repl->add_body ( "XMPP usłyszał: $message\n");
		$repl->send;
		print "\nXMPP - połącznie od:  $from\n";
		print "XMPP usłyszał:  $message\n";
	 }
   },
     contact_request_subscribe => sub {
        my ( $account, $acc, $roster, $contact ) = @_;
        $contact->send_subscribed;
        warn "Subscribed to " . $contact->jid . "\n";
    },
    error => sub {
        my ( $konto1, $acc, $error ) = @_;
        warn "Error encountered: " . $error->string . "\n";
        $j->broadcast;
    },
    disconnect => sub {
        warn "Got disconnected: [@_]\n";
        $j->broadcast;
    }, 
);


$konto1 ->start;
$konto2 ->start;
print "Robocik melduje pełną gotowość bojową :\)\n";


$j->wait;
