stanza.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. from sleekxmpp.stanza import Error
  8. from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
  9. class LegacyError(ElementBase):
  10. """
  11. Older XMPP implementations used code based error messages, similar
  12. to HTTP response codes. Since then, error condition elements have
  13. been introduced. XEP-0086 provides a mapping between the new
  14. condition elements and a combination of error types and the older
  15. response codes.
  16. Also see <http://xmpp.org/extensions/xep-0086.html>.
  17. Example legacy error stanzas:
  18. <error xmlns="jabber:client" code="501" type="cancel">
  19. <feature-not-implemented
  20. xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
  21. </error>
  22. <error code="402" type="auth">
  23. <payment-required
  24. xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
  25. </error>
  26. Attributes:
  27. error_map -- A map of error conditions to error types and
  28. code values.
  29. Methods:
  30. setup -- Overrides ElementBase.setup
  31. set_condition -- Remap the type and code interfaces when a
  32. condition is set.
  33. """
  34. name = 'legacy'
  35. namespace = Error.namespace
  36. plugin_attrib = name
  37. interfaces = set(('condition',))
  38. overrides = ['set_condition']
  39. error_map = {'bad-request': ('modify', '400'),
  40. 'conflict': ('cancel', '409'),
  41. 'feature-not-implemented': ('cancel', '501'),
  42. 'forbidden': ('auth', '403'),
  43. 'gone': ('modify', '302'),
  44. 'internal-server-error': ('wait', '500'),
  45. 'item-not-found': ('cancel', '404'),
  46. 'jid-malformed': ('modify', '400'),
  47. 'not-acceptable': ('modify', '406'),
  48. 'not-allowed': ('cancel', '405'),
  49. 'not-authorized': ('auth', '401'),
  50. 'payment-required': ('auth', '402'),
  51. 'recipient-unavailable': ('wait', '404'),
  52. 'redirect': ('modify', '302'),
  53. 'registration-required': ('auth', '407'),
  54. 'remote-server-not-found': ('cancel', '404'),
  55. 'remote-server-timeout': ('wait', '504'),
  56. 'resource-constraint': ('wait', '500'),
  57. 'service-unavailable': ('cancel', '503'),
  58. 'subscription-required': ('auth', '407'),
  59. 'undefined-condition': (None, '500'),
  60. 'unexpected-request': ('wait', '400')}
  61. def setup(self, xml):
  62. """Don't create XML for the plugin."""
  63. self.xml = ET.Element('')
  64. def set_condition(self, value):
  65. """
  66. Set the error type and code based on the given error
  67. condition value.
  68. Arguments:
  69. value -- The new error condition.
  70. """
  71. self.parent().set_condition(value)
  72. error_data = self.error_map.get(value, None)
  73. if error_data is not None:
  74. if error_data[0] is not None:
  75. self.parent()['type'] = error_data[0]
  76. self.parent()['code'] = error_data[1]