Skip to content
Blog Perl: Send a chat message to your gmail buddy using Jabber

Perl: Send a chat message to your gmail buddy using Jabber

Net::Jabber provides a Perl user with access to the Jabber Instant Messaging protocol. Net::Jabber is a convenient tool to use for any perl script that would like to utilize the Jabber Instant Messaging protocol. While not a client in and of itself, it provides all of the necessary back-end functions to make a CGI client or command-line perl client feasible and easy to use. Net::Jabber is a wrapper around the rest of the official Net::Jabber::xxxxxx packages.

Below a sample script to send a chat message to your gmail buddy.

#!/usr/bin/perl

# # # # # # # # # # # # # # # # # # # # # # # # # # #
# Program:  sendmessage.pl                          #
# Author:   Dipin Krishna                           #
# # # # # # # # # # # # # # # # # # # # # # # # # # #

use strict;
use Net::Jabber;

my $sttime=time;
if( $#ARGV != 3 ) {
  print "\n".'Usage: ./sendmessage.pl USERNAME PASSWORD 
                   "BUDDY@gmail.com" "MESSAGE"'."\n\n";
  exit 0;
}
print "Username:$ARGV[0]\n";
my $username = "$ARGV[0]";
my $password = "$ARGV[1]";
my $to = "$ARGV[2]";
my $msg = "$ARGV[3]";
print "$to: $msg\n";

my $resource = "dipin";
my $hostname = 'talk.google.com';
my $port = 5222;
my $componentname = 'gmail.com';
my $Contype = 'tcpip';
my $tls = 1;

my $Con = new Net::Jabber::Client();
$Con->SetCallBacks(presence=>\&presence,
  message=>\&message );

my $status = $Con->Connect(
  hostname => $hostname, port => $port,
  componentname => $componentname,
  connectiontype => $Contype, tls => $tls);

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

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

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

if ($result[0] ne "ok") {
  print "ERROR: Authorization failed: $result[0] - $result[1]\n";
}
else
{
  print "Logged in Sucessfull!\n";
  $Con->PresenceSend(show=>"Available");
  print "Sending Message!\n";
  $Con->MessageSend(to=>"$to",
    subject=>"Test",
    body=>"$msg\n",
    priority=>10);
}

2 thoughts on “Perl: Send a chat message to your gmail buddy using Jabber”

  1. Pingback: Perl: Send a chat message to your gmail buddy using Jabber | Linux Tips

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.