Class: Jabber::Protocol::Iq

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/protocol.rb

Overview

A class used to build/parse IQ requests/responses

Constant Summary collapse

ERROR =
"error"
GET =
"get"
SET =
"set"
RESULT =
"result"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, id = nil) ⇒ Iq

Default constructor to build an Iq object

session

[Jabber::Session] The Jabber session instance

id

[String=nil] The (optional) id of the Iq object



423
424
425
426
# File 'lib/jabber4r/protocol.rb', line 423

def initialize(session,id=nil)
  @session=session
  @id=id
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def data
  @data
end

#errorObject

Returns the value of attribute error.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def error
  @error
end

#errorcodeObject

Returns the value of attribute errorcode.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def errorcode
  @errorcode
end

#fromObject

Returns the value of attribute from.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def from
  @from
end

#idObject

Returns the value of attribute id.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def id
  @id
end

#sessionObject

Returns the value of attribute session.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def session
  @session
end

#toObject

Returns the value of attribute to.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def to
  @to
end

#typeObject

Returns the value of attribute type.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def type
  @type
end

#xmlnsObject

Returns the value of attribute xmlns.



394
395
396
# File 'lib/jabber4r/protocol.rb', line 394

def xmlns
  @xmlns
end

Class Method Details

.from_element(session, element) ⇒ Object

Factory to build an IQ object from xml element

session

[Jabber::Session] The Jabber session instance

element

[Jabber::Protocol::ParsedXMLElement] The received XML object

return

[Jabber::Protocol::Iq] The newly created Iq object



407
408
409
410
411
412
413
414
415
416
417
# File 'lib/jabber4r/protocol.rb', line 407

def Iq.from_element(session, element)
  iq = Iq.new(session)
  iq.from = Jabber::JID.new(element.attr_from) if element.attr_from
  iq.to = Jabber::JID.new(element.attr_to) if element.attr_to
  iq.type = element.attr_type
  iq.id = element.attr_id
  iq.session=session
  iq.xmlns=element.query.attr_xmlns
  iq.data=element.query
  return iq
end

.gen_add_rosteritem(session, id, jid, name) ⇒ Object

Generates an IQ Roster Item add request XML element

session

[Session] The session

id

[String] The message id

jid

[JID] The Jabber ID to add to the roster

name

[String] The full name

return

[String] The XML data to send



484
485
486
487
488
489
490
# File 'lib/jabber4r/protocol.rb', line 484

def Iq.gen_add_rosteritem(session, id, jid, name)
  iq = Iq.new(session, id)
  iq.type = "set"
  iq.xmlns = "jabber:iq:roster"
  iq.data = XMLElement.new("item").add_attribute("jid", jid).add_attribute("name", name).to_s
  return iq
end

.gen_auth(session, id, username, password, resource) ⇒ Object

Generates an IQ authortization request XML element

id

[String] The message id

username

[String] The username

password

[String] The password

resource

[String] The resource to bind this session to

return

[String] The XML data to send



501
502
503
504
505
506
507
508
509
510
# File 'lib/jabber4r/protocol.rb', line 501

def Iq.gen_auth(session, id, username, password, resource)
  iq = Iq.new(session, id)
  iq.type = "set"
  iq.xmlns = "jabber:iq:auth"
  iq.data = XMLElement.new("username").add_data(username).to_s 
  iq.data << XMLElement.new("password").add_data(password).to_s
  iq.data << XMLElement.new("resource").add_data(resource).to_s
  return iq
  #element = XMLElement.new("iq", {"type"=>"set", "id"=>id}).add_child("query", {"xmlns"=>"jabber:iq:auth"}).add_child("username").add_data(username).to_parent.add_child("password").add_data(password).to_parent.add_child("resource").add_data(resource).to_parent.to_s
end

.gen_auth_digest(session, id, username, digest, resource) ⇒ Object

Generates an IQ digest authortization request XML element

id

[String] The message id

username

[String] The username

digest

[String] The SHA-1 hash of the sessionid and the password

resource

[String] The resource to bind this session to

return

[String] The XML data to send



521
522
523
524
525
526
527
528
529
530
# File 'lib/jabber4r/protocol.rb', line 521

def Iq.gen_auth_digest(session, id, username, digest, resource)
  iq = Iq.new(session, id)
  iq.type = "set"
  iq.xmlns = "jabber:iq:auth"
  iq.data = XMLElement.new("username").add_data(username).to_s 
  iq.data << XMLElement.new("digest").add_data(digest).to_s
  iq.data << XMLElement.new("resource").add_data(resource).to_s
  return iq
  #return XMLElement.new("iq", {"type"=>"set", "id"=>id}).add_child("query", {"xmlns"=>"jabber:iq:auth"}).add_child("username").add_data(username).to_parent.add_child("digest").add_data(digest).to_parent.add_child("resource").add_data(resource).to_parent.to_s
end

.gen_oob(session, to, url, desc = "") ⇒ Object

Generates an IQ out of bounds XML element

to

[JID] The Jabber ID to send to

url

[String] The data to send

desc

