summaryrefslogtreecommitdiff
path: root/poezio
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2019-08-23 23:26:19 +0200
committerMaxime “pep” Buquet <pep@bouah.net>2019-12-27 18:58:48 +0100
commit1dd713b8577658710e767c9b171d073b2b2877b4 (patch)
treed5963704cbb89929502b29a5858b75cfeb530153 /poezio
parentb1e913d772cf748719ef5313e6655f46855f180d (diff)
downloadpoezio-1dd713b8577658710e767c9b171d073b2b2877b4.tar.gz
poezio-1dd713b8577658710e767c9b171d073b2b2877b4.tar.bz2
poezio-1dd713b8577658710e767c9b171d073b2b2877b4.tar.xz
poezio-1dd713b8577658710e767c9b171d073b2b2877b4.zip
omemo: add a wrapper around _encrypt() to handle user feedback
(yes, another one)
Diffstat (limited to 'poezio')
-rw-r--r--poezio/plugin_e2ee.py34
1 files changed, 31 insertions, 3 deletions
diff --git a/poezio/plugin_e2ee.py b/poezio/plugin_e2ee.py
index ebb31078..94084d41 100644
--- a/poezio/plugin_e2ee.py
+++ b/poezio/plugin_e2ee.py
@@ -23,6 +23,7 @@ from poezio.tabs import (
StaticConversationTab,
)
from poezio.plugin import BasePlugin
+from poezio.theming import get_theme, dump_tuple
from asyncio import iscoroutinefunction
@@ -43,6 +44,12 @@ EME_TAG = 'encryption'
JCLIENT_NS = 'jabber:client'
HINTS_NS = 'urn:xmpp:hints'
+class NothingToEncrypt(Exception):
+ """
+ Exception to raise inside the _encrypt filter on stanzas that do not need
+ to be processed.
+ """
+
class E2EEPlugin(BasePlugin):
"""Interface for E2EE plugins.
@@ -129,7 +136,7 @@ class E2EEPlugin(BasePlugin):
# encrypted is encrypted, and no plain element slips in.
# Using a stream filter might be a bit too much, but at least we're
# sure poezio is not sneaking anything past us.
- self.core.xmpp.add_filter('out', self._encrypt)
+ self.core.xmpp.add_filter('out', self._encrypt_wrapper)
for tab_t in (DynamicConversationTab, StaticConversationTab, PrivateTab, MucTab):
self.api.add_tab_command(
@@ -193,6 +200,27 @@ class E2EEPlugin(BasePlugin):
def _encryption_enabled(self, jid: JID) -> bool:
return jid in self._enabled_tabs and self._enabled_tabs[jid] == self.encrypt
+ async def _encrypt_wrapper(self, stanza: StanzaBase) -> Optional[StanzaBase]:
+ """
+ Wrapper around _encrypt() to handle errors and display the message after encryption.
+ """
+ try:
+ result = await self._encrypt(stanza, passthrough=True)
+ except NothingToEncrypt:
+ return stanza
+ except Exception as exc:
+ jid = stanza['to']
+ tab = self.core.tabs.by_name_and_class(jid, ChatTab)
+ msg = ' \n\x19%s}Could not send message: %s' % (
+ dump_tuple(get_theme().COLOR_CHAR_NACK),
+ exc,
+ )
+ tab.nack_message(msg, stanza['id'], stanza['from'])
+ # TODO: display exceptions to the user properly
+ log.error('Exception in encrypt:', exc_info=True)
+ return None
+ return result
+
def _decrypt(self, message: Message, tab: ChatTabs) -> None:
has_eme = False
@@ -219,13 +247,13 @@ class E2EEPlugin(BasePlugin):
async def _encrypt(self, stanza: StanzaBase) -> Optional[StanzaBase]:
if not isinstance(stanza, Message) or stanza['type'] not in ('chat', 'groupchat'):
- return stanza
+ raise NothingToEncrypt()
message = stanza
jid = stanza['to']
tab = self.core.tabs.by_name_and_class(jid, ChatTab)
if not self._encryption_enabled(jid):
- return message
+ raise NothingToEncrypt()
log.debug('Sending %s message: %r', self.encryption_name, message)