2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-07-30 03:58:25 +00:00
|
|
|
|
|
|
|
from sleekxmpp.stanza.rootstanza import RootStanza
|
2010-11-17 20:13:09 +00:00
|
|
|
from sleekxmpp.xmlstream import StanzaBase, ET
|
2010-12-07 22:19:39 +00:00
|
|
|
from sleekxmpp.xmlstream.handler import Waiter, Callback
|
2012-04-08 20:30:52 +00:00
|
|
|
from sleekxmpp.xmlstream.matcher import MatcherId
|
2011-05-31 19:48:43 +00:00
|
|
|
from sleekxmpp.exceptions import IqTimeout, IqError
|
2010-07-30 03:58:25 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class Iq(RootStanza):
|
2010-07-30 03:58:25 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XMPP <iq> stanzas, or info/query stanzas, are XMPP's method of
|
|
|
|
requesting and modifying information, similar to HTTP's GET and
|
|
|
|
POST methods.
|
|
|
|
|
|
|
|
Each <iq> stanza must have an 'id' value which associates the
|
|
|
|
stanza with the response stanza. XMPP entities must always
|
|
|
|
be given a response <iq> stanza with a type of 'result' after
|
|
|
|
sending a stanza of type 'get' or 'set'.
|
|
|
|
|
|
|
|
Most uses cases for <iq> stanzas will involve adding a <query>
|
|
|
|
element whose namespace indicates the type of information
|
|
|
|
desired. However, some custom XMPP applications use <iq> stanzas
|
|
|
|
as a carrier stanza for an application-specific protocol instead.
|
|
|
|
|
|
|
|
Example <iq> Stanzas:
|
|
|
|
<iq to="user@example.com" type="get" id="314">
|
|
|
|
<query xmlns="http://jabber.org/protocol/disco#items" />
|
|
|
|
</iq>
|
|
|
|
|
|
|
|
<iq to="user@localhost" type="result" id="17">
|
|
|
|
<query xmlns='jabber:iq:roster'>
|
|
|
|
<item jid='otheruser@example.net'
|
|
|
|
name='John Doe'
|
|
|
|
subscription='both'>
|
|
|
|
<group>Friends</group>
|
|
|
|
</item>
|
|
|
|
</query>
|
|
|
|
</iq>
|
|
|
|
|
|
|
|
Stanza Interface:
|
|
|
|
query -- The namespace of the <query> element if one exists.
|
|
|
|
|
2010-07-30 18:11:24 +00:00
|
|
|
Attributes:
|
|
|
|
types -- May be one of: get, set, result, or error.
|
|
|
|
|
2010-07-30 03:58:25 +00:00
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
__init__ -- Overrides StanzaBase.__init__.
|
|
|
|
unhandled -- Send error if there are no handlers.
|
|
|
|
set_payload -- Overrides StanzaBase.set_payload.
|
|
|
|
set_query -- Add or modify a <query> element.
|
|
|
|
get_query -- Return the namespace of the <query> element.
|
|
|
|
del_query -- Remove the <query> element.
|
|
|
|
reply -- Overrides StanzaBase.reply
|
|
|
|
send -- Overrides StanzaBase.send
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
name = 'iq'
|
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'query'))
|
|
|
|
types = set(('get', 'result', 'set', 'error'))
|
|
|
|
plugin_attrib = name
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Initialize a new <iq> stanza with an 'id' value.
|
|
|
|
|
|
|
|
Overrides StanzaBase.__init__.
|
|
|
|
"""
|
|
|
|
StanzaBase.__init__(self, *args, **kwargs)
|
|
|
|
if self['id'] == '':
|
|
|
|
if self.stream is not None:
|
2011-04-08 20:14:22 +00:00
|
|
|
self['id'] = self.stream.new_id()
|
2010-07-30 03:58:25 +00:00
|
|
|
else:
|
|
|
|
self['id'] = '0'
|
|
|
|
|
|
|
|
def unhandled(self):
|
|
|
|
"""
|
|
|
|
Send a feature-not-implemented error if the stanza is not handled.
|
|
|
|
|
|
|
|
Overrides StanzaBase.unhandled.
|
|
|
|
"""
|
|
|
|
if self['type'] in ('get', 'set'):
|
|
|
|
self.reply()
|
|
|
|
self['error']['condition'] = 'feature-not-implemented'
|
|
|
|
self['error']['text'] = 'No handlers registered for this request.'
|
|
|
|
self.send()
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_payload(self, value):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
Set the XML contents of the <iq> stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- An XML object to use as the <iq> stanza's contents
|
|
|
|
"""
|
|
|
|
self.clear()
|
2010-10-18 01:38:22 +00:00
|
|
|
StanzaBase.set_payload(self, value)
|
2010-07-30 03:58:25 +00:00
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_query(self, value):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
Add or modify a <query> element.
|
|
|
|
|
|
|
|
Query elements are differentiated by their namespace.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- The namespace of the <query> element.
|
|
|
|
"""
|
|
|
|
query = self.xml.find("{%s}query" % value)
|
|
|
|
if query is None and value:
|
|
|
|
self.clear()
|
|
|
|
query = ET.Element("{%s}query" % value)
|
|
|
|
self.xml.append(query)
|
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_query(self):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""Return the namespace of the <query> element."""
|
2012-06-19 16:47:31 +00:00
|
|
|
for child in self.xml:
|
2010-07-30 03:58:25 +00:00
|
|
|
if child.tag.endswith('query'):
|
|
|
|
ns = child.tag.split('}')[0]
|
|
|
|
if '{' in ns:
|
|
|
|
ns = ns[1:]
|
|
|
|
return ns
|
|
|
|
return ''
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_query(self):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""Remove the <query> element."""
|
2012-06-19 16:47:31 +00:00
|
|
|
for child in self.xml:
|
2010-07-30 03:58:25 +00:00
|
|
|
if child.tag.endswith('query'):
|
|
|
|
self.xml.remove(child)
|
|
|
|
return self
|
|
|
|
|
2011-02-11 20:20:26 +00:00
|
|
|
def reply(self, clear=True):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
Send a reply <iq> stanza.
|
|
|
|
|
|
|
|
Overrides StanzaBase.reply
|
|
|
|
|
|
|
|
Sets the 'type' to 'result' in addition to the default
|
|
|
|
StanzaBase.reply behavior.
|
2011-02-11 20:20:26 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
clear -- Indicates if existing content should be
|
|
|
|
removed before replying. Defaults to True.
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
self['type'] = 'result'
|
2011-02-11 20:20:26 +00:00
|
|
|
StanzaBase.reply(self, clear)
|
2010-07-30 03:58:25 +00:00
|
|
|
return self
|
|
|
|
|
2011-05-27 23:59:52 +00:00
|
|
|
def send(self, block=True, timeout=None, callback=None, now=False):
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
|
|
|
Send an <iq> stanza over the XML stream.
|
|
|
|
|
|
|
|
The send call can optionally block until a response is received or
|
|
|
|
a timeout occurs. Be aware that using blocking in non-threaded event
|
2010-12-07 22:19:39 +00:00
|
|
|
handlers can drastically impact performance. Otherwise, a callback
|
|
|
|
handler can be provided that will be executed when the Iq stanza's
|
|
|
|
result reply is received. Be aware though that that the callback
|
|
|
|
handler will not be executed in its own thread.
|
|
|
|
|
|
|
|
Using both block and callback is not recommended, and only the
|
|
|
|
callback argument will be used in that case.
|
2010-07-30 03:58:25 +00:00
|
|
|
|
|
|
|
Overrides StanzaBase.send
|
|
|
|
|
|
|
|
Arguments:
|
2010-12-07 22:19:39 +00:00
|
|
|
block -- Specify if the send call will block until a response
|
|
|
|
is received, or a timeout occurs. Defaults to True.
|
|
|
|
timeout -- The length of time (in seconds) to wait for a response
|
|
|
|
before exiting the send call if blocking is used.
|
|
|
|
Defaults to sleekxmpp.xmlstream.RESPONSE_TIMEOUT
|
|
|
|
callback -- Optional reference to a stream handler function. Will
|
|
|
|
be executed when a reply stanza is received.
|
2011-05-27 23:59:52 +00:00
|
|
|
now -- Indicates if the send queue should be skipped and send
|
|
|
|
the stanza immediately. Used during stream
|
|
|
|
initialization. Defaults to False.
|
2010-07-30 03:58:25 +00:00
|
|
|
"""
|
2010-11-17 20:13:09 +00:00
|
|
|
if timeout is None:
|
|
|
|
timeout = self.stream.response_timeout
|
2010-12-07 22:19:39 +00:00
|
|
|
if callback is not None and self['type'] in ('get', 'set'):
|
2011-02-13 21:30:57 +00:00
|
|
|
handler_name = 'IqCallback_%s' % self['id']
|
|
|
|
handler = Callback(handler_name,
|
2012-04-08 20:30:52 +00:00
|
|
|
MatcherId(self['id']),
|
2010-12-07 22:19:39 +00:00
|
|
|
callback,
|
|
|
|
once=True)
|
|
|
|
self.stream.register_handler(handler)
|
2011-05-27 23:59:52 +00:00
|
|
|
StanzaBase.send(self, now=now)
|
2011-02-13 21:30:57 +00:00
|
|
|
return handler_name
|
2010-12-07 22:19:39 +00:00
|
|
|
elif block and self['type'] in ('get', 'set'):
|
2012-04-08 20:30:52 +00:00
|
|
|
waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id']))
|
2010-12-07 22:19:39 +00:00
|
|
|
self.stream.register_handler(waitfor)
|
2011-05-27 23:59:52 +00:00
|
|
|
StanzaBase.send(self, now=now)
|
2011-05-31 19:48:43 +00:00
|
|
|
result = waitfor.wait(timeout)
|
|
|
|
if not result:
|
2011-06-01 22:28:33 +00:00
|
|
|
raise IqTimeout(self)
|
2011-05-31 19:48:43 +00:00
|
|
|
if result['type'] == 'error':
|
|
|
|
raise IqError(result)
|
|
|
|
return result
|
2010-07-30 03:58:25 +00:00
|
|
|
else:
|
2011-05-27 23:59:52 +00:00
|
|
|
return StanzaBase.send(self, now=now)
|
2011-01-12 13:55:33 +00:00
|
|
|
|
|
|
|
def _set_stanza_values(self, values):
|
|
|
|
"""
|
|
|
|
Set multiple stanza interface values using a dictionary.
|
|
|
|
|
|
|
|
Stanza plugin values may be set usind nested dictionaries.
|
|
|
|
|
|
|
|
If the interface 'query' is given, then it will be set
|
|
|
|
last to avoid duplication of the <query /> element.
|
|
|
|
|
|
|
|
Overrides ElementBase._set_stanza_values.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
values -- A dictionary mapping stanza interface with values.
|
|
|
|
Plugin interfaces may accept a nested dictionary that
|
|
|
|
will be used recursively.
|
|
|
|
"""
|
|
|
|
query = values.get('query', '')
|
|
|
|
if query:
|
|
|
|
del values['query']
|
|
|
|
StanzaBase._set_stanza_values(self, values)
|
|
|
|
self['query'] = query
|
|
|
|
else:
|
|
|
|
StanzaBase._set_stanza_values(self, values)
|
|
|
|
return self
|
2011-02-14 18:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
Iq.setPayload = Iq.set_payload
|
|
|
|
Iq.getQuery = Iq.get_query
|
|
|
|
Iq.setQuery = Iq.set_query
|
|
|
|
Iq.delQuery = Iq.del_query
|