OTR plugin for Gajim 1.0+
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
  2. # Copyright (C) 2019 Pavel R. <pd at narayana dot im>
  3. # This file is part of Gajim OTR Plugin.
  4. #
  5. # Gajim OTR Plugin is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published
  7. # by the Free Software Foundation; version 3 only.
  8. #
  9. # This software is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You can always obtain full license text at <http://www.gnu.org/licenses/>.
  15. # TODO: Fingerprints authentication GUI
  16. # TODO: SMP authentication GUI
  17. import logging
  18. from gajim.plugins import GajimPlugin
  19. from gajim.common import app
  20. from otrplugin.otr import OTR
  21. class OTRPlugin(GajimPlugin):
  22. log = logging.getLogger('gajim.p.otr')
  23. def init(self):
  24. self.encryption_name = 'OTR'
  25. self.description = 'Provides Off-the-Record encryption'
  26. self.instances = {}
  27. self.get_instance = lambda acct: self.instances.get(acct) or self.instances.setdefault(acct,OTR(self,acct))
  28. self.events_handlers = {
  29. 'before-change-show': (10, self._on_status_change),
  30. }
  31. self.gui_extension_points = {
  32. 'encrypt' + self.encryption_name: (self._encrypt_message, None),
  33. 'decrypt': (self._decrypt_message, None),
  34. }
  35. @staticmethod
  36. def activate_encryption(ctl):
  37. return True
  38. @staticmethod
  39. def encrypt_file(file, account, callback):
  40. callback(file)
  41. def _encrypt_message(self,con,event,callback):
  42. if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages
  43. self.get_instance(event.conn.name).encrypt(event,callback)
  44. def _decrypt_message(self,con,event,callback):
  45. if (event.encrypted) or (event.name[0:2] == 'gc') or not (event.msgtxt or '').startswith('?OTR'): return # skip non-OTR messages..
  46. if (event.name[0:3] == 'mam'): return setattr(event,'msgtxt','') # skip MAM messages (we can not decrypt OTR out of session)..
  47. if (app.config.get_per('encryption','%s-%s'%(event.conn.name,event.jid),'encryption')!=self.encryption_name): return # skip all when encryption not set to OTR
  48. self.get_instance(event.conn.name).decrypt(event,callback)
  49. def _on_status_change(self,event):
  50. if event.show == 'offline':
  51. for ctx in self.get_instance(event.conn.name).ctxs.values(): ctx.state and ctx.disconnect()