diff --git a/example-config.yaml b/example-config.yaml index 27c64991..6f9e8df5 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -33,6 +33,17 @@ appservice: # implicitly. external: https://example.com/public + # Provisioning API part of the web server for automated portal creation and fetching information. + # Used by things like Dimension (https://dimension.t2bot.io/). + provisioning: + # Whether or not the provisioning API should be enabled. + enabled: true + # The prefix to use in the provisioning API endpoints. + prefix: /_matrix/provision + # The shared secret to authorize users of the API. + # You can generate a decent secret with `pwgen -snc 32 1` + shared_secret: "Very secret shared secret" + # The unique ID of this appservice. id: telegram # Username of the appservice bot. diff --git a/mautrix_telegram/__main__.py b/mautrix_telegram/__main__.py index 8fce6699..84d01698 100644 --- a/mautrix_telegram/__main__.py +++ b/mautrix_telegram/__main__.py @@ -39,6 +39,7 @@ from .portal import init as init_portal from .puppet import init as init_puppet from .formatter import init as init_formatter from .public import PublicBridgeWebsite +from .provisioning_api import ProvisioningAPI from .context import Context parser = argparse.ArgumentParser( @@ -74,9 +75,9 @@ db_factory = orm.sessionmaker(bind=db_engine) db_session = orm.scoping.scoped_session(db_factory) Base.metadata.bind = db_engine -telethon_session_container = AlchemySessionContainer(engine=db_engine, session=db_session, - table_base=Base, table_prefix="telethon_", - manage_tables=False) +session_container = AlchemySessionContainer(engine=db_engine, session=db_session, + table_base=Base, table_prefix="telethon_", + manage_tables=False) loop = asyncio.get_event_loop() @@ -85,11 +86,16 @@ appserv = AppService(config["homeserver.address"], config["homeserver.domain"], config["appservice.bot_username"], log="mau.as", loop=loop, verify_ssl=config["homeserver.verify_ssl"]) -context = Context(appserv, db_session, config, loop, None, None, telethon_session_container) +context = Context(appserv, db_session, config, loop, None, None, session_container) if config["appservice.public.enabled"]: public = PublicBridgeWebsite(loop) - appserv.app.add_subapp(config.get("appservice.public.prefix", "/public"), public.app) + appserv.app.add_subapp(config["appservice.public.prefix"] or "/public", public.app) + +if config["appservice.provisioning.enabled"]: + provisioning_api = ProvisioningAPI(loop) + appserv.app.add_subapp(config["appservice.provisioning.prefix"] or "/_matrix/provisioning", + provisioning_api.app) with appserv.run(config["appservice.hostname"], config["appservice.port"]) as start: init_db(db_session) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index dd1fe03b..d15e9e66 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -328,5 +328,5 @@ class AbstractUser: def init(context): global config, MAX_DELETIONS AbstractUser.az, AbstractUser.db, config, AbstractUser.loop, _ = context - AbstractUser.session_container = context.telethon_session_container + AbstractUser.session_container = context.session_container MAX_DELETIONS = config.get("bridge.max_telegram_delete", 10) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index fb2e14a2..07944b3c 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -159,6 +159,10 @@ class Config(DictWithRecursion): copy("appservice.public.prefix") copy("appservice.public.external") + copy("appservice.provisioning.enabled") + copy("appservice.provisioning.prefix") + copy("appservice.provisioning.shared_secret") + copy("appservice.id") copy("appservice.bot_username") copy("appservice.bot_displayname") diff --git a/mautrix_telegram/context.py b/mautrix_telegram/context.py index 530f1eed..dfb32b59 100644 --- a/mautrix_telegram/context.py +++ b/mautrix_telegram/context.py @@ -17,14 +17,14 @@ class Context: - def __init__(self, az, db, config, loop, bot, mx, telethon_session_container): + def __init__(self, az, db, config, loop, bot, mx, session_container): self.az = az self.db = db self.config = config self.loop = loop self.bot = bot self.mx = mx - self.telethon_session_container = telethon_session_container + self.session_container = session_container def __iter__(self): yield self.az diff --git a/mautrix_telegram/provisioning_api.py b/mautrix_telegram/provisioning_api.py new file mode 100644 index 00000000..db89c506 --- /dev/null +++ b/mautrix_telegram/provisioning_api.py @@ -0,0 +1,27 @@ +# -*- coding: future_fstrings -*- +# mautrix-telegram - A Matrix-Telegram puppeting bridge +# Copyright (C) 2018 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 . +from aiohttp import web +import logging + + +class ProvisioningAPI: + log = logging.getLogger("mau.provisioning") + + def __init__(self, loop): + self.loop = loop + + self.app = web.Application(loop=loop)