example-bot: attempt at returning encrypted messages
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
af8c731307
commit
9e70954a0e
1 changed files with 25 additions and 2 deletions
|
@ -36,6 +36,8 @@ class EchoBot(ClientXMPP):
|
|||
slixmpp repository.
|
||||
"""
|
||||
|
||||
eme_ns = 'eu.siacs.conversations.axolotl'
|
||||
|
||||
def __init__(self, jid, password):
|
||||
ClientXMPP.__init__(self, jid, password)
|
||||
|
||||
|
@ -140,9 +142,30 @@ class EchoBot(ClientXMPP):
|
|||
async def encrypted_reply(self, original_msg, body) -> None:
|
||||
"""Helper to reply with encrypted messages"""
|
||||
|
||||
# TODO: Send the message encrypted.
|
||||
return await self.plain_reply(msg, body)
|
||||
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]
|
||||
|
||||
while True:
|
||||
try:
|
||||
# `encrypt_message` excepts the plaintext to be sent, a list of
|
||||
# bare JIDs to encrypt to, and optionally a dict of problems to
|
||||
# expect per bare JID.
|
||||
recipients = [mto]
|
||||
encrypt = await self['xep_0384'].encrypt_message(body, recipients)
|
||||
msg.append(encrypt)
|
||||
return msg.send()
|
||||
except UndecidedException as exn:
|
||||
# The library prevents us from sending a message to an
|
||||
# untrusted/undecided barejid, so we need to make a decision here.
|
||||
# This is where you prompt your user to ask what to do.
|
||||
self['xep_0384'].trust(exn.bare_jid, exn.device, exn.ik)
|
||||
except Exception as exn:
|
||||
break
|
||||
|
||||
return None
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Setup the command line arguments.
|
||||
|
|
Loading…
Reference in a new issue