diff src/plugins/plugin_sec_otr.py @ 1136:ea2bbdf5b541

plugin OTR: added start/refresh and end session menus
author Goffi <goffi@goffi.org>
date Mon, 25 Aug 2014 21:32:23 +0200
parents 3158f9e08760
children 768f1f1ef12c
line wrap: on
line diff
--- a/src/plugins/plugin_sec_otr.py	Mon Aug 25 21:32:23 2014 +0200
+++ b/src/plugins/plugin_sec_otr.py	Mon Aug 25 21:32:23 2014 +0200
@@ -20,7 +20,8 @@
 # XXX: thanks to Darrik L Mazey for his documentation (https://blog.darmasoft.net/2013/06/30/using-pure-python-otr.html)
 #      this implentation is based on it
 
-from sat.core.i18n import _
+from sat.core.i18n import _, D_
+from sat.core.constants import Const as C
 from sat.core.log import getLogger
 from sat.core import exceptions
 log = getLogger(__name__)
@@ -32,6 +33,7 @@
 
 NS_OTR = "otr_plugin"
 PRIVATE_KEY = "PRIVATE KEY"
+MAIN_MENU = D_('OTR')
 
 DEFAULT_POLICY_FLAGS = {
   'ALLOW_V1':False,
@@ -163,6 +165,8 @@
         self.context_managers = {}
         host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000)
         host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000)
+        host.importMenu((MAIN_MENU, D_("Start/Refresh")), self._startRefresh, security_limit=0, help_string=D_("Start or refresh an OTR session"), type_=C.MENU_SINGLE)
+        host.importMenu((MAIN_MENU, D_("End session")), self._endSession, security_limit=0, help_string=D_("Finish an OTR session"), type_=C.MENU_SINGLE)
 
     def _fixPotr(self):
         # FIXME: potr fix for bad unicode handling
@@ -193,6 +197,41 @@
         else:
             client.otr_priv_key = None
 
+    def _startRefresh(self, menu_data, profile):
+        """Start or refresh an OTR session
+
+        @param menu_data: %(menu_data)s
+        @param profile: %(doc_profile)s
+        """
+        try:
+            to_jid = jid.JID(menu_data['jid'])
+            if not to_jid.resource:
+                to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: temporary and unsecure, must be changed when frontends are refactored
+        except KeyError:
+            log.error(_("jid key is not present !"))
+            return defer.fail(exceptions.DataError)
+        otrctx = self.context_managers[profile].getContextForUser(to_jid)
+        query = otrctx.sendMessage(0, '?OTRv?')
+        otrctx.inject(query)
+        return {}
+
+    def _endSession(self, menu_data, profile):
+        """End an OTR session
+
+        @param menu_data: %(menu_data)s
+        @param profile: %(doc_profile)s
+        """
+        try:
+            to_jid = jid.JID(menu_data['jid'])
+            if not to_jid.resource:
+                to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: temporary and unsecure, must be changed when frontends are refactored
+        except KeyError:
+            log.error(_("jid key is not present !"))
+            return defer.fail(exceptions.DataError)
+        otrctx = self.context_managers[profile].getContextForUser(to_jid)
+        otrctx.disconnect()
+        return {}
+
     def _receivedTreatment(self, data, profile):
         from_jid = jid.JID(data['from'])
         log.debug(u"_receivedTreatment [from_jid = %s]" % from_jid)