Listing 2: Perl script that uses the Net::Jabber modules

#!/usr/bin/perl

# QJab
# Send a quick Jabber message

#
# Perl Directives
#

use strict;

#
# Includes
#

use Net::Jabber qw(Client);

#
# Global Variables
#

use vars qw/@Msg $Server $Username $Password $Recipient $Client/;

# Internal settings go here.  This lists the primary Jabber server we 
# log onto, and what username and password we use

$Server="chat.example.com";
$Username="robot";
$Password="dhgt6490";

#
# Subroutines
#

# Read from STDIN, but stop if we encounter a line with just
# "--", which we could presume is the start of an email signature.

sub getMsg
{
my (@msg,$line);
while ($line=<STDIN>)
{
	last if ($line eq "--\n");
	push(@msg,$line);
}
return(@msg);
}

# Connect to the Jabber server, and then pass along
# our authentication information.

sub login
{
my ($server,$username,$password)=@_;
my ($client,$result);
$client=Net::Jabber::Client->new();
$client->Connect( hostname => $server) ||
	die("Unable to connect to $server\n");
($result)=$client->AuthSend( username => $username, 
	password => $password ,
	resource => "qjab_$$" );  # Attach PID to resource to prevent overlap
if ($result ne "ok")
{
	die("Authentication failed\n");
}
return($client);
}

# Send the message
sub send
{
my ($client,$recipient,@msg)=@_;
my ($msgstring);
$msgstring=join("\n",@msg);
$client->MessageSend(to => $recipient,
	body => $msgstring);
return;
}

#
# Main Program Block
#

# Read arguments to determine who to send the message to
$Recipient=shift();

unless ($Recipient)
{
        die("Usage: $0 [recipient_jid]\n");
}

# Read message from standard input
(@Msg)=&getMsg();

# Connect to the Jabber server
$Client=&login($Server,$Username,$Password);

# Send the message
&send($Client,$Recipient,@Msg);
