[omemo] Refactor encryption methods
This commit is contained in:
@@ -27,6 +27,7 @@ import shutil
|
|||||||
import nbxmpp
|
import nbxmpp
|
||||||
import binascii
|
import binascii
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
from nbxmpp.simplexml import Node
|
from nbxmpp.simplexml import Node
|
||||||
@@ -45,7 +46,7 @@ from .xmpp import (
|
|||||||
unpack_device_list_update, unpack_encrypted)
|
unpack_device_list_update, unpack_encrypted)
|
||||||
|
|
||||||
from common.connection_handlers_events import (
|
from common.connection_handlers_events import (
|
||||||
MessageReceivedEvent, MamMessageReceivedEvent)
|
MessageReceivedEvent, MamMessageReceivedEvent, MessageNotSentEvent)
|
||||||
|
|
||||||
|
|
||||||
IQ_CALLBACK = {}
|
IQ_CALLBACK = {}
|
||||||
@@ -614,53 +615,56 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
exception or error occurs
|
exception or error occurs
|
||||||
"""
|
"""
|
||||||
account = event.conn.name
|
account = event.conn.name
|
||||||
if account in self.disabled_accounts:
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
if not event.msg_iq.getTag('body'):
|
if account in self.disabled_accounts:
|
||||||
return
|
raise OMEMOError('Account disabled in OMEMO config')
|
||||||
state = self.get_omemo_state(account)
|
|
||||||
full_jid = str(event.msg_iq.getAttr('to'))
|
|
||||||
to_jid = gajim.get_jid_without_resource(full_jid)
|
|
||||||
|
|
||||||
plaintext = event.msg_iq.getBody()
|
|
||||||
msg_dict = state.create_gc_msg(
|
|
||||||
gajim.get_jid_from_account(account),
|
|
||||||
to_jid,
|
|
||||||
plaintext.encode('utf8'))
|
|
||||||
if not msg_dict:
|
|
||||||
return True
|
|
||||||
|
|
||||||
self.cleanup_stanza(event)
|
self.cleanup_stanza(event)
|
||||||
|
|
||||||
self.gc_message[msg_dict['payload']] = plaintext
|
if not event.message:
|
||||||
encrypted_node = OmemoMessage(msg_dict)
|
callback(event)
|
||||||
|
return
|
||||||
|
|
||||||
event.msg_iq.addChild(node=encrypted_node)
|
state = self.get_omemo_state(account)
|
||||||
|
to_jid = gajim.get_jid_without_resource(event.jid)
|
||||||
|
own_jid = gajim.get_jid_from_account(account)
|
||||||
|
|
||||||
# XEP-0380: Explicit Message Encryption
|
msg_dict = state.create_gc_msg(
|
||||||
if not event.msg_iq.getTag('encryption', attrs={'xmlns': NS_EME}):
|
own_jid, to_jid, event.message.encode('utf8'))
|
||||||
eme_node = Node('encryption', attrs={'xmlns': NS_EME,
|
if not msg_dict:
|
||||||
'name': 'OMEMO',
|
raise OMEMOError('Error while encrypting')
|
||||||
'namespace': NS_OMEMO})
|
|
||||||
event.msg_iq.addChild(node=eme_node)
|
|
||||||
|
|
||||||
# Add Message for devices that dont support OMEMO
|
except OMEMOError as error:
|
||||||
support_msg = 'You received a message encrypted with ' \
|
log.error(error)
|
||||||
'OMEMO but your client doesnt support OMEMO.'
|
gajim.nec.push_incoming_event(
|
||||||
event.msg_iq.setBody(support_msg)
|
MessageNotSentEvent(
|
||||||
|
None, conn=conn, jid=event.jid, message=event.message,
|
||||||
# Store Hint for MAM
|
error=error, time_=time.time(), session=None))
|
||||||
store = Node('store', attrs={'xmlns': NS_HINTS})
|
|
||||||
event.msg_iq.addChild(node=store)
|
|
||||||
|
|
||||||
self.print_msg_to_log(event.msg_iq)
|
|
||||||
|
|
||||||
callback(event)
|
|
||||||
except Exception as e:
|
|
||||||
log.debug(e)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.gc_message[msg_dict['payload']] = event.message
|
||||||
|
encrypted_node = OmemoMessage(msg_dict)
|
||||||
|
|
||||||
|
event.msg_iq.addChild(node=encrypted_node)
|
||||||
|
|
||||||
|
# XEP-0380: Explicit Message Encryption
|
||||||
|
eme_node = Node('encryption', attrs={'xmlns': NS_EME,
|
||||||
|
'name': 'OMEMO',
|
||||||
|
'namespace': NS_OMEMO})
|
||||||
|
event.msg_iq.addChild(node=eme_node)
|
||||||
|
|
||||||
|
# Add Message for devices that dont support OMEMO
|
||||||
|
support_msg = 'You received a message encrypted with ' \
|
||||||
|
'OMEMO but your client doesnt support OMEMO.'
|
||||||
|
event.msg_iq.setBody(support_msg)
|
||||||
|
|
||||||
|
# Store Hint for MAM
|
||||||
|
store = Node('store', attrs={'xmlns': NS_HINTS})
|
||||||
|
event.msg_iq.addChild(node=store)
|
||||||
|
|
||||||
|
self.print_msg_to_log(event.msg_iq)
|
||||||
|
callback(event)
|
||||||
|
|
||||||
def _encrypt_message(self, conn, event, callback):
|
def _encrypt_message(self, conn, event, callback):
|
||||||
""" Manipulates the outgoing stanza
|
""" Manipulates the outgoing stanza
|
||||||
|
|
||||||
@@ -676,44 +680,49 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
exception or error occurs
|
exception or error occurs
|
||||||
"""
|
"""
|
||||||
account = event.conn.name
|
account = event.conn.name
|
||||||
if account in self.disabled_accounts:
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
if not event.msg_iq.getTag('body'):
|
if account in self.disabled_accounts:
|
||||||
|
raise OMEMOError('Account disabled in OMEMO config')
|
||||||
|
|
||||||
|
self.cleanup_stanza(event)
|
||||||
|
|
||||||
|
if not event.message:
|
||||||
|
callback(event)
|
||||||
return
|
return
|
||||||
|
|
||||||
state = self.get_omemo_state(account)
|
state = self.get_omemo_state(account)
|
||||||
full_jid = str(event.msg_iq.getAttr('to'))
|
to_jid = gajim.get_jid_without_resource(event.jid)
|
||||||
to_jid = gajim.get_jid_without_resource(full_jid)
|
own_jid = gajim.get_jid_from_account(account)
|
||||||
|
|
||||||
plaintext = event.msg_iq.getBody().encode('utf8')
|
plaintext = event.message.encode('utf8')
|
||||||
|
msg_dict = state.create_msg(own_jid, to_jid, plaintext)
|
||||||
msg_dict = state.create_msg(
|
|
||||||
gajim.get_jid_from_account(account), to_jid, plaintext)
|
|
||||||
if not msg_dict:
|
if not msg_dict:
|
||||||
return True
|
raise OMEMOError('Error while encrypting')
|
||||||
|
|
||||||
encrypted_node = OmemoMessage(msg_dict)
|
except OMEMOError as error:
|
||||||
self.cleanup_stanza(event)
|
log.error(error)
|
||||||
|
gajim.nec.push_incoming_event(
|
||||||
|
MessageNotSentEvent(
|
||||||
|
None, conn=conn, jid=event.jid, message=event.message,
|
||||||
|
error=error, time_=time.time(), session=event.session))
|
||||||
|
return
|
||||||
|
|
||||||
event.msg_iq.addChild(node=encrypted_node)
|
encrypted_node = OmemoMessage(msg_dict)
|
||||||
|
event.msg_iq.addChild(node=encrypted_node)
|
||||||
|
|
||||||
# XEP-0380: Explicit Message Encryption
|
# XEP-0380: Explicit Message Encryption
|
||||||
if not event.msg_iq.getTag('encryption', attrs={'xmlns': NS_EME}):
|
eme_node = Node('encryption', attrs={'xmlns': NS_EME,
|
||||||
eme_node = Node('encryption', attrs={'xmlns': NS_EME,
|
'name': 'OMEMO',
|
||||||
'name': 'OMEMO',
|
'namespace': NS_OMEMO})
|
||||||
'namespace': NS_OMEMO})
|
event.msg_iq.addChild(node=eme_node)
|
||||||
event.msg_iq.addChild(node=eme_node)
|
|
||||||
|
|
||||||
# Store Hint for MAM
|
# Store Hint for MAM
|
||||||
store = Node('store', attrs={'xmlns': NS_HINTS})
|
store = Node('store', attrs={'xmlns': NS_HINTS})
|
||||||
event.msg_iq.addChild(node=store)
|
event.msg_iq.addChild(node=store)
|
||||||
self.print_msg_to_log(event.msg_iq)
|
self.print_msg_to_log(event.msg_iq)
|
||||||
event.xhtml = None
|
event.xhtml = None
|
||||||
event.encrypted = self.encryption_name
|
event.encrypted = self.encryption_name
|
||||||
callback(event)
|
callback(event)
|
||||||
except Exception as e:
|
|
||||||
log.debug(e)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def cleanup_stanza(obj):
|
def cleanup_stanza(obj):
|
||||||
@@ -1118,3 +1127,7 @@ class OmemoPlugin(GajimPlugin):
|
|||||||
"""
|
"""
|
||||||
state = self.get_omemo_state(account)
|
state = self.get_omemo_state(account)
|
||||||
state.encryption.deactivate(jid)
|
state.encryption.deactivate(jid)
|
||||||
|
|
||||||
|
|
||||||
|
class OMEMOError(Exception):
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user