2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-06-30 17:54:53 +00:00
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
2010-03-26 21:32:16 +00:00
|
|
|
|
2010-06-30 17:54:53 +00:00
|
|
|
See the file license.txt for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2009-12-10 01:23:03 +00:00
|
|
|
from .. xmlstream.stanzabase import StanzaBase
|
|
|
|
from xml.etree import cElementTree as ET
|
2009-12-11 01:29:46 +00:00
|
|
|
from .. xmlstream.handler.waiter import Waiter
|
|
|
|
from .. xmlstream.matcher.id import MatcherId
|
2010-01-05 21:56:48 +00:00
|
|
|
from . rootstanza import RootStanza
|
2009-12-10 01:23:03 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class Iq(RootStanza):
|
2009-12-17 01:54:22 +00:00
|
|
|
interfaces = set(('type', 'to', 'from', 'id','query'))
|
2009-12-10 01:23:03 +00:00
|
|
|
types = set(('get', 'result', 'set', 'error'))
|
|
|
|
name = 'iq'
|
2010-04-14 08:23:17 +00:00
|
|
|
plugin_attrib = name
|
2009-12-10 01:23:03 +00:00
|
|
|
namespace = 'jabber:client'
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
StanzaBase.__init__(self, *args, **kwargs)
|
|
|
|
if self['id'] == '':
|
2010-01-05 21:56:48 +00:00
|
|
|
if self.stream is not None:
|
|
|
|
self['id'] = self.stream.getNewId()
|
|
|
|
else:
|
|
|
|
self['id'] = '0'
|
2009-12-17 01:54:22 +00:00
|
|
|
|
|
|
|
def unhandled(self):
|
2010-01-20 09:42:53 +00:00
|
|
|
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()
|
2009-12-10 01:23:03 +00:00
|
|
|
|
|
|
|
def setPayload(self, value):
|
|
|
|
self.clear()
|
|
|
|
StanzaBase.setPayload(self, value)
|
2010-05-22 14:40:30 +00:00
|
|
|
return self
|
2009-12-10 01:23:03 +00:00
|
|
|
|
2009-12-17 01:54:22 +00:00
|
|
|
def setQuery(self, value):
|
|
|
|
query = self.xml.find("{%s}query" % value)
|
2010-01-08 01:45:11 +00:00
|
|
|
if query is None and value:
|
2009-12-17 01:54:22 +00:00
|
|
|
self.clear()
|
|
|
|
query = ET.Element("{%s}query" % value)
|
|
|
|
self.xml.append(query)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getQuery(self):
|
2010-01-08 01:45:11 +00:00
|
|
|
for child in self.xml.getchildren():
|
2009-12-17 01:54:22 +00:00
|
|
|
if child.tag.endswith('query'):
|
|
|
|
ns =child.tag.split('}')[0]
|
|
|
|
if '{' in ns:
|
|
|
|
ns = ns[1:]
|
|
|
|
return ns
|
|
|
|
return ''
|
|
|
|
|
2009-12-22 10:05:53 +00:00
|
|
|
def reply(self):
|
|
|
|
self['type'] = 'result'
|
|
|
|
StanzaBase.reply(self)
|
|
|
|
return self
|
|
|
|
|
2009-12-17 01:54:22 +00:00
|
|
|
def delQuery(self):
|
|
|
|
for child in self.getchildren():
|
|
|
|
if child.tag.endswith('query'):
|
|
|
|
self.xml.remove(child)
|
|
|
|
return self
|
|
|
|
|
2010-07-06 18:16:46 +00:00
|
|
|
def send(self, block=True, timeout=10, priority=5, init=False):
|
2009-12-22 10:05:53 +00:00
|
|
|
if block and self['type'] in ('get', 'set'):
|
2009-12-11 01:29:46 +00:00
|
|
|
waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id']))
|
|
|
|
self.stream.registerHandler(waitfor)
|
2010-07-06 18:16:46 +00:00
|
|
|
StanzaBase.send(self, priority, init)
|
2009-12-11 01:29:46 +00:00
|
|
|
return waitfor.wait(timeout)
|
|
|
|
else:
|
2010-07-06 18:16:46 +00:00
|
|
|
return StanzaBase.send(self, priority, init)
|