diff sat/core/sat_main.py @ 2646:712cb4ff3e13

core: new EncryptionHandler class which manage encrypted session as a core feature: Plugin handling encryption can now register using host.registerEncryptionPlugin, and an encryption session can now be started using messageEncryptionStart bridge method. This will make encryption handling more easy, as we now know if a session is clear or e2e encrypted, and which plugin handle it.
author Goffi <goffi@goffi.org>
date Sun, 29 Jul 2018 19:22:56 +0200
parents f2cf1daa42cb
children 4e130cc9bfc0
line wrap: on
line diff
--- a/sat/core/sat_main.py	Sun Jul 29 19:22:51 2018 +0200
+++ b/sat/core/sat_main.py	Sun Jul 29 19:22:56 2018 +0200
@@ -32,6 +32,7 @@
 from sat.core.constants import Const as C
 from sat.memory import memory
 from sat.memory import cache
+from sat.memory import encryption
 from sat.tools import async_trigger as trigger
 from sat.tools import utils
 from sat.tools.common import dynamic_import
@@ -110,6 +111,8 @@
         )
         self.bridge.register_method("getWaitingSub", self.memory.getWaitingSub)
         self.bridge.register_method("messageSend", self._messageSend)
+        self.bridge.register_method("messageEncryptionStart",
+                                    self._messageEncryptionStart)
         self.bridge.register_method("getConfig", self._getConfig)
         self.bridge.register_method("setParam", self.setParam)
         self.bridge.register_method("getParamA", self.memory.getStringParamA)
@@ -649,6 +652,11 @@
             return False
         return self.profiles[profile].isConnected()
 
+    ## Encryption ##
+
+    def registerEncryptionPlugin(self, *args, **kwargs):
+        return encryption.EncryptionHandler.registerPlugin(*args, **kwargs)
+
     ## XMPP methods ##
 
     def _messageSend(self, to_jid_s, message, subject=None, mess_type="auto", extra=None,
@@ -665,6 +673,12 @@
             {unicode(key): unicode(value) for key, value in extra.items()},
         )
 
+    def _messageEncryptionStart(self, to_jid_s, encryption_ns,
+                                profile_key=C.PROF_KEY_NONE):
+        client = self.getClient(profile_key)
+        to_jid = jid.JID(to_jid_s)
+        return client.encryption.start(to_jid, encryption_ns.strip() or None)
+
     def _setPresence(self, to="", show="", statuses=None, profile_key=C.PROF_KEY_NONE):
         return self.setPresence(jid.JID(to) if to else None, show, statuses, profile_key)