[String=""] The description of the data

return

[String] The XML data to send



540
541
542
543
544
545
546
547
548
# File 'lib/jabber4r/protocol.rb', line 540

def Iq.gen_oob(session, to, url, desc="")
  iq = Iq.new(session, nil)
  iq.type = "set"
  iq.xmlns = "jabber:iq:oob"
  iq.data = XMLElement.new("url").add_data(url).to_s
  iq.data << XMLElement.new("desc").add_data(desc).to_s
  return iq
  #return XMLElement.new("iq", {"type"=>"set"}).add_child("query", {"xmlns"=>"jabber:iq:oob"}).add_child("url").add_data(url).to_parent.add_child("desc").add_data(data).to_parent.to_s
end

.gen_registration(session, id, username, password, email, name) ⇒ Object

Generates an IQ authortization request XML element

id

[String] The message id

username

[String] The username

password

[String] The password

email

[String] The email address of the account

name

[String] The full name

return

[String] The XML data to send



464
465
466
467
468
469
470
471
472
473
# File 'lib/jabber4r/protocol.rb', line 464

def Iq.gen_registration(session, id, username, password, email, name)
  iq = Iq.new(session, id)
  iq.type = "set"
  iq.xmlns = "jabber:iq:register"
  iq.data = XMLElement.new("username").add_data(username).to_s 
  iq.data << XMLElement.new("password").add_data(password).to_s
  iq.data << XMLElement.new("email").add_data(email).to_s
  iq.data << XMLElement.new("name").add_data(name).to_s
  return iq
end

.gen_roster(session, id) ⇒ Object

Generates an IQ roster request XML element

id

[String] The message id

return

[String] The XML data to send



446
447
448
449
450
451
452
# File 'lib/jabber4r/protocol.rb', line 446

def Iq.gen_roster(session, id)
  iq = Iq.new(session, id)
  iq.type = "get"
  iq.xmlns = "jabber:iq:roster"
  return iq
  #return XMLElement.new("iq", {"type"=>"get", "id"=>id}).add_child("query", {"xmlns"=>"jabber:iq:roster"}).to_s
end

.gen_vcard(session, id, to) ⇒ Object

Generates an VCard request XML element

id

[String] The message ID

to

[JID] The jabber id of the account to get the VCard for

return

[String] The XML data to send



557
558
559
560
561
562
563
564
# File 'lib/jabber4r/protocol.rb', line 557

def Iq.gen_vcard(session, id, to)
  iq = Iq.new(session, id)
  iq.xmlns = "vcard-temp"
  iq.type = "get"
  iq.to = to
  return iq
  #return XMLElement.new("iq", {"type"=>"get", "id"=>id, "to"=>to}).add_child("query", {"xmlns"=>"vcard-temp"}).to_s
end

.get_private(session, id, ename, ns) ⇒ Object

Return an IQ object that uses the jabber:iq:private namespace



431
432
433
434
435
436
437
# File 'lib/jabber4r/protocol.rb', line 431

def Iq.get_private(session,id,ename,ns)
  iq=Iq.new(session,id)
  iq.type="get"
  iq.xmlns="jabber:iq:private"
  iq.data=XMLElement.new(ename,{'xmlns' => ns});
  return iq
end

Instance Method Details

#replyObject

Builds a reply to an existing Iq

return

[Jabber::Protocol::Iq] The result Iq



598
599
600
601
602
603
604
605
# File 'lib/jabber4r/protocol.rb', line 598

def reply
  iq = Iq.new(@session,@id)
  iq.to = @from
  iq.id = @id
  iq.type = 'result'
  @is_reply = true
  return iq
end

#send(wait = false, &block) ⇒ Object

Sends the IQ to the Jabber service for delivery

wait

[Boolean = false] Wait for reply before return?

&block

[Block] A block to process the message replies



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/jabber4r/protocol.rb', line 575

def send(wait=false, &block)
  if wait
    iq = nil
    blockedThread = Thread.current
    @session.connection.send(self.to_s, block) do |je| 
      if je.element_tag == "iq" and je.attr_id == @id
        je.consume_element
        iq = Iq.from_element(@session, je)
        blockedThread.wakeup
      end
    end
    Thread.stop
    rturn iq
  else
    @session.connection.send(self.to_s, block) if @session
  end
end

#to_sObject

see to_xml



629
630
631
# File 'lib/jabber4r/protocol.rb', line 629

def to_s
  to_xml
end

#to_xmlObject

Generates XML that complies with the Jabber protocol for sending the Iq through the Jabber service.

return

[String] The XML string.



613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/jabber4r/protocol.rb', line 613

def to_xml
  elem = XMLElement.new("iq", { "type"=>@type})
  elem.add_attribute("to" ,@to) if @to
  elem.add_attribute("id", @id) if @id
  elem.add_child("query").add_attribute("xmlns",@xmlns).add_data(@data.to_s)
  if @type=="error" then
    e=elem.add_child("error");
    e.add_attribute("code",@errorcode) if @errorcode
    e.add_data(@error) if @error
  end
  return elem.to_s
end