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-04-09 03:45:29 -04:00
|
|
|
from typing import TYPE_CHECKING
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
from mautrix.bridge import BaseMatrixHandler
|
|
|
|
from mautrix.types import (
|
|
|
|
Event,
|
|
|
|
EventID,
|
|
|
|
EventType,
|
2022-04-10 04:26:09 -04:00
|
|
|
ReceiptEvent,
|
2022-02-25 02:22:50 -05:00
|
|
|
RedactionEvent,
|
|
|
|
RoomID,
|
|
|
|
SingleReceiptEventContent,
|
2022-04-13 01:12:56 -04:00
|
|
|
PowerLevelStateEventContent,
|
|
|
|
StateEvent,
|
2022-02-25 02:22:50 -05:00
|
|
|
UserID,
|
|
|
|
)
|
|
|
|
|
2022-04-14 00:55:58 -04:00
|
|
|
from . import portal as po, user as u
|
2022-04-10 04:26:09 -04:00
|
|
|
from .db import Message as DBMessage
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .__main__ import KakaoTalkBridge
|
|
|
|
|
|
|
|
|
|
|
|
class MatrixHandler(BaseMatrixHandler):
|
|
|
|
def __init__(self, bridge: KakaoTalkBridge) -> None:
|
|
|
|
prefix, suffix = bridge.config["bridge.username_template"].format(userid=":").split(":")
|
|
|
|
homeserver = bridge.config["homeserver.domain"]
|
|
|
|
self.user_id_prefix = f"@{prefix}"
|
|
|
|
self.user_id_suffix = f"{suffix}:{homeserver}"
|
|
|
|
super().__init__(bridge=bridge)
|
|
|
|
|
2022-04-01 05:07:05 -04:00
|
|
|
@staticmethod
|
|
|
|
async def allow_bridging_message(user: u.User, portal: po.Portal) -> bool:
|
2022-04-10 02:21:00 -04:00
|
|
|
if user.is_connected:
|
|
|
|
return True
|
|
|
|
if user.relay_whitelisted and portal.has_relay:
|
|
|
|
relay_user = await portal.get_relay_user()
|
|
|
|
return relay_user and relay_user.is_connected
|
|
|
|
return False
|
2022-04-01 05:07:05 -04:00
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
async def send_welcome_message(self, room_id: RoomID, inviter: u.User) -> None:
|
|
|
|
await super().send_welcome_message(room_id, inviter)
|
|
|
|
if not inviter.notice_room:
|
|
|
|
inviter.notice_room = room_id
|
|
|
|
await inviter.save()
|
|
|
|
await self.az.intent.send_notice(
|
|
|
|
room_id, "This room has been marked as your KakaoTalk bridge notice room."
|
|
|
|
)
|
|
|
|
|
|
|
|
async def handle_invite(
|
|
|
|
self, room_id: RoomID, user_id: UserID, invited_by: u.User, event_id: EventID
|
|
|
|
) -> None:
|
|
|
|
# TODO handle puppet and user invites for group chats
|
|
|
|
# The rest can probably be ignored
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def handle_join(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None:
|
|
|
|
user = await u.User.get_by_mxid(user_id)
|
|
|
|
|
|
|
|
portal = await po.Portal.get_by_mxid(room_id)
|
|
|
|
if not portal:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not user.relay_whitelisted:
|
|
|
|
await portal.main_intent.kick_user(
|
|
|
|
room_id, user.mxid, "You are not whitelisted on this KakaoTalk bridge."
|
|
|
|
)
|
|
|
|
return
|
|
|
|
elif (
|
|
|
|
not await user.is_logged_in()
|
|
|
|
and not portal.has_relay
|
|
|
|
and not self.config["bridge.allow_invites"]
|
|
|
|
):
|
|
|
|
await portal.main_intent.kick_user(
|
|
|
|
room_id, user.mxid, "You are not logged in to this KakaoTalk bridge."
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.log.debug(f"{user.mxid} joined {room_id}")
|
|
|
|
# await portal.join_matrix(user, event_id)
|
|
|
|
|
|
|
|
async def handle_leave(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None:
|
|
|
|
portal = await po.Portal.get_by_mxid(room_id)
|
|
|
|
if not portal:
|
|
|
|
return
|
|
|
|
|
|
|
|
user = await u.User.get_by_mxid(user_id, create=False)
|
|
|
|
if not user:
|
|
|
|
return
|
|
|
|
|
|
|
|
await portal.handle_matrix_leave(user)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def handle_redaction(
|
|
|
|
room_id: RoomID, user_id: UserID, event_id: EventID, redaction_event_id: EventID
|
|
|
|
) -> None:
|
|
|
|
user = await u.User.get_by_mxid(user_id)
|
|
|
|
if not user:
|
|
|
|
return
|
|
|
|
|
|
|
|
portal = await po.Portal.get_by_mxid(room_id)
|
|
|
|
if not portal:
|
|
|
|
return
|
|
|
|
|
|
|
|
await portal.handle_matrix_redaction(user, event_id, redaction_event_id)
|
|
|
|
|
2022-04-10 23:05:43 -04:00
|
|
|
""" TODO
|
2022-02-25 02:22:50 -05:00
|
|
|
@classmethod
|
|
|
|
async def handle_reaction(
|
|
|
|
cls,
|
|
|
|
room_id: RoomID,
|
|
|
|
user_id: UserID,
|
|
|
|
event_id: EventID,
|
|
|
|
content: ReactionEventContent,
|
|
|
|
) -> None:
|
|
|
|
if content.relates_to.rel_type != RelationType.ANNOTATION:
|
|
|
|
cls.log.debug(
|
|
|
|
f"Ignoring m.reaction event in {room_id} from {user_id} with unexpected "
|
|
|
|
f"relation type {content.relates_to.rel_type}"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
user = await u.User.get_by_mxid(user_id)
|
2022-04-13 01:12:56 -04:00
|
|
|
if not user or not user.is_connected:
|
2022-02-25 02:22:50 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
portal = await po.Portal.get_by_mxid(room_id)
|
|
|
|
if not portal:
|
|
|
|
return
|
|
|
|
|
|
|
|
await portal.handle_matrix_reaction(
|
|
|
|
user, event_id, content.relates_to.event_id, content.relates_to.key
|
|
|
|
)
|
2022-04-10 23:05:43 -04:00
|
|
|
"""
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
async def handle_read_receipt(
|
|
|
|
self,
|
|
|
|
user: u.User,
|
|
|
|
portal: po.Portal,
|
|
|
|
event_id: EventID,
|
|
|
|
data: SingleReceiptEventContent,
|
|
|
|
) -> None:
|
2022-04-10 04:26:09 -04:00
|
|
|
if not user.is_connected:
|
2022-02-25 02:22:50 -05:00
|
|
|
return
|
|
|
|
message = await DBMessage.get_by_mxid(event_id, portal.mxid)
|
2022-04-12 01:30:29 -04:00
|
|
|
if message:
|
|
|
|
await user.client.mark_read(portal.channel_props, message.ktid)
|
2022-04-10 04:26:09 -04:00
|
|
|
|
2022-04-13 01:12:56 -04:00
|
|
|
@classmethod
|
|
|
|
async def handle_power_level(
|
|
|
|
cls,
|
|
|
|
room_id: RoomID,
|
|
|
|
user_id: UserID,
|
|
|
|
prev_content: PowerLevelStateEventContent,
|
|
|
|
content: PowerLevelStateEventContent,
|
2022-04-13 15:29:44 -04:00
|
|
|
event_id: EventID,
|
2022-04-13 01:12:56 -04:00
|
|
|
) -> None:
|
|
|
|
user = await u.User.get_by_mxid(user_id)
|
2022-04-13 15:29:44 -04:00
|
|
|
if not user:
|
2022-04-13 01:12:56 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
portal = await po.Portal.get_by_mxid(room_id)
|
|
|
|
if not portal:
|
|
|
|
return
|
|
|
|
|
2022-04-13 15:29:44 -04:00
|
|
|
await portal.handle_matrix_power_level(user, prev_content, content, event_id)
|
2022-04-13 01:12:56 -04:00
|
|
|
|
2022-04-10 04:26:09 -04:00
|
|
|
async def handle_ephemeral_event(
|
|
|
|
self, evt: ReceiptEvent | Event
|
|
|
|
) -> None:
|
|
|
|
if evt.type == EventType.RECEIPT:
|
|
|
|
await self.handle_receipt(evt)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
async def handle_event(self, evt: Event) -> None:
|
|
|
|
if evt.type == EventType.ROOM_REDACTION:
|
|
|
|
evt: RedactionEvent
|
|
|
|
await self.handle_redaction(evt.room_id, evt.sender, evt.redacts, evt.event_id)
|
2022-04-10 23:05:43 -04:00
|
|
|
""" TODO
|
2022-02-25 02:22:50 -05:00
|
|
|
elif evt.type == EventType.REACTION:
|
|
|
|
evt: ReactionEvent
|
|
|
|
await self.handle_reaction(evt.room_id, evt.sender, evt.event_id, evt.content)
|
2022-04-10 23:05:43 -04:00
|
|
|
"""
|
2022-04-13 01:12:56 -04:00
|
|
|
|
|
|
|
async def handle_state_event(self, evt: StateEvent) -> None:
|
|
|
|
if evt.type == EventType.ROOM_POWER_LEVELS:
|
2022-04-13 15:29:44 -04:00
|
|
|
await self.handle_power_level(evt.room_id, evt.sender, evt.prev_content, evt.content, evt.event_id)
|