From 0963f44443f6ac57a53caad4483f5ac2adbdbf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 19 Feb 2019 19:17:56 +0100 Subject: [PATCH] Use nbxmpp's parsed EME data --- gajim/common/const.py | 17 ++++++++ gajim/common/modules/mam.py | 8 ++-- gajim/common/modules/message.py | 7 ++-- gajim/common/modules/misc.py | 39 ------------------- gajim/common/modules/util.py | 8 ++++ gajim/common/zeroconf/client_zeroconf.py | 7 +++- .../zeroconf/connection_handlers_zeroconf.py | 19 +++------ 7 files changed, 41 insertions(+), 64 deletions(-) diff --git a/gajim/common/const.py b/gajim/common/const.py index 94e453df3..2dd2573b4 100644 --- a/gajim/common/const.py +++ b/gajim/common/const.py @@ -200,6 +200,23 @@ class MUCUser(IntEnum): AFFILIATION_TEXT = 4 +EME_MESSAGES = { + 'urn:xmpp:otr:0': + _('This message was encrypted with OTR ' + 'and could not be decrypted.'), + 'jabber:x:encrypted': + _('This message was encrypted with Legacy ' + 'OpenPGP and could not be decrypted. You can install ' + 'the PGP plugin to handle those messages.'), + 'urn:xmpp:openpgp:0': + _('This message was encrypted with ' + 'OpenPGP for XMPP and could not be decrypted.'), + 'fallback': + _('This message was encrypted with %s ' + 'and could not be decrypted.') +} + + ACTIVITIES = { 'doing_chores': { 'category': _('Doing Chores'), diff --git a/gajim/common/modules/mam.py b/gajim/common/modules/mam.py index 8f258b91a..4466e25f3 100644 --- a/gajim/common/modules/mam.py +++ b/gajim/common/modules/mam.py @@ -33,7 +33,7 @@ from gajim.common.helpers import AdditionalDataDict from gajim.common.modules.misc import parse_delay from gajim.common.modules.misc import parse_oob from gajim.common.modules.misc import parse_correction -from gajim.common.modules.misc import parse_eme +from gajim.common.modules.util import get_eme_message log = logging.getLogger('gajim.c.m.archiving') @@ -170,11 +170,9 @@ class MAM: else: app.plugin_manager.extension_point( 'decrypt', self._con, event, self._decryption_finished) - if not event.encrypted: - eme = parse_eme(event.message) - if eme is not None: - event.msgtxt = eme + if properties.eme is not None: + event.msgtxt = get_eme_message(properties.eme) self._decryption_finished(event) raise nbxmpp.NodeProcessed diff --git a/gajim/common/modules/message.py b/gajim/common/modules/message.py index b5e9c8c6f..f0a931ea5 100644 --- a/gajim/common/modules/message.py +++ b/gajim/common/modules/message.py @@ -28,10 +28,10 @@ from gajim.common.nec import NetworkIncomingEvent from gajim.common.nec import NetworkEvent from gajim.common.helpers import AdditionalDataDict from gajim.common.const import KindConstant +from gajim.common.modules.util import get_eme_message from gajim.common.modules.security_labels import parse_securitylabel from gajim.common.modules.user_nickname import parse_nickname from gajim.common.modules.misc import parse_delay -from gajim.common.modules.misc import parse_eme from gajim.common.modules.misc import parse_correction from gajim.common.modules.misc import parse_attention from gajim.common.modules.misc import parse_form @@ -187,9 +187,8 @@ class Message: app.plugin_manager.extension_point( 'decrypt', self._con, event, self._on_message_decrypted) if not event.encrypted: - eme = parse_eme(event.stanza) - if eme is not None: - event.msgtxt = eme + if properties.eme is not None: + event.msgtxt = get_eme_message(properties.eme) self._on_message_decrypted(event) def _on_message_decrypted(self, event): diff --git a/gajim/common/modules/misc.py b/gajim/common/modules/misc.py index e0358adaa..c70cf7ed7 100644 --- a/gajim/common/modules/misc.py +++ b/gajim/common/modules/misc.py @@ -19,50 +19,11 @@ import logging import nbxmpp from gajim.common import app -from gajim.common.i18n import _ from gajim.common.modules.date_and_time import parse_datetime log = logging.getLogger('gajim.c.m.misc') -# XEP-0380: Explicit Message Encryption - -_eme_namespaces = { - 'urn:xmpp:otr:0': - _('This message was encrypted with OTR ' - 'and could not be decrypted.'), - 'jabber:x:encrypted': - _('This message was encrypted with Legacy ' - 'OpenPGP and could not be decrypted. You can install ' - 'the PGP plugin to handle those messages.'), - 'urn:xmpp:openpgp:0': - _('This message was encrypted with ' - 'OpenPGP for XMPP and could not be decrypted.'), - 'fallback': - _('This message was encrypted with %s ' - 'and could not be decrypted.') -} - - -def parse_eme(stanza): - enc_tag = stanza.getTag('encryption', namespace=nbxmpp.NS_EME) - if enc_tag is None: - return - - ns = enc_tag.getAttr('namespace') - if ns is None: - log.warning('No namespace on EME message') - return - - if ns in _eme_namespaces: - log.info('Found not decrypted message: %s', ns) - return _eme_namespaces.get(ns) - - enc_name = enc_tag.getAttr('name') - log.info('Found not decrypted message: %s', enc_name or ns) - return _eme_namespaces.get('fallback') % enc_name or ns - - # XEP-0203: Delayed Delivery def parse_delay(stanza, epoch=True, convert='utc', from_=None, not_from=None): diff --git a/gajim/common/modules/util.py b/gajim/common/modules/util.py index 7ae4b5929..5ff3891aa 100644 --- a/gajim/common/modules/util.py +++ b/gajim/common/modules/util.py @@ -20,6 +20,7 @@ from functools import wraps from functools import partial from gajim.common import app +from gajim.common.const import EME_MESSAGES def from_xs_boolean(value: Union[str, bool]) -> bool: @@ -73,3 +74,10 @@ def store_publish(func): return return func(self, *args, **kwargs) return func_wrapper + + +def get_eme_message(eme_data): + try: + return EME_MESSAGES[eme_data.namespace] + except KeyError: + return EME_MESSAGES['fallback'] % eme_data.name diff --git a/gajim/common/zeroconf/client_zeroconf.py b/gajim/common/zeroconf/client_zeroconf.py index 7944d3c3a..889f6059f 100644 --- a/gajim/common/zeroconf/client_zeroconf.py +++ b/gajim/common/zeroconf/client_zeroconf.py @@ -28,6 +28,7 @@ from unittest.mock import Mock import nbxmpp from nbxmpp import dispatcher_nb from nbxmpp import simplexml +from nbxmpp.structs import StanzaHandler from nbxmpp.plugin import PlugIn from nbxmpp.idlequeue import IdleObject from nbxmpp.transports_nb import DATA_RECEIVED @@ -322,8 +323,10 @@ class P2PClient(IdleObject): def _register_handlers(self): self._caller.peerhost = self.Connection._sock.getsockname() - self.RegisterHandler('message', lambda conn, - data: self._caller._messageCB(self.Server, conn, data)) + + self.RegisterHandler(*StanzaHandler(name='message', + callback=self._caller._messageCB)) + self.RegisterHandler('iq', self._caller._siSetCB, 'set', nbxmpp.NS_SI) self.RegisterHandler('iq', self._caller._siErrorCB, 'error', nbxmpp.NS_SI) diff --git a/gajim/common/zeroconf/connection_handlers_zeroconf.py b/gajim/common/zeroconf/connection_handlers_zeroconf.py index 9c1b52f7d..31059c4b4 100644 --- a/gajim/common/zeroconf/connection_handlers_zeroconf.py +++ b/gajim/common/zeroconf/connection_handlers_zeroconf.py @@ -27,14 +27,13 @@ import nbxmpp from gajim.common import app from gajim.common.protocol.bytestream import ConnectionSocks5BytestreamZeroconf -from gajim.common.zeroconf.zeroconf import Constant from gajim.common import connection_handlers from gajim.common.i18n import _ from gajim.common.helpers import AdditionalDataDict from gajim.common.nec import NetworkIncomingEvent, NetworkEvent from gajim.common.const import KindConstant from gajim.common.modules.user_nickname import parse_nickname -from gajim.common.modules.misc import parse_eme +from gajim.common.modules.util import get_eme_message from gajim.common.modules.misc import parse_correction from gajim.common.modules.misc import parse_attention from gajim.common.modules.misc import parse_oob @@ -65,11 +64,11 @@ class ConnectionHandlersZeroconf(ConnectionSocks5BytestreamZeroconf, connection_handlers.ConnectionJingle.__init__(self) connection_handlers.ConnectionHandlersBase.__init__(self) - def _messageCB(self, ip, con, stanza): + def _messageCB(self, _con, stanza, properties): """ Called when we receive a message """ - log.debug('Zeroconf MessageCB') + log.info('Zeroconf MessageCB') app.nec.push_incoming_event(NetworkEvent( 'raw-message-received', @@ -85,13 +84,6 @@ class ConnectionHandlersZeroconf(ConnectionSocks5BytestreamZeroconf, fjid = str(stanza.getFrom()) - if fjid is None: - for key in self.connection.zeroconf.contacts: - if ip == self.connection.zeroconf.contacts[key][ - Constant.ADDRESS]: - fjid = key - break - jid, resource = app.get_room_and_nick_from_fjid(fjid) thread_id = stanza.getThread() @@ -132,9 +124,8 @@ class ConnectionHandlersZeroconf(ConnectionSocks5BytestreamZeroconf, app.plugin_manager.extension_point( 'decrypt', self, event, self._on_message_decrypted) if not event.encrypted: - eme = parse_eme(event.stanza) - if eme is not None: - event.msgtxt = eme + if properties.eme is not None: + event.msgtxt = get_eme_message(properties.eme) self._on_message_decrypted(event) def _on_message_decrypted(self, event):