2022-02-25 02:22:50 -05:00
|
|
|
# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
|
|
|
|
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
|
|
|
|
#
|
|
|
|
# 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 __future__ import annotations
|
|
|
|
|
2022-07-14 01:41:24 -04:00
|
|
|
from typing import Any, Dict
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
from mautrix.bridge import Bridge
|
|
|
|
from mautrix.types import RoomID, UserID
|
|
|
|
|
|
|
|
from .config import Config
|
|
|
|
from .db import init as init_db, upgrade_table
|
|
|
|
from .matrix import MatrixHandler
|
|
|
|
from .portal import Portal
|
|
|
|
from .puppet import Puppet
|
|
|
|
from .user import User
|
|
|
|
from .kt.client import Client as KakaoTalkClient
|
|
|
|
from .version import linkified_version, version
|
2022-04-10 23:40:11 -04:00
|
|
|
#from .web import PublicBridgeWebsite
|
2022-02-25 02:22:50 -05:00
|
|
|
from . import commands as _
|
|
|
|
|
|
|
|
|
|
|
|
class KakaoTalkBridge(Bridge):
|
|
|
|
name = "matrix-appservice-kakaotalk"
|
|
|
|
module = "matrix_appservice_kakaotalk"
|
|
|
|
command = "python -m matrix-appservice-kakaotalk"
|
|
|
|
description = "A Matrix-KakaoTalk puppeting bridge."
|
|
|
|
repo_url = "https://src.miscworks.net/fair/matrix-appservice-kakaotalk"
|
|
|
|
version = version
|
|
|
|
markdown_version = linkified_version
|
|
|
|
config_class = Config
|
|
|
|
matrix_class = MatrixHandler
|
|
|
|
upgrade_table = upgrade_table
|
|
|
|
|
|
|
|
config: Config
|
|
|
|
matrix: MatrixHandler
|
2022-07-14 01:41:24 -04:00
|
|
|
#public_website: Optional[PublicBridgeWebsite] None
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
def prepare_config(self)->None:
|
|
|
|
super().prepare_config()
|
|
|
|
|
|
|
|
def prepare_db(self) -> None:
|
|
|
|
super().prepare_db()
|
|
|
|
init_db(self.db)
|
|
|
|
|
2022-04-10 23:40:11 -04:00
|
|
|
""" TODO Implement web login
|
2022-02-25 02:22:50 -05:00
|
|
|
def prepare_bridge(self) -> None:
|
|
|
|
super().prepare_bridge()
|
|
|
|
if self.config["appservice.public.enabled"]:
|
|
|
|
secret = self.config["appservice.public.shared_secret"]
|
|
|
|
self.public_website = PublicBridgeWebsite(loop=self.loop, shared_secret=secret)
|
|
|
|
self.az.app.add_subapp(
|
|
|
|
self.config["appservice.public.prefix"], self.public_website.app
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.public_website = None
|
2022-04-10 23:40:11 -04:00
|
|
|
"""
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
def prepare_stop(self) -> None:
|
2022-04-08 05:04:46 -04:00
|
|
|
self.log.debug("Stopping RPC connection")
|
|
|
|
KakaoTalkClient.stop_cls()
|
2022-02-25 02:22:50 -05:00
|
|
|
self.log.debug("Stopping puppet syncers")
|
|
|
|
for puppet in Puppet.by_custom_mxid.values():
|
|
|
|
puppet.stop()
|
|
|
|
self.log.debug("Stopping kakaotalk listeners")
|
|
|
|
User.shutdown = True
|
|
|
|
self.add_shutdown_actions(user.save() for user in User.by_mxid.values())
|
|
|
|
|
|
|
|
async def start(self) -> None:
|
2022-04-08 05:04:46 -04:00
|
|
|
KakaoTalkClient.init_cls(self.config)
|
2022-02-25 02:22:50 -05:00
|
|
|
self.add_startup_actions(User.init_cls(self))
|
|
|
|
self.add_startup_actions(Puppet.init_cls(self))
|
|
|
|
Portal.init_cls(self)
|
|
|
|
if self.config["bridge.resend_bridge_info"]:
|
|
|
|
self.add_startup_actions(self.resend_bridge_info())
|
|
|
|
await super().start()
|
2022-04-10 23:40:11 -04:00
|
|
|
""" TODO Implement web login
|
2022-02-25 02:22:50 -05:00
|
|
|
if self.public_website:
|
|
|
|
self.public_website.ready_wait.set_result(None)
|
2022-04-10 23:40:11 -04:00
|
|
|
"""
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
async def resend_bridge_info(self) -> None:
|
|
|
|
self.config["bridge.resend_bridge_info"] = False
|
|
|
|
self.config.save()
|
|
|
|
self.log.info("Re-sending bridge info state event to all portals")
|
|
|
|
async for portal in Portal.all():
|
|
|
|
await portal.update_bridge_info()
|
|
|
|
self.log.info("Finished re-sending bridge info state events")
|
|
|
|
|
|
|
|
async def get_portal(self, room_id: RoomID) -> Portal:
|
|
|
|
return await Portal.get_by_mxid(room_id)
|
|
|
|
|
|
|
|
async def get_puppet(self, user_id: UserID, create: bool = False) -> Puppet:
|
|
|
|
return await Puppet.get_by_mxid(user_id, create=create)
|
|
|
|
|
|
|
|
async def get_double_puppet(self, user_id: UserID) -> Puppet:
|
|
|
|
return await Puppet.get_by_custom_mxid(user_id)
|
|
|
|
|
|
|
|
async def get_user(self, user_id: UserID, create: bool = True) -> User:
|
|
|
|
return await User.get_by_mxid(user_id, create=create)
|
|
|
|
|
|
|
|
def is_bridge_ghost(self, user_id: UserID) -> bool:
|
|
|
|
return bool(Puppet.get_id_from_mxid(user_id))
|
|
|
|
|
|
|
|
async def count_logged_in_users(self) -> int:
|
|
|
|
return len([user for user in User.by_ktid.values() if user.ktid])
|
|
|
|
|
2022-07-14 01:41:24 -04:00
|
|
|
async def manhole_global_namespace(self, user_id: UserID) -> Dict[str, Any]:
|
2022-02-25 02:22:50 -05:00
|
|
|
return {
|
|
|
|
**await super().manhole_global_namespace(user_id),
|
|
|
|
"User": User,
|
|
|
|
"Portal": Portal,
|
|
|
|
"Puppet": Puppet,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KakaoTalkBridge().run()
|