Make it work
This commit is contained in:
+4
-1
@@ -325,7 +325,10 @@ telegram:
|
|||||||
logging:
|
logging:
|
||||||
version: 1
|
version: 1
|
||||||
formatters:
|
formatters:
|
||||||
precise:
|
colored:
|
||||||
|
(): mautrix_telegram.util.ColorFormatter
|
||||||
|
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
|
||||||
|
normal:
|
||||||
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
|
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
|
||||||
handlers:
|
handlers:
|
||||||
file:
|
file:
|
||||||
|
|||||||
@@ -91,9 +91,12 @@ class TelegramBridge(Bridge):
|
|||||||
init_portal(context)
|
init_portal(context)
|
||||||
puppet_startup = init_puppet(context)
|
puppet_startup = init_puppet(context)
|
||||||
user_startup = init_user(context)
|
user_startup = init_user(context)
|
||||||
self.startup_actions = chain(puppet_startup, user_startup,
|
bot_startup = [self.bot.start()] if self.bot else []
|
||||||
[self.bot.start] if self.bot else [])
|
self.startup_actions = chain(puppet_startup, user_startup, bot_startup)
|
||||||
|
|
||||||
async def stop(self) -> None:
|
async def stop(self) -> None:
|
||||||
self.shutdown_actions = [user.stop() for user in User.by_tgid.values()]
|
self.shutdown_actions = [user.stop() for user in User.by_tgid.values()]
|
||||||
await super().stop()
|
await super().stop()
|
||||||
|
|
||||||
|
|
||||||
|
TelegramBridge().run()
|
||||||
|
|||||||
@@ -65,10 +65,10 @@ class CommandHandler(BaseCommandHandler):
|
|||||||
needs_matrix_puppeting: bool
|
needs_matrix_puppeting: bool
|
||||||
needs_admin: bool
|
needs_admin: bool
|
||||||
|
|
||||||
def __init__(self, handler: Callable[[CommandEvent], Awaitable[EventID]], needs_auth: bool,
|
def __init__(self, handler: Callable[[CommandEvent], Awaitable[EventID]],
|
||||||
needs_puppeting: bool, needs_matrix_puppeting: bool, needs_admin: bool,
|
|
||||||
management_only: bool, name: str, help_text: str, help_args: str,
|
management_only: bool, name: str, help_text: str, help_args: str,
|
||||||
help_section: HelpSection) -> None:
|
help_section: HelpSection, needs_auth: bool, needs_puppeting: bool,
|
||||||
|
needs_matrix_puppeting: bool, needs_admin: bool,) -> None:
|
||||||
super().__init__(handler, management_only, name, help_text, help_args, help_section,
|
super().__init__(handler, management_only, name, help_text, help_args, help_section,
|
||||||
needs_auth=needs_auth, needs_puppeting=needs_puppeting,
|
needs_auth=needs_auth, needs_puppeting=needs_puppeting,
|
||||||
needs_matrix_puppeting=needs_matrix_puppeting, needs_admin=needs_admin)
|
needs_matrix_puppeting=needs_matrix_puppeting, needs_admin=needs_admin)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from .deduplication import init as init_dedup
|
|||||||
from ..context import Context
|
from ..context import Context
|
||||||
|
|
||||||
|
|
||||||
class Portal(BasePortal, PortalMatrix, PortalTelegram, PortalMetadata):
|
class Portal(PortalMatrix, PortalTelegram, PortalMetadata):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,13 @@ from .base import BasePortal
|
|||||||
from .portal_matrix import PortalMatrix
|
from .portal_matrix import PortalMatrix
|
||||||
from .portal_metadata import PortalMetadata
|
from .portal_metadata import PortalMetadata
|
||||||
from .portal_telegram import PortalTelegram
|
from .portal_telegram import PortalTelegram
|
||||||
|
from ..context import Context
|
||||||
|
|
||||||
Portal = Union[BasePortal, PortalMatrix, PortalMetadata, PortalTelegram]
|
Portal = Union[BasePortal, PortalMatrix, PortalMetadata, PortalTelegram]
|
||||||
|
|
||||||
|
|
||||||
|
def init(context: Context) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["Portal", "init"]
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ class BasePortal(ABC):
|
|||||||
self.photo_id = photo_id
|
self.photo_id = photo_id
|
||||||
self.local_config = json.loads(local_config or "{}")
|
self.local_config = json.loads(local_config or "{}")
|
||||||
self._db_instance = db_instance
|
self._db_instance = db_instance
|
||||||
|
self._main_intent = None
|
||||||
self.deleted = False
|
self.deleted = False
|
||||||
self.log = self.base_log.getChild(self.tgid_log) if self.tgid else self.base_log
|
self.log = self.base_log.getChild(self.tgid_log) if self.tgid else self.base_log
|
||||||
|
|
||||||
@@ -302,9 +303,9 @@ class BasePortal(ABC):
|
|||||||
config=json.dumps(self.local_config))
|
config=json.dumps(self.local_config))
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
self.db_instance.update(mxid=self.mxid, username=self.username, title=self.title,
|
self.db_instance.edit(mxid=self.mxid, username=self.username, title=self.title,
|
||||||
about=self.about, photo_id=self.photo_id,
|
about=self.about, photo_id=self.photo_id,
|
||||||
config=json.dumps(self.local_config))
|
config=json.dumps(self.local_config))
|
||||||
|
|
||||||
def delete(self) -> None:
|
def delete(self) -> None:
|
||||||
try:
|
try:
|
||||||
@@ -321,11 +322,11 @@ class BasePortal(ABC):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_db(cls, db_portal: DBPortal) -> 'Portal':
|
def from_db(cls, db_portal: DBPortal) -> 'Portal':
|
||||||
return Portal(tgid=db_portal.tgid, tg_receiver=db_portal.tg_receiver,
|
return cls(tgid=db_portal.tgid, tg_receiver=db_portal.tg_receiver,
|
||||||
peer_type=db_portal.peer_type, mxid=db_portal.mxid,
|
peer_type=db_portal.peer_type, mxid=db_portal.mxid,
|
||||||
username=db_portal.username, megagroup=db_portal.megagroup,
|
username=db_portal.username, megagroup=db_portal.megagroup,
|
||||||
title=db_portal.title, about=db_portal.about, photo_id=db_portal.photo_id,
|
title=db_portal.title, about=db_portal.about, photo_id=db_portal.photo_id,
|
||||||
local_config=db_portal.config, db_instance=db_portal)
|
local_config=db_portal.config, db_instance=db_portal)
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
# region Class instance lookup
|
# region Class instance lookup
|
||||||
@@ -380,7 +381,7 @@ class BasePortal(ABC):
|
|||||||
return cls.from_db(db_portal)
|
return cls.from_db(db_portal)
|
||||||
|
|
||||||
if peer_type:
|
if peer_type:
|
||||||
portal = Portal(tgid, peer_type=peer_type, tg_receiver=tg_receiver)
|
portal = cls(tgid, peer_type=peer_type, tg_receiver=tg_receiver)
|
||||||
portal.db_instance.insert()
|
portal.db_instance.insert()
|
||||||
return portal
|
return portal
|
||||||
|
|
||||||
@@ -465,14 +466,14 @@ class BasePortal(ABC):
|
|||||||
|
|
||||||
def init(context: Context) -> None:
|
def init(context: Context) -> None:
|
||||||
global config
|
global config
|
||||||
Portal.az, config, Portal.loop, Portal.bot = context.core
|
BasePortal.az, config, BasePortal.loop, BasePortal.bot = context.core
|
||||||
Portal.max_initial_member_sync = config["bridge.max_initial_member_sync"]
|
BasePortal.max_initial_member_sync = config["bridge.max_initial_member_sync"]
|
||||||
Portal.sync_channel_members = config["bridge.sync_channel_members"]
|
BasePortal.sync_channel_members = config["bridge.sync_channel_members"]
|
||||||
Portal.sync_matrix_state = config["bridge.sync_matrix_state"]
|
BasePortal.sync_matrix_state = config["bridge.sync_matrix_state"]
|
||||||
Portal.public_portals = config["bridge.public_portals"]
|
BasePortal.public_portals = config["bridge.public_portals"]
|
||||||
Portal.filter_mode = config["bridge.filter.mode"]
|
BasePortal.filter_mode = config["bridge.filter.mode"]
|
||||||
Portal.filter_list = config["bridge.filter.list"]
|
BasePortal.filter_list = config["bridge.filter.list"]
|
||||||
Portal.alias_template = config.get("bridge.alias_template", "telegram_{groupname}")
|
BasePortal.alias_template = config.get("bridge.alias_template", "telegram_{groupname}")
|
||||||
Portal.hs_domain = config["homeserver.domain"]
|
BasePortal.hs_domain = config["homeserver.domain"]
|
||||||
Portal.mx_alias_regex = re.compile(
|
BasePortal.mx_alias_regex = re.compile(
|
||||||
f"#{Portal.alias_template.format(groupname='(.+)')}:{Portal.hs_domain}")
|
f"#{BasePortal.alias_template.format(groupname='(.+)')}:{BasePortal.hs_domain}")
|
||||||
|
|||||||
@@ -491,8 +491,8 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC):
|
|||||||
if entity:
|
if entity:
|
||||||
break
|
break
|
||||||
if not entity:
|
if not entity:
|
||||||
self.log.error(
|
self.log.error("Failed to fully migrate to upgraded Matrix room: "
|
||||||
"Failed to fully migrate to upgraded Matrix room: no Telegram user found.")
|
"no Telegram user found.")
|
||||||
return
|
return
|
||||||
await self.update_matrix_room(user, entity, direct=self.peer_type == "user")
|
await self.update_matrix_room(user, entity, direct=self.peer_type == "user")
|
||||||
self.log.info(f"Upgraded room from {old_room} to {self.mxid}")
|
self.log.info(f"Upgraded room from {old_room} to {self.mxid}")
|
||||||
@@ -503,7 +503,7 @@ class PortalMatrix(BasePortal, MautrixBasePortal, ABC):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
self.mxid = new_id
|
self.mxid = new_id
|
||||||
self.db_instance.update(mxid=self.mxid)
|
self.db_instance.edit(mxid=self.mxid)
|
||||||
self.by_mxid[self.mxid] = self
|
self.by_mxid[self.mxid] = self
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ class PortalMetadata(BasePortal, ABC):
|
|||||||
existing.delete()
|
existing.delete()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
self.db_instance.update(tgid=new_id, tg_receiver=new_id, peer_type=self.peer_type)
|
self.db_instance.edit(tgid=new_id, tg_receiver=new_id, peer_type=self.peer_type)
|
||||||
old_id = self.tgid
|
old_id = self.tgid
|
||||||
self.tgid = new_id
|
self.tgid = new_id
|
||||||
self.tg_receiver = new_id
|
self.tg_receiver = new_id
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class FakeLock:
|
|||||||
|
|
||||||
class PortalSendLock:
|
class PortalSendLock:
|
||||||
_send_locks: Dict[int, Lock]
|
_send_locks: Dict[int, Lock]
|
||||||
_noop_lock: Lock = FakeLock
|
_noop_lock: Lock = FakeLock()
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._send_locks = {}
|
self._send_locks = {}
|
||||||
|
|||||||
@@ -152,11 +152,11 @@ class Puppet(CustomPuppetMixin):
|
|||||||
db_puppet.disable_updates, db_instance=db_puppet)
|
db_puppet.disable_updates, db_instance=db_puppet)
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
self.db_instance.update(access_token=self.access_token, custom_mxid=self.custom_mxid,
|
self.db_instance.edit(access_token=self.access_token, custom_mxid=self.custom_mxid,
|
||||||
username=self.username, displayname=self.displayname,
|
username=self.username, displayname=self.displayname,
|
||||||
displayname_source=self.displayname_source, photo_id=self.photo_id,
|
displayname_source=self.displayname_source, photo_id=self.photo_id,
|
||||||
is_bot=self.is_bot, matrix_registered=self.is_registered,
|
is_bot=self.is_bot, matrix_registered=self.is_registered,
|
||||||
disable_updates=self.disable_updates)
|
disable_updates=self.disable_updates)
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
# region Info updating
|
# region Info updating
|
||||||
@@ -405,6 +405,6 @@ def init(context: 'Context') -> Iterable[Awaitable[Any]]:
|
|||||||
index = displayname_template.index("{displayname}")
|
index = displayname_template.index("{displayname}")
|
||||||
length = len("{displayname}")
|
length = len("{displayname}")
|
||||||
Puppet._displayname_prefix = displayname_template[:index]
|
Puppet._displayname_prefix = displayname_template[:index]
|
||||||
Puppet._displayname_suffix = displayname_template[index+length:]
|
Puppet._displayname_suffix = displayname_template[index + length:]
|
||||||
|
|
||||||
return (puppet.start() for puppet in Puppet.all_with_custom_mxid())
|
return (puppet.start() for puppet in Puppet.all_with_custom_mxid())
|
||||||
|
|||||||
@@ -145,8 +145,8 @@ class User(AbstractUser):
|
|||||||
saved_contacts=self.saved_contacts, portals=self.db_portals)
|
saved_contacts=self.saved_contacts, portals=self.db_portals)
|
||||||
|
|
||||||
def save(self, contacts: bool = False, portals: bool = False) -> None:
|
def save(self, contacts: bool = False, portals: bool = False) -> None:
|
||||||
self.db_instance.update(tgid=self.tgid, tg_username=self.username, tg_phone=self.phone,
|
self.db_instance.edit(tgid=self.tgid, tg_username=self.username, tg_phone=self.phone,
|
||||||
saved_contacts=self.saved_contacts)
|
saved_contacts=self.saved_contacts)
|
||||||
if contacts:
|
if contacts:
|
||||||
self.db_instance.contacts = self.db_contacts
|
self.db_instance.contacts = self.db_contacts
|
||||||
if portals:
|
if portals:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
from .file_transfer import transfer_file_to_matrix, convert_image
|
from .file_transfer import transfer_file_to_matrix, convert_image
|
||||||
from .format_duration import format_duration
|
from .format_duration import format_duration
|
||||||
from .recursive_dict import recursive_del, recursive_set, recursive_get
|
from .recursive_dict import recursive_del, recursive_set, recursive_get
|
||||||
|
from .color_log import ColorFormatter
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# mautrix-telegram - A Matrix-Telegram puppeting bridge
|
||||||
|
# Copyright (C) 2019 Tulir Asokan
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
from mautrix.util.color_log import ColorFormatter as BaseColorFormatter, PREFIX, MXID_COLOR, RESET
|
||||||
|
|
||||||
|
TELETHON_COLOR = PREFIX + "35;1m" # magenta
|
||||||
|
TELETHON_MODULE_COLOR = PREFIX + "35m"
|
||||||
|
|
||||||
|
|
||||||
|
class ColorFormatter(BaseColorFormatter):
|
||||||
|
def _color_name(self, module: str) -> str:
|
||||||
|
if module.startswith("telethon"):
|
||||||
|
prefix, user_id, module = module.split(".", 2)
|
||||||
|
return (f"{TELETHON_COLOR}{prefix}{RESET}."
|
||||||
|
f"{MXID_COLOR}{user_id}{RESET}."
|
||||||
|
f"{TELETHON_MODULE_COLOR}{module}{RESET}")
|
||||||
|
return super()._color_name(module)
|
||||||
Reference in New Issue
Block a user