Tutorial / Howto / How-to...

perl script to send jabber message to Google Talk / Googletalk Instant Messaging using XMPP protocol and SASL PLAIN authentication. Check that these perl modules are installed :

  • IO ::Socket ::SSL (>=0.81 ?)
  • XML ::Stream
  • Net ::XMPP
  • Authen ::SASL

#!/usr/bin/perl -w
#
# script to send jabber message to Google Talk Instant Messaging
#   using XMPP protocol and SASL PLAIN authentication.
#
# author: Thus0 <Thus0@free.fr>
# Copyright (c) 2005, Thus0 <thus0@free.fr>. All rights reserved.
#
# released under the terms of the GNU General Public License v2

use strict;
use Net::XMPP;


## Configuration

my $username = "thus0";
my $password = "XXXXX";

my $to = "petrus";
my $body = "hello world";

my $resource = "PerlBot";

## End of configuration

#------------------------------------

# Google Talk & Jabber parameters :

my $hostname = 'talk.google.com';
my $port = 5222;
my $componentname = 'gmail.com';
my $connectiontype = 'tcpip';
my $tls = 1;

#------------------------------------

my $Connection = new Net::XMPP::Client();

# Connect to talk.google.com
my $status = $Connection->Connect(
       hostname => $hostname, port => $port,
       componentname => $componentname,
       connectiontype => $connectiontype, tls => $tls);

if (!(defined($status))) {
   print "ERROR:  XMPP connection failed.\n";
   print "        ($!)\n";
   exit(0);
}

# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

# Authenticate
my @result = $Connection->AuthSend(
       username => $username, password => $password,
       resource => $resource);

if ($result[0] ne "ok") {
   print "ERROR: Authorization failed: $result[0] - $result[1]\n";
   exit(0);
}

# Send message
$Connection->MessageSend(
       to => "$to\@$componentname", body => $body,
       resource => $resource);