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-29 15:06:10 +00:00
|
|
|
|
2012-02-18 19:40:34 +00:00
|
|
|
from sleekxmpp.xmlstream import ElementBase, ET
|
2010-07-29 15:06:10 +00:00
|
|
|
|
2009-12-15 04:37:10 +00:00
|
|
|
|
|
|
|
class Error(ElementBase):
|
2010-07-29 15:06:10 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XMPP stanzas of type 'error' should include an <error> stanza that
|
|
|
|
describes the nature of the error and how it should be handled.
|
|
|
|
|
|
|
|
Use the 'XEP-0086: Error Condition Mappings' plugin to include error
|
|
|
|
codes used in older XMPP versions.
|
|
|
|
|
|
|
|
Example error stanza:
|
|
|
|
<error type="cancel" code="404">
|
|
|
|
<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
|
|
|
|
<text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
|
|
|
|
The item was not found.
|
|
|
|
</text>
|
|
|
|
</error>
|
|
|
|
|
|
|
|
Stanza Interface:
|
|
|
|
code -- The error code used in older XMPP versions.
|
|
|
|
condition -- The name of the condition element.
|
|
|
|
text -- Human readable description of the error.
|
|
|
|
type -- Error type indicating how the error should be handled.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
conditions -- The set of allowable error condition elements.
|
|
|
|
condition_ns -- The namespace for the condition element.
|
|
|
|
types -- A set of values indicating how the error
|
|
|
|
should be treated.
|
|
|
|
|
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
setup -- Overrides ElementBase.setup.
|
|
|
|
get_condition -- Retrieve the name of the condition element.
|
|
|
|
set_condition -- Add a condition element.
|
|
|
|
del_condition -- Remove the condition element.
|
|
|
|
get_text -- Retrieve the contents of the <text> element.
|
|
|
|
set_text -- Set the contents of the <text> element.
|
|
|
|
del_text -- Remove the <text> element.
|
2010-07-29 15:06:10 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
name = 'error'
|
|
|
|
plugin_attrib = 'error'
|
2012-06-16 21:12:13 +00:00
|
|
|
interfaces = set(('code', 'condition', 'text', 'type',
|
|
|
|
'gone', 'redirect'))
|
2010-07-29 15:06:10 +00:00
|
|
|
sub_interfaces = set(('text',))
|
2011-08-31 07:40:33 +00:00
|
|
|
plugin_attrib_map = {}
|
|
|
|
plugin_tag_map = {}
|
2010-07-29 15:06:10 +00:00
|
|
|
conditions = set(('bad-request', 'conflict', 'feature-not-implemented',
|
|
|
|
'forbidden', 'gone', 'internal-server-error',
|
|
|
|
'item-not-found', 'jid-malformed', 'not-acceptable',
|
|
|
|
'not-allowed', 'not-authorized', 'payment-required',
|
|
|
|
'recipient-unavailable', 'redirect',
|
|
|
|
'registration-required', 'remote-server-not-found',
|
|
|
|
'remote-server-timeout', 'resource-constraint',
|
|
|
|
'service-unavailable', 'subscription-required',
|
|
|
|
'undefined-condition', 'unexpected-request'))
|
|
|
|
condition_ns = 'urn:ietf:params:xml:ns:xmpp-stanzas'
|
|
|
|
types = set(('cancel', 'continue', 'modify', 'auth', 'wait'))
|
|
|
|
|
|
|
|
def setup(self, xml=None):
|
|
|
|
"""
|
|
|
|
Populate the stanza object using an optional XML object.
|
|
|
|
|
|
|
|
Overrides ElementBase.setup.
|
|
|
|
|
|
|
|
Sets a default error type and condition, and changes the
|
|
|
|
parent stanza's type to 'error'.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- Use an existing XML object for the stanza's values.
|
|
|
|
"""
|
|
|
|
if ElementBase.setup(self, xml):
|
|
|
|
#If we had to generate XML then set default values.
|
|
|
|
self['type'] = 'cancel'
|
|
|
|
self['condition'] = 'feature-not-implemented'
|
|
|
|
if self.parent is not None:
|
|
|
|
self.parent()['type'] = 'error'
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_condition(self):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""Return the condition element's name."""
|
|
|
|
for child in self.xml.getchildren():
|
|
|
|
if "{%s}" % self.condition_ns in child.tag:
|
2011-07-03 05:50:31 +00:00
|
|
|
cond = child.tag.split('}', 1)[-1]
|
|
|
|
if cond in self.conditions:
|
|
|
|
return cond
|
2010-07-29 15:06:10 +00:00
|
|
|
return ''
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_condition(self, value):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""
|
|
|
|
Set the tag name of the condition element.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- The tag name of the condition element.
|
|
|
|
"""
|
|
|
|
if value in self.conditions:
|
|
|
|
del self['condition']
|
|
|
|
self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
|
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_condition(self):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""Remove the condition element."""
|
|
|
|
for child in self.xml.getchildren():
|
|
|
|
if "{%s}" % self.condition_ns in child.tag:
|
2010-07-30 03:55:13 +00:00
|
|
|
tag = child.tag.split('}', 1)[-1]
|
|
|
|
if tag in self.conditions:
|
|
|
|
self.xml.remove(child)
|
2010-07-29 15:06:10 +00:00
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_text(self):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""Retrieve the contents of the <text> element."""
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text('{%s}text' % self.condition_ns)
|
2010-07-29 15:06:10 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_text(self, value):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""
|
|
|
|
Set the contents of the <text> element.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- The new contents for the <text> element.
|
|
|
|
"""
|
2010-10-18 01:38:22 +00:00
|
|
|
self._set_sub_text('{%s}text' % self.condition_ns, text=value)
|
2010-07-29 15:06:10 +00:00
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_text(self):
|
2010-07-29 15:06:10 +00:00
|
|
|
"""Remove the <text> element."""
|
2010-10-18 01:38:22 +00:00
|
|
|
self._del_sub('{%s}text' % self.condition_ns)
|
2010-07-29 15:06:10 +00:00
|
|
|
return self
|
2011-02-14 18:49:43 +00:00
|
|
|
|
2012-06-16 21:12:13 +00:00
|
|
|
def get_gone(self):
|
|
|
|
return self._get_sub_text('{%s}gone' % self.condition_ns, '')
|
|
|
|
|
|
|
|
def get_redirect(self):
|
|
|
|
return self._get_sub_text('{%s}redirect' % self.condition_ns, '')
|
|
|
|
|
|
|
|
def set_gone(self, value):
|
|
|
|
del self['condition']
|
|
|
|
if value:
|
|
|
|
return self._set_sub_text('{%s}gone' % self.condition_ns, value)
|
|
|
|
|
|
|
|
def set_redirect(self, value):
|
|
|
|
del self['condition']
|
|
|
|
if value:
|
|
|
|
ns = self.condition_ns
|
|
|
|
return self._set_sub_text('{%s}redirect' % ns, value)
|
|
|
|
|
|
|
|
def del_gone(self):
|
|
|
|
self._del_sub('{%s}gone' % self.condition_ns)
|
|
|
|
|
|
|
|
def del_redirect(self):
|
|
|
|
self._del_sub('{%s}redirect' % self.condition_ns)
|
|
|
|
|
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.
|
|
|
|
Error.getCondition = Error.get_condition
|
|
|
|
Error.setCondition = Error.set_condition
|
|
|
|
Error.delCondition = Error.del_condition
|
|
|
|
Error.getText = Error.get_text
|
|
|
|
Error.setText = Error.set_text
|
|
|
|
Error.delText = Error.del_text
|