Mercurial > libervia-backend
diff src/plugins/plugin_sec_otr.py @ 1955:633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
/!\ database schema has been modified, do a backup before updating
message have been refactored, here are the main changes:
- languages are now handled
- all messages have an uid (internal to SàT)
- message updating is anticipated
- subject is now first class
- new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend
- minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow
- threads handling
- delayed messages are saved into history
- info messages may also be saved in history (e.g. to keep track of people joining/leaving a room)
- duplicate messages should be avoided
- historyGet return messages in right order, no need to sort again
- plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR)
- XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element
- /!\ jp and Libervia are currently broken, as some features of Primitivus
It has been put in one huge commit to avoid breaking messaging between changes.
This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 24 May 2016 22:11:04 +0200 |
parents | 2daf7b4c6756 |
children | a2bc5089c2eb |
line wrap: on
line diff
--- a/src/plugins/plugin_sec_otr.py Mon Apr 18 18:35:19 2016 +0200 +++ b/src/plugins/plugin_sec_otr.py Tue May 24 22:11:04 2016 +0200 @@ -32,6 +32,8 @@ from sat.memory import persistent import potr import copy +import time +import uuid NS_OTR = "otr_plugin" PRIVATE_KEY = "PRIVATE KEY" @@ -73,11 +75,15 @@ msg = msg_str.decode('utf-8') client = self.user.client log.debug(u'inject(%s, appdata=%s, to=%s)' % (msg, appdata, self.peer)) - mess_data = {'message': msg, - 'type': 'chat', + mess_data = { 'from': client.jid, - 'to': self.peer, - 'subject': None, + 'to': self.peer, + 'uid': unicode(uuid.uuid4()), + 'message': {'': msg}, + 'subject': {}, + 'type': 'chat', + 'extra': {}, + 'timestamp': time.time(), } self.host.generateMessageXML(mess_data) client.xmlstream.send(mess_data['xml']) @@ -107,7 +113,7 @@ return client = self.user.client - self.host.bridge.newMessage(client.jid.full(), + self.host.bridge.messageNew(client.jid.full(), feedback, mess_type=C.MESS_TYPE_INFO, to_jid=self.peer.full(), @@ -202,7 +208,7 @@ self.context_managers = {} self.skipped_profiles = set() host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000) - host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000) + host.trigger.add("messageSend", self.messageSendTrigger, priority=100000) host.bridge.addMethod("skipOTR", ".plugin", in_sign='s', out_sign='', method=self._skipOTR) 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) @@ -275,7 +281,7 @@ 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?') + query = otrctx.messageSend(0, '?OTRv?') otrctx.inject(query) return {} @@ -406,18 +412,21 @@ encrypted = True try: - res = otrctx.receiveMessage(data['body'].encode('utf-8')) + message = data['message'].itervalues().next() # FIXME: Q&D fix for message refactoring, message is now a dict + res = otrctx.receiveMessage(message.encode('utf-8')) except potr.context.UnencryptedMessage: if otrctx.state == potr.context.STATE_ENCRYPTED: log.warning(u"Received unencrypted message in an encrypted context (from %(jid)s)" % {'jid': from_jid.full()}) client = self.host.getClient(profile) - self.host.bridge.newMessage(from_jid.full(), + self.host.bridge.messageNew(from_jid.full(), _(u"WARNING: received unencrypted data in a supposedly encrypted context"), mess_type=C.MESS_TYPE_INFO, to_jid=client.jid.full(), extra={}, profile=client.profile) encrypted = False + except StopIteration: + return data if not encrypted: return data @@ -425,7 +434,7 @@ if res[0] != None: # decrypted messages handling. # receiveMessage() will return a tuple, the first part of which will be the decrypted message - data['body'] = res[0].decode('utf-8') + data['message'] = {'':res[0].decode('utf-8')} # FIXME: Q&D fix for message refactoring, message is now a dict raise failure.Failure(exceptions.SkipHistory()) # we send the decrypted message to frontends, but we don't want it in history else: raise failure.Failure(exceptions.CancelError()) # no message at all (no history, no signal) @@ -433,19 +442,24 @@ def _receivedTreatmentForSkippedProfiles(self, data, profile): """This profile must be skipped because the frontend manages OTR itself, but we still need to check if the message must be stored in history or not""" - body = data['body'].encode('utf-8') - if body.startswith(potr.proto.OTRTAG): + try: + message = data['message'].itervalues().next().encode('utf-8') # FIXME: Q&D fix for message refactoring, message is now a dict + except StopIteration: + return data + if message.startswith(potr.proto.OTRTAG): raise failure.Failure(exceptions.SkipHistory()) return data - def MessageReceivedTrigger(self, message, post_treat, profile): + def MessageReceivedTrigger(self, client, message, post_treat): + profile = client.profile if profile in self.skipped_profiles: post_treat.addCallback(self._receivedTreatmentForSkippedProfiles, profile) else: post_treat.addCallback(self._receivedTreatment, profile) return True - def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): + def messageSendTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): + profile = client.profile if profile in self.skipped_profiles: return True to_jid = copy.copy(mess_data['to']) @@ -455,13 +469,19 @@ if mess_data['type'] != 'groupchat' and otrctx.state != potr.context.STATE_PLAINTEXT: if otrctx.state == potr.context.STATE_ENCRYPTED: log.debug(u"encrypting message") - otrctx.sendMessage(0, mess_data['message'].encode('utf-8')) - client = self.host.getClient(profile) + try: + msg = mess_data['message'][''] + except KeyError: + try: + msg = mess_data['message'].itervalues().next() + except StopIteration: + log.warning(u"No message found") + return False + otrctx.sendMessage(0, msg.encode('utf-8')) self.host.sendMessageToBridge(mess_data, client) else: feedback = D_("Your message was not sent because your correspondent closed the encrypted conversation on his/her side. Either close your own side, or refresh the session.") - client = self.host.getClient(profile) - self.host.bridge.newMessage(to_jid.full(), + self.host.bridge.messageNew(to_jid.full(), feedback, mess_type=C.MESS_TYPE_INFO, to_jid=client.jid.full(),