Dear monks
I am playing with the Chat module Net::XMPP and so far it is working great. I am able to send and receive instant messages. What I am trying to do now is to use the CONNECT function in order to create an account on a server from the client if the account the client is trying to connect to does not exist (provided the service provider allow account creation from client). Here I fail. Please see the code below:
Updated: to reconnect to server, just in case, as suggested by roboticus (however same error with the Execute operation)
#!/usr/bin/perl
use strict;
use warnings;
use Net::XMPP;
use Mozilla::CA qw( );
my ($recipient, $message) = @ARGV;
if(!($recipient) || !($message)) {
print "Usage: $0 \n";
exit;
}
my $hostname = '';
my $sender = '';
my $password = ';
my $connection = new Net::XMPP::Client();
print "Connecting to server. ";
my $status = $connection->Connect(
hostname => $hostname,
connectiontype => 'tcpip',
tls => 1,
port => 5222,
ssl_ca_path => Mozilla::CA::SSL_ca_file(),
);
die("XMPP connection failed $!") if ! defined($status);
print "Done!\n";
my @result = $connection->AuthSend(
hostname => $hostname,
username => $sender,
password => $password);
#die("XMPP authentication failed") if $result[0] ne 'ok';
if ($result[0] ne 'ok'){
print "XMPP authentication failed. Creating a new account\n";
print "Connecting again to server. ";
my $status = $connection->Connect(
hostname => $hostname,
connectiontype => 'tcpip',
register=>1,
tls => 1,
port => 5222,
ssl_ca_path => Mozilla::CA::SSL_ca_file(),
);
die("XMPP connection failed $!") if ! defined($status);
print "Done!\n";
print "Trying to register new account. ";
my @result = $connection->Execute(
hostname=>$hostname,
port=>5222,
tls=>1,
username=>$sender,
password=>$password,
#resource=>"new",
register=>1,
connectiontype=>'tcpip',
#connecttimeout=>string,
connectattempts=>2,
connectsleep=>2,
processtimeout=>3,
);
}
die("Creation account failed") if $result[0] ne 'ok';
my @messages = ('first message', 'second', 'third');
foreach (@messages){
die("XMPP message failed") if ($connection->MessageSend(to => $recipie
+nt, body => $_) != 0);
sleep 3;
}
print "Message sent!\n";
The error I get is:
There was an error in the last call to Process that you did not check
+for and
handle. You should always check the output of the Process call. If i
+t was
undef then there was a fatal error that you need to check. There is a
+n error
in your program