diff sat/memory/encryption.py @ 2810:c161a294fffd

core: added a base menu allowing to set encryption session or show the trust management UI.
author Goffi <goffi@goffi.org>
date Sun, 24 Feb 2019 14:11:08 +0100
parents 003b8b4b56a7
children ab2696e34d29
line wrap: on
line diff
--- a/sat/memory/encryption.py	Sun Feb 24 14:09:44 2019 +0100
+++ b/sat/memory/encryption.py	Sun Feb 24 14:11:08 2019 +0100
@@ -17,12 +17,14 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from functools import partial
 from sat.core.i18n import D_, _
 from sat.core.constants import Const as C
 from sat.core import exceptions
 from collections import namedtuple
 from sat.core.log import getLogger
 from sat.tools.common import data_format
+from twisted.words.protocols.jabber import jid
 from twisted.internet import defer
 from twisted.python import failure
 import copy
@@ -257,7 +259,7 @@
         """Stop an encryption session with an entity
 
         @param entity(jid.JID): entity with who the encryption session must be stopped
-            must be bare jid is the algorithm encrypt for all devices
+            must be bare jid if the algorithm encrypt for all devices
         @param namespace(unicode): namespace of the session to stop
             when specified, used to check we stop the right encryption session
         """
@@ -357,6 +359,59 @@
         else:
             return defer.maybeDeferred(get_trust_ui, self.client, entity_jid)
 
+    ## Menus ##
+
+    @classmethod
+    def _importMenus(cls, host):
+        host.importMenu(
+             (D_(u"Encryption"), D_(u"unencrypted (plain text)")),
+             partial(cls._onMenuUnencrypted, host=host),
+             security_limit=0,
+             help_string=D_(u"End encrypted session"),
+             type_=C.MENU_SINGLE,
+        )
+        for plg in cls.getPlugins():
+            host.importMenu(
+                 (D_(u"Encryption"), plg.name),
+                 partial(cls._onMenuName, host=host, plg=plg),
+                 security_limit=0,
+                 help_string=D_(u"Start {name} session").format(name=plg.name),
+                 type_=C.MENU_SINGLE,
+            )
+            host.importMenu(
+                 (D_(u"Encryption"), D_(u"⛨ {name} trust").format(name=plg.name)),
+                 partial(cls._onMenuTrust, host=host, plg=plg),
+                 security_limit=0,
+                 help_string=D_(u"Manage {name} trust").format(name=plg.name),
+                 type_=C.MENU_SINGLE,
+            )
+
+    @classmethod
+    def _onMenuUnencrypted(cls, data, host, profile):
+        client = host.getClient(profile)
+        peer_jid = jid.JID(data[u'jid']).userhostJID()
+        d = client.encryption.stop(peer_jid)
+        d.addCallback(lambda __: {})
+        return d
+
+    @classmethod
+    def _onMenuName(cls, data, host, plg, profile):
+        client = host.getClient(profile)
+        peer_jid = jid.JID(data[u'jid'])
+        if not plg.directed:
+            peer_jid = peer_jid.userhostJID()
+        d = client.encryption.start(peer_jid, plg.namespace, replace=True)
+        d.addCallback(lambda __: {})
+        return d
+
+    @classmethod
+    @defer.inlineCallbacks
+    def _onMenuTrust(cls, data, host, plg, profile):
+        client = host.getClient(profile)
+        peer_jid = jid.JID(data[u'jid']).userhostJID()
+        ui = yield client.encryption.getTrustUI(peer_jid, plg.namespace)
+        defer.returnValue({u'xmlui': ui.toXml()})
+
     ## Triggers ##
 
     def setEncryptionFlag(self, mess_data):