nosave.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2013 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.stanza import Iq, Message
  9. from sleekxmpp.xmlstream.handler import Callback
  10. from sleekxmpp.xmlstream.matcher import StanzaPath
  11. from sleekxmpp.xmlstream import register_stanza_plugin
  12. from sleekxmpp.plugins import BasePlugin
  13. from sleekxmpp.plugins.google.nosave import stanza
  14. log = logging.getLogger(__name__)
  15. class GoogleNoSave(BasePlugin):
  16. """
  17. Google: Off the Record Chats
  18. NOTE: This is NOT an encryption method.
  19. Also see <https://developers.google.com/talk/jep_extensions/otr>.
  20. """
  21. name = 'google_nosave'
  22. description = 'Google: Off the Record Chats'
  23. dependencies = set(['google_settings'])
  24. stanza = stanza
  25. def plugin_init(self):
  26. register_stanza_plugin(Message, stanza.NoSave)
  27. register_stanza_plugin(Iq, stanza.NoSaveQuery)
  28. self.xmpp.register_handler(
  29. Callback('Google Nosave',
  30. StanzaPath('iq@type=set/google_nosave'),
  31. self._handle_nosave_change))
  32. def plugin_end(self):
  33. self.xmpp.remove_handler('Google Nosave')
  34. def enable(self, jid=None, block=True, timeout=None, callback=None):
  35. if jid is None:
  36. self.xmpp['google_settings'].update({'archiving_enabled': False},
  37. block=block, timeout=timeout, callback=callback)
  38. else:
  39. iq = self.xmpp.Iq()
  40. iq['type'] = 'set'
  41. iq['google_nosave']['item']['jid'] = jid
  42. iq['google_nosave']['item']['value'] = True
  43. return iq.send(block=block, timeout=timeout, callback=callback)
  44. def disable(self, jid=None, block=True, timeout=None, callback=None):
  45. if jid is None:
  46. self.xmpp['google_settings'].update({'archiving_enabled': True},
  47. block=block, timeout=timeout, callback=callback)
  48. else:
  49. iq = self.xmpp.Iq()
  50. iq['type'] = 'set'
  51. iq['google_nosave']['item']['jid'] = jid
  52. iq['google_nosave']['item']['value'] = False
  53. return iq.send(block=block, timeout=timeout, callback=callback)
  54. def get(self, block=True, timeout=None, callback=None):
  55. iq = self.xmpp.Iq()
  56. iq['type'] = 'get'
  57. iq.enable('google_nosave')
  58. return iq.send(block=block, timeout=timeout, callback=callback)
  59. def _handle_nosave_change(self, iq):
  60. reply = self.xmpp.Iq()
  61. reply['type'] = 'result'
  62. reply['id'] = iq['id']
  63. reply['to'] = iq['from']
  64. reply.send()
  65. self.xmpp.event('google_nosave_change', iq)