static.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. import logging
  8. import sleekxmpp
  9. from sleekxmpp.plugins.xep_0030 import StaticDisco
  10. log = logging.getLogger(__name__)
  11. class StaticExtendedDisco(object):
  12. """
  13. Extend the default StaticDisco implementation to provide
  14. support for extended identity information.
  15. """
  16. def __init__(self, static):
  17. """
  18. Augment the default XEP-0030 static handler object.
  19. Arguments:
  20. static -- The default static XEP-0030 handler object.
  21. """
  22. self.static = static
  23. def set_extended_info(self, jid, node, ifrom, data):
  24. """
  25. Replace the extended identity data for a JID/node combination.
  26. The data parameter may provide:
  27. data -- Either a single data form, or a list of data forms.
  28. """
  29. with self.static.lock:
  30. self.del_extended_info(jid, node, ifrom, data)
  31. self.add_extended_info(jid, node, ifrom, data)
  32. def add_extended_info(self, jid, node, ifrom, data):
  33. """
  34. Add additional extended identity data for a JID/node combination.
  35. The data parameter may provide:
  36. data -- Either a single data form, or a list of data forms.
  37. """
  38. with self.static.lock:
  39. self.static.add_node(jid, node)
  40. forms = data.get('data', [])
  41. if not isinstance(forms, list):
  42. forms = [forms]
  43. info = self.static.get_node(jid, node)['info']
  44. for form in forms:
  45. info.append(form)
  46. def del_extended_info(self, jid, node, ifrom, data):
  47. """
  48. Replace the extended identity data for a JID/node combination.
  49. The data parameter is not used.
  50. """
  51. with self.static.lock:
  52. if self.static.node_exists(jid, node):
  53. info = self.static.get_node(jid, node)['info']
  54. for form in info['substanza']:
  55. info.xml.remove(form.xml)