echo_bot: Don't pass the whole message to plain_reply and encrypted_reply anymore

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2021-07-12 19:29:09 +02:00
parent cea2345f29
commit ba028d98c8
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -77,25 +77,27 @@ class EchoBot(ClientXMPP):
for stanza objects and the Message stanza to see
how it may be used.
"""
mfrom = mto = msg['from']
mtype = msg['type']
if msg['type'] not in ('chat', 'normal'):
if mtype not in ('chat', 'normal'):
return None
if not self['xep_0384'].is_encrypted(msg):
await self.plain_reply(msg, 'Echo unencrypted message:%(body)s' % msg)
await self.plain_reply(mto, mtype, 'Echo unencrypted message:%(body)s' % msg)
return None
try:
mfrom = msg['from']
encrypted = msg['omemo_encrypted']
body = self['xep_0384'].decrypt_message(encrypted, mfrom, allow_untrusted)
await self.encrypted_reply(msg, 'Echo: %s' % body.decode("utf8"))
decoded = body.decode('utf8')
await self.encrypted_reply(mto, mtype, 'Echo: %s' % decoded)
return None
except (MissingOwnKey,):
# The message is missing our own key, it was not encrypted for
# us, and we can't decrypt it.
await self.plain_reply(
msg,
mto, mtype,
'Error: Message not encrypted for me.',
)
return None
@ -107,7 +109,7 @@ class EchoBot(ClientXMPP):
# best if we send an encrypted message directly. XXX: Is it
# where we talk about self-healing messages?
await self.encrypted_reply(
msg,
mto, mtype,
'Error: Message uses an encrypted '
'session I don\'t know about.',
)
@ -122,7 +124,7 @@ class EchoBot(ClientXMPP):
# trusted, or in undecided state, if they decide to decrypt it
# anyway.
await self.plain_reply(
msg,
mto, mtype,
"Error: Your device '%s' is not in my trusted devices." % exn.device,
)
# We resend, setting the `allow_untrusted` parameter to True.
@ -140,22 +142,18 @@ class EchoBot(ClientXMPP):
return None
async def plain_reply(self, original_msg, body):
async def plain_reply(self, mto: JID, mtype: str, body):
"""
Helper to reply to messages
"""
mto = original_msg['from']
mtype = original_msg['type']
msg = self.make_message(mto=mto, mtype=mtype)
msg['body'] = body
return msg.send()
async def encrypted_reply(self, original_msg, body):
async def encrypted_reply(self, mto: JID, mtype: str, body):
"""Helper to reply with encrypted messages"""
mto = original_msg['from']
mtype = original_msg['type']
msg = self.make_message(mto=mto, mtype=mtype)
msg['eme']['namespace'] = self.eme_ns
msg['eme']['name'] = self['xep_0380'].mechanisms[self.eme_ns]
@ -201,7 +199,7 @@ class EchoBot(ClientXMPP):
# generic message. The receiving end-user at this
# point can bring up the issue if it happens.
self.plain_reply(
original_msg,
mto, mtype,
'Could not find keys for device "%d" of recipient "%s". Skipping.' %
(error.device, error.bare_jid),
)
@ -210,13 +208,13 @@ class EchoBot(ClientXMPP):
device_list.append(error.device)
except (IqError, IqTimeout) as exn:
self.plain_reply(
original_msg,
mto, mfrom,
'An error occured while fetching information on a recipient.\n%r' % exn,
)
return None
except Exception as exn:
await self.plain_reply(
original_msg,
mto, mfrom,
'An error occured while attempting to encrypt.\n%r' % exn,
)
raise