xep_0222.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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_0222(BasePlugin):
  12. """
  13. XEP-0222: Persistent Storage of Public Data via PubSub
  14. """
  15. name = 'xep_0222'
  16. description = 'XEP-0222: Persistent Storage of Public Data via PubSub'
  17. dependencies = set(['xep_0163', 'xep_0060', 'xep_0004'])
  18. profile = {'pubsub#persist_items': True,
  19. 'pubsub#send_last_published_item': 'never'}
  20. def configure(self, node):
  21. """
  22. Update a node's configuration to match the public storage profile.
  23. """
  24. config = self.xmpp['xep_0004'].Form()
  25. config['type'] = 'submit'
  26. for field, value in self.profile.items():
  27. config.add_field(var=field, value=value)
  28. return self.xmpp['xep_0060'].set_node_config(None, node, config,
  29. ifrom=ifrom,
  30. block=block,
  31. callback=callback,
  32. timeout=timeout)
  33. def store(self, stanza, node=None, id=None, ifrom=None, options=None,
  34. block=True, callback=None, timeout=None):
  35. """
  36. Store public data via PEP.
  37. This is just a (very) thin wrapper around the XEP-0060 publish()
  38. method to set the defaults expected by PEP.
  39. Arguments:
  40. stanza -- The private content to store.
  41. node -- The node to publish the content to. If not specified,
  42. the stanza's namespace will be used.
  43. id -- Optionally specify the ID of the item.
  44. options -- Publish options to use, which will be modified to
  45. fit the persistent storage option profile.
  46. ifrom -- Specify the sender's JID.
  47. block -- Specify if the send call will block until a response
  48. is received, or a timeout occurs. Defaults to True.
  49. timeout -- The length of time (in seconds) to wait for a response
  50. before exiting the send call if blocking is used.
  51. Defaults to sleekxmpp.xmlstream.RESPONSE_TIMEOUT
  52. callback -- Optional reference to a stream handler function. Will
  53. be executed when a reply stanza is received.
  54. """
  55. if not options:
  56. options = self.xmpp['xep_0004'].stanza.Form()
  57. options['type'] = 'submit'
  58. options.add_field(
  59. var='FORM_TYPE',
  60. ftype='hidden',
  61. value='http://jabber.org/protocol/pubsub#publish-options')
  62. fields = options['fields']
  63. for field, value in self.profile.items():
  64. if field not in fields:
  65. options.add_field(var=field)
  66. options['fields'][field]['value'] = value
  67. return self.xmpp['xep_0163'].publish(stanza, node,
  68. options=options,
  69. ifrom=ifrom,
  70. block=block,
  71. callback=callback,
  72. timeout=timeout)
  73. def retrieve(self, node, id=None, item_ids=None, ifrom=None,
  74. block=True, callback=None, timeout=None):
  75. """
  76. Retrieve public data via PEP.
  77. This is just a (very) thin wrapper around the XEP-0060 publish()
  78. method to set the defaults expected by PEP.
  79. Arguments:
  80. node -- The node to retrieve content from.
  81. id -- Optionally specify the ID of the item.
  82. item_ids -- Specify a group of IDs. If id is also specified, it
  83. will be included in item_ids.
  84. ifrom -- Specify the sender's JID.
  85. block -- Specify if the send call will block until a response
  86. is received, or a timeout occurs. Defaults to True.
  87. timeout -- The length of time (in seconds) to wait for a response
  88. before exiting the send call if blocking is used.
  89. Defaults to sleekxmpp.xmlstream.RESPONSE_TIMEOUT
  90. callback -- Optional reference to a stream handler function. Will
  91. be executed when a reply stanza is received.
  92. """
  93. if item_ids is None:
  94. item_ids = []
  95. if id is not None:
  96. item_ids.append(id)
  97. return self.xmpp['xep_0060'].get_items(None, node,
  98. item_ids=item_ids,
  99. ifrom=ifrom,
  100. block=block,
  101. callback=callback,
  102. timeout=timeout)
  103. register_plugin(XEP_0222)