error.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2010 Nathanael C. Fritz
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. from sleekxmpp.xmlstream import ElementBase, ET
  8. class Error(ElementBase):
  9. """
  10. XMPP stanzas of type 'error' should include an <error> stanza that
  11. describes the nature of the error and how it should be handled.
  12. Use the 'XEP-0086: Error Condition Mappings' plugin to include error
  13. codes used in older XMPP versions.
  14. Example error stanza:
  15. <error type="cancel" code="404">
  16. <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
  17. <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
  18. The item was not found.
  19. </text>
  20. </error>
  21. Stanza Interface:
  22. code -- The error code used in older XMPP versions.
  23. condition -- The name of the condition element.
  24. text -- Human readable description of the error.
  25. type -- Error type indicating how the error should be handled.
  26. Attributes:
  27. conditions -- The set of allowable error condition elements.
  28. condition_ns -- The namespace for the condition element.
  29. types -- A set of values indicating how the error
  30. should be treated.
  31. Methods:
  32. setup -- Overrides ElementBase.setup.
  33. get_condition -- Retrieve the name of the condition element.
  34. set_condition -- Add a condition element.
  35. del_condition -- Remove the condition element.
  36. get_text -- Retrieve the contents of the <text> element.
  37. set_text -- Set the contents of the <text> element.
  38. del_text -- Remove the <text> element.
  39. """
  40. namespace = 'jabber:client'
  41. name = 'error'
  42. plugin_attrib = 'error'
  43. interfaces = set(('code', 'condition', 'text', 'type',
  44. 'gone', 'redirect', 'by'))
  45. sub_interfaces = set(('text',))
  46. plugin_attrib_map = {}
  47. plugin_tag_map = {}
  48. conditions = set(('bad-request', 'conflict', 'feature-not-implemented',
  49. 'forbidden', 'gone', 'internal-server-error',
  50. 'item-not-found', 'jid-malformed', 'not-acceptable',
  51. 'not-allowed', 'not-authorized', 'payment-required',
  52. 'recipient-unavailable', 'redirect',
  53. 'registration-required', 'remote-server-not-found',
  54. 'remote-server-timeout', 'resource-constraint',
  55. 'service-unavailable', 'subscription-required',
  56. 'undefined-condition', 'unexpected-request'))
  57. condition_ns = 'urn:ietf:params:xml:ns:xmpp-stanzas'
  58. types = set(('cancel', 'continue', 'modify', 'auth', 'wait'))
  59. def setup(self, xml=None):
  60. """
  61. Populate the stanza object using an optional XML object.
  62. Overrides ElementBase.setup.
  63. Sets a default error type and condition, and changes the
  64. parent stanza's type to 'error'.
  65. Arguments:
  66. xml -- Use an existing XML object for the stanza's values.
  67. """
  68. if ElementBase.setup(self, xml):
  69. #If we had to generate XML then set default values.
  70. self['type'] = 'cancel'
  71. self['condition'] = 'feature-not-implemented'
  72. if self.parent is not None:
  73. self.parent()['type'] = 'error'
  74. def get_condition(self):
  75. """Return the condition element's name."""
  76. for child in self.xml:
  77. if "{%s}" % self.condition_ns in child.tag:
  78. cond = child.tag.split('}', 1)[-1]
  79. if cond in self.conditions:
  80. return cond
  81. return ''
  82. def set_condition(self, value):
  83. """
  84. Set the tag name of the condition element.
  85. Arguments:
  86. value -- The tag name of the condition element.
  87. """
  88. if value in self.conditions:
  89. del self['condition']
  90. self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
  91. return self
  92. def del_condition(self):
  93. """Remove the condition element."""
  94. for child in self.xml:
  95. if "{%s}" % self.condition_ns in child.tag:
  96. tag = child.tag.split('}', 1)[-1]
  97. if tag in self.conditions:
  98. self.xml.remove(child)
  99. return self
  100. def get_text(self):
  101. """Retrieve the contents of the <text> element."""
  102. return self._get_sub_text('{%s}text' % self.condition_ns)
  103. def set_text(self, value):
  104. """
  105. Set the contents of the <text> element.
  106. Arguments:
  107. value -- The new contents for the <text> element.
  108. """
  109. self._set_sub_text('{%s}text' % self.condition_ns, text=value)
  110. return self
  111. def del_text(self):
  112. """Remove the <text> element."""
  113. self._del_sub('{%s}text' % self.condition_ns)
  114. return self
  115. def get_gone(self):
  116. return self._get_sub_text('{%s}gone' % self.condition_ns, '')
  117. def get_redirect(self):
  118. return self._get_sub_text('{%s}redirect' % self.condition_ns, '')
  119. def set_gone(self, value):
  120. if value:
  121. del self['condition']
  122. return self._set_sub_text('{%s}gone' % self.condition_ns, value)
  123. elif self['condition'] == 'gone':
  124. del self['condition']
  125. def set_redirect(self, value):
  126. if value:
  127. del self['condition']
  128. ns = self.condition_ns
  129. return self._set_sub_text('{%s}redirect' % ns, value)
  130. elif self['condition'] == 'redirect':
  131. del self['condition']
  132. def del_gone(self):
  133. self._del_sub('{%s}gone' % self.condition_ns)
  134. def del_redirect(self):
  135. self._del_sub('{%s}redirect' % self.condition_ns)
  136. # To comply with PEP8, method names now use underscores.
  137. # Deprecated method names are re-mapped for backwards compatibility.
  138. Error.getCondition = Error.get_condition
  139. Error.setCondition = Error.set_condition
  140. Error.delCondition = Error.del_condition
  141. Error.getText = Error.get_text
  142. Error.setText = Error.set_text
  143. Error.delText = Error.del_text