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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ERROR = None
  18. import logging
  19. from gajim.plugins import GajimPlugin
  20. from gajim.common import app
  21. try: from .otr import *
  22. except ImportError: ERROR = 'python3-potr is missing'
  23. # ...
  24. class OTRPlugin(GajimPlugin):
  25. log = logging.getLogger('gajim.p.otr')
  26. # init plugin #
  27. def init(self):
  28. self.encryption_name = 'OTR'
  29. self.description = 'Provides Off-the-Record encryption'
  30. self.activatable = (not ERROR)
  31. self.available_text = (ERROR)
  32. self.instances = {}
  33. self.getinstance = lambda acct: self.instances.get(acct) or self.instances.setdefault(acct,OTR(self,acct))
  34. self.gui_extension_points = {
  35. 'encrypt' + self.encryption_name: (self._encrypt_message, None),
  36. 'decrypt': (self._decrypt_message, None),
  37. }
  38. # activate encryption #
  39. @staticmethod
  40. def activate_encryption(ctl):
  41. return True
  42. # encrypt file (just return same file)
  43. @staticmethod
  44. def encrypt_file(file, account, callback):
  45. callback(file)
  46. # encrypt message #
  47. def _encrypt_message(self,con,event,callback):
  48. if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages
  49. otr = self.getinstance(event.conn.name)
  50. otr.encrypt(event,callback)
  51. # decrypt message #
  52. def _decrypt_message(self,con,event,callback):
  53. if (event.encrypted) or (event.name[0:2] == 'gc') or not (event.msgtxt or '').startswith('?OTR'): return # skip non-OTR messages..
  54. if (event.name[0:3] == 'mam'): return setattr(event,'msgtxt','') # skip MAM messages (we can not decrypt OTR out of session)..
  55. 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
  56. otr = self.getinstance(event.conn.name)
  57. otr.decrypt(event,callback)