echo_bot: Use CoroutineCallback rather than Callback
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
dbeaca6b6a
commit
f397b0e8d7
1 changed files with 11 additions and 27 deletions
|
@ -21,7 +21,7 @@ from argparse import ArgumentParser
|
|||
from slixmpp import ClientXMPP, JID
|
||||
from slixmpp.exceptions import IqTimeout, IqError
|
||||
from slixmpp.stanza import Message
|
||||
from slixmpp.xmlstream.handler import Callback
|
||||
from slixmpp.xmlstream.handler import CoroutineCallback
|
||||
from slixmpp.xmlstream.matcher import MatchXPath
|
||||
import slixmpp_omemo
|
||||
from slixmpp_omemo import PluginCouldNotLoad, MissingOwnKey, EncryptionPrepareException
|
||||
|
@ -56,10 +56,9 @@ class EchoBot(ClientXMPP):
|
|||
self.cmd_re: re.Pattern = re.compile('^%s(?P<command>\w+)(?:\s+(?P<args>.*))?' % self.cmd_prefix)
|
||||
|
||||
self.add_event_handler("session_start", self.start)
|
||||
self.add_event_handler("message", self.plain_handler) # Matches any message with body
|
||||
self.register_handler(Callback('Encrypted', # Matches any encrypted message
|
||||
MatchXPath(f'{{{self.default_ns}}}message/{{{self.eme_ns}}}encrypted'),
|
||||
self.encrypted_handler,
|
||||
self.register_handler(CoroutineCallback('Messages',
|
||||
MatchXPath(f'{{{self.default_ns}}}message'),
|
||||
self.message_handler,
|
||||
))
|
||||
|
||||
def start(self, _event) -> None:
|
||||
|
@ -118,27 +117,7 @@ class EchoBot(ClientXMPP):
|
|||
body = '''Debug level set to 'error'.'''
|
||||
return await self.encrypted_reply(mto, mtype, body)
|
||||
|
||||
def plain_handler(self, msg: Message) -> None:
|
||||
asyncio.ensure_future(self.plain_message(msg))
|
||||
|
||||
async def plain_message(self, msg: Message) -> None:
|
||||
if self['xep_0384'].is_encrypted(msg): # Already handled by 'encrypted_handler'
|
||||
return None
|
||||
|
||||
mfrom = mto = msg['from']
|
||||
mtype = msg['type']
|
||||
|
||||
if mtype not in ('chat', 'normal'):
|
||||
return None
|
||||
|
||||
if self.debug_level == LEVEL_DEBUG:
|
||||
await self.plain_reply(mto, mtype, 'Echo unencrypted message:%(body)s' % msg)
|
||||
return None
|
||||
|
||||
def encrypted_handler(self, msg: Message) -> None:
|
||||
asyncio.ensure_future(self.encrypted_message(msg))
|
||||
|
||||
async def encrypted_message(self, msg: Message, allow_untrusted: bool = False) -> None:
|
||||
async def message_handler(self, msg: Message, allow_untrusted: bool = False) -> None:
|
||||
"""
|
||||
Process incoming message stanzas. Be aware that this also
|
||||
includes MUC messages and error messages. It is usually
|
||||
|
@ -156,6 +135,11 @@ class EchoBot(ClientXMPP):
|
|||
if mtype not in ('chat', 'normal'):
|
||||
return None
|
||||
|
||||
if self['xep_0384'].is_encrypted(msg): # Already handled by 'encrypted_handler'
|
||||
if self.debug_level == LEVEL_DEBUG:
|
||||
await self.plain_reply(mto, mtype, 'Echo unencrypted message:%(body)s' % msg)
|
||||
return None
|
||||
|
||||
try:
|
||||
encrypted = msg['omemo_encrypted']
|
||||
body = await self['xep_0384'].decrypt_message(encrypted, mfrom, allow_untrusted)
|
||||
|
@ -205,7 +189,7 @@ class EchoBot(ClientXMPP):
|
|||
"Error: Your device '%s' is not in my trusted devices." % exn.device,
|
||||
)
|
||||
# We resend, setting the `allow_untrusted` parameter to True.
|
||||
await self.message(msg, allow_untrusted=True)
|
||||
await self.message_handler(msg, allow_untrusted=True)
|
||||
return None
|
||||
except (EncryptionPrepareException,):
|
||||
# Slixmpp tried its best, but there were errors it couldn't
|
||||
|
|
Loading…
Reference in a new issue