State 3: Retrieve a vCard
If we receive a single argument in the request, we’ll take it to be
a JID, passed from the link in the table build in the previous state,
and immediately build an IQ-get to retrieve the vCard. The JID is
to be found in the first element in the @a
array—$a[0].
else {
my $iq = $nf->newNode('iq');
$iq->attr('to', $a[0]);
$iq->attr('type', IQ_GET);
$iq->insertTag('vcard', NS_VCARD);
Notice how the name of the query tag in this query is not
query, but vcard.
See Section 6.5.1 in Chapter 6 for details.
The retrieval query is in the form of an IQ-get, rather than an IQ-set. As we needed to send information in our JUD query (the search criteria), an IQ-set was appropriate. Here, an IQ-get is appropriate as we’re not including any information to qualify our request; all we need to send is this:
<iq type='get' to='dj@gnu.mine.nu'> <vcard xmlns='vcard-temp'/> </iq>
After sending the <iq/> element,
waiting for the response, and checking for any errors, we extract the vCard detail and display it:
my $result = $c->ask($iq);
if ($result->attr('type') eq IQ_ERROR) {
$r->print("Sorry, cannot retrieve the vCard for $a[0]");
$r->print("</body></html>");
$c->disconnect;
return;
}The structure of the vCard namespace is rather complicated and long-winded, and it’s common for many of the fields to remain unfilled. So to keep the script simple, we’re going to display all the top-level fields that aren’t empty:
my $vcard = $result->getTag('', NS_VCARD); print ("<strong>$a[0]</strong>\n"); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access