mam.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 permissio
  6. """
  7. import logging
  8. import sleekxmpp
  9. from sleekxmpp.stanza import Message, Iq
  10. from sleekxmpp.exceptions import XMPPError
  11. from sleekxmpp.xmlstream.handler import Collector
  12. from sleekxmpp.xmlstream.matcher import StanzaPath
  13. from sleekxmpp.xmlstream import register_stanza_plugin
  14. from sleekxmpp.plugins import BasePlugin
  15. from sleekxmpp.plugins.xep_0313 import stanza
  16. log = logging.getLogger(__name__)
  17. class XEP_0313(BasePlugin):
  18. """
  19. XEP-0313 Message Archive Management
  20. """
  21. name = 'xep_0313'
  22. description = 'XEP-0313: Message Archive Management'
  23. dependencies = set(['xep_0030', 'xep_0050', 'xep_0059', 'xep_0297'])
  24. stanza = stanza
  25. def plugin_init(self):
  26. register_stanza_plugin(Iq, stanza.MAM)
  27. register_stanza_plugin(Iq, stanza.Preferences)
  28. register_stanza_plugin(Message, stanza.Result)
  29. register_stanza_plugin(Message, stanza.Archived, iterable=True)
  30. register_stanza_plugin(stanza.Result, self.xmpp['xep_0297'].stanza.Forwarded)
  31. register_stanza_plugin(stanza.MAM, self.xmpp['xep_0059'].stanza.Set)
  32. def retrieve(self, jid=None, start=None, end=None, with_jid=None, ifrom=None,
  33. block=True, timeout=None, callback=None, iterator=False):
  34. iq = self.xmpp.Iq()
  35. query_id = iq['id']
  36. iq['to'] = jid
  37. iq['from'] = ifrom
  38. iq['type'] = 'get'
  39. iq['mam']['queryid'] = query_id
  40. iq['mam']['start'] = start
  41. iq['mam']['end'] = end
  42. iq['mam']['with'] = with_jid
  43. collector = Collector(
  44. 'MAM_Results_%s' % query_id,
  45. StanzaPath('message/mam_result@queryid=%s' % query_id))
  46. self.xmpp.register_handler(collector)
  47. if iterator:
  48. return self.xmpp['xep_0059'].iterate(iq, 'mam', 'results')
  49. elif not block and callback is not None:
  50. def wrapped_cb(iq):
  51. results = collector.stop()
  52. if iq['type'] == 'result':
  53. iq['mam']['results'] = results
  54. callback(iq)
  55. return iq.send(block=block, timeout=timeout, callback=wrapped_cb)
  56. else:
  57. try:
  58. resp = iq.send(block=block, timeout=timeout, callback=callback)
  59. resp['mam']['results'] = collector.stop()
  60. return resp
  61. except XMPPError as e:
  62. collector.stop()
  63. raise e
  64. def set_preferences(self, jid=None, default=None, always=None, never=None,
  65. ifrom=None, block=True, timeout=None, callback=None):
  66. iq = self.xmpp.Iq()
  67. iq['type'] = 'set'
  68. iq['to'] = jid
  69. iq['from'] = ifrom
  70. iq['mam_prefs']['default'] = default
  71. iq['mam_prefs']['always'] = always
  72. iq['mam_prefs']['never'] = never
  73. return iq.send(block=block, timeout=timeout, callback=callback)
  74. def get_configuration_commands(self, jid, **kwargs):
  75. return self.xmpp['xep_0030'].get_items(
  76. jid=jid,
  77. node='urn:xmpp:mam#configure',
  78. **kwargs)