Fw: [JDEV] Proper use of Perl Net::Jabber calls

Yanming Xiao xiao at legatovideo.com
Wed Jan 8 09:07:58 CST 2003


Rebbaj,

I found a segment of code in my files listed in following, and 
you can see that I am using "$iq->GetXML()" and "$iq_result->GetXML()"
to check the IQ packet to be sent and the IQ packet result. 
As long as the format of XML is right, it should work. 

So, please check your XML before sending. 

Good luck.

-Yanming


...

    print "Connecting: $username\@$server:$port/$resource\n";

    # initialize the Jabber client object
#    $Connection = new Net::Jabber::Client(debuglevel=>2, debugfile=>"debug", debugtime=>1);
    $Connection = new Net::Jabber::Client;

    print ".\n";
    # try the connection and get the status of it
    $status = $Connection->Connect("hostname" => $server,
       "port" => $port);
    print ".\n";
    # If something's wrong with the connection
    if (!(defined($status))) 
    {
 print "<br />ERROR:  Jabber server is down or connection was not allowed.\n";
 print "<br />($!)\n";
 
 &print_file("./jru-bottom.inc");
 exit(0);
    }

    print ".\n";

    # Set callbacks for incoming info
   $Connection->SetCallBacks("message" => \&InMessage,
    "presence" => \&InPresence,
    "iq" => \&InIQ);

    # Connect!
    $Connection->Connect();
    print ".\n";
    # Try authorization
    @result = $Connection->AuthSend("username" => $username,
    "password" => $password,
    "resource" => $resource);
    print ".\n";
    # If the auth went awry
    if ($result[0] ne "ok") 
    {
 print "<br />ERROR: Authorization failed: $result[0] - $result[1]\n<br />";
 exit(0);
    }
    print ".\n";
    print "<br />Logged in successfully to $server:$port as $username...\n";

#    print "<br />Getting your roster...\n";
#
#   this step is important --- to behavior as a regualr Jabber client.  
#
    %roster = $Connection->RosterGet();

### Start of Subscription ####

print "<br />Sending presence...\n";
$Connection->PresenceSend();
#$Connection->Process(1);

my ($iq, $iq_query, $iq_result, my $from);
#
#<iq id="jcl_44" to="1st at mla.lvj1" type="set">
# <query xmlns="jabber:iq:register">
#  <nick>reminder</nick>
# </query>
#</iq>
#

$from = "$username\@$server/$resource";

print "<br />from= $from\n";

$iq = Net::Jabber::IQ->new();
$iq->SetIQ(
 from  => $from,
 type  => 'set',
 to    => $service_jid,
 id    => 'jru_02',
 query => 'info'
);

$iq_query = $iq->NewQuery("jabber:iq:register");
$iq_query->SetNick($nick);

print "<br />Sending iq...\n".$iq->GetXML()."\n";

$iq_result = $Connection->SendAndReceiveWithID($iq, 1);
# Success
if ($iq_result->GetType() eq 'result')
{
        print "<br />";
        print $iq_result->GetXML();
        print "<br />\n";
}
# Failure
else
{
 print "<br />Error: ", $iq_result->GetErrorCode(), " (", $iq_result->GetError(), ")\n";
# 
#         print "<br />ERROR: IQ failed: $result[0] - $result[1]\n<br />";
#  print "<br /><TEXTAREA NAME=RECV ROWS=10 COLS=70>\n";
#  print $result->toStr;
#                print "</TEXTAREA><br />\n";
#
 exit(0);
}

#$Connection->Process(1);

##########################
my $presence = Net::Jabber::Presence->new();
$presence->SetTo($service_jid);
#$presence->SetFrom($from);
$presence->SetType('subscribed'); 


#while( $first_run && defined($Connection->Process()))
print "<br />Sending presence...\n".$presence->GetXML()."\n";
$iq_result = $Connection->SendAndReceiveWithID($presence, 1);


### End of Subscription ####

    # Should be all done, so why don't we disconnect?  Sounds like fun to me.. but fun to you?  Could be.  Give it a shot, anyway...
    print "<br />Disconnecting...\n";
#
    #$do_subscribe = '';
    #$query->delete_all();
    #print $query->start_multipart_form();
    #print $query->end_form();
#
    $Connection->Disconnect();
    print "<br />Disconnected.\n";

