plugin OTR: added start/refresh and end session menus

This commit is contained in:
Goffi 2014-08-25 21:32:23 +02:00
commit 5dce151148

View file

@ -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 @@ from sat.memory import persistent
NS_OTR = "otr_plugin"
PRIVATE_KEY = "PRIVATE KEY"
MAIN_MENU = D_('OTR')
DEFAULT_POLICY_FLAGS = {
'ALLOW_V1':False,
@ -163,6 +165,8 @@ class OTR(object):
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 @@ class OTR(object):
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)