Esempio n. 1
0
 async def apply_relay_message_format(
     self, sender: br.BaseUser, content: MessageEventContent
 ) -> None:
     if self.relay_formatted_body and content.get("format", None) != Format.HTML:
         content["format"] = Format.HTML
         content["formatted_body"] = html.escape(content.body).replace("\n", "<br/>")
     tpl = self.bridge.config["bridge.relay.message_formats"].get(
         content.msgtype.value, "$sender_displayname: $message"
     )
     displayname = await self.get_displayname(sender)
     username, _ = self.az.intent.parse_user_id(sender.mxid)
     tpl_args = {
         "sender_mxid": sender.mxid,
         "sender_username": username,
         "sender_displayname": html.escape(displayname),
         "formatted_body": content["formatted_body"],
         "body": content.body,
         "message": content.body,
     }
     content.body = Template(tpl).safe_substitute(tpl_args)
     if self.relay_formatted_body and "formatted_body" in content:
         tpl_args["message"] = content["formatted_body"]
         content["formatted_body"] = Template(tpl).safe_substitute(tpl_args)
     if self.relay_emote_to_text and content.msgtype == MessageType.EMOTE:
         content.msgtype = MessageType.TEXT
Esempio n. 2
0
    async def _apply_msg_format(self, sender: 'u.User',
                                content: MessageEventContent) -> None:
        if isinstance(
                content,
                TextMessageEventContent) and content.format != Format.HTML:
            content.format = Format.HTML
            content.formatted_body = escape_html(content.body).replace(
                "\n", "<br/>")

        tpl = (self.get_config(f"message_formats.[{content.msgtype.value}]")
               or "<b>$sender_displayname</b>: $message")
        displayname = await self.get_displayname(sender)
        tpl_args = dict(sender_mxid=sender.mxid,
                        sender_username=sender.mxid_localpart,
                        sender_displayname=escape_html(displayname),
                        body=content.body)
        if isinstance(content, TextMessageEventContent):
            tpl_args["formatted_body"] = content.formatted_body
            tpl_args["message"] = content.formatted_body
            content.formatted_body = Template(tpl).safe_substitute(tpl_args)
        else:
            tpl_args["message"] = content.body
            content.body = Template(tpl).safe_substitute(tpl_args)