changeset 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 00d905e1b0ef
children a26b1ad2d3a4
files sat/core/sat_main.py sat/memory/encryption.py
diffstat 2 files changed, 69 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/sat/core/sat_main.py	Sun Feb 24 14:09:44 2019 +0100
+++ b/sat/core/sat_main.py	Sun Feb 24 14:11:08 2019 +0100
@@ -17,6 +17,10 @@
 # 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 glob import glob
+import sys
+import os.path
+import uuid
 import sat
 from sat.core.i18n import _, languageSwitch
 from sat.core import patches
@@ -30,7 +34,6 @@
 from sat.core import exceptions
 from sat.core.log import getLogger
 
-log = getLogger(__name__)
 from sat.core.constants import Const as C
 from sat.memory import memory
 from sat.memory import cache
@@ -41,10 +44,6 @@
 from sat.tools.common import regex
 from sat.stdui import ui_contact_list, ui_profile_manager
 import sat.plugins
-from glob import glob
-import sys
-import os.path
-import uuid
 
 
 try:
@@ -52,6 +51,7 @@
 except ImportError:
     from ordereddict import OrderedDict
 
+log = getLogger(__name__)
 
 class SAT(service.Service):
     def __init__(self):
@@ -206,9 +206,14 @@
                 )
             )
             sys.exit(1)
+        self._addBaseMenus()
         self.initialised.callback(None)
         log.info(_(u"Backend is ready"))
 
+    def _addBaseMenus(self):
+        """Add base menus"""
+        encryption.EncryptionHandler._importMenus(self)
+
     def _unimport_plugin(self, plugin_path):
         """remove a plugin from sys.modules if it is there"""
         try:
@@ -932,7 +937,7 @@
         d_list = defer.DeferredList(d_list)
         # XXX: 10 seconds may be too low for slow connections (e.g. mobiles)
         #      but for discovery, that's also the time the user will wait the first time
-        #      before seing the page.
+        #      before seing the page, if something goes wrong.
         d_list.addTimeout(10, reactor)
         infos_data = yield d_list
 
@@ -1188,14 +1193,8 @@
         """
         return tuple((p.lower().strip() for p in path))
 
-    def importMenu(
-        self,
-        path,
-        callback,
-        security_limit=C.NO_SECURITY_LIMIT,
-        help_string="",
-        type_=C.MENU_GLOBAL,
-    ):
+    def importMenu(self, path, callback, security_limit=C.NO_SECURITY_LIMIT,
+                   help_string="", type_=C.MENU_GLOBAL):
         """register a new menu for frontends
 
         @param path(iterable[unicode]): path to go to the menu
--- 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):