GoogleTalk with perl
Par Thus0, samedi 5 novembre 2005 à 06:33 :: perl ::#76
Tutorial / Howto / How-To...
GTalk.pm is a perl library to send messages to Google Talk instant messaging using XMPP protocol, with SASL PLAIN authentication. It uses XML ::Stream and Net ::XMPP perl modules.
updated (2005/09/12) : you don’t need GTalk.pm anymore. You can use Net ::XMPP to send directly Google Talk messages. See my new tutorial : Google Talk with perl (bis)
Examples
Example n°1 : example.pl
example.pl is a perl script which illustrates how to use GTalk.pm module to send a message to GoogleTalk.
#!/usr/bin/perl -w
#
# script to send jabber message to GoogleTalk 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 GTalk;
use strict;
&main;
#################################################################
#
# main - Main subroutine
#
#################################################################
sub main ()
{
my $username = "thus0";
my $password = "test123";
my $to = "petrus\@gmail.com";
my $body = "hello world";
GTalk::GTalk(username=>$username, password=>$password,
to=>$to, body=>$body);
#GTalk::GTalk(debuglevel=>2, debugfile=>'gtalk.log',
# username=>$username, password=>$password,
# to=>$to, body=>$body);
exit(0);
}
Example n°2 : sendgtalk.pl
sendgtalk.pl is a perl script to send a message to GoogleTalk from command line, similar to sendxmpp or mail program.
$ sendgtalk.pl -h
sendgtalk.pl (c) 2005 Thus0 <Thus0@free.fr>
Usage: echo "body" | sendgtalk.pl [-u "username"] [-p "password"] [-d] "to"
-u "username" gmail username. Ex: thus0 (do not append suffix : thus0@gmail.com)
-p "password" gmail password
-d Turn on debugging.
"to" gmail recipient. Ex: petrus@gmail.com
"body" message to send
Example : echo "Hello World" | sendgtalk.pl -u thus0 -p test123 petrus@gmail.comTranscript
Here is a transcript of a connection to Google Talk instant messaging :
XML::Stream: new: hostname = (localhost)
XML::Stream: SetCallBacks: tag(node) func(CODE(0x876c2e0))
XMPP::Conn: SetCallBacks: tag(message) func(CODE(0x8164afc))
XMPP::Conn: SetCallBacks: tag(presence) func(CODE(0x8164a9c))
XMPP::Conn: SetCallBacks: tag(iq) func(CODE(0x8164a3c))
XMPP::Conn: SetDirectXPathCallBacks: xpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-tls"]) func(CODE(0x87915fc))
XMPP::Conn: SetDirectXPathCallBacks: xpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-sasl"]) func(CODE(0x8791710))
XMPP::Conn: Connect: host(talk.google.com:5222) namespace(jabber:client)
XMPP::Conn: Connect: timeout(10)
XML::Stream: Connect: type(tcpip)
XML::Stream: Connect: Got a connection
XML::Stream: Send: (<?xml version='1.0'?><stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='gmail.com' from='localhost' xml:lang='en' >)
XML::Stream: Read: buff(<?xml version="1.0" encoding="UTF-8"?><stream:stream from="gmail.com" id="XXXXXXXXXXXXXXXX" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">)
XML::Stream: Read: buff(<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms></stream:features>)
XMPP::Conn: Connect: connection made
XML::Stream: SetCallBacks: tag(node) func(CODE(0x8791860))
XML::Stream: Send: (<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>)
XML::Stream: Read: buff(<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>)
XML::Stream: TLSClientProceed: Convert normal socket to SSL
XML::Stream: TLSClientProceed: sock(IO::Socket::INET=GLOB(0x8792f24))
XML::Stream: LoadSSL: Load the IO::Socket::SSL module
XML::Stream: LoadSSL: Success
XML::Stream: TLSClientProceed: ssl_sock(IO::Socket::SSL=GLOB(0x8792f24))
XML::Stream: TLSClientProceed: SSL: We are secure
XML::Stream: Send: (<?xml version='1.0'?><stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='gmail.com' from='localhost' xml:lang='en' >)
XML::Stream: Read: buff(<?xml version="1.0" encoding="UTF-8"?><stream:stream from="gmail.com" id="XXXXXXXXXXXXXXXX" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">)
XML::Stream: Read: buff(<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms></stream:features>)
XMPP::Conn: AuthSASL: shiney new auth
XML::Stream: Send: (<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</auth>)
XMPP::Conn: AuthSASL: haven't authed yet... let's wait.
XMPP::Conn: Process: timeout(1)
XML::Stream: Read: buff(<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>)
XMPP::Conn: AuthSASL: We authed!
XML::Stream: Send: (<?xml version='1.0'?><stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='gmail.com' from='localhost' xml:lang='en' >)
XML::Stream: Read: buff(<?xml version="1.0" encoding="UTF-8"?><stream:stream from="gmail.com" id="XXXXXXXXXXXXXXXX" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">)
XML::Stream: Read: buff(<stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>)
XMPP::Conn: AuthSASL: We got a new session. sid(XXXXXXXXXXXXXXXX)
XMPP::Conn: AuthSASL: Binding to resource
XMPP::Conn: SendAndReceiveWithID: object(Net::XMPP::IQ=HASH(0x88a89d4))
XMPP::Conn: SendWithID: id(netjabber-0)
XMPP::Conn: SendWithID: in(<iq type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>GTalk.pl</resource></bind></iq>)
XMPP::Conn: RegisterID: tag(iq) id(netjabber-0)
XMPP::Conn: SendWithID: out(<iq id='netjabber-0' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>GTalk.pl</resource></bind></iq>)
XMPP::Conn: SendXML: sent(<iq id='netjabber-0' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>GTalk.pl</resource></bind></iq>)
XML::Stream: Send: (<iq id='netjabber-0' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>GTalk.pl</resource></bind></iq>)
XMPP::Conn: SendAndReceiveWithID: sent with id(netjabber-0)
XMPP::Conn: WaitForID: id(netjabber-0)
XMPP::Conn: ReceivedID: id(netjabber-0)
XMPP::Conn: ReceivedID: nope...
XMPP::Conn: WaitForID: haven't gotten it yet... let's wait for more packets
XMPP::Conn: Process: timeout(1)
XML::Stream: Read: buff(<iq id="netjabber-0" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>thus0@gmail.com/GTalk.plXXXXXXXX</jid></bind></iq>)
XMPP::Conn: CallBack: sid(XXXXXXXXXXXXXXXX) received(<iq id='netjabber-0' type='result'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>thus0@gmail.com/GTalk.plXXXXXXXX</jid></bind></iq>)
XMPP::Conn: CallBack: tag(iq)
XMPP::Conn: CallBack: id(netjabber-0)
XMPP::Conn: CallBack: we either want it or were waiting for it.
XMPP::Conn: CallBack: check directxpath
XMPP::Conn: CallBack: check directxpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-sasl"])
XMPP::Conn: CallBack: check directxpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-tls"])
XMPP::Conn: BuildObject: tag(iq) package(Net::XMPP::IQ)
XMPP::Conn: CheckID: tag(iq) id(netjabber-0)
XMPP::Conn: CheckID: we have that here somewhere...
XMPP::Conn: CallBack: found registry entry: tag(iq) id(netjabber-0)
XMPP::Conn: DeregisterID: tag(iq) id(netjabber-0)
XMPP::Conn: CallBack: they still want it... we still got it...
XMPP::Conn: GotID: id(netjabber-0) xml(<iq id='netjabber-0' type='result'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>thus0@gmail.com/GTalk.plXXXXXXXX</jid></bind></iq>)
XMPP::Conn: ReceivedID: id(netjabber-0)
XMPP::Conn: ReceivedID: id(netjabber-0)
XMPP::Conn: WaitForID: we got it!
XMPP::Conn: GetID: id(netjabber-0)
XMPP::Conn: ReceivedID: id(netjabber-0)
XMPP::Conn: CleanID: id(netjabber-0)
XMPP::Conn: AuthSASL: Starting session
XMPP::Conn: SendAndReceiveWithID: object(Net::XMPP::IQ=HASH(0x88ac8ac))
XMPP::Conn: SendWithID: id(netjabber-1)
XMPP::Conn: SendWithID: in(<iq type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>)
XMPP::Conn: RegisterID: tag(iq) id(netjabber-1)
XMPP::Conn: SendWithID: out(<iq id='netjabber-1' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>)
XMPP::Conn: SendXML: sent(<iq id='netjabber-1' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>)
XML::Stream: Send: (<iq id='netjabber-1' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>)
XMPP::Conn: SendAndReceiveWithID: sent with id(netjabber-1)
XMPP::Conn: WaitForID: id(netjabber-1)
XMPP::Conn: ReceivedID: id(netjabber-1)
XMPP::Conn: ReceivedID: nope...
XMPP::Conn: WaitForID: haven't gotten it yet... let's wait for more packets
XMPP::Conn: Process: timeout(1)
XML::Stream: Read: buff(<iq type="result" id="netjabber-1"/>)
XMPP::Conn: CallBack: sid(XXXXXXXXXXXXXXXX) received(<iq id='netjabber-1' type='result'/>)
XMPP::Conn: CallBack: tag(iq)
XMPP::Conn: CallBack: id(netjabber-1)
XMPP::Conn: CallBack: we either want it or were waiting for it.
XMPP::Conn: CallBack: check directxpath
XMPP::Conn: CallBack: check directxpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-sasl"])
XMPP::Conn: CallBack: check directxpath(/[@xmlns="urn:ietf:params:xml:ns:xmpp-tls"])
XMPP::Conn: BuildObject: tag(iq) package(Net::XMPP::IQ)
XMPP::Conn: CheckID: tag(iq) id(netjabber-1)
XMPP::Conn: CheckID: we have that here somewhere...
XMPP::Conn: CallBack: found registry entry: tag(iq) id(netjabber-1)
XMPP::Conn: DeregisterID: tag(iq) id(netjabber-1)
XMPP::Conn: CallBack: they still want it... we still got it...
XMPP::Conn: GotID: id(netjabber-1) xml(<iq id='netjabber-1' type='result'/>)
XMPP::Conn: ReceivedID: id(netjabber-1)
XMPP::Conn: ReceivedID: id(netjabber-1)
XMPP::Conn: WaitForID: we got it!
XMPP::Conn: GetID: id(netjabber-1)
XMPP::Conn: ReceivedID: id(netjabber-1)
XMPP::Conn: CleanID: id(netjabber-1)
XMPP::Conn: SendXML: sent(<message to='petrus@gmail.com'><body>Hello World</body><subject>GTalk</subject></message>)
XML::Stream: Send: (<message to='petrus@gmail.com'><body>Hello World </body><subject>GTalk</subject></message>)
XML::Stream: Send: (</stream:stream>)
XML::Stream: SetCallBacks: tag(node) func(CODE(0x88a7888))
XMPP::Conn: Disconnect: bye byeDocumentation
- transcript of Rod Chavez Chat (Google Talk team)

Commentaires
Aucun commentaire