Add room pill bridging. Fixes #62
This commit is contained in:
@@ -168,7 +168,7 @@ class IntentAPI:
|
|||||||
self.mxid = mxid
|
self.mxid = mxid
|
||||||
self.log = log
|
self.log = log
|
||||||
|
|
||||||
results = self.mxid_regex.search(mxid)
|
results = self.mxid_regex.match(mxid)
|
||||||
if not results:
|
if not results:
|
||||||
raise ValueError("invalid MXID")
|
raise ValueError("invalid MXID")
|
||||||
self.localpart = results.group(1)
|
self.localpart = results.group(1)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import logging
|
|||||||
|
|
||||||
from telethon.tl.types import *
|
from telethon.tl.types import *
|
||||||
|
|
||||||
from .. import user as u, puppet as p
|
from .. import user as u, puppet as pu, portal as po
|
||||||
from ..db import Message as DBMessage
|
from ..db import Message as DBMessage
|
||||||
from .util import add_surrogates, remove_surrogates
|
from .util import add_surrogates, remove_surrogates
|
||||||
|
|
||||||
@@ -30,7 +30,8 @@ log = logging.getLogger("mau.fmt.mx")
|
|||||||
|
|
||||||
|
|
||||||
class MatrixParser(HTMLParser):
|
class MatrixParser(HTMLParser):
|
||||||
mention_regex = re.compile("https://matrix.to/#/(@.+)")
|
mention_regex = re.compile("https://matrix.to/#/(@.+:.+)")
|
||||||
|
room_regex = re.compile("https://matrix.to/#/(#.+:.+)")
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -69,10 +70,11 @@ class MatrixParser(HTMLParser):
|
|||||||
url = attrs["href"]
|
url = attrs["href"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
mention = self.mention_regex.search(url)
|
mention = self.mention_regex.match(url)
|
||||||
|
room = self.room_regex.match(url)
|
||||||
if mention:
|
if mention:
|
||||||
mxid = mention.group(1)
|
mxid = mention.group(1)
|
||||||
user = p.Puppet.get_by_mxid(mxid, create=False)
|
user = pu.Puppet.get_by_mxid(mxid, create=False)
|
||||||
if not user:
|
if not user:
|
||||||
user = u.User.get_by_mxid(mxid, create=False)
|
user = u.User.get_by_mxid(mxid, create=False)
|
||||||
if not user:
|
if not user:
|
||||||
@@ -83,6 +85,12 @@ class MatrixParser(HTMLParser):
|
|||||||
else:
|
else:
|
||||||
entity_type = MessageEntityMentionName
|
entity_type = MessageEntityMentionName
|
||||||
args["user_id"] = user.tgid
|
args["user_id"] = user.tgid
|
||||||
|
elif room:
|
||||||
|
username = po.Portal.get_username_from_mx_alias(room.group(1))
|
||||||
|
portal = po.Portal.find_by_username(username)
|
||||||
|
if portal and portal.username:
|
||||||
|
url = f"@{portal.username}"
|
||||||
|
entity_type = MessageEntityMention
|
||||||
elif url.startswith("mailto:"):
|
elif url.startswith("mailto:"):
|
||||||
url = url[len("mailto:"):]
|
url = url[len("mailto:"):]
|
||||||
entity_type = MessageEntityEmail
|
entity_type = MessageEntityEmail
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import random
|
|||||||
import mimetypes
|
import mimetypes
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
import magic
|
import magic
|
||||||
|
|
||||||
@@ -44,6 +45,9 @@ class Portal:
|
|||||||
az = None
|
az = None
|
||||||
bot = None
|
bot = None
|
||||||
bridge_notices = False
|
bridge_notices = False
|
||||||
|
mx_alias_regex = None
|
||||||
|
hs_domain = None
|
||||||
|
mxid_regex = None
|
||||||
by_mxid = {}
|
by_mxid = {}
|
||||||
by_tgid = {}
|
by_tgid = {}
|
||||||
|
|
||||||
@@ -278,9 +282,7 @@ class Portal:
|
|||||||
return levels
|
return levels
|
||||||
|
|
||||||
def _get_room_alias(self, username=None):
|
def _get_room_alias(self, username=None):
|
||||||
username = username or self.username
|
return self.alias_template.format(groupname=username or self.username)
|
||||||
return config.get("bridge.alias_template", "telegram_{groupname}").format(
|
|
||||||
groupname=username)
|
|
||||||
|
|
||||||
async def sync_telegram_users(self, source, users):
|
async def sync_telegram_users(self, source, users):
|
||||||
allowed_tgids = set()
|
allowed_tgids = set()
|
||||||
@@ -1110,6 +1112,28 @@ class Portal:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_username_from_mx_alias(cls, alias):
|
||||||
|
match = cls.mx_alias_regex.match(alias)
|
||||||
|
if match:
|
||||||
|
return match.group(1)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def find_by_username(cls, username):
|
||||||
|
if not username:
|
||||||
|
return None
|
||||||
|
|
||||||
|
for _, portal in cls.by_tgid.items():
|
||||||
|
if portal.username == username:
|
||||||
|
return portal
|
||||||
|
|
||||||
|
portal = DBPortal.query.filter(DBPortal.username == username).one_or_none()
|
||||||
|
if portal:
|
||||||
|
return cls.from_db(portal)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_tgid(cls, tgid, tg_receiver=None, peer_type=None):
|
def get_by_tgid(cls, tgid, tg_receiver=None, peer_type=None):
|
||||||
tg_receiver = tg_receiver or tgid
|
tg_receiver = tg_receiver or tgid
|
||||||
@@ -1163,3 +1187,7 @@ def init(context):
|
|||||||
global config
|
global config
|
||||||
Portal.az, Portal.db, config, _, Portal.bot = context
|
Portal.az, Portal.db, config, _, Portal.bot = context
|
||||||
Portal.bridge_notices = config["bridge.bridge_notices"]
|
Portal.bridge_notices = config["bridge.bridge_notices"]
|
||||||
|
Portal.alias_template = config.get("bridge.alias_template", "telegram_{groupname}")
|
||||||
|
Portal.hs_domain = config["homeserver"]["domain"]
|
||||||
|
localpart = Portal.alias_template.format(groupname="(.+)")
|
||||||
|
Portal.mx_alias_regex = re.compile(f"#{localpart}:{Portal.hs_domain}")
|
||||||
|
|||||||
Reference in New Issue
Block a user