Merge pull request #152 from turt2live/travis/display_name

Add configuration for basic message formats
This commit is contained in:
Tulir Asokan
2018-06-08 12:01:14 +03:00
committed by GitHub
3 changed files with 37 additions and 5 deletions
+12
View File
@@ -115,6 +115,18 @@ bridge:
# WARNING: Probably buggy, might get stuck in infinite loop. # WARNING: Probably buggy, might get stuck in infinite loop.
catch_up: false catch_up: false
# The formats to use when sending messages to Telegram via the relay bot.
# Available variables:
# $sender_display_name - The display name of the sender
# $message - The message text itself
message_formats:
m_text:
plain: "<$sender_display_name> $message"
html: "&lt;$sender_display_name&gt; $message"
m_emote:
plain: "* $sender_display_name $message"
html: "* $sender_display_name $message"
filter: filter:
# Filter mode to use. Either "blacklist" or "whitelist". # Filter mode to use. Either "blacklist" or "whitelist".
# If the mode is "blacklist", the listed chats will never be bridged. An empty blacklist disables the filter. # If the mode is "blacklist", the listed chats will never be bridged. An empty blacklist disables the filter.
+5
View File
@@ -181,6 +181,11 @@ class Config(DictWithRecursion):
copy("bridge.native_stickers") copy("bridge.native_stickers")
copy("bridge.catch_up") copy("bridge.catch_up")
copy("bridge.message_formats.m_text.plain")
copy("bridge.message_formats.m_text.html")
copy("bridge.message_formats.m_emote.plain")
copy("bridge.message_formats.m_emote.html")
copy("bridge.filter.mode") copy("bridge.filter.mode")
copy("bridge.filter.list") copy("bridge.filter.list")
+20 -5
View File
@@ -16,6 +16,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from collections import deque from collections import deque
from datetime import datetime from datetime import datetime
from string import Template
from html import escape as escape_html
import asyncio import asyncio
import random import random
import mimetypes import mimetypes
@@ -656,16 +658,28 @@ class Portal:
msgtype = message["msgtype"] msgtype = message["msgtype"]
if msgtype == "m.emote": if msgtype == "m.emote":
if "formatted_body" in message: if "formatted_body" in message:
message["formatted_body"] = f"* {sender.displayname} {message['formatted_body']}" tpl = config["bridge.message_formats.m_emote.html"]
message["body"] = f"* {sender.displayname} {message['body']}" tpl_args = dict(sender_display_name=sender.displayname,
message=message['formatted_body'])
message["formatted_body"] = Template(tpl).safe_substitute(tpl_args)
tpl = config["bridge.message_formats.m_emote.plain"]
tpl_args = dict(sender_display_name=sender.displayname, message=message['body'])
message["body"] = Template(tpl).safe_substitute(tpl_args)
message["msgtype"] = "m.text" message["msgtype"] = "m.text"
elif not sender.logged_in: elif not sender.logged_in:
html = message["formatted_body"] if "formatted_body" in message else None html = message["formatted_body"] if "formatted_body" in message else None
text = message["body"] text = message["body"]
if msgtype == "m.text": if msgtype == "m.text":
if html: # We use the plain text as HTML if available to ensure that we represent
html = f"&lt;{sender.displayname}&gt; {html}" # the event consistently.
text = f"<{sender.displayname}> {text}" if not html:
html = escape_html(text)
tpl = config["bridge.message_formats.m_text.html"]
tpl_args = dict(sender_display_name=sender.displayname,message=html)
html = Template(tpl).safe_substitute(tpl_args)
tpl = config["bridge.message_formats.m_text.plain"]
tpl_args = dict(sender_display_name=sender.displayname, message=text)
text = Template(tpl).safe_substitute(tpl_args)
else: else:
msgtype = msgtype[len("m."):] msgtype = msgtype[len("m."):]
prefix = { prefix = {
@@ -681,6 +695,7 @@ class Portal:
text = f"{sender.displayname} sent {prefix}{msgtype}{text}" text = f"{sender.displayname} sent {prefix}{msgtype}{text}"
if html: if html:
message["formatted_body"] = html message["formatted_body"] = html
message["format"] = "org.matrix.custom.html"
message["body"] = text message["body"] = text
async def _matrix_event_to_entities(self, client, event): async def _matrix_event_to_entities(self, client, event):