matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/commands/conn.py
Andrew Ferrazzutti e952c05d35 Many fixes thanks to mypy
Also add some missing license headers
2022-04-28 03:26:08 -04:00

113 lines
3.9 KiB
Python

# 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 mautrix.bridge.commands import HelpSection, command_handler
from mautrix.types import SerializerError
from .typehint import CommandEvent
from ..kt.client.errors import CommandException
SECTION_CONNECTION = HelpSection("Connection management", 15, "")
@command_handler(
needs_auth=False,
management_only=True,
help_section=SECTION_CONNECTION,
help_text="Mark this room as your bridge notice room",
)
async def set_notice_room(evt: CommandEvent) -> None:
evt.sender.notice_room = evt.room_id
await evt.sender.save()
await evt.reply("This room has been marked as your bridge notice room")
@command_handler(
needs_auth=True,
management_only=True,
help_section=SECTION_CONNECTION,
help_text="Disconnect from KakaoTalk chats, but remain logged into profile-management commands",
)
async def disconnect(evt: CommandEvent) -> None:
if not evt.sender.is_connected:
await evt.reply("You are already disconnected from KakaoTalk chats")
return
await evt.mark_read()
await evt.sender.client.disconnect()
await evt.reply("Successfully disconnected from KakaoTalk chats. To reconnect, use the `sync` command.")
@command_handler(
needs_auth=True,
management_only=True,
help_section=SECTION_CONNECTION,
help_text="Check if you're logged into KakaoTalk and retrieve your account information",
)
async def whoami(evt: CommandEvent) -> None:
await evt.mark_read()
try:
own_info = await evt.sender.get_own_info()
except SerializerError:
evt.sender.log.exception("Failed to deserialize settings struct")
own_info = None
except CommandException as e:
await evt.reply(f"Error from KakaoTalk: {e}")
if own_info:
await evt.reply(
f"You're logged in as `{own_info.more.uuid}` (nickname: {own_info.more.nickName}, user ID: {evt.sender.ktid})."
)
else:
await evt.reply(
f"You're logged in, but the bridge is unable to retrieve your profile information (user ID: {evt.sender.ktid})."
)
@command_handler(
needs_auth=True,
management_only=True,
help_section=SECTION_CONNECTION,
help_text="Check if you're connected to KakaoTalk chats",
)
async def ping(evt: CommandEvent) -> None:
assert evt.sender.client
is_connected = evt.sender.is_connected and await evt.sender.client.is_connected()
await evt.reply(
f"You are {'connected to' if is_connected else '**disconnected** from'} KakaoTalk chats."
)
@command_handler(
needs_auth=True,
management_only=True,
help_section=SECTION_CONNECTION,
help_text="(Re)connect to KakaoTalk chats & sync any missed chat updates",
help_args="[number_of_channels_to_sync]",
)
async def sync(evt: CommandEvent) -> None:
try:
sync_count = int(evt.args[0])
except IndexError:
sync_count = None
except ValueError:
await evt.reply("**Usage:** `sync [number_of_channels_to_sync]`")
return
await evt.mark_read()
if await evt.sender.connect_and_sync(sync_count):
await evt.reply("Sync complete")
else:
await evt.reply("Sync failed")