The setup_Jabber() function
Here we define the setup_Jabber() function,
which is called
to set up the connection to the Jabber server and authenticate with
a predefined user:
# Set up Jabber client connection, sending intial presence
sub setup_Jabber {
my ($server, $port, $user, $pass, $resource, $initial_status) = @_;
my $connection = new Net::Jabber::Client;
# Connect
my $status = $connection->Connect( hostname => $server,
port => $port );
die "Cannot connect to Jabber server $server on port $port\n"
unless $status;
# Callbacks
$connection->SetCallBacks( presence => \&InPresence );
# Ident/Auth
my @result = $connection->AuthSend( username => $user,
password => $pass,
resource => $resource );
die "Ident/Auth failed: $result[0] - $result[1]\n"
if $result[0] ne "ok";
# Roster
$connection->RosterGet();
# Initial presence dependent upon initial status
&set_presence($connection, $initial_status);
return $connection;
}
First, we instantiate a new Net::Jabber::Client
object. Net::Jabber distinguishes between client-
and component-based connections to Jabber; the component-based equivalent
of this class is Net::Jabber::Component. The
Connect() method is passed arguments that specify
the hostname and port of the Jabber server to connect to. It returns a
0 status if the connection could not be made.
We can register handlers for Jabber elements received over the XML stream carried by the connection we just made. Here we are interested in incoming presence subscription or unsubscription ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access