stanza.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2012 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 base64 import b64encode, b64decode
  8. from sleekxmpp.util import bytes as sbytes
  9. from sleekxmpp.xmlstream import ET, ElementBase, register_stanza_plugin
  10. class Data(ElementBase):
  11. name = 'data'
  12. namespace = 'urn:xmpp:avatar:data'
  13. plugin_attrib = 'avatar_data'
  14. interfaces = set(['value'])
  15. def get_value(self):
  16. if self.xml.text:
  17. return b64decode(sbytes(self.xml.text))
  18. return ''
  19. def set_value(self, value):
  20. if value:
  21. self.xml.text = b64encode(sbytes(value))
  22. # Python3 base64 encoded is bytes and needs to be decoded to string
  23. if isinstance(self.xml.text, bytes):
  24. self.xml.text = self.xml.text.decode()
  25. else:
  26. self.xml.text = ''
  27. def del_value(self):
  28. self.xml.text = ''
  29. class MetaData(ElementBase):
  30. name = 'metadata'
  31. namespace = 'urn:xmpp:avatar:metadata'
  32. plugin_attrib = 'avatar_metadata'
  33. interfaces = set()
  34. def add_info(self, id, itype, ibytes, height=None, width=None, url=None):
  35. info = Info()
  36. info.values = {'id': id,
  37. 'type': itype,
  38. 'bytes': '%s' % ibytes,
  39. 'height': height,
  40. 'width': width,
  41. 'url': url}
  42. self.append(info)
  43. def add_pointer(self, xml):
  44. if not isinstance(xml, Pointer):
  45. pointer = Pointer()
  46. pointer.append(xml)
  47. self.append(pointer)
  48. else:
  49. self.append(xml)
  50. class Info(ElementBase):
  51. name = 'info'
  52. namespace = 'urn:xmpp:avatar:metadata'
  53. plugin_attrib = 'info'
  54. plugin_multi_attrib = 'items'
  55. interfaces = set(['bytes', 'height', 'id', 'type', 'url', 'width'])
  56. class Pointer(ElementBase):
  57. name = 'pointer'
  58. namespace = 'urn:xmpp:avatar:metadata'
  59. plugin_attrib = 'pointer'
  60. plugin_multi_attrib = 'pointers'
  61. interfaces = set()
  62. register_stanza_plugin(MetaData, Info, iterable=True)
  63. register_stanza_plugin(MetaData, Pointer, iterable=True)