Class: Jabber::Iq

Inherits:
XMLStanza show all
Defined in:
lib/xmpp4r/iq.rb

Overview

IQ: Information/Query (see RFC3920 - 9.2.3

A class used to build/parse IQ requests/responses

Constant Summary collapse

@@element_classes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMLStanza

answer, #answer, #error, #from, #from=, #id, #id=, #normalize, #set_from, #set_id, #set_to, #to, #to=

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, #replace_element_text

Constructor Details

#initialize(type = nil, to = nil) ⇒ Iq

Build a new <iq/> stanza

type
Symbol

or nil, see Iq#type

to
JID

Recipient


22
23
24
25
26
27
28
29
30
# File 'lib/xmpp4r/iq.rb', line 22

def initialize(type = nil, to = nil)
  super("iq")
  if not to.nil?
    set_to(to)
  end 
  if not type.nil?
    set_type(type)
  end 
end

Class Method Details

.add_elementclass(name, elementclass) ⇒ Object

Add a class by name. Elements with this name will be automatically converted to the specific class. Used for <query/>, <vCard>, <pubsub> etc.

name
String

Element name

elementclass
Class

Target class


218
219
220
# File 'lib/xmpp4r/iq.rb', line 218

def Iq.add_elementclass(name, elementclass)
  @@element_classes[name] = elementclass
end

.import(xmlstanza) ⇒ Object

Create a new iq from a stanza, copies all attributes and children from xmlstanza

xmlstanza
REXML::Element

Source stanza

return
Iq

New stanza


112
113
114
# File 'lib/xmpp4r/iq.rb', line 112

def Iq.import(xmlstanza)
  Iq::new.import(xmlstanza)
end

.new_authset(jid, password) ⇒ Object

Create a new jabber:iq:auth set Stanza.


141
142
143
144
145
146
147
148
149
150
# File 'lib/xmpp4r/iq.rb', line 141

def Iq.new_authset(jid, password)
  iq = Iq::new(:set)
  query = IqQuery::new
  query.add_namespace('jabber:iq:auth')
  query.add(REXML::Element::new('username').add_text(jid.node))
  query.add(REXML::Element::new('password').add_text(password))
  query.add(REXML::Element::new('resource').add_text(jid.resource)) if not jid.resource.nil?
  iq.add(query)
  iq
end

.new_authset_digest(jid, session_id, password) ⇒ Object

Create a new jabber:iq:auth set Stanza for Digest authentication


154
155
156
157
158
159
160
161
162
163
# File 'lib/xmpp4r/iq.rb', line 154

def Iq.new_authset_digest(jid, session_id, password)
  iq = Iq::new(:set)
  query = IqQuery::new
  query.add_namespace('jabber:iq:auth')
  query.add(REXML::Element::new('username').add_text(jid.node))
  query.add(REXML::Element::new('digest').add_text(Digest::SHA1.hexdigest(session_id + password)))
  query.add(REXML::Element::new('resource').add_text(jid.resource)) if not jid.resource.nil?
  iq.add(query)
  iq
end

.new_browsegetObject

Create a new jabber:iq:roster get Stanza.


193
194
195
196
197
198
199
# File 'lib/xmpp4r/iq.rb', line 193

def Iq.new_browseget
  iq = Iq::new(:get)
  query = IqQuery::new
  query.add_namespace('jabber:iq:browse')
  iq.add(query)
  iq
end

.new_query(type = nil, to = nil) ⇒ Object

Create a new Iq stanza with an unspecified query child (<query/> has no namespace)


132
133
134
135
136
137
# File 'lib/xmpp4r/iq.rb', line 132

def Iq.new_query(type = nil, to = nil)
  iq = Iq::new(type, to)
  query = IqQuery::new
  iq.add(query)
  iq
end

.new_register(username = nil, password = nil) ⇒ Object

Create a new jabber:iq:register set stanza for service/server registration

username
String

(Element will be ommited if unset)

password
String

(Element will be ommited if unset)


169
170
171
172
173
174
175
176
177
# File 'lib/xmpp4r/iq.rb', line 169

def Iq.new_register(username=nil, password=nil)
  iq = Iq::new(:set)
  query = IqQuery::new
  query.add_namespace('jabber:iq:register')
  query.add(REXML::Element::new('username').add_text(username)) if username
  query.add(REXML::Element::new('password').add_text(password)) if password
  iq.add(query)
  iq
end

.new_rostergetObject

Create a new jabber:iq:roster get Stanza.

IqQueryRoster is unused here because possibly not require'd


183
184
185
186
187
188
189
# File 'lib/xmpp4r/iq.rb', line 183

def Iq.new_rosterget
  iq = Iq::new(:get)
  query = IqQuery::new
  query.add_namespace('jabber:iq:roster')
  iq.add(query)
  iq
end

.new_rostersetObject

Create a new jabber:iq:roster set Stanza.


203
204
205
206
207
208
209
# File 'lib/xmpp4r/iq.rb', line 203

def Iq.new_rosterset
  iq = Iq::new(:set)
  query = IqQuery::new
  query.add_namespace('jabber:iq:roster')
  iq.add(query)
  iq
end

Instance Method Details

#queryObject

Returns the iq's query child, or nil

result
IqQuery

75
76
77
# File 'lib/xmpp4r/iq.rb', line 75

def query 
  first_element('query')
end

#query=(newquery) ⇒ Object

Delete old elements named newquery.name

newquery
REXML::Element

will be added


83
84
85
86
# File 'lib/xmpp4r/iq.rb', line 83

def query=(newquery)
  delete_elements(newquery.name)
  add(newquery)
end

#querynsObject

Returns the iq's query's namespace, or nil

result
String

91
92
93
94
95
96
97
98
# File 'lib/xmpp4r/iq.rb', line 91

def queryns 
  e = first_element('query')
  if e
    return e.namespace
  else
    return nil
  end
end

#set_type(v) ⇒ Object

Set the type of the Iq stanza (chaining-friendly)

v
Symbol

or nil


67
68
69
70
# File 'lib/xmpp4r/iq.rb', line 67

def set_type(v)
  self.type = v
  self
end

#typeObject

Get the type of the Iq stanza

The following values are allowed:

  • :get

  • :set

  • :result

  • :error

result
Symbol

or nil


41
42
43
44
45
46
47
48
49
# File 'lib/xmpp4r/iq.rb', line 41

def type
  case super
    when 'get' then :get
    when 'set' then :set
    when 'result' then :result
    when 'error' then :error
    else nil
  end
end

#type=(v) ⇒ Object

Set the type of the Iq stanza (see Iq#type)

v
Symbol

or nil


54
55
56
57
58
59
60
61
62
# File 'lib/xmpp4r/iq.rb', line 54

def type=(v)
  case v
    when :get then super('get')
    when :set then super('set')
    when :result then super('result')
    when :error then super('error')
    else super(nil)
  end
end

#typed_add(element) ⇒ Object

Add an element to the Iq stanza

element
REXML::Element

Element to add.

Will be automatically converted (imported) to a class registered with add_elementclass


121
122
123
124
125
126
127
# File 'lib/xmpp4r/iq.rb', line 121

def typed_add(element)
  if element.kind_of?(REXML::Element) && @@element_classes.has_key?(element.name)
    super(@@element_classes[element.name]::import(element))
  else
    super(element)
  end
end

#vcardObject

Returns the iq's <vCard/> child, or nil

result
IqVcard

103
104
105
# File 'lib/xmpp4r/iq.rb', line 103

def vcard 
  first_element('vCard')
end