# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://home.gna.org/xmpp4r/
require 'xmpp4r/xmppelement'
require 'xmpp4r/errors'
require 'xmpp4r/jid'
module Jabber
##
# root class of all Jabber XML elements
class XMPPStanza < XMPPElement
##
# Compose a response by doing the following:
# * Create a new XMPPStanza of the same subclass
# with the same element-name
# * Import xmppstanza if import is true
# * Swap 'to' and 'from'
# * Copy 'id'
# * Does not take care about the type
#
# *Attention*: Be careful when answering to stanzas with
# type == :error - answering to an error may generate
# another error on the other side, which could be leading to a
# ping-pong effect quickly!
#
# xmppstanza:: [XMPPStanza] source
# import:: [true or false] Copy attributes and children of source
# result:: [XMPPStanza] answer stanza
def XMPPStanza.answer(xmppstanza, import=true)
x = xmppstanza.class.new
if import
x.import(xmppstanza)
end
x.from = xmppstanza.to
x.to = xmppstanza.from
x.id = xmppstanza.id
x
end
##
# Return the first child
def error
first_element('error')
end
##
# Compose a response of this XMPPStanza
# (see XMPPStanza.answer)
# result:: [XMPPStanza] New constructed stanza
def answer(import=true)
XMPPStanza.answer(self, import)
end
##
# Makes some changes to the structure of an XML element to help
# it respect the specification. For example, in a message, we should
# have <