January 2002
Beginner
480 pages
13h 15m
English
Before taking the Demo::JBook script apart, let’s have a look at the script in its entirety, shown in
Example 10-6. Written in Perl, Demo::JBook uses the Jabber::Connection library.
package Demo::JBook; use strict; use Jabber::Connection; use Jabber::NodeFactory; use Jabber::NS qw(:iq :misc); use constant SERVER => 'gnu.mine.nu'; use constant USER => 'jbook'; use constant PASS => 'pass'; use constant RESOURCE => 'jbook'; use constant JUD => 'jud.gnu.mine.nu'; sub handler { my $r = shift; my @a = $r->args; my $nf = Jabber::NodeFactory->new; $r->content_type('text/html'); $r->send_http_header; $r->print("<html><head><title>JBook</title></head><body>"); $r->print("<h1><a href="/jbook">JBook</a></h1>"); # Connect to the Jabber server my $c = Jabber::Connection->new(server => SERVER); unless ($c->connect) { $r->print("Sorry, no connection to Jabber available at ".SERVER); $r->print("</body></html>"); return; } $c->auth(USER, PASS, RESOURCE); # No arguments: Instructions if (scalar @a == 0) { # Construct IQ-get in iq:search namespace my $iq = $nf->newNode('iq'); $iq->attr('to', JUD); $iq->attr('type', IQ_GET); $iq->insertTag('query', NS_SEARCH); # Send the IQ-get my $result = $c->ask($iq); if ($result->attr('type') eq IQ_ERROR) { $r->print("sorry, no connection to JUD available at ".JUD); $r->print("</body></html>"); $c->disconnect; return; } my $info = $result->getTag('', NS_SEARCH); # Display the results in ...Read now
Unlock full access