xep_0163.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. import logging
  8. from sleekxmpp.xmlstream import register_stanza_plugin
  9. from sleekxmpp.plugins.base import BasePlugin, register_plugin
  10. log = logging.getLogger(__name__)
  11. class XEP_0163(BasePlugin):
  12. """
  13. XEP-0163: Personal Eventing Protocol (PEP)
  14. """
  15. name = 'xep_0163'
  16. description = 'XEP-0163: Personal Eventing Protocol (PEP)'
  17. dependencies = set(['xep_0030', 'xep_0060', 'xep_0115'])
  18. def register_pep(self, name, stanza):
  19. """
  20. Setup and configure events and stanza registration for
  21. the given PEP stanza:
  22. - Add disco feature for the PEP content.
  23. - Register disco interest in the PEP content.
  24. - Map events from the PEP content's namespace to the given name.
  25. :param str name: The event name prefix to use for PEP events.
  26. :param stanza: The stanza class for the PEP content.
  27. """
  28. pubsub_stanza = self.xmpp['xep_0060'].stanza
  29. register_stanza_plugin(pubsub_stanza.EventItem, stanza)
  30. self.add_interest(stanza.namespace)
  31. self.xmpp['xep_0030'].add_feature(stanza.namespace)
  32. self.xmpp['xep_0060'].map_node_event(stanza.namespace, name)
  33. def add_interest(self, namespace, jid=None):
  34. """
  35. Mark an interest in a PEP subscription by including a disco
  36. feature with the '+notify' extension.
  37. Arguments:
  38. namespace -- The base namespace to register as an interest, such
  39. as 'http://jabber.org/protocol/tune'. This may also
  40. be a list of such namespaces.
  41. jid -- Optionally specify the JID.
  42. """
  43. if not isinstance(namespace, set) and not isinstance(namespace, list):
  44. namespace = [namespace]
  45. for ns in namespace:
  46. self.xmpp['xep_0030'].add_feature('%s+notify' % ns,
  47. jid=jid)
  48. self.xmpp['xep_0115'].update_caps(jid)
  49. def remove_interest(self, namespace, jid=None):
  50. """
  51. Mark an interest in a PEP subscription by including a disco
  52. feature with the '+notify' extension.
  53. Arguments:
  54. namespace -- The base namespace to remove as an interest, such
  55. as 'http://jabber.org/protocol/tune'. This may also
  56. be a list of such namespaces.
  57. jid -- Optionally specify the JID.
  58. """
  59. if not isinstance(namespace, (set, list)):
  60. namespace = [namespace]
  61. for ns in namespace:
  62. self.xmpp['xep_0030'].del_feature(jid=jid,
  63. feature='%s+notify' % namespace)
  64. self.xmpp['xep_0115'].update_caps(jid)
  65. def publish(self, stanza, node=None, id=None, options=None, ifrom=None,
  66. block=True, callback=None, timeout=None):
  67. """
  68. Publish a PEP update.
  69. This is just a (very) thin wrapper around the XEP-0060 publish()
  70. method to set the defaults expected by PEP.
  71. Arguments:
  72. stanza -- The PEP update stanza to publish.
  73. node -- The node to publish the item to. If not specified,
  74. the stanza's namespace will be used.
  75. id -- Optionally specify the ID of the item.
  76. options -- A form of publish options.
  77. ifrom -- Specify the sender's JID.
  78. block -- Specify if the send call will block until a response
  79. is received, or a timeout occurs. Defaults to True.
  80. timeout -- The length of time (in seconds) to wait for a response
  81. before exiting the send call if blocking is used.
  82. Defaults to sleekxmpp.xmlstream.RESPONSE_TIMEOUT
  83. callback -- Optional reference to a stream handler function. Will
  84. be executed when a reply stanza is received.
  85. """
  86. if node is None:
  87. node = stanza.namespace
  88. if id is None:
  89. id = 'current'
  90. return self.xmpp['xep_0060'].publish(ifrom, node,
  91. id=id,
  92. payload=stanza.xml,
  93. options=options,
  94. ifrom=ifrom,
  95. block=block,
  96. callback=callback,
  97. timeout=timeout)
  98. register_plugin(XEP_0163)