From 1633ff006c59bb77bb3d4bf0c90cb240ea3b63f6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 27 Dec 2019 01:34:26 +0100 Subject: [PATCH] Escape RoyalCode in Matrix messages --- royalnet/serf/matrix/__init__.py | 4 +++- royalnet/serf/matrix/escape.py | 15 +++++++++++++++ royalnet/serf/matrix/matrixserf.py | 3 ++- royalnet/version.py | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 royalnet/serf/matrix/escape.py diff --git a/royalnet/serf/matrix/__init__.py b/royalnet/serf/matrix/__init__.py index 09d99dcd..534814a9 100644 --- a/royalnet/serf/matrix/__init__.py +++ b/royalnet/serf/matrix/__init__.py @@ -1,5 +1,7 @@ from .matrixserf import MatrixSerf +from .escape import escape __all__ = [ "MatrixSerf", -] \ No newline at end of file + "escape", +] diff --git a/royalnet/serf/matrix/escape.py b/royalnet/serf/matrix/escape.py new file mode 100644 index 00000000..738b3e7c --- /dev/null +++ b/royalnet/serf/matrix/escape.py @@ -0,0 +1,15 @@ +def escape(string: str) -> str: + """Escape a string to be sent through Matrix, and format it using RoyalCode. + + Underlines are currently unsupported. + + Warning: + Currently escapes everything, even items in code blocks.""" + return string.replace("[b]", "**") \ + .replace("[/b]", "**") \ + .replace("[i]", "_") \ + .replace("[/i]", "_") \ + .replace("[c]", "`") \ + .replace("[/c]", "`") \ + .replace("[p]", "```") \ + .replace("[/p]", "```") diff --git a/royalnet/serf/matrix/matrixserf.py b/royalnet/serf/matrix/matrixserf.py index adf3f8a0..f826a07a 100644 --- a/royalnet/serf/matrix/matrixserf.py +++ b/royalnet/serf/matrix/matrixserf.py @@ -6,6 +6,7 @@ import royalnet.backpack as rb import royalnet.commands as rc import royalnet.utils as ru from ..serf import Serf +from .escape import escape try: @@ -77,7 +78,7 @@ class MatrixSerf(Serf): async def reply(data, text: str): await self.client.room_send(room_id=data.room.room_id, message_type="m.room.message", content={ "msgtype": "m.text", - "body": text + "body": escape(text) }) async def get_author(data, error_if_none=False): diff --git a/royalnet/version.py b/royalnet/version.py index c45e457a..286c364d 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.2.1" +semantic = "5.2.2"