#    print "\n<script type=\"text/javascript\">\n<!--\nfunction popUpLog() {\n newWindow = window.open('', 'newWin', 'toolbar=no,location=no,scrollbars=yes,resizable=yes,width=300,height=300')\n\n   newWindow.document.write(\"<html><head><title>Jabber roster utility Log</title></head><body bgcolor=ffffff text=000000><b><font face=sans-serif><div align=left><br />", $log, "</div></font></b></body></html>\")\n}\n -->\n </script>";
#    print "<br /><a href=\"\#\" onmousedown=\"popUpLog(); return true;\">Click here</a> to view a log of all users updated and all errors.\n<br /><br />";
#
#    print "<hr width=\"75%\" noshade=\"noshade\" />";


----- Original Message ----- 
From: rebbaj rebbaj 
To: jdev at jabber.org 
Sent: Tuesday, January 07, 2003 5:39 PM
Subject: Re: [JDEV] Proper use of Perl Net::Jabber calls


Thanks Yanming.  I took a good long look at the code found at http://www.jabberstudio.org/cgi-bin/viewcvs.cgi/*checkout*/netjabber/examples/client.pl?rev=1.4&content-type=text/plain 

What I still do not understand is why the $Connection->RosterGet(); call does not result in all of the roster entries being sent back from the server and hence triggering the sub InIQ routine.  If I manually enter the following XML "<iq type="get"><query xmlns="jabber:iq:roster"/></iq>, I get all the entries back. 

The docs state "RosterGet() - sends an empty Net::Jabber::IQ::Roster tag to the server so the server will send the Roster to the client." 

Any other ideas? 

Regards, 

Rebbaj 

 Yanming Xiao <xiao at legatovideo.com> wrote: 

  Rabbaj,

  It seems to me that you are using 2 different packages.  

  There is a function called "RosterGet()" in Net::Jabber::Client. 
  find the sample code in:
  http://jru.jabberstudio.org/


  -Yanming
    ----- Original Message ----- 
    From: rebbaj rebbaj 
    To: jdev at jabber.org 
    Sent: Tuesday, January 07, 2003 9:40 AM
    Subject: [JDEV] Proper use of Perl Net::Jabber calls


    Hello jabber people.

    I am hoping that someone can help me out with how the Perl Net::Jabber library should be used.  Note that I am also somewhat new to perl so if I get confused by some of the syntax, you will have to excuse me.  The examples in the Oreily book were rather helpful but I seem to be having problems in calling and receiving IQ information.

    Here is what I am trying to send and receive in XML:

    SENT:
    <iq id="jcl_9" type="get">
        <query xmlns="jabber:iq:roster"/>
    </iq>

    RECV:
    <iq from='rebbaj at jabber.org/Perl' id='jcl_9' type='result'>
      <query xmlns='jabber:iq:roster'>
        <item jid='rebbaj at jabber.com' name='rebbaj' subscription='both'/>
        <item jid='rebbaj2 at jabber.com' name='rebbaj2' subscription='both'/>
      </query>
    </iq>

    The code stub that I have written to do this is as follows.  I suspect that I have the flow a bit wrong and I am not using the optimal calls to parse the resulting returned roster data...  Any help would be appreciated....

    rebbaj

    ------------------------------------------------
    #!/usr/bin/perl
    use Net::Jabber qw(Client);
    use strict;
    my $c=Net::Jabber::Client->new();
    $c->Connect('hostname'=> 'jabber.org', 'port'=>5222);

    $c->AuthSend('username'=> 'rebbaj',
          'password'=> 'secret',
          'resource'=> 'Perl');

    $c->SetCallBacks('presence'=> \&handle_presence);
    $c->SetCallBacks('iq'=> \&handle_iq);
    $c->PresenceSend();

    My $IQ = new Net::Jabber::IQ();
    $IQ->NewQuery("jabber:iq:roster");  # Does this actually make the IQ get request?$c->Send($IQ); # is this call necessary?

    print "involking process\n";
    $c->Process();






    $c->Disconnect;
    exit(0);

    sub handle_presence {
     my ($sid, $presence) = @_;

     my $show = $presence->GetShow() || 'online';
     
     print "presence packet received $show\n";
    }

    sub handle_iq {
     my ($sid, $iq) = @_;

     my $querytag = $iq->GetQuery();


     print "iq packet received $querytag\n";
    }






----------------------------------------------------------------------------
    With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits your needs






--------------------------------------------------------------------------------
With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits your needs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.jabber.org/jdev/attachments/20030108/7cc98933/attachment-0002.htm>


More information about the JDev mailing list