comparison 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
comparison
equal deleted inserted replaced
2645:f2cf1daa42cb 2646:712cb4ff3e13
30 30
31 log = getLogger(__name__) 31 log = getLogger(__name__)
32 from sat.core.constants import Const as C 32 from sat.core.constants import Const as C
33 from sat.memory import memory 33 from sat.memory import memory
34 from sat.memory import cache 34 from sat.memory import cache
35 from sat.memory import encryption
35 from sat.tools import async_trigger as trigger 36 from sat.tools import async_trigger as trigger
36 from sat.tools import utils 37 from sat.tools import utils
37 from sat.tools.common import dynamic_import 38 from sat.tools.common import dynamic_import
38 from sat.tools.common import regex 39 from sat.tools.common import regex
39 from sat.stdui import ui_contact_list, ui_profile_manager 40 from sat.stdui import ui_contact_list, ui_profile_manager
108 self.bridge.register_method( 109 self.bridge.register_method(
109 "getPresenceStatuses", self.memory._getPresenceStatuses 110 "getPresenceStatuses", self.memory._getPresenceStatuses
110 ) 111 )
111 self.bridge.register_method("getWaitingSub", self.memory.getWaitingSub) 112 self.bridge.register_method("getWaitingSub", self.memory.getWaitingSub)
112 self.bridge.register_method("messageSend", self._messageSend) 113 self.bridge.register_method("messageSend", self._messageSend)
114 self.bridge.register_method("messageEncryptionStart",
115 self._messageEncryptionStart)
113 self.bridge.register_method("getConfig", self._getConfig) 116 self.bridge.register_method("getConfig", self._getConfig)
114 self.bridge.register_method("setParam", self.setParam) 117 self.bridge.register_method("setParam", self.setParam)
115 self.bridge.register_method("getParamA", self.memory.getStringParamA) 118 self.bridge.register_method("getParamA", self.memory.getStringParamA)
116 self.bridge.register_method("asyncGetParamA", self.memory.asyncGetStringParamA) 119 self.bridge.register_method("asyncGetParamA", self.memory.asyncGetStringParamA)
117 self.bridge.register_method( 120 self.bridge.register_method(
647 raise exceptions.ProfileUnknownError(profile_key) 650 raise exceptions.ProfileUnknownError(profile_key)
648 if profile not in self.profiles: 651 if profile not in self.profiles:
649 return False 652 return False
650 return self.profiles[profile].isConnected() 653 return self.profiles[profile].isConnected()
651 654
655 ## Encryption ##
656
657 def registerEncryptionPlugin(self, *args, **kwargs):
658 return encryption.EncryptionHandler.registerPlugin(*args, **kwargs)
659
652 ## XMPP methods ## 660 ## XMPP methods ##
653 661
654 def _messageSend(self, to_jid_s, message, subject=None, mess_type="auto", extra=None, 662 def _messageSend(self, to_jid_s, message, subject=None, mess_type="auto", extra=None,
655 profile_key=C.PROF_KEY_NONE,): 663 profile_key=C.PROF_KEY_NONE,):
656 client = self.getClient(profile_key) 664 client = self.getClient(profile_key)
662 message, 670 message,
663 subject, 671 subject,
664 mess_type, 672 mess_type,
665 {unicode(key): unicode(value) for key, value in extra.items()}, 673 {unicode(key): unicode(value) for key, value in extra.items()},
666 ) 674 )
675
676 def _messageEncryptionStart(self, to_jid_s, encryption_ns,
677 profile_key=C.PROF_KEY_NONE):
678 client = self.getClient(profile_key)
679 to_jid = jid.JID(to_jid_s)
680 return client.encryption.start(to_jid, encryption_ns.strip() or None)
667 681
668 def _setPresence(self, to="", show="", statuses=None, profile_key=C.PROF_KEY_NONE): 682 def _setPresence(self, to="", show="", statuses=None, profile_key=C.PROF_KEY_NONE):
669 return self.setPresence(jid.JID(to) if to else None, show, statuses, profile_key) 683 return self.setPresence(jid.JID(to) if to else None, show, statuses, profile_key)
670 684
671 def setPresence( 685 def setPresence(